Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Oct 20, 2023
1 parent 793ab8e commit eb61d35
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
10 changes: 5 additions & 5 deletions crates/compiler/test_gen/src/helpers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ impl<'a> ImportDispatcher for TestDispatcher<'a> {
self.wasi.dispatch(function_name, arguments, memory)
} else if module_name == "env" && function_name == "send_panic_msg_to_rust" {
let msg_ptr = arguments[0].expect_i32().unwrap();
let tag = arguments[1].expect_i32().unwrap();
let panic_tag = arguments[1].expect_i32().unwrap();
let roc_msg = RocStr::decode(memory, msg_ptr as _);
let msg = match tag {
0 => format!(r#"Roc failed with message: "{}""#, roc_msg),
1 => format!(r#"User crash with message: "{}""#, roc_msg),
tag => format!(r#"Got an invald panic tag: "{}""#, tag),
let msg = match panic_tag {
0 => format!(r#"Roc failed with message: "{roc_msg}""#),
1 => format!(r#"User crash with message: "{roc_msg}""#),
tag => format!(r#"Got an invald panic tag: "{panic_tag}""#),
};
panic!("{}", msg)
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/compiler/test_gen/src/helpers/wasm_test_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ void roc_dealloc(void *ptr, unsigned int alignment)

//--------------------------

extern void send_panic_msg_to_rust(void* msg, uint32_t tag_id);
extern void send_panic_msg_to_rust(void* msg, uint32_t panic_tag);

void roc_panic(void* msg, unsigned int tag_id)
void roc_panic(void* msg, unsigned int panic_tag)
{
send_panic_msg_to_rust(msg, tag_id);
send_panic_msg_to_rust(msg, panic_tag);
exit(101);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/repl_wasm/src/repl_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ void roc_dealloc(void *ptr, unsigned int alignment)

//--------------------------

extern void send_panic_msg_to_js(void *ptr, unsigned int alignment);
extern void send_panic_msg_to_js(void *ptr, unsigned int panic_tag);

void roc_panic(void *ptr, unsigned int alignment)
void roc_panic(void *ptr, unsigned int panic_tag)
{
send_panic_msg_to_js(ptr, alignment);
send_panic_msg_to_js(ptr, panic_tag);
#if ENABLE_PRINTF
char *msg = (char *)ptr;
fprintf(stderr,
Expand Down
39 changes: 25 additions & 14 deletions www/public/repl/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,33 @@ async function processInputQueue() {

var ROC_PANIC_INFO = null;

function send_panic_msg_to_js(rocstr_ptr, tag_id) {
function send_panic_msg_to_js(rocstr_ptr, panic_tag) {
const { memory } = repl.app.exports;
const memoryBuffer = new Uint8Array(memory.buffer);

const rocStrBytes = new Uint8Array(memory.buffer, rocstr_ptr, 12);
const rocStrBytes = new Int8Array(memory.buffer, rocstr_ptr, 12);
const finalByte = rocStrBytes[11]

const strPtr = rocStrBytes[0] | (rocStrBytes[1] << 8) | (rocStrBytes[2] << 16) | (rocStrBytes[3] << 24);
const strLen = rocStrBytes[4] | (rocStrBytes[5] << 8) | (rocStrBytes[6] << 16) | (rocStrBytes[7] << 24);
let stringBytes = "";
if (finalByte < 0) {
// small string
const length = finalByte ^ 0b1000_0000;
stringBytes = new Uint8Array(memory.buffer, rocstr_ptr, length);
} else {
// big string
const rocStrWords = new Uint32Array(memory.buffer, rocstr_ptr, 3);
const [ptr, len, _cap] = rocStrWords;

const SEAMLESS_SLICE_BIT = 1 << 31;
const length = len & (~SEAMLESS_SLICE_BIT);

stringBytes = new Uint8Array(memory.buffer, ptr, length);
}

const stringBytes = new Uint8Array(memory.buffer, strPtr, strLen);
const decoder = new TextDecoder('utf-8');
const decodedString = decoder.decode(stringBytes);
const decodedString = repl.textDecoder.decode(stringBytes);

ROC_PANIC_INFO = {
msg: decodedString,
tag_id: tag_id,
panic_tag: panic_tag,
};
}

Expand Down Expand Up @@ -215,10 +226,10 @@ function js_run_app() {
throw e;
} else {
// when roc_panic set an error message, display it
const { msg, tag_id } = ROC_PANIC_INFO;
const { msg, panic_tag } = ROC_PANIC_INFO;
ROC_PANIC_INFO = null;

console.error(format_roc_panig_message(msg, tag_id));
console.error(format_roc_panic_message(msg, panic_tag));
}
}

Expand All @@ -227,16 +238,16 @@ function js_run_app() {
return memory.buffer.byteLength;
}

function format_roc_panig_message(msg, tag) {
switch (tag) {
function format_roc_panic_message(msg, panic_tag) {
switch (panic_tag) {
case 0: {
return `Roc failed with message: "${msg}"`;
}
case 1: {
return `User crash with message: "${msg}"`;
}
default: {
return `Got an invalid panic tag: "${tag}"`;
return `Got an invalid panic tag: "${panic_tag}"`;
}
}
}
Expand Down

0 comments on commit eb61d35

Please sign in to comment.