Skip to content

Commit

Permalink
Replace calls to get/unwrap_or with get_or_undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Aug 22, 2021
1 parent 853cb13 commit ef48ac9
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 130 deletions.
30 changes: 16 additions & 14 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::{
};
use std::cmp::{max, min, Ordering};

use super::JsArgs;

/// JavaScript `Array` built-in implementation.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Array;
Expand Down Expand Up @@ -681,7 +683,7 @@ impl Array {
// i. Let kValue be ? Get(O, Pk).
let k_value = o.get(pk, context)?;
// ii. Perform ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
let this_arg = args.get(1).cloned().unwrap_or_else(JsValue::undefined);
let this_arg = args.get_or_undefined(1);
callback.call(&this_arg, &[k_value, k.into(), o.clone().into()], context)?;
}
// d. Set k to k + 1.
Expand Down Expand Up @@ -1008,7 +1010,7 @@ impl Array {
return context.throw_type_error("Array.prototype.every: callback is not callable");
};

let this_arg = args.get(1).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(1);

// 4. Let k be 0.
// 5. Repeat, while k < len,
Expand Down Expand Up @@ -1052,15 +1054,15 @@ impl Array {
// 2. Let len be ? LengthOfArrayLike(O).
let len = o.length_of_array_like(context)?;
// 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
let callback = args.get(0).cloned().unwrap_or_default();
let callback = args.get_or_undefined(0);
if !callback.is_function() {
return context.throw_type_error("Array.prototype.map: Callbackfn is not callable");
}

// 4. Let A be ? ArraySpeciesCreate(O, len).
let a = Self::array_species_create(&o, len, context)?;

let this_arg = args.get(1).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(1);

// 5. Let k be 0.
// 6. Repeat, while k < len,
Expand Down Expand Up @@ -1138,7 +1140,7 @@ impl Array {
}
};

let search_element = args.get(0).cloned().unwrap_or_default();
let search_element = args.get_or_undefined(0);

// 10. Repeat, while k < len,
while k < len {
Expand Down Expand Up @@ -1214,7 +1216,7 @@ impl Array {
IntegerOrInfinity::Integer(n) => len + n,
};

let search_element = args.get(0).cloned().unwrap_or_default();
let search_element = args.get_or_undefined(0);

// 8. Repeat, while k ≥ 0,
while k >= 0 {
Expand Down Expand Up @@ -1264,7 +1266,7 @@ impl Array {
}
};

let this_arg = args.get(1).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(1);

// 4. Let k be 0.
let mut k = 0;
Expand Down Expand Up @@ -1325,7 +1327,7 @@ impl Array {
}
};

let this_arg = args.get(1).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(1);

