drop: Wasm data instruction

The data.drop data instruction discards the data contained by a passive data definition, freeing up its memory, after being used in a memory.init.

Note: Active data segments are dropped automatically during module instantiation, and therefore are not available to drop via data.drop.

Try it

(module
  (memory (export "memory") 1)
  (data $greeting "Hello")

  (func (export "init")
    i32.const 0       ;; destination offset in memory
    i32.const 0       ;; offset into the data segment
    i32.const 5       ;; number of bytes to copy
    memory.init $greeting
    data.drop $greeting
  )
)
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}")).then((result) => {
  result.instance.exports.init();
  const memBuffer = result.instance.exports.memory.buffer;
  const memArray = new Uint8Array(memBuffer, 0, 5);
  console.log(new TextDecoder().decode(memArray));
});

In the above example, we define a memory and a data called $greeting that contains the string Hello. We then invoke memory.init to copy the string over to the memory. With this done, the data is no longer needed, so we call data.drop to free up the memory it was using.

Syntax

data.drop identifier
data.drop

The data.drop instruction type. Must always be included first.

identifier

The identifier for the data you want to drop. This can be one of the following:

name

An identifying name set for the data when it was first defined. This must begin with a $ symbol, for example $my_data.

index

The data's index number, for example 0 for the first data in the wasm module, 1 for the second, etc.

Type

[] -> []

Binary encoding

Instruction Binary format Example text => binary
data.drop 0xfc 9:u32 x:dataidx data.drop 0 => 0xfc 0x09 0x00

Specifications

Specification
WebAssembly Core Specification
# -hrefsyntax-instr-memorymathsfdatadropx%E2%91%A0

Browser compatibility

See also