recursal/MDN huggingface dataset:
Extend
======

The **`extend`** instructions, are used to convert (extend) numbers of type `i32` to type `i64`. There are signed and unsigned versions of this instruction.

Try it
------

Syntax
------


```wasm
;; push an i32 onto the stack
i32.const 10

;; sign-extend from i32 to i64
i64.extend_i32_s

;; the top item on the stack will now be the value 10 of type i64
```


| Instruction | Binary opcode |
| --- | --- |
| `i64.extend_i32_s` | `0xac` |
| `i64.extend_i32_u` | `0xad` |
Copy sign
=========

The **`copysign`** instructions, are used to copy just the sign bit from one number to another.

Try it
------

Syntax
------


```wasm
;; load two numbers onto the stack
f32.const 10
f32.const -1

;; copy just the sign bit from the second number (-1) to the first (10)
f32.copysign

;; the top item on the stack will now be -10
```


| Instruction | Binary opcode |
| --- | --- |
| `f32.copysign` | `0x98` |
| `f64.copysign` | `0xa6` |
Demote
======

The **`demote`** instruction, is used to convert (demote) numbers of type `f64` to type `f32`.

Try it
------

Syntax
------


```wasm
;; push an f64 onto the stack
f64.const 10.5

;; demote from f64 to f32
f32.demote_f64

;; the top item on the stack will now be the value 10.5 of type f32
```


| Instruction | Binary opcode |
| --- | --- |
| `f32.demote_f64` | `0xb6` |
Floor
=====

The **`floor`** instructions, are used for getting the value of a number rounded down to the next integer.

**`floor`** differs from **`trunc`** when used on negative numbers, **`floor`** will round down in those cases while **`trunc`** will round up.

Try it
------

Syntax
------


```wasm
;; load a number onto the stack
f32.const -2.7

;; round down
f32.floor

;; the top item on the stack will now be -3
```


| Instruction | Binary opcode |
| --- | --- |
| `f32.floor` | `0x8e` |
| `f64.floor` | `0x9c` |
Truncate (float to float)
=========================

The **`trunc`** instructions, short for *truncate*, are used for getting the value of a number without its fractional part.

**`trunc`** differs from **`floor`** when used on negative numbers, **`floor`** will round down in those cases while **`trunc`** will round up.

There's another **`trunc`** instruction that truncates the fractional part of a floating point and converts it to an integer.

Try it
------

Syntax
------


```wasm
;; load a number onto the stack
f32.const 2.7

;; discard the fractional part (.7)
f32.trunc

;; the top item on the stack will now be 2
```


| Instruction | Binary opcode |
| --- | --- |
| `f32.trunc` | `0x8f` |
| `f64.trunc` | `0x9d` |
Load
====

The **`load`** instructions, are used to load a number from memory onto the stack.

For the integer numbers, you can also load a narrower number from memory and extend it into a wider type, e.g. load an unsigned 8-bit number and convert it into an i32 (**`i32.load8_u`**). These instructions are separate for signed and unsigned numbers.

Try it
------

Syntax
------


```wasm
;; the offset from where to load the number
i32.const 0

;; load the number at position 0
i32.load
```


| Instruction | Binary opcode |
| --- | --- |
| `i32.load` | `0x28` |
| `i64.load` | `0x29` |
| `f32.load` | `0x2a` |
| `f64.load` | `0x2b` |
| `i32.load8_s` | `0x2c` |
| `i32.load8_u` | `0x2d` |
| `i32.load16_s` | `0x2e` |
| `i32.load16_u` | `0x2f` |
| `i64.load8_s` | `0x30` |
| `i64.load8_u` | `0x31` |
| `i64.load16_s` | `0x32` |
| `i64.load16_u` | `0x33` |
| `i64.load32_s` | `0x34` |
| `i64.load32_u` | `0x35` |
Grow
====

The **`grow`** instruction, increases the size of the memory instance by a specified number of pages, each page is sized 64KiB.

