From 641dce135782f0ebd346afabbca588bcaa7b2bbf Mon Sep 17 00:00:00 2001 From: croraf Date: Fri, 3 Jul 2020 17:14:04 +0200 Subject: [PATCH] Refactor exec/expression into exec/call and exec/new (#529) --- boa/src/exec/{expression => call}/mod.rs | 38 ++--------------------- boa/src/exec/mod.rs | 7 +++-- boa/src/exec/new/mod.rs | 39 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 39 deletions(-) rename boa/src/exec/{expression => call}/mod.rs (58%) create mode 100644 boa/src/exec/new/mod.rs diff --git a/boa/src/exec/expression/mod.rs b/boa/src/exec/call/mod.rs similarity index 58% rename from boa/src/exec/expression/mod.rs rename to boa/src/exec/call/mod.rs index 8d77eff856..363c165327 100644 --- a/boa/src/exec/expression/mod.rs +++ b/boa/src/exec/call/mod.rs @@ -1,12 +1,7 @@ -//! Expression execution. - use super::{Executable, Interpreter, InterpreterState}; use crate::{ - builtins::{ - object::{ObjectData, INSTANCE_PROTOTYPE, PROTOTYPE}, - value::{ResultValue, Type, Value}, - }, - syntax::ast::node::{Call, New, Node}, + builtins::value::{ResultValue, Type}, + syntax::ast::node::{Call, Node}, BoaProfiler, }; @@ -53,32 +48,3 @@ impl Executable for Call { fnct_result } } - -impl Executable for New { - fn run(&self, interpreter: &mut Interpreter) -> ResultValue { - // let (callee, args) = match call.as_ref() { - // Node::Call(callee, args) => (callee, args), - // _ => unreachable!("Node::New(ref call): 'call' must only be Node::Call type."), - // }; - - let func_object = self.expr().run(interpreter)?; - let mut v_args = Vec::with_capacity(self.args().len()); - for arg in self.args() { - v_args.push(arg.run(interpreter)?); - } - let this = Value::new_object(None); - // Create a blank object, then set its __proto__ property to the [Constructor].prototype - this.set_internal_slot(INSTANCE_PROTOTYPE, func_object.get_field(PROTOTYPE)); - - match func_object { - Value::Object(ref obj) => { - let obj = obj.borrow(); - if let ObjectData::Function(ref func) = obj.data { - return func.construct(func_object.clone(), &this, &v_args, interpreter); - } - interpreter.throw_type_error("not a constructor") - } - _ => Ok(Value::undefined()), - } - } -} diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index 8999040ff9..479ebf775a 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -3,13 +3,14 @@ mod array; mod block; mod break_node; +mod call; mod conditional; mod declaration; mod exception; -mod expression; mod field; mod identifier; mod iteration; +mod new; mod object; mod operator; mod return_smt; @@ -630,7 +631,7 @@ impl Executable for Node { Node::Identifier(ref identifier) => identifier.run(interpreter), Node::GetConstField(ref get_const_field_node) => get_const_field_node.run(interpreter), Node::GetField(ref get_field) => get_field.run(interpreter), - Node::Call(ref expr) => expr.run(interpreter), + Node::Call(ref call) => call.run(interpreter), Node::WhileLoop(ref while_loop) => while_loop.run(interpreter), Node::DoWhileLoop(ref do_while) => do_while.run(interpreter), Node::ForLoop(ref for_loop) => for_loop.run(interpreter), @@ -641,7 +642,7 @@ impl Executable for Node { // Node::FunctionDecl(ref decl) => decl.run(interpreter), // - Node::FunctionExpr(ref expr) => expr.run(interpreter), + Node::FunctionExpr(ref function_expr) => function_expr.run(interpreter), Node::ArrowFunctionDecl(ref decl) => decl.run(interpreter), Node::BinOp(ref op) => op.run(interpreter), Node::UnaryOp(ref op) => op.run(interpreter), diff --git a/boa/src/exec/new/mod.rs b/boa/src/exec/new/mod.rs new file mode 100644 index 0000000000..b611b617aa --- /dev/null +++ b/boa/src/exec/new/mod.rs @@ -0,0 +1,39 @@ +use super::{Executable, Interpreter}; +use crate::{ + builtins::{ + object::{ObjectData, INSTANCE_PROTOTYPE, PROTOTYPE}, + value::{ResultValue, Value}, + }, + syntax::ast::node::New, + BoaProfiler, +}; + +impl Executable for New { + fn run(&self, interpreter: &mut Interpreter) -> ResultValue { + let _timer = BoaProfiler::global().start_event("New", "exec"); + // let (callee, args) = match call.as_ref() { + // Node::Call(callee, args) => (callee, args), + // _ => unreachable!("Node::New(ref call): 'call' must only be Node::Call type."), + // }; + + let func_object = self.expr().run(interpreter)?; + let mut v_args = Vec::with_capacity(self.args().len()); + for arg in self.args() { + v_args.push(arg.run(interpreter)?); + } + let this = Value::new_object(None); + // Create a blank object, then set its __proto__ property to the [Constructor].prototype + this.set_internal_slot(INSTANCE_PROTOTYPE, func_object.get_field(PROTOTYPE)); + + match func_object { + Value::Object(ref obj) => { + let obj = obj.borrow(); + if let ObjectData::Function(ref func) = obj.data { + return func.construct(func_object.clone(), &this, &v_args, interpreter); + } + interpreter.throw_type_error("not a constructor") + } + _ => Ok(Value::undefined()), + } + } +}