xbintsc 0.3.4 → 0.3.5

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 (81) hide show
  1. package/dist/src/codegen/generator/calls/invocation.js +9 -0
  2. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  3. package/dist/src/codegen/generator/context.d.ts +15 -1
  4. package/dist/src/codegen/generator/context.js +29 -4
  5. package/dist/src/codegen/generator/context.js.map +1 -1
  6. package/dist/tests/ast/factory.test.d.ts +1 -0
  7. package/dist/tests/ast/factory.test.js +35 -0
  8. package/dist/tests/ast/factory.test.js.map +1 -0
  9. package/dist/tests/ast/visitor.test.d.ts +1 -0
  10. package/dist/tests/ast/visitor.test.js +53 -0
  11. package/dist/tests/ast/visitor.test.js.map +1 -0
  12. package/dist/tests/cli/main.test.js +109 -9
  13. package/dist/tests/cli/main.test.js.map +1 -1
  14. package/dist/tests/codegen/closures.test.d.ts +1 -0
  15. package/dist/tests/codegen/closures.test.js +32 -0
  16. package/dist/tests/codegen/closures.test.js.map +1 -0
  17. package/dist/tests/codegen/expressions.test.d.ts +1 -0
  18. package/dist/tests/codegen/expressions.test.js +149 -0
  19. package/dist/tests/codegen/expressions.test.js.map +1 -0
  20. package/dist/tests/codegen/llvm.test.js +14 -0
  21. package/dist/tests/codegen/llvm.test.js.map +1 -1
  22. package/dist/tests/codegen/values.test.d.ts +1 -0
  23. package/dist/tests/codegen/values.test.js +48 -0
  24. package/dist/tests/codegen/values.test.js.map +1 -0
  25. package/dist/tests/diagnostics/diagnostic.test.d.ts +1 -0
  26. package/dist/tests/diagnostics/diagnostic.test.js +117 -0
  27. package/dist/tests/diagnostics/diagnostic.test.js.map +1 -0
  28. package/dist/tests/diagnostics/source.test.d.ts +1 -0
  29. package/dist/tests/diagnostics/source.test.js +58 -0
  30. package/dist/tests/diagnostics/source.test.js.map +1 -0
  31. package/dist/tests/driver/modules.test.d.ts +1 -0
  32. package/dist/tests/driver/modules.test.js +140 -0
  33. package/dist/tests/driver/modules.test.js.map +1 -0
  34. package/dist/tests/driver/paths.test.js +17 -1
  35. package/dist/tests/driver/paths.test.js.map +1 -1
  36. package/dist/tests/driver/runtime-lib.test.d.ts +1 -0
  37. package/dist/tests/driver/runtime-lib.test.js +19 -0
  38. package/dist/tests/driver/runtime-lib.test.js.map +1 -0
  39. package/dist/tests/driver/toolchain-provider.test.js +91 -0
  40. package/dist/tests/driver/toolchain-provider.test.js.map +1 -1
  41. package/dist/tests/e2e/compile-node.test.js +11 -0
  42. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  43. package/dist/tests/extensions/node.test.d.ts +1 -0
  44. package/dist/tests/extensions/node.test.js +32 -0
  45. package/dist/tests/extensions/node.test.js.map +1 -0
  46. package/dist/tests/lexer/identifiers.test.d.ts +1 -0
  47. package/dist/tests/lexer/identifiers.test.js +13 -0
  48. package/dist/tests/lexer/identifiers.test.js.map +1 -0
  49. package/dist/tests/lexer/numbers.test.d.ts +1 -0
  50. package/dist/tests/lexer/numbers.test.js +23 -0
  51. package/dist/tests/lexer/numbers.test.js.map +1 -0
  52. package/dist/tests/lexer/strings.test.d.ts +1 -0
  53. package/dist/tests/lexer/strings.test.js +72 -0
  54. package/dist/tests/lexer/strings.test.js.map +1 -0
  55. package/dist/tests/parser/declarations.test.d.ts +1 -0
  56. package/dist/tests/parser/declarations.test.js +70 -0
  57. package/dist/tests/parser/declarations.test.js.map +1 -0
  58. package/dist/tests/parser/expressions.test.d.ts +1 -0
  59. package/dist/tests/parser/expressions.test.js +47 -0
  60. package/dist/tests/parser/expressions.test.js.map +1 -0
  61. package/dist/tests/parser/helpers.test.d.ts +1 -0
  62. package/dist/tests/parser/helpers.test.js +58 -0
  63. package/dist/tests/parser/helpers.test.js.map +1 -0
  64. package/dist/tests/parser/literals.test.d.ts +1 -0
  65. package/dist/tests/parser/literals.test.js +70 -0
  66. package/dist/tests/parser/literals.test.js.map +1 -0
  67. package/dist/tests/parser/module-syntax.test.d.ts +1 -0
  68. package/dist/tests/parser/module-syntax.test.js +91 -0
  69. package/dist/tests/parser/module-syntax.test.js.map +1 -0
  70. package/dist/tests/parser/statements.test.d.ts +1 -0
  71. package/dist/tests/parser/statements.test.js +85 -0
  72. package/dist/tests/parser/statements.test.js.map +1 -0
  73. package/dist/tests/parser/type-members.test.d.ts +1 -0
  74. package/dist/tests/parser/type-members.test.js +86 -0
  75. package/dist/tests/parser/type-members.test.js.map +1 -0
  76. package/dist/tests/parser/types.test.d.ts +1 -0
  77. package/dist/tests/parser/types.test.js +143 -0
  78. package/dist/tests/parser/types.test.js.map +1 -0
  79. package/package.json +1 -1
  80. package/src/codegen/generator/calls/invocation.ts +8 -0
  81. package/src/codegen/generator/context.ts +30 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xbintsc",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "xbintsc - a TypeScript binary compiler (TypeScript -> LLVM IR -> native binary)",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -206,6 +206,14 @@ export const invocationCallMethods: InvocationCallMethods = {