The **`grow`** instruction returns previous size of memory, in pages, if the operation was successful, and returns **`-1`** if the operation failed.

Try it
------

Syntax
------


```wasm
;; load the number of memory pages to grow the memory by
i32.const 3

;; grow the memory by 3 pages
memory.grow

;; the top item on the stack will now either be the previous number of pages (success) or `-1` (failure)
```


| Instruction | Binary opcode |
| --- | --- |
| `memory.grow` | `0x40` |
Store
=====

The **`store`** instructions, are used to store a number in memory.

For the integer numbers, you can also store a wide typed number as a narrower number in memory, e.g. store a 32-bit number in an 8-bit slot (**`i32.store8`**). If the number doesn't fit in the narrower number type it will wrap.

Try it
------

Syntax
------


```wasm
;; the offset in memory where to store the number
i32.const 0

;; the number to store
i32.const 20

;; store 20 at position 0
i32.store
```


| Instruction | Binary opcode |
| --- | --- |
| `i32.store` | `0x36` |
| `i64.store` | `0x37` |
| `f32.store` | `0x38` |
| `f64.store` | `0x39` |
| `i32.store8` | `0x3a` |
| `i32.store16` | `0x3b` |
| `i64.store8` | `0x3c` |
| `i64.store16` | `0x3d` |
| `i64.store32` | `0x3e` |
Size
====

The **`size`** instruction, returns the amount of pages the memory instance currently has, each page is sized 64KiB.

Try it
------

Syntax
------


```wasm
;; get the amount of pages the memory has
memory.size
```


| Instruction | Binary opcode |
| --- | --- |
| `memory.size` | `0x3f` |
WebAssembly.Global
==================

A **`WebAssembly.Global`** object represents a global variable instance, accessible from both JavaScript and importable/exportable across one or more `WebAssembly.Module` instances. This allows dynamic linking of multiple modules.

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

`WebAssembly.Global()`

Creates a new `Global` object.

Global instances
----------------

All `Global` instances inherit from the `Global()` constructor's prototype object — this can be modified to affect all `Global` instances.

### Instance properties

`Global.prototype.constructor`

Returns the function that created this object's instance. By default this is the `WebAssembly.Global()` constructor.

`Global.prototype[@@toStringTag]`

The initial value of the @@toStringTag property is the String value "WebAssembly.Global".

`Global.prototype.value`

The value contained inside the global variable — this can be used to directly set and get the global's value.

### Instance methods

`Global.prototype.valueOf()`

Old-style method that returns the value contained inside the global variable.

Examples
--------

### Creating a new Global instance

The following example shows a new global instance being created using the `WebAssembly.Global()` constructor. It is being defined as a mutable `i32` type, with a value of 0.

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).


```js
const output = document.getElementById("output");

function assertEq(msg, got, expected) {
  const result =
    got === expected
      ? `SUCCESS! Got: ${got}<br>`
      : `FAIL!<br>Got: ${got}<br>Expected: ${expected}<br>`;
  output.innerHTML += `Testing ${msg}: ${result}`;
}

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:** You can see the example running live on GitHub; see also the source code.

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

| Specification |
| --- |
| WebAssembly JavaScript Interface # globals |

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
* Import/Export mutable globals proposal
WebAssembly.compileStreaming()
==============================

 The **`WebAssembly.compileStreaming()`** static method compiles a `WebAssembly.Module` directly from a streamed underlying source.
 This function is useful if it is necessary to compile a module before it can be instantiated (otherwise, the `WebAssembly.instantiateStreaming()` function should be used).


**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.compileStreaming(source)
```

### 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 and compile.

### Return value

A `Promise` that resolves to a `WebAssembly.Module` object representing the compiled module.

### Exceptions

* If `source` is not a `Response` or `Promise` resolving to a `Response`, the promise rejects with a `TypeError`.
* If compilation fails, the promise rejects with a `WebAssembly.CompileError`.
* If the `source` is a `Promise` that rejects, the promise rejects with the error.
* If the `source`'s `Result` has an error (e.g. bad MIME type), the promise rejects with an error.
Examples
--------

