@agoric/xsnap 0.14.3-u14.0 → 0.14.3-u16.1

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 (137) hide show
  1. package/README.md +3 -3
  2. package/api.js +4 -2
  3. package/build.env +1 -1
  4. package/moddable/modules/data/base64/base64.js +28 -0
  5. package/moddable/modules/data/base64/manifest.json +11 -0
  6. package/moddable/modules/data/base64/modBase64.c +188 -0
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +106 -0
  8. package/moddable/modules/data/crc/crc.c +205 -0
  9. package/moddable/modules/data/crc/crc.js +36 -0
  10. package/moddable/modules/data/crc/manifest.json +8 -0
  11. package/moddable/modules/data/hex/hex.js +28 -0
  12. package/moddable/modules/data/hex/manifest.json +11 -0
  13. package/moddable/modules/data/hex/modHex.c +139 -0
  14. package/moddable/modules/data/logical/logical.js +32 -0
  15. package/moddable/modules/data/logical/modLogical.c +98 -0
  16. package/moddable/modules/data/qrcode/manifest.json +9 -0
  17. package/moddable/modules/data/qrcode/qrcode.c +93 -0
  18. package/moddable/modules/data/qrcode/qrcode.js +23 -0
  19. package/moddable/modules/data/qrcode/qrcodegen.c +1025 -0
  20. package/moddable/modules/data/qrcode/qrcodegen.h +267 -0
  21. package/moddable/modules/data/text/decoder/manifest.json +8 -0
  22. package/moddable/modules/data/text/decoder/textdecoder.c +480 -0
  23. package/moddable/modules/data/text/decoder/textdecoder.js +27 -0
  24. package/moddable/modules/data/text/encoder/manifest.json +8 -0
  25. package/moddable/modules/data/text/encoder/textencoder.c +232 -0
  26. package/moddable/modules/data/text/encoder/textencoder.js +24 -0
  27. package/moddable/modules/data/tinyint/tinyint.c +150 -0
  28. package/moddable/modules/data/tinyint/tinyint.js +53 -0
  29. package/moddable/modules/data/url/manifest.json +17 -0
  30. package/moddable/modules/data/url/url.c +1959 -0
  31. package/moddable/modules/data/url/url.js +210 -0
  32. package/moddable/modules/data/wavreader/manifest.json +8 -0
  33. package/moddable/modules/data/wavreader/wavreader.js +128 -0
  34. package/moddable/modules/data/zlib/deflate.c +161 -0
  35. package/moddable/modules/data/zlib/deflate.js +63 -0
  36. package/moddable/modules/data/zlib/inflate.c +145 -0
  37. package/moddable/modules/data/zlib/inflate.js +66 -0
  38. package/moddable/modules/data/zlib/manifest_deflate.json +9 -0
  39. package/moddable/modules/data/zlib/manifest_inflate.json +9 -0
  40. package/moddable/modules/data/zlib/miniz.c +4924 -0
  41. package/moddable/xs/includes/xs.d.ts +73 -0
  42. package/moddable/xs/includes/xs.h +1533 -0
  43. package/moddable/xs/includes/xsmc.h +206 -0
  44. package/moddable/xs/makefiles/lin/makefile +33 -0
  45. package/moddable/xs/makefiles/lin/xsc.mk +118 -0
  46. package/moddable/xs/makefiles/lin/xsid.mk +90 -0
  47. package/moddable/xs/makefiles/lin/xsl.mk +168 -0
  48. package/moddable/xs/makefiles/lin/xst.mk +201 -0
  49. package/moddable/xs/makefiles/mac/makefile +33 -0
  50. package/moddable/xs/makefiles/mac/xsc.mk +130 -0
  51. package/moddable/xs/makefiles/mac/xsid.mk +102 -0
  52. package/moddable/xs/makefiles/mac/xsl.mk +177 -0
  53. package/moddable/xs/makefiles/mac/xst.mk +203 -0
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +52 -0
  55. package/moddable/xs/makefiles/win/build.bat +26 -0
  56. package/moddable/xs/makefiles/win/xsc.mak +142 -0
  57. package/moddable/xs/makefiles/win/xsid.mak +113 -0
  58. package/moddable/xs/makefiles/win/xsl.mak +186 -0
  59. package/moddable/xs/makefiles/win/xst.mak +195 -0
  60. package/moddable/xs/platforms/lin_xs.h +99 -0
  61. package/moddable/xs/platforms/mac_xs.h +97 -0
  62. package/moddable/xs/platforms/wasm_xs.h +79 -0
  63. package/moddable/xs/platforms/win_xs.h +104 -0
  64. package/moddable/xs/platforms/xsHost.h +63 -0
  65. package/moddable/xs/platforms/xsPlatform.h +618 -0
  66. package/moddable/xs/sources/xsAPI.c +2555 -0
  67. package/moddable/xs/sources/xsAll.c +294 -0
  68. package/moddable/xs/sources/xsAll.h +2741 -0
  69. package/moddable/xs/sources/xsArguments.c +222 -0
  70. package/moddable/xs/sources/xsArray.c +2657 -0
  71. package/moddable/xs/sources/xsAtomics.c +844 -0
  72. package/moddable/xs/sources/xsBigInt.c +1859 -0
  73. package/moddable/xs/sources/xsBoolean.c +109 -0
  74. package/moddable/xs/sources/xsCode.c +4493 -0
  75. package/moddable/xs/sources/xsCommon.c +1710 -0
  76. package/moddable/xs/sources/xsCommon.h +1142 -0
  77. package/moddable/xs/sources/xsDataView.c +2890 -0
  78. package/moddable/xs/sources/xsDate.c +1541 -0
  79. package/moddable/xs/sources/xsDebug.c +2710 -0
  80. package/moddable/xs/sources/xsDefaults.c +134 -0
  81. package/moddable/xs/sources/xsError.c +353 -0
  82. package/moddable/xs/sources/xsFunction.c +776 -0
  83. package/moddable/xs/sources/xsGenerator.c +865 -0
  84. package/moddable/xs/sources/xsGlobal.c +839 -0
  85. package/moddable/xs/sources/xsJSON.c +1091 -0
  86. package/moddable/xs/sources/xsLexical.c +1969 -0
  87. package/moddable/xs/sources/xsLockdown.c +933 -0
  88. package/moddable/xs/sources/xsMapSet.c +1649 -0
  89. package/moddable/xs/sources/xsMarshall.c +1020 -0
  90. package/moddable/xs/sources/xsMath.c +624 -0
  91. package/moddable/xs/sources/xsMemory.c +1941 -0
  92. package/moddable/xs/sources/xsModule.c +3101 -0
  93. package/moddable/xs/sources/xsNumber.c +560 -0
  94. package/moddable/xs/sources/xsObject.c +1102 -0
  95. package/moddable/xs/sources/xsPlatforms.c +480 -0
  96. package/moddable/xs/sources/xsProfile.c +577 -0
  97. package/moddable/xs/sources/xsPromise.c +1199 -0
  98. package/moddable/xs/sources/xsProperty.c +636 -0
  99. package/moddable/xs/sources/xsProxy.c +1014 -0
  100. package/moddable/xs/sources/xsRegExp.c +1168 -0
  101. package/moddable/xs/sources/xsRun.c +4889 -0
  102. package/moddable/xs/sources/xsScope.c +1293 -0
  103. package/moddable/xs/sources/xsScript.c +288 -0
  104. package/moddable/xs/sources/xsScript.h +1186 -0
  105. package/moddable/xs/sources/xsSnapshot.c +2161 -0
  106. package/moddable/xs/sources/xsSnapshot.h +51 -0
  107. package/moddable/xs/sources/xsSourceMap.c +218 -0
  108. package/moddable/xs/sources/xsString.c +3332 -0
  109. package/moddable/xs/sources/xsSymbol.c +503 -0
  110. package/moddable/xs/sources/xsSyntaxical.c +4193 -0
  111. package/moddable/xs/sources/xsTree.c +1893 -0
  112. package/moddable/xs/sources/xsType.c +1488 -0
  113. package/moddable/xs/sources/xsdtoa.c +6672 -0
  114. package/moddable/xs/sources/xsmc.c +340 -0
  115. package/moddable/xs/sources/xsre.c +7578 -0
  116. package/package.json +37 -20
  117. package/scripts/get_xsnap_version.sh +14 -0
  118. package/scripts/test-package.sh +21 -0
  119. package/src/avaAssertXS.js +6 -2
  120. package/src/avaHandler.cjs +2 -5
  121. package/src/avaXS.js +7 -8
  122. package/src/build.js +161 -28
  123. package/src/replay.js +0 -3
  124. package/src/xsnap.js +105 -91
  125. package/src/xsrepl.js +2 -3
  126. package/xsnap-native/xsnap/makefiles/lin/makefile +10 -0
  127. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +156 -0
  128. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +144 -0
  129. package/xsnap-native/xsnap/makefiles/mac/makefile +10 -0
  130. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +165 -0
  131. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +153 -0
  132. package/xsnap-native/xsnap/sources/xsnap-worker.c +1008 -0
  133. package/xsnap-native/xsnap/sources/xsnap.c +717 -0
  134. package/xsnap-native/xsnap/sources/xsnap.h +142 -0
  135. package/xsnap-native/xsnap/sources/xsnapPlatform.c +1501 -0
  136. package/xsnap-native/xsnap/sources/xsnapPlatform.h +105 -0
  137. package/CHANGELOG.md +0 -654
