Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow v8::Data as a generic for get_*_from_snapshot_once #1393

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,29 @@ impl<'s> HandleScope<'s, ()> {
}
}

// TODO(mmastrac): When the never type is stabilized, we can replace this trait
// with type bounds (https://github.com/rust-lang/rust/issues/35121):

// for<'l> DataError: From<<Local<'s, Data> as TryInto<Local<'l, T>>>::Error>,
mod get_data_sealed {
use crate::DataError;
use std::convert::Infallible;

pub trait ToDataError {
fn to_data_error(self) -> DataError;
}
impl ToDataError for DataError {
fn to_data_error(self) -> DataError {
self
}
}
impl ToDataError for Infallible {
fn to_data_error(self) -> DataError {
unreachable!()
}
}
}

impl<'s> HandleScope<'s> {
/// Return data that was previously attached to the isolate snapshot via
/// SnapshotCreator, and removes the reference to it. If called again with
Expand All @@ -259,15 +282,22 @@ impl<'s> HandleScope<'s> {
) -> Result<Local<'s, T>, DataError>
where
T: 'static,
for<'l> Local<'l, Data>: TryInto<Local<'l, T>, Error = DataError>,
for<'l> <Local<'s, Data> as TryInto<Local<'l, T>>>::Error:
get_data_sealed::ToDataError,
for<'l> Local<'l, Data>: TryInto<Local<'l, T>>,
{
unsafe {
self
let Some(res) = self
.cast_local(|sd| {
raw::v8__Isolate__GetDataFromSnapshotOnce(sd.get_isolate_ptr(), index)
})
.ok_or_else(DataError::no_data::<T>)
.and_then(|data| data.try_into())
}) else {
return Err(DataError::no_data::<T>());
};
use get_data_sealed::ToDataError;
match res.try_into() {
Ok(x) => Ok(x),
Err(e) => Err(e.to_data_error()),
}
}
}

Expand All @@ -284,18 +314,22 @@ impl<'s> HandleScope<'s> {
) -> Result<Local<'s, T>, DataError>
where
T: 'static,
for<'l> Local<'l, Data>: TryInto<Local<'l, T>, Error = DataError>,
for<'l> <Local<'s, Data> as TryInto<Local<'l, T>>>::Error:
get_data_sealed::ToDataError,
for<'l> Local<'l, Data>: TryInto<Local<'l, T>>,
{
unsafe {
self
let Some(res) = self
.cast_local(|sd| {
raw::v8__Context__GetDataFromSnapshotOnce(
sd.get_current_context(),
index,
)
})
.ok_or_else(DataError::no_data::<T>)
.and_then(|data| data.try_into())
raw::v8__Context__GetDataFromSnapshotOnce(sd.get_current_context(), index)
}) else {
return Err(DataError::no_data::<T>());
};
use get_data_sealed::ToDataError;
match res.try_into() {
Ok(x) => Ok(x),
Err(e) => Err(e.to_data_error()),
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5277,7 +5277,7 @@
// value.
{
let params = v8::Isolate::create_params().snapshot_blob(startup_data);
let isolate = &mut v8::Isolate::new(params);

Check failure on line 5280 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

temporary value dropped while borrowed
{
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
Expand All @@ -5290,25 +5290,28 @@
let true_val = v8::Boolean::new(scope, true).into();
assert!(result.same_value(true_val));

let isolate_data = scope

Check failure on line 5293 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
.get_isolate_data_from_snapshot_once::<v8::Value>(isolate_data_index);

Check failure on line 5294 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
assert!(isolate_data.unwrap() == v8::Number::new(scope, 1.0));
let no_data_err = scope

Check failure on line 5296 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
.get_isolate_data_from_snapshot_once::<v8::Value>(isolate_data_index);

Check failure on line 5297 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
assert!(matches!(no_data_err, Err(v8::DataError::NoData { .. })));

let context_data = scope

Check failure on line 5300 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
.get_context_data_from_snapshot_once::<v8::Value>(context_data_index);

Check failure on line 5301 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
assert!(context_data.unwrap() == v8::Number::new(scope, 2.0));
let no_data_err = scope

Check failure on line 5303 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
.get_context_data_from_snapshot_once::<v8::Value>(context_data_index);

Check failure on line 5304 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
assert!(matches!(no_data_err, Err(v8::DataError::NoData { .. })));

let bad_type_err = scope

Check failure on line 5307 in tests/test_api.rs

View workflow job for this annotation

GitHub Actions / release x86_64-unknown-linux-gnu

implementation of `TryFrom` is not general enough
.get_context_data_from_snapshot_once::<v8::Private>(
context_data_index_2,
);
assert!(matches!(bad_type_err, Err(v8::DataError::BadType { .. })));
// Ensure we can compile a request for v8::Data
scope
.get_context_data_from_snapshot_once::<v8::Data>(context_data_index_2);
}
}
}
Expand Down
Loading