@onda-lang/processor-abi 0.8.6 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -81,10 +81,11 @@ time and reuse the payload and workspace regions.
81
81
  ## Delegate batches
82
82
 
83
83
  The package exports `DELEGATE_RECORD_HEADER_SIZE_BYTES`, `DELEGATE_BATCH_SIZE_BYTES`,
84
- `writeDelegateBatch()`, `readDelegateBatch()`, and `decodeDelegateRecords()` for allocation-free
85
- call-scoped delegate collection. Descriptor entries expose exact fixed payload sizes or dynamic
86
- minimum sizes. A complete fixed record occupies the twelve-byte header (index, payload size, and
87
- call-local output sequence) plus its payload; no exact
84
+ `writeDelegateBatch()`, `readDelegateBatch()`, and `decodeDelegateBatch()` for call-scoped delegate
85
+ collection. Batch preparation and generated publication are allocation-free;
86
+ `decodeDelegateRecords()` remains available for an already-isolated storage region. Descriptor
87
+ entries expose exact fixed payload sizes or dynamic minimum sizes. A complete fixed record occupies
88
+ the twelve-byte header (index, payload size, and call-local output sequence) plus its payload; no exact
88
89
  whole-call capacity exists because occurrence counts and slice lengths may be runtime-dependent.
89
90
 
90
91
  ```js
@@ -93,8 +94,7 @@ import {
93
94
  writeDelegateBatch,
94
95
  writeExecutionOutput,
95
96
  resetExecutionOutput,
96
- readDelegateBatch,
97
- decodeDelegateRecords,
97
+ decodeDelegateBatch,
98
98
  } from "@onda-lang/processor-abi";
99
99
 
100
100
  const delegates = artifact.metadata.metadata.delegates;
@@ -109,15 +109,12 @@ writeExecutionOutput(memory, outputAddress, batchAddress, 0);
109
109
  // Before onda_process (or initialization):
110
110
  resetExecutionOutput(memory, outputAddress);
111
111
  // Pass outputAddress as the final entry argument. onda_event_N resets it before preflight.
112
- const batch = readDelegateBatch(memory, batchAddress);
113
- const storage = new Uint8Array(memory.buffer, storageAddress, batch.usedBytes);
114
- const occurrences = decodeDelegateRecords(
115
- storage,
116
- batch.usedBytes,
117
- delegates,
118
- artifact.metadata.target.byte_order,
112
+ const { occurrences, overflowCount } = decodeDelegateBatch(
113
+ memory,
114
+ batchAddress,
115
+ artifact.metadata,
119
116
  );
120
- if (batch.overflowCount) reportOverflow(batch.overflowCount);
117
+ if (overflowCount) reportOverflow(overflowCount);
121
118
  ```
122
119
 
123
120
  Allocate the descriptor and storage before realtime execution and consume records before the next
package/api.md CHANGED
@@ -121,8 +121,10 @@ versions rather than guessing layout compatibility.
121
121
  `createParamControl(metadata)` validates one scalar parameter descriptor and returns a reusable
122
122
  `OndaPreparedParamControl`. For an array, `createParamControl(metadata, index)` prepares one element;
123
123
  `paramElementMetadata(metadata, index)` projects its scalar descriptor, default, and storage offset.
124
- The Web Audio adapter accepts indexed names such as `offsets[1]` in `setParam` and
125
- `setParamNormalized`, and exact-length arrays in `setParam("offsets", values)`.
124
+ The Web Audio adapter exposes `setParamElement(nameOrIndex, element, value)` and
125
+ `setParamElementNormalized(nameOrIndex, element, normalized)` for individual scalar or fixed-array
126
+ elements. Indexed names such as `offsets[1]` remain accepted by `setParam` and
127
+ `setParamNormalized`; exact-length arrays use `setParam("offsets", values)`.
126
128
  `createParamDomain(domain)` does the same from already-decoded values.
