@ball-lang/engine 1.3.5 → 1.3.7

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.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EACL,UAAU,IAAI,cAAc,GAC7B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,wEAAwE;AACxE,0EAA0E;AAC1E,4EAA4E;AAC5E,0EAA0E;AAC1E,MAAM,MAAM,GAAG,iBAAiB,CAAC,SAAgB,CAAC,CAAC;AACnD,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;AACnC,MAAM,qBAAqB,GAAG,MAAM,CAAC,qBAAqB,CAAC;AAC3D,MAAM,gBAAgB,GAAG,SAAS,CAAC,gBAAgB,CAAC;AACpD,MAAM,yBAAyB,GAAG,MAAM,CAAC,yBAAyB,CAAC;AACnE,MAAM,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;AAC/C,MAAM,mBAAmB,GAAG,MAAM,CAAC,mBAAmB,CAAC;AACvD,MAAM,kBAAkB,GAAG,MAAM,CAAC,kBAAkB,CAAC;AAwBrD,MAAM,OAAO,UAAU;IACb,eAAe,CAAiB;IAChC,OAAO,GAAa,EAAE,CAAC;IAE/B,YAAY,OAAY,EAAE,UAA6B,EAAE;QACvD,yEAAyE;QACzE,2EAA2E;QAC3E,wDAAwD;QACxD,MAAM,MAAM,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;QAC3E,MAAM,U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UAAU,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;QAC5D,eAAe,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QACtC,mBAAmB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAE1C,0EAA0E;QAC1E,4EAA4E;QAC5E,gEAAgE;IAClE,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,GAAG;QACP,MAAM,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,CAAC;QACjC,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,gFAAgF;IAChF,SAAS;QACP,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF;AAED,iEAAiE;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ball-lang/engine",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "description": "Ball language engine — interprets Ball programs in TypeScript/JavaScript. Runs in Node.js and browsers.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -677,17 +677,9 @@ function __ball_cascade(target: any, ops: any[]): any {
677
677
  Object.defineProperty(sp, 'isNotEmpty', {
678
678
  configurable: true, get() { return this.length !== 0; },
679
679
  });
680
- // Make undefined/null safe for .isEmpty/.isNotEmpty via a global wrapper.
681
- // Proto3 JSON omits empty strings, so code like call.module.isEmpty needs
682
- // to handle undefined gracefully.
683
- const _origObjDefProp = Object.defineProperty;
684
- for (const boolProp of ['isEmpty', 'isNotEmpty']) {
685
- if (!(undefined as any)?.[boolProp]) {
686
- // Can't add properties to undefined/null, but we handle it in the
687
- // generated code via ?.isEmpty fallback patterns. The preamble's
688
- // protoWrap handles most cases.
689
- }
690
- }
680
+ // Note: undefined/null safety for .isEmpty/.isNotEmpty is handled in the
681
+ // generated code via optional-chaining (?.isEmpty) patterns and protoWrap;
682
+ // properties cannot be installed on undefined/null directly.
691
683
  // Dart String methods not on JS String.
692
684
  if (!sp.contains) sp.contains = function (s: any) { return this.includes(s); };
693
685
  if (!sp.replaceFirst) sp.replaceFirst = function (from: any, to: any) {
@@ -893,7 +885,6 @@ function __ball_cascade(target: any, ops: any[]): any {
893
885
  return Object.keys(this).filter((k: string) => !k.startsWith('__')).length;
894
886
  });
895
887
 
896
- // Number polyfills moved earlier (see "Dart Number polyfills" block above).
897
888
  // runtimeType — Dart's Object.runtimeType. Returns the Dart-style
898
889
  // type name for any JS value. Used by the compiled engine for type
899
890
  // checking and error messages.
@@ -1894,6 +1885,10 @@ export class BallEngine {
1894
1885
  } else {
1895
1886
  if ((('arg' + __ball_to_string(i)) in inputMap)) {
1896
1887
  scope.bind(p, __ball_index(inputMap, ('arg' + __ball_to_string(i))));
1888
+ } else {
1889
+ if ((((__ball_eq(i, 0) && __ball_eq(params.length, 1)) && ('value' in inputMap)) && this._isSetter(func))) {
1890
+ scope.bind(p, __ball_index(inputMap, 'value'));
1891
+ }
1897
1892
  }
1898
1893
  }
1899
1894
  }
