Exception: MiniRacer::EvalError

Inherits:
Error
  • Object
show all
Defined in:
lib/mini_racer.rb,
ext/mini_racer_extension/mini_racer_extension.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2036

static VALUE context_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE kwargs, a, k, v;
    pthread_attr_t attr;
    const char *cause;
    pthread_t thr;
    Snapshot *ss;
    Context *c;
    char *s;
    int r;

    TypedData_Get_Struct(self, Context, &context_type, c);
    rb_scan_args(argc, argv, ":", &kwargs);
    if (NIL_P(kwargs))
        goto init;
    a = rb_ary_new();
    rb_hash_foreach(kwargs, collect, a);
    while (RARRAY_LENINT(a)) {
        v = rb_ary_pop(a);
        k = rb_ary_pop(a);
        k = rb_sym2str(k);
        s = RSTRING_PTR(k);
        if (!strcmp(s, "ensure_gc_after_idle")) {
            Check_Type(v, T_FIXNUM);
            c->idle_gc = FIX2LONG(v);
            if (c->idle_gc < 0 || c->idle_gc > INT32_MAX)
                rb_raise(rb_eArgError, "bad ensure_gc_after_idle");
        } else if (!strcmp(s, "max_memory")) {
            Check_Type(v, T_FIXNUM);
            c->max_memory = FIX2LONG(v);
            if (c->max_memory < 0 || c->max_memory >= UINT32_MAX)
                rb_raise(rb_eArgError, "bad max_memory");
        } else if (!strcmp(s, "marshal_stack_depth")) { // backcompat, ignored
            Check_Type(v, T_FIXNUM);
        } else if (!strcmp(s, "timeout")) {
            Check_Type(v, T_FIXNUM);
            c->timeout = FIX2LONG(v);
            if (c->timeout < 0 || c->timeout > INT32_MAX)
                rb_raise(rb_eArgError, "bad timeout");
        } else if (!strcmp(s, "snapshot")) {
            if (NIL_P(v))
                continue;
            TypedData_Get_Struct(v, Snapshot, &snapshot_type, ss);
            if (buf_put(&c->snapshot, RSTRING_PTR(ss->blob), RSTRING_LENINT(ss->blob)))
                rb_raise(runtime_error, "out of memory");
        } else if (!strcmp(s, "verbose_exceptions")) {
            c->verbose_exceptions = !(v == Qfalse || v == Qnil);
        } else if (!strcmp(s, "host_namespace")) {
            const char *ns = NULL;
            if (v == Qtrue) {
                ns = "MiniRacer"; // default brand, like Deno's `Deno`
            } else if (v != Qnil && v != Qfalse) {
                Check_Type(v, T_STRING);
                ns = StringValueCStr(v); // raises on embedded NUL
            }
            if (ns && *ns) {
                // The name becomes a global, so require a valid (ASCII) JS
                // identifier; otherwise it would only be reachable through
                // globalThis["..."] rather than as `<name>.method()`.
                for (const char *q = ns; *q; q++) {
                    int ch = (unsigned char)*q;
                    int ident_start = ch == '_' || ch == '$' ||
                        (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
                    int ident_char = ident_start || (ch >= '0' && ch <= '9');
                    if (!(q == ns ? ident_start : ident_char))
                        rb_raise(rb_eArgError,
                                 "host_namespace must be a valid identifier: %s", ns);
                }
                // store the name plus its NUL terminator
                buf_reset(&c->host_namespace);
                if (buf_put(&c->host_namespace, ns, strlen(ns) + 1))
                    rb_raise(runtime_error, "out of memory");
            }
        } else {
            rb_raise(runtime_error, "bad keyword: %s", s);
        }
    }
init:
    if (single_threaded) {
        v8_once_init();
        c->pst = v8_thread_init(c, c->snapshot.buf, c->snapshot.len, c->max_memory, c->verbose_exceptions,
                                c->host_namespace.len ? (const char *)c->host_namespace.buf : NULL);
    } else {
        cause = "pthread_attr_init";
        if ((r = pthread_attr_init(&attr)))
            goto fail;
        pthread_attr_setstacksize(&attr, 2<<20); // 2 MiB
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        // v8 thread takes ownership of |c|
        cause = "pthread_create";
        r = pthread_create(&thr, &attr, v8_thread_start, c);
        pthread_attr_destroy(&attr);
        if (r)
            goto fail;
        rb_thread_call_without_gvl(context_boot_wait, c, NULL, NULL);
    }
    // Deferred to first Context.new so Platform.set_flags! still has effect
    // on the tag (which depends on V8 flags applied during v8_global_init).
    {
        static int version_tag_defined;
        if (!version_tag_defined) {
            VALUE m = rb_const_get(rb_cObject, rb_intern("MiniRacer"));
            rb_define_const(m, "V8_CACHED_DATA_VERSION_TAG",
                            UINT2NUM(v8_cached_data_version_tag()));
            version_tag_defined = 1;
        }
    }
    return Qnil;
fail:
    rb_raise(runtime_error, "Context.initialize: %s: %s", cause, strerror(r));
    return Qnil; // pacify compiler
}

