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

Add a ContextData struct to inject host defined types from the context #3802

Merged
merged 25 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c721858
Add more utility traits and funtions to boa_interop
hansl Mar 29, 2024
a499b88
cargo clippy
hansl Mar 29, 2024
bbcf043
Readd the safe version for Fn which also implements Copy
hansl Apr 1, 2024
4b0f47d
Use a new trait for converting a Rust type to a JsResult<JsValue>
hansl Apr 4, 2024
64fff68
cargo clippy
hansl Apr 4, 2024
529dd78
Add a test for returning result and Fn()
hansl Apr 4, 2024
74da95a
Merge branch 'main' into boa_interop_2
hansl Apr 6, 2024
921b35a
Seal both IntoJsFunction and IntoJsFunctionUnsafe
hansl Apr 6, 2024
d8815ac
Try Borrowing and return a nice error instead of panicking
hansl Apr 6, 2024
78d541f
Address comments
hansl Apr 6, 2024
3ff54d5
Rename into_js_function to into_js_function_copied
hansl Apr 6, 2024
3ea6baf
Move TryIntoJsResult to boa_engine
hansl Apr 8, 2024
35fea4e
Move JsRest to be at the end only of arguments and add JsAll
hansl Apr 8, 2024
deda29b
use from_copy_closure and remove unsafe
hansl Apr 9, 2024
f6dd8a3
Add a HostDefined struct to grab host defined in the argument list
hansl Apr 9, 2024
fc0c7dc
cargo fmt and clippy
hansl Apr 9, 2024
cbaee86
Explain why we need Clone
hansl Apr 9, 2024
644937b
Remove the vector from JsRest<> and use a reference
hansl Apr 9, 2024
2989c91
Merge branch 'main' into boa_interop_3
hansl Apr 10, 2024
2c2e1fb
Add HostDefined to context as Data, and rename the injector in boa_in…
hansl Apr 10, 2024
fffb526
Use TypeError instead if the type was not found in context
hansl Apr 11, 2024
05f1781
Update core/interop/src/lib.rs
hansl Apr 11, 2024
73e71f2
cargo fmt
hansl Apr 11, 2024
911052e
cargo fmt
hansl Apr 11, 2024
757554a
Merge remote-tracking branch 'upstream/main' into boa_interop_3
hansl Apr 18, 2024
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
31 changes: 30 additions & 1 deletion core/engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
realm::Realm,
script::Script,
vm::{ActiveRunnable, CallFrame, Vm},
JsNativeError, JsResult, JsString, JsValue, Source,
HostDefined, JsNativeError, JsResult, JsString, JsValue, NativeObject, Source,
};

use self::intrinsics::StandardConstructor;
Expand Down Expand Up @@ -115,6 +115,8 @@ pub struct Context {

/// Unique identifier for each parser instance used during the context lifetime.
parser_identifier: u32,

data: HostDefined,
}

impl std::fmt::Debug for Context {
Expand Down Expand Up @@ -585,6 +587,32 @@ impl Context {
pub fn can_block(&self) -> bool {
self.can_block
}

/// Insert a type into the context-specific [`HostDefined`] field.
#[inline]
pub fn insert_data<T: NativeObject>(&mut self, value: T) -> Option<Box<T>> {
self.data.insert(value)
}

/// Check if the context-specific [`HostDefined`] has type T.
#[inline]
#[must_use]
pub fn has_data<T: NativeObject>(&self) -> bool {
self.data.has::<T>()
}

/// Remove type T from the context-specific [`HostDefined`], if it exists.
#[inline]
pub fn remove_data<T: NativeObject>(&mut self) -> Option<Box<T>> {
self.data.remove::<T>()
}

/// Get type T from the context-specific [`HostDefined`], if it exists.
#[inline]
#[must_use]
pub fn get_data<T: NativeObject>(&self) -> Option<&T> {
self.data.get::<T>()
}
}

// ==== Private API ====
Expand Down Expand Up @@ -1070,6 +1098,7 @@ impl ContextBuilder {
root_shape,
parser_identifier: 0,
can_block: self.can_block,
data: HostDefined::default(),
};

builtins::set_default_global_bindings(&mut context)?;
Expand Down
48 changes: 24 additions & 24 deletions core/interop/src/into_js_function_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ macro_rules! impl_into_js_function {
($($id: ident: $t: ident),*) => {
impl<$($t,)* R, T> IntoJsFunctionSealed<($($t,)*), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)*) -> R + 'static
{}

impl<$($t,)* R, T> IntoJsFunctionSealed<($($t,)* ContextArgToken,), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)* &mut Context) -> R + 'static
{}

