forked from bellard/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptkl.c
392 lines (347 loc) · 9.72 KB
/
ptkl.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* ptkl
*
* Copyright (c) 2017-2024 Fabrice Bellard
* Copyright (c) 2017-2024 Charlie Gordon
* Copyright (c) 2025 Tony Pujals
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__GLIBC__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif
#include "ptklargs.h"
#include "cutils.h"
#include "quickjs-libc.h"
extern const uint8_t qjsc_repl[];
extern const uint32_t qjsc_repl_size;
extern int compile(int argc, char **argv);
// Automatically compiled with ecmascript standard bigint support, so this
// just disables non-standard bigdecimal, bigfloat, and related extensions
static int bignum_ext = 0;
static int eval_buf(JSContext *ctx, const void *buf, const size_t buf_len,
const char *filename, const int eval_flags) {
JSValue val;
int ret;
if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) {
// For modules, compile then run to be able to set import.meta
val = JS_Eval(ctx, buf, buf_len, filename,
eval_flags | JS_EVAL_FLAG_COMPILE_ONLY);
if (!JS_IsException(val)) {
js_module_set_import_meta(ctx, val, TRUE, TRUE);
val = JS_EvalFunction(ctx, val);
}
val = js_std_await(ctx, val);
} else {
val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
}
if (JS_IsException(val)) {
js_std_dump_error(ctx);
ret = -1;
} else {
ret = 0;
}
JS_FreeValue(ctx, val);
return ret;
}
static int eval_file(JSContext *ctx, const char *filename, int module) {
int eval_flags;
size_t buf_len;
uint8_t *buf = js_load_file(ctx, &buf_len, filename);
if (!buf) {
perror(filename);
exit(1);
}
if (module < 0) {
module = has_suffix(filename, ".mjs") ||
JS_DetectModule((const char *) buf, buf_len);
}
if (module)
eval_flags = JS_EVAL_TYPE_MODULE;
else
eval_flags = JS_EVAL_TYPE_GLOBAL;
const int ret = eval_buf(ctx, buf, buf_len, filename, eval_flags);
js_free(ctx, buf);
return ret;
}
// Also used to initialize the worker context
static JSContext *JS_NewCustomContext(JSRuntime *rt) {
JSContext *ctx = JS_NewContext(rt);
if (!ctx)
return nullptr;
if (bignum_ext) {
JS_AddIntrinsicBigFloat(ctx);
JS_AddIntrinsicBigDecimal(ctx);
JS_AddIntrinsicOperators(ctx);
JS_EnableBignumExt(ctx, TRUE);
}
/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
return ctx;
}
#if defined(__APPLE__)
#define MALLOC_OVERHEAD 0
#else
#define MALLOC_OVERHEAD 8
#endif
struct trace_malloc_data {
uint8_t *base;
};
static unsigned long long js_trace_malloc_ptr_offset(const uint8_t *ptr,
const struct
trace_malloc_data *dp) {
return ptr - dp->base;
}
// Default memory allocation functions with memory limitation
static size_t js_trace_malloc_usable_size(const void *ptr) {
#if defined(__APPLE__)
return malloc_size(ptr);
#elif defined(EMSCRIPTEN)
return 0;
#elif defined(__linux__) || defined(__GLIBC__)
return malloc_usable_size((void *)ptr);
#else
/* change this to `return 0;` if compilation fails */
return malloc_usable_size((void *)ptr);
#endif
}
static void
__attribute__((format(printf, 2, 3)))
js_trace_malloc_printf(const JSMallocState *s, const char *fmt, ...) {
va_list ap;
int c;
va_start(ap, fmt);
while ((c = *fmt++) != '\0') {
if (c == '%') {
/* only handle %p and %zd */
if (*fmt == 'p') {
const uint8_t *ptr = va_arg(ap, void *);
if (ptr == nullptr) {
printf("nullptr");
} else {
printf("H%+06lld.%zd",
js_trace_malloc_ptr_offset(
ptr, s->opaque),
js_trace_malloc_usable_size(
ptr));
}
fmt++;
continue;
}
if (fmt[0] == 'z' && fmt[1] == 'd') {
const size_t sz = va_arg(ap, size_t);
printf("%zd", sz);
fmt += 2;
continue;
}
}
putc(c, stdout);
}
va_end(ap);
}
static void js_trace_malloc_init(struct trace_malloc_data *s) {
free(s->base = malloc(8));
}
static void *js_trace_malloc(JSMallocState *s, const size_t size) {
// Do not allocate zero bytes: behavior is platform dependent
assert(size != 0);
if (unlikely(s->malloc_size + size > s->malloc_limit))
return nullptr;
void *ptr = malloc(size);
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
if (ptr) {
s->malloc_count++;
s->malloc_size += js_trace_malloc_usable_size(ptr) +
MALLOC_OVERHEAD;
}
return ptr;
}
static void js_trace_free(JSMallocState *s, void *ptr) {
if (!ptr)
return;
js_trace_malloc_printf(s, "F %p\n", ptr);
s->malloc_count--;
s->malloc_size -= js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
free(ptr);
}
static void *js_trace_realloc(JSMallocState *s, void *ptr, const size_t size) {
if (!ptr) {
if (size == 0)
return nullptr;
return js_trace_malloc(s, size);
}
const size_t old_size = js_trace_malloc_usable_size(ptr);
if (size == 0) {
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
s->malloc_count--;
s->malloc_size -= old_size + MALLOC_OVERHEAD;
free(ptr);
return nullptr;
}
if (s->malloc_size + size - old_size > s->malloc_limit)
return nullptr;
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
void *tmp = realloc(ptr, size);
if (!tmp) {
fprintf(stderr, "realloc failed, out of memory\n");
exit(1);
}
ptr = tmp;
js_trace_malloc_printf(s, " -> %p\n", ptr);
s->malloc_size += js_trace_malloc_usable_size(ptr) - old_size;
return ptr;
}
static const JSMallocFunctions trace_mf = {
js_trace_malloc,
js_trace_free,
js_trace_realloc,
js_trace_malloc_usable_size,
};
int main(int argc, char **argv) {
if (argc > 1 && !strcmp(argv[1], "compile")) {
for (int i = 1; i < argc; i++) {
argv[i] = argv[i + 1];
}
return compile(--argc, argv);
}
JSRuntime *rt;
struct trace_malloc_data trace_data = {nullptr};
struct runtime_opts opts = {};
const int optind = parse_runtime_args(argc, argv, &opts);
// Initialize the runtime
if (opts.trace_memory) {
js_trace_malloc_init(&trace_data);
rt = JS_NewRuntime2(&trace_mf, &trace_data);
} else {
rt = JS_NewRuntime();
}
if (!rt) {
fprintf(stderr, "ptkl: cannot allocate JS runtime\n");
exit(2);
}
// Configure limits
if (opts.memory_limit != 0)
JS_SetMemoryLimit(rt, opts.memory_limit);
if (opts.stack_size != 0)
JS_SetMaxStackSize(rt, opts.stack_size);
// Set bignum extension before creating main and worker contexts
bignum_ext = opts.bignum_ext;
// Workers
js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);
// Runtime context
JSContext *ctx = JS_NewCustomContext(rt);
if (!ctx) {
fprintf(stderr, "ptkl: cannot allocate JS context\n");
exit(2);
}
// ES6 module loader
JS_SetModuleLoaderFunc(rt, nullptr, js_module_loader, nullptr);
// Handle options
if (opts.dump_unhandled_promise_rejection) {
JS_SetHostPromiseRejectionTracker(
rt, js_std_promise_rejection_tracker,
nullptr);
}
if (!opts.empty_run) {
js_std_add_helpers(ctx, argc - optind, argv + optind);
// make 'std' and 'os' visible to non module code
if (opts.load_std) {
const char *str = "import * as std from 'std';\n"
"import * as os from 'os';\n"
"globalThis.std = std;\n"
"globalThis.os = os;\n";
eval_buf(ctx, str, strlen(str), "<input>",
JS_EVAL_TYPE_MODULE);
}
for (int i = 0; i < opts.include_count; i++) {
if (eval_file(ctx, opts.include_list[i], opts.module))
goto fail;
}
if (opts.expr) {
if (eval_buf(ctx, opts.expr, strlen(opts.expr),
"<cmdline>", 0))
goto fail;
} else if (optind >= argc) {
/* interactive mode */
opts.interactive = 1;
} else {
const char *filename = argv[optind];
if (eval_file(ctx, filename, opts.module))
goto fail;
}
if (opts.interactive) {
js_std_eval_binary(ctx, qjsc_repl, qjsc_repl_size, 0);
}
js_std_loop(ctx);
}
if (opts.dump_memory) {
JSMemoryUsage stats;
JS_ComputeMemoryUsage(rt, &stats);
JS_DumpMemoryUsage(stdout, &stats, rt);
}
js_std_free_handlers(rt);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
if (opts.empty_run && opts.dump_memory) {
clock_t t[5];
double best[5];
for (int i = 0; i < 100; i++) {
t[0] = clock();
rt = JS_NewRuntime();
t[1] = clock();
ctx = JS_NewContext(rt);
t[2] = clock();
JS_FreeContext(ctx);
t[3] = clock();
JS_FreeRuntime(rt);
t[4] = clock();
for (int j = 4; j > 0; j--) {
const unsigned ms =
1000 * (t[j] - t[j - 1]) /
CLOCKS_PER_SEC;
if (i == 0 || best[j] > ms)
best[j] = ms;
}
}
printf(
"\nInstantiation times (ms): %.3f = %.3f+%.3f+%.3f+%.3f\n",
best[1] + best[2] + best[3] + best[4],
best[1], best[2], best[3], best[4]);
}
return 0;
fail:
js_std_free_handlers(rt);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return 1;
}