ruby.wasm icon indicating copy to clipboard operation
ruby.wasm copied to clipboard

Passing a block to JS::Object#new method

Open ledsun opened this issue 1 year ago • 0 comments

Cannot pass block to JS::Object#new method. Therefore, to initialize a Promise object by passing a callback function to it, we must use JS.eval as follows:

JS.eval("return new Promise((resolve) => setTimeout(resolve, 1000))")

If we can pass a block to the JS::Object#new method, we can write as follows:

JS.global[:Promise].new do |resolve|
  JS.global.setTimeout(resolve, 1000)
end

This is a first prototype. Currently, the unit test fails with the following error:

RbFatalError: Ruby Fatal Error: Ruby APIs that may rewind the VM stack are prohibited under nested VM operation (rb_wasm_handle_fiber_unwind) 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')")

ledsun avatar Feb 18 '24 14:02 ledsun