@@ -0,0 +1,717 @@
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
+ xsCollectGarbage();
519
+ }
520
+
521
+ void xs_issueCommand(xsMachine* the)
522
+ {
523
+ char path[C_PATH_MAX];
524
+ FILE* file;
525
+ size_t length;
526
+ void* data;
527
+ size_t argLength;
528
+ void* argData;
529
+
530
+ sprintf(path, "%05d-command.dat", gxStep);
531
+ gxStep++;
532
+
533
+ file = fopen(path, "rb");
534
+ if (!file) xsUnknownError("cannot open %s", path);
535
+ fseek(file, 0, SEEK_END);
536
+ length = ftell(file);
537
+ fseek(file, 0, SEEK_SET);
538
+ data = c_malloc(length);
539
+ length = fread(data, 1, length, file);
540
+ fclose(file);
541
+
542
+ argLength = xsGetArrayBufferLength(xsArg(0));
543
+ argData = xsToArrayBuffer(xsArg(0));
544
+
545
+ if ((length != argLength) || c_memcmp(data, argData, length)) {
546
+ fprintf(stderr, "### %s %.*s\n", path, (int)argLength, (char*)argData);
547
+ // fprintf(stderr, "@@@ %s %.*s\n", path, (int)length, (char*)data);
548
+ }
549
+ else
550
+ fprintf(stderr, "### %s\n", path);
551
+ c_free(data);
552
+
553
+ sprintf(path, "%05d-reply.dat", gxStep);
554
+ fprintf(stderr, "### %s\n", path);
555
+ gxStep++;
556
+ file = fopen(path, "rb");
557
+ if (!file) xsUnknownError("cannot open %s", path);
558
+ fseek(file, 0, SEEK_END);
559
+ length = ftell(file);
560
+ fseek(file, 0, SEEK_SET);
561
+ xsResult = xsArrayBuffer(NULL, (xsIntegerValue)length);
562
+ data = xsToArrayBuffer(xsResult);
563
+ length = fread(data, 1, length, file);
564
+ fclose(file);
565
+ }
566
+
567
+ #if 0
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
+ #endif
613
+
614
+ void xs_performance_now(xsMachine *the)
615
+ {
616
+ c_timeval tv;
617
+ c_gettimeofday(&tv, NULL);
618
+ xsResult = xsNumber((double)(tv.tv_sec * 1000.0) + ((double)(tv.tv_usec) / 1000.0));
619
+ }
620
+
621
+ void xs_print(xsMachine* the)
622
+ {
623
+ xsIntegerValue c = xsToInteger(xsArgc), i;
624
+ xsStringValue string, p, q;
625
+ xsVars(1);
626
+ xsVar(0) = xsGet(xsGlobal, xsID("String"));
627
+ for (i = 0; i < c; i++) {
628
+ xsArg(i) = xsCallFunction1(xsVar(0), xsUndefined, xsArg(i));
629
+ }
630
+ #ifdef mxMetering
631
+ if (gxMeteringPrint)
632
+ fprintf(stdout, "[%u] ", xsGetCurrentMeter(the));
633
+ #endif
634
+ for (i = 0; i < c; i++) {
635
+ if (i)
636
+ fprintf(stdout, " ");
637
+ xsArg(i) = xsCallFunction1(xsVar(0), xsUndefined, xsArg(i));
638
+ p = string = xsToString(xsArg(i));
639
+ #if mxCESU8
640
+ for (;;) {
641
+ xsIntegerValue character;
642
+ q = fxUTF8Decode(p, &character);
643
+ again:
644
+ if (character == C_EOF)
645
+ break;
646
+ if (character == 0) {
647
+ if (p > string) {
648
+ char c = *p;
649
+ *p = 0;
650
+ fprintf(stdout, "%s", string);
651
+ *p = c;
652
+ }
653
+ string = q;
654
+ }
655
+ else if ((0x0000D800 <= character) && (character <= 0x0000DBFF)) {
656
+ xsStringValue r = q;
657
+ xsIntegerValue surrogate;
658
+ q = fxUTF8Decode(r, &surrogate);
659
+ if ((0x0000DC00 <= surrogate) && (surrogate <= 0x0000DFFF)) {
660
+ char buffer[5];
661
+ character = (xsIntegerValue)(0x00010000 + ((character & 0x03FF) << 10) + (surrogate & 0x03FF));
662
+ if (p > string) {
663
+ char c = *p;
664
+ *p = 0;
665
+ fprintf(stdout, "%s", string);
666
+ *p = c;
667
+ }
668
+ p = fxUTF8Encode(buffer, character);
669
+ *p = 0;
670
+ fprintf(stdout, "%s", buffer);
671
+ string = q;
672
+ }
673
+ else {
674
+ p = r;
675
+ character = surrogate;
676
+ goto again;
677
+ }
678
+ }
679
+ p = q;
680
+ }
681
+ #endif
682
+ fprintf(stdout, "%s", string);
683
+ }
684
+ fprintf(stdout, "\n");
685
+ }
686
+
687
+ void xs_resetMeter(xsMachine* the)
688
+ {
689
+ #if mxMetering
690
+ xsIntegerValue argc = xsToInteger(xsArgc);
691
+ if (argc < 2) {
692
+ xsTypeError("expected newMeterLimit, newMeterIndex");
693
+ }
694
+ xsResult = xsInteger(xsGetCurrentMeter(the));
695
+ gxCurrentMeter = xsToInteger(xsArg(0));
696
+ xsSetCurrentMeter(the, xsToInteger(xsArg(1)));
697
+ #endif
698
+ }
699
+
700
+ void xs_setImmediate(xsMachine* the)
701
+ {
702
+ xsSetTimer(0, 0);
703
+ }
704
+
705
+ void xs_setInterval(xsMachine* the)
706
+ {
707
+ xsSetTimer(xsToNumber(xsArg(1)), 1);
708
+ }
709
+
710
+ void xs_setTimeout(xsMachine* the)
711
+ {
712
+ xsSetTimer(xsToNumber(xsArg(1)), 0);
713
+ }
714
+
715
+
716
+
717
+