This document is an early draft, porting the official JS/WebAssembly interface specification to WebIDL and Bikeshed. It should currently not be used as the WebAssembly specification.
This API is, initially, the only API for accessing WebAssembly [WEBASSEMBLY] from the web platform, through a bridge to explicitly construct modules from ECMAScript [ECMASCRIPT].
In future versions, WebAssembly may
be loaded and run directly from an HTML <script type='module'>
tag—and
any other Web API that loads ES6 modules via URL—as part of ES6 Module integration.)
Note: WebAssembly JS API declaration file for TypeScript can be found here which enable autocompletion and make TypeScript compiler happy.
1. Sample API Usage
This section is non-normative.
Given demo.wat
(encoded to demo.wasm
):
(module (import "js" "import1" (func $i1)) (import "js" "import2" (func $i2)) (func $main (call $i1)) (start $main) (func (export "f") (call $i2)) )
and the following JavaScript, run in a browser:
var importObj = {js: { import1: () => console.log("hello,"), import2: () => console.log("world!") }}; fetch('demo.wasm').then(response => response.arrayBuffer() ).then(buffer => WebAssembly.instantiate(buffer, importObj) ).then(({module, instance}) => instance.exports.f() );
2. The WebAssembly Namespace
dictionaryWebAssemblyInstantiatedSource
{ required Modulemodule
; required Instanceinstance
; }; [Exposed=(Window,Worker,Worklet)] namespaceWebAssembly
{ boolean validate(BufferSourcebytes
); Promise<Module> compile(BufferSourcebytes
); Promise<WebAssemblyInstantiatedSource> instantiate( BufferSourcebytes
, optional objectimportObject
); Promise<Instance> instantiate( ModulemoduleObject
, optional objectimportObject
); };
BufferSource
bytes, perform the following steps:
-
Let module be decode_module(bytes). If module is error, return error.
-
If validate_module(module) is error, return error.
-
Return module.
validate(bytes)
method, when invoked, performs the following steps:
-
Let moduleObject be a new
Module
object. -
Set moduleObject.[[Module]] to module.
-
Set moduleObject.[[Bytes]] to bytes.
-
Return moduleObject.
BufferSource
bytes, with the promise promise, using optional task source taskSource, perform the following steps:
-
In parallel, compile the WebAssembly module bytes and store the result as module.
-
When the above operation completes, queue a task to perform the following steps. If taskSource was provided, queue the task on that task source.
-
If module is error, reject promise with a
CompileError
exception. -
Otherwise,
-
Construct a WebAssembly module object from module and let moduleObject be the result.
-
Resolve promise with moduleObject.
-
-
compile(bytes)
method, when invoked, performs the following steps:
-
Let stableBytes be a copy of the bytes held by the buffer bytes.
-
Let promise be a new promise.
-
Asynchronously compile a WebAssembly module from stableBytes with promise.
-
Return promise.
Module
moduleObject and imports importObject, perform the following steps:
-
Let module be moduleObject.[[Module]].
-
If module.imports is not an empty list, and importObject is undefined, throw a
TypeError
exception. -
Let funcs be an empty list of callable JavaScript objects.
-
Let tables be an empty list of
Table
objects.Note: The funcs, memories and tables lists are collected and used in order to cache the JavaScript objects corresponding to WebAssembly objects. If a WebAssembly object is exported multiple times, it will appear in exports as the same object instance. Moreover, if a JavaScript wrapper of a WebAssembly object was imported, and then present on the export, a new wrapper will not need to be created. However, if an ordinary JS function is imported, it will appear as wrapped by an Exported Function.
-
Let imports be an empty list of external values.
-
For each (moduleName, componentName, externtype) in module_imports(module), do
-
Let o be ? Get(importObject, moduleName).
-
Let v be ? Get(o, componentName)
-
If externtype is of the form 𝖿𝗎𝗇𝖼 functype,
-
If IsCallable(v) is false, throw a
LinkError
exception. -
If v has a [[FunctionAddress]] internal slot, and therefore is an Exported Function,
-
Let funcaddr be the value of v’s [[FunctionAddress]] internal slot.
Note: The signature is checked by instantiate_module invoked below.
-
Append v to funcs.
-
-
Otherwise,
-
Create a host function from v and let funcaddr be the result.
-
-
Let externfunc be the external value 𝖿𝗎𝗇𝖼 funcaddr
-
Append externfunc to imports.
-
-
If externtype is of the form 𝗀𝗅𝗈𝖻𝖺𝗅 globaltype,
-
If globaltype is i64 or Type(v) is not Number, throw a
LinkError
exception. -
Let value be ToWebAssemblyValue(v, globaltype.valtype)
-
Assert: globaltype.mut is const, as verified by WebAssembly validation.
-
Let store be the current agent cluster’s associated store.
-
Let (store, globaladdr) be alloc_global(store, globaltype, value).
-
Set the current agent cluster’s associated store to store.
-
Let externglobal be 𝗀𝗅𝗈𝖻𝖺𝗅 globaladdr.
-
Append externglobal to imports.
-
-
If externtype is of the form 𝗆𝖾𝗆 memtype,
Note: instantiate_module invoked below will check the imported
Memory
's size against the importing module’s requirements.-
Append v to memories.
-
Let externmem be the external value 𝗆𝖾𝗆 v.[[Memory]].
-
Append externmem to imports.
-
-
Otherwise, externtype is of the form 𝗍𝖺𝖻𝗅𝖾 tabletype,
Note: The table’s length, etc. is checked by instantiate_module invoked below.
-
Append v to tables.
-
Let tableaddr be v.[[Table]]
-
Let externtable be the external value 𝗍𝖺𝖻𝗅𝖾 tableaddr.
-
Append externtable to imports.
-
For each element func of v.[[Values]],
-
If func is not null, append func to funcs.
-
-
-
-
Let (store, instance) be instantiate_module(store, module, imports), up through step 16 of the instantiation algorithm, but omitting execution of the start function.
-
If instance is error, throw a
LinkError
exception, or other appropriate exception type. -
For each tableaddr in instance.𝗍𝖺𝖻𝗅𝖾𝖺𝖽𝖽𝗋𝗌,
-
If there is no element in tables whose table.[[Table]] is tableaddr:
-
Let table be a new table object from tableaddr.
-
Append table to tables.
-
-
Otherwise:
-
Let table be the element in tables where table.[[Table]] is tableaddr.
-
-
For each index i from 0 to the length of table.[[Values]] − 1, do
-
Let funcaddr be read_table(store, tableaddr, i).
-
Assert: funcaddr is not error because this read is in bounds.
-
If funcaddr is not empty,
-
If funcs contains any func where func.[[ExportedAddress]] is funcaddr,
-
Let func be that function object.
-
-
Otherwise:
-
Let func be a a new Exported Function created from funcaddr.
-
Append func to funcs.
-
-
Set the table.[[Values]][i] to func.
Note: The table and function objects created by the above steps are only observable for tables that are either imported or exported.
-
-
-
-
Let startfuncaddr be the start function of instance.
-
Let (store, ret) be invoke_func(store, startfuncaddr, empty).
-
If ret is error, throw an exception. This exception should be a WebAssembly
RuntimeError
exception, unless otherwise indicated by the WebAssembly error mapping. -
Assert: ret is empty.
-
Let exportsObject be ! ObjectCreate(null).
-
For each pair (nameBytes, externtype) in module_exports(module),
-
Let name be nameBytes decoded as UTF-8.
-
Assert: name is not failure (module is valid).
-
Let externval be get_export(instance, name).
-
If externtype is of the form 𝖿𝗎𝗇𝖼 functype,
-
If funcs contains an entry func where func.[[FunctionAddress]] is externval, let value be func
-
Otherwise,
-
Let func be the result of a new Exported Function from externval.
-
Append func to funcs.
-
Let value be func.
-
-
-
If externtype is of the form 𝗀𝗅𝗈𝖻𝖺𝗅 globaltype,
-
If globaltype.valtype is an i64, throw a
LinkError
exception. -
Assert: globaltype.mut is const.
-
Let value be ToJSValue(read_global(store, externval)).
-
-
If externtype is of the form 𝗆𝖾𝗆 memtype,
-
If there is an element memory in memories such that memory.[[Memory]] is externval, then let value be memory
-
Otherwise:
-
Let memory be a new Memory object from externval.
-
Append memory to memories.
-
Let value be memory.
-
-
-
Otherwise, externtype is of the form 𝗍𝖺𝖻𝗅𝖾 tabletype,
-
Let value be the unique table in tables such that table.[[Table]] is externval.
-
-
Let status be ! CreateDataProperty(exportsObject, name, value).
-
Assert: status is true.
Note: the validity and uniqueness checks performed during WebAssembly module validation ensure that each property name is valid and no properties are defined twice.
-
-
Perform ! SetIntegrityLevel(exportsObject,
"frozen"
). -
Let instanceObject be a new
Instance
object whose internal [[Instance]] slot is set to instance and the [[Exports]] slot to exportsObject. -
Return instanceObject.
-
Let promise be a new promise
-
Upon fulfillment of promiseOfModule with value module:
-
Instantiate the WebAssembly module module importing importObject, and let instance be the result. If this throws an exception, catch it, reject promise with the exception, and abort these substeps.
-
Let result be a
WebAssemblyInstantiatedSource
dictionary withmodule
set to module andinstance
set to instance. -
Resolve promise with result.
-
-
Upon rejection of promiseOfModule with reason reason:
-
Reject promise with reason.
-
-
Return promise.
Note: It would be valid to perform certain parts of the instantiation in parallel, but several parts need to happen in the event loop, including JavaScript operations to access the importObject and execution of the start function.
instantiate(bytes, importObject)
method, when invoked, performs the following steps:
-
Let stableBytes be a copy of the bytes held by the buffer bytes.
-
Let promiseOfModule be a new promise.
-
Asynchronously compile a WebAssembly module from stableBytes with promiseOfModule.
-
Instantiate promiseOfModule with imports importObject and return the result.
instantiate(moduleObject, importObject)
method, when invoked, performs the following steps:
-
Let promise be a new promise.
-
Queue a task to perform the following steps:
-
Instantiate the WebAssembly module module importing importObject, and let instance be the result. If this throws an exception, catch it, and reject promise with the exception.
-
Resolve promise with instance.
-
-
Return promise
Note: A follow-on streaming API is documented in the WebAssembly design repository.
2.1. Modules
enumImportExportKind
{"function"
,"table"
,"memory"
,"global"
}; dictionaryModuleExportDescriptor
{ required DOMStringname
; required ImportExportKindkind
; // Note: Other fields such as signature may be added in the future. }; dictionaryModuleImportDescriptor
{ required DOMStringmodule
; required DOMStringname
; required ImportExportKindkind
; }; [LegacyNamespace=WebAssembly, Constructor(BufferSourcebytes
), Exposed=(Window,Worker,Worklet)] interfaceModule
{ static sequence<ModuleExportDescriptor>exports
(Modulemodule
); static sequence<ModuleImportDescriptor>imports
(Modulemodule
); static sequence<ArrayBuffer>customSections
(Modulemodule
, DOMStringsectionName
); };
-
"function" if type is of the form 𝖿𝗎𝗇𝖼 functype
-
"table" if type is of the form 𝗍𝖺𝖻𝗅𝖾 tabletype
-
"memory" if type is of the form 𝗆𝖾𝗆 memtype
-
"global" if type is of the form 𝗀𝗅𝗈𝖻𝖺𝗅 globaltype
exports(moduleObject)
method, when invoked, performs the following steps:
-
Let module be moduleObject.[[Module]].
-
Let exports be an empty list.
-
For each (nameBytes, type) in module_exports(module)
-
Let name be nameBytes decoded as UTF-8.
-
Assert: name is not failure (module is valid).
-
Let kind be the string value of the extern type type.
-
Let obj be a new
ModuleExportDescriptor
dictionary withname
name andkind
kind. -
Append obj to the end of exports.
-
-
Return exports.
imports(moduleObject)
method, when invoked, performs the following steps:
-
Let module be moduleObject.[[Module]].
-
Let imports be an empty list.
-
For each (moduleBytes, nameBytes, type) in module_imports(module),
-
Let moduleName be moduleBytes decoded as UTF-8.
-
Let name be nameBytes decoded as UTF-8.
-
Assert: The previous operations are not failure, as module is valid.
-
Let kind be the string value of the extern type type.
-
Let obj be a new
ModuleImportDescriptor
dictionary withmodule
moduleName,name
name andkind
kind. -
Append obj to the end of imports.
-
-
Return imports.
customSections(moduleObject, sectionName)
method, when invoked, performs the following steps:
-
Let bytes be moduleObject.[[Bytes]].
-
Let customSections be an empty list of
ArrayBuffer
s. -
For each custom section customSection in the binary format of bytes,
-
Let name be the
name
of the custom section, decoded as UTF-8. -
Assert: name is not failure (moduleObject.[[Module]] is valid).
-
If name equals secondName as string values,
-
Append a new
ArrayBuffer
containing a copy of the bytes held by the buffer bytes for the range matched by this customsec production.
-
-
-
Return customSections.
Module(bytes)
constructor, when invoked, performs the follwing steps:
-
Let stableBytes be a copy of the bytes held by the buffer bytes.
-
Compile the WebAssembly module stableBytes and store the result as module.
-
If module is error, throw a
CompileError
exception. -
Return module.
2.2. Instances
[LegacyNamespace=WebAssembly, Constructor(Modulemodule
, optional objectimportObject
), Exposed=(Window,Worker,Worklet)] interfaceInstance
{ readonly attribute object exports; };
Instance(module, importObject)
constructor, when invoked, instantiates the WebAssembly module module importing importObject and returns the result. exports
attribute of Instance
returns the receiver’s [[Exports]] internal slot. 2.3. Memories
dictionaryMemoryDescriptor
{ required [EnforceRange] unsigned longinitial
; [EnforceRange] unsigned longmaximum
; }; [LegacyNamespace=WebAssembly, Constructor(MemoryDescriptordescriptor
), Exposed=(Window,Worker,Worklet)] interfaceMemory
{ void grow([EnforceRange] unsigned longdelta
); readonly attribute ArrayBufferbuffer
; };
Memory
object represents a single memory instance which can be simultaneously referenced by multiple Instance
objects. Each Memory
object has two internal slots:
-
[[Memory]] : a memory address
-
[[BufferObject]] : an
ArrayBuffer
whose Data Block is identified with the above memory address
-
Let block be a Data Block which is identified with the underlying memory of memaddr
-
Let buffer be a new
ArrayBuffer
whose [[ArrayBufferData]] is block and [[ArrayBufferByteLength]] is set to the length of block. -
Return a new
Memory
instance with [[Memory]] set to memaddr and [[BufferObject]] set to buffer.
Any attempts to detach buffer, other than the detachment performed by grow(delta)
, will throw a TypeError
exception. Specifying this behavior requires changes to the ECMAScript specification.
Memory(descriptor)
constructor, when invoked, performs the following steps:
-
If descriptor.maximum is present, let maximum be descriptor.maximum; otherwise, let maximum be empty.
-
Let memtype be { min descriptor.initial, max maximum }
-
Let store be the current agent cluster’s associated store.
-
Let (store, memaddr) be alloc_mem(store, memtype). If allocation fails, throw a
RangeError
exception. -
Set the current agent cluster’s associated store to store.
-
Create a memory object from the memory address memaddr and return the result.
grow(delta)
method, when invoked, performs the following steps:
-
Let memory be the Memory instance.
-
Let store be the current agent cluster’s associated store.
-
Let memaddr be memory.[[Memory]].
-
Let ret be the size_mem(store, memaddr).
-
Let store be grow_mem(store, memaddr, delta).
-
If store is error, throw a
RangeError
exception. -
Set the current agent cluster’s associated store to store.
-
Perform ! DetachArrayBuffer(memory.[[BufferObject]]).
-
Let block be a Data Block which is identified with the underlying memory of memaddr.
-
Assert: The size of block is the same as size_mem(store, memaddr) * 64 Ki
-
Let buffer be a new
ArrayBuffer
whose [[ArrayBufferData]] is block and [[ArrayBufferByteLength]] is set to the length of block. -
Set memory.[[BufferObject]] to buffer.
-
Return ret.
2.4. Tables
enumTableKind
{"anyfunc"
, }; dictionaryTableDescriptor
{ required TableKindelement
; required [EnforceRange] unsigned longinitial
; [EnforceRange] unsigned longmaximum
; }; [LegacyNamespace=WebAssembly, Constructor(TableDescriptordescriptor
), Exposed=(Window,Worker,Worklet)] interfaceTable
{ voidgrow
([EnforceRange] unsigned longdelta
); Function?get
([EnforceRange] unsigned longdelta
); voidset
([EnforceRange] unsigned longdelta
, Function?value
); readonly attribute unsigned long length; };
Table
object represents a single table instance which can be simultaneously referenced by multiple Instance
objects. Each Table
object has one internal slots:
-
[[Table]] : a table address
-
[[Values]] : a List whose elements are either null or Exported Functions.
-
Let values be a list whose length is size_table(store, tableaddr) where each element is null.
-
Return a new
Table
instance with [[Table]] set to tableaddr and [[Values]] set to values.
Table(descriptor)
constructor, when invoked, performs the following steps:
-
Let n be descriptor.initial.
-
If descriptor.maximum is present, let m be descriptor.maximum; otherwise, let m be empty.
-
If m is not empty and m < n, throw a
RangeError
exception. -
Let type be the table type {𝗆𝗂𝗇 n, 𝗆𝖺𝗑 m} 𝖺𝗇𝗒𝖿𝗎𝗇𝖼.
-
Let store be the current agent cluster’s associated store.
-
Let (store, tableaddr) be alloc_table(store, type).
-
Set the current agent cluster’s associated store to store.
-
Create a table object from the table address tableaddr and return the result.
grow(d)
method, when invoked, performs the following steps:
-
Let tableaddr be the Table instance’s [[Table]] internal slot.
-
Let initialSize be the length of the Table instance’s [[Values]] internal slot.
-
Let store be the current agent cluster’s associated store.
-
Let result be grow_table(store, tableaddr, d).
-
If result is error, throw a
RangeError
exception.Note: The above exception may happen due to either insufficient memory or an invalid size parameter.
-
Set the current agent cluster’s associated store to store.
-
Append null to the Table instance’s [[Values]] internal slot d − initialSize times.
-
Return initialSize.
length
attribute of Table
returns the length of the table’s [[Values]] internal slot. get(index)
method, when invoked, performs the following steps:
-
Let values be the Table instance’s [[Values]] internal slot.
-
Let size be the length of values.
-
If index ≥ size, throw a
RangeError
exception. -
Return values[index].
set(index, value)
method, when invoked, performs the following steps:
-
Let tableaddr be the Table instance’s [[Table]] internal slot.
-
Let values be the Table instance’s [[Values]] internal slot.
-
If value is null, let elem be an empty function element.
-
Otherwise,
-
If value does not have a [[FunctionAddress]] internal slot, throw a
TypeError
exception. -
Let funcaddr be value.[[FunctionAddress]].
-
-
Let store be the current agent cluster’s associated store.
-
Let store be write_table(store, tableaddr, index, funcaddr).
-
If store is error, throw a
RangeError
exception. -
Set the current agent cluster’s associated store to store.
-
Set values[index] to value.
-
Return undefined.
2.5. Exported Functions
A WebAssembly function is made available in JavaScript as an Exported Function. Exported Functions are Built-in Function Objects which are not constructors, and which have a [[FunctionAddress]] internal slot. This slot holds a function address relative to the current agent cluster’s associated store.
-
Let store be the current agent cluster’s associated store.
-
Let funcinst be store.𝖿𝗎𝗇𝖼𝗌[funcaddr].
-
If funcinst is of the form {𝗍𝗒𝗉𝖾 functype, 𝗁𝗈𝗌𝗍𝖼𝗈𝖽𝖾 hostfunc},
-
Assert: hostfunc is a JavaScript object and IsCallable(hostfunc) is true.
-
Let name be ? Get(hostfunc, "name").
-
Return ? ToString(name).
-
Otherwise,
-
Let moduleinst be funcinst.𝗆𝗈𝖽𝗎𝗅𝖾.
-
Assert: funcaddr is contained in moduleinst.𝖿𝗎𝗇𝖼𝖺𝖽𝖽𝗋𝗌.
-
Let index be the index of moduleinst.𝖿𝗎𝗇𝖼𝖺𝖽𝖽𝗋𝗌 where funcaddr is found.
-
Return ! ToString(index).
-
Let steps be "call the Exported Function funcaddr with arguments."
-
Let realm be the current Realm.
-
Let function be CreateBuiltinFunction(realm, steps, %FunctionPrototype%, « [[FunctionAddress]]).
-
Set function.[[FunctionAddress]] to funcaddr.
-
Let store be the current agent cluster’s associated store.
-
Let functype be type_func(store, funcaddr).
-
Let [arguments] → [results] be functype.
-
Let arity be the length of arguments.
-
Perform ! DefinePropertyOrThrow(function,
"length"
, PropertyDescriptor {[[Value]]: arity, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true}). -
Let name be the name of the WebAssembly function funcaddr.
-
Perform ! SetFunctionName(function, ! ToString(index)).
-
Return function.
-
Let store be the current agent cluster’s associated store.
-
Let functype be type_func(store, funcaddr).
-
Let [parameters] → [results] be functype.
-
If parameters or results contains an i64, throw a
TypeError
.Note: the above error is thrown each time the [[Call]] method is invoked.
-
Let args be an empty list of WebAssembly values.
-
Let i be 0.
-
For each type t of parameters,
-
If the length of argValues > i, let arg be argValues[i].
-
Otherwise, let arg be undefined.
-
Append ToWebAssemblyValue(arg, t) to args.
-
-
Let (store, ret) be the result of invoke_func(store, funcaddr, args).
-
Set the current agent cluster’s associated store to store.
-
If ret is error, throw an exception. This exception should be a WebAssembly
RuntimeError
exception, unless otherwise indicated by the WebAssembly error mapping. -
If outArity is 0, return undefined.
-
Otherwise, return ToJSValue(v), where v is the singular element of ret.
Note: Calling an Exported Function executes in the [[Realm]] of the callee Exported Function, as per the definition of built-in function objects.
Note: Exported Functions do not have a [[Construct]] method and thus it is not possible to call one with the new
operator.
-
Let hostfunc be a host function which performs the following steps when called:
-
If the signature contains an i64 (as argument or result), the host function throws a
TypeError
when called. -
Let arguments be a list of the arguments of the invocation of this function.
-
Let jsArguments be an empty list.
-
For each arg in arguments,
-
Let ret be ? Call(func, undefined, jsArguments). If an exception is thrown, trigger a WebAssembly trap, and propagate the exception to the enclosing JavaScript.
-
Let [parameters] → [results] be functype.
-
If results is empty, return undefined.
-
Otherwise, return ToWebAssemblyValue(ret, results[0]).
-
-
Let store be the current agent cluster’s associated store.
-
Let (store, funcaddr) be alloc_func(store, functype, hostfunc).
-
Set the current agent cluster’s associated store to store.
-
Return funcaddr
Assert: w is not of the form 𝗂𝟨𝟦.𝖼𝗈𝗇𝗌𝗍 i64.
-
If w is of the form 𝗂𝟥𝟤.𝖼𝗈𝗇𝗌𝗍 i32, return the Number value for i32.
-
If w is of the form 𝖿𝟥𝟤.𝖼𝗈𝗇𝗌𝗍 f32, return the Number value for f32.
-
If w is of the form 𝖿𝟨𝟦.𝖼𝗈𝗇𝗌𝗍 f64, return the Number value for f64.
Note: Implementations may optionally replace the NaN payload with any other NaN payload at this point in the f32 or f64 cases; such a change would not be observable through NumberToRawBytes.
Assert: type is not 𝗂𝟨𝟦.𝖼𝗈𝗇𝗌𝗍.
2.6. Error Objects
WebAssembly defines three Error classes. WebAssembly errors have the following custom bindings:
-
Unlike normal interface types, the interface prototype object for these exception classes must have as its [[Prototype]] the intrinsic object %ErrorPrototype%.
-
The constructor and properties of WebAssembly errors is as specified for
NativeError
. -
If an implementation gives native Error objects special powers or nonstandard properties (such as a stack property), it should also expose those on these exception instances.
[LegacyNamespace=WebAssembly] interface
CompileError
{ }; [LegacyNamespace=WebAssembly] interfaceLinkError
{ }; [LegacyNamespace=WebAssembly] interfaceRuntimeError
{ };
3. Error Condition Mappings to JavaScript
Running WebAssembly programs encounter certain events which halt execution of the WebAssembly code. WebAssembly code (currently) has no way to catch these conditions and thus an exception will necessarily propagate to the enclosing non-WebAssembly caller (whether it is a browser, JavaScript or another runtime system) where it is handled like a normal JavaScript exception.
If WebAssembly calls JavaScript via import and the JavaScript throws an exception, the exception is propagated through the WebAssembly activation to the enclosing caller.
Because JavaScript exceptions can be handled, and JavaScript can continue to call WebAssembly exports after a trap has been handled, traps do not, in general, prevent future execution.
3.1. Stack Overflow
Whenever a stack overflow occurs in WebAssembly code, the same class of exception is thrown as for a stack overflow in JavaScript. The particular exception here is implementation-defined in both cases.
Note: ECMAScript doesn’t specify any sort of behavior on stack overflow; implementations have been observed to throw RangeError
, InternalError or Error. Any is valid here.
3.2. Out of Memory
Whenever validation, compilation or instantiation run out of memory, the same class of exception is thrown as for out of memory conditions in JavaScript. The particular exception here is implementation-defined in both cases.
Note: ECMAScript doesn’t specify any sort of behavior on out-of-memory conditions; implementations have been observed to throw OOMError and to crash. Either is valid here.
4. Interaction of the WebAssembly Store with JavaScript
Note: WebAssembly semantics are defined in terms of a store, representing the state of the world, or "memory". WebAssembly operations take a store and return a new store.
Each agent cluster has an associated store. When a new agent cluster is created, its associated store is set to the result of init_store().
Elements of the WebAssembly store may be identified with JavaScript values. In particular, each WebAssembly memory instance with a corresponding Memory
object is identified with a JavaScript Data Block; modifications to this Data Block are identified to updating the agent cluster’s store to a store which reflects those changes, and vice versa.