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)
} 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 {

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);
}

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

View file

@ -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 stringBytes = new Uint8Array(memory.buffer, strPtr, strLen);
const decoder = new TextDecoder('utf-8');
const decodedString = decoder.decode(stringBytes);
const SEAMLESS_SLICE_BIT = 1 << 31;
const length = len & (~SEAMLESS_SLICE_BIT);
stringBytes = new Uint8Array(memory.buffer, ptr, length);
}
const decodedString = repl.textDecoder.decode(stringBytes);
ROC_PANIC_INFO = {
msg: decodedString,
tag_id: tag_id,
panic_tag: panic_tag,
};
}
@ -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));
}
}
@ -227,8 +238,8 @@ 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}"`;
}
@ -236,7 +247,7 @@ function format_roc_panig_message(msg, tag) {
return `User crash with message: "${msg}"`;
}
default: {
return `Got an invalid panic tag: "${tag}"`;
return `Got an invalid panic tag: "${panic_tag}"`;
}
}
}