[Wasm] Add option to use js-string-builtins
for using JS string to implement string
#4994
Labels
enhancement
Feature request (that does not concern language semantics, see "language")
wasm
Applies to the WebAssembly backend only
In future (at least after seeing how it goes after the origin trial on Chrome), it might be a good idea to introduce an option like
useExperimentalJSStringBuiltins
, and if this option is turned ON, generate a WebAssembly module that implements JavaScript string operations using thejs-string-builtins
proposals.What is the
js-string-builtins
proposalThe js-string-builtin proposal aims to improve the efficiency of string operations by allowing us to directly use a subset of the JS String API through built-in WebAssembly functions. These builtin functions can be efficiently called from within WebAssembly modules without the need for JS glue code, the implementations are provided by the Wasm runtime. (This import mechanism is said to be more efficient than regular imports)
We can use these builtin functions by giving a
WebAssemblyCompileOptions
like this:The builtin functions include
(from|into)CharCodeArray
to converti16array
<=>externref (js string)
,cast
,test
, and so on(This proposal also provides a way to encode/decode UTF-8 strings through
wasm:text-encoder
andwasm:text-decoder
)Why
For example, instead of converting individual character codes using
String.fromCharCode
and concatenating them ingenCreateStringFromData
scala-js/linker/shared/src/main/scala/org/scalajs/linker/backend/wasmemitter/CoreWasmLib.scala
Lines 626 to 683 in 8d26747
the
"wasm:js-string" "fromCharCodeArray"
builtin function can be used to convert ani16array
directly to a JavaScript string more efficiently.https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md#wasmjs-string-fromcharcodearray
Also, the builtin functions should be called more efficiently than imported functions, and we can expect some more efficient string operations.
When to work on it
Not now, we should see how it goes after the Origin trial
--experimental-wasm-imported-strings
?12.2.279
(node.js v22 already on that version)The text was updated successfully, but these errors were encountered: