@casadi/casadi-reader 0.0.0-poc

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 ADDED
@@ -0,0 +1,740 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+
9
+ // src/scheme.js
10
+ var scheme_default = {
11
+ "wire": {
12
+ "magic": 123456789012345,
13
+ "protocol": 3
14
+ },
15
+ "operations": {
16
+ "OP_ACOS": 17,
17
+ "OP_ACOSH": 41,
18
+ "OP_ADD": 1,
19
+ "OP_ADDNONZEROS": 71,
20
+ "OP_ADDNONZEROS_PARAM": 72,
21
+ "OP_AND": 24,
22
+ "OP_ASIN": 16,
23
+ "OP_ASINH": 40,
24
+ "OP_ASSERTION": 76,
25
+ "OP_ASSIGN": 0,
26
+ "OP_ATAN": 18,
27
+ "OP_ATAN2": 43,
28
+ "OP_ATANH": 42,
29
+ "OP_BILIN": 58,
30
+ "OP_BSPLINE": 90,
31
+ "OP_CALL": 48,
32
+ "OP_CEIL": 27,
33
+ "OP_CONST": 44,
34
+ "OP_CONSTPOW": 9,
35
+ "OP_CONVEXIFY": 91,
36
+ "OP_COPYSIGN": 31,
37
+ "OP_COS": 14,
38
+ "OP_COSH": 38,
39
+ "OP_DETERMINANT": 55,
40
+ "OP_DIAGCAT": 62,
41
+ "OP_DIAGSPLIT": 65,
42
+ "OP_DIV": 4,
43
+ "OP_DOT": 57,
44
+ "OP_DUMP": 98,
45
+ "OP_EINSTEIN": 89,
46
+ "OP_EQ": 21,
47
+ "OP_ERF": 33,
48
+ "OP_ERFINV": 86,
49
+ "OP_EXP": 6,
50
+ "OP_EXPM1": 94,
51
+ "OP_FABS": 29,
52
+ "OP_FIND": 49,
53
+ "OP_FLOOR": 26,
54
+ "OP_FMAX": 35,
55
+ "OP_FMIN": 34,
56
+ "OP_FMOD": 28,
57
+ "OP_GETNONZEROS": 69,
58
+ "OP_GETNONZEROS_PARAM": 70,
59
+ "OP_HORZCAT": 60,
60
+ "OP_HORZREPMAT": 84,
61
+ "OP_HORZREPSUM": 85,
62
+ "OP_HORZSPLIT": 63,
63
+ "OP_HYPOT": 95,
64
+ "OP_IF_ELSE_ZERO": 32,
65
+ "OP_INPUT": 45,
66
+ "OP_INV": 36,
67
+ "OP_INVERSE": 56,
68
+ "OP_KRON": 99,
69
+ "OP_KRON_CONTRACT": 100,
70
+ "OP_LE": 20,
71
+ "OP_LIFT": 88,
72
+ "OP_LOG": 7,
73
+ "OP_LOG1P": 93,
74
+ "OP_LOGSUMEXP": 96,
75
+ "OP_LOW": 50,
76
+ "OP_LT": 19,
77
+ "OP_MAP": 51,
78
+ "OP_MMAX": 83,
79
+ "OP_MMIN": 82,
80
+ "OP_MONITOR": 77,
81
+ "OP_MTIMES": 52,
82
+ "OP_MUL": 3,
83
+ "OP_NE": 22,
84
+ "OP_NEG": 5,
85
+ "OP_NORM1": 79,
86
+ "OP_NORM2": 78,
87
+ "OP_NORMF": 81,
88
+ "OP_NORMINF": 80,
89
+ "OP_NOT": 23,
90
+ "OP_OR": 25,
91
+ "OP_OUTPUT": 46,
92
+ "OP_PARAMETER": 47,
93
+ "OP_POW": 8,
94
+ "OP_PRINTME": 87,
95
+ "OP_PROJECT": 75,
96
+ "OP_RANK1": 59,
97
+ "OP_REMAINDER": 97,
98
+ "OP_RESHAPE": 66,
99
+ "OP_SETNONZEROS": 73,
100
+ "OP_SETNONZEROS_PARAM": 74,
101
+ "OP_SIGN": 30,
102
+ "OP_SIN": 13,
103
+ "OP_SINH": 37,
104
+ "OP_SOLVE": 53,
105
+ "OP_SPARSITY_CAST": 92,
106
+ "OP_SQ": 11,
107
+ "OP_SQRT": 10,
108
+ "OP_SUB": 2,
109
+ "OP_SUBASSIGN": 68,
110
+ "OP_SUBREF": 67,
111
+ "OP_TAN": 15,
112
+ "OP_TANH": 39,
113
+ "OP_TRANSPOSE": 54,
114
+ "OP_TWICE": 12,
115
+ "OP_VERTCAT": 61,
116
+ "OP_VERTSPLIT": 64
117
+ },
118
+ "class_versions": {
119
+ "AlpaqaInterface": [
120
+ 1
121
+ ],
122
+ "BSplineInterpolant": [
123
+ 1
124
+ ],
125
+ "Bisection": [
126
+ 1
127
+ ],
128
+ "BlazingSplineFunction": [
129
+ 2
130
+ ],
131
+ "Blocksqp": [
132
+ 1
133
+ ],
134
+ "BonminInterface": [
135
+ 1
136
+ ],
137
+ "CbcInterface": [
138
+ 1
139
+ ],
140
+ "ClarabelInterface": [
141
+ 1
142
+ ],
143
+ "ClpInterface": [
144
+ 1
145
+ ],
146
+ "Collocation": [
147
+ 2
148
+ ],
149
+ "Conic": [
150
+ 4
151
+ ],
152
+ "ConoptInterface": [
153
+ 1
154
+ ],
155
+ "CplexInterface": [
156
+ 1
157
+ ],
158
+ "CvodesInterface": [
159
+ 3
160
+ ],
161
+ "DaqpInterface": [
162
+ 1
163
+ ],
164
+ "DirResource": [
165
+ 1
166
+ ],
167
+ "Dump": [
168
+ 1
169
+ ],
170
+ "External": [
171
+ 2
172
+ ],
173
+ "FatropConicInterface": [
174
+ 1
175
+ ],
176
+ "FatropInterface": [
177
+ 1
178
+ ],
179
+ "Feasiblesqpmethod": [
180
+ 3
181
+ ],
182
+ "FixedStepIntegrator": [
183
+ 3
184
+ ],
185
+ "Fmu2": [
186
+ 2
187
+ ],
188
+ "Fmu3": [
189
+ 1
190
+ ],
191
+ "FmuFunction": [
192
+ 6
193
+ ],
194
+ "FmuInternal": [
195
+ 4
196
+ ],
197
+ "FunctionInternal": [
198
+ 8
199
+ ],
200
+ "GenericExternal": [
201
+ 1
202
+ ],
203
+ "GurobiInterface": [
204
+ 2
205
+ ],
206
+ "HighsInterface": [
207
+ 1
208
+ ],
209
+ "HpipmInterface": [
210
+ 1
211
+ ],
212
+ "IdasInterface": [
213
+ 2
214
+ ],
215
+ "ImplicitFixedStepIntegrator": [
216
+ 2
217
+ ],
218
+ "ImporterInternal": [
219
+ 1
220
+ ],
221
+ "Integrator": [
222
+ 3
223
+ ],
224
+ "Interpolant": [
225
+ 2
226
+ ],
227
+ "IpoptInterface": [
228
+ 3
229
+ ],
230
+ "Ipqp": [
231
+ 1
232
+ ],
233
+ "KnitroInterface": [
234
+ 2
235
+ ],
236
+ "LapackLu": [
237
+ 1
238
+ ],
239
+ "LinearInterpolant": [
240
+ 1
241
+ ],
242
+ "LinsolLdl": [
243
+ 1
244
+ ],
245
+ "LinsolQr": [
246
+ 2
247
+ ],
248
+ "MXFunction": [
249
+ 3
250
+ ],
251
+ "MadmpecInterface": [
252
+ 1
253
+ ],
254
+ "MadnlpInterface": [
255
+ 2
256
+ ],
257
+ "MosekInterface": [
258
+ 1
259
+ ],
260
+ "Mumps": [
261
+ 1
262
+ ],
263
+ "Newton": [
264
+ 1
265
+ ],
266
+ "Nlpsol": [
267
+ 5
268
+ ],
269
+ "OnnxFunction": [
270
+ 2
271
+ ],
272
+ "OnnxRuntimeInterface": [
273
+ 1
274
+ ],
275
+ "OoqpInterface": [
276
+ 1
277
+ ],
278
+ "OracleFunction": [
279
+ 3
280
+ ],
281
+ "OsqpInterface": [
282
+ 2
283
+ ],
284
+ "PiqpInterface": [
285
+ 1
286
+ ],
287
+ "ProtoFunction": [
288
+ 2
289
+ ],
290
+ "ProxqpInterface": [
291
+ 1
292
+ ],
293
+ "QpToNlp": [
294
+ 1
295
+ ],
296
+ "QpoasesInterface": [
297
+ 1
298
+ ],
299
+ "Qrqp": [
300
+ 1
301
+ ],
302
+ "ResourceInternal": [
303
+ 1
304
+ ],
305
+ "Rootfinder": [
306
+ 3
307
+ ],
308
+ "RungeKutta": [
309
+ 2
310
+ ],
311
+ "SLEQPInterface": [
312
+ 1
313
+ ],
314
+ "SXFunction": [
315
+ 4
316
+ ],
317
+ "SnoptInterface": [
318
+ 1
319
+ ],
320
+ "Sqpmethod": [
321
+ 3
322
+ ],
323
+ "SundialsInterface": [
324
+ 2
325
+ ],
326
+ "SuperscsInterface": [
327
+ 1
328
+ ],
329
+ "Switch": [
330
+ 1
331
+ ],
332
+ "SymbolicQr": [
333
+ 1
334
+ ],
335
+ "UnoInterface": [
336
+ 1
337
+ ],
338
+ "WorhpInterface": [
339
+ 1
340
+ ],
341
+ "XFunction": [
342
+ 1
343
+ ],
344
+ "XpressInterface": [
345
+ 2
346
+ ],
347
+ "ZipMemResource": [
348
+ 1
349
+ ],
350
+ "ZipResource": [
351
+ 1
352
+ ]
353
+ }
354
+ };
355
+
356
+ // src/casadi-ops.js
357
+ var OP = Object.fromEntries(Object.entries(scheme_default.operations).map(([name, id]) => [name.slice(3).toLowerCase(), id]));
358
+
359
+ // src/source.js
360
+ var EncodedSource = class {
361
+ constructor(text, { lazy = false, maxBytes = 1024 * 1024 * 1024 } = {}) {
362
+ if (typeof text !== "string") throw Error("Expected .casadi file text");
363
+ this.text = text.trim();
364
+ this.byteLength = this.text.length / 2;
365
+ this.decodedBytes = 0;
366
+ if (!Number.isSafeInteger(this.byteLength) || this.byteLength > maxBytes) throw Error("Invalid .casadi encoding or file too large");
367
+ if (!lazy && /[^a-p]/.test(this.text)) throw Error("Invalid .casadi encoding");
368
+ }
369
+ byte(offset) {
370
+ if (!Number.isSafeInteger(offset) || offset < 0 || offset >= this.byteLength) throw Error("Encoded source offset out of bounds");
371
+ const a = this.text.charCodeAt(2 * offset) - 97, b = this.text.charCodeAt(2 * offset + 1) - 97;
372
+ if (a < 0 || a > 15 || b < 0 || b > 15) throw Error("Invalid .casadi encoding at byte " + offset);
373
+ this.decodedBytes++;
374
+ return a | b << 4;
375
+ }
376
+ read(offset, length) {
377
+ if (!Number.isSafeInteger(length) || length < 0 || offset < 0 || offset + length > this.byteLength) throw Error("Encoded source range out of bounds");
378
+ return Uint8Array.from({ length }, (_, i) => this.byte(offset + i));
379
+ }
380
+ view(offset, length) {
381
+ return new DataView(this.read(offset, length).buffer);
382
+ }
383
+ };
384
+ var _source;
385
+ var LazyBlob = class {
386
+ constructor(source, offset, byteLength) {
387
+ __privateAdd(this, _source);
388
+ __privateSet(this, _source, source);
389
+ this.offset = offset;
390
+ this.byteLength = byteLength;
391
+ }
392
+ read(offset = 0, length = this.byteLength - offset) {
393
+ if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || offset < 0 || length < 0 || offset + length > this.byteLength) throw Error("Blob slice out of bounds");
394
+ return __privateGet(this, _source).read(this.offset + offset, length);
395
+ }
396
+ toJSON() {
397
+ return { kind: "deferred_bytes", offset: this.offset, byteLength: this.byteLength, encoding: "casadi-nibbles" };
398
+ }
399
+ };
400
+ _source = new WeakMap();
401
+ var _file;
402
+ var LazyFileBlob = class {
403
+ constructor(file, offset, byteLength) {
404
+ __privateAdd(this, _file);
405
+ __privateSet(this, _file, file);
406
+ this.offset = offset;
407
+ this.byteLength = byteLength;
408
+ }
409
+ async read(offset = 0, length = this.byteLength - offset) {
410
+ if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || offset < 0 || length < 0 || offset + length > this.byteLength) throw Error("Blob slice out of bounds");
411
+ const text = await __privateGet(this, _file).slice(2 * (this.offset + offset), 2 * (this.offset + offset + length)).text();
412
+ return new EncodedSource(text).read(0, length);
413
+ }
414
+ toJSON() {
415
+ return { kind: "deferred_file_bytes", offset: this.offset, byteLength: this.byteLength, encoding: "casadi-nibbles" };
416
+ }
417
+ };
418
+ _file = new WeakMap();
419
+
420
+ // src/index.js
421
+ var names = new Map(Object.entries(OP).map(([name, id]) => [id, name]));
422
+ var unary = new Set([
423
+ "neg",
424
+ "exp",
425
+ "log",
426
+ "sqrt",
427
+ "sq",
428
+ "twice",
429
+ "sin",
430
+ "cos",
431
+ "tan",
432
+ "asin",
433
+ "acos",
434
+ "atan",
435
+ "not",
436
+ "floor",
437
+ "ceil",
438
+ "fabs",
439
+ "sign",
440
+ "erf",
441
+ "inv",
442
+ "sinh",
443
+ "cosh",
444
+ "tanh",
445
+ "asinh",
446
+ "acosh",
447
+ "atanh"
448
+ ].map((n) => OP[n]));
449
+ var binary = new Set([
450
+ "add",
451
+ "sub",
452
+ "mul",
453
+ "div",
454
+ "pow",
455
+ "constpow",
456
+ "lt",
457
+ "le",
458
+ "eq",
459
+ "ne",
460
+ "and",
461
+ "or",
462
+ "fmod",
463
+ "copysign",
464
+ "if_else_zero",
465
+ "fmin",
466
+ "fmax",
467
+ "atan2"
468
+ ].map((n) => OP[n]));
469
+ var mappingOps = /* @__PURE__ */ new Set([OP.getnonzeros, OP.setnonzeros, OP.addnonzeros]);
470
+ var maxItems = 1e6;
471
+ var Reader = class {
472
+ constructor(text, options = {}) {
473
+ this.source = new EncodedSource(text, options);
474
+ this.lazy = !!options.lazy;
475
+ this.pos = 0;
476
+ this.shared = [];
477
+ this.depth = 0;
478
+ this.length = this.source.byteLength;
479
+ }
480
+ fail(message) {
481
+ throw Error(`.casadi byte ${this.pos}: ${message}`);
482
+ }
483
+ take(n) {
484
+ if (n < 0 || this.pos + n > this.length) this.fail("Truncated file");
485
+ const p = this.pos;
486
+ this.pos += n;
487
+ return p;
488
+ }
489
+ byte() {
490
+ return this.source.byte(this.take(1));
491
+ }
492
+ bool() {
493
+ const b = this.byte();
494
+ if (b > 1) this.fail("Invalid boolean");
495
+ return !!b;
496
+ }
497
+ int() {
498
+ return this.source.view(this.take(4), 4).getInt32(0, true);
499
+ }
500
+ long() {
501
+ const n = this.source.view(this.take(8), 8).getBigInt64(0, true);
502
+ if (n > BigInt(Number.MAX_SAFE_INTEGER) || n < BigInt(Number.MIN_SAFE_INTEGER)) this.fail("Integer outside JavaScript safe range");
503
+ return Number(n);
504
+ }
505
+ double() {
506
+ return this.source.view(this.take(8), 8).getFloat64(0, true);
507
+ }
508
+ count(n) {
509
+ if (!Number.isSafeInteger(n) || n < 0 || n > maxItems) this.fail("Invalid or excessive collection size");
510
+ return n;
511
+ }
512
+ string() {
513
+ const n = this.count(this.int()), start = this.take(n);
514
+ return new TextDecoder("utf-8", { fatal: true }).decode(this.source.read(start, n));
515
+ }
516
+ vector(read) {
517
+ const n = this.count(this.long());
518
+ return Array.from({ length: n }, () => read.call(this));
519
+ }
520
+ blob() {
521
+ const length = this.long(), offset = this.take(length);
522
+ return this.lazy ? new LazyBlob(this.source, offset, length) : this.source.read(offset, length);
523
+ }
524
+ emptyMap() {
525
+ if (this.long() !== 0) this.fail("Nonempty option/cache dictionaries are outside this proof of concept");
526
+ return {};
527
+ }
528
+ version(name, expected) {
529
+ if (!scheme_default.class_versions[name]?.includes(expected)) this.fail(`Vendored scheme does not describe ${name} version ${expected}`);
530
+ const v = this.int();
531
+ if (v !== expected) this.fail(`Unsupported ${name} serialization version ${v}; expected ${expected}`);
532
+ }
533
+ object(kind, read) {
534
+ const flag = this.byte();
535
+ if (flag === 114) {
536
+ const id = this.long(), entry = this.shared[id];
537
+ if (!entry || entry.kind !== kind && entry.value !== null) this.fail(`Invalid shared-object reference ${id}: expected ${kind}, got ${entry?.kind}; objects=${this.shared.map((e) => e.kind)}`);
538
+ return entry.value;
539
+ }
540
+ if (flag !== 100) this.fail("Invalid shared-object definition");
541
+ if (++this.depth > 256) this.fail("Object nesting limit exceeded");
542
+ const value = read.call(this);
543
+ --this.depth;
544
+ if (this.shared.length >= maxItems) this.fail("Too many shared objects");
545
+ this.shared.push({ kind, value });
546
+ return value;
547
+ }
548
+ sparsity() {
549
+ return this.object("S", function() {
550
+ const v = this.vector(this.long);
551
+ if (!v.length) return null;
552
+ const [rows, cols] = v;
553
+ this.count(rows);
554
+ this.count(cols);
555
+ const colind = v.slice(2, 3 + cols), row = v.slice(3 + cols);
556
+ if (colind.length !== cols + 1 || colind[0] !== 0 || colind[cols] !== row.length || colind.some((x, i) => x < 0 || x > row.length || i && x < colind[i - 1]) || row.some((x) => x < 0 || x >= rows)) this.fail("Invalid sparsity");
557
+ return { shape: [rows, cols], colind, row };
558
+ });
559
+ }
560
+ slice() {
561
+ return { start: this.long(), stop: this.long(), step: this.long() };
562
+ }
563
+ expand(s) {
564
+ if (!s.step) this.fail("Zero slice step");
565
+ const n = this.count(Math.max(0, Math.ceil((s.stop - s.start) / s.step)));
566
+ return Array.from({ length: n }, (_, i) => s.start + i * s.step);
567
+ }
568
+ mx() {
569
+ return this.object("X", function() {
570
+ const op = this.int(), name = names.get(op) || String(op);
571
+ let subtype, value;
572
+ if (binary.has(op)) subtype = this.byte();
573
+ else if (op === OP.const) {
574
+ subtype = String.fromCharCode(this.byte());
575
+ if (subtype === "D") value = this.double();
576
+ else if (subtype === "I") value = this.long();
577
+ else if (["0", "1", "-"].includes(subtype)) value = subtype === "-" ? -1 : Number(subtype);
578
+ else if (subtype !== "a") this.fail(`Unsupported constant subtype ${subtype}`);
579
+ } else if (mappingOps.has(op)) subtype = String.fromCharCode(this.byte());
580
+ else if (!unary.has(op) && ![OP.parameter, OP.input, OP.output, OP.reshape, OP.transpose].includes(op)) this.fail(`Unsupported MX operation ${name} (${op})`);
581
+ const deps = this.vector(this.mx), sp = this.sparsity(), info = {};
582
+ if (!sp && op !== OP.output) this.fail("Null node sparsity");
583
+ const node = { op, deps, sp, info };
584
+ if (unary.has(op) || binary.has(op)) {
585
+ if (this.int() !== op) this.fail("Inconsistent operation ID");
586
+ } else if (op === OP.parameter) node.symbol = this.string();
587
+ else if (op === OP.input || op === OP.output) {
588
+ info.ind = this.long();
589
+ info.segment = this.long();
590
+ info.offset = this.long();
591
+ } else if (op === OP.const) {
592
+ node.constants = subtype === "a" ? this.vector(this.double) : Array(sp.row.length).fill(value);
593
+ } else if (mappingOps.has(op)) {
594
+ if (subtype === "a") {
595
+ info.nz = this.vector(this.long);
596
+ node.mapping = info.nz;
597
+ } else if (subtype === "b") {
598
+ info.slice = this.slice();
599
+ node.mapping = this.expand(info.slice);
600
+ } else if (subtype === "c") {
601
+ info.inner = this.slice();
602
+ info.outer = this.slice();
603
+ const inner = this.expand(info.inner), outer = this.expand(info.outer);
604
+ this.count(inner.length * outer.length);
605
+ node.mapping = outer.flatMap((o) => inner.map((i) => i + o));
606
+ } else this.fail(`Unsupported mapping subtype ${subtype}`);
607
+ if (op !== OP.getnonzeros) info.add = op === OP.addnonzeros;
608
+ }
609
+ return node;
610
+ });
611
+ }
612
+ function() {
613
+ return this.object("F", function() {
614
+ if (this.bool()) return null;
615
+ const type = this.string();
616
+ if (type !== "MXFunction") this.fail(`Unsupported function type ${type}; this prototype reads MXFunction`);
617
+ this.version("ProtoFunction", 2);
618
+ const name = this.string();
619
+ for (let i = 0; i < 5; i++) this.bool();
620
+ this.version("FunctionInternal", 8);
621
+ this.vector(this.bool);
622
+ this.vector(this.bool);
623
+ const ins = this.vector(this.sparsity), outs = this.vector(this.sparsity);
624
+ const inputNames = this.vector(this.string), outputNames = this.vector(this.string);
625
+ if (this.bool()) this.fail("JIT functions are unsupported");
626
+ this.bool();
627
+ if (this.string() !== "source") this.fail("Linked/embedded JIT libraries are unsupported");
628
+ this.bool();
629
+ this.string();
630
+ this.emptyMap();
631
+ this.string();
632
+ this.bool();
633
+ this.emptyMap();
634
+ this.function();
635
+ this.double();
636
+ for (let i = 0; i < 8; i++) this.bool();
637
+ this.double();
638
+ this.double();
639
+ this.bool();
640
+ this.bool();
641
+ this.long();
642
+ this.bool();
643
+ this.double();
644
+ this.string();
645
+ this.bool();
646
+ this.bool();
647
+ this.bool();
648
+ this.long();
649
+ this.bool();
650
+ this.bool();
651
+ this.string();
652
+ this.string();
653
+ for (let i = 0; i < 4; i++) this.emptyMap();
654
+ this.function();
655
+ this.vector(this.function);
656
+ for (let i = 0; i < 8; i++) this.long();
657
+ this.version("XFunction", 1);
658
+ const inputNodes = this.vector(this.mx);
659
+ this.version("MXFunction", 3);
660
+ const n = this.count(this.long()), instructions = [];
661
+ for (let i = 0; i < n; i++) instructions.push({ node: this.mx(), arg: this.vector(this.long), res: this.vector(this.long) });
662
+ this.vector(this.long);
663
+ this.vector(this.mx);
664
+ this.vector(this.double);
665
+ this.bool();
666
+ this.bool();
667
+ this.bool();
668
+ this.vector(this.mx);
669
+ if (ins.length !== inputNames.length || outs.length !== outputNames.length) this.fail("Invalid function ports");
670
+ return { name, type, ins, outs, inputNames, outputNames, inputNodes, instructions };
671
+ });
672
+ }
673
+ };
674
+ function decodeCasadi(text, options = {}) {
675
+ const r = new Reader(text, options);
676
+ if (r.long() !== scheme_default.wire.magic) r.fail("Invalid serialization magic");
677
+ if (r.long() !== scheme_default.wire.protocol) r.fail("Unsupported serialization protocol");
678
+ if (r.bool()) r.fail("Debug serialization is outside this proof of concept");
679
+ if (r.byte() !== 5) r.fail("Expected a serialized Function");
680
+ const f = r.function();
681
+ if (!f) r.fail("Null Function");
682
+ if (r.pos !== r.length) r.fail("Trailing data or unsupported serialization layout");
683
+ const ids = new Map(r.shared.filter((e) => e.value !== null).map((e, i) => [e.value, i]));
684
+ const ref = (value) => value === null ? null : ids.get(value);
685
+ const objects = r.shared.filter((e) => e.value !== null).map(({ kind, value }) => {
686
+ if (kind === "S") return { kind: "sparsity", ...value };
687
+ if (kind === "X") return { kind: "mx", ...value, operation: names.get(value.op), ...value.constants ? { constants: value.constants.map(String) } : {}, deps: value.deps.map(ref), sp: ref(value.sp) };
688
+ if (kind === "F") return {
689
+ kind: "function",
690
+ ...value,
691
+ ins: value.ins.map(ref),
692
+ outs: value.outs.map(ref),
693
+ inputNodes: value.inputNodes.map(ref),
694
+ instructions: value.instructions.map((i) => ({ ...i, node: ref(i.node) }))
695
+ };
696
+ throw Error("Unsupported object kind");
697
+ });
698
+ return { format: "casadi_json", version: 1, serializationProtocol: 3, root: ref(f), objects };
699
+ }
700
+ function decodeResource(text, options = {}) {
701
+ return resourceDocument(new Reader(text, options));
702
+ }
703
+ function resourceDocument(r) {
704
+ if (r.long() !== scheme_default.wire.magic || r.long() !== scheme_default.wire.protocol) r.fail("Unsupported serialization header");
705
+ if (r.bool()) r.fail("Debug Resource streams are not supported by this entry point");
706
+ const resource = r.object("R", function() {
707
+ this.version("ResourceInternal", 1);
708
+ const type = this.string(), serializeMode = this.string();
709
+ this.version(type, 1);
710
+ if (type === "ZipMemResource") return { type, serializeMode, blob: this.blob() };
711
+ if (type === "ZipResource" || type === "DirResource") return { type, serializeMode, path: this.string() };
712
+ this.fail("Unsupported resource type " + type);
713
+ });
714
+ if (r.pos !== r.length) r.fail("Trailing Resource data");
715
+ return { format: "casadi_resource", version: 1, resource };
716
+ }
717
+ var readCasadi = decodeCasadi;
718
+ async function openResource(file, { lazy = false, maxBytes = 1024 * 1024 * 1024 } = {}) {
719
+ if (!Number.isSafeInteger(file.size) || file.size % 2 || file.size / 2 > maxBytes) throw Error("Invalid encoded file size");
720
+ if (!lazy) return decodeResource(await file.text(), { maxBytes });
721
+ for (let prefix = 256; ; prefix *= 2) {
722
+ const text = await file.slice(0, Math.min(file.size, prefix)).text();
723
+ const reader = new Reader(text, { lazy: true, maxBytes });
724
+ reader.length = file.size / 2;
725
+ try {
726
+ const document = resourceDocument(reader), blob = document.resource.blob;
727
+ if (blob instanceof LazyBlob) document.resource.blob = new LazyFileBlob(file, blob.offset, blob.byteLength);
728
+ return document;
729
+ } catch (error) {
730
+ if (!/offset out of bounds|range out of bounds/.test(error.message) || prefix >= file.size || prefix >= 1024 * 1024) throw error;
731
+ }
732
+ }
733
+ }
734
+ export {
735
+ LazyBlob,
736
+ decodeCasadi,
737
+ decodeResource,
738
+ openResource,
739
+ readCasadi
740
+ };