Skip to content

example of calling js from wasm ? #158

Closed

Description

@jeromeetienne

hello, im trying to learn how to call js function from assemblyscript.

is there a working example i could look at ?

thanks

Activity

  1. MaxGraey commented on Jul 4, 2018

    @MaxGraey
    Member

    Hi, Jerome.
    You can define functions inside typescript like:

    main.ts

    declare namespace console {
       function logi(val: i32);
    }
    
    // also you can do this
    @external("env", "logf")
    declare function logf(val: f64);
    
    console.logi(123);
    logf(1e10);
    
    // if you not specify this via namespace of @external decorator external will be imported 
    // with name of module i.e "main" in this case.

    On javascript side you should write something like this:

    index.js

    WebAssembly.instantiateStreaming(fetch("build/main.wasm"), {
      env: {
        // import as @external("env", "logf")
        logf(value) {
          console.log("logf: " + value);
        },
        abort(msg, file, line, column) {
          console.error("abort called at main.ts:" + line + ":" + column);
        }
      },
      console: {
        // import as console.logi
        logi(value) {
          console.log("logi: " + value);
        }
      }
    }).then(result => {
      // const exports = result.instance.exports
    });
  2. MaxGraey commented on Jul 4, 2018

    @MaxGraey
    Member

    Arguments which can be passed between JS <-> WASM is pretty limit it's just i32 and f64. For passing strings, arrays and etc you can use this package

  3. MaxGraey commented on Jul 5, 2018

    @MaxGraey
    Member

    By the way after approved this PR AssemblyScript in WebAssembly Studio works again

  4. dcodeIO commented on Oct 27, 2018

    @dcodeIO
    Member

    Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary!

  5. trusktr commented on May 27, 2019

    @trusktr
    Member

    For the life of me, I can't figure this out (well, short of diving into the AssemblyScript source code). I posted more details in #384

    EDIT: As described in #384, I resorted to using @external syntax, and placed that in a separate file so as not to taint the other files with non-portable syntax.

  6. PaperPrototype commented on Nov 17, 2025

    @PaperPrototype

    for a scripting playground do I have the user specify externs or is there a way to not need to declare them but instead have them simply exist in the global scope?

    Or I guess in other words how do I instantiate the standard library. Maybe I could manually add the declare before the user code with string combine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions