mini_racer_rs 1.5.1 → 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 +14 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +25 -25
- 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/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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ac6808f9735e55f734d5279ac4a61df43c360de9fd62475417fb3a86eed95419
|
|
4
|
+
data.tar.gz: 775a078c2e03d715bce13eca7deac20a9df74be55adec4d1942717042ffdf29d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9b4a2ee6af6bf08c29484eaab6228ae78b05c17e9c57457b12c339d013047855371b600b1e1f870e62a02d2f04bf75f69e779aab07f094c73179fea3f132634d
|
|
7
|
+
data.tar.gz: 2662edd8f467161d2f4d7c93a29e3033feddac65411ce5e5118590aa8c216c09b36f52b259d0aa88285b6876839a13df59bc4d80b48df9133a4203beb337c0d5
|
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.5.2](https://github.com/andymai/mini_racer_rs/compare/v1.5.1...v1.5.2) (2026-10-24)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **ext:** keep set_flags! values rooted and stop dropping interrupts in waits ([#152](https://github.com/andymai/mini_racer_rs/issues/152)) ([b32e5cd](https://github.com/andymai/mini_racer_rs/commit/b32e5cd22a1f0f36cc4fa1e7b516c8867829b70f))
|
|
9
|
+
* **pool:** build replacements outside the timeout scope and root shutdown's queues ([#151](https://github.com/andymai/mini_racer_rs/issues/151)) ([60b292d](https://github.com/andymai/mini_racer_rs/commit/60b292df11e7802b9b8429848788176698b463e9))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Performance
|
|
13
|
+
|
|
14
|
+
* **engine:** decode a shared UTF-8 source in place ([#155](https://github.com/andymai/mini_racer_rs/issues/155)) ([f37538f](https://github.com/andymai/mini_racer_rs/commit/f37538fa2931438c4cd5a40d7647d2bbfc92d356))
|
|
15
|
+
* **engine:** remember 4,096 sightings of large scripts ([#156](https://github.com/andymai/mini_racer_rs/issues/156)) ([656b60f](https://github.com/andymai/mini_racer_rs/commit/656b60fe62c976ae06786460bb24004c127dccd8))
|
|
16
|
+
|
|
3
17
|
## [1.5.1](https://github.com/andymai/mini_racer_rs/compare/v1.5.0...v1.5.1) (2026-10-23)
|
|
4
18
|
|
|
5
19
|
|
data/Cargo.lock
CHANGED
|
@@ -593,7 +593,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
|
593
593
|
|
|
594
594
|
[[package]]
|
|
595
595
|
name = "mini_racer_engine"
|
|
596
|
-
version = "1.5.
|
|
596
|
+
version = "1.5.2"
|
|
597
597
|
dependencies = [
|
|
598
598
|
"deno_core_icudata",
|
|
599
599
|
"libc",
|
|
@@ -606,7 +606,7 @@ dependencies = [
|
|
|
606
606
|
|
|
607
607
|
[[package]]
|
|
608
608
|
name = "mini_racer_rs"
|
|
609
|
-
version = "1.5.
|
|
609
|
+
version = "1.5.2"
|
|
610
610
|
dependencies = [
|
|
611
611
|
"magnus",
|
|
612
612
|
"mini_racer_engine",
|
|
@@ -617,7 +617,7 @@ dependencies = [
|
|
|
617
617
|
|
|
618
618
|
[[package]]
|
|
619
619
|
name = "mini_racer_sourcemap"
|
|
620
|
-
version = "1.5.
|
|
620
|
+
version = "1.5.2"
|
|
621
621
|
dependencies = [
|
|
622
622
|
"memchr",
|
|
623
623
|
"serde",
|
|
@@ -627,7 +627,7 @@ dependencies = [
|
|
|
627
627
|
|
|
628
628
|
[[package]]
|
|
629
629
|
name = "mini_racer_sync"
|
|
630
|
-
version = "1.5.
|
|
630
|
+
version = "1.5.2"
|
|
631
631
|
dependencies = [
|
|
632
632
|
"loom",
|
|
633
633
|
"thiserror",
|
|
@@ -635,7 +635,7 @@ dependencies = [
|
|
|
635
635
|
|
|
636
636
|
[[package]]
|
|
637
637
|
name = "mini_racer_wire"
|
|
638
|
-
version = "1.5.
|
|
638
|
+
version = "1.5.2"
|
|
639
639
|
dependencies = [
|
|
640
640
|
"criterion",
|
|
641
641
|
"proptest",
|
data/Cargo.toml
CHANGED
|
@@ -15,7 +15,7 @@ license = "MIT"
|
|
|
15
15
|
publish = false
|
|
16
16
|
repository = "https://github.com/andymai/mini_racer_rs"
|
|
17
17
|
rust-version = "1.91"
|
|
18
|
-
version = "1.5.
|
|
18
|
+
version = "1.5.2"
|
|
19
19
|
|
|
20
20
|
[workspace.dependencies]
|
|
21
21
|
criterion = {version = "0.8", default-features = false, features = ["cargo_bench_support"]}
|
data/README.md
CHANGED
|
@@ -21,9 +21,9 @@ mini_racer_rs implements the `MiniRacer` API from mini_racer 0.22.2. It defines
|
|
|
21
21
|
|
|
22
22
|
## Highlights
|
|
23
23
|
|
|
24
|
-
- **Round trips:** 5 to 9 times faster (0.75 µs vs 6.
|
|
25
|
-
- **Babel through ExecJS:** 1.
|
|
26
|
-
- **Bundle load:**
|
|
24
|
+
- **Round trips:** 5 to 9 times faster (0.75 µs vs 6.50 µs per call).
|
|
25
|
+
- **Babel through ExecJS:** 1.93x faster (5.09 ms vs 9.85 ms).
|
|
26
|
+
- **Bundle load:** 9.2x faster for a 44 MB production server rendering bundle (105 ms vs 965 ms).
|
|
27
27
|
- **Memory:** A context holding the MUI bundle uses 43 MB vs 110 MB.
|
|
28
28
|
- **Forks:** After loading and rendering React: 20 of 20 clean mini_racer_rs exits vs 20 of 20 mini_racer aborts.
|
|
29
29
|
- **V8:** V8 15.2.124.1 vs V8 13.6.233.17 in mini_racer 0.22.2.
|
|
@@ -33,7 +33,7 @@ mini_racer_rs implements the `MiniRacer` API from mini_racer 0.22.2. It defines
|
|
|
33
33
|
- **Callback jumps:** A `throw` out of an attached callback ends the script with `Error: terminated` and continues after `eval`; mini_racer 0.22.2 crashes with a segmentation fault.
|
|
34
34
|
- **Checks:** Script-enforced crate boundaries, four fenced `unsafe` modules, loom, wire-codec fuzzing, nightly sanitizers and mini_racer differential tests.
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
The round trip, Babel, memory, fiber scheduler, and bundle load numbers compare with mini_racer 0.22.2, while the fork numbers compare with mini_racer 0.22.1. See [Benchmarks](#benchmarks) for the method.
|
|
37
37
|
|
|
38
38
|
## Install
|
|
39
39
|
|
|
@@ -212,39 +212,39 @@ Before forking, evaluate each bundle of 1 MiB or more twice in the master, then
|
|
|
212
212
|
|
|
213
213
|
For Ruby's `fork`, `IO.popen("-")`, or `Process.daemon`, mini_racer_rs waits for context runners, pauses its worker pool and waits for running tasks. An async `WebAssembly.compile` in a new default-mode child completes on the child's own pool. On Ubuntu 24.04 with glibc 2.39, 20 of 20 mini_racer 0.22.1 children aborted with signal 6 after React rendering; all mini_racer_rs children exited cleanly. mini_racer 0.22.2 has the same fork handling. On Fedora, `test/stress/fork_busy.rb` aborted with mini_racer 0.22.2 in 3 of 3 runs, after 5, 14 and 19 forks, when an evaluating parent thread raised `TypeError`. mini_racer_rs completed 300 forks per run on Fedora and Ubuntu.
|
|
214
214
|
|
|
215
|
-
With `mini_racer` 0.22.
|
|
215
|
+
With `mini_racer` 0.22.2, each worker compiles the bundle from source on its first load because it has no code cache.
|
|
216
216
|
|
|
217
217
|
## Benchmarks
|
|
218
218
|
|
|
219
|
-
| Workload | mini_racer 0.22.
|
|
219
|
+
| Workload | mini_racer 0.22.2 | mini_racer_rs | Ratio |
|
|
220
220
|
|---|---:|---:|---:|
|
|
221
|
-
| Babel transform of a 3 KB JSX component, through ExecJS |
|
|
222
|
-
| Terser minify of a 78 KB library, through ExecJS | 51.
|
|
223
|
-
| React server render of 1,000 items (17 KB of HTML), through ExecJS | 192 µs |
|
|
221
|
+
| Babel transform of a 3 KB JSX component, through ExecJS | 9.8 ms | 5.1 ms | 1.9x |
|
|
222
|
+
| Terser minify of a 78 KB library, through ExecJS | 51.3 ms | 49.9 ms | 1.0x |
|
|
223
|
+
| React server render of 1,000 items (17 KB of HTML), through ExecJS | 192 µs | 175 µs | 1.1x |
|
|
224
224
|
| React in a new context: load the bundle and render 1,000 items, through ExecJS | 11.8 ms | 11.8 ms | 1.0x |
|
|
225
|
-
| Handlebars render of 50 rows, through `Context#call` |
|
|
226
|
-
| Autoprefixer on Bootstrap's 280 KB stylesheet, through ExecJS | 26.
|
|
227
|
-
| MUI dashboard server render of 200 rows and 299 icons (469 KB of HTML), through ExecJS |
|
|
228
|
-
| MUI in a new context: load the 8.2 MB bundle and render once, through ExecJS |
|
|
229
|
-
| Load a 44 MB production server rendering bundle into a new context, through ExecJS |
|
|
225
|
+
| Handlebars render of 50 rows, through `Context#call` | 50.0 µs | 35.0 µs | 1.4x |
|
|
226
|
+
| Autoprefixer on Bootstrap's 280 KB stylesheet, through ExecJS | 26.0 ms | 24.0 ms | 1.1x |
|
|
227
|
+
| MUI dashboard server render of 200 rows and 299 icons (469 KB of HTML), through ExecJS | 31.4 ms | 29.2 ms | 1.1x |
|
|
228
|
+
| MUI in a new context: load the 8.2 MB bundle and render once, through ExecJS | 164 ms | 104 ms | 1.6x |
|
|
229
|
+
| Load a 44 MB production server rendering bundle into a new context, through ExecJS | 965 ms | 105 ms | 9.2x |
|
|
230
230
|
|
|
231
|
-
Results are medians of 5 runs. Every workload produced identical output in both gems. The eight library workloads use one run of the Ruby 3.4 extension from the published mini_racer_rs 1.
|
|
231
|
+
Results are medians of 5 runs. Every workload produced identical output in both gems. The eight library workloads use one run of the Ruby 3.4 extension from the published mini_racer_rs 1.5.1 `x86_64-linux-gnu` gem against mini_racer 0.22.2. The 44 MB production bundle load uses a second run of the same gem against mini_racer 0.22.2. Benchmarks ran on Ruby 3.4.2 and Linux with an AMD Ryzen 9 7950X3D. The governor was `performance`. Both gems were pinned to CPUs 0 through 7.
|
|
232
232
|
|
|
233
|
-
The per-call and memory figures below come from the
|
|
233
|
+
The per-call and memory figures below come from the first run of that extension on the same machine, except the production bundle row, which comes from the second run. One watchdog thread serves the whole process, making a call with `timeout:` fast. Scripts of 1 MiB or more use a process-wide V8 code cache. Contexts loading the same large script share one source copy.
|
|
234
234
|
|
|
235
|
-
| Measure | mini_racer 0.22.
|
|
235
|
+
| Measure | mini_racer 0.22.2 | mini_racer_rs |
|
|
236
236
|
|---|---:|---:|
|
|
237
|
-
| eval round trip | 6.
|
|
238
|
-
| call round trip | 6.
|
|
239
|
-
| callback round trip | 6.
|
|
240
|
-
| call on a context with `timeout:` | 20.
|
|
241
|
-
| call inside `MiniRacerRs.timeout` (mini_racer: on a context with `timeout:`) | 20.
|
|
237
|
+
| eval round trip | 6.58 µs | 1.13 µs |
|
|
238
|
+
| call round trip | 6.50 µs | 0.75 µs |
|
|
239
|
+
| callback round trip | 6.13 µs | 1.11 µs |
|
|
240
|
+
| call on a context with `timeout:` | 20.42 µs | 0.78 µs |
|
|
241
|
+
| call inside `MiniRacerRs.timeout` (mini_racer: on a context with `timeout:`) | 20.22 µs | 0.87 µs |
|
|
242
242
|
| Babel inside V8 | 10.3 ms | 5.3 ms |
|
|
243
|
-
| startup plus Babel | 44.
|
|
243
|
+
| startup plus Babel | 44.8 ms | 13.3 ms |
|
|
244
244
|
| context holding Babel | 28.5 MB | 15.0 MB |
|
|
245
245
|
| context holding the 8.2 MB MUI bundle | 110 MB | 43 MB |
|
|
246
|
-
| context holding the 44 MB production bundle
|
|
247
|
-
| peak resident memory with 20,000 contexts left to Ruby's garbage collector | 2,
|
|
246
|
+
| context holding the 44 MB production bundle | 301 MB | 44 MB |
|
|
247
|
+
| peak resident memory with 20,000 contexts left to Ruby's garbage collector | 2,918 MB | 108 MB |
|
|
248
248
|
|
|
249
249
|
```sh
|
|
250
250
|
bundle exec ruby bench/run.rb --cpus 0-7
|
|
@@ -9,7 +9,11 @@ use crate::control::JsText;
|
|
|
9
9
|
|
|
10
10
|
const MAX_ENTRIES: usize = 8;
|
|
11
11
|
const MAX_BYTES: usize = 256 << 20;
|
|
12
|
-
|
|
12
|
+
// 32 KiB of digests, so a bundle loaded again after thousands of large scripts
|
|
13
|
+
// evaluated once is still seen a second time: a list of 64 forgot one after 70
|
|
14
|
+
// such scripts, so only its fourth load read a cache. Each large eval scans
|
|
15
|
+
// it, in about 2 microseconds when full.
|
|
16
|
+
const MAX_SIGHTINGS: usize = 4096;
|
|
13
17
|
const MAX_REFUSED: usize = 8;
|
|
14
18
|
// Units read from each end of a source, and a smaller window from each of a
|
|
15
19
|
// few points between, to tell one sighting from another without reading it all.
|
|
@@ -292,7 +296,7 @@ pub(crate) fn enabled() -> bool {
|
|
|
292
296
|
#[cfg(test)]
|
|
293
297
|
#[allow(clippy::panic, reason = "a test fails by panicking")]
|
|
294
298
|
mod tests {
|
|
295
|
-
use super::{CodeCache, Plan};
|
|
299
|
+
use super::{CodeCache, MAX_SIGHTINGS, Plan};
|
|
296
300
|
use crate::control::JsText;
|
|
297
301
|
|
|
298
302
|
fn cache() -> &'static CodeCache {
|
|
@@ -397,6 +401,29 @@ mod tests {
|
|
|
397
401
|
));
|
|
398
402
|
}
|
|
399
403
|
|
|
404
|
+
// The list holds as many sightings as it keeps and no more: a script is
|
|
405
|
+
// seen again after one fewer others, and forgotten after that many more.
|
|
406
|
+
#[test]
|
|
407
|
+
fn a_sighting_is_forgotten_once_the_list_has_had_its_fill_of_others() {
|
|
408
|
+
let cache = cache();
|
|
409
|
+
let script = |seed: usize| JsText::Latin1(seed.to_le_bytes().to_vec());
|
|
410
|
+
for seed in 0..MAX_SIGHTINGS {
|
|
411
|
+
let _ = cache.plan(&name(), &script(seed), None, false);
|
|
412
|
+
}
|
|
413
|
+
assert!(matches!(
|
|
414
|
+
cache.plan(&name(), &script(0), None, false),
|
|
415
|
+
Plan::Make(..)
|
|
416
|
+
));
|
|
417
|
+
for seed in MAX_SIGHTINGS..2 * MAX_SIGHTINGS {
|
|
418
|
+
let _ = cache.plan(&name(), &script(seed), None, false);
|
|
419
|
+
}
|
|
420
|
+
assert_eq!(mini_racer_sync::lock(&cache.sightings).len(), MAX_SIGHTINGS);
|
|
421
|
+
assert!(matches!(
|
|
422
|
+
cache.plan(&name(), &script(0), None, false),
|
|
423
|
+
Plan::Compile(None)
|
|
424
|
+
));
|
|
425
|
+
}
|
|
426
|
+
|
|
400
427
|
#[test]
|
|
401
428
|
fn a_script_being_built_is_built_once() {
|
|
402
429
|
let cache = cache();
|
|
@@ -456,4 +483,21 @@ mod tests {
|
|
|
456
483
|
Plan::Consume(..)
|
|
457
484
|
));
|
|
458
485
|
}
|
|
486
|
+
|
|
487
|
+
#[test]
|
|
488
|
+
fn caches_past_the_size_limit_drop_the_least_recently_used() {
|
|
489
|
+
let cache = cache();
|
|
490
|
+
let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
|
|
491
|
+
cached(cache, &source(10), half());
|
|
492
|
+
cached(cache, &source(11), half());
|
|
493
|
+
assert_eq!(cache.stored(), 2);
|
|
494
|
+
assert!(matches!(
|
|
495
|
+
cache.plan(&name(), &source(11), None, true),
|
|
496
|
+
Plan::Consume(..)
|
|
497
|
+
));
|
|
498
|
+
assert!(matches!(
|
|
499
|
+
cache.plan(&name(), &source(10), None, true),
|
|
500
|
+
Plan::Make(..)
|
|
501
|
+
));
|
|
502
|
+
}
|
|
459
503
|
}
|
|
@@ -5,6 +5,7 @@ use mini_racer_sync::lock;
|
|
|
5
5
|
|
|
6
6
|
use crate::control::JsText;
|
|
7
7
|
use crate::process::code_cache::Key;
|
|
8
|
+
use crate::text;
|
|
8
9
|
|
|
9
10
|
const MAX_SOURCES: usize = 8;
|
|
10
11
|
const MAX_BYTES: usize = 256 << 20;
|
|
@@ -26,16 +27,7 @@ impl Units {
|
|
|
26
27
|
Some(match text {
|
|
27
28
|
JsText::Latin1(bytes) => Self::OneByte(Arc::from(bytes.as_slice())),
|
|
28
29
|
JsText::Utf8(bytes) if bytes.is_ascii() => Self::OneByte(Arc::from(bytes.as_slice())),
|
|
29
|
-
JsText::Utf8(bytes) =>
|
|
30
|
-
let text = std::str::from_utf8(bytes).ok()?;
|
|
31
|
-
text.chars()
|
|
32
|
-
.map(u8::try_from)
|
|
33
|
-
.collect::<Result<_, _>>()
|
|
34
|
-
.map_or_else(
|
|
35
|
-
|_| Self::TwoByte(text.encode_utf16().collect()),
|
|
36
|
-
Self::OneByte,
|
|
37
|
-
)
|
|
38
|
-
}
|
|
30
|
+
JsText::Utf8(bytes) => text::units(bytes)?,
|
|
39
31
|
JsText::Utf16(units) => units
|
|
40
32
|
.iter()
|
|
41
33
|
.map(|&unit| u8::try_from(unit))
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
use std::ffi::{c_char, c_void};
|
|
2
|
+
use std::mem::MaybeUninit;
|
|
2
3
|
use std::ptr::NonNull;
|
|
3
4
|
use std::sync::Arc;
|
|
4
5
|
use std::time::Duration;
|
|
@@ -158,6 +159,47 @@ pub fn utf8_to_utf16(text: &[u8], out: &mut [u16]) -> Option<usize> {
|
|
|
158
159
|
converted.is_ok().then_some(converted.count)
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
/// A shared slice of a length fixed when it is made, written in place from its
|
|
163
|
+
/// first element on, a run at a time, and handed out once every one is
|
|
164
|
+
/// written.
|
|
165
|
+
pub struct Filling<T> {
|
|
166
|
+
slots: Arc<[MaybeUninit<T>]>,
|
|
167
|
+
written: usize,
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
impl<T: Copy> Filling<T> {
|
|
171
|
+
#[must_use]
|
|
172
|
+
pub fn new(len: usize) -> Self {
|
|
173
|
+
Self {
|
|
174
|
+
slots: Arc::new_uninit_slice(len),
|
|
175
|
+
written: 0,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/// Writes `run` after the elements already written; `None`, writing
|
|
180
|
+
/// nothing, when it does not fit.
|
|
181
|
+
pub fn append(&mut self, run: &[T]) -> Option<()> {
|
|
182
|
+
let end = self.written.checked_add(run.len())?;
|
|
183
|
+
let slots = Arc::get_mut(&mut self.slots)?.get_mut(self.written..end)?;
|
|
184
|
+
for (slot, &unit) in slots.iter_mut().zip(run) {
|
|
185
|
+
slot.write(unit);
|
|
186
|
+
}
|
|
187
|
+
self.written = end;
|
|
188
|
+
Some(())
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/// The slice, once every element is written.
|
|
192
|
+
#[must_use]
|
|
193
|
+
pub fn filled(self) -> Option<Arc<[T]>> {
|
|
194
|
+
if self.written != self.slots.len() {
|
|
195
|
+
return None;
|
|
196
|
+
}
|
|
197
|
+
// SAFETY: `append` writes the elements in order from the first and
|
|
198
|
+
// counts only those it wrote, so every one is initialized.
|
|
199
|
+
Some(unsafe { self.slots.assume_init() })
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
161
203
|
/// The address V8 names `isolate` by when it posts the isolate a task.
|
|
162
204
|
#[must_use]
|
|
163
205
|
pub fn isolate_key(isolate: &v8::Isolate) -> usize {
|
|
@@ -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> {
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mini_racer_rs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.5.
|
|
4
|
+
version: 1.5.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Aragon
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-10-
|
|
10
|
+
date: 2026-10-24 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: rb_sys
|
|
@@ -34,6 +34,7 @@ extensions:
|
|
|
34
34
|
- ext/mini_racer_rs/extconf.rb
|
|
35
35
|
extra_rdoc_files: []
|
|
36
36
|
files:
|
|
37
|
+
- ".yardopts"
|
|
37
38
|
- AI-DISCLOSURE.md
|
|
38
39
|
- CHANGELOG.md
|
|
39
40
|
- COMPATIBILITY.md
|
|
@@ -153,7 +154,11 @@ licenses:
|
|
|
153
154
|
- MIT
|
|
154
155
|
metadata:
|
|
155
156
|
homepage_uri: https://github.com/andymai/mini_racer_rs
|
|
157
|
+
source_code_uri: https://github.com/andymai/mini_racer_rs/tree/v1.5.2
|
|
158
|
+
changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.2/CHANGELOG.md
|
|
159
|
+
documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.2/README.md#api-at-a-glance
|
|
156
160
|
bug_tracker_uri: https://github.com/andymai/mini_racer_rs/issues
|
|
161
|
+
funding_uri: https://github.com/sponsors/andymai
|
|
157
162
|
rubygems_mfa_required: 'true'
|
|
158
163
|
rdoc_options: []
|
|
159
164
|
require_paths:
|