zksecurity / wasmati

Write low-level WebAssembly, from JavaScript
MIT License
218 stars 6 forks source link

TypeScript type of call's return value doesn't match runtime type #8

Closed ion1 closed 2 months ago

ion1 commented 2 months ago

This compiles. My editor says the return type of call(print, ...) is [StackVar<"i32">].

const print = importFunc(
  { in: [i32, i32, i32, i32, i32, i32, i32], out: [i32] },
  () => {}
);

const printCentered = func(
  { in: [i32, i32, i32, i32, i32, i32, i32], out: [i32], locals: [] },
  ([text, x, y, color, fixed, scale, small]) => {
    const [width] = call(print, [text, 0, -6, color, fixed, scale, small]);
    const newX = i32.sub(x, i32.div_s(width, 2));
    return call(print, [text, newX, y, color, fixed, scale, small]);
  }
);

However, it fails at runtime with TypeError: call is not a function or its return value is not iterable.

Changing

-    const [width] = call(print, [text, 0, -6, color, fixed, scale, small]);
+    const width: any = call(print, [text, 0, -6, color, fixed, scale, small]);
+    console.info(width);

makes it work and prints { kind: 'stack-var', id: 16, type: 'i32' } which is indeed not an array.

mitschabaude commented 2 months ago

Thanks for finding that, a type bug I will fix

mitschabaude commented 2 months ago

Fixed and released as 0.2.3