oden/oden-js/src/atom.rs
John Doty 9f808cea31 [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.
2023-06-19 08:28:26 -07:00

58 lines
1.3 KiB
Rust

use crate::{ContextRef, Runtime, ValueResult};
use oden_js_sys as sys;
use std::ops::Deref;
pub struct AtomRef {
pub(crate) atom: sys::JSAtom,
}
impl AtomRef {
pub(crate) fn from_raw(atom: sys::JSAtom, _ctx: &ContextRef) -> AtomRef {
AtomRef { atom }
}
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) -> ValueResult {
context.check_exception(unsafe { sys::JS_AtomToValue(context.ctx, self.atom) })
}
pub fn to_string_value(&self, context: &ContextRef) -> ValueResult {
context.check_exception(unsafe { sys::JS_AtomToString(context.ctx, self.atom) })
}
}
pub struct Atom {
atom: AtomRef,
rt: Runtime,
}
impl Atom {
pub(crate) fn from_raw(atom: sys::JSAtom, ctx: &ContextRef) -> Self {
Atom {
atom: AtomRef::from_raw(atom, ctx),
rt: Runtime::from_raw(unsafe { sys::JS_GetRuntime(ctx.ctx) }),
}
}
}
impl Deref for Atom {
type Target = AtomRef;
fn deref(&self) -> &Self::Target {
&self.atom
}
}
impl Drop for Atom {
fn drop(&mut self) {
unsafe {
sys::JS_FreeAtomRT(self.rt.rt, self.atom.atom);
}
}
}