127
129
  Prepared controls expose `constrainPlain`, `normalizedToPlain`, and `plainToNormalized` methods.
128
130
 
@@ -144,7 +146,8 @@ contract.
144
146
  every generated init or process entry. Generated event entries reset before input preflight, so
145
147
  rejected input returns empty batches. After a successful generated call,
146
148
  `readDelegateBatch` or `readPrintBatch` validates the result counters.
147
- `decodeDelegateRecords` converts delegate payloads using `metadata.delegates`.
149
+ `decodeDelegateBatch` validates and decodes a complete delegate batch in one call;
150
+ `decodeDelegateRecords` decodes an already-isolated storage region using `metadata.delegates`.
148
151
  `decodePrintRecords` preserves primitive types and source sites; `formatPrintRecords` and
149
152
  `formatPrintBatch` add canonical text formatting. Both decoded record types expose a `sequence` from
150
153
  the host-reset call-local counter; merge that call's two arrays by this field when presenting one
@@ -186,27 +189,36 @@ The package root re-exports the common artifact validation and file helpers. The
186
189
  - `compileOndaProcessorModule(artifact)` validates and compiles a reusable module off the render thread.
187
190
  - `ondaAudioWorkletNodeOptions(artifact, options?)` builds low-level node options.
188
191
  - `createOndaAudioProcessor(context, artifact, options?)` allocates an uninitialized adapter.
189
- - `createOndaAudioProcessorInitialized(context, artifact, options?)` allocates and fully initializes it.
192
+ - `createOndaAudioProcessorInitialized(context, artifact, options?)` allocates it, requests full
193
+ initialization through the status-bearing control protocol, and resolves only on success.
190
194
  - `flattenedAudioChannelCount(ports?)` totals declared physical channels with validation.
191
195
 
192
196
  `OndaAudioProcessorOptions` accepts initial plain parameter values, external buffers, event,
193
197
  delegate, and print capacities, an optional construction-time `onPrint` listener, a precompiled
194
198
  module, custom node options, and an `AudioWorkletNode` constructor. Pass `onPrint` when using the
195
- initialized constructor if initialization output must be observed; registering a listener after
199
+ initialized factory if initialization output must be observed; registering a listener after
196
200
  construction cannot replay output from an execution that has already completed. The artifact sample
197
201
  rate must equal the context sample rate and it must expose at least one audio input or output.
198
202
  Print and delegate delivery uses a bounded `SharedArrayBuffer` ring and requires cross-origin
199
203
  isolation in browsers; ordinary audio processing remains available when shared memory is absent.
200
204
  Low-level callers that manually pair the returned node options with `OndaAudioProcessor` must pass
201
- `processorOptions.executionOutputRing` as the adapter constructor's fourth argument.
205
+ the artifact metadata as the adapter constructor's second argument and
206
+ `processorOptions.executionOutputRing` as its fourth argument. Event encoding and numeric
207
+ unknown-index classification require that metadata.
202
208
 
203
209
  ### `OndaAudioProcessor`
204
210
 
205
211
  The adapter exposes its `node` and validated `metadata`, plus these operations:
206
212
 
207
- - `setParam(nameOrIndex, plain)` and `setParamNormalized(nameOrIndex, normalized)`.
208
- - `trigger(nameOrIndex, values?)` for input events.
209
- - `onDelegates(listener)` and `onPrint(listener)`, each returning an unsubscribe function.
213
+ - `setParam(nameOrIndex, plain)` and `setParamNormalized(nameOrIndex, normalized)` for whole
214
+ parameters.
215
+ - `setParamElement(nameOrIndex, element, plain)` and
216
+ `setParamElementNormalized(nameOrIndex, element, normalized)` for one scalar or fixed-array
217
+ element.
218
+ - `trigger(nameOrIndex, values?)` for input events. Unknown nonnegative numeric indices are
219
+ successful no-ops, matching native dispatch; unknown names and negative indices are rejected.
220
+ - `onExecutionError(listener)`, `onDelegates(listener)`, and `onPrint(listener)`, each returning an
221
+ unsubscribe function.
210
222
  - `init(mode)`, `snapshot()`, and `restoreSnapshot(bytes)`.
