0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-21 05:02:11 -05:00

fix: make Eternal::get return an Option instead (#1671)

This commit is contained in:
Leo Kettmeir 2024-12-31 06:26:11 -08:00 committed by GitHub
parent f830de59a7
commit 36015e7e25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 10 deletions

View file

@ -1147,16 +1147,17 @@ impl<T> Eternal<T> {
}
}
pub fn get<'s>(&self, scope: &mut HandleScope<'s, ()>) -> Local<'s, T> {
pub fn get<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Option<Local<'s, T>> {
unsafe {
scope
.cast_local(|sd| {
v8__Eternal__Get(
self as *const Self as *const Eternal<Data>,
sd.get_isolate_ptr(),
) as *const T
})
.unwrap()
scope.cast_local(|sd| {
v8__Eternal__Get(
self as *const Self as *const Eternal<Data>,
sd.get_isolate_ptr(),
) as *const T
})
}
}

View file

@ -11990,14 +11990,16 @@ fn test_eternals() {
let str1 = v8::String::new(&mut scope, "hello").unwrap();
assert!(eternal1.is_empty());
assert!(eternal1.get(&mut scope).is_none());
eternal1.set(&mut scope, str1);
assert!(!eternal1.is_empty());
let str1_get = eternal1.get(&mut scope);
let str1_get = eternal1.get(&mut scope).unwrap();
assert_eq!(str1, str1_get);
eternal1.clear();
assert!(eternal1.is_empty());
assert!(eternal1.get(&mut scope).is_none());
}
// Try all 'standalone' methods after isolate has dropped.