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 d14c1e1
Show file tree
Hide file tree
Showing 2 changed files with 26 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

0 comments on commit d14c1e1

Please sign in to comment.