### Compile streaming

The following example (see our compile-streaming.html demo on GitHub, and view it live also) directly streams a Wasm module from an underlying source then compiles it to a `WebAssembly.Module` object. Because the `compileStreaming()` function accepts a promise for a `Response` object, you can directly pass it a `Promise` from calling `fetch()`, without waiting for the promise to fulfill.


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

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


 The resulting module instance is then instantiated using
 `WebAssembly.instantiate()`, and the exported function invoked.

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

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

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
===============

The **`WebAssembly.Tag`** object defines a *type* of a WebAssembly exception that can be thrown to/from WebAssembly code.

 When creating a `WebAssembly.Exception`, the tag defines the data types and order of the values carried by the exception.
 The same unique tag instance must be used to access the values of the exception (for example, when using `Exception.prototype.getArg()`).

Constructing an instance of `Tag` creates a new unique tag.
 This tag can be passed to a WebAssembly module as a tag import, where it will become a typed tag defined in the *tag section* of the WebAssembly module.
 You can also export a tag defined in a module and use it to inspect exceptions thrown from the module.


**Note:** You can't access the values of an exception with a new tag that just happens to have the same parameters; it's a different tag!
 This ensures that WebAssembly modules can keep exception information internal if required.
 Code can still catch and rethrow exceptions that it does not understand.


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

`WebAssembly.Tag()`

Creates a new `WebAssembly.Tag` object.

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

`Tag.prototype.type()`

Returns the object defining the data-types array for the tag (as set in its constructor).

Examples
--------

This code snippet creates a new `Tag` instance.


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


The snippet below shows how we might pass it to a module **example.wasm** during instantiation, using an "import object".


```js
const importObject = {
  extmod: {
    exttag: tagToImport,
  },
};

WebAssembly.instantiateStreaming(fetch("example.wasm"), importObject).then(
  (obj) => {
    // …
  },
);
```


The WebAssembly module might then import the tag as shown below:


```wasm
(module
  (import "extmod" "exttag" (tag $tagname (param i32 f32))
)
```


If the tag was used to throw an exception that propagated to JavaScript, we could use the tag to inspect its values.

**Note:** There are many alternatives. We could also use the tag to create a `WebAssembly.Exception` and throw that from a function called by WebAssembly.

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

| Specification |
| --- |
| WebAssembly JavaScript Interface: Exception Handling # tags |

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.CompileError
========================

The **`WebAssembly.CompileError`** object indicates an error during WebAssembly decoding or validation.

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

`WebAssembly.CompileError()`

Creates a new `WebAssembly.CompileError` object.

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

`WebAssembly.CompileError.prototype.message`

Error message. Inherited from `Error`.

`WebAssembly.CompileError.prototype.name`

Error name. Inherited from `Error`.

`WebAssembly.CompileError.prototype.cause`

Error cause. Inherited from `Error`.

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

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

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

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

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

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

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

Stack trace. Inherited from `Error`.

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

`WebAssembly.CompileError.prototype.toString()`

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

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-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.Module
==================

A **`WebAssembly.Module`** object contains stateless WebAssembly code that has already been compiled by the browser — this can be efficiently shared with Workers, and instantiated multiple times.

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

`WebAssembly.Module()`

Creates a new `Module` object.

Static methods
--------------

`WebAssembly.Module.customSections()`

Given a `Module` and string, returns a copy of the contents of all custom sections in the module with the given string name.

`WebAssembly.Module.exports()`

Given a `Module`, returns an array containing descriptions of all the declared exports.

`WebAssembly.Module.imports()`

Given a `Module`, returns an array containing descriptions of all the declared imports.

Examples
--------

### Sending a compiled module to a worker

The following example compiles the loaded `simple.wasm` byte code using the `WebAssembly.compileStreaming()` method and sends the resulting `Module` instance to a worker using `postMessage()`.

See the `index-compile.html` source code or view it live.


