Typed arrays are views over bytes.
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var bytes = engine.Evaluate( "new Uint8Array(2)").AsObject!; engine.SetProperty(bytes, "0", JsValue.FromNumber(42)); Console.WriteLine( engine.GetProperty(bytes, "0").AsNumber);
42Unzip the example, then dotnet run.
Full program
The input bytes are 16, 32, 48, 64. C# constructs the buffer and typed array, JavaScript changes one byte, and C# copies the values into an output array. The example also reads a two-byte window in both byte orders.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var realm = engine.MainRealm; var buffer = realm.Construct(realm.Get(realm.Global, "ArrayBuffer"), JsValue.FromNumber(4)); var bytes = realm.Construct(realm.Get(realm.Global, "Uint8Array"), buffer); byte[] input = [16, 32, 48, 64]; for (int index = 0; index < input.Length; index++) realm.Set(bytes.AsObject!, index.ToString(), JsValue.FromNumber(input[index])); realm.DefineData(realm.Global, "bytes", bytes); engine.Evaluate("bytes[2] = 51"); byte[] output = new byte[input.Length]; for (int index = 0; index < output.Length; index++) output[index] = (byte)realm.Get(bytes.AsObject!, JsValue.FromNumber(index)).AsNumber; Console.WriteLine("round trip: " + string.Join(",", output)); engine.Evaluate("globalThis.window = new Uint8Array(bytes.buffer, 1, 2)"); Console.WriteLine("window: " + engine.Evaluate("window.byteOffset + ' / ' + window.length").AsString); engine.Evaluate("globalThis.view = new DataView(bytes.buffer, 1, 2)"); Console.WriteLine("big-endian: " + engine.Evaluate("view.getUint16(0, false)").AsNumber); Console.WriteLine("little-endian: " + engine.Evaluate("view.getUint16(0, true)").AsNumber); try { engine.Evaluate("view.getUint16(1)"); } catch (JsException error) { Console.WriteLine(error.Message); }
dotnet run prints:
round trip: 16,32,51,64 window: 1 / 2 big-endian: 8243 little-endian: 13088 RangeError: Offset is outside the bounds of the DataView
Reading beyond the DataView throws JsException carrying the displayed JavaScript RangeError.
Variations
Copy only a typed array’s visible window, not the whole underlying buffer:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var result = engine.Evaluate(""" const all = new Uint8Array([10, 20, 30, 40]); const part = new Uint8Array(all.buffer, 1, 2); part.slice().toHex() """); Console.WriteLine(result.AsString);
141eRelated recipes
The short answer
Create a JavaScript Uint8Array, expose or retrieve it with JsRealm, and use the public property operations to write and read numeric elements. The snippet at the top creates a typed array in JavaScript and writes and reads an element through C#. The full program goes further: it fills a buffer from a C# byte array, lets JavaScript change it and copies the resulting elements back into a new C# array.
This is explicit element-by-element interop. It is not a zero-copy mapping of a C# byte[] into JavaScript memory. Constructing an ArrayBuffer and Uint8Array through realm.Construct uses the same JavaScript constructors available to script, without requiring a special native buffer API.
How do you pass a JavaScript typed array to C#?
Keep the JsValue returned by construction or evaluation. Its AsObject reference can be used with realm.Get to read properties, including numeric element keys. The full program uses numeric JsValue keys when reading and string property names when writing; both address typed-array elements through the public property API.
Read each element as a JavaScript number, then convert it to a C# byte. This sample uses a Uint8Array, whose elements are unsigned bytes, so the conversion matches the input type. Don’t apply the same cast indiscriminately to a floating-point or signed typed array: choose the C# representation to match the JavaScript element type.
The output differs from the input at the element JavaScript changed. That demonstrates both directions without JSON, base64 or an external input file. For an application payload, preserve its length and element type alongside the values rather than treating every typed array as an untyped sequence of bytes.
How do you convert an ArrayBuffer to a Uint8Array?
Pass the buffer to the Uint8Array constructor. A buffer stores bytes; a typed array supplies the element type and a window into those bytes. The full program constructs the buffer in C#, then passes that value as the constructor argument. Later, JavaScript creates a second view over the same buffer with a byte offset and length.
A view is not necessarily the whole buffer. In the example, the window starts at byte offset one and contains two elements, while the underlying buffer contains four bytes. As the MDN buffer documentation explains, the buffer property returns the underlying buffer, which may be larger than the view.
If you need only the visible bytes, respect the view’s offset and length. The variation uses part.slice() to create a copy of that window before converting it to hex. Sending part.buffer instead would include bytes outside the intended window. Sharing views within JavaScript and copying values across the C# boundary are separate operations.
How do you choose byte order with DataView?
Use DataView for multi-byte fields whose byte order is part of a file or protocol. Pass false for big-endian or true for little-endian to the read operation. The two reads in the full program inspect the same bytes at the same offset; only the byte-order argument changes.
OneEcma’s binary-data documentation states that typed-array element access is little-endian in this engine and DataView uses explicit endianness. Choose the format’s byte order deliberately instead of assuming a C# machine’s byte order determines the result.
Offsets on a DataView read are relative to that view, not the beginning of the underlying buffer. The example’s view contains two bytes, so a two-byte read at offset zero fits, while the same read at offset one runs past the view’s end. The final caught RangeError demonstrates that boundary. Validate a payload’s field lengths before reading them; having a larger underlying buffer does not enlarge a shorter view.