Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Add support for CustomAutoRooter #225

Merged
merged 1 commit into from Dec 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/rust.rs
Expand Up @@ -4,7 +4,7 @@

//! Rust wrappers around the raw JS apis

use libc::{size_t, c_uint, c_char};
use libc::{size_t, c_uint, c_char, ptrdiff_t};
use std::char;
use std::ffi;
use std::ptr;
Expand All @@ -23,6 +23,7 @@ use jsapi::{JS_SetErrorReporter, Evaluate3, JSErrorReport};
use jsapi::{JS_SetGCParameter, JSGCParamKey};
use jsapi::{Heap, Cell, HeapCellPostBarrier, HeapCellRelocate, HeapValuePostBarrier, HeapValueRelocate};
use jsapi::{ThingRootKind, ContextFriendFields};
use jsapi::{CustomAutoRooter, AutoGCRooter, _vftable_CustomAutoRooter, AutoGCRooter_enum0};
use jsapi::{Rooted, RootedValue, Handle, MutableHandle, MutableHandleBase, RootedBase};
use jsapi::{MutableHandleValue, HandleValue, HandleObject, HandleBase};
use jsapi::AutoObjectVector;
Expand Down Expand Up @@ -336,6 +337,42 @@ impl<T> Drop for Rooted<T> {
}
}

impl CustomAutoRooter {
pub fn new(cx: *mut JSContext, vftable: &'static _vftable_CustomAutoRooter)
-> CustomAutoRooter {
CustomAutoRooter::new_with_addr(cx, vftable, unsafe { return_address() })
}

pub fn new_with_addr(cx: *mut JSContext, vftable: &'static _vftable_CustomAutoRooter, addr: *const u8) -> CustomAutoRooter {
let ctxfriend: &mut ContextFriendFields = unsafe {
&mut *(cx as *mut ContextFriendFields)
};

let rooter = CustomAutoRooter {
_vftable: vftable as *const _,
_base: AutoGCRooter {
down: ctxfriend.autoGCRooters,
tag_: AutoGCRooter_enum0::CUSTOM as ptrdiff_t,
stackTop: &mut ctxfriend.autoGCRooters as *mut _,
}
};

ctxfriend.autoGCRooters = unsafe {
(addr as *const *const u8).offset(1) as *const AutoGCRooter as *mut _
};
rooter
}
}

impl Drop for CustomAutoRooter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bleh, another destructor on a repr(C) type :/

fn drop(&mut self) {
unsafe {
assert!(*self._base.stackTop == mem::transmute(&self._base));
*self._base.stackTop = self._base.down;
}
}
}

impl Default for jsid {
fn default() -> jsid { JSID_VOID }
}
Expand Down