diff --git a/core/engine/src/builtins/array_buffer/mod.rs b/core/engine/src/builtins/array_buffer/mod.rs index 5b8c5a1bfa5..7e9e8c4e75c 100644 --- a/core/engine/src/builtins/array_buffer/mod.rs +++ b/core/engine/src/builtins/array_buffer/mod.rs @@ -496,19 +496,12 @@ impl ArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("get ArrayBuffer.prototype.byteLength called with invalid `this`") - })?; + let buf = require_internal_slot!(this, Self, "ArrayBuffer"); // 4. If IsDetachedBuffer(O) is true, return +0𝔽. // 5. Let length be O.[[ArrayBufferByteLength]]. // 6. Return 𝔽(length). - Ok(buf.len().into()) + Ok(buf.borrow().data().len().into()) } /// [`get ArrayBuffer.prototype.maxByteLength`][spec]. @@ -522,18 +515,12 @@ impl ArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message( - "get ArrayBuffer.prototype.maxByteLength called with invalid `this`", - ) - })?; + let buf = require_internal_slot!(this, Self, "ArrayBuffer"); // 4. If IsDetachedBuffer(O) is true, return +0𝔽. - let Some(data) = buf.bytes() else { + let buf_data = buf.borrow(); + let buf_data = buf_data.data(); + let Some(data) = buf_data.bytes() else { return Ok(JsValue::from(0)); }; @@ -542,7 +529,7 @@ impl ArrayBuffer { // 6. Else, // a. Let length be O.[[ArrayBufferMaxByteLength]]. // 7. Return 𝔽(length). - Ok(buf.max_byte_len.unwrap_or(data.len() as u64).into()) + Ok(buf_data.max_byte_len.unwrap_or(data.len() as u64).into()) } /// [`get ArrayBuffer.prototype.resizable`][spec]. @@ -556,17 +543,10 @@ impl ArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("get ArrayBuffer.prototype.resizable called with invalid `this`") - })?; + let buf = require_internal_slot!(this, Self, "ArrayBuffer"); // 4. If IsFixedLengthArrayBuffer(O) is false, return true; otherwise return false. - Ok(JsValue::from(!buf.is_fixed_len())) + Ok(JsValue::from(!buf.borrow().data().is_fixed_len())) } /// [`get ArrayBuffer.prototype.detached`][spec]. @@ -581,17 +561,10 @@ impl ArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("get ArrayBuffer.prototype.detached called with invalid `this`") - })?; + let buf = require_internal_slot!(this, Self, "ArrayBuffer"); // 4. Return IsDetachedBuffer(O). - Ok(buf.is_detached().into()) + Ok(buf.borrow().data().is_detached().into()) } /// [`ArrayBuffer.prototype.resize ( newLength )`][spec]. diff --git a/core/engine/src/builtins/array_buffer/shared.rs b/core/engine/src/builtins/array_buffer/shared.rs index 5c4b30b8482..dd6e1d84e71 100644 --- a/core/engine/src/builtins/array_buffer/shared.rs +++ b/core/engine/src/builtins/array_buffer/shared.rs @@ -225,17 +225,10 @@ impl SharedArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("SharedArrayBuffer.byteLength called with invalid value") - })?; + let buf = require_internal_slot!(this, Self, "SharedArrayBuffer"); // 4. Let length be ArrayBufferByteLength(O, seq-cst). - let len = buf.bytes(Ordering::SeqCst).len() as u64; + let len = buf.borrow().data().bytes(Ordering::SeqCst).len() as u64; // 5. Return 𝔽(length). Ok(len.into()) @@ -252,17 +245,10 @@ impl SharedArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("get SharedArrayBuffer.growable called with invalid `this`") - })?; + let buf = require_internal_slot!(this, Self, "SharedArrayBuffer"); // 4. If IsFixedLengthArrayBuffer(O) is false, return true; otherwise return false. - Ok(JsValue::from(!buf.is_fixed_len())) + Ok(JsValue::from(!buf.borrow().data().is_fixed_len())) } /// [`get SharedArrayBuffer.prototype.maxByteLength`][spec]. @@ -276,21 +262,14 @@ impl SharedArrayBuffer { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception. - let object = this.as_object(); - let buf = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("get SharedArrayBuffer.maxByteLength called with invalid value") - })?; + let buf = require_internal_slot!(this, Self, "SharedArrayBuffer"); // 4. If IsFixedLengthArrayBuffer(O) is true, then // a. Let length be O.[[ArrayBufferByteLength]]. // 5. Else, // a. Let length be O.[[ArrayBufferMaxByteLength]]. // 6. Return 𝔽(length). - Ok(buf.data.buffer.len().into()) + Ok(buf.borrow().data().data.buffer.len().into()) } /// [`SharedArrayBuffer.prototype.grow ( newLength )`][spec]. diff --git a/core/engine/src/builtins/dataview/mod.rs b/core/engine/src/builtins/dataview/mod.rs index f5b80942c48..87b8628bbf5 100644 --- a/core/engine/src/builtins/dataview/mod.rs +++ b/core/engine/src/builtins/dataview/mod.rs @@ -331,14 +331,11 @@ impl DataView { ) -> JsResult { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[DataView]]). - let object = this.as_object(); - let view = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?; + let view = require_internal_slot!(this, Self, "DataView"); // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. // 4. Let buffer be O.[[ViewedArrayBuffer]]. - let buffer = view.viewed_array_buffer.clone(); + let view_data = view.borrow(); + let buffer = view_data.data().viewed_array_buffer.clone(); // 5. Return buffer. Ok(buffer.into()) } @@ -361,18 +358,15 @@ impl DataView { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[DataView]]). // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. - let object = this.as_object(); - let view = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?; + let view = require_internal_slot!(this, Self, "DataView"); // 4. Let viewRecord be MakeDataViewWithBufferWitnessRecord(O, seq-cst). // 5. If IsViewOutOfBounds(viewRecord) is true, throw a TypeError exception. - let buffer = view.viewed_array_buffer.as_buffer(); + let view_data = view.borrow(); + let buffer = view_data.data().viewed_array_buffer.as_buffer(); let Some(slice) = buffer .bytes(Ordering::SeqCst) - .filter(|s| !view.is_out_of_bounds(s.len())) + .filter(|s| !view.borrow().data().is_out_of_bounds(s.len())) else { return Err(JsNativeError::typ() .with_message("view out of bounds for its inner buffer") @@ -380,7 +374,8 @@ impl DataView { }; // 6. Let size be GetViewByteLength(viewRecord). - let size = view.byte_length(slice.len()); + let view_data = view.borrow(); + let size = view_data.data().byte_length(slice.len()); // 7. Return 𝔽(size). Ok(size.into()) @@ -404,19 +399,16 @@ impl DataView { ) -> JsResult { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[DataView]]). - let object = this.as_object(); - let view = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?; + let view = require_internal_slot!(this, Self, "DataView"); // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. - let buffer = view.viewed_array_buffer.as_buffer(); + let view_data = view.borrow(); + let buffer = view_data.data().viewed_array_buffer.as_buffer(); // 4. Let viewRecord be MakeDataViewWithBufferWitnessRecord(O, seq-cst). // 5. If IsViewOutOfBounds(viewRecord) is true, throw a TypeError exception. if buffer .bytes(Ordering::SeqCst) - .filter(|b| !view.is_out_of_bounds(b.len())) + .filter(|b| !view.borrow().data().is_out_of_bounds(b.len())) .is_none() { return Err(JsNativeError::typ() @@ -425,7 +417,8 @@ impl DataView { } // 6. Let offset be O.[[ByteOffset]]. - let offset = view.byte_offset; + let view_data = view.borrow(); + let offset = view_data.data().byte_offset; // 7. Return 𝔽(offset). Ok(offset.into()) } @@ -448,11 +441,7 @@ impl DataView { ) -> JsResult { // 1. Perform ? RequireInternalSlot(view, [[DataView]]). // 2. Assert: view has a [[ViewedArrayBuffer]] internal slot. - let object = view.as_object(); - let view = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?; + let view = require_internal_slot!(view, Self, "DataView"); // 3. Let getIndex be ? ToIndex(requestIndex). let get_index = request_index.to_index(context)?; @@ -463,10 +452,11 @@ impl DataView { // 6. Let viewRecord be MakeDataViewWithBufferWitnessRecord(view, unordered). // 7. NOTE: Bounds checking is not a synchronizing operation when view's backing buffer is a growable SharedArrayBuffer. // 8. If IsViewOutOfBounds(viewRecord) is true, throw a TypeError exception. - let buffer = view.viewed_array_buffer.as_buffer(); + let view_data = view.borrow(); + let buffer = view_data.data().viewed_array_buffer.as_buffer(); let Some(data) = buffer .bytes(Ordering::Relaxed) - .filter(|buf| !view.is_out_of_bounds(buf.len())) + .filter(|buf| !view.borrow().data().is_out_of_bounds(buf.len())) else { return Err(JsNativeError::typ() .with_message("view out of bounds for its inner buffer") @@ -474,10 +464,12 @@ impl DataView { }; // 5. Let viewOffset be view.[[ByteOffset]]. - let view_offset = view.byte_offset; + let view_data = view.borrow(); + let view_offset = view_data.data().byte_offset; // 9. Let viewSize be GetViewByteLength(viewRecord). - let view_size = view.byte_length(data.len()); + let view_data = view.borrow(); + let view_size = view_data.data().byte_length(data.len()); // 10. Let elementSize be the Element Size value specified in Table 71 for Element Type type. let element_size = size_of::() as u64; @@ -790,11 +782,7 @@ impl DataView { ) -> JsResult { // 1. Perform ? RequireInternalSlot(view, [[DataView]]). // 2. Assert: view has a [[ViewedArrayBuffer]] internal slot. - let object = view.as_object(); - let view = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?; + let view = require_internal_slot!(view, Self, "DataView"); // 3. Let getIndex be ? ToIndex(requestIndex). let get_index = request_index.to_index(context)?; @@ -809,11 +797,15 @@ impl DataView { // 8. Let viewRecord be MakeDataViewWithBufferWitnessRecord(view, unordered). // 9. NOTE: Bounds checking is not a synchronizing operation when view's backing buffer is a growable SharedArrayBuffer. // 10. If IsViewOutOfBounds(viewRecord) is true, throw a TypeError exception. - let mut buffer = view.viewed_array_buffer.as_buffer_mut(); + let viewed_buffer = { + let view_data = view.borrow(); + view_data.data().viewed_array_buffer.clone() + }; + let mut buffer = viewed_buffer.as_buffer_mut(); let Some(mut data) = buffer .bytes(Ordering::Relaxed) - .filter(|buf| !view.is_out_of_bounds(buf.len())) + .filter(|buf| !view.borrow().data().is_out_of_bounds(buf.len())) else { return Err(JsNativeError::typ() .with_message("view out of bounds for its inner buffer") @@ -821,10 +813,12 @@ impl DataView { }; // 11. Let viewSize be GetViewByteLength(viewRecord). - let view_size = view.byte_length(data.len()); + let view_data = view.borrow(); + let view_size = view_data.data().byte_length(data.len()); // 7. Let viewOffset be view.[[ByteOffset]]. - let view_offset = view.byte_offset; + let view_data = view.borrow(); + let view_offset = view_data.data().byte_offset; // 12. Let elementSize be the Element Size value specified in Table 71 for Element Type type. let elem_size = size_of::(); diff --git a/core/engine/src/builtins/date/mod.rs b/core/engine/src/builtins/date/mod.rs index 0c82b5b570f..cbba5d64c1d 100644 --- a/core/engine/src/builtins/date/mod.rs +++ b/core/engine/src/builtins/date/mod.rs @@ -446,11 +446,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -485,11 +482,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -527,11 +521,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -559,11 +550,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -597,11 +585,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -635,11 +620,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -673,11 +655,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -712,11 +691,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -750,11 +726,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -791,12 +764,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Return dateObject.[[DateValue]]. - Ok(this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0 - .into()) + let date = require_internal_slot!(this, Date, "Date"); + Ok(date.borrow().data().0.into()) } /// `Date.prototype.getTimeZoneOffset()`. @@ -818,11 +787,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If t is NaN, return NaN. if t.is_nan() { @@ -850,20 +816,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let mut t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let mut t = date_obj.borrow().data().0; // 4. Let dt be ? ToNumber(date). let dt = args.get_or_undefined(0).to_number(context)?; @@ -892,14 +848,8 @@ impl Date { time_clip(new_date) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 9. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 10. Return u. Ok(JsValue::from(u)) @@ -920,20 +870,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let t = date_obj.borrow().data().0; let t = if LOCAL { // 5. If t is NaN, set t to +0𝔽; otherwise, set t to LocalTime(t). @@ -975,14 +915,8 @@ impl Date { time_clip(new_date) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 10. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 11. Return u. Ok(JsValue::from(u)) @@ -1005,20 +939,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let mut t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let mut t = date_obj.borrow().data().0; // 4. Let h be ? ToNumber(hour). let h = args.get_or_undefined(0).to_number(context)?; @@ -1062,14 +986,8 @@ impl Date { time_clip(date) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 15. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 16. Return u. Ok(JsValue::from(u)) @@ -1089,20 +1007,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let mut t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let mut t = date_obj.borrow().data().0; // 4. Set ms to ? ToNumber(ms). let ms = args.get_or_undefined(0).to_number(context)?; @@ -1136,14 +1044,8 @@ impl Date { time_clip(make_date(day(t), time)) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 9. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 10. Return u. Ok(JsValue::from(u)) @@ -1163,20 +1065,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let mut t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let mut t = date_obj.borrow().data().0; // 4. Let m be ? ToNumber(min). let m = args.get_or_undefined(0).to_number(context)?; @@ -1214,14 +1106,8 @@ impl Date { time_clip(date) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 13. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 14. Return u. Ok(JsValue::from(u)) @@ -1242,20 +1128,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let mut t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let mut t = date_obj.borrow().data().0; // 4. Let m be ? ToNumber(month). let m = args.get_or_undefined(0).to_number(context)?; @@ -1290,14 +1166,8 @@ impl Date { time_clip(new_date) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 11. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 12. Return u. Ok(JsValue::from(u)) @@ -1317,20 +1187,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let mut t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let mut t = date_obj.borrow().data().0; // 4. Let s be ? ToNumber(sec). let s = args.get_or_undefined(0).to_number(context)?; @@ -1365,14 +1225,8 @@ impl Date { time_clip(date) }; - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 11. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 12. Return u. Ok(JsValue::from(u)) @@ -1399,20 +1253,10 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be dateObject.[[DateValue]]. - let t = date.0; - - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); + let t = date_obj.borrow().data().0; // 4. Let y be ? ToNumber(year). let y = args.get_or_undefined(0).to_number(context)?; @@ -1436,14 +1280,8 @@ impl Date { // 9. Let u be TimeClip(UTC(date)). let u = time_clip(utc_t(date, context.host_hooks().as_ref())); - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 10. Set dateObject.[[DateValue]] to u. - date_mut.0 = u; + date_obj.borrow_mut().data_mut().0 = u; // 11. Return u. Ok(JsValue::from(u)) @@ -1466,32 +1304,16 @@ impl Date { ) -> JsResult { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; + let date_obj = require_internal_slot!(this, Date, "Date"); // 3. Let t be ? ToNumber(time). let t = args.get_or_undefined(0).to_number(context)?; - // NOTE (nekevss): `downcast_ref` is used and then dropped for a short lived borrow. - // ToNumber() may call userland code which can modify the underlying date - // which will cause a panic. In order to avoid this, we drop the borrow, - // here and only `downcast_mut` when date will be modified. - drop(date); - // 4. Let v be TimeClip(t). let v = time_clip(t); - let object = this.as_object(); - let mut date_mut = object - .as_ref() - .and_then(JsObject::downcast_mut::) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?; - // 5. Set dateObject.[[DateValue]] to v. - date_mut.0 = v; + date_obj.borrow_mut().data_mut().0 = v; // 6. Return v. Ok(JsValue::from(v)) @@ -1514,11 +1336,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let tv be dateObject.[[DateValue]]. - let tv = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let tv = date.borrow().data().0; // 4. If tv is NaN, return "Invalid Date". if tv.is_nan() { @@ -1551,11 +1370,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let tv be dateObject.[[DateValue]]. - let tv = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let tv = date.borrow().data().0; // 4. If tv is not finite, throw a RangeError exception. if !tv.is_finite() { @@ -1663,11 +1479,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let x be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If x is NaN, return "Invalid Date". if t.is_nan() { return Ok(JsValue::new(js_string!("Invalid Date"))); @@ -1717,11 +1530,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let x be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If x is NaN, return "Invalid Date". if t.is_nan() { return Ok(JsValue::new(js_string!("Invalid Date"))); @@ -1772,11 +1582,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let x be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. If x is NaN, return "Invalid Date". if t.is_nan() { return Ok(JsValue::new(js_string!("Invalid Date"))); @@ -1817,11 +1624,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let tv be dateObject.[[DateValue]]. - let tv = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let tv = date.borrow().data().0; // 4. Return ToDateString(tv). Ok(JsValue::from(to_date_string_t( @@ -1848,11 +1652,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let tv be dateObject.[[DateValue]]. - let tv = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let tv = date.borrow().data().0; // 4. If tv is NaN, return "Invalid Date". if tv.is_nan() { @@ -1886,11 +1687,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let tv be dateObject.[[DateValue]]. - let tv = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let tv = date.borrow().data().0; // 4. If tv is NaN, return "Invalid Date". if tv.is_nan() { @@ -1990,12 +1788,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Return dateObject.[[DateValue]]. - Ok(this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))? - .0 - .into()) + let date = require_internal_slot!(this, Date, "Date"); + Ok(date.borrow().data().0.into()) } /// [`Date.prototype [ @@toPrimitive ] ( hint )`][spec]. @@ -2063,11 +1857,8 @@ impl Date { // 1. Let dateObject be the this value. // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]). // 3. Let t be dateObject.[[DateValue]]. - let t = this - .as_object() - .and_then(|obj| obj.downcast_ref::().as_deref().copied()) - .ok_or_else(|| js_error!(TypeError: "'this' is not a Date"))? - .0; + let date = require_internal_slot!(this, Date, "Date"); + let t = date.borrow().data().0; // 4. Let ns be ? NumberToBigInt(t) × ℤ(10**6). let ns = i128::from_f64(t) diff --git a/core/engine/src/builtins/date/tests.rs b/core/engine/src/builtins/date/tests.rs index a6b6aed6d2e..b6abf788253 100644 --- a/core/engine/src/builtins/date/tests.rs +++ b/core/engine/src/builtins/date/tests.rs @@ -103,7 +103,7 @@ fn date_this_time_value() { run_test_actions([TestAction::assert_native_error( "({toString: Date.prototype.toString}).toString()", JsNativeErrorKind::Type, - "'this' is not a Date", + "the this object must be a Date object.", )]); } @@ -952,17 +952,17 @@ fn date_proto_to_locale_string_intl() { TestAction::assert_native_error( "Date.prototype.toLocaleString.call({})", JsNativeErrorKind::Type, - "'this' is not a Date", + "the this object must be a Date object.", ), TestAction::assert_native_error( "Date.prototype.toLocaleDateString.call({})", JsNativeErrorKind::Type, - "'this' is not a Date", + "the this object must be a Date object.", ), TestAction::assert_native_error( "Date.prototype.toLocaleTimeString.call({})", JsNativeErrorKind::Type, - "'this' is not a Date", + "the this object must be a Date object.", ), TestAction::assert_eq("new Date(NaN).toLocaleString()", js_str!("Invalid Date")), TestAction::assert("typeof new Date(2020, 6, 8).toLocaleString() === 'string'"), diff --git a/core/engine/src/builtins/intl/collator/mod.rs b/core/engine/src/builtins/intl/collator/mod.rs index 00eb1eb84ea..aa01cd777b1 100644 --- a/core/engine/src/builtins/intl/collator/mod.rs +++ b/core/engine/src/builtins/intl/collator/mod.rs @@ -413,14 +413,7 @@ impl Collator { fn resolved_options(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { // 1. Let collator be the this value. // 2. Perform ? RequireInternalSlot(collator, [[InitializedCollator]]). - let object = this.as_object(); - let collator = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("`resolvedOptions` can only be called on a `Collator` object") - })?; + let collator = require_internal_slot!(this, Self, "Collator"); // 3. Let options be OrdinaryObjectCreate(%Object.prototype%). let options = context @@ -442,14 +435,14 @@ impl Collator { options .create_data_property_or_throw( js_string!("locale"), - js_string!(collator.locale.to_string()), + js_string!(collator.borrow().data().locale.to_string()), context, ) .js_expect("operation must not fail per the spec")?; options .create_data_property_or_throw( js_string!("usage"), - match collator.usage { + match collator.borrow().data().usage { Usage::Search => js_string!("search"), Usage::Sort => js_string!("sort"), }, @@ -459,7 +452,7 @@ impl Collator { options .create_data_property_or_throw( js_string!("sensitivity"), - match collator.sensitivity { + match collator.borrow().data().sensitivity { Sensitivity::Base => js_string!("base"), Sensitivity::Accent => js_string!("accent"), Sensitivity::Case => js_string!("case"), @@ -471,7 +464,7 @@ impl Collator { options .create_data_property_or_throw( js_string!("ignorePunctuation"), - collator.ignore_punctuation, + collator.borrow().data().ignore_punctuation, context, ) .js_expect("operation must not fail per the spec")?; @@ -479,16 +472,22 @@ impl Collator { .create_data_property_or_throw( js_string!("collation"), collator + .borrow() + .data() .collation - .map(|co| js_string!(co.as_str())) + .map(|co: CollationType| js_string!(co.as_str())) .unwrap_or(js_string!("default")), context, ) .js_expect("operation must not fail per the spec")?; options - .create_data_property_or_throw(js_string!("numeric"), collator.numeric, context) + .create_data_property_or_throw( + js_string!("numeric"), + collator.borrow().data().numeric, + context, + ) .js_expect("operation must not fail per the spec")?; - if let Some(kf) = collator.case_first { + if let Some(kf) = collator.borrow().data().case_first { options .create_data_property_or_throw( js_string!("caseFirst"), diff --git a/core/engine/src/builtins/intl/segmenter/mod.rs b/core/engine/src/builtins/intl/segmenter/mod.rs index 459a385f6ed..0374a06e556 100644 --- a/core/engine/src/builtins/intl/segmenter/mod.rs +++ b/core/engine/src/builtins/intl/segmenter/mod.rs @@ -262,15 +262,7 @@ impl Segmenter { fn resolved_options(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { // 1. Let segmenter be the this value. // 2. Perform ? RequireInternalSlot(segmenter, [[InitializedSegmenter]]). - let object = this.as_object(); - let segmenter = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message( - "`resolved_options` can only be called on an `Intl.Segmenter` object", - ) - })?; + let segmenter = require_internal_slot!(this, Self, "Segmenter"); // 3. Let options be OrdinaryObjectCreate(%Object.prototype%). // 4. For each row of Table 19, except the header row, in table order, do @@ -281,12 +273,12 @@ impl Segmenter { let options = ObjectInitializer::new(context) .property( js_string!("locale"), - js_string!(segmenter.locale.to_string()), + js_string!(segmenter.borrow().data().locale.to_string()), Attribute::all(), ) .property( js_string!("granularity"), - js_string!(segmenter.native.granularity().to_string()), + js_string!(segmenter.borrow().data().native.granularity().to_string()), Attribute::all(), ) .build(); diff --git a/core/engine/src/builtins/intl/segmenter/segments.rs b/core/engine/src/builtins/intl/segmenter/segments.rs index f489dce1247..d42dd288621 100644 --- a/core/engine/src/builtins/intl/segmenter/segments.rs +++ b/core/engine/src/builtins/intl/segmenter/segments.rs @@ -55,24 +55,22 @@ impl Segments { fn containing(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let segments be the this value. // 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]). - let object = this.as_object(); - let segments = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("`containing` can only be called on a `Segments` object") - })?; + let segments = require_internal_slot!(this, Self, "Segments"); // 3. Let segmenter be segments.[[SegmentsSegmenter]]. - let segmenter = segments - .segmenter + // 4. Let string be segments.[[SegmentsString]]. + let (segmenter_obj, string) = { + let seg_data = segments.borrow(); + let seg_data = seg_data.data(); + (seg_data.segmenter.clone(), seg_data.string.clone()) + }; + + let segmenter = segmenter_obj .downcast_ref::() .js_expect("segments object should contain a segmenter")?; - // 4. Let string be segments.[[SegmentsString]]. // 5. Let len be the length of string. - let len = segments.string.len() as i64; + let len = string.len() as i64; // 6. Let n be ? ToIntegerOrInfinity(index). let Some(n) = args @@ -89,8 +87,8 @@ impl Segments { // 8. Let startIndex be ! FindBoundary(segmenter, string, n, before). // 9. Let endIndex be ! FindBoundary(segmenter, string, n, after). let (range, is_word_like) = { - let mut segments = segmenter.native.segment(segments.string.variant()); - std::iter::from_fn(|| segments.next().map(|i| (i, segments.is_word_like()))) + let mut seg_iter = segmenter.native.segment(string.variant()); + std::iter::from_fn(|| seg_iter.next().map(|i| (i, seg_iter.is_word_like()))) .tuple_windows() .find(|((i, _), (j, _))| (*i..*j).contains(&n)) .map(|((i, _), (j, word))| ((i..j), word)) @@ -98,10 +96,7 @@ impl Segments { }; // 10. Return ! CreateSegmentDataObject(segmenter, string, startIndex, endIndex). - Ok( - create_segment_data_object(segments.string.clone(), range, is_word_like, context) - .into(), - ) + Ok(create_segment_data_object(string, range, is_word_like, context).into()) } /// [`%SegmentsPrototype% [ @@iterator ] ( )`][spec] diff --git a/core/engine/src/builtins/mod.rs b/core/engine/src/builtins/mod.rs index f17088d9776..e722878ca4c 100644 --- a/core/engine/src/builtins/mod.rs +++ b/core/engine/src/builtins/mod.rs @@ -1,5 +1,27 @@ //! Boa's ECMAScript built-in object implementations, e.g. Object, String, Math, Array, etc. +/// Downcasts `$this` (a `&JsValue`) to a `JsObject<$type>`, returning a +/// `TypeError` if the cast fails. The error message uses `$name` as the +/// type name. +/// +/// This centralizes the repeated this-type-check boilerplate found across +/// all builtin methods. Unlike `downcast_ref`, the returned `JsObject` +/// does **not** hold an active borrow, so callers can `.borrow()` / +/// `.borrow_mut()` only when needed, avoiding panics from long-lived +/// `GcRefCell` borrows. +/// +/// Usage: `let dt = require_internal_slot!(this, Self, "PlainDateTime");` +macro_rules! require_internal_slot { + ($this:expr, $type:ty, $name:expr) => { + $crate::JsValue::as_object($this) + .map(|o| o.clone()) + .and_then(|o| $crate::JsObject::downcast::<$type>(o).ok()) + .ok_or_else(|| { + $crate::js_error!(TypeError: "the this object must be a {} object.", $name) + })? + }; +} + pub mod array; pub mod array_buffer; pub mod async_function; diff --git a/core/engine/src/builtins/temporal/duration/mod.rs b/core/engine/src/builtins/temporal/duration/mod.rs index fad6b6515ba..1d97c401c37 100644 --- a/core/engine/src/builtins/temporal/duration/mod.rs +++ b/core/engine/src/builtins/temporal/duration/mod.rs @@ -331,15 +331,10 @@ impl BuiltInConstructor for Duration { impl Duration { // Internal utility function for getting `Duration` field values. fn get_internal_field(this: &JsValue, field: &DateTimeValues) -> JsResult { - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); - let inner = &duration.inner; + let duration_data = duration.borrow(); + let inner = &duration_data.data().inner; match field { DateTimeValues::Year => Ok(JsValue::new(inner.years())), @@ -522,18 +517,12 @@ impl Duration { fn get_sign(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); // 3. Return 𝔽(! DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], // duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], // duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]])). - Ok((duration.inner.sign() as i8).into()) + Ok((duration.borrow().data().inner.sign() as i8).into()) } /// 7.3.14 get `Temporal.Duration.prototype.blank` @@ -550,20 +539,14 @@ impl Duration { fn get_blank(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); // 3. Let sign be ! DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], // duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], // duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]). // 4. If sign = 0, return true. // 5. Return false. - Ok(duration.inner.is_zero().into()) + Ok(duration.borrow().data().inner.is_zero().into()) } } @@ -642,13 +625,7 @@ impl Duration { ) -> JsResult { // 1. Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); // 3. Let temporalDurationLike be ? ToTemporalPartialDurationRecord(temporalDurationLike). let temporal_duration_like = @@ -660,7 +637,7 @@ impl Duration { // a. Let years be duration.[[Years]]. let years = temporal_duration_like .years - .unwrap_or(duration.inner.years()); + .unwrap_or(duration.borrow().data().inner.years()); // 6. If temporalDurationLike.[[Months]] is not undefined, then // a. Let months be temporalDurationLike.[[Months]]. @@ -668,7 +645,7 @@ impl Duration { // a. Let months be duration.[[Months]]. let months = temporal_duration_like .months - .unwrap_or(duration.inner.months()); + .unwrap_or(duration.borrow().data().inner.months()); // 8. If temporalDurationLike.[[Weeks]] is not undefined, then // a. Let weeks be temporalDurationLike.[[Weeks]]. @@ -676,13 +653,15 @@ impl Duration { // a. Let weeks be duration.[[Weeks]]. let weeks = temporal_duration_like .weeks - .unwrap_or(duration.inner.weeks()); + .unwrap_or(duration.borrow().data().inner.weeks()); // 10. If temporalDurationLike.[[Days]] is not undefined, then // a. Let days be temporalDurationLike.[[Days]]. // 11. Else, // a. Let days be duration.[[Days]]. - let days = temporal_duration_like.days.unwrap_or(duration.inner.days()); + let days = temporal_duration_like + .days + .unwrap_or(duration.borrow().data().inner.days()); // 12. If temporalDurationLike.[[Hours]] is not undefined, then // a. Let hours be temporalDurationLike.[[Hours]]. @@ -690,7 +669,7 @@ impl Duration { // a. Let hours be duration.[[Hours]]. let hours = temporal_duration_like .hours - .unwrap_or(duration.inner.hours()); + .unwrap_or(duration.borrow().data().inner.hours()); // 14. If temporalDurationLike.[[Minutes]] is not undefined, then // a. Let minutes be temporalDurationLike.[[Minutes]]. @@ -698,7 +677,7 @@ impl Duration { // a. Let minutes be duration.[[Minutes]]. let minutes = temporal_duration_like .minutes - .unwrap_or(duration.inner.minutes()); + .unwrap_or(duration.borrow().data().inner.minutes()); // 16. If temporalDurationLike.[[Seconds]] is not undefined, then // a. Let seconds be temporalDurationLike.[[Seconds]]. @@ -706,7 +685,7 @@ impl Duration { // a. Let seconds be duration.[[Seconds]]. let seconds = temporal_duration_like .seconds - .unwrap_or(duration.inner.seconds()); + .unwrap_or(duration.borrow().data().inner.seconds()); // 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then // a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. @@ -714,7 +693,7 @@ impl Duration { // a. Let milliseconds be duration.[[Milliseconds]]. let milliseconds = temporal_duration_like .milliseconds - .unwrap_or(duration.inner.milliseconds()); + .unwrap_or(duration.borrow().data().inner.milliseconds()); // 20. If temporalDurationLike.[[Microseconds]] is not undefined, then // a. Let microseconds be temporalDurationLike.[[Microseconds]]. @@ -722,7 +701,7 @@ impl Duration { // a. Let microseconds be duration.[[Microseconds]]. let microseconds = temporal_duration_like .microseconds - .unwrap_or(duration.inner.microseconds()); + .unwrap_or(duration.borrow().data().inner.microseconds()); // 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then // a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. @@ -730,7 +709,7 @@ impl Duration { // a. Let nanoseconds be duration.[[Nanoseconds]]. let nanoseconds = temporal_duration_like .nanoseconds - .unwrap_or(duration.inner.nanoseconds()); + .unwrap_or(duration.borrow().data().inner.nanoseconds()); // 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). let new_duration = InnerDuration::new( @@ -767,15 +746,10 @@ impl Duration { // 1. Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). // 3. Return ! CreateNegatedTemporalDuration(duration). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; - - create_temporal_duration(duration.inner.negated(), None, context).map(Into::into) + let duration = require_internal_slot!(this, Self, "Duration"); + + create_temporal_duration(duration.borrow().data().inner.negated(), None, context) + .map(Into::into) } /// 7.3.17 `Temporal.Duration.prototype.abs ( )` @@ -795,15 +769,10 @@ impl Duration { // 3. Return ! CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), // abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), // abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; - - create_temporal_duration(duration.inner.abs(), None, context).map(Into::into) + let duration = require_internal_slot!(this, Self, "Duration"); + + create_temporal_duration(duration.borrow().data().inner.abs(), None, context) + .map(Into::into) } /// 7.3.18 `Temporal.Duration.prototype.add ( other [ , options ] )` @@ -824,18 +793,13 @@ impl Duration { ) -> JsResult { // 1.Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); // 3. Return ? AddDurations(add, duration, other). let other = to_temporal_duration_record(args.get_or_undefined(0), context)?; - create_temporal_duration(duration.inner.add(&other)?, None, context).map(Into::into) + create_temporal_duration(duration.borrow().data().inner.add(&other)?, None, context) + .map(Into::into) } /// 7.3.19 `Temporal.Duration.prototype.subtract ( other [ , options ] )` @@ -856,18 +820,17 @@ impl Duration { ) -> JsResult { // 1.Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); let other = to_temporal_duration_record(args.get_or_undefined(0), context)?; // 3. Return ? AddDurations(add, duration, other). - create_temporal_duration(duration.inner.subtract(&other)?, None, context).map(Into::into) + create_temporal_duration( + duration.borrow().data().inner.subtract(&other)?, + None, + context, + ) + .map(Into::into) } /// 7.3.20 `Temporal.Duration.prototype.round ( roundTo )` @@ -888,13 +851,7 @@ impl Duration { ) -> JsResult { // 1. Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); let round_to = match args.first().map(JsValue::variant) { // 3. If roundTo is undefined, then @@ -965,7 +922,7 @@ impl Duration { // NOTE: execute step 21 earlier before initial values are shadowed. // 21. If smallestUnitPresent is false and largestUnitPresent is false, then - let rounded_duration = duration.inner.round_with_provider( + let rounded_duration = duration.borrow().data().inner.round_with_provider( options, relative_to, context.timezone_provider(), @@ -991,13 +948,7 @@ impl Duration { ) -> JsResult { // 1. Let duration be the this value. // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); let total_of = args.get_or_undefined(0); @@ -1045,6 +996,8 @@ impl Duration { .ok_or_else(|| JsNativeError::range().with_message("unit cannot be undefined."))?; Ok(duration + .borrow() + .data() .inner .total_with_provider(unit, relative_to, context.timezone_provider())? .as_inner() @@ -1067,13 +1020,7 @@ impl Duration { args: &[JsValue], context: &mut Context, ) -> JsResult { - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); let options = get_options_object(args.get_or_undefined(0))?; let precision = get_digits_option(&options, context)?; @@ -1081,11 +1028,16 @@ impl Duration { get_option::(&options, js_string!("roundingMode"), context)?; let smallest_unit = get_option::(&options, js_string!("smallestUnit"), context)?; - let result = duration.inner.as_temporal_string(ToStringRoundingOptions { - precision, - smallest_unit, - rounding_mode, - })?; + let result = + duration + .borrow() + .data() + .inner + .as_temporal_string(ToStringRoundingOptions { + precision, + smallest_unit, + rounding_mode, + })?; Ok(JsString::from(result).into()) } @@ -1100,15 +1052,11 @@ impl Duration { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tojson /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/toJSON pub(crate) fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); let result = duration + .borrow() + .data() .inner .as_temporal_string(ToStringRoundingOptions::default())?; @@ -1130,15 +1078,11 @@ impl Duration { _: &mut Context, ) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let duration = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a Duration object.") - })?; + let duration = require_internal_slot!(this, Self, "Duration"); let result = duration + .borrow() + .data() .inner .as_temporal_string(ToStringRoundingOptions::default())?; diff --git a/core/engine/src/builtins/temporal/instant/mod.rs b/core/engine/src/builtins/temporal/instant/mod.rs index 736fe88cc88..2db0185c811 100644 --- a/core/engine/src/builtins/temporal/instant/mod.rs +++ b/core/engine/src/builtins/temporal/instant/mod.rs @@ -286,17 +286,11 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Let ns be instant.[[Nanoseconds]]. // 4. Let ms be floor(ℝ(ns) / 10^6). // 5. Return 𝔽(ms). - Ok(instant.inner.epoch_milliseconds().into()) + Ok(instant.borrow().data().inner.epoch_milliseconds().into()) } /// 8.3.6 get Temporal.Instant.prototype.epochNanoseconds @@ -317,16 +311,10 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Let ns be instant.[[Nanoseconds]]. // 4. Return ns. - Ok(JsBigInt::from(instant.inner.epoch_nanoseconds().as_i128()).into()) + Ok(JsBigInt::from(instant.borrow().data().inner.epoch_nanoseconds().as_i128()).into()) } } @@ -351,18 +339,12 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Return ? AddDurationToOrSubtractDurationFromInstant(add, instant, temporalDurationLike). let temporal_duration_like = to_temporal_duration_record(args.get_or_undefined(0), context)?; - let result = instant.inner.add(&temporal_duration_like)?; + let result = instant.borrow().data().inner.add(&temporal_duration_like)?; create_temporal_instant(result, None, context) } @@ -384,18 +366,16 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Return ? AddDurationToOrSubtractDurationFromInstant(subtract, instant, temporalDurationLike). let temporal_duration_like = to_temporal_duration_record(args.get_or_undefined(0), context)?; - let result = instant.inner.subtract(&temporal_duration_like)?; + let result = instant + .borrow() + .data() + .inner + .subtract(&temporal_duration_like)?; create_temporal_instant(result, None, context) } @@ -417,13 +397,7 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Return ? DifferenceTemporalInstant(until, instant, other, options). let other = to_temporal_instant(args.get_or_undefined(0), context)?; @@ -431,7 +405,7 @@ impl Instant { // Fetch the necessary options. let settings = get_difference_settings(&get_options_object(args.get_or_undefined(1))?, context)?; - let result = instant.inner.until(&other, settings)?; + let result = instant.borrow().data().inner.until(&other, settings)?; create_temporal_duration(result, None, context).map(Into::into) } @@ -453,19 +427,13 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Return ? DifferenceTemporalInstant(since, instant, other, options). let other = to_temporal_instant(args.get_or_undefined(0), context)?; let settings = get_difference_settings(&get_options_object(args.get_or_undefined(1))?, context)?; - let result = instant.inner.since(&other, settings)?; + let result = instant.borrow().data().inner.since(&other, settings)?; create_temporal_duration(result, None, context).map(Into::into) } @@ -487,13 +455,7 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); let round_to = match args.first().map(JsValue::variant) { // 3. If roundTo is undefined, then @@ -563,7 +525,7 @@ impl Instant { // unreachable here functions as 15.a. // 16. Perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, true). // 17. Let roundedNs be RoundTemporalInstant(instant.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). - let result = instant.inner.round(options)?; + let result = instant.borrow().data().inner.round(options)?; // 18. Return ! CreateTemporalInstant(roundedNs). create_temporal_instant(result, None, context) @@ -589,19 +551,13 @@ impl Instant { // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). // 4. If instant.[[Nanoseconds]] ≠ other.[[Nanoseconds]], return false. // 5. Return true. - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be an instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Set other to ? ToTemporalInstant(other). let other = args.get_or_undefined(0); let other_instant = to_temporal_instant(other, context)?; - if *instant.inner != other_instant { + if *instant.borrow().data().inner != other_instant { return Ok(false.into()); } Ok(true.into()) @@ -619,14 +575,7 @@ impl Instant { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/toString /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Instant.html#method.to_ixdtf_string fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("the this object must be a Temporal.Instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); let options = get_options_object(args.get_or_undefined(0))?; @@ -646,11 +595,11 @@ impl Instant { rounding_mode, }; - let ixdtf = instant.inner.to_ixdtf_string_with_provider( - timezone, - options, - context.timezone_provider(), - )?; + let ixdtf = instant + .borrow() + .data() + .inner + .to_ixdtf_string_with_provider(timezone, options, context.timezone_provider())?; Ok(JsString::from(ixdtf).into()) } @@ -666,20 +615,17 @@ impl Instant { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/toLocaleString fn to_locale_string(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("the this object must be a Temporal.Instant object.") - })?; - - let ixdtf = instant.inner.to_ixdtf_string_with_provider( - None, - ToStringRoundingOptions::default(), - context.timezone_provider(), - )?; + let instant = require_internal_slot!(this, Self, "Instant"); + + let ixdtf = instant + .borrow() + .data() + .inner + .to_ixdtf_string_with_provider( + None, + ToStringRoundingOptions::default(), + context.timezone_provider(), + )?; Ok(JsString::from(ixdtf).into()) } @@ -693,20 +639,17 @@ impl Instant { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.instant.tojson /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/toJSON fn to_json(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("the this object must be a Temporal.Instant object.") - })?; - - let ixdtf = instant.inner.to_ixdtf_string_with_provider( - None, - ToStringRoundingOptions::default(), - context.timezone_provider(), - )?; + let instant = require_internal_slot!(this, Self, "Instant"); + + let ixdtf = instant + .borrow() + .data() + .inner + .to_ixdtf_string_with_provider( + None, + ToStringRoundingOptions::default(), + context.timezone_provider(), + )?; Ok(JsString::from(ixdtf).into()) } @@ -743,20 +686,15 @@ impl Instant { ) -> JsResult { // 1. Let instant be the this value. // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). - let object = this.as_object(); - let instant = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ() - .with_message("the this object must be a Temporal.Instant object.") - })?; + let instant = require_internal_slot!(this, Self, "Instant"); // 3. Set timeZone to ? ToTemporalTimeZoneIdentifier(timeZone). let timezone = to_temporal_timezone_identifier(args.get_or_undefined(0), context)?; // 4. Return ! CreateTemporalZonedDateTime(instant.[[EpochNanoseconds]], timeZone, "iso8601"). let zdt = instant + .borrow() + .data() .inner .to_zoned_date_time_iso_with_provider(timezone, context.timezone_provider())?; create_temporal_zoneddatetime(zdt, None, context).map(Into::into) diff --git a/core/engine/src/builtins/temporal/plain_date/mod.rs b/core/engine/src/builtins/temporal/plain_date/mod.rs index e3a220ca78e..6eacb2f4b2b 100644 --- a/core/engine/src/builtins/temporal/plain_date/mod.rs +++ b/core/engine/src/builtins/temporal/plain_date/mod.rs @@ -331,15 +331,9 @@ impl PlainDate { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate/calendarId /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDate.html#method.calendar fn get_calendar_id(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; - - Ok(JsString::from(date.inner.calendar().identifier()).into()) + let date = require_internal_slot!(this, Self, "PlainDate"); + + Ok(JsString::from(date.borrow().data().inner.calendar().identifier()).into()) } /// 3.3.4 get `Temporal.PlainDate.prototype.era` @@ -801,15 +795,9 @@ impl PlainDate { ) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; - - let year_month = date.inner.to_plain_year_month()?; + let date = require_internal_slot!(this, Self, "PlainDate"); + + let year_month = date.borrow().data().inner.to_plain_year_month()?; create_temporal_year_month(year_month, None, context) } @@ -831,15 +819,9 @@ impl PlainDate { ) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; - - let month_day = date.inner.to_plain_month_day()?; + let date = require_internal_slot!(this, Self, "PlainDate"); + + let month_day = date.borrow().data().inner.to_plain_month_day()?; create_temporal_month_day(month_day, None, context) } @@ -857,13 +839,7 @@ impl PlainDate { fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); // 3. Let duration be ? ToTemporalDuration(temporalDurationLike). let duration = to_temporal_duration_record(args.get_or_undefined(0), context)?; @@ -875,7 +851,7 @@ impl PlainDate { // 5. Let calendarRec be ? CreateCalendarMethodsRecord(temporalDate.[[Calendar]], « date-add »). // 6. Return ? AddDate(calendarRec, temporalDate, duration, options). - let resolved_date = date.inner.add(&duration, overflow)?; + let resolved_date = date.borrow().data().inner.add(&duration, overflow)?; create_temporal_date(resolved_date, None, context).map(Into::into) } @@ -893,13 +869,7 @@ impl PlainDate { fn subtract(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); // 3. Let duration be ? ToTemporalDuration(temporalDurationLike). let duration = to_temporal_duration_record(args.get_or_undefined(0), context)?; @@ -911,7 +881,7 @@ impl PlainDate { // 5. Let negatedDuration be CreateNegatedTemporalDuration(duration). // 6. Let calendarRec be ? CreateCalendarMethodsRecord(temporalDate.[[Calendar]], « date-add »). // 7. Return ? AddDate(calendarRec, temporalDate, negatedDuration, options). - let resolved_date = date.inner.subtract(&duration, overflow)?; + let resolved_date = date.borrow().data().inner.subtract(&duration, overflow)?; create_temporal_date(resolved_date, None, context).map(Into::into) } @@ -929,13 +899,7 @@ impl PlainDate { fn with(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); // 3. If ? IsPartialTemporalObject(temporalDateLike) is false, throw a TypeError exception. let Some(partial_object) = @@ -951,7 +915,11 @@ impl PlainDate { // 5. Let fields be ISODateToFields(calendar, temporalDate.[[ISODate]], date). // 6. Let partialDate be ? PrepareCalendarFields(calendar, temporalDateLike, « year, month, month-code, day », « », partial). // 7. Set fields to CalendarMergeFields(calendar, fields, partialDate). - let fields = to_calendar_fields(&partial_object, date.inner.calendar(), context)?; + let fields = to_calendar_fields( + &partial_object, + date.borrow().data().inner.calendar(), + context, + )?; // 8. Let resolvedOptions be ? GetOptionsObject(options). let options = get_options_object(args.get_or_undefined(1))?; @@ -959,7 +927,7 @@ impl PlainDate { let overflow = get_option::(&options, js_string!("overflow"), context)?; // 10. Return ? CalendarDateFromFields(calendarRec, fields, resolvedOptions). - let resolved_date = date.inner.with(fields, overflow)?; + let resolved_date = date.borrow().data().inner.with(fields, overflow)?; create_temporal_date(resolved_date, None, context).map(Into::into) } @@ -977,19 +945,13 @@ impl PlainDate { /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDate.html#method.with_calendar fn with_calendar(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let plainDate be the this value. - let object = this.as_object(); // 2. Perform ? RequireInternalSlot(plainDate, [[InitializedTemporalDate]]). - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); // 3. Let calendar be ? ToTemporalCalendarIdentifier(calendarLike). let calendar = to_temporal_calendar_identifier(args.get_or_undefined(0))?; // 4. Return ! CreateTemporalDate(plainDate.[[ISODate]], calendar). - let resolved_date = date.inner.with_calendar(calendar); + let resolved_date = date.borrow().data().inner.with_calendar(calendar); create_temporal_date(resolved_date, None, context).map(Into::into) } @@ -1007,13 +969,7 @@ impl PlainDate { fn until(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); let other = to_temporal_date(args.get_or_undefined(0), None, context)?; @@ -1021,7 +977,12 @@ impl PlainDate { let options = get_options_object(args.get_or_undefined(1))?; let settings = get_difference_settings(&options, context)?; - create_temporal_duration(date.inner.until(&other, settings)?, None, context).map(Into::into) + create_temporal_duration( + date.borrow().data().inner.until(&other, settings)?, + None, + context, + ) + .map(Into::into) } /// 3.3.26 `Temporal.PlainDate.prototype.since ( other [ , options ] )` @@ -1038,13 +999,7 @@ impl PlainDate { fn since(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); // 3. Return ? DifferenceTemporalPlainDate(since, temporalDate, other, options). let other = to_temporal_date(args.get_or_undefined(0), None, context)?; @@ -1052,7 +1007,12 @@ impl PlainDate { let options = get_options_object(args.get_or_undefined(1))?; let settings = get_difference_settings(&options, context)?; - create_temporal_duration(date.inner.since(&other, settings)?, None, context).map(Into::into) + create_temporal_duration( + date.borrow().data().inner.since(&other, settings)?, + None, + context, + ) + .map(Into::into) } /// 3.3.27 `Temporal.PlainDate.prototype.equals ( other )` @@ -1067,17 +1027,11 @@ impl PlainDate { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate/equals /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDate.html#impl-Eq-for-PlainDate fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); let other = to_temporal_date(args.get_or_undefined(0), None, context)?; - Ok((date.inner == other).into()) + Ok((date.borrow().data().inner == other).into()) } /// 3.3.28 `Temporal.PlainDate.prototype.toPlainDateTime ( [ temporalTime ] )` @@ -1098,13 +1052,7 @@ impl PlainDate { ) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); // 3. Set temporalTime to ? ToTemporalTimeOrMidnight(temporalTime). let time = args @@ -1112,8 +1060,12 @@ impl PlainDate { .map(|v| to_temporal_time(v, None, context)) .transpose()?; // 4. Return ? CreateTemporalDateTime(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], temporalDate.[[Calendar]]). - create_temporal_datetime(date.inner.to_plain_date_time(time)?, None, context) - .map(Into::into) + create_temporal_datetime( + date.borrow().data().inner.to_plain_date_time(time)?, + None, + context, + ) + .map(Into::into) } /// 3.3.29 `Temporal.PlainDate.prototype.toZonedDateTime ( item )` @@ -1134,13 +1086,7 @@ impl PlainDate { ) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); let item = args.get_or_undefined(0); // 3. If item is an Object, then @@ -1174,11 +1120,11 @@ impl PlainDate { (to_temporal_timezone_identifier(item, context)?, None) }; - let result = date.inner.to_zoned_date_time_with_provider( - timezone, - time, - context.timezone_provider(), - )?; + let result = date + .borrow() + .data() + .inner + .to_zoned_date_time_with_provider(timezone, time, context.timezone_provider())?; // 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, temporalDate.[[Calendar]]). create_temporal_zoneddatetime(result, None, context).map(Into::into) @@ -1196,19 +1142,13 @@ impl PlainDate { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate/toString /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDate.html#method.to_ixdtf_string fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; + let date = require_internal_slot!(this, Self, "PlainDate"); let options = get_options_object(args.get_or_undefined(0))?; let display_calendar = get_option::(&options, js_string!("calendarName"), context)? .unwrap_or(DisplayCalendar::Auto); - Ok(JsString::from(date.inner.to_ixdtf_string(display_calendar)).into()) + Ok(JsString::from(date.borrow().data().inner.to_ixdtf_string(display_calendar)).into()) } /// 3.3.31 `Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] )` @@ -1222,15 +1162,9 @@ impl PlainDate { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate/toLocaleString fn to_locale_string(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; - - Ok(JsString::from(date.inner.to_string()).into()) + let date = require_internal_slot!(this, Self, "PlainDate"); + + Ok(JsString::from(date.borrow().data().inner.to_string()).into()) } /// 3.3.32 `Temporal.PlainDate.prototype.toJSON ( )` @@ -1243,15 +1177,9 @@ impl PlainDate { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toJSON /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate/toJSON fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let date = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDate object.") - })?; - - Ok(JsString::from(date.inner.to_string()).into()) + let date = require_internal_slot!(this, Self, "PlainDate"); + + Ok(JsString::from(date.borrow().data().inner.to_string()).into()) } /// 3.3.33 `Temporal.PlainDate.prototype.valueOf ( )` diff --git a/core/engine/src/builtins/temporal/plain_date_time/mod.rs b/core/engine/src/builtins/temporal/plain_date_time/mod.rs index 9a7bc594742..046a959f359 100644 --- a/core/engine/src/builtins/temporal/plain_date_time/mod.rs +++ b/core/engine/src/builtins/temporal/plain_date_time/mod.rs @@ -488,15 +488,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/calendarId /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.calendar fn get_calendar_id(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(JsString::from(dt.inner.calendar().identifier()).into()) + Ok(JsString::from(dt.borrow().data().inner.calendar().identifier()).into()) } /// 5.3.4 get `Temporal.PlainDateTime.prototype.era` @@ -511,15 +505,11 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/era /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.era fn get_era(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); Ok(dt + .borrow() + .data() .inner .era() .map(|s| JsString::from(s.as_str())) @@ -538,15 +528,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/eraYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.era_year fn get_era_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.era_year().into_or_undefined()) + Ok(dt.borrow().data().inner.era_year().into_or_undefined()) } /// 5.3.6 get `Temporal.PlainDateTime.prototype.year` @@ -561,15 +545,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/year /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.year fn get_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.year().into()) + Ok(dt.borrow().data().inner.year().into()) } /// 5.3.7 get `Temporal.PlainDateTime.prototype.month` @@ -584,15 +562,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/month /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html fn get_month(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.month().into()) + Ok(dt.borrow().data().inner.month().into()) } /// 5.3.8 get `Temporal.PlainDateTime.prototype.monthCode` @@ -607,15 +579,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/monthCode /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.month_code fn get_month_code(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(JsString::from(dt.inner.month_code().as_str()).into()) + Ok(JsString::from(dt.borrow().data().inner.month_code().as_str()).into()) } /// 5.3.9 get `Temporal.PlainDateTime.prototype.day` @@ -630,15 +596,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/day /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.day fn get_day(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.day().into()) + Ok(dt.borrow().data().inner.day().into()) } /// 5.3.10 get `Temporal.PlainDateTime.prototype.hour` @@ -655,16 +615,10 @@ impl PlainDateTime { fn get_hour(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Return 𝔽(datedt.[[ISOHour]]). - Ok(dt.inner.hour().into()) + Ok(dt.borrow().data().inner.hour().into()) } /// 5.3.11 get `Temporal.PlainDateTime.prototype.minute` @@ -681,16 +635,10 @@ impl PlainDateTime { fn get_minute(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Return 𝔽(datedt.[[ISOMinute]]). - Ok(dt.inner.minute().into()) + Ok(dt.borrow().data().inner.minute().into()) } /// 5.3.12 get `Temporal.PlainDateTime.prototype.second` @@ -707,16 +655,10 @@ impl PlainDateTime { fn get_second(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Return 𝔽(datedt.[[ISOSecond]]). - Ok(dt.inner.second().into()) + Ok(dt.borrow().data().inner.second().into()) } /// 5.3.13 get `Temporal.PlainDateTime.prototype.millisecond` @@ -733,16 +675,10 @@ impl PlainDateTime { fn get_millisecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Return 𝔽(datedt.[[ISOMillisecond]]). - Ok(dt.inner.millisecond().into()) + Ok(dt.borrow().data().inner.millisecond().into()) } /// 5.3.14 get `Temporal.PlainDateTime.prototype.microsecond` @@ -759,16 +695,10 @@ impl PlainDateTime { fn get_microsecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Return 𝔽(datedt.[[ISOMicrosecond]]). - Ok(dt.inner.microsecond().into()) + Ok(dt.borrow().data().inner.microsecond().into()) } /// 5.3.15 get `Temporal.PlainDateTime.prototype.nanosecond` @@ -785,16 +715,10 @@ impl PlainDateTime { fn get_nanosecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Return 𝔽(datedt.[[ISONanosecond]]). - Ok(dt.inner.nanosecond().into()) + Ok(dt.borrow().data().inner.nanosecond().into()) } /// 5.3.16 get `Temporal.PlainDateTime.prototype.dayOfWeek` @@ -809,15 +733,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/dayOfWeek /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.day_of_week fn get_day_of_week(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.day_of_week().into()) + Ok(dt.borrow().data().inner.day_of_week().into()) } /// 5.3.17 get `Temporal.PlainDateTime.prototype.dayOfYear` @@ -832,15 +750,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/dayOfYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.day_of_year fn get_day_of_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.day_of_year().into()) + Ok(dt.borrow().data().inner.day_of_year().into()) } /// 5.3.18 get `Temporal.PlainDateTime.prototype.weekOfYear` @@ -855,15 +767,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/weekOfYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.week_of_year fn get_week_of_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.week_of_year().into_or_undefined()) + Ok(dt.borrow().data().inner.week_of_year().into_or_undefined()) } /// 5.3.19 get `Temporal.PlainDateTime.prototype.yearOfWeek` @@ -878,15 +784,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/yearOfWeek /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.year_of_week fn get_year_of_week(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.year_of_week().into_or_undefined()) + Ok(dt.borrow().data().inner.year_of_week().into_or_undefined()) } /// 5.3.20 get `Temporal.PlainDateTime.prototype.daysInWeek` @@ -901,15 +801,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/daysInWeek /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.days_in_week fn get_days_in_week(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.days_in_week().into()) + Ok(dt.borrow().data().inner.days_in_week().into()) } /// 5.3.21 get `Temporal.PlainDateTime.prototype.daysInMonth` @@ -924,15 +818,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/daysInMonth /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.days_in_month fn get_days_in_month(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.days_in_month().into()) + Ok(dt.borrow().data().inner.days_in_month().into()) } /// 5.3.22 get `Temporal.PlainDateTime.prototype.daysInYear` @@ -947,15 +835,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/daysInYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.days_in_year fn get_days_in_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.days_in_year().into()) + Ok(dt.borrow().data().inner.days_in_year().into()) } /// 5.3.23 get `Temporal.PlainDateTime.prototype.monthsInYear` @@ -970,15 +852,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/monthsInYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.months_in_year fn get_months_in_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.months_in_year().into()) + Ok(dt.borrow().data().inner.months_in_year().into()) } /// 5.3.24 get `Temporal.PlainDateTime.prototype.inLeapYear` @@ -993,15 +869,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/inLeapYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.in_leap_year fn get_in_leap_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - Ok(dt.inner.in_leap_year().into()) + Ok(dt.borrow().data().inner.in_leap_year().into()) } } @@ -1083,13 +953,7 @@ impl PlainDateTime { fn with(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let plainDateTime be the this value. // 2. Perform ? RequireInternalSlot(plainDateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. If ? IsPartialTemporalObject(temporalDateTimeLike) is false, throw a TypeError exception. let Some(partial_object) = @@ -1109,7 +973,11 @@ impl PlainDateTime { // 11. Set fields.[[Nanosecond]] to plainDateTime.[[ISODateTime]].[[Time]].[[Nanosecond]]. // 12. Let partialDateTime be ? PrepareCalendarFields(calendar, temporalDateTimeLike, « year, month, month-code, day », « hour, minute, second, millisecond, microsecond, nanosecond », partial). // 13. Set fields to CalendarMergeFields(calendar, fields, partialDateTime). - let fields = to_date_time_fields(&partial_object, dt.inner.calendar(), context)?; + let fields = to_date_time_fields( + &partial_object, + dt.borrow().data().inner.calendar(), + context, + )?; // 14. Let resolvedOptions be ? GetOptionsObject(options). let options = get_options_object(args.get_or_undefined(1))?; // 15. Let overflow be ? GetTemporalOverflowOption(resolvedOptions). @@ -1117,7 +985,12 @@ impl PlainDateTime { // 16. Let result be ? InterpretTemporalDateTimeFields(calendar, fields, overflow). // 17. Return ? CreateTemporalDateTime(result, calendar). - create_temporal_datetime(dt.inner.with(fields, overflow)?, None, context).map(Into::into) + create_temporal_datetime( + dt.borrow().data().inner.with(fields, overflow)?, + None, + context, + ) + .map(Into::into) } /// 5.3.26 Temporal.PlainDateTime.prototype.withPlainTime ( `[ plainTimeLike ]` ) @@ -1136,20 +1009,15 @@ impl PlainDateTime { args: &[JsValue], context: &mut Context, ) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let time = args .get_or_undefined(0) .map(|v| to_temporal_time(v, None, context)) .transpose()?; - create_temporal_datetime(dt.inner.with_time(time)?, None, context).map(Into::into) + create_temporal_datetime(dt.borrow().data().inner.with_time(time)?, None, context) + .map(Into::into) } /// 5.3.27 `Temporal.PlainDateTime.prototype.withCalendar ( calendarLike )` @@ -1164,17 +1032,16 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/withCalendar /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.with_calendar fn with_calendar(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let calendar = to_temporal_calendar_identifier(args.get_or_undefined(0))?; - create_temporal_datetime(dt.inner.with_calendar(calendar), None, context).map(Into::into) + create_temporal_datetime( + dt.borrow().data().inner.with_calendar(calendar), + None, + context, + ) + .map(Into::into) } /// 5.3.28 `Temporal.PlainDateTime.prototype.add ( temporalDurationLike [ , options ] )` @@ -1191,13 +1058,7 @@ impl PlainDateTime { fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Let duration be ? ToTemporalDuration(temporalDurationLike). let duration = to_temporal_duration_record(args.get_or_undefined(0), context)?; @@ -1208,7 +1069,12 @@ impl PlainDateTime { // 5. Let calendarRec be ? CreateCalendarMethodsRecord(temporalDate.[[Calendar]], « date-add »). // 6. Return ? AddDate(calendarRec, temporalDate, duration, options). - create_temporal_datetime(dt.inner.add(&duration, overflow)?, None, context).map(Into::into) + create_temporal_datetime( + dt.borrow().data().inner.add(&duration, overflow)?, + None, + context, + ) + .map(Into::into) } /// 5.3.29 `Temporal.PlainDateTime.prototype.subtract ( temporalDurationLike [ , options ] )` @@ -1225,13 +1091,7 @@ impl PlainDateTime { fn subtract(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Let duration be ? ToTemporalDuration(temporalDurationLike). let duration = to_temporal_duration_record(args.get_or_undefined(0), context)?; @@ -1243,8 +1103,12 @@ impl PlainDateTime { // 5. Let negatedDuration be CreateNegatedTemporalDuration(duration). // 6. Let calendarRec be ? CreateCalendarMethodsRecord(temporalDate.[[Calendar]], « date-add »). // 7. Return ? AddDate(calendarRec, temporalDate, negatedDuration, options). - create_temporal_datetime(dt.inner.subtract(&duration, overflow)?, None, context) - .map(Into::into) + create_temporal_datetime( + dt.borrow().data().inner.subtract(&duration, overflow)?, + None, + context, + ) + .map(Into::into) } /// 5.3.30 `Temporal.PlainDateTime.prototype.until ( other [ , options ] )` @@ -1259,20 +1123,19 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/until /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.until fn until(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let other = to_temporal_datetime(args.get_or_undefined(0), None, context)?; let options = get_options_object(args.get_or_undefined(1))?; let settings = get_difference_settings(&options, context)?; - create_temporal_duration(dt.inner.until(&other, settings)?, None, context).map(Into::into) + create_temporal_duration( + dt.borrow().data().inner.until(&other, settings)?, + None, + context, + ) + .map(Into::into) } /// 5.3.31 `Temporal.PlainDateTime.prototype.since ( other [ , options ] )` @@ -1287,20 +1150,19 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/since /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.since fn since(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let other = to_temporal_datetime(args.get_or_undefined(0), None, context)?; let options = get_options_object(args.get_or_undefined(1))?; let settings = get_difference_settings(&options, context)?; - create_temporal_duration(dt.inner.since(&other, settings)?, None, context).map(Into::into) + create_temporal_duration( + dt.borrow().data().inner.since(&other, settings)?, + None, + context, + ) + .map(Into::into) } /// 5.3.32 Temporal.PlainDateTime.prototype.round ( roundTo ) @@ -1315,13 +1177,7 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/round /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.round fn round(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let round_to = match args.first().map(JsValue::variant) { // 3. If roundTo is undefined, then @@ -1369,7 +1225,8 @@ impl PlainDateTime { context, )?; - create_temporal_datetime(dt.inner().round(options)?, None, context).map(Into::into) + create_temporal_datetime(dt.borrow().data().inner().round(options)?, None, context) + .map(Into::into) } /// 5.3.33 Temporal.PlainDateTime.prototype.equals ( other ) @@ -1386,13 +1243,7 @@ impl PlainDateTime { fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Set other to ? ToTemporalDateTime(other). let other = to_temporal_datetime(args.get_or_undefined(0), None, context)?; @@ -1405,7 +1256,7 @@ impl PlainDateTime { // other.[[ISOMicrosecond]], other.[[ISONanosecond]]). // 5. If result is not 0, return false. // 6. Return ? CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]). - Ok((dt.inner == other).into()) + Ok((dt.borrow().data().inner == other).into()) } /// 5.3.34 `Temporal.PlainDateTime.prototype.toString ( [ options ] )` @@ -1420,13 +1271,7 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/with /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.to_ixdtf_string fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let options = get_options_object(args.get_or_undefined(0))?; @@ -1438,7 +1283,7 @@ impl PlainDateTime { get_option::(&options, js_string!("roundingMode"), context)?; let smallest_unit = get_option::(&options, js_string!("smallestUnit"), context)?; - let ixdtf = dt.inner.to_ixdtf_string( + let ixdtf = dt.borrow().data().inner.to_ixdtf_string( ToStringRoundingOptions { precision, smallest_unit, @@ -1460,15 +1305,11 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/with fn to_locale_string(this: &JsValue, _args: &[JsValue], _: &mut Context) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let ixdtf = dt + .borrow() + .data() .inner .to_ixdtf_string(ToStringRoundingOptions::default(), DisplayCalendar::Auto)?; Ok(JsString::from(ixdtf).into()) @@ -1484,15 +1325,11 @@ impl PlainDateTime { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.with /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/with fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); let ixdtf = dt + .borrow() + .data() .inner .to_ixdtf_string(ToStringRoundingOptions::default(), DisplayCalendar::Auto)?; Ok(JsString::from(ixdtf).into()) @@ -1531,13 +1368,7 @@ impl PlainDateTime { ) -> JsResult { // 1. Let dateTime be the this value. // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); // 3. Let timeZone be ? ToTemporalTimeZoneIdentifier(temporalTimeZoneLike). let timezone = to_temporal_timezone_identifier(args.get_or_undefined(0), context)?; // 4. Let resolvedOptions be ? GetOptionsObject(options). @@ -1550,7 +1381,7 @@ impl PlainDateTime { // 6. Let epochNs be ? GetEpochNanosecondsFor(timeZone, dateTime.[[ISODateTime]], disambiguation). // 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, dateTime.[[Calendar]]). - let result = dt.inner.to_zoned_date_time_with_provider( + let result = dt.borrow().data().inner.to_zoned_date_time_with_provider( timezone, disambiguation, context.timezone_provider(), @@ -1570,15 +1401,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/toPlainDate /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.to_plain_date fn to_plain_date(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - let result = dt.inner.to_plain_date(); + let result = dt.borrow().data().inner.to_plain_date(); create_temporal_date(result, None, context).map(Into::into) } @@ -1594,15 +1419,9 @@ impl PlainDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/toPlainTime /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainDateTime.html#method.to_plain_time fn to_plain_time(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let dt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainDateTime object.") - })?; + let dt = require_internal_slot!(this, Self, "PlainDateTime"); - let result = dt.inner.to_plain_time(); + let result = dt.borrow().data().inner.to_plain_time(); create_temporal_time(result, None, context).map(Into::into) } } diff --git a/core/engine/src/builtins/temporal/plain_month_day/mod.rs b/core/engine/src/builtins/temporal/plain_month_day/mod.rs index dde472d2a2b..654e5370301 100644 --- a/core/engine/src/builtins/temporal/plain_month_day/mod.rs +++ b/core/engine/src/builtins/temporal/plain_month_day/mod.rs @@ -192,14 +192,9 @@ impl PlainMonthDay { impl PlainMonthDay { // Helper for retrieving internal fields fn get_internal_field(this: &JsValue, field: &DateTimeValues) -> JsResult { - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; - let inner = &month_day.inner; + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); + let month_day_data = month_day.borrow(); + let inner = &month_day_data.data().inner; match field { DateTimeValues::Day => Ok(inner.day().into()), DateTimeValues::MonthCode => Ok(js_string!(inner.month_code().as_str()).into()), @@ -219,14 +214,8 @@ impl PlainMonthDay { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainMonthDay/calendarId /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainMonthDay.html#method.calendar fn get_calendar_id(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; - Ok(js_string!(month_day.inner.calendar().identifier()).into()) + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); + Ok(js_string!(month_day.borrow().data().inner.calendar().identifier()).into()) } /// 10.3.4 get `Temporal.PlainMonthDay.prototype.day` @@ -277,13 +266,7 @@ impl PlainMonthDay { fn with(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let monthDay be the this value. // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); // 3. If ? IsPartialTemporalObject(temporalMonthDayLike) is false, throw a TypeError exception. let Some(object) = is_partial_temporal_object(args.get_or_undefined(0), context)? else { @@ -294,7 +277,8 @@ impl PlainMonthDay { // 4. Let calendar be monthDay.[[Calendar]]. // 5. Let fields be ISODateToFields(calendar, monthDay.[[ISODate]], month-day). // 6. Let partialMonthDay be ? PrepareCalendarFields(calendar, temporalMonthDayLike, « year, month, month-code, day », « », partial). - let fields = to_calendar_fields(&object, month_day.inner.calendar(), context)?; + let fields = + to_calendar_fields(&object, month_day.borrow().data().inner.calendar(), context)?; // 7. Set fields to CalendarMergeFields(calendar, fields, partialMonthDay). // 8. Let resolvedOptions be ? GetOptionsObject(options). let resolved_options = get_options_object(args.get_or_undefined(1))?; @@ -302,7 +286,11 @@ impl PlainMonthDay { let overflow = get_option::(&resolved_options, js_string!("overflow"), context)?; // 10. Let isoDate be ? CalendarMonthDayFromFields(calendar, fields, overflow). // 11. Return ! CreateTemporalMonthDay(isoDate, calendar). - create_temporal_month_day(month_day.inner.with(fields, overflow)?, None, context) + create_temporal_month_day( + month_day.borrow().data().inner.with(fields, overflow)?, + None, + context, + ) } /// 10.3.7 `Temporal.PlainMonthDay.prototype.equals ( other )` @@ -317,18 +305,12 @@ impl PlainMonthDay { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainMonthDay/equals /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainMonthDay.html#impl-PartialEq-for-PlainMonthDay fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); let other = to_temporal_month_day(args.get_or_undefined(0), &JsValue::undefined(), context)?; - Ok((month_day.inner == other).into()) + Ok((month_day.borrow().data().inner == other).into()) } /// 10.3.8 `Temporal.PlainMonthDay.prototype.toString ( [ options ] )` @@ -345,13 +327,7 @@ impl PlainMonthDay { fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let monthDay be the this value. // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); // 3. Set options to ? NormalizeOptionsObject(options). let options = get_options_object(args.get_or_undefined(0))?; @@ -361,7 +337,11 @@ impl PlainMonthDay { get_option::(&options, js_string!("calendarName"), context)? .unwrap_or(DisplayCalendar::Auto); - let ixdtf = month_day.inner.to_ixdtf_string(show_calendar); + let ixdtf = month_day + .borrow() + .data() + .inner + .to_ixdtf_string(show_calendar); Ok(JsString::from(ixdtf).into()) } @@ -380,15 +360,9 @@ impl PlainMonthDay { _: &mut Context, ) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; - - Ok(JsString::from(month_day.inner.to_string()).into()) + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); + + Ok(JsString::from(month_day.borrow().data().inner.to_string()).into()) } /// 10.3.10 `Temporal.PlainMonthDay.prototype.toJSON ( )` @@ -401,15 +375,9 @@ impl PlainMonthDay { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.tojson /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainMonthDay/toJSON pub(crate) fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; - - Ok(JsString::from(month_day.inner.to_string()).into()) + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); + + Ok(JsString::from(month_day.borrow().data().inner.to_string()).into()) } /// 9.3.11 `Temporal.PlainMonthDay.prototype.valueOf ( )` @@ -441,13 +409,7 @@ impl PlainMonthDay { fn to_plain_date(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let monthDay be the this value. // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). - let object = this.as_object(); - let month_day = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainMonthDay object.") - })?; + let month_day = require_internal_slot!(this, Self, "PlainMonthDay"); // 3. If item is not an Object, then let Some(item) = args.get_or_undefined(0).as_object() else { @@ -474,7 +436,11 @@ impl PlainMonthDay { // 7. Let mergedFields be CalendarMergeFields(calendar, fields, inputFields). // 8. Let isoDate be ? CalendarDateFromFields(calendar, mergedFields, constrain). // 9. Return ! CreateTemporalDate(isoDate, calendar). - let result = month_day.inner.to_plain_date(Some(fields))?; + let result = month_day + .borrow() + .data() + .inner + .to_plain_date(Some(fields))?; create_temporal_date(result, None, context).map(Into::into) } } diff --git a/core/engine/src/builtins/temporal/plain_time/mod.rs b/core/engine/src/builtins/temporal/plain_time/mod.rs index ba1782db971..e33a191ae7f 100644 --- a/core/engine/src/builtins/temporal/plain_time/mod.rs +++ b/core/engine/src/builtins/temporal/plain_time/mod.rs @@ -266,16 +266,10 @@ impl PlainTime { fn get_hour(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Return 𝔽(temporalTime.[[ISOHour]]). - Ok(time.inner.hour().into()) + Ok(time.borrow().data().inner.hour().into()) } /// 4.3.4 get `Temporal.PlainTime.prototype.minute` @@ -292,16 +286,10 @@ impl PlainTime { fn get_minute(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Return 𝔽(temporalTime.[[ISOMinute]]). - Ok(time.inner.minute().into()) + Ok(time.borrow().data().inner.minute().into()) } /// 4.3.5 get `Temporal.PlainTime.prototype.second` @@ -318,16 +306,10 @@ impl PlainTime { fn get_second(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Return 𝔽(temporalTime.[[ISOSecond]]). - Ok(time.inner.second().into()) + Ok(time.borrow().data().inner.second().into()) } /// 4.3.6 get `Temporal.PlainTime.prototype.millisecond` @@ -344,16 +326,10 @@ impl PlainTime { fn get_millisecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Return 𝔽(temporalTime.[[ISOMillisecond]]). - Ok(time.inner.millisecond().into()) + Ok(time.borrow().data().inner.millisecond().into()) } /// 4.3.7 get `Temporal.PlainTime.prototype.microsecond` @@ -370,16 +346,10 @@ impl PlainTime { fn get_microsecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Return 𝔽(temporalTime.[[ISOMicrosecond]]). - Ok(time.inner.microsecond().into()) + Ok(time.borrow().data().inner.microsecond().into()) } /// 4.3.8 get `Temporal.PlainTime.prototype.nanosecond` @@ -396,16 +366,10 @@ impl PlainTime { fn get_nanosecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Return 𝔽(temporalTime.[[ISONanosecond]]). - Ok(time.inner.nanosecond().into()) + Ok(time.borrow().data().inner.nanosecond().into()) } } @@ -468,19 +432,14 @@ impl PlainTime { fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let temporal_duration_like = args.get_or_undefined(0); let duration = to_temporal_duration_record(temporal_duration_like, context)?; // 3. Return ? AddDurationToOrSubtractDurationFromPlainTime(add, temporalTime, temporalDurationLike). - create_temporal_time(time.inner.add(&duration)?, None, context).map(Into::into) + create_temporal_time(time.borrow().data().inner.add(&duration)?, None, context) + .map(Into::into) } /// 4.3.10 `Temporal.PlainTime.prototype.subtract ( temporalDurationLike )` @@ -497,19 +456,18 @@ impl PlainTime { fn subtract(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let temporal_duration_like = args.get_or_undefined(0); let duration = to_temporal_duration_record(temporal_duration_like, context)?; // 3. Return ? AddDurationToOrSubtractDurationFromPlainTime(subtract, temporalTime, temporalDurationLike). - create_temporal_time(time.inner.subtract(&duration)?, None, context).map(Into::into) + create_temporal_time( + time.borrow().data().inner.subtract(&duration)?, + None, + context, + ) + .map(Into::into) } /// 4.3.11 `Temporal.PlainTime.prototype.with ( temporalTimeLike [ , options ] )` @@ -526,13 +484,7 @@ impl PlainTime { fn with(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1.Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. If ? IsPartialTemporalObject(temporalTimeLike) is false, throw a TypeError exception. // 4. Set options to ? GetOptionsObject(options). @@ -552,7 +504,9 @@ impl PlainTime { let overflow = get_option::(&options, js_string!("overflow"), context)?; create_temporal_time( - time.inner + time.borrow() + .data() + .inner .with(partial.as_temporal_partial_time(overflow)?, overflow)?, None, context, @@ -572,20 +526,14 @@ impl PlainTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime/until /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainTime.html#method.until fn until(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let other = to_temporal_time(args.get_or_undefined(0), None, context)?; let settings = get_difference_settings(&get_options_object(args.get_or_undefined(1))?, context)?; - let result = time.inner.until(&other, settings)?; + let result = time.borrow().data().inner.until(&other, settings)?; create_temporal_duration(result, None, context).map(Into::into) } @@ -602,20 +550,14 @@ impl PlainTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime/since /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainTime.html#method.since fn since(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let other = to_temporal_time(args.get_or_undefined(0), None, context)?; let settings = get_difference_settings(&get_options_object(args.get_or_undefined(1))?, context)?; - let result = time.inner.since(&other, settings)?; + let result = time.borrow().data().inner.since(&other, settings)?; create_temporal_duration(result, None, context).map(Into::into) } @@ -634,13 +576,7 @@ impl PlainTime { fn round(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let round_to = match args.first().map(JsValue::variant) { // 3. If roundTo is undefined, then @@ -693,7 +629,7 @@ impl PlainTime { // 11. Assert: maximum is not undefined. // 12. Perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false). // 13. Let result be RoundTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], roundingIncrement, smallestUnit, roundingMode). - let result = time.inner.round(options)?; + let result = time.borrow().data().inner.round(options)?; // 14. Return ! CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]). create_temporal_time(result, None, context).map(Into::into) @@ -713,13 +649,7 @@ impl PlainTime { fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let temporalTime be the this value. // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); // 3. Set other to ? ToTemporalTime(other). let other = to_temporal_time(args.get_or_undefined(0), None, context)?; @@ -730,7 +660,7 @@ impl PlainTime { // 8. If temporalTime.[[ISOMicrosecond]] ≠ other.[[ISOMicrosecond]], return false. // 9. If temporalTime.[[ISONanosecond]] ≠ other.[[ISONanosecond]], return false. // 10. Return true. - Ok((time.inner == other).into()) + Ok((time.borrow().data().inner == other).into()) } /// 4.3.16 `Temporal.PlainTime.prototype.toString ( [ options ] )` @@ -745,13 +675,7 @@ impl PlainTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime/toString /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainTime.html#method.to_ixdtf_string fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let options = get_options_object(args.get_or_undefined(0))?; @@ -766,7 +690,7 @@ impl PlainTime { smallest_unit, }; - let ixdtf = time.inner.to_ixdtf_string(options)?; + let ixdtf = time.borrow().data().inner.to_ixdtf_string(options)?; Ok(JsString::from(ixdtf).into()) } @@ -782,15 +706,11 @@ impl PlainTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime/toLocaleString fn to_locale_string(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let ixdtf = time + .borrow() + .data() .inner .to_ixdtf_string(ToStringRoundingOptions::default())?; Ok(JsString::from(ixdtf).into()) @@ -806,15 +726,11 @@ impl PlainTime { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tojson /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime/toJSON fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let time = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a PlainTime object.") - })?; + let time = require_internal_slot!(this, Self, "PlainTime"); let ixdtf = time + .borrow() + .data() .inner .to_ixdtf_string(ToStringRoundingOptions::default())?; Ok(JsString::from(ixdtf).into()) diff --git a/core/engine/src/builtins/temporal/plain_year_month/mod.rs b/core/engine/src/builtins/temporal/plain_year_month/mod.rs index 1c328da5997..fe8b875d24d 100644 --- a/core/engine/src/builtins/temporal/plain_year_month/mod.rs +++ b/core/engine/src/builtins/temporal/plain_year_month/mod.rs @@ -297,14 +297,9 @@ impl PlainYearMonth { impl PlainYearMonth { // Helper for retrieving internal fields fn get_internal_field(this: &JsValue, field: &DateTimeValues) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - let inner = &year_month.inner; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + let year_month_data = year_month.borrow(); + let inner = &year_month_data.data().inner; match field { DateTimeValues::Year => Ok(inner.year().into()), DateTimeValues::Month => Ok(inner.month().into()), @@ -353,15 +348,11 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/era /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.era fn get_era(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); Ok(year_month + .borrow() + .data() .inner .era() .map(|s| JsString::from(s.as_str())) @@ -380,14 +371,13 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/eraYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.era_year fn get_era_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - Ok(year_month.inner.era_year().into_or_undefined()) + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + Ok(year_month + .borrow() + .data() + .inner + .era_year() + .into_or_undefined()) } /// 9.3.6 get `Temporal.PlainYearMonth.prototype.year` @@ -447,14 +437,9 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/daysInYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.days_in_year fn get_days_in_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - let inner = &year_month.inner; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + let year_month_data = year_month.borrow(); + let inner = &year_month_data.data().inner; Ok(inner.days_in_year().into()) } @@ -470,14 +455,9 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/daysInMonth /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.days_in_month fn get_days_in_month(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - let inner = &year_month.inner; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + let year_month_data = year_month.borrow(); + let inner = &year_month_data.data().inner; Ok(inner.days_in_month().into()) } @@ -493,14 +473,9 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/monthsInYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.months_in_year fn get_months_in_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - let inner = &year_month.inner; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + let year_month_data = year_month.borrow(); + let inner = &year_month_data.data().inner; Ok(inner.months_in_year().into()) } @@ -516,15 +491,9 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/inLeapYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.in_leap_year fn get_in_leap_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - - Ok(year_month.inner.in_leap_year().into()) + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + + Ok(year_month.borrow().data().inner.in_leap_year().into()) } } @@ -545,13 +514,7 @@ impl PlainYearMonth { fn with(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let yearMonth be the this value. // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); // 3. If ? IsPartialTemporalObject(temporalYearMonthLike) is false, throw a TypeError exception. let Some(obj) = is_partial_temporal_object(args.get_or_undefined(0), context)? else { @@ -563,7 +526,11 @@ impl PlainYearMonth { // 5. Let fields be ISODateToFields(calendar, yearMonth.[[ISODate]], year-month). // 6. Let partialYearMonth be ? PrepareCalendarFields(calendar, temporalYearMonthLike, « year, month, month-code », « », partial). // 7. Set fields to CalendarMergeFields(calendar, fields, partialYearMonth). - let fields = to_year_month_calendar_fields(&obj, year_month.inner.calendar(), context)?; + let fields = to_year_month_calendar_fields( + &obj, + year_month.borrow().data().inner.calendar(), + context, + )?; // NOTE: Update temporal_rs to handle this. if fields.is_empty() { return Err(JsNativeError::typ() @@ -576,7 +543,11 @@ impl PlainYearMonth { let overflow = get_option::(&resolved_options, js_string!("overflow"), context)? .unwrap_or_default(); // 10. Let isoDate be ? CalendarYearMonthFromFields(calendar, fields, overflow). - let result = year_month.inner.with(fields, Some(overflow))?; + let result = year_month + .borrow() + .data() + .inner + .with(fields, Some(overflow))?; // 11. Return ! CreateTemporalYearMonth(isoDate, calendar). create_temporal_year_month(result, None, context) } @@ -629,17 +600,11 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/until /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.until fn until(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); let other = to_temporal_year_month(args.get_or_undefined(0), None, context)?; - if year_month.inner.calendar() != other.calendar() { + if year_month.borrow().data().inner.calendar() != other.calendar() { return Err(JsNativeError::range() .with_message("calendars are not the same.") .into()); @@ -648,7 +613,7 @@ impl PlainYearMonth { let resolved_options = get_options_object(args.get_or_undefined(1))?; // TODO: Disallowed units must be rejected in `temporal_rs`. let settings = get_difference_settings(&resolved_options, context)?; - let result = year_month.inner.until(&other, settings)?; + let result = year_month.borrow().data().inner.until(&other, settings)?; create_temporal_duration(result, None, context).map(Into::into) } @@ -664,17 +629,11 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/since /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#method.since fn since(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); let other = to_temporal_year_month(args.get_or_undefined(0), None, context)?; - if year_month.inner.calendar() != other.calendar() { + if year_month.borrow().data().inner.calendar() != other.calendar() { return Err(JsNativeError::range() .with_message("calendars are not the same.") .into()); @@ -683,7 +642,7 @@ impl PlainYearMonth { let resolved_options = get_options_object(args.get_or_undefined(1))?; // TODO: Disallowed units must be rejected in `temporal_rs`. let settings = get_difference_settings(&resolved_options, context)?; - let result = year_month.inner.since(&other, settings)?; + let result = year_month.borrow().data().inner.since(&other, settings)?; create_temporal_duration(result, None, context).map(Into::into) } @@ -699,17 +658,11 @@ impl PlainYearMonth { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/equals /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.PlainYearMonth.html#impl-PartialEq-for-PlainYearMonth fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); let other = to_temporal_year_month(args.get_or_undefined(0), None, context)?; - Ok((year_month.inner == other).into()) + Ok((year_month.borrow().data().inner == other).into()) } /// 9.3.19 `Temporal.PlainYearMonth.prototype.toString ( [ options ] )` @@ -726,13 +679,7 @@ impl PlainYearMonth { fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let YearMonth be the this value. // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); // 3. Set options to ? NormalizeOptionsObject(options). let options = get_options_object(args.get_or_undefined(0))?; @@ -742,7 +689,11 @@ impl PlainYearMonth { get_option::(&options, js_string!("calendarName"), context)? .unwrap_or(DisplayCalendar::Auto); - let ixdtf = year_month.inner.to_ixdtf_string(show_calendar); + let ixdtf = year_month + .borrow() + .data() + .inner + .to_ixdtf_string(show_calendar); Ok(JsString::from(ixdtf).into()) } @@ -761,15 +712,9 @@ impl PlainYearMonth { _: &mut Context, ) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - - Ok(JsString::from(year_month.inner.to_string()).into()) + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + + Ok(JsString::from(year_month.borrow().data().inner.to_string()).into()) } /// 9.3.21 `Temporal.PlainYearMonth.prototype.toJSON ( )` @@ -782,15 +727,9 @@ impl PlainYearMonth { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.tojson /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/toJSON pub(crate) fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; - - Ok(JsString::from(year_month.inner.to_string()).into()) + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); + + Ok(JsString::from(year_month.borrow().data().inner.to_string()).into()) } /// 9.3.22 `Temporal.PlainYearMonth.prototype.valueOf ( )` @@ -822,13 +761,7 @@ impl PlainYearMonth { fn to_plain_date(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let yearMonth be the this value. // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). - let object = this.as_object(); - let year_month = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("this value must be a PlainYearMonth object.") - })?; + let year_month = require_internal_slot!(this, Self, "PlainYearMonth"); // 3. If item is not an Object, then let Some(obj) = args.get_or_undefined(0).as_object() else { @@ -854,7 +787,11 @@ impl PlainYearMonth { // 7. Let mergedFields be CalendarMergeFields(calendar, fields, inputFields). // 8. Let isoDate be ? CalendarDateFromFields(calendar, mergedFields, constrain). - let result = year_month.inner.to_plain_date(Some(fields))?; + let result = year_month + .borrow() + .data() + .inner + .to_plain_date(Some(fields))?; // 9. Return ! CreateTemporalDate(isoDate, calendar). create_temporal_date(result, None, context).map(Into::into) } diff --git a/core/engine/src/builtins/temporal/zoneddatetime/mod.rs b/core/engine/src/builtins/temporal/zoneddatetime/mod.rs index 2d0019c98db..824259e5566 100644 --- a/core/engine/src/builtins/temporal/zoneddatetime/mod.rs +++ b/core/engine/src/builtins/temporal/zoneddatetime/mod.rs @@ -478,15 +478,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/calendarId /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.calendar fn get_calendar_id(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(JsString::from(zdt.inner.calendar().identifier()).into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(JsString::from(zdt.borrow().data().inner.calendar().identifier()).into()) } /// 6.3.4 get `Temporal.ZonedDateTime.prototype.timeZoneId` @@ -501,16 +495,12 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/timeZoneId /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.timezone fn get_timezone_id(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); Ok(JsString::from( - zdt.inner + zdt.borrow() + .data() + .inner .time_zone() .identifier_with_provider(context.timezone_provider())?, ) @@ -529,15 +519,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/era /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.era fn get_era(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - let era = zdt.inner.era(); + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + let era = zdt.borrow().data().inner.era(); Ok(era .map(|tinystr| JsString::from(tinystr.cow_to_lowercase())) .into_or_undefined()) @@ -555,15 +539,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/eraYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.era_year fn get_era_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.era_year().into_or_undefined()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.era_year().into_or_undefined()) } /// 6.3.7 get `Temporal.ZonedDateTime.prototype.year` @@ -578,15 +556,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/year /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.year fn get_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.year().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.year().into()) } /// 6.3.8 get `Temporal.ZonedDateTime.prototype.month` @@ -601,15 +573,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/month /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.month fn get_month(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.month().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.month().into()) } /// 6.3.9 get `Temporal.ZonedDateTime.prototype.monthCode` @@ -624,15 +590,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/monthCode /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.month_code fn get_month_code(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(JsString::from(zdt.inner.month_code().as_str()).into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(JsString::from(zdt.borrow().data().inner.month_code().as_str()).into()) } /// 6.3.10 get `Temporal.ZonedDateTime.prototype.day` @@ -647,15 +607,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/day /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.day fn get_day(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.day().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.day().into()) } /// 6.3.11 get `Temporal.ZonedDateTime.prototype.hour` @@ -670,15 +624,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/hour /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.hour fn get_hour(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.hour().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.hour().into()) } /// 6.3.12 get `Temporal.ZonedDateTime.prototype.minute` @@ -693,15 +641,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/minute /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.minute fn get_minute(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.minute().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.minute().into()) } /// 6.3.13 get `Temporal.ZonedDateTime.prototype.second` @@ -716,15 +658,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/second /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.second fn get_second(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.second().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.second().into()) } /// 6.3.14 get `Temporal.ZonedDateTime.prototype.millisecond` @@ -739,15 +675,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/millisecond /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.millisecond fn get_millisecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.millisecond().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.millisecond().into()) } /// 6.3.15 get `Temporal.ZonedDateTime.prototype.microsecond` @@ -762,15 +692,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/microsecond /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.microsecond fn get_microsecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.microsecond().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.microsecond().into()) } /// 6.3.16 get `Temporal.ZonedDateTime.prototype.nanosecond` @@ -785,15 +709,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/nanosecond /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.nanosecond fn get_nanosecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.nanosecond().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.nanosecond().into()) } /// 6.3.17 get `Temporal.ZonedDateTime.prototype.epochMilliseconds` @@ -808,15 +726,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/epochMilliseconds /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.epoch_milliseconds fn get_epoch_milliseconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.epoch_milliseconds().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.epoch_milliseconds().into()) } /// 6.3.18 get `Temporal.ZonedDateTime.prototype.epochNanoseconds` @@ -831,15 +743,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/epochNanoseconds /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.epoch_nanoseconds fn get_epoch_nanoseconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(JsBigInt::from(zdt.inner.epoch_nanoseconds().as_i128()).into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(JsBigInt::from(zdt.borrow().data().inner.epoch_nanoseconds().as_i128()).into()) } /// 6.3.19 get `Temporal.ZonedDateTime.prototype.dayOfWeek` @@ -854,15 +760,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/dayOfWeek /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.day_of_week fn get_day_of_week(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.day_of_week().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.day_of_week().into()) } /// 6.3.20 get `Temporal.ZonedDateTime.prototype.dayOfYear` @@ -877,15 +777,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/dayOfYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.day_of_year fn get_day_of_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.day_of_year().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.day_of_year().into()) } /// 6.3.21 get `Temporal.ZonedDateTime.prototype.weekOfYear` @@ -900,15 +794,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/weekOfYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.week_of_year fn get_week_of_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.week_of_year().into_or_undefined()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.week_of_year().into_or_undefined()) } /// 6.3.22 get `Temporal.ZonedDateTime.prototype.yearOfWeek` @@ -923,15 +811,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/yearOfWeek /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.year_of_week fn get_year_of_week(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.year_of_week().into_or_undefined()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.year_of_week().into_or_undefined()) } /// 6.3.23 get `Temporal.ZonedDateTime.prototype.hoursInDay` @@ -946,15 +828,11 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/hoursInDay /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.hours_in_day fn get_hours_in_day(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); Ok(zdt + .borrow() + .data() .inner .hours_in_day_with_provider(context.timezone_provider())? .into()) @@ -972,15 +850,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/daysInWeek /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.days_in_week fn get_days_in_week(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.days_in_week().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.days_in_week().into()) } /// 6.3.25 get `Temporal.ZonedDateTime.prototype.daysInMonth` @@ -995,15 +867,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/daysInMonth /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.days_in_month fn get_days_in_month(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.days_in_month().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.days_in_month().into()) } /// 6.3.26 get `Temporal.ZonedDateTime.prototype.daysInYear` @@ -1018,15 +884,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/daysInYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.days_in_year fn get_days_in_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.days_in_year().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.days_in_year().into()) } /// 6.3.27 get `Temporal.ZonedDateTime.prototype.monthsInYear` @@ -1041,15 +901,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/monthsInYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.months_in_year fn get_months_in_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.months_in_year().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.months_in_year().into()) } /// 6.3.28 get `Temporal.ZonedDateTime.prototype.inLeapYear` @@ -1064,15 +918,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/inLeapYear /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.in_leap_year fn get_in_leap_year(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.in_leap_year().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.in_leap_year().into()) } /// 6.3.29 get Temporal.ZonedDateTime.prototype.offsetNanoseconds @@ -1087,15 +935,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/offsetNanoseconds /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.offset_nanoseconds fn get_offset_nanoseconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(zdt.inner.offset_nanoseconds().into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(zdt.borrow().data().inner.offset_nanoseconds().into()) } /// 6.3.30 get Temporal.ZonedDateTime.prototype.offset @@ -1110,15 +952,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/offset /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.offset fn get_offset(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - Ok(JsString::from(zdt.inner.offset()).into()) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + Ok(JsString::from(zdt.borrow().data().inner.offset()).into()) } } @@ -1178,13 +1014,7 @@ impl ZonedDateTime { fn with(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let zonedDateTime be the this value. // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); // 3. If ? IsPartialTemporalObject(temporalZonedDateTimeLike) is false, throw a TypeError exception. let Some(obj) = is_partial_temporal_object(args.get_or_undefined(0), context)? else { return Err(JsNativeError::typ() @@ -1208,7 +1038,7 @@ impl ZonedDateTime { // 18. Set fields to CalendarMergeFields(calendar, fields, partialZonedDateTime). let (fields, _) = to_zoned_date_time_fields( &obj, - zdt.inner.calendar(), + zdt.borrow().data().inner.calendar(), ZdtFieldsType::NoTimeZone, context, )?; @@ -1224,7 +1054,7 @@ impl ZonedDateTime { // 22. Let overflow be ? GetTemporalOverflowOption(resolvedOptions). let overflow = get_option::(&resolved_options, js_string!("overflow"), context)?; - let result = zdt.inner.with_with_provider( + let result = zdt.borrow().data().inner.with_with_provider( fields, disambiguation, offset, @@ -1250,13 +1080,7 @@ impl ZonedDateTime { args: &[JsValue], context: &mut Context, ) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let time = args .get_or_undefined(0) @@ -1264,6 +1088,8 @@ impl ZonedDateTime { .transpose()?; let inner = zdt + .borrow() + .data() .inner .with_plain_time_and_provider(time, context.timezone_provider())?; create_temporal_zoneddatetime(inner, None, context).map(Into::into) @@ -1281,17 +1107,13 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/withTimeZone /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.with_timezone fn with_timezone(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let timezone = to_temporal_timezone_identifier(args.get_or_undefined(0), context)?; let inner = zdt + .borrow() + .data() .inner .with_time_zone_with_provider(timezone, context.timezone_provider())?; create_temporal_zoneddatetime(inner, None, context).map(Into::into) @@ -1309,17 +1131,11 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/withCalendar /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.with_calendar fn with_calendar(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let calendar = to_temporal_calendar_identifier(args.get_or_undefined(0))?; - let inner = zdt.inner.with_calendar(calendar); + let inner = zdt.borrow().data().inner.with_calendar(calendar); create_temporal_zoneddatetime(inner, None, context).map(Into::into) } @@ -1335,22 +1151,18 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/add /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.add fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let duration = to_temporal_duration(args.get_or_undefined(0), context)?; let options = get_options_object(args.get_or_undefined(1))?; let overflow = get_option::(&options, js_string!("overflow"), context)?; - let result = - zdt.inner - .add_with_provider(&duration, overflow, context.timezone_provider())?; + let result = zdt.borrow().data().inner.add_with_provider( + &duration, + overflow, + context.timezone_provider(), + )?; create_temporal_zoneddatetime(result, None, context).map(Into::into) } @@ -1366,22 +1178,18 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/subtract /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.subtract fn subtract(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let duration = to_temporal_duration(args.get_or_undefined(0), context)?; let options = get_options_object(args.get_or_undefined(1))?; let overflow = get_option::(&options, js_string!("overflow"), context)?; - let result = - zdt.inner - .subtract_with_provider(&duration, overflow, context.timezone_provider())?; + let result = zdt.borrow().data().inner.subtract_with_provider( + &duration, + overflow, + context.timezone_provider(), + )?; create_temporal_zoneddatetime(result, None, context).map(Into::into) } @@ -1397,22 +1205,18 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/until /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.until fn until(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let other = to_temporal_zoneddatetime(args.get_or_undefined(0), None, context)?; let options = get_options_object(args.get_or_undefined(1))?; let settings = get_difference_settings(&options, context)?; - let result = - zdt.inner - .until_with_provider(&other, settings, context.timezone_provider())?; + let result = zdt.borrow().data().inner.until_with_provider( + &other, + settings, + context.timezone_provider(), + )?; create_temporal_duration(result, None, context).map(Into::into) } @@ -1428,22 +1232,18 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/since /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.since fn since(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let other = to_temporal_zoneddatetime(args.get_or_undefined(0), None, context)?; let options = get_options_object(args.get_or_undefined(1))?; let settings = get_difference_settings(&options, context)?; - let result = - zdt.inner - .since_with_provider(&other, settings, context.timezone_provider())?; + let result = zdt.borrow().data().inner.since_with_provider( + &other, + settings, + context.timezone_provider(), + )?; create_temporal_duration(result, None, context).map(Into::into) } @@ -1461,13 +1261,7 @@ impl ZonedDateTime { fn round(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { // 1. Let zonedDateTime be the this value. // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let round_to = match args.first().map(JsValue::variant) { // 3. If roundTo is undefined, then @@ -1521,6 +1315,8 @@ impl ZonedDateTime { )?; let result = zdt + .borrow() + .data() .inner .round_with_provider(options, context.timezone_provider())?; create_temporal_zoneddatetime(result, None, context).map(Into::into) @@ -1538,16 +1334,12 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/equals /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#impl-PartialEq-for-ZonedDateTime fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let other = to_temporal_zoneddatetime(args.get_or_undefined(0), None, context)?; Ok(zdt + .borrow() + .data() .inner .equals_with_provider(&other, context.timezone_provider())? .into()) @@ -1565,13 +1357,7 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/toString /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.to_ixdtf_string fn to_string(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let options = get_options_object(args.get_or_undefined(0))?; @@ -1594,7 +1380,7 @@ impl ZonedDateTime { smallest_unit, rounding_mode, }; - let ixdtf = zdt.inner.to_ixdtf_string_with_provider( + let ixdtf = zdt.borrow().data().inner.to_ixdtf_string_with_provider( show_offset, display_timezone, show_calendar, @@ -1616,15 +1402,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/toLocaleString fn to_locale_string(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { // TODO: Update for ECMA-402 compliance - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - let ixdtf = zdt.inner.to_ixdtf_string_with_provider( + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + let ixdtf = zdt.borrow().data().inner.to_ixdtf_string_with_provider( DisplayOffset::Auto, DisplayTimeZone::Auto, DisplayCalendar::Auto, @@ -1645,15 +1425,9 @@ impl ZonedDateTime { /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tojson /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/toJSON fn to_json(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - let ixdtf = zdt.inner.to_ixdtf_string_with_provider( + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + let ixdtf = zdt.borrow().data().inner.to_ixdtf_string_with_provider( DisplayOffset::Auto, DisplayTimeZone::Auto, DisplayCalendar::Auto, @@ -1691,15 +1465,11 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/startOfDay /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.start_of_day fn start_of_day(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let new = zdt + .borrow() + .data() .inner .start_of_day_with_provider(context.timezone_provider())?; create_temporal_zoneddatetime(new, None, context).map(Into::into) @@ -1724,13 +1494,7 @@ impl ZonedDateTime { // 1. Let zonedDateTime be the this value. // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). // 3. Let timeZone be zonedDateTime.[[TimeZone]]. - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); let direction_param = args.get_or_undefined(0); // 4. If directionParam is undefined, throw a TypeError exception. @@ -1767,6 +1531,8 @@ impl ZonedDateTime { // Step 8-12 let result = zdt + .borrow() + .data() .inner .get_time_zone_transition_with_provider(direction, context.timezone_provider())?; @@ -1788,15 +1554,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/toInstant /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.to_instant fn to_instant(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - create_temporal_instant(zdt.inner.to_instant(), None, context) + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + create_temporal_instant(zdt.borrow().data().inner.to_instant(), None, context) } /// 6.3.48 `Temporal.ZonedDateTime.prototype.toPlainDate ( )` @@ -1811,15 +1571,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/toPlainDate /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.to_plain_date fn to_plain_date(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - let inner = zdt.inner.to_plain_date(); + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + let inner = zdt.borrow().data().inner.to_plain_date(); create_temporal_date(inner, None, context).map(Into::into) } @@ -1835,15 +1589,9 @@ impl ZonedDateTime { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/toPlainTime /// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.ZonedDateTime.html#method.to_plain_time fn to_plain_time(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - let new = zdt.inner.to_plain_time(); + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + let new = zdt.borrow().data().inner.to_plain_time(); create_temporal_time(new, None, context).map(Into::into) } @@ -1863,15 +1611,9 @@ impl ZonedDateTime { _: &[JsValue], context: &mut Context, ) -> JsResult { - let object = this.as_object(); - let zdt = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("the this object must be a ZonedDateTime object.") - })?; - - let new = zdt.inner.to_plain_date_time(); + let zdt = require_internal_slot!(this, Self, "ZonedDateTime"); + + let new = zdt.borrow().data().inner.to_plain_date_time(); create_temporal_datetime(new, None, context).map(Into::into) } } diff --git a/core/engine/src/builtins/typed_array/builtin.rs b/core/engine/src/builtins/typed_array/builtin.rs index 98a3299e8a2..118e0eeae79 100644 --- a/core/engine/src/builtins/typed_array/builtin.rs +++ b/core/engine/src/builtins/typed_array/builtin.rs @@ -409,17 +409,11 @@ impl BuiltinTypedArray { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]). // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. - let object = this.as_object(); - let ta = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("`this` is not a typed array object") - })?; + let ta = require_internal_slot!(this, TypedArray, "TypedArray"); // 4. Let buffer be O.[[ViewedArrayBuffer]]. // 5. Return buffer. - Ok(ta.viewed_array_buffer().clone().into()) + Ok(ta.borrow().data().viewed_array_buffer().clone().into()) } /// `get %TypedArray%.prototype.byteLength` @@ -432,16 +426,12 @@ impl BuiltinTypedArray { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]). // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. - let object = this.as_object(); - let ta = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("`this` is not a typed array object") - })?; + let ta = require_internal_slot!(this, TypedArray, "TypedArray"); // 4. Let taRecord be MakeTypedArrayWithBufferWitnessRecord(O, seq-cst). - let buf_len = ta + let ta_data = ta.borrow(); + let ta_data = ta_data.data(); + let buf_len = ta_data .viewed_array_buffer() .as_buffer() .bytes(Ordering::SeqCst) @@ -450,7 +440,7 @@ impl BuiltinTypedArray { // 5. Let size be TypedArrayByteLength(taRecord). // 6. Return 𝔽(size). - Ok(ta.byte_length(buf_len).into()) + Ok(ta_data.byte_length(buf_len).into()) } /// `get %TypedArray%.prototype.byteOffset` @@ -463,29 +453,27 @@ impl BuiltinTypedArray { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]). // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. - let object = this.as_object(); - let ta = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("Value is not a typed array object") - })?; + let ta = require_internal_slot!(this, TypedArray, "TypedArray"); // 4. Let taRecord be MakeTypedArrayWithBufferWitnessRecord(O, seq-cst). // 5. If IsTypedArrayOutOfBounds(taRecord) is true, return +0𝔽. - if ta - .viewed_array_buffer() - .as_buffer() - .bytes(Ordering::SeqCst) - .filter(|s| !ta.is_out_of_bounds(s.len())) - .is_none() { - return Ok(0.into()); + let ta_data = ta.borrow(); + let ta_data = ta_data.data(); + if ta_data + .viewed_array_buffer() + .as_buffer() + .bytes(Ordering::SeqCst) + .filter(|s| !ta_data.is_out_of_bounds(s.len())) + .is_none() + { + return Ok(0.into()); + } } // 6. Let offset be O.[[ByteOffset]]. // 7. Return 𝔽(offset). - Ok(ta.byte_offset().into()) + Ok(ta.borrow().data().byte_offset().into()) } /// `%TypedArray%.prototype.copyWithin ( target, start [ , end ] )` @@ -1327,27 +1315,23 @@ impl BuiltinTypedArray { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]). // 3. Assert: O has [[ViewedArrayBuffer]] and [[ArrayLength]] internal slots. - let object = this.as_object(); - let ta = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - JsNativeError::typ().with_message("`this` is not a typed array object") - })?; + let ta = require_internal_slot!(this, TypedArray, "TypedArray"); // 4. Let taRecord be MakeTypedArrayWithBufferWitnessRecord(O, seq-cst). // 5. If IsTypedArrayOutOfBounds(taRecord) is true, return +0𝔽. - let buf = ta.viewed_array_buffer().as_buffer(); + let ta_data = ta.borrow(); + let ta_data = ta_data.data(); + let buf = ta_data.viewed_array_buffer().as_buffer(); let Some(buf) = buf .bytes(Ordering::SeqCst) - .filter(|s| !ta.is_out_of_bounds(s.len())) + .filter(|s| !ta_data.is_out_of_bounds(s.len())) else { return Ok(0.into()); }; // 6. Let length be TypedArrayLength(taRecord). // 7. Return 𝔽(length). - Ok(ta.array_length(buf.len()).into()) + Ok(ta.borrow().data().array_length(buf.len()).into()) } /// `%TypedArray%.prototype.map ( callbackfn [ , thisArg ] )` diff --git a/core/engine/src/builtins/weak/weak_ref.rs b/core/engine/src/builtins/weak/weak_ref.rs index a2bd0c4cde2..cb0bdd921c6 100644 --- a/core/engine/src/builtins/weak/weak_ref.rs +++ b/core/engine/src/builtins/weak/weak_ref.rs @@ -108,15 +108,7 @@ impl WeakRef { pub(crate) fn deref(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { // 1. Let weakRef be the this value. // 2. Perform ? RequireInternalSlot(weakRef, [[WeakRefTarget]]). - let object = this.as_object(); - let weak_ref = object - .as_ref() - .and_then(JsObject::downcast_ref::>) - .ok_or_else(|| { - JsNativeError::typ().with_message( - "WeakRef.prototype.deref: expected `this` to be a `WeakRef` object", - ) - })?; + let weak_ref = require_internal_slot!(this, WeakGc, "WeakRef"); // 3. Return WeakRefDeref(weakRef). @@ -124,7 +116,7 @@ impl WeakRef { // https://tc39.es/ecma262/multipage/managing-memory.html#sec-weakrefderef // 1. Let target be weakRef.[[WeakRefTarget]]. // 2. If target is not empty, then - if let Some(object) = weak_ref.upgrade() { + if let Some(object) = weak_ref.borrow().data().upgrade() { let object = JsObject::from(object); // a. Perform AddToKeptObjects(target). @@ -229,7 +221,7 @@ mod tests { run_test_actions([TestAction::assert_native_error( "WeakRef.prototype.deref.call({})", JsNativeErrorKind::Type, - "WeakRef.prototype.deref: expected `this` to be a `WeakRef` object", + "the this object must be a WeakRef object.", )]); } diff --git a/core/engine/src/builtins/weak_map/mod.rs b/core/engine/src/builtins/weak_map/mod.rs index 5ef5abc8c83..268ce068e17 100644 --- a/core/engine/src/builtins/weak_map/mod.rs +++ b/core/engine/src/builtins/weak_map/mod.rs @@ -175,14 +175,7 @@ impl WeakMap { ) -> JsResult { // 1. Let M be the this value. // 2. Perform ? RequireInternalSlot(M, [[WeakMapData]]). - let object = this.as_object(); - let map = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - js_error!(TypeError: - "WeakMap.prototype.get: expected 'this' to be a WeakMap object") - })?; + let map = require_internal_slot!(this, NativeWeakMap, "WeakMap"); // 3. Let entries be M.[[WeakMapData]]. // 4. If key is not an Object, return undefined. @@ -193,7 +186,7 @@ impl WeakMap { // 5. For each Record { [[Key]], [[Value]] } p of entries, do // a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]]. // 6. Return undefined. - if let Some(entry) = map.get(key.inner()) + if let Some(entry) = map.borrow().data().get(key.inner()) && let Some(val) = entry.value() { Ok(val.clone()) @@ -217,14 +210,7 @@ impl WeakMap { ) -> JsResult { // 1. Let M be the this value. // 2. Perform ? RequireInternalSlot(M, [[WeakMapData]]). - let object = this.as_object(); - let map = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - js_error!(TypeError: - "WeakMap.prototype.has: expected 'this' to be a WeakMap object") - })?; + let map = require_internal_slot!(this, NativeWeakMap, "WeakMap"); // 3. Let entries be M.[[WeakMapData]]. // 4. If key is not an Object, return false. @@ -235,7 +221,7 @@ impl WeakMap { // 5. For each Record { [[Key]], [[Value]] } p of entries, do // a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return true. // 6. Return false. - Ok(map.contains_key(key.inner()).into()) + Ok(map.borrow().data().contains_key(key.inner()).into()) } /// `WeakMap.prototype.set ( key, value )` diff --git a/core/engine/src/builtins/weak_set/mod.rs b/core/engine/src/builtins/weak_set/mod.rs index 7e9575f802f..88058d53719 100644 --- a/core/engine/src/builtins/weak_set/mod.rs +++ b/core/engine/src/builtins/weak_set/mod.rs @@ -232,14 +232,7 @@ impl WeakSet { ) -> JsResult { // 1. Let S be the this value. // 2. Perform ? RequireInternalSlot(S, [[WeakSetData]]). - let object = this.as_object(); - let set = object - .as_ref() - .and_then(JsObject::downcast_ref::) - .ok_or_else(|| { - js_error!(TypeError: - "WeakSet.prototype.has: expected 'this' to be a WeakSet object") - })?; + let set = require_internal_slot!(this, NativeWeakSet, "WeakSet"); // 3. Let entries be the List that is S.[[WeakSetData]]. // 4. If Type(value) is not Object, return false. @@ -251,7 +244,7 @@ impl WeakSet { // 5. For each element e of entries, do // a. If e is not empty and SameValue(e, value) is true, return true. // 6. Return false. - Ok(set.contains_key(value.inner()).into()) + Ok(set.borrow().data().contains_key(value.inner()).into()) } }