WebAssembly.Global
WebAssembly.Global
๊ฐ์ฒด๋ ์ ์ญ ๋ณ์ ์ธ์คํด์ค๋ฅผ ๋ํ๋ด๋ฉฐ JavaScript ๋ฐ ํ๋ ์ด์์ WebAssembly.Module
์ธ์คํด์ค์์ ๊ฐ์ ธ ์ค๊ฑฐ๋ ๋ด๋ณด๋ผ ์ ์์ต๋๋ค. ์ด๋ ๊ฒํ๋ฉด ์ฌ๋ฌ ๋ชจ๋์ ๋์ ์ผ๋ก ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.Constructor Syntax
var myGlobal = new WebAssembly.Global(descriptor, value);
Parameters
- descriptor
- A
GlobalDescriptor
dictionary object, ๋ ๊ฐ์ ์์ฑ์ด ํฌํจ๋์ด ์์ต๋๋ค.value
:USVString
๋ ์ ์ญ์ ๋ฐ์ดํฐ ์ ํ์ ๋ํ๋ ๋๋ค. i32, i64, f32 ๋ฐ f64 ์ค ํ๋์ ๋๋ค.mutable
: ์ ์ญ ๋ณ์๋ฅผ ๋ณ๊ฒฝํ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ ๋ถ์ธ ๊ฐ์ ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋กfalse
์ ๋๋ค.
- value
- ๋ณ์์ ํฌํจ ๋ ๊ฐ์
๋๋ค. ๋ณ์์ ๋ฐ์ดํฐ ํ์
๊ณผ ์ผ์นํ๋ ํ ๋ชจ๋ ๊ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋ณ์์ ํฌํจ ๋ ๊ฐ์ ๋๋ค. ๋ณ์์ ๋ฐ์ดํฐ ํ์ ๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๊ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ฐ์ ์ง์ ํ์ง ์์ผ๋ฉดDefaultValue
์๊ณ ๋ฆฌ์ฆ์ ์ง์ ๋๋๋ก ์ ๋ ฅ ๋ 0 ๊ฐ์ด ์ฌ์ฉ๋ฉ๋๋ค.
Function properties of the Module
constructor
None.
Global instances
๋ชจ๋ Global
์ธ์คํด์ค๋ Global()
์์ฑ์์ prototype object์์ ์์๋ฐ์ต๋๋ค.์ด ์ธ์คํด์ค๋ ๋ชจ๋ Global
์ธ์คํด์ค์ ์ํฅ์ ๋ฏธ์น๋๋ก ์์ ํ ์ ์์ต๋๋ค.
Instance properties
Global.prototype.constructor
- ์ด ๊ฐ์ฒด์ ์ธ์คํด์ค๋ฅผ ์์ฑ ํ ํจ์๋ฅผ ๋๋ ค์ค๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๊ฒ์
WebAssembly.Global()
์์ฑ์์ ๋๋ค. Global.prototype[@@toStringTag]
- @@toStringTag ์์ฑ์ ์ด๊ธฐ ๊ฐ์ String ๊ฐ์ธ "WebAssembly.Global"์ ๋๋ค.
Global.prototype.value
- ์ ์ญ ๋ณ์์ ํฌํจ ๋ ๊ฐ - ์ ์ญ ๊ฐ์ ์ง์ ์ค์ ํ๊ณ ๊ฐ์ ธ ์ค๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Instance methods
Global.prototype.valueOf()
- ์ ์ญ ๋ณ์์ ํฌํจ ๋ ๊ฐ์ ๋ฐํํ๋ Old-style ๋ฉ์๋์ ๋๋ค.
Examples
๋ค์ ์์ ์์๋ WebAssembly.Global()
์์ฑ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ง๋๋ ์ ์ ์ญ ์ธ์คํด์ค๋ฅผ ๋ณด์ฌ์ค๋๋ค. ๊ฐ์ด 0 ์ธ ๋ณ๊ฒฝ ๊ฐ๋ฅํ i32
์ ํ์ผ๋ก ์ ์๋ฉ๋๋ค.
The value of the global is then changed, first to 42
using the Global.value
property, and then to 43 using the incGlobal()
function exported out of the global.wasm
module (this adds 1 to whatever value is given to it and then returns the new value).
๊ทธ๋ฐ ๋ค์ global.value
์์ฑ์ ์ฌ์ฉํ์ฌ 42
๊น์ง ์ ์ญ ๊ฐ์ ๋ณ๊ฒฝ ํ ๋ค์ global.wasm
๋ชจ๋์์ ๋ด ๋ณด๋ธ incGlobal()
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ 43์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค.(์ด๊ฒ์ ๊ฐ์ด ์ฃผ์ด์ง ๊ฐ์ 1์ ๋ํ ๋ค์ ์ ๊ฐ์ ๋ฐํํฉ๋๋ค.)
const output = document.getElementById('output');
function assertEq(msg, got, expected) {
output.innerHTML += `Testing ${msg}: `;
if (got !== expected)
output.innerHTML += `FAIL!<br>Got: ${got}<br>Expected: ${expected}<br>`;
else
output.innerHTML += `SUCCESS! Got: ${got}<br>`;
}
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);
});
Note: GitHub์์ ์คํ์ค์ธ ์์ (running live on GitHub)๋ฅผ ๋ณผ ์ ์์ต๋๋ค. source code๋ ์ฐธ์กฐํ์ญ์์ค.
Specifications
Specification | Status | Comment |
---|---|---|
WebAssembly JavaScript Interface The definition of 'WebAssembly.Global()' in that specification. |
Working Draft | Initial draft definition. |
Browser compatibility
BCD tables only load in the browser