@ugo-studio/jspp 0.1.3 → 0.1.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.
Files changed (69) hide show
  1. package/README.md +2 -2
  2. package/dist/analysis/scope.js +16 -4
  3. package/dist/analysis/typeAnalyzer.js +253 -20
  4. package/dist/ast/types.js +6 -0
  5. package/dist/cli.js +1 -2
  6. package/dist/core/codegen/class-handlers.js +127 -0
  7. package/dist/core/codegen/control-flow-handlers.js +464 -0
  8. package/dist/core/codegen/declaration-handlers.js +31 -14
  9. package/dist/core/codegen/expression-handlers.js +429 -117
  10. package/dist/core/codegen/function-handlers.js +91 -15
  11. package/dist/core/codegen/helpers.js +66 -2
  12. package/dist/core/codegen/index.js +17 -6
  13. package/dist/core/codegen/literal-handlers.js +3 -0
  14. package/dist/core/codegen/statement-handlers.js +133 -204
  15. package/dist/core/codegen/visitor.js +29 -3
  16. package/package.json +3 -3
  17. package/src/prelude/any_value.hpp +658 -634
  18. package/src/prelude/any_value_access.hpp +103 -0
  19. package/src/prelude/any_value_defines.hpp +151 -0
  20. package/src/prelude/any_value_helpers.hpp +246 -225
  21. package/src/prelude/exception.hpp +31 -0
  22. package/src/prelude/exception_helpers.hpp +49 -0
  23. package/src/prelude/index.hpp +18 -9
  24. package/src/prelude/library/console.hpp +13 -13
  25. package/src/prelude/library/error.hpp +111 -0
  26. package/src/prelude/library/global.hpp +15 -4
  27. package/src/prelude/library/performance.hpp +2 -2
  28. package/src/prelude/library/promise.hpp +121 -0
  29. package/src/prelude/library/symbol.hpp +3 -4
  30. package/src/prelude/library/timer.hpp +92 -0
  31. package/src/prelude/scheduler.hpp +145 -0
  32. package/src/prelude/types.hpp +10 -1
  33. package/src/prelude/utils/access.hpp +174 -0
  34. package/src/prelude/utils/log_any_value/array.hpp +245 -0
  35. package/src/prelude/utils/log_any_value/config.hpp +32 -0
  36. package/src/prelude/utils/log_any_value/function.hpp +37 -0
  37. package/src/prelude/utils/log_any_value/fwd.hpp +15 -0
  38. package/src/prelude/utils/log_any_value/helpers.hpp +62 -0
  39. package/src/prelude/utils/log_any_value/log_any_value.hpp +94 -0
  40. package/src/prelude/utils/log_any_value/object.hpp +119 -0
  41. package/src/prelude/utils/log_any_value/primitives.hpp +41 -0
  42. package/src/prelude/{operators.hpp → utils/operators.hpp} +29 -10
  43. package/src/prelude/{well_known_symbols.hpp → utils/well_known_symbols.hpp} +0 -1
  44. package/src/prelude/values/array.hpp +3 -2
  45. package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
  46. package/src/prelude/values/function.hpp +76 -51
  47. package/src/prelude/values/helpers/array.hpp +20 -11
  48. package/src/prelude/values/helpers/function.hpp +125 -77
  49. package/src/prelude/values/helpers/iterator.hpp +13 -7
  50. package/src/prelude/values/helpers/object.hpp +36 -6
  51. package/src/prelude/values/helpers/promise.hpp +181 -0
  52. package/src/prelude/values/helpers/string.hpp +3 -3
  53. package/src/prelude/values/helpers/symbol.hpp +2 -2
  54. package/src/prelude/values/iterator.hpp +13 -5
  55. package/src/prelude/values/object.hpp +6 -2
  56. package/src/prelude/values/promise.hpp +73 -0
  57. package/src/prelude/values/prototypes/array.hpp +16 -16
  58. package/src/prelude/values/prototypes/function.hpp +4 -4
  59. package/src/prelude/values/prototypes/iterator.hpp +11 -10
  60. package/src/prelude/values/prototypes/object.hpp +26 -0
  61. package/src/prelude/values/prototypes/promise.hpp +124 -0
  62. package/src/prelude/values/prototypes/string.hpp +26 -26
  63. package/src/prelude/values/prototypes/symbol.hpp +5 -3
  64. package/src/prelude/values/string.hpp +1 -1
  65. package/src/prelude/values/symbol.hpp +1 -1
  66. package/src/prelude/access.hpp +0 -91
  67. package/src/prelude/error.hpp +0 -31
  68. package/src/prelude/error_helpers.hpp +0 -59
  69. package/src/prelude/log_string.hpp +0 -407
