From 180170d860921f93a7e28e57b8d0dae84f8076ba Mon Sep 17 00:00:00 2001 From: robbym Date: Wed, 25 Jul 2018 18:27:27 -0700 Subject: [PATCH] Add --v8-options flag (#405) --- src/main.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index f9f0172b6e..fda1988e1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,10 @@ extern crate msg_rs as msg_generated; extern crate url; use libc::c_int; +use std::env; use std::ffi::CStr; use std::ffi::CString; +use std::mem; use std::ptr; mod handlers; @@ -18,15 +20,45 @@ use binding::{ 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) -> (Vec, Vec) { + let mut rest = vec![]; + + // Filter out args that shouldn't be passed to V8 + let mut args: Vec = args + .into_iter() + .filter(|arg| { + if arg.as_str() == "--help" { + rest.push(arg.clone()); + return false; + } + + true + }) + .collect(); + + // Replace args being sent to V8 + for idx in 0..args.len() { + if args[idx] == "--v8-options" { + mem::swap(args.get_mut(idx).unwrap(), &mut String::from("--help")); + } + } + + (args, rest) +} + // Pass the command line arguments to v8. // Returns a vector of command line arguments that v8 did not understand. -fn set_flags() -> Vec { +fn set_flags(args: Vec) -> Vec { // deno_set_flags(int* argc, char** argv) mutates argc and argv to remove // flags that v8 understands. - // Convert command line arguments to a vector of C strings. - let mut argv = std::env::args() - .map(|arg| CString::new(arg).unwrap().into_bytes_with_nul()) + // First parse core args, then converto to a vector of C strings. + let (argv, rest) = parse_core_args(args); + let mut argv = argv + .iter() + .map(|arg| CString::new(arg.as_str()).unwrap().into_bytes_with_nul()) .collect::>(); + // Make a new array, that can be modified by V8::SetFlagsFromCommandLine(), // containing mutable raw pointers to the individual command line args. let mut c_argv = argv @@ -36,7 +68,7 @@ fn set_flags() -> Vec { // Store the length of the argv array in a local variable. We'll pass a // pointer to this local variable to deno_set_flags(), which then // updates its value. - let mut c_argc = 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. unsafe { deno_set_flags(&mut c_argc, c_argv.as_mut_ptr()); @@ -51,7 +83,8 @@ fn set_flags() -> Vec { let slice = cstr.to_str().unwrap(); slice.to_string() }) - .collect::>() + .chain(rest.into_iter()) + .collect() } type DenoException<'a> = &'a str; @@ -90,12 +123,25 @@ impl Drop for Deno { } } +#[test] +fn test_parse_core_args_1() { + let js_args = + parse_core_args(vec!["deno".to_string(), "--v8-options".to_string()]); + assert!(js_args == (vec!["deno".to_string(), "--help".to_string()], vec![])); +} + +#[test] +fn test_parse_core_args_2() { + let js_args = parse_core_args(vec!["deno".to_string(), "--help".to_string()]); + assert!(js_args == (vec!["deno".to_string()], vec!["--help".to_string()])); +} + fn main() { log::set_max_level(log::LevelFilter::Debug); unsafe { deno_init() }; - set_flags(); + let _js_args = set_flags(env::args().collect()); /* let v = unsafe { deno_v8_version() };