Class Method Details

.load(blob) ⇒ Object



2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2249

static VALUE snapshot_load(VALUE klass, VALUE blob)
{
    Snapshot *ss;
    VALUE self;

    Check_Type(blob, T_STRING);
    self = snapshot_alloc(klass);
    TypedData_Get_Struct(self, Snapshot, &snapshot_type, ss);
    ss->blob = rb_str_dup(blob);
    rb_enc_associate(ss->blob, rb_ascii8bit_encoding());
    ENC_CODERANGE_SET(ss->blob, ENC_CODERANGE_VALID);
    return self;
}

Instance Method Details

#attach(name, proc) ⇒ Object



1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1711

static VALUE context_attach(VALUE self, VALUE name, VALUE proc)
{
    Context *c;
    VALUE e;
    Ser s;

    TypedData_Get_Struct(self, Context, &context_type, c);
    // request is (A)ttach, [name, id] array
    ser_init1(&s, 'A');
    ser_array_begin(&s, 2);
    add_string(&s, name);
    ser_int(&s, RARRAY_LENINT(c->procs));
    ser_array_end(&s, 2);
    rb_ary_push(c->procs, proc);
    // response is an exception or undefined
    e = rendezvous(c, &s.b);
    handle_exception(e);
    return Qnil;
}

#cache_rejected?Boolean

Returns:

  • (Boolean)


2405
2406
2407
2408
2409
2410
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2405

static VALUE script_cache_rejected_p(VALUE self)
{
    Script *script;
    TypedData_Get_Struct(self, Script, &script_type, script);
    return script->cache_rejected ? Qtrue : Qfalse;
}

#cached_dataObject



2398
2399
2400
2401
2402
2403
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2398

static VALUE script_cached_data(VALUE self)
{
    Script *script;
    TypedData_Get_Struct(self, Script, &script_type, script);
    return script->cached_data;
}

#call(*args) ⇒ Object



1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1782

static VALUE context_call(int argc, VALUE *argv, VALUE self)
{
    VALUE name, args;
    VALUE a, e;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Context, &context_type, c);
    rb_scan_args(argc, argv, "1*", &name, &args);
    Check_Type(name, T_STRING);
    rb_ary_unshift(args, name);
    // request is (C)all, [name, args...] array
    ser_init1(&s, 'C');
    if (serialize(&s, args)) {
        ser_reset(&s);
        rb_raise(runtime_error, "Context.call: %s", s.err);
    }
    // response is [result, err] array
    a = rendezvous(c, &s.b); // takes ownership of |s.b|
    e = rb_ary_pop(a);
    handle_exception(e);
    return rb_ary_pop(a);
}

#compile(*args) ⇒ Object



2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2276

