quickjs 0.1.2
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE +21 -0
- data/Rakefile +22 -0
- data/ext/quickjsrb/extconf.rb +45 -0
- data/ext/quickjsrb/quickjs/LICENSE +22 -0
- data/ext/quickjsrb/quickjs/cutils.c +631 -0
- data/ext/quickjsrb/quickjs/cutils.h +347 -0
- data/ext/quickjsrb/quickjs/libbf.c +8475 -0
- data/ext/quickjsrb/quickjs/libbf.h +535 -0
- data/ext/quickjsrb/quickjs/libregexp-opcode.h +57 -0
- data/ext/quickjsrb/quickjs/libregexp.c +2501 -0
- data/ext/quickjsrb/quickjs/libregexp.h +55 -0
- data/ext/quickjsrb/quickjs/libunicode-table.h +4557 -0
- data/ext/quickjsrb/quickjs/libunicode.c +1910 -0
- data/ext/quickjsrb/quickjs/libunicode.h +182 -0
- data/ext/quickjsrb/quickjs/list.h +99 -0
- data/ext/quickjsrb/quickjs/qjs.c +564 -0
- data/ext/quickjsrb/quickjs/qjsc.c +761 -0
- data/ext/quickjsrb/quickjs/qjscalc.c +4005 -0
- data/ext/quickjsrb/quickjs/quickjs-atom.h +273 -0
- data/ext/quickjsrb/quickjs/quickjs-libc.c +4052 -0
- data/ext/quickjsrb/quickjs/quickjs-libc.h +60 -0
- data/ext/quickjsrb/quickjs/quickjs-opcode.h +372 -0
- data/ext/quickjsrb/quickjs/quickjs.c +55978 -0
- data/ext/quickjsrb/quickjs/quickjs.h +1087 -0
- data/ext/quickjsrb/quickjs/repl.c +2057 -0
- data/ext/quickjsrb/quickjs/run-test262.c +2216 -0
- data/ext/quickjsrb/quickjs/unicode_gen.c +3225 -0
- data/ext/quickjsrb/quickjs/unicode_gen_def.h +291 -0
- data/ext/quickjsrb/quickjsrb.c +105 -0
- data/ext/quickjsrb/quickjsrb.h +14 -0
- data/lib/quickjs/version.rb +5 -0
- data/lib/quickjs.rb +28 -0
- data/sig/quickjs.rbs +4 -0
- metadata +81 -0
| @@ -0,0 +1,564 @@ | |
| 1 | 
            +
            /*
         | 
| 2 | 
            +
             * QuickJS stand alone interpreter
         | 
| 3 | 
            +
             *
         | 
| 4 | 
            +
             * Copyright (c) 2017-2021 Fabrice Bellard
         | 
| 5 | 
            +
             * Copyright (c) 2017-2021 Charlie Gordon
         | 
| 6 | 
            +
             *
         | 
| 7 | 
            +
             * Permission is hereby granted, free of charge, to any person obtaining a copy
         | 
| 8 | 
            +
             * of this software and associated documentation files (the "Software"), to deal
         | 
| 9 | 
            +
             * in the Software without restriction, including without limitation the rights
         | 
| 10 | 
            +
             * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         | 
| 11 | 
            +
             * copies of the Software, and to permit persons to whom the Software is
         | 
| 12 | 
            +
             * furnished to do so, subject to the following conditions:
         | 
| 13 | 
            +
             *
         | 
| 14 | 
            +
             * The above copyright notice and this permission notice shall be included in
         | 
| 15 | 
            +
             * all copies or substantial portions of the Software.
         | 
| 16 | 
            +
             *
         | 
| 17 | 
            +
             * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         | 
| 18 | 
            +
             * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         | 
| 19 | 
            +
             * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
         | 
| 20 | 
            +
             * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         | 
| 21 | 
            +
             * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         | 
| 22 | 
            +
             * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
         | 
| 23 | 
            +
             * THE SOFTWARE.
         | 
| 24 | 
            +
             */
         | 
| 25 | 
            +
            #include <stdlib.h>
         | 
| 26 | 
            +
            #include <stdio.h>
         | 
| 27 | 
            +
            #include <stdarg.h>
         | 
| 28 | 
            +
            #include <inttypes.h>
         | 
| 29 | 
            +
            #include <string.h>
         | 
| 30 | 
            +
            #include <assert.h>
         | 
| 31 | 
            +
            #include <unistd.h>
         | 
| 32 | 
            +
            #include <errno.h>
         | 
| 33 | 
            +
            #include <fcntl.h>
         | 
| 34 | 
            +
            #include <time.h>
         | 
| 35 | 
            +
            #if defined(__APPLE__)
         | 
| 36 | 
            +
            #include <malloc/malloc.h>
         | 
| 37 | 
            +
            #elif defined(__linux__)
         | 
