Support JS String Builtins
I hope jco transpile can implement JS String Builtins Proposal.
This helps to significantly reduce the size of browsers by reducing string-related functions and constant conversions.
The specific logic is to determine whether (import "wasm:js-string" ...) exists in core module.
If it exists, then add builtins: ['js-string'] to instantiateCore function.
Another logic is to add the --imported-string-constants option.
When --imported-string-constants STATIC_STRING is entered, the importedStringConstants: "STATIC_STRING" parameter will be added to the instantiateCore function
We could easily add this to Jco, as you have described. Will keep this open to track. Are there toolchains that would be able to utilize this feature already?
moonbit supports this: https://www.moonbitlang.com/blog/js-string-builtins
This is the artificial:
;; jco transpile palindrome.wat --imported-string-constants "_"
(core module
(type (;0;) (func (param externref) (result i32)))
(type (;1;) (func (param externref i32) (result i32)))
(type (;2;) (func (param (ref extern)) (result (ref extern))))
(type (;3;) (func (param i32)))
(type (;4;) (func))
(import "wasm:js-string" "length" (func (;0;) (type 0)))
(import "wasm:js-string" "charCodeAt" (func (;1;) (type 1)))
(import "_" "true" (global (;0;) (ref extern)))
(import "_" "false" (global (;1;) (ref extern)))
(memory (;0;) 1)
(export "palindrome" (func 2))
(start 3)
(func (;2;) (type 2) (param (ref extern)) (result (ref extern))
(local i32 i32 i32)
local.get 0
call 0
local.tee 1
i32.const 2
i32.div_s
local.set 2
i32.const 0
loop (type 3) (param i32) ;; label = @1
local.tee 3
local.get 2
i32.lt_s
if ;; label = @2
local.get 0
local.get 3
call 1
local.get 0
local.get 1
local.get 3
i32.sub
i32.const 1
i32.sub
call 1
i32.ne
if ;; label = @3
global.get 1
return
end
local.get 3
i32.const 1
i32.add
br 1 (;@1;)
end
end
global.get 0
)
(func (;3;) (type 4))
)
Note you could also experiment with this today using Jco's custom instantiation mode. I think one of the hardest questions will just be what string builtin import name to use.