mini_racer_rs 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/COMPATIBILITY.md +9 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +42 -0
- data/crates/mini_racer_engine/src/context.rs +44 -16
- data/crates/mini_racer_engine/src/control.rs +70 -1
- data/crates/mini_racer_engine/src/error.rs +40 -0
- data/crates/mini_racer_engine/src/lib.rs +3 -2
- data/crates/mini_racer_engine/src/process/platform.rs +360 -2
- data/crates/mini_racer_engine/src/process/watchdog.rs +63 -14
- data/crates/mini_racer_engine/src/runner.rs +274 -50
- data/crates/mini_racer_engine/src/sys.rs +26 -0
- data/crates/mini_racer_sourcemap/src/map.rs +3 -2
- data/crates/mini_racer_sync/src/termination.rs +182 -44
- data/ext/mini_racer_rs/src/context.rs +41 -23
- data/ext/mini_racer_rs/src/errors.rs +21 -6
- data/ext/mini_racer_rs/src/gc_root.rs +31 -1
- data/ext/mini_racer_rs/src/heap_limit.rs +42 -0
- data/ext/mini_racer_rs/src/lib.rs +6 -0
- data/ext/mini_racer_rs/src/options.rs +89 -7
- data/ext/mini_racer_rs/src/pool.rs +811 -0
- data/ext/mini_racer_rs/src/rb_raw.rs +186 -1
- data/ext/mini_racer_rs/src/timeout.rs +132 -0
- data/lib/mini_racer_rs/heap_limit_error.rb +7 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- data/lib/mini_racer_rs.rb +1 -0
- metadata +5 -1
|
@@ -38,12 +38,10 @@ struct CauseSet(u8);
|
|
|
38
38
|
|
|
39
39
|
impl CauseSet {
|
|
40
40
|
// The causes that outlive the dispatch they arrived in, as upstream's
|
|
41
|
-
// non-watchdog termination bit does.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
| TerminationCause::HeapExhausted.bit(),
|
|
46
|
-
);
|
|
41
|
+
// non-watchdog termination bit does. An exhausted heap has no counterpart
|
|
42
|
+
// there and ends with its dispatch: the runner settles the heap once the
|
|
43
|
+
// dispatch is done, so a later script has nothing of it to report.
|
|
44
|
+
const PENDING: Self = Self(TerminationCause::Stop.bit() | TerminationCause::MaxMemory.bit());
|
|
47
45
|
|
|
48
46
|
// The causes upstream records in its request flag beside the one-shot
|
|
49
47
|
// interrupt, which V8 can use up while the flag stays.
|
|
@@ -127,7 +125,8 @@ impl DispatchKind {
|
|
|
127
125
|
}
|
|
128
126
|
|
|
129
127
|
/// A request to terminate. Timeouts and cancellations name the dispatch they
|
|
130
|
-
/// belong to and are dropped if it has already ended
|
|
128
|
+
/// belong to and are dropped if it has already ended; an exhausted heap is
|
|
129
|
+
/// dropped when none is armed.
|
|
131
130
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
132
131
|
pub enum Interrupt {
|
|
133
132
|
Stop,
|
|
@@ -198,6 +197,14 @@ struct Armed {
|
|
|
198
197
|
terminated: bool,
|
|
199
198
|
// `settle` has given the verdict, and the result is being serialized.
|
|
200
199
|
settled: bool,
|
|
200
|
+
// Its own timeout fired, which a nested script's must not take away.
|
|
201
|
+
timed_out: bool,
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#[derive(Clone, Copy)]
|
|
205
|
+
struct NestedTimer {
|
|
206
|
+
id: DispatchId,
|
|
207
|
+
fired: bool,
|
|
201
208
|
}
|
|
202
209
|
|
|
203
210
|
struct State<T> {
|
|
@@ -212,8 +219,9 @@ struct State<T> {
|
|
|
212
219
|
requests: u64,
|
|
213
220
|
disposing: bool,
|
|
214
221
|
suspended: bool,
|
|
215
|
-
//
|
|
216
|
-
|
|
222
|
+
// Nested scripts timed on their own, innermost last, each ending before
|
|
223
|
+
// the ones it is nested in.
|
|
224
|
+
nested: Vec<NestedTimer>,
|
|
217
225
|
next: u64,
|
|
218
226
|
}
|
|
219
227
|
|
|
@@ -225,7 +233,7 @@ impl<T: Terminator> State<T> {
|
|
|
225
233
|
}
|
|
226
234
|
self.cancel_armed();
|
|
227
235
|
self.suspended = false;
|
|
228
|
-
self.
|
|
236
|
+
self.nested.clear();
|
|
229
237
|
self.armed = None;
|
|
230
238
|
if self.disposing {
|
|
231
239
|
return Some(Halt::Disposed);
|
|
@@ -276,7 +284,7 @@ impl<T: Terminator> State<T> {
|
|
|
276
284
|
}
|
|
277
285
|
self.cancel_armed();
|
|
278
286
|
self.suspended = false;
|
|
279
|
-
self.
|
|
287
|
+
self.nested.clear();
|
|
280
288
|
let armed = self.armed.take()?;
|
|
281
289
|
if self.disposing {
|
|
282
290
|
return Some(Halt::Disposed);
|
|
@@ -308,7 +316,7 @@ impl<T> Default for TermCell<T> {
|
|
|
308
316
|
requests: 0,
|
|
309
317
|
disposing: false,
|
|
310
318
|
suspended: false,
|
|
311
|
-
|
|
319
|
+
nested: Vec::new(),
|
|
312
320
|
next: 0,
|
|
313
321
|
}),
|
|
314
322
|
}
|
|
@@ -345,6 +353,7 @@ impl<T: Terminator> TermCell<T> {
|
|
|
345
353
|
kind,
|
|
346
354
|
terminated: false,
|
|
347
355
|
settled: false,
|
|
356
|
+
timed_out: false,
|
|
348
357
|
});
|
|
349
358
|
if !state.live().is_empty() {
|
|
350
359
|
state.terminate_armed();
|
|
@@ -354,13 +363,25 @@ impl<T: Terminator> TermCell<T> {
|
|
|
354
363
|
}
|
|
355
364
|
|
|
356
365
|
/// Records `interrupt` and terminates the armed dispatch. Returns false
|
|
357
|
-
/// when the interrupt names a dispatch that is no longer armed
|
|
366
|
+
/// when the interrupt names a dispatch that is no longer armed, or is an
|
|
367
|
+
/// exhausted heap with none armed, which has no script to end.
|
|
358
368
|
pub fn request(&self, interrupt: Interrupt) -> bool {
|
|
359
369
|
let mut state = lock(&self.state);
|
|
360
|
-
if let Some(id) = interrupt.dispatch()
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
370
|
+
if let Some(id) = interrupt.dispatch() {
|
|
371
|
+
let timeout = matches!(interrupt, Interrupt::Timeout(_));
|
|
372
|
+
if let Some(armed) = state.armed.as_mut().filter(|armed| armed.id == id) {
|
|
373
|
+
armed.timed_out |= timeout;
|
|
374
|
+
} else if let Some(timer) = state
|
|
375
|
+
.nested
|
|
376
|
+
.iter_mut()
|
|
377
|
+
.find(|timer| timeout && timer.id == id)
|
|
378
|
+
{
|
|
379
|
+
timer.fired = true;
|
|
380
|
+
} else {
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if interrupt == Interrupt::HeapExhausted && state.armed.is_none() {
|
|
364
385
|
return false;
|
|
365
386
|
}
|
|
366
387
|
state.causes = state.causes.with(interrupt.cause());
|
|
@@ -488,34 +509,36 @@ impl<T: Terminator> TermCell<T> {
|
|
|
488
509
|
}
|
|
489
510
|
}
|
|
490
511
|
|
|
491
|
-
/// Gives a script
|
|
492
|
-
///
|
|
493
|
-
///
|
|
494
|
-
///
|
|
495
|
-
///
|
|
512
|
+
/// Gives a nested script a timeout of its own: an id the timeout names.
|
|
513
|
+
/// The caller asks only when nothing around the script ends it sooner: a
|
|
514
|
+
/// script nested in an untimed dispatch (a pump or a microtask checkpoint,
|
|
515
|
+
/// through a callback), as `mini_racer` arms its watchdog for any script
|
|
516
|
+
/// when none is armed, or one given a shorter timeout than every script
|
|
517
|
+
/// around it. `None` when nothing is armed.
|
|
496
518
|
pub fn time_nested(&self) -> Option<DispatchId> {
|
|
497
519
|
let mut state = lock(&self.state);
|
|
498
|
-
|
|
499
|
-
if armed.kind == DispatchKind::Script || state.nested_timer.is_some() {
|
|
500
|
-
return None;
|
|
501
|
-
}
|
|
520
|
+
state.armed?;
|
|
502
521
|
let id = DispatchId(state.next);
|
|
503
522
|
state.next = state.next.wrapping_add(1);
|
|
504
|
-
state.
|
|
523
|
+
state.nested.push(NestedTimer { id, fired: false });
|
|
505
524
|
drop(state);
|
|
506
525
|
Some(id)
|
|
507
526
|
}
|
|
508
527
|
|
|
509
528
|
/// Ends a nested script's own timeout. One that fired is dropped, and the
|
|
510
529
|
/// dispatch around the script carries on unless another cause needs the
|
|
511
|
-
/// termination, as `mini_racer` lifts its watchdog's.
|
|
530
|
+
/// termination, as `mini_racer` lifts its watchdog's. The armed
|
|
531
|
+
/// dispatch's own timeout, or an enclosing nested script's, once fired,
|
|
532
|
+
/// still needs it.
|
|
512
533
|
pub fn untime_nested(&self, id: DispatchId) {
|
|
513
534
|
let mut state = lock(&self.state);
|
|
514
|
-
|
|
535
|
+
let Some(index) = state.nested.iter().position(|timer| timer.id == id) else {
|
|
515
536
|
return;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
|
|
537
|
+
};
|
|
538
|
+
let timer = state.nested.remove(index);
|
|
539
|
+
let others = state.armed.is_some_and(|armed| armed.timed_out)
|
|
540
|
+
|| state.nested.iter().any(|timer| timer.fired);
|
|
541
|
+
if timer.fired && !others && state.causes.contains(TerminationCause::Timeout) {
|
|
519
542
|
let causes = state.causes.without(TerminationCause::Timeout);
|
|
520
543
|
state.set_causes(causes);
|
|
521
544
|
if state.causes.is_empty() && !state.disposing {
|
|
@@ -588,9 +611,9 @@ impl<T: Terminator> TermCell<T> {
|
|
|
588
611
|
|
|
589
612
|
/// Disarms `id` once its reply is complete. After `settle`, a cause that
|
|
590
613
|
/// landed while the result serialized, without stopping it, leaves the
|
|
591
|
-
/// result alone: a timeout or
|
|
592
|
-
/// cause waits for the next script, as `mini_racer`
|
|
593
|
-
/// that was never settled ends as `end` ends it.
|
|
614
|
+
/// result alone: a timeout, cancel or exhausted heap is dropped, and a
|
|
615
|
+
/// stop or `max_memory` cause waits for the next script, as `mini_racer`
|
|
616
|
+
/// leaves them. A dispatch that was never settled ends as `end` ends it.
|
|
594
617
|
pub fn finish(&self, id: DispatchId) -> Option<Halt> {
|
|
595
618
|
lock(&self.state).finish(id)
|
|
596
619
|
}
|
|
@@ -648,6 +671,40 @@ mod tests {
|
|
|
648
671
|
}
|
|
649
672
|
|
|
650
673
|
const STOPPED: Option<Halt> = Some(Halt::Terminated(TerminationCause::Stop));
|
|
674
|
+
const EXHAUSTED: Option<Halt> = Some(Halt::Terminated(TerminationCause::HeapExhausted));
|
|
675
|
+
|
|
676
|
+
// Whatever kind of dispatch the heap filled up in, and however it ended,
|
|
677
|
+
// the next script runs; with none armed, it is not recorded at all.
|
|
678
|
+
#[test]
|
|
679
|
+
fn an_exhausted_heap_ends_with_its_dispatch() {
|
|
680
|
+
let (cell, calls) = cell();
|
|
681
|
+
assert!(!cell.request(Interrupt::HeapExhausted));
|
|
682
|
+
assert_eq!(calls.terminate.get(), 0);
|
|
683
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
684
|
+
let id = cell
|
|
685
|
+
.begin(DispatchKind::Maintenance)
|
|
686
|
+
.expect("nothing armed");
|
|
687
|
+
cell.request(Interrupt::HeapExhausted);
|
|
688
|
+
assert_eq!(calls.terminate.get(), 1);
|
|
689
|
+
assert_eq!(cell.check(DispatchKind::Script), EXHAUSTED);
|
|
690
|
+
assert_eq!(cell.end(id), None);
|
|
691
|
+
assert!(!cell.refuses(DispatchKind::Script));
|
|
692
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
693
|
+
|
|
694
|
+
let id = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
695
|
+
assert_eq!(cell.settle(id), None);
|
|
696
|
+
cell.request(Interrupt::HeapExhausted);
|
|
697
|
+
assert_eq!(cell.finish(id), None);
|
|
698
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
699
|
+
|
|
700
|
+
let id = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
701
|
+
cell.request(Interrupt::Stop);
|
|
702
|
+
assert_eq!(cell.settle(id), STOPPED);
|
|
703
|
+
cell.request(Interrupt::HeapExhausted);
|
|
704
|
+
assert_eq!(cell.check(DispatchKind::Script), EXHAUSTED);
|
|
705
|
+
assert_eq!(cell.end(id), EXHAUSTED);
|
|
706
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
707
|
+
}
|
|
651
708
|
|
|
652
709
|
#[test]
|
|
653
710
|
fn a_stop_while_idle_is_reported_by_the_next_script_only() {
|
|
@@ -946,7 +1003,6 @@ mod tests {
|
|
|
946
1003
|
let (cell, calls) = cell();
|
|
947
1004
|
let pump = cell.begin(DispatchKind::Pump).expect("nothing armed");
|
|
948
1005
|
let nested = cell.time_nested().expect("an untimed dispatch");
|
|
949
|
-
assert_eq!(cell.time_nested(), None);
|
|
950
1006
|
|
|
951
1007
|
assert!(cell.request(Interrupt::Timeout(nested)));
|
|
952
1008
|
assert_eq!(calls.terminate.get(), 1);
|
|
@@ -979,14 +1035,96 @@ mod tests {
|
|
|
979
1035
|
}
|
|
980
1036
|
|
|
981
1037
|
#[test]
|
|
982
|
-
fn
|
|
983
|
-
let (cell,
|
|
984
|
-
let
|
|
985
|
-
|
|
986
|
-
|
|
1038
|
+
fn a_script_nested_in_a_script_can_be_timed_on_its_own() {
|
|
1039
|
+
let (cell, calls) = cell();
|
|
1040
|
+
let outer = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
1041
|
+
let nested = cell.time_nested().expect("a dispatch is armed");
|
|
1042
|
+
|
|
1043
|
+
assert!(cell.request(Interrupt::Timeout(nested)));
|
|
1044
|
+
assert_eq!(
|
|
1045
|
+
cell.check(DispatchKind::Script),
|
|
1046
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1047
|
+
);
|
|
1048
|
+
cell.untime_nested(nested);
|
|
1049
|
+
assert_eq!(calls.cancel.get(), 1);
|
|
1050
|
+
assert_eq!(cell.end(outer), None);
|
|
987
1051
|
assert_eq!(cell.time_nested(), None);
|
|
988
1052
|
}
|
|
989
1053
|
|
|
1054
|
+
#[test]
|
|
1055
|
+
fn a_nested_timeout_leaves_the_outer_scripts_own() {
|
|
1056
|
+
let (cell, calls) = cell();
|
|
1057
|
+
let outer = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
1058
|
+
let nested = cell.time_nested().expect("a dispatch is armed");
|
|
1059
|
+
cell.request(Interrupt::Timeout(nested));
|
|
1060
|
+
cell.request(Interrupt::Timeout(outer));
|
|
1061
|
+
|
|
1062
|
+
cell.untime_nested(nested);
|
|
1063
|
+
assert_eq!(calls.cancel.get(), 0);
|
|
1064
|
+
assert_eq!(
|
|
1065
|
+
cell.end(outer),
|
|
1066
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
#[test]
|
|
1071
|
+
fn nested_timers_stack_and_each_drops_only_its_own_timeout() {
|
|
1072
|
+
let (cell, calls) = cell();
|
|
1073
|
+
let pump = cell.begin(DispatchKind::Pump).expect("nothing armed");
|
|
1074
|
+
let enclosing = cell.time_nested().expect("a dispatch is armed");
|
|
1075
|
+
let inner = cell.time_nested().expect("a dispatch is armed");
|
|
1076
|
+
|
|
1077
|
+
assert!(cell.request(Interrupt::Timeout(inner)));
|
|
1078
|
+
cell.untime_nested(inner);
|
|
1079
|
+
assert_eq!(calls.cancel.get(), 1);
|
|
1080
|
+
assert_eq!(cell.check(DispatchKind::Script), None);
|
|
1081
|
+
assert!(!cell.request(Interrupt::Timeout(inner)));
|
|
1082
|
+
|
|
1083
|
+
assert!(cell.request(Interrupt::Timeout(enclosing)));
|
|
1084
|
+
assert_eq!(
|
|
1085
|
+
cell.check(DispatchKind::Script),
|
|
1086
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1087
|
+
);
|
|
1088
|
+
cell.untime_nested(enclosing);
|
|
1089
|
+
assert_eq!(calls.cancel.get(), 2);
|
|
1090
|
+
assert_eq!(cell.end(pump), None);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
#[test]
|
|
1094
|
+
fn an_enclosing_nested_timeout_that_fired_survives_the_inner_end() {
|
|
1095
|
+
let (cell, calls) = cell();
|
|
1096
|
+
let pump = cell.begin(DispatchKind::Pump).expect("nothing armed");
|
|
1097
|
+
let enclosing = cell.time_nested().expect("a dispatch is armed");
|
|
1098
|
+
let inner = cell.time_nested().expect("a dispatch is armed");
|
|
1099
|
+
cell.request(Interrupt::Timeout(inner));
|
|
1100
|
+
cell.request(Interrupt::Timeout(enclosing));
|
|
1101
|
+
|
|
1102
|
+
cell.untime_nested(inner);
|
|
1103
|
+
assert_eq!(calls.cancel.get(), 0);
|
|
1104
|
+
assert_eq!(
|
|
1105
|
+
cell.check(DispatchKind::Script),
|
|
1106
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1107
|
+
);
|
|
1108
|
+
cell.untime_nested(enclosing);
|
|
1109
|
+
assert_eq!(calls.cancel.get(), 1);
|
|
1110
|
+
assert_eq!(cell.end(pump), None);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
#[test]
|
|
1114
|
+
fn a_nested_timer_that_never_fired_drops_no_timeout() {
|
|
1115
|
+
let (cell, calls) = cell();
|
|
1116
|
+
let outer = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
1117
|
+
let nested = cell.time_nested().expect("a dispatch is armed");
|
|
1118
|
+
cell.request(Interrupt::Timeout(outer));
|
|
1119
|
+
|
|
1120
|
+
cell.untime_nested(nested);
|
|
1121
|
+
assert_eq!(calls.cancel.get(), 0);
|
|
1122
|
+
assert_eq!(
|
|
1123
|
+
cell.end(outer),
|
|
1124
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1125
|
+
);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
990
1128
|
#[test]
|
|
991
1129
|
fn a_child_takes_over_a_stop_and_a_memory_cause_but_no_timeout() {
|
|
992
1130
|
let (idle, _) = cell();
|
|
@@ -1028,10 +1166,10 @@ mod tests {
|
|
|
1028
1166
|
fn a_dispose_or_an_exhausted_heap_is_never_lifted() {
|
|
1029
1167
|
let (exhausted, calls) = cell();
|
|
1030
1168
|
exhausted.request(Interrupt::Stop);
|
|
1031
|
-
exhausted.request(Interrupt::HeapExhausted);
|
|
1032
1169
|
let outer = exhausted
|
|
1033
1170
|
.begin(DispatchKind::Script)
|
|
1034
1171
|
.expect("nothing armed");
|
|
1172
|
+
exhausted.request(Interrupt::HeapExhausted);
|
|
1035
1173
|
assert!(!exhausted.spend(exhausted.mark(), true));
|
|
1036
1174
|
assert!(exhausted.refuses(DispatchKind::Script));
|
|
1037
1175
|
assert_eq!(calls.cancel.get(), 0);
|
|
@@ -1199,9 +1337,9 @@ mod tests {
|
|
|
1199
1337
|
Some(Halt::Terminated(TerminationCause::MaxMemory))
|
|
1200
1338
|
);
|
|
1201
1339
|
|
|
1202
|
-
cell.request(Interrupt::HeapExhausted);
|
|
1340
|
+
assert!(!cell.request(Interrupt::HeapExhausted));
|
|
1203
1341
|
cell.relocked();
|
|
1204
|
-
assert!(cell.refuses(DispatchKind::Script));
|
|
1342
|
+
assert!(!cell.refuses(DispatchKind::Script));
|
|
1205
1343
|
}
|
|
1206
1344
|
|
|
1207
1345
|
// mini_racer's restore_termination: a nested script that reports a stop
|
|
@@ -6,18 +6,20 @@ use magnus::encoding::{EncodingCapable, RbEncoding};
|
|
|
6
6
|
use magnus::gc::Marker;
|
|
7
7
|
use magnus::r_hash::ForEach;
|
|
8
8
|
use magnus::scan_args::scan_args;
|
|
9
|
+
use magnus::typed_data::Obj;
|
|
9
10
|
use magnus::value::{LazyId, ReprValue};
|
|
10
11
|
use magnus::{
|
|
11
12
|
Class, DataTypeFunctions, Error, Fixnum, Module, RArray, RHash, RModule, RString, Ruby, Symbol,
|
|
12
|
-
TypedData, Value,
|
|
13
|
+
TryConvert, TypedData, Value,
|
|
13
14
|
};
|
|
14
|
-
use mini_racer_engine::
|
|
15
|
+
use mini_racer_engine::process::platform;
|
|
16
|
+
use mini_racer_engine::{ContextConfig, ContextHandle, Counts, EngineError, JsText, ScriptOptions};
|
|
15
17
|
|
|
16
18
|
use crate::encode;
|
|
17
19
|
use crate::gc_root::OwnedRoot;
|
|
18
20
|
use crate::host::{Callbacks, RubyHost, Waiter};
|
|
19
21
|
use crate::options::{self, OptionError, OptionValue};
|
|
20
|
-
use crate::{decode, errors, export, rb_raw, snapshot};
|
|
22
|
+
use crate::{decode, errors, export, rb_raw, snapshot, timeout};
|
|
21
23
|
|
|
22
24
|
// What Ruby's GC is told a live context holds outside its heap: roughly an
|
|
23
25
|
// isolate and its runner thread.
|
|
@@ -144,7 +146,7 @@ fn resolve_date_time(ruby: &Ruby) -> Result<(), Error> {
|
|
|
144
146
|
Ok(())
|
|
145
147
|
}
|
|
146
148
|
|
|
147
|
-
fn option_value(ruby: &Ruby, value: Value) -> OptionValue {
|
|
149
|
+
pub fn option_value(ruby: &Ruby, value: Value) -> OptionValue {
|
|
148
150
|
let other = || {
|
|
149
151
|
if value.is_nil() {
|
|
150
152
|
OptionValue::Nil
|
|
@@ -184,16 +186,34 @@ fn config(ruby: &Ruby, keywords: RHash) -> Result<ContextConfig, Error> {
|
|
|
184
186
|
pairs.push((key, value));
|
|
185
187
|
Ok(ForEach::Continue)
|
|
186
188
|
})?;
|
|
187
|
-
|
|
189
|
+
let config =
|
|
190
|
+
options::configure(pairs.into_iter()).map_err(|error| option_error(ruby, &error))?;
|
|
191
|
+
Ok(ContextConfig {
|
|
192
|
+
heap_limit: platform::heap_limit(),
|
|
193
|
+
..config
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
pub fn option_error(ruby: &Ruby, error: &OptionError) -> Error {
|
|
198
|
+
let message = error.to_string();
|
|
199
|
+
match *error {
|
|
188
200
|
OptionError::Type(_) | OptionError::SnapshotType(_) => {
|
|
189
|
-
errors::raised(ruby, ruby.exception_type_error(),
|
|
190
|
-
}
|
|
191
|
-
OptionError::Range(_) => {
|
|
192
|
-
errors::raised(ruby, ruby.exception_arg_error(), error.to_string())
|
|
201
|
+
errors::raised(ruby, ruby.exception_type_error(), message)
|
|
193
202
|
}
|
|
194
|
-
OptionError::
|
|
203
|
+
OptionError::Range(_) => errors::raised(ruby, ruby.exception_arg_error(), message),
|
|
204
|
+
OptionError::Keyword(_) => errors::runtime(ruby, message),
|
|
195
205
|
OptionError::InvalidSnapshot(invalid) => errors::engine(ruby, invalid.into()),
|
|
196
|
-
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/// Whether `value` is a `MiniRacer::Context` that can no longer run anything.
|
|
210
|
+
/// Makes no Ruby call.
|
|
211
|
+
pub fn is_disposed(ruby: &Ruby, value: Value) -> bool {
|
|
212
|
+
// Matched by name first, since a failed conversion builds its message
|
|
213
|
+
// through Ruby calls.
|
|
214
|
+
rb_raw::typed_data_name(ruby, value).as_deref() == Some("mini_racer/context")
|
|
215
|
+
&& Obj::<Context>::try_convert(value)
|
|
216
|
+
.is_ok_and(|context| context.handle.get().is_none_or(ContextHandle::is_disposed))
|
|
197
217
|
}
|
|
198
218
|
|
|
199
219
|
impl Context {
|
|
@@ -282,12 +302,12 @@ impl Context {
|
|
|
282
302
|
_ => JsText::from("<eval>"),
|
|
283
303
|
};
|
|
284
304
|
let source = js_text(ruby, source)?;
|
|
305
|
+
let options = ScriptOptions {
|
|
306
|
+
awaited,
|
|
307
|
+
timeout: timeout::current(ruby)?,
|
|
308
|
+
};
|
|
285
309
|
let bytes = rb_self.dispatch(ruby, |host| {
|
|
286
|
-
|
|
287
|
-
handle.eval_await(filename, source, host)
|
|
288
|
-
} else {
|
|
289
|
-
handle.eval(filename, source, host)
|
|
290
|
-
}
|
|
310
|
+
handle.evaluate(filename, source, options, host)
|
|
291
311
|
})?;
|
|
292
312
|
decode::value(ruby, &bytes)
|
|
293
313
|
}
|
|
@@ -311,13 +331,11 @@ impl Context {
|
|
|
311
331
|
let request = encode::call(ruby, name, args, |message| {
|
|
312
332
|
errors::runtime(ruby, [b"Context.call: ".as_slice(), &message].concat())
|
|
313
333
|
})?;
|
|
314
|
-
let
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
})?;
|
|
334
|
+
let options = ScriptOptions {
|
|
335
|
+
awaited,
|
|
336
|
+
timeout: timeout::current(ruby)?,
|
|
337
|
+
};
|
|
338
|
+
let bytes = rb_self.dispatch(ruby, |host| handle.invoke(request, options, host))?;
|
|
321
339
|
decode::value(ruby, &bytes)
|
|
322
340
|
}
|
|
323
341
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
use magnus::value::ReprValue;
|
|
2
|
-
use magnus::{
|
|
3
|
-
|
|
2
|
+
use magnus::{
|
|
3
|
+
Class, Error, Exception, ExceptionClass, Module, Object, RModule, RObject, Ruby, Value,
|
|
4
|
+
};
|
|
5
|
+
use mini_racer_engine::{EngineError, FlagError, HeapLimitError, JsErrorKind, TerminationCause};
|
|
4
6
|
|
|
5
7
|
use crate::export;
|
|
6
8
|
use crate::gc_root::StaticRoot;
|
|
@@ -58,6 +60,7 @@ roots! {
|
|
|
58
60
|
PLATFORM_INITIALIZED = "MiniRacer"::"PlatformAlreadyInitialized";
|
|
59
61
|
INTERNAL = "MiniRacer"::"InternalError";
|
|
60
62
|
SNAPSHOT = "MiniRacer"::"SnapshotError";
|
|
63
|
+
HEAP_LIMIT = "MiniRacerRs"::"HeapLimitError";
|
|
61
64
|
}
|
|
62
65
|
|
|
63
66
|
pub fn engine(ruby: &Ruby, error: EngineError) -> Error {
|
|
@@ -72,9 +75,8 @@ pub fn engine(ruby: &Ruby, error: EngineError) -> Error {
|
|
|
72
75
|
| TerminationCause::Timeout
|
|
73
76
|
| TerminationCause::Cancelled,
|
|
74
77
|
) => &TERMINATED,
|
|
75
|
-
JsErrorKind::Terminated(
|
|
76
|
-
|
|
77
|
-
) => &OUT_OF_MEMORY,
|
|
78
|
+
JsErrorKind::Terminated(TerminationCause::MaxMemory) => &OUT_OF_MEMORY,
|
|
79
|
+
JsErrorKind::Terminated(TerminationCause::HeapExhausted) => &HEAP_LIMIT,
|
|
78
80
|
// mini_racer raises its disposed error when a callback's
|
|
79
81
|
// exception escapes but none is kept for it.
|
|
80
82
|
JsErrorKind::HostException => {
|
|
@@ -91,12 +93,15 @@ pub fn engine(ruby: &Ruby, error: EngineError) -> Error {
|
|
|
91
93
|
EngineError::Internal(message) => raised(ruby, INTERNAL.get(ruby), &message),
|
|
92
94
|
EngineError::Snapshot(message) => snapshot(ruby, &message),
|
|
93
95
|
EngineError::InvalidSnapshot(_) => raised(ruby, SNAPSHOT.get(ruby), error.to_string()),
|
|
96
|
+
EngineError::HeapLimitOverridden => {
|
|
97
|
+
Error::new(ruby.exception_arg_error(), error.to_string())
|
|
98
|
+
}
|
|
94
99
|
}
|
|
95
100
|
}
|
|
96
101
|
|
|
97
102
|
pub fn flag(ruby: &Ruby, error: FlagError) -> Error {
|
|
98
103
|
match error {
|
|
99
|
-
FlagError::Nul | FlagError::TooLong => {
|
|
104
|
+
FlagError::Nul | FlagError::TooLong | FlagError::OverridesHeapLimit(_) => {
|
|
100
105
|
raised(ruby, ruby.exception_arg_error(), error.to_string())
|
|
101
106
|
}
|
|
102
107
|
FlagError::AlreadyInitialized => {
|
|
@@ -105,12 +110,22 @@ pub fn flag(ruby: &Ruby, error: FlagError) -> Error {
|
|
|
105
110
|
}
|
|
106
111
|
}
|
|
107
112
|
|
|
113
|
+
pub fn heap_limit(ruby: &Ruby, error: HeapLimitError) -> Error {
|
|
114
|
+
Error::new(ruby.exception_arg_error(), error.to_string())
|
|
115
|
+
}
|
|
116
|
+
|
|
108
117
|
// A JavaScript error object decoded into Ruby keeps its cause here, as in
|
|
109
118
|
// mini_racer, rather than in the cause Ruby gives a raised exception.
|
|
110
119
|
fn cause(_ruby: &Ruby, rb_self: RObject) -> Result<Value, Error> {
|
|
111
120
|
rb_self.ivar_get("@cause")
|
|
112
121
|
}
|
|
113
122
|
|
|
123
|
+
/// Whether a context that raised `exception` is spent: out of memory, or
|
|
124
|
+
/// disposed.
|
|
125
|
+
pub fn spends_context(ruby: &Ruby, exception: Exception) -> bool {
|
|
126
|
+
exception.is_kind_of(OUT_OF_MEMORY.get(ruby)) || exception.is_kind_of(DISPOSED.get(ruby))
|
|
127
|
+
}
|
|
128
|
+
|
|
114
129
|
pub fn script_error(ruby: &Ruby) -> ExceptionClass {
|
|
115
130
|
SCRIPT.get(ruby)
|
|
116
131
|
}
|
|
@@ -5,7 +5,11 @@ use std::sync::OnceLock;
|
|
|
5
5
|
use magnus::gc::Marker;
|
|
6
6
|
use magnus::typed_data::Obj;
|
|
7
7
|
use magnus::value::{Lazy, Opaque, ReprValue};
|
|
8
|
-
use magnus::{
|
|
8
|
+
use magnus::{
|
|
9
|
+
Class, DataTypeFunctions, Error, Module, Object, RModule, Ruby, TryConvert, TypedData, Value,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
use crate::rb_raw;
|
|
9
13
|
|
|
10
14
|
pub struct StaticRoot<T: ReprValue>(Lazy<T>);
|
|
11
15
|
|
|
@@ -173,6 +177,32 @@ impl Spill {
|
|
|
173
177
|
}
|
|
174
178
|
}
|
|
175
179
|
|
|
180
|
+
// A hidden instance variable, which Ruby code cannot read or change.
|
|
181
|
+
const SPAWNED: &str = "spawned_with";
|
|
182
|
+
|
|
183
|
+
/// Starts a Ruby thread that runs `run` with `value`, which the thread object
|
|
184
|
+
/// keeps alive until the thread ends. `run` must handle whatever its Ruby
|
|
185
|
+
/// calls raise.
|
|
186
|
+
pub fn spawn<T: ReprValue + TryConvert>(
|
|
187
|
+
ruby: &Ruby,
|
|
188
|
+
value: T,
|
|
189
|
+
run: impl FnOnce(&Ruby, T) + Send + 'static,
|
|
190
|
+
) -> Result<(), Error> {
|
|
191
|
+
// The thread reads `value` back from its own instance variable, which
|
|
192
|
+
// compaction updates if the value moves before the thread first runs; a
|
|
193
|
+
// copy held here would go stale. The thread waits for the GVL this one
|
|
194
|
+
// holds until the variable is set.
|
|
195
|
+
let thread = rb_raw::thread_create(
|
|
196
|
+
ruby,
|
|
197
|
+
Box::new(move |ruby: &Ruby| {
|
|
198
|
+
if let Ok(value) = ruby.thread_current().ivar_get::<_, T>(SPAWNED) {
|
|
199
|
+
run(ruby, value);
|
|
200
|
+
}
|
|
201
|
+
}),
|
|
202
|
+
)?;
|
|
203
|
+
thread.ivar_set(SPAWNED, value.as_value())
|
|
204
|
+
}
|
|
205
|
+
|
|
176
206
|
// Each holder's class is looked up once, by a Ruby call whose failure panics;
|
|
177
207
|
// near the end of a fiber's stack Ruby fails it with SystemStackError, so it is
|
|
178
208
|
// looked up here instead, at load, with room to spare.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
use magnus::value::ReprValue;
|
|
2
|
+
use magnus::{Error, Integer, Object, RModule, Ruby, Value};
|
|
3
|
+
use mini_racer_engine::process::platform;
|
|
4
|
+
|
|
5
|
+
use crate::{errors, export, rb_raw};
|
|
6
|
+
|
|
7
|
+
pub fn init(rs: RModule) -> Result<(), Error> {
|
|
8
|
+
rs.define_singleton_method("heap_limit", export!(function limit, 0))?;
|
|
9
|
+
rs.define_singleton_method("heap_limit=", export!(function set_limit, 1))?;
|
|
10
|
+
Ok(())
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
fn limit(ruby: &Ruby) -> Value {
|
|
14
|
+
platform::heap_limit()
|
|
15
|
+
.and_then(|limit| u64::try_from(limit).ok())
|
|
16
|
+
.map_or_else(
|
|
17
|
+
|| ruby.qnil().as_value(),
|
|
18
|
+
|limit| ruby.integer_from_u64(limit).as_value(),
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Checked as `Context.new` checks its integer options: anything but an
|
|
23
|
+
// Integer is a TypeError, and an Integer out of range, however large, an
|
|
24
|
+
// ArgumentError.
|
|
25
|
+
fn set_limit(ruby: &Ruby, value: Value) -> Result<Value, Error> {
|
|
26
|
+
let limit = if value.is_nil() {
|
|
27
|
+
None
|
|
28
|
+
} else {
|
|
29
|
+
let integer = Integer::from_value(value).ok_or_else(|| {
|
|
30
|
+
let name = rb_raw::type_name(ruby, value);
|
|
31
|
+
errors::raised(
|
|
32
|
+
ruby,
|
|
33
|
+
ruby.exception_type_error(),
|
|
34
|
+
format!("wrong argument type {name} (expected Integer)"),
|
|
35
|
+
)
|
|
36
|
+
})?;
|
|
37
|
+
let bytes = integer.to_i64().ok().and_then(|n| usize::try_from(n).ok());
|
|
38
|
+
Some(bytes.unwrap_or(usize::MAX))
|
|
39
|
+
};
|
|
40
|
+
platform::set_heap_limit(limit).map_err(|error| errors::heap_limit(ruby, error))?;
|
|
41
|
+
Ok(value)
|
|
42
|
+
}
|
|
@@ -10,9 +10,11 @@ mod fork;
|
|
|
10
10
|
)]
|
|
11
11
|
mod gc_root;
|
|
12
12
|
mod guard;
|
|
13
|
+
mod heap_limit;
|
|
13
14
|
mod host;
|
|
14
15
|
mod options;
|
|
15
16
|
mod platform;
|
|
17
|
+
mod pool;
|
|
16
18
|
#[allow(
|
|
17
19
|
unsafe_code,
|
|
18
20
|
reason = "the only module that calls rb-sys directly (invariant I6)"
|
|
@@ -21,6 +23,7 @@ mod rb_raw;
|
|
|
21
23
|
mod snapshot;
|
|
22
24
|
mod source_maps;
|
|
23
25
|
mod spin;
|
|
26
|
+
mod timeout;
|
|
24
27
|
|
|
25
28
|
use magnus::{Error, Module, Ruby};
|
|
26
29
|
|
|
@@ -49,7 +52,10 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
49
52
|
context::init(ruby, racer)?;
|
|
50
53
|
snapshot::init(ruby, racer)?;
|
|
51
54
|
source_maps::init(rs)?;
|
|
55
|
+
heap_limit::init(rs)?;
|
|
56
|
+
timeout::init(ruby, rs)?;
|
|
52
57
|
counters::init(rs)?;
|
|
58
|
+
pool::init(ruby, rs)?;
|
|
53
59
|
fork::init(ruby, rs)?;
|
|
54
60
|
Ok(())
|
|
55
61
|
}
|