// 4. Let k be 0.
let mut k = 0;
Expand Down Expand Up @@ -1425,7 +1427,7 @@ impl Array {
let source_len = o.length_of_array_like(context)?;

// 3. If ! IsCallable(mapperFunction) is false, throw a TypeError exception.
let mapper_function = args.get(0).cloned().unwrap_or_default();
let mapper_function = args.get_or_undefined(0);
if !mapper_function.is_function() {
return context.throw_type_error("flatMap mapper function is not callable");
}
Expand All @@ -1441,7 +1443,7 @@ impl Array {
0,
1,
Some(mapper_function.as_object().unwrap()),
&args.get(1).cloned().unwrap_or_default(),
&args.get_or_undefined(1),
context,
)?;

Expand Down Expand Up @@ -1592,7 +1594,7 @@ impl Array {
// 10. Else, let final be min(relativeEnd, len).
let final_ = Self::get_relative_end(context, args.get(2), len)?;

let value = args.get(0).cloned().unwrap_or_default();
let value = args.get_or_undefined(0);

// 11. Repeat, while k < final,
while k < final_ {
Expand Down Expand Up @@ -1663,7 +1665,7 @@ impl Array {
}
}

let search_element = args.get(0).cloned().unwrap_or_default();
let search_element = args.get_or_undefined(0);

// 10. Repeat, while k < len,
while k < len {
Expand Down Expand Up @@ -1783,7 +1785,7 @@ impl Array {
"missing argument 0 when calling function Array.prototype.filter",
)
})?;
let this_val = args.get(1).cloned().unwrap_or_else(JsValue::undefined);
let this_val = args.get_or_undefined(1);

if !callback.is_callable() {
return context.throw_type_error("the callback must be callable");
Expand Down Expand Up @@ -1865,7 +1867,7 @@ impl Array {
// i. Let kValue be ? Get(O, Pk).
let k_value = o.get(k, context)?;
// ii. Let testResult be ! ToBoolean(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
let this_arg = args.get(1).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(1);
let test_result = callback
.call(&this_arg, &[k_value, k.into(), o.clone().into()], context)?
.to_boolean();
Expand Down
8 changes: 3 additions & 5 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

use crate::{
builtins::BuiltIn,
builtins::{BuiltIn, JsArgs},
object::{ConstructorBuilder, ObjectData},
property::Attribute,
symbol::WellKnownSymbols,
Expand Down Expand Up @@ -214,10 +214,8 @@ impl BigInt {
fn calculate_as_uint_n(args: &[JsValue], context: &mut Context) -> Result<(JsBigInt, u32)> {
use std::convert::TryFrom;

let undefined_value = JsValue::undefined();

let bits_arg = args.get(0).unwrap_or(&undefined_value);
let bigint_arg = args.get(1).unwrap_or(&undefined_value);
let bits_arg = args.get_or_undefined(0);
let bigint_arg = args.get_or_undefined(1);

let bits = bits_arg.to_index(context)?;
let bits = u32::try_from(bits).unwrap_or(u32::MAX);
Expand Down
7 changes: 3 additions & 4 deletions boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
mod tests;

use crate::{
builtins::BuiltIn,
builtins::{BuiltIn, JsArgs},
object::ObjectInitializer,
property::Attribute,
value::{display::display_obj, JsValue},
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn formatter(data: &[JsValue], context: &mut Context) -> Result<String> {
}
/* object, FIXME: how to render this properly? */
'o' | 'O' => {
let arg = data.get(arg_index).cloned().unwrap_or_default();
let arg = data.get_or_undefined(arg_index);
formatted.push_str(&format!("{}", arg.display()));
arg_index += 1
}
Expand Down Expand Up @@ -556,9 +556,8 @@ impl Console {
/// [spec]: https://console.spec.whatwg.org/#dir
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/dir
pub(crate) fn dir(_: &JsValue, args: &[JsValue], context: &mut Context) -> Result<JsValue> {
let undefined = JsValue::undefined();
logger(
LogMessage::Info(display_obj(args.get(0).unwrap_or(&undefined), true)),
LogMessage::Info(display_obj(&args.get_or_undefined(0), true)),
context.console(),
);

Expand Down
8 changes: 5 additions & 3 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use bitflags::bitflags;
use std::fmt::{self, Debug};
use std::rc::Rc;

use super::JsArgs;

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -309,7 +311,7 @@ impl BuiltInFunctionObject {
if !this.is_function() {
return context.throw_type_error(format!("{} is not a function", this.display()));
}
let this_arg: JsValue = args.get(0).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(0);
// TODO?: 3. Perform PrepareForTailCall
let start = if !args.is_empty() { 1 } else { 0 };
context.call(this, &this_arg, &args[start..])
Expand All @@ -330,8 +332,8 @@ impl BuiltInFunctionObject {
if !this.is_function() {
return context.throw_type_error(format!("{} is not a function", this.display()));
}
let this_arg = args.get(0).cloned().unwrap_or_default();
let arg_array = args.get(1).cloned().unwrap_or_default();
let this_arg = args.get_or_undefined(0);
let arg_array = args.get_or_undefined(1);
if arg_array.is_null_or_undefined() {
// TODO?: 3.a. PrepareForTailCall
return context.call(this, &this_arg, &[]);
Expand Down
10 changes: 6 additions & 4 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use map_iterator::{MapIterationKind, MapIterator};

use self::ordered_map::MapLock;

use super::JsArgs;

pub mod ordered_map;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -273,7 +275,7 @@ impl Map {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let key = args.get(0).cloned().unwrap_or_default();
let key = args.get_or_undefined(0);

let (deleted, size) = if let Some(object) = this.as_object() {
if let Some(map) = object.borrow_mut().as_map_mut() {
Expand All @@ -300,7 +302,7 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.get
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
pub(crate) fn get(this: &JsValue, args: &[JsValue], context: &mut Context) -> Result<JsValue> {
let key = args.get(0).cloned().unwrap_or_default();
let key = args.get_or_undefined(0);

if let JsValue::Object(ref object) = this {
let object = object.borrow();
Expand Down Expand Up @@ -345,7 +347,7 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.has
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
pub(crate) fn has(this: &JsValue, args: &[JsValue], context: &mut Context) -> Result<JsValue> {
let key = args.get(0).cloned().unwrap_or_default();
let key = args.get_or_undefined(0);

if let JsValue::Object(ref object) = this {
let object = object.borrow();
Expand Down Expand Up @@ -377,7 +379,7 @@ impl Map {
}

let callback_arg = &args[0];
let this_arg = args.get(1).cloned().unwrap_or_else(JsValue::undefined);
let this_arg = args.get_or_undefined(1);

let mut index = 0;

Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn init(context: &mut Context) {
}
}

pub(crate) trait JsArgs {
pub trait JsArgs {
fn get_or_undefined(&self, index: usize) -> JsValue;
}

Expand Down
10 changes: 5 additions & 5 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//! [spec]: https://tc39.es/ecma262/#sec-number-object
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

use super::function::make_builtin_fn;
use super::string::is_trimmable_whitespace;
use super::{function::make_builtin_fn, JsArgs};
use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, ObjectData, PROTOTYPE},
Expand Down Expand Up @@ -392,12 +392,12 @@ impl Number {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let precision = args.get(0).cloned().unwrap_or_default();
let precision = args.get_or_undefined(0);

// 1 & 6
let mut this_num = Self::this_number_value(this, context)?;
// 2
if precision == JsValue::undefined() {
if precision.is_undefined() {
return Self::to_string(this, &[], context);
}

Expand Down Expand Up @@ -720,7 +720,7 @@ impl Number {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
if let (Some(val), radix) = (args.get(0), args.get(1)) {
if let (Some(val), radix) = (args.get(0), args.get_or_undefined(1)) {
// 1. Let inputString be ? ToString(string).
let input_string = val.to_string(context)?;

Expand All @@ -745,7 +745,7 @@ impl Number {
}

// 6. Let R be ℝ(? ToInt32(radix)).
let mut var_r = radix.cloned().unwrap_or_default().to_i32(context)?;
let mut var_r = radix.to_i32(context)?;

// 7. Let stripPrefix be true.
let mut strip_prefix = true;
Expand Down
26 changes: 11 additions & 15 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

use crate::{
builtins::BuiltIn,
builtins::{BuiltIn, JsArgs},
object::{
ConstructorBuilder, Object as BuiltinObject, ObjectData, ObjectInitializer, PROTOTYPE,
},
Expand Down Expand Up @@ -126,8 +126,8 @@ impl Object {
/// [spec]: https://tc39.es/ecma262/#sec-object.create
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
pub fn create(_: &JsValue, args: &[JsValue], context: &mut Context) -> Result<JsValue> {
let prototype = args.get(0).cloned().unwrap_or_else(JsValue::undefined);
let properties = args.get(1).cloned().unwrap_or_else(JsValue::undefined);
let prototype = args.get_or_undefined(0);
let properties = args.get_or_undefined(1);

let obj = match prototype {
JsValue::Object(_) | JsValue::Null => JsValue::new(BuiltinObject::with_prototype(
Expand Down Expand Up @@ -164,10 +164,7 @@ impl Object {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let object = args
.get(0)
.unwrap_or(&JsValue::undefined())
.to_object(context)?;
let object = args.get_or_undefined(0).to_object(context)?;
if let Some(key) = args.get(1) {
let key = key.to_property_key(context)?;

Expand Down Expand Up @@ -266,8 +263,8 @@ impl Object {

/// Uses the SameValue algorithm to check equality of objects
pub fn is(_: &JsValue, args: &[JsValue], _: &mut Context) -> Result<JsValue> {
let x = args.get(0).cloned().unwrap_or_else(JsValue::undefined);
let y = args.get(1).cloned().unwrap_or_else(JsValue::undefined);
let x = args.get_or_undefined(0);
let y = args.get_or_undefined(1);

Ok(JsValue::same_value(&x, &y).into())
}
Expand Down Expand Up @@ -309,7 +306,7 @@ impl Object {
.clone();

// 2. If Type(proto) is neither Object nor Null, throw a TypeError exception.
let proto = args.get(1).cloned().unwrap_or_default();
let proto = args.get_or_undefined(1);
if !matches!(proto.get_type(), Type::Object | Type::Null) {
return ctx.throw_type_error(format!(
"expected an object or null, got {}",
Expand Down Expand Up @@ -352,8 +349,7 @@ impl Object {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let undefined = JsValue::undefined();
let mut v = args.get(0).unwrap_or(&undefined).clone();
let mut v = args.get_or_undefined(0);
if !v.is_object() {
return Ok(JsValue::new(false));
}
Expand All @@ -375,7 +371,7 @@ impl Object {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let object = args.get(0).cloned().unwrap_or_else(JsValue::undefined);
let object = args.get_or_undefined(0);
if let Some(object) = object.as_object() {
let key = args
.get(1)
Expand Down Expand Up @@ -409,10 +405,10 @@ impl Object {
args: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let arg = args.get(0).cloned().unwrap_or_default();
let arg = args.get_or_undefined(0);
let arg_obj = arg.as_object();
if let Some(mut obj) = arg_obj {
let props = args.get(1).cloned().unwrap_or_else(JsValue::undefined);
let props = args.get_or_undefined(1);
obj.define_properties(props, context)?;
Ok(arg)
} else {
Expand Down
Loading

0 comments on commit ef48ac9

Please sign in to comment.