@foxystar/molang 0.1.0 → 0.1.1

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.
@@ -27,6 +27,7 @@ export declare class MolangRuntime {
27
27
  evaluate(expr: Expr): any;
28
28
  private toNumber;
29
29
  private toBoolean;
30
+ private resolveCallable;
30
31
  private applyOperator;
31
32
  static normalizePath(path: string): string[];
32
33
  private suggestPath;
@@ -109,7 +109,8 @@ export class MolangRuntime {
109
109
  throw new ReturnSignal(value);
110
110
  }
111
111
  case "ConditionalStatement": {
112
- const condition = this.evaluate(stmt.condition);
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
- return this.options.strict
409
- ? Boolean(v)
410
- : this.toNumber(v) !== 0;
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.0",
3
+ "version": "0.1.1",
4
4
  "description": "A fast, extensible, and safe implementation of the Molang expression language.",
5
5
  "author": {
6
6
  "name": "FoxyStar Studios",
@@ -27,6 +27,7 @@ export declare class MolangRuntime {
27
27
  evaluate(expr: Expr): any;
28
28
  private toNumber;
29
29
  private toBoolean;
30
+ private resolveCallable;
30
31
  private applyOperator;
31
32
  static normalizePath(path: string): string[];
32
33
  private suggestPath;
@@ -138,7 +138,8 @@ class MolangRuntime {
138
138
  throw new ReturnSignal(value);
139
139
  }
140
140
  case "ConditionalStatement": {
141
- const condition = this.evaluate(stmt.condition);
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
- return this.options.strict
438
- ? Boolean(v)
439
- : this.toNumber(v) !== 0;
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) {