Skip to content

Commit

Permalink
Feature gate nan boxing and implement fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed May 20, 2022
1 parent 6353a77 commit c3dc570
Show file tree
Hide file tree
Showing 11 changed files with 1,135 additions and 843 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme = "../README.md"
[features]
profiler = ["boa_profiler/profiler"]
deser = ["boa_interner/serde"]
nan_boxing = []

# Enable Boa's WHATWG console object implementation.
console = []
Expand Down Expand Up @@ -43,6 +44,8 @@ once_cell = "1.10.0"
tap = "1.0.1"
icu = "0.5.0"
sptr = "0.3.1"
cfg-if = "1.0.0"
num_enum = "0.5.7"

[dev-dependencies]
criterion = "0.3.5"
Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/builtins/dataview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ impl DataView {
) -> JsResult<JsValue> {
let byte_length = args.get_or_undefined(2);

let buffer_obj = args
.get_or_undefined(0);
let buffer_obj = buffer_obj.as_object()
let buffer_obj = args.get_or_undefined(0);
let buffer_obj = buffer_obj
.as_object()
.ok_or_else(|| context.construct_type_error("buffer must be an ArrayBuffer"))?;

// 1. If NewTarget is undefined, throw a TypeError exception.
Expand Down
12 changes: 10 additions & 2 deletions boa_engine/src/builtins/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ impl Proxy {
}

// 2. Return ? ProxyCreate(target, handler).
Self::create(&args.get_or_undefined(0), &args.get_or_undefined(1), context)
Self::create(
&args.get_or_undefined(0),
&args.get_or_undefined(1),
context,
)
}

// `10.5.14 ProxyCreate ( target, handler )`
Expand Down Expand Up @@ -131,7 +135,11 @@ impl Proxy {
/// [spec]: https://tc39.es/ecma262/#sec-proxy.revocable
fn revocable(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let p be ? ProxyCreate(target, handler).
let p = Self::create(&args.get_or_undefined(0),& args.get_or_undefined(1), context)?;
let p = Self::create(
&args.get_or_undefined(0),
&args.get_or_undefined(1),
context,
)?;

// 3. Let revoker be ! CreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] »).
// 4. Set revoker.[[RevocableProxy]] to p.
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ impl String {
let fill_string = args.get_or_undefined(1);

// 2. Return ? StringPad(O, maxLength, fillString, start).
Self::string_pad(this, &max_length,& fill_string, Placement::Start, context)
Self::string_pad(this, &max_length, &fill_string, Placement::Start, context)
}

/// String.prototype.trim()
Expand Down
144 changes: 87 additions & 57 deletions boa_engine/src/value/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::JsString;
use boa_profiler::Profiler;

use crate::{object::JsObject, JsBigInt, JsString, JsSymbol};

use super::{Display, JsValue};

Expand All @@ -9,63 +11,116 @@ impl From<&Self> for JsValue {
}
}

impl<T> From<T> for JsValue
where
T: Into<JsString>,
{
#[inline]
fn from(value: T) -> Self {
let _timer = Profiler::global().start_event("From<String>", "value");

Self::string(value.into())
}
}

impl From<char> for JsValue {
#[inline]
fn from(value: char) -> Self {
Self::new(value.to_string())
}
}

impl From<JsSymbol> for JsValue {
#[inline]
fn from(value: JsSymbol) -> Self {
Self::symbol(value)
}
}

impl From<f32> for JsValue {
#[allow(clippy::float_cmp)]
#[inline]
fn from(value: f32) -> Self {
// if value as i32 as f64 == value {
// Self::Integer(value as i32)
// } else {
Self::rational(value.into())
// }
}
}

impl From<f64> for JsValue {
#[allow(clippy::float_cmp)]
#[inline]
fn from(value: f64) -> Self {
// if value as i32 as f64 == value {
// Self::Integer(value as i32)
// } else {
Self::rational(value)
// }
}
}

impl From<u8> for JsValue {
#[inline]
fn from(value: u8) -> Self {
Self::from(value as i32)
Self::integer(value.into())
}
}

impl From<i8> for JsValue {
#[inline]
fn from(value: i8) -> Self {
Self::from(value as i32)
Self::integer(value.into())
}
}

impl From<u16> for JsValue {
#[inline]
fn from(value: u16) -> Self {
Self::from(value as i32)
Self::integer(value.into())
}
}

impl From<i16> for JsValue {
#[inline]
fn from(value: i16) -> Self {
Self::from(value as i32)
Self::integer(value.into())
}
}

impl From<u32> for JsValue {
#[inline]
fn from(value: u32) -> Self {
if let Ok(integer) = i32::try_from(value) {
Self::from(integer)
Self::integer(integer)
} else {
Self::from(value as f64)
Self::rational(value.into())
}
}
}

impl From<usize> for JsValue {
impl From<i32> for JsValue {
#[inline]
fn from(value: usize) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::from(value)
} else {
Self::from(value as f64)
}
fn from(value: i32) -> Self {
Self::integer(value)
}
}

impl From<JsBigInt> for JsValue {
#[inline]
fn from(value: JsBigInt) -> Self {
Self::bigint(value)
}
}

impl From<isize> for JsValue {
impl From<usize> for JsValue {
#[inline]
fn from(value: isize) -> Self {
fn from(value: usize) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::from(value)
Self::integer(value)
} else {
Self::from(value as f64)
Self::rational(value as f64)
}
}
}
Expand All @@ -74,9 +129,9 @@ impl From<u64> for JsValue {
#[inline]
fn from(value: u64) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::from(value)
Self::integer(value)
} else {
Self::from(value as f64)
Self::rational(value as f64)
}
}
}
Expand All @@ -85,57 +140,32 @@ impl From<i64> for JsValue {
#[inline]
fn from(value: i64) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::from(value)
Self::integer(value)
} else {
Self::from(value as f64)
Self::rational(value as f64)
}
}
}

impl From<f32> for JsValue {
#[allow(clippy::float_cmp)]
#[inline]
fn from(value: f32) -> Self {
// if value as i32 as f64 == value {
// Self::Integer(value as i32)
// } else {
Self::from(value as f64)
// }
}
}

impl From<char> for JsValue {
#[inline]
fn from(value: char) -> Self {
Self::from(value.to_string())
}
}

impl From<&str> for JsValue {
#[inline]
fn from(string: &str) -> Self {
Self::from(JsString::new(string))
}
}

impl From<Box<str>> for JsValue {
impl From<bool> for JsValue {
#[inline]
fn from(string: Box<str>) -> Self {
Self::from(JsString::new(string))
fn from(value: bool) -> Self {
Self::boolean(value)
}
}

impl From<&String> for JsValue {
impl From<JsObject> for JsValue {
#[inline]
fn from(string: &String) -> Self {
Self::from(JsString::new(string.as_str()))
fn from(object: JsObject) -> Self {
let _timer = Profiler::global().start_event("From<JsObject>", "value");
Self::object(object)
}
}

impl From<String> for JsValue {
impl From<()> for JsValue {
#[inline]
fn from(string: String) -> Self {
Self::from(JsString::new(string))
fn from(_: ()) -> Self {
Self::null()
}
}

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/value/equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl JsValue {
// b. If n is NaN, return false.
// c. Return the result of the comparison x == n.
(JsVariant::BigInt(a), JsVariant::String(ref b)) => match JsBigInt::from_string(b) {
Some(b) => a == b,
Some(b) => *a == b,
None => false,
},

Expand Down
Loading

0 comments on commit c3dc570

Please sign in to comment.