recursal/MDN huggingface dataset:
WebAssembly.RuntimeError
========================

The **`WebAssembly.RuntimeError`** object is the error type that is thrown whenever WebAssembly specifies a trap.

Constructor
-----------

`WebAssembly.RuntimeError()`

Creates a new `WebAssembly.RuntimeError` object.

Instance properties
-------------------

`WebAssembly.RuntimeError.prototype.message`

Error message. Inherited from `Error`.

`WebAssembly.RuntimeError.prototype.name`

Error name. Inherited from `Error`.

`WebAssembly.RuntimeError.prototype.cause`

Error cause. Inherited from `Error`.

`WebAssembly.RuntimeError.prototype.fileName` 
Non-standard

Path to file that raised this error. Inherited from `Error`.

`WebAssembly.RuntimeError.prototype.lineNumber` 
Non-standard

Line number in file that raised this error. Inherited from `Error`.

`WebAssembly.RuntimeError.prototype.columnNumber` 
Non-standard

Column number in line that raised this error. Inherited from `Error`.

`WebAssembly.RuntimeError.prototype.stack` 
Non-standard

Stack trace. Inherited from `Error`.

Instance methods
----------------

`WebAssembly.RuntimeError.prototype.toString()`

Returns a string representing the specified `Error` object. Inherited from `Error`.

Examples
--------

### Creating a new RuntimeError instance

The following snippet creates a new `RuntimeError` instance, and logs its details to the console:


```js
try {
  throw new WebAssembly.RuntimeError("Hello", "someFile", 10);
} catch (e) {
  console.log(e instanceof WebAssembly.RuntimeError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "RuntimeError"
  console.log(e.fileName); // "someFile"
  console.log(e.lineNumber); // 10
  console.log(e.columnNumber); // 0
  console.log(e.stack); // returns the location where the code was run
}
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # exceptiondef-runtimeerror |
| ECMAScript Language Specification # sec-native-error-types-used-in-this-standard |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.validate()
======================

 The **`WebAssembly.validate()`** static method validates a given typed array of WebAssembly binary
 code, returning whether the bytes form a valid Wasm module (`true`) or not
 (`false`).

Syntax
------


```js
WebAssembly.validate(bufferSource)
```

### Parameters

`bufferSource`

 A typed array or ArrayBuffer
 containing WebAssembly binary code to be validated.


### Return value

 A boolean that specifies whether `bufferSource` is valid Wasm code
 (`true`) or not (`false`).

### Exceptions

 If `bufferSource` is not a typed array or ArrayBuffer,
 a `TypeError` is thrown.

Examples
--------

### Using validate

 The following example (see the validate.html source code,
 and see it live too)
 fetches a Wasm module and converts it into a typed array.
 The `validate()` method is then used to check whether the module is valid.


```js
fetch("simple.wasm")
  .then((response) => response.arrayBuffer())
  .then((bytes) => {
    const valid = WebAssembly.validate(bytes);
    console.log(
      `The given bytes are ${valid ? "" : "not "}a valid Wasm module`,
    );
  });
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # dom-webassembly-validate |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.Table
=================

The **`WebAssembly.Table`** object is a JavaScript wrapper object — an array-like structure representing a WebAssembly table, which stores homogeneous references. A table created by JavaScript or in WebAssembly code will be accessible and mutable from both JavaScript and WebAssembly.

**Note:** Tables can currently only store function references, or host references, but this will likely be expanded in the future.

Constructor
-----------

`WebAssembly.Table()`

Creates a new `Table` object.

Instance properties
-------------------

`Table.prototype.length` Read only 

Returns the length of the table, i.e. the number of elements in the table.

Instance methods
----------------

`Table.prototype.get()`

Accessor function — gets the element stored at a given index.

`Table.prototype.grow()`

Increases the size of the `Table` instance by a specified number of elements.

`Table.prototype.set()`

Sets an element stored at a given index to a given value.

Examples
--------

### Creating a new WebAssembly Table instance

