Skip to content

Commit

Permalink
Improve ergonomics of from_proto_and_data
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Oct 5, 2021
1 parent 419e4fc commit b555c0b
Show file tree
Hide file tree
Showing 29 changed files with 56 additions and 63 deletions.
4 changes: 2 additions & 2 deletions boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ArrayIterator {
context: &Context,
) -> JsValue {
let array_iterator = JsObject::from_proto_and_data(
Some(context.iterator_prototypes().array_iterator()),
context.iterator_prototypes().array_iterator(),
ObjectData::array_iterator(Self::new(array, kind)),
);
array_iterator.into()
Expand Down Expand Up @@ -129,7 +129,7 @@ impl ArrayIterator {

// Create prototype
let array_iterator =
JsObject::from_proto_and_data(Some(iterator_prototype), ObjectData::ordinary());
JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &array_iterator, 0, context);

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl Array {
Some(prototype) => prototype,
None => context.standard_objects().array_object().prototype(),
};
let array = JsObject::from_proto_and_data(Some(prototype), ObjectData::array());
let array = JsObject::from_proto_and_data(prototype, ObjectData::array());

// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
crate::object::internal_methods::ordinary_define_own_property(
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Boolean {
}
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::boolean_object, context)?;
let boolean = JsObject::from_proto_and_data(Some(prototype), ObjectData::boolean(data));
let boolean = JsObject::from_proto_and_data(prototype, ObjectData::boolean(data));

Ok(boolean.into())
}
Expand Down
8 changes: 4 additions & 4 deletions boa/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl Date {
/// [spec]: https://tc39.es/ecma262/#sec-date-constructor
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
pub(crate) fn make_date_now(prototype: JsObject) -> JsObject {
JsObject::from_proto_and_data(Some(prototype), ObjectData::date(Date::default()))
JsObject::from_proto_and_data(prototype, ObjectData::date(Date::default()))
}

/// `Date(value)`
Expand Down Expand Up @@ -425,7 +425,7 @@ impl Date {

let tv = tv.filter(|time| Self::time_clip(time.timestamp_millis() as f64).is_some());
Ok(JsObject::from_proto_and_data(
Some(prototype),
prototype,
ObjectData::date(Date(tv)),
))
}
Expand Down Expand Up @@ -466,7 +466,7 @@ impl Date {
// If any of the args are infinity or NaN, return an invalid date.
if !check_normal_opt!(year, month, day, hour, min, sec, milli) {
return Ok(JsObject::from_proto_and_data(
Some(prototype),
prototype,
ObjectData::date(Date(None)),
));
}
Expand Down Expand Up @@ -494,7 +494,7 @@ impl Date {
);

Ok(JsObject::from_proto_and_data(
Some(prototype),
prototype,
ObjectData::date(date),
))
}
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl EvalError {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Error {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl RangeError {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl ReferenceError {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl SyntaxError {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl TypeError {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl UriError {
) -> JsResult<JsValue> {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
let obj = JsObject::from_proto_and_data(Some(prototype), ObjectData::error());
let obj = JsObject::from_proto_and_data(prototype, ObjectData::error());
if let Some(message) = args.get(0) {
if !message.is_undefined() {
obj.set("message", message.to_string(context)?, false, context)?;
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub(crate) fn make_builtin_fn<N>(
let _timer = BoaProfiler::global().start_event(&format!("make_builtin_fn: {}", &name), "init");

let function = JsObject::from_proto_and_data(
Some(interpreter.standard_objects().function_object().prototype()),
interpreter.standard_objects().function_object().prototype(),
ObjectData::function(Function::Native {
function,
constructable: false,
Expand Down Expand Up @@ -397,7 +397,7 @@ impl BuiltInFunctionObject {
get_prototype_from_constructor(new_target, StandardObjects::function_object, context)?;

let this = JsObject::from_proto_and_data(
Some(prototype),
prototype,
ObjectData::function(Function::Native {
function: |_, _, _| Ok(JsValue::undefined()),
constructable: true,
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/map/map_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl MapIterator {
lock,
};
let map_iterator = JsObject::from_proto_and_data(
Some(context.iterator_prototypes().map_iterator()),
context.iterator_prototypes().map_iterator(),
ObjectData::map_iterator(iter),
);
return Ok(map_iterator.into());
Expand Down Expand Up @@ -129,7 +129,7 @@ impl MapIterator {

// Create prototype
let map_iterator =
JsObject::from_proto_and_data(Some(iterator_prototype), ObjectData::ordinary());
JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &map_iterator, 0, context);

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand Down
3 changes: 1 addition & 2 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ impl Map {
// 3. Set map.[[MapData]] to a new empty List.
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::map_object, context)?;
let map =
JsObject::from_proto_and_data(Some(prototype), ObjectData::map(OrderedMap::new()));
let map = JsObject::from_proto_and_data(prototype, ObjectData::map(OrderedMap::new()));

// 4. If iterable is either undefined or null, return map.
let iterable = match args.get_or_undefined(0) {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Number {
}
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::number_object, context)?;
let this = JsObject::from_proto_and_data(Some(prototype), ObjectData::number(data));
let this = JsObject::from_proto_and_data(prototype, ObjectData::number(data));
Ok(this.into())
}

Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/object/for_in_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ForInIterator {
/// [spec]: https://tc39.es/ecma262/#sec-createforiniterator
pub(crate) fn create_for_in_iterator(object: JsValue, context: &Context) -> JsValue {
let for_in_iterator = JsObject::from_proto_and_data(
Some(context.iterator_prototypes().for_in_iterator()),
context.iterator_prototypes().for_in_iterator(),
ObjectData::for_in_iterator(Self::new(object)),
);
for_in_iterator.into()
Expand Down Expand Up @@ -135,7 +135,7 @@ impl ForInIterator {

// Create prototype
let for_in_iterator =
JsObject::from_proto_and_data(Some(iterator_prototype), ObjectData::ordinary());
JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &for_in_iterator, 0, context);

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Object {
StandardObjects::object_object,
context,
)?;
let object = JsObject::from_proto_and_data(Some(prototype), ObjectData::ordinary());
let object = JsObject::from_proto_and_data(prototype, ObjectData::ordinary());
return Ok(object.into());
}
if let Some(arg) = args.get(0) {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl RegExp {
fn alloc(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let proto = get_prototype_from_constructor(this, StandardObjects::regexp_object, context)?;

Ok(JsObject::from_proto_and_data(Some(proto), ObjectData::ordinary()).into())
Ok(JsObject::from_proto_and_data(proto, ObjectData::ordinary()).into())
}

/// `22.2.3.2.2 RegExpInitialize ( obj, pattern, flags )`
Expand Down
5 changes: 2 additions & 3 deletions boa/src/builtins/regexp/regexp_string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl RegExpStringIterator {
// 5. Return ! CreateIteratorFromClosure(closure, "%RegExpStringIteratorPrototype%", %RegExpStringIteratorPrototype%).

let regexp_string_iterator = JsObject::from_proto_and_data(
Some(context.iterator_prototypes().regexp_string_iterator()),
context.iterator_prototypes().regexp_string_iterator(),
ObjectData::reg_exp_string_iterator(Self::new(
matcher.clone(),
string,
Expand Down Expand Up @@ -162,8 +162,7 @@ impl RegExpStringIterator {
let _timer = BoaProfiler::global().start_event("RegExp String Iterator", "init");

// Create prototype
let result =
JsObject::from_proto_and_data(Some(iterator_prototype), ObjectData::ordinary());
let result = JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &result, 0, context);

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand Down
3 changes: 1 addition & 2 deletions boa/src/builtins/set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ impl Set {
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::set_object, context)?;

let obj =
JsObject::from_proto_and_data(Some(prototype), ObjectData::set(OrderedSet::default()));
let obj = JsObject::from_proto_and_data(prototype, ObjectData::set(OrderedSet::default()));

let iterable = args.get_or_undefined(0);
// 4
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/set/set_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl SetIterator {
context: &Context,
) -> JsValue {
let set_iterator = JsObject::from_proto_and_data(
Some(context.iterator_prototypes().set_iterator()),
context.iterator_prototypes().set_iterator(),
ObjectData::set_iterator(Self::new(set, kind)),
);
set_iterator.into()
Expand Down Expand Up @@ -146,7 +146,7 @@ impl SetIterator {

// Create prototype
let set_iterator =
JsObject::from_proto_and_data(Some(iterator_prototype), ObjectData::ordinary());
JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &set_iterator, 0, context);

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl String {
// 4. Set S.[[GetOwnProperty]] as specified in 10.4.3.1.
// 5. Set S.[[DefineOwnProperty]] as specified in 10.4.3.2.
// 6. Set S.[[OwnPropertyKeys]] as specified in 10.4.3.3.
let s = JsObject::from_proto_and_data(Some(prototype), ObjectData::string(value));
let s = JsObject::from_proto_and_data(prototype, ObjectData::string(value));

// 8. Perform ! DefinePropertyOrThrow(S, "length", PropertyDescriptor { [[Value]]: 𝔽(length),
// [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/string/string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl StringIterator {

pub fn create_string_iterator(string: JsValue, context: &mut Context) -> JsResult<JsValue> {
let string_iterator = JsObject::from_proto_and_data(
Some(context.iterator_prototypes().string_iterator()),
context.iterator_prototypes().string_iterator(),
ObjectData::string_iterator(Self::new(string)),
);
Ok(string_iterator.into())
Expand Down Expand Up @@ -84,7 +84,7 @@ impl StringIterator {

// Create prototype
let array_iterator =
JsObject::from_proto_and_data(Some(iterator_prototype), ObjectData::ordinary());
JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &array_iterator, 0, context);

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand Down
2 changes: 1 addition & 1 deletion boa/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<T: Class> ClassConstructor for T {

let native_instance = Self::constructor(this, args, context)?;
let object_instance = JsObject::from_proto_and_data(
Some(prototype),
prototype,
ObjectData::native_object(Box::new(native_instance)),
);
Ok(object_instance.into())
Expand Down
8 changes: 5 additions & 3 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,10 @@ impl Context {
/// Constructs an object with the `%Object.prototype%` prototype.
#[inline]
pub fn construct_object(&self) -> JsObject {
let object_prototype = self.standard_objects().object_object().prototype();
JsObject::from_proto_and_data(Some(object_prototype), ObjectData::ordinary())
JsObject::from_proto_and_data(
self.standard_objects().object_object().prototype(),
ObjectData::ordinary(),
)
}

/// <https://tc39.es/ecma262/#sec-call>
Expand Down Expand Up @@ -715,7 +717,7 @@ impl Context {
};

let function =
JsObject::from_proto_and_data(Some(function_prototype), ObjectData::function(func));
JsObject::from_proto_and_data(function_prototype, ObjectData::function(func));

// Set constructor field to the newly created Value (function object)
let constructor = PropertyDescriptor::builder()
Expand Down
4 changes: 2 additions & 2 deletions boa/src/object/jsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ impl JsObject {
/// Create a `JsObject` and automatically set its internal methods and
/// internal slots from the `data` provided.
#[inline]
pub fn from_proto_and_data(prototype: Option<JsObject>, data: ObjectData) -> Self {
pub fn from_proto_and_data<O: Into<Option<JsObject>>>(prototype: O, data: ObjectData) -> Self {
Self::from_object(Object {
data,
prototype: prototype.map_or(JsValue::Null, JsValue::new),
prototype: prototype.into().map_or(JsValue::Null, JsValue::new),
extensible: true,
properties: Default::default(),
})
Expand Down
10 changes: 4 additions & 6 deletions boa/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,10 @@ impl<'context> FunctionBuilder<'context> {
#[inline]
pub fn build(&mut self) -> JsObject {
let function = JsObject::from_proto_and_data(
Some(
self.context
.standard_objects()
.function_object()
.prototype(),
),
self.context
.standard_objects()
.function_object()
.prototype(),
ObjectData::function(self.function.take().unwrap()),
);
let property = PropertyDescriptor::builder()
Expand Down
Loading

0 comments on commit b555c0b

Please sign in to comment.