mini_racer_rs 1.0.4 → 1.0.6
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 +14 -0
- data/COMPATIBILITY.md +7 -1
- data/Cargo.lock +4 -4
- data/Cargo.toml +1 -1
- data/README.md +1 -1
- data/crates/mini_racer_engine/src/lib.rs +2 -0
- data/crates/mini_racer_engine/src/request.rs +58 -22
- data/crates/mini_racer_engine/src/sys.rs +37 -0
- data/crates/mini_racer_engine/src/text.rs +182 -0
- data/crates/mini_racer_wire/src/write.rs +8 -3
- data/ext/mini_racer_rs/src/decode.rs +29 -0
- data/ext/mini_racer_rs/src/encode.rs +83 -15
- data/ext/mini_racer_rs/src/errors.rs +9 -1
- data/ext/mini_racer_rs/src/gc_root.rs +5 -0
- data/ext/mini_racer_rs/src/host.rs +16 -2
- data/ext/mini_racer_rs/src/lib.rs +2 -0
- data/ext/mini_racer_rs/src/rb_raw.rs +24 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 34cc61e0568863bc59e52837ee5366a5ee8c560d22ad81454ca29bdb55666c45
|
|
4
|
+
data.tar.gz: cc817e64000b31c6baa15daa84103b0eca1022c2ddde7f2ff949f1e8ab808588
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7ed16776a34d4b948408c4ab73a9024357ed383b3b294a6384a702559765bf4d9e4d4e1a481b86457a833bf498c89e9dc4e5e6ed364fab8de4fa7c71fc428d72
|
|
7
|
+
data.tar.gz: 7dc43391ef4ef78d9a133c2646460648b849cf946417c958fdf8538a94c70eee24fc61f2cc00395f5e3e6aaa8b904e6af663e3c16ca2bd783538b0dd29397330
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.6](https://github.com/andymai/mini_racer_rs/compare/v1.0.5...v1.0.6) (2026-10-19)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **decode:** raise SystemStackError when a decode runs out of stack ([#95](https://github.com/andymai/mini_racer_rs/issues/95)) ([7d21103](https://github.com/andymai/mini_racer_rs/commit/7d2110338352c0259eef2e4f22f439a4875765eb))
|
|
9
|
+
|
|
10
|
+
## [1.0.5](https://github.com/andymai/mini_racer_rs/compare/v1.0.4...v1.0.5) (2026-10-19)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Performance
|
|
14
|
+
|
|
15
|
+
* **encode:** send Ruby strings to V8 in the form V8 stores them ([#93](https://github.com/andymai/mini_racer_rs/issues/93)) ([62f9c85](https://github.com/andymai/mini_racer_rs/commit/62f9c851917cb0ce7d760b5fc545aa014df0a53b))
|
|
16
|
+
|
|
3
17
|
## [1.0.4](https://github.com/andymai/mini_racer_rs/compare/v1.0.3...v1.0.4) (2026-10-19)
|
|
4
18
|
|
|
5
19
|
|
data/COMPATIBILITY.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
| Engine | Done | Planned | Deviation |
|
|
6
6
|
|---|---|---|---|
|
|
7
|
-
| CRuby | 54 | 0 |
|
|
7
|
+
| CRuby | 54 | 0 | 27 |
|
|
8
8
|
| TruffleRuby | 58 | 0 | 0 |
|
|
9
9
|
|
|
10
10
|
## Surface
|
|
@@ -192,6 +192,12 @@ mini_racer_rs returns the same filtered results from a snapshot-based context as
|
|
|
192
192
|
|
|
193
193
|
Approved 2026-10-16.
|
|
194
194
|
|
|
195
|
+
### `behavior:stack-exhaustion@ruby`
|
|
196
|
+
|
|
197
|
+
mini_racer_rs raises `SystemStackError` with `stack level too deep` when converting a JavaScript result on a Ruby fiber or thread with too little machine stack remaining for Ruby to call a C function. When converting arguments for an attached Ruby callable, mini_racer_rs does not call the callable and gives JavaScript an Error with `stack level too deep`; if uncaught, `eval` raises that `SystemStackError` in Ruby. In both cases, the context remains usable and can evaluate `1 + 1` to `2` and dispose. Across 736 runs on 128 KiB fiber and thread stacks, using release and debug builds with and without `GC.stress` and compaction, mini_racer_rs never hung or crashed. On the same stack sizes without `GC.stress`, across 120 runs per conversion kind, mini_racer 0.22.1 results returned 59 times, raised `SystemStackError` 49 times, and hung 12 times, all for values containing Latin-1 or two-byte strings. mini_racer 0.22.1 callbacks delivered `stack level too deep` to JavaScript 87 times, returned 7 times, crashed with a segmentation fault 10 times, hung 8 times, and left 8 contexts unable to dispose with `context dispose: Device or resource busy`; with `GC.stress` and compaction, many result and callback runs aborted with `[BUG] system stack overflow during GC`. The tradeoff is stack cost: in a release build, a 95-level object result needed about 30 KiB beyond Ruby's method-call limit with mini_racer_rs, versus about 10 KiB with mini_racer 0.22.1, so near the end of a stack mini_racer_rs refuses some results that mini_racer 0.22.1 returns. At Ruby 3.4's default machine stacks, 512 KiB for a fiber and 1 MiB for a thread, mini_racer_rs converts every kind of value nested 95 levels deep.
|
|
198
|
+
|
|
199
|
+
Approved 2026-10-18.
|
|
200
|
+
|
|
195
201
|
### `behavior:timeout-bounds-leftover-microtasks@ruby`
|
|
196
202
|
|
|
197
203
|
mini_racer_rs applies the context’s timeout once more while running microtasks left by a script that reached its own timeout. Both gems run leftover microtasks before returning, but mini_racer 0.22.1's watchdog has already fired for that call, so it runs them without a time limit. For example, with `timeout: 200`, evaluating `Promise.resolve().then(() => { while (true) {} }); while (true) {}` never returns in mini_racer. In mini_racer_rs it raises `MiniRacer::ScriptTerminatedError` after about 400 ms, and the context remains usable. This also means mini_racer_rs cuts short a finite leftover microtask that needs more than the second 200 ms, while mini_racer finishes it. Microtasks left by a script ended another way, such as by `stop`, a memory limit, or an interrupt, keep the original deadline in both gems. Without `timeout:`, both run leftover microtasks without a limit. mini_racer_rs differs because `timeout:` often bounds untrusted JavaScript, and an endless microtask should not remove that bound.
|
data/Cargo.lock
CHANGED
|
@@ -458,7 +458,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
|
458
458
|
|
|
459
459
|
[[package]]
|
|
460
460
|
name = "mini_racer_engine"
|
|
461
|
-
version = "1.0.
|
|
461
|
+
version = "1.0.6"
|
|
462
462
|
dependencies = [
|
|
463
463
|
"deno_core_icudata",
|
|
464
464
|
"libc",
|
|
@@ -470,7 +470,7 @@ dependencies = [
|
|
|
470
470
|
|
|
471
471
|
[[package]]
|
|
472
472
|
name = "mini_racer_rs"
|
|
473
|
-
version = "1.0.
|
|
473
|
+
version = "1.0.6"
|
|
474
474
|
dependencies = [
|
|
475
475
|
"magnus",
|
|
476
476
|
"mini_racer_engine",
|
|
@@ -481,7 +481,7 @@ dependencies = [
|
|
|
481
481
|
|
|
482
482
|
[[package]]
|
|
483
483
|
name = "mini_racer_sync"
|
|
484
|
-
version = "1.0.
|
|
484
|
+
version = "1.0.6"
|
|
485
485
|
dependencies = [
|
|
486
486
|
"loom",
|
|
487
487
|
"thiserror",
|
|
@@ -489,7 +489,7 @@ dependencies = [
|
|
|
489
489
|
|
|
490
490
|
[[package]]
|
|
491
491
|
name = "mini_racer_wire"
|
|
492
|
-
version = "1.0.
|
|
492
|
+
version = "1.0.6"
|
|
493
493
|
dependencies = [
|
|
494
494
|
"proptest",
|
|
495
495
|
"thiserror",
|
data/Cargo.toml
CHANGED
data/README.md
CHANGED
|
@@ -14,8 +14,10 @@ mod spin;
|
|
|
14
14
|
reason = "the engine's only FFI fence: symbols the linker routes into V8"
|
|
15
15
|
)]
|
|
16
16
|
mod sys;
|
|
17
|
+
mod text;
|
|
17
18
|
|
|
18
19
|
pub use context::{ContextHandle, Host, Unparker, WaitHandle, WaitOutcome};
|
|
19
20
|
pub use control::{CallRequest, CallbackReply, ContextConfig, JsText};
|
|
20
21
|
pub use error::{EngineError, FlagError, InvalidSnapshot, JsError, JsErrorKind, TerminationCause};
|
|
21
22
|
pub use snapshot::{SnapshotBlob, WeakSnapshotBlob};
|
|
23
|
+
pub use text::{Decoded, Transcoder};
|
|
@@ -4,6 +4,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
4
4
|
use mini_racer_wire::{WRITE_VERSION, tag};
|
|
5
5
|
use v8::ValueDeserializerHelper;
|
|
6
6
|
|
|
7
|
+
use crate::sys;
|
|
8
|
+
|
|
7
9
|
// Deeper values are left to V8, which reads as deep as its stack allows.
|
|
8
10
|
const MAX_DEPTH: u32 = 64;
|
|
9
11
|
// Far below V8's own limit on an array's length, which it enforces itself.
|
|
@@ -48,16 +50,16 @@ pub const fn worth_building(objects: usize, properties: usize) -> bool {
|
|
|
48
50
|
/// which its deserializer makes, element by element as a sparse array, so a
|
|
49
51
|
/// function returning an argument costs far more than it need.
|
|
50
52
|
///
|
|
51
|
-
/// A two-byte string is read by V8's own
|
|
52
|
-
///
|
|
53
|
-
///
|
|
54
|
-
///
|
|
55
|
-
///
|
|
56
|
-
///
|
|
57
|
-
///
|
|
58
|
-
///
|
|
59
|
-
///
|
|
60
|
-
/// (`TermCell::refuses`).
|
|
53
|
+
/// A two-byte string whose units all fit Latin-1 is read by V8's own
|
|
54
|
+
/// deserializer from its bytes alone, which keeps it two-byte as V8's read of
|
|
55
|
+
/// the whole would. The whole request is `Unbuilt` for anything else this would
|
|
56
|
+
/// not build as V8 does or that V8 would refuse (a binary, a key that is not a
|
|
57
|
+
/// string or an int32, a key given twice, an array inside itself, an object of
|
|
58
|
+
/// more than `MAX_KEYS` keys, nesting past `MAX_DEPTH`), decided from the bytes
|
|
59
|
+
/// before any value is made. A termination requested as it reads leaves it
|
|
60
|
+
/// `Unbuilt` too: V8 acts on one before each value it reads, an array or object
|
|
61
|
+
/// beginning as a value does, so this looks before the request and before each
|
|
62
|
+
/// value. One requested before the dispatch refused it (`TermCell::refuses`).
|
|
61
63
|
pub fn read<'s, 'b>(
|
|
62
64
|
scope: &v8::PinScope<'s, '_>,
|
|
63
65
|
bytes: &'b [u8],
|
|
@@ -74,6 +76,7 @@ pub fn read<'s, 'b>(
|
|
|
74
76
|
objects: Vec::new(),
|
|
75
77
|
elements: Vec::new(),
|
|
76
78
|
shapes: Vec::new(),
|
|
79
|
+
units: Vec::new(),
|
|
77
80
|
};
|
|
78
81
|
match builder.call() {
|
|
79
82
|
Some(call) => Read::Built(call),
|
|
@@ -119,6 +122,17 @@ impl<'b> Cursor<'b> {
|
|
|
119
122
|
}
|
|
120
123
|
}
|
|
121
124
|
|
|
125
|
+
// A value's tag, past the padding V8 skips, which aligns a two-byte
|
|
126
|
+
// string's units.
|
|
127
|
+
fn tag(&mut self) -> Option<u8> {
|
|
128
|
+
loop {
|
|
129
|
+
match self.byte()? {
|
|
130
|
+
tag::PADDING => {}
|
|
131
|
+
tag => return Some(tag),
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
122
136
|
fn varint(&mut self) -> Option<u64> {
|
|
123
137
|
let mut value = 0u64;
|
|
124
138
|
for shift in (0..64).step_by(7) {
|
|
@@ -153,9 +167,9 @@ impl<'b> Cursor<'b> {
|
|
|
153
167
|
.filter(|text| text.len() <= v8::String::MAX_LENGTH)
|
|
154
168
|
}
|
|
155
169
|
|
|
156
|
-
fn two_byte(&mut self) -> Option
|
|
157
|
-
|
|
158
|
-
|
|
170
|
+
fn two_byte(&mut self) -> Option<&'b [u8]> {
|
|
171
|
+
self.text()
|
|
172
|
+
.filter(|text| text.len().is_multiple_of(2) && text.len() / 2 <= v8::String::MAX_LENGTH)
|
|
159
173
|
}
|
|
160
174
|
|
|
161
175
|
// The length of `[path, ...arguments]`, past V8's header.
|
|
@@ -312,7 +326,7 @@ impl Check<'_> {
|
|
|
312
326
|
}
|
|
313
327
|
|
|
314
328
|
fn value(&mut self, depth: u32) -> Option<()> {
|
|
315
|
-
match self.bytes.
|
|
329
|
+
match self.bytes.tag()? {
|
|
316
330
|
tag::INT32 => {
|
|
317
331
|
self.bytes.varint_u32()?;
|
|
318
332
|
}
|
|
@@ -323,7 +337,9 @@ impl Check<'_> {
|
|
|
323
337
|
tag::UTF8_STRING | tag::ONE_BYTE_STRING => {
|
|
324
338
|
self.bytes.string()?;
|
|
325
339
|
}
|
|
326
|
-
tag::TWO_BYTE_STRING =>
|
|
340
|
+
tag::TWO_BYTE_STRING => {
|
|
341
|
+
self.bytes.two_byte()?;
|
|
342
|
+
}
|
|
327
343
|
tag::BIGINT => {
|
|
328
344
|
self.bytes.bigint_bytes()?;
|
|
329
345
|
}
|
|
@@ -393,6 +409,9 @@ struct Builder<'s, 'b, 'p, 'i> {
|
|
|
393
409
|
// The names last made for each key position at each depth, so objects of
|
|
394
410
|
// one shape share them instead of looking each up again.
|
|
395
411
|
shapes: Vec<Vec<(Key<'b>, v8::Local<'s, v8::String>)>>,
|
|
412
|
+
// A two-byte string's units where the request's bytes hold them
|
|
413
|
+
// unaligned.
|
|
414
|
+
units: Vec<u16>,
|
|
396
415
|
}
|
|
397
416
|
|
|
398
417
|
// Reads again what `Check` accepted, so only a termination stops it, or V8's
|
|
@@ -419,7 +438,7 @@ impl<'s, 'b> Builder<'s, 'b, '_, '_> {
|
|
|
419
438
|
self.unterminated()?;
|
|
420
439
|
let scope = self.scope;
|
|
421
440
|
let at = self.bytes;
|
|
422
|
-
Some(match self.bytes.
|
|
441
|
+
Some(match self.bytes.tag()? {
|
|
423
442
|
tag::INT32 => v8::Integer::new(scope, zigzag(self.bytes.varint_u32()?)).into(),
|
|
424
443
|
tag::DOUBLE => v8::Number::new(scope, self.bytes.double()?).into(),
|
|
425
444
|
tag::NULL => v8::null(scope).into(),
|
|
@@ -459,17 +478,34 @@ impl<'s, 'b> Builder<'s, 'b, '_, '_> {
|
|
|
459
478
|
tag::BEGIN_DENSE_ARRAY => self.array(depth.checked_sub(1)?)?,
|
|
460
479
|
tag::BEGIN_OBJECT => self.object(depth.checked_sub(1)?)?,
|
|
461
480
|
tag::TWO_BYTE_STRING => {
|
|
462
|
-
self.bytes.two_byte()?;
|
|
463
|
-
self.two_byte(at)?
|
|
481
|
+
let text = self.bytes.two_byte()?;
|
|
482
|
+
self.two_byte(text, at)?
|
|
464
483
|
}
|
|
465
484
|
_ => return None,
|
|
466
485
|
})
|
|
467
486
|
}
|
|
468
487
|
|
|
469
|
-
//
|
|
470
|
-
//
|
|
471
|
-
//
|
|
472
|
-
|
|
488
|
+
// V8's API makes a one-byte string of units that all fit Latin-1, where
|
|
489
|
+
// its deserializer keeps them two-byte, so only a string with a wider
|
|
490
|
+
// unit is made through the API, from the request's own bytes where the
|
|
491
|
+
// padding before them aligned them.
|
|
492
|
+
fn two_byte(&mut self, text: &[u8], at: Cursor<'b>) -> Option<v8::Local<'s, v8::Value>> {
|
|
493
|
+
let pairs = text.as_chunks::<2>().0;
|
|
494
|
+
if !pairs.iter().any(|&[_, high]| high != 0) {
|
|
495
|
+
return self.deserialized(at);
|
|
496
|
+
}
|
|
497
|
+
let units = if let Some(units) = sys::units(text) {
|
|
498
|
+
units
|
|
499
|
+
} else {
|
|
500
|
+
self.units.clear();
|
|
501
|
+
self.units
|
|
502
|
+
.extend(pairs.iter().map(|&pair| u16::from_le_bytes(pair)));
|
|
503
|
+
&self.units
|
|
504
|
+
};
|
|
505
|
+
v8::String::new_from_two_byte(self.scope, units, v8::NewStringType::Normal).map(Into::into)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
fn deserialized(&mut self, at: Cursor<'b>) -> Option<v8::Local<'s, v8::Value>> {
|
|
473
509
|
let len = at.rest.len().checked_sub(self.bytes.rest.len())?;
|
|
474
510
|
let mut bytes = vec![tag::HEADER, u8::try_from(WRITE_VERSION).ok()?];
|
|
475
511
|
bytes.extend_from_slice(at.rest.get(..len)?);
|
|
@@ -121,6 +121,43 @@ unsafe extern "C" fn release_two_byte(data: *mut u16, length: usize) {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
/// The UTF-16 units `bytes` hold, in the host's order as V8's deserializer
|
|
125
|
+
/// reads them, without a copy; `None` when they do not start where a unit
|
|
126
|
+
/// can be read from or hold an odd byte.
|
|
127
|
+
#[must_use]
|
|
128
|
+
pub fn units(bytes: &[u8]) -> Option<&[u16]> {
|
|
129
|
+
// SAFETY: every bit pattern is a valid u16, and `align_to` hands out only
|
|
130
|
+
// the aligned middle of `bytes`, borrowed for as long as `bytes` is.
|
|
131
|
+
let (head, units, tail) = unsafe { bytes.align_to::<u16>() };
|
|
132
|
+
(head.is_empty() && tail.is_empty()).then_some(units)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// Writes the Latin-1 bytes of `text` at the start of `out` when `text` is
|
|
136
|
+
/// valid UTF-8 and every character in it is at most U+00FF, and returns how
|
|
137
|
+
/// many; `None` otherwise, or when `out` is shorter than `text`.
|
|
138
|
+
pub fn utf8_to_latin1(text: &[u8], out: &mut [u8]) -> Option<usize> {
|
|
139
|
+
if out.len() < text.len() {
|
|
140
|
+
return None;
|
|
141
|
+
}
|
|
142
|
+
// SAFETY: simdutf writes at most one byte per byte of `text`, which `out`
|
|
143
|
+
// has room for.
|
|
144
|
+
let converted = unsafe { v8::simdutf::convert_utf8_to_latin1_with_errors(text, out) };
|
|
145
|
+
converted.is_ok().then_some(converted.count)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// Writes the UTF-16 units of `text` at the start of `out` when `text` is
|
|
149
|
+
/// valid UTF-8, and returns how many; `None` otherwise, or when `out` is
|
|
150
|
+
/// shorter than `text`.
|
|
151
|
+
pub fn utf8_to_utf16(text: &[u8], out: &mut [u16]) -> Option<usize> {
|
|
152
|
+
if out.len() < text.len() {
|
|
153
|
+
return None;
|
|
154
|
+
}
|
|
155
|
+
// SAFETY: simdutf writes at most one unit per byte of `text`, which `out`
|
|
156
|
+
// has room for.
|
|
157
|
+
let converted = unsafe { v8::simdutf::convert_utf8_to_utf16le_with_errors(text, out) };
|
|
158
|
+
converted.is_ok().then_some(converted.count)
|
|
159
|
+
}
|
|
160
|
+
|
|
124
161
|
/// The address V8 names `isolate` by when it posts the isolate a task.
|
|
125
162
|
#[must_use]
|
|
126
163
|
pub fn isolate_key(isolate: &v8::Isolate) -> usize {
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
use crate::sys;
|
|
2
|
+
|
|
3
|
+
// V8 decodes shorter UTF-8 faster than a conversion starts: converting all of
|
|
4
|
+
// it made arrays of short BMP and emoji strings about a tenth slower to send.
|
|
5
|
+
const SHORT: usize = 16;
|
|
6
|
+
// UTF-8 longer than this goes to V8 as mini_racer sends it. Decoded here, it
|
|
7
|
+
// takes a buffer of two bytes per byte and a request up to twice its length, both
|
|
8
|
+
// under the GVL: a 100 MB String with one character past Latin-1 would peak
|
|
9
|
+
// near 500 MB where its UTF-8 takes 200 MB. Below this, that extra memory
|
|
10
|
+
// stays within 24 MiB, and the values measured, up to 1 MB, all gain.
|
|
11
|
+
const LONG: usize = 8 << 20;
|
|
12
|
+
const WIDE_LEAD: u8 = 0xC4;
|
|
13
|
+
|
|
14
|
+
/// The characters of valid UTF-8 as V8 stores them once it has decoded it:
|
|
15
|
+
/// one byte each when every one is at most U+00FF, UTF-16 units otherwise.
|
|
16
|
+
pub enum Decoded<'a> {
|
|
17
|
+
OneByte(&'a [u8]),
|
|
18
|
+
TwoByte(&'a [u16]),
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Decodes UTF-8 ahead of V8, which copies one-byte and two-byte text where
|
|
22
|
+
/// it would decode UTF-8 a byte at a time, into buffers kept for the next
|
|
23
|
+
/// text.
|
|
24
|
+
#[derive(Default)]
|
|
25
|
+
pub struct Transcoder {
|
|
26
|
+
one_byte: Vec<u8>,
|
|
27
|
+
two_byte: Vec<u16>,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl Transcoder {
|
|
31
|
+
/// `text` as V8 would decode it; `None` when it is short enough for V8 to
|
|
32
|
+
/// decode as fast, past `LONG` bytes, or not valid UTF-8, whose
|
|
33
|
+
/// replacement characters are V8's to choose.
|
|
34
|
+
#[expect(
|
|
35
|
+
clippy::inline_always,
|
|
36
|
+
reason = "the encoder calls it for each non-ASCII value; outlined, it slowed short ones 12%"
|
|
37
|
+
)]
|
|
38
|
+
#[inline(always)]
|
|
39
|
+
pub fn decode(&mut self, text: &[u8]) -> Option<Decoded<'_>> {
|
|
40
|
+
if text.len() < SHORT || text.len() > LONG {
|
|
41
|
+
return None;
|
|
42
|
+
}
|
|
43
|
+
self.decode_long(text)
|
|
44
|
+
}
|
|
45
|
+
|
|
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
|
+
fn decode_long(&mut self, text: &[u8]) -> Option<Decoded<'_>> {
|
|
50
|
+
if text.iter().fold(0, |most, &byte| most.max(byte)) < WIDE_LEAD {
|
|
51
|
+
grow(&mut self.one_byte, text.len());
|
|
52
|
+
let count = sys::utf8_to_latin1(text, &mut self.one_byte)?;
|
|
53
|
+
return self.one_byte.get(..count).map(Decoded::OneByte);
|
|
54
|
+
}
|
|
55
|
+
grow(&mut self.two_byte, text.len());
|
|
56
|
+
let count = sys::utf8_to_utf16(text, &mut self.two_byte)?;
|
|
57
|
+
self.two_byte.get(..count).map(Decoded::TwoByte)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Each has room for as many units as the text has bytes, which simdutf needs,
|
|
62
|
+
// and is made afresh as it grows: the allocator hands out a long one's zeroed
|
|
63
|
+
// pages untouched, where zeroing it in place would commit room for three
|
|
64
|
+
// times the units a CJK text decodes to.
|
|
65
|
+
fn grow<T: Copy + Default>(buffer: &mut Vec<T>, len: usize) {
|
|
66
|
+
if buffer.len() < len {
|
|
67
|
+
*buffer = vec![T::default(); len];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[cfg(test)]
|
|
72
|
+
mod tests {
|
|
73
|
+
use super::{Decoded, LONG, SHORT, Transcoder};
|
|
74
|
+
|
|
75
|
+
// What V8 makes of valid UTF-8, worked out from its characters.
|
|
76
|
+
fn expected(text: &str) -> (Option<Vec<u8>>, Vec<u16>) {
|
|
77
|
+
let latin1 = text.chars().map(|c| u8::try_from(c).ok()).collect();
|
|
78
|
+
(latin1, text.encode_utf16().collect())
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fn check(transcoder: &mut Transcoder, text: &str) {
|
|
82
|
+
let (latin1, utf16) = expected(text);
|
|
83
|
+
match (transcoder.decode(text.as_bytes()), latin1) {
|
|
84
|
+
(Some(Decoded::OneByte(bytes)), Some(latin1)) => assert_eq!(bytes, latin1, "{text:?}"),
|
|
85
|
+
(Some(Decoded::TwoByte(units)), None) => assert_eq!(units, utf16, "{text:?}"),
|
|
86
|
+
(None, _) => assert!(text.len() < SHORT, "{text:?} left to V8"),
|
|
87
|
+
_ => panic!("{text:?} decoded in the wrong form"),
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#[test]
|
|
92
|
+
fn valid_text_decodes_to_the_units_v8_stores() {
|
|
93
|
+
let mut transcoder = Transcoder::default();
|
|
94
|
+
let pad = "the quick brown fox ";
|
|
95
|
+
for text in [
|
|
96
|
+
"caf\u{e9}",
|
|
97
|
+
"caf\u{e9} na\u{ef}ve Z\u{fc}rich cr\u{e8}me \u{ff}",
|
|
98
|
+
"\u{6771}\u{4eac} \u{5927}\u{962a} \u{540d}\u{53e4}\u{5c4b}",
|
|
99
|
+
"\u{1f600} party \u{1f389} fun \u{1f680} go \u{10ffff}",
|
|
100
|
+
"0123456789abcde\u{e9}",
|
|
101
|
+
"0123456789abc\u{e9}",
|
|
102
|
+
"0123456789abcd\u{e9}",
|
|
103
|
+
] {
|
|
104
|
+
check(&mut transcoder, text);
|
|
105
|
+
}
|
|
106
|
+
let ranges = [
|
|
107
|
+
0x80..0x100,
|
|
108
|
+
0x100..0x800,
|
|
109
|
+
0x800..0xD800,
|
|
110
|
+
0xE000..0x1_0000,
|
|
111
|
+
0x1_0000..0x11_0000,
|
|
112
|
+
];
|
|
113
|
+
for range in ranges {
|
|
114
|
+
for start in range.clone().step_by(61) {
|
|
115
|
+
let text: String = (start..range.end.min(start + 23))
|
|
116
|
+
.filter_map(char::from_u32)
|
|
117
|
+
.collect();
|
|
118
|
+
check(&mut transcoder, &format!("{pad}{text}"));
|
|
119
|
+
check(&mut transcoder, &format!("{text}{pad}"));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
let latin1: String = (0x80..0x100).filter_map(char::from_u32).collect();
|
|
123
|
+
check(&mut transcoder, &latin1.repeat(9));
|
|
124
|
+
check(&mut transcoder, &format!("{}\u{100}", latin1.repeat(9)));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Values of a megabyte, the longest measured, are decoded; past `LONG`
|
|
128
|
+
// text goes as UTF-8.
|
|
129
|
+
#[test]
|
|
130
|
+
fn text_up_to_long_is_decoded_and_longer_is_left_to_v8() {
|
|
131
|
+
let mut transcoder = Transcoder::default();
|
|
132
|
+
let megabyte = "caf\u{e9} ".repeat((1 << 20) / 6);
|
|
133
|
+
assert!(matches!(
|
|
134
|
+
transcoder.decode(megabyte.as_bytes()),
|
|
135
|
+
Some(Decoded::OneByte(_))
|
|
136
|
+
));
|
|
137
|
+
let megabyte = "\u{1f600} go ".repeat((1 << 20) / 8);
|
|
138
|
+
assert!(matches!(
|
|
139
|
+
transcoder.decode(megabyte.as_bytes()),
|
|
140
|
+
Some(Decoded::TwoByte(_))
|
|
141
|
+
));
|
|
142
|
+
let wide = "\u{6771}";
|
|
143
|
+
let longest = format!("{wide}{}", "a".repeat(LONG - wide.len()));
|
|
144
|
+
assert_eq!(longest.len(), LONG);
|
|
145
|
+
match transcoder.decode(longest.as_bytes()) {
|
|
146
|
+
Some(Decoded::TwoByte(units)) => assert_eq!(units.len(), LONG - 2),
|
|
147
|
+
_ => panic!("text of `LONG` bytes is decoded"),
|
|
148
|
+
}
|
|
149
|
+
assert!(
|
|
150
|
+
transcoder
|
|
151
|
+
.decode(format!("{longest}a").as_bytes())
|
|
152
|
+
.is_none()
|
|
153
|
+
);
|
|
154
|
+
let latin1 = "\u{e9}".repeat(LONG / 2 + 1);
|
|
155
|
+
assert!(transcoder.decode(latin1.as_bytes()).is_none());
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#[test]
|
|
159
|
+
fn invalid_or_short_text_is_left_to_v8() {
|
|
160
|
+
let mut transcoder = Transcoder::default();
|
|
161
|
+
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
|
+
] {
|
|
175
|
+
for text in [[pad, invalid].concat(), [invalid, pad].concat()] {
|
|
176
|
+
assert!(transcoder.decode(&text).is_none(), "{text:x?}");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
assert!(transcoder.decode("caf\u{e9}".as_bytes()).is_none());
|
|
180
|
+
assert!(transcoder.decode(b"").is_none());
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -157,9 +157,14 @@ impl<'a> Writer<'a> {
|
|
|
157
157
|
}
|
|
158
158
|
self.out.push(tag::TWO_BYTE_STRING);
|
|
159
159
|
self.len(bytes);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
160
|
+
// Written into slots made for them at once, which the compiler turns
|
|
161
|
+
// into wide copies; a unit at a time ran ten times slower.
|
|
162
|
+
let start = self.out.len();
|
|
163
|
+
self.out.resize(start.saturating_add(bytes), 0);
|
|
164
|
+
if let Some(slots) = self.out.get_mut(start..) {
|
|
165
|
+
for (slot, unit) in slots.as_chunks_mut::<2>().0.iter_mut().zip(units) {
|
|
166
|
+
*slot = unit.to_le_bytes();
|
|
167
|
+
}
|
|
163
168
|
}
|
|
164
169
|
}
|
|
165
170
|
|
|
@@ -25,6 +25,12 @@ static JAVASCRIPT_FUNCTION: StaticRoot<RClass> = StaticRoot::new(|ruby| {
|
|
|
25
25
|
.expect("init defines MiniRacer::JavaScriptFunction")
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
+
// Resolved at load, as the error classes are, rather than first near the end
|
|
29
|
+
// of the stack, where its lookup could fail and panic.
|
|
30
|
+
pub fn init(ruby: &Ruby) {
|
|
31
|
+
JAVASCRIPT_FUNCTION.get(ruby);
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
29
35
|
enum Number {
|
|
30
36
|
Integer(i64),
|
|
@@ -273,6 +279,9 @@ struct RubyVisitor<'r> {
|
|
|
273
279
|
// Arrays and sets open around the current value.
|
|
274
280
|
arrays: u32,
|
|
275
281
|
scratch: Vec<u8>,
|
|
282
|
+
// The deepest stack address at which a container began with the room
|
|
283
|
+
// Ruby keeps for a C function still left.
|
|
284
|
+
roomy: usize,
|
|
276
285
|
}
|
|
277
286
|
|
|
278
287
|
impl RubyVisitor<'_> {
|
|
@@ -382,6 +391,24 @@ impl RubyVisitor<'_> {
|
|
|
382
391
|
rb_raw::hash_aset_pairs(ruby, hash, &[[key, value]])
|
|
383
392
|
}
|
|
384
393
|
|
|
394
|
+
// A machine stack that runs out under the decoder's recursion faults, and
|
|
395
|
+
// Ruby's handler then jumps over every Rust frame to raise. The decoder
|
|
396
|
+
// stops instead while Ruby's margin is left, so its error unwinds
|
|
397
|
+
// normally. Only a container deeper than any checked before asks Ruby,
|
|
398
|
+
// since the stack grows down on every supported platform.
|
|
399
|
+
fn check_stack(&mut self) -> Result<(), Error> {
|
|
400
|
+
let here = 0u8;
|
|
401
|
+
let at = std::ptr::from_ref(std::hint::black_box(&here)).addr();
|
|
402
|
+
if at >= self.roomy {
|
|
403
|
+
return Ok(());
|
|
404
|
+
}
|
|
405
|
+
if rb_raw::stack_exhausted(self.ruby) {
|
|
406
|
+
return Err(errors::stack_exhausted(self.ruby));
|
|
407
|
+
}
|
|
408
|
+
self.roomy = at;
|
|
409
|
+
Ok(())
|
|
410
|
+
}
|
|
411
|
+
|
|
385
412
|
fn bad_state<T>(&self) -> Result<T, Error> {
|
|
386
413
|
Err(errors::runtime(self.ruby, "bad state"))
|
|
387
414
|
}
|
|
@@ -559,6 +586,7 @@ impl Visitor for RubyVisitor<'_> {
|
|
|
559
586
|
}
|
|
560
587
|
|
|
561
588
|
fn begin(&mut self, container: Container) -> Result<Begun<Frame, Value>, Error> {
|
|
589
|
+
self.check_stack()?;
|
|
562
590
|
self.flush()?;
|
|
563
591
|
let ruby = self.ruby;
|
|
564
592
|
let frame = match container {
|
|
@@ -732,6 +760,7 @@ pub fn value(ruby: &Ruby, bytes: &[u8]) -> Result<Value, Error> {
|
|
|
732
760
|
keys: None,
|
|
733
761
|
arrays: 0,
|
|
734
762
|
scratch: Vec::new(),
|
|
763
|
+
roomy: usize::MAX,
|
|
735
764
|
};
|
|
736
765
|
decode_value(bytes, RESULT_LIMITS, &mut visitor).map_err(|e| match e {
|
|
737
766
|
DecodeError::Visitor(error) => error,
|
|
@@ -9,7 +9,7 @@ use magnus::{
|
|
|
9
9
|
Error, Fixnum, Float, Module, Object, RArray, RClass, RHash, RModule, RObject, RString, Ruby,
|
|
10
10
|
Time, Value,
|
|
11
11
|
};
|
|
12
|
-
use mini_racer_engine::CallRequest;
|
|
12
|
+
use mini_racer_engine::{CallRequest, Decoded, Transcoder};
|
|
13
13
|
use mini_racer_wire::Writer;
|
|
14
14
|
|
|
15
15
|
use crate::gc_root::{OnceRoot, Pins, StaticRoot};
|
|
@@ -34,6 +34,12 @@ static BINARY: StaticRoot<RClass> = StaticRoot::new(|ruby| {
|
|
|
34
34
|
.expect("the vocabulary defines MiniRacer::Binary")
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
+
// Resolved at load, as the error classes are, rather than first near the end
|
|
38
|
+
// of the stack, where its lookup could fail and panic.
|
|
39
|
+
pub fn init(ruby: &Ruby) {
|
|
40
|
+
BINARY.get(ruby);
|
|
41
|
+
}
|
|
42
|
+
|
|
37
43
|
enum EncodeError {
|
|
38
44
|
// mini_racer's text for a value it cannot send, cut as it cuts it.
|
|
39
45
|
Unsupported(Vec<u8>),
|
|
@@ -56,6 +62,8 @@ impl From<Error> for EncodeError {
|
|
|
56
62
|
enum Item {
|
|
57
63
|
// A pinned value, by its index in the holder.
|
|
58
64
|
Value(usize),
|
|
65
|
+
// A pinned key of a hash.
|
|
66
|
+
Key(usize),
|
|
59
67
|
// An element read when its turn comes, as serialize1 reads them, so a
|
|
60
68
|
// to_time that changes the array is seen.
|
|
61
69
|
Element { array: usize, index: isize },
|
|
@@ -101,6 +109,7 @@ struct Encoder<'r, 'o> {
|
|
|
101
109
|
// whether to build a call's arguments itself.
|
|
102
110
|
objects: usize,
|
|
103
111
|
properties: usize,
|
|
112
|
+
transcoder: Transcoder,
|
|
104
113
|
}
|
|
105
114
|
|
|
106
115
|
/// Writes `value` as `mini_racer`'s `serialize` does, with the object ids V8
|
|
@@ -131,7 +140,7 @@ pub fn call(
|
|
|
131
140
|
let mut encoder = Encoder::new(ruby, &mut bytes);
|
|
132
141
|
let count = length(args.len().saturating_add(1));
|
|
133
142
|
encoder.writer.begin_array(count);
|
|
134
|
-
encoder.string(name);
|
|
143
|
+
encoder.string(name, Text::Name);
|
|
135
144
|
args.iter()
|
|
136
145
|
.try_for_each(|&arg| encoder.top(arg))
|
|
137
146
|
.map_err(|error| error.raise(unsupported))?;
|
|
@@ -153,6 +162,22 @@ impl EncodeError {
|
|
|
153
162
|
}
|
|
154
163
|
}
|
|
155
164
|
|
|
165
|
+
// How mini_racer sends a String, by its encoding's name.
|
|
166
|
+
enum Sent {
|
|
167
|
+
Utf8,
|
|
168
|
+
Latin1,
|
|
169
|
+
Utf16,
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// A key or a call's name goes in the form mini_racer sends unless it is
|
|
173
|
+
// ASCII: the runner builds only calls whose keys are UTF-8 or ASCII, and one
|
|
174
|
+
// is seldom long enough to be worth decoding.
|
|
175
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
176
|
+
enum Text {
|
|
177
|
+
Value,
|
|
178
|
+
Name,
|
|
179
|
+
}
|
|
180
|
+
|
|
156
181
|
fn length(len: usize) -> u32 {
|
|
157
182
|
u32::try_from(len).unwrap_or(u32::MAX)
|
|
158
183
|
}
|
|
@@ -181,6 +206,7 @@ impl<'r, 'o> Encoder<'r, 'o> {
|
|
|
181
206
|
seen: HashMap::default(),
|
|
182
207
|
objects: 0,
|
|
183
208
|
properties: 0,
|
|
209
|
+
transcoder: Transcoder::default(),
|
|
184
210
|
}
|
|
185
211
|
}
|
|
186
212
|
}
|
|
@@ -214,6 +240,10 @@ impl Encoder<'_, '_> {
|
|
|
214
240
|
Item::EndArray(length) => self.writer.end_array(length),
|
|
215
241
|
Item::EndObject(count) => self.writer.end_object(count),
|
|
216
242
|
Item::Value(index) => self.value(self.pinned(index))?,
|
|
243
|
+
Item::Key(index) => {
|
|
244
|
+
let key = self.pinned(index);
|
|
245
|
+
self.key(key, rb_raw::ruby_type(self.ruby, key));
|
|
246
|
+
}
|
|
217
247
|
Item::Element { array, index } => {
|
|
218
248
|
let element = RArray::from_value(self.pinned(array))
|
|
219
249
|
.map(|array| array.entry::<Value>(index))
|
|
@@ -239,14 +269,30 @@ impl Encoder<'_, '_> {
|
|
|
239
269
|
RubyType::True => self.writer.bool(true),
|
|
240
270
|
RubyType::False => self.writer.bool(false),
|
|
241
271
|
RubyType::String => {
|
|
242
|
-
|
|
272
|
+
let text = RString::from_value(value).unwrap_or_else(|| ruby.str_new(""));
|
|
273
|
+
self.string(text, Text::Value);
|
|
243
274
|
}
|
|
244
|
-
RubyType::Symbol => self.string(rb_raw::sym2str(ruby, value)),
|
|
275
|
+
RubyType::Symbol => self.string(rb_raw::sym2str(ruby, value), Text::Value),
|
|
245
276
|
_ => return false,
|
|
246
277
|
}
|
|
247
278
|
true
|
|
248
279
|
}
|
|
249
280
|
|
|
281
|
+
// A String, a Symbol or an Integer, the keys `hash` sends.
|
|
282
|
+
fn key(&mut self, key: Value, kind: RubyType) {
|
|
283
|
+
let ruby = self.ruby;
|
|
284
|
+
match kind {
|
|
285
|
+
RubyType::String => {
|
|
286
|
+
let text = RString::from_value(key).unwrap_or_else(|| ruby.str_new(""));
|
|
287
|
+
self.string(text, Text::Name);
|
|
288
|
+
}
|
|
289
|
+
RubyType::Symbol => self.string(rb_raw::sym2str(ruby, key), Text::Name),
|
|
290
|
+
_ => {
|
|
291
|
+
self.immediate(key, kind);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
250
296
|
fn value(&mut self, value: Value) -> Result<(), EncodeError> {
|
|
251
297
|
let kind = rb_raw::ruby_type(self.ruby, value);
|
|
252
298
|
if self.immediate(value, kind) {
|
|
@@ -338,7 +384,7 @@ impl Encoder<'_, '_> {
|
|
|
338
384
|
);
|
|
339
385
|
let item_kind = rb_raw::ruby_type(self.ruby, item);
|
|
340
386
|
if keys_fit && pairs.is_empty() && immediate(item_kind) {
|
|
341
|
-
self.
|
|
387
|
+
self.key(key, key_kind);
|
|
342
388
|
self.immediate(item, item_kind);
|
|
343
389
|
written = written.saturating_add(1);
|
|
344
390
|
} else {
|
|
@@ -357,7 +403,7 @@ impl Encoder<'_, '_> {
|
|
|
357
403
|
self.work.push(Item::EndObject(count));
|
|
358
404
|
for (key, item) in pairs.into_iter().rev() {
|
|
359
405
|
self.work.push(Item::Value(item));
|
|
360
|
-
self.work.push(Item::
|
|
406
|
+
self.work.push(Item::Key(key));
|
|
361
407
|
}
|
|
362
408
|
Ok(())
|
|
363
409
|
}
|
|
@@ -465,21 +511,43 @@ impl Encoder<'_, '_> {
|
|
|
465
511
|
|
|
466
512
|
// mini_racer tells encodings apart by name. The ones it sends as UTF-8
|
|
467
513
|
// that strings most often have are told by index, without the lookup.
|
|
468
|
-
|
|
514
|
+
// Bytes it sends as UTF-8 that are all ASCII are the same characters as
|
|
515
|
+
// one-byte text, which V8 copies where it would decode UTF-8. A value's
|
|
516
|
+
// other valid UTF-8 goes as the one-byte or two-byte text V8 would decode
|
|
517
|
+
// it to.
|
|
518
|
+
fn string(&mut self, text: RString, kind: Text) {
|
|
469
519
|
let ruby = self.ruby;
|
|
470
520
|
let index = text.enc_get();
|
|
471
521
|
let utf8 = index == ruby.utf8_encindex()
|
|
472
522
|
|| index == ruby.usascii_encindex()
|
|
473
523
|
|| index == ruby.ascii8bit_encindex();
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
}
|
|
524
|
+
let sent = if utf8 {
|
|
525
|
+
Sent::Utf8
|
|
526
|
+
} else {
|
|
478
527
|
match RbEncoding::from(index).name() {
|
|
479
|
-
"ISO-8859-1" =>
|
|
480
|
-
"UTF-16LE" =>
|
|
481
|
-
_ =>
|
|
528
|
+
"ISO-8859-1" => Sent::Latin1,
|
|
529
|
+
"UTF-16LE" => Sent::Utf16,
|
|
530
|
+
_ => Sent::Utf8,
|
|
482
531
|
}
|
|
483
|
-
}
|
|
532
|
+
};
|
|
533
|
+
let writer = &mut self.writer;
|
|
534
|
+
match sent {
|
|
535
|
+
Sent::Latin1 => rb_raw::with_string_bytes(ruby, text, |bytes| writer.latin1(bytes)),
|
|
536
|
+
Sent::Utf16 => rb_raw::with_string_bytes(ruby, text, |bytes| writer.utf16le(bytes)),
|
|
537
|
+
Sent::Utf8 => {
|
|
538
|
+
let ascii = rb_raw::cached_ascii(ruby, text);
|
|
539
|
+
let transcoder = (kind == Text::Value).then_some(&mut self.transcoder);
|
|
540
|
+
rb_raw::with_string_bytes(ruby, text, |bytes| {
|
|
541
|
+
if ascii.unwrap_or_else(|| bytes.is_ascii()) {
|
|
542
|
+
return writer.latin1(bytes);
|
|
543
|
+
}
|
|
544
|
+
match transcoder.and_then(|transcoder| transcoder.decode(bytes)) {
|
|
545
|
+
Some(Decoded::OneByte(latin1)) => writer.latin1(latin1),
|
|
546
|
+
Some(Decoded::TwoByte(units)) => writer.utf16_aligned(units),
|
|
547
|
+
None => writer.utf8(bytes),
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
}
|
|
484
552
|
}
|
|
485
553
|
}
|
|
@@ -38,8 +38,10 @@ macro_rules! roots {
|
|
|
38
38
|
StaticRoot::new(|ruby| resolved(ruby, $module, $name));
|
|
39
39
|
)*
|
|
40
40
|
|
|
41
|
+
// Resolved now, since a root resolved first near the end of the stack
|
|
42
|
+
// would panic on the SystemStackError its Ruby calls raise there.
|
|
41
43
|
pub fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
42
|
-
$( class(ruby, $module, $name)?; )*
|
|
44
|
+
$( class(ruby, $module, $name)?; $root.get(ruby); )*
|
|
43
45
|
SCRIPT.get(ruby).define_method("cause", export!(method cause, 0))?;
|
|
44
46
|
Ok(())
|
|
45
47
|
}
|
|
@@ -154,3 +156,9 @@ pub fn runtime(ruby: &Ruby, message: impl AsRef<[u8]>) -> Error {
|
|
|
154
156
|
pub fn internal(ruby: &Ruby, message: impl AsRef<[u8]>) -> Error {
|
|
155
157
|
raised(ruby, INTERNAL.get(ruby), message)
|
|
156
158
|
}
|
|
159
|
+
|
|
160
|
+
pub const STACK_EXHAUSTED: &str = "stack level too deep";
|
|
161
|
+
|
|
162
|
+
pub fn stack_exhausted(ruby: &Ruby) -> Error {
|
|
163
|
+
Error::new(ruby.exception_sys_stack_error(), STACK_EXHAUSTED)
|
|
164
|
+
}
|
|
@@ -173,10 +173,15 @@ impl Spill {
|
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
// Each holder's class is looked up once, by a Ruby call whose failure panics;
|
|
177
|
+
// near the end of a fiber's stack Ruby fails it with SystemStackError, so it is
|
|
178
|
+
// looked up here instead, at load, with room to spare.
|
|
176
179
|
pub fn init(ruby: &Ruby, rs: RModule) -> Result<(), Error> {
|
|
177
180
|
for name in ["Pins", "Spill"] {
|
|
178
181
|
rs.define_class(name, ruby.class_object())?
|
|
179
182
|
.undef_default_alloc_func();
|
|
180
183
|
}
|
|
184
|
+
Pins::class(ruby);
|
|
185
|
+
Spill::class(ruby);
|
|
181
186
|
Ok(())
|
|
182
187
|
}
|
|
@@ -210,6 +210,12 @@ impl<'r> RubyHost<'r> {
|
|
|
210
210
|
fn run(&self, request: &[u8]) -> Result<Vec<u8>, Error> {
|
|
211
211
|
let ruby = self.ruby;
|
|
212
212
|
let bad = || errors::runtime(ruby, "bad callback id");
|
|
213
|
+
// An unoptimized build's frames from the eval to the decoder's first
|
|
214
|
+
// container take all of Ruby's margin, so a callback is refused here
|
|
215
|
+
// first; the callable would need the room anyway.
|
|
216
|
+
if rb_raw::stack_exhausted(ruby) {
|
|
217
|
+
return Err(errors::stack_exhausted(ruby));
|
|
218
|
+
}
|
|
213
219
|
let args = RArray::from_value(decode::value(ruby, request)?).ok_or_else(bad)?;
|
|
214
220
|
let id = Integer::from_value(args.pop::<Value>()?).ok_or_else(bad)?;
|
|
215
221
|
let id = isize::try_from(id.to_i64()?).map_err(|_| bad())?;
|
|
@@ -243,13 +249,21 @@ fn exception(ruby: &Ruby, error: &Error) -> Option<Exception> {
|
|
|
243
249
|
}
|
|
244
250
|
|
|
245
251
|
// What V8 throws for a raising callback: the exception's to_s, in its own
|
|
246
|
-
// encoding, or mini_racer's fallback text.
|
|
252
|
+
// encoding, or mini_racer's fallback text. A callback refused for want of
|
|
253
|
+
// stack has no room left to call to_s, so its text is the one Ruby raises
|
|
254
|
+
// SystemStackError with, as mini_racer's callbacks hand JavaScript.
|
|
247
255
|
fn message(ruby: &Ruby, exception: Exception) -> JsText {
|
|
248
256
|
exception
|
|
249
257
|
.funcall::<_, _, RString>("to_s", ())
|
|
250
258
|
.ok()
|
|
251
259
|
.and_then(|text| context::js_text(ruby, text).ok())
|
|
252
|
-
.unwrap_or_else(||
|
|
260
|
+
.unwrap_or_else(|| {
|
|
261
|
+
if exception.is_kind_of(ruby.exception_sys_stack_error()) {
|
|
262
|
+
JsText::from(errors::STACK_EXHAUSTED)
|
|
263
|
+
} else {
|
|
264
|
+
JsText::from("Ruby exception")
|
|
265
|
+
}
|
|
266
|
+
})
|
|
253
267
|
}
|
|
254
268
|
|
|
255
269
|
impl Host for RubyHost<'_> {
|
|
@@ -40,6 +40,8 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
40
40
|
racer.define_class("JavaScriptFunction", ruby.class_object())?;
|
|
41
41
|
gc_root::init(ruby, rs)?;
|
|
42
42
|
errors::init(ruby)?;
|
|
43
|
+
decode::init(ruby);
|
|
44
|
+
encode::init(ruby);
|
|
43
45
|
platform::init(ruby, racer)?;
|
|
44
46
|
context::init(ruby, racer)?;
|
|
45
47
|
snapshot::init(ruby, racer)?;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
use std::ffi::{c_int, c_long, c_void};
|
|
2
2
|
|
|
3
|
+
use magnus::encoding::Coderange;
|
|
3
4
|
use magnus::rb_sys::{AsRawId, AsRawValue, FromRawValue, protect};
|
|
4
5
|
use magnus::value::{IntoId, LazyId, ReprValue};
|
|
5
6
|
use magnus::{Error, RArray, RHash, RString, Ruby, Value};
|
|
@@ -104,6 +105,17 @@ pub fn fiber_scheduler(_ruby: &Ruby) -> bool {
|
|
|
104
105
|
scheduler != rb_sys::Qnil as rb_sys::VALUE
|
|
105
106
|
}
|
|
106
107
|
|
|
108
|
+
/// Whether the current thread's or fiber's machine stack has less room left
|
|
109
|
+
/// than Ruby keeps for a C function, where its own method calls raise
|
|
110
|
+
/// `SystemStackError`.
|
|
111
|
+
pub fn stack_exhausted(_ruby: &Ruby) -> bool {
|
|
112
|
+
// SAFETY: the `&Ruby` argument proves this thread holds the GVL, and
|
|
113
|
+
// ruby_stack_check only records the stack pointer, as Ruby's own checks do
|
|
114
|
+
// on every method call, and compares it with the bounds Ruby keeps for the
|
|
115
|
+
// current thread or fiber.
|
|
116
|
+
unsafe { rb_sys::ruby_stack_check() != 0 }
|
|
117
|
+
}
|
|
118
|
+
|
|
107
119
|
pub fn dbl2big(_ruby: &Ruby, n: f64) -> Value {
|
|
108
120
|
// SAFETY: rb_dbl2big allocates a Bignum and requires the GVL, which the
|
|
109
121
|
// `&Ruby` argument proves this thread holds.
|
|
@@ -343,6 +355,18 @@ pub fn sym2str(ruby: &Ruby, symbol: Value) -> RString {
|
|
|
343
355
|
RString::from_value(name).unwrap_or_else(|| ruby.str_new(""))
|
|
344
356
|
}
|
|
345
357
|
|
|
358
|
+
/// Whether every byte of `s` is ASCII, from the coderange Ruby keeps in the
|
|
359
|
+
/// String's flags; `None` while Ruby has not worked it out. Working it out
|
|
360
|
+
/// here would check every character of non-ASCII text one at a time, slower
|
|
361
|
+
/// than reading the bytes. Never 7-bit in an encoding that is not
|
|
362
|
+
/// ASCII-compatible.
|
|
363
|
+
pub fn cached_ascii(_ruby: &Ruby, s: RString) -> Option<bool> {
|
|
364
|
+
match s.enc_coderange() {
|
|
365
|
+
Coderange::Unknown => None,
|
|
366
|
+
known => Some(known == Coderange::SevenBit),
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
346
370
|
/// Runs `read` over the string's bytes without copying them; `read` must not
|
|
347
371
|
/// call Ruby, which could change or move the string.
|
|
348
372
|
pub fn with_string_bytes<R>(_ruby: &Ruby, s: RString, read: impl FnOnce(&[u8]) -> R) -> R {
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mini_racer_rs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Aragon
|
|
@@ -64,6 +64,7 @@ files:
|
|
|
64
64
|
- crates/mini_racer_engine/src/snapshot.rs
|
|
65
65
|
- crates/mini_racer_engine/src/spin.rs
|
|
66
66
|
- crates/mini_racer_engine/src/sys.rs
|
|
67
|
+
- crates/mini_racer_engine/src/text.rs
|
|
67
68
|
- crates/mini_racer_sync/Cargo.toml
|
|
68
69
|
- crates/mini_racer_sync/src/channel.rs
|
|
69
70
|
- crates/mini_racer_sync/src/fork.rs
|