Skip to content

Impossible to use wasm:js-string functions in webassembly #136594

Open
@socketpair

Description

@socketpair

Here you can see an example of WASM code how to use concat JS builtin:

https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/JavaScript_builtins#wasm_module

(func $concat (import "wasm:js-string" "concat")
    (param externref externref) (result (ref extern)))

So, in C I defined it in the following way:

__externref_t js_concat(__externref_t, __externref_t) 
__attribute__((import_module("wasm:js-string"), import_name("concat")));

Next, I'm trying to run the code that uses definition above in the following way:

const { instance } = await WebAssembly.instantiateStreaming(
  fetch('trie_search.wasm'),
  {
      env: {  },
  },
  {
      builtins: ["js-string"],
      importedStringConstants: "Something",
  },
);
instance.exports.my_function();

Browser console says:

Uncaught (in promise) CompileError: WebAssembly.instantiateStreaming():
Imported builtin function "wasm:js-string" "concat" has incorrect signature

I guess, the problem in return value. in WAT language it should look like (ref extern). How to express it in C (Clang)?

https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md#wasmjs-string-concat
https://github.com/WebAssembly/function-references/blob/master/proposals/function-references/Overview.md#binary-format

(module
  (global $h (import "string_constants" "hello ") externref)
  (global $w (import "string_constants" "world!") externref)

  (func $concat (import "wasm:js-string" "concat")
    (param externref externref) (result (ref extern)))

  (func $log (import "m" "log") (param externref))
  (func (export "main")
    (call $log (call $concat (global.get $h) (global.get $w))))
)

Compiles perfectly well using binryen, but how to write an equivalent using C and clang ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions