Skip to content

Commit

Permalink
Merge e5e36bb into fecd33d
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored May 9, 2020
2 parents fecd33d + e5e36bb commit 1fab45b
Show file tree
Hide file tree
Showing 33 changed files with 939 additions and 1,029 deletions.
254 changes: 112 additions & 142 deletions boa/src/builtins/array/mod.rs

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests;
use crate::{
builtins::{
object::{internal_methods_trait::ObjectInternalMethods, Object, ObjectKind, PROTOTYPE},
value::{to_value, ResultValue, Value, ValueData},
value::{ResultValue, Value, ValueData},
},
exec::Interpreter,
};
Expand All @@ -29,7 +29,7 @@ pub fn construct_boolean(this: &mut Value, args: &[Value], _: &mut Interpreter)
if let Some(ref value) = args.get(0) {
this.set_internal_slot("BooleanData", to_boolean(value));
} else {
this.set_internal_slot("BooleanData", to_boolean(&to_value(false)));
this.set_internal_slot("BooleanData", to_boolean(&Value::from(false)));
}

// no need to return `this` as its passed by reference
Expand All @@ -41,7 +41,7 @@ pub fn call_boolean(_: &mut Value, args: &[Value], _: &mut Interpreter) -> Resul
// Get the argument, if any
match args.get(0) {
Some(ref value) => Ok(to_boolean(value)),
None => Ok(to_boolean(&to_value(false))),
None => Ok(to_boolean(&Value::from(false))),
}
}