static VALUE context_compile(int argc, VALUE *argv, VALUE self)
{
    VALUE a, e, source, filename, cached_data, produce_cache, kwargs;
    VALUE script_v, result;
    Script *script;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Context, &context_type, c);
    rb_scan_args(argc, argv, "1:", &source, &kwargs);
    Check_Type(source, T_STRING);
    filename = Qnil;
    cached_data = Qnil;
    produce_cache = Qfalse;
    if (!NIL_P(kwargs)) {
        filename = rb_hash_aref(kwargs, ID2SYM(id_filename));
        cached_data = rb_hash_aref(kwargs, ID2SYM(id_cached_data));
        produce_cache = rb_hash_aref(kwargs, ID2SYM(id_produce_cache));
    }
    if (NIL_P(filename))
        filename = rb_str_new_cstr("<compile>");
    Check_Type(filename, T_STRING);
    if (!NIL_P(cached_data)) {
        Check_Type(cached_data, T_STRING);
        // Refuse non-binary encodings so a user reading a cache file without
        // 'rb' mode gets a clear error instead of mangled bytes flowing to V8.
        if (rb_enc_get(cached_data) != rb_ascii8bit_encoding())
            rb_raise(rb_eEncodingError,
                     "cached_data must be ASCII-8BIT (binary), got %s",
                     rb_enc_name(rb_enc_get(cached_data)));
    }
    ser_init1(&s, 'K');
    ser_array_begin(&s, 4);
    add_string(&s, filename);
    add_string(&s, source);
    if (NIL_P(cached_data)) {
        ser_null(&s);
    } else {
        ser_uint8array(&s, (const uint8_t *)RSTRING_PTR(cached_data),
                       RSTRING_LENINT(cached_data));
    }
    ser_bool(&s, RTEST(produce_cache));
    ser_array_end(&s, 4);
    a = rendezvous(c, &s.b);
    e = rb_ary_pop(a);
    handle_exception(e);
    result = rb_ary_pop(a);
    Check_Type(result, T_ARRAY);

    script_v = rb_obj_alloc(script_class); // skip the raising initialize
    TypedData_Get_Struct(script_v, Script, &script_type, script);
    script->context = self;
    script->handle_id = NUM2INT(rb_ary_entry(result, 0));
    script->cached_data = rb_ary_entry(result, 1);
    script->cache_rejected = RTEST(rb_ary_entry(result, 2));
    return script_v;
}

#compile_module(*args) ⇒ Object



2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2440

static VALUE context_compile_module(int argc, VALUE *argv, VALUE self)
{
    VALUE a, e, source, filename, cached_data, produce_cache, kwargs;
    VALUE module_v, result;
    Module *m;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    rb_scan_args(argc, argv, "1:", &source, &kwargs);
    Check_Type(source, T_STRING);
    filename = Qnil;
    cached_data = Qnil;
    produce_cache = Qfalse;
    if (!NIL_P(kwargs)) {
        filename = rb_hash_aref(kwargs, ID2SYM(id_filename));
        cached_data = rb_hash_aref(kwargs, ID2SYM(id_cached_data));
        produce_cache = rb_hash_aref(kwargs, ID2SYM(id_produce_cache));
    }
    if (NIL_P(filename))
        filename = rb_str_new_cstr("<compile_module>");
    Check_Type(filename, T_STRING);
    if (!NIL_P(cached_data)) {
        Check_Type(cached_data, T_STRING);
        // Refuse non-binary encodings so a user reading a cache file without
        // 'rb' mode gets a clear error instead of mangled bytes flowing to V8.
        if (rb_enc_get(cached_data) != rb_ascii8bit_encoding())
            rb_raise(rb_eEncodingError,
                     "cached_data must be ASCII-8BIT (binary), got %s",
                     rb_enc_name(rb_enc_get(cached_data)));
    }
    ser_init1(&s, 'O');
    ser_array_begin(&s, 4);
    add_string(&s, filename);
    add_string(&s, source);
    if (NIL_P(cached_data)) {
        ser_null(&s);
    } else {
        ser_uint8array(&s, (const uint8_t *)RSTRING_PTR(cached_data),
                       RSTRING_LENINT(cached_data));
    }
    ser_bool(&s, RTEST(produce_cache));
    ser_array_end(&s, 4);
    a = rendezvous(c, &s.b);
    e = rb_ary_pop(a);
    handle_exception(e);
    result = rb_ary_pop(a);
    Check_Type(result, T_ARRAY);

    module_v = rb_obj_alloc(module_class); // skip the raising initialize
    TypedData_Get_Struct(module_v, Module, &module_type, m);
    m->context = self;
    m->handle_id = NUM2INT(rb_ary_entry(result, 0));
    m->cached_data = rb_ary_entry(result, 1);
    m->cache_rejected = RTEST(rb_ary_entry(result, 2));
    return module_v;
}

#disposeObject



