mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-12-24 08:09:16 -05:00
Add set_flags_from_command_line_with_usage (#536)
The motivation for this is to enable embedders of rusty_v8 to pass their own usage string and have their usage printed before V8's options.
This commit is contained in:
parent
98bb4506d9
commit
4e8eaf94c3
3 changed files with 36 additions and 4 deletions
22
src/V8.rs
22
src/V8.rs
|
@ -11,7 +11,11 @@ use crate::support::UniqueRef;
|
|||
use crate::support::UnitType;
|
||||
|
||||
extern "C" {
|
||||
fn v8__V8__SetFlagsFromCommandLine(argc: *mut c_int, argv: *mut *mut c_char);
|
||||
fn v8__V8__SetFlagsFromCommandLine(
|
||||
argc: *mut c_int,
|
||||
argv: *mut *mut c_char,
|
||||
usage: *const c_char,
|
||||
);
|
||||
fn v8__V8__SetFlagsFromString(flags: *const u8, length: usize);
|
||||
fn v8__V8__SetEntropySource(callback: EntropySource);
|
||||
fn v8__V8__GetVersion() -> *const c_char;
|
||||
|
@ -83,6 +87,12 @@ pub fn assert_initialized() {
|
|||
/// Returns a vector of command line arguments that V8 did not understand.
|
||||
/// TODO: Check whether this is safe to do after globally initializing v8.
|
||||
pub fn set_flags_from_command_line(args: Vec<String>) -> Vec<String> {
|
||||
set_flags_from_command_line_with_usage(args, None)
|
||||
}
|
||||
pub fn set_flags_from_command_line_with_usage(
|
||||
args: Vec<String>,
|
||||
usage: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
// deno_set_v8_flags(int* argc, char** argv) mutates argc and argv to remove
|
||||
// flags that v8 understands.
|
||||
|
||||
|
@ -102,8 +112,16 @@ pub fn set_flags_from_command_line(args: Vec<String>) -> Vec<String> {
|
|||
// 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.
|
||||
let c_usage = match usage {
|
||||
Some(str) => CString::new(str).unwrap().into_raw() as *const c_char,
|
||||
None => std::ptr::null(),
|
||||
};
|
||||
unsafe {
|
||||
v8__V8__SetFlagsFromCommandLine(&mut c_argv_len, c_argv.as_mut_ptr())
|
||||
v8__V8__SetFlagsFromCommandLine(
|
||||
&mut c_argv_len,
|
||||
c_argv.as_mut_ptr(),
|
||||
c_usage,
|
||||
);
|
||||
};
|
||||
// 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);
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "v8/include/v8.h"
|
||||
#include "v8/src/execution/isolate-utils-inl.h"
|
||||
#include "v8/src/execution/isolate-utils.h"
|
||||
#include "v8/src/flags/flags.h"
|
||||
#include "v8/src/objects/objects-inl.h"
|
||||
#include "v8/src/objects/objects.h"
|
||||
#include "v8/src/objects/smi.h"
|
||||
|
@ -89,8 +90,12 @@ v8::MaybeLocal<v8::Promise> HostImportModuleDynamicallyCallback(
|
|||
}
|
||||
|
||||
extern "C" {
|
||||
void v8__V8__SetFlagsFromCommandLine(int* argc, char** argv) {
|
||||
v8::V8::SetFlagsFromCommandLine(argc, argv, true);
|
||||
void v8__V8__SetFlagsFromCommandLine(int* argc, char** argv,
|
||||
const char* usage) {
|
||||
namespace i = v8::internal;
|
||||
using HelpOptions = i::FlagList::HelpOptions;
|
||||
HelpOptions help_options = HelpOptions(HelpOptions::kExit, usage);
|
||||
i::FlagList::SetFlagsFromCommandLine(argc, argv, true, help_options);
|
||||
}
|
||||
|
||||
void v8__V8__SetFlagsFromString(const char* flags, size_t length) {
|
||||
|
|
|
@ -999,6 +999,15 @@ fn set_flags_from_command_line() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_flags_from_command_line_with_usage() {
|
||||
let r = v8::V8::set_flags_from_command_line_with_usage(
|
||||
vec!["binaryname".to_string(), "--help".to_string()],
|
||||
Some("Usage: binaryname --startup-src=file\n\n"),
|
||||
);
|
||||
assert_eq!(r, vec!["binaryname".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspector_string_view() {
|
||||
let chars = b"Hello world!";
|
||||
|
|
Loading…
Reference in a new issue