The following example (see table2.html source code and live version) creates a new WebAssembly Table instance with an initial size of 2 elements. We then print out the table length and contents of the two indexes (retrieved via `Table.prototype.get()` to show that the length is two and both elements are `null`.


```js
const tbl = new WebAssembly.Table({ initial: 2, element: "anyfunc" });
console.log(tbl.length); // "2"
console.log(tbl.get(0)); // "null"
console.log(tbl.get(1)); // "null"
```


We then create an import object that contains the table:


```js
const importObj = {
  js: { tbl },
};
```


Finally, we load and instantiate a Wasm module (table2.wasm) using the `WebAssembly.instantiateStreaming()` method. The table2.wasm module contains two functions (one that returns 42 and another that returns 83) and stores both into elements 0 and 1 of the imported table (see text representation). So after instantiation, the table still has length 2, but the elements now contain callable Exported WebAssembly Functions which we can call from JS.


```js
WebAssembly.instantiateStreaming(fetch("table2.wasm"), importObject).then(
  (obj) => {
    console.log(tbl.length);
    console.log(tbl.get(0)());
    console.log(tbl.get(1)());
  },
);
```


Note how you've got to include a second function invocation operator at the end of the accessor to actually invoke the referenced function and log the value stored inside it (e.g. `get(0)()` rather than `get(0)`) .

This example shows that we're creating and accessing the table from JavaScript, but the same table is visible and callable inside the Wasm instance too.

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # tables |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.instantiateStreaming()
==================================

 The **`WebAssembly.instantiateStreaming()`** static method compiles
 and instantiates a WebAssembly module directly from a streamed underlying source. This
 is the most efficient, optimized way to load Wasm code.


**Note:** Webpages that have strict Content Security Policy (CSP) might block WebAssembly from compiling and executing modules.
 For more information on allowing WebAssembly compilation and execution, see the script-src CSP.


Syntax
------


```js
WebAssembly.instantiateStreaming(source, importObject)
```

### Parameters

`source`

 A `Response`
 object or a promise that will fulfill with one, representing the underlying source of
 a Wasm module you want to stream, compile, and instantiate.


`importObject` Optional

 An object containing the values to be imported into the newly-created
 `Instance`, such as functions or `WebAssembly.Memory` objects.
 There must be one matching property for each declared import of the compiled module or
 else a
 `WebAssembly.LinkError`
 is thrown.


### Return value

 A `Promise` that resolves to a `ResultObject` which contains two
 fields:

* `module`: A `WebAssembly.Module` object representing the
 compiled WebAssembly module. This `Module` can be instantiated again or
 shared via postMessage().
* `instance`: A `WebAssembly.Instance` object that contains all
 the Exported WebAssembly functions.
### Exceptions

* If either of the parameters are not of the correct type or structure, a
 `TypeError` is thrown.
* If the operation fails, the promise rejects with a
 `WebAssembly.CompileError`, `WebAssembly.LinkError`, or
 `WebAssembly.RuntimeError`, depending on the cause of the failure.
Examples
--------

### Instantiating streaming

 The following example (see our instantiate-streaming.html
 demo on GitHub, and view it live also)
 directly streams a Wasm module from an underlying source then
 compiles and instantiates it, the promise fulfilling with a `ResultObject`.
 Because the `instantiateStreaming()` function accepts a promise for a `Response`
 object, you can directly pass it a `fetch()`
 call, and it will pass the response into the function when it fulfills.


```js
const importObject = { imports: { imported\_func: (arg) => console.log(arg) } };

WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
  (obj) => obj.instance.exports.exported\_func(),
);
```


 The `ResultObject`'s instance member is then accessed, and the contained
 exported function invoked.


**Note:** For this to work, `.wasm` files should be returned
 with an `application/wasm` MIME type by the server.


Specifications
--------------

| Specification |
| --- |
| WebAssembly Web API # dom-webassembly-instantiatestreaming |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.RuntimeError() constructor
======================================

 The **`WebAssembly.RuntimeError()`** constructor creates a new
 WebAssembly `RuntimeError` object — the type that is thrown whenever
 WebAssembly specifies a trap.

Syntax
------


```js
new WebAssembly.RuntimeError()
new WebAssembly.RuntimeError(message)
new WebAssembly.RuntimeError(message, options)
new WebAssembly.RuntimeError(message, fileName)
new WebAssembly.RuntimeError(message, fileName, lineNumber)
```

### Parameters

`message` Optional

Human-readable description of the error.

`options` Optional

An object that has the following properties:

`cause` Optional

 A property indicating the specific cause of the error.
 When catching and re-throwing an error with a more-specific or useful error message, this property can be used to pass the original error.



`fileName` Optional 
Non-standard

The name of the file containing the code that caused the exception.

`lineNumber` Optional 
Non-standard

The line number of the code that caused the exception.

Examples
--------

### Creating a new RuntimeError instance

 The following snippet creates a new `RuntimeError` instance, and logs its
 details to the console:


```js
try {
  throw new WebAssembly.RuntimeError("Hello", "someFile", 10);
} catch (e) {
  console.log(e instanceof WebAssembly.RuntimeError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "RuntimeError"
  console.log(e.fileName); // "someFile"
  console.log(e.lineNumber); // 10
  console.log(e.columnNumber); // 0
  console.log(e.stack); // returns the location where the code was run
}
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # exceptiondef-runtimeerror |
| ECMAScript Language Specification # sec-nativeerror-constructors |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.CompileError() constructor
======================================

 The **`WebAssembly.CompileError()`** constructor creates a new
 WebAssembly `CompileError` object, which indicates an error during
 WebAssembly decoding or validation.

Syntax
------


```js
new WebAssembly.CompileError()
new WebAssembly.CompileError(message)
new WebAssembly.CompileError(message, options)
new WebAssembly.CompileError(message, fileName)
new WebAssembly.CompileError(message, fileName, lineNumber)
```

### Parameters

`message` Optional

Human-readable description of the error.

`options` Optional

An object that has the following properties:

`cause` Optional

 A property indicating the specific cause of the error.
 When catching and re-throwing an error with a more-specific or useful error message, this property can be used to pass the original error.



`fileName` Optional 
Non-standard

The name of the file containing the code that caused the exception.

`lineNumber` Optional 
Non-standard

The line number of the code that caused the exception.

Examples
--------

### Creating a new CompileError instance

 The following snippet creates a new `CompileError` instance, and logs its
 details to the console:


```js
try {
  throw new WebAssembly.CompileError("Hello", "someFile", 10);
} catch (e) {
  console.log(e instanceof CompileError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "CompileError"
  console.log(e.fileName); // "someFile"
  console.log(e.lineNumber); // 10
  console.log(e.columnNumber); // 0
  console.log(e.stack); // returns the location where the code was run
}
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # exceptiondef-compileerror |
| ECMAScript Language Specification # sec-nativeerror-constructors |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.Tag() constructor
=============================

The **`WebAssembly.Tag()`** constructor creates a new `WebAssembly.Tag` object.

Syntax
------


```js
new WebAssembly.Tag(type)
```

### Parameters

`type`

An object that can contain the following members:

`parameters`

An array of data types (`"i32"`, `"i64"`, `"f32"`, `"f64"`, `"v128"`, `"externref"`, `"anyfunc"`).


### Exceptions

`TypeError`

Thrown if at least one of these conditions are met:

* The `type` parameter is not an object.
* The `type.parameters` property is not supplied.
* The `type.parameters` contains an unsupported data type.

Examples
--------

This creates a tag with two values.


```js
const tag = new WebAssembly.Tag({ parameters: ["i32", "i64"] });
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface: Exception Handling # dom-tag-tag |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.Tag.prototype.type()
================================

The **`type()`** prototype method of the `Tag` object can be used to get the sequence of data types associated with the tag.

Syntax
------


```js
type()
```

### Parameters

None

### Return value

An object with a property named `parameters` that references the array of data types associated with this `Tag`.

This is a copy of the `type` object that was originally passed into the `Tag()` constructor.

Examples
--------

 This code snippet creates a tag defining two data types and then exports them using `type()`.
 The result is printed to the console:


```js
const tag = new WebAssembly.Tag({ parameters: ["i32", "i64"] });
console.log(tag.type());

// Console output:
// Object { parameters: (2) […] }
// parameters: Array [ "i32", "i64" ]
// <prototype>: Object { … }
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface: Exception Handling # dom-tag-type |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.Memory.prototype.buffer
===================================

The read-only **`buffer`** prototype property of the `WebAssembly.Memory` object returns the buffer contained in the memory. Depending on whether or not the memory was constructed with `shared: true`, the buffer is either an `ArrayBuffer` or a `SharedArrayBuffer`.

Examples
--------

### Using buffer

The following example (see memory.html on GitHub, and view it live also) fetches and instantiates the loaded memory.wasm bytecode using the `WebAssembly.instantiateStreaming()` function, while importing the memory created in the line above. It then stores some values in that memory, exports a function, and uses the exported function to sum those values.


```js
const memory = new WebAssembly.Memory({
  initial: 10,
  maximum: 100,
});

WebAssembly.instantiateStreaming(fetch("memory.wasm"), {
  js: { mem: memory },
}).then((obj) => {
  const summands = new Uint32Array(memory.buffer);
  for (let i = 0; i < 10; i++) {
    summands[i] = i;
  }
  const sum = obj.instance.exports.accumulate(0, 10);
  console.log(sum);
});
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # dom-memory-buffer |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.Memory.prototype.grow()
===================================

The **`grow()`** prototype method of the `WebAssembly.Memory` object increases the size of the memory instance by a specified number of WebAssembly pages.

Syntax
------


```js
grow(delta)
```

### Parameters

`delta`

The number of WebAssembly pages you want to grow the memory by (each one is 64KiB in size).

### Return value

The previous size of the memory, in units of WebAssembly pages.

### Exceptions

* `RangeError`: If the current size added with `delta` exceeds the Memory instance's maximum size capacity.
Examples
--------

### Using grow

The following example creates a new WebAssembly Memory instance with an initial size of 1 page (64KiB), and a maximum size of 10 pages (640KiB).


```js
const memory = new WebAssembly.Memory({
  initial: 1,
  maximum: 10,
});
```


We can then grow the instance by one page like so:


```js
const bytesPerPage = 64 \* 1024;
console.log(memory.buffer.byteLength / bytesPerPage); // "1"
console.log(memory.grow(1)); // "1"
console.log(memory.buffer.byteLength / bytesPerPage); // "2"
```


Note the return value of `grow()` here is the previous number of WebAssembly pages.

### Detachment upon growing

 Every call to `grow` will detach any references to the old `buffer`, even for `grow(0)`!
 Detachment means that the `ArrayBuffer`'s `byteLength` becomes zero, and it no longer has bytes accessible to JavaScript.
 Accessing the `buffer` property after calling `grow`, will yield an `ArrayBuffer` with the correct length.


```js
const memory = new WebAssembly.Memory({
  initial: 1,
});
const oldMemoryView = new Uint8Array(memory.buffer);
memory.grow(1);
// the array is empty!
console.log(oldMemoryView); // Uint8Array []
```



```js
const memory = new WebAssembly.Memory({
  initial: 1,
});
memory.grow(1);
const currentMemoryView = new Uint8Array(memory.buffer);
// the array is full of zeros
console.log(currentMemoryView); // Uint8Array(131072) [ 0, 0, 0, ... ]
// 131072 = 64KiB \* 2
```


For a shared `Memory` instance, the initial `buffer` (which would be a `SharedArrayBuffer` in such case) will not become detached, but rather its length will not be updated. Accesses to the `buffer` property after growing will yield a larger `SharedArrayBuffer` which may access a larger span of memory than the buffer from before growing the `Memory`. Every `SharedArrayBuffer` from the `buffer` property will all refer to the start of the same memory address range, and thus manipulate the same data.

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # dom-memory-grow |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API
WebAssembly.Module.customSections()
===================================

 The **`WebAssembly.Module.customSections()`** static method returns a copy
 of the contents of all custom sections in the given module with the given string name.

Syntax
------


```js
WebAssembly.Module.customSections(module, sectionName)
```

### Parameters

`module`

 The `WebAssembly.Module` object whose custom sections are being
 considered.


`sectionName`

The string name of the desired custom section.

### Return value

A (possibly empty) array containing `ArrayBuffer` copies of the contents of all custom sections matching `sectionName`.

### Exceptions

 If `module` is not a `WebAssembly.Module` object instance, a
 `TypeError` is thrown.

Description
-----------

 A Wasm module consists of a sequence of **sections**. Most of these
 sections are fully specified and validated by the Wasm spec, but modules can also
 contain **custom sections** that are ignored and skipped over during
 validation. (Read High level structure
 for information on section structures, and how normal sections
 ("known sections") and custom sections are distinguished.)

 This provides developers with a way to include custom data inside Wasm modules for other purposes,
 for example the name custom section,
 which allows developers to provide names for all the functions and
 locals in the module (like "symbols" in a native build).

 Note that the WebAssembly text format currently doesn't have a syntax specified for
 adding new custom sections; you can however add a name section to your Wasm during
 conversion from text format over to Wasm. The `wast2wasm` command available as part of
 the wabt tool has a
 `--debug-names` option — specify this during conversion to get a Wasm with a
 names custom section, for example:


```bash
wast2wasm simple-name-section.was -o simple-name-section.wasm --debug-names
```

Examples
--------

### Using customSections

 The following example uses `WebAssembly.Module.customSections` to check
 if a loaded module instance contains a "name" custom section. A module contains a "name" custom section if `WebAssembly.Module.customSections`
 returns an `ArrayBuffer` with a length greater than 0.

 See custom-section.html source code
 and live example.


```js
WebAssembly.compileStreaming(fetch("simple-name-section.wasm")).then((mod) => {
  const nameSections = WebAssembly.Module.customSections(mod, "name");
  if (nameSections.length !== 0) {
    console.log("Module contains a name section");
    console.log(nameSections[0]);
  }
});
```

Specifications
--------------

| Specification |
| --- |
| WebAssembly JavaScript Interface # dom-module-customsections |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* WebAssembly overview page
* WebAssembly concepts
* Using the WebAssembly JavaScript API