Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read file input in bytes instead of string #979

Merged
merged 1 commit into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
23 changes: 15 additions & 8 deletions boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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())
Expand All @@ -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(),
}
Expand Down
14 changes: 8 additions & 6 deletions boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions boa_tester/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
}
Expand All @@ -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 =
Expand Down Expand Up @@ -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() {
Expand All @@ -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!(
Expand Down