@sdeverywhere/runtime 0.2.7 → 0.2.8

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
@@ -51,8 +51,8 @@ var __async = (__this, __arguments, generator) => {
51
51
  };
52
52
 
53
53
  // src/index.ts
54
- var src_exports = {};
55
- __export(src_exports, {
54
+ var index_exports = {};
55
+ __export(index_exports, {
56
56
  BufferedRunModelParams: () => BufferedRunModelParams,
57
57
  MockJsModel: () => MockJsModel,
58
58
  MockWasmModule: () => MockWasmModule,
@@ -62,14 +62,18 @@ __export(src_exports, {
62
62
  Outputs: () => Outputs,
63
63
  ReferencedRunModelParams: () => ReferencedRunModelParams,
64
64
  Series: () => Series,
65
+ createConstantDef: () => createConstantDef,
65
66
  createInputValue: () => createInputValue,
66
67
  createLookupDef: () => createLookupDef,
67
68
  createRunnableModel: () => createRunnableModel,
68
69
  createSynchronousModelRunner: () => createSynchronousModelRunner,
70
+ decodeConstants: () => decodeConstants,
69
71
  decodeLookups: () => decodeLookups,
72
+ encodeConstants: () => encodeConstants,
70
73
  encodeLookups: () => encodeLookups,
71
74
  encodeVarIndices: () => encodeVarIndices,
72
75
  execJsModel: () => execJsModel,
76
+ getEncodedConstantBufferLengths: () => getEncodedConstantBufferLengths,
73
77
  getEncodedLookupBufferLengths: () => getEncodedLookupBufferLengths,
74
78
  getEncodedVarIndicesLength: () => getEncodedVarIndicesLength,
75
79
  getJsModelFunctions: () => getJsModelFunctions,
@@ -78,7 +82,7 @@ __export(src_exports, {
78
82
  perfElapsed: () => perfElapsed,
79
83
  perfNow: () => perfNow
80
84
  });
81
- module.exports = __toCommonJS(src_exports);
85
+ module.exports = __toCommonJS(index_exports);
82
86
 
83
87
  // src/_shared/inputs.ts
84
88
  function createInputValue(varId, defaultValue, initialValue) {
@@ -251,6 +255,66 @@ function encodeVarIndices(varSpecs, indicesArray) {
251
255
  }
252
256
  }
253
257
  }
258
+ function getEncodedConstantBufferLengths(constantDefs) {
259
+ var _a;
260
+ let constantIndicesLength = 1;
261
+ let constantsLength = 0;
262
+ for (const constantDef of constantDefs) {
263
+ const varSpec = constantDef.varRef.varSpec;
264
+ if (varSpec === void 0) {
265
+ throw new Error("Cannot compute constant buffer lengths until all constant var specs are defined");
266
+ }
267
+ constantIndicesLength += 2;
268
+ const subCount = ((_a = varSpec.subscriptIndices) == null ? void 0 : _a.length) || 0;
269
+ constantIndicesLength += subCount;
270
+ constantsLength += 1;
271
+ }
272
+ return {
273
+ constantIndicesLength,
274
+ constantsLength
275
+ };
276
+ }
277
+ function encodeConstants(constantDefs, constantIndicesArray, constantsArray) {
278
+ let ci = 0;
279
+ constantIndicesArray[ci++] = constantDefs.length;
280
+ let constantDataOffset = 0;
281
+ for (const constantDef of constantDefs) {
282
+ const varSpec = constantDef.varRef.varSpec;
283
+ constantIndicesArray[ci++] = varSpec.varIndex;
284
+ const subs = varSpec.subscriptIndices;
285
+ const subCount = (subs == null ? void 0 : subs.length) || 0;
286
+ constantIndicesArray[ci++] = subCount;
287
+ for (let i = 0; i < subCount; i++) {
288
+ constantIndicesArray[ci++] = subs[i];
289
+ }
290
+ constantsArray[constantDataOffset++] = constantDef.value;
291
+ }
292
+ }
293
+ function decodeConstants(constantIndicesArray, constantsArray) {
294
+ const constantDefs = [];
295
+ let ci = 0;
296
+ const constantCount = constantIndicesArray[ci++];
297
+ for (let i = 0; i < constantCount; i++) {
298
+ const varIndex = constantIndicesArray[ci++];
299
+ const subCount = constantIndicesArray[ci++];
300
+ const subscriptIndices = subCount > 0 ? Array(subCount) : void 0;
301
+ for (let subIndex = 0; subIndex < subCount; subIndex++) {
302
+ subscriptIndices[subIndex] = constantIndicesArray[ci++];
303
+ }
304
+ const varSpec = {
305
+ varIndex,
306
+ subscriptIndices
307
+ };
308
+ const value = constantsArray[i];
309
+ constantDefs.push({
310
+ varRef: {
311
+ varSpec
312
+ },
313
+ value
314
+ });
315
+ }
316
+ return constantDefs;
317
+ }
254
318
  function getEncodedLookupBufferLengths(lookupDefs) {
255
319
  var _a, _b;
256
320
  let lookupIndicesLength = 1;
@@ -332,6 +396,14 @@ function decodeLookups(lookupIndicesArray, lookupsArray) {
332
396
  return lookupDefs;
333
397
  }
334
398
 
399
+ // src/_shared/constant-def.ts
400
+ function createConstantDef(varRef, value) {
401
+ return {
402
+ varRef,
403
+ value
404
+ };
405
+ }
406
+
335
407
  // src/_shared/lookup-def.ts
336
408
  function createLookupDef(varRef, points) {
337
409
  let flatPoints;
@@ -502,7 +574,7 @@ function resolveVarRef(listing, varRef, varKind) {
502
574
  }
503
575
 
504
576
  // src/runnable-model/buffered-run-model-params.ts
505
- var headerLengthInElements = 16;
577
+ var headerLengthInElements = 20;
506
578
  var extrasLengthInElements = 1;
507
579
  var Int32Section = class {
508
580
  constructor() {
@@ -547,6 +619,10 @@ var BufferedRunModelParams = class {
547
619
  this.outputs = new Float64Section();
548
620
  /** The output indices section of the `encoded` buffer. */
549
621
  this.outputIndices = new Int32Section();
622
+ /** The constant values section of the `encoded` buffer. */
623
+ this.constants = new Float64Section();
624
+ /** The constant indices section of the `encoded` buffer. */
625
+ this.constantIndices = new Int32Section();
550
626
  /** The lookup data section of the `encoded` buffer. */
551
627
  this.lookups = new Float64Section();
552
628
  /** The lookup indices section of the `encoded` buffer. */
@@ -614,6 +690,13 @@ var BufferedRunModelParams = class {
614
690
  }
615
691
  }
616
692
  // from RunModelParams interface
693
+ getConstants() {
694
+ if (this.constantIndices.lengthInElements === 0) {
695
+ return void 0;
696
+ }
697
+ return decodeConstants(this.constantIndices.view, this.constants.view);
698
+ }
699
+ // from RunModelParams interface
617
700
  getLookups() {
618
701
  if (this.lookupIndices.lengthInElements === 0) {
619
702
  return void 0;
@@ -658,6 +741,19 @@ var BufferedRunModelParams = class {
658
741
  } else {
659
742
  outputIndicesLengthInElements = 0;
660
743
  }
744
+ let constantsLengthInElements;
745
+ let constantIndicesLengthInElements;
746
+ if ((options == null ? void 0 : options.constants) !== void 0 && options.constants.length > 0) {
747
+ for (const constantDef of options.constants) {
748
+ resolveVarRef(this.listing, constantDef.varRef, "constant");
749
+ }
750
+ const encodedLengths = getEncodedConstantBufferLengths(options.constants);
751
+ constantsLengthInElements = encodedLengths.constantsLength;
752
+ constantIndicesLengthInElements = encodedLengths.constantIndicesLength;
753
+ } else {
754
+ constantsLengthInElements = 0;
755
+ constantIndicesLengthInElements = 0;
756
+ }
661
757
  let lookupsLengthInElements;
662
758
  let lookupIndicesLengthInElements;
663
759
  if ((options == null ? void 0 : options.lookups) !== void 0 && options.lookups.length > 0) {
@@ -685,6 +781,8 @@ var BufferedRunModelParams = class {
685
781
  const inputsOffsetInBytes = section("float64", inputsLengthInElements);
686
782
  const outputsOffsetInBytes = section("float64", outputsLengthInElements);
687
783
  const outputIndicesOffsetInBytes = section("int32", outputIndicesLengthInElements);
784
+ const constantsOffsetInBytes = section("float64", constantsLengthInElements);
785
+ const constantIndicesOffsetInBytes = section("int32", constantIndicesLengthInElements);
688
786
  const lookupsOffsetInBytes = section("float64", lookupsLengthInElements);
689
787
  const lookupIndicesOffsetInBytes = section("int32", lookupIndicesLengthInElements);
690
788
  const requiredLengthInBytes = byteOffset;
@@ -703,6 +801,10 @@ var BufferedRunModelParams = class {
703
801
  headerView[headerIndex++] = outputsLengthInElements;
704
802
  headerView[headerIndex++] = outputIndicesOffsetInBytes;
705
803
  headerView[headerIndex++] = outputIndicesLengthInElements;
804
+ headerView[headerIndex++] = constantsOffsetInBytes;
805
+ headerView[headerIndex++] = constantsLengthInElements;
806
+ headerView[headerIndex++] = constantIndicesOffsetInBytes;
807
+ headerView[headerIndex++] = constantIndicesLengthInElements;
706
808
  headerView[headerIndex++] = lookupsOffsetInBytes;
707
809
  headerView[headerIndex++] = lookupsLengthInElements;
708
810
  headerView[headerIndex++] = lookupIndicesOffsetInBytes;
@@ -711,6 +813,8 @@ var BufferedRunModelParams = class {
711
813
  this.extras.update(this.encoded, extrasOffsetInBytes, extrasLengthInElements);
712
814
  this.outputs.update(this.encoded, outputsOffsetInBytes, outputsLengthInElements);
713
815
  this.outputIndices.update(this.encoded, outputIndicesOffsetInBytes, outputIndicesLengthInElements);
816
+ this.constants.update(this.encoded, constantsOffsetInBytes, constantsLengthInElements);
817
+ this.constantIndices.update(this.encoded, constantIndicesOffsetInBytes, constantIndicesLengthInElements);
714
818
  this.lookups.update(this.encoded, lookupsOffsetInBytes, lookupsLengthInElements);
715
819
  this.lookupIndices.update(this.encoded, lookupIndicesOffsetInBytes, lookupIndicesLengthInElements);
716
820
  const inputsView = this.inputs.view;
@@ -725,6 +829,9 @@ var BufferedRunModelParams = class {
725
829
  if (this.outputIndices.view) {
726
830
  encodeVarIndices(outputVarSpecs, this.outputIndices.view);
727
831
  }
832
+ if (constantIndicesLengthInElements > 0) {
833
+ encodeConstants(options.constants, this.constantIndices.view, this.constants.view);
834
+ }
728
835
  if (lookupIndicesLengthInElements > 0) {
729
836
  encodeLookups(options.lookups, this.lookupIndices.view, this.lookups.view);
730
837
  }
@@ -753,6 +860,10 @@ var BufferedRunModelParams = class {
753
860
  const outputsLengthInElements = headerView[headerIndex++];
754
861
  const outputIndicesOffsetInBytes = headerView[headerIndex++];
755
862
  const outputIndicesLengthInElements = headerView[headerIndex++];
863
+ const constantsOffsetInBytes = headerView[headerIndex++];
864
+ const constantsLengthInElements = headerView[headerIndex++];
865
+ const constantIndicesOffsetInBytes = headerView[headerIndex++];
866
+ const constantIndicesLengthInElements = headerView[headerIndex++];
756
867
  const lookupsOffsetInBytes = headerView[headerIndex++];
757
868
  const lookupsLengthInElements = headerView[headerIndex++];
758
869
  const lookupIndicesOffsetInBytes = headerView[headerIndex++];
@@ -761,9 +872,11 @@ var BufferedRunModelParams = class {
761
872
  const inputsLengthInBytes = inputsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
762
873
  const outputsLengthInBytes = outputsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
763
874
  const outputIndicesLengthInBytes = outputIndicesLengthInElements * Int32Array.BYTES_PER_ELEMENT;
875
+ const constantsLengthInBytes = constantsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
876
+ const constantIndicesLengthInBytes = constantIndicesLengthInElements * Int32Array.BYTES_PER_ELEMENT;
764
877
  const lookupsLengthInBytes = lookupsLengthInElements * Float64Array.BYTES_PER_ELEMENT;
765
878
  const lookupIndicesLengthInBytes = lookupIndicesLengthInElements * Int32Array.BYTES_PER_ELEMENT;
766
- const requiredLengthInBytes = headerLengthInBytes + extrasLengthInBytes + inputsLengthInBytes + outputsLengthInBytes + outputIndicesLengthInBytes + lookupsLengthInBytes + lookupIndicesLengthInBytes;
879
+ const requiredLengthInBytes = headerLengthInBytes + extrasLengthInBytes + inputsLengthInBytes + outputsLengthInBytes + outputIndicesLengthInBytes + constantsLengthInBytes + constantIndicesLengthInBytes + lookupsLengthInBytes + lookupIndicesLengthInBytes;
767
880
  if (buffer.byteLength < requiredLengthInBytes) {
768
881
  throw new Error("Buffer must be long enough to contain sections declared in header");
769
882
  }
@@ -771,6 +884,8 @@ var BufferedRunModelParams = class {
771
884
  this.inputs.update(this.encoded, inputsOffsetInBytes, inputsLengthInElements);
772
885
  this.outputs.update(this.encoded, outputsOffsetInBytes, outputsLengthInElements);
773
886
  this.outputIndices.update(this.encoded, outputIndicesOffsetInBytes, outputIndicesLengthInElements);
887
+ this.constants.update(this.encoded, constantsOffsetInBytes, constantsLengthInElements);
888
+ this.constantIndices.update(this.encoded, constantIndicesOffsetInBytes, constantIndicesLengthInElements);
774
889
  this.lookups.update(this.encoded, lookupsOffsetInBytes, lookupsLengthInElements);
775
890
  this.lookupIndices.update(this.encoded, lookupIndicesOffsetInBytes, lookupIndicesLengthInElements);
776
891
  }
@@ -847,6 +962,14 @@ var ReferencedRunModelParams = class {
847
962
  }
848
963
  }
849
964
  // from RunModelParams interface
965
+ getConstants() {
966
+ if (this.constants !== void 0 && this.constants.length > 0) {
967
+ return this.constants;
968
+ } else {
969
+ return void 0;
970
+ }
971
+ }
972
+ // from RunModelParams interface
850
973
  getLookups() {
851
974
  if (this.lookups !== void 0 && this.lookups.length > 0) {
852
975
  return this.lookups;
@@ -876,7 +999,13 @@ var ReferencedRunModelParams = class {
876
999
  this.inputs = inputs;
877
1000
  this.outputs = outputs;
878
1001
  this.outputsLengthInElements = outputs.varIds.length * outputs.seriesLength;
1002
+ this.constants = options == null ? void 0 : options.constants;
879
1003
  this.lookups = options == null ? void 0 : options.lookups;
1004
+ if (this.constants) {
1005
+ for (const constantDef of this.constants) {
1006
+ resolveVarRef(this.listing, constantDef.varRef, "constant");
1007
+ }
1008
+ }
880
1009
  if (this.lookups) {
881
1010
  for (const lookupDef of this.lookups) {
882
1011
  resolveVarRef(this.listing, lookupDef.varRef, "lookup");
@@ -1356,6 +1485,7 @@ var BaseRunnableModel = class {
1356
1485
  const t0 = perfNow();
1357
1486
  (_a = this.onRunModel) == null ? void 0 : _a.call(this, inputsArray, outputsArray, {
1358
1487
  outputIndices: outputIndicesArray,
1488
+ constants: params.getConstants(),
1359
1489
  lookups: params.getLookups()
1360
1490
  });
1361
1491
  const elapsed = perfElapsed(t0);
@@ -1397,13 +1527,14 @@ function initJsModel(model) {
1397
1527
  inputs,
1398
1528
  outputs,
1399
1529
  options == null ? void 0 : options.outputIndices,
1530
+ options == null ? void 0 : options.constants,
1400
1531
  options == null ? void 0 : options.lookups,
1401
1532
  void 0
1402
1533
  );
1403
1534
  }
1404
1535
  });
1405
1536
  }
1406
- function runJsModel(model, initialTime, finalTime, timeStep, saveFreq, numSavePoints, inputs, outputs, outputIndices, lookups, stopAfterTime) {
1537
+ function runJsModel(model, initialTime, finalTime, timeStep, saveFreq, numSavePoints, inputs, outputs, outputIndices, constants, lookups, stopAfterTime) {
1407
1538
  let time = initialTime;
1408
1539
  model.setTime(time);
1409
1540
  const fnContext = {
@@ -1412,6 +1543,11 @@ function runJsModel(model, initialTime, finalTime, timeStep, saveFreq, numSavePo
1412
1543
  };
1413
1544
  model.getModelFunctions().setContext(fnContext);
1414
1545
  model.initConstants();
1546
+ if (constants !== void 0) {
1547
+ for (const constantDef of constants) {
1548
+ model.setConstant(constantDef.varRef.varSpec, constantDef.value);
1549
+ }
1550
+ }
1415
1551
  if (lookups !== void 0) {
1416
1552
  for (const lookupDef of lookups) {
1417
1553
  model.setLookup(lookupDef.varRef.varSpec, lookupDef.points);
@@ -1498,6 +1634,7 @@ var MockJsModel = class {
1498
1634
  // from JsModel interface
1499
1635
  this.kind = "js";
1500
1636
  this.vars = /* @__PURE__ */ new Map();
1637
+ this.constants = /* @__PURE__ */ new Map();
1501
1638
  this.lookups = /* @__PURE__ */ new Map();
1502
1639
  this.outputVarIds = options.outputVarIds;
1503
1640
  this.outputVarNames = options.outputVarIds;
@@ -1550,6 +1687,14 @@ var MockJsModel = class {
1550
1687
  setInputs() {
1551
1688
  }
1552
1689
  // from JsModel interface
1690
+ setConstant(varSpec, value) {
1691
+ const varId = this.varIdForSpec(varSpec);
1692
+ if (varId === void 0) {
1693
+ throw new Error(`No constant variable found for spec ${varSpec}`);
1694
+ }
1695
+ this.constants.set(varId, value);
1696
+ }
1697
+ // from JsModel interface
1553
1698
  setLookup(varSpec, points) {
1554
1699
  const varId = this.varIdForSpec(varSpec);
1555
1700
  if (varId === void 0) {
@@ -1574,6 +1719,7 @@ var MockJsModel = class {
1574
1719
  }
1575
1720
  // from JsModel interface
1576
1721
  initConstants() {
1722
+ this.constants.clear();
1577
1723
  }
1578
1724
  // from JsModel interface
1579
1725
  initLevels() {
@@ -1581,7 +1727,7 @@ var MockJsModel = class {
1581
1727
  // from JsModel interface
1582
1728
  evalAux() {
1583
1729
  var _a;
1584
- (_a = this.onEvalAux) == null ? void 0 : _a.call(this, this.vars, this.lookups);
1730
+ (_a = this.onEvalAux) == null ? void 0 : _a.call(this, this.vars, this.constants.size > 0 ? this.constants : void 0, this.lookups);
1585
1731
  }
1586
1732
  // from JsModel interface
1587
1733
  evalLevels() {
@@ -1664,11 +1810,18 @@ var WasmModel = class {
1664
1810
  this.outputVarIds = wasmModule.outputVarIds;
1665
1811
  this.modelListing = wasmModule.modelListing;
1666
1812
  this.wasmSetLookup = wasmModule.cwrap("setLookup", null, ["number", "number", "number", "number"]);
1667
- this.wasmRunModel = wasmModule.cwrap("runModelWithBuffers", null, ["number", "number", "number"]);
1813
+ this.wasmRunModel = wasmModule.cwrap("runModelWithBuffers", null, [
1814
+ "number",
1815
+ "number",
1816
+ "number",
1817
+ "number",
1818
+ "number",
1819
+ "number"
1820
+ ]);
1668
1821
  }
1669
1822
  // from RunnableModel interface
1670
1823
  runModel(params) {
1671
- var _a, _b, _c, _d, _e, _f, _g;
1824
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1672
1825
  const lookups = params.getLookups();
1673
1826
  if (lookups !== void 0) {
1674
1827
  for (const lookupDef of lookups) {
@@ -1704,7 +1857,48 @@ var WasmModel = class {
1704
1857
  this.wasmSetLookup(varIndex, subIndicesAddress, pointsAddress, numPoints);
1705
1858
  }
1706
1859
  }
1707
- params.copyInputs((_d = this.inputsBuffer) == null ? void 0 : _d.getArrayView(), (numElements) => {
1860
+ let constantIndicesBuffer;
1861
+ let constantValuesBuffer;
1862
+ const constants = params.getConstants();
1863
+ if (constants !== void 0 && constants.length > 0) {
1864
+ let totalIndicesSize = 1;
1865
+ for (const constantDef of constants) {
1866
+ const numSubElements = ((_d = constantDef.varRef.varSpec.subscriptIndices) == null ? void 0 : _d.length) || 0;
1867
+ totalIndicesSize += 2 + numSubElements;
1868
+ }
1869
+ if (this.constantIndicesBuffer === void 0 || this.constantIndicesBuffer.numElements < totalIndicesSize) {
1870
+ (_e = this.constantIndicesBuffer) == null ? void 0 : _e.dispose();
1871
+ this.constantIndicesBuffer = createInt32WasmBuffer(this.wasmModule, totalIndicesSize);
1872
+ }
1873
+ const numConstants = constants.length;
1874
+ if (this.constantValuesBuffer === void 0 || this.constantValuesBuffer.numElements < numConstants) {
1875
+ (_f = this.constantValuesBuffer) == null ? void 0 : _f.dispose();
1876
+ this.constantValuesBuffer = createFloat64WasmBuffer(this.wasmModule, numConstants);
1877
+ }
1878
+ const indicesView = this.constantIndicesBuffer.getArrayView();
1879
+ const valuesView = this.constantValuesBuffer.getArrayView();
1880
+ let indicesOffset = 0;
1881
+ let valuesOffset = 0;
1882
+ indicesView[indicesOffset++] = numConstants;
1883
+ for (const constantDef of constants) {
1884
+ const varSpec = constantDef.varRef.varSpec;
1885
+ const numSubElements = ((_g = varSpec.subscriptIndices) == null ? void 0 : _g.length) || 0;
1886
+ indicesView[indicesOffset++] = varSpec.varIndex;
1887
+ indicesView[indicesOffset++] = numSubElements;
1888
+ if (numSubElements > 0) {
1889
+ for (let i = 0; i < numSubElements; i++) {
1890
+ indicesView[indicesOffset++] = varSpec.subscriptIndices[i];
1891
+ }
1892
+ }
1893
+ valuesView[valuesOffset++] = constantDef.value;
1894
+ }
1895
+ constantIndicesBuffer = this.constantIndicesBuffer;
1896
+ constantValuesBuffer = this.constantValuesBuffer;
1897
+ } else {
1898
+ constantIndicesBuffer = void 0;
1899
+ constantValuesBuffer = void 0;
1900
+ }
1901
+ params.copyInputs((_h = this.inputsBuffer) == null ? void 0 : _h.getArrayView(), (numElements) => {
1708
1902
  var _a2;
1709
1903
  (_a2 = this.inputsBuffer) == null ? void 0 : _a2.dispose();
1710
1904
  this.inputsBuffer = createFloat64WasmBuffer(this.wasmModule, numElements);
@@ -1712,7 +1906,7 @@ var WasmModel = class {
1712
1906
  });
1713
1907
  let outputIndicesBuffer;
1714
1908
  if (params.getOutputIndicesLength() > 0) {
1715
- params.copyOutputIndices((_e = this.outputIndicesBuffer) == null ? void 0 : _e.getArrayView(), (numElements) => {
1909
+ params.copyOutputIndices((_i = this.outputIndicesBuffer) == null ? void 0 : _i.getArrayView(), (numElements) => {
1716
1910
  var _a2;
1717
1911
  (_a2 = this.outputIndicesBuffer) == null ? void 0 : _a2.dispose();
1718
1912
  this.outputIndicesBuffer = createInt32WasmBuffer(this.wasmModule, numElements);
@@ -1724,14 +1918,19 @@ var WasmModel = class {
1724
1918
  }
1725
1919
  const outputsLengthInElements = params.getOutputsLength();
1726
1920
  if (this.outputsBuffer === void 0 || this.outputsBuffer.numElements < outputsLengthInElements) {
1727
- (_f = this.outputsBuffer) == null ? void 0 : _f.dispose();
1921
+ (_j = this.outputsBuffer) == null ? void 0 : _j.dispose();
1728
1922
  this.outputsBuffer = createFloat64WasmBuffer(this.wasmModule, outputsLengthInElements);
1729
1923
  }
1730
1924
  const t0 = perfNow();
1731
1925
  this.wasmRunModel(
1732
- ((_g = this.inputsBuffer) == null ? void 0 : _g.getAddress()) || 0,
1926
+ ((_k = this.inputsBuffer) == null ? void 0 : _k.getAddress()) || 0,
1927
+ // Always pass 0 (NULL) for input indices, since we assume that all input values are
1928
+ // provided and are in the same order as the input variables defined in the model spec
1929
+ 0,
1733
1930
  this.outputsBuffer.getAddress(),
1734
- (outputIndicesBuffer == null ? void 0 : outputIndicesBuffer.getAddress()) || 0
1931
+ (outputIndicesBuffer == null ? void 0 : outputIndicesBuffer.getAddress()) || 0,
1932
+ (constantValuesBuffer == null ? void 0 : constantValuesBuffer.getAddress()) || 0,
1933
+ (constantIndicesBuffer == null ? void 0 : constantIndicesBuffer.getAddress()) || 0
1735
1934
  );
1736
1935
  const elapsed = perfElapsed(t0);
1737
1936
  params.storeOutputs(this.outputsBuffer.getArrayView());
@@ -1739,13 +1938,17 @@ var WasmModel = class {
1739
1938
  }
1740
1939
  // from RunnableModel interface
1741
1940
  terminate() {
1742
- var _a, _b, _c;
1941
+ var _a, _b, _c, _d, _e;
1743
1942
  (_a = this.inputsBuffer) == null ? void 0 : _a.dispose();
1744
1943
  this.inputsBuffer = void 0;
1745
1944
  (_b = this.outputsBuffer) == null ? void 0 : _b.dispose();
1746
1945
  this.outputsBuffer = void 0;
1747
1946
  (_c = this.outputIndicesBuffer) == null ? void 0 : _c.dispose();
1748
1947
  this.outputIndicesBuffer = void 0;
1948
+ (_d = this.constantValuesBuffer) == null ? void 0 : _d.dispose();
1949
+ this.constantValuesBuffer = void 0;
1950
+ (_e = this.constantIndicesBuffer) == null ? void 0 : _e.dispose();
1951
+ this.constantIndicesBuffer = void 0;
1749
1952
  }
1750
1953
  };
1751
1954
  function initWasmModel(wasmModule) {
@@ -1761,6 +1964,7 @@ var MockWasmModule = class {
1761
1964
  this.mallocOffset = 8;
1762
1965
  this.allocs = /* @__PURE__ */ new Map();
1763
1966
  this.lookups = /* @__PURE__ */ new Map();
1967
+ this.constants = /* @__PURE__ */ new Map();
1764
1968
  this.initialTime = options.initialTime;
1765
1969
  this.finalTime = options.finalTime;
1766
1970
  this.outputVarIds = options.outputVarIds;
@@ -1800,11 +2004,35 @@ var MockWasmModule = class {
1800
2004
  this.lookups.set(varId, new JsModelLookup(numPoints, points));
1801
2005
  };
1802
2006
  case "runModelWithBuffers":
1803
- return (inputsAddress, outputsAddress, outputIndicesAddress) => {
2007
+ return (inputsAddress, _inputIndicesAddress, outputsAddress, outputIndicesAddress, constantValuesAddress, constantIndicesAddress) => {
1804
2008
  const inputs = this.getHeapView("float64", inputsAddress);
1805
2009
  const outputs = this.getHeapView("float64", outputsAddress);
1806
2010
  const outputIndices = this.getHeapView("int32", outputIndicesAddress);
1807
- this.onRunModel(inputs, outputs, this.lookups, outputIndices);
2011
+ this.constants.clear();
2012
+ if (constantValuesAddress !== 0 && constantIndicesAddress !== 0) {
2013
+ const constantValues = this.getHeapView("float64", constantValuesAddress);
2014
+ const constantIndices = this.getHeapView("int32", constantIndicesAddress);
2015
+ const numConstants = constantIndices[0];
2016
+ let indicesOffset = 1;
2017
+ let valuesOffset = 0;
2018
+ for (let i = 0; i < numConstants; i++) {
2019
+ const varIndex = constantIndices[indicesOffset++];
2020
+ const subCount = constantIndices[indicesOffset++];
2021
+ indicesOffset += subCount;
2022
+ const varId = this.varIdForSpec({ varIndex });
2023
+ if (varId) {
2024
+ this.constants.set(varId, constantValues[valuesOffset++]);
2025
+ }
2026
+ }
2027
+ }
2028
+ this.onRunModel(
2029
+ inputs,
2030
+ outputs,
2031
+ this.constants.size > 0 ? this.constants : void 0,
2032
+ this.lookups,
2033
+ outputIndices
2034
+ );
2035
+ this.constants.clear();
1808
2036
  };
1809
2037
  default:
1810
2038
  throw new Error(`Unhandled call to cwrap with function name '${fname}'`);
@@ -1882,7 +2110,7 @@ function createRunnerFromRunnableModel(model) {
1882
2110
  }
1883
2111
  return runModelSync(inputs, outputs, options);
1884
2112
  },
1885
- terminate: () => __async(this, null, function* () {
2113
+ terminate: () => __async(null, null, function* () {
1886
2114
  if (!terminated) {
1887
2115
  model.terminate();
1888
2116
  terminated = true;
@@ -2169,14 +2397,18 @@ var ModelContextImpl = class {
2169
2397
  Outputs,
2170
2398
  ReferencedRunModelParams,
2171
2399
  Series,
2400
+ createConstantDef,
2172
2401
  createInputValue,
2173
2402
  createLookupDef,
2174
2403
  createRunnableModel,
2175
2404
  createSynchronousModelRunner,
2405
+ decodeConstants,
2176
2406
  decodeLookups,
2407
+ encodeConstants,
2177
2408
  encodeLookups,
2178
2409
  encodeVarIndices,
2179
2410
  execJsModel,
2411
+ getEncodedConstantBufferLengths,
2180
2412
  getEncodedLookupBufferLengths,
2181
2413
  getEncodedVarIndicesLength,
2182
2414
  getJsModelFunctions,