211
223
  - `readControlOutputs()` and `readBuffer(nameOrIndex)`.
212
224
  - `request(type, fields?, transfer?)` for adapter protocol extensions.
@@ -224,7 +236,24 @@ each init, event, or process segment.
224
236
 
225
237
  `ONDA_INIT_FULL` clears and initializes all physical state. `ONDA_INIT_PRESERVE_PINNED` retains
226
238
  pinned state according to the processor ABI. The package also re-exports the prepared and one-shot
227
- parameter conversion helpers.
239
+ parameter conversion helpers. Snapshot restoration performs its internal full initialization
240
+ silently, without delivering init prints or delegates.
241
+
242
+ Raw WebAssembly exports use the same generated status values as native processor objects:
243
+ `PROCESSOR_EXECUTION_OK` (`0`), `PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE` (`1`), and event-only
244
+ `PROCESSOR_EXECUTION_INPUT_REJECTED` (`2`). The Web Audio adapter translates host-side validation
245
+ problems into rejected promises before generated execution. It also rejects init and event promises
246
+ when generated code returns a nonzero status: status `1` invalidates the live processor, while event
247
+ status `2` preserves it. These generated failures use `OndaExecutionError`, whose `operation` and
248
+ `status` fields retain the machine-readable failure. The Web Audio root re-exports all three
249
+ `PROCESSOR_EXECUTION_*` constants for direct comparison. Render-time process failure invalidates the
250
+ processor and silences subsequent callbacks. `onExecutionError(listener)` receives its typed
251
+ `OndaExecutionError`; requested init and event failures remain on their promise rejection path and
252
+ are not reported twice.
253
+ The raw `@onda-lang/processor-abi` package exposes `PROCESSOR_BEGIN_BLOCK`,
254
+ `PROCESSOR_END_BLOCK`, and `PROCESSOR_FULL_BLOCK` for native and direct WebAssembly hosts that use
255
+ segmented processing. Web Audio schedules render quanta internally and has no segmented-process
256
+ method.
228
257
 
229
258
  `@onda-lang/webaudio/worklet` is the side-effect-only AudioWorklet registration module and has no
230
259
  named exports.
@@ -271,8 +300,11 @@ PRINT_RECORD_HEADER_SIZE_BYTES
271
300
  PROCESSOR_ABI_VERSION
272
301
  PROCESSOR_ARTIFACT_FORMAT
273
302
  PROCESSOR_ARTIFACT_FORMAT_VERSION
303
+ PROCESSOR_BEGIN_BLOCK
304
+ PROCESSOR_END_BLOCK
274
305
  PROCESSOR_EXECUTION_OK
275
306
  PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE
307
+ PROCESSOR_FULL_BLOCK
276
308
  PROCESSOR_INIT_FULL
277
309
  PROCESSOR_INIT_PRESERVE_PINNED
278
310
  PROCESSOR_SNAPSHOT_FORMAT_VERSION
@@ -281,6 +313,7 @@ createParamControl
281
313
  paramElementMetadata
282
314
  createParamDomain
283
315
  createProcessorArtifactFiles
316
+ decodeDelegateBatch
284
317
  decodeDelegateRecords
285
318
  decodePrintRecords
286
319
  formatPrintBatch
@@ -320,12 +353,19 @@ OndaArtifactError
320
353
  OndaBinaryenError
321
354
  OndaBinaryenOptions
322
355
  OndaProcessorArtifact
356
+ OndaProcessorInitMode
323
357
  OndaProcessorMetadata
324
358
  PROCESSOR_ABI_VERSION
325
359
  PROCESSOR_ARTIFACT_FORMAT
