百科狗-知识改变命运!
--

WebAssembly.Global - JavaScript WebAssembly 对象

乐乐1年前 (2023-11-21)阅读数 22#技术干货
文章标签实例

WebAssembly.Global

WebAssembly.Global - JavaScript WebAssembly 对象

WebAssembly.Global对象表示一个全局变量实例,可以被JavaScript 和importable/exportable 访问,跨越一个或多个WebAssembly.Module实例.他允许被多个modules动态连接.

构造函数语法

var myGlobal = new WebAssembly.Global(descriptor, value);

参数

descriptorGlobalDescriptor包含2个属性的表:
  • value: A USVString表示全局变量的数据类型.可以是i32,i64,f32,或f64.
  • mutable:布尔值决定是否可以修改.默认是false.
value可以是任意变量值. This can be any value,需要其类型与变量类型匹配.如果变量没有定义,使用0代替,参考DefaultValue algorithm.

Global 构造函数属性

None.

Global 实例

所有的Global实例继承自Global()构造函数 prototype object —修改会影响所有Global实例.

实例属性

Global.prototype.constructorReturns the function that created this object's instance. By default this is the WebAssembly.Global() constructor.Global.prototype[@@toStringTag]The initial value of the @@toStringTag property is the String value "WebAssembly.Global".Global.prototype.valueThe value contained inside the global variable — this can be used to directly set and get the global's value.

实例方法

Global.prototype.valueOf()Old-style method that returns the value contained inside the global variable.

例子

以下例子展示了使用WebAssembly.Global()构造函数创建一个新的实例.它定义为可修饰的类型为i32, 值为 0.

global的值发生改变,首先设置Global.value为42,然后使用导出函数incGlobal()增加为43.导出函数在global.wasm模块中(它将参数的值加一并返回).

const output = document.getElementById('output');

function assertEq(msg, got, expected) {
    output.innerHTML += `Testing ${msg}: `;
    if (got !== expected)
        output.innerHTML += `FAIL!
Got: ${got}
Expected: ${expected}
`; else output.innerHTML += `SUCCESS! Got: ${got}
`; } assertEq("WebAssembly.Global exists", typeof WebAssembly.Global, "function"); const global = new WebAssembly.Global({value:'i32', mutable:true}, 0); WebAssembly.instantiateStreaming(fetch('global.wasm'), { js: { global } }) .then(({instance}) => { assertEq("getting initial value from wasm", instance.exports.getGlobal(), 0); global.value = 42; assertEq("getting JS-updated value from wasm", instance.exports.getGlobal(), 42); instance.exports.incGlobal(); assertEq("getting wasm-updated value from JS", global.value, 43); });

注意:你可以在running live on GitHub 查看例子;也可以访问source code.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)