impl<$($t,)* R, T> IntoJsFunctionSealed<($($t,)* JsRest,), R> for T
impl<$($t,)* R, T> IntoJsFunctionSealed<($($t,)* JsRest<'_>,), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)* JsRest) -> R + 'static
T: FnMut($($t,)* JsRest<'_>) -> R + 'static
{}

impl<$($t,)* R, T> IntoJsFunctionSealed<($($t,)* JsRest, ContextArgToken), R> for T
impl<$($t,)* R, T> IntoJsFunctionSealed<($($t,)* JsRest<'_>, ContextArgToken), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)* JsRest, &mut Context) -> R + 'static
T: FnMut($($t,)* JsRest<'_>, &mut Context) -> R + 'static
{}

impl<$($t,)* R, T> UnsafeIntoJsFunction<($($t,)*), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)*) -> R + 'static,
{
Expand All @@ -68,11 +68,11 @@ macro_rules! impl_into_js_function {
}
}

impl<$($t,)* R, T> UnsafeIntoJsFunction<($($t,)* JsRest,), R> for T
impl<$($t,)* R, T> UnsafeIntoJsFunction<($($t,)* JsRest<'_>,), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)* JsRest) -> R + 'static,
T: FnMut($($t,)* JsRest<'_>) -> R + 'static,
{
#[allow(unused_variables)]
unsafe fn into_js_function_unsafe(self, _context: &mut Context) -> NativeFunction {
Expand All @@ -96,7 +96,7 @@ macro_rules! impl_into_js_function {

impl<$($t,)* R, T> UnsafeIntoJsFunction<($($t,)* ContextArgToken,), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)* &mut Context) -> R + 'static,
{
Expand All @@ -116,11 +116,11 @@ macro_rules! impl_into_js_function {
}
}

impl<$($t,)* R, T> UnsafeIntoJsFunction<($($t,)* JsRest, ContextArgToken), R> for T
impl<$($t,)* R, T> UnsafeIntoJsFunction<($($t,)* JsRest<'_>, ContextArgToken), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: FnMut($($t,)* JsRest, &mut Context) -> R + 'static,
T: FnMut($($t,)* JsRest<'_>, &mut Context) -> R + 'static,
{
#[allow(unused_variables)]
unsafe fn into_js_function_unsafe(self, _context: &mut Context) -> NativeFunction {
Expand All @@ -141,7 +141,7 @@ macro_rules! impl_into_js_function {
// Safe versions for `Fn(..) -> ...`.
impl<$($t,)* R, T> IntoJsFunctionCopied<($($t,)*), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: Fn($($t,)*) -> R + 'static + Copy,
{
Expand All @@ -159,11 +159,11 @@ macro_rules! impl_into_js_function {
}
}

impl<$($t,)* R, T> IntoJsFunctionCopied<($($t,)* JsRest,), R> for T
impl<$($t,)* R, T> IntoJsFunctionCopied<($($t,)* JsRest<'_>,), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: Fn($($t,)* JsRest) -> R + 'static + Copy,
T: Fn($($t,)* JsRest<'_>) -> R + 'static + Copy,
{
#[allow(unused_variables)]
fn into_js_function_copied(self, _context: &mut Context) -> NativeFunction {
Expand All @@ -181,7 +181,7 @@ macro_rules! impl_into_js_function {

impl<$($t,)* R, T> IntoJsFunctionCopied<($($t,)* ContextArgToken,), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: Fn($($t,)* &mut Context) -> R + 'static + Copy,
{
Expand All @@ -199,11 +199,11 @@ macro_rules! impl_into_js_function {
}
}

impl<$($t,)* R, T> IntoJsFunctionCopied<($($t,)* JsRest, ContextArgToken), R> for T
impl<$($t,)* R, T> IntoJsFunctionCopied<($($t,)* JsRest<'_>, ContextArgToken), R> for T
where
$($t: TryFromJsArgument + 'static,)*
$($t: for<'a> TryFromJsArgument<'a> + 'static,)*
R: TryIntoJsResult,
T: Fn($($t,)* JsRest, &mut Context) -> R + 'static + Copy,
T: Fn($($t,)* JsRest<'_>, &mut Context) -> R + 'static + Copy,
{
#[allow(unused_variables)]
fn into_js_function_copied(self, _context: &mut Context) -> NativeFunction {
Expand Down
Loading
Loading