326
360
  PROCESSOR_ARTIFACT_FORMAT_VERSION
361
+ PROCESSOR_BEGIN_BLOCK
362
+ PROCESSOR_END_BLOCK
363
+ PROCESSOR_EXECUTION_INPUT_REJECTED
327
364
  PROCESSOR_EXECUTION_OK
328
365
  PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE
366
+ PROCESSOR_FULL_BLOCK
367
+ PROCESSOR_INIT_FULL
368
+ PROCESSOR_INIT_PRESERVE_PINNED
329
369
  PROCESSOR_SNAPSHOT_FORMAT_VERSION
330
370
  SUPPORTED_MIR_SCHEMA_VERSION
331
371
  compileTrustedMir
@@ -366,6 +406,7 @@ OndaLspAnalysisOptions
366
406
  OndaLspMessage
367
407
  OndaMaterializedProjectFile
368
408
  OndaProcessorArtifact
409
+ OndaProcessorInitMode
369
410
  OndaProcessorMetadata
370
411
  OndaProjectBufferInfo
371
412
  OndaProjectCapabilities
@@ -383,8 +424,14 @@ OndaWorkerLike
383
424
  PROCESSOR_ABI_VERSION
384
425
  PROCESSOR_ARTIFACT_FORMAT
385
426
  PROCESSOR_ARTIFACT_FORMAT_VERSION
427
+ PROCESSOR_BEGIN_BLOCK
428
+ PROCESSOR_END_BLOCK
429
+ PROCESSOR_EXECUTION_INPUT_REJECTED
386
430
  PROCESSOR_EXECUTION_OK
387
431
  PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE
432
+ PROCESSOR_FULL_BLOCK
433
+ PROCESSOR_INIT_FULL
434
+ PROCESSOR_INIT_PRESERVE_PINNED
388
435
  PROCESSOR_SNAPSHOT_FORMAT_VERSION
389
436
  WorkerCompilerOptions
390
437
  createCompiler
@@ -408,11 +455,16 @@ OndaAudioProcessor
408
455
  OndaAudioProcessorOptions
409
456
  OndaAudioPrintBatch
410
457
  OndaAudioPrintListener
458
+ OndaExecutionError
459
+ OndaExecutionErrorListener
411
460
  OndaInitMode
412
461
  OndaParamDomain
413
462
  OndaPreparedParamControl
414
463
  OndaProcessorArtifact
415
464
  OndaProcessorMetadata
465
+ PROCESSOR_EXECUTION_INPUT_REJECTED
466
+ PROCESSOR_EXECUTION_OK
467
+ PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE
416
468
  compileOndaProcessorModule
417
469
  constrainParamPlain
418
470
  createOndaAudioProcessor
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onda-lang/processor-abi",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "type": "module",
5
5
  "description": "Shared Onda processor artifact schema, validation, and integrity helpers",
6
6
  "license": "MIT",
package/src/index.d.ts CHANGED
@@ -6,6 +6,9 @@ export const PROCESSOR_EXECUTION_OK: 0;
6
6
  export const PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE: 1;
7
7
  export const PROCESSOR_INIT_PRESERVE_PINNED: 0;
8
8
  export const PROCESSOR_INIT_FULL: 1;
9
+ export const PROCESSOR_BEGIN_BLOCK: 1;
10
+ export const PROCESSOR_END_BLOCK: 2;
11
+ export const PROCESSOR_FULL_BLOCK: 3;
9
12
  export type OndaProcessorInitMode = 0 | 1;
10
13
  export const PROCESSOR_SNAPSHOT_FORMAT_VERSION: 1;
11
14
  /** Bytes preceding the payload of every packed delegate occurrence. */
@@ -415,7 +418,7 @@ export function writeDelegateBatch(
415
418
  storageAddress: number,
416
419
  capacityBytes: number,
417
420
  ): void;
