Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CustomAutoRooter #225

Merged
merged 1 commit into from Dec 16, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add support for CustomAutoRooter

  • Loading branch information
michaelwu committed Dec 16, 2015
commit a91d5e46aad928b66ac70dec10f5fc8ab3ee1de3
@@ -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;
@@ -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;
@@ -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 {

This comment has been minimized.

@jdm

jdm Dec 16, 2015

Member

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 }
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.