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

{{page('/ko/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Global/prototype', 'Properties')}}

Instance methods

{{page('/ko/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Global/prototype', 'Methods')}}

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

See also