From 4b53900c80207db105e2eafb1a75d857418f387a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 16 Mar 2024 09:47:10 +0000 Subject: [PATCH] Include Ruby exception message in VM interleaving error This would help debugging the nested VM operation error. --- packages/npm-packages/ruby-wasm-wasi/src/vm.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/npm-packages/ruby-wasm-wasi/src/vm.ts b/packages/npm-packages/ruby-wasm-wasi/src/vm.ts index 6631164acd..a72b8cfd77 100644 --- a/packages/npm-packages/ruby-wasm-wasi/src/vm.ts +++ b/packages/npm-packages/ruby-wasm-wasi/src/vm.ts @@ -137,8 +137,7 @@ export class RubyVM { const str = new TextDecoder().decode( new Uint8Array(memory.buffer, messagePtr, messageLen), ); - throw new RbFatalError( - "Ruby APIs that may rewind the VM stack are prohibited under nested VM operation " + + let message = "Ruby APIs that may rewind the VM stack are prohibited under nested VM operation " + `(${str})\n` + "Nested VM operation means that the call stack has sandwitched JS frames like JS -> Ruby -> JS -> Ruby " + "caused by something like `window.rubyVM.eval(\"JS.global[:rubyVM].eval('Fiber.yield')\")`\n" + @@ -148,8 +147,13 @@ export class RubyVM { " Note that `evalAsync` JS API switches fibers internally\n" + " 2. Raising uncaught exceptions\n" + " Please catch all exceptions inside the nested operation\n" + - " 3. Calling Continuation APIs\n", - ); + " 3. Calling Continuation APIs\n"; + + const error = new RbValue(this.guest.rbErrinfo(), this, this.privateObject()); + if (error.call("nil?").toString() === "false") { + message += "\n" + this.exceptionFormatter.format(error, this, this.privateObject()); + } + throw new RbFatalError(message); }, }; // NOTE: The GC may collect objects that are still referenced by Wasm