forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
119 lines (113 loc) · 3.56 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* global Compartment, trace, snapshot, globalThis */
trace(`in main module\n`);
import { Snapshot } from 'snapshot';
function traceError(thunk) {
try {
return thunk();
} catch (err) {
trace(`Error: ${err.message}\n`);
throw err;
}
}
const cases = [
{
input: undefined,
note: 'undefined',
parts: [
'00', // kind
'00', // flag
'0000', '00000000', // ID, ID name length
'FE', // next is NULL ("kind" -2)
],
struct: { next: null, kind: 0, flag: 0, id: 0 }
},
{
input: null,
struct: { next: null, kind: 1, flag: 0, id: 0, value: null },
},
{
input: true,
struct: { next: null, kind: 2, flag: 0, id: 0, value: true },
},
{
input: 1,
struct: { next: null, kind: 3, flag: 0, id: 0, value: 1 },
},
{
input: 1.5,
},
{
input: 2.5,
},
{
input: 'Hello World',
struct: { next: null, kind: 6, flag: 0, id: 0, value: "Hello World" },
},
{
input: 'Hello World'.repeat(20),
parts: [
'05', // kind
'00', '0000', '00000000', // flag, id, id name length
'DC000000', // string value length 220
'48656C6C6F20576F726C6448656C6C6F20576F726C64', // string value (repeat 0, 1)
'48656C6C6F20576F726C6448656C6C6F20576F726C64', // string value (cont.)
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'48656C6C6F20576F726C6448656C6C6F20576F726C64',
'FE', // NULL
]
},
{
input: [1, 2],
},
{
input: { x: 1 },
},
{
// we use eval() to avoid functions from ROM
input: (1, eval)(`(function f1(x, y, z) { return 0; })`),
note: 'function f() { ... }',
parts: null,
},
{
input: (1, eval)(`() => 1`),
note: 'arrow function',
parts: null,
},
{
input: eval(`(function f1(x, y, z) { return 0; })`),
note: 'direct eval',
parts: null,
},
];
export default function main() {
const exits = [Object.prototype, Array.prototype, String.prototype, Function.prototype,
Snapshot,
traceError,
globalThis];
const cycle = [1];
cycle.push(cycle);
cases.push({ input: cycle });
for (const aCase of cases) {
const { input: root, parts } = aCase;
const s1 = new Snapshot();
trace(`calling Snapshot.dump(type ${typeof root}) case keys: ${Object.keys(aCase)}...\n`);
const rawbuf = s1.dump(root, exits);
const actual = s1.tohex(rawbuf);
trace(`snapshot: ${rawbuf.byteLength} 0x${actual}\n`);
if (typeof parts != 'undefined' && parts !== null) {
const expected = '00'.repeat(exits.length) + parts.join('');
if (actual !== expected) {
trace(`FAIL: expected [${expected}].\n`);
}
}
const info = traceError(() => s1.restore(rawbuf, exits.length));
const { self, next, kind, flag, id, value } = info;
trace(`snapshot value: ${JSON.stringify({ self, next, kind, flag, id, value }, null, 2)}\n`);
}
}