| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/WebAssembly/Guides/Using_the_JavaScript_API | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
, Emscripten, , WebAssembly JavaScript API. .
: , , , WebAssembly.
, WebAssembly JavaScript API, wasm- web-.
: webassembly-examples GitHub.
wasm-! simple.wasm .
, wasm- HTML- index.html ( HTML ).
, , ( WebAssembly wasm):
(module
(func $i (import "imports" "imported_func") (param i32))
(func (export "exported_func")
i32.const 42
call $i))
import - $i imports.imported_func. JavaScript-, wasm-. <script></script> HTML-, :
var importObject = {
imports: { imported_func: (arg) => console.log(arg) },
};
Firefox 58 - () WebAssembly . WebAssembly.compileStreaming() WebAssembly.instantiateStreaming(). , - , (Response) ArrayBuffer .
(. instantiate-streaming.html GitHub ) instantiateStreaming() wasm-, JavaScript , , . .
:
WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
(obj) => obj.instance.exports.exported_func(),
);
WebAssembly- exported_func, JavaScript- imported_func, (42), WebAssembly. , WebAssembly, !
: , , WebAssembly- JavaScript- . , WebAssembly JavaScript. , .
, WebAssembly.compile / WebAssembly.instantiate.
-, ArrayBuffer wasm-.
:
fetch("simple.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject))
.then((results) => {
results.instance.exports.exported_func();
});
Firefox 54+, wasm-, -. , "wasm://".
Firefox, wasm- , wasm ( , , , ..). . WebAssembly debugging with Firefox DevTools -.
WebAssembly, , (Linear Memory), . , . C/C++ .
C/C++ , , WebAssembly ( ) , WebAssembly. web- ( WebAssembly) .
JavaScript-, WebAssembly ArrayBuffer c . - . , WebAssembly.Memory(), ().
.
HTML ( HTML ) memory.html. <script></script> .
, :
var memory = new WebAssembly.Memory({ initial: 10, maximum: 100 });
(initial) (maximum) 64KB. , 640KB, 6.4MB.
WebAssembly getter/setter buffer, ArrayBuffer. , 42 , :
new Uint32Array(memory.buffer)[0] = 42;
:
new Uint32Array(memory.buffer)[0];
- , , , JavaScript-.
Memory.prototype.grow(), ( 64KB) WebAssembly:
memory.grow(1);
, , WebAssembly.RangeError. , .
:
ArrayBuffer , Memory.prototype.grow() buffer ArrayBuffer ( byteLength) ArrayBuffer "", , .
, . , . , JavaScript- WebAssembly c WebAssembly.Memory , ( Instance.prototype.exports).
, , WebAssembly- , , JavaScript- , wasm- .
: memory.wat.
memory.html , wasm-:
WebAssembly.instantiateStreaming(fetch("memory.wasm"), {
js: { mem: memory },
}).then((results) => {
// add code here
});
, , (mem), accumulate() . , :
var i32 = new Uint32Array(memory.buffer);
for (var i = 0; i < 10; i++) {
i32[i] = i;
}
var sum = results.instance.exports.accumulate(0, 10);
console.log(sum);
, Uint32Array buffer (Memory.prototype.buffer), .
, JavaScript . :
WebAssembly - , JavaScript WebAssembly . , , , , .
, , . WebAssembly, WebAssembly - - . .
C/C++, . C/C++, , , . , , .
, WebAssembly , . , , .
Table.prototype.set(), , Table.prototype.grow(), , . "- " , . Table.prototype.get() JavaScript- wasm-.
- WebAssembly, : 0 13, 1 42.
: table.wat.
HTML table.html.
, , wasm-, <script> :
WebAssembly.instantiateStreaming(fetch("table.wasm")).then(
function (results) {
// add code here
},
);
- , :
var tbl = results.instance.exports.tbl;
console.log(tbl.get(0)()); // 13
console.log(tbl.get(1)()); // 42
, , . , Table.prototype.get(), .
:
table.html (. ) fetchAndInstantiate().
WebAssembly , JavaScript WebAssembly (WebAssembly.Module) . , . WebAssembly JavaScript-, WebAssembly.Global(), :
const global = new WebAssembly.Global({ value: "i32", mutable: true }, 0);
, 2 :
, 2 , :
value: , , WebAssembly i32, i64, f32, f64.mutable: , ., . .
? , i32 0.
42 Global.value, 43 incGlobal() global.wasm ( 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);
},
);
: GitHub; .
WebAssembly, . WebAssembly :
- WebAssembly JavaScript API WebAssembly JavaScript, , , . .
This page was last modified on 29 . 2026 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |