diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-06-16 01:43:23 +0200 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2018-06-17 12:43:04 +0200 |
commit | f1dcfbb59d7baad9b1d1c085d67c98896c6dc623 (patch) | |
tree | 94290c22c4ae251b80ce23aa676330ec30253052 /deno2/main.rs | |
parent | 993d58c410c18486ac2fc58460a0211484d57690 (diff) |
First pass at deno rust
Diffstat (limited to 'deno2/main.rs')
-rw-r--r-- | deno2/main.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/deno2/main.rs b/deno2/main.rs new file mode 100644 index 000000000..3415e3a24 --- /dev/null +++ b/deno2/main.rs @@ -0,0 +1,39 @@ +extern crate libc; +use libc::c_char; +use libc::c_int; +use std::ffi::CStr; +use std::ffi::CString; + +#[link(name = "deno", kind = "static")] +extern "C" { + fn deno_v8_version() -> *const c_char; + fn deno_init(); + + // Note: `deno_set_flags` actually takes `char**` as it's second argument, + // not `const char**`, so this is technically incorrect. However it doesn't + // actually modify the contents of the strings, so it's not unsafe. + // TODO: use the correct function signature. + fn deno_set_flags(argc: *mut c_int, argv: *mut *const c_char); +} + +fn set_flags() { + // Create a vector of zero terminated c strings. + let mut argv = std::env::args() + .map(|arg| CString::new(arg).unwrap().as_ptr()) + .collect::<Vec<_>>(); + let mut argc = argv.len() as c_int; + unsafe { + // pass the pointer of the vector's internal buffer to a C function + deno_set_flags(&mut argc, argv.as_mut_ptr()); + }; +} + +fn main() { + println!("Hi"); + set_flags(); + unsafe { deno_init() }; + let v = unsafe { deno_v8_version() }; + let c_str = unsafe { CStr::from_ptr(v) }; + let version = c_str.to_str().unwrap(); + println!("version: {}", version); +} |