@ugo-studio/jspp 0.3.0 → 0.3.2
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 +25 -25
- package/README.md +20 -12
- package/dist/cli/args.js +22 -0
- package/dist/cli/compiler.js +53 -0
- package/dist/cli/index.js +43 -107
- package/dist/cli/pch.js +71 -0
- package/dist/cli/runner.js +23 -0
- package/dist/cli/spinner.js +27 -11
- package/dist/cli/transpiler.js +20 -0
- package/dist/cli/utils.js +59 -0
- package/dist/cli/wasm.js +70 -0
- package/dist/index.js +17 -6
- package/dist/{analysis → interpreter/analysis}/scope.js +38 -3
- package/dist/{analysis → interpreter/analysis}/typeAnalyzer.js +563 -28
- package/dist/{core → interpreter/core}/codegen/class-handlers.js +1 -1
- package/dist/{core → interpreter/core}/codegen/control-flow-handlers.js +12 -11
- package/dist/{core → interpreter/core}/codegen/declaration-handlers.js +28 -9
- package/dist/{core → interpreter/core}/codegen/destructuring-handlers.js +9 -4
- package/dist/{core → interpreter/core}/codegen/expression-handlers.js +82 -88
- package/dist/{core → interpreter/core}/codegen/function-handlers.js +159 -46
- package/dist/{core → interpreter/core}/codegen/helpers.js +170 -25
- package/dist/interpreter/core/codegen/index.js +156 -0
- package/dist/{core → interpreter/core}/codegen/literal-handlers.js +9 -0
- package/dist/{core → interpreter/core}/codegen/statement-handlers.js +47 -7
- package/package.json +6 -4
- package/scripts/precompile-headers.ts +293 -50
- package/scripts/setup-compiler.ts +63 -63
- package/scripts/setup-emsdk.ts +114 -0
- package/src/prelude/any_value.cpp +888 -0
- package/src/prelude/any_value.hpp +29 -24
- package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
- package/src/prelude/exception.hpp +27 -27
- package/src/prelude/iterator_instantiations.hpp +10 -0
- package/src/prelude/{index.hpp → jspp.hpp} +13 -17
- package/src/prelude/library/array.cpp +191 -0
- package/src/prelude/library/array.hpp +5 -178
- package/src/prelude/library/boolean.cpp +30 -0
- package/src/prelude/library/boolean.hpp +14 -0
- package/src/prelude/library/console.cpp +125 -0
- package/src/prelude/library/console.hpp +9 -97
- package/src/prelude/library/error.cpp +100 -0
- package/src/prelude/library/error.hpp +8 -108
- package/src/prelude/library/function.cpp +69 -0
- package/src/prelude/library/function.hpp +6 -5
- package/src/prelude/library/global.cpp +98 -0
- package/src/prelude/library/global.hpp +12 -28
- package/src/prelude/library/global_usings.hpp +15 -0
- package/src/prelude/library/math.cpp +261 -0
- package/src/prelude/library/math.hpp +8 -288
- package/src/prelude/library/object.cpp +379 -0
- package/src/prelude/library/object.hpp +5 -267
- package/src/prelude/library/performance.cpp +21 -0
- package/src/prelude/library/performance.hpp +5 -20
- package/src/prelude/library/process.cpp +38 -0
- package/src/prelude/library/process.hpp +3 -31
- package/src/prelude/library/promise.cpp +131 -0
- package/src/prelude/library/promise.hpp +5 -116
- package/src/prelude/library/symbol.cpp +56 -0
- package/src/prelude/library/symbol.hpp +5 -46
- package/src/prelude/library/timer.cpp +88 -0
- package/src/prelude/library/timer.hpp +11 -87
- package/src/prelude/runtime.cpp +19 -0
- package/src/prelude/types.hpp +26 -20
- package/src/prelude/utils/access.hpp +123 -32
- package/src/prelude/utils/assignment_operators.hpp +119 -99
- package/src/prelude/utils/log_any_value/array.hpp +61 -40
- package/src/prelude/utils/log_any_value/function.hpp +39 -39
- package/src/prelude/utils/log_any_value/log_any_value.hpp +1 -1
- package/src/prelude/utils/log_any_value/object.hpp +60 -3
- package/src/prelude/utils/log_any_value/primitives.hpp +1 -1
- package/src/prelude/utils/operators.hpp +109 -94
- package/src/prelude/utils/operators_native.hpp +349 -0
- package/src/prelude/utils/well_known_symbols.hpp +24 -24
- package/src/prelude/values/array.cpp +1399 -0
- package/src/prelude/values/array.hpp +4 -0
- package/src/prelude/values/async_iterator.cpp +251 -0
- package/src/prelude/values/async_iterator.hpp +60 -32
- package/src/prelude/values/boolean.cpp +64 -0
- package/src/prelude/values/function.cpp +262 -0
- package/src/prelude/values/function.hpp +10 -30
- package/src/prelude/values/iterator.cpp +309 -0
- package/src/prelude/values/iterator.hpp +33 -64
- package/src/prelude/values/number.cpp +221 -0
- package/src/prelude/values/object.cpp +200 -0
- package/src/prelude/values/object.hpp +4 -0
- package/src/prelude/values/promise.cpp +479 -0
- package/src/prelude/values/promise.hpp +9 -2
- package/src/prelude/values/prototypes/array.hpp +46 -1348
- package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
- package/src/prelude/values/prototypes/boolean.hpp +24 -0
- package/src/prelude/values/prototypes/function.hpp +7 -46
- package/src/prelude/values/prototypes/iterator.hpp +15 -191
- package/src/prelude/values/prototypes/number.hpp +30 -210
- package/src/prelude/values/prototypes/object.hpp +7 -23
- package/src/prelude/values/prototypes/promise.hpp +8 -186
- package/src/prelude/values/prototypes/string.hpp +28 -553
- package/src/prelude/values/prototypes/symbol.hpp +9 -70
- package/src/prelude/values/shape.hpp +52 -52
- package/src/prelude/values/string.cpp +485 -0
- package/src/prelude/values/symbol.cpp +89 -0
- package/src/prelude/values/symbol.hpp +101 -101
- package/dist/cli/file-utils.js +0 -20
- package/dist/cli-utils/args.js +0 -59
- package/dist/cli-utils/colors.js +0 -9
- package/dist/cli-utils/file-utils.js +0 -20
- package/dist/cli-utils/spinner.js +0 -55
- package/dist/cli.js +0 -153
- package/dist/core/codegen/index.js +0 -86
- package/src/prelude/any_value_access.hpp +0 -170
- package/src/prelude/any_value_defines.hpp +0 -190
- package/src/prelude/any_value_helpers.hpp +0 -374
- package/src/prelude/utils/operators_primitive.hpp +0 -337
- package/src/prelude/values/helpers/array.hpp +0 -199
- package/src/prelude/values/helpers/async_iterator.hpp +0 -275
- package/src/prelude/values/helpers/function.hpp +0 -109
- package/src/prelude/values/helpers/iterator.hpp +0 -145
- package/src/prelude/values/helpers/object.hpp +0 -104
- package/src/prelude/values/helpers/promise.hpp +0 -254
- package/src/prelude/values/helpers/string.hpp +0 -37
- package/src/prelude/values/helpers/symbol.hpp +0 -21
- /package/dist/{ast → interpreter/ast}/symbols.js +0 -0
- /package/dist/{ast → interpreter/ast}/types.js +0 -0
- /package/dist/{core → interpreter/core}/codegen/visitor.js +0 -0
- /package/dist/{core → interpreter/core}/constants.js +0 -0
- /package/dist/{core → interpreter/core}/error.js +0 -0
- /package/dist/{core → interpreter/core}/parser.js +0 -0
- /package/dist/{core → interpreter/core}/traverser.js +0 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { DeclaredSymbols } from "../../ast/symbols.js";
|
|
3
|
+
import { generateDestructuring } from "./destructuring-handlers.js";
|
|
4
|
+
import { generateLambdaComponents, generateNativeLambda, generateWrappedLambda, } from "./function-handlers.js";
|
|
5
|
+
import { escapeString, findEnclosingFunctionDeclarationFromReturnStatement, generateUniqueExceptionName, generateUniqueName, getDeclaredSymbols, getDerefCode, getJsVarName, getReturnCommand, getScopeForNode, hoistDeclaration, indent, isAsyncFunction, isBuiltinObject, isDeclarationCalledAsFunction, isDeclarationUsedAsValue, isDeclarationUsedBeforeInitialization, isGeneratorFunction, isVariableUsedWithoutDeclaration, markSymbolAsInitialized, needsTopLevelAwait, prepareScopeSymbolsForVisit, validateFunctionParams, } from "./helpers.js";
|
|
6
|
+
import { visit } from "./visitor.js";
|
|
7
|
+
export class CodeGenerator {
|
|
8
|
+
indentationLevel = 0;
|
|
9
|
+
typeAnalyzer;
|
|
10
|
+
isTypescript = false;
|
|
11
|
+
moduleFunctionName;
|
|
12
|
+
globalThisVar;
|
|
13
|
+
uniqueNameCounter = 0;
|
|
14
|
+
isWasm = false;
|
|
15
|
+
wasmExports = [];
|
|
16
|
+
// visitor
|
|
17
|
+
visit = visit;
|
|
18
|
+
// helpers
|
|
19
|
+
getDeclaredSymbols = getDeclaredSymbols;
|
|
20
|
+
generateUniqueName = generateUniqueName;
|
|
21
|
+
generateUniqueExceptionName = generateUniqueExceptionName;
|
|
22
|
+
hoistDeclaration = hoistDeclaration;
|
|
23
|
+
getScopeForNode = getScopeForNode;
|
|
24
|
+
indent = indent;
|
|
25
|
+
escapeString = escapeString;
|
|
26
|
+
getJsVarName = getJsVarName;
|
|
27
|
+
getDerefCode = getDerefCode;
|
|
28
|
+
getReturnCommand = getReturnCommand;
|
|
29
|
+
isBuiltinObject = isBuiltinObject;
|
|
30
|
+
isGeneratorFunction = isGeneratorFunction;
|
|
31
|
+
isAsyncFunction = isAsyncFunction;
|
|
32
|
+
prepareScopeSymbolsForVisit = prepareScopeSymbolsForVisit;
|
|
33
|
+
markSymbolAsInitialized = markSymbolAsInitialized;
|
|
34
|
+
isDeclarationCalledAsFunction = isDeclarationCalledAsFunction;
|
|
35
|
+
isDeclarationUsedAsValue = isDeclarationUsedAsValue;
|
|
36
|
+
isDeclarationUsedBeforeInitialization = isDeclarationUsedBeforeInitialization;
|
|
37
|
+
isVariableUsedWithoutDeclaration = isVariableUsedWithoutDeclaration;
|
|
38
|
+
validateFunctionParams = validateFunctionParams;
|
|
39
|
+
generateDestructuring = generateDestructuring;
|
|
40
|
+
findEnclosingFunctionDeclarationFromReturnStatement = findEnclosingFunctionDeclarationFromReturnStatement;
|
|
41
|
+
// function handlers
|
|
42
|
+
generateLambdaComponents = generateLambdaComponents;
|
|
43
|
+
generateNativeLambda = generateNativeLambda;
|
|
44
|
+
generateWrappedLambda = generateWrappedLambda;
|
|
45
|
+
/**
|
|
46
|
+
* Main entry point for the code generation process.
|
|
47
|
+
*/
|
|
48
|
+
generate(ast, analyzer, isTypescript, isWasm = false) {
|
|
49
|
+
this.typeAnalyzer = analyzer;
|
|
50
|
+
this.isTypescript = isTypescript;
|
|
51
|
+
this.isWasm = isWasm;
|
|
52
|
+
this.wasmExports = [];
|
|
53
|
+
this.moduleFunctionName = this.generateUniqueName("__module_entry_point_", this.getDeclaredSymbols(ast));
|
|
54
|
+
this.globalThisVar = this.generateUniqueName("__this_val__", this.getDeclaredSymbols(ast));
|
|
55
|
+
const isAsyncModule = needsTopLevelAwait(ast);
|
|
56
|
+
const moduleReturnType = isAsyncModule
|
|
57
|
+
? "jspp::JsPromise"
|
|
58
|
+
: "jspp::AnyValue";
|
|
59
|
+
const moduleReturnCommand = isAsyncModule ? "co_return" : "return";
|
|
60
|
+
let declarations = `#include "jspp.hpp"\n#include "library/global_usings.hpp"\n`;
|
|
61
|
+
if (isWasm) {
|
|
62
|
+
declarations += `#include <emscripten.h>\n`;
|
|
63
|
+
}
|
|
64
|
+
declarations += `\n`;
|
|
65
|
+
// module function code
|
|
66
|
+
let moduleCode = `${moduleReturnType} ${this.moduleFunctionName}() {\n`;
|
|
67
|
+
this.indentationLevel++;
|
|
68
|
+
moduleCode +=
|
|
69
|
+
`${this.indent()}jspp::AnyValue ${this.globalThisVar} = global;\n`;
|
|
70
|
+
const context = {
|
|
71
|
+
currentScopeNode: ast,
|
|
72
|
+
isMainContext: true,
|
|
73
|
+
isInsideFunction: true,
|
|
74
|
+
isFunctionBody: true,
|
|
75
|
+
isInsideAsyncFunction: isAsyncModule,
|
|
76
|
+
globalScopeSymbols: new DeclaredSymbols(),
|
|
77
|
+
localScopeSymbols: new DeclaredSymbols(),
|
|
78
|
+
};
|
|
79
|
+
const generatedBody = this.visit(ast, context);
|
|
80
|
+
moduleCode += generatedBody;
|
|
81
|
+
moduleCode +=
|
|
82
|
+
`${this.indent()}${moduleReturnCommand} jspp::Constants::UNDEFINED;\n`;
|
|
83
|
+
this.indentationLevel--;
|
|
84
|
+
moduleCode += "}\n\n";
|
|
85
|
+
// Wasm Exports
|
|
86
|
+
let wasmGlobalPointers = "";
|
|
87
|
+
let wasmWrappers = "";
|
|
88
|
+
if (isWasm) {
|
|
89
|
+
for (const exp of this.wasmExports) {
|
|
90
|
+
const paramTypes = exp.params.map((_, i) => `jspp::AnyValue`);
|
|
91
|
+
const paramList = paramTypes.length > 0
|
|
92
|
+
? `, ${paramTypes.join(", ")}`
|
|
93
|
+
: "";
|
|
94
|
+
const pointerName = `__wasm_export_ptr_${exp.jsName}`;
|
|
95
|
+
wasmGlobalPointers +=
|
|
96
|
+
`std::function<jspp::AnyValue(jspp::AnyValue${paramList})> ${pointerName} = nullptr;\n`;
|
|
97
|
+
const wrapperParamList = exp.params.map((_, i) => `double p${i}`).join(", ");
|
|
98
|
+
const callArgsList = exp.params.map((_, i) => `jspp::AnyValue::make_number(p${i})`);
|
|
99
|
+
const callArgs = callArgsList.length > 0
|
|
100
|
+
? `, ${callArgsList.join(", ")}`
|
|
101
|
+
: "";
|
|
102
|
+
wasmWrappers += `extern "C" EMSCRIPTEN_KEEPALIVE\n`;
|
|
103
|
+
wasmWrappers +=
|
|
104
|
+
`double wasm_export_${exp.jsName}(${wrapperParamList}) {\n`;
|
|
105
|
+
wasmWrappers += ` if (!${pointerName}) return 0;\n`;
|
|
106
|
+
wasmWrappers +=
|
|
107
|
+
` auto res = ${pointerName}(global${callArgs});\n`;
|
|
108
|
+
wasmWrappers +=
|
|
109
|
+
` return jspp::Operators_Private::ToNumber(res);\n`;
|
|
110
|
+
wasmWrappers += `}\n\n`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// main function code
|
|
114
|
+
let mainCode = "int main(int argc, char** argv) {\n";
|
|
115
|
+
this.indentationLevel++;
|
|
116
|
+
mainCode += `${this.indent()}try {\n`;
|
|
117
|
+
this.indentationLevel++;
|
|
118
|
+
mainCode += `${this.indent()}jspp::initialize_runtime();\n`;
|
|
119
|
+
mainCode += `${this.indent()}jspp::setup_process_argv(argc, argv);\n`;
|
|
120
|
+
if (isAsyncModule) {
|
|
121
|
+
mainCode +=
|
|
122
|
+
`${this.indent()}auto p = ${this.moduleFunctionName}();\n`;
|
|
123
|
+
mainCode +=
|
|
124
|
+
`${this.indent()}p.then(nullptr, [](jspp::AnyValue err) {\n`;
|
|
125
|
+
this.indentationLevel++;
|
|
126
|
+
mainCode +=
|
|
127
|
+
`${this.indent()}auto error = std::make_shared<jspp::AnyValue>(err);\n`;
|
|
128
|
+
this.indentationLevel++;
|
|
129
|
+
mainCode +=
|
|
130
|
+
`${this.indent()}console.call_own_property("error", std::span<const jspp::AnyValue>((const jspp::AnyValue[]){*error}, 1));\n`;
|
|
131
|
+
mainCode += `${this.indent()}std::exit(1);\n`;
|
|
132
|
+
this.indentationLevel--;
|
|
133
|
+
mainCode += `${this.indent()}});\n`;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
mainCode += `${this.indent()}${this.moduleFunctionName}();\n`;
|
|
137
|
+
}
|
|
138
|
+
mainCode += `${this.indent()}jspp::Scheduler::instance().run();\n`;
|
|
139
|
+
this.indentationLevel--;
|
|
140
|
+
mainCode += `${this.indent()}} catch (const std::exception& ex) {\n`;
|
|
141
|
+
this.indentationLevel++;
|
|
142
|
+
mainCode +=
|
|
143
|
+
`${this.indent()}auto error = std::make_shared<jspp::AnyValue>(jspp::Exception::exception_to_any_value(ex));\n`;
|
|
144
|
+
this.indentationLevel++;
|
|
145
|
+
mainCode +=
|
|
146
|
+
`${this.indent()}console.call_own_property("error", std::span<const jspp::AnyValue>((const jspp::AnyValue[]){*error}, 1));\n`;
|
|
147
|
+
mainCode += `${this.indent()}return 1;\n`;
|
|
148
|
+
this.indentationLevel--;
|
|
149
|
+
mainCode += `${this.indent()}}\n`;
|
|
150
|
+
mainCode += `${this.indent()}return 0;\n`;
|
|
151
|
+
this.indentationLevel--;
|
|
152
|
+
mainCode += `}`;
|
|
153
|
+
return declarations + wasmGlobalPointers + wasmWrappers + moduleCode +
|
|
154
|
+
mainCode;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -11,6 +11,15 @@ export function visitIdentifier(node) {
|
|
|
11
11
|
return node.text;
|
|
12
12
|
}
|
|
13
13
|
export function visitNumericLiteral(node) {
|
|
14
|
+
const getOuterExpr = (n) => !ts.isParenthesizedExpression(n) &&
|
|
15
|
+
n.kind !== ts.SyntaxKind.FirstLiteralToken
|
|
16
|
+
? n
|
|
17
|
+
: getOuterExpr(n.parent);
|
|
18
|
+
const outerExpr = getOuterExpr(node);
|
|
19
|
+
if (!ts.isPropertyAccessExpression(outerExpr) &&
|
|
20
|
+
!ts.isElementAccessExpression(outerExpr)) {
|
|
21
|
+
return this.escapeString(node.text);
|
|
22
|
+
}
|
|
14
23
|
if (node.text === "0" || (node.text.startsWith("0.") &&
|
|
15
24
|
!node.text.substring(2).split("").some((c) => c !== "0")))
|
|
16
25
|
return "jspp::Constants::ZERO";
|
|
@@ -52,14 +52,31 @@ export function visitSourceFile(node, context) {
|
|
|
52
52
|
// Mark before further visits
|
|
53
53
|
this.markSymbolAsInitialized(funcName, contextForFunctions.globalScopeSymbols, contextForFunctions.localScopeSymbols);
|
|
54
54
|
this.markSymbolAsInitialized(funcName, globalScopeSymbols, localScopeSymbols);
|
|
55
|
+
// Check for @export comment
|
|
56
|
+
let exported = false;
|
|
57
|
+
if (this.isWasm) {
|
|
58
|
+
const fullText = sourceFile.text;
|
|
59
|
+
const commentRanges = ts.getLeadingCommentRanges(fullText, stmt.pos);
|
|
60
|
+
if (commentRanges) {
|
|
61
|
+
for (const range of commentRanges) {
|
|
62
|
+
const comment = fullText.substring(range.pos, range.end);
|
|
63
|
+
if (comment.includes("@export")) {
|
|
64
|
+
exported = true;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
55
70
|
// Update features in the symbol registry
|
|
56
71
|
const nativeName = this.generateUniqueName(`__${funcName}_native_`, hoistedSymbols);
|
|
72
|
+
const argumentKeywordIsUsed = this.isVariableUsedWithoutDeclaration("arguments", stmt.body);
|
|
57
73
|
hoistedSymbols.update(funcName, {
|
|
58
74
|
features: {
|
|
59
75
|
native: {
|
|
60
76
|
type: "lambda",
|
|
61
77
|
name: nativeName,
|
|
62
78
|
parameters: this.validateFunctionParams(stmt.parameters),
|
|
79
|
+
argumentKeywordIsUsed,
|
|
63
80
|
},
|
|
64
81
|
},
|
|
65
82
|
});
|
|
@@ -70,9 +87,18 @@ export function visitSourceFile(node, context) {
|
|
|
70
87
|
noTypeSignature: true,
|
|
71
88
|
});
|
|
72
89
|
// Generate native lambda
|
|
73
|
-
if (this.isDeclarationCalledAsFunction(stmt, node)) {
|
|
90
|
+
if (this.isDeclarationCalledAsFunction(stmt, node) || exported) {
|
|
74
91
|
const nativeLambda = this.generateNativeLambda(lambdaComps);
|
|
75
92
|
code += `${this.indent()}auto ${nativeName} = ${nativeLambda};\n`;
|
|
93
|
+
if (exported) {
|
|
94
|
+
this.wasmExports.push({
|
|
95
|
+
jsName: funcName,
|
|
96
|
+
nativeName,
|
|
97
|
+
params: Array.from(stmt.parameters),
|
|
98
|
+
});
|
|
99
|
+
code +=
|
|
100
|
+
`${this.indent()}__wasm_export_ptr_${funcName} = ${nativeName};\n`;
|
|
101
|
+
}
|
|
76
102
|
}
|
|
77
103
|
// Generate AnyValue wrapped lamda
|
|
78
104
|
if (this.isDeclarationUsedAsValue(stmt, node) ||
|
|
@@ -151,12 +177,14 @@ export function visitBlock(node, context) {
|
|
|
151
177
|
this.markSymbolAsInitialized(funcName, globalScopeSymbols, localScopeSymbols);
|
|
152
178
|
// Update features in the symbol registry
|
|
153
179
|
const nativeName = this.generateUniqueName(`__${funcName}_native_`, hoistedSymbols);
|
|
180
|
+
const argumentKeywordIsUsed = this.isVariableUsedWithoutDeclaration("arguments", stmt.body);
|
|
154
181
|
hoistedSymbols.update(funcName, {
|
|
155
182
|
features: {
|
|
156
183
|
native: {
|
|
157
184
|
type: "lambda",
|
|
158
185
|
name: nativeName,
|
|
159
186
|
parameters: this.validateFunctionParams(stmt.parameters),
|
|
187
|
+
argumentKeywordIsUsed,
|
|
160
188
|
},
|
|
161
189
|
},
|
|
162
190
|
});
|
|
@@ -238,7 +266,7 @@ export function visitEnumDeclaration(node, context) {
|
|
|
238
266
|
const typeInfo = this.typeAnalyzer.scopeManager.lookupFromScope(name, scope);
|
|
239
267
|
// Mark as initialized
|
|
240
268
|
this.markSymbolAsInitialized(name, context.globalScopeSymbols, context.localScopeSymbols);
|
|
241
|
-
const enumVar = typeInfo
|
|
269
|
+
const enumVar = typeInfo?.needsHeapAllocation ? `(*${name})` : name;
|
|
242
270
|
let code = `${this.indent()}${enumVar} = jspp::AnyValue::make_object({});\n`;
|
|
243
271
|
code += `${this.indent()}{\n`;
|
|
244
272
|
this.indentationLevel++;
|
|
@@ -337,10 +365,7 @@ export function visitIfStatement(node, context) {
|
|
|
337
365
|
const ifStmt = node;
|
|
338
366
|
const isBinaryExpression = ts.isBinaryExpression(ifStmt.expression) &&
|
|
339
367
|
constants.booleanOperators.includes(ifStmt.expression.operatorToken.kind);
|
|
340
|
-
const condition = this.visit(ifStmt.expression,
|
|
341
|
-
...context,
|
|
342
|
-
supportedNativeLiterals: isBinaryExpression ? ["boolean"] : undefined,
|
|
343
|
-
});
|
|
368
|
+
const condition = this.visit(ifStmt.expression, context);
|
|
344
369
|
const thenStmt = this.visit(ifStmt.thenStatement, {
|
|
345
370
|
...context,
|
|
346
371
|
isFunctionBody: false,
|
|
@@ -656,7 +681,8 @@ export function visitCatchClause(node, context) {
|
|
|
656
681
|
const exceptionValueCode = `jspp::Exception::exception_to_any_value(${exceptionName})`;
|
|
657
682
|
if (ts.isIdentifier(catchClause.variableDeclaration.name)) {
|
|
658
683
|
const varName = catchClause.variableDeclaration.name.text;
|
|
659
|
-
code +=
|
|
684
|
+
code +=
|
|
685
|
+
`${this.indent()}jspp::AnyValue ${varName} = ${exceptionValueCode};\n`;
|
|
660
686
|
}
|
|
661
687
|
else {
|
|
662
688
|
code += this.generateDestructuring(catchClause.variableDeclaration.name, exceptionValueCode, context) + ";\n";
|
|
@@ -797,6 +823,20 @@ export function visitReturnStatement(node, context) {
|
|
|
797
823
|
!typeInfo.isBuiltin) {
|
|
798
824
|
finalExpr = this.getDerefCode(exprText, this.getJsVarName(expr), context, typeInfo);
|
|
799
825
|
}
|
|
826
|
+
const exprReturnType = this.typeAnalyzer.inferNodeReturnType(expr);
|
|
827
|
+
if (exprReturnType === "number" &&
|
|
828
|
+
context.isInsideNativeLambda &&
|
|
829
|
+
context.isInsideFunction) {
|
|
830
|
+
const funcDecl = this
|
|
831
|
+
.findEnclosingFunctionDeclarationFromReturnStatement(expr);
|
|
832
|
+
if (funcDecl) {
|
|
833
|
+
const funcReturnType = this.typeAnalyzer
|
|
834
|
+
.inferFunctionReturnType(funcDecl);
|
|
835
|
+
if (funcReturnType === "number") {
|
|
836
|
+
finalExpr = `${finalExpr}.as_double()`;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
800
840
|
}
|
|
801
841
|
return `${this.indent()}${returnCmd} ${finalExpr};\n`;
|
|
802
842
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ugo-studio/jspp",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A modern transpiler that converts JavaScript code into high-performance, standard C++23.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -14,14 +14,16 @@
|
|
|
14
14
|
"scripts"
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
|
-
"postinstall": "bun run scripts/setup-compiler.ts",
|
|
17
|
+
"postinstall": "bun run scripts/setup-compiler.ts && bun run scripts/setup-emsdk.ts && bun run scripts/precompile-headers.ts",
|
|
18
18
|
"dev": "bun run src/cli/index.ts",
|
|
19
19
|
"typecheck": "tsc --noEmit",
|
|
20
|
-
"precompile": "bun run scripts/precompile-headers.ts",
|
|
21
20
|
"test": "bun test",
|
|
22
21
|
"build": "tsc",
|
|
23
22
|
"prepack": "bun run build",
|
|
24
|
-
"publish:npm": "npm publish --access=public"
|
|
23
|
+
"publish:npm": "npm publish --access=public",
|
|
24
|
+
"script:setup-compiler": "bun run scripts/setup-compiler.ts",
|
|
25
|
+
"script:setup-emsdk": "bun run scripts/setup-emsdk.ts",
|
|
26
|
+
"script:precompile-headers": "bun run scripts/precompile-headers.ts"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"@types/bun": "latest"
|