| 38 | 
            +
            #include <malloc.h>
         | 
| 39 | 
            +
            #elif defined(__FreeBSD__)
         | 
| 40 | 
            +
            #include <malloc_np.h>
         | 
| 41 | 
            +
            #endif
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            #include "cutils.h"
         | 
| 44 | 
            +
            #include "quickjs-libc.h"
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            extern const uint8_t qjsc_repl[];
         | 
| 47 | 
            +
            extern const uint32_t qjsc_repl_size;
         | 
| 48 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 49 | 
            +
            extern const uint8_t qjsc_qjscalc[];
         | 
| 50 | 
            +
            extern const uint32_t qjsc_qjscalc_size;
         | 
| 51 | 
            +
            static int bignum_ext;
         | 
| 52 | 
            +
            #endif
         | 
| 53 | 
            +
             | 
| 54 | 
            +
            static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
         | 
| 55 | 
            +
                                const char *filename, int eval_flags)
         | 
| 56 | 
            +
            {
         | 
| 57 | 
            +
                JSValue val;
         | 
| 58 | 
            +
                int ret;
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) {
         | 
| 61 | 
            +
                    /* for the modules, we compile then run to be able to set
         | 
| 62 | 
            +
                       import.meta */
         | 
| 63 | 
            +
                    val = JS_Eval(ctx, buf, buf_len, filename,
         | 
| 64 | 
            +
                                  eval_flags | JS_EVAL_FLAG_COMPILE_ONLY);
         | 
| 65 | 
            +
                    if (!JS_IsException(val)) {
         | 
| 66 | 
            +
                        js_module_set_import_meta(ctx, val, TRUE, TRUE);
         | 
| 67 | 
            +
                        val = JS_EvalFunction(ctx, val);
         | 
| 68 | 
            +
                    }
         | 
| 69 | 
            +
                    val = js_std_await(ctx, val);
         | 
| 70 | 
            +
                } else {
         | 
| 71 | 
            +
                    val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
         | 
| 72 | 
            +
                }
         | 
| 73 | 
            +
                if (JS_IsException(val)) {
         | 
| 74 | 
            +
                    js_std_dump_error(ctx);
         | 
| 75 | 
            +
                    ret = -1;
         | 
| 76 | 
            +
                } else {
         | 
| 77 | 
            +
                    ret = 0;
         | 
| 78 | 
            +
                }
         | 
| 79 | 
            +
                JS_FreeValue(ctx, val);
         | 
| 80 | 
            +
                return ret;
         | 
| 81 | 
            +
            }
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            static int eval_file(JSContext *ctx, const char *filename, int module)
         | 
| 84 | 
            +
            {
         | 
| 85 | 
            +
                uint8_t *buf;
         | 
| 86 | 
            +
                int ret, eval_flags;
         | 
| 87 | 
            +
                size_t buf_len;
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                buf = js_load_file(ctx, &buf_len, filename);
         | 
| 90 | 
            +
                if (!buf) {
         | 
| 91 | 
            +
                    perror(filename);
         | 
| 92 | 
            +
                    exit(1);
         | 
| 93 | 
            +
                }
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                if (module < 0) {
         | 
| 96 | 
            +
                    module = (has_suffix(filename, ".mjs") ||
         | 
| 97 | 
            +
                              JS_DetectModule((const char *)buf, buf_len));
         | 
| 98 | 
            +
                }
         | 
| 99 | 
            +
                if (module)
         | 
| 100 | 
            +
                    eval_flags = JS_EVAL_TYPE_MODULE;
         | 
| 101 | 
            +
                else
         | 
| 102 | 
            +
                    eval_flags = JS_EVAL_TYPE_GLOBAL;
         | 
| 103 | 
            +
                ret = eval_buf(ctx, buf, buf_len, filename, eval_flags);
         | 
| 104 | 
            +
                js_free(ctx, buf);
         | 
| 105 | 
            +
                return ret;
         | 
| 106 | 
            +
            }
         | 
| 107 | 
            +
             | 
| 108 | 
            +
            /* also used to initialize the worker context */
         | 
| 109 | 
            +
            static JSContext *JS_NewCustomContext(JSRuntime *rt)
         | 
| 110 | 
            +
            {
         | 
| 111 | 
            +
                JSContext *ctx;
         | 
| 112 | 
            +
                ctx = JS_NewContext(rt);
         | 
| 113 | 
            +
                if (!ctx)
         | 
| 114 | 
            +
                    return NULL;
         | 
| 115 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 116 | 
            +
                if (bignum_ext) {
         | 
| 117 | 
            +
                    JS_AddIntrinsicBigFloat(ctx);
         | 
| 118 | 
            +
                    JS_AddIntrinsicBigDecimal(ctx);
         | 
| 119 | 
            +
                    JS_AddIntrinsicOperators(ctx);
         | 
| 120 | 
            +
                    JS_EnableBignumExt(ctx, TRUE);
         | 
| 121 | 
            +
                }
         | 
| 122 | 
            +
            #endif
         | 
| 123 | 
            +
                /* system modules */
         | 
| 124 | 
            +
                js_init_module_std(ctx, "std");
         | 
| 125 | 
            +
                js_init_module_os(ctx, "os");
         | 
| 126 | 
            +
                return ctx;
         | 
| 127 | 
            +
            }
         | 
