Comments (11)
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.
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.
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.
@aminya Oh haha! Well glad it got figured out! π π
from as-bind.
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.
@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.
Hello @aminya ! π Thanks for bringing this up. Could you provide the code that is causing this error? Thanks! π
from as-bind.
@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.
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.
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.
Oh I could totally add an error for that. Opened #44
from as-bind.
Related Issues (20)
- add support for exported classes HOT 4
- [good first issue] Error handling for wrong JS glue.
- How does this compare to host bindings? HOT 1
- File as-bind.ts not found
- Not working with the latest AssemblyScript version HOT 3
- Add support for Array<T> HOT 2
- Find a better way to transpose the type table in the README
- as-bind seems to not be able to handle proxies? HOT 1
- Add links to as-loader package HOT 1
- RFE: Document the metadata format HOT 7
- Using AsBind in Node can have some package resolution issues
- The new version(0.7.0) doesn't work. HOT 6
- README is not keeping up with the new version(0.7.0) HOT 1
- Strings can get corrupted, due to them not being a Pinned GC Object HOT 4
- Does as-bind work with the newest assemblyscript version? HOT 2
- [Feature Request] add binding support for declare fucntion exported in a namespace HOT 3
- [Feature Request] add support for generic function HOT 1
- [Bug] Float32Array was bind to ArrayBuffer
- __getArray is not a function HOT 2
- feature request: generate typescript definiton for module HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from as-bind.