Skip to content

Commit

Permalink
Add methods to get Cx to ContextInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Jul 24, 2024
1 parent bdf0183 commit 889b75b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
8 changes: 6 additions & 2 deletions crates/neon/src/context/internal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, ffi::c_void, mem::MaybeUninit};

use crate::{
context::ModuleContext,
context::{Cx, ModuleContext},
handle::Handle,
result::NeonResult,
sys::{self, raw},
Expand Down Expand Up @@ -47,7 +47,11 @@ impl Env {
}

pub trait ContextInternal<'cx>: Sized {
fn env(&self) -> Env;
fn cx(&self) -> &Cx<'cx>;
fn cx_mut(&mut self) -> &mut Cx<'cx>;
fn env(&self) -> Env {
self.cx().env
}
}

fn default_main(mut cx: ModuleContext) -> NeonResult<()> {
Expand Down
28 changes: 20 additions & 8 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ impl<'cx> Cx<'cx> {
}

impl<'cx> ContextInternal<'cx> for Cx<'cx> {
fn env(&self) -> Env {
self.env
fn cx(&self) -> &Cx<'cx> {
self
}

fn cx_mut(&mut self) -> &mut Cx<'cx> {
self
}
}

Expand Down Expand Up @@ -664,13 +668,13 @@ impl<'cx> Deref for ModuleContext<'cx> {
type Target = Cx<'cx>;

fn deref(&self) -> &Self::Target {
&self.cx
self.cx()
}
}

impl<'cx> DerefMut for ModuleContext<'cx> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.cx
self.cx_mut()
}
}

Expand Down Expand Up @@ -727,8 +731,12 @@ impl<'cx> ModuleContext<'cx> {
}

impl<'cx> ContextInternal<'cx> for ModuleContext<'cx> {
fn env(&self) -> Env {
self.cx.env
fn cx(&self) -> &Cx<'cx> {
&self.cx
}

fn cx_mut(&mut self) -> &mut Cx<'cx> {
&mut self.cx
}
}

Expand Down Expand Up @@ -873,8 +881,12 @@ impl<'cx> FunctionContext<'cx> {
}

impl<'cx> ContextInternal<'cx> for FunctionContext<'cx> {
fn env(&self) -> Env {
self.cx.env
fn cx(&self) -> &Cx<'cx> {
&self.cx
}

fn cx_mut(&mut self) -> &mut Cx<'cx> {
&mut self.cx
}
}

Expand Down
24 changes: 24 additions & 0 deletions crates/neon/src/types_impl/extract/with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{
context::{Context, Cx},
result::JsResult,
types::extract::TryIntoJs,
};

pub struct With<F>(pub F);

impl<'cx, F, O> TryIntoJs<'cx> for With<F>
where
F: FnOnce(&mut Cx) -> O,
O: TryIntoJs<'cx>,
{
type Value = O::Value;

fn try_into_js<C>(self, cx: &mut C) -> JsResult<'cx, Self::Value>
where
C: Context<'cx>,
{
(self.0)(cx).try_into_js(cx)
}
}

impl<F> super::private::Sealed for With<F> {}

0 comments on commit 889b75b

Please sign in to comment.