starlight-cli 1.0.31 → 1.0.32
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/index.js +27 -7
- package/package.json +1 -1
- package/src/evaluator.js +27 -7
package/dist/index.js
CHANGED
|
@@ -1497,14 +1497,34 @@ async evaluate(node, env = this.global) {
|
|
|
1497
1497
|
return val;
|
|
1498
1498
|
}
|
|
1499
1499
|
case 'NewExpression': {
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1500
|
+
const callee = await this.evaluate(node.callee, env);
|
|
1501
|
+
|
|
1502
|
+
if (typeof callee === 'object' && callee.body) {
|
|
1503
|
+
// This is a Starlight "func" object — wrap it in JS function
|
|
1504
|
+
const Constructor = function(...args) {
|
|
1505
|
+
const newEnv = new Environment(callee.env);
|
|
1506
|
+
newEnv.define('this', this); // bind 'this' for the constructor
|
|
1507
|
+
for (let i = 0; i < callee.params.length; i++) {
|
|
1508
|
+
newEnv.define(callee.params[i], args[i]);
|
|
1503
1509
|
}
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1510
|
+
return this.evaluate(callee.body, newEnv);
|
|
1511
|
+
}.bind(this);
|
|
1512
|
+
|
|
1513
|
+
const args = [];
|
|
1514
|
+
for (const a of node.arguments) args.push(await this.evaluate(a, env));
|
|
1515
|
+
|
|
1516
|
+
return new Constructor(...args);
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
if (typeof callee !== 'function') {
|
|
1520
|
+
throw new Error('NewExpression callee is not a function');
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
const args = [];
|
|
1524
|
+
for (const a of node.arguments) args.push(await this.evaluate(a, env));
|
|
1525
|
+
return new callee(...args);
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1508
1528
|
default:
|
|
1509
1529
|
throw new Error(`Unknown node type in evaluator: ${node.type}`);
|
|
1510
1530
|
}
|
package/package.json
CHANGED
package/src/evaluator.js
CHANGED
|
@@ -154,14 +154,34 @@ async evaluate(node, env = this.global) {
|
|
|
154
154
|
return val;
|
|
155
155
|
}
|
|
156
156
|
case 'NewExpression': {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
157
|
+
const callee = await this.evaluate(node.callee, env);
|
|
158
|
+
|
|
159
|
+
if (typeof callee === 'object' && callee.body) {
|
|
160
|
+
// This is a Starlight "func" object — wrap it in JS function
|
|
161
|
+
const Constructor = function(...args) {
|
|
162
|
+
const newEnv = new Environment(callee.env);
|
|
163
|
+
newEnv.define('this', this); // bind 'this' for the constructor
|
|
164
|
+
for (let i = 0; i < callee.params.length; i++) {
|
|
165
|
+
newEnv.define(callee.params[i], args[i]);
|
|
160
166
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
167
|
+
return this.evaluate(callee.body, newEnv);
|
|
168
|
+
}.bind(this);
|
|
169
|
+
|
|
170
|
+
const args = [];
|
|
171
|
+
for (const a of node.arguments) args.push(await this.evaluate(a, env));
|
|
172
|
+
|
|
173
|
+
return new Constructor(...args);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (typeof callee !== 'function') {
|
|
177
|
+
throw new Error('NewExpression callee is not a function');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const args = [];
|
|
181
|
+
for (const a of node.arguments) args.push(await this.evaluate(a, env));
|
|
182
|
+
return new callee(...args);
|
|
183
|
+
}
|
|
184
|
+
|
|
165
185
|
default:
|
|
166
186
|
throw new Error(`Unknown node type in evaluator: ${node.type}`);
|
|
167
187
|
}
|