| 128 | 
            +
             | 
| 129 | 
            +
            #if defined(__APPLE__)
         | 
| 130 | 
            +
            #define MALLOC_OVERHEAD  0
         | 
| 131 | 
            +
            #else
         | 
| 132 | 
            +
            #define MALLOC_OVERHEAD  8
         | 
| 133 | 
            +
            #endif
         | 
| 134 | 
            +
             | 
| 135 | 
            +
            struct trace_malloc_data {
         | 
| 136 | 
            +
                uint8_t *base;
         | 
| 137 | 
            +
            };
         | 
| 138 | 
            +
             | 
| 139 | 
            +
            static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr,
         | 
| 140 | 
            +
                                                            struct trace_malloc_data *dp)
         | 
| 141 | 
            +
            {
         | 
| 142 | 
            +
                return ptr - dp->base;
         | 
| 143 | 
            +
            }
         | 
| 144 | 
            +
             | 
| 145 | 
            +
            /* default memory allocation functions with memory limitation */
         | 
| 146 | 
            +
            static size_t js_trace_malloc_usable_size(const void *ptr)
         | 
| 147 | 
            +
            {
         | 
| 148 | 
            +
            #if defined(__APPLE__)
         | 
| 149 | 
            +
                return malloc_size(ptr);
         | 
| 150 | 
            +
            #elif defined(_WIN32)
         | 
| 151 | 
            +
                return _msize((void *)ptr);
         | 
| 152 | 
            +
            #elif defined(EMSCRIPTEN)
         | 
| 153 | 
            +
                return 0;
         | 
| 154 | 
            +
            #elif defined(__linux__)
         | 
| 155 | 
            +
                return malloc_usable_size((void *)ptr);
         | 
| 156 | 
            +
            #else
         | 
| 157 | 
            +
                /* change this to `return 0;` if compilation fails */
         | 
| 158 | 
            +
                return malloc_usable_size((void *)ptr);
         | 
| 159 | 
            +
            #endif
         | 
| 160 | 
            +
            }
         | 
| 161 | 
            +
             | 
| 162 | 
            +
            static void
         | 
| 163 | 
            +
            #ifdef _WIN32
         | 
| 164 | 
            +
            /* mingw printf is used */
         | 
| 165 | 
            +
            __attribute__((format(gnu_printf, 2, 3)))
         | 
| 166 | 
            +
            #else
         | 
| 167 | 
            +
            __attribute__((format(printf, 2, 3)))
         | 
| 168 | 
            +
            #endif
         | 
| 169 | 
            +
                js_trace_malloc_printf(JSMallocState *s, const char *fmt, ...)
         | 
| 170 | 
            +
            {
         | 
| 171 | 
            +
                va_list ap;
         | 
| 172 | 
            +
                int c;
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                va_start(ap, fmt);
         | 
| 175 | 
            +
                while ((c = *fmt++) != '\0') {
         | 
| 176 | 
            +
                    if (c == '%') {
         | 
| 177 | 
            +
                        /* only handle %p and %zd */
         | 
| 178 | 
            +
                        if (*fmt == 'p') {
         | 
| 179 | 
            +
                            uint8_t *ptr = va_arg(ap, void *);
         | 
| 180 | 
            +
                            if (ptr == NULL) {
         | 
| 181 | 
            +
                                printf("NULL");
         | 
| 182 | 
            +
                            } else {
         | 
| 183 | 
            +
                                printf("H%+06lld.%zd",
         | 
| 184 | 
            +
                                       js_trace_malloc_ptr_offset(ptr, s->opaque),
         | 
| 185 | 
            +
                                       js_trace_malloc_usable_size(ptr));
         | 
| 186 | 
            +
                            }
         | 
| 187 | 
            +
                            fmt++;
         | 
| 188 | 
            +
                            continue;
         | 
| 189 | 
            +
                        }
         | 
| 190 | 
            +
                        if (fmt[0] == 'z' && fmt[1] == 'd') {
         | 
| 191 | 
            +
                            size_t sz = va_arg(ap, size_t);
         | 
| 192 | 
            +
                            printf("%zd", sz);
         | 
| 193 | 
            +
                            fmt += 2;
         | 
| 194 | 
            +
                            continue;
         | 
| 195 | 
            +
                        }
         | 
| 196 | 
            +
                    }
         | 
| 197 | 
            +
                    putc(c, stdout);
         | 
| 198 | 
            +
                }
         | 
| 199 | 
            +
                va_end(ap);
         | 
| 200 | 
            +
            }
         | 
