From 880792e422deffa9d10c83118a628e91d09b3d8d Mon Sep 17 00:00:00 2001 From: Jevan Chan Date: Fri, 18 Dec 2020 02:04:39 -0800 Subject: [PATCH] Read file input in bytes instead of string (#979) --- boa/src/context.rs | 5 +++-- boa/src/lib.rs | 23 +++++++++++++++-------- boa_cli/src/main.rs | 14 ++++++++------ boa_tester/src/exec.rs | 15 ++++++++------- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/boa/src/context.rs b/boa/src/context.rs index 0fb8aab682..2a251db874 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -677,10 +677,11 @@ impl Context { /// ``` #[allow(clippy::unit_arg, clippy::drop_copy)] #[inline] - pub fn eval(&mut self, src: &str) -> Result { + pub fn eval>(&mut self, src: T) -> Result { let main_timer = BoaProfiler::global().start_event("Main", "Main"); + let src_bytes: &[u8] = src.as_ref(); - let parsing_result = Parser::new(src.as_bytes(), false) + let parsing_result = Parser::new(src_bytes, false) .parse_all() .map_err(|e| e.to_string()); diff --git a/boa/src/lib.rs b/boa/src/lib.rs index 6244593c11..33ed6ee2cc 100644 --- a/boa/src/lib.rs +++ b/boa/src/lib.rs @@ -78,16 +78,19 @@ pub type Result = StdResult; /// It will return either the statement list AST node for the code, or a parsing error if something /// goes wrong. #[inline] -pub fn parse(src: &str, strict_mode: bool) -> StdResult { - Parser::new(src.as_bytes(), strict_mode).parse_all() +pub fn parse>(src: T, strict_mode: bool) -> StdResult { + let src_bytes: &[u8] = src.as_ref(); + Parser::new(src_bytes, strict_mode).parse_all() } /// Execute the code using an existing Context /// The str is consumed and the state of the Context is changed #[cfg(test)] -pub(crate) fn forward(context: &mut Context, src: &str) -> String { +pub(crate) fn forward>(context: &mut Context, src: T) -> String { + let src_bytes: &[u8] = src.as_ref(); + // Setup executor - let expr = match parse(src, false) { + let expr = match parse(src_bytes, false) { Ok(res) => res, Err(e) => { return format!( @@ -111,10 +114,12 @@ pub(crate) fn forward(context: &mut Context, src: &str) -> String { /// If the interpreter fails parsing an error value is returned instead (error object) #[allow(clippy::unit_arg, clippy::drop_copy)] #[cfg(test)] -pub(crate) fn forward_val(context: &mut Context, src: &str) -> Result { +pub(crate) fn forward_val>(context: &mut Context, src: T) -> Result { let main_timer = BoaProfiler::global().start_event("Main", "Main"); + + let src_bytes: &[u8] = src.as_ref(); // Setup executor - let result = parse(src, false) + let result = parse(src_bytes, false) .map_err(|e| { context .throw_syntax_error(e.to_string()) @@ -131,8 +136,10 @@ pub(crate) fn forward_val(context: &mut Context, src: &str) -> Result { /// Create a clean Context and execute the code #[cfg(test)] -pub(crate) fn exec(src: &str) -> String { - match Context::new().eval(src) { +pub(crate) fn exec>(src: T) -> String { + let src_bytes: &[u8] = src.as_ref(); + + match Context::new().eval(src_bytes) { Ok(value) => value.display().to_string(), Err(error) => error.display().to_string(), } diff --git a/boa_cli/src/main.rs b/boa_cli/src/main.rs index db776d1fb5..2abc10d510 100644 --- a/boa_cli/src/main.rs +++ b/boa_cli/src/main.rs @@ -28,7 +28,7 @@ use boa::{syntax::ast::node::StatementList, Context}; use colored::*; use rustyline::{config::Config, error::ReadlineError, EditMode, Editor}; -use std::{fs::read_to_string, path::PathBuf}; +use std::{fs::read, path::PathBuf}; use structopt::{clap::arg_enum, StructOpt}; mod helper; @@ -104,10 +104,11 @@ arg_enum! { /// /// Returns a error of type String with a message, /// if the token stream has a parsing error. -fn parse_tokens(src: &str) -> Result { +fn parse_tokens>(src: T) -> Result { use boa::syntax::parser::Parser; - Parser::new(src.as_bytes(), false) + let src_bytes: &[u8] = src.as_ref(); + Parser::new(src_bytes, false) .parse_all() .map_err(|e| format!("ParsingError: {}", e)) } @@ -116,9 +117,10 @@ fn parse_tokens(src: &str) -> Result { /// /// Returns a error of type String with a error message, /// if the source has a syntax or parsing error. -fn dump(src: &str, args: &Opt) -> Result<(), String> { +fn dump>(src: T, args: &Opt) -> Result<(), String> { + let src_bytes: &[u8] = src.as_ref(); if let Some(ref arg) = args.dump_ast { - let ast = parse_tokens(src)?; + let ast = parse_tokens(src_bytes)?; match arg { Some(format) => match format { @@ -142,7 +144,7 @@ pub fn main() -> Result<(), std::io::Error> { let mut context = Context::new(); for file in &args.files { - let buffer = read_to_string(file)?; + let buffer = read(file)?; if args.has_dump_flag() { if let Err(e) = dump(&buffer, &args) { diff --git a/boa_tester/src/exec.rs b/boa_tester/src/exec.rs index 5ac6f5a0d2..aa011deec6 100644 --- a/boa_tester/src/exec.rs +++ b/boa_tester/src/exec.rs @@ -128,7 +128,7 @@ impl Test { match self.set_up_env(&harness, strict) { Ok(mut context) => { - let res = context.eval(&self.content); + let res = context.eval(&self.content.as_ref()); let passed = res.is_ok(); let text = match res { @@ -156,7 +156,7 @@ impl Test { self.name ); - match parse(&self.content, strict) { + match parse(&self.content.as_ref(), strict) { Ok(n) => (false, format!("{:?}", n)), Err(e) => (true, format!("Uncaught {}", e)), } @@ -169,11 +169,11 @@ impl Test { phase: Phase::Runtime, ref error_type, } => { - if let Err(e) = parse(&self.content, strict) { + if let Err(e) = parse(&self.content.as_ref(), strict) { (false, format!("Uncaught {}", e)) } else { match self.set_up_env(&harness, strict) { - Ok(mut context) => match context.eval(&self.content) { + Ok(mut context) => match context.eval(&self.content.as_ref()) { Ok(res) => (false, format!("{}", res.display())), Err(e) => { let passed = @@ -271,10 +271,10 @@ impl Test { } context - .eval(&harness.assert) + .eval(&harness.assert.as_ref()) .map_err(|e| format!("could not run assert.js:\n{}", e.display()))?; context - .eval(&harness.sta) + .eval(&harness.sta.as_ref()) .map_err(|e| format!("could not run sta.js:\n{}", e.display()))?; for include in self.includes.iter() { @@ -283,7 +283,8 @@ impl Test { &harness .includes .get(include) - .ok_or_else(|| format!("could not find the {} include file.", include))?, + .ok_or_else(|| format!("could not find the {} include file.", include))? + .as_ref(), ) .map_err(|e| { format!(