Assemblyscript: how can I get strings from AS in JS

Created on 3 Aug 2018  路  6Comments  路  Source: AssemblyScript/assemblyscript

I'm pretty new in AS and WA, and my current doubt is how I can get Strings and Arrays from JavaScript.

index.js

const fs = require("fs");
const abort = () => { throw new Error('....') };
const compiled = new WebAssembly.Module(fs.readFileSync("aaa.wasm"));
const instance = new WebAssembly.Instance(compiled, { env: { abort } })
const lib = instance.exports

console.log('  say', ':', lib.say("oi"))
console.log('  say', ':', lib.say("hi"))
console.log('  add', ':', lib.add(100, 100))
console.log('teste', ':', lib.teste())

index.ts

import 'allocator/buddy';

export function say(hello: string): string {
  return hello + " world"
}

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function teste(): string {
  let x = "ZZZZZZZZZZZZ"
  return x;
}

copile and run with nodejs

$ asc assembly\index.ts -o aaa.wasm 
$ node index.js

what I get (I suspect these integers is memory pointer, I'm right?)

  say : 3136
  say : 3168
  add : 200
teste : 84

what I expect

  say : oi world
  say : hi world
  add : 200
teste : ZZZZZZZZZZZZ

Sorry for my poor english, I'm from Brazil and portuguese is my main language, and tks for the help.

question

Most helpful comment

Just posting my tests as reference/example to others adventurers programmers:

index.ts

import "allocator/tlsf";

export { memory };

export function say(hello: string): string {
  return hello + " world"
}

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function teste(): string {
  let x = "ZZZZZZZZZZZZ"
  return x;
}

export class X {
  teste(): String {
    return "Aew";
  }
}

index.js

const loader = require("./assemblyscript/lib/loader/index");
const fs = require("fs");
const file = fs.readFileSync("index.wasm")
const lib = loader.instantiateBuffer(file, {});
const x = new lib.X()

console.log('   say :' , lib.getString(lib.say(lib.newString("oi"))))
console.log('   say :' , lib.getString(lib.say(lib.newString("hi"))))
console.log('   add :' , lib.add(100, 100))
console.log(' teste :' , lib.getString(lib.teste()))
console.log('method :' , lib.getString(x.teste()))

compile and run

位 asc index.ts -o index.wasm
位 node index.js

results

   say : oi world
   say : hi world
   add : 200
 teste : ZZZZZZZZZZZZ
method : Aew

All 6 comments

The loader provides utility to read a string from memory using its pointer (here: getString), but you can also do it manually based on the memory layout. A string, for example, is a 32-bit .length followed by .length 16-bit character codes.

I read the loader documentation and try to use it withou success,
Am I doing something wrong?
Am I need install loader via npm?

index.ts

const loader = require("@assemblyscript/loader");
const file = fs.readFileSync("aaa.wasm")
const fs = require("fs");
const myModule = loader.instatiateBuffer(file, {});

error

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module '@assemblyscript/loader'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\terminal\user\personal\wasmTs\index.js:1:78)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

The loader is not on npm yet. For now, try:

const loader = require("assemblyscript/lib/loader");

Wow, this worked!!!

I found a mistake in the loader documentation, in the example is write const myModule = loader.instatiateBuffer(fs.readFileSync("myModule.wasm"), myImports); instead instantiateBuffer.

Thanks so much for the help! ^^

Just posting my tests as reference/example to others adventurers programmers:

index.ts

import "allocator/tlsf";

export { memory };

export function say(hello: string): string {
  return hello + " world"
}

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function teste(): string {
  let x = "ZZZZZZZZZZZZ"
  return x;
}

export class X {
  teste(): String {
    return "Aew";
  }
}

index.js

const loader = require("./assemblyscript/lib/loader/index");
const fs = require("fs");
const file = fs.readFileSync("index.wasm")
const lib = loader.instantiateBuffer(file, {});
const x = new lib.X()

console.log('   say :' , lib.getString(lib.say(lib.newString("oi"))))
console.log('   say :' , lib.getString(lib.say(lib.newString("hi"))))
console.log('   add :' , lib.add(100, 100))
console.log(' teste :' , lib.getString(lib.teste()))
console.log('method :' , lib.getString(x.teste()))

compile and run

位 asc index.ts -o index.wasm
位 node index.js

results

   say : oi world
   say : hi world
   add : 200
 teste : ZZZZZZZZZZZZ
method : Aew

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blooddy picture blooddy  路  3Comments

jerrywdlee picture jerrywdlee  路  4Comments

torch2424 picture torch2424  路  3Comments

lastmjs picture lastmjs  路  4Comments

dcodeIO picture dcodeIO  路  4Comments