| 201 | 
            +
             | 
| 202 | 
            +
            static void js_trace_malloc_init(struct trace_malloc_data *s)
         | 
| 203 | 
            +
            {
         | 
| 204 | 
            +
                free(s->base = malloc(8));
         | 
| 205 | 
            +
            }
         | 
| 206 | 
            +
             | 
| 207 | 
            +
            static void *js_trace_malloc(JSMallocState *s, size_t size)
         | 
| 208 | 
            +
            {
         | 
| 209 | 
            +
                void *ptr;
         | 
| 210 | 
            +
             | 
| 211 | 
            +
                /* Do not allocate zero bytes: behavior is platform dependent */
         | 
| 212 | 
            +
                assert(size != 0);
         | 
| 213 | 
            +
             | 
| 214 | 
            +
                if (unlikely(s->malloc_size + size > s->malloc_limit))
         | 
| 215 | 
            +
                    return NULL;
         | 
| 216 | 
            +
                ptr = malloc(size);
         | 
| 217 | 
            +
                js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
         | 
| 218 | 
            +
                if (ptr) {
         | 
| 219 | 
            +
                    s->malloc_count++;
         | 
| 220 | 
            +
                    s->malloc_size += js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
         | 
| 221 | 
            +
                }
         | 
| 222 | 
            +
                return ptr;
         | 
| 223 | 
            +
            }
         | 
| 224 | 
            +
             | 
| 225 | 
            +
            static void js_trace_free(JSMallocState *s, void *ptr)
         | 
| 226 | 
            +
            {
         | 
| 227 | 
            +
                if (!ptr)
         | 
| 228 | 
            +
                    return;
         | 
| 229 | 
            +
             | 
| 230 | 
            +
                js_trace_malloc_printf(s, "F %p\n", ptr);
         | 
| 231 | 
            +
                s->malloc_count--;
         | 
| 232 | 
            +
                s->malloc_size -= js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
         | 
| 233 | 
            +
                free(ptr);
         | 
| 234 | 
            +
            }
         | 
| 235 | 
            +
             | 
| 236 | 
            +
            static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
         | 
| 237 | 
            +
            {
         | 
| 238 | 
            +
                size_t old_size;
         | 
| 239 | 
            +
             | 
| 240 | 
            +
                if (!ptr) {
         | 
| 241 | 
            +
                    if (size == 0)
         | 
| 242 | 
            +
                        return NULL;
         | 
| 243 | 
            +
                    return js_trace_malloc(s, size);
         | 
| 244 | 
            +
                }
         | 
| 245 | 
            +
                old_size = js_trace_malloc_usable_size(ptr);
         | 
| 246 | 
            +
                if (size == 0) {
         | 
| 247 | 
            +
                    js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
         | 
| 248 | 
            +
                    s->malloc_count--;
         | 
| 249 | 
            +
                    s->malloc_size -= old_size + MALLOC_OVERHEAD;
         | 
| 250 | 
            +
                    free(ptr);
         | 
| 251 | 
            +
                    return NULL;
         | 
| 252 | 
            +
                }
         | 
| 253 | 
            +
                if (s->malloc_size + size - old_size > s->malloc_limit)
         | 
| 254 | 
            +
                    return NULL;
         | 
| 255 | 
            +
             | 
| 256 | 
            +
                js_trace_malloc_printf(s, "R %zd %p", size, ptr);
         | 
| 257 | 
            +
             | 
| 258 | 
            +
                ptr = realloc(ptr, size);
         | 
| 259 | 
            +
                js_trace_malloc_printf(s, " -> %p\n", ptr);
         | 
| 260 | 
            +
                if (ptr) {
         | 
| 261 | 
            +
                    s->malloc_size += js_trace_malloc_usable_size(ptr) - old_size;
         | 
| 262 | 
            +
                }
         | 
| 263 | 
            +
                return ptr;
         | 
| 264 | 
            +
            }
         | 
| 265 | 
            +
             | 
| 266 | 
            +
            static const JSMallocFunctions trace_mf = {
         | 
| 267 | 
            +
                js_trace_malloc,
         | 
| 268 | 
            +
                js_trace_free,
         | 
| 269 | 
            +
                js_trace_realloc,
         | 
| 270 | 
            +
                js_trace_malloc_usable_size,
         | 
| 271 | 
            +
            };
         | 
| 272 | 
            +
             | 
| 273 | 
            +
            #define PROG_NAME "qjs"
         | 
| 274 | 
            +
             | 
| 275 | 
            +
            void help(void)
         | 
