Giter Site home page Giter Site logo

Comments (11)

aminya avatar aminya commented on August 25, 2024 4

Hi @torch2424! Thank you for helping! This is my code:

asbind_loader

import fs from "fs"
import { AsBind } from "as-bind";
const wasm = fs.readFileSync("./dist/build/release.wasm");
export const myWasm = AsBind.instantiateSync(wasm)

index.ts

import {myWasm} from './asbind_loader';

// just to generate an array
import { Chance } from "chance"
const chance = new Chance()

const arrnumber = chance.n(chance.floating, 1000)  // or just [1,2,3,4,5]
const arr = new Float64Array(arrnumber)

console.log('AS =', myWasm.exports.for_array64(arr));

function for_arrayts(arr: number[]): number {
  let sum = 0
  for (let i = 0, l = arr.length; i < l; ++i) {
    sum += arr[i]
  }
  return sum
}

console.log('TS =', for_arrayts(arr as number[]));

assembly/index.ts

export function for_array64(arr: Array<f64>): f64 { // I tried Float64Array too
  let sum = 0.0
  for (let i = 0, l = arr.length; i < l; ++i) {
    sum += arr[i]
  }
  return sum
}

from as-bind.

torch2424 avatar torch2424 commented on August 25, 2024 1

Hello! Again, per my comment on #27 , I just saw this, got buried in Github notifications 😒

But! Super happy to help you out! πŸ˜„

Hmm, so my initial though is that BigInt for Javascript / Wasm is still in progress, thus, passing back 64 bit integers may not work πŸ€” But I wrote a test, and it seems to work for me?

So this took me a good hour to figure out, but the fix is a bit entertaining.

So it seems like you cannot use Array<f64>, instead you would want to use: Float64Array, which is the appropriate TypedArray type in AssemblyScript.

Thus, your wasm.ts should become:

export function for_array(arr: Float64Array): f64 {
  let sum = 0.0
  for (let i= 0, l= arr.length; i < l; ++i) {
    sum += arr[i]
  }
  return sum
}

I'm going to keep this issue open as a documentation issue on my end. I'll do some more clarification of types πŸ˜„

from as-bind.

torch2424 avatar torch2424 commented on August 25, 2024 1

@aminya

You are welcome! πŸ˜„ πŸŽ‰

I think it is beneficial to have more examples in the whole AssemblyScript handbook (starting by the examples taken from the tests). Similar to this documentation for Julia langugea: https://docs.julialang.org/en/v1/base/arrays/#Basic-functions-1. Each function and data type should have at least an example! The examples can be very simple. For example, using different data types in a function, and calling some methods of them, looping over them etc. This is better for learning purposes rather than a real world complex example. Learning by examples is always better than only reading the documentation smile.

Ah so I totally agree! I created: WasmByExample as like a way to learn how to accomplish small tasks by example. Albeit, it doesn't have an example for every global / feature in the AS stdlib. I think it's a good start πŸ˜„

That being said, I build as-bind, because I thought it'd probably be less work to build a tool, than write an example on passing data types between AS and JS and how to handle that haha! So I was planning on working on WasmByExample for as-bind examples as I start writing those examples. So it's coming! πŸ˜„ πŸ‘

Also, if you would like to help with documentation for AssemblyScript stuff, I'd be stoked to point you in the right direction for that! Currently, it's all in a gitbook, but I can sync up with @dcodeIO on how we can make that more open so we can contribute examples πŸ˜„

Thanks! πŸ˜„ πŸ‘

from as-bind.

torch2424 avatar torch2424 commented on August 25, 2024 1

@aminya Oh haha! Well glad it got figured out! πŸ˜‚ πŸ˜„

from as-bind.

aminya avatar aminya commented on August 25, 2024

Hello! Again, per my comment on #27 , I just saw this, got buried in Github notifications 😒

But! Super happy to help you out! πŸ˜„

Hmm, so my initial though is that BigInt for Javascript / Wasm is still in progress, thus, passing back 64 bit integers may not work πŸ€” But I wrote a test, and it seems to work for me?

So this took me a good hour to figure out, but the fix is a bit entertaining.

So it seems like you cannot use Array<f64>, instead you would want to use: Float64Array, which is the appropriate TypedArray type in AssemblyScript.

Thus, your wasm.ts should become:

export function for_array(arr: Float64Array): f64 {
  let sum = 0.0
  for (let i= 0, l= arr.length; i < l; ++i) {
    sum += arr[i]
  }
  return sum
}

I'm going to keep this issue open as a documentation issue on my end. I'll do some more clarification of types πŸ˜„

Thank you for the response! I think it is beneficial to have more examples in the whole AssemblyScript handbook (starting by the examples taken from the tests). Similar to this documentation for Julia langugea: https://docs.julialang.org/en/v1/base/arrays/#Basic-functions-1.

