@agoric/xsnap 0.14.3-u14.0 → 0.14.3-u16.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 (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,577 @@
1
+ /*
2
+ * Copyright (c) 2016-2017 Moddable Tech, Inc.
3
+ *
4
+ * This file is part of the Moddable SDK Runtime.
5
+ *
6
+ * The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ * This file incorporates work covered by the following copyright and
20
+ * permission notice:
21
+ *
22
+ * Copyright (C) 2010-2016 Marvell International Ltd.
23
+ * Copyright (C) 2002-2010 Kinoma, Inc.
24
+ *
25
+ * Licensed under the Apache License, Version 2.0 (the "License");
26
+ * you may not use this file except in compliance with the License.
27
+ * You may obtain a copy of the License at
28
+ *
29
+ * http://www.apache.org/licenses/LICENSE-2.0
30
+ *
31
+ * Unless required by applicable law or agreed to in writing, software
32
+ * distributed under the License is distributed on an "AS IS" BASIS,
33
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
+ * See the License for the specific language governing permissions and
35
+ * limitations under the License.
36
+ */
37
+
38
+
39
+ #define _GNU_SOURCE
40
+ #include "xsAll.h"
41
+ #if mxMacOSX || mxLinux
42
+ #include <dlfcn.h>
43
+ #endif
44
+
45
+ #ifdef mxProfile
46
+
47
+ typedef struct sxProfiler txProfiler;
48
+ typedef struct sxProfilerRecord txProfilerRecord;
49
+ typedef struct sxProfilerSample txProfilerSample;
50
+
51
+ struct sxProfiler {
52
+ txU8 when;
53
+ txU8 former;
54
+ txU8 start;
55
+ txU8 stop;
56
+ txU4 interval;
57
+ txU4 recordCount;
58
+ txProfilerRecord** records;
59
+ txU8 sampleCount;
60
+ txU8 sampleIndex;
61
+ txProfilerSample* samples;
62
+ txProfilerRecord* host;
63
+ txProfilerRecord* gc;
64
+ };
65
+
66
+ struct sxProfilerRecord {
67
+ txID recordID;
68
+ txID constructorID;
69
+ txID prototypeID;
70
+ txID functionID;
71
+ txCallback functionAddress;
72
+ txID file;
73
+ txInteger line;
74
+ txInteger hitCount;
75
+ txInteger calleeCount;
76
+ txID* callees;
77
+ txInteger flags;
78
+ };
79
+
80
+ struct sxProfilerSample {
81
+ txID recordID;
82
+ txU4 delta;
83
+ };
84
+
85
+ static txProfilerRecord* fxFrameToProfilerRecord(txMachine* the, txSlot* frame);
86
+ static txU8 fxGetTicks();
87
+ static void fxInsertProfilerCallee(txMachine* the, txProfilerRecord* record, txID recordID);
88
+ static txU8 fxMicrosecondsToTicks(txU8 microseconds);
89
+ static void fxPrintID(txMachine* the, FILE* file, txID id);
90
+ static void fxPrintProfiler(txMachine* the, void* stream);
91
+ static void fxPrintString(txMachine* the, FILE* file, txString theString);
92
+ static void fxPushProfilerSample(txMachine* the, txID recordID, txU4 delta);
93
+ static txProfilerRecord* fxNewProfilerRecord(txMachine* the, txID recordID);
94
+ static txID fxRemoveProfilerCycle(txMachine* the, txProfiler* profiler, txID recordID);
95
+ static txU8 fxTicksToMicroseconds(txU8 ticks);
96
+
97
+ void fxCheckProfiler(txMachine* the, txSlot* frame)
98
+ {
99
+ txProfiler* profiler = the->profiler;
100
+ if (!profiler)
101
+ return;
102
+ txU8 when = profiler->when;
103
+ txU8 time = fxGetTicks();
104
+ if (when < time) {
105
+ txProfilerRecord* callee = C_NULL;
106
+ txProfilerRecord* record = C_NULL;
107
+ if (frame) {
108
+ record = fxFrameToProfilerRecord(the, frame);
109
+ frame = frame->next;
110
+ }
111
+ else {
112
+ frame = the->frame;
113
+ if (frame)
114
+ record = profiler->gc;
115
+ }
116
+ if (record) {
117
+ callee = record;
118
+ while (frame) {
119
+ txProfilerRecord* parent = fxFrameToProfilerRecord(the, frame);
120
+ if (parent) {
121
+ fxInsertProfilerCallee(the, parent, callee->recordID);
122
+ callee = parent;
123
+ }
124
+ frame = frame->next;
125
+ }
126
+ }
127
+ if (callee)
128
+ fxInsertProfilerCallee(the, profiler->host, callee->recordID);
129
+ else
130
+ record = profiler->host;
131
+ txU4 interval = profiler->interval;
132
+ record->hitCount++;
133
+ fxPushProfilerSample(the, record->recordID, (txU4)(time - profiler->former));
134
+ profiler->former = time;
135
+ profiler->when = time + interval - (time % interval);
136
+ }
137
+ }
138
+
139
+ void fxCreateProfiler(txMachine* the)
140
+ {
141
+ txProfiler* profiler = the->profiler = c_malloc(sizeof(txProfiler));
142
+ if (profiler == C_NULL)
143
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
144
+ profiler->interval = (txU4)fxMicrosecondsToTicks(1250);
145
+ profiler->former = fxGetTicks();
146
+ profiler->when = profiler->former + profiler->interval;
147
+ profiler->start = profiler->former;
148
+
149
+ profiler->recordCount = 2;
150
+ profiler->records = (txProfilerRecord**)c_calloc(sizeof(txProfilerRecord*), profiler->recordCount);
151
+ if (profiler->records == C_NULL)
152
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
153
+
154
+ profiler->sampleCount = 1024;
155
+ profiler->sampleIndex = 0;
156
+ profiler->samples = (txProfilerSample*)c_malloc((size_t)(profiler->sampleCount * sizeof(txProfilerSample)));
157
+ if (profiler->samples == C_NULL)
158
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
159
+ profiler->host = fxNewProfilerRecord(the, 0);
160
+ profiler->gc = fxNewProfilerRecord(the, 1);
161
+ }
162
+
163
+ void fxDeleteProfiler(txMachine* the, void* stream)
164
+ {
165
+ txProfiler* profiler = the->profiler;
166
+ profiler->stop = profiler->when;
167
+ fxPrintProfiler(the, stream);
168
+ c_free(profiler->samples);
169
+ txU4 recordIndex = 0;
170
+ while (recordIndex < profiler->recordCount) {
171
+ txProfilerRecord* record = profiler->records[recordIndex];
172
+ if (record) {
173
+ if (record->callees)
174
+ c_free(record->callees);
175
+ c_free(record);
176
+ }
177
+ recordIndex++;
178
+ }
179
+ c_free(profiler->records);
180
+ c_free(profiler);
181
+ the->profiler = C_NULL;
182
+ }
183
+
184
+ txProfilerRecord* fxFrameToProfilerRecord(txMachine* the, txSlot* frame)
185
+ {
186
+ txProfiler* profiler = the->profiler;
187
+ txSlot* function = frame + 3;
188
+ if (function->kind == XS_REFERENCE_KIND) {
189
+ function = function->value.reference;
190
+ if (mxIsFunction(function)) {
191
+ txSlot* code = mxFunctionInstanceCode(function);
192
+ txSlot* home = mxFunctionInstanceHome(function);
193
+ txID recordID = home->ID;
194
+ txProfilerRecord* record = C_NULL;
195
+ if (recordID < profiler->recordCount)
196
+ record = profiler->records[recordID];
197
+ if (record)
198
+ return record;
199
+ record = fxNewProfilerRecord(the, recordID);
200
+ record->functionID = code->ID;
201
+ home = home->value.home.object;
202
+ if (home) {
203
+ if (mxIsFunction(home)) {
204
+ record->constructorID = mxFunctionInstanceCode(home)->ID;
205
+ }
206
+ else {
207
+ txSlot* property = home->next;
208
+ while (property && (property->flag & XS_INTERNAL_FLAG))
209
+ property = property->next;
210
+ while (property) {
211
+ if (property->ID == mxID(_constructor))
212
+ break;
213
+ property = property->next;
214
+ }
215
+ if (property) {
216
+ if (property->kind == XS_REFERENCE_KIND) {
217
+ property = property->value.reference;
218
+ if (mxIsFunction(property)) {
219
+ record->constructorID = mxFunctionInstanceCode(property)->ID;
220
+ record->prototypeID = mxID(_prototype);
221
+ }
222
+ }
223
+ }
224
+ else {
225
+ property = home->next;
226
+ while (property) {
227
+ if (property->ID == mxID(_Symbol_toStringTag))
228
+ break;
229
+ property = property->next;
230
+ }
231
+ if (property) {
232
+ if ((property->kind == XS_STRING_KIND) || (property->kind == XS_STRING_X_KIND)) {
233
+ record->prototypeID = fxFindName(the, property->value.string);
234
+ }
235
+ }
236
+ }
237
+ }
238
+ }
239
+ if ((code->kind == XS_CODE_KIND) || (code->kind == XS_CODE_X_KIND)) {
240
+ txByte* p = code->value.code.address + 2;
241
+ if (*p == XS_CODE_FILE) {
242
+ txID file;
243
+ txS2 line;
244
+ p++;
245
+ mxDecodeID(p, file);
246
+ p++;
247
+ mxDecode2(p, line);
248
+ record->file = file;
249
+ record->line = line;
250
+ }
251
+ record->functionAddress = C_NULL;
252
+ }
253
+ else
254
+ record->functionAddress = code->value.callback.address;
255
+ return record;
256
+ }
257
+ }
258
+ return C_NULL;
259
+ }
260
+
261
+ txU8 fxGetTicks()
262
+ {
263
+ #if mxMacOSX
264
+ return clock_gettime_nsec_np(CLOCK_MONOTONIC);
265
+ #elif mxWindows
266
+ LARGE_INTEGER counter;
267
+ QueryPerformanceCounter(&counter);
268
+ return counter.QuadPart;
269
+ #else
270
+ c_timeval tv;
271
+ c_gettimeofday(&tv, NULL);
272
+ return (tv.tv_sec * 1000000ULL) + tv.tv_usec;
273
+ #endif
274
+ }
275
+
276
+ void fxInsertProfilerCallee(txMachine* the, txProfilerRecord* record, txID recordID)
277
+ {
278
+ txInteger count = record->calleeCount;
279
+ if (count == 0) {
280
+ record->callees = c_malloc(sizeof(txID));
281
+ if (record->callees == C_NULL)
282
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
283
+ record->calleeCount = 1;
284
+ record->callees[0] = recordID;
285
+ return;
286
+ }
287
+ txInteger min = 0;
288
+ txInteger max = count;
289
+ txInteger mid;
290
+ txID* callee = record->callees;
291
+ while (min < max) {
292
+ mid = (min + max) >> 1;
293
+ callee = record->callees + mid;
294
+ if (recordID < *callee)
295
+ max = mid;
296
+ else if (recordID > *callee)
297
+ min = mid + 1;
298
+ else
299
+ return;
300
+ }
301
+ if (recordID > *callee)
302
+ mid++;
303
+ record->calleeCount++;
304
+ record->callees = c_realloc(record->callees, record->calleeCount * sizeof(txID));
305
+ if (record->callees == C_NULL)
306
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
307
+ callee = record->callees + mid;
308
+ if (mid < count)
309
+ c_memmove(callee + 1, callee, (count - mid) * sizeof(txID));
310
+ *callee = recordID;
311
+ }
312
+
313
+ txU8 fxMicrosecondsToTicks(txU8 microseconds)
314
+ {
315
+ #if mxMacOSX
316
+ return microseconds * 1000;
317
+ #elif mxWindows
318
+ LARGE_INTEGER frequency;
319
+ QueryPerformanceFrequency(&frequency);
320
+ return (frequency.QuadPart * microseconds) / 1000000ULL;
321
+ #else
322
+ return microseconds;
323
+ #endif
324
+ }
325
+
326
+ txProfilerRecord* fxNewProfilerRecord(txMachine* the, txID recordID)
327
+ {
328
+ txProfiler* profiler = the->profiler;
329
+ txU4 recordCount = profiler->recordCount;
330
+ txProfilerRecord* record;
331
+ if (recordID >= recordCount) {
332
+ recordCount = recordID + 1;
333
+ profiler->records = (txProfilerRecord**)c_realloc(profiler->records, recordCount * sizeof(txProfilerRecord*));
334
+ if (profiler->records == C_NULL)
335
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
336
+ c_memset(profiler->records + profiler->recordCount, 0, (recordCount - profiler->recordCount) * sizeof(txProfilerRecord*));
337
+ profiler->recordCount = recordCount;
338
+ }
339
+ record = c_malloc(sizeof(txProfilerRecord));
340
+ if (record == C_NULL)
341
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
342
+ record->recordID = recordID;
343
+ record->hitCount = 0;
344
+ record->constructorID = XS_NO_ID;
345
+ record->prototypeID = XS_NO_ID;
346
+ record->functionID = XS_NO_ID;
347
+ record->file = XS_NO_ID;
348
+ record->line = 0;
349
+ record->calleeCount = 0;
350
+ record->callees = C_NULL;
351
+ record->flags = 0;
352
+ profiler->records[recordID] = record;
353
+ return record;
354
+ }
355
+
356
+ void fxPrintID(txMachine* the, FILE* file, txID id)
357
+ {
358
+ txSlot* key = fxGetKey(the, id);
359
+ if (key) {
360
+ if ((key->kind == XS_KEY_KIND) || (key->kind == XS_KEY_X_KIND)) {
361
+ fxPrintString(the, file, key->value.key.string);
362
+ return;
363
+ }
364
+ if ((key->kind == XS_STRING_KIND) || (key->kind == XS_STRING_X_KIND)) {
365
+ fprintf(file, "[");
366
+ fxPrintString(the, file, key->value.string);
367
+ fprintf(file, "]");
368
+ return;
369
+ }
370
+ }
371
+ fprintf(file, "?");
372
+ }
373
+
374
+ void fxPrintProfiler(txMachine* the, void* stream)
375
+ {
376
+ // https://chromedevtools.github.io/devtools-protocol/tot/Profiler/#type-Profile
377
+ txProfiler* profiler = the->profiler;
378
+ FILE* file = stream;
379
+ char buffer[22];
380
+ char name[36];
381
+
382
+ fxRemoveProfilerCycle(the, profiler, 0);
383
+
384
+ if (!file) {
385
+ time_t timer;
386
+ struct tm* tm_info;
387
+ timer = time(NULL);
388
+ tm_info = localtime(&timer);
389
+ strftime(buffer, 22, "XS-%y-%m-%d-%H-%M-%S-", tm_info);
390
+ snprintf(name, sizeof(name), "%s%03llu.cpuprofile", buffer, (profiler->stop / 1000) % 1000);
391
+ file = fopen(name, "w");
392
+ }
393
+
394
+ fprintf(file, "{\"nodes\":[");
395
+ txU4 recordIndex = 0;
396
+ while (recordIndex < profiler->recordCount) {
397
+ txProfilerRecord* record = profiler->records[recordIndex];
398
+ if (record) {
399
+ if (recordIndex > 0)
400
+ fprintf(file, ",");
401
+ fprintf(file, "{\"id\":%d,\"callFrame\":{\"functionName\":\"", record->recordID);
402
+ if (recordIndex == 0)
403
+ fprintf(file, "(host)");
404
+ else if (recordIndex == 1)
405
+ fprintf(file, "(gc)");
406
+ else if (record->functionID != XS_NO_ID) {
407
+ if (record->constructorID != XS_NO_ID) {
408
+ fxPrintID(the, file, record->constructorID);
409
+ fprintf(file, ".");
410
+ }
411
+ if (record->prototypeID != XS_NO_ID) {
412
+ fxPrintID(the, file, record->prototypeID);
413
+ fprintf(file, ".");
414
+ }
415
+ fxPrintID(the, file, record->functionID);
416
+ }
417
+ else if (record->functionAddress) {
418
+ #if mxMacOSX || mxLinux
419
+ Dl_info info;
420
+ if (dladdr(record->functionAddress, &info) && info.dli_sname)
421
+ fprintf(file, "@%s",info.dli_sname );
422
+ else
423
+ #endif
424
+ fprintf(file, "@anonymous-%d", record->recordID);
425
+ }
426
+ else
427
+ fprintf(file, "(anonymous-%d)", record->recordID);
428
+
429
+ fprintf(file, "\",\"scriptId\":\"0\",\"url\":\"");
430
+ if (record->file != XS_NO_ID)
431
+ fxPrintID(the, file, record->file);
432
+ fprintf(file, "\",\"lineNumber\":%d,\"columnNumber\":-1},\"hitCount\":%d,\"children\":[", record->line - 1, record->hitCount);
433
+ txInteger calleeIndex = 0;
434
+ txBoolean comma = 0;
435
+ while (calleeIndex < record->calleeCount) {
436
+ txID recordID = record->callees[calleeIndex];
437
+ if (recordID != XS_NO_ID) {
438
+ if (comma)
439
+ fprintf(file, ",");
440
+ fprintf(file, "%d", recordID);
441
+ comma = 1;
442
+ }
443
+ calleeIndex++;
444
+ }
445
+ fprintf(file, "]}");
446
+ }
447
+ recordIndex++;
448
+ }
449
+ fprintf(file, "],\"startTime\":%llu,\"endTime\":%llu,\"samples\":[", fxTicksToMicroseconds(profiler->start), fxTicksToMicroseconds(profiler->when));
450
+ {
451
+ txU8 sampleIndex = 0;
452
+ while (sampleIndex < profiler->sampleIndex) {
453
+ txProfilerSample* sample = profiler->samples + sampleIndex;
454
+ if (sampleIndex > 0)
455
+ fprintf(file, ",");
456
+ fprintf(file, "%d", sample->recordID);
457
+ sampleIndex++;
458
+ }
459
+ }
460
+ fprintf(file, "],\"timeDeltas\":[");
461
+ {
462
+ txU8 sampleIndex = 0;
463
+ while (sampleIndex < profiler->sampleIndex) {
464
+ txProfilerSample* sample = profiler->samples + sampleIndex;
465
+ if (sampleIndex > 0)
466
+ fprintf(file, ",");
467
+ fprintf(file, "%llu", fxTicksToMicroseconds(sample->delta));
468
+ sampleIndex++;
469
+ }
470
+ }
471
+ fprintf(file, "]}");
472
+ fclose(file);
473
+ }
474
+
475
+ void fxPrintString(txMachine* the, FILE* file, txString theString)
476
+ {
477
+ char buffer[16];
478
+ for (;;) {
479
+ txInteger character;
480
+ char *p;
481
+ theString = mxStringByteDecode(theString, &character);
482
+ if (character == C_EOF)
483
+ break;
484
+ p = buffer;
485
+ if ((character < 32) || ((0xD800 <= character) && (character <= 0xDFFF))) {
486
+ *p++ = '\\';
487
+ *p++ = 'u';
488
+ p = fxStringifyUnicodeEscape(p, character, '\\');
489
+ }
490
+ else if (character == '"') {
491
+ *p++ = '\\';
492
+ *p++ = '"';
493
+ }
494
+ else if (character == '\\') {
495
+ *p++ = '\\';
496
+ *p++ = '\\';
497
+ }
498
+ else
499
+ p = mxStringByteEncode(p, character);
500
+ *p = 0;
501
+ fprintf(file, "%s", buffer);
502
+ }
503
+ }
504
+
505
+ void fxPushProfilerSample(txMachine* the, txID recordID, txU4 delta)
506
+ {
507
+ txProfiler* profiler = the->profiler;
508
+ txU8 sampleCount = profiler->sampleCount;
509
+ txU8 sampleIndex = profiler->sampleIndex;
510
+ txProfilerSample* sample;
511
+ if (sampleIndex == sampleCount) {
512
+ sampleCount += 1024;
513
+ profiler->samples = (txProfilerSample*)c_realloc(profiler->samples, (size_t)(sampleCount * sizeof(txProfilerSample)));
514
+ if (profiler->samples == C_NULL)
515
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
516
+ profiler->sampleCount = sampleCount;
517
+ }
518
+ sample = profiler->samples + sampleIndex;
519
+ sample->recordID = recordID;
520
+ sample->delta = delta;
521
+ profiler->sampleIndex = sampleIndex + 1;
522
+ }
523
+
524
+ txID fxRemoveProfilerCycle(txMachine* the, txProfiler* profiler, txID recordID)
525
+ {
526
+ txProfilerRecord* record = profiler->records[recordID];
527
+ if (record->flags & 1)
528
+ return XS_NO_ID;
529
+ if (record->flags & 2)
530
+ return recordID;
531
+ record->flags |= 3;
532
+ txInteger calleeIndex = 0;
533
+ while (calleeIndex < record->calleeCount) {
534
+ txID calleeID = record->callees[calleeIndex];
535
+ if (calleeID)
536
+ record->callees[calleeIndex] = fxRemoveProfilerCycle(the, profiler, calleeID);
537
+ calleeIndex++;
538
+ }
539
+ record->flags &= ~1;
540
+ return recordID;
541
+ }
542
+
543
+ void fxResumeProfiler(txMachine* the)
544
+ {
545
+ txProfiler* profiler = the->profiler;
546
+ if (!profiler)
547
+ return;
548
+ txU8 delta = fxGetTicks() - profiler->stop;
549
+ profiler->when += delta;
550
+ profiler->former += delta;
551
+ profiler->start += delta;
552
+ }
553
+
554
+ void fxSuspendProfiler(txMachine* the)
555
+ {
556
+ txProfiler* profiler = the->profiler;
557
+ if (!profiler)
558
+ return;
559
+ profiler->stop = fxGetTicks();
560
+ }
561
+
562
+ txU8 fxTicksToMicroseconds(txU8 ticks)
563
+ {
564
+ #if mxMacOSX
565
+ return ticks / 1000;
566
+ #elif mxWindows
567
+ LARGE_INTEGER frequency;
568
+ QueryPerformanceFrequency(&frequency);
569
+ return (1000000ULL * ticks) / frequency.QuadPart;
570
+ #else
571
+ return ticks;
572
+ #endif
573
+ }
574
+
575
+
576
+
577
+ #endif /* mxProfile */