@@ -1,20 +1,41 @@
1
1
  import ts from "typescript";
2
2
  import { CodeGenerator } from "./";
3
- export function generateLambda(node, isAssignment = false, capture = "[=]") {
3
+ export function generateLambda(node, context, options) {
4
+ const isAssignment = options?.isAssignment || false;
5
+ const capture = options?.capture || "[=]";
4
6
  const declaredSymbols = this.getDeclaredSymbols(node);
5
7
  const argsName = this.generateUniqueName("__args_", declaredSymbols);
6
8
  const isInsideGeneratorFunction = this.isGeneratorFunction(node);
7
- const returnCmd = this.getReturnCmd({
9
+ const isInsideAsyncFunction = this.isAsyncFunction(node);
10
+ const returnCmd = this.getReturnCommand({
8
11
  isInsideGeneratorFunction: isInsideGeneratorFunction,
12
+ isInsideAsyncFunction: isInsideAsyncFunction,
9
13
  });
10
14
  const funcReturnType = isInsideGeneratorFunction
11
15
  ? "jspp::JsIterator<jspp::AnyValue>"
12
- : "jspp::AnyValue";
13
- let lambda = `${capture}(const std::vector<jspp::AnyValue>& ${argsName}) mutable -> ${funcReturnType} `;
16
+ : (isInsideAsyncFunction ? "jspp::JsPromise" : "jspp::AnyValue");
17
+ const isArrow = ts.isArrowFunction(node);
18
+ // For generators and async functions, we MUST copy arguments because the coroutine suspends immediately
19
+ // and references to temporary arguments would dangle.
20
+ const paramThisType = (isInsideGeneratorFunction || isInsideAsyncFunction)
21
+ ? "jspp::AnyValue"
22
+ : "const jspp::AnyValue&";
23
+ const paramArgsType = (isInsideGeneratorFunction || isInsideAsyncFunction)
24
+ ? "std::vector<jspp::AnyValue>"
25
+ : "const std::vector<jspp::AnyValue>&";
26
+ const thisArgParam = isArrow
27
+ ? "const jspp::AnyValue&" // Arrow functions are never generators in this parser
28
+ : `${paramThisType} ${this.globalThisVar}`;
29
+ let lambda = `${capture}(${thisArgParam}, ${paramArgsType} ${argsName}) mutable -> ${funcReturnType} `;
30
+ const topLevelScopeSymbols = this.prepareScopeSymbolsForVisit(context.topLevelScopeSymbols, context.currentScopeSymbols);
14
31
  const visitContext = {
15
32
  isMainContext: false,
16
33
  isInsideFunction: true,
17
34
  isFunctionBody: false,
35
+ lambdaName: undefined,
36
+ topLevelScopeSymbols,
37
+ currentScopeSymbols: new Map(),
38
+ superClassVar: context.superClassVar,
18
39
  };
19
40
  if (node.body) {
20
41
  if (ts.isBlock(node.body)) {
@@ -25,15 +46,49 @@ export function generateLambda(node, isAssignment = false, capture = "[=]") {
25
46
  const defaultValue = p.initializer
26
47
  ? this.visit(p.initializer, visitContext)
27
48
  : "jspp::AnyValue::make_undefined()";
28
- paramExtraction +=
29
- `${this.indent()}auto ${name} = ${argsName}.size() > ${i} ? ${argsName}[${i}] : ${defaultValue};\n`;
49
+ if (!!p.dotDotDotToken) {
50
+ if (node.parameters.length - 1 !== i) {
51
+ throw new SyntaxError("Rest parameter must be last formal parameter.");
52
+ }
53
+ const tempName = `temp_${name}`;
54
+ paramExtraction +=
55
+ `${this.indent()}auto ${name} = jspp::AnyValue::make_undefined();\n`;
56
+ paramExtraction += `${this.indent()}{\n`;
57
+ this.indentationLevel++;
58
+ paramExtraction +=
59
+ `${this.indent()}std::vector<std::optional<jspp::AnyValue>> ${tempName};\n`;
60
+ paramExtraction +=
61
+ `${this.indent()}if (${argsName}.size() > ${i}) {\n`;
62
+ this.indentationLevel++;
63
+ paramExtraction +=
64
+ `${this.indent()}${tempName}.reserve(${argsName}.size() - ${i});\n`;
65
+ this.indentationLevel--;
66
+ paramExtraction += `${this.indent()}}\n`;
67
+ paramExtraction +=
68
+ `${this.indent()}for (size_t i = ${i}; i < ${argsName}.size(); i++) {\n`;
69
+ this.indentationLevel++;
70
+ paramExtraction +=
71
+ `${this.indent()}${tempName}.push_back(${argsName}[i]);\n`;
72
+ this.indentationLevel--;
73
+ paramExtraction += `${this.indent()}}\n`;
74
+ paramExtraction +=
75
+ `${this.indent()}${name} = jspp::AnyValue::make_array(std::move(${tempName}));\n`;
76
+ this.indentationLevel--;
77
+ paramExtraction += `${this.indent()}}\n`;
78
+ }
79
+ else {
80
+ paramExtraction +=
81
+ `${this.indent()}auto ${name} = ${argsName}.size() > ${i} ? ${argsName}[${i}] : ${defaultValue};\n`;
82
+ }
30
83
  });
31
84
  this.indentationLevel--;
32
85
  const blockContent = this.visit(node.body, {
86
+ ...visitContext,
33
87
  isMainContext: false,
34
88
  isInsideFunction: true,
35
89
  isFunctionBody: true,
36
90
  isInsideGeneratorFunction: isInsideGeneratorFunction,
91
+ isInsideAsyncFunction: isInsideAsyncFunction,
37
92
  });
38
93
  // The block visitor already adds braces, so we need to inject the param extraction.
39
94
  lambda += "{\n" + paramExtraction + blockContent.substring(2);
@@ -50,9 +105,12 @@ export function generateLambda(node, isAssignment = false, capture = "[=]") {
50
105
  `${this.indent()}auto ${name} = ${argsName}.size() > ${i} ? ${argsName}[${i}] : ${defaultValue};\n`;
51
106
  });
52
107
  lambda += `${this.indent()}${returnCmd} ${this.visit(node.body, {
108
+ ...visitContext,
53
109
  isMainContext: false,
54
110
  isInsideFunction: true,
55
111
  isFunctionBody: false,
112
+ isInsideGeneratorFunction: isInsideGeneratorFunction,
113
+ isInsideAsyncFunction: isInsideAsyncFunction,
56
114
  })};
57
115
  `;
58
116
  this.indentationLevel--;
@@ -68,17 +126,29 @@ export function generateLambda(node, isAssignment = false, capture = "[=]") {
68
126
  // Handle generator function
69
127
  if (isInsideGeneratorFunction) {
70
128
  signature =
71
- "jspp::JsIterator<jspp::AnyValue>(const std::vector<jspp::AnyValue>&)";
129
+ "jspp::JsIterator<jspp::AnyValue>(const jspp::AnyValue&, const std::vector<jspp::AnyValue>&)";
130
+ callable = `std::function<${signature}>(${lambda})`;
131
+ method = `jspp::AnyValue::make_generator`;
132
+ } // Handle async function
133
+ else if (isInsideAsyncFunction) {
134
+ signature =
135
+ "jspp::JsPromise(const jspp::AnyValue&, const std::vector<jspp::AnyValue>&)";
72
136
  callable = `std::function<${signature}>(${lambda})`;
73
- method = `jspp::AnyValue::make_function`;
137
+ method = `jspp::AnyValue::make_async_function`;
74
138
  } // Handle normal function
75
139
  else {
76
- signature = `jspp::AnyValue(const std::vector<jspp::AnyValue>&)`;
140
+ signature =
141
+ `jspp::AnyValue(const jspp::AnyValue&, const std::vector<jspp::AnyValue>&)`;
77
142
  callable = `std::function<${signature}>(${lambda})`;
78
- method = `jspp::AnyValue::make_function`;
143
+ if (options?.isClass) {
144
+ method = `jspp::AnyValue::make_class`;
145
+ }
146
+ else {
147
+ method = `jspp::AnyValue::make_function`;
148
+ }
79
149
  }
80
150
  const funcName = node.name?.getText();
81
- const fullExpression = `${method}(${callable}, "${funcName || ""}")`;
151
+ const fullExpression = `${method}(${callable}, "${context?.lambdaName || funcName || ""}")`;
82
152
  if (ts.isFunctionDeclaration(node) && !isAssignment && funcName) {
83
153
  return `${this.indent()}auto ${funcName} = ${fullExpression};\n`;
84
154
  }
@@ -89,12 +159,12 @@ export function visitFunctionDeclaration(node, context) {
89
159
  // This will now be handled by the Block visitor for hoisting.
90
160
  // However, we still need to generate the lambda for assignment.
91
161
  // The block visitor will wrap this in an assignment.
92
- return this.generateLambda(node);
162
+ return this.generateLambda(node, context);
93
163
  }
94
164
  return "";
95
165
  }
96
166
  export function visitArrowFunction(node, context) {
97
- return this.generateLambda(node);
167
+ return this.generateLambda(node, context);
98
168
  }
99
169
  export function visitFunctionExpression(node, context) {
100
170
  const funcExpr = node;
@@ -104,12 +174,18 @@ export function visitFunctionExpression(node, context) {
104
174
  this.indentationLevel++;
105
175
  code +=
106
176
  `${this.indent()}auto ${funcName} = std::make_shared<jspp::AnyValue>();\n`;
107
- const lambda = this.generateLambda(funcExpr, false, "[=]");
177
+ const lambda = this.generateLambda(funcExpr, {
178
+ ...context,
179
+ lambdaName: funcName,
180
+ }, {
181
+ isAssignment: true,
182
+ capture: "[=]",
183
+ });
108
184
  code += `${this.indent()}*${funcName} = ${lambda};\n`;
109
185
  code += `${this.indent()}return *${funcName};\n`;
110
186
  this.indentationLevel--;
111
187
  code += `${this.indent()}})()`;
112
188
  return code;
113
189
  }
114
- return this.generateLambda(node);
190
+ return this.generateLambda(node, context);
115
191
  }
@@ -1,5 +1,6 @@
1
1
  import ts from "typescript";
2
2
  import { BUILTIN_OBJECTS, Scope } from "../../analysis/scope";
3
+ import { DeclaredSymbolType } from "../../ast/types";
3
4
  import { CodeGenerator } from "./";
4
5
  export function isBuiltinObject(node) {
5
6
  return BUILTIN_OBJECTS.values().some((obj) => obj.name === node.text);
@@ -15,6 +16,10 @@ export function getDeclaredSymbols(node) {
15
16
  // Handles function declarations
16
17
  symbols.add(child.name.getText());
17
18
  }
19
+ else if (ts.isClassDeclaration(child) && child.name) {
20
+ // Handles class declarations
21
+ symbols.add(child.name.getText());
22
+ }
18
23
  else if (ts.isParameter(child)) {
19
24
  // Handles function parameters
20
25
  symbols.add(child.name.getText());
@@ -75,8 +80,55 @@ export function escapeString(str) {
75
80
  export function getJsVarName(node) {
76
81
  return `"${node.text}"`;
77
82
  }
78
- export function getReturnCmd(context) {
79
- return context.isInsideGeneratorFunction ? "co_return" : "return";
83
+ export function getDerefCode(nodeText, varName, typeInfo) {
84
+ // Make sure varName is incased in quotes
85
+ if (!varName.startsWith('"'))
86
+ varName = '"' + varName;
87
+ if (!varName.endsWith('"'))
88
+ varName = varName + '"';
89
+ if (typeInfo && typeInfo.needsHeapAllocation) {
90
+ return `jspp::Access::deref_ptr(${nodeText}, ${varName})`;
91
+ }
92
+ return `jspp::Access::deref_stack(${nodeText}, ${varName})`;
93
+ }
94
+ export function getReturnCommand(context) {
95
+ return (context.isInsideGeneratorFunction || context.isInsideAsyncFunction) ? "co_return" : "return";
96
+ }
97
+ export function hoistDeclaration(decl, hoistedSymbols) {
98
+ const name = decl.name?.getText();
99
+ if (!name) {
100
+ return `/* Unknown declaration name: ${ts.SyntaxKind[decl.kind]} */`;
101
+ }
102
+ const isLetOrConst = (decl.parent.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) !==
103
+ 0;
104
+ const symbolType = isLetOrConst
105
+ ? DeclaredSymbolType.letOrConst
106
+ : (ts.isFunctionDeclaration(decl)
107
+ ? DeclaredSymbolType.function
108
+ : (ts.isClassDeclaration(decl) ? DeclaredSymbolType.letOrConst : DeclaredSymbolType.var));
109
+ if (hoistedSymbols.has(name)) {
110
+ const existingType = hoistedSymbols.get(name);
111
+ // Don't allow multiple declaration of `letOrConst` or `function` or `class` variables
112
+ if (existingType === DeclaredSymbolType.letOrConst ||
113
+ existingType === DeclaredSymbolType.function ||
114
+ existingType !== symbolType) {
115
+ throw new SyntaxError(`Identifier '${name}' has already been declared`);
116
+ }
117
+ // `var` variables can be declared multiple times
118
+ return "";
119
+ }
120
+ hoistedSymbols.set(name, symbolType);
121
+ const scope = this.getScopeForNode(decl);
122
+ const typeInfo = this.typeAnalyzer.scopeManager.lookupFromScope(name, scope);
123
+ const initializer = isLetOrConst || ts.isClassDeclaration(decl)
124
+ ? "jspp::AnyValue::make_uninitialized()"
125
+ : "jspp::AnyValue::make_undefined()";
126
+ if (typeInfo.needsHeapAllocation) {
127
+ return `${this.indent()}auto ${name} = std::make_shared<jspp::AnyValue>(${initializer});\n`;
128
+ }
129
+ else {
130
+ return `${this.indent()}jspp::AnyValue ${name} = ${initializer};\n`;
131
+ }
80
132
  }
81
133
  export function isGeneratorFunction(node) {
82
134
  return ((ts.isFunctionDeclaration(node) ||
@@ -85,3 +137,15 @@ export function isGeneratorFunction(node) {
85
137
  !!node.asteriskToken // generator indicator
86
138
  );
87
139
  }
140
+ export function isAsyncFunction(node) {
141
+ return ((ts.isFunctionDeclaration(node) ||
142
+ ts.isFunctionExpression(node) ||
143
+ ts.isMethodDeclaration(node) ||
144
+ ts.isArrowFunction(node)) &&
145
+ (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Async) !== 0);
146
+ }
147
+ export function prepareScopeSymbolsForVisit(topLevel, local) {
148
+ const newTopLevel = new Map(topLevel);
149
+ local.forEach((v, k) => newTopLevel.set(k, v));
150
+ return newTopLevel;
151
+ }
@@ -1,11 +1,12 @@
1
1
  import { TypeAnalyzer } from "../../analysis/typeAnalyzer";
2
2
  import { generateLambda } from "./function-handlers";
3
- import { escapeString, generateUniqueExceptionName, generateUniqueName, getDeclaredSymbols, getJsVarName, getReturnCmd, getScopeForNode, indent, isBuiltinObject, isGeneratorFunction, } from "./helpers";
3
+ import { escapeString, generateUniqueExceptionName, generateUniqueName, getDeclaredSymbols, getDerefCode, getJsVarName, getReturnCommand, getScopeForNode, hoistDeclaration, indent, isAsyncFunction, isBuiltinObject, isGeneratorFunction, prepareScopeSymbolsForVisit, } from "./helpers";
4
4
  import { visit } from "./visitor";
5
5
  const CONTAINER_FUNCTION_NAME = "__container__";
6
6
  export class CodeGenerator {
7
7
  indentationLevel = 0;
8
8
  typeAnalyzer;
9
+ globalThisVar;
9
10
  exceptionCounter = 0;
10
11
  // visitor
11
12
  visit = visit;
@@ -13,13 +14,17 @@ export class CodeGenerator {
13
14
  getDeclaredSymbols = getDeclaredSymbols;
14
15
  generateUniqueName = generateUniqueName;
15
16
  generateUniqueExceptionName = generateUniqueExceptionName;
17
+ hoistDeclaration = hoistDeclaration;
16
18
  getScopeForNode = getScopeForNode;
17
19
  indent = indent;
18
20
  escapeString = escapeString;
19
21
  getJsVarName = getJsVarName;
20
- getReturnCmd = getReturnCmd;
22
+ getDerefCode = getDerefCode;
23
+ getReturnCommand = getReturnCommand;
21
24
  isBuiltinObject = isBuiltinObject;
22
25
  isGeneratorFunction = isGeneratorFunction;
26
+ isAsyncFunction = isAsyncFunction;
27
+ prepareScopeSymbolsForVisit = prepareScopeSymbolsForVisit;
23
28
  // function handlers
24
29
  generateLambda = generateLambda;
25
30
  /**
@@ -27,27 +32,33 @@ export class CodeGenerator {
27
32
  */
28
33
  generate(ast, analyzer) {
29
34
  this.typeAnalyzer = analyzer;
35
+ this.globalThisVar = this.generateUniqueName("__this_val__", this.getDeclaredSymbols(ast));
30
36
  const declarations = `#include "index.hpp"\n\n`;
31
37
  let containerCode = `jspp::AnyValue ${CONTAINER_FUNCTION_NAME}() {\n`;
32
38
  this.indentationLevel++;
39
+ containerCode +=
40
+ `${this.indent()}jspp::AnyValue ${this.globalThisVar} = global;\n`;
33
41
  containerCode += this.visit(ast, {
34
42
  isMainContext: true,
35
43
  isInsideFunction: true,
36
44
  isFunctionBody: true,
45
+ topLevelScopeSymbols: new Map(),
46
+ currentScopeSymbols: new Map(),
37
47
  });
38
48
  this.indentationLevel--;
39
49
  containerCode += " return jspp::AnyValue::make_undefined();\n";
40
50
  containerCode += "}\n\n";
41
51
  let mainCode = "int main() {\n";
42
- mainCode += ` std::ios::sync_with_stdio(false);\n`;
43
- mainCode += ` std::cin.tie(nullptr);\n`;
52
+ // std::ios::sync_with_stdio(false); // Removed to fix console output buffering
53
+ // std::cin.tie(nullptr); // Removed to fix console output buffering
44
54
  mainCode += ` try {\n`;
45
55
  mainCode += ` ${CONTAINER_FUNCTION_NAME}();\n`;
56
+ mainCode += ` jspp::Scheduler::instance().run();\n`;
46
57
  mainCode += ` } catch (const std::exception& ex) {\n`;
47
58
  mainCode +=
48
- " auto error = std::make_shared<jspp::AnyValue>(jspp::RuntimeError::error_to_value(ex));\n{\n";
59
+ " auto error = std::make_shared<jspp::AnyValue>(jspp::Exception::exception_to_any_value(ex));\n{\n";
49
60
  mainCode +=
50
- ` console.get_own_property("error").as_function("console.error")->call({*error});\n`;
61
+ ` ([&](){ auto __obj = console; return __obj.get_own_property("error").as_function("console.error")->call(__obj, {*error}); })();\n`;
51
62
  mainCode += ` return 1;\n}\n`;
52
63
  mainCode += ` }\n`;
53
64
  mainCode += " return 0;\n}";
@@ -30,3 +30,6 @@ export function visitUndefinedKeyword() {
30
30
  export function visitNullKeyword() {
31
31
  return "jspp::AnyValue::make_null()";
32
32
  }
33
+ export function visitThisKeyword() {
34
+ return `${this.globalThisVar}`;
35
+ }