Skip to content

Improve exception handling #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/witapi/bindgen/rb-abi-guest.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ void __wasm_export_rb_abi_guest_rstring_ptr_post_return(int32_t arg0) {
free((void*) (*((int32_t*) (arg0 + 0))));
}
}
__attribute__((export_name("rb-vm-bugreport: func() -> ()")))
void __wasm_export_rb_abi_guest_rb_vm_bugreport(void) {
rb_abi_guest_rb_vm_bugreport();
}
1 change: 1 addition & 0 deletions ext/witapi/bindgen/rb-abi-guest.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern "C"
rb_abi_guest_rb_abi_value_t rb_abi_guest_rb_errinfo(void);
void rb_abi_guest_rb_clear_errinfo(void);
void rb_abi_guest_rstring_ptr(rb_abi_guest_rb_abi_value_t value, rb_abi_guest_string_t *ret0);
void rb_abi_guest_rb_vm_bugreport(void);
#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions ext/witapi/bindgen/rb-abi-guest.wit
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ rb-errinfo: func() -> rb-abi-value
rb-clear-errinfo: func()

rstring-ptr: func(value: rb-abi-value) -> string

rb-vm-bugreport: func()
4 changes: 4 additions & 0 deletions ext/witapi/witapi-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,8 @@ uint32_t rb_abi_guest_rb_abi_value_data_ptr(rb_abi_guest_rb_abi_value_t self) {
return (uint32_t)DATA_PTR(obj);
}

void rb_vm_bugreport(const void *);

void rb_abi_guest_rb_vm_bugreport(void) { rb_vm_bugreport(NULL); }

void Init_witapi(void) {}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class RbAbiGuest {
rbErrinfo(): RbAbiValue;
rbClearErrinfo(): void;
rstringPtr(value: RbAbiValue): string;
rbVmBugreport(): void;
}

export class RbIseq {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ export class RbAbiGuest {
this._exports["cabi_post_rstring-ptr"](ret);
return result1;
}
rbVmBugreport() {
this._exports['rb-vm-bugreport: func() -> ()']();
}
}

export class RbIseq {
Expand Down
45 changes: 37 additions & 8 deletions packages/npm-packages/ruby-wasm-wasi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ class RbExceptionFormatter {
})();

const backtrace = error.call("backtrace");
if (backtrace.call("nil?").toString() === "true") {
return this.formatString(
error.call("class").toString(),
error.toString()
);
}
const firstLine = backtrace.call("at", zeroLiteral);
const restLines = backtrace
.call("drop", oneLiteral)
Expand All @@ -419,9 +425,13 @@ class RbExceptionFormatter {
formatString(
klass: string,
message: string,
backtrace: [string, string]
backtrace?: [string, string]
): string {
return `${backtrace[0]}: ${message} (${klass})\n${backtrace[1]}`;
if (backtrace) {
return `${backtrace[0]}: ${message} (${klass})\n${backtrace[1]}`;
} else {
return `${klass}: ${message}`;
}
}
}

Expand Down Expand Up @@ -461,6 +471,21 @@ const checkStatusTag = (
}
};

function wrapRbOperation<R>(vm: RubyVM, body: () => R): R {
try {
return body();
} catch (e) {
if (e instanceof WebAssembly.RuntimeError && e.message === "unreachable") {
vm.guest.rbVmBugreport();
const error = new RbError(`Something went wrong in Ruby VM: ${e}`);
error.stack = e.stack;
throw error;
} else {
throw e;
}
}
}

const callRbMethod = (
vm: RubyVM,
privateObject: RubyVMPrivate,
Expand All @@ -469,14 +494,18 @@ const callRbMethod = (
args: RbAbi.RbAbiValue[]
) => {
const mid = vm.guest.rbIntern(callee + "\0");
const [value, status] = vm.guest.rbFuncallvProtect(recv, mid, args);
checkStatusTag(status, vm, privateObject);
return value;
return wrapRbOperation(vm, () => {
const [value, status] = vm.guest.rbFuncallvProtect(recv, mid, args);
checkStatusTag(status, vm, privateObject);
return value;
});
};
const evalRbCode = (vm: RubyVM, privateObject: RubyVMPrivate, code: string) => {
const [value, status] = vm.guest.rbEvalStringProtect(code + "\0");
checkStatusTag(status, vm, privateObject);
return new RbValue(value, vm, privateObject);
return wrapRbOperation(vm, () => {
const [value, status] = vm.guest.rbEvalStringProtect(code + "\0");
checkStatusTag(status, vm, privateObject);
return new RbValue(value, vm, privateObject);
});
};

/**
Expand Down