changes after review

This commit is contained in:
Folkert 2023-10-20 16:14:09 +02:00
parent 793ab8ec16
commit eb61d352f5
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
4 changed files with 36 additions and 25 deletions

View file

@ -202,12 +202,12 @@ impl<'a> ImportDispatcher for TestDispatcher<'a> {
self.wasi.dispatch(function_name, arguments, memory) self.wasi.dispatch(function_name, arguments, memory)
} else if module_name == "env" && function_name == "send_panic_msg_to_rust" { } else if module_name == "env" && function_name == "send_panic_msg_to_rust" {
let msg_ptr = arguments[0].expect_i32().unwrap(); 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 roc_msg = RocStr::decode(memory, msg_ptr as _);
let msg = match tag { let msg = match panic_tag {
0 => format!(r#"Roc failed with message: "{}""#, roc_msg), 0 => format!(r#"Roc failed with message: "{roc_msg}""#),
1 => format!(r#"User crash with message: "{}""#, roc_msg), 1 => format!(r#"User crash with message: "{roc_msg}""#),
tag => format!(r#"Got an invald panic tag: "{}""#, tag), tag => format!(r#"Got an invald panic tag: "{panic_tag}""#),
}; };
panic!("{}", msg) panic!("{}", msg)
} else { } else {

View file

@ -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); exit(101);
} }

View file

@ -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 #if ENABLE_PRINTF
char *msg = (char *)ptr; char *msg = (char *)ptr;
fprintf(stderr, fprintf(stderr,

View file

@ -167,22 +167,33 @@ async function processInputQueue() {
var ROC_PANIC_INFO = null; 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 { 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); let stringBytes = "";
const strLen = rocStrBytes[4] | (rocStrBytes[5] << 8) | (rocStrBytes[6] << 16) | (rocStrBytes[7] << 24); 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 stringBytes = new Uint8Array(memory.buffer, strPtr, strLen); const SEAMLESS_SLICE_BIT = 1 << 31;
const decoder = new TextDecoder('utf-8'); const length = len & (~SEAMLESS_SLICE_BIT);
const decodedString = decoder.decode(stringBytes);
stringBytes = new Uint8Array(memory.buffer, ptr, length);
}
const decodedString = repl.textDecoder.decode(stringBytes);
ROC_PANIC_INFO = { ROC_PANIC_INFO = {
msg: decodedString, msg: decodedString,
tag_id: tag_id, panic_tag: panic_tag,
}; };
} }
@ -215,10 +226,10 @@ function js_run_app() {
throw e; throw e;
} else { } else {
// when roc_panic set an error message, display it // 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; ROC_PANIC_INFO = null;
console.error(format_roc_panig_message(msg, tag_id)); console.error(format_roc_panic_message(msg, panic_tag));
} }
} }
@ -227,8 +238,8 @@ function js_run_app() {
return memory.buffer.byteLength; return memory.buffer.byteLength;
} }
function format_roc_panig_message(msg, tag) { function format_roc_panic_message(msg, panic_tag) {
switch (tag) { switch (panic_tag) {
case 0: { case 0: {
return `Roc failed with message: "${msg}"`; return `Roc failed with message: "${msg}"`;
} }
@ -236,7 +247,7 @@ function format_roc_panig_message(msg, tag) {
return `User crash with message: "${msg}"`; return `User crash with message: "${msg}"`;
} }
default: { default: {
return `Got an invalid panic tag: "${tag}"`; return `Got an invalid panic tag: "${panic_tag}"`;
} }
} }
} }