0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-30 11:15:13 -05:00
denoland-deno/src/main.rs

162 lines
3.5 KiB
Rust
Raw Normal View History

extern crate flatbuffers;
extern crate futures;
extern crate hyper;
2018-07-26 17:54:22 -04:00
extern crate libc;
2018-09-09 19:21:22 -04:00
extern crate msg_rs as msg;
2018-08-24 00:36:45 +02:00
extern crate rand;
2018-07-24 01:45:23 -04:00
extern crate tempfile;
extern crate tokio;
extern crate url;
2018-07-26 17:54:22 -04:00
#[macro_use]
extern crate log;
extern crate hyper_rustls;
extern crate remove_dir_all;
extern crate ring;
2018-07-26 17:54:22 -04:00
mod deno_dir;
mod errors;
mod flags;
2018-07-26 17:54:22 -04:00
mod fs;
pub mod handlers;
mod libdeno;
mod net;
mod version;
2018-07-04 14:50:28 -04:00
use libc::c_void;
use std::collections::HashMap;
2018-07-25 18:27:27 -07:00
use std::env;
2018-06-16 01:43:23 +02:00
use std::ffi::CStr;
use std::ffi::CString;
2018-06-29 15:07:19 -07:00
type DenoException<'a> = &'a str;
pub struct Deno {
ptr: *const libdeno::DenoC,
2018-07-26 17:54:22 -04:00
dir: deno_dir::DenoDir,
rt: tokio::runtime::current_thread::Runtime,
timers: HashMap<u32, futures::sync::oneshot::Sender<()>>,
argv: Vec<String>,
flags: flags::DenoFlags,
2018-06-22 23:19:43 +02:00
}
static DENO_INIT: std::sync::Once = std::sync::ONCE_INIT;
2018-06-22 23:19:43 +02:00
impl Deno {
fn new(argv: Vec<String>) -> Box<Deno> {
DENO_INIT.call_once(|| {
unsafe { libdeno::deno_init() };
});
let (flags, argv_rest) = flags::set_flags(argv);
let mut deno_box = Box::new(Deno {
ptr: 0 as *const libdeno::DenoC,
dir: deno_dir::DenoDir::new(flags.reload, None).unwrap(),
rt: tokio::runtime::current_thread::Runtime::new().unwrap(),
timers: HashMap::new(),
argv: argv_rest,
flags,
});
(*deno_box).ptr = unsafe {
libdeno::deno_new(
deno_box.as_ref() as *const _ as *const c_void,
handlers::msg_from_js,
)
};
deno_box
}
2018-06-22 23:19:43 +02:00
fn execute(
&mut self,
js_filename: &str,
js_source: &str,
) -> Result<(), DenoException> {
let filename = CString::new(js_filename).unwrap();
let source = CString::new(js_source).unwrap();
let r = unsafe {
libdeno::deno_execute(self.ptr, filename.as_ptr(), source.as_ptr())
};
if r == 0 {
let ptr = unsafe { libdeno::deno_last_exception(self.ptr) };
let cstr = unsafe { CStr::from_ptr(ptr) };
return Err(cstr.to_str().unwrap());
2018-06-22 23:19:43 +02:00
}
Ok(())
}
2018-06-29 15:07:19 -07:00
}
2018-06-22 23:19:43 +02:00
2018-06-29 15:07:19 -07:00
impl Drop for Deno {
fn drop(&mut self) {
unsafe { libdeno::deno_delete(self.ptr) }
}
2018-06-22 23:19:43 +02:00
}
pub fn from_c<'a>(d: *const libdeno::DenoC) -> &'a mut Deno {
let ptr = unsafe { libdeno::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 argv = vec![String::from("./deno"), String::from("hello.js")];
let d = Deno::new(argv);
let d2 = from_c(d.ptr);
assert!(d.ptr == d2.ptr);
2018-07-26 17:54:22 -04:00
assert!(d.dir.root.join("gen") == d.dir.gen, "Sanity check");
}
static LOGGER: Logger = Logger;
struct Logger;
impl log::Log for Logger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
2018-08-07 13:33:36 -04:00
metadata.level() <= log::max_level()
}
fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
println!("{} RS - {}", record.level(), record.args());
}
}
fn flush(&self) {}
}
2018-06-16 01:43:23 +02:00
fn main() {
log::set_logger(&LOGGER).unwrap();
2018-07-16 23:25:50 -04:00
let js_args = flags::v8_set_flags(env::args().collect());
2018-06-22 23:19:43 +02:00
let mut d = Deno::new(js_args);
2018-09-03 00:04:44 +09:00
let mut log_level = log::LevelFilter::Info;
2018-06-22 23:19:43 +02:00
if d.flags.help {
flags::print_usage();
std::process::exit(0);
}
if d.flags.version {
version::print_version();
std::process::exit(0);
}
2018-09-03 00:04:44 +09:00
if d.flags.log_debug {
log_level = log::LevelFilter::Debug;
}
log::set_max_level(log_level);
d.execute("deno_main.js", "denoMain();")
.unwrap_or_else(|err| {
error!("{}", err);
std::process::exit(1);
});
// Start the Tokio event loop
d.rt.run().expect("err");
2018-06-16 01:43:23 +02:00
}