206
206
  return result;
207
207
  }
208
208
  }
209
+ // Modules with no namespace dispatcher (`fs`, `child_process`, ...): lower
210
+ // `import fs from "node:fs"; fs.readFileSync(...)` through the module's
211
+ // named exports, exactly like a named import of `readFileSync`.
212
+ const moduleExports = this.moduleExportsOfSymbol(targetSymbol);
213
+ if (moduleExports) {
214
+ const exported = moduleExports[method];
215
+ if (exported) return this.emitModuleExport(node, exported);
216
+ }
209
217
  }
210
218
 
211
219
  void BUILTIN_METHODS;
@@ -15,6 +15,7 @@ import type {
15
15
  BuiltinFunction,
16
16
  ExtensionModule,
17
17
  ModuleExport,
18
+ ModuleExports,
18
19
  } from "../../extensions/registry.js";
19
20
  import type { CodegenOptions, FunctionState } from "./state.js";
20
21
  import { escapeBytes, isErrorFamily, kindName, utf8Bytes } from "./tables.js";
@@ -29,6 +30,13 @@ export class GeneratorContext {
29
30
  readonly importExports = new Map<number, ModuleExport>();
30
31
  /** Imported symbol id -> the namespace it aliases (e.g. `path`). */
31
32
  readonly importNamespaces = new Map<number, string>();
33
+ /**
34
+ * Imported symbol id -> the named exports of the module it aliases, for
35
+ * modules that do not expose a runtime namespace dispatcher (`fs`,
36
+ * `child_process`, `crypto`, ...). Lets `import fs from "node:fs"` lower
37
+ * `fs.readFileSync(...)` straight to the module's runtime symbol.
38
+ */
39
+ readonly importModuleExports = new Map<number, ModuleExports>();
32
40
  readonly globals: string[] = [];
33
41
  readonly functions: string[] = [];
34
42
  readonly strings = new Map<string, { label: string; length: number }>();
@@ -61,9 +69,9 @@ export class GeneratorContext {
61
69
  if (!module) continue;
62
70
  const clause = declaration.importClause;
63
71
  if (!clause) continue;
64
- if (clause.name && module.namespace) {
72
+ if (clause.name) {
65
73
  const symbol = this.binding.symbolOfDeclaration.get(clause.name);
66
- if (symbol) this.importNamespaces.set(symbol.id, module.namespace);
74
+ if (symbol) this.bindModuleAlias(symbol, module);
67
75
  }
68
76
  const bindings = clause.namedBindings;
69
77
  if (bindings && bindings.kind === SyntaxKind.NamedImports) {
@@ -73,19 +81,37 @@ export class GeneratorContext {
73
81
  const symbol = this.binding.symbolOfDeclaration.get(specifier.name);
74
82
  if (symbol && exported) this.importExports.set(symbol.id, exported);
75
83
  }
76
- } else if (bindings && bindings.kind === SyntaxKind.NamespaceImport && module.namespace) {
84
+ } else if (bindings && bindings.kind === SyntaxKind.NamespaceImport) {
77
85
  const symbol = this.binding.symbolOfDeclaration.get(bindings.name);
78
- if (symbol) this.importNamespaces.set(symbol.id, module.namespace);
86
+ if (symbol) this.bindModuleAlias(symbol, module);
79
87
  }
80
88
  }
81
89
  }
82
90
 
91
+ /**
92
+ * Bind a default/namespace import to either the module's runtime namespace
93
+ * dispatcher (when it has one) or its named exports (when it does not).
94
+ */
95
+ private bindModuleAlias(symbol: SymbolInfo, module: ExtensionModule): void {
96
+ if (module.namespace) {
97
+ this.importNamespaces.set(symbol.id, module.namespace);
98
+ } else if (module.exports) {
99
+ this.importModuleExports.set(symbol.id, module.exports);
100
+ }
101
+ }
102
+
83
103
  /** Namespace name an imported alias refers to, if any. */
84
104
  namespaceOfSymbol(symbol: SymbolInfo | undefined): string | undefined {
85
105
  if (!symbol || symbol.kind !== SymbolKind.Import) return undefined;
86
106
  return this.importNamespaces.get(symbol.id);
87
107
  }
88
108
 
109
+ /** Named exports a default/namespace import aliases, if any. */
110
+ moduleExportsOfSymbol(symbol: SymbolInfo | undefined): ModuleExports | undefined {
111
+ if (!symbol || symbol.kind !== SymbolKind.Import) return undefined;
112
+ return this.importModuleExports.get(symbol.id);
113
+ }
114
+
89
115
  // -- emission primitives -------------------------------------------------
90
116
 
91
117
  emit(line: string): void {