@@ -627,9 +627,23 @@ export function createEngineSetup(mod: EngineModule) {
627
627
  // for functions that the compiled engine's _buildStdDispatch doesn't cover
628
628
  // (collection higher-order functions, string helpers, etc.).
629
629
 
630
- function registerExtraStdFunctions(stdHandler: StdHandler): void {
630
+ /**
631
+ * Register the hand-written std-function overrides on `stdHandler`.
632
+ *
633
+ * `engine` is the compiled BallEngine instance the handler is wired to.
634
+ * It is REQUIRED for handlers that allocate proportionally to their input
635
+ * (list_filled / list_generate): they charge the allocation against the
636
+ * engine's memory accounting so `maxMemoryBytes` is enforced (#24). The
637
+ * compiled engine's handler-call protocol passes a bound `callFunction`
638
+ * (not the engine) as the third argument, so the engine must be captured
639
+ * here at registration time.
640
+ */
641
+ function registerExtraStdFunctions(stdHandler: StdHandler, engine: any): void {
631
642
  const _r = stdHandler.register.bind(stdHandler);
632
643
  const _m = (i: any) => (typeof i === 'object' && i !== null) ? i : {};
644
+ // Mirrors the compiled engine's `_ballPointerBytes` accounting constant.
645
+ const _ptrBytes = 8;
646
+ const _track = (bytes: number) => engine._trackMemoryAllocation(bytes);
633
647
 
634
648
  // ── Collection: list_* ─────────────────────────────────────────────
635
649
  _r('list_foreach', async (i: any) => {
@@ -1054,12 +1068,17 @@ export function createEngineSetup(mod: EngineModule) {
1054
1068
  return val;
1055
1069
  });
1056
1070
 
1057
- // Override dart_list_generate (compiled version's lambda calling may fail)
1071
+ // Override dart_list_generate (compiled version's lambda calling may fail).
1072
+ // These overrides shadow the compiled `_stdListFilled`/`_stdListGenerate`
1073
+ // implementations, which already track allocations — so they must charge
1074
+ // the allocation against the engine's memory accounting themselves
1075
+ // (maxMemoryBytes, #24).
1058
1076
  _r('dart_list_generate', async (i: any) => {
1059
1077
  const m = _m(i);
1060
1078
  const count = Number(m['count'] ?? m['arg0'] ?? 0);
1061
1079
  const gen = m['generator'] ?? m['arg1'];
1062
1080
  if (typeof gen !== 'function') return [];
1081
+ _track(Math.max(0, count | 0) * _ptrBytes);
1063
1082
  const result: any[] = [];
1064
1083
  for (let idx = 0; idx < count; idx++) {
1065
1084
  let v = gen(idx);
@@ -1071,11 +1090,13 @@ export function createEngineSetup(mod: EngineModule) {
1071
1090
  _r('dart_list_filled', (i: any) => {
1072
1091
  const m = _m(i);
1073
1092
  const count = Number(m['count'] ?? m['length'] ?? m['arg0'] ?? 0);
1093
+ _track(Math.max(0, count | 0) * _ptrBytes);
1074
1094
  return Array(Math.max(0, count | 0)).fill(m['value'] ?? m['arg1'] ?? null);
1075
1095
  });
1076
1096
  _r('list_filled', (i: any) => {
1077
1097
  const m = _m(i);
1078
1098
  const count = Number(m['count'] ?? m['length'] ?? m['arg0'] ?? 0);
1099
+ _track(Math.max(0, count | 0) * _ptrBytes);
1079
1100
  return Array(Math.max(0, count | 0)).fill(m['value'] ?? m['arg1'] ?? null);
1080
1101
  });
1081
1102
  _r('list_generate', (i: any) => {
@@ -1083,6 +1104,7 @@ export function createEngineSetup(mod: EngineModule) {
1083
1104
  const count = Number(m['count'] ?? m['length'] ?? m['arg0'] ?? 0);
1084
1105
  const fn = m['function'] ?? m['generator'] ?? m['callback'] ?? m['value'];
1085
1106
  if (typeof fn !== 'function') return [];
1107
+ _track(Math.max(0, count | 0) * _ptrBytes);
1086
1108
  const out: any[] = [];
1087
1109
  for (let i2 = 0; i2 < count; i2++) {
1088
1110
  let r = fn(i2);
package/src/index.ts CHANGED
@@ -63,7 +63,22 @@ export class BallEngine {
63
63
  // the `@type` envelope (if present) before normalizing; callers passing an
64
64
  // already-unwrapped Program object are still supported.
65
65
  const parsed = typeof program === 'string' ? JSON.parse(program) : program;
66
- const normalized = protoWrap(unwrapBallFile(parsed));
66
+ const unwrapped = unwrapBallFile(parsed);
67
+ const normalized = protoWrap(unwrapped);
68
+
69
+ // The compiled engine's program-size validation (run() →
70
+ // _validateProgramLimits) calls `program.writeToBuffer()` — the Dart
71
+ // protobuf binary encoding. Plain-JSON programs have no protobuf runtime
72
+ // here, so expose the UTF-8 JSON encoding of the (unwrapped) input as the
73
+ // byte-size source. Non-enumerable so program traversal never sees it.
74
+ if (typeof normalized === 'object' && normalized !== null) {
75
+ Object.defineProperty(normalized, 'writeToBuffer', {
76
+ value: () => new TextEncoder().encode(JSON.stringify(unwrapped)),
77
+ enumerable: false,
78
+ writable: true,
79
+ configurable: true,
80
+ });
81
+ }
67
82
 
68
83
  const stdHandler = new StdModuleHandler();
69
84
  const methodHandler = new MethodDispatchHandler();
@@ -108,7 +123,7 @@ export class BallEngine {
108
123
  patchScopeBindings(this._compiledEngine._globalScope);
109
124
  }
110
125
 
111
- registerExtraStdFunctions(stdHandler);
126
+ registerExtraStdFunctions(stdHandler, this._compiledEngine);
112
127
  seedGlobalScope(this._compiledEngine);
113
128
  patchCompiledEngine(this._compiledEngine);
114
129