funkscript 0.0.0-stage → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +204 -2
- package/dist/Widget.tsx +9 -0
- package/dist/ast/types.d.ts +513 -0
- package/dist/ast/types.d.ts.map +1 -0
- package/dist/ast/types.js +5 -0
- package/dist/ast/types.js.map +1 -0
- package/dist/ast/walk.d.ts +8 -0
- package/dist/ast/walk.d.ts.map +1 -0
- package/dist/ast/walk.js +23 -0
- package/dist/ast/walk.js.map +1 -0
- package/dist/cli/build-bun.d.ts +24 -0
- package/dist/cli/build-bun.d.ts.map +1 -0
- package/dist/cli/build-bun.js +190 -0
- package/dist/cli/build-bun.js.map +1 -0
- package/dist/cli/build-watch.d.ts +21 -0
- package/dist/cli/build-watch.d.ts.map +1 -0
- package/dist/cli/build-watch.js +244 -0
- package/dist/cli/build-watch.js.map +1 -0
- package/dist/cli/build.d.ts +4 -0
- package/dist/cli/build.d.ts.map +1 -0
- package/dist/cli/build.js +338 -0
- package/dist/cli/build.js.map +1 -0
- package/dist/cli/check.d.ts +4 -0
- package/dist/cli/check.d.ts.map +1 -0
- package/dist/cli/check.js +278 -0
- package/dist/cli/check.js.map +1 -0
- package/dist/cli/commands.d.ts +13 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +262 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/doc.d.ts +3 -0
- package/dist/cli/doc.d.ts.map +1 -0
- package/dist/cli/doc.js +71 -0
- package/dist/cli/doc.js.map +1 -0
- package/dist/cli/doctor.d.ts +3 -0
- package/dist/cli/doctor.d.ts.map +1 -0
- package/dist/cli/doctor.js +121 -0
- package/dist/cli/doctor.js.map +1 -0
- package/dist/cli/emit.d.ts +4 -0
- package/dist/cli/emit.d.ts.map +1 -0
- package/dist/cli/emit.js +87 -0
- package/dist/cli/emit.js.map +1 -0
- package/dist/cli/explain.d.ts +3 -0
- package/dist/cli/explain.d.ts.map +1 -0
- package/dist/cli/explain.js +103 -0
- package/dist/cli/explain.js.map +1 -0
- package/dist/cli/fmt.d.ts +3 -0
- package/dist/cli/fmt.d.ts.map +1 -0
- package/dist/cli/fmt.js +146 -0
- package/dist/cli/fmt.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +186 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/init.d.ts +10 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +412 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/io.d.ts +9 -0
- package/dist/cli/io.d.ts.map +1 -0
- package/dist/cli/io.js +113 -0
- package/dist/cli/io.js.map +1 -0
- package/dist/cli/output-collisions.d.ts +2 -0
- package/dist/cli/output-collisions.d.ts.map +1 -0
- package/dist/cli/output-collisions.js +39 -0
- package/dist/cli/output-collisions.js.map +1 -0
- package/dist/cli/pipeline-style.d.ts +3 -0
- package/dist/cli/pipeline-style.d.ts.map +1 -0
- package/dist/cli/pipeline-style.js +45 -0
- package/dist/cli/pipeline-style.js.map +1 -0
- package/dist/cli/repl.d.ts +37 -0
- package/dist/cli/repl.d.ts.map +1 -0
- package/dist/cli/repl.js +334 -0
- package/dist/cli/repl.js.map +1 -0
- package/dist/cli/run.d.ts +4 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +185 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/cli/test.d.ts +3 -0
- package/dist/cli/test.d.ts.map +1 -0
- package/dist/cli/test.js +124 -0
- package/dist/cli/test.js.map +1 -0
- package/dist/cli/tsc.d.ts +3 -0
- package/dist/cli/tsc.d.ts.map +1 -0
- package/dist/cli/tsc.js +143 -0
- package/dist/cli/tsc.js.map +1 -0
- package/dist/cli/typecheck.d.ts +23 -0
- package/dist/cli/typecheck.d.ts.map +1 -0
- package/dist/cli/typecheck.js +65 -0
- package/dist/cli/typecheck.js.map +1 -0
- package/dist/compiler/ast-inspection.d.ts +18 -0
- package/dist/compiler/ast-inspection.d.ts.map +1 -0
- package/dist/compiler/ast-inspection.js +96 -0
- package/dist/compiler/ast-inspection.js.map +1 -0
- package/dist/compiler/boolean-operators.d.ts +8 -0
- package/dist/compiler/boolean-operators.d.ts.map +1 -0
- package/dist/compiler/boolean-operators.js +84 -0
- package/dist/compiler/boolean-operators.js.map +1 -0
- package/dist/compiler/check.d.ts +87 -0
- package/dist/compiler/check.d.ts.map +1 -0
- package/dist/compiler/check.js +1830 -0
- package/dist/compiler/check.js.map +1 -0
- package/dist/compiler/diagnostics.d.ts +6 -0
- package/dist/compiler/diagnostics.d.ts.map +1 -0
- package/dist/compiler/diagnostics.js +136 -0
- package/dist/compiler/diagnostics.js.map +1 -0
- package/dist/compiler/effect-mutation-rule.d.ts +4 -0
- package/dist/compiler/effect-mutation-rule.d.ts.map +1 -0
- package/dist/compiler/effect-mutation-rule.js +348 -0
- package/dist/compiler/effect-mutation-rule.js.map +1 -0
- package/dist/compiler/entry-rules.d.ts +5 -0
- package/dist/compiler/entry-rules.d.ts.map +1 -0
- package/dist/compiler/entry-rules.js +89 -0
- package/dist/compiler/entry-rules.js.map +1 -0
- package/dist/compiler/imported-record-functions.d.ts +4 -0
- package/dist/compiler/imported-record-functions.d.ts.map +1 -0
- package/dist/compiler/imported-record-functions.js +706 -0
- package/dist/compiler/imported-record-functions.js.map +1 -0
- package/dist/compiler/imported-schemas.d.ts +21 -0
- package/dist/compiler/imported-schemas.d.ts.map +1 -0
- package/dist/compiler/imported-schemas.js +461 -0
- package/dist/compiler/imported-schemas.js.map +1 -0
- package/dist/compiler/incremental-build.d.ts +23 -0
- package/dist/compiler/incremental-build.d.ts.map +1 -0
- package/dist/compiler/incremental-build.js +90 -0
- package/dist/compiler/incremental-build.js.map +1 -0
- package/dist/compiler/incremental-dependencies.d.ts +7 -0
- package/dist/compiler/incremental-dependencies.d.ts.map +1 -0
- package/dist/compiler/incremental-dependencies.js +190 -0
- package/dist/compiler/incremental-dependencies.js.map +1 -0
- package/dist/compiler/js-interop-nullability.d.ts +9 -0
- package/dist/compiler/js-interop-nullability.d.ts.map +1 -0
- package/dist/compiler/js-interop-nullability.js +57 -0
- package/dist/compiler/js-interop-nullability.js.map +1 -0
- package/dist/compiler/module-resolution.d.ts +6 -0
- package/dist/compiler/module-resolution.d.ts.map +1 -0
- package/dist/compiler/module-resolution.js +44 -0
- package/dist/compiler/module-resolution.js.map +1 -0
- package/dist/compiler/modules/output-collisions.d.ts +2 -0
- package/dist/compiler/modules/output-collisions.d.ts.map +1 -0
- package/dist/compiler/modules/output-collisions.js +39 -0
- package/dist/compiler/modules/output-collisions.js.map +1 -0
- package/dist/compiler/modules/output-plan.d.ts +7 -0
- package/dist/compiler/modules/output-plan.d.ts.map +1 -0
- package/dist/compiler/modules/output-plan.js +37 -0
- package/dist/compiler/modules/output-plan.js.map +1 -0
- package/dist/compiler/modules/resolution.d.ts +8 -0
- package/dist/compiler/modules/resolution.d.ts.map +1 -0
- package/dist/compiler/modules/resolution.js +48 -0
- package/dist/compiler/modules/resolution.js.map +1 -0
- package/dist/compiler/numeric-comparisons.d.ts +9 -0
- package/dist/compiler/numeric-comparisons.d.ts.map +1 -0
- package/dist/compiler/numeric-comparisons.js +153 -0
- package/dist/compiler/numeric-comparisons.js.map +1 -0
- package/dist/compiler/optional-arguments.d.ts +11 -0
- package/dist/compiler/optional-arguments.d.ts.map +1 -0
- package/dist/compiler/optional-arguments.js +246 -0
- package/dist/compiler/optional-arguments.js.map +1 -0
- package/dist/compiler/package-fingerprint.d.ts +7 -0
- package/dist/compiler/package-fingerprint.d.ts.map +1 -0
- package/dist/compiler/package-fingerprint.js +178 -0
- package/dist/compiler/package-fingerprint.js.map +1 -0
- package/dist/compiler/package-record-contracts.d.ts +5 -0
- package/dist/compiler/package-record-contracts.d.ts.map +1 -0
- package/dist/compiler/package-record-contracts.js +126 -0
- package/dist/compiler/package-record-contracts.js.map +1 -0
- package/dist/compiler/pipeline-companions.d.ts +18 -0
- package/dist/compiler/pipeline-companions.d.ts.map +1 -0
- package/dist/compiler/pipeline-companions.js +1042 -0
- package/dist/compiler/pipeline-companions.js.map +1 -0
- package/dist/compiler/pipelines/catalog.d.ts +15 -0
- package/dist/compiler/pipelines/catalog.d.ts.map +1 -0
- package/dist/compiler/pipelines/catalog.js +55 -0
- package/dist/compiler/pipelines/catalog.js.map +1 -0
- package/dist/compiler/pipelines/companions.d.ts +18 -0
- package/dist/compiler/pipelines/companions.d.ts.map +1 -0
- package/dist/compiler/pipelines/companions.js +1042 -0
- package/dist/compiler/pipelines/companions.js.map +1 -0
- package/dist/compiler/pipelines/resolution.d.ts +38 -0
- package/dist/compiler/pipelines/resolution.d.ts.map +1 -0
- package/dist/compiler/pipelines/resolution.js +571 -0
- package/dist/compiler/pipelines/resolution.js.map +1 -0
- package/dist/compiler/project-config.d.ts +28 -0
- package/dist/compiler/project-config.d.ts.map +1 -0
- package/dist/compiler/project-config.js +155 -0
- package/dist/compiler/project-config.js.map +1 -0
- package/dist/compiler/providers.d.ts +4 -0
- package/dist/compiler/providers.d.ts.map +1 -0
- package/dist/compiler/providers.js +101 -0
- package/dist/compiler/providers.js.map +1 -0
- package/dist/compiler/record-contracts.d.ts +6 -0
- package/dist/compiler/record-contracts.d.ts.map +1 -0
- package/dist/compiler/record-contracts.js +41 -0
- package/dist/compiler/record-contracts.js.map +1 -0
- package/dist/compiler/rules/control-flow.d.ts +10 -0
- package/dist/compiler/rules/control-flow.d.ts.map +1 -0
- package/dist/compiler/rules/control-flow.js +19 -0
- package/dist/compiler/rules/control-flow.js.map +1 -0
- package/dist/compiler/rules/diagnostic.d.ts +4 -0
- package/dist/compiler/rules/diagnostic.d.ts.map +1 -0
- package/dist/compiler/rules/diagnostic.js +2 -0
- package/dist/compiler/rules/diagnostic.js.map +1 -0
- package/dist/compiler/rules/module-scope-refs.d.ts +5 -0
- package/dist/compiler/rules/module-scope-refs.d.ts.map +1 -0
- package/dist/compiler/rules/module-scope-refs.js +47 -0
- package/dist/compiler/rules/module-scope-refs.js.map +1 -0
- package/dist/compiler/schemas/analysis.d.ts +13 -0
- package/dist/compiler/schemas/analysis.d.ts.map +1 -0
- package/dist/compiler/schemas/analysis.js +24 -0
- package/dist/compiler/schemas/analysis.js.map +1 -0
- package/dist/compiler/schemas/imported-record-functions.d.ts +4 -0
- package/dist/compiler/schemas/imported-record-functions.d.ts.map +1 -0
- package/dist/compiler/schemas/imported-record-functions.js +706 -0
- package/dist/compiler/schemas/imported-record-functions.js.map +1 -0
- package/dist/compiler/schemas/imported-schemas.d.ts +21 -0
- package/dist/compiler/schemas/imported-schemas.d.ts.map +1 -0
- package/dist/compiler/schemas/imported-schemas.js +461 -0
- package/dist/compiler/schemas/imported-schemas.js.map +1 -0
- package/dist/compiler/source-map.d.ts +6 -0
- package/dist/compiler/source-map.d.ts.map +1 -0
- package/dist/compiler/source-map.js +127 -0
- package/dist/compiler/source-map.js.map +1 -0
- package/dist/compiler/typescript-context.d.ts +33 -0
- package/dist/compiler/typescript-context.d.ts.map +1 -0
- package/dist/compiler/typescript-context.js +200 -0
- package/dist/compiler/typescript-context.js.map +1 -0
- package/dist/compiler.d.ts +77 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +497 -0
- package/dist/compiler.js.map +1 -0
- package/dist/emitter/assemblyscript.d.ts +5 -0
- package/dist/emitter/assemblyscript.d.ts.map +1 -0
- package/dist/emitter/assemblyscript.js +979 -0
- package/dist/emitter/assemblyscript.js.map +1 -0
- package/dist/emitter/declarations.d.ts +46 -0
- package/dist/emitter/declarations.d.ts.map +1 -0
- package/dist/emitter/declarations.js +739 -0
- package/dist/emitter/declarations.js.map +1 -0
- package/dist/emitter/emitter.d.ts +165 -0
- package/dist/emitter/emitter.d.ts.map +1 -0
- package/dist/emitter/emitter.js +2448 -0
- package/dist/emitter/emitter.js.map +1 -0
- package/dist/emitter/imports.d.ts +6 -0
- package/dist/emitter/imports.d.ts.map +1 -0
- package/dist/emitter/imports.js +73 -0
- package/dist/emitter/imports.js.map +1 -0
- package/dist/emitter/indent.d.ts +3 -0
- package/dist/emitter/indent.d.ts.map +1 -0
- package/dist/emitter/indent.js +31 -0
- package/dist/emitter/indent.js.map +1 -0
- package/dist/emitter/jsx.d.ts +20 -0
- package/dist/emitter/jsx.d.ts.map +1 -0
- package/dist/emitter/jsx.js +82 -0
- package/dist/emitter/jsx.js.map +1 -0
- package/dist/emitter/local-bindings.d.ts +6 -0
- package/dist/emitter/local-bindings.d.ts.map +1 -0
- package/dist/emitter/local-bindings.js +32 -0
- package/dist/emitter/local-bindings.js.map +1 -0
- package/dist/emitter/match.d.ts +56 -0
- package/dist/emitter/match.d.ts.map +1 -0
- package/dist/emitter/match.js +1287 -0
- package/dist/emitter/match.js.map +1 -0
- package/dist/emitter/pipeline-catalog.d.ts +15 -0
- package/dist/emitter/pipeline-catalog.d.ts.map +1 -0
- package/dist/emitter/pipeline-catalog.js +55 -0
- package/dist/emitter/pipeline-catalog.js.map +1 -0
- package/dist/emitter/pipeline-resolution.d.ts +38 -0
- package/dist/emitter/pipeline-resolution.d.ts.map +1 -0
- package/dist/emitter/pipeline-resolution.js +571 -0
- package/dist/emitter/pipeline-resolution.js.map +1 -0
- package/dist/emitter/runtime-helpers.d.ts +10 -0
- package/dist/emitter/runtime-helpers.d.ts.map +1 -0
- package/dist/emitter/runtime-helpers.js +22 -0
- package/dist/emitter/runtime-helpers.js.map +1 -0
- package/dist/emitter/schemas.d.ts +36 -0
- package/dist/emitter/schemas.d.ts.map +1 -0
- package/dist/emitter/schemas.js +766 -0
- package/dist/emitter/schemas.js.map +1 -0
- package/dist/emitter/statements.d.ts +23 -0
- package/dist/emitter/statements.d.ts.map +1 -0
- package/dist/emitter/statements.js +216 -0
- package/dist/emitter/statements.js.map +1 -0
- package/dist/emitter/tagged-match.d.ts +23 -0
- package/dist/emitter/tagged-match.d.ts.map +1 -0
- package/dist/emitter/tagged-match.js +109 -0
- package/dist/emitter/tagged-match.js.map +1 -0
- package/dist/emitter/types.d.ts +22 -0
- package/dist/emitter/types.d.ts.map +1 -0
- package/dist/emitter/types.js +231 -0
- package/dist/emitter/types.js.map +1 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +98 -0
- package/dist/errors.js.map +1 -0
- package/dist/formatter/comments.d.ts +25 -0
- package/dist/formatter/comments.d.ts.map +1 -0
- package/dist/formatter/comments.js +283 -0
- package/dist/formatter/comments.js.map +1 -0
- package/dist/formatter/index.d.ts +71 -0
- package/dist/formatter/index.d.ts.map +1 -0
- package/dist/formatter/index.js +1113 -0
- package/dist/formatter/index.js.map +1 -0
- package/dist/formatter/operator-precedence.d.ts +3 -0
- package/dist/formatter/operator-precedence.d.ts.map +1 -0
- package/dist/formatter/operator-precedence.js +27 -0
- package/dist/formatter/operator-precedence.js.map +1 -0
- package/dist/formatter/prettier.d.ts +7 -0
- package/dist/formatter/prettier.d.ts.map +1 -0
- package/dist/formatter/prettier.js +51 -0
- package/dist/formatter/prettier.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/interop/callback.d.ts +38 -0
- package/dist/interop/callback.d.ts.map +1 -0
- package/dist/interop/callback.js +130 -0
- package/dist/interop/callback.js.map +1 -0
- package/dist/interop/index.d.ts +6 -0
- package/dist/interop/index.d.ts.map +1 -0
- package/dist/interop/index.js +6 -0
- package/dist/interop/index.js.map +1 -0
- package/dist/interop/js.d.ts +16 -0
- package/dist/interop/js.d.ts.map +1 -0
- package/dist/interop/js.js +35 -0
- package/dist/interop/js.js.map +1 -0
- package/dist/interop/record.d.ts +9 -0
- package/dist/interop/record.d.ts.map +1 -0
- package/dist/interop/record.js +18 -0
- package/dist/interop/record.js.map +1 -0
- package/dist/interop/valibot.d.ts +9 -0
- package/dist/interop/valibot.d.ts.map +1 -0
- package/dist/interop/valibot.js +94 -0
- package/dist/interop/valibot.js.map +1 -0
- package/dist/interop/zod.d.ts +45 -0
- package/dist/interop/zod.d.ts.map +1 -0
- package/dist/interop/zod.js +143 -0
- package/dist/interop/zod.js.map +1 -0
- package/dist/lexer/lexer.d.ts +65 -0
- package/dist/lexer/lexer.d.ts.map +1 -0
- package/dist/lexer/lexer.js +1335 -0
- package/dist/lexer/lexer.js.map +1 -0
- package/dist/lexer/tokens.d.ts +96 -0
- package/dist/lexer/tokens.d.ts.map +1 -0
- package/dist/lexer/tokens.js +150 -0
- package/dist/lexer/tokens.js.map +1 -0
- package/dist/lsp/code-action-worker.d.ts +2 -0
- package/dist/lsp/code-action-worker.d.ts.map +1 -0
- package/dist/lsp/code-action-worker.js +17 -0
- package/dist/lsp/code-action-worker.js.map +1 -0
- package/dist/lsp/completion.d.ts +11 -0
- package/dist/lsp/completion.d.ts.map +1 -0
- package/dist/lsp/completion.js +260 -0
- package/dist/lsp/completion.js.map +1 -0
- package/dist/lsp/definition.d.ts +42 -0
- package/dist/lsp/definition.d.ts.map +1 -0
- package/dist/lsp/definition.js +1074 -0
- package/dist/lsp/definition.js.map +1 -0
- package/dist/lsp/diagnostics-worker.d.ts +2 -0
- package/dist/lsp/diagnostics-worker.d.ts.map +1 -0
- package/dist/lsp/diagnostics-worker.js +169 -0
- package/dist/lsp/diagnostics-worker.js.map +1 -0
- package/dist/lsp/docs.d.ts +37 -0
- package/dist/lsp/docs.d.ts.map +1 -0
- package/dist/lsp/docs.js +830 -0
- package/dist/lsp/docs.js.map +1 -0
- package/dist/lsp/error-channel.d.ts +3 -0
- package/dist/lsp/error-channel.d.ts.map +1 -0
- package/dist/lsp/error-channel.js +48 -0
- package/dist/lsp/error-channel.js.map +1 -0
- package/dist/lsp/hover.d.ts +5 -0
- package/dist/lsp/hover.d.ts.map +1 -0
- package/dist/lsp/hover.js +651 -0
- package/dist/lsp/hover.js.map +1 -0
- package/dist/lsp/imports.d.ts +5 -0
- package/dist/lsp/imports.d.ts.map +1 -0
- package/dist/lsp/imports.js +317 -0
- package/dist/lsp/imports.js.map +1 -0
- package/dist/lsp/index.d.ts +16 -0
- package/dist/lsp/index.d.ts.map +1 -0
- package/dist/lsp/index.js +17 -0
- package/dist/lsp/index.js.map +1 -0
- package/dist/lsp/inlay-hints.d.ts +3 -0
- package/dist/lsp/inlay-hints.d.ts.map +1 -0
- package/dist/lsp/inlay-hints.js +236 -0
- package/dist/lsp/inlay-hints.js.map +1 -0
- package/dist/lsp/interop-rename.d.ts +26 -0
- package/dist/lsp/interop-rename.d.ts.map +1 -0
- package/dist/lsp/interop-rename.js +346 -0
- package/dist/lsp/interop-rename.js.map +1 -0
- package/dist/lsp/match-arms.d.ts +3 -0
- package/dist/lsp/match-arms.d.ts.map +1 -0
- package/dist/lsp/match-arms.js +341 -0
- package/dist/lsp/match-arms.js.map +1 -0
- package/dist/lsp/navigation.d.ts +23 -0
- package/dist/lsp/navigation.d.ts.map +1 -0
- package/dist/lsp/navigation.js +430 -0
- package/dist/lsp/navigation.js.map +1 -0
- package/dist/lsp/parsed-documents.d.ts +25 -0
- package/dist/lsp/parsed-documents.d.ts.map +1 -0
- package/dist/lsp/parsed-documents.js +47 -0
- package/dist/lsp/parsed-documents.js.map +1 -0
- package/dist/lsp/rename-worker.d.ts +2 -0
- package/dist/lsp/rename-worker.d.ts.map +1 -0
- package/dist/lsp/rename-worker.js +27 -0
- package/dist/lsp/rename-worker.js.map +1 -0
- package/dist/lsp/server.d.ts +78 -0
- package/dist/lsp/server.d.ts.map +1 -0
- package/dist/lsp/server.js +1098 -0
- package/dist/lsp/server.js.map +1 -0
- package/dist/lsp/thunk-pipeline.d.ts +4 -0
- package/dist/lsp/thunk-pipeline.d.ts.map +1 -0
- package/dist/lsp/thunk-pipeline.js +95 -0
- package/dist/lsp/thunk-pipeline.js.map +1 -0
- package/dist/lsp/validation.d.ts +8 -0
- package/dist/lsp/validation.d.ts.map +1 -0
- package/dist/lsp/validation.js +124 -0
- package/dist/lsp/validation.js.map +1 -0
- package/dist/lsp/workspace.d.ts +8 -0
- package/dist/lsp/workspace.d.ts.map +1 -0
- package/dist/lsp/workspace.js +70 -0
- package/dist/lsp/workspace.js.map +1 -0
- package/dist/math.ts +1 -0
- package/dist/parser/declarations.d.ts +21 -0
- package/dist/parser/declarations.d.ts.map +1 -0
- package/dist/parser/declarations.js +626 -0
- package/dist/parser/declarations.js.map +1 -0
- package/dist/parser/errors.d.ts +9 -0
- package/dist/parser/errors.d.ts.map +1 -0
- package/dist/parser/errors.js +11 -0
- package/dist/parser/errors.js.map +1 -0
- package/dist/parser/index.d.ts +9 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +9 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/jsx.d.ts +5 -0
- package/dist/parser/jsx.d.ts.map +1 -0
- package/dist/parser/jsx.js +188 -0
- package/dist/parser/jsx.js.map +1 -0
- package/dist/parser/operator-expressions.d.ts +16 -0
- package/dist/parser/operator-expressions.d.ts.map +1 -0
- package/dist/parser/operator-expressions.js +146 -0
- package/dist/parser/operator-expressions.js.map +1 -0
- package/dist/parser/parser.d.ts +104 -0
- package/dist/parser/parser.d.ts.map +1 -0
- package/dist/parser/parser.js +1216 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/patterns.d.ts +9 -0
- package/dist/parser/patterns.d.ts.map +1 -0
- package/dist/parser/patterns.js +292 -0
- package/dist/parser/patterns.js.map +1 -0
- package/dist/parser/pipeline-expressions.d.ts +10 -0
- package/dist/parser/pipeline-expressions.d.ts.map +1 -0
- package/dist/parser/pipeline-expressions.js +57 -0
- package/dist/parser/pipeline-expressions.js.map +1 -0
- package/dist/parser/scope.d.ts +17 -0
- package/dist/parser/scope.d.ts.map +1 -0
- package/dist/parser/scope.js +56 -0
- package/dist/parser/scope.js.map +1 -0
- package/dist/parser/statements.d.ts +13 -0
- package/dist/parser/statements.d.ts.map +1 -0
- package/dist/parser/statements.js +737 -0
- package/dist/parser/statements.js.map +1 -0
- package/dist/parser/types.d.ts +13 -0
- package/dist/parser/types.d.ts.map +1 -0
- package/dist/parser/types.js +371 -0
- package/dist/parser/types.js.map +1 -0
- package/dist/plugins/bun-preload.d.ts +4 -0
- package/dist/plugins/bun-preload.d.ts.map +1 -0
- package/dist/plugins/bun-preload.js +18 -0
- package/dist/plugins/bun-preload.js.map +1 -0
- package/dist/plugins/bun.d.ts +115 -0
- package/dist/plugins/bun.d.ts.map +1 -0
- package/dist/plugins/bun.js +240 -0
- package/dist/plugins/bun.js.map +1 -0
- package/dist/plugins/esbuild.d.ts +45 -0
- package/dist/plugins/esbuild.d.ts.map +1 -0
- package/dist/plugins/esbuild.js +184 -0
- package/dist/plugins/esbuild.js.map +1 -0
- package/dist/plugins/type-view.d.ts +30 -0
- package/dist/plugins/type-view.d.ts.map +1 -0
- package/dist/plugins/type-view.js +245 -0
- package/dist/plugins/type-view.js.map +1 -0
- package/dist/plugins/vite.d.ts +56 -0
- package/dist/plugins/vite.d.ts.map +1 -0
- package/dist/plugins/vite.js +182 -0
- package/dist/plugins/vite.js.map +1 -0
- package/dist/service.ts +3 -0
- package/dist/service.ts.map +1 -0
- package/dist/testing/index.d.ts +24 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +56 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tooling/doc.d.ts +82 -0
- package/dist/tooling/doc.d.ts.map +1 -0
- package/dist/tooling/doc.js +624 -0
- package/dist/tooling/doc.js.map +1 -0
- package/dist/tooling/idiomatic.d.ts +20 -0
- package/dist/tooling/idiomatic.d.ts.map +1 -0
- package/dist/tooling/idiomatic.js +154 -0
- package/dist/tooling/idiomatic.js.map +1 -0
- package/dist/tooling/migrate.d.ts +62 -0
- package/dist/tooling/migrate.d.ts.map +1 -0
- package/dist/tooling/migrate.js +608 -0
- package/dist/tooling/migrate.js.map +1 -0
- package/dist/tooling/pipeline-style.d.ts +12 -0
- package/dist/tooling/pipeline-style.d.ts.map +1 -0
- package/dist/tooling/pipeline-style.js +208 -0
- package/dist/tooling/pipeline-style.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +25 -0
- package/dist/version.js.map +1 -0
- package/package.json +128 -4
- package/src/ast/types.ts +753 -0
- package/src/ast/walk.ts +35 -0
- package/src/cli/build-bun.ts +249 -0
- package/src/cli/build-watch.ts +299 -0
- package/src/cli/build.ts +450 -0
- package/src/cli/check.ts +342 -0
- package/src/cli/commands.ts +301 -0
- package/src/cli/doc.ts +81 -0
- package/src/cli/doctor.ts +173 -0
- package/src/cli/emit.ts +138 -0
- package/src/cli/explain.ts +127 -0
- package/src/cli/fmt.ts +170 -0
- package/src/cli/index.ts +227 -0
- package/src/cli/init.ts +491 -0
- package/src/cli/io.ts +150 -0
- package/src/cli/pipeline-style.ts +55 -0
- package/src/cli/repl.ts +443 -0
- package/src/cli/run.ts +238 -0
- package/src/cli/test.ts +148 -0
- package/src/cli/tsc.ts +207 -0
- package/src/cli/typecheck.ts +100 -0
- package/src/compiler/ast-inspection.ts +121 -0
- package/src/compiler/boolean-operators.ts +108 -0
- package/src/compiler/check.ts +2763 -0
- package/src/compiler/diagnostics.ts +179 -0
- package/src/compiler/effect-mutation-rule.ts +407 -0
- package/src/compiler/entry-rules.ts +116 -0
- package/src/compiler/incremental-build.ts +131 -0
- package/src/compiler/incremental-dependencies.ts +200 -0
- package/src/compiler/js-interop-nullability.ts +67 -0
- package/src/compiler/modules/output-collisions.ts +48 -0
- package/src/compiler/modules/output-plan.ts +46 -0
- package/src/compiler/modules/resolution.ts +56 -0
- package/src/compiler/numeric-comparisons.ts +213 -0
- package/src/compiler/optional-arguments.ts +335 -0
- package/src/compiler/package-fingerprint.ts +216 -0
- package/src/compiler/package-record-contracts.ts +156 -0
- package/src/compiler/pipelines/catalog.ts +1128 -0
- package/src/compiler/pipelines/companions.ts +1279 -0
- package/src/compiler/pipelines/resolution.ts +684 -0
- package/src/compiler/project-config.ts +221 -0
- package/src/compiler/record-contracts.ts +46 -0
- package/src/compiler/rules/control-flow.ts +57 -0
- package/src/compiler/rules/diagnostic.ts +11 -0
- package/src/compiler/rules/module-scope-refs.ts +63 -0
- package/src/compiler/schemas/analysis.ts +45 -0
- package/src/compiler/schemas/imported-record-functions.ts +984 -0
- package/src/compiler/schemas/imported-schemas.ts +761 -0
- package/src/compiler/source-map.ts +165 -0
- package/src/compiler/typescript-context.ts +250 -0
- package/src/compiler.ts +865 -0
- package/src/emitter/assemblyscript.ts +1220 -0
- package/src/emitter/declarations.ts +916 -0
- package/src/emitter/emitter.ts +3128 -0
- package/src/emitter/imports.ts +89 -0
- package/src/emitter/indent.ts +39 -0
- package/src/emitter/jsx.ts +122 -0
- package/src/emitter/local-bindings.ts +40 -0
- package/src/emitter/match.ts +1631 -0
- package/src/emitter/runtime-helpers.ts +31 -0
- package/src/emitter/schemas.ts +969 -0
- package/src/emitter/statements.ts +310 -0
- package/src/emitter/tagged-match.ts +171 -0
- package/src/emitter/types.ts +289 -0
- package/src/errors.ts +115 -0
- package/src/formatter/comments.ts +350 -0
- package/src/formatter/index.ts +1457 -0
- package/src/formatter/operator-precedence.ts +36 -0
- package/src/formatter/prettier.ts +61 -0
- package/src/index.ts +16 -0
- package/src/interop/callback.ts +188 -0
- package/src/interop/js.ts +66 -0
- package/src/interop/record.ts +36 -0
- package/src/interop/valibot.ts +141 -0
- package/src/interop/zod.ts +219 -0
- package/src/lexer/lexer.ts +1521 -0
- package/src/lexer/tokens.ts +174 -0
- package/src/lsp/code-action-worker.ts +31 -0
- package/src/lsp/completion.ts +343 -0
- package/src/lsp/definition.ts +1165 -0
- package/src/lsp/diagnostics-worker.ts +198 -0
- package/src/lsp/docs.ts +864 -0
- package/src/lsp/error-channel.ts +63 -0
- package/src/lsp/hover.ts +803 -0
- package/src/lsp/imports.ts +380 -0
- package/src/lsp/index.ts +55 -0
- package/src/lsp/inlay-hints.ts +308 -0
- package/src/lsp/interop-rename.ts +454 -0
- package/src/lsp/match-arms.ts +400 -0
- package/src/lsp/navigation.ts +553 -0
- package/src/lsp/parsed-documents.ts +64 -0
- package/src/lsp/rename-worker.ts +40 -0
- package/src/lsp/server.ts +1437 -0
- package/src/lsp/thunk-pipeline.ts +117 -0
- package/src/lsp/validation.ts +147 -0
- package/src/lsp/workspace.ts +74 -0
- package/src/parser/declarations.ts +808 -0
- package/src/parser/errors.ts +18 -0
- package/src/parser/jsx.ts +253 -0
- package/src/parser/operator-expressions.ts +170 -0
- package/src/parser/parser.ts +1565 -0
- package/src/parser/patterns.ts +342 -0
- package/src/parser/pipeline-expressions.ts +77 -0
- package/src/parser/scope.ts +69 -0
- package/src/parser/statements.ts +831 -0
- package/src/parser/types.ts +453 -0
- package/src/plugins/bun-preload.ts +25 -0
- package/src/plugins/bun.ts +448 -0
- package/src/plugins/esbuild.ts +308 -0
- package/src/plugins/type-view.ts +326 -0
- package/src/plugins/vite.ts +294 -0
- package/src/testing/index.ts +72 -0
- package/src/tooling/doc.ts +786 -0
- package/src/tooling/idiomatic.ts +202 -0
- package/src/tooling/pipeline-style.ts +248 -0
- package/src/version.ts +27 -0
|
@@ -0,0 +1,1220 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { AsDeclaration } from "../ast/types.js";
|
|
3
|
+
import { FunScriptError } from "../errors.js";
|
|
4
|
+
|
|
5
|
+
const cache = new Map<string, string>();
|
|
6
|
+
const prepared = new WeakMap<AsDeclaration, string>();
|
|
7
|
+
|
|
8
|
+
const SCALAR_TYPES = new Set(["i32", "u32", "i64", "u64", "f32", "f64"]);
|
|
9
|
+
const BOOLEAN_TYPES = new Set(["bool", "boolean"]);
|
|
10
|
+
const BIGINT_SCALAR_TYPES = new Set(["i64", "u64"]);
|
|
11
|
+
const TYPED_ARRAY_TYPES = new Set([
|
|
12
|
+
"Float32Array",
|
|
13
|
+
"Float64Array",
|
|
14
|
+
"Int32Array",
|
|
15
|
+
"Uint32Array",
|
|
16
|
+
"Uint8Array",
|
|
17
|
+
"Int8Array",
|
|
18
|
+
"Uint16Array",
|
|
19
|
+
"Int16Array",
|
|
20
|
+
"Uint8ClampedArray",
|
|
21
|
+
"BigInt64Array",
|
|
22
|
+
"BigUint64Array",
|
|
23
|
+
"Int64Array",
|
|
24
|
+
"Uint64Array",
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
function parseArrayElementType(t: string): string | null {
|
|
28
|
+
const trimmed = t.trim();
|
|
29
|
+
if (trimmed.endsWith("[]")) return trimmed.slice(0, -2).trim();
|
|
30
|
+
const match = /^Array<(.+)>$/.exec(trimmed);
|
|
31
|
+
if (match) return match[1].trim();
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseTupleElementTypeList(t: string): string[] | null {
|
|
36
|
+
const trimmed = t.trim();
|
|
37
|
+
if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) return null;
|
|
38
|
+
const inner = trimmed.slice(1, -1).trim();
|
|
39
|
+
if (inner === "") return [];
|
|
40
|
+
const elements: string[] = [];
|
|
41
|
+
let depth = 0;
|
|
42
|
+
let start = 0;
|
|
43
|
+
for (let i = 0; i < inner.length; i++) {
|
|
44
|
+
const c = inner[i];
|
|
45
|
+
if (c === "[" || c === "<" || c === "{" || c === "(") depth++;
|
|
46
|
+
else if (c === "]" || c === ">" || c === "}" || c === ")") depth--;
|
|
47
|
+
else if (c === "," && depth === 0) {
|
|
48
|
+
elements.push(inner.slice(start, i).trim());
|
|
49
|
+
start = i + 1;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
elements.push(inner.slice(start).trim());
|
|
53
|
+
return elements;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function tupleClassName(elements: string[]): string {
|
|
57
|
+
const sanitize = (s: string) => s.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
58
|
+
return `__fs_tuple_${elements.length}_${elements.map(sanitize).join("_")}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function transformKernelSource(code: string): {
|
|
62
|
+
transformed: string;
|
|
63
|
+
allTuples: Map<string, string[]>;
|
|
64
|
+
} {
|
|
65
|
+
const source = ts.createSourceFile(
|
|
66
|
+
"kernel.ts",
|
|
67
|
+
code,
|
|
68
|
+
ts.ScriptTarget.Latest,
|
|
69
|
+
true,
|
|
70
|
+
);
|
|
71
|
+
const replacements: { start: number; end: number; text: string }[] = [];
|
|
72
|
+
const allTuples = new Map<string, string[]>();
|
|
73
|
+
|
|
74
|
+
function recordTuple(elemTypes: string[]) {
|
|
75
|
+
const name = tupleClassName(elemTypes);
|
|
76
|
+
if (!allTuples.has(name)) {
|
|
77
|
+
allTuples.set(name, elemTypes);
|
|
78
|
+
for (const e of elemTypes) {
|
|
79
|
+
const inner = parseTupleElementTypeList(e);
|
|
80
|
+
if (inner) recordTuple(inner);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return name;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const tupleVarTypes = new Map<string, string[]>();
|
|
87
|
+
function preScan(node: ts.Node) {
|
|
88
|
+
if (ts.isParameter(node) && node.type && ts.isTupleTypeNode(node.type)) {
|
|
89
|
+
const elemTypes = node.type.elements.map((e) => e.getText(source).trim());
|
|
90
|
+
tupleVarTypes.set(node.name.getText(source), elemTypes);
|
|
91
|
+
}
|
|
92
|
+
if (
|
|
93
|
+
ts.isVariableDeclaration(node) &&
|
|
94
|
+
node.type &&
|
|
95
|
+
ts.isTupleTypeNode(node.type)
|
|
96
|
+
) {
|
|
97
|
+
const elemTypes = node.type.elements.map((e) => e.getText(source).trim());
|
|
98
|
+
tupleVarTypes.set(node.name.getText(source), elemTypes);
|
|
99
|
+
}
|
|
100
|
+
ts.forEachChild(node, preScan);
|
|
101
|
+
}
|
|
102
|
+
preScan(source);
|
|
103
|
+
|
|
104
|
+
function scan(node: ts.Node) {
|
|
105
|
+
if (ts.isTupleTypeNode(node)) {
|
|
106
|
+
const elemTypes = node.elements.map((e) => e.getText(source).trim());
|
|
107
|
+
const clsName = recordTuple(elemTypes);
|
|
108
|
+
replacements.push({
|
|
109
|
+
start: node.getStart(source),
|
|
110
|
+
end: node.getEnd(),
|
|
111
|
+
text: clsName,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (
|
|
115
|
+
ts.isElementAccessExpression(node) &&
|
|
116
|
+
ts.isNumericLiteral(node.argumentExpression)
|
|
117
|
+
) {
|
|
118
|
+
const exprText = node.expression.getText(source);
|
|
119
|
+
if (tupleVarTypes.has(exprText)) {
|
|
120
|
+
const idx = node.argumentExpression.text;
|
|
121
|
+
replacements.push({
|
|
122
|
+
start: node.getStart(source),
|
|
123
|
+
end: node.getEnd(),
|
|
124
|
+
text: `${exprText}._${idx}`,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (
|
|
129
|
+
ts.isVariableDeclaration(node) &&
|
|
130
|
+
node.type &&
|
|
131
|
+
ts.isTupleTypeNode(node.type)
|
|
132
|
+
) {
|
|
133
|
+
if (node.initializer && ts.isArrayLiteralExpression(node.initializer)) {
|
|
134
|
+
const elemTypes = node.type.elements.map((e) =>
|
|
135
|
+
e.getText(source).trim(),
|
|
136
|
+
);
|
|
137
|
+
const clsName = recordTuple(elemTypes);
|
|
138
|
+
replacements.push({
|
|
139
|
+
start: node.initializer.getStart(source),
|
|
140
|
+
end: node.initializer.getStart(source) + 1,
|
|
141
|
+
text: `__fs_make_${clsName}(`,
|
|
142
|
+
});
|
|
143
|
+
replacements.push({
|
|
144
|
+
start: node.initializer.getEnd() - 1,
|
|
145
|
+
end: node.initializer.getEnd(),
|
|
146
|
+
text: `)`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (
|
|
151
|
+
ts.isReturnStatement(node) &&
|
|
152
|
+
node.expression &&
|
|
153
|
+
ts.isArrayLiteralExpression(node.expression)
|
|
154
|
+
) {
|
|
155
|
+
let parent = node.parent;
|
|
156
|
+
while (
|
|
157
|
+
parent &&
|
|
158
|
+
!ts.isFunctionDeclaration(parent) &&
|
|
159
|
+
!ts.isArrowFunction(parent) &&
|
|
160
|
+
!ts.isFunctionExpression(parent) &&
|
|
161
|
+
!ts.isMethodDeclaration(parent)
|
|
162
|
+
) {
|
|
163
|
+
parent = parent.parent;
|
|
164
|
+
}
|
|
165
|
+
if (parent && parent.type && ts.isTupleTypeNode(parent.type)) {
|
|
166
|
+
const elemTypes = parent.type.elements.map((e) =>
|
|
167
|
+
e.getText(source).trim(),
|
|
168
|
+
);
|
|
169
|
+
const clsName = recordTuple(elemTypes);
|
|
170
|
+
replacements.push({
|
|
171
|
+
start: node.expression.getStart(source),
|
|
172
|
+
end: node.expression.getStart(source) + 1,
|
|
173
|
+
text: `__fs_make_${clsName}(`,
|
|
174
|
+
});
|
|
175
|
+
replacements.push({
|
|
176
|
+
start: node.expression.getEnd() - 1,
|
|
177
|
+
end: node.expression.getEnd(),
|
|
178
|
+
text: `)`,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (
|
|
183
|
+
ts.isArrowFunction(node) &&
|
|
184
|
+
node.type &&
|
|
185
|
+
ts.isTupleTypeNode(node.type) &&
|
|
186
|
+
ts.isArrayLiteralExpression(node.body)
|
|
187
|
+
) {
|
|
188
|
+
const elemTypes = node.type.elements.map((e) => e.getText(source).trim());
|
|
189
|
+
const clsName = recordTuple(elemTypes);
|
|
190
|
+
replacements.push({
|
|
191
|
+
start: node.body.getStart(source),
|
|
192
|
+
end: node.body.getStart(source) + 1,
|
|
193
|
+
text: `__fs_make_${clsName}(`,
|
|
194
|
+
});
|
|
195
|
+
replacements.push({
|
|
196
|
+
start: node.body.getEnd() - 1,
|
|
197
|
+
end: node.body.getEnd(),
|
|
198
|
+
text: `)`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
ts.forEachChild(node, scan);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
scan(source);
|
|
205
|
+
|
|
206
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
207
|
+
let transformed = code;
|
|
208
|
+
for (const r of replacements) {
|
|
209
|
+
transformed =
|
|
210
|
+
transformed.slice(0, r.start) + r.text + transformed.slice(r.end);
|
|
211
|
+
}
|
|
212
|
+
return { transformed, allTuples };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const toJsTypedArray = (t: string): string =>
|
|
216
|
+
t === "Int64Array"
|
|
217
|
+
? "BigInt64Array"
|
|
218
|
+
: t === "Uint64Array"
|
|
219
|
+
? "BigUint64Array"
|
|
220
|
+
: t;
|
|
221
|
+
|
|
222
|
+
/** Compile a numeric, string, boolean, struct, tuple, or array kernel with SIMD enabled. */
|
|
223
|
+
export async function prepareAssemblyScript(
|
|
224
|
+
decl: AsDeclaration,
|
|
225
|
+
): Promise<void> {
|
|
226
|
+
const { transformed, allTuples } = transformKernelSource(decl.code);
|
|
227
|
+
const source = ts.createSourceFile(
|
|
228
|
+
"kernel.ts",
|
|
229
|
+
decl.code,
|
|
230
|
+
ts.ScriptTarget.Latest,
|
|
231
|
+
true,
|
|
232
|
+
);
|
|
233
|
+
const signatures: { name: string; params: string[]; result: string }[] = [];
|
|
234
|
+
const classes = new Map<
|
|
235
|
+
string,
|
|
236
|
+
{ name: string; fields: { name: string; type: string }[] }
|
|
237
|
+
>();
|
|
238
|
+
|
|
239
|
+
for (const statement of source.statements) {
|
|
240
|
+
if (ts.isClassDeclaration(statement) && statement.name) {
|
|
241
|
+
const fields: { name: string; type: string }[] = [];
|
|
242
|
+
for (const member of statement.members) {
|
|
243
|
+
if (ts.isPropertyDeclaration(member) && member.name) {
|
|
244
|
+
const fieldName = member.name.getText(source);
|
|
245
|
+
const fieldType = member.type?.getText(source) ?? "";
|
|
246
|
+
fields.push({ name: fieldName, type: fieldType });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
classes.set(statement.name.text, { name: statement.name.text, fields });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const isValidFieldType = (t: string): boolean => {
|
|
254
|
+
if (
|
|
255
|
+
SCALAR_TYPES.has(t) ||
|
|
256
|
+
BOOLEAN_TYPES.has(t) ||
|
|
257
|
+
TYPED_ARRAY_TYPES.has(t) ||
|
|
258
|
+
t === "string" ||
|
|
259
|
+
classes.has(t)
|
|
260
|
+
)
|
|
261
|
+
return true;
|
|
262
|
+
const elem = parseArrayElementType(t);
|
|
263
|
+
if (elem) {
|
|
264
|
+
return (
|
|
265
|
+
SCALAR_TYPES.has(elem) ||
|
|
266
|
+
BOOLEAN_TYPES.has(elem) ||
|
|
267
|
+
elem === "string" ||
|
|
268
|
+
classes.has(elem) ||
|
|
269
|
+
(parseTupleElementTypeList(elem)?.every(isValidFieldType) ?? false)
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
const tupleElems = parseTupleElementTypeList(t);
|
|
273
|
+
if (tupleElems && tupleElems.length > 0) {
|
|
274
|
+
return tupleElems.every(isValidFieldType);
|
|
275
|
+
}
|
|
276
|
+
return false;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const isValidParamType = (p: string) => isValidFieldType(p);
|
|
280
|
+
const isValidReturnType = (r: string) => r === "void" || isValidFieldType(r);
|
|
281
|
+
|
|
282
|
+
for (const statement of source.statements) {
|
|
283
|
+
if (
|
|
284
|
+
ts.isImportDeclaration(statement) ||
|
|
285
|
+
ts.isExportDeclaration(statement)
|
|
286
|
+
) {
|
|
287
|
+
throw new FunScriptError(
|
|
288
|
+
"AssemblyScript blocks cannot import or re-export modules",
|
|
289
|
+
decl.span,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
if (ts.isClassDeclaration(statement)) {
|
|
293
|
+
const cls = classes.get(statement.name?.text ?? "");
|
|
294
|
+
if (cls) {
|
|
295
|
+
for (const f of cls.fields) {
|
|
296
|
+
if (!isValidFieldType(f.type)) {
|
|
297
|
+
throw new FunScriptError(
|
|
298
|
+
`AssemblyScript struct '${cls.name}' has unsupported field type '${f.type}'`,
|
|
299
|
+
decl.span,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (
|
|
307
|
+
!ts.canHaveModifiers(statement) ||
|
|
308
|
+
!ts
|
|
309
|
+
.getModifiers(statement)
|
|
310
|
+
?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)
|
|
311
|
+
)
|
|
312
|
+
continue;
|
|
313
|
+
if (
|
|
314
|
+
!ts.isFunctionDeclaration(statement) ||
|
|
315
|
+
!statement.name ||
|
|
316
|
+
statement.typeParameters?.length ||
|
|
317
|
+
!statement.body
|
|
318
|
+
) {
|
|
319
|
+
throw new FunScriptError(
|
|
320
|
+
"AssemblyScript blocks may only export numeric functions or struct classes",
|
|
321
|
+
decl.span,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
const result = statement.type?.getText(source) ?? "";
|
|
325
|
+
const params = statement.parameters.map(
|
|
326
|
+
(p) => p.type?.getText(source) ?? "",
|
|
327
|
+
);
|
|
328
|
+
if (
|
|
329
|
+
!isValidReturnType(result) ||
|
|
330
|
+
params.some((p) => !isValidParamType(p)) ||
|
|
331
|
+
statement.parameters.some(
|
|
332
|
+
(p) => p.dotDotDotToken || p.questionToken || p.initializer,
|
|
333
|
+
)
|
|
334
|
+
) {
|
|
335
|
+
throw new FunScriptError(
|
|
336
|
+
"AssemblyScript exports require explicit i32, u32, f32, or f64, or TypedArray parameter and return types",
|
|
337
|
+
decl.span,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
signatures.push({ name: statement.name.text, params, result });
|
|
341
|
+
}
|
|
342
|
+
if (!signatures.length)
|
|
343
|
+
throw new FunScriptError(
|
|
344
|
+
"AssemblyScript block must export at least one numeric function",
|
|
345
|
+
decl.span,
|
|
346
|
+
);
|
|
347
|
+
let binary = cache.get(decl.code);
|
|
348
|
+
if (!binary) {
|
|
349
|
+
try {
|
|
350
|
+
interface AscModule {
|
|
351
|
+
compileString(
|
|
352
|
+
source: string,
|
|
353
|
+
options: object,
|
|
354
|
+
): Promise<{
|
|
355
|
+
error: unknown;
|
|
356
|
+
binary: Uint8Array | null;
|
|
357
|
+
stderr: { toString(): string };
|
|
358
|
+
}>;
|
|
359
|
+
}
|
|
360
|
+
const asc = (await import(
|
|
361
|
+
"assemblyscript/" + "asc"
|
|
362
|
+
)) as unknown as AscModule;
|
|
363
|
+
let compileSource =
|
|
364
|
+
transformed +
|
|
365
|
+
"\nclass BigInt64Array extends Int64Array {}\nclass BigUint64Array extends Uint64Array {}\nexport function __funscript_reset_heap(): void { __reset(); }\n";
|
|
366
|
+
|
|
367
|
+
for (const [clsName, elemTypes] of allTuples) {
|
|
368
|
+
const fieldsDef = elemTypes
|
|
369
|
+
.map(
|
|
370
|
+
(t, fi) =>
|
|
371
|
+
` _${fi}: ${BOOLEAN_TYPES.has(t) ? "bool" : parseTupleElementTypeList(t) ? tupleClassName(parseTupleElementTypeList(t)!) : t};`,
|
|
372
|
+
)
|
|
373
|
+
.join("\n");
|
|
374
|
+
let opDef = "";
|
|
375
|
+
const firstType = elemTypes[0];
|
|
376
|
+
if (elemTypes.every((t) => t === firstType)) {
|
|
377
|
+
const asElem = BOOLEAN_TYPES.has(firstType)
|
|
378
|
+
? "bool"
|
|
379
|
+
: parseTupleElementTypeList(firstType)
|
|
380
|
+
? tupleClassName(parseTupleElementTypeList(firstType)!)
|
|
381
|
+
: firstType;
|
|
382
|
+
const branches = elemTypes
|
|
383
|
+
.map((_, fi) => `if (index == ${fi}) return this._${fi};`)
|
|
384
|
+
.join(" ");
|
|
385
|
+
opDef = `\n @operator("[]")\n __get(index: i32): ${asElem} { ${branches} return this._0; }`;
|
|
386
|
+
}
|
|
387
|
+
compileSource += `class ${clsName} {\n${fieldsDef}${opDef}\n}\n`;
|
|
388
|
+
|
|
389
|
+
const ctorParams = elemTypes
|
|
390
|
+
.map(
|
|
391
|
+
(t, fi) =>
|
|
392
|
+
`_${fi}: ${BOOLEAN_TYPES.has(t) ? "bool" : parseTupleElementTypeList(t) ? tupleClassName(parseTupleElementTypeList(t)!) : t}`,
|
|
393
|
+
)
|
|
394
|
+
.join(", ");
|
|
395
|
+
const ctorBody = elemTypes
|
|
396
|
+
.map((_, fi) => `o._${fi} = _${fi};`)
|
|
397
|
+
.join(" ");
|
|
398
|
+
compileSource += `export function __fs_make_${clsName}(${ctorParams}): ${clsName} { const o = changetype<${clsName}>(__new(offsetof<${clsName}>(), idof<${clsName}>())); ${ctorBody} return o; }\n`;
|
|
399
|
+
|
|
400
|
+
for (let fi = 0; fi < elemTypes.length; fi++) {
|
|
401
|
+
const t = elemTypes[fi];
|
|
402
|
+
const asType = BOOLEAN_TYPES.has(t)
|
|
403
|
+
? "bool"
|
|
404
|
+
: parseTupleElementTypeList(t)
|
|
405
|
+
? tupleClassName(parseTupleElementTypeList(t)!)
|
|
406
|
+
: t;
|
|
407
|
+
compileSource += `export function __fs_get_${clsName}_${fi}(o: ${clsName}): ${asType} { return o._${fi}; }\n`;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
for (const cls of classes.values()) {
|
|
412
|
+
for (const field of cls.fields) {
|
|
413
|
+
const tupleElems = parseTupleElementTypeList(field.type);
|
|
414
|
+
const asFieldType = BOOLEAN_TYPES.has(field.type)
|
|
415
|
+
? "bool"
|
|
416
|
+
: tupleElems
|
|
417
|
+
? tupleClassName(tupleElems)
|
|
418
|
+
: field.type;
|
|
419
|
+
compileSource += `export function __fs_get_${cls.name}_${field.name}(obj: ${cls.name}): ${asFieldType} { return obj.${field.name}; }\n`;
|
|
420
|
+
}
|
|
421
|
+
const ctorParams = cls.fields
|
|
422
|
+
.map((f) => {
|
|
423
|
+
const tupleElems = parseTupleElementTypeList(f.type);
|
|
424
|
+
const asType = BOOLEAN_TYPES.has(f.type)
|
|
425
|
+
? "bool"
|
|
426
|
+
: tupleElems
|
|
427
|
+
? tupleClassName(tupleElems)
|
|
428
|
+
: f.type;
|
|
429
|
+
return `${f.name}: ${asType}`;
|
|
430
|
+
})
|
|
431
|
+
.join(", ");
|
|
432
|
+
const ctorBody = cls.fields
|
|
433
|
+
.map((f) => `o.${f.name} = ${f.name};`)
|
|
434
|
+
.join(" ");
|
|
435
|
+
compileSource += `export function __fs_make_${cls.name}(${ctorParams}): ${cls.name} { const o = changetype<${cls.name}>(__new(offsetof<${cls.name}>(), idof<${cls.name}>())); ${ctorBody} return o; }\n`;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const arrayElemTypes = new Set<string>();
|
|
439
|
+
const registerArray = (t: string) => {
|
|
440
|
+
const elem = parseArrayElementType(t);
|
|
441
|
+
if (elem) arrayElemTypes.add(elem);
|
|
442
|
+
const tupleElems = parseTupleElementTypeList(t);
|
|
443
|
+
if (tupleElems) {
|
|
444
|
+
for (const te of tupleElems) registerArray(te);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
for (const cls of classes.values()) {
|
|
448
|
+
for (const f of cls.fields) registerArray(f.type);
|
|
449
|
+
}
|
|
450
|
+
for (const sig of signatures) {
|
|
451
|
+
registerArray(sig.result);
|
|
452
|
+
for (const p of sig.params) registerArray(p);
|
|
453
|
+
}
|
|
454
|
+
for (const [, elemTypes] of allTuples) {
|
|
455
|
+
for (const t of elemTypes) registerArray(t);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
for (const elem of arrayElemTypes) {
|
|
459
|
+
const tupleElems = parseTupleElementTypeList(elem);
|
|
460
|
+
const asElem = BOOLEAN_TYPES.has(elem)
|
|
461
|
+
? "bool"
|
|
462
|
+
: tupleElems
|
|
463
|
+
? tupleClassName(tupleElems)
|
|
464
|
+
: elem;
|
|
465
|
+
const safeElem =
|
|
466
|
+
elem === "boolean"
|
|
467
|
+
? "bool"
|
|
468
|
+
: tupleElems
|
|
469
|
+
? tupleClassName(tupleElems)
|
|
470
|
+
: elem;
|
|
471
|
+
compileSource += `export function __fs_arr_len_${safeElem}(arr: ${asElem}[]): i32 { return arr.length; }\n`;
|
|
472
|
+
compileSource += `export function __fs_arr_get_${safeElem}(arr: ${asElem}[], i: i32): ${asElem} { return arr[i]; }\n`;
|
|
473
|
+
compileSource += `export function __fs_arr_make_${safeElem}(len: i32): ${asElem}[] { return new Array<${asElem}>(len); }\n`;
|
|
474
|
+
compileSource += `export function __fs_arr_set_${safeElem}(arr: ${asElem}[], i: i32, val: ${asElem}): void { arr[i] = val; }\n`;
|
|
475
|
+
}
|
|
476
|
+
const result = await asc.compileString(compileSource, {
|
|
477
|
+
optimizeLevel: 3,
|
|
478
|
+
runtime: "stub",
|
|
479
|
+
exportRuntime: true,
|
|
480
|
+
enable: ["simd"],
|
|
481
|
+
});
|
|
482
|
+
if (result.error || !result.binary) {
|
|
483
|
+
const detail = result.stderr.toString();
|
|
484
|
+
const location = /(?:input|stdin)\.ts\((\d+),(\d+)\)/.exec(detail);
|
|
485
|
+
if (location && decl.codeStart) {
|
|
486
|
+
const line = Number(location[1]);
|
|
487
|
+
const column = Number(location[2]);
|
|
488
|
+
const lines = decl.code.split("\n");
|
|
489
|
+
if (
|
|
490
|
+
line >= 1 &&
|
|
491
|
+
line <= lines.length &&
|
|
492
|
+
column >= 1 &&
|
|
493
|
+
column <= lines[line - 1].length + 1
|
|
494
|
+
) {
|
|
495
|
+
const preceding = lines
|
|
496
|
+
.slice(0, line - 1)
|
|
497
|
+
.reduce((sum, part) => sum + part.length + 1, 0);
|
|
498
|
+
const start = {
|
|
499
|
+
offset: decl.codeStart.offset + preceding + column - 1,
|
|
500
|
+
line: decl.codeStart.line + line - 1,
|
|
501
|
+
column: line === 1 ? decl.codeStart.column + column - 1 : column,
|
|
502
|
+
};
|
|
503
|
+
throw new FunScriptError(
|
|
504
|
+
`AssemblyScript compilation failed: ${detail}`,
|
|
505
|
+
{
|
|
506
|
+
start,
|
|
507
|
+
end: {
|
|
508
|
+
...start,
|
|
509
|
+
offset: start.offset + 1,
|
|
510
|
+
column: start.column + 1,
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
throw new Error(detail);
|
|
517
|
+
}
|
|
518
|
+
const wasm = (
|
|
519
|
+
globalThis as unknown as { WebAssembly?: typeof WebAssembly }
|
|
520
|
+
).WebAssembly;
|
|
521
|
+
if (!wasm) {
|
|
522
|
+
throw new Error(
|
|
523
|
+
"WebAssembly is not supported in the current environment",
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
const imports: { module: string; name: string; kind: string }[] =
|
|
527
|
+
wasm.Module.imports(new wasm.Module(result.binary));
|
|
528
|
+
if (
|
|
529
|
+
imports.some(
|
|
530
|
+
(i) =>
|
|
531
|
+
i.module !== "env" || i.name !== "abort" || i.kind !== "function",
|
|
532
|
+
)
|
|
533
|
+
)
|
|
534
|
+
throw new Error("AssemblyScript kernel has unsupported imports");
|
|
535
|
+
binary = Buffer.from(result.binary).toString("base64");
|
|
536
|
+
if (cache.size >= 64) cache.delete(cache.keys().next().value!);
|
|
537
|
+
cache.set(decl.code, binary);
|
|
538
|
+
} catch (cause) {
|
|
539
|
+
if (cause instanceof FunScriptError) throw cause;
|
|
540
|
+
throw new FunScriptError(
|
|
541
|
+
`AssemblyScript compilation failed: ${String(cause)}`,
|
|
542
|
+
decl.span,
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
prepared.set(decl, binary);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function emitAssemblyScript(decl: AsDeclaration): string {
|
|
550
|
+
const binary = prepared.get(decl) ?? cache.get(decl.code);
|
|
551
|
+
if (!binary)
|
|
552
|
+
throw new FunScriptError(
|
|
553
|
+
"AssemblyScript block has not been compiled",
|
|
554
|
+
decl.span,
|
|
555
|
+
);
|
|
556
|
+
const source = ts.createSourceFile(
|
|
557
|
+
"kernel.ts",
|
|
558
|
+
decl.code,
|
|
559
|
+
ts.ScriptTarget.Latest,
|
|
560
|
+
true,
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
const classes = new Map<
|
|
564
|
+
string,
|
|
565
|
+
{ name: string; fields: { name: string; type: string }[] }
|
|
566
|
+
>();
|
|
567
|
+
for (const statement of source.statements) {
|
|
568
|
+
if (ts.isClassDeclaration(statement) && statement.name) {
|
|
569
|
+
const fields: { name: string; type: string }[] = [];
|
|
570
|
+
for (const member of statement.members) {
|
|
571
|
+
if (ts.isPropertyDeclaration(member) && member.name) {
|
|
572
|
+
const fieldName = member.name.getText(source);
|
|
573
|
+
const fieldType = member.type?.getText(source) ?? "";
|
|
574
|
+
fields.push({ name: fieldName, type: fieldType });
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
classes.set(statement.name.text, { name: statement.name.text, fields });
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const signatures = source.statements
|
|
582
|
+
.filter(ts.isFunctionDeclaration)
|
|
583
|
+
.filter((s) =>
|
|
584
|
+
ts.getModifiers(s)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword),
|
|
585
|
+
)
|
|
586
|
+
.map((s) => ({
|
|
587
|
+
name: s.name!.text,
|
|
588
|
+
params: s.parameters.map((p) => ({
|
|
589
|
+
type: p.type?.getText(source) ?? "",
|
|
590
|
+
name: p.name.getText(source),
|
|
591
|
+
})),
|
|
592
|
+
result: s.type?.getText(source) ?? "void",
|
|
593
|
+
}));
|
|
594
|
+
|
|
595
|
+
const prefix = `__funWasm${decl.span.start.offset}`;
|
|
596
|
+
const lines = [
|
|
597
|
+
`let ${prefix}Instance: Record<string, unknown> | undefined;`,
|
|
598
|
+
`const ${prefix}Get = (): Record<string, unknown> => {`,
|
|
599
|
+
` if (${prefix}Instance !== undefined) return ${prefix}Instance;`,
|
|
600
|
+
` const bytes = Uint8Array.from(atob(${JSON.stringify(binary)}), c => c.charCodeAt(0));`,
|
|
601
|
+
` const runtime = (globalThis as unknown as { WebAssembly: { Module: new (bytes: Uint8Array) => object; Instance: new (module: object, imports: { env: { abort: () => never } }) => { exports: Record<string, unknown> } } }).WebAssembly;`,
|
|
602
|
+
` return ${prefix}Instance = new runtime.Instance(new runtime.Module(bytes), { env: { abort: () => { throw new Error("AssemblyScript abort"); } } }).exports;`,
|
|
603
|
+
`};`,
|
|
604
|
+
`class ${prefix}WasmHandle<T = unknown> {`,
|
|
605
|
+
` readonly _tag = "WasmHandle" as const;`,
|
|
606
|
+
` constructor(readonly ptr: number, readonly read: () => T) {}`,
|
|
607
|
+
`};`,
|
|
608
|
+
`const ${prefix}Alloc = (exports: Record<string, unknown>, arr: ArrayBufferView, Ctor: { new (buffer: ArrayBuffer, byteOffset: number, length: number): { set(source: any): void }; BYTES_PER_ELEMENT: number }): { viewPtr: number; bufPtr: number; arr: ArrayBufferView; Ctor: { new (buffer: ArrayBuffer, byteOffset: number, length: number): { set(source: any): void }; BYTES_PER_ELEMENT: number } } => {`,
|
|
609
|
+
` const byteLength = arr.byteLength;`,
|
|
610
|
+
` const newFn = exports.__new as (size: number, id: number) => number;`,
|
|
611
|
+
` const bufPtr = newFn(byteLength, 0);`,
|
|
612
|
+
` const viewPtr = newFn(12, 0);`,
|
|
613
|
+
` const mem = (exports.memory as { buffer: ArrayBuffer }).buffer;`,
|
|
614
|
+
` const mem32 = new Uint32Array(mem);`,
|
|
615
|
+
` mem32[viewPtr >> 2] = bufPtr;`,
|
|
616
|
+
` mem32[(viewPtr + 4) >> 2] = bufPtr;`,
|
|
617
|
+
` mem32[(viewPtr + 8) >> 2] = byteLength;`,
|
|
618
|
+
` new Ctor(mem, bufPtr, (arr as unknown as { length: number }).length).set(arr as any);`,
|
|
619
|
+
` return { viewPtr, bufPtr, arr, Ctor };`,
|
|
620
|
+
`};`,
|
|
621
|
+
`const ${prefix}Read = <T extends { slice(): T }>(exports: Record<string, unknown>, ptr: number, Ctor: { new (buffer: ArrayBuffer, byteOffset: number, length: number): T; BYTES_PER_ELEMENT: number }): T => {`,
|
|
622
|
+
` const mem = (exports.memory as { buffer: ArrayBuffer }).buffer;`,
|
|
623
|
+
` const mem32 = new Uint32Array(mem);`,
|
|
624
|
+
` const dataStart = mem32[(ptr + 4) >> 2];`,
|
|
625
|
+
` const byteLength = mem32[(ptr + 8) >> 2];`,
|
|
626
|
+
` const len = byteLength / Ctor.BYTES_PER_ELEMENT;`,
|
|
627
|
+
` return new Ctor(mem, dataStart, len).slice();`,
|
|
628
|
+
`};`,
|
|
629
|
+
`const ${prefix}Sync = (exports: Record<string, unknown>, handle: { viewPtr: number; bufPtr: number; arr: ArrayBufferView; Ctor: { new (buffer: ArrayBuffer, byteOffset: number, length: number): { set(source: any): void }; BYTES_PER_ELEMENT: number } }): void => {`,
|
|
630
|
+
` const mem = (exports.memory as { buffer: ArrayBuffer }).buffer;`,
|
|
631
|
+
` (handle.arr as unknown as { set(source: unknown): void }).set(new handle.Ctor(mem, handle.bufPtr, (handle.arr as unknown as { length: number }).length));`,
|
|
632
|
+
`};`,
|
|
633
|
+
`const ${prefix}AllocString = (exports: Record<string, unknown>, str: string): number => {`,
|
|
634
|
+
` const buf = new Uint16Array(str.length);`,
|
|
635
|
+
` for (let i = 0; i < str.length; i++) buf[i] = str.charCodeAt(i);`,
|
|
636
|
+
` const byteLength = buf.byteLength;`,
|
|
637
|
+
` const newFn = exports.__new as (size: number, id: number) => number;`,
|
|
638
|
+
` const ptr = newFn(byteLength, 2);`,
|
|
639
|
+
` const mem = (exports.memory as { buffer: ArrayBuffer }).buffer;`,
|
|
640
|
+
` new Uint8Array(mem, ptr, byteLength).set(new Uint8Array(buf.buffer));`,
|
|
641
|
+
` return ptr;`,
|
|
642
|
+
`};`,
|
|
643
|
+
`const ${prefix}ReadString = (exports: Record<string, unknown>, ptr: number): string => {`,
|
|
644
|
+
` if (!ptr) return "";`,
|
|
645
|
+
` const mem = (exports.memory as { buffer: ArrayBuffer }).buffer;`,
|
|
646
|
+
` const len = new Uint32Array(mem, ptr - 4, 1)[0];`,
|
|
647
|
+
` return new TextDecoder("utf-16le").decode(new Uint8Array(mem, ptr, len));`,
|
|
648
|
+
`};`,
|
|
649
|
+
];
|
|
650
|
+
|
|
651
|
+
const toTsType = (t: string): string => {
|
|
652
|
+
if (t === "void") return "void";
|
|
653
|
+
if (BOOLEAN_TYPES.has(t)) return "boolean";
|
|
654
|
+
if (t === "string") return "string";
|
|
655
|
+
if (TYPED_ARRAY_TYPES.has(t)) return toJsTypedArray(t);
|
|
656
|
+
if (BIGINT_SCALAR_TYPES.has(t)) return "bigint";
|
|
657
|
+
if (SCALAR_TYPES.has(t)) return "number";
|
|
658
|
+
const elem = parseArrayElementType(t);
|
|
659
|
+
if (elem) return `readonly ${toTsType(elem)}[]`;
|
|
660
|
+
const tupleElems = parseTupleElementTypeList(t);
|
|
661
|
+
if (tupleElems) return `readonly [${tupleElems.map(toTsType).join(", ")}]`;
|
|
662
|
+
return t;
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const isHandleEligible = (t: string): boolean =>
|
|
666
|
+
TYPED_ARRAY_TYPES.has(t) ||
|
|
667
|
+
classes.has(t) ||
|
|
668
|
+
parseTupleElementTypeList(t) !== null ||
|
|
669
|
+
parseArrayElementType(t) !== null;
|
|
670
|
+
|
|
671
|
+
const toTsParamType = (t: string): string => {
|
|
672
|
+
const base = toTsType(t);
|
|
673
|
+
return isHandleEligible(t)
|
|
674
|
+
? `${base} | ${prefix}WasmHandle<${base}>`
|
|
675
|
+
: base;
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
const { allTuples } = transformKernelSource(decl.code);
|
|
679
|
+
const registerTuple = (t: string) => {
|
|
680
|
+
const tupleElems = parseTupleElementTypeList(t);
|
|
681
|
+
if (tupleElems) {
|
|
682
|
+
const name = tupleClassName(tupleElems);
|
|
683
|
+
if (!allTuples.has(name)) {
|
|
684
|
+
allTuples.set(name, tupleElems);
|
|
685
|
+
for (const te of tupleElems) registerTuple(te);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
for (const cls of classes.values()) {
|
|
690
|
+
for (const f of cls.fields) registerTuple(f.type);
|
|
691
|
+
}
|
|
692
|
+
for (const sig of signatures) {
|
|
693
|
+
registerTuple(sig.result);
|
|
694
|
+
for (const p of sig.params) registerTuple(p.type);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
const arrayElemTypes = new Set<string>();
|
|
698
|
+
const registerArray = (t: string) => {
|
|
699
|
+
const elem = parseArrayElementType(t);
|
|
700
|
+
if (elem) arrayElemTypes.add(elem);
|
|
701
|
+
const tupleElems = parseTupleElementTypeList(t);
|
|
702
|
+
if (tupleElems) {
|
|
703
|
+
for (const te of tupleElems) registerArray(te);
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
for (const cls of classes.values()) {
|
|
707
|
+
for (const f of cls.fields) registerArray(f.type);
|
|
708
|
+
}
|
|
709
|
+
for (const sig of signatures) {
|
|
710
|
+
registerArray(sig.result);
|
|
711
|
+
for (const p of sig.params) registerArray(p.type);
|
|
712
|
+
}
|
|
713
|
+
for (const [, elemTypes] of allTuples) {
|
|
714
|
+
for (const t of elemTypes) registerArray(t);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
for (const [clsName, elemTypes] of allTuples) {
|
|
718
|
+
lines.push(
|
|
719
|
+
`function ${prefix}Read_${clsName}(exports: Record<string, unknown>, ptr: number): readonly [${elemTypes.map(toTsType).join(", ")}] {`,
|
|
720
|
+
` return [`,
|
|
721
|
+
...elemTypes.map((t, fi) => {
|
|
722
|
+
const getter = `(exports.__fs_get_${clsName}_${fi} as (p: number) => any)(ptr)`;
|
|
723
|
+
if (BOOLEAN_TYPES.has(t)) return ` Boolean(${getter}),`;
|
|
724
|
+
if (t === "string")
|
|
725
|
+
return ` ${prefix}ReadString(exports, ${getter}),`;
|
|
726
|
+
if (TYPED_ARRAY_TYPES.has(t))
|
|
727
|
+
return ` ${prefix}Read(exports, ${getter}, ${toJsTypedArray(t)}),`;
|
|
728
|
+
if (classes.has(t))
|
|
729
|
+
return ` ${prefix}Read_${t}(exports, ${getter}),`;
|
|
730
|
+
const innerTuple = parseTupleElementTypeList(t);
|
|
731
|
+
if (innerTuple)
|
|
732
|
+
return ` ${prefix}Read_${tupleClassName(innerTuple)}(exports, ${getter}),`;
|
|
733
|
+
const elem = parseArrayElementType(t);
|
|
734
|
+
if (elem) {
|
|
735
|
+
const tupleInner = parseTupleElementTypeList(elem);
|
|
736
|
+
const safeElem =
|
|
737
|
+
elem === "boolean"
|
|
738
|
+
? "bool"
|
|
739
|
+
: tupleInner
|
|
740
|
+
? tupleClassName(tupleInner)
|
|
741
|
+
: elem;
|
|
742
|
+
return ` ${prefix}Read_Array_${safeElem}(exports, ${getter}),`;
|
|
743
|
+
}
|
|
744
|
+
if (t === "i64") return ` BigInt.asIntN(64, ${getter}),`;
|
|
745
|
+
if (t === "u64") return ` BigInt.asUintN(64, ${getter}),`;
|
|
746
|
+
if (t === "u32") return ` ((${getter}) >>> 0),`;
|
|
747
|
+
return ` ${getter},`;
|
|
748
|
+
}),
|
|
749
|
+
` ] as const;`,
|
|
750
|
+
`}`,
|
|
751
|
+
`function ${prefix}Alloc_${clsName}(exports: Record<string, unknown>, tup: readonly [${elemTypes.map(toTsType).join(", ")}]): number {`,
|
|
752
|
+
...elemTypes.map((t, fi) => {
|
|
753
|
+
if (BOOLEAN_TYPES.has(t))
|
|
754
|
+
return ` const __f${fi} = tup[${fi}] ? 1 : 0;`;
|
|
755
|
+
if (t === "string")
|
|
756
|
+
return ` const __f${fi} = ${prefix}AllocString(exports, tup[${fi}]);`;
|
|
757
|
+
if (TYPED_ARRAY_TYPES.has(t))
|
|
758
|
+
return ` const __h${fi} = ${prefix}Alloc(exports, tup[${fi}], ${toJsTypedArray(t)});\n const __f${fi} = __h${fi}.viewPtr;`;
|
|
759
|
+
if (classes.has(t))
|
|
760
|
+
return ` const __f${fi} = ${prefix}Alloc_${t}(exports, tup[${fi}]);`;
|
|
761
|
+
const innerTuple = parseTupleElementTypeList(t);
|
|
762
|
+
if (innerTuple)
|
|
763
|
+
return ` const __f${fi} = ${prefix}Alloc_${tupleClassName(innerTuple)}(exports, tup[${fi}]);`;
|
|
764
|
+
const elem = parseArrayElementType(t);
|
|
765
|
+
if (elem) {
|
|
766
|
+
const tupleInner = parseTupleElementTypeList(elem);
|
|
767
|
+
const safeElem =
|
|
768
|
+
elem === "boolean"
|
|
769
|
+
? "bool"
|
|
770
|
+
: tupleInner
|
|
771
|
+
? tupleClassName(tupleInner)
|
|
772
|
+
: elem;
|
|
773
|
+
return ` const __f${fi} = ${prefix}Alloc_Array_${safeElem}(exports, tup[${fi}]);`;
|
|
774
|
+
}
|
|
775
|
+
return ` const __f${fi} = tup[${fi}];`;
|
|
776
|
+
}),
|
|
777
|
+
` return (exports.__fs_make_${clsName} as (...args: any[]) => number)(${elemTypes.map((_, fi) => `__f${fi}`).join(", ")});`,
|
|
778
|
+
`}`,
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
for (const elem of arrayElemTypes) {
|
|
783
|
+
const tupleElems = parseTupleElementTypeList(elem);
|
|
784
|
+
const safeElem =
|
|
785
|
+
elem === "boolean"
|
|
786
|
+
? "bool"
|
|
787
|
+
: tupleElems
|
|
788
|
+
? tupleClassName(tupleElems)
|
|
789
|
+
: elem;
|
|
790
|
+
const tsElem = toTsType(elem);
|
|
791
|
+
const elemReadExpr = BOOLEAN_TYPES.has(elem)
|
|
792
|
+
? "Boolean(raw)"
|
|
793
|
+
: elem === "string"
|
|
794
|
+
? `${prefix}ReadString(exports, raw)`
|
|
795
|
+
: classes.has(elem)
|
|
796
|
+
? `${prefix}Read_${elem}(exports, raw)`
|
|
797
|
+
: tupleElems
|
|
798
|
+
? `${prefix}Read_${tupleClassName(tupleElems)}(exports, raw)`
|
|
799
|
+
: elem === "i64"
|
|
800
|
+
? "BigInt.asIntN(64, raw)"
|
|
801
|
+
: elem === "u64"
|
|
802
|
+
? "BigInt.asUintN(64, raw)"
|
|
803
|
+
: elem === "u32"
|
|
804
|
+
? "(raw >>> 0)"
|
|
805
|
+
: "raw";
|
|
806
|
+
|
|
807
|
+
const itemAllocExpr = BOOLEAN_TYPES.has(elem)
|
|
808
|
+
? ""
|
|
809
|
+
: elem === "string"
|
|
810
|
+
? `const __v = ${prefix}AllocString(exports, item);`
|
|
811
|
+
: classes.has(elem)
|
|
812
|
+
? `const __v = ${prefix}Alloc_${elem}(exports, item);`
|
|
813
|
+
: tupleElems
|
|
814
|
+
? `const __v = ${prefix}Alloc_${tupleClassName(tupleElems)}(exports, item);`
|
|
815
|
+
: "";
|
|
816
|
+
const itemVal = BOOLEAN_TYPES.has(elem)
|
|
817
|
+
? "item ? 1 : 0"
|
|
818
|
+
: elem === "string" || classes.has(elem) || tupleElems
|
|
819
|
+
? "__v"
|
|
820
|
+
: elem === "u32"
|
|
821
|
+
? "(item >>> 0)"
|
|
822
|
+
: "item";
|
|
823
|
+
|
|
824
|
+
lines.push(
|
|
825
|
+
`function ${prefix}Read_Array_${safeElem}(exports: Record<string, unknown>, ptr: number): ${tsElem}[] {`,
|
|
826
|
+
` if (!ptr) return [];`,
|
|
827
|
+
` const len = (exports.__fs_arr_len_${safeElem} as (p: number) => number)(ptr);`,
|
|
828
|
+
` const res: ${tsElem}[] = new Array(len);`,
|
|
829
|
+
` for (let i = 0; i < len; i++) {`,
|
|
830
|
+
` const raw = (exports.__fs_arr_get_${safeElem} as (p: number, i: number) => any)(ptr, i);`,
|
|
831
|
+
` res[i] = ${elemReadExpr};`,
|
|
832
|
+
` }`,
|
|
833
|
+
` return res;`,
|
|
834
|
+
`}`,
|
|
835
|
+
`function ${prefix}Alloc_Array_${safeElem}(exports: Record<string, unknown>, arr: readonly ${tsElem}[]): number {`,
|
|
836
|
+
` const len = arr.length;`,
|
|
837
|
+
` const arrPtr = (exports.__fs_arr_make_${safeElem} as (n: number) => number)(len);`,
|
|
838
|
+
` for (let i = 0; i < len; i++) {`,
|
|
839
|
+
` const item = arr[i];`,
|
|
840
|
+
` ${itemAllocExpr}`,
|
|
841
|
+
` (exports.__fs_arr_set_${safeElem} as (a: number, i: number, v: any) => void)(arrPtr, i, ${itemVal});`,
|
|
842
|
+
` }`,
|
|
843
|
+
` return arrPtr;`,
|
|
844
|
+
`}`,
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
for (const cls of classes.values()) {
|
|
849
|
+
lines.push(
|
|
850
|
+
`export type ${cls.name} = {`,
|
|
851
|
+
...cls.fields.map((f) => ` readonly ${f.name}: ${toTsType(f.type)};`),
|
|
852
|
+
`};`,
|
|
853
|
+
`function ${prefix}Read_${cls.name}(exports: Record<string, unknown>, ptr: number): ${cls.name} {`,
|
|
854
|
+
` return {`,
|
|
855
|
+
...cls.fields.map((f) => {
|
|
856
|
+
const getter = `(exports.__fs_get_${cls.name}_${f.name} as (p: number) => any)(ptr)`;
|
|
857
|
+
if (BOOLEAN_TYPES.has(f.type)) {
|
|
858
|
+
return ` ${f.name}: Boolean(${getter}),`;
|
|
859
|
+
}
|
|
860
|
+
if (f.type === "string") {
|
|
861
|
+
return ` ${f.name}: ${prefix}ReadString(exports, ${getter}),`;
|
|
862
|
+
}
|
|
863
|
+
if (TYPED_ARRAY_TYPES.has(f.type)) {
|
|
864
|
+
return ` ${f.name}: ${prefix}Read(exports, ${getter}, ${toJsTypedArray(f.type)}),`;
|
|
865
|
+
}
|
|
866
|
+
if (classes.has(f.type)) {
|
|
867
|
+
return ` ${f.name}: ${prefix}Read_${f.type}(exports, ${getter}),`;
|
|
868
|
+
}
|
|
869
|
+
const tupleElems = parseTupleElementTypeList(f.type);
|
|
870
|
+
if (tupleElems) {
|
|
871
|
+
return ` ${f.name}: ${prefix}Read_${tupleClassName(tupleElems)}(exports, ${getter}),`;
|
|
872
|
+
}
|
|
873
|
+
const elem = parseArrayElementType(f.type);
|
|
874
|
+
if (elem) {
|
|
875
|
+
const tupleInner = parseTupleElementTypeList(elem);
|
|
876
|
+
const safeElem =
|
|
877
|
+
elem === "boolean"
|
|
878
|
+
? "bool"
|
|
879
|
+
: tupleInner
|
|
880
|
+
? tupleClassName(tupleInner)
|
|
881
|
+
: elem;
|
|
882
|
+
return ` ${f.name}: ${prefix}Read_Array_${safeElem}(exports, ${getter}),`;
|
|
883
|
+
}
|
|
884
|
+
if (f.type === "i64") {
|
|
885
|
+
return ` ${f.name}: BigInt.asIntN(64, ${getter}),`;
|
|
886
|
+
}
|
|
887
|
+
if (f.type === "u64") {
|
|
888
|
+
return ` ${f.name}: BigInt.asUintN(64, ${getter}),`;
|
|
889
|
+
}
|
|
890
|
+
if (f.type === "u32") {
|
|
891
|
+
return ` ${f.name}: ((${getter}) >>> 0),`;
|
|
892
|
+
}
|
|
893
|
+
return ` ${f.name}: ${getter},`;
|
|
894
|
+
}),
|
|
895
|
+
` };`,
|
|
896
|
+
`}`,
|
|
897
|
+
`function ${prefix}Alloc_${cls.name}(exports: Record<string, unknown>, obj: ${cls.name}): number {`,
|
|
898
|
+
...cls.fields.map((f, fi) => {
|
|
899
|
+
if (BOOLEAN_TYPES.has(f.type)) {
|
|
900
|
+
return ` const __f${fi} = obj.${f.name} ? 1 : 0;`;
|
|
901
|
+
}
|
|
902
|
+
if (f.type === "string") {
|
|
903
|
+
return ` const __f${fi} = ${prefix}AllocString(exports, obj.${f.name});`;
|
|
904
|
+
}
|
|
905
|
+
if (TYPED_ARRAY_TYPES.has(f.type)) {
|
|
906
|
+
return ` const __h${fi} = ${prefix}Alloc(exports, obj.${f.name}, ${toJsTypedArray(f.type)});\n const __f${fi} = __h${fi}.viewPtr;`;
|
|
907
|
+
}
|
|
908
|
+
if (classes.has(f.type)) {
|
|
909
|
+
return ` const __f${fi} = ${prefix}Alloc_${f.type}(exports, obj.${f.name});`;
|
|
910
|
+
}
|
|
911
|
+
const tupleElems = parseTupleElementTypeList(f.type);
|
|
912
|
+
if (tupleElems) {
|
|
913
|
+
return ` const __f${fi} = ${prefix}Alloc_${tupleClassName(tupleElems)}(exports, obj.${f.name});`;
|
|
914
|
+
}
|
|
915
|
+
const elem = parseArrayElementType(f.type);
|
|
916
|
+
if (elem) {
|
|
917
|
+
const tupleInner = parseTupleElementTypeList(elem);
|
|
918
|
+
const safeElem =
|
|
919
|
+
elem === "boolean"
|
|
920
|
+
? "bool"
|
|
921
|
+
: tupleInner
|
|
922
|
+
? tupleClassName(tupleInner)
|
|
923
|
+
: elem;
|
|
924
|
+
return ` const __f${fi} = ${prefix}Alloc_Array_${safeElem}(exports, obj.${f.name});`;
|
|
925
|
+
}
|
|
926
|
+
return ` const __f${fi} = obj.${f.name};`;
|
|
927
|
+
}),
|
|
928
|
+
` return (exports.__fs_make_${cls.name} as (...args: any[]) => number)(${cls.fields.map((_, fi) => `__f${fi}`).join(", ")});`,
|
|
929
|
+
`}`,
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
const generateValidation = (
|
|
934
|
+
valExpr: string,
|
|
935
|
+
type: string,
|
|
936
|
+
fnName: string,
|
|
937
|
+
paramName: string,
|
|
938
|
+
): string => {
|
|
939
|
+
const handlePrefix = isHandleEligible(type)
|
|
940
|
+
? `if (typeof ${valExpr} === "object" && ${valExpr} !== null && (((${valExpr} as any)._tag === "WasmHandle") || (${valExpr} as any) instanceof ${prefix}WasmHandle)) {} else `
|
|
941
|
+
: "";
|
|
942
|
+
if (BOOLEAN_TYPES.has(type)) {
|
|
943
|
+
return `if (typeof ${valExpr} !== "boolean") return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: "boolean", value: ${valExpr} });`;
|
|
944
|
+
}
|
|
945
|
+
if (TYPED_ARRAY_TYPES.has(type)) {
|
|
946
|
+
const jsType = toJsTypedArray(type);
|
|
947
|
+
return `${handlePrefix}if (!(${valExpr} instanceof ${jsType})) return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: ${JSON.stringify(jsType)}, value: ${valExpr} });`;
|
|
948
|
+
}
|
|
949
|
+
if (type === "string") {
|
|
950
|
+
return `${handlePrefix}if (typeof ${valExpr} !== "string") return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: "string", value: ${valExpr} });`;
|
|
951
|
+
}
|
|
952
|
+
if (classes.has(type)) {
|
|
953
|
+
return `${handlePrefix}if (typeof ${valExpr} !== "object" || ${valExpr} === null) return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: ${JSON.stringify(type)}, value: ${valExpr} });`;
|
|
954
|
+
}
|
|
955
|
+
const elem = parseArrayElementType(type);
|
|
956
|
+
if (elem) {
|
|
957
|
+
return `${handlePrefix}if (!Array.isArray(${valExpr})) return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: "Array", value: ${valExpr} });`;
|
|
958
|
+
}
|
|
959
|
+
const tupleElems = parseTupleElementTypeList(type);
|
|
960
|
+
if (tupleElems) {
|
|
961
|
+
const head = `${handlePrefix}if (!Array.isArray(${valExpr}) || ${valExpr}.length !== ${tupleElems.length}) return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: ${JSON.stringify(type)}, value: ${valExpr} });`;
|
|
962
|
+
const checks = tupleElems
|
|
963
|
+
.map((te, idx) =>
|
|
964
|
+
generateValidation(
|
|
965
|
+
`${valExpr}[${idx}]`,
|
|
966
|
+
te,
|
|
967
|
+
fnName,
|
|
968
|
+
`${paramName}[${idx}]`,
|
|
969
|
+
),
|
|
970
|
+
)
|
|
971
|
+
.join(" ");
|
|
972
|
+
return `${head} ${checks}`;
|
|
973
|
+
}
|
|
974
|
+
const invalid =
|
|
975
|
+
type === "i64"
|
|
976
|
+
? `typeof ${valExpr} !== "bigint" || ${valExpr} < -9223372036854775808n || ${valExpr} > 9223372036854775807n`
|
|
977
|
+
: type === "u64"
|
|
978
|
+
? `typeof ${valExpr} !== "bigint" || ${valExpr} < 0n || ${valExpr} > 18446744073709551615n`
|
|
979
|
+
: type === "i32"
|
|
980
|
+
? `!Number.isInteger(${valExpr}) || ${valExpr} < -2147483648 || ${valExpr} > 2147483647`
|
|
981
|
+
: type === "u32"
|
|
982
|
+
? `!Number.isInteger(${valExpr}) || ${valExpr} < 0 || ${valExpr} > 4294967295`
|
|
983
|
+
: type === "f32"
|
|
984
|
+
? `!Number.isFinite(${valExpr}) || Math.abs(${valExpr}) > 3.4028234663852886e38`
|
|
985
|
+
: `!Number.isFinite(${valExpr})`;
|
|
986
|
+
return `if (${invalid}) return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: ${JSON.stringify(type)}, value: ${valExpr} });`;
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
for (const fn of signatures) {
|
|
990
|
+
const isBufferResult = TYPED_ARRAY_TYPES.has(fn.result);
|
|
991
|
+
const isStringResult = fn.result === "string";
|
|
992
|
+
const isClassResult = classes.has(fn.result);
|
|
993
|
+
const isArrayResult = parseArrayElementType(fn.result) !== null;
|
|
994
|
+
const isTupleResult = parseTupleElementTypeList(fn.result) !== null;
|
|
995
|
+
const isBooleanResult = BOOLEAN_TYPES.has(fn.result);
|
|
996
|
+
const isHandleResult =
|
|
997
|
+
isBufferResult ||
|
|
998
|
+
isStringResult ||
|
|
999
|
+
isClassResult ||
|
|
1000
|
+
isArrayResult ||
|
|
1001
|
+
isTupleResult;
|
|
1002
|
+
const tsReturnType = toTsType(fn.result);
|
|
1003
|
+
const rawReturnType = isHandleResult
|
|
1004
|
+
? `${prefix}WasmHandle<${tsReturnType}>`
|
|
1005
|
+
: tsReturnType;
|
|
1006
|
+
|
|
1007
|
+
const args = fn.params
|
|
1008
|
+
.map((p, i) => `arg${i}: ${toTsParamType(p.type)}`)
|
|
1009
|
+
.join(", ");
|
|
1010
|
+
const passedArgs = fn.params.map((_, i) => `arg${i}`).join(", ");
|
|
1011
|
+
|
|
1012
|
+
const hasBuffers =
|
|
1013
|
+
fn.params.some(
|
|
1014
|
+
(p) =>
|
|
1015
|
+
TYPED_ARRAY_TYPES.has(p.type) ||
|
|
1016
|
+
p.type === "string" ||
|
|
1017
|
+
classes.has(p.type) ||
|
|
1018
|
+
parseArrayElementType(p.type) !== null ||
|
|
1019
|
+
parseTupleElementTypeList(p.type) !== null,
|
|
1020
|
+
) ||
|
|
1021
|
+
isBufferResult ||
|
|
1022
|
+
isStringResult ||
|
|
1023
|
+
isClassResult ||
|
|
1024
|
+
isArrayResult ||
|
|
1025
|
+
isTupleResult ||
|
|
1026
|
+
decl.code.includes("Array") ||
|
|
1027
|
+
decl.code.includes("new ") ||
|
|
1028
|
+
decl.code.includes("v128");
|
|
1029
|
+
const wasmFnType =
|
|
1030
|
+
fn.result === "void"
|
|
1031
|
+
? "(...args: (number | bigint)[]) => void"
|
|
1032
|
+
: isBufferResult ||
|
|
1033
|
+
isStringResult ||
|
|
1034
|
+
isClassResult ||
|
|
1035
|
+
isArrayResult ||
|
|
1036
|
+
isTupleResult
|
|
1037
|
+
? "(...args: (number | bigint)[]) => number"
|
|
1038
|
+
: isBooleanResult
|
|
1039
|
+
? "(...args: (number | bigint)[]) => number"
|
|
1040
|
+
: BIGINT_SCALAR_TYPES.has(fn.result)
|
|
1041
|
+
? "(...args: (number | bigint)[]) => bigint"
|
|
1042
|
+
: `(...args: (number | bigint)[]) => number`;
|
|
1043
|
+
|
|
1044
|
+
const errorType = `{ readonly _tag: "WasmInputError"; readonly function: string; readonly parameter: string; readonly expected: string; readonly value: unknown } | { readonly _tag: "WasmTrapError"; readonly function: string; readonly cause: unknown } | { readonly _tag: "WasmInitializationError"; readonly function: string; readonly cause: unknown }`;
|
|
1045
|
+
|
|
1046
|
+
const validations = fn.params
|
|
1047
|
+
.map((param, i) =>
|
|
1048
|
+
generateValidation(`arg${i}`, param.type, fn.name, param.name),
|
|
1049
|
+
)
|
|
1050
|
+
.join(" ");
|
|
1051
|
+
|
|
1052
|
+
let syncBody: string;
|
|
1053
|
+
let effectTryBody: string;
|
|
1054
|
+
let rawBody: string;
|
|
1055
|
+
|
|
1056
|
+
const readExpr =
|
|
1057
|
+
fn.result === "void"
|
|
1058
|
+
? ""
|
|
1059
|
+
: isBooleanResult
|
|
1060
|
+
? `Boolean(__res)`
|
|
1061
|
+
: isBufferResult
|
|
1062
|
+
? `${prefix}Read(exports, __res, ${toJsTypedArray(fn.result)})`
|
|
1063
|
+
: isStringResult
|
|
1064
|
+
? `${prefix}ReadString(exports, __res)`
|
|
1065
|
+
: isClassResult
|
|
1066
|
+
? `${prefix}Read_${fn.result}(exports, __res)`
|
|
1067
|
+
: isTupleResult
|
|
1068
|
+
? `${prefix}Read_${tupleClassName(parseTupleElementTypeList(fn.result)!)}(exports, __res)`
|
|
1069
|
+
: isArrayResult
|
|
1070
|
+
? (() => {
|
|
1071
|
+
const elem = parseArrayElementType(fn.result)!;
|
|
1072
|
+
const tupleElems = parseTupleElementTypeList(elem);
|
|
1073
|
+
const safeElem =
|
|
1074
|
+
elem === "boolean"
|
|
1075
|
+
? "bool"
|
|
1076
|
+
: tupleElems
|
|
1077
|
+
? tupleClassName(tupleElems)
|
|
1078
|
+
: elem;
|
|
1079
|
+
return `${prefix}Read_Array_${safeElem}(exports, __res)`;
|
|
1080
|
+
})()
|
|
1081
|
+
: fn.result === "u32"
|
|
1082
|
+
? `((__res) >>> 0)`
|
|
1083
|
+
: fn.result === "u64"
|
|
1084
|
+
? `BigInt.asUintN(64, __res as bigint)`
|
|
1085
|
+
: fn.result === "i64"
|
|
1086
|
+
? `BigInt.asIntN(64, __res as bigint)`
|
|
1087
|
+
: `__res`;
|
|
1088
|
+
|
|
1089
|
+
if (!hasBuffers) {
|
|
1090
|
+
const passed = fn.params
|
|
1091
|
+
.map((p, i) =>
|
|
1092
|
+
BOOLEAN_TYPES.has(p.type) ? `(arg${i} ? 1 : 0)` : `arg${i}`,
|
|
1093
|
+
)
|
|
1094
|
+
.join(", ");
|
|
1095
|
+
const call = `(${prefix}Get()[${JSON.stringify(fn.name)}] as ${wasmFnType})(${passed})`;
|
|
1096
|
+
const effectCall = `(exports[${JSON.stringify(fn.name)}] as ${wasmFnType})(${passed})`;
|
|
1097
|
+
const raw =
|
|
1098
|
+
fn.result === "void"
|
|
1099
|
+
? `${call};`
|
|
1100
|
+
: isBooleanResult
|
|
1101
|
+
? `Boolean(${call})`
|
|
1102
|
+
: fn.result === "u32"
|
|
1103
|
+
? `(${call} as number >>> 0)`
|
|
1104
|
+
: fn.result === "u64"
|
|
1105
|
+
? `BigInt.asUintN(64, ${call} as bigint)`
|
|
1106
|
+
: fn.result === "i64"
|
|
1107
|
+
? `BigInt.asIntN(64, ${call} as bigint)`
|
|
1108
|
+
: call;
|
|
1109
|
+
const effectRaw =
|
|
1110
|
+
fn.result === "void"
|
|
1111
|
+
? `${effectCall};`
|
|
1112
|
+
: isBooleanResult
|
|
1113
|
+
? `Boolean(${effectCall})`
|
|
1114
|
+
: fn.result === "u32"
|
|
1115
|
+
? `(${effectCall} as number >>> 0)`
|
|
1116
|
+
: fn.result === "u64"
|
|
1117
|
+
? `BigInt.asUintN(64, ${effectCall} as bigint)`
|
|
1118
|
+
: fn.result === "i64"
|
|
1119
|
+
? `BigInt.asIntN(64, ${effectCall} as bigint)`
|
|
1120
|
+
: effectCall;
|
|
1121
|
+
syncBody = fn.result === "void" ? raw : `return ${raw};`;
|
|
1122
|
+
effectTryBody = fn.result === "void" ? effectRaw : `return ${effectRaw};`;
|
|
1123
|
+
rawBody = syncBody;
|
|
1124
|
+
} else {
|
|
1125
|
+
const allocs: string[] = [];
|
|
1126
|
+
const passedVars: string[] = [];
|
|
1127
|
+
const syncs: string[] = [];
|
|
1128
|
+
|
|
1129
|
+
fn.params.forEach((p, i) => {
|
|
1130
|
+
if (BOOLEAN_TYPES.has(p.type)) {
|
|
1131
|
+
passedVars.push(`(arg${i} ? 1 : 0)`);
|
|
1132
|
+
} else if (TYPED_ARRAY_TYPES.has(p.type)) {
|
|
1133
|
+
const jsType = toJsTypedArray(p.type);
|
|
1134
|
+
allocs.push(
|
|
1135
|
+
`const __isH${i} = Boolean(arg${i} && (arg${i} as any)._tag === "WasmHandle");`,
|
|
1136
|
+
`const __h${i} = __isH${i} ? { viewPtr: (arg${i} as any).ptr } : ${prefix}Alloc(exports, arg${i} as any, ${jsType});`,
|
|
1137
|
+
);
|
|
1138
|
+
passedVars.push(`__h${i}.viewPtr`);
|
|
1139
|
+
syncs.push(`if (!__isH${i}) ${prefix}Sync(exports, __h${i} as any);`);
|
|
1140
|
+
} else if (p.type === "string") {
|
|
1141
|
+
allocs.push(
|
|
1142
|
+
`const __h${i} = (arg${i} && (arg${i} as any)._tag === "WasmHandle") ? (arg${i} as any).ptr : ${prefix}AllocString(exports, arg${i} as any);`,
|
|
1143
|
+
);
|
|
1144
|
+
passedVars.push(`__h${i}`);
|
|
1145
|
+
} else if (classes.has(p.type)) {
|
|
1146
|
+
allocs.push(
|
|
1147
|
+
`const __h${i} = (arg${i} && (arg${i} as any)._tag === "WasmHandle") ? (arg${i} as any).ptr : ${prefix}Alloc_${p.type}(exports, arg${i} as any);`,
|
|
1148
|
+
);
|
|
1149
|
+
passedVars.push(`__h${i}`);
|
|
1150
|
+
} else if (parseTupleElementTypeList(p.type)) {
|
|
1151
|
+
const tupleElems = parseTupleElementTypeList(p.type)!;
|
|
1152
|
+
const clsName = tupleClassName(tupleElems);
|
|
1153
|
+
allocs.push(
|
|
1154
|
+
`const __h${i} = (arg${i} && (arg${i} as any)._tag === "WasmHandle") ? (arg${i} as any).ptr : ${prefix}Alloc_${clsName}(exports, arg${i} as any);`,
|
|
1155
|
+
);
|
|
1156
|
+
passedVars.push(`__h${i}`);
|
|
1157
|
+
} else if (parseArrayElementType(p.type)) {
|
|
1158
|
+
const elem = parseArrayElementType(p.type)!;
|
|
1159
|
+
const tupleElems = parseTupleElementTypeList(elem);
|
|
1160
|
+
const safeElem =
|
|
1161
|
+
elem === "boolean"
|
|
1162
|
+
? "bool"
|
|
1163
|
+
: tupleElems
|
|
1164
|
+
? tupleClassName(tupleElems)
|
|
1165
|
+
: elem;
|
|
1166
|
+
allocs.push(
|
|
1167
|
+
`const __h${i} = (arg${i} && (arg${i} as any)._tag === "WasmHandle") ? (arg${i} as any).ptr : ${prefix}Alloc_Array_${safeElem}(exports, arg${i} as any);`,
|
|
1168
|
+
);
|
|
1169
|
+
passedVars.push(`__h${i}`);
|
|
1170
|
+
} else {
|
|
1171
|
+
passedVars.push(`arg${i}`);
|
|
1172
|
+
}
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1175
|
+
const callExpr = `(exports[${JSON.stringify(fn.name)}] as ${wasmFnType})(${passedVars.join(", ")})`;
|
|
1176
|
+
const returnExpr =
|
|
1177
|
+
fn.result === "void"
|
|
1178
|
+
? `try { ${callExpr}; ${syncs.join(" ")} } finally { (exports.__funscript_reset_heap as () => void)(); } return;`
|
|
1179
|
+
: `try { const __res = ${callExpr} as any; ${syncs.join(" ")} return ${readExpr}; } finally { (exports.__funscript_reset_heap as () => void)(); }`;
|
|
1180
|
+
|
|
1181
|
+
const rawReturnExpr =
|
|
1182
|
+
fn.result === "void"
|
|
1183
|
+
? `${callExpr}; ${syncs.join(" ")} return;`
|
|
1184
|
+
: isHandleResult
|
|
1185
|
+
? `const __res = ${callExpr} as number; ${syncs.join(" ")} return new ${prefix}WasmHandle(__res, () => ${readExpr});`
|
|
1186
|
+
: `const __res = ${callExpr} as any; ${syncs.join(" ")} return ${readExpr};`;
|
|
1187
|
+
|
|
1188
|
+
syncBody = `const exports = ${prefix}Get(); ${allocs.join(" ")} ${returnExpr}`;
|
|
1189
|
+
effectTryBody = `${allocs.join(" ")} ${returnExpr}`;
|
|
1190
|
+
rawBody = `const exports = ${prefix}Get(); ${allocs.join(" ")} ${rawReturnExpr}`;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
lines.push(
|
|
1194
|
+
`const ${fn.name} = Object.assign(`,
|
|
1195
|
+
` (${args}): ${tsReturnType} => { ${syncBody} },`,
|
|
1196
|
+
` {`,
|
|
1197
|
+
` raw: (${args}): ${rawReturnType} => { ${rawBody} },`,
|
|
1198
|
+
` reset: (): void => { ((${prefix}Get()).__funscript_reset_heap as () => void)(); },`,
|
|
1199
|
+
` scoped: (${args}): Effect.Effect<${rawReturnType}, ${errorType}, Scope.Scope> =>`,
|
|
1200
|
+
` Effect.acquireRelease(`,
|
|
1201
|
+
` Effect.sync((): ${rawReturnType} => ${fn.name}.raw(${passedArgs})),`,
|
|
1202
|
+
` () => Effect.sync((): void => { ((${prefix}Get()).__funscript_reset_heap as () => void)(); })`,
|
|
1203
|
+
` ),`,
|
|
1204
|
+
` effect: (${args}): Effect.Effect<${tsReturnType}, ${errorType}> =>`,
|
|
1205
|
+
` Effect.suspend((): Effect.Effect<${tsReturnType}, ${errorType}> => {`,
|
|
1206
|
+
` ${validations}`,
|
|
1207
|
+
` return Effect.flatMap(`,
|
|
1208
|
+
` Effect.try({ try: ${prefix}Get, catch: (cause) => ({ _tag: "WasmInitializationError" as const, function: ${JSON.stringify(fn.name)}, cause }) }),`,
|
|
1209
|
+
` (exports) => Effect.try({`,
|
|
1210
|
+
` try: () => { ${effectTryBody} },`,
|
|
1211
|
+
` catch: (cause) => ({ _tag: "WasmTrapError" as const, function: ${JSON.stringify(fn.name)}, cause })`,
|
|
1212
|
+
` })`,
|
|
1213
|
+
` );`,
|
|
1214
|
+
` })`,
|
|
1215
|
+
` }`,
|
|
1216
|
+
`);`,
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
return lines.join("\n");
|
|
1220
|
+
}
|