1760
1761
1762
1763
1764
1765
1766
1767
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1760

static VALUE context_dispose(VALUE self)
{
    Context *c;

    TypedData_Get_Struct(self, Context, &context_type, c);
    rb_thread_call_without_gvl(context_dispose_do, c, NULL, NULL);
    return Qnil;
}

#disposed?Boolean

Returns:

  • (Boolean)


2433
2434
2435
2436
2437
2438
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2433

static VALUE script_disposed_p(VALUE self)
{
    Script *script;
    TypedData_Get_Struct(self, Script, &script_type, script);
    return script->disposed ? Qtrue : Qfalse;
}

#dumpObject



2241
2242
2243
2244
2245
2246
2247
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2241

static VALUE snapshot_dump(VALUE self)
{
    Snapshot *ss;

    TypedData_Get_Struct(self, Snapshot, &snapshot_type, ss);
    return ss->blob;
}

#dynamic_import_resolverObject



2812
2813
2814
2815
2816
2817
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2812

static VALUE context_get_dynamic_import_resolver(VALUE self)
{
    Context *c;
    TypedData_Get_Struct(self, Context, &context_type, c);
    return c->dynamic_import_resolver;
}

#dynamic_import_resolver=(blk) ⇒ Object



2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2801

static VALUE context_set_dynamic_import_resolver(VALUE self, VALUE blk)
{
    Context *c;
    TypedData_Get_Struct(self, Context, &context_type, c);
    if (!NIL_P(blk) && !rb_respond_to(blk, rb_intern("call")))
        rb_raise(rb_eTypeError,
                 "dynamic_import_resolver must respond to #call or be nil");
    c->dynamic_import_resolver = blk;
    return blk;
}

#eval(*args) ⇒ Object



1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1806

static VALUE context_eval(int argc, VALUE *argv, VALUE self)
{
    VALUE a, e, source, filename, kwargs;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Context, &context_type, c);
    filename = Qnil;
    rb_scan_args(argc, argv, "1:", &source, &kwargs);
    Check_Type(source, T_STRING);
    if (!NIL_P(kwargs))
        filename = rb_hash_aref(kwargs, rb_id2sym(rb_intern("filename")));
    if (NIL_P(filename))
        filename = rb_str_new_cstr("<eval>");
    Check_Type(filename, T_STRING);
    // request is (E)val, [filename, source] array
    ser_init1(&s, 'E');
    ser_array_begin(&s, 2);
    add_string(&s, filename);
    add_string(&s, source);
    ser_array_end(&s, 2);
    // response is [result, errname] array
    a = rendezvous(c, &s.b); // takes ownership of |s.b|
    e = rb_ary_pop(a);
    handle_exception(e);
    return rb_ary_pop(a);
}

#evaluateObject



2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2700

static VALUE module_evaluate(VALUE self)
{
    VALUE a, e;
    Module *m;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Module, &module_type, m);
    if (m->disposed)
        rb_raise(runtime_error, "disposed module");
    TypedData_Get_Struct(m->context, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    ser_init1(&s, 'V');
    ser_int(&s, m->handle_id);
    a = rendezvous(c, &s.b);
    e = rb_ary_pop(a);
    handle_exception(e);
    return rb_ary_pop(a);
}

#heap_snapshotObject



1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1856

static VALUE context_heap_snapshot(VALUE self)
{
    Buf req, res;
    Context *c;

    TypedData_Get_Struct(self, Context, &context_type, c);
    buf_init(&req);
    buf_putc(&req, 'H');              // (H)eap snapshot, returns plain bytes
    rendezvous_no_des(c, &req, &res); // takes ownership of |req|
    return rb_utf8_str_new((char *)res.buf, res.len);
}

#heap_statsObject



1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1834

static VALUE context_heap_stats(VALUE self)
{
    VALUE a, h, k, v;
    Context *c;
    int i, n;
    Buf b;

    TypedData_Get_Struct(self, Context, &context_type, c);
    buf_init(&b);
    buf_putc(&b, 'S');     // (S)tats, returns object
    h = rendezvous(c, &b); // takes ownership of |b|
    a = rb_ary_new();
    rb_hash_foreach(h, collect, a);
    for (i = 0, n = RARRAY_LENINT(a); i < n; i += 2) {
        k = rb_ary_entry(a, i+0);
        v = rb_ary_entry(a, i+1);
        rb_hash_delete(h, k);
        rb_hash_aset(h, rb_str_intern(k), v); // turn "key" into :key
    }
    return h;
}

