@divmain/jdm-asm 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (397) hide show
  1. package/.github/workflows/ci.yml +53 -0
  2. package/.oxfmtrc.json +16 -0
  3. package/.oxlintrc.json +183 -0
  4. package/AGENTS.md +81 -0
  5. package/README.md +769 -0
  6. package/asconfig.json +23 -0
  7. package/benchmarks/fixtures.ts +111 -0
  8. package/benchmarks/input-fixtures.ts +80 -0
  9. package/benchmarks/run.ts +913 -0
  10. package/benchmarks/worker-pool.ts +223 -0
  11. package/benchmarks/worker.ts +374 -0
  12. package/dist/index.d.ts +996 -0
  13. package/dist/index.js +12239 -0
  14. package/dist/index.js.map +1 -0
  15. package/package.json +49 -0
  16. package/scripts/run-all-tests.ts +220 -0
  17. package/src/compiler/EXPRESSION_SUBSETS.md +228 -0
  18. package/src/compiler/asc-compiler.ts +315 -0
  19. package/src/compiler/ast-types.ts +215 -0
  20. package/src/compiler/build.ts +56 -0
  21. package/src/compiler/cache.ts +414 -0
  22. package/src/compiler/code-generators.ts +211 -0
  23. package/src/compiler/codegen/index.ts +15 -0
  24. package/src/compiler/codegen/js-marshal.ts +999 -0
  25. package/src/compiler/codegen/js-validation.ts +243 -0
  26. package/src/compiler/codegen.ts +19 -0
  27. package/src/compiler/compile-time-validation.ts +507 -0
  28. package/src/compiler/cst-visitor.ts +434 -0
  29. package/src/compiler/errors.ts +227 -0
  30. package/src/compiler/expression-parser.ts +536 -0
  31. package/src/compiler/graph.ts +197 -0
  32. package/src/compiler/index.ts +199 -0
  33. package/src/compiler/input-validation.ts +33 -0
  34. package/src/compiler/marshal-gen.ts +21 -0
  35. package/src/compiler/nodes/context-resolvers.ts +197 -0
  36. package/src/compiler/nodes/decision-table.ts +507 -0
  37. package/src/compiler/nodes/decision.ts +292 -0
  38. package/src/compiler/nodes/expression-compiler.ts +526 -0
  39. package/src/compiler/nodes/expression.ts +425 -0
  40. package/src/compiler/nodes/function.ts +316 -0
  41. package/src/compiler/nodes/input.ts +60 -0
  42. package/src/compiler/nodes/switch.ts +547 -0
  43. package/src/compiler/optimizer.ts +948 -0
  44. package/src/compiler/orchestrator.ts +352 -0
  45. package/src/compiler/parser.ts +115 -0
  46. package/src/compiler/result-selection.ts +161 -0
  47. package/src/compiler/runtime/index.ts +26 -0
  48. package/src/compiler/runtime-codegen.ts +211 -0
  49. package/src/compiler/runtime-validation-codegen.ts +294 -0
  50. package/src/compiler/runtime.ts +452 -0
  51. package/src/compiler/schema.ts +245 -0
  52. package/src/compiler/switch-branch-detection.ts +92 -0
  53. package/src/compiler/types.ts +136 -0
  54. package/src/compiler/unary-ast-transforms.ts +148 -0
  55. package/src/compiler/unary-parser.ts +301 -0
  56. package/src/compiler/unary-transform.ts +161 -0
  57. package/src/compiler/utils.ts +27 -0
  58. package/src/compiler/virtual-fs.ts +90 -0
  59. package/src/compiler/wasm-instantiate.ts +127 -0
  60. package/src/index.ts +1 -0
  61. package/src/runtime/arrays.ts +579 -0
  62. package/src/runtime/context.ts +189 -0
  63. package/src/runtime/expressions.ts +1811 -0
  64. package/src/runtime/index.ts +8 -0
  65. package/src/runtime/memory.ts +607 -0
  66. package/src/runtime/strings.ts +260 -0
  67. package/src/runtime/tables.ts +96 -0
  68. package/src/runtime/tsconfig.json +4 -0
  69. package/src/runtime/values.ts +209 -0
  70. package/test-data/README.md +83 -0
  71. package/test-data/decision-tables/basic/8k.json +87992 -0
  72. package/test-data/decision-tables/basic/affiliate-commission-calculator.json +228 -0
  73. package/test-data/decision-tables/basic/airline-loyalty-points-calculations.json +285 -0
  74. package/test-data/decision-tables/basic/airline-upgrade-eligibility.json +466 -0
  75. package/test-data/decision-tables/basic/auto-insurance-premium-calculator.json +412 -0
  76. package/test-data/decision-tables/basic/booking-personalization-system.json +553 -0
  77. package/test-data/decision-tables/basic/care-team-assignment-system.json +585 -0
  78. package/test-data/decision-tables/basic/claim-validation-system.json +307 -0
  79. package/test-data/decision-tables/basic/clinical-lab-result-interpreter.json +433 -0
  80. package/test-data/decision-tables/basic/clinical-treatment-protocol.json +474 -0
  81. package/test-data/decision-tables/basic/credit-limit-adjustment.json +479 -0
  82. package/test-data/decision-tables/basic/customer-eligibility-engine.json +551 -0
  83. package/test-data/decision-tables/basic/customer-lifetime-value.json +200 -0
  84. package/test-data/decision-tables/basic/customer-onboarding-kyc-verification.json +611 -0
  85. package/test-data/decision-tables/basic/customer-service-escalation.json +191 -0
  86. package/test-data/decision-tables/basic/decision-table-discounts.json +168 -0
  87. package/test-data/decision-tables/basic/decision-table-shipping.json +398 -0
  88. package/test-data/decision-tables/basic/delivery-route-optimizer.json +271 -0
  89. package/test-data/decision-tables/basic/device-compatibility-checker.json +303 -0
  90. package/test-data/decision-tables/basic/disaster-relief-fund-allocation.json +296 -0
  91. package/test-data/decision-tables/basic/dynamic-fx-rate-pricing-system.json +237 -0
  92. package/test-data/decision-tables/basic/dynamic-marketplace-comission-calculator.json +242 -0
  93. package/test-data/decision-tables/basic/dynamic-shipping-cost-calculator.json +378 -0
  94. package/test-data/decision-tables/basic/dynamic-tarrif-engine.json +289 -0
  95. package/test-data/decision-tables/basic/dynamic-ticket-pricing.json +325 -0
  96. package/test-data/decision-tables/basic/empty-column-with-space.json +100 -0
  97. package/test-data/decision-tables/basic/empty-column-without-space.json +100 -0
  98. package/test-data/decision-tables/basic/environment-compliance-assessment.json +386 -0
  99. package/test-data/decision-tables/basic/expression-table-map.json +313 -0
  100. package/test-data/decision-tables/basic/flash-sale-eligibility.json +366 -0
  101. package/test-data/decision-tables/basic/flight-dispatch-decision-system.json +455 -0
  102. package/test-data/decision-tables/basic/flight-rebooking-fee-calculator.json +406 -0
  103. package/test-data/decision-tables/basic/government-assistance.json +299 -0
  104. package/test-data/decision-tables/basic/grant-funding-distribution.json +307 -0
  105. package/test-data/decision-tables/basic/hazardous-materials-management-system.json +414 -0
  106. package/test-data/decision-tables/basic/immigration-eligibility-evaluator.json +765 -0
  107. package/test-data/decision-tables/basic/import-duties-calculator.json +318 -0
  108. package/test-data/decision-tables/basic/insurance-agent-commission.json +228 -0
  109. package/test-data/decision-tables/basic/insurance-coverage-calculator.json +362 -0
  110. package/test-data/decision-tables/basic/insurance-underwriting-risk.json +321 -0
  111. package/test-data/decision-tables/basic/international-roaming-policy-manager.json +199 -0
  112. package/test-data/decision-tables/basic/legacy-plan-management.json +434 -0
  113. package/test-data/decision-tables/basic/marketplace-listing-verification-system.json +334 -0
  114. package/test-data/decision-tables/basic/medication-dosage-calculator.json +318 -0
  115. package/test-data/decision-tables/basic/merch-bags.json +171 -0
  116. package/test-data/decision-tables/basic/municipal-permit-evaluation-system.json +364 -0
  117. package/test-data/decision-tables/basic/mvno-partner-enablement.json +313 -0
  118. package/test-data/decision-tables/basic/partner-revenue-sharing.json +244 -0
  119. package/test-data/decision-tables/basic/payment-routing-and-fee-calculator.json +475 -0
  120. package/test-data/decision-tables/basic/policy-discount-calculator.json +307 -0
  121. package/test-data/decision-tables/basic/policy-eligibility-analyzer.json +299 -0
  122. package/test-data/decision-tables/basic/product-listing-scoring.json +358 -0
  123. package/test-data/decision-tables/basic/realtime-fraud-detection.json +235 -0
  124. package/test-data/decision-tables/basic/regional-compliance-manager.json +278 -0
  125. package/test-data/decision-tables/basic/returns-and-refund-policy.json +366 -0
  126. package/test-data/decision-tables/basic/returns-processing-system.json +448 -0
  127. package/test-data/decision-tables/basic/school-district-resource-allocation.json +282 -0
  128. package/test-data/decision-tables/basic/seat-map-optimization.json +325 -0
  129. package/test-data/decision-tables/basic/seller-fee-calculator.json +307 -0
  130. package/test-data/decision-tables/basic/service-level-agreement-enforcement.json +575 -0
  131. package/test-data/decision-tables/basic/smart-financial-product-matcher.json +249 -0
  132. package/test-data/decision-tables/basic/supply-chain-risk.json +316 -0
  133. package/test-data/decision-tables/basic/table-loop.json +93 -0
  134. package/test-data/decision-tables/basic/table.json +76 -0
  135. package/test-data/decision-tables/basic/traffic-violation-penalty-calculator.json +436 -0
  136. package/test-data/decision-tables/basic/transaction-compliance-classifier.json +525 -0
  137. package/test-data/decision-tables/basic/vehicle-claims-resolution.json +310 -0
  138. package/test-data/decision-tables/basic/warehouse-storage-location.json +345 -0
  139. package/test-data/decision-tables/hit-policy-collect/collect-multiple-matches.json +127 -0
  140. package/test-data/decision-tables/hit-policy-collect/collect-no-match.json +95 -0
  141. package/test-data/decision-tables/hit-policy-first/first-match.json +103 -0
  142. package/test-data/decision-tables/hit-policy-first/no-match.json +95 -0
  143. package/test-data/decision-tables/hit-policy-output-order/output-order-respected.json +94 -0
  144. package/test-data/decision-tables/hit-policy-output-order/string-output-order.json +94 -0
  145. package/test-data/decision-tables/hit-policy-priority/priority-respected.json +86 -0
  146. package/test-data/decision-tables/hit-policy-rule-order/rule-order-respected.json +94 -0
  147. package/test-data/decision-tables/hit-policy-unique/all-match-error.json +89 -0
  148. package/test-data/decision-tables/hit-policy-unique/multiple-match-error.json +89 -0
  149. package/test-data/decision-tables/hit-policy-unique/no-match.json +88 -0
  150. package/test-data/decision-tables/hit-policy-unique/unique-match.json +99 -0
  151. package/test-data/expressions/arithmetic/error-cyclic.json +114 -0
  152. package/test-data/expressions/arithmetic/error-missing-input.json +54 -0
  153. package/test-data/expressions/arithmetic/error-missing-output.json +54 -0
  154. package/test-data/expressions/arithmetic/expression-default.json +93 -0
  155. package/test-data/expressions/arithmetic/expression-fields.json +94 -0
  156. package/test-data/expressions/arithmetic/expression-loop.json +94 -0
  157. package/test-data/expressions/arithmetic/expression-passthrough.json +108 -0
  158. package/test-data/expressions/arithmetic/expression.json +69 -0
  159. package/test-data/expressions/arithmetic/nested-request.json +125 -0
  160. package/test-data/expressions/arithmetic/number-function.json +58 -0
  161. package/test-data/expressions/arithmetic/test-number-functions.json +68 -0
  162. package/test-data/expressions/functions/all.json +149 -0
  163. package/test-data/expressions/functions/avg.json +89 -0
  164. package/test-data/expressions/functions/filter.json +109 -0
  165. package/test-data/expressions/functions/flat.json +167 -0
  166. package/test-data/expressions/functions/map-strings.json +65 -0
  167. package/test-data/expressions/functions/map.json +73 -0
  168. package/test-data/expressions/functions/reduce.json +49 -0
  169. package/test-data/expressions/functions/some.json +175 -0
  170. package/test-data/expressions/functions/sort-strings.json +97 -0
  171. package/test-data/expressions/functions/sort.json +97 -0
  172. package/test-data/expressions/logical/logical-and.json +116 -0
  173. package/test-data/expressions/logical/logical-complex.json +260 -0
  174. package/test-data/expressions/logical/logical-not.json +111 -0
  175. package/test-data/expressions/logical/logical-or.json +123 -0
  176. package/test-data/expressions/string/string-comparison.json +128 -0
  177. package/test-data/expressions/string/string-concat.json +106 -0
  178. package/test-data/expressions/string/string-contains.json +125 -0
  179. package/test-data/expressions/string/string-endsWith.json +113 -0
  180. package/test-data/expressions/string/string-indexOf.json +131 -0
  181. package/test-data/expressions/string/string-join.json +92 -0
  182. package/test-data/expressions/string/string-lower.json +94 -0
  183. package/test-data/expressions/string/string-replace.json +130 -0
  184. package/test-data/expressions/string/string-split.json +101 -0
  185. package/test-data/expressions/string/string-startsWith.json +113 -0
  186. package/test-data/expressions/string/string-substring.json +138 -0
  187. package/test-data/expressions/string/string-trim.json +100 -0
  188. package/test-data/expressions/string/string-upper.json +94 -0
  189. package/test-data/other/custom.json +51 -0
  190. package/test-data/other/customer-input-schema.json +34 -0
  191. package/test-data/other/customer-output-schema.json +34 -0
  192. package/test-data/other/passthrough.json +31 -0
  193. package/test-data/sub-decisions/basic/$nodes-child.json +31 -0
  194. package/test-data/sub-decisions/basic/$nodes-parent.json +49 -0
  195. package/test-data/sub-decisions/basic/recursive-table1.json +49 -0
  196. package/test-data/sub-decisions/basic/recursive-table2.json +49 -0
  197. package/test-data/sub-decisions/complex-multi/approval-decision.json +31 -0
  198. package/test-data/sub-decisions/complex-multi/complex-dag.json +175 -0
  199. package/test-data/sub-decisions/complex-multi/credit-check.json +31 -0
  200. package/test-data/sub-decisions/complex-multi/customer-segmentation.json +31 -0
  201. package/test-data/sub-decisions/complex-multi/discount-eligibility.json +31 -0
  202. package/test-data/sub-decisions/complex-multi/eligibility-check.json +31 -0
  203. package/test-data/sub-decisions/complex-multi/final-offer.json +31 -0
  204. package/test-data/sub-decisions/complex-multi/income-verification.json +31 -0
  205. package/test-data/sub-decisions/complex-multi/linear-chain.json +121 -0
  206. package/test-data/sub-decisions/complex-multi/pricing-calculation.json +31 -0
  207. package/test-data/sub-decisions/complex-multi/product-eligibility.json +31 -0
  208. package/test-data/sub-decisions/complex-multi/risk-assessment.json +31 -0
  209. package/test-data/sub-decisions/complex-multi/shared-validation.json +31 -0
  210. package/test-data/sub-decisions/complex-multi/validation.json +31 -0
  211. package/test-data/sub-decisions/diamond/decision-a.json +31 -0
  212. package/test-data/sub-decisions/diamond/decision-b.json +31 -0
  213. package/test-data/sub-decisions/diamond/decision-c.json +31 -0
  214. package/test-data/sub-decisions/diamond/decision-shared.json +31 -0
  215. package/test-data/sub-decisions/diamond/diamond-pattern.json +109 -0
  216. package/test-data/sub-decisions/error-propagation/parent-calls-error.json +44 -0
  217. package/test-data/sub-decisions/error-propagation/sub-decision-with-error.json +60 -0
  218. package/test-data/switch-nodes/basic/account-dormancy-management.json +245 -0
  219. package/test-data/switch-nodes/basic/application-risk-assessment.json +474 -0
  220. package/test-data/switch-nodes/basic/cellular-data-rollover-system.json +281 -0
  221. package/test-data/switch-nodes/basic/clinical-pathway-selection.json +454 -0
  222. package/test-data/switch-nodes/basic/insurance-prior-authorization.json +467 -0
  223. package/test-data/switch-nodes/basic/last-mile-delivery-assignment.json +373 -0
  224. package/test-data/switch-nodes/basic/loan-approval.json +469 -0
  225. package/test-data/switch-nodes/basic/multi-switch.json +498 -0
  226. package/test-data/switch-nodes/basic/online-checkin-eligibility.json +285 -0
  227. package/test-data/switch-nodes/basic/order-consolidation-system.json +493 -0
  228. package/test-data/switch-nodes/basic/seller-approval-workflow.json +383 -0
  229. package/test-data/switch-nodes/basic/set-fee.json +243 -0
  230. package/test-data/switch-nodes/basic/shipping-carrier-selector.json +379 -0
  231. package/test-data/switch-nodes/basic/switch-node.json +167 -0
  232. package/test-data/switch-nodes/basic/switch-performance-2.json +1307 -0
  233. package/test-data/switch-nodes/basic/switch-performance.json +691 -0
  234. package/test-data/switch-nodes/basic/tax-exemption.json +295 -0
  235. package/test-data/switch-nodes/basic/warehouse-cross-docking.json +313 -0
  236. package/test-data/switch-nodes/default-cases/switch-with-default.json +134 -0
  237. package/test-data/zen-reference/$nodes-child.json +69 -0
  238. package/test-data/zen-reference/$nodes-parent.json +34 -0
  239. package/test-data/zen-reference/8k.json +87992 -0
  240. package/test-data/zen-reference/credit-analysis.json +324 -0
  241. package/test-data/zen-reference/custom.json +51 -0
  242. package/test-data/zen-reference/customer-input-schema.json +34 -0
  243. package/test-data/zen-reference/customer-output-schema.json +34 -0
  244. package/test-data/zen-reference/error-cyclic.json +114 -0
  245. package/test-data/zen-reference/error-missing-input.json +54 -0
  246. package/test-data/zen-reference/error-missing-output.json +54 -0
  247. package/test-data/zen-reference/expression.json +69 -0
  248. package/test-data/zen-reference/function-v2.json +48 -0
  249. package/test-data/zen-reference/function.json +46 -0
  250. package/test-data/zen-reference/graphs/account-dormancy-management.json +245 -0
  251. package/test-data/zen-reference/graphs/affiliate-commission-calculator.json +228 -0
  252. package/test-data/zen-reference/graphs/airline-loyalty-points-calculations.json +285 -0
  253. package/test-data/zen-reference/graphs/airline-upgrade-eligibility.json +466 -0
  254. package/test-data/zen-reference/graphs/aml.json +537 -0
  255. package/test-data/zen-reference/graphs/application-risk-assessment.json +474 -0
  256. package/test-data/zen-reference/graphs/auto-insurance-premium-calculator.json +412 -0
  257. package/test-data/zen-reference/graphs/booking-personalization-system.json +553 -0
  258. package/test-data/zen-reference/graphs/care-team-assignment-system.json +585 -0
  259. package/test-data/zen-reference/graphs/cellular-data-rollover-system.json +281 -0
  260. package/test-data/zen-reference/graphs/claim-validation-system.json +307 -0
  261. package/test-data/zen-reference/graphs/clinical-lab-result-interpreter.json +433 -0
  262. package/test-data/zen-reference/graphs/clinical-pathway-selection.json +454 -0
  263. package/test-data/zen-reference/graphs/clinical-treatment-protocol.json +474 -0
  264. package/test-data/zen-reference/graphs/company-analysis.json +390 -0
  265. package/test-data/zen-reference/graphs/credit-limit-adjustment.json +479 -0
  266. package/test-data/zen-reference/graphs/customer-eligibility-engine.json +551 -0
  267. package/test-data/zen-reference/graphs/customer-lifetime-value.json +200 -0
  268. package/test-data/zen-reference/graphs/customer-onboarding-kyc-verification.json +611 -0
  269. package/test-data/zen-reference/graphs/customer-service-escalation.json +191 -0
  270. package/test-data/zen-reference/graphs/decision-table-discounts.json +168 -0
  271. package/test-data/zen-reference/graphs/decision-table-shipping.json +398 -0
  272. package/test-data/zen-reference/graphs/delivery-route-optimizer.json +271 -0
  273. package/test-data/zen-reference/graphs/device-compatibility-checker.json +303 -0
  274. package/test-data/zen-reference/graphs/disaster-relief-fund-allocation.json +296 -0
  275. package/test-data/zen-reference/graphs/dynamic-fx-rate-pricing-system.json +237 -0
  276. package/test-data/zen-reference/graphs/dynamic-marketplace-comission-calculator.json +242 -0
  277. package/test-data/zen-reference/graphs/dynamic-shipping-cost-calculator.json +378 -0
  278. package/test-data/zen-reference/graphs/dynamic-tarrif-engine.json +289 -0
  279. package/test-data/zen-reference/graphs/dynamic-ticket-pricing.json +325 -0
  280. package/test-data/zen-reference/graphs/empty-column-with-space.json +100 -0
  281. package/test-data/zen-reference/graphs/empty-column-without-space.json +100 -0
  282. package/test-data/zen-reference/graphs/environment-compliance-assessment.json +386 -0
  283. package/test-data/zen-reference/graphs/expression-default.json +93 -0
  284. package/test-data/zen-reference/graphs/expression-fields.json +94 -0
  285. package/test-data/zen-reference/graphs/expression-loop.json +94 -0
  286. package/test-data/zen-reference/graphs/expression-passthrough.json +108 -0
  287. package/test-data/zen-reference/graphs/expression-table-map.json +313 -0
  288. package/test-data/zen-reference/graphs/flash-sale-eligibility.json +366 -0
  289. package/test-data/zen-reference/graphs/flight-dispatch-decision-system.json +455 -0
  290. package/test-data/zen-reference/graphs/flight-rebooking-fee-calculator.json +406 -0
  291. package/test-data/zen-reference/graphs/government-assistance.json +299 -0
  292. package/test-data/zen-reference/graphs/grant-funding-distribution.json +307 -0
  293. package/test-data/zen-reference/graphs/hazardous-materials-management-system.json +414 -0
  294. package/test-data/zen-reference/graphs/immigration-eligibility-evaluator.json +765 -0
  295. package/test-data/zen-reference/graphs/import-duties-calculator.json +318 -0
  296. package/test-data/zen-reference/graphs/insurance-agent-commission.json +228 -0
  297. package/test-data/zen-reference/graphs/insurance-breakdown.json +421 -0
  298. package/test-data/zen-reference/graphs/insurance-coverage-calculator.json +362 -0
  299. package/test-data/zen-reference/graphs/insurance-prior-authorization.json +467 -0
  300. package/test-data/zen-reference/graphs/insurance-underwriting-risk.json +321 -0
  301. package/test-data/zen-reference/graphs/international-roaming-policy-manager.json +199 -0
  302. package/test-data/zen-reference/graphs/last-mile-delivery-assignment.json +373 -0
  303. package/test-data/zen-reference/graphs/legacy-plan-management.json +434 -0
  304. package/test-data/zen-reference/graphs/loan-approval.json +469 -0
  305. package/test-data/zen-reference/graphs/marketplace-listing-verification-system.json +334 -0
  306. package/test-data/zen-reference/graphs/medication-dosage-calculator.json +318 -0
  307. package/test-data/zen-reference/graphs/merch-bags.json +171 -0
  308. package/test-data/zen-reference/graphs/multi-switch.json +498 -0
  309. package/test-data/zen-reference/graphs/municipal-permit-evaluation-system.json +364 -0
  310. package/test-data/zen-reference/graphs/mvno-partner-enablement.json +313 -0
  311. package/test-data/zen-reference/graphs/nested-request.json +125 -0
  312. package/test-data/zen-reference/graphs/online-checkin-eligibility.json +285 -0
  313. package/test-data/zen-reference/graphs/order-consolidation-system.json +493 -0
  314. package/test-data/zen-reference/graphs/partner-revenue-sharing.json +244 -0
  315. package/test-data/zen-reference/graphs/payment-routing-and-fee-calculator.json +475 -0
  316. package/test-data/zen-reference/graphs/policy-discount-calculator.json +307 -0
  317. package/test-data/zen-reference/graphs/policy-eligibility-analyzer.json +299 -0
  318. package/test-data/zen-reference/graphs/product-listing-scoring.json +358 -0
  319. package/test-data/zen-reference/graphs/realtime-fraud-detection.json +235 -0
  320. package/test-data/zen-reference/graphs/regional-compliance-manager.json +278 -0
  321. package/test-data/zen-reference/graphs/returns-and-refund-policy.json +366 -0
  322. package/test-data/zen-reference/graphs/returns-processing-system.json +448 -0
  323. package/test-data/zen-reference/graphs/school-district-resource-allocation.json +282 -0
  324. package/test-data/zen-reference/graphs/seat-map-optimization.json +325 -0
  325. package/test-data/zen-reference/graphs/seller-approval-workflow.json +383 -0
  326. package/test-data/zen-reference/graphs/seller-fee-calculator.json +307 -0
  327. package/test-data/zen-reference/graphs/service-level-agreement-enforcement.json +575 -0
  328. package/test-data/zen-reference/graphs/set-fee.json +243 -0
  329. package/test-data/zen-reference/graphs/shipping-carrier-selector.json +379 -0
  330. package/test-data/zen-reference/graphs/smart-financial-product-matcher.json +249 -0
  331. package/test-data/zen-reference/graphs/supply-chain-risk.json +316 -0
  332. package/test-data/zen-reference/graphs/table-loop.json +93 -0
  333. package/test-data/zen-reference/graphs/tax-exemption.json +295 -0
  334. package/test-data/zen-reference/graphs/traffic-violation-penalty-calculator.json +436 -0
  335. package/test-data/zen-reference/graphs/transaction-compliance-classifier.json +525 -0
  336. package/test-data/zen-reference/graphs/vehicle-claims-resolution.json +310 -0
  337. package/test-data/zen-reference/graphs/warehouse-cross-docking.json +313 -0
  338. package/test-data/zen-reference/graphs/warehouse-storage-location.json +345 -0
  339. package/test-data/zen-reference/http-function.json +34 -0
  340. package/test-data/zen-reference/infinite-function.json +46 -0
  341. package/test-data/zen-reference/js/imports.js +25 -0
  342. package/test-data/zen-reference/passthrough.json +31 -0
  343. package/test-data/zen-reference/recursive-table1.json +49 -0
  344. package/test-data/zen-reference/recursive-table2.json +49 -0
  345. package/test-data/zen-reference/sleep-function.json +34 -0
  346. package/test-data/zen-reference/switch-node.json +167 -0
  347. package/test-data/zen-reference/switch-performance-2.json +1307 -0
  348. package/test-data/zen-reference/switch-performance.json +691 -0
  349. package/test-data/zen-reference/table.json +76 -0
  350. package/tests/helpers/index.ts +73 -0
  351. package/tests/helpers/mock-context.ts +231 -0
  352. package/tests/helpers/round-trip.ts +398 -0
  353. package/tests/helpers/test-harness-comparison.ts +325 -0
  354. package/tests/helpers/test-harness-wasm.ts +710 -0
  355. package/tests/helpers/test-harness.ts +28 -0
  356. package/tests/helpers/wasm-test.ts +659 -0
  357. package/tests/integration/compilation-errors.test.ts +864 -0
  358. package/tests/integration/decision-tables.test.ts +531 -0
  359. package/tests/integration/edge-cases.test.ts +787 -0
  360. package/tests/integration/expressions.test.ts +513 -0
  361. package/tests/integration/function-node-integration.test.ts +182 -0
  362. package/tests/integration/sub-decisions.test.ts +108 -0
  363. package/tests/integration/switch-nodes.test.ts +399 -0
  364. package/tests/integration/unary-or-matching.test.ts +53 -0
  365. package/tests/integration/wasm-data-types.test.ts +398 -0
  366. package/tests/integration/wasm-errors.test.ts +199 -0
  367. package/tests/integration/wasm-execution.test.ts +348 -0
  368. package/tests/integration/wasm-memory.test.ts +228 -0
  369. package/tests/scripts/analyze-coverage.ts +166 -0
  370. package/tests/scripts/categorize-tests.ts +396 -0
  371. package/tests/scripts/coverage-analysis.ts +836 -0
  372. package/tests/unit/compiler/cache.test.ts +238 -0
  373. package/tests/unit/compiler/errors.test.ts +316 -0
  374. package/tests/unit/compiler/graph-scalability.test.ts +510 -0
  375. package/tests/unit/compiler/graph.test.ts +878 -0
  376. package/tests/unit/compiler/input-validation.test.ts +447 -0
  377. package/tests/unit/compiler/logical-and-parser.test.ts +143 -0
  378. package/tests/unit/compiler/logical-not-parser.test.ts +107 -0
  379. package/tests/unit/compiler/logical-or-parser.test.ts +236 -0
  380. package/tests/unit/compiler/marshal-gen/marshal-gen.test.ts +97 -0
  381. package/tests/unit/compiler/nodes/decision-table.test.ts +103 -0
  382. package/tests/unit/compiler/nodes/decision.test.ts +182 -0
  383. package/tests/unit/compiler/nodes/function-compile.test.ts +204 -0
  384. package/tests/unit/compiler/nodes/function.test.ts +176 -0
  385. package/tests/unit/compiler/nodes/input.test.ts +30 -0
  386. package/tests/unit/compiler/nodes/switch.test.ts +127 -0
  387. package/tests/unit/compiler/optimizer-cache.test.ts +327 -0
  388. package/tests/unit/compiler/optimizer-implementation.test.ts +625 -0
  389. package/tests/unit/compiler/parser.test.ts +508 -0
  390. package/tests/unit/compiler/runtime-error-cleanup.test.ts +426 -0
  391. package/tests/unit/compiler/runtime-validation.test.ts +303 -0
  392. package/tests/unit/compiler/runtime.test.ts +221 -0
  393. package/tests/unit/compiler/schema/schema.test.ts +248 -0
  394. package/tests/unit/compiler/unary-ast-transforms.test.ts +245 -0
  395. package/tsconfig.json +27 -0
  396. package/tsup.config.ts +11 -0
  397. package/vitest.config.ts +12 -0
