Skip to content

Commit

Permalink
Fix some suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Dec 7, 2021
1 parent 9828673 commit 980e719
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
5 changes: 5 additions & 0 deletions boa/src/environment/lexical_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ impl Context {
.recursive_get_this_binding(self)
}

pub(crate) fn get_global_this_binding(&mut self) -> JsResult<JsValue> {
let global = self.realm.global_env.clone();
global.get_this_binding(self)
}

pub(crate) fn create_mutable_binding(
&mut self,
name: &str,
Expand Down
47 changes: 27 additions & 20 deletions boa/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ use crate::{
object::{internal_methods::get_prototype_from_constructor, JsObject, ObjectData},
property::PropertyDescriptor,
syntax::ast::node::FormalParameter,
vm::Opcode,
vm::{call_frame::FinallyReturn, CallFrame, Opcode},
Context, JsResult, JsString, JsValue,
};
use gc::Gc;

use std::{cmp::Ordering, convert::TryInto, fmt::Write, mem::size_of};

use super::{call_frame::FinallyReturn, CallFrame};
use std::{convert::TryInto, fmt::Write, mem::size_of};

/// This represents whether a value can be read from [`CodeBlock`] code.
pub unsafe trait Readable {}
Expand Down Expand Up @@ -566,13 +563,12 @@ impl JsObject {
let arg_count = args.len();

// Push function arguments to the stack.
let args = match code.params.len().cmp(&args.len()) {
Ordering::Greater => {
let mut v = args.to_vec();
v.extend(vec![JsValue::Undefined; code.params.len() - args.len()]);
v
}
Ordering::Less | Ordering::Equal => args.to_vec(),
let args = if code.params.len() > args.len() {
let mut v = args.to_vec();
v.extend(vec![JsValue::Undefined; code.params.len() - args.len()]);
v
} else {
args.to_vec()
};

for arg in args.iter().rev() {
Expand All @@ -581,10 +577,16 @@ impl JsObject {

let param_count = code.params.len();

let this = if this.is_null_or_undefined() {
context.get_global_this_binding().expect("global env must have this binding")
} else {
this.to_object(context).expect("conversion to object cannot fail here").into()
};

context.vm.push_frame(CallFrame {
prev: None,
code,
this: this.clone(),
this,
pc: 0,
catch: Vec::new(),
finally_return: FinallyReturn::None,
Expand Down Expand Up @@ -728,13 +730,12 @@ impl JsObject {
let arg_count = args.len();

// Push function arguments to the stack.
let args = match code.params.len().cmp(&args.len()) {
Ordering::Greater => {
let mut v = args.to_vec();
v.extend(vec![JsValue::Undefined; code.params.len() - args.len()]);
v
}
Ordering::Less | Ordering::Equal => args.to_vec(),
let args = if code.params.len() > args.len() {
let mut v = args.to_vec();
v.extend(vec![JsValue::Undefined; code.params.len() - args.len()]);
v
} else {
args.to_vec()
};

for arg in args.iter().rev() {
Expand All @@ -743,6 +744,12 @@ impl JsObject {

let param_count = code.params.len();

let this = if this.is_null_or_undefined() {
context.get_global_this_binding().expect("global env must have this binding")
} else {
this.to_object(context).expect("conversion to object cannot fail here").into()
};

context.vm.push_frame(CallFrame {
prev: None,
code,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl Context {
let this = &self.vm.frame().this;

let new_env = FunctionEnvironmentRecord::new(
this.clone().as_object().unwrap().clone(), //TODO: is this ok? this_function object on stack mb?
this.clone().as_object().expect("this must always be an object").clone(),
if is_constructor || !is_lexical {
Some(this.clone())
} else {
Expand Down
10 changes: 5 additions & 5 deletions boa_tester/src/exec/js262.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ fn detach_array_buffer(
///
/// Accepts a string value as its first argument and executes it as an ECMAScript script.
fn eval_script(_this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// eprintln!("called $262.evalScript()");

if let Some(source_text) = args.get(0).and_then(|val| val.as_string()) {
#[cfg(not(feature = "vm"))]
match boa::parse(source_text.as_str(), false) {
// TODO: check strict
Err(e) => context.throw_type_error(format!("Uncaught Syntax Error: {}", e)),
#[cfg(not(feature = "vm"))]
Ok(statement_list) => statement_list.run(context),
// Calling eval here parses the code a second time.
// TODO: We can fix this after we have have defined the public api for the vm executer.
#[cfg(feature = "vm")]
Ok(_) => context.eval(source_text.as_str())
}
#[cfg(feature = "vm")]
context.eval(source_text.as_str())
} else {
Ok(JsValue::undefined())
}
Expand Down

0 comments on commit 980e719

Please sign in to comment.