@agoric/xsnap 0.14.3-dev-b047af2.0 → 0.14.3-dev-2a16ddd.0

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.
Files changed (125) hide show
  1. package/package.json +8 -15
  2. package/src/build.js +4 -61
  3. package/moddable/modules/data/base64/base64.js +0 -28
  4. package/moddable/modules/data/base64/manifest.json +0 -11
  5. package/moddable/modules/data/base64/modBase64.c +0 -188
  6. package/moddable/modules/data/binaryMessage/BinaryMessage.js +0 -106
  7. package/moddable/modules/data/crc/crc.c +0 -205
  8. package/moddable/modules/data/crc/crc.js +0 -36
  9. package/moddable/modules/data/crc/manifest.json +0 -8
  10. package/moddable/modules/data/hex/hex.js +0 -28
  11. package/moddable/modules/data/hex/manifest.json +0 -11
  12. package/moddable/modules/data/hex/modHex.c +0 -139
  13. package/moddable/modules/data/logical/logical.js +0 -32
  14. package/moddable/modules/data/logical/modLogical.c +0 -98
  15. package/moddable/modules/data/qrcode/manifest.json +0 -9
  16. package/moddable/modules/data/qrcode/qrcode.c +0 -93
  17. package/moddable/modules/data/qrcode/qrcode.js +0 -23
  18. package/moddable/modules/data/qrcode/qrcodegen.c +0 -1025
  19. package/moddable/modules/data/qrcode/qrcodegen.h +0 -267
  20. package/moddable/modules/data/text/decoder/manifest.json +0 -8
  21. package/moddable/modules/data/text/decoder/textdecoder.c +0 -480
  22. package/moddable/modules/data/text/decoder/textdecoder.js +0 -27
  23. package/moddable/modules/data/text/encoder/manifest.json +0 -8
  24. package/moddable/modules/data/text/encoder/textencoder.c +0 -232
  25. package/moddable/modules/data/text/encoder/textencoder.js +0 -24
  26. package/moddable/modules/data/tinyint/tinyint.c +0 -150
  27. package/moddable/modules/data/tinyint/tinyint.js +0 -53
  28. package/moddable/modules/data/url/manifest.json +0 -17
  29. package/moddable/modules/data/url/url.c +0 -1959
  30. package/moddable/modules/data/url/url.js +0 -210
  31. package/moddable/modules/data/wavreader/manifest.json +0 -8
  32. package/moddable/modules/data/wavreader/wavreader.js +0 -128
  33. package/moddable/modules/data/zlib/deflate.c +0 -161
  34. package/moddable/modules/data/zlib/deflate.js +0 -63
  35. package/moddable/modules/data/zlib/inflate.c +0 -145
  36. package/moddable/modules/data/zlib/inflate.js +0 -66
  37. package/moddable/modules/data/zlib/manifest_deflate.json +0 -9
  38. package/moddable/modules/data/zlib/manifest_inflate.json +0 -9
  39. package/moddable/modules/data/zlib/miniz.c +0 -4924
  40. package/moddable/xs/includes/xs.d.ts +0 -73
  41. package/moddable/xs/includes/xs.h +0 -1533
  42. package/moddable/xs/includes/xsmc.h +0 -206
  43. package/moddable/xs/makefiles/lin/makefile +0 -33
  44. package/moddable/xs/makefiles/lin/xsc.mk +0 -118
  45. package/moddable/xs/makefiles/lin/xsid.mk +0 -90
  46. package/moddable/xs/makefiles/lin/xsl.mk +0 -168
  47. package/moddable/xs/makefiles/lin/xst.mk +0 -201
  48. package/moddable/xs/makefiles/mac/makefile +0 -33
  49. package/moddable/xs/makefiles/mac/xsc.mk +0 -130
  50. package/moddable/xs/makefiles/mac/xsid.mk +0 -102
  51. package/moddable/xs/makefiles/mac/xsl.mk +0 -177
  52. package/moddable/xs/makefiles/mac/xst.mk +0 -203
  53. package/moddable/xs/makefiles/mac/xst_no_asan.txt +0 -52
  54. package/moddable/xs/makefiles/win/build.bat +0 -26
  55. package/moddable/xs/makefiles/win/xsc.mak +0 -142
  56. package/moddable/xs/makefiles/win/xsid.mak +0 -113
  57. package/moddable/xs/makefiles/win/xsl.mak +0 -186
  58. package/moddable/xs/makefiles/win/xst.mak +0 -195
  59. package/moddable/xs/platforms/lin_xs.h +0 -99
  60. package/moddable/xs/platforms/mac_xs.h +0 -97
  61. package/moddable/xs/platforms/wasm_xs.h +0 -79
  62. package/moddable/xs/platforms/win_xs.h +0 -104
  63. package/moddable/xs/platforms/xsHost.h +0 -63
  64. package/moddable/xs/platforms/xsPlatform.h +0 -618
  65. package/moddable/xs/sources/xsAPI.c +0 -2555
  66. package/moddable/xs/sources/xsAll.c +0 -294
  67. package/moddable/xs/sources/xsAll.h +0 -2741
  68. package/moddable/xs/sources/xsArguments.c +0 -222
  69. package/moddable/xs/sources/xsArray.c +0 -2657
  70. package/moddable/xs/sources/xsAtomics.c +0 -844
  71. package/moddable/xs/sources/xsBigInt.c +0 -1859
  72. package/moddable/xs/sources/xsBoolean.c +0 -109
  73. package/moddable/xs/sources/xsCode.c +0 -4493
  74. package/moddable/xs/sources/xsCommon.c +0 -1710
  75. package/moddable/xs/sources/xsCommon.h +0 -1142
  76. package/moddable/xs/sources/xsDataView.c +0 -2890
  77. package/moddable/xs/sources/xsDate.c +0 -1541
  78. package/moddable/xs/sources/xsDebug.c +0 -2710
  79. package/moddable/xs/sources/xsDefaults.c +0 -134
  80. package/moddable/xs/sources/xsError.c +0 -353
  81. package/moddable/xs/sources/xsFunction.c +0 -776
  82. package/moddable/xs/sources/xsGenerator.c +0 -865
  83. package/moddable/xs/sources/xsGlobal.c +0 -839
  84. package/moddable/xs/sources/xsJSON.c +0 -1091
  85. package/moddable/xs/sources/xsLexical.c +0 -1969
  86. package/moddable/xs/sources/xsLockdown.c +0 -933
  87. package/moddable/xs/sources/xsMapSet.c +0 -1649
  88. package/moddable/xs/sources/xsMarshall.c +0 -1020
  89. package/moddable/xs/sources/xsMath.c +0 -624
  90. package/moddable/xs/sources/xsMemory.c +0 -1941
  91. package/moddable/xs/sources/xsModule.c +0 -3101
  92. package/moddable/xs/sources/xsNumber.c +0 -560
  93. package/moddable/xs/sources/xsObject.c +0 -1102
  94. package/moddable/xs/sources/xsPlatforms.c +0 -480
  95. package/moddable/xs/sources/xsProfile.c +0 -577
  96. package/moddable/xs/sources/xsPromise.c +0 -1199
  97. package/moddable/xs/sources/xsProperty.c +0 -636
  98. package/moddable/xs/sources/xsProxy.c +0 -1014
  99. package/moddable/xs/sources/xsRegExp.c +0 -1168
  100. package/moddable/xs/sources/xsRun.c +0 -4889
  101. package/moddable/xs/sources/xsScope.c +0 -1293
  102. package/moddable/xs/sources/xsScript.c +0 -288
  103. package/moddable/xs/sources/xsScript.h +0 -1186
  104. package/moddable/xs/sources/xsSnapshot.c +0 -2161
  105. package/moddable/xs/sources/xsSnapshot.h +0 -51
  106. package/moddable/xs/sources/xsSourceMap.c +0 -218
  107. package/moddable/xs/sources/xsString.c +0 -3332
  108. package/moddable/xs/sources/xsSymbol.c +0 -503
  109. package/moddable/xs/sources/xsSyntaxical.c +0 -4193
  110. package/moddable/xs/sources/xsTree.c +0 -1893
  111. package/moddable/xs/sources/xsType.c +0 -1488
  112. package/moddable/xs/sources/xsdtoa.c +0 -6672
  113. package/moddable/xs/sources/xsmc.c +0 -340
  114. package/moddable/xs/sources/xsre.c +0 -7578
  115. package/xsnap-native/xsnap/makefiles/lin/makefile +0 -10
  116. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +0 -153
  117. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +0 -144
  118. package/xsnap-native/xsnap/makefiles/mac/makefile +0 -10
  119. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +0 -162
  120. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +0 -153
  121. package/xsnap-native/xsnap/sources/xsnap-worker.c +0 -996
  122. package/xsnap-native/xsnap/sources/xsnap.c +0 -716
  123. package/xsnap-native/xsnap/sources/xsnap.h +0 -142
  124. package/xsnap-native/xsnap/sources/xsnapPlatform.c +0 -1496
  125. package/xsnap-native/xsnap/sources/xsnapPlatform.h +0 -105
