Skip to content

Commit

Permalink
Add from_c() to get a Deno object from ptr.
Browse files Browse the repository at this point in the history
This is a utility function for CodeCache and other handlers.
  • Loading branch information
ry committed Jul 29, 2018
1 parent e744550 commit 20a41aa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ rust_test("test_rs") {
source_root = "src/main.rs"
extern = main_extern
deps = [
":deno_bindings",
":libdeno",
]
}

Expand Down
2 changes: 2 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ void deno_init() {
v8::V8::Initialize();
}

void* deno_get_data(Deno* d) { return d->data; }

const char* deno_v8_version() { return v8::V8::GetVersion(); }

// TODO(ry) Remove these when we call deno_reply_start from Rust.
Expand Down
1 change: 1 addition & 0 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
pub fn deno_new(data: *const c_void, cb: DenoRecvCb) -> *const DenoC;
pub fn deno_delete(d: *const DenoC);
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_execute(
d: *const DenoC,
Expand Down
3 changes: 3 additions & 0 deletions src/deno.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ void deno_set_flags(int* argc, char** argv);
Deno* deno_new(void* data, deno_recv_cb cb);
void deno_delete(Deno* d);

// Returns the void* data provided in deno_new.
void* deno_get_data(Deno*);

// Returns false on error.
// Get error text with deno_last_exception().
// 0 = fail, 1 = success
Expand Down
58 changes: 40 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ extern crate msg_rs as msg_generated;
extern crate url;

use libc::c_int;
use libc::c_void;
use std::env;
use std::ffi::CStr;
use std::ffi::CString;
use std::mem;
use std::ptr;

mod handlers;
pub use handlers::*;
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
fn parse_core_args(args: Vec<String>) -> (Vec<String>, Vec<String>) {
Expand Down Expand Up @@ -71,7 +67,7 @@ fn set_flags(args: Vec<String>) -> Vec<String> {
let mut c_argc = c_argv.len() as c_int;
// Let v8 parse the arguments it recognizes and remove them from c_argv.
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.
c_argv.truncate(c_argc as usize);
Expand All @@ -89,14 +85,28 @@ fn set_flags(args: Vec<String>) -> Vec<String> {

type DenoException<'a> = &'a str;

struct Deno {
ptr: *const DenoC,
pub struct Deno {
ptr: *const binding::DenoC,
}

static DENO_INIT: std::sync::Once = std::sync::ONCE_INIT;

impl Deno {
fn new() -> Deno {
let ptr = unsafe { deno_new(ptr::null(), deno_handle_msg_from_js) };
Deno { ptr: ptr }
fn new<'a>() -> &'a mut Deno {
DENO_INIT.call_once(|| {
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(
Expand All @@ -106,10 +116,11 @@ impl Deno {
) -> Result<(), DenoException> {
let filename = CString::new(js_filename).unwrap();
let source = CString::new(js_source).unwrap();
let r =
unsafe { deno_execute(self.ptr, filename.as_ptr(), source.as_ptr()) };
let r = unsafe {
binding::deno_execute(self.ptr, filename.as_ptr(), source.as_ptr())
};
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) };
return Err(cstr.to_str().unwrap());
}
Expand All @@ -119,7 +130,7 @@ impl Deno {

impl Drop for Deno {
fn drop(&mut self) {
unsafe { deno_delete(self.ptr) }
unsafe { binding::deno_delete(self.ptr) }
}
}

Expand All @@ -136,6 +147,19 @@ fn test_parse_core_args_2() {
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;

Expand All @@ -158,8 +182,6 @@ fn main() {
log::set_logger(&LOGGER).unwrap();
log::set_max_level(log::LevelFilter::Info);

unsafe { deno_init() };

let _js_args = set_flags(env::args().collect());

/*
Expand All @@ -169,7 +191,7 @@ fn main() {
println!("version: {}", version);
*/

let mut d = Deno::new();
let d = Deno::new();

d.execute("deno_main.js", "denoMain();")
.unwrap_or_else(|err| {
Expand Down

0 comments on commit 20a41aa

Please sign in to comment.