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

TypedArray forEach method have an unknown error #1051

Closed
echoloyuk opened this issue Jan 10, 2020 · 9 comments
Closed

TypedArray forEach method have an unknown error #1051

echoloyuk opened this issue Jan 10, 2020 · 9 comments

Comments

@echoloyuk
Copy link

here is my demo code:

export function sum(arr: Uint32Array): i32 {
  let a: i32 = 0;
  arr.forEach((value: i32) => {
    a += value;
  });
  return a;
}

as the code sample means, function sum will return the array's sum. I use forEach to get the sum to param: a, and then return. but when i execute asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug command and then gets an error, with useless detail. I still cannot know whats going on?

The error in terminal is:

as1@1.0.0 asbuild:untouched /Users/mac/myProject/assemblyscript/as1
> asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug

ERROR: AssertionError: assertion failed
    at t.assert (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:556244)
    at m.compileBinaryExpression (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:258503)
    at m.compileExpression (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:245392)
    at m.compileExpressionStatement (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:234125)
    at m.compileStatement (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:230467)
    at m.compileStatements (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:231414)
    at m.compileFunctionBody (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:220994)
    at m.compileFunction (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:223519)
    at m.compileFunctionExpression (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:305108)
    at m.compileExpression (/Users/mac/myProject/assemblyscript/as1/node_modules/assemblyscript/dist/assemblyscript.js:1:245670)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! as1@1.0.0 asbuild:untouched: `asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the as1@1.0.0 asbuild:untouched script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mac/.npm/_logs/2020-01-10T16_49_30_104Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! as1@1.0.0 asbuild: `npm run asbuild:untouched && npm run asbuild:optimized`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the as1@1.0.0 asbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mac/.npm/_logs/2020-01-10T16_49_30_130Z-debug.log
@MaxGraey
Copy link
Member

AssermblyScript doesn't support closures yet

@dcodeIO
Copy link
Member

dcodeIO commented Jan 10, 2020

Strange though that this is still hitting an assertion, will investigate.

@MaxGraey
Copy link
Member

Yeah, after #955 it should emit error properly. Hmm

@echoloyuk
Copy link
Author

AssermblyScript doesn't support closures yet

Is there any recommend ways? Now I change to:

export function sum(arr: Uint32Array): i32 {
  let a: i32 = 0;
  arr.forEach(function(value: i32) {
    a += value;
  });
  return a;
}

And the error still the same

@dcodeIO
Copy link
Member

dcodeIO commented Jan 10, 2020

Seems this affects compound expressions like += only since these don't do the common denominator check that would otherwise yield a proper error.

@MaxGraey
Copy link
Member

MaxGraey commented Jan 10, 2020

Because it still capture a variable in lexical environment.
Workarounds:

  1. using for/while loop
  2. using global for a:
let a: u32 = 0;
export function sum(arr: Uint32Array): u32 {
  arr.forEach(value => { a += value });
  return a;
}
  1. using reduce:
export function sum(arr: Uint32Array): u32 {
  return arr.reduce((a, b) => a + b, 0 as u32);
}

@echoloyuk
Copy link
Author

Ohhhhh~~~ I got that. "doesn't support closures" means I cannot use any ref params outside forEach in the loop function. Looks little wierd~~.

@echoloyuk
Copy link
Author

Because it still capture a variable in lexical environment.
Workarounds:

  1. using for/while loop
  2. using global for a:
let a: u32 = 0;
export function sum(arr: Uint32Array): u32 {
  arr.forEach(value => { a += value });
  return a;
}
  1. using reduce:
export function sum(arr: Uint32Array): u32 {
  return arr.reduce((a, b) => a + b, 0 as u32);
}

Thank you so much~~~, finally I change to the for loop. it works.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 10, 2020

Half a fix for at least the assertion in #1046, but would probably be better to have a never type to handle that properly.

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

No branches or pull requests

3 participants