Expand All @@ -55,7 +55,7 @@ pub fn call_boolean(_: &mut Value, args: &[Value], _: &mut Interpreter) -> Resul
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/toString
pub fn to_string(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue {
let b = this_boolean_value(this);
Ok(to_value(b.to_string()))
Ok(Value::from(b.to_string()))
}

/// The valueOf() method returns the primitive value of a `Boolean` object.
Expand All @@ -75,12 +75,12 @@ pub fn value_of(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultVal
/// Creates a new boolean value from the input
pub fn to_boolean(value: &Value) -> Value {
match *value.deref().borrow() {
ValueData::Object(_) => to_value(true),
ValueData::String(ref s) if !s.is_empty() => to_value(true),
ValueData::Rational(n) if n != 0.0 && !n.is_nan() => to_value(true),
ValueData::Integer(n) if n != 0 => to_value(true),
ValueData::Boolean(v) => to_value(v),
_ => to_value(false),
ValueData::Object(_) => Value::from(true),
ValueData::String(ref s) if !s.is_empty() => Value::from(true),
ValueData::Rational(n) if n != 0.0 && !n.is_nan() => Value::from(true),
ValueData::Integer(n) if n != 0 => Value::from(true),
ValueData::Boolean(v) => Value::from(v),
_ => Value::from(false),
}
}

Expand All @@ -92,18 +92,18 @@ pub fn to_boolean(value: &Value) -> Value {
/// [spec]: https://tc39.es/ecma262/#sec-thisbooleanvalue
pub fn this_boolean_value(value: &Value) -> Value {
match *value.deref().borrow() {
ValueData::Boolean(v) => to_value(v),
ValueData::Boolean(v) => Value::from(v),
ValueData::Object(ref v) => (v).deref().borrow().get_internal_slot("BooleanData"),
_ => to_value(false),
_ => Value::from(false),
}
}

/// Create a new `Boolean` object.
pub fn create(global: &Value) -> Value {
// Create Prototype
// https://tc39.es/ecma262/#sec-properties-of-the-boolean-prototype-object
let prototype = ValueData::new_obj(Some(global));
prototype.set_internal_slot("BooleanData", to_boolean(&to_value(false)));
let prototype = Value::new_object(Some(global));
prototype.set_internal_slot("BooleanData", to_boolean(&Value::from(false)));

make_builtin_fn!(to_string, named "toString", of prototype);
make_builtin_fn!(value_of, named "valueOf", of prototype);
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/boolean/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{builtins::value::same_value, forward, forward_val};

#[test]
fn check_boolean_constructor_is_function() {
let global = ValueData::new_obj(None);
let global = Value::new_object(None);
let boolean_constructor = create(&global);
assert_eq!(boolean_constructor.is_function(), true);
}
Expand Down
56 changes: 28 additions & 28 deletions boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ mod tests;
use crate::{
builtins::{
object::InternalState,
value::{
display_obj, from_value, to_value, undefined, FromValue, ResultValue, Value, ValueData,
},
value::{display_obj, ResultValue, Value},
},
exec::Interpreter,
};
Expand All @@ -48,10 +46,11 @@ pub enum LogMessage {
}

/// Helper function that returns the argument at a specified index.
fn get_arg_at_index<T: FromValue + Default>(args: &[Value], index: usize) -> Option<T> {
args.get(index)
.cloned()
.map(|s| from_value::<T>(s).expect("Convert error"))
fn get_arg_at_index<'a, T>(args: &'a [Value], index: usize) -> Option<T>
where
T: From<&'a Value> + Default,
{
args.get(index).map(|s| T::from(s))
}

/// Helper function for logging messages.
Expand Down Expand Up @@ -147,20 +146,20 @@ pub fn assert(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVa
let mut args: Vec<Value> = args.iter().skip(1).cloned().collect();
let message = "Assertion failed".to_string();
if args.is_empty() {
args.push(to_value::<String>(message));
args.push(Value::from(message));
} else if !args[0].is_string() {
args.insert(0, to_value::<String>(message));
args.insert(0, Value::from(message));
} else {
let concat = format!("{}: {}", message, args[0]);
args[0] = to_value::<String>(concat);
args[0] = Value::from(concat);
}

this.with_internal_state_ref(|state| {
logger(LogMessage::Error(formatter(&args[..])), state)
});
}

Ok(undefined())
Ok(Value::undefined())
}

/// `console.clear()`
Expand All @@ -178,7 +177,7 @@ pub fn clear(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue
state.groups.clear();
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.debug(...data)`
Expand All @@ -193,7 +192,7 @@ pub fn clear(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/debug
pub fn debug(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
this.with_internal_state_ref(|state| logger(LogMessage::Log(formatter(&args[..])), state));
Ok(undefined())
Ok(Value::undefined())
}

/// `console.error(...data)`
Expand All @@ -208,7 +207,7 @@ pub fn debug(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVal
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/error
pub fn error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
this.with_internal_state_ref(|state| logger(LogMessage::Error(formatter(&args[..])), state));
Ok(undefined())
Ok(Value::undefined())
}

/// `console.info(...data)`
Expand All @@ -223,7 +222,7 @@ pub fn error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVal
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/info
pub fn info(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
this.with_internal_state_ref(|state| logger(LogMessage::Info(formatter(&args[..])), state));
Ok(undefined())
Ok(Value::undefined())
}

/// `console.log(...data)`
Expand All @@ -238,7 +237,7 @@ pub fn info(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValu
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/log
pub fn log(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
this.with_internal_state_ref(|state| logger(LogMessage::Log(formatter(&args[..])), state));
Ok(undefined())
Ok(Value::undefined())
}

/// `console.trace(...data)`
Expand All @@ -264,7 +263,7 @@ pub fn trace(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVal
});
}

Ok(undefined())
Ok(Value::undefined())
}

/// `console.warn(...data)`
Expand All @@ -279,7 +278,7 @@ pub fn trace(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVal
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/warn
pub fn warn(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
this.with_internal_state_ref(|state| logger(LogMessage::Warn(formatter(&args[..])), state));
Ok(undefined())
Ok(Value::undefined())
}

/// `console.count(label)`
Expand All @@ -303,7 +302,7 @@ pub fn count(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVal
logger(LogMessage::Info(format!("{} {}", msg, c)), state);
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.countReset(label)`
Expand All @@ -325,7 +324,7 @@ pub fn count_reset(this: &mut Value, args: &[Value], _: &mut Interpreter) -> Res
logger(LogMessage::Warn(format!("countReset {}", label)), state);
});

Ok(undefined())
Ok(Value::undefined())
}

/// Returns current system time in ms.
Expand Down Expand Up @@ -361,7 +360,7 @@ pub fn time(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValu
}
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.timeLog(label, ...data)`
Expand Down Expand Up @@ -393,7 +392,7 @@ pub fn time_log(this: &mut Value, args: &[Value], _: &mut Interpreter) -> Result
}
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.timeEnd(label)`
Expand Down Expand Up @@ -424,7 +423,7 @@ pub fn time_end(this: &mut Value, args: &[Value], _: &mut Interpreter) -> Result
}
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.group(...data)`
Expand All @@ -445,7 +444,7 @@ pub fn group(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVal
state.groups.push(group_label);
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.groupEnd(label)`
Expand All @@ -463,7 +462,7 @@ pub fn group_end(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultVa
state.groups.pop();
});

