@sdeverywhere/compile 0.7.21 → 0.7.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdeverywhere/compile",
3
- "version": "0.7.21",
3
+ "version": "0.7.22",
4
4
  "description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -153,11 +153,19 @@ ${chunkedFunctions('evalLevels', Model.levelVars(), ' // Evaluate levels.')}`
153
153
  let setLookupBody
154
154
  if (spec.customLookups === true || Array.isArray(spec.customLookups)) {
155
155
  setLookupBody = `\
156
+ Lookup** pLookup = NULL;
156
157
  switch (varIndex) {
157
158
  ${setLookupImpl(Model.varIndexInfo(), spec.customLookups)}
158
159
  default:
159
160
  fprintf(stderr, "No lookup found for var index %zu in setLookup\\n", varIndex);
160
161
  break;
162
+ }
163
+ if (pLookup != NULL) {
164
+ if (*pLookup == NULL) {
165
+ *pLookup = __new_lookup(numPoints, /*copy=*/true, points);
166
+ } else {
167
+ __set_lookup(*pLookup, numPoints, points);
168
+ }
161
169
  }`
162
170
  } else {
163
171
  let msg = 'The setLookup function was not enabled for the generated model. '
@@ -200,19 +208,6 @@ void setInputsFromBuffer(double* inputData) {
200
208
  ${inputsFromBufferImpl()}
201
209
  }
202
210
 
203
- void replaceLookup(Lookup** lookup, double* points, size_t numPoints) {
204
- if (lookup == NULL) {
205
- return;
206
- }
207
- if (*lookup != NULL) {
208
- __delete_lookup(*lookup);
209
- *lookup = NULL;
210
- }
211
- if (points != NULL) {
212
- *lookup = __new_lookup(numPoints, /*copy=*/true, points);
213
- }
214
- }
215
-
216
211
  void setLookup(size_t varIndex, size_t* subIndices, double* points, size_t numPoints) {
217
212
  ${setLookupBody}
218
213
  }
@@ -441,7 +436,7 @@ ${section(chunk)}
441
436
  return inputVars.join('\n')
442
437
  }
443
438
  function setLookupImpl(varIndexInfo, customLookups) {
444
- // Emit `replaceLookup` calls for all lookups and data variables that can be overridden
439
+ // Emit case statements for all lookups and data variables that can be overridden
445
440
  // at runtime
446
441
  let includeCase
447
442
  if (Array.isArray(customLookups)) {
@@ -467,7 +462,7 @@ ${section(chunk)}
467
462
  }
468
463
  let c = ''
469
464
  c += ` case ${info.varIndex}:\n`
470
- c += ` replaceLookup(&${lookupVar}, points, numPoints);\n`
465
+ c += ` pLookup = &${lookupVar};\n`
471
466
  c += ` break;`
472
467
  return c
473
468
  })
