@foxystar/molang 0.1.0 → 0.1.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/esm/runtime/context.js
CHANGED
|
@@ -6,7 +6,11 @@ export const DEFAULT_CONTEXT = {
|
|
|
6
6
|
math: MolangMath,
|
|
7
7
|
variable: {},
|
|
8
8
|
temp: {},
|
|
9
|
-
context: {
|
|
9
|
+
context: {
|
|
10
|
+
log: (...args) => {
|
|
11
|
+
console.log(...args);
|
|
12
|
+
}
|
|
13
|
+
},
|
|
10
14
|
query: {
|
|
11
15
|
block_state: (_name) => {
|
|
12
16
|
throw new Error("This function call can only be used in block context");
|
package/esm/runtime/runtime.d.ts
CHANGED
package/esm/runtime/runtime.js
CHANGED
|
@@ -109,7 +109,8 @@ export class MolangRuntime {
|
|
|
109
109
|
throw new ReturnSignal(value);
|
|
110
110
|
}
|
|
111
111
|
case "ConditionalStatement": {
|
|
112
|
-
|
|
112
|
+
let condition = this.evaluate(stmt.condition);
|
|
113
|
+
condition = this.resolveCallable(condition, stmt.condition);
|
|
113
114
|
if (this.toBoolean(condition)) {
|
|
114
115
|
return this.evaluateStatement(stmt.then);
|
|
115
116
|
}
|
|
@@ -405,9 +406,32 @@ export class MolangRuntime {
|
|
|
405
406
|
return Number(value) || 0;
|
|
406
407
|
}
|
|
407
408
|
toBoolean(v) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
409
|
+
if (this.options.strict) {
|
|
410
|
+
return Boolean(v);
|
|
411
|
+
}
|
|
412
|
+
if (typeof v === "boolean")
|
|
413
|
+
return v;
|
|
414
|
+
if (typeof v === "number")
|
|
415
|
+
return v !== 0;
|
|
416
|
+
// undefined / null -> false
|
|
417
|
+
if (v === undefined || v === null)
|
|
418
|
+
return false;
|
|
419
|
+
// objects -> true
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
resolveCallable(value, expr) {
|
|
423
|
+
if (typeof value === "function" && value.length === 0) {
|
|
424
|
+
try {
|
|
425
|
+
return value();
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
if (error instanceof MolangRuntimeError) {
|
|
429
|
+
throw error;
|
|
430
|
+
}
|
|
431
|
+
throw new MolangRuntimeError(error instanceof Error ? error.message : String(error), expr, this.source);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return value;
|
|
411
435
|
}
|
|
412
436
|
applyOperator(operator, left, right, expr) {
|
|
413
437
|
switch (operator) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foxystar/molang",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A fast, extensible, and safe implementation of the Molang expression language.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "FoxyStar Studios",
|
|
@@ -32,6 +32,9 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@foxystar/math": "^0.1.3"
|
|
34
34
|
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@minecraft/server": "^2.9.0-rc.1.26.40-preview.29"
|
|
37
|
+
},
|
|
35
38
|
"devDependencies": {
|
|
36
39
|
"@types/node": "^20.9.0"
|
|
37
40
|
},
|
|
@@ -9,7 +9,11 @@ exports.DEFAULT_CONTEXT = {
|
|
|
9
9
|
math: math_js_1.MolangMath,
|
|
10
10
|
variable: {},
|
|
11
11
|
temp: {},
|
|
12
|
-
context: {
|
|
12
|
+
context: {
|
|
13
|
+
log: (...args) => {
|
|
14
|
+
console.log(...args);
|
|
15
|
+
}
|
|
16
|
+
},
|
|
13
17
|
query: {
|
|
14
18
|
block_state: (_name) => {
|
|
15
19
|
throw new Error("This function call can only be used in block context");
|
|
@@ -138,7 +138,8 @@ class MolangRuntime {
|
|
|
138
138
|
throw new ReturnSignal(value);
|
|
139
139
|
}
|
|
140
140
|
case "ConditionalStatement": {
|
|
141
|
-
|
|
141
|
+
let condition = this.evaluate(stmt.condition);
|
|
142
|
+
condition = this.resolveCallable(condition, stmt.condition);
|
|
142
143
|
if (this.toBoolean(condition)) {
|
|
143
144
|
return this.evaluateStatement(stmt.then);
|
|
144
145
|
}
|
|
@@ -434,9 +435,32 @@ class MolangRuntime {
|
|
|
434
435
|
return Number(value) || 0;
|
|
435
436
|
}
|
|
436
437
|
toBoolean(v) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
438
|
+
if (this.options.strict) {
|
|
439
|
+
return Boolean(v);
|
|
440
|
+
}
|
|
441
|
+
if (typeof v === "boolean")
|
|
442
|
+
return v;
|
|
443
|
+
if (typeof v === "number")
|
|
444
|
+
return v !== 0;
|
|
445
|
+
// undefined / null -> false
|
|
446
|
+
if (v === undefined || v === null)
|
|
447
|
+
return false;
|
|
448
|
+
// objects -> true
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
resolveCallable(value, expr) {
|
|
452
|
+
if (typeof value === "function" && value.length === 0) {
|
|
453
|
+
try {
|
|
454
|
+
return value();
|
|
455
|
+
}
|
|
456
|
+
catch (error) {
|
|
457
|
+
if (error instanceof error_js_1.MolangRuntimeError) {
|
|
458
|
+
throw error;
|
|
459
|
+
}
|
|
460
|
+
throw new error_js_1.MolangRuntimeError(error instanceof Error ? error.message : String(error), expr, this.source);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return value;
|
|
440
464
|
}
|
|
441
465
|
applyOperator(operator, left, right, expr) {
|
|
442
466
|
switch (operator) {
|