@@ -1,716 +0,0 @@
1
- #include "xsnap.h"
2
-
3
- #define SNAPSHOT_SIGNATURE "xsnap 1"
4
-
5
- extern void fxDumpSnapshot(xsMachine* the, xsSnapshot* snapshot);
6
-
7
- static void xsBuildAgent(xsMachine* the);
8
- static void xsPrintUsage();
9
- static void xsReplay(xsMachine* machine);
10
-
11
- //static void xs_clearTimer(xsMachine* the);
12
- static void xs_currentMeterLimit(xsMachine* the);
13
- static void xs_gc(xsMachine* the);
14
- static void xs_issueCommand(xsMachine* the);
15
- static void xs_lockdown(xsMachine *the);
16
- static void xs_performance_now(xsMachine* the);
17
- static void xs_print(xsMachine* the);
18
- static void xs_resetMeter(xsMachine* the);
19
- static void xs_setImmediate(xsMachine* the);
20
- //static void xs_setInterval(xsMachine* the);
21
- //static void xs_setTimeout(xsMachine* the);
22
-
23
- extern void xs_textdecoder(xsMachine *the);
24
- extern void xs_textdecoder_decode(xsMachine *the);
25
- extern void xs_textdecoder_get_encoding(xsMachine *the);
26
- extern void xs_textdecoder_get_ignoreBOM(xsMachine *the);
27
- extern void xs_textdecoder_get_fatal(xsMachine *the);
28
-
29
- extern void xs_textencoder(xsMachine *the);
30
- extern void xs_textencoder_encode(xsMachine *the);
31
- extern void xs_textencoder_encodeInto(xsMachine *the);
32
-
33
- extern void modInstallTextDecoder(xsMachine *the);
34
- extern void modInstallTextEncoder(xsMachine *the);
35
-
36
- extern void xs_base64_encode(xsMachine *the);
37
- extern void xs_base64_decode(xsMachine *the);
38
- extern void modInstallBase64(xsMachine *the);
39
-
40
- // The order of the callbacks materially affects how they are introduced to
41
- // code that runs from a snapshot, so must be consistent in the face of
42
- // upgrade.
43
- #define mxSnapshotCallbackCount 18
44
- xsCallback gxSnapshotCallbacks[mxSnapshotCallbackCount] = {
45
- xs_issueCommand, // 0
46
- xs_print, // 1
47
- xs_setImmediate, // 2
48
- xs_gc, // 3
49
- xs_performance_now, // 4
50
- xs_currentMeterLimit, // 5
51
- xs_resetMeter, // 6
52
-
53
- xs_textdecoder, // 7
54
- xs_textdecoder_decode, // 8
55
- xs_textdecoder_get_encoding, // 9
56
- xs_textdecoder_get_ignoreBOM, // 10
57
- xs_textdecoder_get_fatal, // 11
58
-
59
- xs_textencoder, // 12
60
- xs_textencoder_encode, // 13
61
- xs_textencoder_encodeInto, // 14
62
-
63
- xs_base64_encode, // 15
64
- xs_base64_decode, // 16
65
-
66
- fx_harden, // 17
67
- // fx_setInterval,
68
- // fx_setTimeout,
69
- // fx_clearTimer,
70
- };
71
-
72
- static int xsSnapshopRead(void* stream, void* address, size_t size)
73
- {
74
- return (fread(address, size, 1, stream) == 1) ? 0 : errno;
75
- }
76
-
77
- static int xsSnapshopWrite(void* stream, void* address, size_t size)
78
- {
79
- return (fwrite(address, size, 1, stream) == 1) ? 0 : errno;
80
- }
81
-
82
- static xsUnsignedValue gxCurrentMeter = 0;
83
- static xsBooleanValue gxMeteringPrint = 0;
84
- static xsUnsignedValue gxMeteringLimit = 0;
85
- #ifdef mxMetering
86
- static xsBooleanValue xsMeteringCallback(xsMachine* the, xsUnsignedValue index)
87
- {
88
- if (index > gxMeteringLimit) {
89
- // fprintf(stderr, "too much computation\n");
90
- return 0;
91
- }
92
- // fprintf(stderr, "%d\n", index);
93
- return 1;
94
- }
95
- #endif
96
-
97
- int main(int argc, char* argv[])
98
- {
99
- int argi;
100
- int argd = 0;
101
- int argp = 0;
102
- int argr = 0;
103
- int argw = 0;
104
- int error = 0;
105
- int interval = 0;
106
- int option = 0;
107
- int parserBufferSize = 8192 * 1024;
108
- int profiling = 0;
109
- xsCreation _creation = {
110
- 32 * 1024 * 1024, /* initialChunkSize */
111
- 4 * 1024 * 1024, /* incrementalChunkSize */
112
- 256 * 1024, /* initialHeapCount */
113
- 128 * 1024, /* incrementalHeapCount */
114
- 4096, /* stackCount */
115
- 32000, /* initialKeyCount */
116
- 8000, /* incrementalKeyCount */
117
- 1993, /* nameModulo */
118
- 127, /* symbolModulo */
119
- parserBufferSize, /* parserBufferSize */
120
- 1993, /* parserTableModulo */
121
- };
122
- xsCreation* creation = &_creation;
123
- xsSnapshot snapshot = {
124
- SNAPSHOT_SIGNATURE,
125
- sizeof(SNAPSHOT_SIGNATURE) - 1,
126
- gxSnapshotCallbacks,
127
- mxSnapshotCallbackCount,
128
- xsSnapshopRead,
129
- xsSnapshopWrite,
130
- NULL,
131
- 0,
132
- NULL,
133
- NULL,
134
- NULL,
135
- 0,
136
- NULL,
137
- };
138
- xsMachine* machine;
139
- char path[C_PATH_MAX];
140
- char* dot;
141
-
142
- if (argc == 1) {
143
- xsPrintUsage();
144
- return 0;
145
- }
146
- for (argi = 1; argi < argc; argi++) {
147
- if (argv[argi][0] != '-')
148
- continue;
149
- if (!strcmp(argv[argi], "-d")) {
150
- argi++;
151
- if (argi < argc)
152
- argd = argi;
153
- else {
154
- xsPrintUsage();
155
- return 1;
156
- }
157
- option = 5;
158
- }
159
- else if (!strcmp(argv[argi], "-e"))
160
- option = 1;
161
- else if (!strcmp(argv[argi], "-h"))
162
- xsPrintUsage();
163
- else if (!strcmp(argv[argi], "-i")) {
164
- argi++;
165
- if (argi < argc)
166
- interval = atoi(argv[argi]);
167
- else {
168
- xsPrintUsage();
169
- return 1;
170
- }
171
- }
172
- else if (!strcmp(argv[argi], "-l")) {
173
- argi++;
174
- if (argi < argc)
175
- gxMeteringLimit = atoi(argv[argi]);
176
- else {
177
- xsPrintUsage();
178
- return 1;
179
- }
180
- }
181
- else if (!strcmp(argv[argi], "-m"))
182
- option = 2;
183
- else if (!strcmp(argv[argi], "-p")) {
184
- profiling = 1;
185
- argi++;
186
- if ((argi < argc) && (argv[argi][0] != '-'))
187
- argp = argi;
188
- else
189
- argi--;
190
- }
191
- else if (!strcmp(argv[argi], "-q"))
192
- gxMeteringPrint = 1;
193
- else if (!strcmp(argv[argi], "-r")) {
194
- argi++;
195
- if (argi < argc)
196
- argr = argi;
197
- else {
198
- xsPrintUsage();
199
- return 1;
200
- }
201
- }
202
- else if (!strcmp(argv[argi], "-s"))
203
- option = 3;
204
- else if (!strcmp(argv[argi], "-t"))
205
- option = 4;
206
- else if (!strcmp(argv[argi], "-v")) {
207
- xsVersion(path, sizeof(path));
208
- printf("XS %s\n", path);
209
- }
210
- else if (!strcmp(argv[argi], "-w")) {
211
- argi++;
212
- if (argi < argc)
213
- argw = argi;
214
- else {
215
- xsPrintUsage();
216
- return 1;
217
- }
218
- }
219
- else {
220
- xsPrintUsage();
221
- return 1;
222
- }
223
- }
224
- if (gxMeteringLimit) {
225
- if (interval == 0)
226
- interval = 1;
227
- }
228
- xsInitializeSharedCluster();
229
- if (argr) {
230
- snapshot.stream = fopen(argv[argr], "rb");
231
- if (snapshot.stream) {
232
- machine = xsReadSnapshot(&snapshot, "xsnap", NULL);
233
- fclose(snapshot.stream);
234
- }
235
- else
236
- snapshot.error = errno;
237
- if (snapshot.error) {
238
- fprintf(stderr, "cannot read snapshot %s: %s\n", argv[argr], strerror(snapshot.error));
239
- return 1;
240
- }
241
- }
242
- else {
243
- machine = xsCreateMachine(creation, "xsnap", NULL);
244
- xsBuildAgent(machine);
245
- }
246
- if (profiling)
247
- fxStartProfiling(machine);
248
- xsBeginMetering(machine, xsMeteringCallback, interval);
249
- {
250
- if (option == 5) {
251
- snapshot.stream = fopen(argv[argd], "rb");
252
- if (snapshot.stream) {
253
- fxDumpSnapshot(machine, &snapshot);
254
- fclose(snapshot.stream);
255
- }
256
- else
257
- snapshot.error = errno;
258
- if (snapshot.error) {
259
- fprintf(stderr, "cannot dump snapshot %s: %s\n", argv[argr], strerror(snapshot.error));
260
- return 1;
261
- }
262
- }
263
- else if (option == 4) {
264
- fprintf(stderr, "%p\n", machine);
265
- xsReplay(machine);
266
- }
267
- else {
268
- xsBeginHost(machine);
269
- {
270
- xsVars(1);
271
- for (argi = 1; argi < argc; argi++) {
272
- if (!strcmp(argv[argi], "-i")) {
273
- argi++;
274
- continue;
275
- }
276
- if (!strcmp(argv[argi], "-l")) {
277
- argi++;
278
- continue;
279
- }
280
- if (argv[argi][0] == '-')
281
- continue;
282
- if (argi == argp)
283
- continue;
284
- if (argi == argr)
285
- continue;
286
- if (argi == argw)
287
- continue;
288
- if (option == 1) {
289
- xsResult = xsString(argv[argi]);
290
- xsCall1(xsGlobal, xsID("eval"), xsResult);
291
- }
292
- else {
293
- if (!c_realpath(argv[argi], path))
294
- xsURIError("file not found: %s", argv[argi]);
295
- dot = strrchr(path, '.');
296
- if (((option == 0) && dot && !c_strcmp(dot, ".mjs")) || (option == 2))
297
- xsRunModuleFile(path);
298
- else
299
- xsRunProgramFile(path);
300
- }
301
- }
302
- }
303
- xsRunLoop(machine);
304
- xsEndHost(machine);
305
- if (argw) {
306
- snapshot.stream = fopen(argv[argw], "wb");
307
- if (snapshot.stream) {
308
- xsWriteSnapshot(machine, &snapshot);
309
- fclose(snapshot.stream);
310
- }
311
- else
312
- snapshot.error = errno;
313
- if (snapshot.error) {
314
- fprintf(stderr, "cannot write snapshot %s: %s\n", argv[argw], strerror(snapshot.error));
315
- }
316
- }
317
- }
318
- }
319
- xsEndMetering(machine);
320
- if (profiling) {
321
- if (argp) {
322
- FILE* stream = fopen(argv[argp], "w");
323
- if (stream)
324
- fxStopProfiling(machine, stream);
325
- else
326
- fprintf(stderr, "cannot write profile %s: %s\n", argv[argp], strerror(errno));
327
- }
328
- else
329
- fxStopProfiling(machine, C_NULL);
330
- }
331
- if (machine->abortStatus)
332
- error = machine->abortStatus;
333
- xsDeleteMachine(machine);
334
- xsTerminateSharedCluster();
335
- return error;
336
- }
337
-
338
- void xsBuildAgent(xsMachine* machine)
339
- {
340
- xsBeginHost(machine);
341
- xsVars(1);
342
-
343
- // xsResult = xsNewHostFunction(xs_clearTimer, 1);
344
- // xsDefine(xsGlobal, xsID("clearImmediate"), xsResult, xsDontEnum);
345
- xsResult = xsNewHostFunction(xs_setImmediate, 1);
346
- xsDefine(xsGlobal, xsID("setImmediate"), xsResult, xsDontEnum);
347
-
348
- // xsResult = xsNewHostFunction(xs_clearTimer, 1);
349
- // xsDefine(xsGlobal, xsID("clearInterval"), xsResult, xsDontEnum);
350
- // xsResult = xsNewHostFunction(xs_setInterval, 1);
351
- // xsDefine(xsGlobal, xsID("setInterval"), xsResult, xsDontEnum);
352
-
353
- // xsResult = xsNewHostFunction(xs_clearTimer, 1);
354
- // xsDefine(xsGlobal, xsID("clearTimeout"), xsResult, xsDontEnum);
355
- // xsResult = xsNewHostFunction(xs_setTimeout, 1);
356
- // xsDefine(xsGlobal, xsID("setTimeout"), xsResult, xsDontEnum);
357
-
358
- xsResult = xsNewHostFunction(xs_gc, 1);
359
- xsDefine(xsGlobal, xsID("gc"), xsResult, xsDontEnum);
360
- xsResult = xsNewHostFunction(xs_print, 1);
361
- xsDefine(xsGlobal, xsID("print"), xsResult, xsDontEnum);
362
-
363
- xsResult = xsNewHostFunction(xs_issueCommand, 1);
364
- xsDefine(xsGlobal, xsID("issueCommand"), xsResult, xsDontEnum);
365
-
366
- xsResult = xsNewObject();
367
- xsVar(0) = xsNewHostFunction(xs_performance_now, 0);
368
- xsDefine(xsResult, xsID("now"), xsVar(0), xsDontEnum);
369
- xsDefine(xsGlobal, xsID("performance"), xsResult, xsDontEnum);
370
-
371
- xsResult = xsNewHostFunction(xs_currentMeterLimit, 1);
372
- xsDefine(xsGlobal, xsID("currentMeterLimit"), xsResult, xsDontEnum);
373
- xsResult = xsNewHostFunction(xs_resetMeter, 1);
374
- xsDefine(xsGlobal, xsID("resetMeter"), xsResult, xsDontEnum);
375
-
376
- modInstallTextDecoder(the);
377
- modInstallTextEncoder(the);
378
- modInstallBase64(the);
379
- //
380
- xsResult = xsNewHostFunction(fx_harden, 1);
381
- xsDefine(xsGlobal, xsID("harden"), xsResult, xsDontEnum);
382
- // xsResult = xsNewHostFunction(xs_lockdown, 0);
383
- // xsDefine(xsGlobal, xsID("lockdown"), xsResult, xsDontEnum);
384
- // xsResult = xsNewHostFunction(fx_petrify, 1);
385
- // xsDefine(xsGlobal, xsID("petrify"), xsResult, xsDontEnum);
386
- // xsResult = xsNewHostFunction(fx_mutabilities, 1);
387
- // xsDefine(xsGlobal, xsID("mutabilities"), xsResult, xsDontEnum);
388
-
389
- xsEndHost(machine);
390
- }
391
-
392
- void xsPrintUsage()
393
- {
394
- printf("xsnap [-h] [-e] [i <interval] [l <limit] [-m] [-r <snapshot>] [-s] [-v] [-w <snapshot>] strings...\n");
395
- printf("\t-d <snapshot>: dump snapshot to stderr\n");
396
- printf("\t-e: eval strings\n");
397
- printf("\t-h: print this help message\n");
398
- printf("\t-i <interval>: metering interval (default to 1)\n");
399
- printf("\t-l <limit>: metering limit (default to none)\n");
400
- printf("\t-m: strings are paths to modules\n");
401
- printf("\t-r <snapshot>: read snapshot to create the XS machine\n");
402
- printf("\t-s: strings are paths to scripts\n");
403
- printf("\t-v: print XS version\n");
404
- printf("\t-w <snapshot>: write snapshot of the XS machine at exit\n");
405
- printf("without -e, -m, -s:\n");
406
- printf("\tif the extension is .mjs, strings are paths to modules\n");
407
- printf("\telse strings are paths to scripts\n");
408
- }
409
-
410
- static int gxStep = 0;
411
-
412
- void xsReplay(xsMachine* machine)
413
- {
414
- char path[C_PATH_MAX];
415
- char* names[6] = { "-evaluate.dat", "-issueCommand.dat", "-snapshot.dat", "-command.dat", "-reply.dat", "-options.json", };
416
- for (;;) {
417
- int which;
418
- for (which = 0; which < 6; which++) {
419
- sprintf(path, "%05d%s", gxStep, names[which]);
420
- {
421
- #if mxWindows
422
- DWORD attributes = GetFileAttributes(path);
423
- if ((attributes != 0xFFFFFFFF) && (!(attributes & FILE_ATTRIBUTE_DIRECTORY)))
424
- #else
425
- struct stat a_stat;
426
- if ((stat(path, &a_stat) == 0) && (S_ISREG(a_stat.st_mode)))
427
- #endif
428
- {
429
- gxStep++;
430
- fprintf(stderr, "### %s\n", path);
431
- FILE* file = fopen(path, "rb");
432
- if (file) {
433
- size_t length;
434
- fseek(file, 0, SEEK_END);
435
- length = ftell(file);
436
- fseek(file, 0, SEEK_SET);
437
- if (which == 0) {
438
- xsBeginHost(machine);
439
- xsStringValue string;
440
- xsResult = xsStringBuffer(NULL, (xsIntegerValue)length);
441
- string = xsToString(xsResult);
442
- length = fread(string, 1, length, file);
443
- string[length] = 0;
444
- fclose(file);
445
- xsCall1(xsGlobal, xsID("eval"), xsResult);
446
- fxRunLoop(machine);
447
- xsEndHost(machine);
448
- }
449
- else if (which == 1) {
450
- xsBeginHost(machine);
451
- xsResult = xsArrayBuffer(NULL, (xsIntegerValue)length);
452
- length = fread(xsToArrayBuffer(xsResult), 1, length, file);
453
- fclose(file);
454
- xsCall1(xsGlobal, xsID("handleCommand"), xsResult);
455
- fxRunLoop(machine);
456
- xsEndHost(machine);
457
- }
458
- else if (which == 2) {
459
- // xsBeginHost(machine);
460
- // xsCollectGarbage();
461
- // xsEndHost(machine);
462
- // fclose(file);
463
- char buffer[1024];
464
- char* slash;
465
- xsSnapshot snapshot = {
466
- SNAPSHOT_SIGNATURE,
467
- sizeof(SNAPSHOT_SIGNATURE) - 1,
468
- gxSnapshotCallbacks,
469
- mxSnapshotCallbackCount,
470
- xsSnapshopRead,
471
- xsSnapshopWrite,
472
- NULL,
473
- 0,
474
- NULL,
475
- NULL,
476
- NULL,
477
- 0,
478
- NULL
479
- };
480
- length = fread(buffer, 1, length, file);
481
- buffer[length] = 0;
482
- fclose(file);
483
- slash = c_strrchr(buffer, '/');
484
- if (slash) slash++;
485
- else slash = buffer;
486
- snapshot.stream = fopen(slash, "wb");
487
- if (snapshot.stream) {
488
- xsWriteSnapshot(machine, &snapshot);
489
- fclose(snapshot.stream);
490
- }
491
- }
492
- else
493
- fclose(file);
494
- }
495
- break;
496
- }
497
- }
498
- }
499
- if (which == 6)
500
- break;
501
- }
502
- }
503
-
504
- void xs_clearTimer(xsMachine* the)
505
- {
506
- xsClearTimer();
507
- }
508
-
509
- void xs_currentMeterLimit(xsMachine* the)
510
- {
511
- #if mxMetering
512
- xsResult = xsInteger(gxCurrentMeter);
513
- #endif
514
- }
515
-
516
- void xs_gc(xsMachine* the)
517
- {
518
- fprintf(stderr, "gc()\n");
519
- xsCollectGarbage();
520
- }
521
-
522
- void xs_issueCommand(xsMachine* the)
523
- {
524
- char path[C_PATH_MAX];
525
- FILE* file;
526
- size_t length;
527
- void* data;
528
- size_t argLength;
529
- void* argData;
530
-
531
- sprintf(path, "%05d-command.dat", gxStep);
532
- gxStep++;
533
-
534
- file = fopen(path, "rb");
535
- if (!file) xsUnknownError("cannot open %s", path);
536
- fseek(file, 0, SEEK_END);
537
- length = ftell(file);
538
- fseek(file, 0, SEEK_SET);
539
- data = c_malloc(length);
540
- length = fread(data, 1, length, file);
541
- fclose(file);
542
-
543
- argLength = xsGetArrayBufferLength(xsArg(0));
544
- argData = xsToArrayBuffer(xsArg(0));
545
-
546
- if ((length != argLength) || c_memcmp(data, argData, length)) {
547
- fprintf(stderr, "### %s %.*s\n", path, (int)argLength, (char*)argData);
548
- // fprintf(stderr, "@@@ %s %.*s\n", path, (int)length, (char*)data);
549
- }
550
- else
551
- fprintf(stderr, "### %s\n", path);
552
- c_free(data);
553
-
554
- sprintf(path, "%05d-reply.dat", gxStep);
555
- fprintf(stderr, "### %s\n", path);
556
- gxStep++;
557
- file = fopen(path, "rb");
558
- if (!file) xsUnknownError("cannot open %s", path);
559
- fseek(file, 0, SEEK_END);
560
- length = ftell(file);
561
- fseek(file, 0, SEEK_SET);
562
- xsResult = xsArrayBuffer(NULL, (xsIntegerValue)length);
563
- data = xsToArrayBuffer(xsResult);
564
- length = fread(data, 1, length, file);
565
- fclose(file);
566
- }
567
-
568
- void xs_lockdown(xsMachine *the)
569
- {
570
- fx_lockdown(the);
571
-
572
- xsResult = xsGet(xsGlobal, xsID("Base64"));
573
- xsCall1(xsGlobal, xsID("harden"), xsResult);
574
- xsResult = xsGet(xsGlobal, xsID("TextDecoder"));
575
- xsCall1(xsGlobal, xsID("harden"), xsResult);
576
- xsResult = xsGet(xsGlobal, xsID("TextEncoder"));
577
- xsCall1(xsGlobal, xsID("harden"), xsResult);
578
-
579
- // xsResult = xsGet(xsGlobal, xsID("clearImmediate"));
580
- // xsCall1(xsGlobal, xsID("harden"), xsResult);
581
- // xsResult = xsGet(xsGlobal, xsID("clearInterval"));
582
- // xsCall1(xsGlobal, xsID("harden"), xsResult);
583
- // xsResult = xsGet(xsGlobal, xsID("clearTimeout"));
584
- // xsCall1(xsGlobal, xsID("harden"), xsResult);
585
- xsResult = xsGet(xsGlobal, xsID("currentMeterLimit"));
586
- xsCall1(xsGlobal, xsID("harden"), xsResult);
587
- xsResult = xsGet(xsGlobal, xsID("gc"));
588
- xsCall1(xsGlobal, xsID("harden"), xsResult);
589
- xsResult = xsGet(xsGlobal, xsID("harden"));
590
- xsCall1(xsGlobal, xsID("harden"), xsResult);
591
- xsResult = xsGet(xsGlobal, xsID("issueCommand"));
592
- xsCall1(xsGlobal, xsID("harden"), xsResult);
593
- xsResult = xsGet(xsGlobal, xsID("lockdown"));
594
- xsCall1(xsGlobal, xsID("harden"), xsResult);
595
- xsResult = xsGet(xsGlobal, xsID("mutabilities"));
596
- xsCall1(xsGlobal, xsID("harden"), xsResult);
597
- xsResult = xsGet(xsGlobal, xsID("performance"));
598
- xsCall1(xsGlobal, xsID("harden"), xsResult);
599
- xsResult = xsGet(xsGlobal, xsID("petrify"));
600
- xsCall1(xsGlobal, xsID("harden"), xsResult);
601
- xsResult = xsGet(xsGlobal, xsID("print"));
602
- xsCall1(xsGlobal, xsID("harden"), xsResult);
603
- xsResult = xsGet(xsGlobal, xsID("resetMeter"));
604
- xsCall1(xsGlobal, xsID("harden"), xsResult);
605
- xsResult = xsGet(xsGlobal, xsID("setImmediate"));
606
- xsCall1(xsGlobal, xsID("harden"), xsResult);
607
- // xsResult = xsGet(xsGlobal, xsID("setInterval"));
608
- // xsCall1(xsGlobal, xsID("harden"), xsResult);
609
- // xsResult = xsGet(xsGlobal, xsID("setTimeout"));
610
- // xsCall1(xsGlobal, xsID("harden"), xsResult);
611
- }
612
-
613
- void xs_performance_now(xsMachine *the)
614
- {
615
- c_timeval tv;
616
- c_gettimeofday(&tv, NULL);
617
- xsResult = xsNumber((double)(tv.tv_sec * 1000.0) + ((double)(tv.tv_usec) / 1000.0));
618
- }
619
-
620
- void xs_print(xsMachine* the)
621
- {
622
- xsIntegerValue c = xsToInteger(xsArgc), i;
623
- xsStringValue string, p, q;
624
- xsVars(1);
625
- xsVar(0) = xsGet(xsGlobal, xsID("String"));
626
- for (i = 0; i < c; i++) {
627
- xsArg(i) = xsCallFunction1(xsVar(0), xsUndefined, xsArg(i));
628
- }
629
- #ifdef mxMetering
630
- if (gxMeteringPrint)
631
- fprintf(stdout, "[%u] ", xsGetCurrentMeter(the));
632
- #endif
633
- for (i = 0; i < c; i++) {
634
- if (i)
635
- fprintf(stdout, " ");
636
- xsArg(i) = xsCallFunction1(xsVar(0), xsUndefined, xsArg(i));
637
- p = string = xsToString(xsArg(i));
638
- #if mxCESU8
639
- for (;;) {
640
- xsIntegerValue character;
641
- q = fxUTF8Decode(p, &character);
642
- again:
643
- if (character == C_EOF)
644
- break;
645
- if (character == 0) {
646
- if (p > string) {
647
- char c = *p;
648
- *p = 0;
649
- fprintf(stdout, "%s", string);
650
- *p = c;
651
- }
652
- string = q;
653
- }
654
- else if ((0x0000D800 <= character) && (character <= 0x0000DBFF)) {
655
- xsStringValue r = q;
656
- xsIntegerValue surrogate;
657
- q = fxUTF8Decode(r, &surrogate);
658
- if ((0x0000DC00 <= surrogate) && (surrogate <= 0x0000DFFF)) {
659
- char buffer[5];
660
- character = (xsIntegerValue)(0x00010000 + ((character & 0x03FF) << 10) + (surrogate & 0x03FF));
661
- if (p > string) {
662
- char c = *p;
663
- *p = 0;
664
- fprintf(stdout, "%s", string);
665
- *p = c;
666
- }
667
- p = fxUTF8Encode(buffer, character);
668
- *p = 0;
669
- fprintf(stdout, "%s", buffer);
670
- string = q;
671
- }
672
- else {
673
- p = r;
674
- character = surrogate;
675
- goto again;
676
- }
677
- }
678
- p = q;
679
- }
680
- #endif
681
- fprintf(stdout, "%s", string);
682
- }
683
- fprintf(stdout, "\n");
684
- }
685
-
686
- void xs_resetMeter(xsMachine* the)
687
- {
688
- #if mxMetering
689
- xsIntegerValue argc = xsToInteger(xsArgc);
690
- if (argc < 2) {
691
- xsTypeError("expected newMeterLimit, newMeterIndex");
692
- }
693
- xsResult = xsInteger(xsGetCurrentMeter(the));
694
- gxCurrentMeter = xsToInteger(xsArg(0));
695
- xsSetCurrentMeter(the, xsToInteger(xsArg(1)));
696
- #endif
697
- }
698
-
699
- void xs_setImmediate(xsMachine* the)
700
- {
701
- xsSetTimer(0, 0);
702
- }
703
-
704
- void xs_setInterval(xsMachine* the)
705
- {
706
- xsSetTimer(xsToNumber(xsArg(1)), 1);
707
- }
708
-
709
- void xs_setTimeout(xsMachine* the)
710
- {
711
- xsSetTimer(xsToNumber(xsArg(1)), 0);
712
- }
713
-
714
-
715
-
716
-