@@ -251,10 +251,15 @@ ${chunkedFunctions('evalLevels', true, Model.levelVars(), ' // Evaluate levels'
251
251
  }
252
252
  const varIndex = varSpec.varIndex;
253
253
  const subs = varSpec.subscriptIndices;
254
+ let lookup;
254
255
  switch (varIndex) {
255
256
  ${setLookupImpl(Model.varIndexInfo(), spec.customLookups)}
256
257
  default:
257
258
  throw new Error(\`No lookup found for var index \${varIndex} in setLookup\`);
259
+ }
260
+ if (lookup) {
261
+ const size = points ? points.length / 2 : 0;
262
+ lookup.setData(size, points);
258
263
  }`
259
264
  } else {
260
265
  let msg = 'The setLookup function was not enabled for the generated model. '
@@ -307,7 +312,7 @@ ${customOutputSection(Model.varIndexInfo(), spec.customOutputs)}
307
312
  return `\
308
313
  /*export*/ function setInputs(valueAtIndex /*: (index: number) => number*/) {${inputsFromBufferImpl()}}
309
314
 
310
- /*export*/ function setLookup(varSpec /*: VarSpec*/, points /*: Float64Array*/) {
315
+ /*export*/ function setLookup(varSpec /*: VarSpec*/, points /*: Float64Array | undefined*/) {
311
316
  ${setLookupBody}
312
317
  }
313
318
 
@@ -517,7 +522,7 @@ ${section(chunk)}
517
522
  return inputVars
518
523
  }
519
524
  function setLookupImpl(varIndexInfo, customLookups) {
520
- // Emit `createLookup` calls for all lookups and data variables that can be overridden
525
+ // Emit case statements for all lookups and data variables that can be overridden
521
526
  // at runtime
522
527
  let overrideAllowed
523
528
  if (Array.isArray(customLookups)) {
@@ -543,7 +548,7 @@ ${section(chunk)}
543
548
  }
544
549
  let c = ''
545
550
  c += ` case ${info.varIndex}:\n`
546
- c += ` ${lookupVar} = fns.createLookup(points.length / 2, points);\n`
551
+ c += ` lookup = ${lookupVar};\n`
547
552
  c += ` break;`
548
553
  return c
549
554
  })
@@ -113,13 +113,12 @@ export function generateEquation(variable, mode, extData, directData, modelDir,
113
113
  // Apply special handling for lookup variables. The data for lookup variables is already
114
114
  // defined as a set of explicit data points (stored in the `Variable` instance).
115
115
  if (variable.isLookup()) {
116
- if (variable.varSubtype === 'gameInputs') {
117
- // For a synthesized game inputs lookup, there is no data array (the data is expected
118
- // to be supplied at runtime), so don't emit decl or init code for these
119
- return []
116
+ // Emit decl/init code for the lookup
117
+ const lookupDef = generateLookupFromPoints(variable, mode, /*copy=*/ false, cLhs, loopIndexVars, outFormat)
118
+ if (lookupDef.length > 0) {
119
+ return [...openLoops, ...lookupDef, ...closeLoops]
120
120
  } else {
121
- // For all other lookups, emit decl/init code for the lookup
122
- return generateLookupFromPoints(variable, mode, /*copy=*/ false, cLhs, loopIndexVars, outFormat)
121
+ return []
123
122
  }
124
123
  }
125
124
 
@@ -16,10 +16,23 @@ import { isDimension, isTrivialDimension, sub } from '../_shared/subscript.js'
16
16
  */
17
17
  export function generateLookupFromPoints(variable, mode, copy, varLhs, loopIndexVars, outFormat) {
18
18
  if (variable.points.length === 0) {
19
- throw new Error(`Empty lookup data array for ${variable.modelLHS}`)
20
- }
21
-
22
- if (copy) {
19
+ // In the case where the lookup data array is empty (typically this only occurs in the
20
+ // case of game inputs), generate a new empty lookup
21
+ if (mode === 'decl') {
22
+ // Nothing to emit in decl mode
23
+ return []
24
+ } else if (mode === 'init-lookups') {
25
+ // In init mode, generate a new empty lookup
26
+ switch (outFormat) {
27
+ case 'c':
28
+ return [` ${varLhs} = __new_lookup(0, /*copy=*/false, NULL);`]
29
+ case 'js':
30
+ return [` ${varLhs} = fns.createLookup(0, undefined);`]
31
+ default:
32
+ throw new Error(`Unhandled output format '${outFormat}'`)
33
+ }
34
+ }
35
+ } else if (copy) {
23
36
  // Inline the data points and copy them when initializing the lookup
24
37
  if (mode === 'decl') {
25
38
  // Nothing to emit in decl mode
@@ -42,7 +42,7 @@ function toposort(nodes, edges) {
42
42
  } catch (e) {
43
43
  nodeRep = ''
44
44
  }
45
- throw new Error('Found cyclic dependency during toposort:\n' + [...predecessors].join(' →\n') + nodeRep)
45
+ throw new Error('Found cyclic dependency during toposort:\n' + [...predecessors].join(' →\n') + ' →' + nodeRep)
46
46
  }
47
47
 
48
48
  if (!nodesHash.has(node)) {