418
- /** Read and validate the result descriptor after successful generated execution. */
421
+ /** Read and validate the complete result envelope and packed record layout. */
419
422
  export function readDelegateBatch(
420
423
  memory: WebAssembly.Memory | ArrayBuffer | ArrayBufferView | DataView,
421
424
  batchAddress: number,
@@ -447,6 +450,14 @@ export function decodePrintRecords(
447
450
  logSites: OndaLogSiteMetadata[],
448
451
  byteOrder?: "little_endian" | "big_endian",
449
452
  ): OndaPrintEntry[];
453
+ export function decodeDelegateBatch(
454
+ memory: WebAssembly.Memory | ArrayBuffer | ArrayBufferView | DataView,
455
+ delegateBatchAddress: number,
456
+ metadata: OndaProcessorMetadata | {
457
+ delegates: OndaDelegateMetadata[];
458
+ target?: OndaTargetInfo;
459
+ },
460
+ ): { occurrences: OndaDelegateOccurrence[]; overflowCount: number };
450
461
  export function formatPrintBatch(
451
462
  memory: WebAssembly.Memory,
452
463
  printBatchAddress: number,
package/src/index.js CHANGED
@@ -12,10 +12,14 @@ export const PROCESSOR_EXECUTION_OK = 0;
12
12
  export const PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE = 1;
13
13
  export const PROCESSOR_INIT_PRESERVE_PINNED = 0;
14
14
  export const PROCESSOR_INIT_FULL = 1;
15
+ export const PROCESSOR_BEGIN_BLOCK = 1 << 0;
16
+ export const PROCESSOR_END_BLOCK = 1 << 1;
17
+ export const PROCESSOR_FULL_BLOCK = PROCESSOR_BEGIN_BLOCK | PROCESSOR_END_BLOCK;
15
18
  export const PROCESSOR_SNAPSHOT_FORMAT_VERSION = 1;
16
- export const DELEGATE_RECORD_HEADER_SIZE_BYTES = 12;
19
+ const BATCH_RECORD_HEADER_SIZE_BYTES = 12;
20
+ export const DELEGATE_RECORD_HEADER_SIZE_BYTES = BATCH_RECORD_HEADER_SIZE_BYTES;
17
21
  export const DELEGATE_BATCH_SIZE_BYTES = 20;
18
- export const PRINT_RECORD_HEADER_SIZE_BYTES = 12;
22
+ export const PRINT_RECORD_HEADER_SIZE_BYTES = BATCH_RECORD_HEADER_SIZE_BYTES;
19
23
  export const PRINT_BATCH_SIZE_BYTES = 20;
20
24
  export const EXECUTION_OUTPUT_SIZE_BYTES = 12;
21
25
 
@@ -939,6 +943,9 @@ export function writeDelegateBatch(
939
943
  if (storageAddress > 0xffff_ffff || capacityBytes > 0xffff_ffff) {
940
944
  throw new OndaArtifactError("delegate batch addresses and sizes must fit u32");
941
945
  }
946
+ if (storageAddress !== 0 && capacityBytes !== 0) {
947
+ requireMemoryRange(view, storageAddress, capacityBytes, "delegate batch storage");
948
+ }
942
949
  view.setUint32(batchAddress, storageAddress, true);
943
950
  view.setUint32(batchAddress + 4, capacityBytes, true);
944
951
  view.setUint32(batchAddress + 8, 0, true);
@@ -947,15 +954,12 @@ export function writeDelegateBatch(
947
954
  }
948
955
 
949
956
  export function readDelegateBatch(memory, batchAddress) {
950
- const view = writableDataView(memory);
951
- requireMemoryRange(view, batchAddress, DELEGATE_BATCH_SIZE_BYTES, "delegate batch");
952
- return {
953
- storageAddress: view.getUint32(batchAddress, true),
954
- capacityBytes: view.getUint32(batchAddress + 4, true),
955
- usedBytes: view.getUint32(batchAddress + 8, true),
956
- recordCount: view.getUint32(batchAddress + 12, true),
957
- overflowCount: view.getUint32(batchAddress + 16, true),
958
- };
957
+ return readRecordBatch(
958
+ memory,
959
+ batchAddress,
960
+ DELEGATE_BATCH_SIZE_BYTES,
961
+ "delegate batch",
962
+ );
959
963
  }
960
964
 
961
965
  export function writePrintBatch(memory, batchAddress, storageAddress, capacityBytes) {
@@ -966,6 +970,9 @@ export function writePrintBatch(memory, batchAddress, storageAddress, capacityBy
966
970
  if (storageAddress > 0xffff_ffff || capacityBytes > 0xffff_ffff) {
967
971
  throw new OndaArtifactError("print batch addresses and sizes must fit u32");
968
972
  }
973
+ if (storageAddress !== 0 && capacityBytes !== 0) {
974
+ requireMemoryRange(view, storageAddress, capacityBytes, "print batch storage");
975
+ }
969
976
  view.setUint32(batchAddress, storageAddress, true);
970
977
  view.setUint32(batchAddress + 4, capacityBytes, true);
971
978
  view.setUint32(batchAddress + 8, 0, true);
@@ -974,15 +981,65 @@ export function writePrintBatch(memory, batchAddress, storageAddress, capacityBy
974
981
  }
975
982
 
976
983
  export function readPrintBatch(memory, batchAddress) {
984
+ return readRecordBatch(
985
+ memory,
986
+ batchAddress,
987
+ PRINT_BATCH_SIZE_BYTES,
988
+ "print batch",
989
+ );
990
+ }
991
+
992
+ function readRecordBatch(memory, batchAddress, descriptorSize, label) {
977
993
  const view = writableDataView(memory);
978
- requireMemoryRange(view, batchAddress, PRINT_BATCH_SIZE_BYTES, "print batch");
979
- return {
994
+ requireMemoryRange(view, batchAddress, descriptorSize, label);
995
+ const batch = {
980
996
  storageAddress: view.getUint32(batchAddress, true),
981
997
  capacityBytes: view.getUint32(batchAddress + 4, true),
982
998
  usedBytes: view.getUint32(batchAddress + 8, true),
983
999
  recordCount: view.getUint32(batchAddress + 12, true),
984
1000
  overflowCount: view.getUint32(batchAddress + 16, true),
985
1001
  };
1002
+ if (batch.usedBytes > batch.capacityBytes) {
1003
+ throw new OndaArtifactError(`${label} usedBytes exceeds capacityBytes`);
1004
+ }
1005
+ if (batch.recordCount !== 0 && batch.storageAddress === 0) {
1006
+ throw new OndaArtifactError(`${label} has records without storage`);
1007
+ }
1008
+ if (batch.storageAddress !== 0 && batch.capacityBytes !== 0) {
1009
+ requireMemoryRange(
1010
+ view,
1011
+ batch.storageAddress,
1012
+ batch.capacityBytes,
1013
+ `${label} storage`,
1014
+ );
1015
+ }
1016
+ if ((batch.recordCount === 0) !== (batch.usedBytes === 0)) {
1017
+ throw new OndaArtifactError(`${label} recordCount does not match packed storage`);
1018
+ }
1019
+ validateBatchRecordLayout(view, batch, label);
1020
+ return batch;
1021
+ }
1022
+
1023
+ function validateBatchRecordLayout(view, batch, label) {
1024
+ if (batch.recordCount > Math.floor(batch.usedBytes / BATCH_RECORD_HEADER_SIZE_BYTES)) {
1025
+ throw new OndaArtifactError(`${label} recordCount exceeds packed storage`);
1026
+ }
1027
+ let cursor = batch.storageAddress;
1028
+ const end = cursor + batch.usedBytes;
1029
+ for (let record = 0; record < batch.recordCount; record += 1) {
1030
+ if (end - cursor < BATCH_RECORD_HEADER_SIZE_BYTES) {
1031
+ throw new OndaArtifactError(`${label} ends in a partial record header`);
1032
+ }
1033
+ const payloadBytes = view.getUint32(cursor + 4, true);
1034
+ cursor += BATCH_RECORD_HEADER_SIZE_BYTES;
1035
+ if (payloadBytes > end - cursor) {
1036
+ throw new OndaArtifactError(`${label} ends in a partial record payload`);
1037
+ }
1038
+ cursor += payloadBytes;
1039
+ }
1040
+ if (cursor !== end) {
1041
+ throw new OndaArtifactError(`${label} contains trailing record bytes`);
1042
+ }
986
1043
  }
987
1044
 
988
1045
  export function writeExecutionOutput(
@@ -1184,13 +1241,7 @@ function escapedPrintLabel(label) {
1184
1241
 
1185
1242
  export function formatPrintBatch(memory, printBatchAddress, metadata) {
1186
1243
  const batch = readPrintBatch(memory, printBatchAddress);
1187
- const bytes = asUint8Array(memory.buffer ?? memory);
1188
- if (batch.usedBytes > batch.capacityBytes) {
1189
- throw new OndaArtifactError("print batch usedBytes exceeds capacityBytes");
1190
- }
1191
- if (batch.storageAddress + batch.usedBytes > bytes.byteLength) {
1192
- throw new OndaArtifactError("print batch storage exceeds WebAssembly memory");
1193
- }
1244
+ const bytes = memoryBytes(memory);
1194
1245
  const formatted = formatPrintRecords(
1195
1246
  bytes.subarray(batch.storageAddress, batch.storageAddress + batch.usedBytes),
1196
1247
  batch.usedBytes,
@@ -1203,6 +1254,21 @@ export function formatPrintBatch(memory, printBatchAddress, metadata) {
1203
1254
  return formatted;
1204
1255
  }
1205
1256
 
1257
+ export function decodeDelegateBatch(memory, delegateBatchAddress, metadata) {
1258
+ const batch = readDelegateBatch(memory, delegateBatchAddress);
1259
+ const bytes = memoryBytes(memory);
1260
+ const occurrences = decodeDelegateRecords(
1261
+ bytes.subarray(batch.storageAddress, batch.storageAddress + batch.usedBytes),
1262
+ batch.usedBytes,
1263
+ metadata.metadata?.delegates ?? metadata.delegates,
1264
+ metadata.target?.byte_order ?? "little_endian",
1265
+ );
1266
+ if (occurrences.length !== batch.recordCount) {
1267
+ throw new OndaArtifactError("delegate batch recordCount does not match packed storage");
1268
+ }
1269
+ return { occurrences, overflowCount: batch.overflowCount };
1270
+ }
1271
+
1206
1272
  export function formatPrintRecords(storage, usedBytes, metadata, overflowCount = 0) {
1207
1273
  requireInteger(overflowCount, "overflowCount", 0);
1208
1274
  const records = decodePrintRecords(
@@ -1307,9 +1373,14 @@ function writableDataView(memory) {
1307
1373
  throw new OndaArtifactError("delegate batch memory must be WebAssembly memory or a buffer view");
1308
1374
  }
1309
1375
 
1376
+ function memoryBytes(memory) {
1377
+ return asUint8Array(memory instanceof WebAssembly.Memory ? memory.buffer : memory);
1378
+ }
1379
+
1310
1380
  function requireMemoryRange(view, address, size, label) {
1311
1381
  requireInteger(address, `${label} address`, 0);
1312
- if (address + size > view.byteLength) {
1382
+ requireInteger(size, `${label} size`, 0);
1383
+ if (address > view.byteLength || size > view.byteLength - address) {
1313
1384
  throw new OndaArtifactError(`${label} exceeds memory`);
1314
1385
  }
1315
1386
  }