Skip to content

Commit

Permalink
Fix mutliple isolate borrows with #[stack_trace] (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Jan 14, 2025
1 parent 2ba615a commit 8596923
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
49 changes: 22 additions & 27 deletions ops/op2/dispatch_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,6 @@ pub(crate) fn generate_dispatch_fast(
quote!()
};

let with_isolate = if generator_state.needs_fast_isolate
&& !generator_state.needs_fast_scope
{
generator_state.needs_opctx = true;
gs_quote!(generator_state(opctx, scope) =>
(let mut #scope = unsafe { &mut *#opctx.isolate };)
)
} else {
quote!()
};

let with_opctx = if generator_state.needs_opctx {
generator_state.needs_fast_api_callback_options = true;
gs_quote!(generator_state(opctx, fast_api_callback_options) => {
Expand All @@ -492,15 +481,13 @@ pub(crate) fn generate_dispatch_fast(
};

let with_self = if generator_state.needs_self {
generator_state.needs_fast_api_callback_options = true;
generator_state.needs_fast_isolate = true;
let throw_exception = throw_type_error(
generator_state,
format!("expected {}", &generator_state.self_ty),
);
gs_quote!(generator_state(self_ty, fast_api_callback_options) => {
// SAFETY: Isolate is valid if this function is being called.
let isolate = unsafe { &mut *#fast_api_callback_options.isolate };
let Some(self_) = deno_core::_ops::try_unwrap_cppgc_object::<#self_ty>(isolate, this.into()) else {
gs_quote!(generator_state(self_ty, scope) => {
let Some(self_) = deno_core::_ops::try_unwrap_cppgc_object::<#self_ty>(&mut #scope, this.into()) else {
#throw_exception
};
let self_ = &*self_;
Expand All @@ -509,6 +496,17 @@ pub(crate) fn generate_dispatch_fast(
quote!()
};

let with_isolate = if generator_state.needs_fast_isolate
&& !generator_state.needs_fast_scope
&& !generator_state.needs_stack_trace
{
generator_state.needs_fast_api_callback_options = true;
gs_quote!(generator_state(scope, fast_api_callback_options) =>
(let mut #scope = unsafe { &mut *#fast_api_callback_options.isolate };)
)
} else {
quote!()
};
let with_scope = if generator_state.needs_scope {
let create_scope = create_scope(generator_state);
gs_quote!(generator_state(scope) => {
Expand Down Expand Up @@ -591,8 +589,8 @@ pub(crate) fn generate_dispatch_fast(
#with_opstate;
#with_stack_trace
#with_js_runtime_state
#with_self
#with_isolate
#with_self
let #result = {
#(#call_args)*
#call (#(#call_names),*)
Expand Down Expand Up @@ -632,6 +630,7 @@ fn map_v8_fastcall_arg_to_arg(
needs_scope,
needs_opctx,
needs_fast_api_callback_options,
needs_fast_isolate,
needs_js_runtime_state,
..
} = generator_state;
Expand Down Expand Up @@ -799,13 +798,11 @@ fn map_v8_fastcall_arg_to_arg(
let ty =
syn::parse_str::<syn::Path>(ty).expect("Failed to reparse state type");

*needs_fast_api_callback_options = true;
*needs_fast_isolate = true;
let throw_exception =
throw_type_error(generator_state, format!("expected {ty:?}"));
gs_quote!(generator_state(fast_api_callback_options) => {
// SAFETY: Isolate is valid if this function is being called.
let isolate = unsafe { &mut *#fast_api_callback_options.isolate };
let Some(#arg_ident) = deno_core::_ops::try_unwrap_cppgc_object::<#ty>(isolate, #arg_ident) else {
gs_quote!(generator_state(scope) => {
let Some(#arg_ident) = deno_core::_ops::try_unwrap_cppgc_object::<#ty>(&mut #scope, #arg_ident) else {
#throw_exception
};
let #arg_ident = &*#arg_ident;
Expand All @@ -815,15 +812,13 @@ fn map_v8_fastcall_arg_to_arg(
let ty =
syn::parse_str::<syn::Path>(ty).expect("Failed to reparse state type");

*needs_fast_api_callback_options = true;
*needs_fast_isolate = true;
let throw_exception =
throw_type_error(generator_state, format!("expected {ty:?}"));
gs_quote!(generator_state(fast_api_callback_options) => {
// SAFETY: Isolate is valid if this function is being called.
let isolate = unsafe { &mut *#fast_api_callback_options.isolate };
gs_quote!(generator_state(scope) => {
let #arg_ident = if #arg_ident.is_null_or_undefined() {
None
} else if let Some(#arg_ident) = deno_core::_ops::try_unwrap_cppgc_object::<#ty>(isolate, #arg_ident) {
} else if let Some(#arg_ident) = deno_core::_ops::try_unwrap_cppgc_object::<#ty>(&mut #scope, #arg_ident) {
Some(#arg_ident)
} else {
#throw_exception
Expand Down
16 changes: 8 additions & 8 deletions ops/op2/test_cases/sync/cppgc_resource.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ops/op2/test_cases/sync/object_wrap.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions testing/checkin/runner/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ impl DOMPoint {
#[fast]
#[symbol("symbolMethod")]
fn with_symbol(&self) {}

#[fast]
#[stack_trace]
fn with_stack_trace(&self) {}
}

#[repr(u8)]
Expand Down

0 comments on commit 8596923

Please sign in to comment.