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

Initial implementation of Map() #550

Merged
merged 33 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d79b1c7
Initial implementation of Map()
joshwd36 Jul 5, 2020
352a78d
Remove impl InternalState for HashMap
joshwd36 Jul 5, 2020
aadf771
Fix clippy lint
joshwd36 Jul 5, 2020
cf925ce
Actually fix clippy lint
joshwd36 Jul 5, 2020
029863f
Fix ok_or lint
joshwd36 Jul 5, 2020
85a3702
format code
joshwd36 Jul 5, 2020
11c4004
Use IndexMap instead of HashMap
joshwd36 Jul 6, 2020
72d8786
Initial implementation of Map()
joshwd36 Jul 5, 2020
f11be4d
Remove impl InternalState for HashMap
joshwd36 Jul 5, 2020
57c87d9
Fix clippy lint
joshwd36 Jul 5, 2020
329e580
Actually fix clippy lint
joshwd36 Jul 5, 2020
3bc79d9
Fix ok_or lint
joshwd36 Jul 5, 2020
07f64af
format code
joshwd36 Jul 5, 2020
5e9358b
Rebase upstream
joshwd36 Jul 11, 2020
84293d9
Prevent recursive display for map and array
joshwd36 Jul 11, 2020
d381a91
Merge
joshwd36 Jul 11, 2020
e663666
Run rustfmt
joshwd36 Jul 11, 2020
96e82d2
Merge create into init
joshwd36 Jul 11, 2020
cce249a
Fix return type on init
joshwd36 Jul 11, 2020
a6e093a
Initial implementation of preventing Map() being called as a function
joshwd36 Jul 11, 2020
77a0833
Use bitflags for callable and constructable
joshwd36 Jul 11, 2020
374f854
FunctionFlags doesn't have to be public
joshwd36 Jul 11, 2020
f21e217
Initial implementation of Map()
joshwd36 Jul 5, 2020
3c37294
Rebase upstream
joshwd36 Jul 11, 2020
a9853b8
Prevent recursive display for map and array
joshwd36 Jul 11, 2020
2a6d23e
Initial implementation of Map()
joshwd36 Jul 5, 2020
0af0d8e
Use IndexMap instead of HashMap
joshwd36 Jul 6, 2020
c05028e
Run rustfmt
joshwd36 Jul 11, 2020
1db2ee4
Remove artifact from rebase
joshwd36 Jul 14, 2020
1730256
Use into() for value creation
joshwd36 Jul 14, 2020
7716cbd
Remove GcCell around OrderedMap
joshwd36 Jul 14, 2020
ae519c3
Remove commented get_map function
joshwd36 Jul 14, 2020
213747b
Merge branch 'master' into map
HalidOdat Jul 17, 2020
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
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rustc-hash = "1.1.0"
num-bigint = { version = "0.3.0", features = ["serde"] }
num-integer = "0.1.43"
bitflags = "1.2.1"
indexmap = "1.4.0"

# Optional Dependencies
serde = { version = "1.0.114", features = ["derive"], optional = true }
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ impl Array {
global,
prototype,
true,
true,
);

// Static Methods
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl BigInt {
global,
prototype,
false,
true,
);

make_builtin_fn(Self::as_int_n, "asIntN", &bigint_object, 2);
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl Boolean {
global,
prototype,
true,
true,
);

(Self::NAME, boolean_object)
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl Error {
global,
prototype,
true,
true,
);

(Self::NAME, error_object)
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl RangeError {
global,
prototype,
true,
true,
);

(Self::NAME, range_error_object)
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl ReferenceError {
global,
prototype,
true,
true,
);

(Self::NAME, reference_error_object)
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl SyntaxError {
global,
prototype,
true,
true,
);

(Self::NAME, syntax_error_object)
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl TypeError {
global,
prototype,
true,
true,
);

(Self::NAME, type_error_object)
Expand Down
60 changes: 48 additions & 12 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
syntax::ast::node::{FormalParameter, StatementList},
BoaProfiler,
};
use bitflags::bitflags;
use gc::{unsafe_empty_trace, Finalize, Trace};
use std::fmt::{self, Debug};

Expand Down Expand Up @@ -90,6 +91,43 @@ unsafe impl Trace for FunctionBody {
unsafe_empty_trace!();
}

bitflags! {
#[derive(Finalize, Default)]
struct FunctionFlags: u8 {
const CALLABLE = 0b0000_0001;
const CONSTRUCTABLE = 0b0000_0010;
}
}

impl FunctionFlags {
fn from_parameters(callable: bool, constructable: bool) -> Self {
let mut flags = Self::default();

if callable {
flags |= Self::CALLABLE;
}
if constructable {
flags |= Self::CONSTRUCTABLE;
}

flags
}

#[inline]
fn is_callable(&self) -> bool {
self.contains(Self::CALLABLE)
}

#[inline]
fn is_constructable(&self) -> bool {
self.contains(Self::CONSTRUCTABLE)
}
}

unsafe impl Trace for FunctionFlags {
unsafe_empty_trace!();
}

/// Boa representation of a Function Object.
///
/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects>
Expand All @@ -103,10 +141,8 @@ pub struct Function {
pub this_mode: ThisMode,
// Environment, built-in functions don't need Environments
pub environment: Option<Environment>,
/// Is it constructable
constructable: bool,
/// Is it callable.
callable: bool,
/// Is it constructable or
flags: FunctionFlags,
}

impl Function {
Expand All @@ -126,8 +162,7 @@ impl Function {
environment: scope,
params: parameter_list.into(),
this_mode,
constructable,
callable,
flags: FunctionFlags::from_parameters(callable, constructable),
}
}

Expand Down Expand Up @@ -183,7 +218,7 @@ impl Function {
interpreter: &mut Interpreter,
) -> ResultValue {
let _timer = BoaProfiler::global().start_event("function::call", "function");
if self.callable {
if self.flags.is_callable() {
match self.body {
FunctionBody::BuiltIn(func) => func(this, args_list, interpreter),
FunctionBody::Ordinary(ref body) => {
Expand Down Expand Up @@ -249,7 +284,7 @@ impl Function {
args_list: &[Value],
interpreter: &mut Interpreter,
) -> ResultValue {
if self.constructable {
if self.flags.is_constructable() {
match self.body {
FunctionBody::BuiltIn(func) => {
func(this, args_list, interpreter)?;
Expand Down Expand Up @@ -351,12 +386,12 @@ impl Function {

/// Returns true if the function object is callable.
pub fn is_callable(&self) -> bool {
self.callable
self.flags.is_callable()
}

/// Returns true if the function object is constructable.
pub fn is_constructable(&self) -> bool {
self.constructable
self.flags.is_constructable()
}
}

Expand Down Expand Up @@ -420,13 +455,14 @@ pub fn make_constructor_fn(
global: &Value,
prototype: Value,
constructable: bool,
callable: bool,
) -> Value {
let _timer =
BoaProfiler::global().start_event(&format!("make_constructor_fn: {}", name), "init");

// Create the native function
let mut function = Function::builtin(Vec::new(), body);
function.constructable = constructable;
function.flags = FunctionFlags::from_parameters(callable, constructable);

let mut constructor = Object::function(function);

Expand Down Expand Up @@ -507,7 +543,7 @@ pub fn init(global: &Value) -> (&str, Value) {
let prototype = Value::new_object(Some(global));

let function_object =
make_constructor_fn("Function", 1, make_function, global, prototype, true);
make_constructor_fn("Function", 1, make_function, global, prototype, true, true);

("Function", function_object)
}
Loading