summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobby Madruga <robbymadruga@gmail.com>2018-06-29 15:07:19 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-07-03 21:22:39 +0200
commit7fcaf7d35dc94d3fe40faf59002131201ba24dc4 (patch)
tree8b97bc615eae7b83e70bbed0b8aa6197d97832aa /src
parent92d01f851918ab2b16f661f9c739bcfb61dd3a15 (diff)
Rustify Deno API
Diffstat (limited to 'src')
-rw-r--r--src/main.rs33
1 files changed, 16 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs
index 2ec347e6e..99c3e4420 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -78,6 +78,8 @@ extern "C" fn on_message(_d: *const DenoC, channel_ptr: *const c_char, _buf: den
println!("got message in rust {}", channel);
}
+type DenoException<'a> = &'a str;
+
struct Deno {
ptr: *const DenoC,
}
@@ -88,20 +90,21 @@ impl Deno {
Deno { ptr: ptr }
}
- fn execute(&self, js_filename: &str, js_source: &str) -> bool {
+ fn execute(&mut self, js_filename: &str, js_source: &str) -> Result<(), DenoException> {
let filename = CString::new(js_filename).unwrap();
let source = CString::new(js_source).unwrap();
let r = unsafe { deno_execute(self.ptr, filename.as_ptr(), source.as_ptr()) };
- r != 0
- }
-
- fn last_exception(&self) -> &str {
- let ptr = unsafe { deno_last_exception(self.ptr) };
- let cstr = unsafe { CStr::from_ptr(ptr) };
- cstr.to_str().unwrap()
+ if r == 0 {
+ let ptr = unsafe { deno_last_exception(self.ptr) };
+ let cstr = unsafe { CStr::from_ptr(ptr) };
+ return Err(cstr.to_str().unwrap());
+ }
+ Ok(())
}
+}
- fn destroy(self) {
+impl Drop for Deno {
+ fn drop(&mut self) {
unsafe { deno_delete(self.ptr) }
}
}
@@ -118,14 +121,10 @@ fn main() {
println!("version: {}", version);
*/
- let d = Deno::new();
+ let mut d = Deno::new();
- let ok = d.execute("deno_main.js", "denoMain();");
- if !ok {
- let e = d.last_exception();
- println!("Error {}\n", e);
+ d.execute("deno_main.js", "denoMain();").unwrap_or_else(|err| {
+ println!("Error {}\n", err);
std::process::exit(1);
- }
-
- d.destroy();
+ });
}