Frontory

Open code, practice, and notes →

Download source with English comments · for reading

WebAssembly · 10 Array Copies and Bounds

Series contents · Technology map

Stage: Memory and Array Boundaries · External example · preview instructions · Storage ID: webassembly-step-10

Goals and learning outcomes

Prerequisites

Prepare the experiment results for Shared storage and Byte order from Unit 9, “Views and Endianness”.

Key concepts and explanations

Set

TypedArray.set copies values into the target memory. The original array may have separate storage.

Bounds

offset+length×4 must fit within memory. Check capacity before increasing the length.

Practice setup

First check the execution scope guidance on the example screen. The following describes the environment needed to extend the assignment in a separate project. The iframe preview is an experiment with WebAssembly.Memory and JS reference computations; do not present it as execution of an actual Wasm module. Practice with actual modules in a separate Node 22+ environment or a controlled local web project. Use kernel.wat·kernel.wasm·benchmark.mjs in the existing src/labs/webassembly-02·03 folders, and rebuild with the official wat2wasm tool when modifying WAT. Preserve CSP·isolation, and record measurements yourself.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. In day-10.mjs in a separate practice project, copy source into Int32Array(memory.buffer,offset,n) and calculate the sum. Refer to the existing Unit 1·14·28 examples only as starting points.
  2. Input conditions: arrays of length 0·4·1003. Baseline: 4 elements. Run under the baseline conditions, then record observations and errors in notes/day-10.md.
  3. Variable: length; changed value: 1003 elements. Run again through the same input path. Deliverable to save: a table of input·memory·result ranges. Hypothesis to check: All inputs fit within the specified range, and the sums match. Compare it with the actual results, and record whether they agree and why they differ.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
length4 elements1003 elementsAll inputs fit within the specified range, and the sums match.

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. Which processing stage does Set affect in this exercise? Input cases: arrays of length 0·4·1003.
  2. Variable: length. Hypothesis to check: All inputs fit within the specified range, and the sums match. Did the actual results agree with the hypothesis? Explain the evidence in terms of Bounds.

Official references

Linked example and execution scope

Example source · Runtime environments and limits

Standalone WAT + Node assertions; iframe CSP blocks Wasm compilation.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Views and Endianness · Next: Memory Grow and View Lifetime

Open code, practice, and notes →