@sdeverywhere/runtime 0.2.5 → 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,
@@ -884,18 +896,59 @@ var _NA_ = -Number.MAX_VALUE;
884
896
  // src/js-model/js-model-lookup.ts
885
897
  var JsModelLookup = class {
886
898
  /**
887
- * @param n The number of (x,y) pairs in the lookup.
899
+ * @param size The number of (x,y) pairs in the lookup.
888
900
  * @param data The lookup data, as (x,y) pairs. The length of the array must be
889
901
  * >= 2*n. Note that the data will be stored by reference, so if there is a chance
890
902
  * that the array will be reused or modified by other code, be sure to pass in a
891
903
  * copy of the array.
892
904
  */
893
- constructor(n, data) {
894
- this.n = n;
895
- this.data = data;
896
- if (data.length < n * 2) {
897
- 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;
898
950
  }
951
+ this.invertedData = void 0;
899
952
  this.lastInput = Number.MAX_VALUE;
900
953
  this.lastHitIndex = 0;
901
954
  }
@@ -904,8 +957,8 @@ var JsModelLookup = class {
904
957
  }
905
958
  getValueForY(y) {
906
959
  if (this.invertedData === void 0) {
907
- const numValues = this.n * 2;
908
- const normalData = this.data;
960
+ const numValues = this.activeSize * 2;
961
+ const normalData = this.activeData;
909
962
  const invertedData = Array(numValues);
910
963
  for (let i = 0; i < numValues; i += 2) {
911
964
  invertedData[i] = normalData[i + 1];
@@ -920,11 +973,11 @@ var JsModelLookup = class {
920
973
  * NOTE: The x values are assumed to be monotonically increasing.
921
974
  */
922
975
  getValue(input, useInvertedData, mode) {
923
- if (this.n === 0) {
976
+ if (this.activeSize === 0) {
924
977
  return _NA_;
925
978
  }
926
- const data = useInvertedData ? this.invertedData : this.data;
927
- const max = this.n * 2;
979
+ const data = useInvertedData ? this.invertedData : this.activeData;
980
+ const max = this.activeSize * 2;
928
981
  const useCachedValues = !useInvertedData;
929
982
  let startIndex;
930
983
  if (useCachedValues && input >= this.lastInput) {
@@ -983,10 +1036,10 @@ var JsModelLookup = class {
983
1036
  * no points) or if the provided time is earlier than the first data point.
984
1037
  */
985
1038
  getValueForGameTime(time, defaultValue) {
986
- if (this.n <= 0) {
1039
+ if (this.activeSize <= 0) {
987
1040
  return defaultValue;
988
1041
  }
989
- const x0 = this.data[0];
1042
+ const x0 = this.activeData[0];
990
1043
  if (time < x0) {
991
1044
  return defaultValue;
992
1045
  }
@@ -1001,33 +1054,34 @@ var JsModelLookup = class {
1001
1054
  * lookup behavior, so we implement it as a separate method here.
1002
1055
  */
1003
1056
  getValueBetweenTimes(input, mode) {
1004
- if (this.n === 0) {
1057
+ if (this.activeSize === 0) {
1005
1058
  return _NA_;
1006
1059
  }
1007
- const max = this.n * 2;
1060
+ const data = this.activeData;
1061
+ const max = this.activeSize * 2;
1008
1062
  switch (mode) {
1009
1063
  case "forward": {
1010
1064
  input = Math.floor(input);
1011
1065
  for (let xi = 0; xi < max; xi += 2) {
1012
- const x = this.data[xi];
1066
+ const x = data[xi];
1013
1067
  if (x >= input) {
1014
- return this.data[xi + 1];
1068
+ return data[xi + 1];
1015
1069
  }
1016
1070
  }
1017
- return this.data[max - 1];
1071
+ return data[max - 1];
1018
1072
  }
1019
1073
  case "backward": {
1020
1074
  input = Math.floor(input);
1021
1075
  for (let xi = 2; xi < max; xi += 2) {
1022
- const x = this.data[xi];
1076
+ const x = data[xi];
1023
1077
  if (x >= input) {
1024
- return this.data[xi - 1];
1078
+ return data[xi - 1];
1025
1079
  }
1026
1080
  }
1027
1081
  if (max >= 4) {
1028
- return this.data[max - 3];
1082
+ return data[max - 3];
1029
1083
  } else {
1030
- return this.data[1];
1084
+ return data[1];
1031
1085
  }
1032
1086
  }
1033
1087
  case "interpolate":
@@ -1039,17 +1093,17 @@ var JsModelLookup = class {
1039
1093
  throw new Error(msg);
1040
1094
  }
1041
1095
  for (let xi = 2; xi < max; xi += 2) {
1042
- const x = this.data[xi];
1096
+ const x = data[xi];
1043
1097
  if (x >= input) {
1044
- const last_x = this.data[xi - 2];
1045
- const last_y = this.data[xi - 1];
1046
- 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];
1047
1101
  const dx = x - last_x;
1048
1102
  const dy = y - last_y;
1049
1103
  return last_y + dy / dx * (input - last_x);
1050
1104
  }
1051
1105
  }
1052
- return this.data[max - 1];
1106
+ return data[max - 1];
1053
1107
  }
1054
1108
  }
1055
1109
  }
@@ -1500,7 +1554,8 @@ var MockJsModel = class {
1500
1554
  if (varId === void 0) {
1501
1555
  throw new Error(`No lookup variable found for spec ${varSpec}`);
1502
1556
  }
1503
- 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));
1504
1559
  }
1505
1560
  // from JsModel interface
1506
1561
  storeOutputs(storeValue) {
@@ -1629,14 +1684,21 @@ var WasmModel = class {
1629
1684
  } else {
1630
1685
  subIndicesAddress = 0;
1631
1686
  }
1632
- const numLookupElements = lookupDef.points.length;
1633
- if (this.lookupDataBuffer === void 0 || this.lookupDataBuffer.numElements < numLookupElements) {
1634
- (_c = this.lookupDataBuffer) == null ? void 0 : _c.dispose();
1635
- 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;
1636
1701
  }
1637
- this.lookupDataBuffer.getArrayView().set(lookupDef.points);
1638
- const pointsAddress = this.lookupDataBuffer.getAddress();
1639
- const numPoints = numLookupElements / 2;
1640
1702
  const varIndex = varSpec.varIndex;
1641
1703
  this.wasmSetLookup(varIndex, subIndicesAddress, pointsAddress, numPoints);
1642
1704
  }