```js
const worker = new Worker("wasm\_worker.js");

WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>
  worker.postMessage(mod),
);
```


The worker function `wasm_worker.js` defines an import object for the module to use. The function then sets up an event handler to receive the module from the main thread. When the module is received, we create an instance from it using the `WebAssembly.instantiate()` method and invoke an exported function from inside it.


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

onmessage = (e) => {
  console.log("module received from main thread");
  const mod = e.data;

  WebAssembly.instantiate(mod, importObject).then((instance) => {
    instance.exports.exported\_func();
  });
};
```

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

| Specification |
| --- |
| WebAssembly JavaScript Interface # modules |

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.LinkError
=====================

The **`WebAssembly.LinkError`** object indicates an error during module instantiation (besides traps from the start function).

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

`WebAssembly.LinkError()`

Creates a new `WebAssembly.LinkError` object.

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

`WebAssembly.LinkError.prototype.message`

Error message. Inherited from `Error`.

`WebAssembly.LinkError.prototype.name`

Error name. Inherited from `Error`.

`WebAssembly.LinkError.prototype.cause`

Error cause. Inherited from `Error`.

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

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

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

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

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

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

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

Stack trace. Inherited from `Error`.

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

`WebAssembly.LinkError.prototype.toString()`

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

Examples
--------

### Creating a new LinkError instance

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


```js
try {
  throw new WebAssembly.LinkError("Hello", "someFile", 10);
} catch (e) {
  console.log(e instanceof LinkError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "LinkError"
  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-linkerror |
| 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.Instance
====================

A **`WebAssembly.Instance`** object is a stateful, executable instance of a `WebAssembly.Module`. `Instance` objects contain all the Exported WebAssembly functions that allow calling into WebAssembly code from JavaScript.

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

`WebAssembly.Instance()`

Creates a new `Instance` object.

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

`exports`

Returns an object containing as its members all the functions exported from the WebAssembly module instance, to allow them to be accessed and used by JavaScript. Read-only.

Examples
--------

### Synchronously instantiating a WebAssembly module

The `WebAssembly.Instance()` constructor function can be called to synchronously instantiate a given `WebAssembly.Module` object, for example:


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

fetch("simple.wasm")
  .then((response) => response.arrayBuffer())
  .then((bytes) => {
    const mod = new WebAssembly.Module(bytes);
    const instance = new WebAssembly.Instance(mod, importObject);
    instance.exports.exported\_func();
  });
```


The preferred way to get an `Instance` is asynchronously, for example using the `WebAssembly.instantiateStreaming()` function like this:


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

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


This also demonstrates how the `exports` property is used to access exported functions.

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

| Specification |
| --- |
| WebAssembly JavaScript Interface # instances |

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.compile()
=====================

 The **`WebAssembly.compile()`** static method compiles WebAssembly binary code into a `WebAssembly.Module` object.
 This function is useful if it is necessary to compile a module before it can be instantiated (otherwise, the `WebAssembly.instantiate()` function should be used).


**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.compile(bufferSource)
```

### Parameters

`bufferSource`

 A typed array or `ArrayBuffer`
 containing the binary code of the Wasm module you want to compile.


### Return value

 A `Promise` that resolves to a `WebAssembly.Module` object
 representing the compiled module.

### Exceptions

* If `bufferSource` is not a typed array or `ArrayBuffer`,
 the promise rejects with a `TypeError`.
* If compilation fails, the promise rejects with a
 `WebAssembly.CompileError`.
Examples
--------

### Using compile

 The following example compiles the loaded simple.wasm byte code using the
 `compile()` function and then sends it to a worker using postMessage().


```js
const worker = new Worker("wasm\_worker.js");

fetch("simple.wasm")
  .then((response) => response.arrayBuffer())
  .then((bytes) => WebAssembly.compile(bytes))
  .then((mod) => worker.postMessage(mod));
```


**Note:** You'll probably want to use
 `WebAssembly.compileStreaming()` in most cases, as it is more efficient
 than `compile()`.


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

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

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