From 9945885ca8036d3d6148a59e81fce4f795a2a651 Mon Sep 17 00:00:00 2001 From: David Beckley Date: Sat, 13 Jun 2015 04:32:50 -0700 Subject: [PATCH] Work on docs --- Cargo.toml | 4 ++++ src/interpret.rs | 14 ++++++++++++++ src/lib.rs | 2 ++ src/main.rs | 4 ++-- src/runtime.rs | 15 ++++++--------- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 97b4a41..39b4eb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,10 @@ name = "ack" version = "0.1.0" authors = ["David Beckley "] +[[bin]] +name = "ack" +doc = false + [dependencies] maplit = "0.1.1" libc = "0.1" diff --git a/src/interpret.rs b/src/interpret.rs index 4229f42..7099522 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -21,6 +21,7 @@ impl GetFromResult for JSResult { } } +/// Contains the state for an interpreter thread #[derive(Debug, Clone)] pub struct Context { pub this: Value, @@ -28,6 +29,19 @@ pub struct Context { pub global: Object } +impl Context { + /// Creates a top-level context + /// + /// `this`, `local`, and `global` are all set to `obj`. + pub fn new(obj: Object) -> Context { + Context { + this: Value::Object(obj.clone()), + local: obj.clone(), + global: obj + } + } +} + #[derive(Debug, Clone)] pub struct UserFunction { pub id: Option, diff --git a/src/lib.rs b/src/lib.rs index 03699af..61c13c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +//! Embeddable JavaScript interpreter. + #[macro_use] extern crate maplit; diff --git a/src/main.rs b/src/main.rs index 7333908..41d2e0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ fn is_interactive() -> bool { } fn start_repl() { - let mut ack = Ack::new(); + let mut ack = Ack::create_stdlib(); loop { print!(">>> "); @@ -63,7 +63,7 @@ fn run_script(mut file: T) -> bool { s }; - let result = Ack::new().eval(&source); + let result = Ack::create_stdlib().eval(&source); if let &Err(ref e) = &result { let mut t = term::stderr().unwrap(); diff --git a/src/runtime.rs b/src/runtime.rs index 7805635..d930460 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -9,15 +9,12 @@ pub type Ack = interpret::Context; /// A high-level interface for the interpreter impl Ack { - pub fn new() -> Ack { - let obj = create_stdlib(); - Ack { - this: interpret::Value::Object(obj.clone()), - local: obj.clone(), - global: obj.clone() - } + /// Create a context with the JavaScript standard library + pub fn create_stdlib() -> Ack { + Context::new(create_stdlib()) } + /// Parse and evaluate `source` in this context pub fn eval(&mut self, source: &str) -> JSResult { let parsed = parser::parse(source); @@ -31,8 +28,8 @@ impl Ack { } } -/// Create a default global object -pub fn create_stdlib() -> interpret::Object { +/// This is private, anyway +fn create_stdlib() -> interpret::Object { use interpret::*; let function_prototype = Object::new();