ruby / ruby/ruby.wasm

`JS::Object#await` is unavailable inside Proc converted into JS closure

未关闭
#459 0 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Ruby
星标
875
派生
68
平均合并
9 天 5 小时
30 天内合并 PR
1

描述

Reproducer

<html>
  <script src="https://cdn.jsdelivr.net/npm/@ruby/3.3-wasm-wasi@2.6.0/dist/browser.script.iife.js"></script>
  <script type="text/ruby" data-eval="async">
  require "js"

  JS.global.setTimeout(-> {
    JS.global[:Promise].resolve.await
  }, 0)
  </script>
</html>
browser.script.iife.js:2731 Uncaught Error: /bundle/gems/js-2.6.0/lib/js.rb:86:in `await': JS::Object#await can be called only from RubyVM#evalAsync or RbValue#callAsync JS API
If you are using browser.script.iife.js, please ensure that you specify `data-eval="async"` in your script tag
e.g. <script type="text/ruby" data-eval="async">puts :hello</script>
Or <script type="text/ruby" data-eval="async" src="path/to/script.rb"></script> (RuntimeError)
/bundle/gems/js-2.6.0/lib/js.rb:240:in `await'
eval_async:9:in `block in <main>'
    at checkStatusTag (browser.script.iife.js:2731:21)
    at browser.script.iife.js:2766:11
    at wrapRbOperation (browser.script.iife.js:2738:18)
    at callRbMethod (browser.script.iife.js:2764:14)
    at RbValue.call (browser.script.iife.js:2547:30)
    at browser.script.iife.js:2322:38

Workaround

Wrap the block with Fiber.new do ... end.transfer.

JS.global.setTimeout(-> {
  Fiber.new do
    JS.global[:Promise].resolve.await
  end.transfer
}, 0)

Solutions

We have several options to solve the issue

  1. Always enter the body of Proc in Ruby with callAsync instead of call
  2. Add conversion method to create an async version of JS function from proc.
  • e.g.
JS.global.setTimeout(JS.async do
  JS.global[:Promise].resolve.await
end, 0)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从此 issue 中的 JS::Object#await 复现用例和变通方案开始,然后检查 packages/npm-packages/ruby-wasm-wasi/src/vm.ts 中链接的 call/callAsync 实现附近的代码。比较来自 setTimeout 的 Proc 回调如何进入 Ruby,并确定哪个提议的方向能够保留异步行为。当回调可以 await Promise 而不需要手动转移 Fiber 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, ruby, wasm
领域
devtools, web-dev
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。