@@ -0,0 +1,8 @@
1
+ // Main entry point - exports runtime components
2
+
3
+ export * from './values';
4
+ export * from './context';
5
+ export * from './expressions';
6
+ export * from './tables';
7
+ export * from './memory';
8
+ export * from './strings';
@@ -0,0 +1,607 @@
1
+ // assembly/runtime/memory.ts (AssemblyScript)
2
+ // Memory layout and management utilities for WASM linear memory
3
+
4
+ import { writeString } from './strings';
5
+
6
+ // ============================================================================
7
+ // Memory Layout Constants
8
+ // ============================================================================
9
+
10
+ // Reserved space for null pointer trap (first 4 bytes)
11
+ // Accessing address 0 should trap, catching null/nullish pointer bugs
12
+ export const NULL_POINTER_TRAP_SIZE: usize = 4;
13
+
14
+ // Fixed memory region offsets
15
+ export const INPUT_BUFFER_START: usize = NULL_POINTER_TRAP_SIZE; // 0x0004
16
+
17
+ // Schema-specific field layouts require different buffer sizes. Computing sizes at
18
+ // compile time based on actual schema ensures optimal memory usage per decision.
19
+ export let INPUT_BUFFER_SIZE: usize = 0;
20
+
21
+ // Variable-length input data starts after the fixed input buffer
22
+ export let INPUT_VAR_START: usize = 0;
23
+
24
+ // Output buffer follows input variable data
25
+ export let OUTPUT_START: usize = 0;
26
+
27
+ // Computed at compilation time based on input/output schema field sizes
28
+ export let OUTPUT_BUFFER_SIZE: usize = 0;
29
+
30
+ // Variable-length output data starts after the fixed output buffer
31
+ export let OUTPUT_VAR_START: usize = 0;
32
+
33
+ // Runtime heap for intermediate values (temporary arrays, string concat buffers, etc.)
34
+ export let HEAP_START: usize = 0;
35
+
36
+ // ============================================================================
37
+ // Header Constants
38
+ // ============================================================================
39
+
40
+ // Schema hash size in bytes
41
+ export const SCHEMA_HASH_SIZE: usize = 8;
42
+
43
+ // String descriptor format: [offset (4 bytes)] [length (4 bytes)]
44
+ export const STRING_DESC_SIZE: usize = 8;
45
+
46
+ // Array descriptor format: [offset (4 bytes)] [length (4 bytes)]
47
+ export const ARRAY_DESC_SIZE: usize = 8;
48
+
49
+ // ============================================================================
50
+ // Memory Allocation
51
+ // ============================================================================
52
+
53
+ // Decision evaluation is single-pass (no GC needed), and most allocations are temporary
54
+ // (freed after result is marshaled). Linear bump allocator is fast and simple.
55
+ let heapPointer: usize = 0;
56
+
57
+ /**
58
+ * Initialize memory layout.
59
+ *
60
+ * This function must be called once at startup with the computed sizes
61
+ * for the input and output buffers based on the schema.
62
+ *
63
+ * @param inputFixedSize - Size of fixed input fields in bytes
64
+ * @param outputFixedSize - Size of fixed output fields in bytes
65
+ */
66
+ export function initializeMemoryLayout(inputFixedSize: usize, outputFixedSize: usize): void {
67
+ INPUT_BUFFER_SIZE = inputFixedSize;
68
+ INPUT_VAR_START = INPUT_BUFFER_START + inputFixedSize;
69
+
70
+ OUTPUT_BUFFER_SIZE = outputFixedSize;
71
+ OUTPUT_START = INPUT_VAR_START; // Output follows input variable data
72
+ OUTPUT_VAR_START = OUTPUT_START + outputFixedSize;
73
+
74
+ HEAP_START = OUTPUT_VAR_START; // Heap follows output variable data
75
+ heapPointer = HEAP_START; // Reset heap pointer
76
+ }
77
+
78
+ /**
79
+ * Get the current memory layout info.
80
+ *
81
+ * @returns Object with all memory region offsets and sizes
82
+ */
83
+ export function getMemoryLayout(): MemoryLayout {
84
+ return new MemoryLayout(
85
+ NULL_POINTER_TRAP_SIZE,
86
+ INPUT_BUFFER_START,
87
+ INPUT_BUFFER_SIZE,
88
+ INPUT_VAR_START,
89
+ OUTPUT_START,
90
+ OUTPUT_BUFFER_SIZE,
91
+ OUTPUT_VAR_START,
92
+ HEAP_START,
93
+ );
94
+ }
95
+
96
+ /**
97
+ * Memory layout information structure.
98
+ */
99
+ export class MemoryLayout {
100
+ nullPointerTrapSize: usize;
101
+ inputBufferStart: usize;
102
+ inputBufferSize: usize;
103
+ inputVarStart: usize;
104
+ outputStart: usize;
105
+ outputBufferSize: usize;
106
+ outputVarStart: usize;
107
+ heapStart: usize;
108
+
109
+ constructor(
110
+ nullPointerTrapSize: usize,
111
+ inputBufferStart: usize,
112
+ inputBufferSize: usize,
113
+ inputVarStart: usize,
114
+ outputStart: usize,
115
+ outputBufferSize: usize,
116
+ outputVarStart: usize,
117
+ heapStart: usize,
118
+ ) {
119
+ this.nullPointerTrapSize = nullPointerTrapSize;
120
+ this.inputBufferStart = inputBufferStart;
121
+ this.inputBufferSize = inputBufferSize;
122
+ this.inputVarStart = inputVarStart;
123
+ this.outputStart = outputStart;
124
+ this.outputBufferSize = outputBufferSize;
125
+ this.outputVarStart = outputVarStart;
126
+ this.heapStart = heapStart;
127
+ }
128
+ }
129
+
130
+ // ============================================================================
131
+ // Simple Linear Heap Allocator
132
+ // ============================================================================
133
+
134
+ /**
135
+ * Allocate memory from the runtime heap.
136
+ *
137
+ * A simple linear allocator - memory is never freed during a single evaluation.
138
+ * Suitable for short-lived operations like decision table evaluation.
139
+ *
140
+ * Memory is never freed during a single evaluation because:
141
+ * (1) evaluations are short-lived (milliseconds),
142
+ * (2) heap is reset between evaluations, and
143
+ * (3) avoiding free list management simplifies allocator and improves performance.
144
+ *
145
+ * @param size - Number of bytes to allocate (must be > 0)
146
+ * @returns Pointer to allocated memory
147
+ * @throws Calls abort() on OUT_OF_MEMORY - this is a critical error that halts execution
148
+ */
149
+ export function heapAlloc(size: usize): usize {
150
+ if (size == 0) return 0;
151
+
152
+ // Get current memory size in bytes (1 page = 64KB = 65536 bytes)
153
+ const memorySize: usize = (<usize>memory.size()) << 16;
154
+
155
+ // Align the heap pointer to 8-byte boundary
156
+ const alignedPtr = alignPtr(heapPointer, 8);
157
+
158
+ // Check for overflow when computing aligned pointer
159
+ // If alignedPtr wrapped around (became smaller than heapPointer), we've overflowed
160
+ if (alignedPtr < heapPointer) {
161
+ SetLastError(RuntimeErrorCode.OUT_OF_MEMORY);
162
+ return 0;
163
+ }
164
+
165
+ // Check for overflow when computing new heap pointer
166
+ // If newHeapPointer would wrap around, we've overflowed
167
+ const maxAllowedSize = memorySize - alignedPtr;
168
+ if (size > maxAllowedSize) {
169
+ SetLastError(RuntimeErrorCode.OUT_OF_MEMORY);
170
+ return 0;
171
+ }
172
+
173
+ const newHeapPointer = alignedPtr + size;
174
+
175
+ // Bounds check: ensure new heap pointer doesn't exceed memory
176
+ if (newHeapPointer > memorySize) {
177
+ SetLastError(RuntimeErrorCode.OUT_OF_MEMORY);
178
+ return 0;
179
+ }
180
+
181
+ // All checks passed, update heap pointer and return allocation
182
+ heapPointer = newHeapPointer;
183
+ return alignedPtr;
184
+ }
185
+
186
+ /**
187
+ * Get the current heap pointer (next allocation will start here).
188
+ *
189
+ * @returns Current heap pointer address
190
+ */
191
+ export function getHeapPointer(): usize {
192
+ return heapPointer;
193
+ }
194
+
195
+ /**
196
+ * Reset the heap pointer to the start of the heap region.
197
+ *
198
+ * This allows reusing heap memory for multiple evaluations.
199
+ */
200
+ export function resetHeap(): void {
201
+ heapPointer = HEAP_START;
202
+ }
203
+
204
+ /**
205
+ * Save the current heap pointer state.
206
+ *
207
+ * @returns Current heap pointer address
208
+ */
209
+ export function saveHeapPointer(): usize {
210
+ return heapPointer;
211
+ }
212
+
213
+ /**
214
+ * Restore the heap pointer to a previously saved state.
215
+ *
216
+ * @param saved - Saved heap pointer address from saveHeapPointer()
217
+ */
218
+ export function restoreHeapPointer(saved: usize): usize {
219
+ const old = heapPointer;
220
+ heapPointer = saved;
221
+ return old;
222
+ }
223
+
224
+ // ============================================================================
225
+ // Pointer Utilities
226
+ // ============================================================================
227
+
228
+ /**
229
+ * Align a pointer to the specified alignment boundary.
230
+ *
231
+ * @param ptr - Pointer to align
232
+ * @param alignment - Alignment boundary (must be a power of 2)
233
+ * @returns Aligned pointer
234
+ */
235
+ export function alignPtr(ptr: usize, alignment: usize): usize {
236
+ return (ptr + alignment - 1) & ~(alignment - 1);
237
+ }
238
+
239
+ /**
240
+ * Check if a pointer is null (0).
241
+ *
242
+ * @param ptr - Pointer to check
243
+ * @returns true if pointer is 0, false otherwise
244
+ */
245
+ export function isNullPtr(ptr: usize): bool {
246
+ return ptr == 0;
247
+ }
248
+
249
+ /**
250
+ * Check if a pointer is valid (non-null and aligned).
251
+ *
252
+ * @param ptr - Pointer to check
253
+ * @param alignment - Required alignment (default: 4 bytes)
254
+ * @returns true if pointer is valid, false otherwise
255
+ */
256
+ export function isValidPtr(ptr: usize, alignment: usize = 4): bool {
257
+ if (ptr == 0) return false;
258
+ return (ptr & (alignment - 1)) == 0;
259
+ }
260
+
261
+ // ============================================================================
262
+ // Buffer Access Helpers
263
+ // ============================================================================
264
+
265
+ /**
266
+ * Get the pointer to a schema hash field.
267
+ *
268
+ * @param basePtr - Base pointer of the buffer
269
+ * @returns Pointer to the schema hash field
270
+ */
271
+ export function getSchemaHashPtr(basePtr: usize): usize {
272
+ return basePtr;
273
+ }
274
+
275
+ /**
276
+ * Read a schema hash from a buffer.
277
+ *
278
+ * @param basePtr - Base pointer of the buffer
279
+ * @returns Schema hash as u64
280
+ */
281
+ export function readSchemaHash(basePtr: usize): u64 {
282
+ return load<u64>(basePtr);
283
+ }
284
+
285
+ /**
286
+ * Write a schema hash to a buffer.
287
+ *
288
+ * @param basePtr - Base pointer of the buffer
289
+ * @param hash - Schema hash to write
290
+ */
291
+ export function writeSchemaHash(basePtr: usize, hash: u64): void {
292
+ store<u64>(basePtr, hash);
293
+ }
294
+
295
+ /**
296
+ * Get the pointer to a field in a fixed-size buffer.
297
+ *
298
+ * @param bufferPtr - Base pointer of the buffer
299
+ * @param fieldOffset - Offset of the field from the buffer start
300
+ * @returns Pointer to the field
301
+ */
302
+ export function getFieldPtr(bufferPtr: usize, fieldOffset: usize): usize {
303
+ return bufferPtr + fieldOffset;
304
+ }
305
+
306
+ // ============================================================================
307
+ // String Descriptor Helpers
308
+ // ============================================================================
309
+
310
+ /**
311
+ * Get the offset and length components of a string descriptor.
312
+ *
313
+ * String descriptor format: [offset (4 bytes)] [length (4 bytes)]
314
+ *
315
+ * @param descPtr - Pointer to the string descriptor
316
+ * @returns Object with offset and length
317
+ */
318
+ export function readStringDesc(descPtr: usize): StringDesc {
319
+ return new StringDesc(load<u32>(descPtr), load<u32>(descPtr + 4));
320
+ }
321
+
322
+ /**
323
+ * Write a string descriptor.
324
+ *
325
+ * @param descPtr - Pointer to the string descriptor
326
+ * @param offset - Offset to the string data
327
+ * @param length - Length of the string data
328
+ */
329
+ export function writeStringDesc(descPtr: usize, offset: u32, length: u32): void {
330
+ store<u32>(descPtr, offset);
331
+ store<u32>(descPtr + 4, length);
332
+ }
333
+
334
+ /**
335
+ * String descriptor structure.
336
+ */
337
+ export class StringDesc {
338
+ offset: u32;
339
+ length: u32;
340
+
341
+ constructor(offset: u32, length: u32) {
342
+ this.offset = offset;
343
+ this.length = length;
344
+ }
345
+
346
+ isEmpty(): bool {
347
+ return this.length == 0;
348
+ }
349
+ }
350
+
351
+ // ============================================================================
352
+ // Array Descriptor Helpers
353
+ // ============================================================================
354
+
355
+ /**
356
+ * Get the offset and length components of an array descriptor.
357
+ *
358
+ * Array descriptor format: [offset (4 bytes)] [length (4 bytes)]
359
+ *
360
+ * @param descPtr - Pointer to the array descriptor
361
+ * @returns Object with offset and length
362
+ */
363
+ export function readArrayDesc(descPtr: usize): ArrayDesc {
364
+ return new ArrayDesc(load<u32>(descPtr), load<u32>(descPtr + 4));
365
+ }
366
+
367
+ /**
368
+ * Write an array descriptor.
369
+ *
370
+ * @param descPtr - Pointer to the array descriptor
371
+ * @param offset - Offset to the array data
372
+ * @param length - Number of elements in the array
373
+ */
374
+ export function writeArrayDesc(descPtr: usize, offset: u32, length: u32): void {
375
+ store<u32>(descPtr, offset);
376
+ store<u32>(descPtr + 4, length);
377
+ }
378
+
379
+ /**
380
+ * Array descriptor structure.
381
+ */
382
+ export class ArrayDesc {
383
+ offset: u32;
384
+ length: u32;
385
+
386
+ constructor(offset: u32, length: u32) {
387
+ this.offset = offset;
388
+ this.length = length;
389
+ }
390
+
391
+ isEmpty(): bool {
392
+ return this.length == 0;
393
+ }
394
+ }
395
+
396
+ // ============================================================================
397
+ // Runtime Error Handling
398
+ // ============================================================================
399
+
400
+ // Error codes for runtime failures
401
+ export enum RuntimeErrorCode {
402
+ NONE = 0,
403
+ SCHEMA_MISMATCH = 1,
404
+ NULL_POINTER = 2,
405
+ INVALID_ACCESS = 3,
406
+ EVALUATION_ERROR = 4,
407
+ OUT_OF_MEMORY = 5,
408
+ }
409
+
410
+ // Last error that occurred during evaluation
411
+ let lastErrorCode: RuntimeErrorCode = RuntimeErrorCode.NONE;
412
+
413
+ // Optional error message (allocated on heap for variable-length messages)
414
+ let errorMessagePtr: usize = 0;
415
+
416
+ /**
417
+ * Check if an error code represents a critical error that should abort execution.
418
+ *
419
+ * Critical errors are unrecoverable conditions where continuing execution would
420
+ * likely lead to memory corruption, crashes, or incorrect results. These errors
421
+ * call abort() to immediately halt the WASM module.
422
+ *
423
+ * Critical errors:
424
+ * - OUT_OF_MEMORY: Memory allocation failed; continuing could corrupt state
425
+ * - NULL_POINTER: Null pointer dereference; memory safety violation
426
+ * - INVALID_ACCESS: Invalid memory access; memory safety violation
427
+ *
428
+ * Non-critical errors (set flag, caller must check hasError()):
429
+ * - SCHEMA_MISMATCH: Input doesn't match schema; recoverable
430
+ * - EVALUATION_ERROR: Logic error in decision evaluation; recoverable
431
+ *
432
+ * @param code - The error code to check
433
+ * @returns true if the error is critical and should abort
434
+ */
435
+ export function isCriticalError(code: RuntimeErrorCode): bool {
436
+ return (
437
+ code === RuntimeErrorCode.OUT_OF_MEMORY ||
438
+ code === RuntimeErrorCode.NULL_POINTER ||
439
+ code === RuntimeErrorCode.INVALID_ACCESS
440
+ );
441
+ }
442
+
443
+ /**
444
+ * Get the last error code that occurred during evaluation.
445
+ *
446
+ * @returns The last error code
447
+ */
448
+ export function getLastError(): RuntimeErrorCode {
449
+ return lastErrorCode;
450
+ }
451
+
452
+ /**
453
+ * Set the last error code and optional message.
454
+ *
455
+ * For critical errors (OUT_OF_MEMORY, NULL_POINTER, INVALID_ACCESS), this function
456
+ * calls abort() to immediately halt execution. Critical errors indicate unrecoverable
457
+ * conditions where continuing would lead to memory corruption or undefined behavior.
458
+ *
459
+ * For non-critical errors (SCHEMA_MISMATCH, EVALUATION_ERROR), callers MUST check
460
+ * hasError() after operations that can fail and handle the error appropriately.
461
+ * Failure to check error state may result in incorrect evaluation results.
462
+ *
463
+ * @param code - The error code
464
+ * @param message - Optional error message (allocated on heap)
465
+ */
466
+ export function SetLastError(code: RuntimeErrorCode, message: usize = 0): void {
467
+ lastErrorCode = code;
468
+ errorMessagePtr = message;
469
+
470
+ // Critical errors abort immediately to prevent corrupted state propagation
471
+ if (isCriticalError(code)) {
472
+ abort();
473
+ }
474
+ }
475
+
476
+ /**
477
+ * Clear the last error (reset to NONE).
478
+ */
479
+ export function clearLastError(): void {
480
+ lastErrorCode = RuntimeErrorCode.NONE;
481
+ errorMessagePtr = 0;
482
+ }
483
+
484
+ /**
485
+ * Set an error with a string message.
486
+ *
487
+ * This is a convenience function that combines SetLastError() with writeString().
488
+ * The error message is allocated on the heap and will be freed by the next error.
489
+ *
490
+ * For critical errors (OUT_OF_MEMORY, NULL_POINTER, INVALID_ACCESS), this function
491
+ * will call abort() after setting the error, immediately halting execution.
492
+ *
493
+ * For non-critical errors (SCHEMA_MISMATCH, EVALUATION_ERROR), callers MUST check
494
+ * hasError() after operations that can fail.
495
+ *
496
+ * @param code - The error code
497
+ * @param message - The error message string
498
+ */
499
+ export function SetLastErrorWithMessage(code: RuntimeErrorCode, message: string): void {
500
+ const messagePtr = writeString(message);
501
+ SetLastError(code, messagePtr);
502
+ }
503
+
504
+ /**
505
+ * Get the pointer to the error message.
506
+ *
507
+ * @returns Pointer to error message (0 if no message)
508
+ */
509
+ export function getErrorMessagePtr(): usize {
510
+ return errorMessagePtr;
511
+ }
512
+
513
+ /**
514
+ * Check if an error occurred.
515
+ *
516
+ * Callers MUST check this function after operations that can set non-critical errors
517
+ * (SCHEMA_MISMATCH, EVALUATION_ERROR). Critical errors (OUT_OF_MEMORY, NULL_POINTER,
518
+ * INVALID_ACCESS) abort immediately and never reach this check.
519
+ *
520
+ * Example usage:
521
+ * ```assemblyscript
522
+ * let result = someOperationThatCanFail();
523
+ * if (hasError()) {
524
+ * // Handle error - return early, propagate error, etc.
525
+ * return Value.Null();
526
+ * }
527
+ * // Continue with valid result
528
+ * ```
529
+ *
530
+ * @returns true if a non-critical error occurred, false otherwise
531
+ */
532
+ export function hasError(): bool {
533
+ return lastErrorCode !== RuntimeErrorCode.NONE;
534
+ }
535
+
536
+ // ============================================================================
537
+ // Memory Validation Helpers
538
+ // ============================================================================
539
+
540
+ /**
541
+ * Check if a pointer is within the valid memory range.
542
+ *
543
+ * This is a basic check that can help catch out-of-bounds accesses.
544
+ * Note: In production, you may want to disable these checks for performance.
545
+ *
546
+ * @param ptr - Pointer to check
547
+ * @param size - Size of the region to check
548
+ * @return true if pointer is within valid range, false otherwise
549
+ */
550
+ export function isValidMemoryRange(ptr: usize, size: usize): bool {
551
+ if (ptr == 0) return false;
552
+
553
+ // Get current memory size (AssemblyScript runtime provides this)
554
+ const memorySize: usize = memory.size() << 16; // 1 page = 64KB
555
+
556
+ return ptr + size <= memorySize;
557
+ }
558
+
559
+ /**
560
+ * Check if a pointer is within the input buffer region.
561
+ *
562
+ * @param ptr - Pointer to check
563
+ * @return true if pointer is within input buffer, false otherwise
564
+ */
565
+ export function isWithinInputBuffer(ptr: usize): bool {
566
+ return ptr >= INPUT_BUFFER_START && ptr < INPUT_VAR_START;
567
+ }
568
+
569
+ /**
570
+ * Check if a pointer is within the input variable data region.
571
+ *
572
+ * @param ptr - Pointer to check
573
+ * @return true if pointer is within input variable data, false otherwise
574
+ */
575
+ export function isWithinInputVar(ptr: usize): bool {
576
+ return ptr >= INPUT_VAR_START && ptr < OUTPUT_START;
577
+ }
578
+
579
+ /**
580
+ * Check if a pointer is within the output buffer region.
581
+ *
582
+ * @param ptr - Pointer to check
583
+ * @return true if pointer is within output buffer, false otherwise
584
+ */
585
+ export function isWithinOutputBuffer(ptr: usize): bool {
586
+ return ptr >= OUTPUT_START && ptr < OUTPUT_VAR_START;
587
+ }
588
+
589
+ /**
590
+ * Check if a pointer is within the output variable data region.
591
+ *
592
+ * @param ptr - Pointer to check
593
+ * @return true if pointer is within output variable data, false otherwise
594
+ */
595
+ export function isWithinOutputVar(ptr: usize): bool {
596
+ return ptr >= OUTPUT_VAR_START && ptr < HEAP_START;
597
+ }
598
+
599
+ /**
600
+ * Check if a pointer is within the heap region.
601
+ *
602
+ * @param ptr - Pointer to check
603
+ * @return true if pointer is within heap, false otherwise
604
+ */
605
+ export function isWithinHeap(ptr: usize): bool {
606
+ return ptr >= HEAP_START;
607
+ }