| 276 | 
            +
            {
         | 
| 277 | 
            +
                printf("QuickJS version " CONFIG_VERSION "\n"
         | 
| 278 | 
            +
                       "usage: " PROG_NAME " [options] [file [args]]\n"
         | 
| 279 | 
            +
                       "-h  --help         list options\n"
         | 
| 280 | 
            +
                       "-e  --eval EXPR    evaluate EXPR\n"
         | 
| 281 | 
            +
                       "-i  --interactive  go to interactive mode\n"
         | 
| 282 | 
            +
                       "-m  --module       load as ES6 module (default=autodetect)\n"
         | 
| 283 | 
            +
                       "    --script       load as ES6 script (default=autodetect)\n"
         | 
| 284 | 
            +
                       "-I  --include file include an additional file\n"
         | 
| 285 | 
            +
                       "    --std          make 'std' and 'os' available to the loaded script\n"
         | 
| 286 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 287 | 
            +
                       "    --bignum       enable the bignum extensions (BigFloat, BigDecimal)\n"
         | 
| 288 | 
            +
                       "    --qjscalc      load the QJSCalc runtime (default if invoked as qjscalc)\n"
         | 
| 289 | 
            +
            #endif
         | 
| 290 | 
            +
                       "-T  --trace        trace memory allocation\n"
         | 
| 291 | 
            +
                       "-d  --dump         dump the memory usage stats\n"
         | 
| 292 | 
            +
                       "    --memory-limit n       limit the memory usage to 'n' bytes\n"
         | 
| 293 | 
            +
                       "    --stack-size n         limit the stack size to 'n' bytes\n"
         | 
| 294 | 
            +
                       "    --unhandled-rejection  dump unhandled promise rejections\n"
         | 
| 295 | 
            +
                       "-q  --quit         just instantiate the interpreter and quit\n");
         | 
| 296 | 
            +
                exit(1);
         | 
| 297 | 
            +
            }
         | 
| 298 | 
            +
             | 
| 299 | 
            +
            int main(int argc, char **argv)
         | 