#instantiateObject



2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2581

static VALUE module_instantiate(VALUE self)
{
    VALUE block;
    Module *m;
    Context *c;
    struct instantiate_args ia;

    if (!rb_block_given_p())
        rb_raise(rb_eArgError, "Module#instantiate requires a resolver block");
    block = rb_block_proc();

    TypedData_Get_Struct(self, Module, &module_type, m);
    if (m->disposed)
        rb_raise(runtime_error, "disposed module");
    TypedData_Get_Struct(m->context, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");

    // Save the previous resolver slot so a re-entrant instantiate from
    // inside this block restores its caller's block on the way out.
    // rb_ensure guarantees restoration even when the resolver block
    // raises (without it, the slot would be left pointing at this call's
    // block and keep it GC-alive until something else overwrites it).
    ia.c = c;
    ia.handle_id = m->handle_id;
    ia.prev_block = c->resolve_block;
    c->resolve_block = block;
    rb_ensure(module_instantiate_body, (VALUE)&ia,
              module_instantiate_restore, (VALUE)&ia);
    return self;
}

#load_module_graph(*args) ⇒ Object

same Module instance instead of being recompiled.



2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2669

static VALUE context_load_module_graph(int argc, VALUE *argv, VALUE self)
{
    VALUE entry_url, kwargs, resolve, fetch_batch;
    Context *c;
    struct load_graph_args la;

    TypedData_Get_Struct(self, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    rb_scan_args(argc, argv, "1:", &entry_url, &kwargs);
    Check_Type(entry_url, T_STRING);
    resolve = fetch_batch = Qnil;
    if (!NIL_P(kwargs)) {
        resolve     = rb_hash_aref(kwargs, ID2SYM(rb_intern("resolve")));
        fetch_batch = rb_hash_aref(kwargs, ID2SYM(rb_intern("fetch_batch")));
    }
    if (!rb_respond_to(resolve, rb_intern("call")))
        rb_raise(rb_eArgError, "load_module_graph requires a resolve: callable");
    if (!rb_respond_to(fetch_batch, rb_intern("call")))
        rb_raise(rb_eArgError, "load_module_graph requires a fetch_batch: callable");

    // Persist the callbacks for the Context's lifetime so dynamic import() can
    // reuse them after this call returns (case A). A later load_module_graph
    // overwrites them; dispose frees them.
    c->graph_resolve_block = resolve;
    c->graph_fetch_block   = fetch_batch;
    la.c = c;
    la.entry_url = entry_url;
    return context_load_module_graph_body((VALUE)&la);
}

#low_memory_notificationObject

takes ownership of |b|



1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1910

static VALUE context_low_memory_notification(VALUE self)
{
    Buf req, res;
    Context *c;

    TypedData_Get_Struct(self, Context, &context_type, c);
    buf_init(&req);
    buf_putc(&req, 'L');              // (L)ow memory notification, returns nothing
    rendezvous_no_des(c, &req, &res); // takes ownership of |req|
    return Qnil;
}

#namespaceObject



2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2721

static VALUE module_namespace(VALUE self)
{
    VALUE a, e;
    Module *m;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Module, &module_type, m);
    if (m->disposed)
        rb_raise(runtime_error, "disposed module");
    TypedData_Get_Struct(m->context, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    ser_init1(&s, 'N');
    ser_int(&s, m->handle_id);
    a = rendezvous(c, &s.b);
    e = rb_ary_pop(a);
    handle_exception(e);
    return rb_ary_pop(a);
}

#perform_microtask_checkpointObject



1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1868

static VALUE context_perform_microtask_checkpoint(VALUE self)
{
    Context *c;
    Buf b;

    TypedData_Get_Struct(self, Context, &context_type, c);
    buf_init(&b);
    buf_putc(&b, 'M');        // (M)icrotask checkpoint, returns nil
    return rendezvous(c, &b); // takes ownership of |b|
}

#pump_message_loopObject



1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1899

static VALUE context_pump_message_loop(VALUE self)
{
    Context *c;
    Buf b;

    TypedData_Get_Struct(self, Context, &context_type, c);
    buf_init(&b);
    buf_putc(&b, 'P');        // (P)ump, returns bool
    return rendezvous(c, &b); // takes ownership of |b|
}

#reset_realmObject

and are invalidated by the reset. Refused from within a host callback.



1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1885

static VALUE context_reset_realm(VALUE self)
{
    Context *c;
    VALUE e;
    Buf b;

    TypedData_Get_Struct(self, Context, &context_type, c);
    buf_init(&b);
    buf_putc(&b, 'F');     // (F)resh realm, returns err or undefined
    e = rendezvous(c, &b); // takes ownership of |b|
    handle_exception(e);
    return Qnil;
}

#runObject



2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2377

static VALUE script_run(VALUE self)
{
    VALUE a, e;
    Script *script;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Script, &script_type, script);
    if (script->disposed)
        rb_raise(runtime_error, "disposed script");
    TypedData_Get_Struct(script->context, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    ser_init1(&s, 'R');
    ser_int(&s, script->handle_id);
    a = rendezvous(c, &s.b);
    e = rb_ary_pop(a);
    handle_exception(e);
    return rb_ary_pop(a);
}

#sizeObject



2263
2264
2265
2266
2267
2268
2269
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2263

static VALUE snapshot_size0(VALUE self)
{
    Snapshot *ss;

    TypedData_Get_Struct(self, Snapshot, &snapshot_type, ss);
    return LONG2FIX(RSTRING_LENINT(ss->blob));
}

#statusObject



2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2742

static VALUE module_status(VALUE self)
{
    VALUE a, e, result;
    Module *m;
    Context *c;
    Ser s;

    TypedData_Get_Struct(self, Module, &module_type, m);
    if (m->disposed)
        rb_raise(runtime_error, "disposed module");
    TypedData_Get_Struct(m->context, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    ser_init1(&s, 'U');
    ser_int(&s, m->handle_id);
    a = rendezvous(c, &s.b);
    e = rb_ary_pop(a);
    handle_exception(e);
    result = rb_ary_pop(a);
    // v8_module_status always replies with a String on success; a non-string
    // would mean the v8 thread fell through to the Undefined fail path with
    // a missing error (shouldn't happen, but check defensively rather than
    // crash rb_str_intern on Qnil).
    Check_Type(result, T_STRING);
    return rb_str_intern(result);
}

#stopObject



1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 1769

static VALUE context_stop(VALUE self)
{
    Context *c;

    // does not grab |mtx| because Context.stop can be called from another
    // thread and then we deadlock if e.g. the V8 thread busy-loops in JS
    TypedData_Get_Struct(self, Context, &context_type, c);
    if (atomic_load(&c->quit))
        rb_raise(context_disposed_error, "disposed context");
    v8_terminate_execution(c->pst);
    return Qnil;
}

#warmup!(arg) ⇒ Object



2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
# File 'ext/mini_racer_extension/mini_racer_extension.c', line 2210

static VALUE snapshot_warmup(VALUE self, VALUE arg)
{
    VALUE a, e, cv;
    Snapshot *ss;
    Context *c;
    DesCtx d;
    Ser s;

    TypedData_Get_Struct(self, Snapshot, &snapshot_type, ss);
    Check_Type(arg, T_STRING);
    cv = context_alloc(context_class);
    context_initialize(0, NULL, cv);
    TypedData_Get_Struct(cv, Context, &context_type, c);
    // request is (W)armup, [snapshot, "warmup code"]
    ser_init1(&s, 'W');
    ser_array_begin(&s, 2);
    ser_string8(&s, (const uint8_t *)RSTRING_PTR(ss->blob), RSTRING_LENINT(ss->blob));
    add_string(&s, arg);
    ser_array_end(&s, 2);
    // response is [arraybuffer, error]
    DesCtx_init(&d);
    d.transcode_latin1 = 0; // don't mangle snapshot binary data
    a = rendezvous1(c, &s.b, &d);
    e = rb_ary_pop(a);
    context_dispose(cv);
    if (*RSTRING_PTR(e))
        rb_raise(snapshot_error, "%s", RSTRING_PTR(e)+1);
    ss->blob = rb_ary_pop(a);
    return self;
}