Each function and data type should have at least an example! The examples can be very simple. For example, using different data types in a function, and calling some methods of them, looping over them etc. This is better for learning purposes rather than a real world complex example.

Learning by examples is always better than only reading the documentation πŸ˜„.

from as-bind.

aminya avatar aminya commented on August 25, 2024

@torch2424 I am getting this error, tying my example in this new version:

❯ npm run start

> wasm-demo@1.0.0 start C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-typescript_fork
> node --experimental-modules --experimental-wasm-modules ./dist/index.js
C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-typescript_fork\node_modules\a
s-bind\dist\as-bind.cjs.js:16
h]=a.charCodeAt(h);return d};a.__getString=function(a){let b=k.buffer;if((new Uint32Array(b))[a+ID_OFFSET>>>2]
!==STRING_ID)throw Error("not a string: "+a);return getStringImpl(b,a)};a.__allocArray=function(a,b){var c=d(a
);if(!(c&(ARRAYBUFFERVIEW|ARRAY|STATICARRAY)))throw Error("not an array: "+a+", flags= "+c);let h=31-Math.clz3
2(c>>>VAL_ALIGN_OFFSET&31),m=b.length,g=q(m<<h,c&STATICARRAY?a:ARRAYBUFFER_ID);if(c&STATICARRAY)a=g;else{a=q(c&ARRAY?ARRAY_SIZE:ARRAYBUFFERVIEW_SIZE,a);var e=new Uint32Array(k.buffer);

                                              ^
Error: not an array: undefined, flags= 32
    at Object.a.__allocArray (C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-
typescript_fork\node_modules\←[4mas-bind←[24m\dist\as-bind.cjs.js:16:273)
    at Object.getRef (C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-typescript_fork\node_modules\←[4mas-bind←[24m\dist\as-bind.cjs.js:31:126)
    at C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-typescript_fork\node_mo
dules\←[4mas-bind←[24m\dist\as-bind.cjs.js:35:222
    at Array.forEach (<anonymous>)
    at Object.e [as for_array64] (C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-typescript_fork\node_modules\←[4mas-bind←[24m\dist\as-bind.cjs.js:34:219)
    at Object.<anonymous> (C:\Users\yahyaaba\Documents\GitHub\JavaScript\@assemblyscript\assemblyscript-in-typ
escript_fork\dist\index.js:43:52)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1236:30)←[39m←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1257:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:1085:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:950:14)←[39m
←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m ←[0m←[35mcode←[0m ELIFECYCLE
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m ←[0m←[35merrno←[0m 1
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m wasm-demo@1.0.0 start: `node --experimental-modules --experi
mental-wasm-modules ./dist/index.js`
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m Exit status 1
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m Failed at the wasm-demo@1.0.0 start script.
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m This is probably not a problem with npm. There is likely add
itional logging output above.
←[0m
←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m A complete log of this run can be found in:
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m     C:\Users\yahyaaba\AppData\Roaming\npm-cache\_logs\2020-0
7-25T21_38_39_389Z-debug.log

from as-bind.

torch2424 avatar torch2424 commented on August 25, 2024

Hello @aminya ! πŸ˜„ Thanks for bringing this up. Could you provide the code that is causing this error? Thanks! πŸ˜„

from as-bind.

torch2424 avatar torch2424 commented on August 25, 2024

@aminya Awesome! Thanks for sharing that! πŸ˜„

So would you happen to have a place where I could build and run the code as well? As I see you are importing a module: import { Chance } from "chance". And I am unsure what it is or what it does.

As these lines:

const arrnumber = chance.n(chance.floating, 1000)  // or just [1,2,3,4,5]
const arr = new Float64Array(arrnumber)

I expect something is up here. As the value passed to a typedArray is the size of the array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray πŸ˜„

from as-bind.

aminya avatar aminya commented on August 25, 2024

I was cleaning up the example to upload it for you that I discovered that I was calling the wrong build command!

Calling asc_optimized instead of asc_asbind

    "asc_optimized": "asc assembly/index.ts -b build/release.wasm -t build/release.wat --enable -O3 --noAssert  --tsdFile ./build/release.d.ts",
    "asc_asbind": "asc ./node_modules/as-bind/lib/assembly/as-bind.ts assembly/index.ts -b build/release.wasm -t build/release.wat --enable -O3",

πŸ€¦β€β™‚οΈ

from as-bind.

aminya avatar aminya commented on August 25, 2024

Yes! I wish we can find a way to detect this miscall and throw a better warning πŸ˜„! I was stuck in this for a week or so.

from as-bind.

torch2424 avatar torch2424 commented on August 25, 2024

Oh I could totally add an error for that. Opened #44

from as-bind.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.