@ugo-studio/jspp 0.2.4 → 0.2.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.
- package/dist/analysis/typeAnalyzer.js +32 -0
- package/dist/ast/symbols.js +57 -15
- package/dist/cli.js +1 -1
- package/dist/core/codegen/control-flow-handlers.js +139 -51
- package/dist/core/codegen/declaration-handlers.js +37 -12
- package/dist/core/codegen/expression-handlers.js +38 -3
- package/dist/core/codegen/function-handlers.js +39 -56
- package/dist/core/codegen/helpers.js +169 -31
- package/dist/core/codegen/index.js +8 -4
- package/dist/core/codegen/statement-handlers.js +78 -35
- package/dist/core/parser.js +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/src/prelude/utils/access.hpp +2 -2
- package/src/prelude/utils/log_any_value/primitives.hpp +7 -0
- package/src/prelude/utils/operators.hpp +4 -6
- package/src/prelude/values/prototypes/function.hpp +18 -0
|
@@ -205,6 +205,15 @@ export class TypeAnalyzer {
|
|
|
205
205
|
this.functionTypeInfo.set(node, funcType);
|
|
206
206
|
this.scopeManager.enterScope(node);
|
|
207
207
|
this.nodeToScope.set(node, this.scopeManager.currentScope);
|
|
208
|
+
// Catch invalid parameters
|
|
209
|
+
node.parameters.forEach((p) => {
|
|
210
|
+
if (p.getText() == "this") {
|
|
211
|
+
const sourceFile = node.getSourceFile();
|
|
212
|
+
const { line, character } = sourceFile
|
|
213
|
+
.getLineAndCharacterOfPosition(p.getStart());
|
|
214
|
+
throw new SyntaxError(`Cannot use 'this' as a parameter name.\n\n${" ".repeat(6)}at ${sourceFile.fileName}:${line + 1}:${character + 1}\n`);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
208
217
|
// Define parameters in the new scope
|
|
209
218
|
node.parameters.forEach((p) => this.scopeManager.define(p.name.getText(), {
|
|
210
219
|
type: "auto",
|
|
@@ -238,6 +247,15 @@ export class TypeAnalyzer {
|
|
|
238
247
|
if (node.name) {
|
|
239
248
|
this.scopeManager.define(node.name.getText(), funcType);
|
|
240
249
|
}
|
|
250
|
+
// Catch invalid parameters
|
|
251
|
+
node.parameters.forEach((p) => {
|
|
252
|
+
if (p.getText() == "this") {
|
|
253
|
+
const sourceFile = node.getSourceFile();
|
|
254
|
+
const { line, character } = sourceFile
|
|
255
|
+
.getLineAndCharacterOfPosition(p.getStart());
|
|
256
|
+
throw new SyntaxError(`Cannot use 'this' as a parameter name.\n\n${" ".repeat(6)}at ${sourceFile.fileName}:${line + 1}:${character + 1}\n`);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
241
259
|
// Define parameters in the new scope
|
|
242
260
|
node.parameters.forEach((p) => this.scopeManager.define(p.name.getText(), {
|
|
243
261
|
type: "auto",
|
|
@@ -270,6 +288,15 @@ export class TypeAnalyzer {
|
|
|
270
288
|
this.scopeManager.define(funcName, funcType);
|
|
271
289
|
this.functionTypeInfo.set(node, funcType);
|
|
272
290
|
}
|
|
291
|
+
// Catch invalid parameters
|
|
292
|
+
node.parameters.forEach((p) => {
|
|
293
|
+
if (p.getText() == "this") {
|
|
294
|
+
const sourceFile = node.getSourceFile();
|
|
295
|
+
const { line, character } = sourceFile
|
|
296
|
+
.getLineAndCharacterOfPosition(p.getStart());
|
|
297
|
+
throw new SyntaxError(`Cannot use 'this' as a parameter name.\n\n${" ".repeat(6)}at ${sourceFile.fileName}:${line + 1}:${character + 1}\n`);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
273
300
|
this.scopeManager.enterScope(node);
|
|
274
301
|
this.nodeToScope.set(node, this.scopeManager.currentScope);
|
|
275
302
|
// Define parameters in the new scope
|
|
@@ -385,6 +412,11 @@ export class TypeAnalyzer {
|
|
|
385
412
|
type = "function";
|
|
386
413
|
needsHeap = true;
|
|
387
414
|
}
|
|
415
|
+
else if (ts.isIdentifier(node.initializer)) {
|
|
416
|
+
const typeInfo = this.scopeManager.lookup(node.initializer.text);
|
|
417
|
+
needsHeap = typeInfo?.needsHeapAllocation ??
|
|
418
|
+
false;
|
|
419
|
+
}
|
|
388
420
|
}
|
|
389
421
|
const typeInfo = {
|
|
390
422
|
type,
|
package/dist/ast/symbols.js
CHANGED
|
@@ -1,32 +1,74 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export var DeclarationType;
|
|
2
|
+
(function (DeclarationType) {
|
|
3
|
+
DeclarationType["var"] = "var";
|
|
4
|
+
DeclarationType["let"] = "let";
|
|
5
|
+
DeclarationType["const"] = "const";
|
|
6
|
+
DeclarationType["function"] = "function";
|
|
7
|
+
DeclarationType["class"] = "class";
|
|
8
|
+
})(DeclarationType || (DeclarationType = {}));
|
|
9
|
+
export class DeclaredSymbol {
|
|
10
|
+
type;
|
|
11
|
+
checked;
|
|
12
|
+
func;
|
|
13
|
+
constructor(type) {
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.checked = {
|
|
16
|
+
initialized: false,
|
|
17
|
+
};
|
|
18
|
+
this.func = null;
|
|
19
|
+
}
|
|
20
|
+
get isMutable() {
|
|
21
|
+
return this.type === DeclarationType.let ||
|
|
22
|
+
this.type === DeclarationType.var;
|
|
23
|
+
}
|
|
24
|
+
updateChecked(update) {
|
|
25
|
+
this.checked = {
|
|
26
|
+
...this.checked,
|
|
27
|
+
...update,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
updateFunc(update) {
|
|
31
|
+
this.func = update
|
|
32
|
+
? {
|
|
33
|
+
...this.func,
|
|
34
|
+
...update,
|
|
35
|
+
}
|
|
36
|
+
: null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
7
39
|
export class DeclaredSymbols {
|
|
8
40
|
symbols;
|
|
9
41
|
constructor(...m) {
|
|
10
42
|
this.symbols = new Map();
|
|
11
43
|
m.forEach((ds) => ds.symbols.forEach((v, k) => this.symbols.set(k, v)));
|
|
12
44
|
}
|
|
45
|
+
get names() {
|
|
46
|
+
return new Set(this.symbols.keys());
|
|
47
|
+
}
|
|
13
48
|
has(name) {
|
|
14
49
|
return this.symbols.has(name);
|
|
15
50
|
}
|
|
16
51
|
get(name) {
|
|
17
52
|
return this.symbols.get(name);
|
|
18
53
|
}
|
|
19
|
-
|
|
20
|
-
|
|
54
|
+
add(name, value) {
|
|
55
|
+
const sym = new DeclaredSymbol(value.type);
|
|
56
|
+
if (value.checked !== undefined)
|
|
57
|
+
sym.updateChecked(value.checked);
|
|
58
|
+
if (value.func !== undefined)
|
|
59
|
+
sym.updateFunc(value.func);
|
|
60
|
+
return this.symbols.set(name, sym);
|
|
21
61
|
}
|
|
22
62
|
update(name, update) {
|
|
23
|
-
const
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
63
|
+
const sym = this.get(name);
|
|
64
|
+
if (sym) {
|
|
65
|
+
if (update.type !== undefined)
|
|
66
|
+
sym.type = update.type;
|
|
67
|
+
if (update.checked !== undefined)
|
|
68
|
+
sym.updateChecked(update.checked);
|
|
69
|
+
if (update.func !== undefined)
|
|
70
|
+
sym.updateFunc(update.func);
|
|
71
|
+
return this.symbols.set(name, sym);
|
|
27
72
|
}
|
|
28
73
|
}
|
|
29
|
-
toSet() {
|
|
30
|
-
return new Set(this.symbols.keys());
|
|
31
|
-
}
|
|
32
74
|
}
|
package/dist/cli.js
CHANGED
|
@@ -41,7 +41,7 @@ async function main() {
|
|
|
41
41
|
const jsCode = await fs.readFile(jsFilePath, "utf-8");
|
|
42
42
|
spinner.update("Transpiling to C++...");
|
|
43
43
|
const interpreter = new Interpreter();
|
|
44
|
-
const { cppCode, preludePath } = interpreter.interpret(jsCode);
|
|
44
|
+
const { cppCode, preludePath } = interpreter.interpret(jsCode, jsFilePath);
|
|
45
45
|
// Ensure directory for cpp file exists (should exist as it's source dir, but for safety if we change logic)
|
|
46
46
|
await fs.mkdir(path.dirname(cppFilePath), { recursive: true });
|
|
47
47
|
await fs.writeFile(cppFilePath, cppCode);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import {
|
|
2
|
+
import { DeclarationType, DeclaredSymbols } from "../../ast/symbols.js";
|
|
3
3
|
import { CodeGenerator } from "./index.js";
|
|
4
4
|
export function visitForStatement(node, context) {
|
|
5
5
|
const forStmt = node;
|
|
@@ -11,9 +11,14 @@ export function visitForStatement(node, context) {
|
|
|
11
11
|
this.indentationLevel++; // Enter a new scope for the for loop
|
|
12
12
|
// Handle initializer
|
|
13
13
|
let initializerCode = "";
|
|
14
|
-
|
|
14
|
+
const conditionContext = {
|
|
15
15
|
...context,
|
|
16
|
-
|
|
16
|
+
globalScopeSymbols: this.prepareScopeSymbolsForVisit(context.globalScopeSymbols, context.localScopeSymbols),
|
|
17
|
+
};
|
|
18
|
+
const statementContext = {
|
|
19
|
+
...context,
|
|
20
|
+
currentLabel: undefined,
|
|
21
|
+
isFunctionBody: false,
|
|
17
22
|
};
|
|
18
23
|
if (forStmt.initializer) {
|
|
19
24
|
if (ts.isVariableDeclarationList(forStmt.initializer)) {
|
|
@@ -32,9 +37,18 @@ export function visitForStatement(node, context) {
|
|
|
32
37
|
const typeInfo = this.typeAnalyzer.scopeManager
|
|
33
38
|
.lookupFromScope(name, scope);
|
|
34
39
|
conditionContext.localScopeSymbols = new DeclaredSymbols();
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
const declType = (varDeclList.flags &
|
|
41
|
+
(ts.NodeFlags.Let)) !==
|
|
42
|
+
0
|
|
43
|
+
? DeclarationType.let
|
|
44
|
+
: DeclarationType.const;
|
|
45
|
+
conditionContext.localScopeSymbols.add(name, {
|
|
46
|
+
type: declType,
|
|
47
|
+
checked: { initialized: true },
|
|
48
|
+
});
|
|
49
|
+
statementContext.localScopeSymbols.add(name, {
|
|
50
|
+
type: declType,
|
|
51
|
+
checked: { initialized: true },
|
|
38
52
|
});
|
|
39
53
|
if (typeInfo.needsHeapAllocation) {
|
|
40
54
|
initializerCode =
|
|
@@ -59,18 +73,15 @@ export function visitForStatement(node, context) {
|
|
|
59
73
|
}
|
|
60
74
|
code += `${this.indent()}for (${initializerCode}; `;
|
|
61
75
|
if (forStmt.condition) {
|
|
62
|
-
code += `is_truthy(${this.visit(forStmt.condition, conditionContext)})`;
|
|
76
|
+
code += `jspp::is_truthy(${this.visit(forStmt.condition, conditionContext)})`;
|
|
63
77
|
}
|
|
64
78
|
code += "; ";
|
|
65
79
|
if (forStmt.incrementor) {
|
|
66
80
|
code += this.visit(forStmt.incrementor, context);
|
|
67
81
|
}
|
|
68
82
|
code += ") ";
|
|
69
|
-
const statementCode = this.visit(forStmt.statement,
|
|
70
|
-
|
|
71
|
-
currentLabel: undefined,
|
|
72
|
-
isFunctionBody: false,
|
|
73
|
-
}).trim();
|
|
83
|
+
const statementCode = this.visit(forStmt.statement, statementContext)
|
|
84
|
+
.trim();
|
|
74
85
|
if (ts.isBlock(node.statement)) {
|
|
75
86
|
let blockContent = statementCode.substring(1, statementCode.length - 2); // remove curly braces
|
|
76
87
|
if (context.currentLabel) {
|
|
@@ -240,7 +251,7 @@ export function visitForOfStatement(node, context) {
|
|
|
240
251
|
`${this.indent()}auto ${nextRes} = ${nextFunc}.call(${iterator}, {}, "next");\n`;
|
|
241
252
|
}
|
|
242
253
|
code +=
|
|
243
|
-
`${this.indent()}while (!is_truthy(${nextRes}.get_own_property("done"))) {\n`;
|
|
254
|
+
`${this.indent()}while (!jspp::is_truthy(${nextRes}.get_own_property("done"))) {\n`;
|
|
244
255
|
this.indentationLevel++;
|
|
245
256
|
code +=
|
|
246
257
|
`${this.indent()}${assignmentTarget} = ${nextRes}.get_own_property("value");\n`;
|
|
@@ -277,7 +288,7 @@ export function visitWhileStatement(node, context) {
|
|
|
277
288
|
const conditionText = condition.kind === ts.SyntaxKind.TrueKeyword ||
|
|
278
289
|
condition.kind === ts.SyntaxKind.FalseKeyword
|
|
279
290
|
? condition.getText()
|
|
280
|
-
: `is_truthy(${this.visit(condition, context)})`;
|
|
291
|
+
: `jspp::is_truthy(${this.visit(condition, context)})`;
|
|
281
292
|
let code = "";
|
|
282
293
|
if (context.currentLabel) {
|
|
283
294
|
code += `${this.indent()}${context.currentLabel}: {\n`;
|
|
@@ -317,7 +328,7 @@ export function visitWhileStatement(node, context) {
|
|
|
317
328
|
}
|
|
318
329
|
export function visitDoStatement(node, context) {
|
|
319
330
|
const condition = node.expression;
|
|
320
|
-
const conditionText = `is_truthy(${this.visit(condition, context)})`;
|
|
331
|
+
const conditionText = `jspp::is_truthy(${this.visit(condition, context)})`;
|
|
321
332
|
let code = "";
|
|
322
333
|
if (context.currentLabel) {
|
|
323
334
|
code += `${this.indent()}${context.currentLabel}: {\n`;
|
|
@@ -358,6 +369,7 @@ export function visitDoStatement(node, context) {
|
|
|
358
369
|
}
|
|
359
370
|
export function visitSwitchStatement(node, context) {
|
|
360
371
|
const switchStmt = node;
|
|
372
|
+
context.currentScopeNode = node; // Update scope node
|
|
361
373
|
let code = "";
|
|
362
374
|
const declaredSymbols = this.getDeclaredSymbols(switchStmt.caseBlock);
|
|
363
375
|
const switchBreakLabel = this.generateUniqueName("__switch_break_", declaredSymbols);
|
|
@@ -374,25 +386,75 @@ export function visitSwitchStatement(node, context) {
|
|
|
374
386
|
code +=
|
|
375
387
|
`${this.indent()}const jspp::AnyValue ${switchValueVar} = ${expressionCode};\n`;
|
|
376
388
|
code += `${this.indent()}bool ${fallthroughVar} = false;\n`;
|
|
377
|
-
//
|
|
378
|
-
const
|
|
389
|
+
// Collect declarations from all clauses
|
|
390
|
+
const funcDecls = [];
|
|
391
|
+
const classDecls = [];
|
|
392
|
+
const blockScopedDecls = [];
|
|
379
393
|
for (const clause of switchStmt.caseBlock.clauses) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
394
|
+
for (const stmt of clause.statements) {
|
|
395
|
+
if (ts.isFunctionDeclaration(stmt)) {
|
|
396
|
+
funcDecls.push(stmt);
|
|
397
|
+
}
|
|
398
|
+
else if (ts.isClassDeclaration(stmt)) {
|
|
399
|
+
classDecls.push(stmt);
|
|
400
|
+
}
|
|
401
|
+
else if (ts.isVariableStatement(stmt)) {
|
|
402
|
+
const isLetOrConst = (stmt.declarationList.flags &
|
|
403
|
+
(ts.NodeFlags.Let | ts.NodeFlags.Const)) !== 0;
|
|
404
|
+
if (isLetOrConst) {
|
|
405
|
+
blockScopedDecls.push(...stmt.declarationList.declarations);
|
|
390
406
|
}
|
|
391
407
|
}
|
|
392
408
|
}
|
|
393
409
|
}
|
|
394
|
-
|
|
395
|
-
|
|
410
|
+
const hoistedSymbols = new DeclaredSymbols();
|
|
411
|
+
// 1. Hoist function declarations
|
|
412
|
+
funcDecls.forEach((func) => {
|
|
413
|
+
code += this.hoistDeclaration(func, hoistedSymbols, node);
|
|
414
|
+
});
|
|
415
|
+
// 2. Hoist class declarations
|
|
416
|
+
classDecls.forEach((cls) => {
|
|
417
|
+
code += this.hoistDeclaration(cls, hoistedSymbols, node);
|
|
418
|
+
});
|
|
419
|
+
// 3. Hoist variable declarations (let/const only)
|
|
420
|
+
blockScopedDecls.forEach((decl) => {
|
|
421
|
+
code += this.hoistDeclaration(decl, hoistedSymbols, node);
|
|
422
|
+
});
|
|
423
|
+
// Compile symbols for other statements
|
|
424
|
+
const globalScopeSymbols = this.prepareScopeSymbolsForVisit(context.globalScopeSymbols, context.localScopeSymbols);
|
|
425
|
+
const localScopeSymbols = new DeclaredSymbols(hoistedSymbols);
|
|
426
|
+
// 4. Assign hoisted functions (Optimization)
|
|
427
|
+
const contextForFunctions = {
|
|
428
|
+
...context,
|
|
429
|
+
localScopeSymbols: new DeclaredSymbols(context.localScopeSymbols, hoistedSymbols),
|
|
430
|
+
};
|
|
431
|
+
funcDecls.forEach((stmt) => {
|
|
432
|
+
const funcName = stmt.name?.getText();
|
|
433
|
+
if (!funcName)
|
|
434
|
+
return;
|
|
435
|
+
const symbol = hoistedSymbols.get(funcName);
|
|
436
|
+
if (!symbol)
|
|
437
|
+
return;
|
|
438
|
+
// Mark initialized
|
|
439
|
+
this.markSymbolAsInitialized(funcName, contextForFunctions.globalScopeSymbols, contextForFunctions.localScopeSymbols);
|
|
440
|
+
this.markSymbolAsInitialized(funcName, globalScopeSymbols, localScopeSymbols);
|
|
441
|
+
// Generate native name
|
|
442
|
+
const nativeName = this.generateUniqueName(`__${funcName}_native_`, hoistedSymbols);
|
|
443
|
+
hoistedSymbols.update(funcName, { func: { nativeName } });
|
|
444
|
+
// Generate lambda
|
|
445
|
+
const lambda = this.generateLambda(stmt, contextForFunctions, {
|
|
446
|
+
isAssignment: true,
|
|
447
|
+
generateOnlyLambda: true,
|
|
448
|
+
nativeName,
|
|
449
|
+
});
|
|
450
|
+
code += `${this.indent()}auto ${nativeName} = ${lambda};\n`;
|
|
451
|
+
// Generate AnyValue wrapper
|
|
452
|
+
if (this.isFunctionUsedAsValue(stmt, node) ||
|
|
453
|
+
this.isFunctionUsedBeforeDeclaration(funcName, node)) {
|
|
454
|
+
const fullExpression = this.generateFullLambdaExpression(stmt, contextForFunctions, nativeName, { isAssignment: true, noTypeSignature: true });
|
|
455
|
+
code += `${this.indent()}*${funcName} = ${fullExpression};\n`;
|
|
456
|
+
}
|
|
457
|
+
});
|
|
396
458
|
let firstIf = true;
|
|
397
459
|
for (const clause of switchStmt.caseBlock.clauses) {
|
|
398
460
|
if (ts.isCaseClause(clause)) {
|
|
@@ -416,15 +478,23 @@ export function visitSwitchStatement(node, context) {
|
|
|
416
478
|
code += `${this.indent()}${fallthroughVar} = true;\n`;
|
|
417
479
|
for (const stmt of clause.statements) {
|
|
418
480
|
if (ts.isFunctionDeclaration(stmt)) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
481
|
+
// Already handled
|
|
482
|
+
}
|
|
483
|
+
else if (ts.isVariableStatement(stmt)) {
|
|
484
|
+
const isLetOrConst = (stmt.declarationList.flags &
|
|
485
|
+
(ts.NodeFlags.Let | ts.NodeFlags.Const)) !==
|
|
486
|
+
0;
|
|
487
|
+
const contextForVisit = {
|
|
488
|
+
...context,
|
|
489
|
+
switchBreakLabel,
|
|
490
|
+
currentLabel: undefined,
|
|
491
|
+
globalScopeSymbols,
|
|
492
|
+
localScopeSymbols,
|
|
493
|
+
isAssignmentOnly: !isLetOrConst,
|
|
494
|
+
};
|
|
495
|
+
const assignments = this.visit(stmt.declarationList, contextForVisit);
|
|
496
|
+
if (assignments) {
|
|
497
|
+
code += `${this.indent()}${assignments};\n`;
|
|
428
498
|
}
|
|
429
499
|
}
|
|
430
500
|
else {
|
|
@@ -432,10 +502,8 @@ export function visitSwitchStatement(node, context) {
|
|
|
432
502
|
...context,
|
|
433
503
|
switchBreakLabel,
|
|
434
504
|
currentLabel: undefined, // Clear currentLabel for nested visits
|
|
435
|
-
|
|
436
|
-
localScopeSymbols
|
|
437
|
-
derefBeforeAssignment: true,
|
|
438
|
-
isAssignmentOnly: ts.isVariableStatement(stmt),
|
|
505
|
+
globalScopeSymbols,
|
|
506
|
+
localScopeSymbols,
|
|
439
507
|
});
|
|
440
508
|
}
|
|
441
509
|
}
|
|
@@ -455,15 +523,35 @@ export function visitSwitchStatement(node, context) {
|
|
|
455
523
|
}
|
|
456
524
|
this.indentationLevel++;
|
|
457
525
|
for (const stmt of clause.statements) {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
526
|
+
if (ts.isFunctionDeclaration(stmt)) {
|
|
527
|
+
// Already handled
|
|
528
|
+
}
|
|
529
|
+
else if (ts.isVariableStatement(stmt)) {
|
|
530
|
+
const isLetOrConst = (stmt.declarationList.flags &
|
|
531
|
+
(ts.NodeFlags.Let | ts.NodeFlags.Const)) !==
|
|
532
|
+
0;
|
|
533
|
+
const contextForVisit = {
|
|
534
|
+
...context,
|
|
535
|
+
switchBreakLabel,
|
|
536
|
+
currentLabel: undefined,
|
|
537
|
+
globalScopeSymbols,
|
|
538
|
+
localScopeSymbols,
|
|
539
|
+
isAssignmentOnly: !isLetOrConst,
|
|
540
|
+
};
|
|
541
|
+
const assignments = this.visit(stmt.declarationList, contextForVisit);
|
|
542
|
+
if (assignments) {
|
|
543
|
+
code += `${this.indent()}${assignments};\n`;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
code += this.visit(stmt, {
|
|
548
|
+
...context,
|
|
549
|
+
switchBreakLabel,
|
|
550
|
+
currentLabel: undefined, // Clear currentLabel for nested visits
|
|
551
|
+
globalScopeSymbols,
|
|
552
|
+
localScopeSymbols,
|
|
553
|
+
});
|
|
554
|
+
}
|
|
467
555
|
}
|
|
468
556
|
this.indentationLevel--;
|
|
469
557
|
code += `${this.indent()}}\n`;
|
|
@@ -12,27 +12,50 @@ export function visitVariableDeclaration(node, context) {
|
|
|
12
12
|
const scope = this.getScopeForNode(varDecl);
|
|
13
13
|
const typeInfo = this.typeAnalyzer.scopeManager.lookupFromScope(name, scope);
|
|
14
14
|
// Mark the symbol as checked
|
|
15
|
-
this.
|
|
15
|
+
this.markSymbolAsInitialized(name, context.globalScopeSymbols, context.localScopeSymbols);
|
|
16
|
+
let nativeLambdaCode = "";
|
|
16
17
|
let initializer = "";
|
|
18
|
+
let shouldSkipDeref = false;
|
|
17
19
|
if (varDecl.initializer) {
|
|
18
20
|
const initExpr = varDecl.initializer;
|
|
19
|
-
const initContext = {
|
|
20
|
-
...context,
|
|
21
|
-
lambdaName: ts.isArrowFunction(initExpr) ? name : undefined, // Pass the variable name for arrow functions
|
|
22
|
-
};
|
|
23
21
|
let initText = ts.isNumericLiteral(initExpr)
|
|
24
22
|
? initExpr.getText()
|
|
25
|
-
: this.visit(initExpr,
|
|
23
|
+
: this.visit(initExpr, context);
|
|
26
24
|
if (ts.isIdentifier(initExpr)) {
|
|
27
25
|
const initScope = this.getScopeForNode(initExpr);
|
|
28
26
|
const initTypeInfo = this.typeAnalyzer.scopeManager.lookupFromScope(initExpr.text, initScope);
|
|
29
27
|
const varName = this.getJsVarName(initExpr);
|
|
28
|
+
// Check if both target and initializer are heap allocated
|
|
29
|
+
if (typeInfo.needsHeapAllocation &&
|
|
30
|
+
initTypeInfo?.needsHeapAllocation) {
|
|
31
|
+
shouldSkipDeref = true;
|
|
32
|
+
}
|
|
30
33
|
if (initTypeInfo &&
|
|
31
34
|
!initTypeInfo.isParameter &&
|
|
32
|
-
!initTypeInfo.isBuiltin
|
|
33
|
-
|
|
35
|
+
!initTypeInfo.isBuiltin &&
|
|
36
|
+
!shouldSkipDeref) {
|
|
37
|
+
initText = this.getDerefCode(initText, varName, context, initTypeInfo);
|
|
34
38
|
}
|
|
35
39
|
}
|
|
40
|
+
else if (ts.isArrowFunction(initExpr)) {
|
|
41
|
+
const initContext = {
|
|
42
|
+
...context,
|
|
43
|
+
lambdaName: name, // Use the variable name as function name
|
|
44
|
+
};
|
|
45
|
+
// Generate and update self name
|
|
46
|
+
const nativeName = this.generateUniqueName(`__${name}_native_`, context.localScopeSymbols, context.globalScopeSymbols);
|
|
47
|
+
context.localScopeSymbols.update(name, { func: { nativeName } });
|
|
48
|
+
// Generate lambda
|
|
49
|
+
const lambda = this.generateLambda(initExpr, initContext, {
|
|
50
|
+
isAssignment: true,
|
|
51
|
+
generateOnlyLambda: true,
|
|
52
|
+
nativeName,
|
|
53
|
+
});
|
|
54
|
+
nativeLambdaCode =
|
|
55
|
+
`auto ${nativeName} = ${lambda};\n${this.indent()}`;
|
|
56
|
+
// Generate AnyValue wrapper
|
|
57
|
+
initText = this.generateFullLambdaExpression(initExpr, initContext, nativeName, { isAssignment: true, noTypeSignature: true });
|
|
58
|
+
}
|
|
36
59
|
initializer = " = " + initText;
|
|
37
60
|
}
|
|
38
61
|
const isLetOrConst = (varDecl.parent.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) !== 0;
|
|
@@ -40,19 +63,21 @@ export function visitVariableDeclaration(node, context) {
|
|
|
40
63
|
(!context.localScopeSymbols.has(name));
|
|
41
64
|
const assignmentTarget = shouldDeref
|
|
42
65
|
? this.getDerefCode(name, name, context, typeInfo)
|
|
43
|
-
: (typeInfo.needsHeapAllocation
|
|
66
|
+
: (typeInfo.needsHeapAllocation && !shouldSkipDeref
|
|
67
|
+
? `*${name}`
|
|
68
|
+
: name);
|
|
44
69
|
if (isLetOrConst) {
|
|
45
70
|
// If there's no initializer, it should be assigned undefined.
|
|
46
71
|
if (!initializer) {
|
|
47
|
-
return `${assignmentTarget} = jspp::Constants::UNDEFINED`;
|
|
72
|
+
return `${nativeLambdaCode}${assignmentTarget} = jspp::Constants::UNDEFINED`;
|
|
48
73
|
}
|
|
49
|
-
return `${assignmentTarget}${initializer}`;
|
|
74
|
+
return `${nativeLambdaCode}${assignmentTarget}${initializer}`;
|
|
50
75
|
}
|
|
51
76
|
// For 'var', it's a bit more complex.
|
|
52
77
|
if (context.isAssignmentOnly) {
|
|
53
78
|
if (!initializer)
|
|
54
79
|
return "";
|
|
55
|
-
return `${assignmentTarget}${initializer}`;
|
|
80
|
+
return `${nativeLambdaCode}${assignmentTarget}${initializer}`;
|
|
56
81
|
}
|
|
57
82
|
else {
|
|
58
83
|
// This case should not be hit with the new hoisting logic,
|
|
@@ -28,6 +28,13 @@ export function visitObjectPropertyName(node, context) {
|
|
|
28
28
|
export function visitObjectLiteralExpression(node, context) {
|
|
29
29
|
const obj = node;
|
|
30
30
|
const objVar = this.generateUniqueName("__obj_", this.getDeclaredSymbols(node));
|
|
31
|
+
if (!obj.properties.some((prop) => ts.isPropertyAssignment(prop) ||
|
|
32
|
+
ts.isShorthandPropertyAssignment(prop) ||
|
|
33
|
+
ts.isMethodDeclaration(prop) || ts.isGetAccessor(prop) ||
|
|
34
|
+
ts.isSetAccessor(prop))) {
|
|
35
|
+
// Empty object
|
|
36
|
+
return `jspp::AnyValue::make_object_with_proto({}, ::Object.get_own_property("prototype"))`;
|
|
37
|
+
}
|
|
31
38
|
let code = `([&]() {\n`;
|
|
32
39
|
code +=
|
|
33
40
|
`${this.indent()} auto ${objVar} = jspp::AnyValue::make_object_with_proto({}, ::Object.get_own_property("prototype"));\n`;
|
|
@@ -102,8 +109,6 @@ export function visitObjectLiteralExpression(node, context) {
|
|
|
102
109
|
}
|
|
103
110
|
}
|
|
104
111
|
this.indentationLevel--;
|
|
105
|
-
// code +=
|
|
106
|
-
// `${this.indent()} ${returnCmd} ${objVar};\n${this.indent()}} )() ))`;
|
|
107
112
|
code += `${this.indent()} return ${objVar};\n${this.indent()}})()`;
|
|
108
113
|
return code;
|
|
109
114
|
}
|
|
@@ -439,6 +444,21 @@ export function visitBinaryExpression(node, context) {
|
|
|
439
444
|
const target = context.derefBeforeAssignment
|
|
440
445
|
? this.getDerefCode(leftText, leftText, context, typeInfo)
|
|
441
446
|
: (typeInfo.needsHeapAllocation ? `*${leftText}` : leftText);
|
|
447
|
+
// Update scope symbols on variable re-assignment
|
|
448
|
+
if (ts.isIdentifier(binExpr.left)) {
|
|
449
|
+
if (!ts.isFunctionDeclaration(binExpr.right)) {
|
|
450
|
+
if (context.localScopeSymbols.has(binExpr.left.text)) {
|
|
451
|
+
context.localScopeSymbols.update(binExpr.left.text, {
|
|
452
|
+
func: null,
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
else if (context.globalScopeSymbols.has(binExpr.left.text)) {
|
|
456
|
+
context.globalScopeSymbols.update(binExpr.left.text, {
|
|
457
|
+
func: null,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
442
462
|
return `${target} ${op} ${rightText}`;
|
|
443
463
|
}
|
|
444
464
|
const leftText = this.visit(binExpr.left, context);
|
|
@@ -535,7 +555,7 @@ export function visitConditionalExpression(node, context) {
|
|
|
535
555
|
...context,
|
|
536
556
|
isFunctionBody: false,
|
|
537
557
|
});
|
|
538
|
-
return `is_truthy(${condition}) ? ${whenTrueStmt} : ${whenFalseStmt}`;
|
|
558
|
+
return `jspp::is_truthy(${condition}) ? ${whenTrueStmt} : ${whenFalseStmt}`;
|
|
539
559
|
}
|
|
540
560
|
export function visitCallExpression(node, context) {
|
|
541
561
|
const callExpr = node;
|
|
@@ -645,6 +665,21 @@ export function visitCallExpression(node, context) {
|
|
|
645
665
|
derefCallee = calleeCode;
|
|
646
666
|
}
|
|
647
667
|
else if (typeInfo) {
|
|
668
|
+
const name = callee.getText();
|
|
669
|
+
const symbol = context.localScopeSymbols.get(name) ??
|
|
670
|
+
context.globalScopeSymbols.get(name);
|
|
671
|
+
// Optimization: Direct lambda call
|
|
672
|
+
if (symbol && symbol.func?.nativeName) {
|
|
673
|
+
const callExpr = `${symbol.func.nativeName}(jspp::Constants::UNDEFINED, ${argsSpan})`;
|
|
674
|
+
if (symbol.func.isGenerator) {
|
|
675
|
+
if (symbol.func.isAsync) {
|
|
676
|
+
return `jspp::AnyValue::from_async_iterator(${callExpr})`;
|
|
677
|
+
}
|
|
678
|
+
return `jspp::AnyValue::from_iterator(${callExpr})`;
|
|
679
|
+
}
|
|
680
|
+
return callExpr;
|
|
681
|
+
}
|
|
682
|
+
// AnyValue function call
|
|
648
683
|
derefCallee = this.getDerefCode(calleeCode, this.getJsVarName(callee), context, typeInfo);
|
|
649
684
|
}
|
|
650
685
|
}
|