Add from_c() to get a Deno object from ptr.

This is a utility function for CodeCache and other handlers.
This commit is contained in:
Ryan Dahl 2018-07-26 17:47:42 -04:00
parent e7445507aa
commit 20a41aa9b5
5 changed files with 47 additions and 19 deletions

View file

@ -78,7 +78,7 @@ rust_test("test_rs") {
source_root = "src/main.rs" source_root = "src/main.rs"
extern = main_extern extern = main_extern
deps = [ deps = [
":deno_bindings", ":libdeno",
] ]
} }

View file

@ -288,6 +288,8 @@ void deno_init() {
v8::V8::Initialize(); v8::V8::Initialize();
} }
void* deno_get_data(Deno* d) { return d->data; }
const char* deno_v8_version() { return v8::V8::GetVersion(); } const char* deno_v8_version() { return v8::V8::GetVersion(); }
// TODO(ry) Remove these when we call deno_reply_start from Rust. // TODO(ry) Remove these when we call deno_reply_start from Rust.

View file

@ -27,6 +27,7 @@ extern "C" {
pub fn deno_new(data: *const c_void, cb: DenoRecvCb) -> *const DenoC; pub fn deno_new(data: *const c_void, cb: DenoRecvCb) -> *const DenoC;
pub fn deno_delete(d: *const DenoC); pub fn deno_delete(d: *const DenoC);
pub fn deno_last_exception(d: *const DenoC) -> *const c_char; pub fn deno_last_exception(d: *const DenoC) -> *const c_char;
pub fn deno_get_data(d: *const DenoC) -> *const c_void;
pub fn deno_set_response(d: *const DenoC, buf: deno_buf); pub fn deno_set_response(d: *const DenoC, buf: deno_buf);
pub fn deno_execute( pub fn deno_execute(
d: *const DenoC, d: *const DenoC,

View file

@ -31,6 +31,9 @@ void deno_set_flags(int* argc, char** argv);
Deno* deno_new(void* data, deno_recv_cb cb); Deno* deno_new(void* data, deno_recv_cb cb);
void deno_delete(Deno* d); void deno_delete(Deno* d);
// Returns the void* data provided in deno_new.
void* deno_get_data(Deno*);
// Returns false on error. // Returns false on error.
// Get error text with deno_last_exception(). // Get error text with deno_last_exception().
// 0 = fail, 1 = success // 0 = fail, 1 = success

View file

@ -6,19 +6,15 @@ extern crate msg_rs as msg_generated;
extern crate url; extern crate url;
use libc::c_int; use libc::c_int;
use libc::c_void;
use std::env; use std::env;
use std::ffi::CStr; use std::ffi::CStr;
use std::ffi::CString; use std::ffi::CString;
use std::mem; use std::mem;
use std::ptr;
mod handlers; mod handlers;
pub use handlers::*; pub use handlers::*;
mod binding; mod binding;
use binding::{
deno_delete, deno_execute, deno_handle_msg_from_js, deno_init,
deno_last_exception, deno_new, deno_set_flags, DenoC,
};
// Returns args passed to V8, followed by args passed to JS // Returns args passed to V8, followed by args passed to JS
fn parse_core_args(args: Vec<String>) -> (Vec<String>, Vec<String>) { fn parse_core_args(args: Vec<String>) -> (Vec<String>, Vec<String>) {
@ -71,7 +67,7 @@ fn set_flags(args: Vec<String>) -> Vec<String> {
let mut c_argc = c_argv.len() as c_int; let mut c_argc = c_argv.len() as c_int;
// Let v8 parse the arguments it recognizes and remove them from c_argv. // Let v8 parse the arguments it recognizes and remove them from c_argv.
unsafe { unsafe {
deno_set_flags(&mut c_argc, c_argv.as_mut_ptr()); binding::deno_set_flags(&mut c_argc, c_argv.as_mut_ptr());
}; };
// If c_argc was updated we have to change the length of c_argv to match. // If c_argc was updated we have to change the length of c_argv to match.
c_argv.truncate(c_argc as usize); c_argv.truncate(c_argc as usize);
@ -89,14 +85,28 @@ fn set_flags(args: Vec<String>) -> Vec<String> {
type DenoException<'a> = &'a str; type DenoException<'a> = &'a str;
struct Deno { pub struct Deno {
ptr: *const DenoC, ptr: *const binding::DenoC,
} }
static DENO_INIT: std::sync::Once = std::sync::ONCE_INIT;
impl Deno { impl Deno {
fn new() -> Deno { fn new<'a>() -> &'a mut Deno {
let ptr = unsafe { deno_new(ptr::null(), deno_handle_msg_from_js) }; DENO_INIT.call_once(|| {
Deno { ptr: ptr } unsafe { binding::deno_init() };
});
let deno_box = Box::new(Deno {
ptr: 0 as *const binding::DenoC,
});
let deno: &'a mut Deno = Box::leak(deno_box);
let external_ptr = deno as *mut _ as *const c_void;
let internal_deno_ptr = unsafe {
binding::deno_new(external_ptr, binding::deno_handle_msg_from_js)
};
deno.ptr = internal_deno_ptr;
deno
} }
fn execute( fn execute(
@ -106,10 +116,11 @@ impl Deno {
) -> Result<(), DenoException> { ) -> Result<(), DenoException> {
let filename = CString::new(js_filename).unwrap(); let filename = CString::new(js_filename).unwrap();
let source = CString::new(js_source).unwrap(); let source = CString::new(js_source).unwrap();
let r = let r = unsafe {
unsafe { deno_execute(self.ptr, filename.as_ptr(), source.as_ptr()) }; binding::deno_execute(self.ptr, filename.as_ptr(), source.as_ptr())
};
if r == 0 { if r == 0 {
let ptr = unsafe { deno_last_exception(self.ptr) }; let ptr = unsafe { binding::deno_last_exception(self.ptr) };
let cstr = unsafe { CStr::from_ptr(ptr) }; let cstr = unsafe { CStr::from_ptr(ptr) };
return Err(cstr.to_str().unwrap()); return Err(cstr.to_str().unwrap());
} }
@ -119,7 +130,7 @@ impl Deno {
impl Drop for Deno { impl Drop for Deno {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { deno_delete(self.ptr) } unsafe { binding::deno_delete(self.ptr) }
} }
} }
@ -136,6 +147,19 @@ fn test_parse_core_args_2() {
assert!(js_args == (vec!["deno".to_string()], vec!["--help".to_string()])); assert!(js_args == (vec!["deno".to_string()], vec!["--help".to_string()]));
} }
pub fn from_c<'a>(d: *const binding::DenoC) -> &'a mut Deno {
let ptr = unsafe { binding::deno_get_data(d) };
let deno_ptr = ptr as *mut Deno;
let deno_box = unsafe { Box::from_raw(deno_ptr) };
Box::leak(deno_box)
}
#[test]
fn test_c_to_rust() {
let d = Deno::new();
let d2 = from_c(d.ptr);
assert!(d.ptr == d2.ptr);
}
static LOGGER: Logger = Logger; static LOGGER: Logger = Logger;
@ -158,8 +182,6 @@ fn main() {
log::set_logger(&LOGGER).unwrap(); log::set_logger(&LOGGER).unwrap();
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
unsafe { deno_init() };
let _js_args = set_flags(env::args().collect()); let _js_args = set_flags(env::args().collect());
/* /*
@ -169,7 +191,7 @@ fn main() {
println!("version: {}", version); println!("version: {}", version);
*/ */
let mut d = Deno::new(); let d = Deno::new();
d.execute("deno_main.js", "denoMain();") d.execute("deno_main.js", "denoMain();")
.unwrap_or_else(|err| { .unwrap_or_else(|err| {