0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

Add binding to v8::V8::SetFlagsFromCommandLine (#8)

This commit is contained in:
Ry Dahl 2019-11-15 12:57:34 -05:00 committed by GitHub
parent 51c250b775
commit c025a918c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 1 deletions

View file

@ -7,10 +7,11 @@ v8_static_library("rusty_v8") {
"src/inspector/client.cc",
"src/platform/task.cc",
"src/string_buffer.cc",
"src/v8.cc",
]
deps = [
"//build/config:shared_library_deps",
":v8",
"//build/config:shared_library_deps",
]
configs = [ ":rusty_v8_config" ]
}

1
Cargo.lock generated
View file

@ -58,6 +58,7 @@ name = "rusty_v8"
version = "0.0.1"
dependencies = [
"cargo_gn 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)",
"which 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

View file

@ -4,6 +4,9 @@ version = "0.0.1"
authors = ["Bert Belder <bertbelder@gmail.com>"]
edition = "2018"
[dependencies]
libc = "0.2.65"
[build-dependencies]
cargo_gn = "0.0.13"
which = "3.0.0"

View file

@ -3,11 +3,14 @@
#![warn(clippy::all)]
#![allow(dead_code)]
extern crate libc;
pub mod inspector;
pub mod platform;
pub mod string_buffer;
pub mod string_view;
pub mod support;
pub mod v8;
pub use string_buffer::StringBuffer;
pub use string_view::StringView;

7
src/v8.cc Normal file
View file

@ -0,0 +1,7 @@
#include "third_party/v8/include/v8.h"
extern "C" {
void v8__V8__SetFlagsFromCommandLine(int *argc, char **argv) {
v8::V8::SetFlagsFromCommandLine(argc, argv, true);
}
}

67
src/v8.rs Normal file
View file

@ -0,0 +1,67 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use libc::c_char;
use libc::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::vec::Vec;
extern "C" {
pub fn v8__V8__SetFlagsFromCommandLine(
argc: *mut c_int,
argv: *mut *mut c_char,
);
}
/// Pass the command line arguments to v8.
/// The first element of args (which usually corresponds to the binary name) is
/// ignored.
/// Returns a vector of command line arguments that V8 did not understand.
pub fn set_flags_from_command_line(args: Vec<String>) -> Vec<String> {
// deno_set_v8_flags(int* argc, char** argv) mutates argc and argv to remove
// flags that v8 understands.
// Make a new array, that can be modified by V8::SetFlagsFromCommandLine(),
// containing mutable raw pointers to the individual command line args.
let mut raw_argv = args
.iter()
.map(|arg| CString::new(arg.as_str()).unwrap().into_bytes_with_nul())
.collect::<Vec<_>>();
let mut c_argv = raw_argv
.iter_mut()
.map(|arg| arg.as_mut_ptr() as *mut c_char)
.collect::<Vec<_>>();
// Store the length of the c_argv array in a local variable. We'll pass
// a pointer to this local variable to deno_set_v8_flags(), which then
// updates its value.
let mut c_argv_len = c_argv.len() as c_int;
// Let v8 parse the arguments it recognizes and remove them from c_argv.
unsafe {
v8__V8__SetFlagsFromCommandLine(&mut c_argv_len, c_argv.as_mut_ptr())
};
// If c_argv_len was updated we have to change the length of c_argv to match.
c_argv.truncate(c_argv_len as usize);
// Copy the modified arguments list into a proper rust vec and return it.
c_argv
.iter()
.map(|ptr| unsafe {
let cstr = CStr::from_ptr(*ptr as *const c_char);
let slice = cstr.to_str().unwrap();
slice.to_string()
})
.collect()
}
#[test]
fn test_set_flags_from_command_line() {
let r = set_flags_from_command_line(vec![
"binaryname".to_string(),
"--log".to_string(),
"--should-be-ignored".to_string(),
]);
assert_eq!(
r,
vec!["binaryname".to_string(), "--should-be-ignored".to_string()]
);
}