| 300 | 
            +
            {
         | 
| 301 | 
            +
                JSRuntime *rt;
         | 
| 302 | 
            +
                JSContext *ctx;
         | 
| 303 | 
            +
                struct trace_malloc_data trace_data = { NULL };
         | 
| 304 | 
            +
                int optind;
         | 
| 305 | 
            +
                char *expr = NULL;
         | 
| 306 | 
            +
                int interactive = 0;
         | 
| 307 | 
            +
                int dump_memory = 0;
         | 
| 308 | 
            +
                int trace_memory = 0;
         | 
| 309 | 
            +
                int empty_run = 0;
         | 
| 310 | 
            +
                int module = -1;
         | 
| 311 | 
            +
                int load_std = 0;
         | 
| 312 | 
            +
                int dump_unhandled_promise_rejection = 0;
         | 
| 313 | 
            +
                size_t memory_limit = 0;
         | 
| 314 | 
            +
                char *include_list[32];
         | 
| 315 | 
            +
                int i, include_count = 0;
         | 
| 316 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 317 | 
            +
                int load_jscalc;
         | 
| 318 | 
            +
            #endif
         | 
| 319 | 
            +
                size_t stack_size = 0;
         | 
| 320 | 
            +
             | 
| 321 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 322 | 
            +
                /* load jscalc runtime if invoked as 'qjscalc' */
         | 
| 323 | 
            +
                {
         | 
| 324 | 
            +
                    const char *p, *exename;
         | 
| 325 | 
            +
                    exename = argv[0];
         | 
| 326 | 
            +
                    p = strrchr(exename, '/');
         | 
| 327 | 
            +
                    if (p)
         | 
| 328 | 
            +
                        exename = p + 1;
         | 
| 329 | 
            +
                    load_jscalc = !strcmp(exename, "qjscalc");
         | 
| 330 | 
            +
                }
         | 
| 331 | 
            +
            #endif
         | 
| 332 | 
            +
             | 
| 333 | 
            +
                /* cannot use getopt because we want to pass the command line to
         | 
| 334 | 
            +
                   the script */
         | 
| 335 | 
            +
                optind = 1;
         | 
| 336 | 
            +
                while (optind < argc && *argv[optind] == '-') {
         | 
| 337 | 
            +
                    char *arg = argv[optind] + 1;
         | 
| 338 | 
            +
                    const char *longopt = "";
         | 
| 339 | 
            +
                    /* a single - is not an option, it also stops argument scanning */
         | 
| 340 | 
            +
                    if (!*arg)
         | 
| 341 | 
            +
                        break;
         | 
| 342 | 
            +
                    optind++;
         | 
| 343 | 
            +
                    if (*arg == '-') {
         | 
| 344 | 
            +
                        longopt = arg + 1;
         | 
| 345 | 
            +
                        arg += strlen(arg);
         | 
| 346 | 
            +
                        /* -- stops argument scanning */
         | 
| 347 | 
            +
                        if (!*longopt)
         | 
| 348 | 
            +
                            break;
         | 
| 349 | 
            +
                    }
         | 
| 350 | 
            +
                    for (; *arg || *longopt; longopt = "") {
         | 
| 351 | 
            +
                        char opt = *arg;
         | 
| 352 | 
            +
                        if (opt)
         | 
| 353 | 
            +
                            arg++;
         | 
| 354 | 
            +
                        if (opt == 'h' || opt == '?' || !strcmp(longopt, "help")) {
         | 
| 355 | 
            +
                            help();
         | 
| 356 | 
            +
                            continue;
         | 
| 357 | 
            +
                        }
         | 
| 358 | 
            +
                        if (opt == 'e' || !strcmp(longopt, "eval")) {
         | 
| 359 | 
            +
                            if (*arg) {
         | 
| 360 | 
            +
                                expr = arg;
         | 
| 361 | 
            +
                                break;
         | 
| 362 | 
            +
                            }
         | 
| 363 | 
            +
                            if (optind < argc) {
         | 
| 364 | 
            +
                                expr = argv[optind++];
         | 
| 365 | 
            +
                                break;
         | 
| 366 | 
            +
                            }
         | 
| 367 | 
            +
                            fprintf(stderr, "qjs: missing expression for -e\n");
         | 
| 368 | 
            +
                            exit(2);
         | 
| 369 | 
            +
                        }
         | 
| 370 | 
            +
                        if (opt == 'I' || !strcmp(longopt, "include")) {
         | 
| 371 | 
            +
                            if (optind >= argc) {
         | 
| 372 | 
            +
                                fprintf(stderr, "expecting filename");
         | 
| 373 | 
            +
                                exit(1);
         | 
| 374 | 
            +
                            }
         | 
| 375 | 
            +
                            if (include_count >= countof(include_list)) {
         | 
| 376 | 
            +
                                fprintf(stderr, "too many included files");
         | 
| 377 | 
            +
                                exit(1);
         | 
| 378 | 
            +
                            }
         | 
| 379 | 
            +
                            include_list[include_count++] = argv[optind++];
         | 
| 380 | 
            +
                            continue;
         | 
| 381 | 
            +
                        }
         | 
| 382 | 
            +
                        if (opt == 'i' || !strcmp(longopt, "interactive")) {
         | 
| 383 | 
            +
                            interactive++;
         | 
| 384 | 
            +
                            continue;
         | 
| 385 | 
            +
                        }
         | 
| 386 | 
            +
                        if (opt == 'm' || !strcmp(longopt, "module")) {
         | 
| 387 | 
            +
                            module = 1;
         | 
| 388 | 
            +
                            continue;
         | 
| 389 | 
            +
                        }
         | 
| 390 | 
            +
                        if (!strcmp(longopt, "script")) {
         | 
| 391 | 
            +
                            module = 0;
         | 
| 392 | 
            +
                            continue;
         | 
| 393 | 
            +
                        }
         | 
| 394 | 
            +
                        if (opt == 'd' || !strcmp(longopt, "dump")) {
         | 
| 395 | 
            +
                            dump_memory++;
         | 
| 396 | 
            +
                            continue;
         | 
| 397 | 
            +
                        }
         | 
| 398 | 
            +
                        if (opt == 'T' || !strcmp(longopt, "trace")) {
         | 
| 399 | 
            +
                            trace_memory++;
         | 
| 400 | 
            +
                            continue;
         | 
| 401 | 
            +
                        }
         | 
| 402 | 
            +
                        if (!strcmp(longopt, "std")) {
         | 
| 403 | 
            +
                            load_std = 1;
         | 
| 404 | 
            +
                            continue;
         | 
| 405 | 
            +
                        }
         | 
| 406 | 
            +
                        if (!strcmp(longopt, "unhandled-rejection")) {
         | 
| 407 | 
            +
                            dump_unhandled_promise_rejection = 1;
         | 
| 408 | 
            +
                            continue;
         | 
| 409 | 
            +
                        }
         | 
| 410 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 411 | 
            +
                        if (!strcmp(longopt, "bignum")) {
         | 
| 412 | 
            +
                            bignum_ext = 1;
         | 
| 413 | 
            +
                            continue;
         | 
| 414 | 
            +
                        }
         | 
| 415 | 
            +
                        if (!strcmp(longopt, "qjscalc")) {
         | 
| 416 | 
            +
                            load_jscalc = 1;
         | 
| 417 | 
            +
                            continue;
         | 
| 418 | 
            +
                        }
         | 
| 419 | 
            +
            #endif
         | 
| 420 | 
            +
                        if (opt == 'q' || !strcmp(longopt, "quit")) {
         | 
| 421 | 
            +
                            empty_run++;
         | 
| 422 | 
            +
                            continue;
         | 
| 423 | 
            +
                        }
         | 
| 424 | 
            +
                        if (!strcmp(longopt, "memory-limit")) {
         | 
| 425 | 
            +
                            if (optind >= argc) {
         | 
| 426 | 
            +
                                fprintf(stderr, "expecting memory limit");
         | 
| 427 | 
            +
                                exit(1);
         | 
| 428 | 
            +
                            }
         | 
| 429 | 
            +
                            memory_limit = (size_t)strtod(argv[optind++], NULL);
         | 
| 430 | 
            +
                            continue;
         | 
| 431 | 
            +
                        }
         | 
| 432 | 
            +
                        if (!strcmp(longopt, "stack-size")) {
         | 
| 433 | 
            +
                            if (optind >= argc) {
         | 
| 434 | 
            +
                                fprintf(stderr, "expecting stack size");
         | 
| 435 | 
            +
                                exit(1);
         | 
| 436 | 
            +
                            }
         | 
| 437 | 
            +
                            stack_size = (size_t)strtod(argv[optind++], NULL);
         | 
| 438 | 
            +
                            continue;
         | 
| 439 | 
            +
                        }
         | 
| 440 | 
            +
                        if (opt) {
         | 
| 441 | 
            +
                            fprintf(stderr, "qjs: unknown option '-%c'\n", opt);
         | 
| 442 | 
            +
                        } else {
         | 
| 443 | 
            +
                            fprintf(stderr, "qjs: unknown option '--%s'\n", longopt);
         | 
| 444 | 
            +
                        }
         | 
| 445 | 
            +
                        help();
         | 
| 446 | 
            +
                    }
         | 
| 447 | 
            +
                }
         | 
| 448 | 
            +
             | 
| 449 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 450 | 
            +
                if (load_jscalc)
         | 
| 451 | 
            +
                    bignum_ext = 1;
         | 
| 452 | 
            +
            #endif
         | 
| 453 | 
            +
             | 
| 454 | 
            +
                if (trace_memory) {
         | 
| 455 | 
            +
                    js_trace_malloc_init(&trace_data);
         | 
| 456 | 
            +
                    rt = JS_NewRuntime2(&trace_mf, &trace_data);
         | 
| 457 | 
            +
                } else {
         | 
| 458 | 
            +
                    rt = JS_NewRuntime();
         | 
| 459 | 
            +
                }
         | 
| 460 | 
            +
                if (!rt) {
         | 
| 461 | 
            +
                    fprintf(stderr, "qjs: cannot allocate JS runtime\n");
         | 
| 462 | 
            +
                    exit(2);
         | 
| 463 | 
            +
                }
         | 
| 464 | 
            +
                if (memory_limit != 0)
         | 
| 465 | 
            +
                    JS_SetMemoryLimit(rt, memory_limit);
         | 
| 466 | 
            +
                if (stack_size != 0)
         | 
| 467 | 
            +
                    JS_SetMaxStackSize(rt, stack_size);
         | 
| 468 | 
            +
                js_std_set_worker_new_context_func(JS_NewCustomContext);
         | 
| 469 | 
            +
                js_std_init_handlers(rt);
         | 
| 470 | 
            +
                ctx = JS_NewCustomContext(rt);
         | 
| 471 | 
            +
                if (!ctx) {
         | 
| 472 | 
            +
                    fprintf(stderr, "qjs: cannot allocate JS context\n");
         | 
| 473 | 
            +
                    exit(2);
         | 
| 474 | 
            +
                }
         | 
| 475 | 
            +
             | 
| 476 | 
            +
                /* loader for ES6 modules */
         | 
| 477 | 
            +
                JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
         | 
| 478 | 
            +
             | 
| 479 | 
            +
                if (dump_unhandled_promise_rejection) {
         | 
| 480 | 
            +
                    JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker,
         | 
| 481 | 
            +
                                                      NULL);
         | 
| 482 | 
            +
                }
         | 
