Browse Source

Read file input in bytes instead of string (#979)

pull/985/head
Jevan Chan 4 years ago committed by GitHub
parent
commit
880792e422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      boa/src/context.rs
  2. 23
      boa/src/lib.rs
  3. 14
      boa_cli/src/main.rs
  4. 15
      boa_tester/src/exec.rs

5
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<Value> {
pub fn eval<T: AsRef<[u8]>>(&mut self, src: T) -> Result<Value> {
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());

23
boa/src/lib.rs

@ -78,16 +78,19 @@ pub type Result<T> = StdResult<T, Value>;
/// 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<StatementList, ParseError> {
Parser::new(src.as_bytes(), strict_mode).parse_all()
pub fn parse<T: AsRef<[u8]>>(src: T, strict_mode: bool) -> StdResult<StatementList, ParseError> {
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<T: AsRef<[u8]>>(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<Value> {
pub(crate) fn forward_val<T: AsRef<[u8]>>(context: &mut Context, src: T) -> Result<Value> {
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<Value> {
/// 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<T: AsRef<[u8]>>(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(),
}

14
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<StatementList, String> {
fn parse_tokens<T: AsRef<[u8]>>(src: T) -> Result<StatementList, String> {
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<StatementList, String> {
///
/// 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<T: AsRef<[u8]>>(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) {

15
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!(

Loading…
Cancel
Save