@terminal3/t3n-sdk 3.1.0 → 3.3.0

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.
@@ -1,9 +1,8 @@
1
- "use jco";
1
+ "use components";
2
2
  import { environment, exit as exit$1, stderr } from '@bytecodealliance/preview2-shim/cli';
3
3
  import { error, streams } from '@bytecodealliance/preview2-shim/io';
4
4
  import { random } from '@bytecodealliance/preview2-shim/random';
5
5
  const { getEnvironment } = environment;
6
- getEnvironment._isHostProvided = true;
7
6
 
8
7
  if (getEnvironment=== undefined) {
9
8
  const err = new Error("unexpectedly undefined local import 'getEnvironment', was 'getEnvironment' available at instantiation?");
@@ -11,8 +10,8 @@ if (getEnvironment=== undefined) {
11
10
  throw err;
12
11
  }
13
12
 
13
+ getEnvironment._isHostProvided = true;
14
14
  const { exit } = exit$1;
15
- exit._isHostProvided = true;
16
15
 
17
16
  if (exit=== undefined) {
18
17
  const err = new Error("unexpectedly undefined local import 'exit', was 'exit' available at instantiation?");
@@ -20,8 +19,8 @@ if (exit=== undefined) {
20
19
  throw err;
21
20
  }
22
21
 
22
+ exit._isHostProvided = true;
23
23
  const { getStderr } = stderr;
24
- getStderr._isHostProvided = true;
25
24
 
26
25
  if (getStderr=== undefined) {
27
26
  const err = new Error("unexpectedly undefined local import 'getStderr', was 'getStderr' available at instantiation?");
@@ -29,8 +28,8 @@ if (getStderr=== undefined) {
29
28
  throw err;
30
29
  }
31
30
 
31
+ getStderr._isHostProvided = true;
32
32
  const { Error: Error$1 } = error;
33
- Error$1._isHostProvided = true;
34
33
 
35
34
  if (Error$1=== undefined) {
36
35
  const err = new Error("unexpectedly undefined local import 'Error$1', was 'Error' available at instantiation?");
@@ -38,8 +37,8 @@ if (Error$1=== undefined) {
38
37
  throw err;
39
38
  }
40
39
 
40
+ Error$1._isHostProvided = true;
41
41
  const { OutputStream } = streams;
42
- OutputStream._isHostProvided = true;
43
42
 
44
43
  if (OutputStream=== undefined) {
45
44
  const err = new Error("unexpectedly undefined local import 'OutputStream', was 'OutputStream' available at instantiation?");
@@ -47,8 +46,8 @@ if (OutputStream=== undefined) {
47
46
  throw err;
48
47
  }
49
48
 
49
+ OutputStream._isHostProvided = true;
50
50
  const { getRandomU64 } = random;
51
- getRandomU64._isHostProvided = true;
52
51
 
53
52
  if (getRandomU64=== undefined) {
54
53
  const err = new Error("unexpectedly undefined local import 'getRandomU64', was 'getRandomU64' available at instantiation?");
@@ -56,6 +55,7 @@ if (getRandomU64=== undefined) {
56
55
  throw err;
57
56
  }
58
57
 
58
+ getRandomU64._isHostProvided = true;
59
59
 
60
60
  function promiseWithResolvers() {
61
61
  if (Promise.withResolvers) {
@@ -70,8 +70,9 @@ function promiseWithResolvers() {
70
70
  return { promise, resolve, reject };
71
71
  }
72
72
  }
73
-
74
73
  const symbolDispose = Symbol.dispose || Symbol.for('dispose');
74
+ const symbolAsyncIterator = Symbol.asyncIterator;
75
+ const symbolIterator = Symbol.iterator;
75
76
 
76
77
  const _debugLog = (...args) => {
77
78
  if (!globalThis?.process?.env?.JCO_DEBUG) { return; }
@@ -166,7 +167,7 @@ async function _clearCurrentTask(args) {
166
167
  const { taskID, componentIdx } = args;
167
168
 
168
169
  const meta = CURRENT_TASK_META[componentIdx];
169
- if (!meta) { throw new Error(`missing current task meta for component idx [${componentIdx}]`); }
170
+ if (!meta) { throw new Error(`missing current task meta for component idx [${componentIdx}]n`); }
170
171
 
171
172
  if (meta.taskID !== taskID) {
172
173
  throw new Error(`task ID [${meta.taskID}] != requested ID [${taskID}]`);
@@ -274,13 +275,54 @@ const _coinFlip = () => { return Math.random() > 0.5; };
274
275
  let SCOPE_ID = 0;
275
276
  const I32_MIN = -2_147_483_648;
276
277
  const I32_MAX = 2_147_483_647;
278
+
279
+ function _isValidNumericPrimitive(ty, v) {
280
+ if (v === undefined || v === null) { return false; }
281
+ switch (ty) {
282
+ case 'bool':
283
+ return v === 0 || v === 1;
284
+ break;
285
+ case 'u8':
286
+ return v >= 0 && v <= 255;
287
+ break;
288
+ case 's8':
289
+ return v >= -128 && v <= 127;
290
+ break;
291
+ case 'u16':
292
+ return v >= 0 && v <= 65535;
293
+ break;
294
+ case 's16':
295
+ return v >= -32768 && v <= 32767;
296
+ case 'u32':
297
+ return v >= 0 && v <= 4_294_967_295;
298
+ case 's32':
299
+ return v >= -2_147_483_648 && v <= 2_147_483_647;
300
+ case 'u64':
301
+ return typeof v === 'bigint' && v >= 0 && v <= 18_446_744_073_709_551_615n;
302
+ case 's64':
303
+ return typeof v === 'bigint' && v >= -9223372036854775808n && v <= 9223372036854775807n;
304
+ break;
305
+ case 'f32':
306
+ case 'f64': return typeof v === 'number';
307
+ default:
308
+ return false;
309
+ }
310
+ return true;
311
+ }
312
+
313
+ function _requireValidNumericPrimitive(ty, v) {
314
+ if (v === undefined || v === null || !_isValidNumericPrimitive(ty, v)) {
315
+ throw new TypeError(`invalid ${ty} value [${v}]`);
316
+ }
317
+ return true;
318
+ }
277
319
  const _typeCheckValidI32 = (n) => typeof n === 'number' && n >= I32_MIN && n <= I32_MAX;
278
320
 
279
321
  const _typeCheckAsyncFn= (f) => {
280
322
  return f instanceof ASYNC_FN_CTOR;
281
323
  };
282
324
 
283
- const ASYNC_FN_CTOR = (async () => {}).constructor;
325
+ let RESOURCE_CALL_BORROWS = [];const ASYNC_FN_CTOR = (async () => {}).constructor;
284
326
 
285
327
  function clearCurrentTask(componentIdx, taskID) {
286
328
  _debugLog('[clearCurrentTask()] args', { componentIdx, taskID });
@@ -569,6 +611,7 @@ class AsyncSubtask {
569
611
  realloc,
570
612
  vals: [subtaskValue],
571
613
  storagePtr: resultPtr,
614
+ stringEncoding: callMetadata.stringEncoding,
572
615
  });
573
616
  }
574
617
  }
@@ -758,6 +801,7 @@ resultCountOrAsync,
758
801
  resultPtr,
759
802
  returnFn,
760
803
  startFn,
804
+ stringEncoding,
761
805
  }
762
806
  });
763
807
 
@@ -923,7 +967,8 @@ function _asyncStartCall(args, callee, paramCount, resultCount, flags) {
923
967
  memory: callerMemory,
924
968
  vals: [res],
925
969
  storagePtr: subtaskCallMeta.resultPtr,
926
- componentIdx: callerComponentIdx
970
+ componentIdx: callerComponentIdx,
971
+ stringEncoding: subtaskCallMeta.stringEncoding,
927
972
  });
928
973
 
929
974
  });
@@ -1161,7 +1206,12 @@ class Waitable {
1161
1206
  let dv = new DataView(new ArrayBuffer());
1162
1207
  const dataView = mem => dv.buffer === mem.buffer ? dv : dv = new DataView(mem.buffer);
1163
1208
 
1164
- const toUint64 = val => BigInt.asUintN(64, BigInt(val));
1209
+ function toUint64(val) {
1210
+ const converted = BigInt(val)
1211
+
1212
+ return BigInt.asUintN(64, converted);
1213
+ }
1214
+
1165
1215
  const TEXT_DECODER_UTF8 = new TextDecoder();
1166
1216
  const TEXT_ENCODER_UTF8 = new TextEncoder();
1167
1217
 
@@ -1350,6 +1400,8 @@ class Waitable {
1350
1400
  storage = [0, 0];
1351
1401
  borrowedHandles = {};
1352
1402
 
1403
+ tmpRetI64HighBits = 0|0;
1404
+
1353
1405
  constructor(opts) {
1354
1406
  this.#id = ++AsyncTask._ID;
1355
1407
 
@@ -1938,6 +1990,7 @@ function _lowerImportBackwardsCompat(args) {
1938
1990
  getMemoryFn,
1939
1991
  getReallocFn,
1940
1992
  importFn,
1993
+ stringEncoding,
1941
1994
  } = args;
1942
1995
 
1943
1996
  let meta = _getGlobalCurrentTaskMeta(componentIdx);
@@ -2017,6 +2070,7 @@ function _lowerImportBackwardsCompat(args) {
2017
2070
  realloc: getReallocFn(),
2018
2071
  resultPtr: params[0],
2019
2072
  lowers: resultLowerFns,
2073
+ stringEncoding,
2020
2074
  }
2021
2075
  });
2022
2076
  task.setReturnMemoryIdx(memoryIdx);
@@ -2146,8 +2200,8 @@ function _lowerImportBackwardsCompat(args) {
2146
2200
  return [val, ctx];
2147
2201
  }
2148
2202
 
2149
- if (ctx.storageLen !== undefined && ctx.storageLen < ctx.storagePtr + 1) {
2150
- throw new Error('not enough storage remaining for lift');
2203
+ if (ctx.storageLen !== undefined && ctx.storageLen < 1) {
2204
+ throw new Error(`insufficient storage ([${ctx.storageLen}] bytes) for lift (u8 requires 1 byte)`);
2151
2205
  }
2152
2206
 
2153
2207
  val = new DataView(ctx.memory.buffer).getUint8(ctx.storagePtr, true);
@@ -2158,6 +2212,7 @@ function _lowerImportBackwardsCompat(args) {
2158
2212
  return [val, ctx];
2159
2213
  }
2160
2214
 
2215
+
2161
2216
  function _liftFlatU16(ctx) {
2162
2217
  _debugLog('[_liftFlatU16()] args', { ctx });
2163
2218
  let val;
@@ -2169,8 +2224,8 @@ function _lowerImportBackwardsCompat(args) {
2169
2224
  return [val, ctx];
2170
2225
  }
2171
2226
 
2172
- if (ctx.storageLen !== undefined && ctx.storageLen < ctx.storagePtr + 2) {
2173
- throw new Error('not enough storage remaining for lift');
2227
+ if (ctx.storageLen !== undefined && ctx.storageLen < 2) {
2228
+ throw new Error(`insufficient storage ([${ctx.storageLen}] bytes) for lift (u16 requires 2 bytes)`);
2174
2229
  }
2175
2230
 
2176
2231
  val = new DataView(ctx.memory.buffer).getUint16(ctx.storagePtr, true);
@@ -2184,6 +2239,7 @@ function _lowerImportBackwardsCompat(args) {
2184
2239
  return [val, ctx];
2185
2240
  }
2186
2241
 
2242
+
2187
2243
  function _liftFlatU32(ctx) {
2188
2244
  _debugLog('[_liftFlatU32()] args', { ctx });
2189
2245
  let val;
@@ -2195,8 +2251,8 @@ function _lowerImportBackwardsCompat(args) {
2195
2251
  return [val, ctx];
2196
2252
  }
2197
2253
 
2198
- if (ctx.storageLen !== undefined && ctx.storageLen < ctx.storagePtr + 4) {
2199
- throw new Error('not enough storage remaining for lift');
2254
+ if (ctx.storageLen !== undefined && ctx.storageLen < 4) {
2255
+ throw new Error(`insufficient storage ([${ctx.storageLen}] bytes) for lift (u32 requires 4 bytes)`);
2200
2256
  }
2201
2257
  val = new DataView(ctx.memory.buffer).getUint32(ctx.storagePtr, true);
2202
2258
  ctx.storagePtr += 4;
@@ -2205,6 +2261,7 @@ function _lowerImportBackwardsCompat(args) {
2205
2261
  return [val, ctx];
2206
2262
  }
2207
2263
 
2264
+
2208
2265
  function _liftFlatVariant(casesAndLiftFns) {
2209
2266
  return function _liftFlatVariantInner(ctx) {
2210
2267
  _debugLog('[_liftFlatVariant()] args', { ctx });
@@ -2260,7 +2317,7 @@ function _lowerImportBackwardsCompat(args) {
2260
2317
  }
2261
2318
 
2262
2319
  function _liftFlatList(meta) {
2263
- const { elemLiftFn, align32, knownLen } = meta;
2320
+ const { elemLiftFn, elemSize32, elemAlign32, knownLen } = meta;
2264
2321
 
2265
2322
  const readValuesAndReset = (ctx, originalPtr, dataPtr, len) => {
2266
2323
  ctx.storagePtr = dataPtr;
@@ -2270,37 +2327,40 @@ function _lowerImportBackwardsCompat(args) {
2270
2327
  val.push(res);
2271
2328
  ctx = nextCtx;
2272
2329
 
2273
- const rem = ctx.storagePtr % align32;
2274
- if (rem !== 0) { ctx.storagePtr += align32 - rem; }
2330
+ const rem = ctx.storagePtr % elemAlign32;
2331
+ if (rem !== 0) { ctx.storagePtr += elemAlign32 - rem; }
2275
2332
  }
2276
2333
  if (originalPtr !== null) { ctx.storagePtr = originalPtr; }
2277
2334
  return [val, ctx];
2278
2335
  };
2279
2336
 
2337
+ // TODO(fix): special case for u8/u16/etc into appropriate type
2338
+
2280
2339
  return function _liftFlatListInner(ctx) {
2281
2340
  _debugLog('[_liftFlatList()] args', { ctx });
2282
2341
 
2283
2342
  let liftResults;
2284
- if (knownLen) { // list with known length
2343
+ if (knownLen !== undefined) { // list with known length
2285
2344
 
2286
2345
  if (ctx.useDirectParams) {
2287
2346
  // list with known length w/ direct params
2288
2347
  const dataPtr = ctx.params[0];
2289
2348
  ctx.params = ctx.params.slice(1);
2290
2349
 
2291
- // TODO: is it possible for all values to come in from params?
2350
+ // TODO(???): is it possible for all values to come in from params?
2292
2351
 
2293
2352
  ctx.useDirectParams = false;
2294
2353
  const originalPtr = ctx.storagePtr;
2295
- ctx.storageLen = 8;
2354
+ ctx.storageLen = knownLen * elemSize32;
2296
2355
 
2297
- liftResults = readValuesAndReset(ctx, originalPtr, dataPtr, len);
2356
+ liftResults = readValuesAndReset(ctx, originalPtr, dataPtr, knownLen);
2298
2357
 
2299
2358
  ctx.useDirectParams = true;
2300
2359
  ctx.storagePtr = null;
2301
2360
  ctx.storageLen = null;
2302
2361
 
2303
2362
  } else {
2363
+ ctx.storageLen = knownLen * elemSize32;
2304
2364
  liftResults = readValuesAndReset(ctx, null, ctx.storagePtr, knownLen);
2305
2365
  }
2306
2366
 
@@ -2314,7 +2374,7 @@ function _lowerImportBackwardsCompat(args) {
2314
2374
 
2315
2375
  ctx.useDirectParams = false;
2316
2376
  const originalPtr = ctx.storagePtr;
2317
- ctx.storageLen = 8;
2377
+ ctx.storageLen = len * elemSize32;
2318
2378
 
2319
2379
  liftResults = readValuesAndReset(ctx, originalPtr, dataPtr, len);
2320
2380
 
@@ -2324,6 +2384,8 @@ function _lowerImportBackwardsCompat(args) {
2324
2384
 
2325
2385
  } else {
2326
2386
  // unknown length list ptr w/ in-memory params
2387
+ ctx.storageLen = 8;
2388
+
2327
2389
  const dataPtrLiftRes = _liftFlatU32(ctx);
2328
2390
  const dataPtr = dataPtrLiftRes[0];
2329
2391
  ctx = dataPtrLiftRes[1];
@@ -2335,6 +2397,7 @@ function _lowerImportBackwardsCompat(args) {
2335
2397
  const originalPtr = ctx.storagePtr;
2336
2398
  ctx.storagePtr = dataPtr;
2337
2399
 
2400
+ ctx.storageLen = len * elemSize32;
2338
2401
  liftResults = readValuesAndReset(ctx, originalPtr, dataPtr, len);
2339
2402
  }
2340
2403
  }
@@ -2355,86 +2418,137 @@ function _liftFlatBorrow(componentTableIdx, size, memory, vals, storagePtr, stor
2355
2418
  throw new Error('flat lift for borrowed resources is not supported!');
2356
2419
  }
2357
2420
 
2421
+
2358
2422
  function _lowerFlatU8(ctx) {
2359
2423
  _debugLog('[_lowerFlatU8()] args', ctx);
2360
- const { memory, realloc, vals, storagePtr, storageLen } = ctx;
2361
- if (vals.length !== 1) {
2362
- throw new Error('unexpected number (' + vals.length + ') of core vals (expected 1)');
2424
+
2425
+ if (ctx.vals.length !== 1) {
2426
+ throw new Error(`unexpected number [${ctx.vals.length}] of vals (expected 1)`);
2363
2427
  }
2364
- if (vals[0] > 255 || vals[0] < 0) { throw new Error('invalid value for core value representing u8'); }
2365
- if (!memory) { throw new Error("missing memory for lower"); }
2366
- new DataView(memory.buffer).setUint32(storagePtr, vals[0], true);
2367
2428
 
2368
- // TODO: ALIGNMENT IS WRONG?
2429
+ _requireValidNumericPrimitive.bind('u8', ctx.vals[0]);
2369
2430
 
2370
- return 1;
2431
+ if (!ctx.memory) { throw new Error("missing memory for lower"); }
2432
+ new DataView(ctx.memory.buffer).setUint32(ctx.storagePtr, ctx.vals[0], true);
2433
+
2434
+ ctx.storagePtr += 1;
2371
2435
  }
2372
2436
 
2373
- function _lowerFlatU16(memory, vals, storagePtr, storageLen) {
2374
- _debugLog('[_lowerFlatU16()] args', { memory, vals, storagePtr, storageLen });
2375
- if (vals.length !== 1) {
2376
- throw new Error('unexpected number (' + vals.length + ') of core vals (expected 1)');
2377
- }
2378
- if (vals[0] > 65_535 || vals[0] < 0) { throw new Error('invalid value for core value representing u16'); }
2379
- new DataView(memory.buffer).setUint16(storagePtr, vals[0], true);
2380
- return 2;
2437
+ function _lowerFlatU16(ctx) {
2438
+ _debugLog('[_lowerFlatU16()] args', { ctx });
2439
+
2440
+ if (!ctx.memory) { throw new Error("missing memory for lower"); }
2441
+ if (ctx.vals.length !== 1) {
2442
+ throw new Error(`unexpected number [${ctx.vals.length}] of vals (expected 1)`);
2443
+ }
2444
+
2445
+ const rem = ctx.storagePtr % 2;
2446
+ if (rem !== 0) { ctx.storagePtr += (2 - rem); }
2447
+
2448
+ _requireValidNumericPrimitive.bind('u16', ctx.vals[0]);
2449
+ new DataView(ctx.memory.buffer).setUint16(ctx.storagePtr, ctx.vals[0], true);
2450
+
2451
+ ctx.storagePtr += 2;
2381
2452
  }
2382
2453
 
2383
2454
  function _lowerFlatU32(ctx) {
2384
2455
  _debugLog('[_lowerFlatU32()] args', { ctx });
2385
- const { memory, realloc, vals, storagePtr, storageLen } = ctx;
2386
- if (vals.length !== 1) { throw new Error('expected single value to lower, got (' + vals.length + ')'); }
2387
- if (vals[0] > 4_294_967_295 || vals[0] < 0) { throw new Error('invalid value for core value representing u32'); }
2388
2456
 
2389
- // TODO(refactor): fail loudly on misaligned flat lowers?
2457
+ if (ctx.vals.length !== 1) {
2458
+ throw new Error(`expected single value to lower, got [${ctx.vals.length}]`);
2459
+ }
2460
+
2390
2461
  const rem = ctx.storagePtr % 4;
2391
2462
  if (rem !== 0) { ctx.storagePtr += (4 - rem); }
2392
2463
 
2393
- new DataView(memory.buffer).setUint32(storagePtr, vals[0], true);
2464
+ _requireValidNumericPrimitive.bind('u32', ctx.vals[0]);
2465
+ new DataView(ctx.memory.buffer).setUint32(ctx.storagePtr, ctx.vals[0], true);
2466
+
2467
+ ctx.storagePtr += 4;
2468
+ }
2469
+
2470
+ function _lowerFlatU64(ctx) {
2471
+ _debugLog('[_lowerFlatU64()] args', { ctx });
2472
+
2473
+ if (ctx.vals.length !== 1) { throw new Error('unexpected number of vals'); }
2474
+
2475
+ const rem = ctx.storagePtr % 8;
2476
+ if (rem !== 0) { ctx.storagePtr += (8 - rem); }
2394
2477
 
2395
- return 4;
2478
+ _requireValidNumericPrimitive.bind('u64', ctx.vals[0]);
2479
+ new DataView(ctx.memory.buffer).setBigUint64(ctx.storagePtr, ctx.vals[0], true);
2480
+
2481
+ ctx.storagePtr += 8;
2396
2482
  }
2397
2483
 
2398
- function _lowerFlatU64(memory, vals, storagePtr, storageLen) {
2399
- _debugLog('[_lowerFlatU64()] args', { memory, vals, storagePtr, storageLen });
2400
- if (vals.length !== 1) { throw new Error('unexpected number of core vals'); }
2401
- if (vals[0] > 18_446_744_073_709_551_615n || vals[0] < 0n) { throw new Error('invalid value for core value representing u64'); }
2402
- new DataView(memory.buffer).setBigUint64(storagePtr, vals[0], true);
2403
- return 8;
2484
+ function _lowerFlatStringAny(ctx) {
2485
+ switch (ctx.stringEncoding) {
2486
+ case 'utf8':
2487
+ return _lowerFlatStringUTF8(ctx);
2488
+ case 'utf16':
2489
+ return _lowerFlatStringUTF16(ctx);
2490
+ default:
2491
+ throw new Error(`missing/unrecognized/unsupported string encoding [${ctx.stringEncoding}]`);
2492
+ }
2404
2493
  }
2405
2494
 
2406
2495
  function _lowerFlatStringUTF8(ctx) {
2407
2496
  _debugLog('[_lowerFlatStringUTF8()] args', ctx);
2497
+ if (!ctx.realloc) { throw new Error('missing realloc during flat string lower'); }
2408
2498
 
2409
- const { memory, realloc, vals, storagePtr, storageLen } = ctx;
2499
+ const s = ctx.vals[0];
2500
+ const { ptr, codepoints } = _utf8AllocateAndEncode(ctx.vals[0], ctx.realloc, ctx.memory);
2410
2501
 
2411
- const s = vals[0];
2412
- const { ptr, len, codepoints } = _utf8AllocateAndEncode(vals[0], realloc, memory);
2502
+ const view = new DataView(ctx.memory.buffer);
2503
+ view.setUint32(ctx.storagePtr, ptr, true);
2504
+ view.setUint32(ctx.storagePtr + 4, codepoints, true);
2413
2505
 
2414
- const view = new DataView(memory.buffer);
2415
- view.setUint32(storagePtr, ptr, true);
2416
- view.setUint32(storagePtr + 4, codepoints, true);
2506
+ ctx.storagePtr += 8;
2507
+ }
2508
+
2509
+ function _lowerFlatStringUTF16(ctx) {
2510
+ _debugLog('[_lowerFlatStringUTF16()] args', { ctx });
2511
+ if (!ctx.realloc) { throw new Error('missing realloc during flat string lower'); }
2417
2512
 
2418
- return len;
2513
+ const s = ctx.vals[0];
2514
+ const { ptr, len, codepoints } = _utf16AllocateAndEncode(ctx.vals[0], ctx.realloc, ctx.memory);
2515
+
2516
+ const view = new DataView(ctx.memory.buffer);
2517
+ view.setUint32(ctx.storagePtr, ptr, true);
2518
+ view.setUint32(ctx.storagePtr + 4, codepoints, true);
2519
+
2520
+ const bytes = new Uint16Array(ctx.memory.buffer, start, codeUnits);
2521
+ if (ctx.memory.buffer.byteLength < start + bytes.byteLength) {
2522
+ throw new Error('memory out of bounds');
2523
+ }
2524
+ if (ctx.storageLen !== undefined && ctx.storageLen !== bytes.byteLength) {
2525
+ throw new Error(`storage length [${ctx.storageLen}] != [${bytes.byteLength}])`);
2526
+ }
2527
+ new Uint16Array(ctx.memory.buffer, ctx.storagePtr).set(bytes);
2528
+
2529
+ ctx.storagePtr += len;
2419
2530
  }
2420
2531
 
2421
2532
  function _lowerFlatVariant(lowerMetas) {
2533
+ let caseLookup = {};
2534
+ for (const [idx, meta] of lowerMetas.entries()) {
2535
+ let tag = meta[0];
2536
+ caseLookup[tag] = { discriminant: idx, meta };
2537
+ }
2538
+
2422
2539
  return function _lowerFlatVariantInner(ctx) {
2423
- _debugLog('[_lowerFlatVariant()] args', ctx);
2424
-
2425
- const { memory, realloc, vals, storageLen, componentIdx } = ctx;
2426
- let storagePtr = ctx.storagePtr;
2540
+ _debugLog('[_lowerFlatVariant()] args', { ctx });
2427
2541
 
2428
- const { tag, val } = vals[0];
2429
- const disc = lowerMetas.findIndex(m => m[0] === tag);
2430
- if (disc === -1) {
2431
- throw new Error(`invalid variant tag/discriminant [${tag}] (valid tags: ${variantMetas.map(m => m[0])})`);
2542
+ const { tag, val } = ctx.vals[0];
2543
+ const variantCase = caseLookup[tag];
2544
+ if (!variantCase) {
2545
+ throw new Error(`missing tag [${tag}] (valid tags: ${Object.keys(caseLookup)})`);
2432
2546
  }
2433
2547
 
2434
- const [ _tag, lowerFn, size32, align32, payloadOffset32 ] = lowerMetas[disc];
2548
+ const [ _tag, lowerFn, size32, align32, payloadOffset32 ] = variantCase.meta;
2435
2549
 
2436
- const originalPtr = ctx.resultPtr;
2437
- ctx.vals = [disc];
2550
+ const originalPtr = ctx.storagePtr;
2551
+ ctx.vals = [variantCase.discriminant];
2438
2552
  let discLowerRes;
2439
2553
  if (lowerMetas.length < 256) {
2440
2554
  discLowerRes = _lowerFlatU8(ctx);
@@ -2443,23 +2557,15 @@ function _lowerFlatVariant(lowerMetas) {
2443
2557
  } else if (lowerMetas.length >= 65536 && lowerMetas.length < 4_294_967_296) {
2444
2558
  discLowerRes = _lowerFlatU32(ctx);
2445
2559
  } else {
2446
- throw new Error('unsupported number of cases [' + lowerMetas.legnth + ']');
2560
+ throw new Error(`unsupported number of cases [${lowerMetas.length}]`);
2447
2561
  }
2448
2562
 
2449
- ctx.resultPtr = originalPtr + payloadOffset32;
2563
+ const payloadOffsetPtr = originalPtr + payloadOffset32;
2564
+ ctx.storagePtr = payloadOffsetPtr;
2565
+ ctx.vals = [val];
2566
+ if (lowerFn) { lowerFn(ctx); }
2450
2567
 
2451
- let payloadBytesWritten = 0;
2452
- if (lowerFn) {
2453
- lowerFn({
2454
- memory,
2455
- realloc,
2456
- vals: [val],
2457
- storagePtr,
2458
- storageLen,
2459
- componentIdx,
2460
- });
2461
- }
2462
- let bytesWritten = payloadOffset + payloadBytesWritten;
2568
+ let bytesWritten = ctx.storagePtr - payloadOffsetPtr;
2463
2569
 
2464
2570
  const rem = ctx.storagePtr % align32;
2465
2571
  if (rem !== 0) {
@@ -2468,74 +2574,148 @@ function _lowerFlatVariant(lowerMetas) {
2468
2574
  bytesWritten += pad;
2469
2575
  }
2470
2576
 
2471
- return bytesWritten;
2577
+ ctx.storagePtr += bytesWritten;
2472
2578
  }
2473
2579
  }
2474
2580
 
2475
- function _lowerFlatList(args) {
2476
- const { elemLowerFn } = args;
2581
+ function _lowerFlatList(meta) {
2582
+ const {
2583
+ elemLowerFn,
2584
+ knownLen,
2585
+ size32,
2586
+ align32,
2587
+ elemSize32,
2588
+ elemAlign32,
2589
+ } = meta;
2590
+
2477
2591
  if (!elemLowerFn) { throw new TypeError("missing/invalid element lower fn for list"); }
2478
2592
 
2479
2593
  return function _lowerFlatListInner(ctx) {
2480
2594
  _debugLog('[_lowerFlatList()] args', { ctx });
2481
2595
 
2482
- if (ctx.params.length < 2) { throw new Error('insufficient params left to lower list'); }
2483
- const storagePtr = ctx.params[0];
2484
- const elemCount = ctx.params[1];
2485
- ctx.params = ctx.params.slice(2);
2486
-
2487
2596
  if (ctx.useDirectParams) {
2597
+ if (ctx.params.length < 2) { throw new Error('insufficient params left to lower list'); }
2598
+ const storagePtr = ctx.params[0];
2599
+ const elemCount = ctx.params[1];
2600
+ ctx.params = ctx.params.slice(2);
2601
+
2488
2602
  const list = ctx.vals[0];
2489
2603
  if (!list) { throw new Error("missing direct param value"); }
2490
2604
 
2491
- const elemLowerCtx = { storagePtr, memory: ctx.memory };
2605
+ const lowerCtx = {
2606
+ storagePtr,
2607
+ memory: ctx.memory,
2608
+ stringEncoding: ctx.stringEncoding,
2609
+ };
2492
2610
  for (let idx = 0; idx < list.length; idx++) {
2493
- elemLowerCtx.vals = list.slice(idx, idx+1);
2494
- elemLowerCtx.storagePtr += elemLowerFn(elemLowerCtx);
2611
+ lowerCtx.vals = list.slice(idx, idx+1);
2612
+ elemLowerFn(lowerCtx);
2495
2613
  }
2496
2614
 
2497
- const bytesLowered = elemLowerCtx.storagePtr - ctx.storagePtr;
2498
- ctx.storagePtr = elemLowerCtx.storagePtr;
2499
- return bytesLowered;
2615
+ const bytesLowered = lowerCtx.storagePtr - ctx.storagePtr;
2616
+ ctx.storagePtr = lowerCtx.storagePtr;
2617
+
2618
+ // TODO: implement parma-only known-length processing
2619
+
2620
+ ctx.storagePtr += bytesLowered;
2621
+ return;
2500
2622
  }
2501
2623
 
2502
- if (ctx.vals.length !== 2) {
2503
- throw new Error('indirect parameter loading must have a pointer and length as vals');
2624
+ // TODO(fix): is it possible to get a vals that are a addr and length here from
2625
+ // a component lower?
2626
+
2627
+ const elems = ctx.vals[0];
2628
+ if (knownLen === undefined) {
2629
+ // unknown length
2630
+ if (!ctx.realloc) { throw new Error('missing realloc during flat string lower'); }
2631
+ const dataPtr = ctx.realloc(0, 0, elemAlign32, elemSize32 * elems.length);
2632
+
2633
+ ctx.vals[0] = dataPtr;
2634
+ _lowerFlatU32(ctx);
2635
+
2636
+ ctx.vals[0] = elems.length;
2637
+ _lowerFlatU32(ctx);
2638
+
2639
+ const origPtr = ctx.storagePtr;
2640
+ ctx.storagePtr = dataPtr;
2641
+
2642
+ ctx.storagePtr = dataPtr;
2643
+ for (const elem of elems) {
2644
+ ctx.vals = [elem];
2645
+ elemLowerFn(ctx);
2646
+ }
2647
+
2648
+ ctx.storagePtr = origPtr;
2649
+
2650
+ } else {
2651
+ // known length
2652
+
2653
+ if (elems.length !== knownLen) {
2654
+ throw new TypeError(`invalid list input of length [${elems.length}], must be length [${knownLen}]`);
2655
+ }
2656
+
2657
+ for (const elem of elems) {
2658
+ ctx.vals = [elem];
2659
+ elemLowerFn(ctx);
2660
+ }
2504
2661
  }
2505
- let [valStartPtr, valLen] = ctx.vals;
2506
- const totalSizeBytes = valLen * size;
2662
+
2663
+ // TODO(fix): special case for u8/u16/etc, we can do a direct copy
2664
+
2665
+ const totalSizeBytes = elems.length * size32;
2507
2666
  if (ctx.storageLen !== undefined && totalSizeBytes > ctx.storageLen) {
2508
2667
  throw new Error('not enough storage remaining for list flat lower');
2509
2668
  }
2510
-
2511
- const data = new Uint8Array(memory.buffer, valStartPtr, totalSizeBytes);
2512
- new Uint8Array(memory.buffer, storagePtr, totalSizeBytes).set(data);
2513
-
2514
- return totalSizeBytes;
2515
2669
  }
2516
2670
  }
2517
2671
 
2518
- function _lowerFlatTuple(size, memory, vals, storagePtr, storageLen) {
2519
- _debugLog('[_lowerFlatTuple()] args', { size, memory, vals, storagePtr, storageLen });
2520
- let [start, len] = vals;
2521
- if (storageLen !== undefined && len > storageLen) {
2522
- throw new Error('not enough storage remaining for tuple flat lower');
2523
- }
2524
- const data = new Uint8Array(memory.buffer, start, len);
2525
- new Uint8Array(memory.buffer, storagePtr, len).set(data);
2526
- return data.byteLength;
2672
+ function _lowerFlatTuple(elemLowerMetas) {
2673
+ return function _lowerFlatTupleInner(ctx) {
2674
+ _debugLog('[_lowerFlatTuple()] args', { ctx });
2675
+ const tuple = ctx.vals[0];
2676
+ for (const [idx, [ lowerFn, size32, align32 ]] of elemLowerMetas.entries()) {
2677
+ ctx.vals = [tuple[idx]];
2678
+ lowerFn(ctx);
2679
+ }
2680
+ }
2527
2681
  }
2528
2682
 
2529
2683
  function _lowerFlatResult(lowerMetas) {
2530
2684
  return function _lowerFlatResultInner(ctx) {
2531
2685
  _debugLog('[_lowerFlatResult()] args', { lowerMetas });
2532
- return _lowerFlatVariant(lowerMetas)(ctx);
2686
+
2687
+ const v = ctx.vals[0];
2688
+ const isNotResultObject = typeof v !== 'object'
2689
+ || Object.keys(v).length !== 2
2690
+ || !('tag' in v)
2691
+ || !('ok' === v.tag || 'err' === v.tag)
2692
+ || !('val' in v);
2693
+ if (isNotResultObject) {
2694
+ ctx.vals[0] = { tag: 'ok', val: v };
2695
+ }
2696
+
2697
+ _lowerFlatVariant(lowerMetas)(ctx);
2533
2698
  };
2534
2699
  }
2535
2700
 
2536
- function _lowerFlatOwn(size, memory, vals, storagePtr, storageLen) {
2537
- _debugLog('[_lowerFlatOwn()] args', { size, memory, vals, storagePtr, storageLen });
2538
- throw new Error('flat lower for owned resources not yet implemented!');
2701
+ function _lowerFlatOwn(meta) {
2702
+ const { lowerFn, componentIdx } = meta;
2703
+
2704
+ return function _lowerFlatOwnInner(ctx) {
2705
+ _debugLog('[_lowerFlatOwn()] args', { ctx });
2706
+ const { createFn } = ctx;
2707
+
2708
+ if (ctx.componentIdx !== componentIdx) {
2709
+ throw new Error(`component index mismatch (expected [${componentIdx}], lift called from [${ctx.componentIdx}])`);
2710
+ }
2711
+
2712
+ const obj = ctx.vals[0];
2713
+ if (obj === undefined || obj === null) { throw new Error('missing resource'); }
2714
+ const handle = lowerFn(obj);
2715
+
2716
+ ctx.vals[0] = handle;
2717
+ _lowerFlatU32(ctx);
2718
+ };
2539
2719
  }
2540
2720
 
2541
2721
  const STREAMS = new RepTable({ target: 'global stream map' });
@@ -2882,9 +3062,48 @@ class ComponentAsyncState {
2882
3062
  return new Waitable({ target: args?.target, });
2883
3063
  }
2884
3064
 
3065
+ createReadableStreamEnd(args) {
3066
+ _debugLog('[ComponentAsyncState#createStreamEnd()] args', args);
3067
+ const { tableIdx, elemMeta, hostInjectFn } = args;
3068
+
3069
+ const { table: localStreamTable, componentIdx } = STREAM_TABLES[tableIdx];
3070
+ if (!localStreamTable) {
3071
+ throw new Error(`missing global stream table lookup for table [${tableIdx}] while creating stream`);
3072
+ }
3073
+ if (componentIdx !== this.#componentIdx) {
3074
+ throw new Error('component idx mismatch while creating stream');
3075
+ }
3076
+
3077
+ const waitable = this.createWaitable();
3078
+ const streamEnd = new StreamReadableEnd({
3079
+ tableIdx,
3080
+ elemMeta,
3081
+ hostInjectFn,
3082
+ pendingBufferMeta: {},
3083
+ target: `stream read end (lowered, @init)`,
3084
+ waitable,
3085
+ });
3086
+
3087
+ streamEnd.setWaitableIdx(this.handles.insert(streamEnd));
3088
+ streamEnd.setHandle(localStreamTable.insert(streamEnd));
3089
+ if (streamEnd.streamTableIdx() !== tableIdx) {
3090
+ throw new Error("unexpectedly mismatched stream table");
3091
+ }
3092
+ const streamEndWaitableIdx = streamEnd.waitableIdx();
3093
+ const streamEndHandle = streamEnd.handle();
3094
+ waitable.setTarget(`waitable for stream read end (lowered, waitable [${streamEndWaitableIdx}])`);
3095
+ streamEnd.setTarget(`stream read end (lowered, waitable [${streamEndWaitableIdx}])`);
3096
+
3097
+ return {
3098
+ waitableIdx: streamEndWaitableIdx,
3099
+ handle: streamEndHandle,
3100
+ streamEnd,
3101
+ };
3102
+ }
3103
+
2885
3104
  createStream(args) {
2886
3105
  _debugLog('[ComponentAsyncState#createStream()] args', args);
2887
- const { tableIdx, elemMeta } = args;
3106
+ const { tableIdx, elemMeta, hostInjectFn } = args;
2888
3107
  if (tableIdx === undefined) { throw new Error("missing table idx while adding stream"); }
2889
3108
  if (elemMeta === undefined) { throw new Error("missing element metadata while adding stream"); }
2890
3109
 
@@ -2901,10 +3120,10 @@ class ComponentAsyncState {
2901
3120
 
2902
3121
  const stream = new InternalStream({
2903
3122
  tableIdx,
2904
- componentIdx: this.#componentIdx,
2905
3123
  elemMeta,
2906
3124
  readWaitable,
2907
3125
  writeWaitable,
3126
+ hostInjectFn,
2908
3127
  });
2909
3128
  stream.setGlobalStreamMapRep(STREAMS.insert(stream));
2910
3129
 
@@ -2929,17 +3148,21 @@ class ComponentAsyncState {
2929
3148
  readEnd.setTarget(`stream read end (waitable [${readEndWaitableIdx}])`);
2930
3149
 
2931
3150
  return {
3151
+ writeEnd,
2932
3152
  writeEndWaitableIdx,
2933
3153
  writeEndHandle,
2934
3154
  readEndWaitableIdx,
2935
3155
  readEndHandle,
3156
+ readEnd,
2936
3157
  };
2937
3158
  }
2938
3159
 
2939
3160
  getStreamEnd(args) {
2940
3161
  _debugLog('[ComponentAsyncState#getStreamEnd()] args', args);
2941
3162
  const { tableIdx, streamEndHandle, streamEndWaitableIdx } = args;
2942
- if (tableIdx === undefined) { throw new Error('missing table idx while getting stream end'); }
3163
+ if (tableIdx === undefined) {
3164
+ throw new Error('missing table idx while getting stream end');
3165
+ }
2943
3166
 
2944
3167
  const { table, componentIdx } = STREAM_TABLES[tableIdx];
2945
3168
  const cstate = getOrCreateAsyncState(componentIdx);
@@ -3026,6 +3249,127 @@ class ComponentAsyncState {
3026
3249
 
3027
3250
  return streamEnd;
3028
3251
  }
3252
+
3253
+ createFuture(args) {
3254
+ _debugLog('[ComponentAsyncState#createFuture()] args', args);
3255
+ const { tableIdx, elemMeta, hostInjectFn } = args;
3256
+ if (tableIdx === undefined) { throw new Error("missing table idx while adding future"); }
3257
+ if (elemMeta === undefined) { throw new Error("missing element metadata while adding future"); }
3258
+
3259
+ const { table: futureTable, componentIdx } = FUTURE_TABLES[tableIdx];
3260
+ if (!futureTable) {
3261
+ throw new Error(`missing global future table lookup for table [${tableIdx}] while creating future`);
3262
+ }
3263
+ if (componentIdx !== this.#componentIdx) {
3264
+ throw new Error('component idx mismatch while creating future');
3265
+ }
3266
+
3267
+ const readWaitable = this.createWaitable();
3268
+ const writeWaitable = this.createWaitable();
3269
+
3270
+ const future = new InternalFuture({
3271
+ tableIdx,
3272
+ componentIdx: this.#componentIdx,
3273
+ elemMeta,
3274
+ readWaitable,
3275
+ writeWaitable,
3276
+ hostInjectFn,
3277
+ });
3278
+ future.setGlobalFutureMapRep(FUTURES.insert(future));
3279
+
3280
+ const writeEnd = future.writeEnd();
3281
+ writeEnd.setWaitableIdx(this.handles.insert(writeEnd));
3282
+ writeEnd.setHandle(futureTable.insert(writeEnd));
3283
+ if (writeEnd.futureTableIdx() !== tableIdx) { throw new Error("unexpectedly mismatched future table"); }
3284
+
3285
+ const writeEndWaitableIdx = writeEnd.waitableIdx();
3286
+ const writeEndHandle = writeEnd.handle();
3287
+ writeWaitable.setTarget(`waitable for future write end (waitable [${writeEndWaitableIdx}])`);
3288
+ writeEnd.setTarget(`future write end (waitable [${writeEndWaitableIdx}])`);
3289
+
3290
+ const readEnd = future.readEnd();
3291
+ readEnd.setWaitableIdx(this.handles.insert(readEnd));
3292
+ readEnd.setHandle(futureTable.insert(readEnd));
3293
+ if (readEnd.futureTableIdx() !== tableIdx) { throw new Error("unexpectedly mismatched future table"); }
3294
+
3295
+ const readEndWaitableIdx = readEnd.waitableIdx();
3296
+ const readEndHandle = readEnd.handle();
3297
+ readWaitable.setTarget(`waitable for read end (waitable [${readEndWaitableIdx}])`);
3298
+ readEnd.setTarget(`future read end (waitable [${readEndWaitableIdx}])`);
3299
+
3300
+ return {
3301
+ writeEnd,
3302
+ writeEndWaitableIdx,
3303
+ writeEndHandle,
3304
+ readEndWaitableIdx,
3305
+ readEndHandle,
3306
+ readEnd,
3307
+ };
3308
+ }
3309
+
3310
+ getFutureEnd(args) {
3311
+ _debugLog('[ComponentAsyncState#getFutureEnd()] args', args);
3312
+ const { tableIdx, futureEndHandle, futureEndWaitableIdx } = args;
3313
+ if (tableIdx === undefined) {
3314
+ throw new Error('missing table idx while getting future end');
3315
+ }
3316
+
3317
+ const { table, componentIdx } = FUTURE_TABLES[tableIdx];
3318
+ const cstate = getOrCreateAsyncState(componentIdx);
3319
+
3320
+ let futureEnd;
3321
+ if (futureEndWaitableIdx !== undefined) {
3322
+ futureEnd = cstate.handles.get(futureEndWaitableIdx);
3323
+ } else if (futureEndHandle !== undefined) {
3324
+ if (!table) { throw new Error(`missing/invalid table [${tableIdx}] while getting future end`); }
3325
+ futureEnd = table.get(futureEndHandle);
3326
+ } else {
3327
+ throw new TypeError("must specify either waitable idx or handle to retrieve future");
3328
+ }
3329
+
3330
+ if (!futureEnd) {
3331
+ throw new Error(`missing future end (tableIdx [${tableIdx}], handle [${futureEndHandle}], waitableIdx [${futureEndWaitableIdx}])`);
3332
+ }
3333
+ if (tableIdx && futureEnd.futureTableIdx() !== tableIdx) {
3334
+ throw new Error(`future end table idx [${futureEnd.futureTableIdx()}] does not match [${tableIdx}]`);
3335
+ }
3336
+
3337
+ return futureEnd;
3338
+ }
3339
+
3340
+ removeFutureEndFromTable(args) {
3341
+ _debugLog('[ComponentAsyncState#removeFutureEndFromTable()] args', args);
3342
+
3343
+ const { tableIdx, futureWaitableIdx } = args;
3344
+ if (tableIdx === undefined) { throw new Error("missing table idx while removing future end"); }
3345
+ if (futureWaitableIdx === undefined) {
3346
+ throw new Error("missing future end waitable idx while removing future end");
3347
+ }
3348
+
3349
+ const { table, componentIdx } = FUTURE_TABLES[tableIdx];
3350
+ if (!table) { throw new Error(`missing/invalid table [${tableIdx}] while removing future end`); }
3351
+
3352
+ const cstate = getOrCreateAsyncState(componentIdx);
3353
+
3354
+ const futureEnd = cstate.handles.get(futureWaitableIdx);
3355
+ if (!futureEnd) {
3356
+ throw new Error(`missing future end (handle [${futureWaitableIdx}], table [${tableIdx}])`);
3357
+ }
3358
+ const handle = futureEnd.handle();
3359
+
3360
+ let removed = cstate.handles.remove(futureWaitableIdx);
3361
+ if (!removed) {
3362
+ throw new Error(`failed to remove futureEnd from handles (waitable idx [${futureWaitableIdx}]), component [${componentIdx}])`);
3363
+ }
3364
+
3365
+ removed = table.remove(handle);
3366
+ if (!removed) {
3367
+ throw new Error(`failed to remove futureEnd from table (handle [${handle}]), table [${tableIdx}], component [${componentIdx}])`);
3368
+ }
3369
+
3370
+ return futureEnd;
3371
+ }
3372
+
3029
3373
  }
3030
3374
 
3031
3375
  const base64Compile = str => WebAssembly.compile(typeof Buffer !== 'undefined' ? Buffer.from(str, 'base64') : Uint8Array.from(atob(str), b => b.charCodeAt(0)));
@@ -3062,6 +3406,8 @@ function getErrorPayload(e) {
3062
3406
  return e;
3063
3407
  }
3064
3408
 
3409
+ const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
3410
+
3065
3411
  function throwInvalidBool() {
3066
3412
  throw new TypeError('invalid variant discriminant for bool');
3067
3413
  }
@@ -3181,8 +3527,9 @@ const _trampoline2 = function() {
3181
3527
  fn: () => getStderr()
3182
3528
  })
3183
3529
  ;
3530
+
3184
3531
  if (!(ret instanceof OutputStream)) {
3185
- throw new TypeError('Resource error: Not a valid "OutputStream" resource.');
3532
+ throw new TypeError('Resource error: Not a valid \"OutputStream\" resource.');
3186
3533
  }
3187
3534
  var handle0 = ret[symbolRscHandle];
3188
3535
  if (!handle0) {
@@ -3190,6 +3537,7 @@ const _trampoline2 = function() {
3190
3537
  captureTable1.set(rep, ret);
3191
3538
  handle0 = rscTableCreateOwn(handleTable1, rep);
3192
3539
  }
3540
+
3193
3541
  _debugLog('[iface="wasi:cli/stderr@0.2.9", function="get-stderr"][Instruction::Return]', {
3194
3542
  funcName: 'get-stderr',
3195
3543
  paramCount: 1,
@@ -3374,8 +3722,9 @@ switch (variant6.tag) {
3374
3722
  case 'last-operation-failed': {
3375
3723
  const e = variant5.val;
3376
3724
  dataView(memory0).setInt8(arg3 + 4, 0, true);
3725
+
3377
3726
  if (!(e instanceof Error$1)) {
3378
- throw new TypeError('Resource error: Not a valid "Error" resource.');
3727
+ throw new TypeError('Resource error: Not a valid \"Error\" resource.');
3379
3728
  }
3380
3729
  var handle4 = e[symbolRscHandle];
3381
3730
  if (!handle4) {
@@ -3383,6 +3732,7 @@ switch (variant6.tag) {
3383
3732
  captureTable0.set(rep, e);
3384
3733
  handle4 = rscTableCreateOwn(handleTable0, rep);
3385
3734
  }
3735
+
3386
3736
  dataView(memory0).setInt32(arg3 + 8, handle4, true);
3387
3737
  break;
3388
3738
  }
@@ -3602,13 +3952,14 @@ function next(arg0, arg1) {
3602
3952
  let offset = 0;
3603
3953
  const dv0 = new DataView(memory0.buffer);
3604
3954
  for (const v of val0) {
3955
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
3605
3956
  dv0.setUint8(ptr0+ offset, v, true);
3606
3957
  offset += 1;
3607
3958
  }
3608
3959
  } else {
3609
3960
  // TypedArray / ArrayBuffer-like, direct copy
3610
3961
  valData0 = new Uint8Array(val0.buffer || val0, val0.byteOffset, valLenBytes0);
3611
- const out0 = new Uint8Array(memory0.buffer, ptr0,valLenBytes0);
3962
+ const out0 = new Uint8Array(memory0.buffer, ptr0, valLenBytes0);
3612
3963
  out0.set(valData0);
3613
3964
  }
3614
3965
 
@@ -3627,13 +3978,14 @@ function next(arg0, arg1) {
3627
3978
  let offset = 0;
3628
3979
  const dv2 = new DataView(memory0.buffer);
3629
3980
  for (const v of val2) {
3981
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
3630
3982
  dv2.setUint8(ptr2+ offset, v, true);
3631
3983
  offset += 1;
3632
3984
  }
3633
3985
  } else {
3634
3986
  // TypedArray / ArrayBuffer-like, direct copy
3635
3987
  valData2 = new Uint8Array(val2.buffer || val2, val2.byteOffset, valLenBytes2);
3636
- const out2 = new Uint8Array(memory0.buffer, ptr2,valLenBytes2);
3988
+ const out2 = new Uint8Array(memory0.buffer, ptr2, valLenBytes2);
3637
3989
  out2.set(valData2);
3638
3990
  }
3639
3991
 
@@ -3734,13 +4086,14 @@ function finish(arg0) {
3734
4086
  let offset = 0;
3735
4087
  const dv0 = new DataView(memory0.buffer);
3736
4088
  for (const v of val0) {
4089
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
3737
4090
  dv0.setUint8(ptr0+ offset, v, true);
3738
4091
  offset += 1;
3739
4092
  }
3740
4093
  } else {
3741
4094
  // TypedArray / ArrayBuffer-like, direct copy
3742
4095
  valData0 = new Uint8Array(val0.buffer || val0, val0.byteOffset, valLenBytes0);
3743
- const out0 = new Uint8Array(memory0.buffer, ptr0,valLenBytes0);
4096
+ const out0 = new Uint8Array(memory0.buffer, ptr0, valLenBytes0);
3744
4097
  out0.set(valData0);
3745
4098
  }
3746
4099
 
@@ -3845,13 +4198,14 @@ function next$1(arg0, arg1) {
3845
4198
  let offset = 0;
3846
4199
  const dv0 = new DataView(memory0.buffer);
3847
4200
  for (const v of val0) {
4201
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
3848
4202
  dv0.setUint8(ptr0+ offset, v, true);
3849
4203
  offset += 1;
3850
4204
  }
3851
4205
  } else {
3852
4206
  // TypedArray / ArrayBuffer-like, direct copy
3853
4207
  valData0 = new Uint8Array(val0.buffer || val0, val0.byteOffset, valLenBytes0);
3854
- const out0 = new Uint8Array(memory0.buffer, ptr0,valLenBytes0);
4208
+ const out0 = new Uint8Array(memory0.buffer, ptr0, valLenBytes0);
3855
4209
  out0.set(valData0);
3856
4210
  }
3857
4211
 
@@ -3870,13 +4224,14 @@ function next$1(arg0, arg1) {
3870
4224
  let offset = 0;
3871
4225
  const dv2 = new DataView(memory0.buffer);
3872
4226
  for (const v of val2) {
4227
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
3873
4228
  dv2.setUint8(ptr2+ offset, v, true);
3874
4229
  offset += 1;
3875
4230
  }
3876
4231
  } else {
3877
4232
  // TypedArray / ArrayBuffer-like, direct copy
3878
4233
  valData2 = new Uint8Array(val2.buffer || val2, val2.byteOffset, valLenBytes2);
3879
- const out2 = new Uint8Array(memory0.buffer, ptr2,valLenBytes2);
4234
+ const out2 = new Uint8Array(memory0.buffer, ptr2, valLenBytes2);
3880
4235
  out2.set(valData2);
3881
4236
  }
3882
4237
 
@@ -3977,13 +4332,14 @@ function finish$1(arg0) {
3977
4332
  let offset = 0;
3978
4333
  const dv0 = new DataView(memory0.buffer);
3979
4334
  for (const v of val0) {
4335
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
3980
4336
  dv0.setUint8(ptr0+ offset, v, true);
3981
4337
  offset += 1;
3982
4338
  }
3983
4339
  } else {
3984
4340
  // TypedArray / ArrayBuffer-like, direct copy
3985
4341
  valData0 = new Uint8Array(val0.buffer || val0, val0.byteOffset, valLenBytes0);
3986
- const out0 = new Uint8Array(memory0.buffer, ptr0,valLenBytes0);
4342
+ const out0 = new Uint8Array(memory0.buffer, ptr0, valLenBytes0);
3987
4343
  out0.set(valData0);
3988
4344
  }
3989
4345
 
@@ -4078,13 +4434,14 @@ function encrypt(arg0, arg1) {
4078
4434
  let offset = 0;
4079
4435
  const dv0 = new DataView(memory0.buffer);
4080
4436
  for (const v of val0) {
4437
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
4081
4438
  dv0.setUint8(ptr0+ offset, v, true);
4082
4439
  offset += 1;
4083
4440
  }
4084
4441
  } else {
4085
4442
  // TypedArray / ArrayBuffer-like, direct copy
4086
4443
  valData0 = new Uint8Array(val0.buffer || val0, val0.byteOffset, valLenBytes0);
4087
- const out0 = new Uint8Array(memory0.buffer, ptr0,valLenBytes0);
4444
+ const out0 = new Uint8Array(memory0.buffer, ptr0, valLenBytes0);
4088
4445
  out0.set(valData0);
4089
4446
  }
4090
4447
 
@@ -4099,13 +4456,14 @@ function encrypt(arg0, arg1) {
4099
4456
  let offset = 0;
4100
4457
  const dv1 = new DataView(memory0.buffer);
4101
4458
  for (const v of val1) {
4459
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
4102
4460
  dv1.setUint8(ptr1+ offset, v, true);
4103
4461
  offset += 1;
4104
4462
  }
4105
4463
  } else {
4106
4464
  // TypedArray / ArrayBuffer-like, direct copy
4107
4465
  valData1 = new Uint8Array(val1.buffer || val1, val1.byteOffset, valLenBytes1);
4108
- const out1 = new Uint8Array(memory0.buffer, ptr1,valLenBytes1);
4466
+ const out1 = new Uint8Array(memory0.buffer, ptr1, valLenBytes1);
4109
4467
  out1.set(valData1);
4110
4468
  }
4111
4469
 
@@ -4200,13 +4558,14 @@ function decrypt(arg0, arg1) {
4200
4558
  let offset = 0;
4201
4559
  const dv0 = new DataView(memory0.buffer);
4202
4560
  for (const v of val0) {
4561
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
4203
4562
  dv0.setUint8(ptr0+ offset, v, true);
4204
4563
  offset += 1;
4205
4564
  }
4206
4565
  } else {
4207
4566
  // TypedArray / ArrayBuffer-like, direct copy
4208
4567
  valData0 = new Uint8Array(val0.buffer || val0, val0.byteOffset, valLenBytes0);
4209
- const out0 = new Uint8Array(memory0.buffer, ptr0,valLenBytes0);
4568
+ const out0 = new Uint8Array(memory0.buffer, ptr0, valLenBytes0);
4210
4569
  out0.set(valData0);
4211
4570
  }
4212
4571
 
@@ -4221,13 +4580,14 @@ function decrypt(arg0, arg1) {
4221
4580
  let offset = 0;
4222
4581
  const dv1 = new DataView(memory0.buffer);
4223
4582
  for (const v of val1) {
4583
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
4224
4584
  dv1.setUint8(ptr1+ offset, v, true);
4225
4585
  offset += 1;
4226
4586
  }
4227
4587
  } else {
4228
4588
  // TypedArray / ArrayBuffer-like, direct copy
4229
4589
  valData1 = new Uint8Array(val1.buffer || val1, val1.byteOffset, valLenBytes1);
4230
- const out1 = new Uint8Array(memory0.buffer, ptr1,valLenBytes1);
4590
+ const out1 = new Uint8Array(memory0.buffer, ptr1, valLenBytes1);
4231
4591
  out1.set(valData1);
4232
4592
  }
4233
4593
 
@@ -4327,13 +4687,14 @@ function validate(arg0, arg1, arg2) {
4327
4687
  let offset = 0;
4328
4688
  const dv1 = new DataView(memory0.buffer);
4329
4689
  for (const v of val1) {
4690
+ _requireValidNumericPrimitive.bind(null, 'u8')(v);
4330
4691
  dv1.setUint8(ptr1+ offset, v, true);
4331
4692
  offset += 1;
4332
4693
  }
4333
4694
  } else {
4334
4695
  // TypedArray / ArrayBuffer-like, direct copy
4335
4696
  valData1 = new Uint8Array(val1.buffer || val1, val1.byteOffset, valLenBytes1);
4336
- const out1 = new Uint8Array(memory0.buffer, ptr1,valLenBytes1);
4697
+ const out1 = new Uint8Array(memory0.buffer, ptr1, valLenBytes1);
4337
4698
  out1.set(valData1);
4338
4699
  }
4339
4700
 
@@ -4447,6 +4808,7 @@ null,
4447
4808
  getPostReturnFn: () => null,
4448
4809
  isCancellable: false,
4449
4810
  memoryIdx: null,
4811
+ stringEncoding: 'utf8',
4450
4812
  getMemoryFn: () => null,
4451
4813
  getReallocFn: () => null,
4452
4814
  importFn: _trampoline0,
@@ -4465,6 +4827,7 @@ null,
4465
4827
  getPostReturnFn: () => null,
4466
4828
  isCancellable: false,
4467
4829
  memoryIdx: null,
4830
+ stringEncoding: 'utf8',
4468
4831
  getMemoryFn: () => null,
4469
4832
  getReallocFn: () => null,
4470
4833
  importFn: _trampoline0,
@@ -4491,12 +4854,16 @@ null,
4491
4854
  isAsync: false,
4492
4855
  isManualAsync: _trampoline2.manuallyAsync,
4493
4856
  paramLiftFns: [],
4494
- resultLowerFns: [_lowerFlatOwn.bind(null, 1)],
4857
+ resultLowerFns: [_lowerFlatOwn({
4858
+ componentIdx: 0,
4859
+ lowerFn: () => { throw new Error('missing/invalid resource metadata'); }
4860
+ })],
4495
4861
  funcTypeIsAsync: false,
4496
4862
  getCallbackFn: () => null,
4497
4863
  getPostReturnFn: () => null,
4498
4864
  isCancellable: false,
4499
4865
  memoryIdx: null,
4866
+ stringEncoding: 'utf8',
4500
4867
  getMemoryFn: () => null,
4501
4868
  getReallocFn: () => null,
4502
4869
  importFn: _trampoline2,
@@ -4509,12 +4876,16 @@ null,
4509
4876
  isAsync: false,
4510
4877
  isManualAsync: _trampoline2.manuallyAsync,
4511
4878
  paramLiftFns: [],
4512
- resultLowerFns: [_lowerFlatOwn.bind(null, 1)],
4879
+ resultLowerFns: [_lowerFlatOwn({
4880
+ componentIdx: 0,
4881
+ lowerFn: () => { throw new Error('missing/invalid resource metadata'); }
4882
+ })],
4513
4883
  funcTypeIsAsync: false,
4514
4884
  getCallbackFn: () => null,
4515
4885
  getPostReturnFn: () => null,
4516
4886
  isCancellable: false,
4517
4887
  memoryIdx: null,
4888
+ stringEncoding: 'utf8',
4518
4889
  getMemoryFn: () => null,
4519
4890
  getReallocFn: () => null,
4520
4891
  importFn: _trampoline2,
@@ -4547,6 +4918,7 @@ null,
4547
4918
  getPostReturnFn: () => null,
4548
4919
  isCancellable: false,
4549
4920
  memoryIdx: null,
4921
+ stringEncoding: 'utf8',
4550
4922
  getMemoryFn: () => null,
4551
4923
  getReallocFn: () => null,
4552
4924
  importFn: _trampoline4,
@@ -4565,6 +4937,7 @@ null,
4565
4937
  getPostReturnFn: () => null,
4566
4938
  isCancellable: false,
4567
4939
  memoryIdx: null,
4940
+ stringEncoding: 'utf8',
4568
4941
  getMemoryFn: () => null,
4569
4942
  getReallocFn: () => null,
4570
4943
  importFn: _trampoline4,
@@ -4577,13 +4950,25 @@ null,
4577
4950
  componentIdx: 0,
4578
4951
  isAsync: false,
4579
4952
  isManualAsync: _trampoline5.manuallyAsync,
4580
- paramLiftFns: [_liftFlatBorrow.bind(null, 1),_liftFlatList({ elemLiftFn: _liftFlatU8, align32: 1, size32: 1 })],
4581
- resultLowerFns: [_lowerFlatResult([[ 'ok', null, 12, 4, 4 ],[ 'err', _lowerFlatVariant([[ 'last-operation-failed', _lowerFlatOwn.bind(null, 0), 8, 4, 4 ],[ 'closed', null, 8, 4, 4 ],]), 12, 4, 4 ],])],
4953
+ paramLiftFns: [_liftFlatBorrow.bind(null, 1),_liftFlatList({
4954
+ elemLiftFn: _liftFlatU8,
4955
+ elemAlign32: 1,
4956
+ elemSize32: 1,
4957
+ })],
4958
+ resultLowerFns: [_lowerFlatResult([
4959
+ [ 'ok', null, 12, 4, 4 ],
4960
+ [ 'err', _lowerFlatVariant([[ 'last-operation-failed', _lowerFlatOwn({
4961
+ componentIdx: 0,
4962
+ lowerFn: () => { throw new Error('missing/invalid resource metadata'); }
4963
+ }), 8, 4, 4 ],[ 'closed', null, 8, 4, 4 ],]), 12, 4, 4 ],
4964
+ ])
4965
+ ],
4582
4966
  funcTypeIsAsync: false,
4583
4967
  getCallbackFn: () => null,
4584
4968
  getPostReturnFn: () => null,
4585
4969
  isCancellable: false,
4586
4970
  memoryIdx: 0,
4971
+ stringEncoding: 'utf8',
4587
4972
  getMemoryFn: () => memory0,
4588
4973
  getReallocFn: () => null,
4589
4974
  importFn: _trampoline5,
@@ -4595,13 +4980,25 @@ null,
4595
4980
  componentIdx: 0,
4596
4981
  isAsync: false,
4597
4982
  isManualAsync: _trampoline5.manuallyAsync,
4598
- paramLiftFns: [_liftFlatBorrow.bind(null, 1),_liftFlatList({ elemLiftFn: _liftFlatU8, align32: 1, size32: 1 })],
4599
- resultLowerFns: [_lowerFlatResult([[ 'ok', null, 12, 4, 4 ],[ 'err', _lowerFlatVariant([[ 'last-operation-failed', _lowerFlatOwn.bind(null, 0), 8, 4, 4 ],[ 'closed', null, 8, 4, 4 ],]), 12, 4, 4 ],])],
4983
+ paramLiftFns: [_liftFlatBorrow.bind(null, 1),_liftFlatList({
4984
+ elemLiftFn: _liftFlatU8,
4985
+ elemAlign32: 1,
4986
+ elemSize32: 1,
4987
+ })],
4988
+ resultLowerFns: [_lowerFlatResult([
4989
+ [ 'ok', null, 12, 4, 4 ],
4990
+ [ 'err', _lowerFlatVariant([[ 'last-operation-failed', _lowerFlatOwn({
4991
+ componentIdx: 0,
4992
+ lowerFn: () => { throw new Error('missing/invalid resource metadata'); }
4993
+ }), 8, 4, 4 ],[ 'closed', null, 8, 4, 4 ],]), 12, 4, 4 ],
4994
+ ])
4995
+ ],
4600
4996
  funcTypeIsAsync: false,
4601
4997
  getCallbackFn: () => null,
4602
4998
  getPostReturnFn: () => null,
4603
4999
  isCancellable: false,
4604
5000
  memoryIdx: 0,
5001
+ stringEncoding: 'utf8',
4605
5002
  getMemoryFn: () => memory0,
4606
5003
  getReallocFn: () => null,
4607
5004
  importFn: _trampoline5,
@@ -4615,12 +5012,13 @@ null,
4615
5012
  isAsync: false,
4616
5013
  isManualAsync: _trampoline6.manuallyAsync,
4617
5014
  paramLiftFns: [_liftFlatBorrow.bind(null, 0)],
4618
- resultLowerFns: [_lowerFlatStringUTF8],
5015
+ resultLowerFns: [_lowerFlatStringAny],
4619
5016
  funcTypeIsAsync: false,
4620
5017
  getCallbackFn: () => null,
4621
5018
  getPostReturnFn: () => null,
4622
5019
  isCancellable: false,
4623
5020
  memoryIdx: 0,
5021
+ stringEncoding: 'utf8',
4624
5022
  getMemoryFn: () => memory0,
4625
5023
  getReallocFn: () => realloc0,
4626
5024
  importFn: _trampoline6,
@@ -4633,12 +5031,13 @@ null,
4633
5031
  isAsync: false,
4634
5032
  isManualAsync: _trampoline6.manuallyAsync,
4635
5033
  paramLiftFns: [_liftFlatBorrow.bind(null, 0)],
4636
- resultLowerFns: [_lowerFlatStringUTF8],
5034
+ resultLowerFns: [_lowerFlatStringAny],
4637
5035
  funcTypeIsAsync: false,
4638
5036
  getCallbackFn: () => null,
4639
5037
  getPostReturnFn: () => null,
4640
5038
  isCancellable: false,
4641
5039
  memoryIdx: 0,
5040
+ stringEncoding: 'utf8',
4642
5041
  getMemoryFn: () => memory0,
4643
5042
  getReallocFn: () => realloc0,
4644
5043
  importFn: _trampoline6,
@@ -4652,12 +5051,17 @@ null,
4652
5051
  isAsync: false,
4653
5052
  isManualAsync: _trampoline7.manuallyAsync,
4654
5053
  paramLiftFns: [],
4655
- resultLowerFns: [_lowerFlatList({ elemLowerFn: _lowerFlatTuple.bind(null, 7), typeIdx: 1 })],
5054
+ resultLowerFns: [_lowerFlatList({
5055
+ elemLowerFn: _lowerFlatTuple([[_lowerFlatStringAny, 16, 4],[_lowerFlatStringAny, 16, 4],]),
5056
+ elemSize32: 16,
5057
+ elemAlign32: 4,
5058
+ })],
4656
5059
  funcTypeIsAsync: false,
4657
5060
  getCallbackFn: () => null,
4658
5061
  getPostReturnFn: () => null,
4659
5062
  isCancellable: false,
4660
5063
  memoryIdx: 0,
5064
+ stringEncoding: 'utf8',
4661
5065
  getMemoryFn: () => memory0,
4662
5066
  getReallocFn: () => realloc0,
4663
5067
  importFn: _trampoline7,
@@ -4670,12 +5074,17 @@ null,
4670
5074
  isAsync: false,
4671
5075
  isManualAsync: _trampoline7.manuallyAsync,
4672
5076
  paramLiftFns: [],
4673
- resultLowerFns: [_lowerFlatList({ elemLowerFn: _lowerFlatTuple.bind(null, 7), typeIdx: 1 })],
5077
+ resultLowerFns: [_lowerFlatList({
5078
+ elemLowerFn: _lowerFlatTuple([[_lowerFlatStringAny, 16, 4],[_lowerFlatStringAny, 16, 4],]),
5079
+ elemSize32: 16,
5080
+ elemAlign32: 4,
5081
+ })],
4674
5082
  funcTypeIsAsync: false,
4675
5083
  getCallbackFn: () => null,
4676
5084
  getPostReturnFn: () => null,
4677
5085
  isCancellable: false,
4678
5086
  memoryIdx: 0,
5087
+ stringEncoding: 'utf8',
4679
5088
  getMemoryFn: () => memory0,
4680
5089
  getReallocFn: () => realloc0,
4681
5090
  importFn: _trampoline7,