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

how can I get strings from AS in JS #204

Closed
drachehavoc opened this issue Aug 3, 2018 · 6 comments
Closed

how can I get strings from AS in JS #204

drachehavoc opened this issue Aug 3, 2018 · 6 comments
Labels

Comments

@drachehavoc
Copy link

drachehavoc commented Aug 3, 2018

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.

@dcodeIO
Copy link
Member

dcodeIO commented Aug 3, 2018

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.

@drachehavoc
Copy link
Author

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)

@dcodeIO
Copy link
Member

dcodeIO commented Aug 3, 2018

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

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

@drachehavoc
Copy link
Author

drachehavoc commented Aug 3, 2018

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! ^^

@drachehavoc
Copy link
Author

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

@drachehavoc drachehavoc changed the title how can I get strings and arrays from AS in JS how can I get strings from AS in JS Aug 4, 2018
@dcodeIO
Copy link
Member

dcodeIO commented Oct 27, 2018

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

@dcodeIO dcodeIO closed this as completed Oct 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants