Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JS String Builtins #539

Open
oovm opened this issue Dec 22, 2024 · 3 comments
Open

Support JS String Builtins #539

oovm opened this issue Dec 22, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@oovm
Copy link

oovm commented Dec 22, 2024

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

@guybedford
Copy link
Collaborator

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?

@guybedford guybedford added the enhancement New feature or request label Dec 30, 2024
@oovm
Copy link
Author

oovm commented Dec 31, 2024

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))
)

@guybedford
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants