@soundscript/soundscript 0.1.16 → 0.1.17

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.
Files changed (38) hide show
  1. package/json.js +1 -1
  2. package/numerics.js +4 -4
  3. package/package.json +6 -6
  4. package/project-transform/src/checker/rules/flow.js +16 -3
  5. package/project-transform/src/checker/rules/flow.ts +20 -3
  6. package/project-transform/src/checker/rules/unsound_syntax.js +0 -3
  7. package/project-transform/src/checker/rules/unsound_syntax.ts +0 -4
  8. package/project-transform/src/cli.js +1 -1
  9. package/project-transform/src/cli.ts +1 -1
  10. package/project-transform/src/compiler/compile_project.js +75 -9
  11. package/project-transform/src/compiler/compile_project.ts +121 -7
  12. package/project-transform/src/compiler/ir.ts +19 -1
  13. package/project-transform/src/compiler/lower.js +10335 -1477
  14. package/project-transform/src/compiler/lower.ts +16826 -4074
  15. package/project-transform/src/compiler/toolchain.js +36 -4
  16. package/project-transform/src/compiler/toolchain.ts +36 -4
  17. package/project-transform/src/compiler/wasm_js_host_runtime.js +134 -0
  18. package/project-transform/src/compiler/wasm_js_host_runtime.ts +146 -0
  19. package/project-transform/src/compiler/wat_arrays.js +4 -1
  20. package/project-transform/src/compiler/wat_arrays.ts +5 -1
  21. package/project-transform/src/compiler/wat_emitter.js +1497 -311
  22. package/project-transform/src/compiler/wat_emitter.ts +2971 -1017
  23. package/project-transform/src/compiler/wat_tagged.js +5 -0
  24. package/project-transform/src/compiler/wat_tagged.ts +5 -0
  25. package/project-transform/src/compiler_generator_runner.js +2139 -19
  26. package/project-transform/src/compiler_generator_runner.ts +2143 -20
  27. package/project-transform/src/compiler_promise_runner.js +4615 -636
  28. package/project-transform/src/compiler_promise_runner.ts +4703 -659
  29. package/project-transform/src/compiler_test_helpers.js +0 -579
  30. package/project-transform/src/compiler_test_helpers.ts +0 -648
  31. package/project-transform/src/frontend/macro_expander.js +4 -6
  32. package/project-transform/src/frontend/macro_expander.ts +4 -6
  33. package/project-transform/src/frontend/macro_operand_semantics.js +124 -1
  34. package/project-transform/src/frontend/macro_operand_semantics.ts +230 -6
  35. package/project-transform/src/frontend/macro_resolver.js +2 -2
  36. package/project-transform/src/frontend/macro_resolver.ts +2 -1
  37. package/project-transform/src/frontend/project_macro_support.js +29 -5
  38. package/project-transform/src/frontend/project_macro_support.ts +46 -10
@@ -1,10 +1,10 @@
1
1
  import { COMPILER_DIAGNOSTIC_CODES, COMPILER_DIAGNOSTIC_MESSAGES, } from '../checker/engine/diagnostic_codes.js';
2
2
  import { CompilerUnsupportedError } from './errors.js';
3
3
  import { createSpecializedObjectKeysHelperName, getFallbackObjectKeysHelperName, } from './object_keys.js';
4
- import { emitArrayRuntimeImports as emitArrayRuntimeImportsFromArrays, getCopyOwnedTaggedArrayToHostHelperName, getHostArrayToOwnedTaggedArrayHelperName, emitOwnedArrayBoundaryHelpers as emitOwnedArrayBoundaryHelpersFromArrays, emitOwnedArrayNativeHelpers as emitOwnedArrayNativeHelpersFromArrays, emitOwnedArrayTypes as emitOwnedArrayTypesFromArrays, getOwnedTaggedArrayToHostHelperName, } from './wat_arrays.js';
4
+ import { emitArrayRuntimeImports as emitArrayRuntimeImportsFromArrays, emitOwnedArrayBoundaryHelpers as emitOwnedArrayBoundaryHelpersFromArrays, emitOwnedArrayNativeHelpers as emitOwnedArrayNativeHelpersFromArrays, emitOwnedArrayTypes as emitOwnedArrayTypesFromArrays, getCopyOwnedTaggedArrayToHostHelperName, getHostArrayToOwnedTaggedArrayHelperName, getOwnedTaggedArrayToHostHelperName, } from './wat_arrays.js';
5
5
  import { emitHostTaggedPrimitiveExternrefToTagged, emitHostTaggedPrimitiveParamAdaptation as emitHostTaggedPrimitiveParamAdaptationFromModule, emitHostTaggedPrimitiveResultAdaptation as emitHostTaggedPrimitiveResultAdaptationFromModule, emitTaggedPrimitiveToHostExternref, getTaggedHostBoundaryUsage, } from './wat_tagged.js';
6
6
  import { emitOwnedStringLiteralHelpers as emitOwnedStringLiteralHelpersFromStrings, emitStringLiteralMetadata as emitStringLiteralMetadataFromStrings, emitStringRuntimeTypes as emitStringRuntimeTypesFromStrings, } from './wat_strings.js';
7
- import { CASED_RANGES, CASE_IGNORABLE_RANGES, LOWER_DELTA_RANGES, LOWER_EXPANSIONS, UPPER_DELTA_RANGES, UPPER_EXPANSIONS, } from './unicode_case_data.js';
7
+ import { CASE_IGNORABLE_RANGES, CASED_RANGES, LOWER_DELTA_RANGES, LOWER_EXPANSIONS, UPPER_DELTA_RANGES, UPPER_EXPANSIONS, } from './unicode_case_data.js';
8
8
  function indent(level) {
9
9
  return ' '.repeat(level);
10
10
  }
@@ -17,11 +17,26 @@ const SOUNDSCRIPT_PROMISE_DRAIN_HELPER_NAME = '__soundscript_promise_drain';
17
17
  const SOUNDSCRIPT_PROMISE_NEW_PENDING_HELPER_NAME = '__soundscript_promise_new_pending';
18
18
  const SOUNDSCRIPT_PROMISE_RESOLVE_INTO_HELPER_NAME = '__soundscript_promise_resolve_into';
19
19
  const SOUNDSCRIPT_PROMISE_REJECT_INTO_HELPER_NAME = '__soundscript_promise_reject_into';
20
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_HELPER_NAME = '__soundscript_async_generator_step';
21
+ const SOUNDSCRIPT_ASYNC_GENERATOR_TAGGED_TO_HOST_HELPER_NAME = '__soundscript_async_generator_tagged_to_host';
22
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME = '__soundscript_async_generator_step_bridge_fulfill_tagged';
23
+ const SOUNDSCRIPT_HOST_SYNC_GENERATOR_WRAP_IMPORT_NAME = '$host_sync_generator_wrap';
24
+ const SOUNDSCRIPT_HOST_ASYNC_GENERATOR_WRAP_IMPORT_NAME = '$host_async_generator_wrap';
20
25
  const SOUNDSCRIPT_HOST_PROMISE_TO_HOST_IMPORT_NAME = '$host_promise_to_host';
21
26
  const SOUNDSCRIPT_HOST_PROMISE_TO_INTERNAL_IMPORT_NAME = '$host_promise_to_internal';
27
+ const SOUNDSCRIPT_HOST_ASYNC_GENERATOR_STEP_IMPORT_NAME = '$host_async_generator_step';
28
+ const SOUNDSCRIPT_HOST_TRY_TAGGED_IMPORT_NAME = '$host_try_tagged';
29
+ const SOUNDSCRIPT_HOST_TRY_RESULT_THREW_IMPORT_NAME = '$host_try_result_threw';
30
+ const SOUNDSCRIPT_HOST_TRY_RESULT_VALUE_IMPORT_NAME = '$host_try_result_value';
22
31
  const SOUNDSCRIPT_PROMISE_THEN_HOST_EXPORT_NAME = '__soundscript_promise_then_host';
23
32
  const SOUNDSCRIPT_PROMISE_BRIDGE_FULFILL_EXPORT_NAME = '__soundscript_promise_bridge_fulfill';
24
33
  const SOUNDSCRIPT_PROMISE_BRIDGE_REJECT_EXPORT_NAME = '__soundscript_promise_bridge_reject';
34
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_EXPORT_NAME = '__soundscript_async_generator_step_bridge_fulfill';
35
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_STRING_ARRAY_EXPORT_NAME = '__soundscript_async_generator_step_bridge_fulfill_string_array';
36
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_NUMBER_ARRAY_EXPORT_NAME = '__soundscript_async_generator_step_bridge_fulfill_number_array';
37
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_BOOLEAN_ARRAY_EXPORT_NAME = '__soundscript_async_generator_step_bridge_fulfill_boolean_array';
38
+ const SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_ARRAY_EXPORT_NAME = '__soundscript_async_generator_step_bridge_fulfill_tagged_array';
39
+ const SOUNDSCRIPT_GENERATOR_INTERNAL_STEP_KEY = '__ss_generator_step';
25
40
  const SOUNDSCRIPT_PROMISE_INTERNAL_STATE_KEY = '__ss_promise_state';
26
41
  const SOUNDSCRIPT_PROMISE_INTERNAL_VALUE_KEY = '__ss_promise_value';
27
42
  const SOUNDSCRIPT_PROMISE_INTERNAL_REACTIONS_KEY = '__ss_promise_reactions';
@@ -40,6 +55,13 @@ const BUILTIN_ERROR_CONSTRUCTOR_NAMES = [
40
55
  'TypeError',
41
56
  'URIError',
42
57
  ];
58
+ const ASYNC_GENERATOR_HOST_TAGGED_ARRAY_BRIDGE_KINDS = {
59
+ includesBoolean: true,
60
+ includesNull: true,
61
+ includesNumber: true,
62
+ includesString: true,
63
+ includesUndefined: true,
64
+ };
43
65
  function getWatValueType(valueType) {
44
66
  switch (valueType) {
45
67
  case 'f64':
@@ -1238,6 +1260,8 @@ function isOwnedStringExpression(expression) {
1238
1260
  case 'owned_boolean_array_join':
1239
1261
  case 'owned_string_array_element':
1240
1262
  return true;
1263
+ case 'box_get':
1264
+ return expression.type === 'owned_string_ref';
1241
1265
  case 'local_get':
1242
1266
  case 'call':
1243
1267
  return expression.type === 'owned_string_ref';
@@ -1769,6 +1793,10 @@ function statementsUseStringHelper(statements, predicate) {
1769
1793
  case 'return':
1770
1794
  case 'dynamic_object_property_set':
1771
1795
  return expressionTreeMatches(statement.value);
1796
+ case 'throw_tagged':
1797
+ return expressionTreeMatches(statement.value);
1798
+ case 'trap':
1799
+ return false;
1772
1800
  case 'specialized_object_field_set':
1773
1801
  return expressionTreeMatches(statement.value);
1774
1802
  case 'owned_heap_array_set':
@@ -1800,7 +1828,8 @@ function isPromiseRuntimeHelperCall(expression) {
1800
1828
  expression.callee === SOUNDSCRIPT_PROMISE_REJECT_HELPER_NAME ||
1801
1829
  expression.callee === SOUNDSCRIPT_PROMISE_RESOLVE_INTO_HELPER_NAME ||
1802
1830
  expression.callee === SOUNDSCRIPT_PROMISE_REJECT_INTO_HELPER_NAME ||
1803
- expression.callee === SOUNDSCRIPT_PROMISE_THEN_HELPER_NAME);
1831
+ expression.callee === SOUNDSCRIPT_PROMISE_THEN_HELPER_NAME ||
1832
+ expression.callee === SOUNDSCRIPT_ASYNC_GENERATOR_STEP_HELPER_NAME);
1804
1833
  }
1805
1834
  function moduleUsesHostPromiseParamBridge(module) {
1806
1835
  return module.functions.some((func) => (func.hostPromiseParams?.length ?? 0) > 0 || func.hostImport?.promiseResult === true);
@@ -1808,21 +1837,71 @@ function moduleUsesHostPromiseParamBridge(module) {
1808
1837
  function moduleUsesHostPromiseResultBridge(module) {
1809
1838
  return module.functions.some((func) => func.hostPromiseResult === true || (func.hostImportPromiseParams?.length ?? 0) > 0);
1810
1839
  }
1840
+ function moduleUsesAsyncGeneratorHostStepBridge(module) {
1841
+ return module.functions.some((func) => func.usesAsyncGeneratorHostStepBridge === true);
1842
+ }
1843
+ function moduleUsesAsyncGeneratorHostResultBridge(module) {
1844
+ return module.functions.some((func) => func.hostAsyncGeneratorResult === true);
1845
+ }
1846
+ function moduleUsesSyncGeneratorHostResultBridge(module) {
1847
+ return module.functions.some((func) => func.hostGeneratorResult === true);
1848
+ }
1849
+ function moduleUsesGeneratorStepClosureHostBridge(module) {
1850
+ return moduleUsesAsyncGeneratorHostStepBridge(module) ||
1851
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
1852
+ moduleUsesAsyncGeneratorHostResultBridge(module);
1853
+ }
1811
1854
  function moduleUsesHostPromiseBridge(module) {
1812
- return moduleUsesHostPromiseParamBridge(module) || moduleUsesHostPromiseResultBridge(module);
1855
+ return moduleUsesHostPromiseParamBridge(module) ||
1856
+ moduleUsesHostPromiseResultBridge(module) ||
1857
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
1858
+ moduleUsesAsyncGeneratorHostStepBridge(module) ||
1859
+ moduleUsesAsyncGeneratorHostResultBridge(module);
1860
+ }
1861
+ function moduleUsesSyncTryCatchRuntime(module) {
1862
+ return module.syncTryCatchClosureSignatureId !== undefined;
1863
+ }
1864
+ function moduleUsesTaggedThrowRuntime(module) {
1865
+ const statementsUseTaggedThrowRuntime = (statements) => statements.some((statement) => {
1866
+ switch (statement.kind) {
1867
+ case 'throw_tagged':
1868
+ return true;
1869
+ case 'if':
1870
+ return statementsUseTaggedThrowRuntime(statement.thenBody) ||
1871
+ statementsUseTaggedThrowRuntime(statement.elseBody);
1872
+ case 'while':
1873
+ return statementsUseTaggedThrowRuntime(statement.body);
1874
+ default:
1875
+ return false;
1876
+ }
1877
+ });
1878
+ return module.functions.some((func) => statementsUseTaggedThrowRuntime(func.body));
1813
1879
  }
1814
1880
  function functionIsHostImported(func) {
1815
1881
  return func.hostImport !== undefined;
1816
1882
  }
1817
1883
  function moduleUsesPromiseRuntime(module) {
1818
1884
  return moduleUsesHostPromiseBridge(module) ||
1885
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
1819
1886
  module.functions.some((func) => statementsUseStringHelper(func.body, isPromiseRuntimeHelperCall));
1820
1887
  }
1888
+ function getGeneratorStepClosureSignature(module) {
1889
+ return module.closureSignatures?.find((signature) => signature.params.length === 2 &&
1890
+ signature.params[0] === 'f64' &&
1891
+ signature.params[1] === 'tagged_ref' &&
1892
+ signature.paramTaggedPrimitiveKinds?.[1] !== undefined &&
1893
+ signature.paramHeapRepresentations?.[1]?.kind === 'dynamic_object_representation' &&
1894
+ signature.resultType === 'heap_ref' &&
1895
+ signature.resultHeapRepresentation?.kind === 'dynamic_object_representation');
1896
+ }
1821
1897
  function moduleUsesPromiseThenRuntime(module) {
1822
- return moduleUsesHostPromiseResultBridge(module) || module.functions.some((func) => statementsUseStringHelper(func.body, (expression) => expression.kind === 'call' &&
1823
- (expression.callee === SOUNDSCRIPT_PROMISE_THEN_HELPER_NAME ||
1824
- expression.callee === SOUNDSCRIPT_PROMISE_RESOLVE_INTO_HELPER_NAME ||
1825
- expression.callee === SOUNDSCRIPT_PROMISE_REJECT_INTO_HELPER_NAME)));
1898
+ return moduleUsesHostPromiseResultBridge(module) ||
1899
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
1900
+ moduleUsesAsyncGeneratorHostResultBridge(module) ||
1901
+ module.functions.some((func) => statementsUseStringHelper(func.body, (expression) => expression.kind === 'call' &&
1902
+ (expression.callee === SOUNDSCRIPT_PROMISE_THEN_HELPER_NAME ||
1903
+ expression.callee === SOUNDSCRIPT_PROMISE_RESOLVE_INTO_HELPER_NAME ||
1904
+ expression.callee === SOUNDSCRIPT_PROMISE_REJECT_INTO_HELPER_NAME)));
1826
1905
  }
1827
1906
  function expressionUsesNullLiteral(expression) {
1828
1907
  if (expression.kind === 'null_literal') {
@@ -1856,12 +1935,18 @@ function collectOwnedStringLiteralIdsFromStatements(statements, literalIds) {
1856
1935
  for (const statement of statements) {
1857
1936
  switch (statement.kind) {
1858
1937
  case 'expression':
1938
+ case 'box_set':
1859
1939
  case 'local_set':
1860
1940
  case 'return':
1861
1941
  case 'dynamic_object_property_set':
1862
1942
  case 'specialized_object_field_set':
1863
1943
  collectOwnedStringLiteralIds(statement.value, literalIds);
1864
1944
  break;
1945
+ case 'throw_tagged':
1946
+ collectOwnedStringLiteralIds(statement.value, literalIds);
1947
+ break;
1948
+ case 'trap':
1949
+ break;
1865
1950
  case 'owned_heap_array_set':
1866
1951
  case 'owned_string_array_set':
1867
1952
  case 'owned_number_array_set':
@@ -1964,18 +2049,20 @@ function moduleUsesObjectKeysArrayRuntime(module) {
1964
2049
  func.resultType === 'owned_array_ref' ||
1965
2050
  func.locals.some((local) => local.type === 'owned_array_ref') ||
1966
2051
  statementsUseStringHelper(func.body, (expression) => getExpressionValueType(expression) === 'owned_array_ref' ||
1967
- expression.kind === 'owned_string_array_element')) || getClassStaticStringArrayFields(module).length > 0 || (module.runtime?.functions.some((func) => func.operations.some((operation) => operation.kind === 'list_specialized_object_keys' ||
1968
- operation.kind === 'list_fallback_object_keys' ||
1969
- operation.kind === 'list_dynamic_object_keys' ||
1970
- (operation.kind === 'list_dynamic_object_entries' &&
1971
- operation.pairValueType === 'owned_string_ref'))) ?? false);
2052
+ expression.kind === 'owned_string_array_element')) || getClassStaticStringArrayFields(module).length > 0 ||
2053
+ (module.runtime?.functions.some((func) => func.operations.some((operation) => operation.kind === 'list_specialized_object_keys' ||
2054
+ operation.kind === 'list_fallback_object_keys' ||
2055
+ operation.kind === 'list_dynamic_object_keys' ||
2056
+ (operation.kind === 'list_dynamic_object_entries' &&
2057
+ operation.pairValueType === 'owned_string_ref'))) ?? false);
1972
2058
  }
1973
2059
  function moduleUsesOwnedHeapArrayRuntime(module) {
1974
- return moduleUsesPromiseRuntime(module) || module.functions.some((func) => func.params.some((param) => param.type === 'owned_heap_array_ref') ||
1975
- func.resultType === 'owned_heap_array_ref' ||
1976
- func.locals.some((local) => local.type === 'owned_heap_array_ref') ||
1977
- statementsUseStringHelper(func.body, (expression) => getExpressionValueType(expression) === 'owned_heap_array_ref' ||
1978
- expression.kind === 'owned_heap_array_element'));
2060
+ return moduleUsesPromiseRuntime(module) ||
2061
+ module.functions.some((func) => func.params.some((param) => param.type === 'owned_heap_array_ref') ||
2062
+ func.resultType === 'owned_heap_array_ref' ||
2063
+ func.locals.some((local) => local.type === 'owned_heap_array_ref') ||
2064
+ statementsUseStringHelper(func.body, (expression) => getExpressionValueType(expression) === 'owned_heap_array_ref' ||
2065
+ expression.kind === 'owned_heap_array_element'));
1979
2066
  }
1980
2067
  function moduleUsesOwnedNumberArrayRuntime(module) {
1981
2068
  return module.functions.some((func) => func.params.some((param) => param.type === 'owned_number_array_ref') ||
@@ -1998,10 +2085,11 @@ function moduleUsesOwnedTaggedArrayRuntime(module) {
1998
2085
  (func.hostTaggedArrayParams?.length ?? 0) > 0 ||
1999
2086
  func.hostTaggedArrayResultKinds !== undefined ||
2000
2087
  statementsUseStringHelper(func.body, (expression) => getExpressionValueType(expression) === 'owned_tagged_array_ref' ||
2001
- expression.kind === 'owned_tagged_array_element')) || getClassStaticTaggedArrayFields(module).length > 0 || (module.runtime?.functions.some((func) => func.operations.some((operation) => (operation.kind === 'list_dynamic_object_values' &&
2002
- operation.resultType === 'owned_tagged_array_ref') ||
2003
- (operation.kind === 'list_dynamic_object_entries' &&
2004
- operation.pairValueType === 'tagged_ref'))) ?? false);
2088
+ expression.kind === 'owned_tagged_array_element')) || getClassStaticTaggedArrayFields(module).length > 0 ||
2089
+ (module.runtime?.functions.some((func) => func.operations.some((operation) => (operation.kind === 'list_dynamic_object_values' &&
2090
+ operation.resultType === 'owned_tagged_array_ref') ||
2091
+ (operation.kind === 'list_dynamic_object_entries' &&
2092
+ operation.pairValueType === 'tagged_ref'))) ?? false);
2005
2093
  }
2006
2094
  function moduleUsesOwnedStringArrayPush(module) {
2007
2095
  return module.functions.some((func) => statementsUseStringHelper(func.body, (expression) => expression.kind === 'owned_string_array_push'));
@@ -2439,15 +2527,16 @@ function moduleUsesOwnedStringCodePointAtRuntime(module) {
2439
2527
  return module.functions.some((func) => statementsUseStringHelper(func.body, expressionUsesOwnedStringCodePointAtHelper));
2440
2528
  }
2441
2529
  function moduleUsesOwnedStringEqualsRuntime(module) {
2442
- return module.functions.some((func) => statementsUseStringHelper(func.body, expressionUsesOwnedStringEqualsHelper)) || getDynamicObjectLayout(module.runtime) !== undefined || (module.runtime?.functions.some((func) => func.operations.some((operation) => operation.kind === 'allocate_dynamic_object' ||
2443
- operation.kind === 'copy_dynamic_object_entries' ||
2444
- operation.kind === 'delete_dynamic_object_property' ||
2445
- operation.kind === 'get_dynamic_object_property' ||
2446
- operation.kind === 'has_dynamic_object_property' ||
2447
- operation.kind === 'list_dynamic_object_entries' ||
2448
- operation.kind === 'list_dynamic_object_keys' ||
2449
- operation.kind === 'list_dynamic_object_values' ||
2450
- operation.kind === 'set_dynamic_object_property')) ?? false);
2530
+ return module.functions.some((func) => statementsUseStringHelper(func.body, expressionUsesOwnedStringEqualsHelper)) || getDynamicObjectLayout(module.runtime) !== undefined ||
2531
+ (module.runtime?.functions.some((func) => func.operations.some((operation) => operation.kind === 'allocate_dynamic_object' ||
2532
+ operation.kind === 'copy_dynamic_object_entries' ||
2533
+ operation.kind === 'delete_dynamic_object_property' ||
2534
+ operation.kind === 'get_dynamic_object_property' ||
2535
+ operation.kind === 'has_dynamic_object_property' ||
2536
+ operation.kind === 'list_dynamic_object_entries' ||
2537
+ operation.kind === 'list_dynamic_object_keys' ||
2538
+ operation.kind === 'list_dynamic_object_values' ||
2539
+ operation.kind === 'set_dynamic_object_property')) ?? false);
2451
2540
  }
2452
2541
  function moduleUsesOwnedStringConcatRuntime(module) {
2453
2542
  return module.functions.some((func) => statementsUseStringHelper(func.body, expressionUsesOwnedStringConcatHelper));
@@ -2542,7 +2631,8 @@ function functionUsesOwnedStringToHostPropertyReadAdaptation(func, operations) {
2542
2631
  });
2543
2632
  }
2544
2633
  function moduleUsesOwnedStringToHostRuntime(module) {
2545
- return module.functions.some((func) => functionUsesOwnedStringToHostReturnAdaptation(func) ||
2634
+ return module.functions.some((func) => (functionIsHostImported(func) && func.params.some((param) => param.type === 'owned_string_ref')) ||
2635
+ functionUsesOwnedStringToHostReturnAdaptation(func) ||
2546
2636
  functionUsesOwnedStringToHostLocalAdaptation(func) ||
2547
2637
  functionUsesOwnedStringToHostPropertyReadAdaptation(func, getRuntimeOperationsForFunction(func, module.runtime)) ||
2548
2638
  statementsUseStringHelper(func.body, expressionUsesOwnedStringToHostHelper)) || moduleUsesHostClosureBoundaryToHostType(module, 'owned_string_ref') ||
@@ -2588,7 +2678,8 @@ function moduleUsesSpecializedHostMethodArrayFieldSyncBoundary(module, valueType
2588
2678
  }
2589
2679
  function moduleUsesFallbackHostMethodArrayPropertySyncBoundary(module, valueType) {
2590
2680
  return module.functions.some((func) => hasHostFallbackObjectResultBoundary(func) &&
2591
- (func.hostFallbackArrayProperties?.some((property) => property.valueType === valueType) ?? false) &&
2681
+ (func.hostFallbackArrayProperties?.some((property) => property.valueType === valueType) ??
2682
+ false) &&
2592
2683
  (func.hostFallbackClosureProperties?.some((property) => (property.methodClosureFunctionIds?.length ?? 0) > 0) ?? false));
2593
2684
  }
2594
2685
  function getSpecializedTaggedArrayFieldBoundaryUsage(module) {
@@ -2659,75 +2750,95 @@ function getSpecializedTaggedArrayFieldBoundaryUsage(module) {
2659
2750
  function moduleUsesOwnedArrayHostParamBoundary(module) {
2660
2751
  return module.functions.some((func) => func.closureFunctionId === undefined &&
2661
2752
  func.params.some((param) => param.type === 'owned_array_ref')) || module.functions.some((func) => func.hostDynamicCollectionParams?.some((param) => param.collectionKind === 'map' || param.valueKind === 'string') === true) || module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_array_ref') &&
2662
- getHostFallbackObjectParamBoundaryNames(func).length > 0) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2753
+ (getHostFallbackObjectParamBoundaryNames(func).length > 0 || hasHostFallbackObjectResultBoundary(func))) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2663
2754
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_array_ref') &&
2664
2755
  module.functions.some((func) => getHostAdaptableSpecializedObjectParamBoundaries(func, createSpecializedObjectLayouts(module.runtime)).some((boundary) => boundary.representation.name === representation.name) ||
2756
+ getHostAdaptableSpecializedObjectResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.name === representation.name ||
2665
2757
  getHostTaggedHeapNullableSpecializedParamBoundaries(func, createSpecializedObjectLayouts(module.runtime)).some((boundary) => boundary.boundary.representation.name === representation.name))) || moduleUsesSpecializedHostMethodArrayFieldSyncBoundary(module, 'owned_array_ref') ||
2666
2758
  (module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2667
- (representation.hostStaticFields?.some((field) => field.valueKind === 'string_array') ?? false)) ?? false) ||
2668
- moduleUsesHostClosureBoundaryFromHostType(module, 'owned_array_ref');
2759
+ (representation.hostStaticFields?.some((field) => field.valueKind === 'string_array') ??
2760
+ false)) ?? false) ||
2761
+ moduleUsesHostClosureBoundaryFromHostType(module, 'owned_array_ref') ||
2762
+ moduleUsesGeneratorStepClosureHostBridge(module);
2669
2763
  }
2670
2764
  function moduleUsesOwnedArrayHostResultBoundary(module) {
2671
- return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_array_ref') || module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_array_ref') &&
2672
- hasHostFallbackObjectResultBoundary(func)) ||
2765
+ return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_array_ref') ||
2766
+ module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_array_ref') &&
2767
+ hasHostFallbackObjectResultBoundary(func)) ||
2673
2768
  module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2674
2769
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_array_ref') &&
2675
2770
  module.functions.some((func) => getHostAdaptableSpecializedObjectResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.name === representation.name ||
2676
2771
  getHostTaggedHeapNullableSpecializedResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.representation.name === representation.name)) ||
