@@ -36,15 +36,15 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
3636 inner : TypedArray :: new ( sc, arraybuffer, TypedArrayKind :: Uint8Array ) ,
3737 } )
3838 } ;
39- let wu32be = register_native_fn ( sc, wu32be_sym, |cx| write_byte ( cx, Endianness :: Big , 4 ) ) ;
40- let wu32le = register_native_fn ( sc, wu32le_sym, |cx| write_byte ( cx, Endianness :: Little , 4 ) ) ;
39+ let wu32be = register_native_fn ( sc, wu32be_sym, |cx, scope | write_byte ( cx, scope , Endianness :: Big , 4 ) ) ;
40+ let wu32le = register_native_fn ( sc, wu32le_sym, |cx, scope | write_byte ( cx, scope , Endianness :: Little , 4 ) ) ;
4141 buffer_prototype. set_property ( wu32be_sym. to_key ( sc) , PropertyValue :: static_default ( wu32be. into ( ) ) , sc) ?;
4242 buffer_prototype. set_property ( wu32le_sym. to_key ( sc) , PropertyValue :: static_default ( wu32le. into ( ) ) , sc) ?;
4343
4444 let buffer_ctor = Function :: new (
4545 sc,
4646 Some ( buffer_sym. into ( ) ) ,
47- FunctionKind :: Native ( |cx | throw ! ( cx . scope, Error , "Buffer() constructor unsupported" ) ) ,
47+ FunctionKind :: Native ( |_ , scope | throw ! ( scope, Error , "Buffer() constructor unsupported" ) ) ,
4848 ) ;
4949 buffer_ctor. set_fn_prototype ( buffer_prototype) ;
5050 let buffer_ctor = sc. register ( buffer_ctor) ;
@@ -77,22 +77,27 @@ enum Endianness {
7777 Big ,
7878}
7979
80- fn write_byte ( cx : CallContext , endianness : Endianness , size : usize ) -> Result < Value , Value > {
80+ fn write_byte (
81+ cx : CallContext ,
82+ scope : & mut LocalScope < ' _ > ,
83+ endianness : Endianness ,
84+ size : usize ,
85+ ) -> Result < Value , Value > {
8186 // TODO: can we just merge this with the TypedArray builtin logic?
8287 let Some ( ValueKind :: Number ( Number ( value) ) ) = cx. args . first ( ) . map ( |s| s. unpack ( ) ) else {
83- throw ! ( cx . scope, Error , "Invalid 'value' argument type" )
88+ throw ! ( scope, Error , "Invalid 'value' argument type" )
8489 } ;
8590 let offset = match cx. args . get ( 1 ) . map ( |v| v. unpack ( ) ) {
8691 Some ( ValueKind :: Number ( Number ( n) ) ) => n as usize ,
87- Some ( _) => throw ! ( cx . scope, TypeError , "Invalid 'offset' argument type" ) ,
92+ Some ( _) => throw ! ( scope, TypeError , "Invalid 'offset' argument type" ) ,
8893 None => 0 ,
8994 } ;
9095
91- let buf = receiver_t :: < Buffer > ( cx . scope , & cx. this , "Buffer.prototype.write*" ) ?;
92- let buf = if let Some ( buf) = buf. inner . arraybuffer ( cx . scope ) . storage ( ) . get ( offset..) {
96+ let buf = receiver_t :: < Buffer > ( scope, & cx. this , "Buffer.prototype.write*" ) ?;
97+ let buf = if let Some ( buf) = buf. inner . arraybuffer ( scope) . storage ( ) . get ( offset..) {
9398 buf
9499 } else {
95- throw ! ( cx . scope, Error , "out of range" )
100+ throw ! ( scope, Error , "out of range" )
96101 } ;
97102 let bytes = match ( size, endianness) {
98103 ( 1 , Endianness :: Little ) => & u8:: to_le_bytes ( value as u8 ) as & [ _ ] ,
@@ -140,25 +145,22 @@ impl Object for Buffer {
140145 extract ! ( self , inner) ;
141146}
142147
143- fn from ( cx : CallContext ) -> Result < Value , Value > {
144- let BufferState { buffer_prototype } = State :: from_vm ( cx . scope ) . store [ BufferKey ] ;
148+ fn from ( cx : CallContext , scope : & mut LocalScope < ' _ > ) -> Result < Value , Value > {
149+ let BufferState { buffer_prototype } = State :: from_vm ( scope) . store [ BufferKey ] ;
145150
146- let source = cx
147- . args
148- . first ( )
149- . or_type_err ( cx. scope , "Missing source to `Buffer.from`" ) ?;
151+ let source = cx. args . first ( ) . or_type_err ( scope, "Missing source to `Buffer.from`" ) ?;
150152
151- let length = source. length_of_array_like ( cx . scope ) ?;
153+ let length = source. length_of_array_like ( scope) ?;
152154 let mut buf = Vec :: with_capacity ( length) ;
153155 for i in 0 ..length {
154156 let item = source
155- . get_property ( i. to_key ( cx . scope ) , cx . scope )
156- . root ( cx . scope ) ?
157- . to_number ( cx . scope ) ? as u8 ;
157+ . get_property ( i. to_key ( scope) , scope)
158+ . root ( scope) ?
159+ . to_number ( scope) ? as u8 ;
158160 buf. push ( Cell :: new ( item) ) ;
159161 }
160162
161- let arraybuffer = cx . scope . register ( ArrayBuffer :: from_storage ( cx . scope , buf) ) ;
163+ let arraybuffer = scope. register ( ArrayBuffer :: from_storage ( scope, buf) ) ;
162164 let instn = Buffer {
163165 inner : TypedArray :: with_obj (
164166 arraybuffer,
@@ -167,15 +169,15 @@ fn from(cx: CallContext) -> Result<Value, Value> {
167169 ) ,
168170 } ;
169171
170- Ok ( Value :: object ( cx . scope . register ( instn) ) )
172+ Ok ( Value :: object ( scope. register ( instn) ) )
171173}
172174
173- fn alloc ( cx : CallContext ) -> Result < Value , Value > {
175+ fn alloc ( cx : CallContext , scope : & mut LocalScope < ' _ > ) -> Result < Value , Value > {
174176 let size = cx
175177 . args
176178 . first ( )
177- . or_type_err ( cx . scope , "Missing size argument to `Buffer.alloc`" ) ?;
178- let size = size. to_number ( cx . scope ) ? as usize ;
179+ . or_type_err ( scope, "Missing size argument to `Buffer.alloc`" ) ?;
180+ let size = size. to_number ( scope) ? as usize ;
179181
180182 let fill = cx. args . get ( 1 ) . copied ( ) ;
181183
@@ -184,14 +186,14 @@ fn alloc(cx: CallContext) -> Result<Value, Value> {
184186 if let ValueKind :: Number ( Number ( num) ) = unpacked {
185187 vec ! [ Cell :: new( num as u8 ) ; size]
186188 } else {
187- throw ! ( cx . scope, Error , "invalid fill argument to Buffer.alloc: {:?}" , unpacked)
189+ throw ! ( scope, Error , "invalid fill argument to Buffer.alloc: {:?}" , unpacked)
188190 }
189191 } else {
190192 vec ! [ Cell :: new( 0 ) ; size]
191193 } ;
192194
193- let BufferState { buffer_prototype, .. } = State :: from_vm ( cx . scope ) . store [ BufferKey ] ;
194- let arraybuffer = cx . scope . register ( ArrayBuffer :: from_storage ( cx . scope , buf) ) ;
195+ let BufferState { buffer_prototype, .. } = State :: from_vm ( scope) . store [ BufferKey ] ;
196+ let arraybuffer = scope. register ( ArrayBuffer :: from_storage ( scope, buf) ) ;
195197 let instn = Buffer {
196198 inner : TypedArray :: with_obj (
197199 arraybuffer,
@@ -200,5 +202,5 @@ fn alloc(cx: CallContext) -> Result<Value, Value> {
200202 ) ,
201203 } ;
202204
203- Ok ( Value :: object ( cx . scope . register ( instn) ) )
205+ Ok ( Value :: object ( scope. register ( instn) ) )
204206}
0 commit comments