bytecodealliance/jco

Transpile bindings for `u32` map to `number` in *.d.ts

Open

#402 创建于 2024年3月12日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)Rust (124 fork)github user discovery
buggood first issueproject/js-component-bindgen

仓库指标

Star
 (952 star)
PR 合并指标
 (平均合并 9小时 50分钟) (30 天内合并 94 个 PR)

描述

I noticed in making this little demo that I could not use signed or out-of-bounds numbers for wasmtime but that in the browser and via node it does.

https://stackoverflow.com/help/minimal-reproducible-example adding to the cli:

../wit/calculator.wit

interface calculate {
    enum op {
        add,
    }
    eval-expression: func(op: op, x: u32, y: u32) -> u32;
}

jco version 1.0.3 generated bindings/interfaces/docs-calculator-calculate.d.ts

export namespace DocsCalculatorCalculate {
  export function evalExpression(op: Op, x: number, y: number): number;
}

cli-calc.js:

// u32 is specified in wit, but typescript bindings specify only `number`
console.log("Unsigned overflow = " + calculate.evalExpression("add", 4294967300, 0));
console.log("Unsigned underflow = " + calculate.evalExpression("add", -4294967200, 0));
//                                   negative should fail types... 😬 ^^^^^
// $ node cli-calc.js
Answer (to life) = 42
Unsigned overflow = 4
Unsigned underflow = 96

Is there a way now to ensure that bindings generated use the WIT specified stronger types? I would think softening the interface to number should not be the default :sweat_smile:

贡献者指南