[js-api] The type confusion in builtin section
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tái cấu trúc
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- documentation
Hướng nghiên cứu
Read the JS API Builtins section and compare its create a builtin function steps with run a host function and the Core Spec's ToJSValue and ToWebAssemblyValue conversions. The issue proposes defining argument and result conversion, trap handling, store handling, and unsigned i32 interpretation across the builtins; work out the array handling details it identifies. Done means the builtin algorithms use one consistent convention and resolve the cited type mismatches.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
In the Core Spec and the rest of the JS API, a numeric WebAssembly value such as (i32.cosnt n) is a term,
and its actual value is compared only after it is taken out of one, with its signedness spelled out.
In the Core Spec, i32.lt_u and i32.lt_s take the payloads c1 and c2 out of two i32.const values and compare them with ilt: unsigned is c1 < c2, and signed is signed_32(c1) < signed_32(c2).
The JS API does the same when it tries to read a value:
ToJSValue matches i32.const u32 and uses signed_32(u32).
The Builtins section however, has no such convention.
create a builtin function only says the host function "executes steps when called",
so nothing says what the steps receive or return,
and the steps mix WebAssembly values, JavaScript values and plain numbers:
i32parameters are used both as numbers and as WebAssembly values.
fromCodePointchecksv > 0x10ffffand then callsToJSValue(v);
charCodeAt/codePointAt(index) andsubstring(start,end) do the same.- Conversions are written in some places only.
FromCharCodeandCharCodeAtassertis of type i32and callToJSValue,
butexternrefparameters are used as if they are already JavaScript values (If v is not a String),
and results are returned unconverted (Return 0.,Return -1., a JavaScript String). - Values of the wrong kind are passed on.
intoCharCodeArraypasses its loop counter, a number, toCharCodeAt, which asserts ani32.
fromCharCodeArraypasses a packedi16array element toFromCharCode, which asserts the same.
AndintoCharCodeArraystores ani32.constvalue into thati16array.
Why it matters
These implicit conversions brings ambiguity. For instance, substring(string, start, end) can have different interpretations:
1. Let string be ? UnwrapString(string).
1. Let length be the length of string.
1. If start > end or start > length,
1. Return the empty string.
1. Return ! Call(String.prototype.substring, string, « ToJSValue(start), ToJSValue(end) »).
For substring("hello", 1, -1):
- If
endstays a WebAssembly value and only>implicitly reads it as a number:- read as signed:
1 > -1, so the result is"". - read as unsigned: the check passes, then
ToJSValueturnsendback into-1,
andString.prototype.substring("hello", 1, -1)gives"h".
- read as signed:
- If
endis already implicitly a number or JSValue when the steps start (which makes the last step'sToJSValueredundant):- converted as signed (
-1):"". (This is what JavaScriptCore returns.) - converted as unsigned (
4294967295):"ello". (This is what V8 and SpiderMonkey return, and likely be the intention.)
- converted as signed (
Suggestion
The proposal's Overview does have a single convention, and V8 and SpiderMonkey follow it.
Builtins "have the same semantics as following create a host function",
so "Calling a function builtin from Wasm will have the Wasm parameters converted to JS values,
and JS results converted back to Wasm values", and each builtin then reinterprets its i32 parameters as unsigned:
// NOTE: `index` is interpreted as a signed 32-bit integer when converted to
// a JS value using standard conversions. Reinterpret as unsigned here.
index >>>= 0;
I would suggest writing the section in that convention:
-
create a builtin function's host function explicitly converts its arguments withToJSValueand the result withToWebAssemblyValue, asrun a host functiondoes, and maps a throw to a trap, perhaps something like:1. Let func parameters → results be the expansion of funcType. 1. Let hostfunc be a host function which performs the following steps when called with store store and arguments arguments: 1. Set the surrounding agent's associated store to store. 1. Let jsArguments be « ». 1. For each arg of arguments, append ToJSValue(arg) to jsArguments. 1. Let result be the result of running steps with jsArguments. 1. Let store be the surrounding agent's associated store. 1. If result is an abrupt completion, return (store, trap). 1. Return (store, « ! ToWebAssemblyValue(result.[[Value]], results[0]) »). 1. ... -
Each builtin starts by reinterpreting its
i32parameters with
ToUint32, and compares them withℝ(...). For example,substring:1. Let string be ? UnwrapString(string). 1. Let start be ! ToUint32(start). 1. Let end be ! ToUint32(end). 1. Let length be the length of string. 1. If ℝ(start) > ℝ(end) or ℝ(start) > length, 1. Return the empty string. 1. Return ! Call(String.prototype.substring, string, « start, end »).(Alternatively, the host function could pass
i32arguments as unsigned Numbers (𝔽(u32)) in the first place, instead ofToJSValue. That saves repeatingToUint32in every builtin) -
Results the steps return as plain numbers
(Return 0.,Return -1.,Return stringLength.,Return the length of string.)
become Numbers (+0𝔽,-1𝔽,𝔽(stringLength)),
so thatToWebAssemblyValuecan convert them like any other JavaScript result. -
The host function explicitly takes and returns the store, as #2326 does for
create a host function.
Some details would still need working out.
For example, with ToJSValue an array parameter arrives as an Exported GC Object,
so fromCharCodeArray and intoCharCodeArray would have to reach the array through its address.
I would work these out in the PR.
Does that direction sound right? If so, I can open a PR for it.
- Ngôn ngữ chính
- WebAssembly
- Star
- 3.5k
- Fork
- 545
- Merge trung bình
- 1 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 25
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của WebAssembly/spec
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
WebAssembly/spec#2321 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
WebAssembly/spec#2333 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 66/100
WebAssembly/spec#2302 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 53/100
WebAssembly/spec#2301 · 9 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
il2al: host function rules become two independent conditionals and ignore the new store `s'`Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
WebAssembly/spec#2279 · 6 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của WebAssembly/spec
Issue tương tự
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
type:documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
AbsaOSS/living-doc#140 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
feedback simulation workshop
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
githubnext/gh-aw-workshop#5896 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
flet-dev/flet#7102 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
kubescape/node-agent#1112 ·
Maintainer thường phản hồi trong vòng 1 ngày