[oden] The big lifetime removal

It turns out that rust can't really reason about the relationship
between the runtime lifetime and the context lifetime in a way that is
actually usable. This removes the lifetime stuff in favor of reference
counting the runtime itself, via a block that we embed in the
pointer. This, I think, it the least worst option here.
This commit is contained in:
John Doty 2023-06-19 08:28:26 -07:00
parent 898b1fe129
commit 9f808cea31
10 changed files with 269 additions and 312 deletions

View file

@ -1,65 +1,58 @@
use crate::{ContextRef, Runtime, ValueResult};
use oden_js_sys as sys;
use std::marker;
use std::ops::Deref;
pub struct AtomRef<'r> {
pub struct AtomRef {
pub(crate) atom: sys::JSAtom,
_marker: marker::PhantomData<&'r Runtime>,
}
impl<'r> AtomRef<'r> {
pub(crate) fn from_raw(atom: sys::JSAtom, _ctx: &ContextRef<'r>) -> AtomRef<'r> {
AtomRef {
atom,
_marker: marker::PhantomData,
}
impl AtomRef {
pub(crate) fn from_raw(atom: sys::JSAtom, _ctx: &ContextRef) -> AtomRef {
AtomRef { atom }
}
pub fn dup(&self, context: &ContextRef<'r>) -> Atom<'r> {
pub fn dup(&self, context: &ContextRef) -> Atom {
unsafe {
sys::JS_DupAtom(context.ctx, self.atom);
}
Atom::from_raw(self.atom, context)
}
pub fn to_value(&self, context: &ContextRef<'r>) -> ValueResult<'r> {
pub fn to_value(&self, context: &ContextRef) -> ValueResult {
context.check_exception(unsafe { sys::JS_AtomToValue(context.ctx, self.atom) })
}
pub fn to_string_value(&self, context: &ContextRef<'r>) -> ValueResult<'r> {
pub fn to_string_value(&self, context: &ContextRef) -> ValueResult {
context.check_exception(unsafe { sys::JS_AtomToString(context.ctx, self.atom) })
}
}
pub struct Atom<'r> {
atom: AtomRef<'r>,
rt: *mut sys::JSRuntime,
_phantom: marker::PhantomData<&'r Runtime>,
pub struct Atom {
atom: AtomRef,
rt: Runtime,
}
impl<'r> Atom<'r> {
pub(crate) fn from_raw(atom: sys::JSAtom, ctx: &ContextRef<'r>) -> Self {
impl Atom {
pub(crate) fn from_raw(atom: sys::JSAtom, ctx: &ContextRef) -> Self {
Atom {
atom: AtomRef::from_raw(atom, ctx),
rt: unsafe { sys::JS_GetRuntime(ctx.ctx) },
_phantom: marker::PhantomData,
rt: Runtime::from_raw(unsafe { sys::JS_GetRuntime(ctx.ctx) }),
}
}
}
impl<'r> Deref for Atom<'r> {
type Target = AtomRef<'r>;
impl Deref for Atom {
type Target = AtomRef;
fn deref(&self) -> &Self::Target {
&self.atom
}
}
impl<'r> Drop for Atom<'r> {
impl Drop for Atom {
fn drop(&mut self) {
unsafe {
sys::JS_FreeAtomRT(self.rt, self.atom.atom);
sys::JS_FreeAtomRT(self.rt.rt, self.atom.atom);
}
}
}