@sdeverywhere/runtime 0.2.7 → 0.2.9

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 CHANGED
@@ -206,6 +206,66 @@ function encodeVarIndices(varSpecs, indicesArray) {
206
206
  }
207
207
  }
208
208
  }
209
+ function getEncodedConstantBufferLengths(constantDefs) {
210
+ var _a;
211
+ let constantIndicesLength = 1;
212
+ let constantsLength = 0;
213
+ for (const constantDef of constantDefs) {
214
+ const varSpec = constantDef.varRef.varSpec;
215
+ if (varSpec === void 0) {
216
+ throw new Error("Cannot compute constant buffer lengths until all constant var specs are defined");
217
+ }
218
+ constantIndicesLength += 2;
219
+ const subCount = ((_a = varSpec.subscriptIndices) == null ? void 0 : _a.length) || 0;
220
+ constantIndicesLength += subCount;
221
+ constantsLength += 1;
222
+ }
223
+ return {
224
+ constantIndicesLength,
225
+ constantsLength
226
+ };
227
+ }
228
+ function encodeConstants(constantDefs, constantIndicesArray, constantsArray) {
229
+ let ci = 0;
230
+ constantIndicesArray[ci++] = constantDefs.length;
231
+ let constantDataOffset = 0;
232
+ for (const constantDef of constantDefs) {
233
+ const varSpec = constantDef.varRef.varSpec;
234
+ constantIndicesArray[ci++] = varSpec.varIndex;
235
+ const subs = varSpec.subscriptIndices;
236
+ const subCount = (subs == null ? void 0 : subs.length) || 0;
237
+ constantIndicesArray[ci++] = subCount;
238
+ for (let i = 0; i < subCount; i++) {
239
+ constantIndicesArray[ci++] = subs[i];
240
+ }
241
+ constantsArray[constantDataOffset++] = constantDef.value;
242
+ }
243
+ }
244
+ function decodeConstants(constantIndicesArray, constantsArray) {
245
+ const constantDefs = [];
246
+ let ci = 0;
247
+ const constantCount = constantIndicesArray[ci++];
248
+ for (let i = 0; i < constantCount; i++) {
249
+ const varIndex = constantIndicesArray[ci++];
250
+ const subCount = constantIndicesArray[ci++];
251
+ const subscriptIndices = subCount > 0 ? Array(subCount) : void 0;
252
+ for (let subIndex = 0; subIndex < subCount; subIndex++) {
253
+ subscriptIndices[subIndex] = constantIndicesArray[ci++];
254
+ }
255
+ const varSpec = {
256
+ varIndex,
257
+ subscriptIndices
258
+ };
259
+ const value = constantsArray[i];
260
+ constantDefs.push({
261
+ varRef: {
262
+ varSpec
263
+ },
264
+ value
265
+ });
266
+ }
267
+ return constantDefs;
268
+ }
209
269
  function getEncodedLookupBufferLengths(lookupDefs) {
210
270
  var _a, _b;
211
271
  let lookupIndicesLength = 1;
@@ -287,6 +347,14 @@ function decodeLookups(lookupIndicesArray, lookupsArray) {
287
347
  return lookupDefs;
288
348
  }
289
349
 
350
+ // src/_shared/constant-def.ts
351
+ function createConstantDef(varRef, value) {
352
+ return {
353
+ varRef,
354
+ value
355
+ };
356
+ }
357
+
290
358
  // src/_shared/lookup-def.ts
291
359
  function createLookupDef(varRef, points) {
292
360
  let flatPoints;
@@ -457,7 +525,7 @@ function resolveVarRef(listing, varRef, varKind) {
457
525
  }
458
526
 
459
527
  // src/runnable-model/buffered-run-model-params.ts
460
- var headerLengthInElements = 16;
528
+ var headerLengthInElements = 20;
461
529
  var extrasLengthInElements = 1;
462
530
  var Int32Section = class {
463
531
  constructor() {
@@ -502,6 +570,10 @@ var BufferedRunModelParams = class {
502
570
  this.outputs = new Float64Section();
503
571
  /** The output indices section of the `encoded` buffer. */
504
572
  this.outputIndices = new Int32Section();
573
+ /** The constant values section of the `encoded` buffer. */
574
+ this.constants = new Float64Section();
575
+ /** The constant indices section of the `encoded` buffer. */
576
+ this.constantIndices = new Int32Section();
505
577
  /** The lookup data section of the `encoded` buffer. */
506
578
  this.lookups = new Float64Section();
507
579
  /** The lookup indices section of the `encoded` buffer. */
@@ -569,6 +641,13 @@ var BufferedRunModelParams = class {
569
641
  }
570
642
  }
571
643
  // from RunModelParams interface
644
+ getConstants() {
645
+ if (this.constantIndices.lengthInElements === 0) {
646
+ return void 0;
647
+ }
648
+ return decodeConstants(this.constantIndices.view, this.constants.view);
649
+ }
650
+ // from RunModelParams interface
572
651
  getLookups() {
573
652
  if (this.lookupIndices.lengthInElements === 0) {
574
653
  return void 0;
@@ -613,6 +692,19 @@ var BufferedRunModelParams = class {
613
692
  } else {
614
693
  outputIndicesLengthInElements = 0;
615
694
  }
695
+ let constantsLengthInElements;
696
+ let constantIndicesLengthInElements;
697
+ if ((options == null ? void 0 : options.constants) !== void 0 && options.constants.length > 0) {
698
+ for (const constantDef of options.constants) {
699
+ resolveVarRef(this.listing, constantDef.varRef, "constant");
700
+ }
701
+ const encodedLengths = getEncodedConstantBufferLengths(options.constants);
702
+ constantsLengthInElements = encodedLengths.constantsLength;
703
+ constantIndicesLengthInElements = encodedLengths.constantIndicesLength;
704
+ } else {
705
+ constantsLengthInElements = 0;
706
+ constantIndicesLengthInElements = 0;
707
+ }
616
708
  let lookupsLengthInElements;
617
709
  let lookupIndicesLengthInElements;
618
710
  if ((options == null ? void 0 : options.lookups) !== void 0 && options.lookups.length > 0) {
@@ -640,6 +732,8 @@ var BufferedRunModelParams = class {
640
732
  const inputsOffsetInBytes = section("float64", inputsLengthInElements);
641
733
  const outputsOffsetInBytes = section("float64", outputsLengthInElements);
642
734
  const outputIndicesOffsetInBytes = section("int32", outputIndicesLengthInElements);
735
+ const constantsOffsetInBytes = section("float64", constantsLengthInElements);
736
+ const constantIndicesOffsetInBytes = section("int32", constantIndicesLengthInElements);
643
737
  const lookupsOffsetInBytes = section("float64", lookupsLengthInElements);
644
738
  const lookupIndicesOffsetInBytes = section("int32", lookupIndicesLengthInElements);
645
739
  const requiredLengthInBytes = byteOffset;
@@ -658,6 +752,10 @@ var BufferedRunModelParams = class {
658
752
  headerView[headerIndex++] = outputsLengthInElements;
659
753
  headerView[headerIndex++] = outputIndicesOffsetInBytes;
660
754
  headerView[headerIndex++] = outputIndicesLengthInElements;
755
+ headerView[headerIndex++] = constantsOffsetInBytes;
756
+ headerView[headerIndex++] = constantsLengthInElements;
757
+ headerView[headerIndex++] = constantIndicesOffsetInBytes;
758
+ headerView[headerIndex++] = constantIndicesLengthInElements;
661
759
  headerView[headerIndex++] = lookupsOffsetInBytes;
662
760
  headerView[headerIndex++] = lookupsLengthInElements;
663
761
  headerView[headerIndex++] = lookupIndicesOffsetInBytes;
@@ -666,6 +764,8 @@ var BufferedRunModelParams = class {
666
764
  this.extras.update(this.encoded, extrasOffsetInBytes, extrasLengthInElements);
667
765
  this.outputs.update(this.encoded, outputsOffsetInBytes, outputsLengthInElements);
668
766
  this.outputIndices.update(this.encoded, outputIndicesOffsetInBytes, outputIndicesLengthInElements);
767
+ this.constants.update(this.encoded, constantsOffsetInBytes, constantsLengthInElements);
768
+ this.constantIndices.update(this.encoded, constantIndicesOffsetInBytes, constantIndicesLengthInElements);
669
769
  this.lookups.update(this.encoded, lookupsOffsetInBytes, lookupsLengthInElements);
670
770
  this.lookupIndices.update(this.encoded, lookupIndicesOffsetInBytes, lookupIndicesLengthInElements);
671
771
  const inputsView = this.inputs.view;
@@ -680,6 +780,9 @@ var BufferedRunModelParams = class {
680
780
  if (this.outputIndices.view) {
681
781
  encodeVarIndices(outputVarSpecs, this.outputIndices.view);
682
782
  }
783
+ if (constantIndicesLengthInElements > 0) {
784
+ encodeConstants(options.constants, this.constantIndices.view, this.constants.view);
785
+ }
683
786
  if (lookupIndicesLengthInElements > 0) {
684
787
  encodeLookups(options.lookups, this.lookupIndices.view, this.lookups.view);
685
788
  }
@@ -708,6 +811,10 @@ var BufferedRunModelParams = class {
708
811
  const outputsLengthInElements = headerView[headerIndex++];
709
812
  const outputIndicesOffsetInBytes = headerView[headerIndex++];
710
813
  const outputIndicesLengthInElements = headerView[headerIndex++];
814
+ const constantsOffsetInBytes = headerView[headerIndex++];
815
+ const constantsLengthInElements = headerView[headerIndex++];
816
+ const constantIndicesOffsetInBytes = headerView[headerIndex++];
817
+ const constantIndicesLengthInElements = headerView[headerIndex++];
711
818
  const lookupsOffsetInBytes = headerView[headerIndex++];
712
819
  const lookupsLengthInElements = headerView[headerIndex++];
713
820
  const lookupIndicesOffsetInBytes = headerView[headerIndex++];
@@ -716,9 +823,11 @@ var BufferedRunModelParams = class {
716
823
  const inputsLengthInBytes = inputsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
717
824
  const outputsLengthInBytes = outputsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
718
825
  const outputIndicesLengthInBytes = outputIndicesLengthInElements * Int32Array.BYTES_PER_ELEMENT;
826
+ const constantsLengthInBytes = constantsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
827
+ const constantIndicesLengthInBytes = constantIndicesLengthInElements * Int32Array.BYTES_PER_ELEMENT;
719
828
  const lookupsLengthInBytes = lookupsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
720
829
  const lookupIndicesLengthInBytes = lookupIndicesLengthInElements * Int32Array.BYTES_PER_ELEMENT;
721
- const requiredLengthInBytes = headerLengthInBytes + extrasLengthInBytes + inputsLengthInBytes + outputsLengthInBytes + outputIndicesLengthInBytes + lookupsLengthInBytes + lookupIndicesLengthInBytes;
830
+ const requiredLengthInBytes = headerLengthInBytes + extrasLengthInBytes + inputsLengthInBytes + outputsLengthInBytes + outputIndicesLengthInBytes + constantsLengthInBytes + constantIndicesLengthInBytes + lookupsLengthInBytes + lookupIndicesLengthInBytes;
722
831
  if (buffer.byteLength < requiredLengthInBytes) {
723
832
  throw new Error("Buffer must be long enough to contain sections declared in header");
724
833
  }
@@ -726,6 +835,8 @@ var BufferedRunModelParams = class {
726
835
  this.inputs.update(this.encoded, inputsOffsetInBytes, inputsLengthInElements);
727
836
  this.outputs.update(this.encoded, outputsOffsetInBytes, outputsLengthInElements);
728
837
  this.outputIndices.update(this.encoded, outputIndicesOffsetInBytes, outputIndicesLengthInElements);
838
+ this.constants.update(this.encoded, constantsOffsetInBytes, constantsLengthInElements);
839
+ this.constantIndices.update(this.encoded, constantIndicesOffsetInBytes, constantIndicesLengthInElements);
729
840
  this.lookups.update(this.encoded, lookupsOffsetInBytes, lookupsLengthInElements);
730
841
  this.lookupIndices.update(this.encoded, lookupIndicesOffsetInBytes, lookupIndicesLengthInElements);
731
842
  }
@@ -802,6 +913,14 @@ var ReferencedRunModelParams = class {
802
913
  }
803
914
  }
804
915
  // from RunModelParams interface
916
+ getConstants() {
917
+ if (this.constants !== void 0 && this.constants.length > 0) {
918
+ return this.constants;
919
+ } else {
920
+ return void 0;
921
+ }
922
+ }
923
+ // from RunModelParams interface
805
924
  getLookups() {
806
925
  if (this.lookups !== void 0 && this.lookups.length > 0) {
807
926
  return this.lookups;
@@ -831,7 +950,13 @@ var ReferencedRunModelParams = class {
831
950
  this.inputs = inputs;
832
951
  this.outputs = outputs;
833
952
  this.outputsLengthInElements = outputs.varIds.length * outputs.seriesLength;
953
+ this.constants = options == null ? void 0 : options.constants;
834
954
  this.lookups = options == null ? void 0 : options.lookups;
955
+ if (this.constants) {
956
+ for (const constantDef of this.constants) {
957
+ resolveVarRef(this.listing, constantDef.varRef, "constant");
958
+ }
959
+ }
835
960
  if (this.lookups) {
836
961
  for (const lookupDef of this.lookups) {
837
962
  resolveVarRef(this.listing, lookupDef.varRef, "lookup");
@@ -1071,6 +1196,8 @@ function getJsModelFunctions() {
1071
1196
  let ctx;
1072
1197
  const cachedVectors = /* @__PURE__ */ new Map();
1073
1198
  const cachedSortVectors = /* @__PURE__ */ new Map();
1199
+ const cachedInvertWorkMatrices = /* @__PURE__ */ new Map();
1200
+ const cachedInvertResultMatrices = /* @__PURE__ */ new Map();
1074
1201
  return {
1075
1202
  setContext(context) {
1076
1203
  ctx = context;
@@ -1105,6 +1232,74 @@ function getJsModelFunctions() {
1105
1232
  INTEGER(x) {
1106
1233
  return Math.trunc(x);
1107
1234
  },
1235
+ INVERT_MATRIX(matrix, n) {
1236
+ let work = cachedInvertWorkMatrices.get(n);
1237
+ if (work === void 0) {
1238
+ work = Array(n);
1239
+ for (let i = 0; i < n; i++) {
1240
+ work[i] = Array(2 * n);
1241
+ }
1242
+ cachedInvertWorkMatrices.set(n, work);
1243
+ }
1244
+ let result = cachedInvertResultMatrices.get(n);
1245
+ if (result === void 0) {
1246
+ result = Array(n);
1247
+ for (let i = 0; i < n; i++) {
1248
+ result[i] = Array(n);
1249
+ }
1250
+ cachedInvertResultMatrices.set(n, result);
1251
+ }
1252
+ for (let i = 0; i < n; i++) {
1253
+ for (let j = 0; j < n; j++) {
1254
+ work[i][j] = matrix[i][j];
1255
+ work[i][n + j] = i === j ? 1 : 0;
1256
+ }
1257
+ }
1258
+ for (let k = 0; k < n; k++) {
1259
+ let pivot = k;
1260
+ let max = Math.abs(work[k][k]);
1261
+ for (let i = k + 1; i < n; i++) {
1262
+ const v = Math.abs(work[i][k]);
1263
+ if (v > max) {
1264
+ max = v;
1265
+ pivot = i;
1266
+ }
1267
+ }
1268
+ if (max === 0) {
1269
+ for (let i = 0; i < n; i++) {
1270
+ for (let j = 0; j < n; j++) {
1271
+ result[i][j] = _NA_;
1272
+ }
1273
+ }
1274
+ return result;
1275
+ }
1276
+ if (pivot !== k) {
1277
+ const tmpRow = work[k];
1278
+ work[k] = work[pivot];
1279
+ work[pivot] = tmpRow;
1280
+ }
1281
+ const p = work[k][k];
1282
+ for (let j = 0; j < 2 * n; j++) {
1283
+ work[k][j] /= p;
1284
+ }
1285
+ for (let i = 0; i < n; i++) {
1286
+ if (i !== k) {
1287
+ const f = work[i][k];
1288
+ if (f !== 0) {
1289
+ for (let j = 0; j < 2 * n; j++) {
1290
+ work[i][j] -= f * work[k][j];
1291
+ }
1292
+ }
1293
+ }
1294
+ }
1295
+ }
1296
+ for (let i = 0; i < n; i++) {
1297
+ for (let j = 0; j < n; j++) {
1298
+ result[i][j] = work[i][n + j];
1299
+ }
1300
+ }
1301
+ return result;
1302
+ },
1108
1303
  LN(x) {
1109
1304
  return Math.log(x);
1110
1305
  },
@@ -1311,6 +1506,7 @@ var BaseRunnableModel = class {
1311
1506
  const t0 = perfNow();
1312
1507
  (_a = this.onRunModel) == null ? void 0 : _a.call(this, inputsArray, outputsArray, {
1313
1508
  outputIndices: outputIndicesArray,
1509
+ constants: params.getConstants(),
1314
1510
  lookups: params.getLookups()
1315
1511
  });
1316
1512
  const elapsed = perfElapsed(t0);
@@ -1352,13 +1548,14 @@ function initJsModel(model) {
1352
1548
  inputs,
1353
1549
  outputs,
1354
1550
  options == null ? void 0 : options.outputIndices,
1551
+ options == null ? void 0 : options.constants,
1355
1552
  options == null ? void 0 : options.lookups,
1356
1553
  void 0
1357
1554
  );
1358
1555
  }
1359
1556
  });
1360
1557
  }
1361
- function runJsModel(model, initialTime, finalTime, timeStep, saveFreq, numSavePoints, inputs, outputs, outputIndices, lookups, stopAfterTime) {
1558
+ function runJsModel(model, initialTime, finalTime, timeStep, saveFreq, numSavePoints, inputs, outputs, outputIndices, constants, lookups, stopAfterTime) {
1362
1559
  let time = initialTime;
1363
1560
  model.setTime(time);
1364
1561
  const fnContext = {
@@ -1367,6 +1564,11 @@ function runJsModel(model, initialTime, finalTime, timeStep, saveFreq, numSavePo
1367
1564
  };
1368
1565
  model.getModelFunctions().setContext(fnContext);
1369
1566
  model.initConstants();
1567
+ if (constants !== void 0) {
1568
+ for (const constantDef of constants) {
1569
+ model.setConstant(constantDef.varRef.varSpec, constantDef.value);
1570
+ }
1571
+ }
1370
1572
  if (lookups !== void 0) {
1371
1573
  for (const lookupDef of lookups) {
1372
1574
  model.setLookup(lookupDef.varRef.varSpec, lookupDef.points);
@@ -1453,6 +1655,7 @@ var MockJsModel = class {
1453
1655
  // from JsModel interface
1454
1656
  this.kind = "js";
1455
1657
  this.vars = /* @__PURE__ */ new Map();
1658
+ this.constants = /* @__PURE__ */ new Map();
1456
1659
  this.lookups = /* @__PURE__ */ new Map();
1457
1660
  this.outputVarIds = options.outputVarIds;
1458
1661
  this.outputVarNames = options.outputVarIds;
@@ -1505,6 +1708,14 @@ var MockJsModel = class {
1505
1708
  setInputs() {
1506
1709
  }
1507
1710
  // from JsModel interface
1711
+ setConstant(varSpec, value) {
1712
+ const varId = this.varIdForSpec(varSpec);
1713
+ if (varId === void 0) {
1714
+ throw new Error(`No constant variable found for spec ${varSpec}`);
1715
+ }
1716
+ this.constants.set(varId, value);
1717
+ }
1718
+ // from JsModel interface
1508
1719
  setLookup(varSpec, points) {
1509
1720
  const varId = this.varIdForSpec(varSpec);
1510
1721
  if (varId === void 0) {
@@ -1529,6 +1740,7 @@ var MockJsModel = class {
1529
1740
  }
1530
1741
  // from JsModel interface
1531
1742
  initConstants() {
1743
+ this.constants.clear();
1532
1744
  }
1533
1745
  // from JsModel interface
1534
1746
  initLevels() {
@@ -1536,7 +1748,7 @@ var MockJsModel = class {
1536
1748
  // from JsModel interface
1537
1749
  evalAux() {
1538
1750
  var _a;
1539
- (_a = this.onEvalAux) == null ? void 0 : _a.call(this, this.vars, this.lookups);
1751
+ (_a = this.onEvalAux) == null ? void 0 : _a.call(this, this.vars, this.constants.size > 0 ? this.constants : void 0, this.lookups);
1540
1752
  }
1541
1753
  // from JsModel interface
1542
1754
  evalLevels() {
@@ -1619,11 +1831,18 @@ var WasmModel = class {
1619
1831
  this.outputVarIds = wasmModule.outputVarIds;
1620
1832
  this.modelListing = wasmModule.modelListing;
1621
1833
  this.wasmSetLookup = wasmModule.cwrap("setLookup", null, ["number", "number", "number", "number"]);
1622
- this.wasmRunModel = wasmModule.cwrap("runModelWithBuffers", null, ["number", "number", "number"]);
1834
+ this.wasmRunModel = wasmModule.cwrap("runModelWithBuffers", null, [
1835
+ "number",
1836
+ "number",
1837
+ "number",
1838
+ "number",
1839
+ "number",
1840
+ "number"
1841
+ ]);
1623
1842
  }
1624
1843
  // from RunnableModel interface
1625
1844
  runModel(params) {
1626
- var _a, _b, _c, _d, _e, _f, _g;
1845
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1627
1846
  const lookups = params.getLookups();
1628
1847
  if (lookups !== void 0) {
1629
1848
  for (const lookupDef of lookups) {
@@ -1659,7 +1878,48 @@ var WasmModel = class {
1659
1878
  this.wasmSetLookup(varIndex, subIndicesAddress, pointsAddress, numPoints);
1660
1879
  }
1661
1880
  }
1662
- params.copyInputs((_d = this.inputsBuffer) == null ? void 0 : _d.getArrayView(), (numElements) => {
1881
+ let constantIndicesBuffer;
1882
+ let constantValuesBuffer;
1883
+ const constants = params.getConstants();
1884
+ if (constants !== void 0 && constants.length > 0) {
1885
+ let totalIndicesSize = 1;
1886
+ for (const constantDef of constants) {
1887
+ const numSubElements = ((_d = constantDef.varRef.varSpec.subscriptIndices) == null ? void 0 : _d.length) || 0;
1888
+ totalIndicesSize += 2 + numSubElements;
1889
+ }
1890
+ if (this.constantIndicesBuffer === void 0 || this.constantIndicesBuffer.numElements < totalIndicesSize) {
1891
+ (_e = this.constantIndicesBuffer) == null ? void 0 : _e.dispose();
1892
+ this.constantIndicesBuffer = createInt32WasmBuffer(this.wasmModule, totalIndicesSize);
1893
+ }
1894
+ const numConstants = constants.length;
1895
+ if (this.constantValuesBuffer === void 0 || this.constantValuesBuffer.numElements < numConstants) {
1896
+ (_f = this.constantValuesBuffer) == null ? void 0 : _f.dispose();
1897
+ this.constantValuesBuffer = createFloat64WasmBuffer(this.wasmModule, numConstants);
1898
+ }
1899
+ const indicesView = this.constantIndicesBuffer.getArrayView();
1900
+ const valuesView = this.constantValuesBuffer.getArrayView();
1901
+ let indicesOffset = 0;
1902
+ let valuesOffset = 0;
1903
+ indicesView[indicesOffset++] = numConstants;
1904
+ for (const constantDef of constants) {
1905
+ const varSpec = constantDef.varRef.varSpec;
1906
+ const numSubElements = ((_g = varSpec.subscriptIndices) == null ? void 0 : _g.length) || 0;
1907
+ indicesView[indicesOffset++] = varSpec.varIndex;
1908
+ indicesView[indicesOffset++] = numSubElements;
1909
+ if (numSubElements > 0) {
1910
+ for (let i = 0; i < numSubElements; i++) {
1911
+ indicesView[indicesOffset++] = varSpec.subscriptIndices[i];
1912
+ }
1913
+ }
1914
+ valuesView[valuesOffset++] = constantDef.value;
1915
+ }
1916
+ constantIndicesBuffer = this.constantIndicesBuffer;
1917
+ constantValuesBuffer = this.constantValuesBuffer;
1918
+ } else {
1919
+ constantIndicesBuffer = void 0;
1920
+ constantValuesBuffer = void 0;
1921
+ }
1922
+ params.copyInputs((_h = this.inputsBuffer) == null ? void 0 : _h.getArrayView(), (numElements) => {
1663
1923
  var _a2;
1664
1924
  (_a2 = this.inputsBuffer) == null ? void 0 : _a2.dispose();
1665
1925
  this.inputsBuffer = createFloat64WasmBuffer(this.wasmModule, numElements);
@@ -1667,7 +1927,7 @@ var WasmModel = class {
1667
1927
  });
1668
1928
  let outputIndicesBuffer;
1669
1929
  if (params.getOutputIndicesLength() > 0) {
1670
- params.copyOutputIndices((_e = this.outputIndicesBuffer) == null ? void 0 : _e.getArrayView(), (numElements) => {
1930
+ params.copyOutputIndices((_i = this.outputIndicesBuffer) == null ? void 0 : _i.getArrayView(), (numElements) => {
1671
1931
  var _a2;
1672
1932
  (_a2 = this.outputIndicesBuffer) == null ? void 0 : _a2.dispose();
1673
1933
  this.outputIndicesBuffer = createInt32WasmBuffer(this.wasmModule, numElements);
@@ -1679,14 +1939,19 @@ var WasmModel = class {
1679
1939
  }
1680
1940
  const outputsLengthInElements = params.getOutputsLength();
1681
1941
  if (this.outputsBuffer === void 0 || this.outputsBuffer.numElements < outputsLengthInElements) {
1682
- (_f = this.outputsBuffer) == null ? void 0 : _f.dispose();
1942
+ (_j = this.outputsBuffer) == null ? void 0 : _j.dispose();
1683
1943
  this.outputsBuffer = createFloat64WasmBuffer(this.wasmModule, outputsLengthInElements);
1684
1944
  }
1685
1945
  const t0 = perfNow();
1686
1946
  this.wasmRunModel(
1687
- ((_g = this.inputsBuffer) == null ? void 0 : _g.getAddress()) || 0,
1947
+ ((_k = this.inputsBuffer) == null ? void 0 : _k.getAddress()) || 0,
1948
+ // Always pass 0 (NULL) for input indices, since we assume that all input values are
1949
+ // provided and are in the same order as the input variables defined in the model spec
1950
+ 0,
1688
1951
  this.outputsBuffer.getAddress(),
1689
- (outputIndicesBuffer == null ? void 0 : outputIndicesBuffer.getAddress()) || 0
1952
+ (outputIndicesBuffer == null ? void 0 : outputIndicesBuffer.getAddress()) || 0,
1953
+ (constantValuesBuffer == null ? void 0 : constantValuesBuffer.getAddress()) || 0,
1954
+ (constantIndicesBuffer == null ? void 0 : constantIndicesBuffer.getAddress()) || 0
1690
1955
  );
1691
1956
  const elapsed = perfElapsed(t0);
1692
1957
  params.storeOutputs(this.outputsBuffer.getArrayView());
@@ -1694,13 +1959,17 @@ var WasmModel = class {
1694
1959
  }
1695
1960
  // from RunnableModel interface
1696
1961
  terminate() {
1697
- var _a, _b, _c;
1962
+ var _a, _b, _c, _d, _e;
1698
1963
  (_a = this.inputsBuffer) == null ? void 0 : _a.dispose();
1699
1964
  this.inputsBuffer = void 0;
1700
1965
  (_b = this.outputsBuffer) == null ? void 0 : _b.dispose();
1701
1966
  this.outputsBuffer = void 0;
1702
1967
  (_c = this.outputIndicesBuffer) == null ? void 0 : _c.dispose();
1703
1968
  this.outputIndicesBuffer = void 0;
1969
+ (_d = this.constantValuesBuffer) == null ? void 0 : _d.dispose();
1970
+ this.constantValuesBuffer = void 0;
1971
+ (_e = this.constantIndicesBuffer) == null ? void 0 : _e.dispose();
1972
+ this.constantIndicesBuffer = void 0;
1704
1973
  }
1705
1974
  };
1706
1975
  function initWasmModel(wasmModule) {
@@ -1716,6 +1985,7 @@ var MockWasmModule = class {
1716
1985
  this.mallocOffset = 8;
1717
1986
  this.allocs = /* @__PURE__ */ new Map();
1718
1987
  this.lookups = /* @__PURE__ */ new Map();
1988
+ this.constants = /* @__PURE__ */ new Map();
1719
1989
  this.initialTime = options.initialTime;
1720
1990
  this.finalTime = options.finalTime;
1721
1991
  this.outputVarIds = options.outputVarIds;
@@ -1755,11 +2025,35 @@ var MockWasmModule = class {
1755
2025
  this.lookups.set(varId, new JsModelLookup(numPoints, points));
1756
2026
  };
1757
2027
  case "runModelWithBuffers":
1758
- return (inputsAddress, outputsAddress, outputIndicesAddress) => {
2028
+ return (inputsAddress, _inputIndicesAddress, outputsAddress, outputIndicesAddress, constantValuesAddress, constantIndicesAddress) => {
1759
2029
  const inputs = this.getHeapView("float64", inputsAddress);
1760
2030
  const outputs = this.getHeapView("float64", outputsAddress);
1761
2031
  const outputIndices = this.getHeapView("int32", outputIndicesAddress);
1762
- this.onRunModel(inputs, outputs, this.lookups, outputIndices);
2032
+ this.constants.clear();
2033
+ if (constantValuesAddress !== 0 && constantIndicesAddress !== 0) {
2034
+ const constantValues = this.getHeapView("float64", constantValuesAddress);
2035
+ const constantIndices = this.getHeapView("int32", constantIndicesAddress);
2036
+ const numConstants = constantIndices[0];
2037
+ let indicesOffset = 1;
2038
+ let valuesOffset = 0;
2039
+ for (let i = 0; i < numConstants; i++) {
2040
+ const varIndex = constantIndices[indicesOffset++];
2041
+ const subCount = constantIndices[indicesOffset++];
2042
+ indicesOffset += subCount;
2043
+ const varId = this.varIdForSpec({ varIndex });
2044
+ if (varId) {
2045
+ this.constants.set(varId, constantValues[valuesOffset++]);
2046
+ }
2047
+ }
2048
+ }
2049
+ this.onRunModel(
2050
+ inputs,
2051
+ outputs,
2052
+ this.constants.size > 0 ? this.constants : void 0,
2053
+ this.lookups,
2054
+ outputIndices
2055
+ );
2056
+ this.constants.clear();
1763
2057
  };
1764
2058
  default:
1765
2059
  throw new Error(`Unhandled call to cwrap with function name '${fname}'`);
@@ -1837,7 +2131,7 @@ function createRunnerFromRunnableModel(model) {
1837
2131
  }
1838
2132
  return runModelSync(inputs, outputs, options);
1839
2133
  },
1840
- terminate: () => __async(this, null, function* () {
2134
+ terminate: () => __async(null, null, function* () {
1841
2135
  if (!terminated) {
1842
2136
  model.terminate();
1843
2137
  terminated = true;
@@ -2123,14 +2417,18 @@ export {
2123
2417
  Outputs,
2124
2418
  ReferencedRunModelParams,
2125
2419
  Series,
2420
+ createConstantDef,
2126
2421
  createInputValue,
2127
2422
  createLookupDef,
2128
2423
  createRunnableModel,
2129
2424
  createSynchronousModelRunner,
2425
+ decodeConstants,
2130
2426
  decodeLookups,
2427
+ encodeConstants,
2131
2428
  encodeLookups,
2132
2429
  encodeVarIndices,
2133
2430
  execJsModel,
2431
+ getEncodedConstantBufferLengths,
2134
2432
  getEncodedLookupBufferLengths,
2135
2433
  getEncodedVarIndicesLength,
2136
2434
  getJsModelFunctions,