| 483 | 
            +
             | 
| 484 | 
            +
                if (!empty_run) {
         | 
| 485 | 
            +
            #ifdef CONFIG_BIGNUM
         | 
| 486 | 
            +
                    if (load_jscalc) {
         | 
| 487 | 
            +
                        js_std_eval_binary(ctx, qjsc_qjscalc, qjsc_qjscalc_size, 0);
         | 
| 488 | 
            +
                    }
         | 
| 489 | 
            +
            #endif
         | 
| 490 | 
            +
                    js_std_add_helpers(ctx, argc - optind, argv + optind);
         | 
| 491 | 
            +
             | 
| 492 | 
            +
                    /* make 'std' and 'os' visible to non module code */
         | 
| 493 | 
            +
                    if (load_std) {
         | 
| 494 | 
            +
                        const char *str = "import * as std from 'std';\n"
         | 
| 495 | 
            +
                            "import * as os from 'os';\n"
         | 
| 496 | 
            +
                            "globalThis.std = std;\n"
         | 
| 497 | 
            +
                            "globalThis.os = os;\n";
         | 
| 498 | 
            +
                        eval_buf(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE);
         | 
| 499 | 
            +
                    }
         | 
| 500 | 
            +
             | 
| 501 | 
            +
                    for(i = 0; i < include_count; i++) {
         | 
| 502 | 
            +
                        if (eval_file(ctx, include_list[i], module))
         | 
| 503 | 
            +
                            goto fail;
         | 
| 504 | 
            +
                    }
         | 
| 505 | 
            +
             | 
| 506 | 
            +
                    if (expr) {
         | 
| 507 | 
            +
                        if (eval_buf(ctx, expr, strlen(expr), "<cmdline>", 0))
         | 
| 508 | 
            +
                            goto fail;
         | 
| 509 | 
            +
                    } else
         | 
| 510 | 
            +
                    if (optind >= argc) {
         | 
| 511 | 
            +
                        /* interactive mode */
         | 
| 512 | 
            +
                        interactive = 1;
         | 
| 513 | 
            +
                    } else {
         | 
| 514 | 
            +
                        const char *filename;
         | 
| 515 | 
            +
                        filename = argv[optind];
         | 
| 516 | 
            +
                        if (eval_file(ctx, filename, module))
         | 
| 517 | 
            +
                            goto fail;
         | 
| 518 | 
            +
                    }
         | 
| 519 | 
            +
                    if (interactive) {
         | 
| 520 | 
            +
                        js_std_eval_binary(ctx, qjsc_repl, qjsc_repl_size, 0);
         | 
| 521 | 
            +
                    }
         | 
| 522 | 
            +
                    js_std_loop(ctx);
         | 
| 523 | 
            +
                }
         | 
