Skip to content

Commit 4c4edd1

Browse files
committed
refactor: extract this-type-check boilerplate into require_internal_slot! macro
Replace the repeated as_object/downcast_ref/ok_or_else boilerplate with a single require_internal_slot! macro defined in builtins/mod.rs. The macro produces two let-bindings in the caller's scope: one for the owned JsObject (to keep it alive) and one for the downcast Ref<T>. Uses the js_error! macro internally for error construction. Temporal modules (163 replacements): - zoneddatetime, plain_date_time, plain_time, plain_year_month, plain_date, duration, instant, plain_month_day Other builtins (32 replacements): - date (9), dataview (5), typed_array (4), array_buffer (4), shared_array_buffer (3), weak_map (2), intl/segmenter (2), intl/collator (1), weak_set (1), weak_ref (1)
1 parent 66a1cd2 commit 4c4edd1

File tree

21 files changed

+272
-1479
lines changed

21 files changed

+272
-1479
lines changed

core/engine/src/builtins/array_buffer/mod.rs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,7 @@ impl ArrayBuffer {
496496
// 1. Let O be the this value.
497497
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
498498
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
499-
let object = this.as_object();
500-
let buf = object
501-
.as_ref()
502-
.and_then(JsObject::downcast_ref::<Self>)
503-
.ok_or_else(|| {
504-
JsNativeError::typ()
505-
.with_message("get ArrayBuffer.prototype.byteLength called with invalid `this`")
506-
})?;
499+
require_internal_slot!(buf = this, Self, "ArrayBuffer");
507500

508501
// 4. If IsDetachedBuffer(O) is true, return +0𝔽.
509502
// 5. Let length be O.[[ArrayBufferByteLength]].
@@ -522,15 +515,7 @@ impl ArrayBuffer {
522515
// 1. Let O be the this value.
523516
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
524517
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
525-
let object = this.as_object();
526-
let buf = object
527-
.as_ref()
528-
.and_then(JsObject::downcast_ref::<Self>)
529-
.ok_or_else(|| {
530-
JsNativeError::typ().with_message(
531-
"get ArrayBuffer.prototype.maxByteLength called with invalid `this`",
532-
)
533-
})?;
518+
require_internal_slot!(buf = this, Self, "ArrayBuffer");
534519

535520
// 4. If IsDetachedBuffer(O) is true, return +0𝔽.
536521
let Some(data) = buf.bytes() else {
@@ -556,14 +541,7 @@ impl ArrayBuffer {
556541
// 1. Let O be the this value.
557542
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
558543
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
559-
let object = this.as_object();
560-
let buf = object
561-
.as_ref()
562-
.and_then(JsObject::downcast_ref::<Self>)
563-
.ok_or_else(|| {
564-
JsNativeError::typ()
565-
.with_message("get ArrayBuffer.prototype.resizable called with invalid `this`")
566-
})?;
544+
require_internal_slot!(buf = this, Self, "ArrayBuffer");
567545

568546
// 4. If IsFixedLengthArrayBuffer(O) is false, return true; otherwise return false.
569547
Ok(JsValue::from(!buf.is_fixed_len()))
@@ -581,14 +559,7 @@ impl ArrayBuffer {
581559
// 1. Let O be the this value.
582560
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
583561
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
584-
let object = this.as_object();
585-
let buf = object
586-
.as_ref()
587-
.and_then(JsObject::downcast_ref::<Self>)
588-
.ok_or_else(|| {
589-
JsNativeError::typ()
590-
.with_message("get ArrayBuffer.prototype.detached called with invalid `this`")
591-
})?;
562+
require_internal_slot!(buf = this, Self, "ArrayBuffer");
592563

593564
// 4. Return IsDetachedBuffer(O).
594565
Ok(buf.is_detached().into())

core/engine/src/builtins/array_buffer/shared.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,7 @@ impl SharedArrayBuffer {
225225
// 1. Let O be the this value.
226226
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
227227
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
228-
let object = this.as_object();
229-
let buf = object
230-
.as_ref()
231-
.and_then(JsObject::downcast_ref::<Self>)
232-
.ok_or_else(|| {
233-
JsNativeError::typ()
234-
.with_message("SharedArrayBuffer.byteLength called with invalid value")
235-
})?;
228+
require_internal_slot!(buf = this, Self, "SharedArrayBuffer");
236229

237230
// 4. Let length be ArrayBufferByteLength(O, seq-cst).
238231
let len = buf.bytes(Ordering::SeqCst).len() as u64;
@@ -252,14 +245,7 @@ impl SharedArrayBuffer {
252245
// 1. Let O be the this value.
253246
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
254247
// 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
255-
let object = this.as_object();
256-
let buf = object
257-
.as_ref()
258-
.and_then(JsObject::downcast_ref::<Self>)
259-
.ok_or_else(|| {
260-
JsNativeError::typ()
261-
.with_message("get SharedArrayBuffer.growable called with invalid `this`")
262-
})?;
248+
require_internal_slot!(buf = this, Self, "SharedArrayBuffer");
263249

264250
// 4. If IsFixedLengthArrayBuffer(O) is false, return true; otherwise return false.
265251
Ok(JsValue::from(!buf.is_fixed_len()))
@@ -276,14 +262,7 @@ impl SharedArrayBuffer {
276262
// 1. Let O be the this value.
277263
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
278264
// 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
279-
let object = this.as_object();
280-
let buf = object
281-
.as_ref()
282-
.and_then(JsObject::downcast_ref::<Self>)
283-
.ok_or_else(|| {
284-
JsNativeError::typ()
285-
.with_message("get SharedArrayBuffer.maxByteLength called with invalid value")
286-
})?;
265+
require_internal_slot!(buf = this, Self, "SharedArrayBuffer");
287266

288267
// 4. If IsFixedLengthArrayBuffer(O) is true, then
289268
// a. Let length be O.[[ArrayBufferByteLength]].

core/engine/src/builtins/dataview/mod.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,7 @@ impl DataView {
331331
) -> JsResult<JsValue> {
332332
// 1. Let O be the this value.
333333
// 2. Perform ? RequireInternalSlot(O, [[DataView]]).
334-
let object = this.as_object();
335-
let view = object
336-
.as_ref()
337-
.and_then(JsObject::downcast_ref::<Self>)
338-
.ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?;
334+
require_internal_slot!(view = this, Self, "DataView");
339335
// 3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
340336
// 4. Let buffer be O.[[ViewedArrayBuffer]].
341337
let buffer = view.viewed_array_buffer.clone();
@@ -361,11 +357,7 @@ impl DataView {
361357
// 1. Let O be the this value.
362358
// 2. Perform ? RequireInternalSlot(O, [[DataView]]).
363359
// 3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
364-
let object = this.as_object();
365-
let view = object
366-
.as_ref()
367-
.and_then(JsObject::downcast_ref::<Self>)
368-
.ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?;
360+
require_internal_slot!(view = this, Self, "DataView");
369361

370362
// 4. Let viewRecord be MakeDataViewWithBufferWitnessRecord(O, seq-cst).
371363
// 5. If IsViewOutOfBounds(viewRecord) is true, throw a TypeError exception.
@@ -404,11 +396,7 @@ impl DataView {
404396
) -> JsResult<JsValue> {
405397
// 1. Let O be the this value.
406398
// 2. Perform ? RequireInternalSlot(O, [[DataView]]).
407-
let object = this.as_object();
408-
let view = object
409-
.as_ref()
410-
.and_then(JsObject::downcast_ref::<Self>)
411-
.ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?;
399+
require_internal_slot!(view = this, Self, "DataView");
412400

413401
// 3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
414402
let buffer = view.viewed_array_buffer.as_buffer();
@@ -448,11 +436,7 @@ impl DataView {
448436
) -> JsResult<JsValue> {
449437
// 1. Perform ? RequireInternalSlot(view, [[DataView]]).
450438
// 2. Assert: view has a [[ViewedArrayBuffer]] internal slot.
451-
let object = view.as_object();
452-
let view = object
453-
.as_ref()
454-
.and_then(JsObject::downcast_ref::<Self>)
455-
.ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?;
439+
require_internal_slot!(view = view, Self, "DataView");
456440

457441
// 3. Let getIndex be ? ToIndex(requestIndex).
458442
let get_index = request_index.to_index(context)?;
@@ -790,11 +774,7 @@ impl DataView {
790774
) -> JsResult<JsValue> {
791775
// 1. Perform ? RequireInternalSlot(view, [[DataView]]).
792776
// 2. Assert: view has a [[ViewedArrayBuffer]] internal slot.
793-
let object = view.as_object();
794-
let view = object
795-
.as_ref()
796-
.and_then(JsObject::downcast_ref::<Self>)
797-
.ok_or_else(|| JsNativeError::typ().with_message("`this` is not a DataView"))?;
777+
require_internal_slot!(view = view, Self, "DataView");
798778

799779
// 3. Let getIndex be ? ToIndex(requestIndex).
800780
let get_index = request_index.to_index(context)?;

0 commit comments

Comments
 (0)