mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
Use 2 space indent.
Apologies for the churn, but my screen is small. rustfmt assumes big monitors.
This commit is contained in:
parent
a7bf154cb8
commit
21c4b8a42d
4 changed files with 254 additions and 248 deletions
|
@ -10,45 +10,45 @@ use libc::uint32_t;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct DenoC {
|
pub struct DenoC {
|
||||||
_unused: [u8; 0],
|
_unused: [u8; 0],
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct deno_buf {
|
pub struct deno_buf {
|
||||||
alloc_ptr: *mut u8,
|
alloc_ptr: *mut u8,
|
||||||
alloc_len: usize,
|
alloc_len: usize,
|
||||||
data_ptr: *mut u8,
|
data_ptr: *mut u8,
|
||||||
data_len: usize,
|
data_len: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
type DenoRecvCb = unsafe extern "C" fn(d: *const DenoC, buf: deno_buf);
|
type DenoRecvCb = unsafe extern "C" fn(d: *const DenoC, buf: deno_buf);
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn deno_init();
|
pub fn deno_init();
|
||||||
pub fn deno_v8_version() -> *const c_char;
|
pub fn deno_v8_version() -> *const c_char;
|
||||||
pub fn deno_set_flags(argc: *mut c_int, argv: *mut *mut c_char);
|
pub fn deno_set_flags(argc: *mut c_int, argv: *mut *mut c_char);
|
||||||
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_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,
|
||||||
js_filename: *const c_char,
|
js_filename: *const c_char,
|
||||||
js_source: *const c_char,
|
js_source: *const c_char,
|
||||||
) -> c_int;
|
) -> c_int;
|
||||||
pub fn deno_handle_msg_from_js(d: *const DenoC, buf: deno_buf);
|
pub fn deno_handle_msg_from_js(d: *const DenoC, buf: deno_buf);
|
||||||
pub fn deno_reply_error(
|
pub fn deno_reply_error(
|
||||||
d: *const DenoC,
|
d: *const DenoC,
|
||||||
cmd_id: uint32_t,
|
cmd_id: uint32_t,
|
||||||
msg: *const c_char,
|
msg: *const c_char,
|
||||||
);
|
);
|
||||||
pub fn deno_reply_null(d: *const DenoC, cmd_id: uint32_t);
|
pub fn deno_reply_null(d: *const DenoC, cmd_id: uint32_t);
|
||||||
pub fn deno_reply_code_fetch(
|
pub fn deno_reply_code_fetch(
|
||||||
d: *const DenoC,
|
d: *const DenoC,
|
||||||
cmd_id: uint32_t,
|
cmd_id: uint32_t,
|
||||||
module_name: *const c_char,
|
module_name: *const c_char,
|
||||||
filename: *const c_char,
|
filename: *const c_char,
|
||||||
source_code: *const c_char,
|
source_code: *const c_char,
|
||||||
output_code: *const c_char,
|
output_code: *const c_char,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
305
src/handlers.rs
305
src/handlers.rs
|
@ -22,125 +22,131 @@ const ASSET_PREFIX: &str = "/$asset$/";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_url() {
|
fn test_url() {
|
||||||
let issue_list_url = Url::parse("https://github.com/rust-lang").unwrap();
|
let issue_list_url = Url::parse("https://github.com/rust-lang").unwrap();
|
||||||
assert!(issue_list_url.scheme() == "https");
|
assert!(issue_list_url.scheme() == "https");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_from_ptr(ptr: *const c_char) -> String {
|
fn string_from_ptr(ptr: *const c_char) -> String {
|
||||||
let cstr = unsafe { CStr::from_ptr(ptr as *const i8) };
|
let cstr = unsafe { CStr::from_ptr(ptr as *const i8) };
|
||||||
String::from(cstr.to_str().unwrap())
|
String::from(cstr.to_str().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_cstring(s: &String) -> CString {
|
fn as_cstring(s: &String) -> CString {
|
||||||
CString::new(s.as_str()).unwrap()
|
CString::new(s.as_str()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prototype: https://github.com/ry/deno/blob/golang/os.go#L56-L68
|
// Prototype: https://github.com/ry/deno/blob/golang/os.go#L56-L68
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn src_file_to_url<P: AsRef<Path>>(filename: P) -> String {
|
fn src_file_to_url<P: AsRef<Path>>(filename: P) -> String {
|
||||||
assert!(SRC_DIR.len() > 0, "SRC_DIR shouldn't be empty");
|
assert!(SRC_DIR.len() > 0, "SRC_DIR shouldn't be empty");
|
||||||
|
|
||||||
let filename = filename.as_ref().to_path_buf();
|
let filename = filename.as_ref().to_path_buf();
|
||||||
let src = (SRC_DIR.as_ref() as &Path).to_path_buf();
|
let src = (SRC_DIR.as_ref() as &Path).to_path_buf();
|
||||||
|
|
||||||
if filename.starts_with(&src) {
|
if filename.starts_with(&src) {
|
||||||
let rest = filename.strip_prefix(&src).unwrap();
|
let rest = filename.strip_prefix(&src).unwrap();
|
||||||
"http://".to_string() + rest.to_str().unwrap()
|
"http://".to_string() + rest.to_str().unwrap()
|
||||||
} else {
|
} else {
|
||||||
String::from(filename.to_str().unwrap())
|
String::from(filename.to_str().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_src_file_to_url() {
|
fn test_src_file_to_url() {
|
||||||
assert_eq!("hello", src_file_to_url("hello"));
|
assert_eq!("hello", src_file_to_url("hello"));
|
||||||
assert_eq!("/hello", src_file_to_url("/hello"));
|
assert_eq!("/hello", src_file_to_url("/hello"));
|
||||||
let x = SRC_DIR.to_string() + "hello";
|
let x = SRC_DIR.to_string() + "hello";
|
||||||
assert_eq!("http://hello", src_file_to_url(&x));
|
assert_eq!("http://hello", src_file_to_url(&x));
|
||||||
let x = SRC_DIR.to_string() + "/hello";
|
let x = SRC_DIR.to_string() + "/hello";
|
||||||
assert_eq!("http://hello", src_file_to_url(&x));
|
assert_eq!("http://hello", src_file_to_url(&x));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prototype: https://github.com/ry/deno/blob/golang/os.go#L70-L98
|
// Prototype: https://github.com/ry/deno/blob/golang/os.go#L70-L98
|
||||||
// Returns (module name, local filename)
|
// Returns (module name, local filename)
|
||||||
fn resolve_module(
|
fn resolve_module(
|
||||||
module_specifier: &String,
|
module_specifier: &String,
|
||||||
containing_file: &String,
|
containing_file: &String,
|
||||||
) -> Result<(String, String), url::ParseError> {
|
) -> Result<(String, String), url::ParseError> {
|
||||||
info!(
|
info!(
|
||||||
"resolve_module before module_specifier {} containing_file {}",
|
"resolve_module before module_specifier {} containing_file {}",
|
||||||
module_specifier, containing_file
|
module_specifier, containing_file
|
||||||
);
|
);
|
||||||
|
|
||||||
//let module_specifier = src_file_to_url(module_specifier);
|
//let module_specifier = src_file_to_url(module_specifier);
|
||||||
//let containing_file = src_file_to_url(containing_file);
|
//let containing_file = src_file_to_url(containing_file);
|
||||||
//let base_url = Url::parse(&containing_file)?;
|
//let base_url = Url::parse(&containing_file)?;
|
||||||
|
|
||||||
let j: Url = if containing_file.as_str().ends_with("/") {
|
let j: Url = if containing_file.as_str().ends_with("/") {
|
||||||
let base = Url::from_directory_path(&containing_file).unwrap();
|
let base = Url::from_directory_path(&containing_file).unwrap();
|
||||||
base.join(module_specifier)?
|
base.join(module_specifier)?
|
||||||
} else if containing_file == "." {
|
} else if containing_file == "." {
|
||||||
Url::from_file_path(module_specifier).unwrap()
|
Url::from_file_path(module_specifier).unwrap()
|
||||||
} else {
|
} else {
|
||||||
let base = Url::from_file_path(&containing_file).unwrap();
|
let base = Url::from_file_path(&containing_file).unwrap();
|
||||||
base.join(module_specifier)?
|
base.join(module_specifier)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut p = j.to_file_path()
|
let mut p = j.to_file_path()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_os_string()
|
.into_os_string()
|
||||||
.into_string()
|
.into_string()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
// On windows, replace backward slashes to forward slashes.
|
// On windows, replace backward slashes to forward slashes.
|
||||||
// TODO(piscisaureus): This may not me be right, I just did it to make
|
// TODO(piscisaureus): This may not me be right, I just did it to make
|
||||||
// the tests pass.
|
// the tests pass.
|
||||||
p = p.replace("\\", "/");
|
p = p.replace("\\", "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
let module_name = p.to_string();
|
let module_name = p.to_string();
|
||||||
let filename = p.to_string();
|
let filename = p.to_string();
|
||||||
|
|
||||||
Ok((module_name, filename))
|
Ok((module_name, filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/ry/deno/blob/golang/os_test.go#L16-L87
|
// https://github.com/ry/deno/blob/golang/os_test.go#L16-L87
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resolve_module() {
|
fn test_resolve_module() {
|
||||||
// The `add_root` macro prepends "C:" to a string if on windows; on posix
|
// The `add_root` macro prepends "C:" to a string if on windows; on posix
|
||||||
// systems it returns the input string untouched. This is necessary because
|
// systems it returns the input string untouched. This is necessary because
|
||||||
// `Url::from_file_path()` fails if the input path isn't an absolute path.
|
// `Url::from_file_path()` fails if the input path isn't an absolute path.
|
||||||
macro_rules! add_root {
|
macro_rules! add_root {
|
||||||
($path:expr) => {
|
($path:expr) => {
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
concat!("C:", $path)
|
concat!("C:", $path)
|
||||||
} else {
|
} else {
|
||||||
$path
|
$path
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let test_cases = [
|
let test_cases = [
|
||||||
(
|
(
|
||||||
"./subdir/print_hello.ts",
|
"./subdir/print_hello.ts",
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts"),
|
add_root!(
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts"),
|
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts"
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts"),
|
),
|
||||||
),
|
add_root!(
|
||||||
(
|
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts"
|
||||||
"testdata/001_hello.js",
|
),
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/"),
|
add_root!(
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js"),
|
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts"
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js"),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
add_root!("/Users/rld/src/deno/hello.js"),
|
"testdata/001_hello.js",
|
||||||
".",
|
add_root!("/Users/rld/go/src/github.com/ry/deno/"),
|
||||||
add_root!("/Users/rld/src/deno/hello.js"),
|
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js"),
|
||||||
add_root!("/Users/rld/src/deno/hello.js"),
|
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js"),
|
||||||
),
|
),
|
||||||
/*
|
(
|
||||||
|
add_root!("/Users/rld/src/deno/hello.js"),
|
||||||
|
".",
|
||||||
|
add_root!("/Users/rld/src/deno/hello.js"),
|
||||||
|
add_root!("/Users/rld/src/deno/hello.js"),
|
||||||
|
),
|
||||||
|
/*
|
||||||
(
|
(
|
||||||
"http://localhost:4545/testdata/subdir/print_hello.ts",
|
"http://localhost:4545/testdata/subdir/print_hello.ts",
|
||||||
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts"),
|
add_root!("/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts"),
|
||||||
|
@ -160,88 +166,87 @@ fn test_resolve_module() {
|
||||||
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/util"),
|
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/util"),
|
||||||
),
|
),
|
||||||
*/
|
*/
|
||||||
];
|
];
|
||||||
for &test in test_cases.iter() {
|
for &test in test_cases.iter() {
|
||||||
let module_specifier = String::from(test.0);
|
let module_specifier = String::from(test.0);
|
||||||
let containing_file = String::from(test.1);
|
let containing_file = String::from(test.1);
|
||||||
let (module_name, filename) =
|
let (module_name, filename) =
|
||||||
resolve_module(&module_specifier, &containing_file).unwrap();
|
resolve_module(&module_specifier, &containing_file).unwrap();
|
||||||
assert_eq!(module_name, test.2);
|
assert_eq!(module_name, test.2);
|
||||||
assert_eq!(filename, test.3);
|
assert_eq!(filename, test.3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/ry/deno/blob/golang/os.go#L100-L154
|
// https://github.com/ry/deno/blob/golang/os.go#L100-L154
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn handle_code_fetch(
|
pub extern "C" fn handle_code_fetch(
|
||||||
d: *const DenoC,
|
d: *const DenoC,
|
||||||
cmd_id: u32,
|
cmd_id: u32,
|
||||||
module_specifier_: *const c_char,
|
module_specifier_: *const c_char,
|
||||||
containing_file_: *const c_char,
|
containing_file_: *const c_char,
|
||||||
) {
|
) {
|
||||||
let module_specifier = string_from_ptr(module_specifier_);
|
let module_specifier = string_from_ptr(module_specifier_);
|
||||||
let containing_file = string_from_ptr(containing_file_);
|
let containing_file = string_from_ptr(containing_file_);
|
||||||
|
|
||||||
let result = resolve_module(&module_specifier, &containing_file);
|
let result = resolve_module(&module_specifier, &containing_file);
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
let err = result.unwrap_err();
|
let err = result.unwrap_err();
|
||||||
let errmsg =
|
let errmsg = format!("{} {} {}", err, module_specifier, containing_file);
|
||||||
format!("{} {} {}", err, module_specifier, containing_file);
|
let errmsg_c = as_cstring(&errmsg);
|
||||||
let errmsg_c = as_cstring(&errmsg);
|
unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
|
||||||
unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
|
return;
|
||||||
return;
|
}
|
||||||
}
|
let (module_name, filename) = result.unwrap();
|
||||||
let (module_name, filename) = result.unwrap();
|
|
||||||
|
|
||||||
let mut source_code = String::new();
|
let mut source_code = String::new();
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"code_fetch. module_name = {} module_specifier = {} containing_file = {} filename = {}",
|
"code_fetch. module_name = {} module_specifier = {} containing_file = {} filename = {}",
|
||||||
module_name, module_specifier, containing_file, filename
|
module_name, module_specifier, containing_file, filename
|
||||||
);
|
);
|
||||||
|
|
||||||
if is_remote(&module_name) {
|
if is_remote(&module_name) {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
} else if module_name.starts_with(ASSET_PREFIX) {
|
} else if module_name.starts_with(ASSET_PREFIX) {
|
||||||
assert!(false, "Asset resolution should be done in JS, not Rust.");
|
assert!(false, "Asset resolution should be done in JS, not Rust.");
|
||||||
} else {
|
} else {
|
||||||
assert!(
|
assert!(
|
||||||
module_name == filename,
|
module_name == filename,
|
||||||
"if a module isn't remote, it should have the same filename"
|
"if a module isn't remote, it should have the same filename"
|
||||||
);
|
);
|
||||||
let result = File::open(&filename);
|
let result = File::open(&filename);
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
let err = result.unwrap_err();
|
let err = result.unwrap_err();
|
||||||
let errmsg = format!("{} {}", err, filename);
|
let errmsg = format!("{} {}", err, filename);
|
||||||
let errmsg_c = as_cstring(&errmsg);
|
let errmsg_c = as_cstring(&errmsg);
|
||||||
unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
|
unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
let mut f = result.unwrap();
|
|
||||||
let result = f.read_to_string(&mut source_code);
|
|
||||||
if result.is_err() {
|
|
||||||
let err = result.unwrap_err();
|
|
||||||
let errmsg = format!("{} {}", err, filename);
|
|
||||||
let errmsg_c = as_cstring(&errmsg);
|
|
||||||
unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
let mut f = result.unwrap();
|
||||||
let output_code = String::new(); //load_output_code_cache(filename, source_code);
|
let result = f.read_to_string(&mut source_code);
|
||||||
|
if result.is_err() {
|
||||||
unsafe {
|
let err = result.unwrap_err();
|
||||||
deno_reply_code_fetch(
|
let errmsg = format!("{} {}", err, filename);
|
||||||
d,
|
let errmsg_c = as_cstring(&errmsg);
|
||||||
cmd_id,
|
unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
|
||||||
as_cstring(&module_name).as_ptr(),
|
return;
|
||||||
as_cstring(&filename).as_ptr(),
|
|
||||||
as_cstring(&source_code).as_ptr(),
|
|
||||||
as_cstring(&output_code).as_ptr(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let output_code = String::new(); //load_output_code_cache(filename, source_code);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
deno_reply_code_fetch(
|
||||||
|
d,
|
||||||
|
cmd_id,
|
||||||
|
as_cstring(&module_name).as_ptr(),
|
||||||
|
as_cstring(&filename).as_ptr(),
|
||||||
|
as_cstring(&source_code).as_ptr(),
|
||||||
|
as_cstring(&output_code).as_ptr(),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_remote(_module_name: &String) -> bool {
|
fn is_remote(_module_name: &String) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
132
src/main.rs
132
src/main.rs
|
@ -9,101 +9,101 @@ use std::ptr;
|
||||||
|
|
||||||
mod binding;
|
mod binding;
|
||||||
use binding::{
|
use binding::{
|
||||||
deno_delete, deno_execute, deno_handle_msg_from_js, deno_init,
|
deno_delete, deno_execute, deno_handle_msg_from_js, deno_init,
|
||||||
deno_last_exception, deno_new, deno_set_flags, DenoC,
|
deno_last_exception, deno_new, deno_set_flags, DenoC,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Pass the command line arguments to v8.
|
// Pass the command line arguments to v8.
|
||||||
// Returns a vector of command line arguments that v8 did not understand.
|
// Returns a vector of command line arguments that v8 did not understand.
|
||||||
fn set_flags() -> Vec<String> {
|
fn set_flags() -> Vec<String> {
|
||||||
// deno_set_flags(int* argc, char** argv) mutates argc and argv to remove
|
// deno_set_flags(int* argc, char** argv) mutates argc and argv to remove
|
||||||
// flags that v8 understands.
|
// flags that v8 understands.
|
||||||
// Convert command line arguments to a vector of C strings.
|
// Convert command line arguments to a vector of C strings.
|
||||||
let mut argv = std::env::args()
|
let mut argv = std::env::args()
|
||||||
.map(|arg| CString::new(arg).unwrap().into_bytes_with_nul())
|
.map(|arg| CString::new(arg).unwrap().into_bytes_with_nul())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
// Make a new array, that can be modified by V8::SetFlagsFromCommandLine(),
|
// Make a new array, that can be modified by V8::SetFlagsFromCommandLine(),
|
||||||
// containing mutable raw pointers to the individual command line args.
|
// containing mutable raw pointers to the individual command line args.
|
||||||
let mut c_argv = argv.iter_mut()
|
let mut c_argv = argv
|
||||||
.map(|arg| arg.as_mut_ptr() as *mut i8)
|
.iter_mut()
|
||||||
.collect::<Vec<_>>();
|
.map(|arg| arg.as_mut_ptr() as *mut i8)
|
||||||
// Store the length of the argv array in a local variable. We'll pass a
|
.collect::<Vec<_>>();
|
||||||
// pointer to this local variable to deno_set_flags(), which then
|
// Store the length of the argv array in a local variable. We'll pass a
|
||||||
// updates its value.
|
// pointer to this local variable to deno_set_flags(), which then
|
||||||
let mut c_argc = argv.len() as c_int;
|
// updates its value.
|
||||||
// Let v8 parse the arguments it recognizes and remove them from c_argv.
|
let mut c_argc = argv.len() as c_int;
|
||||||
unsafe {
|
// Let v8 parse the arguments it recognizes and remove them from c_argv.
|
||||||
deno_set_flags(&mut c_argc, c_argv.as_mut_ptr());
|
unsafe {
|
||||||
};
|
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);
|
// If c_argc was updated we have to change the length of c_argv to match.
|
||||||
// Copy the modified arguments list into a proper rust vec and return it.
|
c_argv.truncate(c_argc as usize);
|
||||||
c_argv
|
// Copy the modified arguments list into a proper rust vec and return it.
|
||||||
.iter()
|
c_argv
|
||||||
.map(|ptr| unsafe {
|
.iter()
|
||||||
let cstr = CStr::from_ptr(*ptr as *const i8);
|
.map(|ptr| unsafe {
|
||||||
let slice = cstr.to_str().unwrap();
|
let cstr = CStr::from_ptr(*ptr as *const i8);
|
||||||
slice.to_string()
|
let slice = cstr.to_str().unwrap();
|
||||||
})
|
slice.to_string()
|
||||||
.collect::<Vec<_>>()
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
type DenoException<'a> = &'a str;
|
type DenoException<'a> = &'a str;
|
||||||
|
|
||||||
struct Deno {
|
struct Deno {
|
||||||
ptr: *const DenoC,
|
ptr: *const DenoC,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deno {
|
impl Deno {
|
||||||
fn new() -> Deno {
|
fn new() -> Deno {
|
||||||
let ptr = unsafe { deno_new(ptr::null(), deno_handle_msg_from_js) };
|
let ptr = unsafe { deno_new(ptr::null(), deno_handle_msg_from_js) };
|
||||||
Deno { ptr: ptr }
|
Deno { ptr: ptr }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(
|
fn execute(
|
||||||
&mut self,
|
&mut self,
|
||||||
js_filename: &str,
|
js_filename: &str,
|
||||||
js_source: &str,
|
js_source: &str,
|
||||||
) -> 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 = unsafe {
|
let r =
|
||||||
deno_execute(self.ptr, filename.as_ptr(), source.as_ptr())
|
unsafe { 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 { 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());
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Deno {
|
impl Drop for Deno {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe { deno_delete(self.ptr) }
|
unsafe { deno_delete(self.ptr) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
log::set_max_level(log::LevelFilter::Debug);
|
log::set_max_level(log::LevelFilter::Debug);
|
||||||
|
|
||||||
unsafe { deno_init() };
|
unsafe { deno_init() };
|
||||||
|
|
||||||
set_flags();
|
set_flags();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let v = unsafe { deno_v8_version() };
|
let v = unsafe { deno_v8_version() };
|
||||||
let c_str = unsafe { CStr::from_ptr(v) };
|
let c_str = unsafe { CStr::from_ptr(v) };
|
||||||
let version = c_str.to_str().unwrap();
|
let version = c_str.to_str().unwrap();
|
||||||
println!("version: {}", version);
|
println!("version: {}", version);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let mut d = Deno::new();
|
let mut d = Deno::new();
|
||||||
|
|
||||||
d.execute("deno_main.js", "denoMain();")
|
d.execute("deno_main.js", "denoMain();")
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
max_width = 80
|
max_width = 80
|
||||||
|
tab_spaces = 2
|
||||||
|
|
Loading…
Reference in a new issue