@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 +119 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +52 -10
- package/dist/index.d.ts +52 -10
- package/dist/index.js +119 -51
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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 (
|
|
311
|
-
|
|
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 =
|
|
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
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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
|
-
|
|
595
|
-
|
|
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
|
|
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(
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
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.
|
|
902
|
-
const normalData = this.
|
|
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.
|
|
976
|
+
if (this.activeSize === 0) {
|
|
918
977
|
return _NA_;
|
|
919
978
|
}
|
|
920
|
-
const data = useInvertedData ? this.invertedData : this.
|
|
921
|
-
const max = this.
|
|
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.
|
|
1039
|
+
if (this.activeSize <= 0) {
|
|
981
1040
|
return defaultValue;
|
|
982
1041
|
}
|
|
983
|
-
const x0 = this.
|
|
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.
|
|
1057
|
+
if (this.activeSize === 0) {
|
|
999
1058
|
return _NA_;
|
|
1000
1059
|
}
|
|
1001
|
-
const
|
|
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 =
|
|
1066
|
+
const x = data[xi];
|
|
1007
1067
|
if (x >= input) {
|
|
1008
|
-
return
|
|
1068
|
+
return data[xi + 1];
|
|
1009
1069
|
}
|
|
1010
1070
|
}
|
|
1011
|
-
return
|
|
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 =
|
|
1076
|
+
const x = data[xi];
|
|
1017
1077
|
if (x >= input) {
|
|
1018
|
-
return
|
|
1078
|
+
return data[xi - 1];
|
|
1019
1079
|
}
|
|
1020
1080
|
}
|
|
1021
1081
|
if (max >= 4) {
|
|
1022
|
-
return
|
|
1082
|
+
return data[max - 3];
|
|
1023
1083
|
} else {
|
|
1024
|
-
return
|
|
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 =
|
|
1096
|
+
const x = data[xi];
|
|
1037
1097
|
if (x >= input) {
|
|
1038
|
-
const last_x =
|
|
1039
|
-
const last_y =
|
|
1040
|
-
const y =
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
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
|
}
|