rusty_racer 0.2.7 → 0.2.9
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 +87 -0
- data/README.md +104 -1
- data/ext/rusty_racer/Cargo.toml +1 -1
- data/ext/rusty_racer/src/lib.rs +1022 -216
- data/ext/rusty_racer/src/marshal.rs +76 -1
- data/ext/rusty_racer/src/ops.rs +403 -34
- data/lib/rusty_racer/version.rb +1 -1
- data/lib/rusty_racer.rb +137 -22
- metadata +2 -2
|
@@ -9,7 +9,7 @@ use std::collections::HashMap;
|
|
|
9
9
|
use std::ffi::c_void;
|
|
10
10
|
|
|
11
11
|
use magnus::rb_sys::{AsRawValue, FromRawValue};
|
|
12
|
-
use magnus::value::ReprValue;
|
|
12
|
+
use magnus::value::{Lazy, ReprValue};
|
|
13
13
|
use magnus::{
|
|
14
14
|
Error, ExceptionClass, IntoValue, RArray, RHash, RString, Ruby, TryConvert, Value, prelude::*,
|
|
15
15
|
};
|
|
@@ -78,6 +78,10 @@ pub(crate) enum JsVal {
|
|
|
78
78
|
// Back-reference to an already-emitted container (preserves identity; makes
|
|
79
79
|
// cycles representable instead of truncating at a depth cap).
|
|
80
80
|
Ref(u32),
|
|
81
|
+
// Ruby RustyRacer::HOST_OBJECT <-> a realm's host object (`crate::host_object`):
|
|
82
|
+
// that of the realm an op runs in — a Context's, a Function handle's, a host
|
|
83
|
+
// function's own — both ways. Another realm's host object reads as an object.
|
|
84
|
+
HostObject,
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
// Cycles and sharing are handled by the Ref table (see JsVal::Ref), so this is
|
|
@@ -340,6 +344,14 @@ fn js_to_jsval_d(
|
|
|
340
344
|
// their toString (the spike's primitive escape hatch).
|
|
341
345
|
if value.is_object() && !value.is_function() {
|
|
342
346
|
let obj = v8::Local::<v8::Object>::try_from(value).unwrap();
|
|
347
|
+
// The realm's own host object crosses back as RustyRacer::HOST_OBJECT,
|
|
348
|
+
// not as a Hash of what the bindings keep there. Only an object with no
|
|
349
|
+
// prototype can be it, which keeps the private read off the common path.
|
|
350
|
+
if obj.get_prototype(scope).is_some_and(|p| p.is_null())
|
|
351
|
+
&& crate::existing_host_object(scope).is_some_and(|host| host == obj)
|
|
352
|
+
{
|
|
353
|
+
return JsVal::HostObject;
|
|
354
|
+
}
|
|
343
355
|
let id = match js_container_id(scope, seen, value, obj, depth) {
|
|
344
356
|
Ok(id) => id,
|
|
345
357
|
Err(jsval) => return jsval,
|
|
@@ -539,6 +551,7 @@ fn jsval_to_js_d<'s>(
|
|
|
539
551
|
JsVal::Ref(id) => built
|
|
540
552
|
.get(scope, id)
|
|
541
553
|
.unwrap_or_else(|| v8::undefined(scope).into()),
|
|
554
|
+
JsVal::HostObject => crate::host_object(scope).into(),
|
|
542
555
|
}
|
|
543
556
|
}
|
|
544
557
|
|
|
@@ -669,9 +682,32 @@ fn jsval_to_ruby_d(
|
|
|
669
682
|
.get(id)
|
|
670
683
|
.copied()
|
|
671
684
|
.unwrap_or_else(|| ruby.qnil().as_value()),
|
|
685
|
+
JsVal::HostObject => ruby.get_inner(&HOST_OBJECT),
|
|
672
686
|
})
|
|
673
687
|
}
|
|
674
688
|
|
|
689
|
+
// RustyRacer::HOST_OBJECT: the Ruby value that stands for a realm's host object
|
|
690
|
+
// as it crosses into JS (JsVal::HostObject). The one instance of
|
|
691
|
+
// RustyRacer::HostObject, whose allocator is undefined once it is made, so no
|
|
692
|
+
// other can be; marked for good (Lazy), so it never moves either.
|
|
693
|
+
fn host_object_inspect(_host: Value) -> &'static str {
|
|
694
|
+
"RustyRacer::HOST_OBJECT"
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
pub(crate) static HOST_OBJECT: Lazy<Value> = Lazy::new(|ruby| {
|
|
698
|
+
let class = ruby
|
|
699
|
+
.define_module("RustyRacer")
|
|
700
|
+
.and_then(|module| module.define_class("HostObject", ruby.class_object()))
|
|
701
|
+
.expect("RustyRacer::HostObject is definable");
|
|
702
|
+
class
|
|
703
|
+
.define_method("inspect", magnus::method!(host_object_inspect, 0))
|
|
704
|
+
.expect("HostObject#inspect is definable");
|
|
705
|
+
let host: Value = class.new_instance(()).expect("a HostObject is allocatable");
|
|
706
|
+
host.freeze();
|
|
707
|
+
class.undef_default_alloc_func();
|
|
708
|
+
host
|
|
709
|
+
});
|
|
710
|
+
|
|
675
711
|
// A Ruby String marshalled by its encoding TAG (the tag is the type):
|
|
676
712
|
// - ASCII-8BIT (binary) -> JsVal::Bytes (a JS Uint8Array);
|
|
677
713
|
// - any text encoding -> JsVal::Str (UTF-8). Already-UTF-8 text is taken
|
|
@@ -842,6 +878,11 @@ fn ruby_to_jsval_d(val: Value, seen: &mut RbSeen, depth: u32) -> Result<JsVal, E
|
|
|
842
878
|
}
|
|
843
879
|
// What only a class, or a conversion method, can say.
|
|
844
880
|
//
|
|
881
|
+
// RustyRacer::HOST_OBJECT -> the realm's host object, by identity.
|
|
882
|
+
if val.as_raw() == ruby.get_inner(&HOST_OBJECT).as_raw() {
|
|
883
|
+
return Ok(JsVal::HostObject);
|
|
884
|
+
}
|
|
885
|
+
//
|
|
845
886
|
// Ruby Time -> JS Date. Before the Numeric fallback: magnus's f64 TryConvert
|
|
846
887
|
// coerces a Time via to_f, so it would otherwise marshal as a bare epoch
|
|
847
888
|
// number. Time#to_f is epoch seconds; Date wants ms.
|
|
@@ -1148,4 +1189,38 @@ mod tests {
|
|
|
1148
1189
|
},
|
|
1149
1190
|
);
|
|
1150
1191
|
}
|
|
1192
|
+
|
|
1193
|
+
#[test]
|
|
1194
|
+
fn the_host_object_is_one_per_realm_and_no_property_of_its_global() {
|
|
1195
|
+
in_context(
|
|
1196
|
+
"globalThis.keys = Reflect.ownKeys(globalThis).length",
|
|
1197
|
+
|scope| {
|
|
1198
|
+
let value = jsval_to_js(
|
|
1199
|
+
scope,
|
|
1200
|
+
JsVal::Array {
|
|
1201
|
+
id: 0,
|
|
1202
|
+
items: vec![JsVal::HostObject, JsVal::HostObject],
|
|
1203
|
+
},
|
|
1204
|
+
);
|
|
1205
|
+
let held = crate::host_object(scope);
|
|
1206
|
+
let key = v8::String::new(scope, "held").unwrap();
|
|
1207
|
+
held.create_data_property(scope, key.into(), v8::Integer::new(scope, 7).into());
|
|
1208
|
+
global_set(scope, "v", value);
|
|
1209
|
+
// (…the global's keys grown by `keys` and `v` alone)
|
|
1210
|
+
assert_eq!(
|
|
1211
|
+
as_string(
|
|
1212
|
+
scope,
|
|
1213
|
+
"[v[0] === v[1], v[0].held, Object.getPrototypeOf(v[0]), Reflect.ownKeys(globalThis).length - keys].join()"
|
|
1214
|
+
),
|
|
1215
|
+
"true,7,,2"
|
|
1216
|
+
);
|
|
1217
|
+
// (…another realm's is its own)
|
|
1218
|
+
let other = v8::Context::new(scope, Default::default());
|
|
1219
|
+
let scope = &mut v8::ContextScope::new(scope, other);
|
|
1220
|
+
let fresh = crate::host_object(scope);
|
|
1221
|
+
assert!(fresh != held);
|
|
1222
|
+
assert!(crate::host_object(scope) == fresh);
|
|
1223
|
+
},
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1151
1226
|
}
|