2677
2772
  (module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2678
- (representation.hostStaticFields?.some((field) => field.valueKind === 'string_array') ?? false)) ?? false) ||
2679
- moduleUsesHostClosureBoundaryToHostType(module, 'owned_array_ref');
2773
+ (representation.hostStaticFields?.some((field) => field.valueKind === 'string_array') ??
2774
+ false)) ?? false) ||
2775
+ moduleUsesHostClosureBoundaryToHostType(module, 'owned_array_ref') ||
2776
+ moduleUsesGeneratorStepClosureHostBridge(module);
2680
2777
  }
2681
2778
  function moduleUsesOwnedNumberArrayHostParamBoundary(module) {
2682
2779
  return module.functions.some((func) => func.closureFunctionId === undefined &&
2683
2780
  func.params.some((param) => param.type === 'owned_number_array_ref')) || module.functions.some((func) => func.hostDynamicCollectionParams?.some((param) => param.valueKind === 'number') === true) || module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_number_array_ref') &&
2684
- getHostFallbackObjectParamBoundaryNames(func).length > 0) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2781
+ (getHostFallbackObjectParamBoundaryNames(func).length > 0 || hasHostFallbackObjectResultBoundary(func))) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2685
2782
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_number_array_ref') &&
2686
2783
  module.functions.some((func) => getHostAdaptableSpecializedObjectParamBoundaries(func, createSpecializedObjectLayouts(module.runtime)).some((boundary) => boundary.representation.name === representation.name) ||
2784
+ getHostAdaptableSpecializedObjectResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.name === representation.name ||
2687
2785
  getHostTaggedHeapNullableSpecializedParamBoundaries(func, createSpecializedObjectLayouts(module.runtime)).some((boundary) => boundary.boundary.representation.name === representation.name))) || moduleUsesSpecializedHostMethodArrayFieldSyncBoundary(module, 'owned_number_array_ref') ||
2688
2786
  (module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2689
- (representation.hostStaticFields?.some((field) => field.valueKind === 'number_array') ?? false)) ?? false) ||
2690
- moduleUsesHostClosureBoundaryFromHostType(module, 'owned_number_array_ref');
2787
+ (representation.hostStaticFields?.some((field) => field.valueKind === 'number_array') ??
2788
+ false)) ?? false) ||
2789
+ moduleUsesHostClosureBoundaryFromHostType(module, 'owned_number_array_ref') ||
2790
+ moduleUsesGeneratorStepClosureHostBridge(module);
2691
2791
  }
2692
2792
  function moduleUsesOwnedNumberArrayHostResultBoundary(module) {
2693
- return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_number_array_ref') || module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_number_array_ref') &&
2694
- hasHostFallbackObjectResultBoundary(func)) ||
2793
+ return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_number_array_ref') ||
2794
+ module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_number_array_ref') &&
2795
+ hasHostFallbackObjectResultBoundary(func)) ||
2695
2796
  module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2696
2797
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_number_array_ref') &&
2697
2798
  module.functions.some((func) => getHostAdaptableSpecializedObjectResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.name === representation.name ||
2698
2799
  getHostTaggedHeapNullableSpecializedResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.representation.name === representation.name)) ||
2699
2800
  (module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2700
- (representation.hostStaticFields?.some((field) => field.valueKind === 'number_array') ?? false)) ?? false) ||
2701
- moduleUsesHostClosureBoundaryToHostType(module, 'owned_number_array_ref');
2801
+ (representation.hostStaticFields?.some((field) => field.valueKind === 'number_array') ??
2802
+ false)) ?? false) ||
2803
+ moduleUsesHostClosureBoundaryToHostType(module, 'owned_number_array_ref') ||
2804
+ moduleUsesGeneratorStepClosureHostBridge(module);
2702
2805
  }
2703
2806
  function moduleUsesOwnedBooleanArrayHostParamBoundary(module) {
2704
2807
  return module.functions.some((func) => func.closureFunctionId === undefined &&
2705
2808
  func.params.some((param) => param.type === 'owned_boolean_array_ref')) || module.functions.some((func) => func.hostDynamicCollectionParams?.some((param) => param.valueKind === 'boolean') === true) || module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_boolean_array_ref') &&
2706
- getHostFallbackObjectParamBoundaryNames(func).length > 0) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2809
+ (getHostFallbackObjectParamBoundaryNames(func).length > 0 || hasHostFallbackObjectResultBoundary(func))) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2707
2810
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_boolean_array_ref') &&
2708
2811
  module.functions.some((func) => getHostAdaptableSpecializedObjectParamBoundaries(func, createSpecializedObjectLayouts(module.runtime)).some((boundary) => boundary.representation.name === representation.name) ||
2812
+ getHostAdaptableSpecializedObjectResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.name === representation.name ||
2709
2813
  getHostTaggedHeapNullableSpecializedParamBoundaries(func, createSpecializedObjectLayouts(module.runtime)).some((boundary) => boundary.boundary.representation.name === representation.name))) || moduleUsesSpecializedHostMethodArrayFieldSyncBoundary(module, 'owned_boolean_array_ref') ||
2710
2814
  (module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2711
- (representation.hostStaticFields?.some((field) => field.valueKind === 'boolean_array') ?? false)) ?? false) ||
2712
- moduleUsesHostClosureBoundaryFromHostType(module, 'owned_boolean_array_ref');
2815
+ (representation.hostStaticFields?.some((field) => field.valueKind === 'boolean_array') ??
2816
+ false)) ?? false) ||
2817
+ moduleUsesHostClosureBoundaryFromHostType(module, 'owned_boolean_array_ref') ||
2818
+ moduleUsesGeneratorStepClosureHostBridge(module);
2713
2819
  }
2714
2820
  function moduleUsesOwnedBooleanArrayHostResultBoundary(module) {
2715
- return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_boolean_array_ref') || module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_boolean_array_ref') &&
2716
- hasHostFallbackObjectResultBoundary(func)) ||
2821
+ return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_boolean_array_ref') ||
2822
+ module.functions.some((func) => func.hostFallbackArrayProperties?.some((property) => property.valueType === 'owned_boolean_array_ref') &&
2823
+ hasHostFallbackObjectResultBoundary(func)) ||
2717
2824
  module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2718
2825
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_boolean_array_ref') &&
2719
2826
  module.functions.some((func) => getHostAdaptableSpecializedObjectResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.name === representation.name ||
2720
2827
  getHostTaggedHeapNullableSpecializedResultRepresentation(func, createSpecializedObjectLayouts(module.runtime))?.representation.name === representation.name)) ||
2721
2828
  (module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2722
- (representation.hostStaticFields?.some((field) => field.valueKind === 'boolean_array') ?? false)) ?? false) ||
2723
- moduleUsesHostClosureBoundaryToHostType(module, 'owned_boolean_array_ref');
2829
+ (representation.hostStaticFields?.some((field) => field.valueKind === 'boolean_array') ??
2830
+ false)) ?? false) ||
2831
+ moduleUsesHostClosureBoundaryToHostType(module, 'owned_boolean_array_ref') ||
2832
+ moduleUsesGeneratorStepClosureHostBridge(module);
2724
2833
  }
2725
2834
  function moduleUsesOwnedHeapArrayHostParamBoundary(module) {
2726
2835
  const specializedUsage = collectHostBoundarySpecializedObjectUsageWithHostClosures(module, createSpecializedObjectLayouts(module.runtime));
2727
2836
  return module.functions.some((func) => func.closureFunctionId === undefined &&
2728
2837
  (func.hostHeapArrayParams?.length ?? 0) > 0) || module.functions.some((func) => (func.hostFallbackHeapArrayProperties?.length ?? 0) > 0 &&
2729
2838
  (getHostFallbackObjectParamBoundaryNames(func).length > 0 ||
2730
- getHostTaggedHeapNullableFallbackParamBoundaryNames(func).length > 0)) || [...specializedUsage.values()].some((usage) => usage.needsParamBoundary &&
2839
+ getHostTaggedHeapNullableFallbackParamBoundaryNames(func).length > 0 ||
2840
+ hasHostFallbackObjectResultBoundary(func) ||
2841
+ hasHostTaggedHeapNullableFallbackResultBoundary(func))) || [...specializedUsage.values()].some((usage) => (usage.needsParamBoundary || usage.needsResultBoundary) &&
2731
2842
  usage.layout.fields.some((field) => field.valueType === 'owned_heap_array_ref')) || moduleUsesHostClosureBoundaryFromHostType(module, 'owned_heap_array_ref');
2732
2843
  }
2733
2844
  function moduleUsesOwnedHeapArrayHostParamCopyBack(module) {
@@ -2759,7 +2870,8 @@ function moduleUsesOwnedHeapArrayHostResultBoundary(module) {
2759
2870
  return module.functions.some((func) => func.closureFunctionId === undefined &&
2760
2871
  func.resultType === 'owned_heap_array_ref' &&
2761
2872
  func.hostHeapArrayResultRepresentation !== undefined) || module.functions.some((func) => (func.hostFallbackHeapArrayProperties?.length ?? 0) > 0 &&
2762
- (hasHostFallbackObjectResultBoundary(func) || hasHostTaggedHeapNullableFallbackResultBoundary(func))) || [...specializedUsage.values()].some((usage) => usage.needsResultBoundary &&
2873
+ (hasHostFallbackObjectResultBoundary(func) ||
2874
+ hasHostTaggedHeapNullableFallbackResultBoundary(func))) || [...specializedUsage.values()].some((usage) => usage.needsResultBoundary &&
2763
2875
  usage.layout.fields.some((field) => field.valueType === 'owned_heap_array_ref')) || moduleUsesHostClosureBoundaryToHostType(module, 'owned_heap_array_ref');
2764
2876
  }
2765
2877
  function moduleUsesOwnedTaggedArrayHostParamBoundary(module) {
@@ -2767,12 +2879,16 @@ function moduleUsesOwnedTaggedArrayHostParamBoundary(module) {
2767
2879
  return module.functions.some((func) => func.closureFunctionId === undefined &&
2768
2880
  func.params.some((param) => param.type === 'owned_tagged_array_ref')) || module.functions.some((func) => (func.hostFallbackTaggedArrayProperties?.length ?? 0) > 0 &&
2769
2881
  (getHostFallbackObjectParamBoundaryNames(func).length > 0 ||
2770
- getHostTaggedHeapNullableFallbackParamBoundaryNames(func).length > 0)) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2882
+ getHostTaggedHeapNullableFallbackParamBoundaryNames(func).length > 0 ||
2883
+ hasHostFallbackObjectResultBoundary(func) ||
2884
+ hasHostTaggedHeapNullableFallbackResultBoundary(func))) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2771
2885
  parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_tagged_array_ref') &&
2772
2886
  module.functions.some((func) => getHostAdaptableSpecializedObjectParamBoundaries(func, layouts).some((boundary) => boundary.representation.name === representation.name) ||
2887
+ getHostAdaptableSpecializedObjectResultRepresentation(func, layouts)?.name === representation.name ||
2773
2888
  getHostTaggedHeapNullableSpecializedParamBoundaries(func, layouts).some((boundary) => boundary.boundary.representation.name === representation.name))) || moduleUsesSpecializedHostMethodArrayFieldSyncBoundary(module, 'owned_tagged_array_ref') ||
2774
2889
  moduleUsesHostClosureBoundaryFromHostType(module, 'owned_tagged_array_ref') ||
2775
- getClassStaticTaggedArrayFields(module).length > 0;
2890
+ getClassStaticTaggedArrayFields(module).length > 0 ||
2891
+ moduleUsesGeneratorStepClosureHostBridge(module);
2776
2892
  }
2777
2893
  function moduleUsesOwnedTaggedArrayHostParamCopyBack(module) {
2778
2894
  return moduleUsesOwnedTaggedArrayHostParamBoundary(module);
@@ -2780,17 +2896,24 @@ function moduleUsesOwnedTaggedArrayHostParamCopyBack(module) {
2780
2896
  function moduleUsesOwnedTaggedArrayHostResultBoundary(module) {
2781
2897
  const layouts = createSpecializedObjectLayouts(module.runtime);
2782
2898
  return module.functions.some((func) => func.closureFunctionId === undefined && func.resultType === 'owned_tagged_array_ref') || module.functions.some((func) => (func.hostFallbackTaggedArrayProperties?.length ?? 0) > 0 &&
2783
- (hasHostFallbackObjectResultBoundary(func) || hasHostTaggedHeapNullableFallbackResultBoundary(func))) || module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2784
- parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_tagged_array_ref') &&
2785
- module.functions.some((func) => getHostAdaptableSpecializedObjectResultRepresentation(func, layouts)?.name === representation.name ||
2786
- getHostTaggedHeapNullableSpecializedResultRepresentation(func, layouts)?.representation.name === representation.name)) || moduleUsesHostClosureBoundaryToHostType(module, 'owned_tagged_array_ref') ||
2787
- getClassStaticTaggedArrayFields(module).length > 0;
2899
+ (hasHostFallbackObjectResultBoundary(func) ||
2900
+ hasHostTaggedHeapNullableFallbackResultBoundary(func))) ||
2901
+ module.runtime?.representations.some((representation) => representation.kind === 'specialized_object_representation' &&
2902
+ parseSpecializedObjectFields(representation.shapeName).some((field) => field.valueType === 'owned_tagged_array_ref') &&
2903
+ module.functions.some((func) => getHostAdaptableSpecializedObjectResultRepresentation(func, layouts)?.name === representation.name ||
2904
+ getHostTaggedHeapNullableSpecializedResultRepresentation(func, layouts)?.representation.name === representation.name)) || moduleUsesHostClosureBoundaryToHostType(module, 'owned_tagged_array_ref') ||
2905
+ getClassStaticTaggedArrayFields(module).length > 0 ||
2906
+ moduleUsesGeneratorStepClosureHostBridge(module);
2788
2907
  }
2789
2908
  function moduleUsedOwnedStringLiteralIds(module) {
2790
2909
  const literalIds = new Set();
2791
2910
  const builtinErrorInstanceOfUsages = collectBuiltinErrorInstanceOfUsages(module);
2792
2911
  const usesHostPromiseErrorBridge = moduleUsesHostPromiseParamBridge(module) ||
2793
- moduleUsesHostPromiseResultBridge(module);
2912
+ moduleUsesHostPromiseResultBridge(module) ||
2913
+ moduleUsesGeneratorStepClosureHostBridge(module) ||
2914
+ moduleUsesSyncTryCatchRuntime(module);
2915
+ const usesTaggedThrowBuiltinErrorBridge = moduleUsesTaggedThrowRuntime(module) &&
2916
+ moduleUsesBuiltinErrorRuntime(module);
2794
2917
  for (const func of module.functions) {
2795
2918
  collectOwnedStringLiteralIdsFromStatements(func.body, literalIds);
2796
2919
  }
@@ -2810,7 +2933,9 @@ function moduleUsedOwnedStringLiteralIds(module) {
2810
2933
  literalIds.add(getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_PROMISE_INTERNAL_ON_FULFILLED_KEY));
2811
2934
  literalIds.add(getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_PROMISE_INTERNAL_ON_REJECTED_KEY));
2812
2935
  }
2813
- if (builtinErrorInstanceOfUsages.length > 0 || usesHostPromiseErrorBridge) {
2936
+ if (builtinErrorInstanceOfUsages.length > 0 ||
2937
+ usesHostPromiseErrorBridge ||
2938
+ usesTaggedThrowBuiltinErrorBridge) {
2814
2939
  const stringLiteralIdsByText = createStringLiteralIds(module);
2815
2940
  literalIds.add(getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_BUILTIN_ERROR_INTERNAL_BRAND_KEY));
2816
2941
  literalIds.add(getRequiredStringLiteralId(stringLiteralIdsByText, 'name'));
@@ -2821,6 +2946,9 @@ function moduleUsedOwnedStringLiteralIds(module) {
2821
2946
  }
2822
2947
  return literalIds;
2823
2948
  }
2949
+ function moduleUsesBuiltinErrorRuntime(module) {
2950
+ return createStringLiteralIds(module).has(SOUNDSCRIPT_BUILTIN_ERROR_INTERNAL_BRAND_KEY);
2951
+ }
2824
2952
  function getClassStaticNumberArrayFields(module) {
2825
2953
  return (module.runtime?.classStaticFields ?? []).filter((field) => field.kind === 'class_static_number_array_field');
2826
2954
  }
