mini_racer_rs 1.5.0 → 1.5.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 +4 -4
- data/.yardopts +7 -0
- data/CHANGELOG.md +22 -0
- data/Cargo.lock +7 -7
- data/Cargo.toml +2 -2
- data/README.md +25 -25
- data/crates/mini_racer_engine/src/context.rs +54 -0
- data/crates/mini_racer_engine/src/process/code_cache.rs +46 -2
- data/crates/mini_racer_engine/src/process/sources.rs +2 -10
- data/crates/mini_racer_engine/src/process/watchdog.rs +49 -8
- data/crates/mini_racer_engine/src/runner.rs +1 -0
- data/crates/mini_racer_engine/src/snapshot.rs +6 -3
- data/crates/mini_racer_engine/src/sys.rs +42 -0
- data/crates/mini_racer_engine/src/text.rs +182 -18
- data/crates/mini_racer_sync/src/channel.rs +135 -0
- data/crates/mini_racer_sync/src/fork.rs +22 -0
- data/crates/mini_racer_sync/src/termination.rs +27 -0
- data/ext/mini_racer_rs/src/gc_root.rs +8 -3
- data/ext/mini_racer_rs/src/host.rs +57 -30
- data/ext/mini_racer_rs/src/platform.rs +8 -3
- data/ext/mini_racer_rs/src/pool.rs +11 -7
- data/ext/mini_racer_rs/src/timeout.rs +14 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- metadata +7 -2
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
use std::sync::Arc;
|
|
2
|
+
|
|
3
|
+
use crate::process::sources::Units;
|
|
1
4
|
use crate::sys;
|
|
2
5
|
|
|
3
6
|
// V8 decodes shorter UTF-8 faster than a conversion starts: converting all of
|
|
@@ -10,6 +13,13 @@ const SHORT: usize = 16;
|
|
|
10
13
|
// stays within 24 MiB, and the values measured, up to 1 MB, all gain.
|
|
11
14
|
const LONG: usize = 8 << 20;
|
|
12
15
|
const WIDE_LEAD: u8 = 0xC4;
|
|
16
|
+
// simdutf needs room for a unit per byte it decodes, more than a source's
|
|
17
|
+
// units, sized exactly, have once it holds a character of two bytes or more,
|
|
18
|
+
// so each run is decoded into a buffer small enough to stay in cache and
|
|
19
|
+
// copied in.
|
|
20
|
+
const RUN: usize = 64 << 10;
|
|
21
|
+
const CONTINUATION_BITS: u8 = 0xC0;
|
|
22
|
+
const CONTINUATION: u8 = 0x80;
|
|
13
23
|
|
|
14
24
|
/// The characters of valid UTF-8 as V8 stores them once it has decoded it:
|
|
15
25
|
/// one byte each when every one is at most U+00FF, UTF-16 units otherwise.
|
|
@@ -43,11 +53,8 @@ impl Transcoder {
|
|
|
43
53
|
self.decode_long(text)
|
|
44
54
|
}
|
|
45
55
|
|
|
46
|
-
// One conversion each: in valid UTF-8 a byte from 0xC4 up starts a
|
|
47
|
-
// character past U+00FF, and text without one that does not convert to
|
|
48
|
-
// Latin-1 is not valid.
|
|
49
56
|
fn decode_long(&mut self, text: &[u8]) -> Option<Decoded<'_>> {
|
|
50
|
-
if text
|
|
57
|
+
if one_byte(text) {
|
|
51
58
|
grow(&mut self.one_byte, text.len());
|
|
52
59
|
let count = sys::utf8_to_latin1(text, &mut self.one_byte)?;
|
|
53
60
|
return self.one_byte.get(..count).map(Decoded::OneByte);
|
|
@@ -68,9 +75,88 @@ fn grow<T: Copy + Default>(buffer: &mut Vec<T>, len: usize) {
|
|
|
68
75
|
}
|
|
69
76
|
}
|
|
70
77
|
|
|
78
|
+
// In valid UTF-8 a byte from 0xC4 up starts a character past U+00FF, and text
|
|
79
|
+
// without one that does not convert to Latin-1 is not valid, so one scan picks
|
|
80
|
+
// the one conversion to run.
|
|
81
|
+
fn one_byte(text: &[u8]) -> bool {
|
|
82
|
+
text.iter().fold(0, |most, &byte| most.max(byte)) < WIDE_LEAD
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// Valid UTF-8 of any length decoded as `Transcoder::decode` decodes it, into
|
|
86
|
+
/// one shared allocation of exactly its units; `None` when it is not valid.
|
|
87
|
+
pub fn units(text: &[u8]) -> Option<Units> {
|
|
88
|
+
if one_byte(text) {
|
|
89
|
+
let len = v8::simdutf::latin1_length_from_utf8(text);
|
|
90
|
+
return filled(text, len, sys::utf8_to_latin1).map(Units::OneByte);
|
|
91
|
+
}
|
|
92
|
+
let len = v8::simdutf::utf16_length_from_utf8(text);
|
|
93
|
+
filled(text, len, sys::utf8_to_utf16).map(Units::TwoByte)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// `len` is simdutf's count of the units, exact only for valid UTF-8, which
|
|
97
|
+
// never decodes to more units than it has bytes. The units are written once,
|
|
98
|
+
// in place: decoded whole into a buffer and then copied into an `Arc`, the
|
|
99
|
+
// 88 MB of units of a 44 MB bundle took 104 ms against 37.
|
|
100
|
+
fn filled<T: Copy + Default>(
|
|
101
|
+
text: &[u8],
|
|
102
|
+
len: usize,
|
|
103
|
+
decode: fn(&[u8], &mut [T]) -> Option<usize>,
|
|
104
|
+
) -> Option<Arc<[T]>> {
|
|
105
|
+
if len > text.len() {
|
|
106
|
+
return None;
|
|
107
|
+
}
|
|
108
|
+
let mut units = sys::Filling::new(len);
|
|
109
|
+
let mut run = vec![T::default(); RUN.min(text.len())];
|
|
110
|
+
let mut rest = text;
|
|
111
|
+
while !rest.is_empty() {
|
|
112
|
+
let (head, tail) = rest.split_at(run_end(rest));
|
|
113
|
+
let count = decode(head, &mut run)?;
|
|
114
|
+
units.append(run.get(..count)?)?;
|
|
115
|
+
rest = tail;
|
|
116
|
+
}
|
|
117
|
+
units.filled()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Runs end where a character starts, so valid UTF-8 splits into runs each
|
|
121
|
+
// valid alone, and invalid UTF-8 leaves at least one run invalid. The search
|
|
122
|
+
// back stops after three continuation bytes: valid UTF-8 has no fourth, and
|
|
123
|
+
// the run after, opening with one, is refused.
|
|
124
|
+
fn run_end(text: &[u8]) -> usize {
|
|
125
|
+
if text.len() <= RUN {
|
|
126
|
+
return text.len();
|
|
127
|
+
}
|
|
128
|
+
let mut end = RUN;
|
|
129
|
+
while end > RUN - 3
|
|
130
|
+
&& text
|
|
131
|
+
.get(end)
|
|
132
|
+
.is_some_and(|&byte| byte & CONTINUATION_BITS == CONTINUATION)
|
|
133
|
+
{
|
|
134
|
+
end -= 1;
|
|
135
|
+
}
|
|
136
|
+
end
|
|
137
|
+
}
|
|
138
|
+
|
|
71
139
|
#[cfg(test)]
|
|
72
140
|
mod tests {
|
|
73
|
-
use super::{Decoded, LONG, SHORT, Transcoder};
|
|
141
|
+
use super::{Decoded, LONG, RUN, SHORT, Transcoder, units};
|
|
142
|
+
use crate::sys;
|
|
143
|
+
|
|
144
|
+
const INVALID: [&[u8]; 14] = [
|
|
145
|
+
b"\xff",
|
|
146
|
+
b"\x80",
|
|
147
|
+
b"\xc0\x80",
|
|
148
|
+
b"\xc1\xbf",
|
|
149
|
+
b"\xc3",
|
|
150
|
+
b"\xc2a",
|
|
151
|
+
b"\xed\xa0\x80",
|
|
152
|
+
b"\xed\xbf\xbf",
|
|
153
|
+
b"\xf4\x90\x80\x80",
|
|
154
|
+
b"\xf5\x80\x80\x80",
|
|
155
|
+
b"\xe0\x80\xaf",
|
|
156
|
+
b"\xf0\x9f\x98",
|
|
157
|
+
b"\xe6\x9d",
|
|
158
|
+
b"\xf0\x9f\x98\x80\x80",
|
|
159
|
+
];
|
|
74
160
|
|
|
75
161
|
// What V8 makes of valid UTF-8, worked out from its characters.
|
|
76
162
|
fn expected(text: &str) -> (Option<Vec<u8>>, Vec<u16>) {
|
|
@@ -159,19 +245,7 @@ mod tests {
|
|
|
159
245
|
fn invalid_or_short_text_is_left_to_v8() {
|
|
160
246
|
let mut transcoder = Transcoder::default();
|
|
161
247
|
let pad = b"the quick brown fox ";
|
|
162
|
-
for invalid in
|
|
163
|
-
&b"\xff"[..],
|
|
164
|
-
b"\x80",
|
|
165
|
-
b"\xc0\x80",
|
|
166
|
-
b"\xc3",
|
|
167
|
-
b"\xed\xa0\x80",
|
|
168
|
-
b"\xed\xbf\xbf",
|
|
169
|
-
b"\xf4\x90\x80\x80",
|
|
170
|
-
b"\xf5\x80\x80\x80",
|
|
171
|
-
b"\xe0\x80\xaf",
|
|
172
|
-
b"\xf0\x9f\x98",
|
|
173
|
-
b"\xe6\x9d",
|
|
174
|
-
] {
|
|
248
|
+
for invalid in INVALID {
|
|
175
249
|
for text in [[pad, invalid].concat(), [invalid, pad].concat()] {
|
|
176
250
|
assert!(transcoder.decode(&text).is_none(), "{text:x?}");
|
|
177
251
|
}
|
|
@@ -179,4 +253,94 @@ mod tests {
|
|
|
179
253
|
assert!(transcoder.decode("caf\u{e9}".as_bytes()).is_none());
|
|
180
254
|
assert!(transcoder.decode(b"").is_none());
|
|
181
255
|
}
|
|
256
|
+
|
|
257
|
+
const WIDE: [&str; 11] = [
|
|
258
|
+
"\u{e9}",
|
|
259
|
+
"\u{ff}",
|
|
260
|
+
"\u{100}",
|
|
261
|
+
"\u{7ff}",
|
|
262
|
+
"\u{800}",
|
|
263
|
+
"\u{feff}",
|
|
264
|
+
"\u{fffd}",
|
|
265
|
+
"\u{ffff}",
|
|
266
|
+
"\u{10000}",
|
|
267
|
+
"\u{1f600}",
|
|
268
|
+
"\u{10ffff}",
|
|
269
|
+
];
|
|
270
|
+
|
|
271
|
+
// Each width of character starting on every byte up to the end of a run
|
|
272
|
+
// and just past it, runs of one width shifted so they end inside one, and
|
|
273
|
+
// widths mixed, besides ASCII of a run, a byte more and two runs.
|
|
274
|
+
fn across_runs() -> Vec<String> {
|
|
275
|
+
let mut texts: Vec<String> = [RUN, RUN + 1, 2 * RUN].map(|len| "a".repeat(len)).into();
|
|
276
|
+
for wide in WIDE {
|
|
277
|
+
for before in 0..=4 {
|
|
278
|
+
texts.push(format!("{}{wide}bcd", "a".repeat(RUN - before)));
|
|
279
|
+
}
|
|
280
|
+
for shift in 0..4 {
|
|
281
|
+
let count = 2 * RUN / wide.len() + 1;
|
|
282
|
+
texts.push(format!("{}{}", "a".repeat(shift), wide.repeat(count)));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
let mut mixed = String::new();
|
|
286
|
+
for wide in WIDE.iter().cycle() {
|
|
287
|
+
if mixed.len() > 3 * RUN {
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
mixed.push('a');
|
|
291
|
+
mixed.push_str(wide);
|
|
292
|
+
}
|
|
293
|
+
texts.push(mixed);
|
|
294
|
+
texts
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Invalid UTF-8 just before the end of a run, across it and after it,
|
|
298
|
+
// ending the text or followed by Latin-1 or wider text, so either
|
|
299
|
+
// conversion meets it; text simdutf counts more units than bytes in; and
|
|
300
|
+
// a fourth continuation byte ending a run.
|
|
301
|
+
fn invalid_across_runs() -> Vec<Vec<u8>> {
|
|
302
|
+
let mut texts = Vec::new();
|
|
303
|
+
for invalid in INVALID {
|
|
304
|
+
for at in [0, RUN - 3, RUN - 2, RUN - 1, RUN, RUN + 1] {
|
|
305
|
+
for after in ["", "caf\u{e9} ", "caf\u{e9} \u{1f600} "] {
|
|
306
|
+
let mut text = vec![b'a'; at];
|
|
307
|
+
text.extend_from_slice(invalid);
|
|
308
|
+
text.extend_from_slice(after.repeat(RUN / 8).as_bytes());
|
|
309
|
+
texts.push(text);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
texts.push(vec![0xF0; 2 * RUN]);
|
|
314
|
+
let mut fourth = vec![b'a'; RUN - 4];
|
|
315
|
+
fourth.extend_from_slice(b"\xf0\x9f\x98\x80\x80");
|
|
316
|
+
fourth.extend_from_slice("\u{1f600}".repeat(RUN).as_bytes());
|
|
317
|
+
texts.push(fourth);
|
|
318
|
+
texts
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// A shared source reads as V8 reads the UTF-8 itself, which a context
|
|
322
|
+
// keeping its own copy hands `String::NewFromUtf8`: the same characters,
|
|
323
|
+
// stored one byte a character or two as V8 stores them.
|
|
324
|
+
#[test]
|
|
325
|
+
fn units_read_as_v8_reads_their_utf8() {
|
|
326
|
+
crate::process::ensure_initialized();
|
|
327
|
+
let mut isolate = v8::Isolate::new(v8::CreateParams::default());
|
|
328
|
+
let context = {
|
|
329
|
+
v8::scope!(let scope, &mut isolate);
|
|
330
|
+
let context = v8::Context::new(scope, v8::ContextOptions::default());
|
|
331
|
+
v8::Global::new(scope, context)
|
|
332
|
+
};
|
|
333
|
+
v8::scope_with_context!(let scope, &mut isolate, &context);
|
|
334
|
+
for (at, text) in across_runs().iter().enumerate() {
|
|
335
|
+
let units = units(text.as_bytes()).unwrap_or_else(|| panic!("text {at} refused"));
|
|
336
|
+
let shared = sys::external_string(scope, &units).expect("a string");
|
|
337
|
+
let own = v8::String::new_from_utf8(scope, text.as_bytes(), v8::NewStringType::Normal)
|
|
338
|
+
.expect("a string");
|
|
339
|
+
assert!(shared.strict_equals(own.into()), "text {at}");
|
|
340
|
+
assert_eq!(shared.is_onebyte(), own.is_onebyte(), "text {at}");
|
|
341
|
+
}
|
|
342
|
+
for (at, text) in invalid_across_runs().iter().enumerate() {
|
|
343
|
+
assert!(units(text).is_none(), "invalid text {at}");
|
|
344
|
+
}
|
|
345
|
+
}
|
|
182
346
|
}
|
|
@@ -477,6 +477,141 @@ mod tests {
|
|
|
477
477
|
}
|
|
478
478
|
}
|
|
479
479
|
|
|
480
|
+
// What a fork's takeover and `is_disposed` read of a channel.
|
|
481
|
+
#[derive(Debug, PartialEq, Eq)]
|
|
482
|
+
struct Seen {
|
|
483
|
+
idle: bool,
|
|
484
|
+
closed: bool,
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const IDLE: Seen = Seen {
|
|
488
|
+
idle: true,
|
|
489
|
+
closed: false,
|
|
490
|
+
};
|
|
491
|
+
const BUSY: Seen = Seen {
|
|
492
|
+
idle: false,
|
|
493
|
+
closed: false,
|
|
494
|
+
};
|
|
495
|
+
const SHUT: Seen = Seen {
|
|
496
|
+
idle: false,
|
|
497
|
+
closed: true,
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
fn seen(channel: &Channel<Text>) -> Seen {
|
|
501
|
+
Seen {
|
|
502
|
+
idle: channel.idle(),
|
|
503
|
+
closed: channel.is_closed(),
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
type Stage = (&'static str, fn(&Channel<Text>), Seen);
|
|
508
|
+
|
|
509
|
+
// An outermost request whose callback nests a request, one send or
|
|
510
|
+
// receive at a time, with what the channel reads as after each.
|
|
511
|
+
fn steps() -> [Stage; 12] {
|
|
512
|
+
[
|
|
513
|
+
(
|
|
514
|
+
"a request not yet received",
|
|
515
|
+
|channel| channel.request("outer").unwrap(),
|
|
516
|
+
BUSY,
|
|
517
|
+
),
|
|
518
|
+
(
|
|
519
|
+
"a request open",
|
|
520
|
+
|channel| assert_eq!(request(channel), Some("outer")),
|
|
521
|
+
BUSY,
|
|
522
|
+
),
|
|
523
|
+
(
|
|
524
|
+
"a callback not yet received",
|
|
525
|
+
|channel| channel.callback("ruby fn").unwrap(),
|
|
526
|
+
BUSY,
|
|
527
|
+
),
|
|
528
|
+
(
|
|
529
|
+
"a callback open",
|
|
530
|
+
|channel| assert_eq!(callback(channel), Some("ruby fn")),
|
|
531
|
+
BUSY,
|
|
532
|
+
),
|
|
533
|
+
(
|
|
534
|
+
"a nested request not yet received",
|
|
535
|
+
|channel| channel.request("inner").unwrap(),
|
|
536
|
+
BUSY,
|
|
537
|
+
),
|
|
538
|
+
(
|
|
539
|
+
"a nested request open",
|
|
540
|
+
|channel| assert_eq!(request(channel), Some("inner")),
|
|
541
|
+
BUSY,
|
|
542
|
+
),
|
|
543
|
+
(
|
|
544
|
+
"a nested response not yet received",
|
|
545
|
+
|channel| channel.respond("inner done").unwrap(),
|
|
546
|
+
BUSY,
|
|
547
|
+
),
|
|
548
|
+
(
|
|
549
|
+
"the callback open again",
|
|
550
|
+
|channel| assert_eq!(response(channel), Some("inner done")),
|
|
551
|
+
BUSY,
|
|
552
|
+
),
|
|
553
|
+
(
|
|
554
|
+
"a reply not yet received",
|
|
555
|
+
|channel| channel.reply("ruby fn done").unwrap(),
|
|
556
|
+
BUSY,
|
|
557
|
+
),
|
|
558
|
+
(
|
|
559
|
+
"the request open again",
|
|
560
|
+
|channel| assert_eq!(reply(channel), Some("ruby fn done")),
|
|
561
|
+
BUSY,
|
|
562
|
+
),
|
|
563
|
+
(
|
|
564
|
+
"a response not yet received",
|
|
565
|
+
|channel| channel.respond("outer done").unwrap(),
|
|
566
|
+
IDLE,
|
|
567
|
+
),
|
|
568
|
+
(
|
|
569
|
+
"the response received, as a claim finds it before sending",
|
|
570
|
+
|channel| assert_eq!(response(channel), Some("outer done")),
|
|
571
|
+
IDLE,
|
|
572
|
+
),
|
|
573
|
+
]
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
#[test]
|
|
577
|
+
fn only_rubys_turn_with_nothing_open_is_idle() {
|
|
578
|
+
let channel = Channel::<Text>::default();
|
|
579
|
+
assert_eq!(seen(&channel), IDLE, "nothing sent");
|
|
580
|
+
for (state, step, expected) in steps() {
|
|
581
|
+
step(&channel);
|
|
582
|
+
assert_eq!(seen(&channel), expected, "{state}");
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// `park_notified` holds its side's flag in the state word while `wait`
|
|
587
|
+
// runs, as a runner parked for its next request holds its own.
|
|
588
|
+
#[test]
|
|
589
|
+
fn a_side_waiting_for_its_turn_leaves_idle_alone() {
|
|
590
|
+
let channel = Channel::<Text>::default();
|
|
591
|
+
channel.park_notified(Side::V8, || {
|
|
592
|
+
assert_eq!(seen(&channel), IDLE, "a runner waiting for a request");
|
|
593
|
+
});
|
|
594
|
+
channel.request("eval").unwrap();
|
|
595
|
+
channel.park_notified(Side::Ruby, || {
|
|
596
|
+
assert_eq!(seen(&channel), BUSY, "Ruby waiting for a response");
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
#[test]
|
|
601
|
+
fn a_closed_channel_is_never_idle() {
|
|
602
|
+
for reached in 0..=steps().len() {
|
|
603
|
+
let channel = Channel::<Text>::default();
|
|
604
|
+
let mut state = "nothing sent";
|
|
605
|
+
for (name, step, _) in steps().into_iter().take(reached) {
|
|
606
|
+
step(&channel);
|
|
607
|
+
state = name;
|
|
608
|
+
}
|
|
609
|
+
assert!(!channel.is_closed(), "open with {state}");
|
|
610
|
+
channel.close();
|
|
611
|
+
assert_eq!(seen(&channel), SHUT, "closed with {state}");
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
480
615
|
#[test]
|
|
481
616
|
fn a_request_and_its_response_change_turns() {
|
|
482
617
|
let channel = Channel::<Text>::default();
|
|
@@ -114,3 +114,25 @@ impl ForkGate {
|
|
|
114
114
|
drop(pending);
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
+
|
|
118
|
+
#[cfg(test)]
|
|
119
|
+
#[cfg(not(loom))]
|
|
120
|
+
mod tests {
|
|
121
|
+
use super::*;
|
|
122
|
+
|
|
123
|
+
#[test]
|
|
124
|
+
fn every_phase_reads_back_as_it_was_set() {
|
|
125
|
+
let phase = RunnerPhase::default();
|
|
126
|
+
assert_eq!(phase.get(), Phase::Running);
|
|
127
|
+
for set in [
|
|
128
|
+
Phase::Deposited,
|
|
129
|
+
Phase::Parked,
|
|
130
|
+
Phase::ParkedForFork,
|
|
131
|
+
Phase::Exited,
|
|
132
|
+
Phase::Running,
|
|
133
|
+
] {
|
|
134
|
+
phase.set(set);
|
|
135
|
+
assert_eq!(phase.get(), set);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -904,6 +904,33 @@ mod tests {
|
|
|
904
904
|
assert_eq!(cell.begin(DispatchKind::Script), Err(TermError::Disposing));
|
|
905
905
|
}
|
|
906
906
|
|
|
907
|
+
// A forked child reads `inherited().disposing` where its parent reads
|
|
908
|
+
// `is_disposing`, so the two must agree throughout.
|
|
909
|
+
#[test]
|
|
910
|
+
fn a_dispose_shows_from_its_start_and_for_good() {
|
|
911
|
+
let (cell, _) = cell();
|
|
912
|
+
let disposing = |cell: &TermCell<Probe>| {
|
|
913
|
+
let disposing = cell.is_disposing();
|
|
914
|
+
assert_eq!(
|
|
915
|
+
cell.inherited().map(|inherited| inherited.disposing),
|
|
916
|
+
Some(disposing)
|
|
917
|
+
);
|
|
918
|
+
disposing
|
|
919
|
+
};
|
|
920
|
+
assert!(!disposing(&cell));
|
|
921
|
+
cell.request(Interrupt::Stop);
|
|
922
|
+
let id = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
923
|
+
cell.request(Interrupt::Timeout(id));
|
|
924
|
+
assert!(!disposing(&cell));
|
|
925
|
+
|
|
926
|
+
cell.dispose();
|
|
927
|
+
assert!(disposing(&cell));
|
|
928
|
+
assert_eq!(cell.end(id), Some(Halt::Disposed));
|
|
929
|
+
assert!(disposing(&cell));
|
|
930
|
+
assert_eq!(cell.begin(DispatchKind::Script), Err(TermError::Disposing));
|
|
931
|
+
assert!(disposing(&cell));
|
|
932
|
+
}
|
|
933
|
+
|
|
907
934
|
#[test]
|
|
908
935
|
fn one_outermost_dispatch_at_a_time() {
|
|
909
936
|
let (cell, _) = cell();
|
|
@@ -9,7 +9,7 @@ use magnus::{
|
|
|
9
9
|
Class, DataTypeFunctions, Error, Module, Object, RModule, Ruby, TryConvert, TypedData, Value,
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
use crate::rb_raw;
|
|
12
|
+
use crate::{rb_raw, timeout};
|
|
13
13
|
|
|
14
14
|
pub struct StaticRoot<T: ReprValue>(Lazy<T>);
|
|
15
15
|
|
|
@@ -181,8 +181,8 @@ impl Spill {
|
|
|
181
181
|
const SPAWNED: &str = "spawned_with";
|
|
182
182
|
|
|
183
183
|
/// Starts a Ruby thread that runs `run` with `value`, which the thread object
|
|
184
|
-
/// keeps alive until the thread ends
|
|
185
|
-
/// calls raise.
|
|
184
|
+
/// keeps alive until the thread ends, outside any `MiniRacerRs.timeout` scope
|
|
185
|
+
/// open where it was started. `run` must handle whatever its Ruby calls raise.
|
|
186
186
|
pub fn spawn<T: ReprValue + TryConvert>(
|
|
187
187
|
ruby: &Ruby,
|
|
188
188
|
value: T,
|
|
@@ -195,6 +195,11 @@ pub fn spawn<T: ReprValue + TryConvert>(
|
|
|
195
195
|
let thread = rb_raw::thread_create(
|
|
196
196
|
ruby,
|
|
197
197
|
Box::new(move |ruby: &Ruby| {
|
|
198
|
+
// A scope left open by the request that started the thread would
|
|
199
|
+
// otherwise bound its scripts, a context's setup block among them.
|
|
200
|
+
// An interrupt the call raises is dropped and `run` goes on: a
|
|
201
|
+
// pool thread left without running would leave its slot building.
|
|
202
|
+
let _ = timeout::leave_inherited(ruby);
|
|
198
203
|
if let Ok(value) = ruby.thread_current().ivar_get::<_, T>(SPAWNED) {
|
|
199
204
|
run(ruby, value);
|
|
200
205
|
}
|
|
@@ -113,7 +113,13 @@ impl<'r> RubyHost<'r> {
|
|
|
113
113
|
// the dispatch, even when the answer came in meanwhile. None means no
|
|
114
114
|
// notifier is to be had, so the thread waits instead.
|
|
115
115
|
fn wait_in_fiber(&mut self, wait: &WaitHandle<'_>) -> Option<WaitOutcome> {
|
|
116
|
-
let io = self.waiter_io(wait)
|
|
116
|
+
let io = match self.waiter_io(wait) {
|
|
117
|
+
Ok(io) => io?,
|
|
118
|
+
Err(error) => {
|
|
119
|
+
self.pending = Some(error);
|
|
120
|
+
return Some(WaitOutcome::Cancel);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
117
123
|
loop {
|
|
118
124
|
let mut raised = None;
|
|
119
125
|
wait.park_notified(|| raised = wait_readable(io).err())
|
|
@@ -130,34 +136,47 @@ impl<'r> RubyHost<'r> {
|
|
|
130
136
|
|
|
131
137
|
// Made once per context and fork epoch, and again if Ruby code closed it;
|
|
132
138
|
// the descriptor is Ruby's own copy, so closing it never touches the
|
|
133
|
-
// engine's.
|
|
134
|
-
|
|
139
|
+
// engine's. An error is an interrupt raised as one of its calls returned,
|
|
140
|
+
// which the dispatch raises, as it does one raised in the wait itself.
|
|
141
|
+
fn waiter_io(&self, wait: &WaitHandle<'_>) -> Result<Option<Value>, Error> {
|
|
135
142
|
let ruby = self.ruby;
|
|
136
|
-
let waiter = self.waiter
|
|
143
|
+
let Some(waiter) = self.waiter else {
|
|
144
|
+
return Ok(None);
|
|
145
|
+
};
|
|
137
146
|
let epoch = fork::epoch();
|
|
138
147
|
if let Some(io) = waiter.io.get(ruby)
|
|
139
148
|
&& waiter.epoch.get() == epoch
|
|
140
|
-
&& !io.funcall::<_, _, bool>("closed?", ())
|
|
149
|
+
&& !io.funcall::<_, _, bool>("closed?", ())?
|
|
141
150
|
{
|
|
142
|
-
return Some(io);
|
|
151
|
+
return Ok(Some(io));
|
|
143
152
|
}
|
|
144
|
-
let fd = wait.notifier()
|
|
153
|
+
let Ok(fd) = wait.notifier() else {
|
|
154
|
+
return Ok(None);
|
|
155
|
+
};
|
|
156
|
+
// An interrupt can end `for_fd` after Ruby made the IO, which this
|
|
157
|
+
// then drops along with closing the descriptor; made without
|
|
158
|
+
// autoclose, that IO never closes the number once it is reused.
|
|
145
159
|
let io: Value = ruby
|
|
146
160
|
.class_io()
|
|
147
|
-
.funcall("for_fd", (fd.as_raw_fd(), kwargs!("autoclose" =>
|
|
148
|
-
.ok()?;
|
|
161
|
+
.funcall("for_fd", (fd.as_raw_fd(), kwargs!("autoclose" => false)))?;
|
|
149
162
|
let _owned_by_io = fd.into_raw_fd();
|
|
150
163
|
waiter.io.set(ruby, io);
|
|
151
164
|
waiter.epoch.set(epoch);
|
|
152
|
-
|
|
165
|
+
io.funcall::<_, _, Value>("autoclose=", (true,))?;
|
|
166
|
+
Ok(Some(io))
|
|
153
167
|
}
|
|
154
168
|
|
|
155
169
|
// Keeps the first exception the scheduler raises for after the dispatch,
|
|
156
170
|
// then leaves the rest of the wait to the thread, so a scheduler that
|
|
157
171
|
// keeps raising cannot spin here.
|
|
158
172
|
fn wait_blocking_in_fiber(&mut self, wait: &WaitHandle<'_>) {
|
|
159
|
-
let
|
|
160
|
-
|
|
173
|
+
let io = match self.waiter_io(wait) {
|
|
174
|
+
Ok(Some(io)) => io,
|
|
175
|
+
Ok(None) => return,
|
|
176
|
+
Err(error) => {
|
|
177
|
+
self.pending.get_or_insert(error);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
161
180
|
};
|
|
162
181
|
while !wait.ready() {
|
|
163
182
|
let mut raised = None;
|
|
@@ -226,21 +245,23 @@ fn exception(ruby: &Ruby, error: &Error) -> Option<Exception> {
|
|
|
226
245
|
}
|
|
227
246
|
|
|
228
247
|
// What V8 throws for a raising callback: the exception's to_s, in its own
|
|
229
|
-
// encoding, or mini_racer's fallback text. A callback
|
|
230
|
-
// stack has no room left to call to_s, so its text is
|
|
231
|
-
// SystemStackError with, as mini_racer's callbacks hand
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
.ok()
|
|
236
|
-
.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
248
|
+
// encoding, or mini_racer's fallback text when to_s raises. A callback
|
|
249
|
+
// refused for want of stack has no room left to call to_s, so its text is
|
|
250
|
+
// the one Ruby raises SystemStackError with, as mini_racer's callbacks hand
|
|
251
|
+
// JavaScript. A throw or Thread#kill out of to_s comes back as the error.
|
|
252
|
+
fn message(ruby: &Ruby, exception: Exception) -> Result<JsText, Error> {
|
|
253
|
+
let text = match exception.funcall::<_, _, RString>("to_s", ()) {
|
|
254
|
+
Ok(text) => context::js_text(ruby, text).ok(),
|
|
255
|
+
Err(error) if matches!(error.error_type(), ErrorType::Jump(_)) => return Err(error),
|
|
256
|
+
Err(_) => None,
|
|
257
|
+
};
|
|
258
|
+
Ok(text.unwrap_or_else(|| {
|
|
259
|
+
if exception.is_kind_of(ruby.exception_sys_stack_error()) {
|
|
260
|
+
JsText::from(errors::STACK_EXHAUSTED)
|
|
261
|
+
} else {
|
|
262
|
+
JsText::from("Ruby exception")
|
|
263
|
+
}
|
|
264
|
+
}))
|
|
244
265
|
}
|
|
245
266
|
|
|
246
267
|
impl Host for RubyHost<'_> {
|
|
@@ -326,8 +347,8 @@ impl Host for RubyHost<'_> {
|
|
|
326
347
|
}
|
|
327
348
|
}
|
|
328
349
|
|
|
329
|
-
// A throw or Thread#kill out of the callable
|
|
330
|
-
// resumes once it is over.
|
|
350
|
+
// A throw or Thread#kill out of the callable, or out of the to_s of what
|
|
351
|
+
// it raised, aborts the dispatch and resumes once it is over.
|
|
331
352
|
fn callback(&mut self, request: &[u8]) -> CallbackReply {
|
|
332
353
|
let error = match self.run(request) {
|
|
333
354
|
Ok(reply) => return CallbackReply::Value(reply),
|
|
@@ -337,7 +358,13 @@ impl Host for RubyHost<'_> {
|
|
|
337
358
|
self.pending.get_or_insert(error);
|
|
338
359
|
return CallbackReply::Abort;
|
|
339
360
|
};
|
|
340
|
-
let message = message(self.ruby, exception)
|
|
361
|
+
let message = match message(self.ruby, exception) {
|
|
362
|
+
Ok(message) => message,
|
|
363
|
+
Err(jump) => {
|
|
364
|
+
self.pending.get_or_insert(jump);
|
|
365
|
+
return CallbackReply::Abort;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
341
368
|
if let Some(callbacks) = &self.callbacks {
|
|
342
369
|
callbacks.raised.set(self.ruby, exception.as_value());
|
|
343
370
|
}
|
|
@@ -16,12 +16,17 @@ fn set_flags(ruby: &Ruby, args: &[Value]) -> Result<(), Error> {
|
|
|
16
16
|
for name in args.splat {
|
|
17
17
|
push(ruby, name, None)?;
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
// Each `to_s` can collect and compact, so the pairs wait in a Ruby array,
|
|
20
|
+
// which the GC sees and updates, rather than in Rust storage.
|
|
21
|
+
let pairs = ruby.ary_new_capa(args.keywords.len().saturating_mul(2));
|
|
20
22
|
args.keywords.foreach(|name: Value, value: Value| {
|
|
21
|
-
pairs.push(
|
|
23
|
+
pairs.push(name)?;
|
|
24
|
+
pairs.push(value)?;
|
|
22
25
|
Ok(magnus::r_hash::ForEach::Continue)
|
|
23
26
|
})?;
|
|
24
|
-
|
|
27
|
+
while !pairs.is_empty() {
|
|
28
|
+
let name: Value = pairs.shift()?;
|
|
29
|
+
let value: Value = pairs.shift()?;
|
|
25
30
|
push(ruby, name, Some(value))?;
|
|
26
31
|
}
|
|
27
32
|
Ok(())
|
|
@@ -791,13 +791,17 @@ impl Pool {
|
|
|
791
791
|
}
|
|
792
792
|
}
|
|
793
793
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
794
|
+
// A push can switch threads, and a wait not yet pushed that then
|
|
795
|
+
// ends leaves its queue referenced by nothing else, so the queues
|
|
796
|
+
// stay rooted in `waiting` until the array holds them, as in
|
|
797
|
+
// `wake_owed`. The mark shares this borrow.
|
|
798
|
+
let queues = ruby.ary_new_capa(rb_self.waiting.borrow().len());
|
|
799
|
+
for waiter in rb_self.waiting.borrow().iter() {
|
|
800
|
+
if let Some(queue) = waiter.queue.get(ruby) {
|
|
801
|
+
queues.push(queue)?;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
while let Some(queue) = queues.shift::<Option<Value>>()? {
|
|
801
805
|
queue.funcall::<_, _, Value>("push", (true,))?;
|
|
802
806
|
}
|
|
803
807
|
rb_self.wake_owed(ruby)?;
|
|
@@ -93,6 +93,20 @@ pub fn current(ruby: &Ruby) -> Result<Option<Duration>, Error> {
|
|
|
93
93
|
.and_then(|scope| scope.0.budget()))
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/// Takes the scope out of the current fiber's storage, for a thread the
|
|
97
|
+
/// extension starts, which serves no request and starts with a copy of its
|
|
98
|
+
/// creator's storage. Ruby removes it before it checks for interrupts as the
|
|
99
|
+
/// call returns, so an interrupt raised then leaves the fiber outside it too.
|
|
100
|
+
pub fn leave_inherited(ruby: &Ruby) -> Result<(), Error> {
|
|
101
|
+
if OPEN.load(Ordering::Relaxed) == 0 {
|
|
102
|
+
return Ok(());
|
|
103
|
+
}
|
|
104
|
+
FIBER
|
|
105
|
+
.get(ruby)
|
|
106
|
+
.funcall::<_, _, Value>(*ASET, (*KEY, ruby.qnil()))
|
|
107
|
+
.map(drop)
|
|
108
|
+
}
|
|
109
|
+
|
|
96
110
|
// The scope lives in fiber storage, which fibers and threads started inside
|
|
97
111
|
// the block inherit; a copy that outlives the block finds the scope closed.
|
|
98
112
|
fn timeout(ruby: &Ruby, milliseconds: Value) -> Result<Value, Error> {
|