mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 13:14:48 +00:00
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:
parent
e7445507aa
commit
20a41aa9b5
5 changed files with 47 additions and 19 deletions
58
src/main.rs
58
src/main.rs
|
@ -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>) {
|
||||
|
@ -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);
|
||||
|
@ -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(
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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());
|
||||
|
||||
/*
|
||||
|
@ -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| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue