Skip to content

Commit d025207

Browse files
authored
Feature boa::Result<T> (#637)
1 parent 6e7b362 commit d025207

File tree

45 files changed

+422
-402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+422
-402
lines changed

boa/src/builtins/array/mod.rs

+53-29
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crate::{
1717
builtins::{
1818
object::{ObjectData, PROTOTYPE},
1919
property::{Attribute, Property},
20-
value::{same_value_zero, ResultValue, Value},
20+
value::{same_value_zero, Value},
2121
},
2222
exec::Interpreter,
23-
BoaProfiler,
23+
BoaProfiler, Result,
2424
};
2525
use std::{
2626
borrow::Borrow,
@@ -39,7 +39,7 @@ impl Array {
3939
pub(crate) const LENGTH: usize = 1;
4040

4141
/// Creates a new `Array` instance.
42-
pub(crate) fn new_array(interpreter: &Interpreter) -> ResultValue {
42+
pub(crate) fn new_array(interpreter: &Interpreter) -> Result<Value> {
4343
let array = Value::new_object(Some(
4444
&interpreter
4545
.realm()
@@ -65,7 +65,7 @@ impl Array {
6565
///
6666
/// `array_obj` can be any array with prototype already set (it will be wiped and
6767
/// recreated from `array_contents`)
68-
pub(crate) fn construct_array(array_obj: &Value, array_contents: &[Value]) -> ResultValue {
68+
pub(crate) fn construct_array(array_obj: &Value, array_contents: &[Value]) -> Result<Value> {
6969
let array_obj_ptr = array_obj.clone();
7070

7171
// Wipe existing contents of the array object
@@ -89,7 +89,7 @@ impl Array {
8989

9090
/// Utility function which takes an existing array object and puts additional
9191
/// values on the end, correctly rewriting the length
92-
pub(crate) fn add_to_array_object(array_ptr: &Value, add_values: &[Value]) -> ResultValue {
92+
pub(crate) fn add_to_array_object(array_ptr: &Value, add_values: &[Value]) -> Result<Value> {
9393
let orig_length = array_ptr.get_field("length").as_number().unwrap() as i32;
9494

9595
for (n, value) in add_values.iter().enumerate() {
@@ -106,7 +106,7 @@ impl Array {
106106
}
107107

108108
/// Create a new array
109-
pub(crate) fn make_array(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
109+
pub(crate) fn make_array(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
110110
// Make a new Object which will internally represent the Array (mapping
111111
// between indices and values): this creates an Object with no prototype
112112

@@ -166,7 +166,7 @@ impl Array {
166166
_this: &Value,
167167
args: &[Value],
168168
_interpreter: &mut Interpreter,
169-
) -> ResultValue {
169+
) -> Result<Value> {
170170
match args.get(0).and_then(|x| x.as_object()) {
171171
Some(object) => Ok(Value::from(object.is_array())),
172172
None => Ok(Value::from(false)),
@@ -185,7 +185,7 @@ impl Array {
185185
///
186186
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.concat
187187
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
188-
pub(crate) fn concat(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
188+
pub(crate) fn concat(this: &Value, args: &[Value], _: &mut Interpreter) -> Result<Value> {
189189
if args.is_empty() {
190190
// If concat is called with no arguments, it returns the original array
191191
return Ok(this.clone());
@@ -222,7 +222,7 @@ impl Array {
222222
///
223223
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.push
224224
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
225-
pub(crate) fn push(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
225+
pub(crate) fn push(this: &Value, args: &[Value], _: &mut Interpreter) -> Result<Value> {
226226
let new_array = Self::add_to_array_object(this, args)?;
227227
Ok(new_array.get_field("length"))
228228
}
@@ -237,8 +237,9 @@ impl Array {
237237
///
238238
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.pop
239239
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
240-
pub(crate) fn pop(this: &Value, _: &[Value], _: &mut Interpreter) -> ResultValue {
240+
pub(crate) fn pop(this: &Value, _: &[Value], _: &mut Interpreter) -> Result<Value> {
241241
let curr_length = this.get_field("length").as_number().unwrap() as i32;
242+
242243
if curr_length < 1 {
243244
return Ok(Value::undefined());
244245
}
@@ -263,7 +264,7 @@ impl Array {
263264
this: &Value,
264265
args: &[Value],
265266
interpreter: &mut Interpreter,
266-
) -> ResultValue {
267+
) -> Result<Value> {
267268
if args.is_empty() {
268269
return Err(Value::from("Missing argument for Array.prototype.forEach"));
269270
}
@@ -295,7 +296,7 @@ impl Array {
295296
///
296297
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.join
297298
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
298-
pub(crate) fn join(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
299+
pub(crate) fn join(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
299300
let separator = if args.is_empty() {
300301
String::from(",")
301302
} else {
@@ -328,7 +329,7 @@ impl Array {
328329
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.tostring
329330
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
330331
#[allow(clippy::wrong_self_convention)]
331-
pub(crate) fn to_string(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> ResultValue {
332+
pub(crate) fn to_string(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
332333
let method_name = "join";
333334
let mut arguments = vec![Value::from(",")];
334335
// 2.
@@ -368,8 +369,9 @@ impl Array {
368369
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.reverse
369370
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
370371
#[allow(clippy::else_if_without_else)]
371-
pub(crate) fn reverse(this: &Value, _: &[Value], _: &mut Interpreter) -> ResultValue {
372+
pub(crate) fn reverse(this: &Value, _: &[Value], _: &mut Interpreter) -> Result<Value> {
372373
let len = this.get_field("length").as_number().unwrap() as i32;
374+
373375
let middle: i32 = len.wrapping_div(2);
374376

375377
for lower in 0..middle {
@@ -406,7 +408,7 @@ impl Array {
406408
///
407409
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.shift
408410
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
409-
pub(crate) fn shift(this: &Value, _: &[Value], _: &mut Interpreter) -> ResultValue {
411+
pub(crate) fn shift(this: &Value, _: &[Value], _: &mut Interpreter) -> Result<Value> {
410412
let len = this.get_field("length").as_number().unwrap() as i32;
411413

412414
if len == 0 {
@@ -448,8 +450,9 @@ impl Array {
448450
///
449451
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.unshift
450452
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
451-
pub(crate) fn unshift(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
453+
pub(crate) fn unshift(this: &Value, args: &[Value], _: &mut Interpreter) -> Result<Value> {
452454
let len = this.get_field("length").as_number().unwrap() as i32;
455+
453456
let arg_c: i32 = args.len() as i32;
454457

455458
if arg_c > 0 {
@@ -496,7 +499,7 @@ impl Array {
496499
this: &Value,
497500
args: &[Value],
498501
interpreter: &mut Interpreter,
499-
) -> ResultValue {
502+
) -> Result<Value> {
500503
if args.is_empty() {
501504
return Err(Value::from(
502505
"missing callback when calling function Array.prototype.every",
@@ -538,7 +541,11 @@ impl Array {
538541
///
539542
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.map
540543
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
541-
pub(crate) fn map(this: &Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
544+
pub(crate) fn map(
545+
this: &Value,
546+
args: &[Value],
547+
interpreter: &mut Interpreter,
548+
) -> Result<Value> {
542549
if args.is_empty() {
543550
return Err(Value::from(
544551
"missing argument 0 when calling function Array.prototype.map",
@@ -585,7 +592,7 @@ impl Array {
585592
///
586593
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.indexof
587594
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
588-
pub(crate) fn index_of(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
595+
pub(crate) fn index_of(this: &Value, args: &[Value], _: &mut Interpreter) -> Result<Value> {
589596
// If no arguments, return -1. Not described in spec, but is what chrome does.
590597
if args.is_empty() {
591598
return Ok(Value::from(-1));
@@ -638,7 +645,11 @@ impl Array {
638645
///
639646
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.lastindexof
640647
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
641-
pub(crate) fn last_index_of(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
648+
pub(crate) fn last_index_of(
649+
this: &Value,
650+
args: &[Value],
651+
_: &mut Interpreter,
652+
) -> Result<Value> {
642653
// If no arguments, return -1. Not described in spec, but is what chrome does.
643654
if args.is_empty() {
644655
return Ok(Value::from(-1));
@@ -685,7 +696,11 @@ impl Array {
685696
///
686697
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.find
687698
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
688-
pub(crate) fn find(this: &Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
699+
pub(crate) fn find(
700+
this: &Value,
701+
args: &[Value],
702+
interpreter: &mut Interpreter,
703+
) -> Result<Value> {
689704
if args.is_empty() {
690705
return Err(Value::from(
691706
"missing callback when calling function Array.prototype.find",
@@ -721,7 +736,7 @@ impl Array {
721736
this: &Value,
722737
args: &[Value],
723738
interpreter: &mut Interpreter,
724-
) -> ResultValue {
739+
) -> Result<Value> {
725740
if args.is_empty() {
726741
return Err(Value::from(
727742
"Missing argument for Array.prototype.findIndex",
@@ -759,8 +774,9 @@ impl Array {
759774
///
760775
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.fill
761776
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
762-
pub(crate) fn fill(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
777+
pub(crate) fn fill(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
763778
let len: i32 = this.get_field("length").as_number().unwrap() as i32;
779+
764780
let default_value = Value::undefined();
765781
let value = args.get(0).unwrap_or(&default_value);
766782
let relative_start = args.get(1).unwrap_or(&default_value).to_number(ctx)? as i32;
@@ -798,7 +814,11 @@ impl Array {
798814
///
799815
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.includes
800816
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
801-
pub(crate) fn includes_value(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
817+
pub(crate) fn includes_value(
818+
this: &Value,
819+
args: &[Value],
820+
_: &mut Interpreter,
821+
) -> Result<Value> {
802822
let search_element = args.get(0).cloned().unwrap_or_else(Value::undefined);
803823

804824
let length = this.get_field("length").as_number().unwrap() as i32;
@@ -832,7 +852,7 @@ impl Array {
832852
this: &Value,
833853
args: &[Value],
834854
interpreter: &mut Interpreter,
835-
) -> ResultValue {
855+
) -> Result<Value> {
836856
let new_array = Self::new_array(interpreter)?;
837857
let len = this.get_field("length").as_number().unwrap() as i32;
838858

@@ -881,7 +901,7 @@ impl Array {
881901
this: &Value,
882902
args: &[Value],
883903
interpreter: &mut Interpreter,
884-
) -> ResultValue {
904+
) -> Result<Value> {
885905
if args.is_empty() {
886906
return Err(Value::from(
887907
"missing argument 0 when calling function Array.prototype.filter",
@@ -931,7 +951,11 @@ impl Array {
931951
///
932952
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.some
933953
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
934-
pub(crate) fn some(this: &Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
954+
pub(crate) fn some(
955+
this: &Value,
956+
args: &[Value],
957+
interpreter: &mut Interpreter,
958+
) -> Result<Value> {
935959
if args.is_empty() {
936960
return Err(Value::from(
937961
"missing callback when calling function Array.prototype.some",
@@ -978,7 +1002,7 @@ impl Array {
9781002
this: &Value,
9791003
args: &[Value],
9801004
interpreter: &mut Interpreter,
981-
) -> ResultValue {
1005+
) -> Result<Value> {
9821006
let this = this.to_object(interpreter)?;
9831007
let callback = match args.get(0) {
9841008
Some(value) if value.is_function() => value,
@@ -1045,7 +1069,7 @@ impl Array {
10451069
this: &Value,
10461070
args: &[Value],
10471071
interpreter: &mut Interpreter,
1048-
) -> ResultValue {
1072+
) -> Result<Value> {
10491073
let this = this.to_object(interpreter)?;
10501074
let callback = match args.get(0) {
10511075
Some(value) if value.is_function() => value,

boa/src/builtins/bigint/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use crate::{
1616
builtins::{
1717
function::{make_builtin_fn, make_constructor_fn},
1818
object::ObjectData,
19-
value::{RcBigInt, ResultValue, Value},
19+
value::{RcBigInt, Value},
2020
},
2121
exec::Interpreter,
22-
BoaProfiler,
22+
BoaProfiler, Result,
2323
};
2424

2525
use gc::{unsafe_empty_trace, Finalize, Trace};
@@ -61,7 +61,7 @@ impl BigInt {
6161
///
6262
/// [spec]: https://tc39.es/ecma262/#sec-thisbigintvalue
6363
#[inline]
64-
fn this_bigint_value(value: &Value, ctx: &mut Interpreter) -> Result<RcBigInt, Value> {
64+
fn this_bigint_value(value: &Value, ctx: &mut Interpreter) -> Result<RcBigInt> {
6565
match value {
6666
// 1. If Type(value) is BigInt, return value.
6767
Value::BigInt(ref bigint) => return Ok(bigint.clone()),
@@ -91,7 +91,7 @@ impl BigInt {
9191
///
9292
/// [spec]: https://tc39.es/ecma262/#sec-bigint-objects
9393
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt
94-
pub(crate) fn make_bigint(_: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
94+
pub(crate) fn make_bigint(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
9595
let data = match args.get(0) {
9696
Some(ref value) => value.to_bigint(ctx)?,
9797
None => RcBigInt::from(Self::from(0)),
@@ -110,7 +110,7 @@ impl BigInt {
110110
/// [spec]: https://tc39.es/ecma262/#sec-bigint.prototype.tostring
111111
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString
112112
#[allow(clippy::wrong_self_convention)]
113-
pub(crate) fn to_string(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
113+
pub(crate) fn to_string(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
114114
let radix = if !args.is_empty() {
115115
args[0].to_integer(ctx)? as i32
116116
} else {
@@ -135,7 +135,7 @@ impl BigInt {
135135
///
136136
/// [spec]: https://tc39.es/ecma262/#sec-bigint.prototype.valueof
137137
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/valueOf
138-
pub(crate) fn value_of(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> ResultValue {
138+
pub(crate) fn value_of(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
139139
Ok(Value::from(Self::this_bigint_value(this, ctx)?))
140140
}
141141

@@ -146,7 +146,7 @@ impl BigInt {
146146
/// [spec]: https://tc39.es/ecma262/#sec-bigint.asintn
147147
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
148148
#[allow(clippy::wrong_self_convention)]
149-
pub(crate) fn as_int_n(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
149+
pub(crate) fn as_int_n(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
150150
let (modulo, bits) = Self::calculate_as_uint_n(args, ctx)?;
151151

152152
if bits > 0 && modulo >= BigInt::from(2).pow(&BigInt::from(bits as i64 - 1)) {
@@ -165,7 +165,7 @@ impl BigInt {
165165
/// [spec]: https://tc39.es/ecma262/#sec-bigint.asuintn
166166
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintN
167167
#[allow(clippy::wrong_self_convention)]
168-
pub(crate) fn as_uint_n(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
168+
pub(crate) fn as_uint_n(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
169169
let (modulo, _) = Self::calculate_as_uint_n(args, ctx)?;
170170

171171
Ok(Value::from(modulo))
@@ -176,7 +176,7 @@ impl BigInt {
176176
/// This function expects the same arguments as `as_uint_n` and wraps the value of a `BigInt`.
177177
/// Additionally to the wrapped unsigned value it returns the converted `bits` argument, so it
178178
/// can be reused from the `as_int_n` method.
179-
fn calculate_as_uint_n(args: &[Value], ctx: &mut Interpreter) -> Result<(BigInt, u32), Value> {
179+
fn calculate_as_uint_n(args: &[Value], ctx: &mut Interpreter) -> Result<(BigInt, u32)> {
180180
use std::convert::TryFrom;
181181

182182
let undefined_value = Value::undefined();

0 commit comments

Comments
 (0)