From 13f0749ac88f23161a637f40f08a8bcc203a970d Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 9 Dec 2019 20:11:42 -0600 Subject: [PATCH] FIX: v0.7.0 completely broke stdlib loading. --- src/ast/mod.rs | 1 + src/build/mod.rs | 5 +++-- src/build/opcode/environment.rs | 30 ++++++++++++++++++++++++++++++ src/build/opcode/runtime.rs | 21 +++++++++++---------- src/build/opcode/vm.rs | 6 +++--- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index d55b8a8..dac0121 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -639,6 +639,7 @@ impl ModuleDef { ); // std/ paths are special and do not get made into absolute paths. if path.starts_with(format!("std{}", main_separator)) { + dbg!(&path); return; } #[cfg(not(windows))] diff --git a/src/build/mod.rs b/src/build/mod.rs index 483c4ed..0f883f2 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -45,7 +45,7 @@ pub mod ir; pub mod opcode; pub mod scope; -mod stdlib; +pub mod stdlib; pub use self::ir::Val; @@ -115,7 +115,8 @@ where stdout: Stdout, stderr: Stderr, ) -> Self { - let environment = Environment::new_with_vars(stdout, stderr, env::vars().collect()); + let mut environment = Environment::new_with_vars(stdout, stderr, env::vars().collect()); + environment.populate_stdlib(); FileBuilder { environment: Rc::new(RefCell::new(environment)), strict: false, diff --git a/src/build/opcode/environment.rs b/src/build/opcode/environment.rs index 1fd1380..a67da97 100644 --- a/src/build/opcode/environment.rs +++ b/src/build/opcode/environment.rs @@ -21,6 +21,7 @@ use super::cache; use super::pointer::OpPointer; use super::Error; use super::Value; +use crate::build::stdlib; use crate::build::AssertCollector; use crate::build::Val; use crate::convert::{ConverterRegistry, ImporterRegistry}; @@ -97,6 +98,35 @@ impl Environment { ) } + fn add_ops_for_path_and_content

(&mut self, path: P, contents: &str) -> Result<(), Error> + where + P: Into + Clone, + { + let path_copy = path.clone(); + self.op_cache.entry(path.clone()).get_pointer_or_else( + || { + let p = path.into(); + let root = p.parent().unwrap(); + let iter = OffsetStrIter::new(contents).with_src_file(&p); + // FIXME(jwall): Unify BuildError and our other Error + let stmts = parse(iter, None).unwrap(); + // then we create an ops from it + let ops = super::translate::AST::translate(stmts, &root); + Ok(ops) + }, + path_copy, + )?; + Ok(()) + } + + pub fn populate_stdlib(&mut self) { + for (p, s) in stdlib::get_libs().drain() { + // We unwrap the error here since we expect stdlibs to + // always compile. + self.add_ops_for_path_and_content(p, s).unwrap(); + } + } + pub fn record_assert_result(&mut self, desc: &str, ok: bool) { self.assert_results.record_assert_result(desc, ok); } diff --git a/src/build/opcode/runtime.rs b/src/build/opcode/runtime.rs index 7fc1fbc..a943dca 100644 --- a/src/build/opcode/runtime.rs +++ b/src/build/opcode/runtime.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. use std::cell::RefCell; -use std::fmt::Debug; +use std::fmt::{Debug, Display}; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; @@ -101,7 +101,7 @@ impl Builtins { // Try a relative path first. let path = path.into(); // stdlib paths are special - if path.starts_with("std/") { + if path.starts_with(format!("std{}", std::path::MAIN_SEPARATOR)) { return Ok(path); } let mut normalized = base_path.into(); @@ -144,18 +144,19 @@ impl Builtins { pos: Position, ) -> Result where - P: Into, + P: Into + Display + Clone, BP: Into, { - // Try a relative path first. // FIXME(jwall): Use import paths if desired. - let normalized = self.normalize_path(base_path, use_import_path, path)?; - match normalized.canonicalize() { + match self.normalize_path(base_path, use_import_path, path.clone()) { Ok(p) => Ok(p), - Err(_e) => Err(Error::new( - format!("Invalid path: {}", normalized.to_string_lossy()), - pos.clone(), - )), + Err(e) => { + //panic!(format!("Error finding path {}: {}", path, e)); + Err(Error::new( + format!("Error finding path {}: {}", path, e), + pos, + )) + } } } diff --git a/src/build/opcode/vm.rs b/src/build/opcode/vm.rs index c69793f..17f05ca 100644 --- a/src/build/opcode/vm.rs +++ b/src/build/opcode/vm.rs @@ -510,7 +510,7 @@ where } fn op_fcall(&mut self, pos: Position) -> Result<(), Error> { - let (f, f_pos) = dbg!(self.pop())?; + let (f, f_pos) = self.pop()?; let (arg_length, _) = self.pop()?; if let &F(ref f) = f.as_ref() { if let &P(Int(arg_length)) = arg_length.as_ref() { @@ -809,7 +809,7 @@ where fn op_index(&mut self, safe: bool, pos: Position) -> Result<(), Error> { // left and then right - let (right, right_pos) = dbg!(self.pop())?; + let (right, right_pos) = self.pop()?; let (left, _) = self.pop()?; match right.as_ref() { &P(Int(i)) => { @@ -824,7 +824,7 @@ where if let &C(Tuple(ref flds, _)) = left.as_ref() { for &(ref key, ref val) in flds.iter() { if key == s { - self.push(dbg!(val).clone(), right_pos)?; + self.push(val.clone(), right_pos)?; return Ok(()); } }