@@ -5,6 +5,7 @@ use dash_proc_macro::Trace;
5
5
use dash_rt:: state:: State ;
6
6
use dash_rt:: typemap:: Key ;
7
7
use dash_vm:: gc:: ObjectId ;
8
+ use dash_vm:: js_std:: receiver_t;
8
9
use dash_vm:: localscope:: LocalScope ;
9
10
use dash_vm:: value:: arraybuffer:: ArrayBuffer ;
10
11
use dash_vm:: value:: function:: native:: { CallContext , register_native_fn} ;
@@ -13,7 +14,7 @@ use dash_vm::value::object::{NamedObject, Object, PropertyValue};
13
14
use dash_vm:: value:: ops:: conversions:: ValueConversion ;
14
15
use dash_vm:: value:: primitive:: Number ;
15
16
use dash_vm:: value:: typedarray:: { TypedArray , TypedArrayKind } ;
16
- use dash_vm:: value:: { Root , Unpack , Value , ValueKind } ;
17
+ use dash_vm:: value:: { ExceptionContext , Root , Unpack , Value , ValueKind } ;
17
18
use dash_vm:: { delegate, extract, throw} ;
18
19
19
20
use crate :: state:: state_mut;
@@ -80,10 +81,7 @@ fn write_byte(cx: CallContext, endianness: Endianness, size: usize) -> Result<Va
80
81
None => 0 ,
81
82
} ;
82
83
83
- let this = cx. this . unpack ( ) ;
84
- let Some ( buf) = this. downcast_ref :: < Buffer > ( cx. scope ) else {
85
- throw ! ( cx. scope, TypeError , "Incompatible Buffer.write* receiver" )
86
- } ;
84
+ let buf = receiver_t :: < Buffer > ( cx. scope , & cx. this , "Buffer.prototype.write*" ) ?;
87
85
let buf = if let Some ( buf) = buf. inner . arraybuffer ( cx. scope ) . storage ( ) . get ( offset..) {
88
86
buf
89
87
} else {
@@ -142,9 +140,10 @@ fn from(cx: CallContext) -> Result<Value, Value> {
142
140
buffer_ctor,
143
141
} = State :: from_vm ( cx. scope ) . store [ BufferKey ] ;
144
142
145
- let Some ( source) = cx. args . first ( ) else {
146
- throw ! ( cx. scope, Error , "Missing source to `Buffer.from`" )
147
- } ;
143
+ let source = cx
144
+ . args
145
+ . first ( )
146
+ . or_type_err ( cx. scope , "Missing source to `Buffer.from`" ) ?;
148
147
149
148
let length = source. length_of_array_like ( cx. scope ) ?;
150
149
let mut buf = Vec :: with_capacity ( length) ;
@@ -170,9 +169,10 @@ fn from(cx: CallContext) -> Result<Value, Value> {
170
169
}
171
170
172
171
fn alloc ( cx : CallContext ) -> Result < Value , Value > {
173
- let Some ( size) = cx. args . first ( ) else {
174
- throw ! ( cx. scope, Error , "Missing size argument to `Buffer.alloc`" )
175
- } ;
172
+ let size = cx
173
+ . args
174
+ . first ( )
175
+ . or_type_err ( cx. scope , "Missing size argument to `Buffer.alloc`" ) ?;
176
176
let size = size. to_number ( cx. scope ) ? as usize ;
177
177
178
178
let fill = cx. args . get ( 1 ) . copied ( ) ;
0 commit comments