@@ -47,15 +47,11 @@ pub enum NonstandardIncoming {
47
47
expr : Box < ast:: IncomingBindingExpression > ,
48
48
} ,
49
49
50
- /// JS is passing a typed slice of data into Rust. Currently this is
51
- /// implemented with a deallocation in the JS shim, hence a custom binding.
52
- ///
53
- /// TODO: we should move deallocation into Rust so we can use a vanilla and
54
- /// standard webidl binding here.
55
- Slice {
50
+ /// A mutable slice of values going from JS to Rust, and after Rust finishes
51
+ /// the JS slice is updated with the current value of the slice.
52
+ MutableSlice {
56
53
kind : VectorKind ,
57
54
val : ast:: IncomingBindingExpression ,
58
- mutable : bool ,
59
55
} ,
60
56
61
57
/// This is either a slice or `undefined` being passed into Rust.
@@ -216,52 +212,11 @@ impl IncomingBuilder {
216
212
Descriptor :: Option ( d) => self . process_option ( d) ?,
217
213
218
214
Descriptor :: String | Descriptor :: Vector ( _) => {
219
- use wasm_webidl_bindings:: ast:: WebidlScalarType :: * ;
220
-
221
215
let kind = arg. vector_kind ( ) . ok_or_else ( || {
222
216
format_err ! ( "unsupported argument type for calling Rust function from JS {:?}" , arg)
223
217
} ) ? ;
224
218
self . wasm . extend ( & [ ValType :: I32 ; 2 ] ) ;
225
- match kind {
226
- VectorKind :: I8 => self . alloc_copy ( Int8Array ) ,
227
- VectorKind :: U8 => self . alloc_copy ( Uint8Array ) ,
228
- VectorKind :: ClampedU8 => self . alloc_copy ( Uint8ClampedArray ) ,
229
- VectorKind :: I16 => self . alloc_copy ( Int16Array ) ,
230
- VectorKind :: U16 => self . alloc_copy ( Uint16Array ) ,
231
- VectorKind :: I32 => self . alloc_copy ( Int32Array ) ,
232
- VectorKind :: U32 => self . alloc_copy ( Uint32Array ) ,
233
- VectorKind :: F32 => self . alloc_copy ( Float32Array ) ,
234
- VectorKind :: F64 => self . alloc_copy ( Float64Array ) ,
235
- VectorKind :: String => {
236
- let expr = ast:: IncomingBindingExpressionAllocUtf8Str {
237
- alloc_func_name : self . alloc_func_name ( ) ,
238
- expr : Box :: new ( self . expr_get ( ) ) ,
239
- } ;
240
- self . webidl . push ( DomString ) ;
241
- self . bindings
242
- . push ( NonstandardIncoming :: Standard ( expr. into ( ) ) ) ;
243
- }
244
- VectorKind :: I64 | VectorKind :: U64 => {
245
- let signed = match kind {
246
- VectorKind :: I64 => true ,
247
- _ => false ,
248
- } ;
249
- self . bindings . push ( NonstandardIncoming :: AllocCopyInt64 {
250
- alloc_func_name : self . alloc_func_name ( ) ,
251
- expr : Box :: new ( self . expr_get ( ) ) ,
252
- signed,
253
- } ) ;
254
- self . webidl . push ( Any ) ;
255
- }
256
- VectorKind :: Anyref => {
257
- self . bindings
258
- . push ( NonstandardIncoming :: AllocCopyAnyrefArray {
259
- alloc_func_name : self . alloc_func_name ( ) ,
260
- expr : Box :: new ( self . expr_get ( ) ) ,
261
- } ) ;
262
- self . webidl . push ( Any ) ;
263
- }
264
- }
219
+ self . alloc_copy_kind ( kind)
265
220
}
266
221
267
222
// Can't be passed from JS to Rust yet
@@ -309,12 +264,15 @@ impl IncomingBuilder {
309
264
)
310
265
} ) ?;
311
266
self . wasm . extend ( & [ ValType :: I32 ; 2 ] ) ;
312
- self . bindings . push ( NonstandardIncoming :: Slice {
313
- kind,
314
- val : self . expr_get ( ) ,
315
- mutable,
316
- } ) ;
317
- self . webidl . push ( ast:: WebidlScalarType :: Any ) ;
267
+ if mutable {
268
+ self . bindings . push ( NonstandardIncoming :: MutableSlice {
269
+ kind,
270
+ val : self . expr_get ( ) ,
271
+ } ) ;
272
+ self . webidl . push ( ast:: WebidlScalarType :: Any ) ;
273
+ } else {
274
+ self . alloc_copy_kind ( kind)
275
+ }
318
276
}
319
277
_ => bail ! (
320
278
"unsupported reference argument type for calling Rust function from JS: {:?}" ,
@@ -445,6 +403,51 @@ impl IncomingBuilder {
445
403
"__wbindgen_malloc" . to_string ( )
446
404
}
447
405
406
+ fn alloc_copy_kind ( & mut self , kind : VectorKind ) {
407
+ use wasm_webidl_bindings:: ast:: WebidlScalarType :: * ;
408
+
409
+ match kind {
410
+ VectorKind :: I8 => self . alloc_copy ( Int8Array ) ,
411
+ VectorKind :: U8 => self . alloc_copy ( Uint8Array ) ,
412
+ VectorKind :: ClampedU8 => self . alloc_copy ( Uint8ClampedArray ) ,
413
+ VectorKind :: I16 => self . alloc_copy ( Int16Array ) ,
414
+ VectorKind :: U16 => self . alloc_copy ( Uint16Array ) ,
415
+ VectorKind :: I32 => self . alloc_copy ( Int32Array ) ,
416
+ VectorKind :: U32 => self . alloc_copy ( Uint32Array ) ,
417
+ VectorKind :: F32 => self . alloc_copy ( Float32Array ) ,
418
+ VectorKind :: F64 => self . alloc_copy ( Float64Array ) ,
419
+ VectorKind :: String => {
420
+ let expr = ast:: IncomingBindingExpressionAllocUtf8Str {
421
+ alloc_func_name : self . alloc_func_name ( ) ,
422
+ expr : Box :: new ( self . expr_get ( ) ) ,
423
+ } ;
424
+ self . webidl . push ( DomString ) ;
425
+ self . bindings
426
+ . push ( NonstandardIncoming :: Standard ( expr. into ( ) ) ) ;
427
+ }
428
+ VectorKind :: I64 | VectorKind :: U64 => {
429
+ let signed = match kind {
430
+ VectorKind :: I64 => true ,
431
+ _ => false ,
432
+ } ;
433
+ self . bindings . push ( NonstandardIncoming :: AllocCopyInt64 {
434
+ alloc_func_name : self . alloc_func_name ( ) ,
435
+ expr : Box :: new ( self . expr_get ( ) ) ,
436
+ signed,
437
+ } ) ;
438
+ self . webidl . push ( Any ) ;
439
+ }
440
+ VectorKind :: Anyref => {
441
+ self . bindings
442
+ . push ( NonstandardIncoming :: AllocCopyAnyrefArray {
443
+ alloc_func_name : self . alloc_func_name ( ) ,
444
+ expr : Box :: new ( self . expr_get ( ) ) ,
445
+ } ) ;
446
+ self . webidl . push ( Any ) ;
447
+ }
448
+ }
449
+ }
450
+
448
451
fn alloc_copy ( & mut self , webidl : ast:: WebidlScalarType ) {
449
452
let expr = ast:: IncomingBindingExpressionAllocCopy {
450
453
alloc_func_name : self . alloc_func_name ( ) ,
0 commit comments