Repository navigation
fix(wasm): host getters read inside an async function are values, not calls (#14720) - #14724
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
An async function body on --target web is emitted as JavaScript. The HIR lowers a bare read of a native getter (`res.status`, `res.ok`, `res.statusText`, `res.headers`) to a NativeMethodCall with no arguments, the same shape as `res.text()`, and the JS emitter called every such member: `toJsValue(r).status()` threw "status is not a function". A zero-argument native member now goes through `__jsNativeMember`, which calls a method and returns any other value as the read. Fixes PerryTS#14720.
4948b70 to
902c1aa
Compare
… throws __jsNativeMember reads a non-function member only when the object has it; an absent member throws TypeError as the call would, instead of yielding undefined.
Summary
Fixes #14720. Refs #14595.
On
--target web, anasyncfunction body is emitted as JavaScript. Inside that body, readingr.status,r.ok,r.statusTextorr.headerson a fetchResponsethrewTypeError: toJsValue(...).status is not a function.Cause: the HIR lowers a bare read of a native getter (
is_fetch_response_getter_name, and the other rows ofnative_property_read_dispatches) to anExpr::NativeMethodCallwith no arguments. That is the same shape as a zero-argument method call such asr.text(). The JS emitter's genericNativeMethodCallarm emitted every such node asobj.member(), so the getter's value was called.Changes
js_fallback.rs: a zero-argument native member on an object now emits__jsNativeMember(obj, name). Calls with arguments are unchanged.wasm_runtime.js: add__jsNativeMember. It reads the member once. If the member is a function it calls it with the object as receiver; any other value is returned as the result of the read.emit/tests.rs: addasync_body_reads_host_getters_and_calls_host_methods. It readsstatus,ok,statusTextandheaders.get(...)from adata:URL response, then callstext().Known limit (same as the HIR's shape): for the modules whose bare reads all dispatch (
net,dns,Headers, …), reading a method as a value without calling it (const g = h.get) still invokes the method. Telling that case apart would need the HIR to mark reads separately from calls.Overlaps: none with #14696, #14664 or #14718.
git merge-treeis clean against each of them and against #14723.__jsNativeMembersits beside__jsMethodGet, which #14664 also edits, in a separate hunk.Test plan
All commands were run on this branch and on upstream/main dc9dd6b. The branch was then rebased onto 64257c3, which changes nothing under
perry-codegen-wasm,perry-hir,perry-transformorperry-parser.async function f(){ const r = await fetch("data:text/plain,hi"); console.log(r.status, r.ok, r.statusText, r.headers.get("content-type")) } f().TypeError: toJsValue(...).status is not a function.200 true OK text/plain, the same asnode --experimental-strip-types.cargo test --release -p perry-codegen-wasm async_body_reads_host:threw TypeError: toJsValue(...).status is not a functionand passes here.js_fallback.rsandwasm_runtime.jsand kept the test. The test failed the same way. Restoring the fixed files gave identical shasums.cargo test --release -p perry-codegen-wasm: 77 passed, 0 failed (main: 76 passed).rustfmt --check --edition 2021on the changed.rsfiles: clean.cargo clippy --release -p perry-codegen-wasm: 227 warnings on this branch and on main.tests/integration/wasm/run_wasm_tests.sh(pointed at each binary): 30 pass / 3 fail on both main and this branch, the same three (04_functions,10_higher_order,19_ffi_imports).BASE_SHA=<merge-base> SKIP_COMPILE_GATES=1 bash scripts/run_lint_gates.sh: the FAIL set (20 gates, environment-dependent) is identical to main's.Checklist
feat:/fix:/docs:/chore:prefix convention used in the log🤖 Generated with Claude Code
https://claude.ai/code/session_01CUQanVsnitcrmYLAWWPc3p
Summary by CodeRabbit
fetchresponse inside an async function could fail. Response details, including status and headers, can now be read correctly, and response methods such as reading the body continue to work as expected. Missing host methods now produce aTypeError.