@sdeverywhere/runtime 0.2.4 → 0.2.6

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.cjs CHANGED
@@ -251,7 +251,7 @@ function encodeVarIndices(varSpecs, indicesArray) {
251
251
  }
252
252
  }
253
253
  function getEncodedLookupBufferLengths(lookupDefs) {
254
- var _a;
254
+ var _a, _b;
255
255
  let lookupIndicesLength = 1;
256
256
  let lookupsLength = 0;
257
257
  for (const lookupDef of lookupDefs) {
@@ -263,7 +263,7 @@ function getEncodedLookupBufferLengths(lookupDefs) {
263
263
  const subCount = ((_a = varSpec.subscriptIndices) == null ? void 0 : _a.length) || 0;
264
264
  lookupIndicesLength += subCount;
265
265
  lookupIndicesLength += 2;
266
- lookupsLength += lookupDef.points.length;
266
+ lookupsLength += ((_b = lookupDef.points) == null ? void 0 : _b.length) || 0;
267
267
  }
268
268
  return {
269
269
  lookupIndicesLength,
@@ -283,10 +283,15 @@ function encodeLookups(lookupDefs, lookupIndicesArray, lookupsArray) {
283
283
  for (let i = 0; i < subCount; i++) {
284
284
  lookupIndicesArray[li++] = subs[i];
285
285
  }
286
- lookupIndicesArray[li++] = lookupDataOffset;
287
- lookupIndicesArray[li++] = lookupDef.points.length;
288
- lookupsArray == null ? void 0 : lookupsArray.set(lookupDef.points, lookupDataOffset);
289
- lookupDataOffset += lookupDef.points.length;
286
+ if (lookupDef.points !== void 0) {
287
+ lookupIndicesArray[li++] = lookupDataOffset;
288
+ lookupIndicesArray[li++] = lookupDef.points.length;
289
+ lookupsArray == null ? void 0 : lookupsArray.set(lookupDef.points, lookupDataOffset);
290
+ lookupDataOffset += lookupDef.points.length;
291
+ } else {
292
+ lookupIndicesArray[li++] = -1;
293
+ lookupIndicesArray[li++] = 0;
294
+ }
290
295
  }
291
296
  }
292
297
  function decodeLookups(lookupIndicesArray, lookupsArray) {
@@ -307,10 +312,14 @@ function decodeLookups(lookupIndicesArray, lookupsArray) {
307
312
  subscriptIndices
308
313
  };
309
314
  let points;
310
- if (lookupsArray) {
311
- points = lookupsArray.slice(lookupDataOffset, lookupDataOffset + lookupDataLength);
315
+ if (lookupDataOffset >= 0) {
316
+ if (lookupsArray) {
317
+ points = lookupsArray.slice(lookupDataOffset, lookupDataOffset + lookupDataLength);
318
+ } else {
319
+ points = new Float64Array(0);
320
+ }
312
321
  } else {
313
- points = new Float64Array(0);
322
+ points = void 0;
314
323
  }
315
324
  lookupDefs.push({
316
325
  varRef: {
@@ -324,11 +333,14 @@ function decodeLookups(lookupIndicesArray, lookupsArray) {
324
333
 
325
334
  // src/_shared/lookup-def.ts
326
335
  function createLookupDef(varRef, points) {
327
- const flatPoints = new Float64Array(points.length * 2);
328
- let i = 0;
329
- for (const p of points) {
330
- flatPoints[i++] = p.x;
331
- flatPoints[i++] = p.y;
336
+ let flatPoints;
337
+ if (points) {
338
+ flatPoints = new Float64Array(points.length * 2);
339
+ let i = 0;
340
+ for (const p of points) {
341
+ flatPoints[i++] = p.x;
342
+ flatPoints[i++] = p.y;
343
+ }
332
344
  }
333
345
  return {
334
346
  varRef,
@@ -591,8 +603,14 @@ var BufferedRunModelParams = class {
591
603
  }
592
604
  // from RunModelParams interface
593
605
  storeOutputs(array) {
594
- var _a;
595
- (_a = this.outputs.view) == null ? void 0 : _a.set(array);
606
+ if (this.outputs.view === void 0) {
607
+ return;
608
+ }
609
+ if (array.length > this.outputs.view.length) {
610
+ this.outputs.view.set(array.subarray(0, this.outputs.view.length));
611
+ } else {
612
+ this.outputs.view.set(array);
613
+ }
596
614
  }
597
615
  // from RunModelParams interface
598
616
  getLookups() {
@@ -878,18 +896,59 @@ var _NA_ = -Number.MAX_VALUE;
878
896
  // src/js-model/js-model-lookup.ts
879
897
  var JsModelLookup = class {
880
898
  /**
881
- * @param n The number of (x,y) pairs in the lookup.
899
+ * @param size The number of (x,y) pairs in the lookup.
882
900
  * @param data The lookup data, as (x,y) pairs. The length of the array must be
883
901
  * >= 2*n. Note that the data will be stored by reference, so if there is a chance
884
902
  * that the array will be reused or modified by other code, be sure to pass in a
885
903
  * copy of the array.
886
904
  */
887
- constructor(n, data) {
888
- this.n = n;
889
- this.data = data;
890
- if (data.length < n * 2) {
891
- throw new Error(`Lookup data array length must be >= 2*size (length=${data.length} size=${n}`);
905
+ constructor(size, data) {
906
+ if (data && data.length < size * 2) {
907
+ throw new Error(`Lookup data array length must be >= 2*size (length=${data.length} size=${size}`);
908
+ }
909
+ this.originalData = data;
910
+ this.originalSize = size;
911
+ this.dynamicData = void 0;
912
+ this.dynamicSize = 0;
913
+ this.activeData = this.originalData;
914
+ this.activeSize = this.originalSize;
915
+ this.lastInput = Number.MAX_VALUE;
916
+ this.lastHitIndex = 0;
917
+ }
918
+ /**
919
+ * Set new data for this lookup instance, or restore the original data.
920
+ *
921
+ * If `data` is undefined, the original data that was supplied to the constructor will
922
+ * be restored as the "active" data. Otherwise, `data` will be copied to an internal
923
+ * data buffer, which will be the "active" data. If `size` is greater than the size
924
+ * passed to previous calls, the internal data buffer will be grown as needed.
925
+ *
926
+ * @param size The number of (x,y) pairs in the lookup.
927
+ * @param data The lookup data, as (x,y) pairs. The length of the array must be
928
+ * >= 2*n. Note that the data will be copied into an internal data buffer, so it
929
+ * is not necessary to defensively copy data before calling this method.
930
+ */
931
+ setData(size, data) {
932
+ if (data) {
933
+ if (data.length < size * 2) {
934
+ throw new Error(`Lookup data array length must be >= 2*size (length=${data.length} size=${size}`);
935
+ }
936
+ const dataLengthInElems = size * 2;
937
+ if (this.dynamicData === void 0 || dataLengthInElems > this.dynamicData.length) {
938
+ this.dynamicData = new Float64Array(dataLengthInElems);
939
+ }
940
+ this.dynamicSize = size;
941
+ if (size > 0) {
942
+ const subarray = data.subarray(0, dataLengthInElems);
943
+ this.dynamicData.set(subarray);
944
+ }
945
+ this.activeData = this.dynamicData;
946
+ this.activeSize = this.dynamicSize;
947
+ } else {
948
+ this.activeData = this.originalData;
949
+ this.activeSize = this.originalSize;
892
950
  }
951
+ this.invertedData = void 0;
893
952
  this.lastInput = Number.MAX_VALUE;
894
953
  this.lastHitIndex = 0;
895
954
  }
@@ -898,8 +957,8 @@ var JsModelLookup = class {
898
957
  }
899
958
  getValueForY(y) {
900
959
  if (this.invertedData === void 0) {
901
- const numValues = this.n * 2;
902
- const normalData = this.data;
960
+ const numValues = this.activeSize * 2;
961
+ const normalData = this.activeData;
903
962
  const invertedData = Array(numValues);
904
963
  for (let i = 0; i < numValues; i += 2) {
905
964
  invertedData[i] = normalData[i + 1];
@@ -914,11 +973,11 @@ var JsModelLookup = class {
914
973
  * NOTE: The x values are assumed to be monotonically increasing.
915
974
  */
916
975
  getValue(input, useInvertedData, mode) {
917
- if (this.n === 0) {
976
+ if (this.activeSize === 0) {
918
977
  return _NA_;
919
978
  }
920
- const data = useInvertedData ? this.invertedData : this.data;
921
- const max = this.n * 2;
979
+ const data = useInvertedData ? this.invertedData : this.activeData;
980
+ const max = this.activeSize * 2;
922
981
  const useCachedValues = !useInvertedData;
923
982
  let startIndex;
924
983
  if (useCachedValues && input >= this.lastInput) {
@@ -977,10 +1036,10 @@ var JsModelLookup = class {
977
1036
  * no points) or if the provided time is earlier than the first data point.
978
1037
  */
979
1038
  getValueForGameTime(time, defaultValue) {
980
- if (this.n <= 0) {
1039
+ if (this.activeSize <= 0) {
981
1040
  return defaultValue;
982
1041
  }
983
- const x0 = this.data[0];
1042
+ const x0 = this.activeData[0];
984
1043
  if (time < x0) {
985
1044
  return defaultValue;
986
1045
  }
@@ -995,33 +1054,34 @@ var JsModelLookup = class {
995
1054
  * lookup behavior, so we implement it as a separate method here.
996
1055
  */
997
1056
  getValueBetweenTimes(input, mode) {
998
- if (this.n === 0) {
1057
+ if (this.activeSize === 0) {
999
1058
  return _NA_;
1000
1059
  }
1001
- const max = this.n * 2;
1060
+ const data = this.activeData;
1061
+ const max = this.activeSize * 2;
1002
1062
  switch (mode) {
1003
1063
  case "forward": {
1004
1064
  input = Math.floor(input);
1005
1065
  for (let xi = 0; xi < max; xi += 2) {
1006
- const x = this.data[xi];
1066
+ const x = data[xi];
1007
1067
  if (x >= input) {
1008
- return this.data[xi + 1];
1068
+ return data[xi + 1];
1009
1069
  }
1010
1070
  }
1011
- return this.data[max - 1];
1071
+ return data[max - 1];
1012
1072
  }
1013
1073
  case "backward": {
1014
1074
  input = Math.floor(input);
1015
1075
  for (let xi = 2; xi < max; xi += 2) {
1016
- const x = this.data[xi];
1076
+ const x = data[xi];
1017
1077
  if (x >= input) {
1018
- return this.data[xi - 1];
1078
+ return data[xi - 1];
1019
1079
  }
1020
1080
  }
1021
1081
  if (max >= 4) {
1022
- return this.data[max - 3];
1082
+ return data[max - 3];
1023
1083
  } else {
1024
- return this.data[1];
1084
+ return data[1];
1025
1085
  }
1026
1086
  }
1027
1087
  case "interpolate":
@@ -1033,17 +1093,17 @@ var JsModelLookup = class {
1033
1093
  throw new Error(msg);
1034
1094
  }
1035
1095
  for (let xi = 2; xi < max; xi += 2) {
1036
- const x = this.data[xi];
1096
+ const x = data[xi];
1037
1097
  if (x >= input) {
1038
- const last_x = this.data[xi - 2];
1039
- const last_y = this.data[xi - 1];
1040
- const y = this.data[xi + 1];
1098
+ const last_x = data[xi - 2];
1099
+ const last_y = data[xi - 1];
1100
+ const y = data[xi + 1];
1041
1101
  const dx = x - last_x;
1042
1102
  const dy = y - last_y;
1043
1103
  return last_y + dy / dx * (input - last_x);
1044
1104
  }
1045
1105
  }
1046
- return this.data[max - 1];
1106
+ return data[max - 1];
1047
1107
  }
1048
1108
  }
1049
1109
  }
@@ -1494,7 +1554,8 @@ var MockJsModel = class {
1494
1554
  if (varId === void 0) {
1495
1555
  throw new Error(`No lookup variable found for spec ${varSpec}`);
1496
1556
  }
1497
- this.lookups.set(varId, new JsModelLookup(points.length / 2, points));
1557
+ const numPoints = points ? points.length / 2 : 0;
1558
+ this.lookups.set(varId, new JsModelLookup(numPoints, points));
1498
1559
  }
1499
1560
  // from JsModel interface
1500
1561
  storeOutputs(storeValue) {
@@ -1623,14 +1684,21 @@ var WasmModel = class {
1623
1684
  } else {
1624
1685
  subIndicesAddress = 0;
1625
1686
  }
1626
- const numLookupElements = lookupDef.points.length;
1627
- if (this.lookupDataBuffer === void 0 || this.lookupDataBuffer.numElements < numLookupElements) {
1628
- (_c = this.lookupDataBuffer) == null ? void 0 : _c.dispose();
1629
- this.lookupDataBuffer = createFloat64WasmBuffer(this.wasmModule, numLookupElements);
1687
+ let pointsAddress;
1688
+ let numPoints;
1689
+ if (lookupDef.points) {
1690
+ const numLookupElements = lookupDef.points.length;
1691
+ if (this.lookupDataBuffer === void 0 || this.lookupDataBuffer.numElements < numLookupElements) {
1692
+ (_c = this.lookupDataBuffer) == null ? void 0 : _c.dispose();
1693
+ this.lookupDataBuffer = createFloat64WasmBuffer(this.wasmModule, numLookupElements);
1694
+ }
1695
+ this.lookupDataBuffer.getArrayView().set(lookupDef.points);
1696
+ pointsAddress = this.lookupDataBuffer.getAddress();
1697
+ numPoints = numLookupElements / 2;
1698
+ } else {
1699
+ pointsAddress = 0;
1700
+ numPoints = 0;
1630
1701
  }
1631
- this.lookupDataBuffer.getArrayView().set(lookupDef.points);
1632
- const pointsAddress = this.lookupDataBuffer.getAddress();
1633
- const numPoints = numLookupElements / 2;
1634
1702
  const varIndex = varSpec.varIndex;
1635
1703
  this.wasmSetLookup(varIndex, subIndicesAddress, pointsAddress, numPoints);
1636
1704
  }