Ok(undefined())
Ok(Value::undefined())
}

/// `console.dir(item, options)`
Expand All @@ -478,18 +477,19 @@ pub fn group_end(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultVa
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/dir
pub fn dir(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
this.with_internal_state_mut(|state: &mut ConsoleState| {
let undefined = Value::undefined();
logger(
LogMessage::Info(display_obj(args.get(0).unwrap_or(&undefined()), true)),
LogMessage::Info(display_obj(args.get(0).unwrap_or(&undefined), true)),
state,
);
});

Ok(undefined())
Ok(Value::undefined())
}

/// Create a new `console` object
pub fn create(global: &Value) -> Value {
let console = ValueData::new_obj(Some(global));
let console = Value::new_object(Some(global));

make_builtin_fn!(assert, named "assert", of console);
make_builtin_fn!(clear, named "clear", of console);
Expand Down
32 changes: 13 additions & 19 deletions boa/src/builtins/console/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::builtins::{console::formatter, value::ValueData};
use gc::Gc;
use crate::builtins::{console::formatter, value::Value};

#[test]
fn formatter_no_args_is_empty_string() {
Expand All @@ -8,24 +7,24 @@ fn formatter_no_args_is_empty_string() {

#[test]
fn formatter_empty_format_string_is_empty_string() {
let val = Gc::new(ValueData::String("".to_string()));
let val = Value::string("".to_string());
let res = formatter(&[val]);
assert_eq!(res, "");
}

#[test]
fn formatter_format_without_args_renders_verbatim() {
let val = [Gc::new(ValueData::String("%d %s %% %f".to_string()))];
let val = [Value::string("%d %s %% %f".to_string())];
let res = formatter(&val);
assert_eq!(res, "%d %s %% %f");
}

#[test]
fn formatter_empty_format_string_concatenates_rest_of_args() {
let val = [
Gc::new(ValueData::String("".to_string())),
Gc::new(ValueData::String("to powinno zostać".to_string())),
Gc::new(ValueData::String("połączone".to_string())),
Value::string("".to_string()),
Value::string("to powinno zostać".to_string()),
Value::string("połączone".to_string()),
];
let res = formatter(&val);
assert_eq!(res, " to powinno zostać połączone");
Expand All @@ -34,12 +33,10 @@ fn formatter_empty_format_string_concatenates_rest_of_args() {
#[test]
fn formatter_utf_8_checks() {
let val = [
Gc::new(ValueData::String(
"Są takie chwile %dą %są tu%sów %привет%ź".to_string(),
)),
Gc::new(ValueData::Integer(123)),
Gc::new(ValueData::Rational(1.23)),
Gc::new(ValueData::String("ł".to_string())),
Value::string("Są takie chwile %dą %są tu%sów %привет%ź".to_string()),
Value::integer(123),
Value::rational(1.23),
Value::string("ł".to_string()),
];
let res = formatter(&val);
assert_eq!(res, "Są takie chwile 123ą 1.23ą tułów %привет%ź");
Expand All @@ -48,8 +45,8 @@ fn formatter_utf_8_checks() {
#[test]
fn formatter_trailing_format_leader_renders() {
let val = [
Gc::new(ValueData::String("%%%%%".to_string())),
Gc::new(ValueData::String("|".to_string())),
Value::string("%%%%%".to_string()),
Value::string("|".to_string()),
];
let res = formatter(&val);
assert_eq!(res, "%%% |")
Expand All @@ -58,10 +55,7 @@ fn formatter_trailing_format_leader_renders() {
#[test]
#[allow(clippy::approx_constant)]
fn formatter_float_format_works() {
let val = [
Gc::new(ValueData::String("%f".to_string())),
Gc::new(ValueData::Rational(3.1415)),
];
let val = [Value::string("%f".to_string()), Value::rational(3.1415)];
let res = formatter(&val);
assert_eq!(res, "3.141500")
}
Loading

0 comments on commit 1fab45b

Please sign in to comment.