| 524 | 
            +
             | 
| 525 | 
            +
                if (dump_memory) {
         | 
| 526 | 
            +
                    JSMemoryUsage stats;
         | 
| 527 | 
            +
                    JS_ComputeMemoryUsage(rt, &stats);
         | 
| 528 | 
            +
                    JS_DumpMemoryUsage(stdout, &stats, rt);
         | 
| 529 | 
            +
                }
         | 
| 530 | 
            +
                js_std_free_handlers(rt);
         | 
| 531 | 
            +
                JS_FreeContext(ctx);
         | 
| 532 | 
            +
                JS_FreeRuntime(rt);
         | 
| 533 | 
            +
             | 
| 534 | 
            +
                if (empty_run && dump_memory) {
         | 
| 535 | 
            +
                    clock_t t[5];
         | 
| 536 | 
            +
                    double best[5];
         | 
| 537 | 
            +
                    int i, j;
         | 
| 538 | 
            +
                    for (i = 0; i < 100; i++) {
         | 
| 539 | 
            +
                        t[0] = clock();
         | 
| 540 | 
            +
                        rt = JS_NewRuntime();
         | 
| 541 | 
            +
                        t[1] = clock();
         | 
| 542 | 
            +
                        ctx = JS_NewContext(rt);
         | 
| 543 | 
            +
                        t[2] = clock();
         | 
| 544 | 
            +
                        JS_FreeContext(ctx);
         | 
| 545 | 
            +
                        t[3] = clock();
         | 
| 546 | 
            +
                        JS_FreeRuntime(rt);
         | 
| 547 | 
            +
                        t[4] = clock();
         | 
| 548 | 
            +
                        for (j = 4; j > 0; j--) {
         | 
| 549 | 
            +
                            double ms = 1000.0 * (t[j] - t[j - 1]) / CLOCKS_PER_SEC;
         | 
| 550 | 
            +
                            if (i == 0 || best[j] > ms)
         | 
| 551 | 
            +
                                best[j] = ms;
         | 
| 552 | 
            +
                        }
         | 
| 553 | 
            +
                    }
         | 
| 554 | 
            +
                    printf("\nInstantiation times (ms): %.3f = %.3f+%.3f+%.3f+%.3f\n",
         | 
| 555 | 
            +
                           best[1] + best[2] + best[3] + best[4],
         | 
| 556 | 
            +
                           best[1], best[2], best[3], best[4]);
         | 
| 557 | 
            +
                }
         | 
| 558 | 
            +
                return 0;
         | 
| 559 | 
            +
             fail:
         | 
| 560 | 
            +
                js_std_free_handlers(rt);
         | 
| 561 | 
            +
                JS_FreeContext(ctx);
         | 
| 562 | 
            +
                JS_FreeRuntime(rt);
         | 
| 563 | 
            +
                return 1;
         | 
| 564 | 
            +
            }
         |