@@ -2854,7 +2982,13 @@ function emitClassStaticFieldStart(module) {
2854
2982
  const stringFields = getClassStaticStringArrayFields(module);
2855
2983
  const taggedFields = getClassStaticTaggedArrayFields(module);
2856
2984
  const heapFields = getClassStaticHeapFields(module);
2857
- const fields = [...numberFields, ...booleanFields, ...stringFields, ...taggedFields, ...heapFields];
2985
+ const fields = [
2986
+ ...numberFields,
2987
+ ...booleanFields,
2988
+ ...stringFields,
2989
+ ...taggedFields,
2990
+ ...heapFields,
2991
+ ];
2858
2992
  if (fields.length === 0) {
2859
2993
  return [];
2860
2994
  }
@@ -2962,7 +3096,8 @@ function parseSpecializedObjectFields(shapeName) {
2962
3096
  if (name !== undefined &&
2963
3097
  optionality !== undefined &&
2964
3098
  valueType?.startsWith(taggedRefPrefix)) {
2965
- const [encodedKinds, encodedRepresentationName] = valueType.slice(taggedRefPrefix.length).split('#');
3099
+ const [encodedKinds, encodedRepresentationName] = valueType.slice(taggedRefPrefix.length)
3100
+ .split('#');
2966
3101
  return {
2967
3102
  name,
2968
3103
  optional: optionality === 'optional',
@@ -3145,9 +3280,7 @@ function emitSpecializedObjectTypes(layoutsByRepresentationName) {
3145
3280
  .filter((field) => isStoredSpecializedObjectField(field))
3146
3281
  .map((field) => `(field (mut ${getSpecializedObjectFieldWatType(field)}))`),
3147
3282
  ];
3148
- const structType = fieldTypes.length === 0
3149
- ? '(struct)'
3150
- : `(struct ${fieldTypes.join(' ')})`;
3283
+ const structType = fieldTypes.length === 0 ? '(struct)' : `(struct ${fieldTypes.join(' ')})`;
3151
3284
  if (!layout.baseRepresentationName) {
3152
3285
  return extendedRepresentationNames.has(layout.representation.name)
3153
3286
  ? `(type $${layout.watTypeId} (sub ${structType}))`
@@ -3222,7 +3355,8 @@ function getHostAdaptableSpecializedObjectResultRepresentation(func, layoutsByRe
3222
3355
  if (func.heapResultRepresentation?.kind !== 'specialized_object_representation') {
3223
3356
  return undefined;
3224
3357
  }
3225
- const representation = func.heapResultRepresentation;
3358
+ const representation = func
3359
+ .heapResultRepresentation;
3226
3360
  return isHostAdaptableSpecializedObjectLayout(getLayoutForRepresentationName(representation.name, layoutsByRepresentationName))
3227
3361
  ? representation
3228
3362
  : undefined;
@@ -3333,12 +3467,10 @@ function emitHostClassConstructorToHostAdaptation(classTagId, level, layoutsByRe
3333
3467
  candidate.hostStaticMethods.length > 0 ||
3334
3468
  candidate.hostStaticFields.length > 0 ||
3335
3469
  candidate.baseRepresentationName !== undefined));
3336
- return layout
3337
- ? [`${indent(level)}call $${getHostClassConstructorToHostHelperName(layout)}`]
3338
- : [
3339
- `${indent(level)}i32.const ${classTagId}`,
3340
- `${indent(level)}call $${getHostObjectClassConstructorFromTagImportFunctionName()}`,
3341
- ];
3470
+ return layout ? [`${indent(level)}call $${getHostClassConstructorToHostHelperName(layout)}`] : [
3471
+ `${indent(level)}i32.const ${classTagId}`,
3472
+ `${indent(level)}call $${getHostObjectClassConstructorFromTagImportFunctionName()}`,
3473
+ ];
3342
3474
  }
3343
3475
  function emitHostClassStaticFieldSyncLines(layout, targetLocalName, level, layoutsByRepresentationName) {
3344
3476
  return layout.hostStaticFields.flatMap((field) => {
@@ -3689,11 +3821,16 @@ function getHostObjectRememberCachedImportFunctionName() {
3689
3821
  function getHostObjectParamCacheImportFieldName(kind, cacheKey) {
3690
3822
  return `${kind}_param_cached:${encodeURIComponent(cacheKey)}`;
3691
3823
  }
3824
+ function encodeWatIdentifierHex(text) {
3825
+ return [...new TextEncoder().encode(text)]
3826
+ .map((byte) => byte.toString(16).padStart(2, '0'))
3827
+ .join('');
3828
+ }
3692
3829
  function getHostObjectLookupParamCachedImportFunctionName(cacheKey) {
3693
- return `host_object_lookup_param_cached__${sanitizeWatIdentifier(cacheKey)}`;
3830
+ return `host_object_lookup_param_cached__${encodeWatIdentifierHex(cacheKey)}`;
3694
3831
  }
3695
3832
  function getHostObjectRememberParamCachedImportFunctionName(cacheKey) {
3696
- return `host_object_remember_param_cached__${sanitizeWatIdentifier(cacheKey)}`;
3833
+ return `host_object_remember_param_cached__${encodeWatIdentifierHex(cacheKey)}`;
3697
3834
  }
3698
3835
  const HOST_FALLBACK_PARAM_CACHE_KEY = 'fallback_object';
3699
3836
  const HOST_FALLBACK_OBJECT_VALUE_KINDS = {
@@ -3734,7 +3871,8 @@ function getHostTaggedHeapNullableFallbackParamBoundaryNames(func) {
3734
3871
  .map((boundary) => boundary.name);
3735
3872
  }
3736
3873
  function hasHostTaggedHeapNullableFallbackResultBoundary(func) {
3737
- return func.hostTaggedHeapNullableResult?.representation.kind === 'fallback_object_representation';
3874
+ return func.hostTaggedHeapNullableResult?.representation.kind ===
3875
+ 'fallback_object_representation';
3738
3876
  }
3739
3877
  function collectHostBoundaryFallbackObjectUsage(module) {
3740
3878
  let needsParamBoundary = false;
@@ -4456,7 +4594,8 @@ function collectHostBoundarySpecializedObjectUsage(module, layoutsByRepresentati
4456
4594
  usageByRepresentationName.set(representation.name, current);
4457
4595
  const nestedUpdates = {
4458
4596
  ...updates,
4459
- needsResultBoundary: updates.needsResultBoundary === true || updates.needsParamCopyBack === true,
4597
+ needsResultBoundary: updates.needsResultBoundary === true ||
4598
+ updates.needsParamCopyBack === true,
4460
4599
  };
4461
4600
  for (const field of layout.fields) {
4462
4601
  if (field.heapRepresentation?.kind === 'specialized_object_representation') {
@@ -4506,7 +4645,8 @@ function collectHostBoundarySpecializedObjectUsage(module, layoutsByRepresentati
4506
4645
  }
4507
4646
  const taggedHeapNullableResult = getHostTaggedHeapNullableSpecializedResultRepresentation(func, layoutsByRepresentationName);
4508
4647
  if (taggedHeapNullableResult) {
4509
- recordUsage(taggedHeapNullableResult.representation, { needsResultBoundary: true });
4648
+ recordUsage(taggedHeapNullableResult
4649
+ .representation, { needsResultBoundary: true });
4510
4650
  }
4511
4651
  for (const boundary of func.hostHeapArrayParams ?? []) {
4512
4652
  if (boundary.representation.kind !== 'specialized_object_representation') {
@@ -4519,7 +4659,8 @@ function collectHostBoundarySpecializedObjectUsage(module, layoutsByRepresentati
4519
4659
  });
4520
4660
  }
4521
4661
  if (func.hostHeapArrayResultRepresentation?.kind === 'specialized_object_representation') {
4522
- recordUsage(func.hostHeapArrayResultRepresentation, { needsResultBoundary: true });
4662
+ recordUsage(func
4663
+ .hostHeapArrayResultRepresentation, { needsResultBoundary: true });
4523
4664
  }
4524
4665
  for (const boundary of func.hostTaggedArrayParams ?? []) {
4525
4666
  if (boundary.representation?.kind !== 'specialized_object_representation') {
@@ -4532,7 +4673,8 @@ function collectHostBoundarySpecializedObjectUsage(module, layoutsByRepresentati
4532
4673
  });
4533
4674
  }
4534
4675
  if (func.hostTaggedArrayResultKinds?.representation?.kind === 'specialized_object_representation') {
4535
- recordUsage(func.hostTaggedArrayResultKinds.representation, { needsResultBoundary: true });
4676
+ recordUsage(func.hostTaggedArrayResultKinds
4677
+ .representation, { needsResultBoundary: true });
4536
4678
  }
4537
4679
  const hasFallbackParamBoundary = getHostFallbackObjectParamBoundaryNames(func).length > 0 ||
4538
4680
  getHostTaggedHeapNullableFallbackParamBoundaryNames(func).length > 0;
@@ -4586,7 +4728,7 @@ function collectHostBoundaryFallbackObjectUsageWithHostClosures(module) {
4586
4728
  let needsParamBoundary = baseUsage?.needsParamBoundary === true;
4587
4729
  const needsParamCopyBack = baseUsage?.needsParamCopyBack === true;
4588
4730
  let needsResultBoundary = baseUsage?.needsResultBoundary === true;
4589
- for (const { signature, needsParamBoundary: needsClosureParamBoundary, needsResultBoundary: needsClosureResultBoundary } of getHostClosureBoundarySignatures(module)) {
4731
+ for (const { signature, needsParamBoundary: needsClosureParamBoundary, needsResultBoundary: needsClosureResultBoundary, } of getHostClosureBoundarySignatures(module)) {
4590
4732
  if (needsClosureParamBoundary &&
4591
4733
  (signature.resultHeapRepresentation?.kind === 'fallback_object_representation' ||
4592
4734
  signature.resultHeapArrayRepresentation?.kind === 'fallback_object_representation')) {
@@ -4672,11 +4814,13 @@ function collectHostBoundarySpecializedObjectUsageWithHostClosures(module, layou
4672
4814
  return;
4673
4815
  }
4674
4816
  for (const field of layout.hostStaticFields) {
4675
- if (field.valueKind === 'heap_object' && field.representation?.kind === 'specialized_object_representation') {
4817
+ if (field.valueKind === 'heap_object' &&
4818
+ field.representation?.kind === 'specialized_object_representation') {
4676
4819
  recordUsage(field.representation, { needsParamBoundary: true, needsResultBoundary: true });
4677
4820
  continue;
4678
4821
  }
4679
- if (field.valueKind === 'tagged_array' && field.representation?.kind === 'specialized_object_representation') {
4822
+ if (field.valueKind === 'tagged_array' &&
4823
+ field.representation?.kind === 'specialized_object_representation') {
4680
4824
  recordUsage(field.representation, { needsParamBoundary: true, needsResultBoundary: true });
4681
4825
  continue;
4682
4826
  }
@@ -4714,21 +4858,23 @@ function collectHostBoundarySpecializedObjectUsageWithHostClosures(module, layou
4714
4858
  }
4715
4859
  }
4716
4860
  }
4717
- for (const { signature, needsParamBoundary: needsClosureParamBoundary, needsResultBoundary: needsClosureResultBoundary } of getHostClosureBoundarySignatures(module)) {
4861
+ for (const { signature, needsParamBoundary: needsClosureParamBoundary, needsResultBoundary: needsClosureResultBoundary, } of getHostClosureBoundarySignatures(module)) {
4718
4862
  if (needsClosureParamBoundary &&
4719
4863
  signature.resultHeapRepresentation?.kind === 'specialized_object_representation') {
4720
4864
  recordUsage(signature.resultHeapRepresentation, { needsParamBoundary: true });
4721
4865
  }
4722
4866
  if (needsClosureParamBoundary &&
4723
4867
  signature.resultHeapArrayRepresentation?.kind === 'specialized_object_representation') {
4724
- recordUsage(signature.resultHeapArrayRepresentation, { needsParamBoundary: true });
4868
+ recordUsage(signature
4869
+ .resultHeapArrayRepresentation, { needsParamBoundary: true });
4725
4870
  }
4726
4871
  if (needsClosureResultBoundary) {
4727
4872
  if (signature.resultHeapRepresentation?.kind === 'specialized_object_representation') {
4728
4873
  recordUsage(signature.resultHeapRepresentation, { needsResultBoundary: true });
4729
4874
  }
4730
4875
  if (signature.resultHeapArrayRepresentation?.kind === 'specialized_object_representation') {
4731
- recordUsage(signature.resultHeapArrayRepresentation, { needsResultBoundary: true });
4876
+ recordUsage(signature
4877
+ .resultHeapArrayRepresentation, { needsResultBoundary: true });
4732
4878
  }
4733
4879
  for (const representation of signature.paramHeapRepresentations ?? []) {
4734
4880
  if (representation?.kind !== 'specialized_object_representation') {
@@ -4894,6 +5040,49 @@ function emitPromiseRuntimeImports(module) {
4894
5040
  `(import "soundscript_promise" "to_internal" (func ${SOUNDSCRIPT_HOST_PROMISE_TO_INTERNAL_IMPORT_NAME} (param externref) (param (ref null eq)) (result (ref null eq))))`,
4895
5041
  ];
4896
5042
  }
5043
+ function emitAsyncGeneratorRuntimeImports(module) {
5044
+ if (!moduleUsesAsyncGeneratorHostStepBridge(module) &&
5045
+ !moduleUsesAsyncGeneratorHostResultBridge(module)) {
5046
+ return [];
5047
+ }
5048
+ return [
5049
+ `(import "soundscript_async_generator" "step" (func ${SOUNDSCRIPT_HOST_ASYNC_GENERATOR_STEP_IMPORT_NAME} (param externref) (param f64) (param externref) (param (ref null eq))))`,
5050
+ ...(moduleUsesAsyncGeneratorHostResultBridge(module)
5051
+ ? [
5052
+ `(import "soundscript_async_generator" "wrap" (func ${SOUNDSCRIPT_HOST_ASYNC_GENERATOR_WRAP_IMPORT_NAME} (param externref) (result externref)))`,
5053
+ ]
5054
+ : []),
5055
+ ];
5056
+ }
5057
+ function emitSyncGeneratorRuntimeImports(module) {
5058
+ if (!moduleUsesSyncGeneratorHostResultBridge(module)) {
5059
+ return [];
5060
+ }
5061
+ return [
5062
+ `(import "soundscript_generator" "wrap" (func ${SOUNDSCRIPT_HOST_SYNC_GENERATOR_WRAP_IMPORT_NAME} (param externref) (result externref)))`,
5063
+ ];
5064
+ }
5065
+ const SOUNDSCRIPT_HOST_THROW_IMPORT_NAME = '$host_throw_value';
5066
+ const SOUNDSCRIPT_THROW_TAGGED_HELPER_NAME = '__soundscript_throw_tagged';
5067
+ const SOUNDSCRIPT_TRY_CATCH_TAGGED_HELPER_NAME = '__soundscript_try_catch_tagged';
5068
+ function emitTaggedThrowRuntimeImports(module) {
5069
+ if (!moduleUsesTaggedThrowRuntime(module)) {
5070
+ return [];
5071
+ }
5072
+ return [
5073
+ `(import "soundscript_throw" "throw" (func ${SOUNDSCRIPT_HOST_THROW_IMPORT_NAME} (param externref)))`,
5074
+ ];
5075
+ }
5076
+ function emitSyncTryCatchRuntimeImports(module) {
5077
+ if (!moduleUsesSyncTryCatchRuntime(module)) {
5078
+ return [];
5079
+ }
5080
+ return [
5081
+ `(import "soundscript_throw" "try_tagged" (func ${SOUNDSCRIPT_HOST_TRY_TAGGED_IMPORT_NAME} (param externref) (result externref)))`,
5082
+ `(import "soundscript_throw" "try_result_threw" (func ${SOUNDSCRIPT_HOST_TRY_RESULT_THREW_IMPORT_NAME} (param externref) (result i32)))`,
5083
+ `(import "soundscript_throw" "try_result_value" (func ${SOUNDSCRIPT_HOST_TRY_RESULT_VALUE_IMPORT_NAME} (param externref) (result externref)))`,
5084
+ ];
5085
+ }
4897
5086
  function emitTaggedValueType(runtime) {
4898
5087
  if (!runtime?.representations.some((representation) => representation.kind === 'tagged_value_representation')) {
4899
5088
  return [];
@@ -4904,22 +5093,34 @@ function emitTaggedValueHelpers(module, stringRuntimeLayout) {
4904
5093
  if (!module.runtime?.representations.some((representation) => representation.kind === 'tagged_value_representation')) {
4905
5094
  return [];
4906
5095
  }
4907
- const usesNull = moduleUsesTaggedNullRuntime(module) || moduleUsesHostPromiseBridge(module);
5096
+ const usesTaggedThrowRuntime = moduleUsesTaggedThrowRuntime(module);
5097
+ const usesAsyncGeneratorBridge = moduleUsesGeneratorStepClosureHostBridge(module);
5098
+ const usesNull = moduleUsesTaggedNullRuntime(module) || moduleUsesHostPromiseBridge(module) ||
5099
+ usesAsyncGeneratorBridge ||
5100
+ usesTaggedThrowRuntime ||
5101
+ moduleUsesSyncTryCatchRuntime(module);
4908
5102
  const taggedHostBoundaryUsage = getTaggedHostBoundaryUsage(module);
4909
5103
  const usesHostFallbackObjectBoundary = moduleUsesHostFallbackObjectBoundary(module);
4910
- const usesHostPromiseParamBoundary = moduleUsesHostPromiseParamBridge(module);
4911
- const usesHostPromiseResultBoundary = moduleUsesHostPromiseResultBridge(module);
5104
+ const usesHostPromiseParamBoundary = moduleUsesHostPromiseParamBridge(module) ||
5105
+ usesAsyncGeneratorBridge;
5106
+ const usesHostPromiseResultBoundary = moduleUsesHostPromiseResultBridge(module) ||
5107
+ usesAsyncGeneratorBridge;
4912
5108
  const specializedTaggedArrayFieldBoundaryUsage = getSpecializedTaggedArrayFieldBoundaryUsage(module);
4913
5109
  const usesTaggedArrayHostStringBoundary = module.functions.some((func) => func.hostTaggedArrayParams?.some((param) => param.includesString === true) === true ||
4914
5110
  func.hostTaggedArrayResultKinds?.includesString === true) || specializedTaggedArrayFieldBoundaryUsage.usesParamStringBoundary ||
4915
5111
  specializedTaggedArrayFieldBoundaryUsage.usesResultStringBoundary;
4916
- const usesTaggedArrayHostResultStringBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesString === true) || specializedTaggedArrayFieldBoundaryUsage.usesResultStringBoundary;
5112
+ const usesTaggedArrayHostResultStringBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesString === true) ||
5113
+ specializedTaggedArrayFieldBoundaryUsage.usesResultStringBoundary;
4917
5114
  const usesTaggedPrimitiveHostStringBoundary = taggedHostBoundaryUsage.usesStringBoundary ||
4918
5115
  usesHostFallbackObjectBoundary ||
4919
5116
  usesHostPromiseParamBoundary ||
4920
- usesHostPromiseResultBoundary;
5117
+ usesHostPromiseResultBoundary ||
5118
+ usesTaggedThrowRuntime ||
5119
+ moduleUsesSyncTryCatchRuntime(module);
4921
5120
  const usesTaggedPrimitiveHostResultStringBoundary = taggedHostBoundaryUsage.usesResultStringBoundary || usesHostFallbackObjectBoundary ||
4922
- usesHostPromiseResultBoundary;
5121
+ usesHostPromiseResultBoundary ||
5122
+ usesTaggedThrowRuntime ||
5123
+ moduleUsesSyncTryCatchRuntime(module);
4923
5124
  const usesTaggedString = moduleUsesTaggedStringRuntime(module) ||
4924
5125
  usesTaggedPrimitiveHostStringBoundary ||
4925
5126
  usesTaggedArrayHostStringBoundary;
@@ -5128,8 +5329,11 @@ function emitStringCodePointAtHelper(module) {
5128
5329
  ];
5129
5330
  }
5130
5331
  function emitStringRuntimeImports(module) {
5131
- const usesHostPromiseParamBoundary = moduleUsesHostPromiseParamBridge(module);
5132
- const usesHostPromiseResultBoundary = moduleUsesHostPromiseResultBridge(module);
5332
+ const usesTaggedThrowRuntime = moduleUsesTaggedThrowRuntime(module);
5333
+ const usesHostPromiseParamBoundary = moduleUsesHostPromiseParamBridge(module) ||
5334
+ moduleUsesGeneratorStepClosureHostBridge(module);
5335
+ const usesHostPromiseResultBoundary = moduleUsesHostPromiseResultBridge(module) ||
5336
+ moduleUsesGeneratorStepClosureHostBridge(module);
5133
5337
  const usesOwnedNumberArrayJoinRuntime = moduleUsesOwnedNumberArrayJoin(module);
5134
5338
  const usesOwnedBooleanArrayJoinRuntime = moduleUsesOwnedBooleanArrayJoin(module);
5135
5339
  const usesStringCodePointAt = moduleUsesStringCodePointAtRuntime(module);
@@ -5150,10 +5354,13 @@ function emitStringRuntimeImports(module) {
5150
5354
  const usesStringSubstring = moduleUsesStringSubstringRuntime(module);
5151
5355
  const taggedHostBoundaryUsage = getTaggedHostBoundaryUsage(module);
5152
5356
  const usesHostFallbackObjectBoundary = moduleUsesHostFallbackObjectBoundary(module);
5357
+ const usesSyncTryCatchRuntime = moduleUsesSyncTryCatchRuntime(module);
5153
5358
  const usesTaggedPrimitiveHostParamBoundary = taggedHostBoundaryUsage.usesHostBoundary ||
5154
5359
  usesHostFallbackObjectBoundary ||
5155
5360
  usesHostPromiseParamBoundary ||
5156
- usesHostPromiseResultBoundary;
5361
+ usesHostPromiseResultBoundary ||
5362
+ usesTaggedThrowRuntime ||
5363
+ usesSyncTryCatchRuntime;
5157
5364
  const usesTaggedPrimitiveHostResultBoundary = taggedHostBoundaryUsage.usesHostBoundary ||
5158
5365
  usesHostFallbackObjectBoundary ||
5159
5366
  usesHostPromiseResultBoundary;
@@ -5163,55 +5370,71 @@ function emitStringRuntimeImports(module) {
5163
5370
  const usesTaggedArrayHostResultBoundary = moduleUsesOwnedTaggedArrayHostResultBoundary(module);
5164
5371
  const usesTaggedArrayHostParamStringBoundary = module.functions.some((func) => func.hostTaggedArrayParams?.some((param) => param.includesString) === true) || specializedTaggedArrayFieldBoundaryUsage.usesParamStringBoundary ||
5165
5372
  classStaticTaggedArrayFields.some((field) => field.includesString);
5166
- const usesTaggedArrayHostResultStringBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesString === true) || specializedTaggedArrayFieldBoundaryUsage.usesResultStringBoundary ||
5373
+ const usesTaggedArrayHostResultStringBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesString === true) ||
5374
+ specializedTaggedArrayFieldBoundaryUsage.usesResultStringBoundary ||
5167
5375
  classStaticTaggedArrayFields.some((field) => field.includesString);
5168
5376
  const usesTaggedArrayHostParamNumberBoundary = module.functions.some((func) => func.hostTaggedArrayParams?.some((param) => param.includesNumber) === true) || specializedTaggedArrayFieldBoundaryUsage.usesParamNumberBoundary ||
5169
5377
  classStaticTaggedArrayFields.some((field) => field.includesNumber);
5170
- const usesTaggedArrayHostResultNumberBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesNumber === true) || specializedTaggedArrayFieldBoundaryUsage.usesResultNumberBoundary ||
5378
+ const usesTaggedArrayHostResultNumberBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesNumber === true) ||
5379
+ specializedTaggedArrayFieldBoundaryUsage.usesResultNumberBoundary ||
5171
5380
  classStaticTaggedArrayFields.some((field) => field.includesNumber);
5172
5381
  const usesTaggedArrayHostParamBooleanBoundary = module.functions.some((func) => func.hostTaggedArrayParams?.some((param) => param.includesBoolean) === true) || specializedTaggedArrayFieldBoundaryUsage.usesParamBooleanBoundary ||
5173
5382
  classStaticTaggedArrayFields.some((field) => field.includesBoolean);
5174
- const usesTaggedArrayHostResultBooleanBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesBoolean === true) || specializedTaggedArrayFieldBoundaryUsage.usesResultBooleanBoundary ||
5383
+ const usesTaggedArrayHostResultBooleanBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesBoolean === true) ||
5384
+ specializedTaggedArrayFieldBoundaryUsage.usesResultBooleanBoundary ||
5175
5385
  classStaticTaggedArrayFields.some((field) => field.includesBoolean);
5176
5386
  const usesTaggedArrayHostParamUndefinedBoundary = module.functions.some((func) => func.hostTaggedArrayParams?.some((param) => param.includesUndefined) === true) || specializedTaggedArrayFieldBoundaryUsage.usesParamUndefinedBoundary ||
5177
5387
  classStaticTaggedArrayFields.some((field) => field.includesUndefined);
5178
- const usesTaggedArrayHostResultUndefinedBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesUndefined === true) || specializedTaggedArrayFieldBoundaryUsage.usesResultUndefinedBoundary ||
5388
+ const usesTaggedArrayHostResultUndefinedBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesUndefined === true) ||
5389
+ specializedTaggedArrayFieldBoundaryUsage.usesResultUndefinedBoundary ||
5179
5390
  classStaticTaggedArrayFields.some((field) => field.includesUndefined);
5180
5391
  const usesTaggedPrimitiveHostParamStringBoundary = taggedHostBoundaryUsage.usesStringBoundary ||
5181
5392
  usesTaggedArrayHostParamStringBoundary ||
5182
5393
  usesHostFallbackObjectBoundary ||
5183
5394
  usesHostPromiseParamBoundary ||
5184
- usesHostPromiseResultBoundary;
5395
+ usesHostPromiseResultBoundary ||
5396
+ usesTaggedThrowRuntime ||
5397
+ usesSyncTryCatchRuntime;
5185
5398
  const usesTaggedPrimitiveHostResultStringBoundary = taggedHostBoundaryUsage.usesStringBoundary ||
5186
5399
  usesTaggedArrayHostParamStringBoundary ||
5187
5400
  usesTaggedArrayHostResultStringBoundary ||
5188
5401
  usesHostFallbackObjectBoundary ||
5189
- usesHostPromiseResultBoundary;
5402
+ usesHostPromiseResultBoundary ||
5403
+ usesTaggedThrowRuntime;
5190
5404
  const usesTaggedPrimitiveHostParamNumberBoundary = taggedHostBoundaryUsage.usesNumberBoundary ||
5191
5405
  usesTaggedArrayHostParamNumberBoundary ||
5192
5406
  usesHostFallbackObjectBoundary ||
5193
5407
  usesHostPromiseParamBoundary ||
5194
- usesHostPromiseResultBoundary;
5195
- const usesTaggedPrimitiveHostResultNumberBoundary = taggedHostBoundaryUsage.usesNumberBoundary || usesTaggedArrayHostParamNumberBoundary ||
5408
+ usesHostPromiseResultBoundary ||
5409
+ usesTaggedThrowRuntime ||
5410
+ usesSyncTryCatchRuntime;
5411
+ const usesTaggedPrimitiveHostResultNumberBoundary = taggedHostBoundaryUsage.usesNumberBoundary ||
5412
+ usesTaggedArrayHostParamNumberBoundary ||
5196
5413
  usesTaggedArrayHostResultNumberBoundary ||
5197
5414
  usesOwnedNumberArrayJoinRuntime ||
5198
5415
  usesHostFallbackObjectBoundary ||
5199
- usesHostPromiseResultBoundary;
5416
+ usesHostPromiseResultBoundary ||
5417
+ usesTaggedThrowRuntime;
5200
5418
  const usesTaggedPrimitiveHostParamBooleanBoundary = taggedHostBoundaryUsage.usesBooleanBoundary ||
5201
5419
  usesTaggedArrayHostParamBooleanBoundary ||
5202
5420
  usesHostFallbackObjectBoundary ||
5203
5421
  usesHostPromiseParamBoundary ||
5204
- usesHostPromiseResultBoundary;
5422
+ usesHostPromiseResultBoundary ||
5423
+ usesTaggedThrowRuntime ||
5424
+ usesSyncTryCatchRuntime;
5205
5425
  const usesTaggedPrimitiveHostResultBooleanBoundary = taggedHostBoundaryUsage.usesBooleanBoundary || usesTaggedArrayHostParamBooleanBoundary ||
5206
5426
  usesTaggedArrayHostResultBooleanBoundary ||
5207
5427
  usesOwnedBooleanArrayJoinRuntime ||
5208
5428
  usesHostFallbackObjectBoundary ||
5209
- usesHostPromiseResultBoundary;
5429
+ usesHostPromiseResultBoundary ||
5430
+ usesTaggedThrowRuntime;
5210
5431
  const usesTaggedPrimitiveHostResultUndefinedBoundary = taggedHostBoundaryUsage.usesUndefinedBoundary ||
5211
5432
  usesTaggedArrayHostParamUndefinedBoundary ||
5212
5433
  usesTaggedArrayHostResultUndefinedBoundary ||
5213
5434
  usesHostFallbackObjectBoundary ||
5214
- usesHostPromiseResultBoundary;
5435
+ usesHostPromiseResultBoundary ||
5436
+ usesTaggedThrowRuntime ||
5437
+ usesSyncTryCatchRuntime;
5215
5438
  const usesHostLengthViewParamBoundary = moduleUsesHostLengthViewParamBoundary(module);
5216
5439
  const usesHostLengthViewResultBoundary = moduleUsesHostLengthViewResultBoundary(module);
5217
5440
  const usesOwnedArrayHostParamBoundary = moduleUsesOwnedArrayHostParamBoundary(module);
@@ -5221,12 +5444,15 @@ function emitStringRuntimeImports(module) {
5221
5444
  usesOwnedArrayHostParamBoundary ||
5222
5445
  usesOwnedArrayHostResultBoundary ||
5223
5446
  usesOwnedNumberArrayJoinRuntime ||
5224
- usesOwnedBooleanArrayJoinRuntime;
5447
+ usesOwnedBooleanArrayJoinRuntime ||
5448
+ usesSyncTryCatchRuntime;
5225
5449
  const usesStringToOwned = moduleUsesStringToOwnedRuntime(module) ||
5226
5450
  usesTaggedPrimitiveHostParamStringBoundary ||
5227
5451
  usesOwnedArrayHostParamBoundary ||
5228
5452
  usesOwnedNumberArrayJoinRuntime ||
5229
- usesOwnedBooleanArrayJoinRuntime;
5453
+ usesOwnedBooleanArrayJoinRuntime ||
5454
+ usesTaggedThrowRuntime ||
5455
+ usesSyncTryCatchRuntime;
5230
5456
  const usesStringConcat = moduleUsesStringConcatRuntime(module) || usesOwnedStringToHost;
5231
5457
  const usesStringEquality = moduleUsesStringEqualityRuntime(module);
5232
5458
  const usesStringLiterals = moduleUsesStringLiteralRuntime(module);
@@ -5394,7 +5620,10 @@ function getHostClosureBoundarySignatures(module) {
5394
5620
  if (field.valueType !== 'closure_ref' || field.closureSignatureId === undefined) {
5395
5621
  continue;
5396
5622
  }
5397
- markUsage(field.closureSignatureId, { needsParamBoundary: true, needsResultBoundary: true });
5623
+ markUsage(field.closureSignatureId, {
5624
+ needsParamBoundary: true,
5625
+ needsResultBoundary: true,
5626
+ });
5398
5627
  }
5399
5628
  }
5400
5629
  if (flags.needsResultBoundary) {
@@ -5402,10 +5631,16 @@ function getHostClosureBoundarySignatures(module) {
5402
5631
  if (field.valueType !== 'closure_ref' || field.closureSignatureId === undefined) {
5403
5632
  continue;
5404
5633
  }
5405
- markUsage(field.closureSignatureId, { needsParamBoundary: true, needsResultBoundary: true });
5634
+ markUsage(field.closureSignatureId, {
5635
+ needsParamBoundary: true,
5636
+ needsResultBoundary: true,
5637
+ });
5406
5638
  }
5407
5639
  for (const method of layout.hostMethods) {
5408
- markUsage(method.closureSignatureId, { needsParamBoundary: true, needsResultBoundary: true });
5640
+ markUsage(method.closureSignatureId, {
5641
+ needsParamBoundary: true,
5642
+ needsResultBoundary: true,
5643
+ });
5409
5644
  }
5410
5645
  }
5411
5646
  for (const field of layout.fields) {
@@ -5459,11 +5694,13 @@ function getHostClosureBoundarySignatures(module) {
5459
5694
  });
5460
5695
  }
5461
5696
  for (const field of layout.hostStaticFields) {
5462
- if (field.valueKind === 'heap_object' && field.representation?.kind === 'specialized_object_representation') {
5697
+ if (field.valueKind === 'heap_object' &&
5698
+ field.representation?.kind === 'specialized_object_representation') {
5463
5699
  markSpecializedLayoutClosureUsage(field.representation, { needsResultBoundary: true });
5464
5700
  continue;
5465
5701
  }
5466
- if (field.valueKind === 'tagged_array' && field.representation?.kind === 'specialized_object_representation') {
5702
+ if (field.valueKind === 'tagged_array' &&
5703
+ field.representation?.kind === 'specialized_object_representation') {
5467
5704
  markSpecializedLayoutClosureUsage(field.representation, { needsResultBoundary: true });
5468
5705
  continue;
5469
5706
  }
@@ -5514,7 +5751,9 @@ function getHostClosureBoundarySignatures(module) {
5514
5751
  }
5515
5752
  }
5516
5753
  }
5517
- if (moduleUsesHostPromiseResultBridge(module)) {
5754
+ if (moduleUsesHostPromiseResultBridge(module) ||
5755
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
5756
+ moduleUsesAsyncGeneratorHostResultBridge(module)) {
5518
5757
  const promiseHandlerSignature = getPromiseFulfilledHandlerClosureSignature(module);
5519
5758
  if (promiseHandlerSignature) {
5520
5759
  markUsage(promiseHandlerSignature.id, {
@@ -5523,6 +5762,20 @@ function getHostClosureBoundarySignatures(module) {
5523
5762
  });
5524
5763
  }
5525
5764
  }
5765
+ if (moduleUsesGeneratorStepClosureHostBridge(module)) {
5766
+ const generatorStepSignature = getGeneratorStepClosureSignature(module);
5767
+ if (generatorStepSignature) {
5768
+ markUsage(generatorStepSignature.id, {
5769
+ needsParamBoundary: true,
5770
+ needsResultBoundary: true,
5771
+ });
5772
+ }
5773
+ }
5774
+ if (module.syncTryCatchClosureSignatureId !== undefined) {
5775
+ markUsage(module.syncTryCatchClosureSignatureId, {
5776
+ needsResultBoundary: true,
5777
+ });
5778
+ }
5526
5779
  for (const signature of module.closureSignatures ?? []) {
5527
5780
  if (signature.resultClassConstructorTagId !== undefined) {
5528
5781
  markClassConstructorSurfaceUsage(signature.resultClassConstructorTagId);
@@ -5559,7 +5812,8 @@ function getHostClosureBoundarySignatures(module) {
5559
5812
  markSpecializedLayoutClosureUsage(signature.resultHeapRepresentation, { needsParamBoundary: true });
5560
5813
  }
5561
5814
  if (signature.resultHeapArrayRepresentation?.kind === 'specialized_object_representation') {
5562
- markSpecializedLayoutClosureUsage(signature.resultHeapArrayRepresentation, { needsParamBoundary: true });
5815
+ markSpecializedLayoutClosureUsage(signature
5816
+ .resultHeapArrayRepresentation, { needsParamBoundary: true });
5563
5817
  }
5564
5818
  for (const representation of signature.paramHeapRepresentations ?? []) {
5565
5819
  if (representation?.kind !== 'specialized_object_representation') {
@@ -5579,7 +5833,8 @@ function getHostClosureBoundarySignatures(module) {
5579
5833
  markSpecializedLayoutClosureUsage(signature.resultHeapRepresentation, { needsResultBoundary: true });
5580
5834
  }
5581
5835
  if (signature.resultHeapArrayRepresentation?.kind === 'specialized_object_representation') {
5582
- markSpecializedLayoutClosureUsage(signature.resultHeapArrayRepresentation, { needsResultBoundary: true });
5836
+ markSpecializedLayoutClosureUsage(signature
5837
+ .resultHeapArrayRepresentation, { needsResultBoundary: true });
5583
5838
  }
5584
5839
  for (const representation of signature.paramHeapRepresentations ?? []) {
5585
5840
  if (representation?.kind !== 'specialized_object_representation') {
@@ -5626,10 +5881,10 @@ function getHostClosureBoundarySignatures(module) {
5626
5881
  }
5627
5882
  function emitClosureRuntimeImports(module) {
5628
5883
  const hostClosureSignatures = getHostClosureBoundarySignatures(module);
5629
- if (hostClosureSignatures.length === 0) {
5884
+ if (hostClosureSignatures.length === 0 && module.syncTryCatchClosureSignatureId === undefined) {
5630
5885
  return [];
5631
5886
  }
5632
- return hostClosureSignatures.flatMap(({ signature, needsResultBoundary }) => [
5887
+ const imports = hostClosureSignatures.flatMap(({ signature, needsResultBoundary }) => [
5633
5888
  '(import "soundscript_closure" ' +
5634
5889
  `"call_${signature.id}" (func $host_closure_call_${signature.id} (param externref)${getHostClosureSourceParamBoundaries(signature).map((param) => ` (param ${param.classTagId !== undefined ? 'externref' : getHostClosureBoundaryWatType(param.type)})`).join('')} (result ${signature.resultClassConstructorTagId !== undefined
5635
5890
  ? 'externref'
@@ -5640,18 +5895,29 @@ function emitClosureRuntimeImports(module) {
5640
5895
  ]
5641
5896
  : []),
5642
5897
  ]);
5898
+ if (module.syncTryCatchClosureSignatureId !== undefined &&
5899
+ !hostClosureSignatures.some(({ signatureId }) => signatureId === module.syncTryCatchClosureSignatureId)) {
5900
+ imports.push(`(import "soundscript_closure" "to_host_${module.syncTryCatchClosureSignatureId}" (func $host_closure_to_host_${module.syncTryCatchClosureSignatureId} (param (ref null eq)) (result externref)))`);
5901
+ }
5902
+ return imports;
5643
5903
  }
5644
5904
  function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackPropertyKeyIdsByKey) {
5645
5905
  const usageByRepresentationName = collectHostBoundarySpecializedObjectUsageWithHostClosures(module, layoutsByRepresentationName);
5646
5906
  const fallbackUsage = collectHostBoundaryFallbackObjectUsageWithHostClosures(module);
5647
5907
  const closureSignatures = module.closureSignatures ?? [];
5648
- const hasClassConstructorParamBoundary = module.functions.some((func) => (func.hostClassConstructorParams?.length ?? 0) > 0) || closureSignatures.some((signature) => signature.sourceParamClassConstructorTagIds?.some((tagId) => tagId !== undefined) === true);
5649
- const hasClassConstructorResultBoundary = module.functions.some((func) => func.hostClassConstructorResultTagId !== undefined) || closureSignatures.some((signature) => signature.resultClassConstructorTagId !== undefined);
5908
+ const hasClassConstructorParamBoundary = module.functions.some((func) => (func.hostClassConstructorParams?.length ?? 0) > 0) ||
5909
+ closureSignatures.some((signature) => signature.sourceParamClassConstructorTagIds?.some((tagId) => tagId !== undefined) === true);
5910
+ const hasClassConstructorResultBoundary = module.functions.some((func) => func.hostClassConstructorResultTagId !== undefined) ||
5911
+ closureSignatures.some((signature) => signature.resultClassConstructorTagId !== undefined);
5650
5912
  const hostDynamicCollectionParams = module.functions.flatMap((func) => func.hostDynamicCollectionParams ?? []);
5651
5913
  const usesHostMapParamBoundary = hostDynamicCollectionParams.some((param) => param.collectionKind === 'map');
5652
5914
  const usesHostSetParamBoundary = hostDynamicCollectionParams.some((param) => param.collectionKind === 'set');
5653
5915
  const usesHostPromiseErrorBridge = moduleUsesHostPromiseParamBridge(module) ||
5654
- moduleUsesHostPromiseResultBridge(module);
5916
+ moduleUsesHostPromiseResultBridge(module) ||
5917
+ moduleUsesGeneratorStepClosureHostBridge(module);
5918
+ const usesTaggedThrowBuiltinErrorBridge = moduleUsesTaggedThrowRuntime(module) &&
5919
+ moduleUsesBuiltinErrorRuntime(module);
5920
+ const usesSyncTryCatch = moduleUsesSyncTryCatchRuntime(module);
5655
5921
  const hostMapValueKinds = new Set(hostDynamicCollectionParams
5656
5922
  .filter((param) => param.collectionKind === 'map')
5657
5923
  .map((param) => param.valueKind));
@@ -5664,7 +5930,9 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5664
5930
  !hasClassConstructorResultBoundary &&
5665
5931
  !usesHostMapParamBoundary &&
5666
5932
  !usesHostSetParamBoundary &&
5667
- !usesHostPromiseErrorBridge) {
5933
+ !usesHostPromiseErrorBridge &&
5934
+ !usesTaggedThrowBuiltinErrorBridge &&
5935
+ !usesSyncTryCatch) {
5668
5936
  return [];
5669
5937
  }
5670
5938
  const hostFallbackPropertyNames = collectHostBoundaryFallbackPropertyNames(module);
@@ -5677,17 +5945,21 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5677
5945
  usage.layout.fields.some((field) => field.valueType === 'heap_ref'));
5678
5946
  const hostSpecializedTaggedArrayFieldsNeedSame = [...usageByRepresentationName.values()].some((usage) => (usage.needsParamCopyBack || usage.needsResultBoundary) &&
5679
5947
  usage.layout.fields.some((field) => field.valueType === 'owned_tagged_array_ref' && field.heapRepresentation !== undefined));
5680
- const hostSpecializedTaggedArrayFieldsNeedIdentityLookup = [...usageByRepresentationName.values()].some((usage) => (usage.needsParamCopyBack || usage.needsResultBoundary) &&
5948
+ const hostSpecializedTaggedArrayFieldsNeedIdentityLookup = [...usageByRepresentationName.values()]
5949
+ .some((usage) => (usage.needsParamCopyBack || usage.needsResultBoundary) &&
5681
5950
  usage.layout.fields.some((field) => field.valueType === 'owned_tagged_array_ref' && field.heapRepresentation !== undefined));
5682
5951
  const needsEmpty = [...usageByRepresentationName.values()].some((usage) => usage.needsResultBoundary) ||
5683
5952
  fallbackUsage?.needsResultBoundary === true ||
5684
- usesHostPromiseErrorBridge;
5953
+ usesHostPromiseErrorBridge ||
5954
+ usesTaggedThrowBuiltinErrorBridge ||
5955
+ usesSyncTryCatch;
5685
5956
  const needsSame = [...usageByRepresentationName.values()].some((usage) => usage.needsParamBoundary) ||
5686
5957
  hostSpecializedHeapFieldsNeedSame ||
5687
5958
  hostSpecializedTaggedArrayFieldsNeedSame ||
5688
5959
  fallbackUsage?.needsParamBoundary === true ||
5689
5960
  (fallbackUsage?.needsResultBoundary === true &&
5690
- (hostFallbackHeapProperties.size > 0 || [...hostFallbackTaggedArrayProperties.values()].some((property) => property.representation !== undefined)));
5961
+ (hostFallbackHeapProperties.size > 0 ||
5962
+ [...hostFallbackTaggedArrayProperties.values()].some((property) => property.representation !== undefined)));
5691
5963
  const needsClassTagGetter = [...usageByRepresentationName.values()].some((usage) => usage.needsParamBoundary &&
5692
5964
  usage.layout.classTagId !== undefined &&
5693
5965
  getSpecializedSubtypeLayouts(usage.layout, layoutsByRepresentationName).length > 0) || hasClassConstructorParamBoundary || hasClassConstructorResultBoundary ||
@@ -5707,7 +5979,8 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5707
5979
  (fallbackUsage?.needsResultBoundary === true &&
5708
5980
  [...hostFallbackTaggedArrayProperties.values()].some((property) => property.representation !== undefined)) ||
5709
5981
  fallbackUsage?.needsResultBoundary === true;
5710
- const needsIdentityRemember = [...usageByRepresentationName.values()].length > 0 || fallbackUsage !== undefined;
5982
+ const needsIdentityRemember = [...usageByRepresentationName.values()].length > 0 ||
5983
+ fallbackUsage !== undefined;
5711
5984
  const getterImports = new Map();
5712
5985
  const setterImports = new Map();
5713
5986
  const hasImports = new Map();
@@ -5715,11 +5988,18 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5715
5988
  const paramCacheLookupImports = new Map();
5716
5989
  const paramCacheRememberImports = new Map();
5717
5990
  const collectionImports = new Map();
5718
- if (usesHostPromiseErrorBridge) {
5991
+ if (usesHostPromiseErrorBridge || usesTaggedThrowBuiltinErrorBridge || usesSyncTryCatch) {
5992
+ getterImports.set('get_tagged:name', `(import "soundscript_object" "get_tagged:${getHostObjectPropertyToken('name')}" (func $${getHostObjectImportFunctionName('get_tagged', 'name')} (param externref) (result externref)))`);
5719
5993
  getterImports.set('get_tagged:message', `(import "soundscript_object" "get_tagged:${getHostObjectPropertyToken('message')}" (func $${getHostObjectImportFunctionName('get_tagged', 'message')} (param externref) (result externref)))`);
5994
+ }
5995
+ if (usesHostPromiseErrorBridge || usesTaggedThrowBuiltinErrorBridge || usesSyncTryCatch) {
5720
5996
  setterImports.set('set_tagged:name', `(import "soundscript_object" "set_tagged:${getHostObjectPropertyToken('name')}" (func $${getHostObjectImportFunctionName('set_tagged', 'name')} (param externref) (param externref)))`);
5721
5997
  setterImports.set('set_tagged:message', `(import "soundscript_object" "set_tagged:${getHostObjectPropertyToken('message')}" (func $${getHostObjectImportFunctionName('set_tagged', 'message')} (param externref) (param externref)))`);
5722
5998
  }
5999
+ if (moduleUsesGeneratorStepClosureHostBridge(module)) {
6000
+ setterImports.set('set_tagged:value', `(import "soundscript_object" "set_tagged:${getHostObjectPropertyToken('value')}" (func $${getHostObjectImportFunctionName('set_tagged', 'value')} (param externref) (param externref)))`);
6001
+ setterImports.set('set_tagged:done', `(import "soundscript_object" "set_tagged:${getHostObjectPropertyToken('done')}" (func $${getHostObjectImportFunctionName('set_tagged', 'done')} (param externref) (param externref)))`);
6002
+ }
5723
6003
  const closureFunctionById = new Map(module.functions
5724
6004
  .filter((func) => func.closureFunctionId !== undefined)
5725
6005
  .map((func) => [func.closureFunctionId, func]));
@@ -5732,11 +6012,12 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5732
6012
  });
5733
6013
  const needsFieldSyncCopyBack = usage.needsResultBoundary &&
5734
6014
  usage.layout.fields.some((field) => (field.methodClosureFunctionIds?.length ?? 0) > 0);
5735
- if (usage.needsParamBoundary) {
6015
+ if (usage.needsParamBoundary || usage.needsResultBoundary) {
5736
6016
  paramCacheLookupImports.set(usage.layout.representation.name, `(import "soundscript_object" "${getHostObjectParamCacheImportFieldName('lookup', usage.layout.representation.name)}" (func $${getHostObjectLookupParamCachedImportFunctionName(usage.layout.representation.name)} (param externref) (result (ref null eq))))`);
5737
6017
  paramCacheRememberImports.set(usage.layout.representation.name, `(import "soundscript_object" "${getHostObjectParamCacheImportFieldName('remember', usage.layout.representation.name)}" (func $${getHostObjectRememberParamCachedImportFunctionName(usage.layout.representation.name)} (param externref) (param (ref null eq))))`);
5738
6018
  }
5739
- if (usage.needsParamBoundary || usage.needsResultBoundary || needsMethodSyncCopyBack || needsFieldSyncCopyBack) {
6019
+ if (usage.needsParamBoundary || usage.needsResultBoundary || needsMethodSyncCopyBack ||
6020
+ needsFieldSyncCopyBack) {
5740
6021
  for (const field of usage.layout.fields) {
5741
6022
  if (field.valueType === 'class_constructor_ref') {
5742
6023
  continue;
@@ -5776,7 +6057,7 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5776
6057
  if (fallbackUsage) {
5777
6058
  const needsFallbackMethodSyncCopyBack = fallbackUsage.needsResultBoundary &&
5778
6059
  [...hostFallbackMethodClosureFunctionIds.values()].some((functionIds) => functionIds.length > 0);
5779
- if (fallbackUsage.needsParamBoundary) {
6060
+ if (fallbackUsage.needsParamBoundary || fallbackUsage.needsResultBoundary) {
5780
6061
  paramCacheLookupImports.set(HOST_FALLBACK_PARAM_CACHE_KEY, `(import "soundscript_object" "${getHostObjectParamCacheImportFieldName('lookup', HOST_FALLBACK_PARAM_CACHE_KEY)}" (func $${getHostObjectLookupParamCachedImportFunctionName(HOST_FALLBACK_PARAM_CACHE_KEY)} (param externref) (result (ref null eq))))`);
5781
6062
  paramCacheRememberImports.set(HOST_FALLBACK_PARAM_CACHE_KEY, `(import "soundscript_object" "${getHostObjectParamCacheImportFieldName('remember', HOST_FALLBACK_PARAM_CACHE_KEY)}" (func $${getHostObjectRememberParamCachedImportFunctionName(HOST_FALLBACK_PARAM_CACHE_KEY)} (param externref) (param (ref null eq))))`);
5782
6063
  }
@@ -5784,7 +6065,9 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5784
6065
  const closureSignatureId = hostFallbackClosureProperties.get(propertyName);
5785
6066
  const classConstructorTagId = hostFallbackClassConstructorProperties.get(propertyName);
5786
6067
  const methodClosureFunctionIds = hostFallbackMethodClosureFunctionIds.get(propertyName) ?? [];
5787
- if (fallbackUsage.needsParamBoundary || needsFallbackMethodSyncCopyBack) {
6068
+ if (fallbackUsage.needsParamBoundary ||
6069
+ fallbackUsage.needsResultBoundary ||
6070
+ needsFallbackMethodSyncCopyBack) {
5788
6071
  hasImports.set(propertyName, `(import "soundscript_object" "has:${getHostObjectPropertyToken(propertyName)}" (func $${getHostFallbackObjectHasImportFunctionName(propertyName)} (param externref) (result i32)))`);
5789
6072
  getterImports.set(`${closureSignatureId !== undefined ? 'get_closure' : 'get_tagged'}:${propertyName}`, closureSignatureId !== undefined
5790
6073
  ? `(import "soundscript_object" "get_closure:${getHostObjectPropertyToken(propertyName)}" (func $${getHostFallbackObjectGetImportFunctionName(propertyName)} (param externref) (result externref)))`
@@ -5869,7 +6152,9 @@ function emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackP
5869
6152
  }
5870
6153
  return [
5871
6154
  ...(needsSame
5872
- ? ['(import "soundscript_object" "same" (func $host_object_same (param externref) (param externref) (result i32)))']
6155
+ ? [
6156
+ '(import "soundscript_object" "same" (func $host_object_same (param externref) (param externref) (result i32)))',
6157
+ ]
5873
6158
  : []),
5874
6159
  ...(needsEmpty
5875
6160
  ? ['(import "soundscript_object" "empty" (func $host_object_empty (result externref)))']
@@ -6079,7 +6364,7 @@ function emitHostSpecializedObjectBoundaryHelpers(module, layoutsByRepresentatio
6079
6364
  usage.layout.fields.some((field) => (field.methodClosureFunctionIds?.length ?? 0) > 0);
6080
6365
  const hasHeapFieldCopyBack = usage.layout.fields.some((field) => field.valueType === 'heap_ref');
6081
6366
  const hasTaggedFieldBoundary = usage.layout.fields.some((field) => field.valueType === 'tagged_ref');
6082
- if (usage.needsParamBoundary) {
6367
+ if (usage.needsParamBoundary || usage.needsResultBoundary) {
6083
6368
  const emitSubtypeParamDispatch = (index) => {
6084
6369
  const subtypeLayout = subtypeLayouts[index];
6085
6370
  if (!subtypeLayout) {
@@ -6097,13 +6382,15 @@ function emitHostSpecializedObjectBoundaryHelpers(module, layoutsByRepresentatio
6097
6382
  `${indent(2)}call $${getHostObjectToSpecializedHelperName(subtypeLayout)}`,
6098
6383
  `${indent(1)}else`,
6099
6384
  ...emitSubtypeParamDispatch(index + 1),
6100
- ...(index + 1 === subtypeLayouts.length ? [
6101
- ...(usage.layout.classTagId === undefined
6102
- ? []
6103
- : [`${indent(2)}i32.const ${usage.layout.classTagId}`]),
6104
- ...usage.layout.fields.flatMap((field) => emitHostSpecializedFieldFromHost(field, layoutsByRepresentationName, 2)),
6105
- `${indent(2)}struct.new $${usage.layout.watTypeId}`,
6106
- ] : []),
6385
+ ...(index + 1 === subtypeLayouts.length
6386
+ ? [
6387
+ ...(usage.layout.classTagId === undefined
6388
+ ? []
6389
+ : [`${indent(2)}i32.const ${usage.layout.classTagId}`]),
6390
+ ...usage.layout.fields.flatMap((field) => emitHostSpecializedFieldFromHost(field, layoutsByRepresentationName, 2)),
6391
+ `${indent(2)}struct.new $${usage.layout.watTypeId}`,
6392
+ ]
6393
+ : []),
6107
6394
  `${indent(1)}end`,
6108
6395
  ];
6109
6396
  };
@@ -6149,18 +6436,16 @@ function emitHostSpecializedObjectBoundaryHelpers(module, layoutsByRepresentatio
6149
6436
  const subtypeLayout = subtypeLayouts[index];
6150
6437
  if (!subtypeLayout) {
6151
6438
  return [
6152
- ...(usage.layout.classTagId === undefined
6153
- ? []
6154
- : [
6155
- ...emitHostClassConstructorToHostAdaptation(usage.layout.classTagId, 1, layoutsByRepresentationName),
6156
- `${indent(1)}drop`,
6157
- `${indent(1)}local.get $target`,
6158
- `${indent(1)}local.get $value`,
6159
- `${indent(1)}struct.get $${usage.layout.watTypeId} 0`,
6160
- `${indent(1)}call $${getHostObjectSetClassTagImportFunctionName()}`,
6161
- `${indent(1)}local.get $target`,
6162
- `${indent(1)}call $${getHostObjectSyncClassPrototypeImportFunctionName()}`,
6163
- ]),
6439
+ ...(usage.layout.classTagId === undefined ? [] : [
6440
+ ...emitHostClassConstructorToHostAdaptation(usage.layout.classTagId, 1, layoutsByRepresentationName),
6441
+ `${indent(1)}drop`,
6442
+ `${indent(1)}local.get $target`,
6443
+ `${indent(1)}local.get $value`,
6444
+ `${indent(1)}struct.get $${usage.layout.watTypeId} 0`,
6445
+ `${indent(1)}call $${getHostObjectSetClassTagImportFunctionName()}`,
6446
+ `${indent(1)}local.get $target`,
6447
+ `${indent(1)}call $${getHostObjectSyncClassPrototypeImportFunctionName()}`,
6448
+ ]),
6164
6449
  ...usage.layout.fields.flatMap((field, fieldIndex) => emitSpecializedFieldCopyToHost(field, usage.layout, layoutsByRepresentationName, fallbackObjectLayout, fieldIndex, 1)),
6165
6450
  ...usage.layout.hostMethods.flatMap((method) => [
6166
6451
  ...(() => {
@@ -6235,18 +6520,16 @@ function emitHostSpecializedObjectBoundaryHelpers(module, layoutsByRepresentatio
6235
6520
  return [
6236
6521
  `${indent(1)}call $host_object_empty`,
6237
6522
  `${indent(1)}local.set $result`,
6238
- ...(usage.layout.classTagId === undefined
6239
- ? []
6240
- : [
6241
- ...emitHostClassConstructorToHostAdaptation(usage.layout.classTagId, 1, layoutsByRepresentationName),
6242
- `${indent(1)}drop`,
6243
- `${indent(1)}local.get $result`,
6244
- `${indent(1)}local.get $value`,
6245
- `${indent(1)}struct.get $${usage.layout.watTypeId} 0`,
6246
- `${indent(1)}call $${getHostObjectSetClassTagImportFunctionName()}`,
6247
- `${indent(1)}local.get $result`,
6248
- `${indent(1)}call $${getHostObjectSyncClassPrototypeImportFunctionName()}`,
6249
- ]),
6523
+ ...(usage.layout.classTagId === undefined ? [] : [
6524
+ ...emitHostClassConstructorToHostAdaptation(usage.layout.classTagId, 1, layoutsByRepresentationName),
6525
+ `${indent(1)}drop`,
6526
+ `${indent(1)}local.get $result`,
6527
+ `${indent(1)}local.get $value`,
6528
+ `${indent(1)}struct.get $${usage.layout.watTypeId} 0`,
6529
+ `${indent(1)}call $${getHostObjectSetClassTagImportFunctionName()}`,
6530
+ `${indent(1)}local.get $result`,
6531
+ `${indent(1)}call $${getHostObjectSyncClassPrototypeImportFunctionName()}`,
6532
+ ]),
6250
6533
  ...usage.layout.fields.flatMap((field, fieldIndex) => emitSpecializedFieldResultToHost(field, usage.layout, layoutsByRepresentationName, fallbackObjectLayout, fieldIndex, 1)),
6251
6534
  ...usage.layout.hostMethods.flatMap((method) => [
6252
6535
  ...(() => {
@@ -6309,7 +6592,7 @@ function emitHostSpecializedObjectBoundaryHelpers(module, layoutsByRepresentatio
6309
6592
  `${indent(1)}(local $entry_value externref)`,
6310
6593
  `${indent(1)}(local $entry_tagged (ref null $tagged_value))`,
6311
6594
  ]
6312
- : []), `${indent(1)}local.get $value`, `${indent(1)}call $${getHostObjectLookupCachedImportFunctionName()}`, `${indent(1)}local.tee $cached`, `${indent(1)}ref.is_null`, `${indent(1)}if`, ...emitSubtypeResultDispatch(0), `${indent(2)}local.set $result`, `${indent(2)}local.get $value`, `${indent(2)}local.get $result`, `${indent(2)}call $${getHostObjectRememberCachedImportFunctionName()}`, `${indent(1)}else`, `${indent(2)}local.get $cached`, `${indent(2)}local.set $result`, `${indent(1)}end`, `${indent(1)}local.get $result`, ')');
6595
+ : []), `${indent(1)}local.get $value`, `${indent(1)}call $${getHostObjectLookupCachedImportFunctionName()}`, `${indent(1)}local.tee $cached`, `${indent(1)}ref.is_null`, `${indent(1)}if`, ...emitSubtypeResultDispatch(0), `${indent(2)}local.set $result`, `${indent(2)}local.get $value`, `${indent(2)}local.get $result`, `${indent(2)}call $${getHostObjectRememberCachedImportFunctionName()}`, `${indent(1)}else`, `${indent(2)}local.get $cached`, `${indent(2)}local.get $value`, `${indent(2)}call $${getCopySpecializedObjectToHostHelperName(usage.layout)}`, `${indent(2)}local.get $cached`, `${indent(2)}local.set $result`, `${indent(1)}end`, `${indent(1)}local.get $result`, ')');
6313
6596
  }
6314
6597
  if (usage.needsResultBoundary && usage.layout.classTagId !== undefined) {
6315
6598
  for (const method of usage.layout.hostMethods) {
@@ -6416,7 +6699,7 @@ function emitHostFallbackObjectBoundaryHelpers(module, layout, fallbackPropertyK
6416
6699
  const captureValueTypes = closureFunction?.closureCaptureValueTypes ?? [];
6417
6700
  return captureValueTypes.length === 1 && captureValueTypes[0] === 'heap_ref';
6418
6701
  }));
6419
- if (usage.needsParamBoundary) {
6702
+ if (usage.needsParamBoundary || usage.needsResultBoundary) {
6420
6703
  lines.push(`(func $${getHostObjectToFallbackHelperName()} (param $value externref) (result (ref null $${layout.watTypeId}))`, `${indent(1)}(local $result (ref null $${layout.watTypeId}))`, `${indent(1)}(local $cached (ref null eq))`, `${indent(1)}(local $entry_value externref)`, `${indent(1)}(local $entry_tag i32)`, `${indent(1)}(local $entry_tagged (ref null $tagged_value))`, `${indent(1)}local.get $value`, `${indent(1)}call $${getHostObjectLookupParamCachedImportFunctionName(HOST_FALLBACK_PARAM_CACHE_KEY)}`, `${indent(1)}local.tee $cached`, `${indent(1)}ref.is_null`, `${indent(1)}if`, `${indent(2)}call $allocate_fallback_object`, `${indent(2)}local.set $result`, ...sortedPropertyEntries.flatMap(([propertyName, propertyKeyId]) => {
6421
6704
  const closureSignatureId = closureProperties.get(propertyName);
6422
6705
  const classConstructorTagId = classConstructorProperties.get(propertyName);
@@ -6503,10 +6786,11 @@ function emitHostFallbackObjectBoundaryHelpers(module, layout, fallbackPropertyK
6503
6786
  ];
6504
6787
  }), `${indent(2)}local.get $value`, `${indent(2)}local.get $result`, `${indent(2)}call $${getHostObjectRememberParamCachedImportFunctionName(HOST_FALLBACK_PARAM_CACHE_KEY)}`, `${indent(1)}else`, `${indent(2)}local.get $cached`, `${indent(2)}ref.cast (ref null $${layout.watTypeId})`, `${indent(2)}local.set $result`, `${indent(1)}end`, `${indent(1)}local.get $result`, `${indent(1)}local.get $value`, `${indent(1)}call $${getHostObjectRememberCachedImportFunctionName()}`, `${indent(1)}local.get $result`, ')');
6505
6788
  }
6506
- if (usage.needsParamCopyBack || needsMethodSyncCopyBack) {
6789
+ if (usage.needsParamCopyBack || needsMethodSyncCopyBack || usage.needsResultBoundary) {
6507
6790
  lines.push(`(func $${getCopyFallbackObjectToHostHelperName()} (param $target externref) (param $value (ref null $${layout.watTypeId}))`, `${indent(1)}(local $entry_tag i32)`, `${indent(1)}(local $entry_value externref)`, `${indent(1)}(local $entry_existing externref)`, `${indent(1)}(local $entry_heap (ref null eq))`, `${indent(1)}(local $entry_tagged (ref null $tagged_value))`, ...sortedPropertyEntries.flatMap(([propertyName, propertyKeyId]) => {
6508
6791
  const closureSignatureId = closureProperties.get(propertyName);
6509
- const methodClosureFunctionIds = (fallbackMethodClosureFunctionIds.get(propertyName) ?? []).filter((functionId) => {
6792
+ const methodClosureFunctionIds = (fallbackMethodClosureFunctionIds.get(propertyName) ?? [])
6793
+ .filter((functionId) => {
6510
6794
  const closureFunction = closureFunctionById.get(functionId);
6511
6795
  const captureValueTypes = closureFunction?.closureCaptureValueTypes ?? [];
6512
6796
  return captureValueTypes.length === 1 && captureValueTypes[0] === 'heap_ref';
@@ -6709,7 +6993,8 @@ function emitHostFallbackObjectBoundaryHelpers(module, layout, fallbackPropertyK
6709
6993
  if (usage.needsResultBoundary) {
6710
6994
  lines.push(`(func $${getFallbackObjectToHostHelperName()} (param $value (ref null $${layout.watTypeId})) (result externref)`, `${indent(1)}(local $result externref)`, `${indent(1)}(local $cached externref)`, `${indent(1)}(local $entry_tag i32)`, `${indent(1)}(local $entry_value externref)`, `${indent(1)}(local $entry_tagged (ref null $tagged_value))`, `${indent(1)}local.get $value`, `${indent(1)}call $${getHostObjectLookupCachedImportFunctionName()}`, `${indent(1)}local.tee $cached`, `${indent(1)}ref.is_null`, `${indent(1)}if`, `${indent(2)}call $host_object_empty`, `${indent(2)}local.set $result`, ...sortedPropertyEntries.flatMap(([propertyName, propertyKeyId]) => {
6711
6995
  const closureSignatureId = closureProperties.get(propertyName);
6712
- const methodClosureFunctionIds = (fallbackMethodClosureFunctionIds.get(propertyName) ?? []).filter((functionId) => {
6996
+ const methodClosureFunctionIds = (fallbackMethodClosureFunctionIds.get(propertyName) ?? [])
6997
+ .filter((functionId) => {
6713
6998
  const closureFunction = closureFunctionById.get(functionId);
6714
6999
  const captureValueTypes = closureFunction?.closureCaptureValueTypes ?? [];
6715
7000
  return captureValueTypes.length === 1 && captureValueTypes[0] === 'heap_ref';
@@ -6832,7 +7117,7 @@ function emitHostFallbackObjectBoundaryHelpers(module, layout, fallbackPropertyK
6832
7117
  ]),
6833
7118
  `${indent(2)}end`,
6834
7119
  ];
6835
- }), `${indent(2)}local.get $value`, `${indent(2)}local.get $result`, `${indent(2)}call $${getHostObjectRememberCachedImportFunctionName()}`, `${indent(1)}else`, `${indent(2)}local.get $cached`, `${indent(2)}local.set $result`, `${indent(1)}end`, `${indent(1)}local.get $result`, ')');
7120
+ }), `${indent(2)}local.get $value`, `${indent(2)}local.get $result`, `${indent(2)}call $${getHostObjectRememberCachedImportFunctionName()}`, `${indent(1)}else`, `${indent(2)}local.get $cached`, `${indent(2)}local.get $value`, `${indent(2)}call $${getCopyFallbackObjectToHostHelperName()}`, `${indent(2)}local.get $cached`, `${indent(2)}local.set $result`, `${indent(1)}end`, `${indent(1)}local.get $result`, ')');
6836
7121
  }
6837
7122
  if (usage.needsResultBoundary) {
6838
7123
  for (const [propertyName, functionIds] of fallbackMethodClosureFunctionIds.entries()) {
@@ -8072,7 +8357,9 @@ function emitOwnedStringLiteralHelpers(module, layout) {
8072
8357
  function emitOwnedStringRuntimeConstruction(codeUnits, layout, level, includeLocalDeclaration = true, localName = 'code_units') {
8073
8358
  return [
8074
8359
  ...(includeLocalDeclaration
8075
- ? [`${indent(level)}(local $${localName} (ref null $${layout.fallbackCodeUnitArrayWatTypeId}))`]
8360
+ ? [
8361
+ `${indent(level)}(local $${localName} (ref null $${layout.fallbackCodeUnitArrayWatTypeId}))`,
8362
+ ]
8076
8363
  : []),
8077
8364
  `${indent(level)}i32.const ${codeUnits.length}`,
8078
8365
  `${indent(level)}array.new_default $${layout.fallbackCodeUnitArrayWatTypeId}`,
@@ -8592,6 +8879,7 @@ function emitOwnedStringNativeHelpers(module, layout) {
8592
8879
  if (!layout || !moduleUsesOwnedStringRuntime(module)) {
8593
8880
  return [];
8594
8881
  }
8882
+ const usesTaggedThrowRuntime = moduleUsesTaggedThrowRuntime(module);
8595
8883
  const usesTrim = moduleUsesOwnedStringTrimRuntime(module);
8596
8884
  const usesToUpperCase = moduleUsesOwnedStringToUpperCaseRuntime(module);
8597
8885
  const usesToLowerCase = moduleUsesOwnedStringToLowerCaseRuntime(module);
@@ -8611,11 +8899,17 @@ function emitOwnedStringNativeHelpers(module, layout) {
8611
8899
  const usesTaggedArrayHostResultStringBoundary = module.functions.some((func) => func.hostTaggedArrayResultKinds?.includesString === true);
8612
8900
  const usesPrimitiveHostStringToHostBoundary = taggedHostBoundaryUsage.usesStringBoundary ||
8613
8901
  usesHostFallbackObjectBoundary ||
8614
- moduleUsesHostPromiseResultBridge(module);
8902
+ moduleUsesHostPromiseResultBridge(module) ||
8903
+ moduleUsesGeneratorStepClosureHostBridge(module) ||
8904
+ usesTaggedThrowRuntime ||
8905
+ moduleUsesSyncTryCatchRuntime(module);
8615
8906
  const usesPrimitiveHostStringFromHostBoundary = taggedHostBoundaryUsage.usesStringBoundary ||
8616
8907
  usesHostFallbackObjectBoundary ||
8617
8908
  moduleUsesHostPromiseParamBridge(module) ||
8618
- moduleUsesHostPromiseResultBridge(module);
8909
+ moduleUsesHostPromiseResultBridge(module) ||
8910
+ moduleUsesGeneratorStepClosureHostBridge(module) ||
8911
+ usesTaggedThrowRuntime ||
8912
+ moduleUsesSyncTryCatchRuntime(module);
8619
8913
  const usesOwnedArrayHostParamBoundary = moduleUsesOwnedArrayHostParamBoundary(module);
8620
8914
  const usesOwnedArrayHostResultBoundary = moduleUsesOwnedArrayHostResultBoundary(module);
8621
8915
  const usesToHost = moduleUsesOwnedStringToHostRuntime(module) ||
@@ -10654,6 +10948,7 @@ function collectSpecializedObjectInstanceOfUsages(module) {
10654
10948
  for (const statement of statements) {
10655
10949
  switch (statement.kind) {
10656
10950
  case 'expression':
10951
+ case 'box_set':
10657
10952
  case 'local_set':
10658
10953
  case 'return':
10659
10954
  case 'specialized_object_field_set':
@@ -10698,6 +10993,7 @@ function collectBuiltinErrorInstanceOfUsages(module) {
10698
10993
  for (const statement of statements) {
10699
10994
  switch (statement.kind) {
10700
10995
  case 'expression':
10996
+ case 'box_set':
10701
10997
  case 'local_set':
10702
10998
  case 'return':
10703
10999
  case 'specialized_object_field_set':
@@ -11593,9 +11889,16 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
11593
11889
  if (!moduleUsesPromiseRuntime(module) || !layout || !stringLayout) {
11594
11890
  return [];
11595
11891
  }
11596
- const usesThen = moduleUsesPromiseThenRuntime(module);
11597
11892
  const usesHostPromiseParamBridge = moduleUsesHostPromiseParamBridge(module);
11598
- const usesHostPromiseResultBridge = moduleUsesHostPromiseResultBridge(module);
11893
+ const usesHostPromiseResultBridge = moduleUsesHostPromiseResultBridge(module) ||
11894
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
11895
+ moduleUsesAsyncGeneratorHostResultBridge(module);
11896
+ const usesThen = moduleUsesPromiseThenRuntime(module) || usesHostPromiseResultBridge;
11897
+ const usesAsyncGeneratorHostStepBridge = moduleUsesAsyncGeneratorHostStepBridge(module) ||
11898
+ moduleUsesAsyncGeneratorHostResultBridge(module);
11899
+ const generatorStepSignature = usesAsyncGeneratorHostStepBridge
11900
+ ? getGeneratorStepClosureSignature(module)
11901
+ : undefined;
11599
11902
  const stringLiteralIdsByText = createStringLiteralIds(module);
11600
11903
  const stateKeyLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_PROMISE_INTERNAL_STATE_KEY);
11601
11904
  const valueKeyLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_PROMISE_INTERNAL_VALUE_KEY);
@@ -11613,22 +11916,36 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
11613
11916
  includesString: true,
11614
11917
  includesUndefined: true,
11615
11918
  };
11616
- const usesHostPromiseErrorBridge = usesHostPromiseParamBridge || usesHostPromiseResultBridge;
11617
- const builtinErrorBrandKeyLiteralId = usesHostPromiseErrorBridge
11919
+ const usesHostPromiseErrorBridge = usesHostPromiseParamBridge || usesHostPromiseResultBridge ||
11920
+ moduleUsesSyncGeneratorHostResultBridge(module) ||
11921
+ usesAsyncGeneratorHostStepBridge;
11922
+ const usesTaggedThrowRuntime = moduleUsesTaggedThrowRuntime(module);
11923
+ const usesHostErrorBridge = usesHostPromiseErrorBridge ||
11924
+ (usesTaggedThrowRuntime && moduleUsesBuiltinErrorRuntime(module));
11925
+ const iteratorResultValueLiteralId = usesAsyncGeneratorHostStepBridge
11926
+ ? getRequiredStringLiteralId(stringLiteralIdsByText, 'value')
11927
+ : undefined;
11928
+ const iteratorResultDoneLiteralId = usesAsyncGeneratorHostStepBridge
11929
+ ? getRequiredStringLiteralId(stringLiteralIdsByText, 'done')
11930
+ : undefined;
11931
+ const builtinErrorBrandKeyLiteralId = usesHostErrorBridge
11618
11932
  ? getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_BUILTIN_ERROR_INTERNAL_BRAND_KEY)
11619
11933
  : undefined;
11620
- const builtinErrorNameLiteralId = usesHostPromiseErrorBridge
11934
+ const builtinErrorNameLiteralId = usesHostErrorBridge
11621
11935
  ? getRequiredStringLiteralId(stringLiteralIdsByText, 'name')
11622
11936
  : undefined;
11623
- const builtinErrorMessageLiteralId = usesHostPromiseErrorBridge
11937
+ const builtinErrorMessageLiteralId = usesHostErrorBridge
11624
11938
  ? getRequiredStringLiteralId(stringLiteralIdsByText, 'message')
11625
11939
  : undefined;
11626
- const builtinErrorLiteralId = usesHostPromiseErrorBridge
11940
+ const builtinErrorLiteralId = usesHostErrorBridge
11627
11941
  ? getRequiredStringLiteralId(stringLiteralIdsByText, 'Error')
11628
11942
  : undefined;
11629
11943
  if (usesHostPromiseResultBridge && !promiseHandlerSignature) {
11630
11944
  throw new Error('Missing promise handler signature for host Promise result bridge.');
11631
11945
  }
11946
+ if (usesAsyncGeneratorHostStepBridge && !generatorStepSignature) {
11947
+ throw new Error('Missing generator step signature for async generator host-step bridge.');
11948
+ }
11632
11949
  return [
11633
11950
  ...(usesThen
11634
11951
  ? [
@@ -12103,6 +12420,10 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
12103
12420
  `${indent(1)}call $__soundscript_promise_fulfill`,
12104
12421
  `${indent(1)}call $${SOUNDSCRIPT_PROMISE_DRAIN_HELPER_NAME}`,
12105
12422
  ')',
12423
+ ]
12424
+ : []),
12425
+ ...((usesHostPromiseParamBridge || usesAsyncGeneratorHostStepBridge)
12426
+ ? [
12106
12427
  `(func $${SOUNDSCRIPT_PROMISE_BRIDGE_REJECT_EXPORT_NAME} (export "${SOUNDSCRIPT_PROMISE_BRIDGE_REJECT_EXPORT_NAME}") (param $promise (ref null eq)) (param $value externref)`,
12107
12428
  `${indent(1)}(local $target (ref null $${layout.watTypeId}))`,
12108
12429
  `${indent(1)}(local $value__host_tag i32)`,
@@ -12136,25 +12457,10 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
12136
12457
  `${indent(4)}i32.const 3`,
12137
12458
  `${indent(4)}i32.eq`,
12138
12459
  `${indent(4)}if`,
12139
- `${indent(6)}call $allocate_dynamic_object`,
12460
+ `${indent(6)}local.get $value`,
12461
+ `${indent(6)}call $${getHostBuiltinErrorToDynamicHelperName()}`,
12140
12462
  `${indent(6)}local.set $value__error_object`,
12141
12463
  `${indent(6)}local.get $value__error_object`,
12142
- `${indent(6)}call $owned_string_literal_${builtinErrorBrandKeyLiteralId}`,
12143
- `${indent(6)}call $owned_string_literal_${builtinErrorLiteralId}`,
12144
- `${indent(6)}call $tag_string`,
12145
- `${indent(6)}call $set_dynamic_object_property`,
12146
- `${indent(6)}local.get $value__error_object`,
12147
- `${indent(6)}call $owned_string_literal_${builtinErrorNameLiteralId}`,
12148
- `${indent(6)}call $owned_string_literal_${builtinErrorLiteralId}`,
12149
- `${indent(6)}call $tag_string`,
12150
- `${indent(6)}call $set_dynamic_object_property`,
12151
- `${indent(6)}local.get $value__error_object`,
12152
- `${indent(6)}call $owned_string_literal_${builtinErrorMessageLiteralId}`,
12153
- `${indent(6)}local.get $value__message`,
12154
- `${indent(6)}call $string_to_owned`,
12155
- `${indent(6)}call $tag_string`,
12156
- `${indent(6)}call $set_dynamic_object_property`,
12157
- `${indent(6)}local.get $value__error_object`,
12158
12464
  `${indent(6)}call $tag_heap_object`,
12159
12465
  `${indent(6)}local.set $value__host_tagged`,
12160
12466
  `${indent(6)}br $value__host_bridge_done`,
@@ -12175,15 +12481,259 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
12175
12481
  ')',
12176
12482
  ]
12177
12483
  : []),
12178
- ...(usesHostPromiseErrorBridge
12484
+ ...(moduleUsesGeneratorStepClosureHostBridge(module)
12485
+ ? [
12486
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_TAGGED_TO_HOST_HELPER_NAME} (param $value (ref null $tagged_value)) (result externref)`,
12487
+ `${indent(1)}(local $value__tag i32)`,
12488
+ `${indent(1)}(local $value__host externref)`,
12489
+ `${indent(1)}(local $value__dynamic (ref null $${layout.watTypeId}))`,
12490
+ `${indent(1)}local.get $value`,
12491
+ `${indent(1)}struct.get $tagged_value 0`,
12492
+ `${indent(1)}local.set $value__tag`,
12493
+ `${indent(1)}block $value__host_done`,
12494
+ `${indent(2)}local.get $value__tag`,
12495
+ `${indent(2)}i32.const 4`,
12496
+ `${indent(2)}i32.eq`,
12497
+ `${indent(2)}if`,
12498
+ `${indent(3)}local.get $value`,
12499
+ `${indent(3)}call $untag_heap_object`,
12500
+ `${indent(3)}ref.test (ref null $${layout.watTypeId})`,
12501
+ `${indent(3)}if`,
12502
+ `${indent(4)}local.get $value`,
12503
+ `${indent(4)}call $untag_heap_object`,
12504
+ `${indent(4)}ref.cast (ref null $${layout.watTypeId})`,
12505
+ `${indent(4)}local.tee $value__dynamic`,
12506
+ `${indent(4)}call $owned_string_literal_${brandKeyLiteralId}`,
12507
+ `${indent(4)}call $get_dynamic_object_property`,
12508
+ `${indent(4)}struct.get $tagged_value 0`,
12509
+ `${indent(4)}i32.const 1`,
12510
+ `${indent(4)}i32.eq`,
12511
+ `${indent(4)}if`,
12512
+ `${indent(5)}local.get $value__dynamic`,
12513
+ `${indent(5)}call ${SOUNDSCRIPT_HOST_PROMISE_TO_HOST_IMPORT_NAME}`,
12514
+ `${indent(5)}local.set $value__host`,
12515
+ `${indent(5)}br $value__host_done`,
12516
+ `${indent(4)}end`,
12517
+ `${indent(3)}end`,
12518
+ ...(moduleUsesBuiltinErrorRuntime(module)
12519
+ ? [
12520
+ `${indent(3)}local.get $value`,
12521
+ `${indent(3)}call $untag_heap_object`,
12522
+ `${indent(3)}ref.test (ref null $${layout.watTypeId})`,
12523
+ `${indent(3)}if`,
12524
+ `${indent(4)}local.get $value`,
12525
+ `${indent(4)}call $untag_heap_object`,
12526
+ `${indent(4)}ref.cast (ref null $${layout.watTypeId})`,
12527
+ `${indent(4)}call $${getHostDynamicBuiltinErrorHelperName()}`,
12528
+ `${indent(4)}local.set $value__host`,
12529
+ `${indent(4)}br $value__host_done`,
12530
+ `${indent(3)}end`,
12531
+ ]
12532
+ : []),
12533
+ `${indent(3)}local.get $value`,
12534
+ `${indent(3)}call $untag_heap_object`,
12535
+ `${indent(3)}ref.test (ref null $owned_string_array)`,
12536
+ `${indent(3)}if`,
12537
+ `${indent(4)}local.get $value`,
12538
+ `${indent(4)}call $untag_heap_object`,
12539
+ `${indent(4)}ref.cast (ref null $owned_string_array)`,
12540
+ `${indent(4)}call $owned_string_array_to_host_array`,
12541
+ `${indent(4)}local.set $value__host`,
12542
+ `${indent(4)}br $value__host_done`,
12543
+ `${indent(3)}end`,
12544
+ `${indent(3)}local.get $value`,
12545
+ `${indent(3)}call $untag_heap_object`,
12546
+ `${indent(3)}ref.test (ref null $owned_number_array)`,
12547
+ `${indent(3)}if`,
12548
+ `${indent(4)}local.get $value`,
12549
+ `${indent(4)}call $untag_heap_object`,
12550
+ `${indent(4)}ref.cast (ref null $owned_number_array)`,
12551
+ `${indent(4)}call $owned_number_array_to_host_array`,
12552
+ `${indent(4)}local.set $value__host`,
12553
+ `${indent(4)}br $value__host_done`,
12554
+ `${indent(3)}end`,
12555
+ `${indent(3)}local.get $value`,
12556
+ `${indent(3)}call $untag_heap_object`,
12557
+ `${indent(3)}ref.test (ref null $owned_boolean_array)`,
12558
+ `${indent(3)}if`,
12559
+ `${indent(4)}local.get $value`,
12560
+ `${indent(4)}call $untag_heap_object`,
12561
+ `${indent(4)}ref.cast (ref null $owned_boolean_array)`,
12562
+ `${indent(4)}call $owned_boolean_array_to_host_array`,
12563
+ `${indent(4)}local.set $value__host`,
12564
+ `${indent(4)}br $value__host_done`,
12565
+ `${indent(3)}end`,
12566
+ `${indent(3)}local.get $value`,
12567
+ `${indent(3)}call $untag_heap_object`,
12568
+ `${indent(3)}ref.test (ref null $owned_tagged_array)`,
12569
+ `${indent(3)}if`,
12570
+ `${indent(4)}local.get $value`,
12571
+ `${indent(4)}call $untag_heap_object`,
12572
+ `${indent(4)}ref.cast (ref null $owned_tagged_array)`,
12573
+ `${indent(4)}call $${getOwnedTaggedArrayToHostHelperName(ASYNC_GENERATOR_HOST_TAGGED_ARRAY_BRIDGE_KINDS)}`,
12574
+ `${indent(4)}local.set $value__host`,
12575
+ `${indent(4)}br $value__host_done`,
12576
+ `${indent(3)}end`,
12577
+ `${indent(3)}unreachable`,
12578
+ `${indent(2)}end`,
12579
+ ...emitTaggedPrimitiveToHostExternref('value', 'value__tag', 'value__host', {
12580
+ includesBoolean: true,
12581
+ includesNull: true,
12582
+ includesNumber: true,
12583
+ includesString: true,
12584
+ includesUndefined: true,
12585
+ }, 2, indent),
12586
+ `${indent(2)}local.set $value__host`,
12587
+ `${indent(1)}end`,
12588
+ `${indent(1)}local.get $value__host`,
12589
+ ')',
12590
+ ]
12591
+ : []),
12592
+ ...(usesAsyncGeneratorHostStepBridge
12593
+ ? [
12594
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME} (param $promise (ref null eq)) (param $done i32) (param $value (ref null $tagged_value))`,
12595
+ `${indent(1)}(local $target (ref null $${layout.watTypeId}))`,
12596
+ `${indent(1)}(local $result (ref null $${layout.watTypeId}))`,
12597
+ `${indent(1)}local.get $promise`,
12598
+ `${indent(1)}ref.cast (ref null $${layout.watTypeId})`,
12599
+ `${indent(1)}local.set $target`,
12600
+ `${indent(1)}call $allocate_dynamic_object`,
12601
+ `${indent(1)}local.set $result`,
12602
+ `${indent(1)}local.get $result`,
12603
+ `${indent(1)}call $owned_string_literal_${iteratorResultValueLiteralId}`,
12604
+ `${indent(1)}local.get $value`,
12605
+ `${indent(1)}call $set_dynamic_object_property`,
12606
+ `${indent(1)}local.get $result`,
12607
+ `${indent(1)}call $owned_string_literal_${iteratorResultDoneLiteralId}`,
12608
+ `${indent(1)}local.get $done`,
12609
+ `${indent(1)}call $tag_boolean`,
12610
+ `${indent(1)}call $set_dynamic_object_property`,
12611
+ `${indent(1)}local.get $target`,
12612
+ `${indent(1)}local.get $result`,
12613
+ `${indent(1)}call $tag_heap_object`,
12614
+ `${indent(1)}call $__soundscript_promise_fulfill`,
12615
+ `${indent(1)}call $${SOUNDSCRIPT_PROMISE_DRAIN_HELPER_NAME}`,
12616
+ ')',
12617
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_HELPER_NAME} (param $step (ref null $closure)) (param $mode f64) (param $value (ref null $tagged_value)) (result (ref null $${layout.watTypeId}))`,
12618
+ `${indent(1)}(local $promise (ref null $${layout.watTypeId}))`,
12619
+ `${indent(1)}(local $step__host externref)`,
12620
+ `${indent(1)}(local $value__host_value externref)`,
12621
+ `${indent(1)}call $${SOUNDSCRIPT_PROMISE_NEW_PENDING_HELPER_NAME}`,
12622
+ `${indent(1)}local.set $promise`,
12623
+ `${indent(1)}local.get $step`,
12624
+ `${indent(1)}call $host_closure_to_host_${generatorStepSignature.id}`,
12625
+ `${indent(1)}local.set $step__host`,
12626
+ `${indent(1)}local.get $value`,
12627
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_TAGGED_TO_HOST_HELPER_NAME}`,
12628
+ `${indent(1)}local.set $value__host_value`,
12629
+ `${indent(1)}local.get $step__host`,
12630
+ `${indent(1)}local.get $mode`,
12631
+ `${indent(1)}local.get $value__host_value`,
12632
+ `${indent(1)}local.get $promise`,
12633
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_ASYNC_GENERATOR_STEP_IMPORT_NAME}`,
12634
+ `${indent(1)}local.get $promise`,
12635
+ ')',
12636
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_EXPORT_NAME} (export "${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_EXPORT_NAME}") (param $promise (ref null eq)) (param $done i32) (param $value externref)`,
12637
+ `${indent(1)}(local $value__host_tag i32)`,
12638
+ `${indent(1)}(local $value__host_tagged (ref null $tagged_value))`,
12639
+ ...(usesHostPromiseErrorBridge
12640
+ ? [
12641
+ `${indent(1)}(local $value__message externref)`,
12642
+ `${indent(1)}(local $value__message_tag i32)`,
12643
+ `${indent(1)}(local $value__error_object (ref null $${layout.watTypeId}))`,
12644
+ ]
12645
+ : []),
12646
+ ...(usesHostPromiseErrorBridge
12647
+ ? [
12648
+ `${indent(1)}local.get $value`,
12649
+ `${indent(1)}call $tagged_type_tag`,
12650
+ `${indent(1)}local.set $value__host_tag`,
12651
+ `${indent(1)}block $value__host_bridge_done`,
12652
+ `${indent(2)}local.get $value__host_tag`,
12653
+ `${indent(2)}i32.const 4`,
12654
+ `${indent(2)}i32.eq`,
12655
+ `${indent(2)}if`,
12656
+ `${indent(4)}local.get $value`,
12657
+ `${indent(4)}call $${getHostObjectImportFunctionName('get_tagged', 'message')}`,
12658
+ `${indent(4)}local.tee $value__message`,
12659
+ `${indent(4)}call $tagged_type_tag`,
12660
+ `${indent(4)}local.set $value__message_tag`,
12661
+ `${indent(4)}local.get $value__message_tag`,
12662
+ `${indent(4)}i32.const 3`,
12663
+ `${indent(4)}i32.eq`,
12664
+ `${indent(4)}if`,
12665
+ `${indent(6)}local.get $value`,
12666
+ `${indent(6)}call $${getHostBuiltinErrorToDynamicHelperName()}`,
12667
+ `${indent(6)}local.set $value__error_object`,
12668
+ `${indent(6)}local.get $value__error_object`,
12669
+ `${indent(6)}call $tag_heap_object`,
12670
+ `${indent(6)}local.set $value__host_tagged`,
12671
+ `${indent(6)}br $value__host_bridge_done`,
12672
+ `${indent(4)}end`,
12673
+ `${indent(2)}end`,
12674
+ ...emitHostTaggedPrimitiveExternrefToTagged('value', 'value__host_tag', 'value__host_tagged', hostPromiseBridgeKinds, 2, indent),
12675
+ `${indent(2)}local.set $value__host_tagged`,
12676
+ `${indent(2)}br $value__host_bridge_done`,
12677
+ `${indent(1)}end`,
12678
+ `${indent(1)}local.get $value__host_tagged`,
12679
+ ]
12680
+ : emitHostTaggedPrimitiveExternrefToTagged('value', 'value__host_tag', 'value__host_tagged', hostPromiseBridgeKinds, 1, indent)),
12681
+ `${indent(1)}local.set $value__host_tagged`,
12682
+ `${indent(1)}local.get $promise`,
12683
+ `${indent(1)}local.get $done`,
12684
+ `${indent(1)}local.get $value__host_tagged`,
12685
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME}`,
12686
+ ')',
12687
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_STRING_ARRAY_EXPORT_NAME} (export "${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_STRING_ARRAY_EXPORT_NAME}") (param $promise (ref null eq)) (param $done i32) (param $value externref)`,
12688
+ `${indent(1)}local.get $promise`,
12689
+ `${indent(1)}local.get $done`,
12690
+ `${indent(1)}local.get $value`,
12691
+ `${indent(1)}call $host_array_to_owned_string_array`,
12692
+ `${indent(1)}call $tag_heap_object`,
12693
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME}`,
12694
+ ')',
12695
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_NUMBER_ARRAY_EXPORT_NAME} (export "${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_NUMBER_ARRAY_EXPORT_NAME}") (param $promise (ref null eq)) (param $done i32) (param $value externref)`,
12696
+ `${indent(1)}local.get $promise`,
12697
+ `${indent(1)}local.get $done`,
12698
+ `${indent(1)}local.get $value`,
12699
+ `${indent(1)}call $host_array_to_owned_number_array`,
12700
+ `${indent(1)}call $tag_heap_object`,
12701
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME}`,
12702
+ ')',
12703
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_BOOLEAN_ARRAY_EXPORT_NAME} (export "${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_BOOLEAN_ARRAY_EXPORT_NAME}") (param $promise (ref null eq)) (param $done i32) (param $value externref)`,
12704
+ `${indent(1)}local.get $promise`,
12705
+ `${indent(1)}local.get $done`,
12706
+ `${indent(1)}local.get $value`,
12707
+ `${indent(1)}call $host_array_to_owned_boolean_array`,
12708
+ `${indent(1)}call $tag_heap_object`,
12709
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME}`,
12710
+ ')',
12711
+ `(func $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_ARRAY_EXPORT_NAME} (export "${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_ARRAY_EXPORT_NAME}") (param $promise (ref null eq)) (param $done i32) (param $value externref)`,
12712
+ `${indent(1)}local.get $promise`,
12713
+ `${indent(1)}local.get $done`,
12714
+ `${indent(1)}local.get $value`,
12715
+ `${indent(1)}call $${getHostArrayToOwnedTaggedArrayHelperName(ASYNC_GENERATOR_HOST_TAGGED_ARRAY_BRIDGE_KINDS)}`,
12716
+ `${indent(1)}call $tag_heap_object`,
12717
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_STEP_BRIDGE_FULFILL_TAGGED_HELPER_NAME}`,
12718
+ ')',
12719
+ ]
12720
+ : []),
12721
+ ...(usesHostErrorBridge
12179
12722
  ? [
12180
12723
  `(func $${getHostBuiltinErrorToDynamicHelperName()} (param $value externref) (result (ref null $${layout.watTypeId}))`,
12724
+ `${indent(1)}(local $name externref)`,
12725
+ `${indent(1)}(local $name_tag i32)`,
12181
12726
  `${indent(1)}(local $message externref)`,
12182
12727
  `${indent(1)}(local $message_tag i32)`,
12183
12728
  `${indent(1)}(local $result (ref null $${layout.watTypeId}))`,
12184
12729
  `${indent(1)}call $allocate_dynamic_object`,
12185
12730
  `${indent(1)}local.set $result`,
12186
12731
  `${indent(1)}local.get $value`,
12732
+ `${indent(1)}call $${getHostObjectImportFunctionName('get_tagged', 'name')}`,
12733
+ `${indent(1)}local.tee $name`,
12734
+ `${indent(1)}call $tagged_type_tag`,
12735
+ `${indent(1)}local.set $name_tag`,
12736
+ `${indent(1)}local.get $value`,
12187
12737
  `${indent(1)}call $${getHostObjectImportFunctionName('get_tagged', 'message')}`,
12188
12738
  `${indent(1)}local.tee $message`,
12189
12739
  `${indent(1)}call $tagged_type_tag`,
@@ -12194,13 +12744,31 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
12194
12744
  `${indent(1)}if`,
12195
12745
  `${indent(2)}local.get $result`,
12196
12746
  `${indent(2)}call $owned_string_literal_${builtinErrorBrandKeyLiteralId}`,
12197
- `${indent(2)}call $owned_string_literal_${builtinErrorLiteralId}`,
12198
- `${indent(2)}call $tag_string`,
12747
+ `${indent(2)}local.get $name_tag`,
12748
+ `${indent(2)}i32.const 3`,
12749
+ `${indent(2)}i32.eq`,
12750
+ `${indent(2)}if (result (ref null $tagged_value))`,
12751
+ `${indent(3)}local.get $name`,
12752
+ `${indent(3)}call $string_to_owned`,
12753
+ `${indent(3)}call $tag_string`,
12754
+ `${indent(2)}else`,
12755
+ `${indent(3)}call $owned_string_literal_${builtinErrorLiteralId}`,
12756
+ `${indent(3)}call $tag_string`,
12757
+ `${indent(2)}end`,
12199
12758
  `${indent(2)}call $set_dynamic_object_property`,
12200
12759
  `${indent(2)}local.get $result`,
12201
12760
  `${indent(2)}call $owned_string_literal_${builtinErrorNameLiteralId}`,
12202
- `${indent(2)}call $owned_string_literal_${builtinErrorLiteralId}`,
12203
- `${indent(2)}call $tag_string`,
12761
+ `${indent(2)}local.get $name_tag`,
12762
+ `${indent(2)}i32.const 3`,
12763
+ `${indent(2)}i32.eq`,
12764
+ `${indent(2)}if (result (ref null $tagged_value))`,
12765
+ `${indent(3)}local.get $name`,
12766
+ `${indent(3)}call $string_to_owned`,
12767
+ `${indent(3)}call $tag_string`,
12768
+ `${indent(2)}else`,
12769
+ `${indent(3)}call $owned_string_literal_${builtinErrorLiteralId}`,
12770
+ `${indent(3)}call $tag_string`,
12771
+ `${indent(2)}end`,
12204
12772
  `${indent(2)}call $set_dynamic_object_property`,
12205
12773
  `${indent(2)}local.get $result`,
12206
12774
  `${indent(2)}call $owned_string_literal_${builtinErrorMessageLiteralId}`,
@@ -12219,6 +12787,55 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
12219
12787
  `${indent(1)}(local $message_tagged (ref null $tagged_value))`,
12220
12788
  `${indent(1)}(local $message_tag i32)`,
12221
12789
  `${indent(1)}(local $message_value externref)`,
12790
+ ...(usesAsyncGeneratorHostStepBridge
12791
+ ? [
12792
+ `${indent(1)}(local $done_tagged (ref null $tagged_value))`,
12793
+ `${indent(1)}(local $done_tag i32)`,
12794
+ `${indent(1)}(local $done_value externref)`,
12795
+ `${indent(1)}(local $value_tagged (ref null $tagged_value))`,
12796
+ `${indent(1)}(local $value_value externref)`,
12797
+ ]
12798
+ : []),
12799
+ ...(usesAsyncGeneratorHostStepBridge
12800
+ ? [
12801
+ `${indent(1)}local.get $value`,
12802
+ `${indent(1)}call $owned_string_literal_${iteratorResultDoneLiteralId}`,
12803
+ `${indent(1)}call $get_dynamic_object_property`,
12804
+ `${indent(1)}local.tee $done_tagged`,
12805
+ `${indent(1)}struct.get $tagged_value 0`,
12806
+ `${indent(1)}local.set $done_tag`,
12807
+ `${indent(1)}local.get $done_tag`,
12808
+ `${indent(1)}i32.const 1`,
12809
+ `${indent(1)}i32.eq`,
12810
+ `${indent(1)}if`,
12811
+ `${indent(2)}call $host_object_empty`,
12812
+ `${indent(2)}local.set $result`,
12813
+ `${indent(2)}local.get $value`,
12814
+ `${indent(2)}call $owned_string_literal_${iteratorResultValueLiteralId}`,
12815
+ `${indent(2)}call $get_dynamic_object_property`,
12816
+ `${indent(2)}local.set $value_tagged`,
12817
+ `${indent(2)}local.get $value_tagged`,
12818
+ `${indent(2)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_TAGGED_TO_HOST_HELPER_NAME}`,
12819
+ `${indent(2)}local.set $value_value`,
12820
+ `${indent(2)}local.get $result`,
12821
+ `${indent(2)}local.get $value_value`,
12822
+ `${indent(2)}call $${getHostObjectImportFunctionName('set_tagged', 'value')}`,
12823
+ ...emitTaggedPrimitiveToHostExternref('done_tagged', 'done_tag', 'done_value', {
12824
+ includesBoolean: true,
12825
+ includesNull: false,
12826
+ includesNumber: false,
12827
+ includesString: false,
12828
+ includesUndefined: false,
12829
+ }, 2, indent),
12830
+ `${indent(2)}local.set $done_value`,
12831
+ `${indent(2)}local.get $result`,
12832
+ `${indent(2)}local.get $done_value`,
12833
+ `${indent(2)}call $${getHostObjectImportFunctionName('set_tagged', 'done')}`,
12834
+ `${indent(2)}local.get $result`,
12835
+ `${indent(2)}return`,
12836
+ `${indent(1)}end`,
12837
+ ]
12838
+ : []),
12222
12839
  `${indent(1)}call $host_object_empty`,
12223
12840
  `${indent(1)}local.set $result`,
12224
12841
  `${indent(1)}local.get $value`,
@@ -12272,6 +12889,124 @@ function emitPromiseRuntimeHelpers(module, layout, stringLayout) {
12272
12889
  : []),
12273
12890
  ];
12274
12891
  }
12892
+ function emitHostBuiltinErrorBridgeHelpers(module, layout) {
12893
+ const usesHostErrorBridge = moduleUsesHostPromiseParamBridge(module) ||
12894
+ moduleUsesHostPromiseResultBridge(module) ||
12895
+ moduleUsesGeneratorStepClosureHostBridge(module) ||
12896
+ moduleUsesSyncTryCatchRuntime(module) ||
12897
+ (moduleUsesTaggedThrowRuntime(module) && moduleUsesBuiltinErrorRuntime(module));
12898
+ if (!usesHostErrorBridge || !layout || moduleUsesPromiseRuntime(module)) {
12899
+ return [];
12900
+ }
12901
+ const stringLiteralIdsByText = createStringLiteralIds(module);
12902
+ const builtinErrorBrandKeyLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_BUILTIN_ERROR_INTERNAL_BRAND_KEY);
12903
+ const builtinErrorNameLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, 'name');
12904
+ const builtinErrorMessageLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, 'message');
12905
+ const builtinErrorLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, 'Error');
12906
+ return [
12907
+ `(func $${getHostBuiltinErrorToDynamicHelperName()} (param $value externref) (result (ref null $${layout.watTypeId}))`,
12908
+ `${indent(1)}(local $name externref)`,
12909
+ `${indent(1)}(local $name_tag i32)`,
12910
+ `${indent(1)}(local $message externref)`,
12911
+ `${indent(1)}(local $message_tag i32)`,
12912
+ `${indent(1)}(local $result (ref null $${layout.watTypeId}))`,
12913
+ `${indent(1)}call $allocate_dynamic_object`,
12914
+ `${indent(1)}local.set $result`,
12915
+ `${indent(1)}local.get $value`,
12916
+ `${indent(1)}call $${getHostObjectImportFunctionName('get_tagged', 'name')}`,
12917
+ `${indent(1)}local.tee $name`,
12918
+ `${indent(1)}call $tagged_type_tag`,
12919
+ `${indent(1)}local.set $name_tag`,
12920
+ `${indent(1)}local.get $value`,
12921
+ `${indent(1)}call $${getHostObjectImportFunctionName('get_tagged', 'message')}`,
12922
+ `${indent(1)}local.tee $message`,
12923
+ `${indent(1)}call $tagged_type_tag`,
12924
+ `${indent(1)}local.set $message_tag`,
12925
+ `${indent(1)}local.get $message_tag`,
12926
+ `${indent(1)}i32.const 3`,
12927
+ `${indent(1)}i32.eq`,
12928
+ `${indent(1)}if`,
12929
+ `${indent(2)}local.get $result`,
12930
+ `${indent(2)}call $owned_string_literal_${builtinErrorBrandKeyLiteralId}`,
12931
+ `${indent(2)}local.get $name_tag`,
12932
+ `${indent(2)}i32.const 3`,
12933
+ `${indent(2)}i32.eq`,
12934
+ `${indent(2)}if (result (ref null $tagged_value))`,
12935
+ `${indent(3)}local.get $name`,
12936
+ `${indent(3)}call $string_to_owned`,
12937
+ `${indent(3)}call $tag_string`,
12938
+ `${indent(2)}else`,
12939
+ `${indent(3)}call $owned_string_literal_${builtinErrorLiteralId}`,
12940
+ `${indent(3)}call $tag_string`,
12941
+ `${indent(2)}end`,
12942
+ `${indent(2)}call $set_dynamic_object_property`,
12943
+ `${indent(2)}local.get $result`,
12944
+ `${indent(2)}call $owned_string_literal_${builtinErrorNameLiteralId}`,
12945
+ `${indent(2)}local.get $name_tag`,
12946
+ `${indent(2)}i32.const 3`,
12947
+ `${indent(2)}i32.eq`,
12948
+ `${indent(2)}if (result (ref null $tagged_value))`,
12949
+ `${indent(3)}local.get $name`,
12950
+ `${indent(3)}call $string_to_owned`,
12951
+ `${indent(3)}call $tag_string`,
12952
+ `${indent(2)}else`,
12953
+ `${indent(3)}call $owned_string_literal_${builtinErrorLiteralId}`,
12954
+ `${indent(3)}call $tag_string`,
12955
+ `${indent(2)}end`,
12956
+ `${indent(2)}call $set_dynamic_object_property`,
12957
+ `${indent(2)}local.get $result`,
12958
+ `${indent(2)}call $owned_string_literal_${builtinErrorMessageLiteralId}`,
12959
+ `${indent(2)}local.get $message`,
12960
+ `${indent(2)}call $string_to_owned`,
12961
+ `${indent(2)}call $tag_string`,
12962
+ `${indent(2)}call $set_dynamic_object_property`,
12963
+ `${indent(1)}end`,
12964
+ `${indent(1)}local.get $result`,
12965
+ ')',
12966
+ `(func $${getHostDynamicBuiltinErrorHelperName()} (param $value (ref null $${layout.watTypeId})) (result externref)`,
12967
+ `${indent(1)}(local $result externref)`,
12968
+ `${indent(1)}(local $name_tagged (ref null $tagged_value))`,
12969
+ `${indent(1)}(local $name_tag i32)`,
12970
+ `${indent(1)}(local $name_value externref)`,
12971
+ `${indent(1)}(local $message_tagged (ref null $tagged_value))`,
12972
+ `${indent(1)}(local $message_tag i32)`,
12973
+ `${indent(1)}(local $message_value externref)`,
12974
+ `${indent(1)}call $host_object_empty`,
12975
+ `${indent(1)}local.set $result`,
12976
+ `${indent(1)}local.get $value`,
12977
+ `${indent(1)}call $owned_string_literal_${builtinErrorNameLiteralId}`,
12978
+ `${indent(1)}call $get_dynamic_object_property`,
12979
+ `${indent(1)}local.set $name_tagged`,
12980
+ ...emitTaggedPrimitiveToHostExternref('name_tagged', 'name_tag', 'name_value', {
12981
+ includesBoolean: false,
12982
+ includesNull: false,
12983
+ includesNumber: false,
12984
+ includesString: true,
12985
+ includesUndefined: true,
12986
+ }, 1, indent),
12987
+ `${indent(1)}local.set $name_value`,
12988
+ `${indent(1)}local.get $result`,
12989
+ `${indent(1)}local.get $name_value`,
12990
+ `${indent(1)}call $${getHostObjectImportFunctionName('set_tagged', 'name')}`,
12991
+ `${indent(1)}local.get $value`,
12992
+ `${indent(1)}call $owned_string_literal_${builtinErrorMessageLiteralId}`,
12993
+ `${indent(1)}call $get_dynamic_object_property`,
12994
+ `${indent(1)}local.set $message_tagged`,
12995
+ ...emitTaggedPrimitiveToHostExternref('message_tagged', 'message_tag', 'message_value', {
12996
+ includesBoolean: false,
12997
+ includesNull: false,
12998
+ includesNumber: false,
12999
+ includesString: true,
13000
+ includesUndefined: true,
13001
+ }, 1, indent),
13002
+ `${indent(1)}local.set $message_value`,
13003
+ `${indent(1)}local.get $result`,
13004
+ `${indent(1)}local.get $message_value`,
13005
+ `${indent(1)}call $${getHostObjectImportFunctionName('set_tagged', 'message')}`,
13006
+ `${indent(1)}local.get $result`,
13007
+ ')',
13008
+ ];
13009
+ }
12275
13010
  function collectSpecializedAllocationsByLocal(operations) {
12276
13011
  const allocationsByLocal = new Map();
12277
13012
  operations.forEach((operation, index) => {
@@ -12552,7 +13287,8 @@ function inferHeapLocalRepresentations(func, operations, dynamicAllocationsByLoc
12552
13287
  case 'list_dynamic_object_entries':
12553
13288
  case 'copy_dynamic_object_entries':
12554
13289
  case 'set_dynamic_object_property':
12555
- if (operation.kind !== 'copy_dynamic_object_entries' && heapLocalNames.has(operation.objectName)) {
13290
+ if (operation.kind !== 'copy_dynamic_object_entries' &&
13291
+ heapLocalNames.has(operation.objectName)) {
12556
13292
  heapLocalRepresentations.set(operation.objectName, {
12557
13293
  kind: 'dynamic',
12558
13294
  layout: dynamicObjectLayout,
@@ -12643,7 +13379,8 @@ function inferHeapLocalRepresentations(func, operations, dynamicAllocationsByLoc
12643
13379
  break;
12644
13380
  }
12645
13381
  const dynamicAllocations = dynamicAllocationsByLocal.get(statement.name);
12646
- const nextDynamicAllocationIndex = dynamicAllocationIndexesByLocal.get(statement.name) ?? 0;
13382
+ const nextDynamicAllocationIndex = dynamicAllocationIndexesByLocal.get(statement.name) ??
13383
+ 0;
12647
13384
  const dynamicAllocation = dynamicAllocations?.[nextDynamicAllocationIndex];
12648
13385
  if (dynamicAllocation) {
12649
13386
  if (!dynamicObjectLayout) {
@@ -12720,7 +13457,8 @@ function inferHeapLocalRepresentations(func, operations, dynamicAllocationsByLoc
12720
13457
  if (statement.value.callee === '__soundscript_promise_new_pending' ||
12721
13458
  statement.value.callee === '__soundscript_promise_resolve' ||
12722
13459
  statement.value.callee === '__soundscript_promise_reject' ||
12723
- statement.value.callee === '__soundscript_promise_then') {
13460
+ statement.value.callee === '__soundscript_promise_then' ||
13461
+ statement.value.callee === SOUNDSCRIPT_ASYNC_GENERATOR_STEP_HELPER_NAME) {
12724
13462
  if (!dynamicObjectLayout) {
12725
13463
  throw createUnsupportedHeapRuntimeBackendError(`Missing dynamic-object layout metadata for Promise helper ${statement.value.callee}.`);
12726
13464
  }
@@ -12878,6 +13616,7 @@ function functionUsesOwnedStringArrayLiteralRuntime(func) {
12878
13616
  }
12879
13617
  switch (statement.kind) {
12880
13618
  case 'expression':
13619
+ case 'box_set':
12881
13620
  case 'local_set':
12882
13621
  case 'return':
12883
13622
  case 'specialized_object_field_set':
@@ -12926,6 +13665,7 @@ function functionUsesOwnedHeapArrayLiteralRuntime(func) {
12926
13665
  }
12927
13666
  switch (statement.kind) {
12928
13667
  case 'expression':
13668
+ case 'box_set':
12929
13669
  case 'local_set':
12930
13670
  case 'return':
12931
13671
  case 'specialized_object_field_set':
@@ -12974,6 +13714,7 @@ function functionUsesOwnedNumberArrayLiteralRuntime(func) {
12974
13714
  }
12975
13715
  switch (statement.kind) {
12976
13716
  case 'expression':
13717
+ case 'box_set':
12977
13718
  case 'local_set':
12978
13719
  case 'return':
12979
13720
  case 'specialized_object_field_set':
@@ -13022,6 +13763,7 @@ function functionUsesOwnedBooleanArrayLiteralRuntime(func) {
13022
13763
  }
13023
13764
  switch (statement.kind) {
13024
13765
  case 'expression':
13766
+ case 'box_set':
13025
13767
  case 'local_set':
13026
13768
  case 'return':
13027
13769
  case 'specialized_object_field_set':
@@ -13070,6 +13812,7 @@ function functionUsesOwnedTaggedArrayLiteralRuntime(func) {
13070
13812
  }
13071
13813
  switch (statement.kind) {
13072
13814
  case 'expression':
13815
+ case 'box_set':
13073
13816
  case 'local_set':
13074
13817
  case 'return':
13075
13818
  case 'specialized_object_field_set':
@@ -13123,9 +13866,8 @@ function createFunctionBackendRuntimeContext(func, functionsByName, runtime, lay
13123
13866
  operation.kind === 'set_dynamic_object_property') ||
13124
13867
  (func.heapParamRepresentations ?? []).some((boundary) => boundary.representation.kind === 'dynamic_object_representation') ||
13125
13868
  (func.heapLocalRepresentations ?? []).some((boundary) => boundary.representation.kind === 'dynamic_object_representation') ||
13126
- func.heapResultRepresentation?.kind === 'dynamic_object_representation'
13127
- ||
13128
- statementsUseStringHelper(func.body, isPromiseRuntimeHelperCall))
13869
+ func.heapResultRepresentation?.kind === 'dynamic_object_representation' ||
13870
+ statementsUseStringHelper(func.body, isPromiseRuntimeHelperCall))
13129
13871
  ? getDynamicObjectLayout(runtime)
13130
13872
  : undefined;
13131
13873
  const heapLocalNames = new Set([
@@ -15293,7 +16035,8 @@ function emitStatement(statement, level, labelCounter, runtime) {
15293
16035
  const specializedAllocation = runtime.specializedAllocationsByLocal.get(statement.name)?.[runtime.specializedAllocationOpIndexesByLocal.get(statement.name) ?? 0];
15294
16036
  if (specializedAllocation) {
15295
16037
  const sourceRepresentation = getLayoutForRepresentationName(specializedAllocation.operation.representation.name, runtime.layoutsByRepresentationName);
15296
- const scratchLocalName = runtime.specializedCallResultScratchLocalsByRepresentationName.get(specializedAllocation.operation.representation.name);
16038
+ const scratchLocalName = runtime
16039
+ .specializedCallResultScratchLocalsByRepresentationName.get(specializedAllocation.operation.representation.name);
15297
16040
  if (!scratchLocalName) {
15298
16041
  throw createUnsupportedHeapRuntimeBackendError(`Missing specialized allocation scratch local for ${specializedAllocation.operation.representation.name}.`);
15299
16042
  }
@@ -15521,15 +16264,14 @@ function emitStatement(statement, level, labelCounter, runtime) {
15521
16264
  ...emitExpression(statement.value, level, runtime),
15522
16265
  `${indent(level)}array.set $owned_tagged_array_data`,
15523
16266
  ];
15524
- case 'specialized_object_field_set':
15525
- {
15526
- const layout = getLayoutForRepresentationName(statement.representation.name, runtime.layoutsByRepresentationName);
15527
- return [
15528
- `${indent(level)}local.get $${statement.objectName}`,
15529
- ...emitExpression(statement.value, level, runtime),
15530
- `${indent(level)}struct.set $${layout.watTypeId} ${getSpecializedObjectFieldSlotIndex(layout, statement.fieldIndex)}`,
15531
- ];
15532
- }
16267
+ case 'specialized_object_field_set': {
16268
+ const layout = getLayoutForRepresentationName(statement.representation.name, runtime.layoutsByRepresentationName);
16269
+ return [
16270
+ `${indent(level)}local.get $${statement.objectName}`,
16271
+ ...emitExpression(statement.value, level, runtime),
16272
+ `${indent(level)}struct.set $${layout.watTypeId} ${getSpecializedObjectFieldSlotIndex(layout, statement.fieldIndex)}`,
16273
+ ];
16274
+ }
15533
16275
  case 'box_set':
15534
16276
  return [
15535
16277
  ...emitExpression(statement.box, level, runtime),
@@ -15544,6 +16286,14 @@ function emitStatement(statement, level, labelCounter, runtime) {
15544
16286
  ...emitExpression(statement.value, level, runtime),
15545
16287
  `${indent(level)}call $set_dynamic_object_property`,
15546
16288
  ];
16289
+ case 'throw_tagged':
16290
+ return [
16291
+ ...emitExpression(statement.value, level, runtime),
16292
+ `${indent(level)}call $${SOUNDSCRIPT_THROW_TAGGED_HELPER_NAME}`,
16293
+ `${indent(level)}unreachable`,
16294
+ ];
16295
+ case 'trap':
16296
+ return [`${indent(level)}unreachable`];
15547
16297
  case 'return':
15548
16298
  if (runtime.currentFunction.resultType === 'heap_ref' &&
15549
16299
  runtime.currentFunction.heapResultRepresentation?.kind ===
@@ -15989,7 +16739,8 @@ function emitInternalClosureTaggedPrimitiveAndHeapToHost(taggedLocalName, tagLoc
15989
16739
  `${indent(level + 3)}local.get $${taggedLocalName}`,
15990
16740
  `${indent(level + 3)}call $untag_heap_object`,
15991
16741
  `${indent(level + 3)}ref.cast ${heapRepresentation.kind === 'specialized_object_representation'
15992
- ? `(ref null $${getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName).watTypeId})`
16742
+ ? `(ref null $${getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName)
16743
+ .watTypeId})`
15993
16744
  : heapRepresentation.kind === 'dynamic_object_representation'
15994
16745
  ? `(ref null $${dynamicObjectLayout?.watTypeId ??
15995
16746
  (() => {
@@ -16036,7 +16787,9 @@ function emitHostClosureBoundaryParamToInternal(valueType, closureSignatureId, t
16036
16787
  `${indent(level)}local.get $${localName}`,
16037
16788
  `${indent(level)}call $${heapRepresentation.kind === 'specialized_object_representation'
16038
16789
  ? getHostObjectToSpecializedHelperName(getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName))
16039
- : getHostObjectToFallbackHelperName()}`,
16790
+ : heapRepresentation.kind === 'dynamic_object_representation'
16791
+ ? getHostBuiltinErrorToDynamicHelperName()
16792
+ : getHostObjectToFallbackHelperName()}`,
16040
16793
  ];
16041
16794
  case 'owned_heap_array_ref':
16042
16795
  if (!heapArrayRepresentation) {
@@ -16097,7 +16850,8 @@ function emitInternalClosureBoundaryParamToHost(valueType, closureSignatureId, t
16097
16850
  return [
16098
16851
  `${indent(level)}local.get $${localName}`,
16099
16852
  `${indent(level)}ref.cast ${heapRepresentation.kind === 'specialized_object_representation'
16100
- ? `(ref null $${getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName).watTypeId})`
16853
+ ? `(ref null $${getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName)
16854
+ .watTypeId})`
16101
16855
  : `(ref null $${fallbackObjectLayout?.watTypeId ??
16102
16856
  (() => {
16103
16857
  throw createUnsupportedHeapRuntimeBackendError('Missing fallback object layout for internal-to-host closure param.');
@@ -16172,13 +16926,17 @@ function emitHostClosureBoundaryResultToInternal(valueType, resultClassConstruct
16172
16926
  return [
16173
16927
  `${indent(level)}call $${heapRepresentation.kind === 'specialized_object_representation'
16174
16928
  ? getHostObjectToSpecializedHelperName(getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName))
16175
- : getHostObjectToFallbackHelperName()}`,
16929
+ : heapRepresentation.kind === 'dynamic_object_representation'
16930
+ ? getHostBuiltinErrorToDynamicHelperName()
16931
+ : getHostObjectToFallbackHelperName()}`,
16176
16932
  ];
16177
16933
  case 'owned_heap_array_ref':
16178
16934
  if (!heapArrayRepresentation) {
16179
16935
  throw new Error('Missing heap-array representation for host-to-internal closure result.');
16180
16936
  }
16181
- return [`${indent(level)}call $${getHostArrayToOwnedHeapArrayHelperName(heapArrayRepresentation)}`];
16937
+ return [
16938
+ `${indent(level)}call $${getHostArrayToOwnedHeapArrayHelperName(heapArrayRepresentation)}`,
16939
+ ];
16182
16940
  case 'owned_array_ref':
16183
16941
  return [`${indent(level)}call $host_array_to_owned_string_array`];
16184
16942
  case 'owned_number_array_ref':
@@ -16205,7 +16963,7 @@ function emitHostClosureBoundaryResultToInternal(valueType, resultClassConstruct
16205
16963
  return [];
16206
16964
  }
16207
16965
  }
16208
- function emitInternalClosureBoundaryResultToHost(valueType, resultClassConstructorTagId, closureSignatureId, taggedKinds, heapRepresentation, heapArrayRepresentation, level, layoutsByRepresentationName, fallbackObjectLayout) {
16966
+ function emitInternalClosureBoundaryResultToHost(valueType, resultClassConstructorTagId, closureSignatureId, taggedKinds, heapRepresentation, heapArrayRepresentation, level, layoutsByRepresentationName, fallbackObjectLayout, dynamicObjectLayout) {
16209
16967
  if (resultClassConstructorTagId !== undefined) {
16210
16968
  return [
16211
16969
  `${indent(level)}drop`,
@@ -16226,20 +16984,30 @@ function emitInternalClosureBoundaryResultToHost(valueType, resultClassConstruct
16226
16984
  }
16227
16985
  return [
16228
16986
  `${indent(level)}ref.cast ${heapRepresentation.kind === 'specialized_object_representation'
16229
- ? `(ref null $${getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName).watTypeId})`
16230
- : `(ref null $${fallbackObjectLayout?.watTypeId ??
16231
- (() => {
16232
- throw createUnsupportedHeapRuntimeBackendError('Missing fallback object layout for internal-to-host closure result.');
16233
- })()})`}`,
16987
+ ? `(ref null $${getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName)
16988
+ .watTypeId})`
16989
+ : heapRepresentation.kind === 'dynamic_object_representation'
16990
+ ? `(ref null $${dynamicObjectLayout?.watTypeId ??
16991
+ (() => {
16992
+ throw createUnsupportedHeapRuntimeBackendError('Missing dynamic object layout for internal-to-host closure result.');
16993
+ })()})`
16994
+ : `(ref null $${fallbackObjectLayout?.watTypeId ??
16995
+ (() => {
16996
+ throw createUnsupportedHeapRuntimeBackendError('Missing fallback object layout for internal-to-host closure result.');
16997
+ })()})`}`,
16234
16998
  `${indent(level)}call $${heapRepresentation.kind === 'specialized_object_representation'
16235
16999
  ? getSpecializedObjectToHostHelperName(getLayoutForRepresentationName(heapRepresentation.name, layoutsByRepresentationName))
16236
- : getFallbackObjectToHostHelperName()}`,
17000
+ : heapRepresentation.kind === 'dynamic_object_representation'
17001
+ ? getHostDynamicBuiltinErrorHelperName()
17002
+ : getFallbackObjectToHostHelperName()}`,
16237
17003
  ];
16238
17004
  case 'owned_heap_array_ref':
16239
17005
  if (!heapArrayRepresentation) {
16240
17006
  throw new Error('Missing heap-array representation for internal-to-host closure result.');
16241
17007
  }
16242
- return [`${indent(level)}call $${getOwnedHeapArrayToHostHelperName(heapArrayRepresentation)}`];
17008
+ return [
17009
+ `${indent(level)}call $${getOwnedHeapArrayToHostHelperName(heapArrayRepresentation)}`,
17010
+ ];
16243
17011
  case 'owned_array_ref':
16244
17012
  return [`${indent(level)}call $owned_string_array_to_host_array`];
16245
17013
  case 'owned_number_array_ref':
@@ -16255,7 +17023,7 @@ function emitInternalClosureBoundaryResultToHost(valueType, resultClassConstruct
16255
17023
  if (heapRepresentation) {
16256
17024
  return [
16257
17025
  `${indent(level)}local.set $result__host_tagged`,
16258
- ...emitInternalClosureTaggedPrimitiveAndHeapToHost('result__host_tagged', 'result__host_tag', 'result__host_value', taggedKinds, heapRepresentation, level, layoutsByRepresentationName, fallbackObjectLayout),
17026
+ ...emitInternalClosureTaggedPrimitiveAndHeapToHost('result__host_tagged', 'result__host_tag', 'result__host_value', taggedKinds, heapRepresentation, level, layoutsByRepresentationName, fallbackObjectLayout, dynamicObjectLayout),
16259
17027
  ];
16260
17028
  }
16261
17029
  return [
@@ -16293,6 +17061,8 @@ function functionNeedsHostExportWrapper(func, moduleUsesPromiseRuntime = false)
16293
17061
  func.hostClassConstructorResultTagId !== undefined ||
16294
17062
  (func.hostClosureParams?.length ?? 0) > 0 ||
16295
17063
  func.hostClosureResultSignatureId !== undefined ||
17064
+ func.hostGeneratorResult === true ||
17065
+ func.hostAsyncGeneratorResult === true ||
16296
17066
  (func.hostDynamicCollectionParams?.length ?? 0) > 0 ||
16297
17067
  (func.hostTaggedPrimitiveParams?.length ?? 0) > 0 ||
16298
17068
  func.hostTaggedPrimitiveResultKinds !== undefined ||
@@ -16313,7 +17083,8 @@ function functionNeedsHostExportWrapper(func, moduleUsesPromiseRuntime = false)
16313
17083
  func.resultType === 'owned_number_array_ref' ||
16314
17084
  func.resultType === 'owned_boolean_array_ref' ||
16315
17085
  func.resultType === 'owned_tagged_array_ref' ||
16316
- (func.resultType === 'owned_heap_array_ref' && func.hostHeapArrayResultRepresentation !== undefined);
17086
+ (func.resultType === 'owned_heap_array_ref' &&
17087
+ func.hostHeapArrayResultRepresentation !== undefined);
16317
17088
  }
16318
17089
  function getHostWrapperRawResultWatType(func, runtime) {
16319
17090
  if (func.hostClassConstructorResultTagId !== undefined) {
@@ -16964,7 +17735,7 @@ function emitHostTaggedPrimitiveAndHeapResultAdaptation(kinds, boundary, objectP
16964
17735
  `${indent(level)}local.get $result__host_value`,
16965
17736
  ];
16966
17737
  }
16967
- function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false) {
17738
+ function emitHostExportWrapper(module, func, runtime, moduleUsesPromiseRuntime = false) {
16968
17739
  if (func.exportName.length === 0 || !functionNeedsHostExportWrapper(func, moduleUsesPromiseRuntime)) {
16969
17740
  return [];
16970
17741
  }
@@ -16987,6 +17758,15 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
16987
17758
  const objectParamBoundaries = getHostAdaptableSpecializedObjectParamBoundaries(func, runtime.layoutsByRepresentationName);
16988
17759
  const objectParamBoundariesByName = new Map(objectParamBoundaries.map((boundary) => [boundary.name, boundary.representation]));
16989
17760
  const hostObjectResultRepresentation = getHostAdaptableSpecializedObjectResultRepresentation(func, runtime.layoutsByRepresentationName);
17761
+ const generatorStepSignature = func.hostGeneratorResult || func.hostAsyncGeneratorResult
17762
+ ? getGeneratorStepClosureSignature(module)
17763
+ : undefined;
17764
+ const generatorStepKeyLiteralId = func.hostGeneratorResult || func.hostAsyncGeneratorResult
17765
+ ? getRequiredStringLiteralId(createStringLiteralIds(module), SOUNDSCRIPT_GENERATOR_INTERNAL_STEP_KEY)
17766
+ : undefined;
17767
+ if ((func.hostGeneratorResult || func.hostAsyncGeneratorResult) && !generatorStepSignature) {
17768
+ throw new Error('Missing generator step signature for generator export wrapper.');
17769
+ }
16990
17770
  const taggedHeapNullableSpecializedParamBoundaries = getHostTaggedHeapNullableSpecializedParamBoundaries(func, runtime.layoutsByRepresentationName);
16991
17771
  const taggedHeapNullableSpecializedResultBoundary = getHostTaggedHeapNullableSpecializedResultRepresentation(func, runtime.layoutsByRepresentationName);
16992
17772
  const fallbackObjectParamBoundaryNames = getHostFallbackObjectParamBoundaryNames(func);
@@ -17001,7 +17781,8 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17001
17781
  ...objectParamBoundaries,
17002
17782
  ...taggedHeapNullableSpecializedParamBoundaries.map((boundary) => ({
17003
17783
  name: boundary.name,
17004
- representation: boundary.boundary.representation,
17784
+ representation: boundary.boundary
17785
+ .representation,
17005
17786
  })),
17006
17787
  ];
17007
17788
  const allFallbackObjectParamBoundaryNames = [
@@ -17029,14 +17810,19 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17029
17810
  }
17030
17811
  }
17031
17812
  const needsOwnedHeapArrayParamSync = heapArrayParamBoundaries.length > 0;
17032
- const needsOwnedArrayParamSync = ownedArrayParamKinds.length > 0 || ownedTaggedArrayParamKinds.length > 0;
17813
+ const needsOwnedArrayParamSync = ownedArrayParamKinds.length > 0 ||
17814
+ ownedTaggedArrayParamKinds.length > 0;
17033
17815
  const needsObjectParamSync = objectParamBoundaries.length > 0;
17034
17816
  const needsFallbackObjectParamSync = fallbackObjectParamBoundaryNames.length > 0;
17035
17817
  const needsTaggedHeapNullableParamSync = (func.hostTaggedHeapNullableParams?.length ?? 0) > 0;
17036
- const needsHostSyncScratch = moduleUsesPromiseRuntime || needsOwnedHeapArrayParamSync || needsOwnedArrayParamSync || needsObjectParamSync ||
17037
- needsFallbackObjectParamSync || needsTaggedHeapNullableParamSync || hostObjectResultRepresentation !== undefined ||
17818
+ const needsHostSyncScratch = moduleUsesPromiseRuntime || needsOwnedHeapArrayParamSync ||
17819
+ needsOwnedArrayParamSync || needsObjectParamSync ||
17820
+ needsFallbackObjectParamSync || needsTaggedHeapNullableParamSync ||
17821
+ func.hostGeneratorResult === true || func.hostAsyncGeneratorResult === true ||
17822
+ hostObjectResultRepresentation !== undefined ||
17038
17823
  taggedHeapNullableSpecializedResultBoundary !== undefined || hasFallbackObjectResultBoundary ||
17039
- hasTaggedHeapNullableFallbackResultBoundary || func.hostHeapArrayResultRepresentation !== undefined;
17824
+ hasTaggedHeapNullableFallbackResultBoundary ||
17825
+ func.hostHeapArrayResultRepresentation !== undefined;
17040
17826
  const header = [
17041
17827
  `(func $${func.name}__export`,
17042
17828
  ` (export "${func.exportName}")`,
@@ -17072,22 +17858,28 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17072
17858
  ? 'externref'
17073
17859
  : func.hostPromiseResult
17074
17860
  ? 'externref'
17075
- : func.hostLengthViewResult
17861
+ : func.hostGeneratorResult
17076
17862
  ? 'externref'
17077
- : hostObjectResultRepresentation || taggedHeapNullableSpecializedResultBoundary
17863
+ : func.hostAsyncGeneratorResult
17078
17864
  ? 'externref'
17079
- : hasFallbackObjectResultBoundary || hasTaggedHeapNullableFallbackResultBoundary
17865
+ : func.hostLengthViewResult
17080
17866
  ? 'externref'
17081
- : func.resultType === 'owned_array_ref' || func.resultType === 'owned_number_array_ref' ||
17082
- func.resultType === 'owned_boolean_array_ref' || func.resultType === 'owned_tagged_array_ref' ||
17083
- (func.resultType === 'owned_heap_array_ref' && func.hostHeapArrayResultRepresentation !== undefined)
17867
+ : hostObjectResultRepresentation || taggedHeapNullableSpecializedResultBoundary
17084
17868
  ? 'externref'
17085
- : func.resultType === 'heap_ref'
17086
- ? getHeapBoundaryWatType(func.heapResultRepresentation?.name ??
17087
- (() => {
17088
- throw createUnloweredHeapRuntimeBackendError();
17089
- })(), runtime)
17090
- : getWatValueType(func.resultType)})`,
17869
+ : hasFallbackObjectResultBoundary || hasTaggedHeapNullableFallbackResultBoundary
17870
+ ? 'externref'
17871
+ : func.resultType === 'owned_array_ref' || func.resultType === 'owned_number_array_ref' ||
17872
+ func.resultType === 'owned_boolean_array_ref' ||
17873
+ func.resultType === 'owned_tagged_array_ref' ||
17874
+ (func.resultType === 'owned_heap_array_ref' &&
17875
+ func.hostHeapArrayResultRepresentation !== undefined)
17876
+ ? 'externref'
17877
+ : func.resultType === 'heap_ref'
17878
+ ? getHeapBoundaryWatType(func.heapResultRepresentation?.name ??
17879
+ (() => {
17880
+ throw createUnloweredHeapRuntimeBackendError();
17881
+ })(), runtime)
17882
+ : getWatValueType(func.resultType)})`,
17091
17883
  ].join('');
17092
17884
  const wrapperLocals = [
17093
17885
  ...ownedArrayParamKinds.map(({ name, kind }) => `${indent(1)}(local $${name}__host_owned ${kind === 'string'
@@ -17165,15 +17957,20 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17165
17957
  .filter((objectParam) => priorParamNames.has(objectParam.name))
17166
17958
  .map((objectParam) => ({
17167
17959
  name: objectParam.name,
17168
- representation: objectParam.boundary.representation,
17960
+ representation: objectParam.boundary
17961
+ .representation,
17169
17962
  }));
17170
17963
  const priorAllSpecializedObjectParams = [
17171
17964
  ...priorObjectParams,
17172
17965
  ...priorTaggedHeapNullableSpecializedParams,
17173
17966
  ];
17174
17967
  const priorFallbackParams = fallbackObjectParamBoundaryNames.filter((name) => priorParamNames.has(name));
17175
- const priorTaggedHeapNullableFallbackParams = taggedHeapNullableFallbackParamBoundaryNames.filter((name) => priorParamNames.has(name));
17176
- const priorAllFallbackParams = [...priorFallbackParams, ...priorTaggedHeapNullableFallbackParams];
17968
+ const priorTaggedHeapNullableFallbackParams = taggedHeapNullableFallbackParamBoundaryNames
17969
+ .filter((name) => priorParamNames.has(name));
17970
+ const priorAllFallbackParams = [
17971
+ ...priorFallbackParams,
17972
+ ...priorTaggedHeapNullableFallbackParams,
17973
+ ];
17177
17974
  const kinds = taggedPrimitiveParamsByName.get(param.name);
17178
17975
  const taggedHeapNullableBoundary = taggedHeapNullableParamsByName.get(param.name);
17179
17976
  const taggedArrayKinds = taggedArrayParamsByName.get(param.name);
@@ -17242,9 +18039,7 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17242
18039
  : [`${indent(1)}local.get $${param.name}`];
17243
18040
  }),
17244
18041
  `${indent(1)}call $${func.name}`,
17245
- ...(needsHostSyncScratch
17246
- ? [`${indent(1)}local.set $result__host_sync_tmp`]
17247
- : []),
18042
+ ...(needsHostSyncScratch ? [`${indent(1)}local.set $result__host_sync_tmp`] : []),
17248
18043
  ...(moduleUsesPromiseRuntime
17249
18044
  ? [`${indent(1)}call $${SOUNDSCRIPT_PROMISE_DRAIN_HELPER_NAME}`]
17250
18045
  : []),
@@ -17316,6 +18111,8 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17316
18111
  ...(func.hostTaggedPrimitiveResultKinds ||
17317
18112
  func.hostTaggedHeapNullableResult ||
17318
18113
  func.hostPromiseResult ||
18114
+ func.hostGeneratorResult ||
18115
+ func.hostAsyncGeneratorResult ||
17319
18116
  func.hostLengthViewResult ||
17320
18117
  hostObjectResultRepresentation !== undefined ||
17321
18118
  taggedHeapNullableSpecializedResultBoundary !== undefined ||
@@ -17325,7 +18122,8 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17325
18122
  func.resultType === 'owned_number_array_ref' ||
17326
18123
  func.resultType === 'owned_boolean_array_ref' ||
17327
18124
  func.resultType === 'owned_tagged_array_ref' ||
17328
- (func.resultType === 'owned_heap_array_ref' && func.hostHeapArrayResultRepresentation !== undefined)
18125
+ (func.resultType === 'owned_heap_array_ref' &&
18126
+ func.hostHeapArrayResultRepresentation !== undefined)
17329
18127
  ? []
17330
18128
  : [`${indent(1)}local.get $result__host_sync_tmp`]),
17331
18129
  ]
@@ -17360,41 +18158,65 @@ function emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime = false)
17360
18158
  ...(needsHostSyncScratch ? [`${indent(1)}local.get $result__host_sync_tmp`] : []),
17361
18159
  `${indent(1)}call ${SOUNDSCRIPT_HOST_PROMISE_TO_HOST_IMPORT_NAME}`,
17362
18160
  ]
17363
- : func.hostLengthViewResult
18161
+ : func.hostGeneratorResult
17364
18162
  ? [
17365
18163
  ...(needsHostSyncScratch ? [`${indent(1)}local.get $result__host_sync_tmp`] : []),
17366
- `${indent(1)}call $host_length_view_from_length`,
18164
+ `${indent(1)}call $owned_string_literal_${generatorStepKeyLiteralId}`,
18165
+ `${indent(1)}call $get_dynamic_object_property`,
18166
+ `${indent(1)}call $untag_heap_object`,
18167
+ `${indent(1)}ref.cast (ref null $closure)`,
18168
+ `${indent(1)}call $host_closure_to_host_${generatorStepSignature.id}`,
18169
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_SYNC_GENERATOR_WRAP_IMPORT_NAME}`,
17367
18170
  ]
17368
- : func.hostClosureResultSignatureId !== undefined
18171
+ : func.hostAsyncGeneratorResult
17369
18172
  ? [
17370
18173
  ...(needsHostSyncScratch ? [`${indent(1)}local.get $result__host_sync_tmp`] : []),
17371
- `${indent(1)}call $host_closure_to_host_${func.hostClosureResultSignatureId}`,
18174
+ `${indent(1)}call $owned_string_literal_${generatorStepKeyLiteralId}`,
18175
+ `${indent(1)}call $get_dynamic_object_property`,
18176
+ `${indent(1)}call $untag_heap_object`,
18177
+ `${indent(1)}ref.cast (ref null $closure)`,
18178
+ `${indent(1)}call $host_closure_to_host_${generatorStepSignature.id}`,
18179
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_ASYNC_GENERATOR_WRAP_IMPORT_NAME}`,
17372
18180
  ]
17373
- : hostObjectResultRepresentation
17374
- ? emitHostSpecializedObjectResultAdaptation(hostObjectResultRepresentation, allSpecializedObjectParamBoundaries, 1, runtime)
17375
- : hasFallbackObjectResultBoundary
17376
- ? emitHostFallbackObjectResultAdaptation(allFallbackObjectParamBoundaryNames, 1)
17377
- : func.resultType === 'owned_array_ref'
17378
- ? needsHostSyncScratch
17379
- ? emitHostOwnedArrayResultAdaptation('string', ownedArrayParamKinds, 1)
17380
- : [`${indent(1)}call $owned_string_array_to_host_array`]
17381
- : func.resultType === 'owned_number_array_ref'
17382
- ? needsHostSyncScratch
17383
- ? emitHostOwnedArrayResultAdaptation('number', ownedArrayParamKinds, 1)
17384
- : [`${indent(1)}call $owned_number_array_to_host_array`]
17385
- : func.resultType === 'owned_boolean_array_ref'
18181
+ : func.hostLengthViewResult
18182
+ ? [
18183
+ ...(needsHostSyncScratch ? [`${indent(1)}local.get $result__host_sync_tmp`] : []),
18184
+ `${indent(1)}call $host_length_view_from_length`,
18185
+ ]
18186
+ : func.hostClosureResultSignatureId !== undefined
18187
+ ? [
18188
+ ...(needsHostSyncScratch ? [`${indent(1)}local.get $result__host_sync_tmp`] : []),
18189
+ `${indent(1)}call $host_closure_to_host_${func.hostClosureResultSignatureId}`,
18190
+ ]
18191
+ : hostObjectResultRepresentation
18192
+ ? emitHostSpecializedObjectResultAdaptation(hostObjectResultRepresentation, allSpecializedObjectParamBoundaries, 1, runtime)
18193
+ : hasFallbackObjectResultBoundary
18194
+ ? emitHostFallbackObjectResultAdaptation(allFallbackObjectParamBoundaryNames, 1)
18195
+ : func.resultType === 'owned_array_ref'
17386
18196
  ? needsHostSyncScratch
17387
- ? emitHostOwnedArrayResultAdaptation('boolean', ownedArrayParamKinds, 1)
17388
- : [`${indent(1)}call $owned_boolean_array_to_host_array`]
17389
- : func.resultType === 'owned_tagged_array_ref' && func.hostTaggedArrayResultKinds
18197
+ ? emitHostOwnedArrayResultAdaptation('string', ownedArrayParamKinds, 1)
18198
+ : [`${indent(1)}call $owned_string_array_to_host_array`]
18199
+ : func.resultType === 'owned_number_array_ref'
17390
18200
  ? needsHostSyncScratch
17391
- ? emitHostOwnedTaggedArrayResultAdaptation(func.hostTaggedArrayResultKinds, ownedTaggedArrayParamKinds, 1)
17392
- : [`${indent(1)}call $${getOwnedTaggedArrayToHostHelperName(func.hostTaggedArrayResultKinds)}`]
17393
- : func.resultType === 'owned_heap_array_ref' && func.hostHeapArrayResultRepresentation
18201
+ ? emitHostOwnedArrayResultAdaptation('number', ownedArrayParamKinds, 1)
18202
+ : [`${indent(1)}call $owned_number_array_to_host_array`]
18203
+ : func.resultType === 'owned_boolean_array_ref'
17394
18204
  ? needsHostSyncScratch
17395
- ? emitHostOwnedHeapArrayResultAdaptation(func.hostHeapArrayResultRepresentation, heapArrayParamBoundaries, 1)
17396
- : [`${indent(1)}call $${getOwnedHeapArrayToHostHelperName(func.hostHeapArrayResultRepresentation)}`]
17397
- : []),
18205
+ ? emitHostOwnedArrayResultAdaptation('boolean', ownedArrayParamKinds, 1)
18206
+ : [`${indent(1)}call $owned_boolean_array_to_host_array`]
18207
+ : func.resultType === 'owned_tagged_array_ref' && func.hostTaggedArrayResultKinds
18208
+ ? needsHostSyncScratch
18209
+ ? emitHostOwnedTaggedArrayResultAdaptation(func.hostTaggedArrayResultKinds, ownedTaggedArrayParamKinds, 1)
18210
+ : [
18211
+ `${indent(1)}call $${getOwnedTaggedArrayToHostHelperName(func.hostTaggedArrayResultKinds)}`,
18212
+ ]
18213
+ : func.resultType === 'owned_heap_array_ref' && func.hostHeapArrayResultRepresentation
18214
+ ? needsHostSyncScratch
18215
+ ? emitHostOwnedHeapArrayResultAdaptation(func.hostHeapArrayResultRepresentation, heapArrayParamBoundaries, 1)
18216
+ : [
18217
+ `${indent(1)}call $${getOwnedHeapArrayToHostHelperName(func.hostHeapArrayResultRepresentation)}`,
18218
+ ]
18219
+ : []),
17398
18220
  ')',
17399
18221
  ];
17400
18222
  }
@@ -17448,6 +18270,11 @@ function collectBoxValueTypesFromStatement(statement, used) {
17448
18270
  case 'specialized_object_field_set':
17449
18271
  collectBoxValueTypesFromExpression(statement.value, used);
17450
18272
  return;
18273
+ case 'throw_tagged':
18274
+ collectBoxValueTypesFromExpression(statement.value, used);
18275
+ return;
18276
+ case 'trap':
18277
+ return;
17451
18278
  case 'box_set':
17452
18279
  used.add(statement.valueType);
17453
18280
  collectBoxValueTypesFromExpression(statement.box, used);
@@ -17505,6 +18332,9 @@ function emitClosureRuntimeTypes(module) {
17505
18332
  ...(usedBoxValueTypes.has('owned_string_ref')
17506
18333
  ? ['(type $box_owned_string (struct (field (mut (ref null $string_runtime)))))']
17507
18334
  : []),
18335
+ ...(usedBoxValueTypes.has('owned_heap_array_ref')
18336
+ ? ['(type $box_owned_heap_array (struct (field (mut (ref null $owned_heap_array)))))']
18337
+ : []),
17508
18338
  ...(usedBoxValueTypes.has('owned_array_ref')
17509
18339
  ? ['(type $box_owned_string_array (struct (field (mut (ref null $owned_string_array)))))']
17510
18340
  : []),
@@ -17514,6 +18344,9 @@ function emitClosureRuntimeTypes(module) {
17514
18344
  ...(usedBoxValueTypes.has('owned_boolean_array_ref')
17515
18345
  ? ['(type $box_owned_boolean_array (struct (field (mut (ref null $owned_boolean_array)))))']
17516
18346
  : []),
18347
+ ...(usedBoxValueTypes.has('owned_tagged_array_ref')
18348
+ ? ['(type $box_owned_tagged_array (struct (field (mut (ref null $owned_tagged_array)))))']
18349
+ : []),
17517
18350
  ...(usedBoxValueTypes.has('tagged_ref')
17518
18351
  ? ['(type $box_tagged (struct (field (mut (ref null $tagged_value)))))']
17519
18352
  : []),
@@ -17533,7 +18366,9 @@ function emitClosureCallHelpers(module, layoutsByRepresentationName, fallbackObj
17533
18366
  if (signatures.length === 0) {
17534
18367
  return [];
17535
18368
  }
17536
- const hostClosureSignatures = new Map(getHostClosureBoundarySignatures(module).map(({ signatureId, signature }) => [signatureId, signature]));
18369
+ const hostClosureSignatures = new Map(getHostClosureBoundarySignatures(module)
18370
+ .filter(({ signatureId }) => signatureId !== module.syncTryCatchClosureSignatureId)
18371
+ .map(({ signatureId, signature }) => [signatureId, signature]));
17537
18372
  const closureFunctionsBySignature = new Map();
17538
18373
  for (const func of module.functions) {
17539
18374
  if (func.closureFunctionId === undefined || func.closureSignatureId === undefined) {
@@ -17648,10 +18483,214 @@ function emitClosureCallHelpers(module, layoutsByRepresentationName, fallbackObj
17648
18483
  ];
17649
18484
  });
17650
18485
  }
17651
- function emitHostClosureInvokeExports(module, layoutsByRepresentationName, fallbackObjectLayout) {
17652
- return getHostClosureBoundarySignatures(module)
17653
- .filter(({ needsResultBoundary }) => needsResultBoundary)
18486
+ function emitSyncTryCatchHelper(module, layoutsByRepresentationName, dynamicObjectLayout) {
18487
+ const signatureId = module.syncTryCatchClosureSignatureId;
18488
+ if (signatureId === undefined) {
18489
+ return [];
18490
+ }
18491
+ if (!dynamicObjectLayout) {
18492
+ throw createUnsupportedHeapRuntimeBackendError('Sync try/catch helper requires the dynamic object runtime.');
18493
+ }
18494
+ const stringLiteralIdsByText = createStringLiteralIds(module);
18495
+ const builtinErrorBrandKeyLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, SOUNDSCRIPT_BUILTIN_ERROR_INTERNAL_BRAND_KEY);
18496
+ const builtinErrorLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, 'Error');
18497
+ const builtinErrorNameLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, 'name');
18498
+ const builtinErrorMessageLiteralId = getRequiredStringLiteralId(stringLiteralIdsByText, 'message');
18499
+ const syncTryHostBuiltinErrorHelperName = '__soundscript_sync_try_host_builtin_error_to_dynamic';
18500
+ return [
18501
+ `(func $${syncTryHostBuiltinErrorHelperName} (param $value externref) (result (ref null $${dynamicObjectLayout.watTypeId}))`,
18502
+ `${indent(1)}(local $name externref)`,
18503
+ `${indent(1)}(local $name_tag i32)`,
18504
+ `${indent(1)}(local $message externref)`,
18505
+ `${indent(1)}(local $message_tag i32)`,
18506
+ `${indent(1)}(local $result (ref null $${dynamicObjectLayout.watTypeId}))`,
18507
+ `${indent(1)}call $allocate_dynamic_object`,
18508
+ `${indent(1)}local.set $result`,
18509
+ `${indent(1)}local.get $value`,
18510
+ `${indent(1)}call $${getHostObjectImportFunctionName('get_tagged', 'name')}`,
18511
+ `${indent(1)}local.tee $name`,
18512
+ `${indent(1)}call $tagged_type_tag`,
18513
+ `${indent(1)}local.set $name_tag`,
18514
+ `${indent(1)}local.get $value`,
18515
+ `${indent(1)}call $${getHostObjectImportFunctionName('get_tagged', 'message')}`,
18516
+ `${indent(1)}local.tee $message`,
18517
+ `${indent(1)}call $tagged_type_tag`,
18518
+ `${indent(1)}local.set $message_tag`,
18519
+ `${indent(1)}local.get $message_tag`,
18520
+ `${indent(1)}i32.const 3`,
18521
+ `${indent(1)}i32.eq`,
18522
+ `${indent(1)}if`,
18523
+ `${indent(2)}local.get $result`,
18524
+ `${indent(2)}call $owned_string_literal_${builtinErrorBrandKeyLiteralId}`,
18525
+ `${indent(2)}local.get $name_tag`,
18526
+ `${indent(2)}i32.const 3`,
18527
+ `${indent(2)}i32.eq`,
18528
+ `${indent(2)}if (result (ref null $tagged_value))`,
18529
+ `${indent(3)}local.get $name`,
18530
+ `${indent(3)}call $string_to_owned`,
18531
+ `${indent(3)}call $tag_string`,
18532
+ `${indent(2)}else`,
18533
+ `${indent(3)}call $owned_string_literal_${builtinErrorLiteralId}`,
18534
+ `${indent(3)}call $tag_string`,
18535
+ `${indent(2)}end`,
18536
+ `${indent(2)}call $set_dynamic_object_property`,
18537
+ `${indent(2)}local.get $result`,
18538
+ `${indent(2)}call $owned_string_literal_${builtinErrorNameLiteralId}`,
18539
+ `${indent(2)}local.get $name_tag`,
18540
+ `${indent(2)}i32.const 3`,
18541
+ `${indent(2)}i32.eq`,
18542
+ `${indent(2)}if (result (ref null $tagged_value))`,
18543
+ `${indent(3)}local.get $name`,
18544
+ `${indent(3)}call $string_to_owned`,
18545
+ `${indent(3)}call $tag_string`,
18546
+ `${indent(2)}else`,
18547
+ `${indent(3)}call $owned_string_literal_${builtinErrorLiteralId}`,
18548
+ `${indent(3)}call $tag_string`,
18549
+ `${indent(2)}end`,
18550
+ `${indent(2)}call $set_dynamic_object_property`,
18551
+ `${indent(2)}local.get $result`,
18552
+ `${indent(2)}call $owned_string_literal_${builtinErrorMessageLiteralId}`,
18553
+ `${indent(2)}local.get $message`,
18554
+ `${indent(2)}call $string_to_owned`,
18555
+ `${indent(2)}call $tag_string`,
18556
+ `${indent(2)}call $set_dynamic_object_property`,
18557
+ `${indent(1)}end`,
18558
+ `${indent(1)}local.get $result`,
18559
+ ')',
18560
+ `(func $${SOUNDSCRIPT_TRY_CATCH_TAGGED_HELPER_NAME} (param $closure (ref null $closure)) (result (ref null $owned_tagged_array))`,
18561
+ `${indent(1)}(local $host_result externref)`,
18562
+ `${indent(1)}(local $host_threw i32)`,
18563
+ `${indent(1)}(local $host_value externref)`,
18564
+ `${indent(1)}(local $host_value_tag i32)`,
18565
+ `${indent(1)}(local $host_value_tagged (ref null $tagged_value))`,
18566
+ `${indent(1)}(local $result (ref null $owned_tagged_array))`,
18567
+ `${indent(1)}local.get $closure`,
18568
+ `${indent(1)}call $host_closure_to_host_${signatureId}`,
18569
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_TRY_TAGGED_IMPORT_NAME}`,
18570
+ `${indent(1)}local.set $host_result`,
18571
+ `${indent(1)}local.get $host_result`,
18572
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_TRY_RESULT_THREW_IMPORT_NAME}`,
18573
+ `${indent(1)}local.set $host_threw`,
18574
+ `${indent(1)}local.get $host_result`,
18575
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_TRY_RESULT_VALUE_IMPORT_NAME}`,
18576
+ `${indent(1)}local.set $host_value`,
18577
+ `${indent(1)}local.get $host_value`,
18578
+ `${indent(1)}call $tagged_type_tag`,
18579
+ `${indent(1)}local.set $host_value_tag`,
18580
+ `${indent(1)}(block $host_value_tagged__done`,
18581
+ `${indent(2)}local.get $host_value_tag`,
18582
+ `${indent(2)}i32.const 0`,
18583
+ `${indent(2)}i32.eq`,
18584
+ `${indent(2)}(if`,
18585
+ `${indent(3)}(then`,
18586
+ `${indent(4)}call $tag_undefined`,
18587
+ `${indent(4)}local.set $host_value_tagged`,
18588
+ `${indent(4)}br $host_value_tagged__done`,
18589
+ `${indent(3)})`,
18590
+ `${indent(2)})`,
18591
+ `${indent(2)}local.get $host_value_tag`,
18592
+ `${indent(2)}i32.const 6`,
18593
+ `${indent(2)}i32.eq`,
18594
+ `${indent(2)}(if`,
18595
+ `${indent(3)}(then`,
18596
+ `${indent(4)}call $tag_null`,
18597
+ `${indent(4)}local.set $host_value_tagged`,
18598
+ `${indent(4)}br $host_value_tagged__done`,
18599
+ `${indent(3)})`,
18600
+ `${indent(2)})`,
18601
+ `${indent(2)}local.get $host_value_tag`,
18602
+ `${indent(2)}i32.const 1`,
18603
+ `${indent(2)}i32.eq`,
18604
+ `${indent(2)}(if`,
18605
+ `${indent(3)}(then`,
18606
+ `${indent(4)}local.get $host_value`,
18607
+ `${indent(4)}call $tagged_boolean_value`,
18608
+ `${indent(4)}call $tag_boolean`,
18609
+ `${indent(4)}local.set $host_value_tagged`,
18610
+ `${indent(4)}br $host_value_tagged__done`,
18611
+ `${indent(3)})`,
18612
+ `${indent(2)})`,
18613
+ `${indent(2)}local.get $host_value_tag`,
18614
+ `${indent(2)}i32.const 2`,
18615
+ `${indent(2)}i32.eq`,
18616
+ `${indent(2)}(if`,
18617
+ `${indent(3)}(then`,
18618
+ `${indent(4)}local.get $host_value`,
18619
+ `${indent(4)}call $tagged_number_value`,
18620
+ `${indent(4)}call $tag_number`,
18621
+ `${indent(4)}local.set $host_value_tagged`,
18622
+ `${indent(4)}br $host_value_tagged__done`,
18623
+ `${indent(3)})`,
18624
+ `${indent(2)})`,
18625
+ `${indent(2)}local.get $host_value_tag`,
18626
+ `${indent(2)}i32.const 3`,
18627
+ `${indent(2)}i32.eq`,
18628
+ `${indent(2)}(if`,
18629
+ `${indent(3)}(then`,
18630
+ `${indent(4)}local.get $host_value`,
18631
+ `${indent(4)}call $string_to_owned`,
18632
+ `${indent(4)}call $tag_string`,
18633
+ `${indent(4)}local.set $host_value_tagged`,
18634
+ `${indent(4)}br $host_value_tagged__done`,
18635
+ `${indent(3)})`,
18636
+ `${indent(2)})`,
18637
+ `${indent(2)}local.get $host_value_tag`,
18638
+ `${indent(2)}i32.const 4`,
18639
+ `${indent(2)}i32.eq`,
18640
+ `${indent(2)}(if`,
18641
+ `${indent(3)}(then`,
18642
+ `${indent(4)}local.get $host_value`,
18643
+ `${indent(4)}call $${syncTryHostBuiltinErrorHelperName}`,
18644
+ `${indent(4)}call $tag_heap_object`,
18645
+ `${indent(4)}local.set $host_value_tagged`,
18646
+ `${indent(4)}br $host_value_tagged__done`,
18647
+ `${indent(3)})`,
18648
+ `${indent(2)})`,
18649
+ `${indent(2)}unreachable`,
18650
+ `${indent(1)})`,
18651
+ `${indent(1)}i32.const 2`,
18652
+ `${indent(1)}array.new_default $owned_tagged_array_data`,
18653
+ `${indent(1)}struct.new $owned_tagged_array`,
18654
+ `${indent(1)}local.set $result`,
18655
+ `${indent(1)}local.get $result`,
18656
+ `${indent(1)}struct.get $owned_tagged_array 0`,
18657
+ `${indent(1)}i32.const 0`,
18658
+ `${indent(1)}local.get $host_threw`,
18659
+ `${indent(1)}call $tag_boolean`,
18660
+ `${indent(1)}array.set $owned_tagged_array_data`,
18661
+ `${indent(1)}local.get $result`,
18662
+ `${indent(1)}struct.get $owned_tagged_array 0`,
18663
+ `${indent(1)}i32.const 1`,
18664
+ `${indent(1)}local.get $host_value_tagged`,
18665
+ `${indent(1)}array.set $owned_tagged_array_data`,
18666
+ `${indent(1)}local.get $result`,
18667
+ ')',
18668
+ ];
18669
+ }
18670
+ function emitHostClosureInvokeExports(module, layoutsByRepresentationName, fallbackObjectLayout, dynamicObjectLayout) {
18671
+ const generatorStepSignature = moduleUsesGeneratorStepClosureHostBridge(module)
18672
+ ? getGeneratorStepClosureSignature(module)
18673
+ : undefined;
18674
+ const usesAsyncGeneratorStepPromiseBridge = moduleUsesAsyncGeneratorHostStepBridge(module) ||
18675
+ moduleUsesAsyncGeneratorHostResultBridge(module);
18676
+ const hostClosureSignatures = getHostClosureBoundarySignatures(module);
18677
+ const syncTryCatchSignature = module.syncTryCatchClosureSignatureId === undefined
18678
+ ? undefined
18679
+ : (module.closureSignatures ?? []).find((signature) => signature.id === module.syncTryCatchClosureSignatureId);
18680
+ const invokeExportSignatures = hostClosureSignatures
18681
+ .filter(({ needsResultBoundary }) => needsResultBoundary);
18682
+ if (syncTryCatchSignature &&
18683
+ !invokeExportSignatures.some(({ signatureId }) => signatureId === syncTryCatchSignature.id)) {
18684
+ invokeExportSignatures.push({
18685
+ signatureId: syncTryCatchSignature.id,
18686
+ signature: syncTryCatchSignature,
18687
+ needsParamBoundary: false,
18688
+ needsResultBoundary: true,
18689
+ });
18690
+ }
18691
+ return invokeExportSignatures
17654
18692
  .flatMap(({ signatureId, signature }) => {
18693
+ const isGeneratorStepSignature = generatorStepSignature?.id === signatureId;
17655
18694
  const sourceParamBoundaries = getHostClosureSourceParamBoundaries(signature);
17656
18695
  const invokeLocals = [
17657
18696
  ...sourceParamBoundaries.flatMap((param) => param.type === 'tagged_ref'
@@ -17667,9 +18706,27 @@ function emitHostClosureInvokeExports(module, layoutsByRepresentationName, fallb
17667
18706
  `${indent(1)}(local $result__host_value externref)`,
17668
18707
  ]
17669
18708
  : []),
18709
+ ...(isGeneratorStepSignature
18710
+ ? [
18711
+ `${indent(1)}(local $result__iterator_result_raw (ref null eq))`,
18712
+ `${indent(1)}(local $result__iterator_result (ref null $${dynamicObjectLayout?.watTypeId ?? 'dynamic_object'}))`,
18713
+ `${indent(1)}(local $result__host_object externref)`,
18714
+ ...(usesAsyncGeneratorStepPromiseBridge
18715
+ ? [`${indent(1)}(local $result__brand_tagged (ref null $tagged_value))`]
18716
+ : []),
18717
+ `${indent(1)}(local $result__value_tagged (ref null $tagged_value))`,
18718
+ `${indent(1)}(local $result__value_tag i32)`,
18719
+ `${indent(1)}(local $result__value_host_value externref)`,
18720
+ `${indent(1)}(local $result__done_tagged (ref null $tagged_value))`,
18721
+ `${indent(1)}(local $result__done_tag i32)`,
18722
+ `${indent(1)}(local $result__done_host_value externref)`,
18723
+ ]
18724
+ : []),
17670
18725
  ];
17671
18726
  return [
17672
- `(func $__soundscript_closure_invoke_${signatureId} (export "__soundscript_closure_invoke_${signatureId}") (param $closure (ref null $closure))${sourceParamBoundaries.map((param) => ` (param $arg_${param.sourceIndex} ${param.classTagId !== undefined ? 'externref' : getHostClosureBoundaryWatType(param.type)})`).join('')} (result ${signature.resultClassConstructorTagId !== undefined
18727
+ `(func $__soundscript_closure_invoke_${signatureId} (export "__soundscript_closure_invoke_${signatureId}") (param $closure (ref null $closure))${sourceParamBoundaries.map((param) => ` (param $arg_${param.sourceIndex} ${param.classTagId !== undefined
18728
+ ? 'externref'
18729
+ : getHostClosureBoundaryWatType(param.type)})`).join('')} (result ${signature.resultClassConstructorTagId !== undefined
17673
18730
  ? 'externref'
17674
18731
  : getHostClosureBoundaryWatType(signature.resultType)})`,
17675
18732
  ...invokeLocals,
@@ -17686,7 +18743,63 @@ function emitHostClosureInvokeExports(module, layoutsByRepresentationName, fallb
17686
18743
  ]
17687
18744
  : emitHostClosureBoundaryParamToInternal(param.type, param.closureSignatureId, param.taggedKinds, param.heapRepresentation, param.heapArrayRepresentation, `arg_${param.sourceIndex}`, 1, layoutsByRepresentationName)),
17688
18745
  `${indent(1)}call $closure_call_${signatureId}`,
17689
- ...emitInternalClosureBoundaryResultToHost(signature.resultType, signature.resultClassConstructorTagId, signature.resultClosureSignatureId, signature.resultTaggedPrimitiveKinds, signature.resultHeapRepresentation, signature.resultHeapArrayRepresentation, 1, layoutsByRepresentationName, fallbackObjectLayout),
18746
+ ...(isGeneratorStepSignature
18747
+ ? [
18748
+ `${indent(1)}local.set $result__iterator_result_raw`,
18749
+ `${indent(1)}local.get $result__iterator_result_raw`,
18750
+ `${indent(1)}ref.cast (ref null $${dynamicObjectLayout?.watTypeId ?? 'dynamic_object'})`,
18751
+ `${indent(1)}local.set $result__iterator_result`,
18752
+ ...(usesAsyncGeneratorStepPromiseBridge
18753
+ ? [
18754
+ `${indent(1)}local.get $result__iterator_result`,
18755
+ `${indent(1)}call $owned_string_literal_${getRequiredStringLiteralId(createStringLiteralIds(module), SOUNDSCRIPT_PROMISE_INTERNAL_BRAND_KEY)}`,
18756
+ `${indent(1)}call $get_dynamic_object_property`,
18757
+ `${indent(1)}local.set $result__brand_tagged`,
18758
+ `${indent(1)}local.get $result__brand_tagged`,
18759
+ `${indent(1)}struct.get $tagged_value 0`,
18760
+ `${indent(1)}i32.const 1`,
18761
+ `${indent(1)}i32.eq`,
18762
+ `${indent(1)}if`,
18763
+ `${indent(2)}local.get $result__brand_tagged`,
18764
+ `${indent(2)}call $untag_boolean`,
18765
+ `${indent(2)}if`,
18766
+ `${indent(3)}local.get $result__iterator_result`,
18767
+ `${indent(3)}call ${SOUNDSCRIPT_HOST_PROMISE_TO_HOST_IMPORT_NAME}`,
18768
+ `${indent(3)}return`,
18769
+ `${indent(2)}end`,
18770
+ `${indent(1)}end`,
18771
+ ]
18772
+ : []),
18773
+ `${indent(1)}call $host_object_empty`,
18774
+ `${indent(1)}local.set $result__host_object`,
18775
+ `${indent(1)}local.get $result__iterator_result`,
18776
+ `${indent(1)}call $owned_string_literal_${getRequiredStringLiteralId(createStringLiteralIds(module), 'value')}`,
18777
+ `${indent(1)}call $get_dynamic_object_property`,
18778
+ `${indent(1)}local.set $result__value_tagged`,
18779
+ `${indent(1)}local.get $result__value_tagged`,
18780
+ `${indent(1)}call $${SOUNDSCRIPT_ASYNC_GENERATOR_TAGGED_TO_HOST_HELPER_NAME}`,
18781
+ `${indent(1)}local.set $result__value_host_value`,
18782
+ `${indent(1)}local.get $result__host_object`,
18783
+ `${indent(1)}local.get $result__value_host_value`,
18784
+ `${indent(1)}call $${getHostObjectImportFunctionName('set_tagged', 'value')}`,
18785
+ `${indent(1)}local.get $result__iterator_result`,
18786
+ `${indent(1)}call $owned_string_literal_${getRequiredStringLiteralId(createStringLiteralIds(module), 'done')}`,
18787
+ `${indent(1)}call $get_dynamic_object_property`,
18788
+ `${indent(1)}local.set $result__done_tagged`,
18789
+ ...emitTaggedPrimitiveToHostExternref('result__done_tagged', 'result__done_tag', 'result__done_host_value', {
18790
+ includesBoolean: true,
18791
+ includesNull: false,
18792
+ includesNumber: false,
18793
+ includesString: false,
18794
+ includesUndefined: false,
18795
+ }, 1, indent),
18796
+ `${indent(1)}local.set $result__done_host_value`,
18797
+ `${indent(1)}local.get $result__host_object`,
18798
+ `${indent(1)}local.get $result__done_host_value`,
18799
+ `${indent(1)}call $${getHostObjectImportFunctionName('set_tagged', 'done')}`,
18800
+ `${indent(1)}local.get $result__host_object`,
18801
+ ]
18802
+ : emitInternalClosureBoundaryResultToHost(signature.resultType, signature.resultClassConstructorTagId, signature.resultClosureSignatureId, signature.resultTaggedPrimitiveKinds, signature.resultHeapRepresentation, signature.resultHeapArrayRepresentation, 1, layoutsByRepresentationName, fallbackObjectLayout, dynamicObjectLayout)),
17690
18803
  ')',
17691
18804
  ];
17692
18805
  });
@@ -17699,7 +18812,8 @@ function emitFunction(module, func, moduleUsesPromiseRuntime, functionsByName, r
17699
18812
  assertFunctionIsBackendLowerable(func);
17700
18813
  const header = [
17701
18814
  `(func $${func.name}`,
17702
- ...(func.exportName.length === 0 || functionNeedsHostExportWrapper(func, moduleUsesPromiseRuntime) ||
18815
+ ...(func.exportName.length === 0 ||
18816
+ functionNeedsHostExportWrapper(func, moduleUsesPromiseRuntime) ||
17703
18817
  func.closureFunctionId !== undefined
17704
18818
  ? []
17705
18819
  : [` (export "${func.exportName}")`]),
@@ -17714,10 +18828,14 @@ function emitFunction(module, func, moduleUsesPromiseRuntime, functionsByName, r
17714
18828
  ]
17715
18829
  : []),
17716
18830
  ...(runtime.ownedHeapArrayScratchLocalName
17717
- ? [`${indent(1)}(local $${runtime.ownedHeapArrayScratchLocalName} (ref null $owned_heap_array))`]
18831
+ ? [
18832
+ `${indent(1)}(local $${runtime.ownedHeapArrayScratchLocalName} (ref null $owned_heap_array))`,
18833
+ ]
17718
18834
  : []),
17719
18835
  ...(runtime.ownedArrayScratchLocalName
17720
- ? [`${indent(1)}(local $${runtime.ownedArrayScratchLocalName} (ref null $owned_string_array))`]
18836
+ ? [
18837
+ `${indent(1)}(local $${runtime.ownedArrayScratchLocalName} (ref null $owned_string_array))`,
18838
+ ]
17721
18839
  : []),
17722
18840
  ...(runtime.ownedNumberArrayScratchLocalName
17723
18841
  ? [
@@ -17740,12 +18858,18 @@ function emitFunction(module, func, moduleUsesPromiseRuntime, functionsByName, r
17740
18858
  : getWatValueType(local.type)})`),
17741
18859
  ...func.body.flatMap((statement) => emitStatement(statement, 1, labelCounter, runtime)),
17742
18860
  ];
17743
- return [header, ...body, ')', ...emitHostExportWrapper(func, runtime, moduleUsesPromiseRuntime)];
18861
+ return [
18862
+ header,
18863
+ ...body,
18864
+ ')',
18865
+ ...emitHostExportWrapper(module, func, runtime, moduleUsesPromiseRuntime),
18866
+ ];
17744
18867
  }
17745
18868
  export function emitCompilerModuleToWat(module) {
17746
18869
  const layoutsByRepresentationName = createSpecializedObjectLayouts(module.runtime);
17747
18870
  const specializedObjectInstanceOfUsages = collectSpecializedObjectInstanceOfUsages(module);
17748
18871
  const builtinErrorInstanceOfUsages = collectBuiltinErrorInstanceOfUsages(module);
18872
+ const dynamicObjectLayout = getDynamicObjectLayout(module.runtime);
17749
18873
  const stringRuntimeLayout = getStringRuntimeLayout(module.runtime) ??
17750
18874
  (moduleUsesOwnedStringRuntime(module) ? createDefaultStringRuntimeLayout() : undefined);
17751
18875
  const usedOwnedStringLiteralIds = moduleUsedOwnedStringLiteralIds(module);
@@ -17775,10 +18899,15 @@ export function emitCompilerModuleToWat(module) {
17775
18899
  const lines = [
17776
18900
  '(module',
17777
18901
  ...emitPromiseRuntimeImports(module).map((line) => `${indent(1)}${line}`),
18902
+ ...emitAsyncGeneratorRuntimeImports(module).map((line) => `${indent(1)}${line}`),
18903
+ ...emitSyncGeneratorRuntimeImports(module).map((line) => `${indent(1)}${line}`),
18904
+ ...emitTaggedThrowRuntimeImports(module).map((line) => `${indent(1)}${line}`),
18905
+ ...emitSyncTryCatchRuntimeImports(module).map((line) => `${indent(1)}${line}`),
17778
18906
  ...emitStringRuntimeImports(module).map((line) => `${indent(1)}${line}`),
17779
18907
  ...emitClosureRuntimeImports(module).map((line) => `${indent(1)}${line}`),
17780
18908
  ...module.functions.flatMap((func) => emitHostImportedFunction(func).map((line) => `${indent(1)}${line}`)),
17781
- ...emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackPropertyKeyIdsByKey).map((line) => `${indent(1)}${line}`),
18909
+ ...emitObjectRuntimeImports(module, layoutsByRepresentationName, fallbackPropertyKeyIdsByKey)
18910
+ .map((line) => `${indent(1)}${line}`),
17782
18911
  ...emitArrayRuntimeImportsFromArrays({
17783
18912
  usesHeapParamBoundary: moduleUsesOwnedHeapArrayHostParamBoundary(module),
17784
18913
  usesHeapParamCopyBack: moduleUsesOwnedHeapArrayHostParamCopyBack(module),
@@ -17808,12 +18937,61 @@ export function emitCompilerModuleToWat(module) {
17808
18937
  ...emitOwnedStringNativeHelpers(module, stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
17809
18938
  ...emitTaggedValueType(module.runtime).map((line) => `${indent(1)}${line}`),
17810
18939
  ...emitTaggedValueHelpers(module, stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
18940
+ ...(moduleUsesTaggedThrowRuntime(module)
18941
+ ? [
18942
+ `(func $${SOUNDSCRIPT_THROW_TAGGED_HELPER_NAME} (param $value (ref null $tagged_value))`,
18943
+ `${indent(1)}(local $value__tag i32)`,
18944
+ `${indent(1)}(local $value__host externref)`,
18945
+ `${indent(1)}local.get $value`,
18946
+ `${indent(1)}struct.get $tagged_value 0`,
18947
+ `${indent(1)}local.set $value__tag`,
18948
+ `${indent(1)}local.get $value__tag`,
18949
+ `${indent(1)}i32.const 4`,
18950
+ `${indent(1)}i32.eq`,
18951
+ `${indent(1)}if`,
18952
+ ...(dynamicObjectLayout && moduleUsesBuiltinErrorRuntime(module)
18953
+ ? [
18954
+ `${indent(2)}local.get $value`,
18955
+ `${indent(2)}call $untag_heap_object`,
18956
+ `${indent(2)}ref.test (ref null $${dynamicObjectLayout.watTypeId})`,
18957
+ `${indent(2)}if`,
18958
+ `${indent(3)}local.get $value`,
18959
+ `${indent(3)}call $untag_heap_object`,
18960
+ `${indent(3)}ref.cast (ref null $${dynamicObjectLayout.watTypeId})`,
18961
+ `${indent(3)}call $${getHostDynamicBuiltinErrorHelperName()}`,
18962
+ `${indent(3)}local.set $value__host`,
18963
+ `${indent(3)}local.get $value__host`,
18964
+ `${indent(3)}call ${SOUNDSCRIPT_HOST_THROW_IMPORT_NAME}`,
18965
+ `${indent(3)}unreachable`,
18966
+ `${indent(2)}end`,
18967
+ ]
18968
+ : []),
18969
+ `${indent(2)}unreachable`,
18970
+ `${indent(1)}end`,
18971
+ ...emitTaggedPrimitiveToHostExternref('value', 'value__tag', 'value__host', {
18972
+ includesBoolean: true,
18973
+ includesNull: true,
18974
+ includesNumber: true,
18975
+ includesString: true,
18976
+ includesUndefined: true,
18977
+ }, 1, indent),
18978
+ `${indent(1)}local.set $value__host`,
18979
+ `${indent(1)}local.get $value__host`,
18980
+ `${indent(1)}call ${SOUNDSCRIPT_HOST_THROW_IMPORT_NAME}`,
18981
+ `${indent(1)}unreachable`,
18982
+ `)`,
18983
+ ].map((line) => `${indent(1)}${line}`)
18984
+ : []),
17811
18985
  ...emitOwnedArrayTypesFromArrays({
17812
18986
  usesOwnedHeapArray: moduleUsesOwnedHeapArrayRuntime(module),
17813
- usesOwnedStringArray: moduleUsesObjectKeysArrayRuntime(module),
17814
- usesOwnedNumberArray: moduleUsesOwnedNumberArrayRuntime(module),
17815
- usesOwnedBooleanArray: moduleUsesOwnedBooleanArrayRuntime(module),
17816
- usesOwnedTaggedArray: moduleUsesOwnedTaggedArrayRuntime(module),
18987
+ usesOwnedStringArray: moduleUsesObjectKeysArrayRuntime(module) ||
18988
+ moduleUsesGeneratorStepClosureHostBridge(module),
18989
+ usesOwnedNumberArray: moduleUsesOwnedNumberArrayRuntime(module) ||
18990
+ moduleUsesGeneratorStepClosureHostBridge(module),
18991
+ usesOwnedBooleanArray: moduleUsesOwnedBooleanArrayRuntime(module) ||
18992
+ moduleUsesGeneratorStepClosureHostBridge(module),
18993
+ usesOwnedTaggedArray: moduleUsesOwnedTaggedArrayRuntime(module) ||
18994
+ moduleUsesGeneratorStepClosureHostBridge(module),
17817
18995
  }).map((line) => `${indent(1)}${line}`),
17818
18996
  ...emitClassStaticFieldGlobals(module).map((line) => `${indent(1)}${line}`),
17819
18997
  ...emitOwnedArrayNativeHelpersFromArrays({
@@ -17910,27 +19088,31 @@ export function emitCompilerModuleToWat(module) {
17910
19088
  usesTaggedParamBoundary: moduleUsesOwnedTaggedArrayHostParamBoundary(module),
17911
19089
  usesTaggedParamCopyBack: moduleUsesOwnedTaggedArrayHostParamCopyBack(module),
17912
19090
  usesTaggedResultBoundary: moduleUsesOwnedTaggedArrayHostResultBoundary(module),
19091
+ extraTaggedKindSets: moduleUsesGeneratorStepClosureHostBridge(module)
19092
+ ? [ASYNC_GENERATOR_HOST_TAGGED_ARRAY_BRIDGE_KINDS]
19093
+ : undefined,
17913
19094
  fallbackObjectWatTypeId: fallbackObjectLayout?.watTypeId,
17914
19095
  indent,
17915
19096
  layoutsByRepresentationName,
17916
19097
  stringRuntimeLayout,
17917
19098
  createUnsupportedHeapRuntimeBackendError,
17918
19099
  }).map((line) => `${indent(1)}${line}`),
17919
- ...emitOwnedHeapArrayBoundaryHelpers(module, layoutsByRepresentationName, fallbackObjectLayout).map((line) => `${indent(1)}${line}`),
19100
+ ...emitOwnedHeapArrayBoundaryHelpers(module, layoutsByRepresentationName, fallbackObjectLayout)
19101
+ .map((line) => `${indent(1)}${line}`),
17920
19102
  ...emitStringCodePointAtHelper(module).map((line) => `${indent(1)}${line}`),
17921
19103
  ...emitFallbackObjectTypes(fallbackObjectLayout).map((line) => `${indent(1)}${line}`),
17922
- ...emitDynamicObjectTypes(getDynamicObjectLayout(module.runtime), stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
19104
+ ...emitDynamicObjectTypes(dynamicObjectLayout, stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
17923
19105
  ...emitClosureRuntimeTypes(module).map((line) => `${indent(1)}${line}`),
17924
19106
  ...emitSpecializedObjectTypes(layoutsByRepresentationName).map((line) => `${indent(1)}${line}`),
17925
19107
  ...emitSpecializedObjectInstanceOfHelpers(specializedObjectInstanceOfUsages, layoutsByRepresentationName)
17926
19108
  .map((line) => `${indent(1)}${line}`),
17927
- ...emitBuiltinErrorInstanceOfHelpers(builtinErrorInstanceOfUsages, getDynamicObjectLayout(module.runtime), stringRuntimeLayout, stringLiteralIdsByText).map((line) => `${indent(1)}${line}`),
19109
+ ...emitBuiltinErrorInstanceOfHelpers(builtinErrorInstanceOfUsages, dynamicObjectLayout, stringRuntimeLayout, stringLiteralIdsByText).map((line) => `${indent(1)}${line}`),
17928
19110
  ...emitHostSpecializedObjectBoundaryHelpers(module, layoutsByRepresentationName).map((line) => `${indent(1)}${line}`),
17929
19111
  ...emitHostClassConstructorBoundaryHelpers(module, layoutsByRepresentationName).map((line) => `${indent(1)}${line}`),
17930
19112
  ...emitHostFallbackObjectBoundaryHelpers(module, fallbackObjectLayout, fallbackPropertyKeyIdsByKey, layoutsByRepresentationName)
17931
19113
  .map((line) => `${indent(1)}${line}`),
17932
19114
  ...emitFallbackRuntimeHelpers(fallbackObjectLayout, fallbackPropertyKeyIdsByKey).map((line) => `${indent(1)}${line}`),
17933
- ...emitDynamicRuntimeHelpers(getDynamicObjectLayout(module.runtime), stringRuntimeLayout, {
19115
+ ...emitDynamicRuntimeHelpers(dynamicObjectLayout, stringRuntimeLayout, {
17934
19116
  usesBooleanValueListing: usesDynamicObjectBooleanValueListing,
17935
19117
  usesCopyEntries: usesDynamicObjectEntryCopy,
17936
19118
  usesKeyListing: usesDynamicObjectKeyListing,
@@ -17941,9 +19123,13 @@ export function emitCompilerModuleToWat(module) {
17941
19123
  usesTaggedValueListing: usesDynamicObjectTaggedValueListing,
17942
19124
  }).map((line) => `${indent(1)}${line}`),
17943
19125
  ...emitHostDynamicCollectionBoundaryHelpers(module, stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
17944
- ...emitClosureCallHelpers(module, layoutsByRepresentationName, fallbackObjectLayout, getDynamicObjectLayout(module.runtime)).map((line) => `${indent(1)}${line}`),
17945
- ...emitPromiseRuntimeHelpers(module, getDynamicObjectLayout(module.runtime), stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
17946
- ...emitHostClosureInvokeExports(module, layoutsByRepresentationName, fallbackObjectLayout).map((line) => `${indent(1)}${line}`),
19126
+ ...emitClosureCallHelpers(module, layoutsByRepresentationName, fallbackObjectLayout, dynamicObjectLayout).map((line) => `${indent(1)}${line}`),
19127
+ ...emitSyncTryCatchHelper(module, layoutsByRepresentationName, dynamicObjectLayout).map((line) => `${indent(1)}${line}`),
19128
+ ...emitPromiseRuntimeHelpers(module, dynamicObjectLayout, stringRuntimeLayout).map((line) => `${indent(1)}${line}`),
19129
+ ...((!usesPromiseRuntime || moduleUsesSyncTryCatchRuntime(module))
19130
+ ? emitHostBuiltinErrorBridgeHelpers(module, dynamicObjectLayout).map((line) => `${indent(1)}${line}`)
19131
+ : []),
19132
+ ...emitHostClosureInvokeExports(module, layoutsByRepresentationName, fallbackObjectLayout, dynamicObjectLayout).map((line) => `${indent(1)}${line}`),
17947
19133
  ...emitClassStaticFieldStart(module).map((line) => `${indent(1)}${line}`),
17948
19134
  ...module.functions.flatMap((func) => emitFunction(module, func, usesPromiseRuntime, functionsByName, module.runtime, layoutsByRepresentationName, stringRuntimeLayout).map((line) => `${indent(1)}${line}`)),
17949
19135
  ')',