【问题标题】:In typescirpt,Is there a way to check the type of a value in wasm which has been loaded by typescirpt, example:i32?在打字稿中,有没有办法检查wasm中已由打字稿加载的值的类型,例如:i32?
【发布时间】:2020-02-02 01:04:37
【问题描述】:

在 WebAssembly 中,我们有 i32 一个 32 位有符号整数,如果我们加载这个 wasm,那么我们可以检查 i32 的类型吗?如果不能通过 javascirpt/typescirpt 检查 i32,是否有另一种方法来检查 wasm 的值?

所以我尝试构建wasm,typeof返回一个“数字”

enter image description here

main.js

WebAssembly.instantiateStreaming(fetch("../out/main.wasm"), {
  main: {
    sayHello() {
      console.log("Hello from WebAssembly!");
    }
  },
  env: {
    abort(_msg, _file, line, column) {
      console.error("abort called at main.ts:" + line + ":" + column);
    }
  },
}).then(result => {
  const exports = result.instance.exports;
  const addResult = exports.add(19, 23);
  document.getElementById("container").textContent = "Result: " + exports.add(19, 23) + "Type:" + (typeof addResult);
}).catch(console.error);

那么,还有其他方法可以检查 wasm 的值吗?

【问题讨论】:

    标签: javascript typescript typescript-typings webassembly assemblyscript


    【解决方案1】:

    你可以使用typeof运算符

    typeof 运算符返回其操作数的数据类型,操作数可以是任何对象、函数或变量。

    例如:

    输入:typeof "raman" 输出字符串

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 2020-09-28
    • 1970-01-01
    • 2020-11-03
    • 2021-10-04
    • 1970-01-01
    • 2020-12-01
    • 2019-06-27
    相关资源
    最近更新 更多