@@ -366,6 +366,31 @@ pub struct ErrorContext {
366
366
}
367
367
368
368
impl ErrorContext {
369
+ /// Call the `error-context.new` canonical built-in function.
370
+ pub fn new ( debug_message : & str ) -> ErrorContext {
371
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
372
+ {
373
+ _ = debug_message;
374
+ unreachable ! ( ) ;
375
+ }
376
+
377
+ #[ cfg( target_arch = "wasm32" ) ]
378
+ {
379
+ #[ link( wasm_import_module = "$root" ) ]
380
+ extern "C" {
381
+ #[ link_name = "[error-context-new;encoding=utf8]" ]
382
+ fn context_new ( _: * const u8 , _: usize ) -> i32 ;
383
+ }
384
+
385
+ unsafe {
386
+ let handle = context_new ( debug_message. as_ptr ( ) , debug_message. len ( ) ) ;
387
+ // SAFETY: Handles (including error context handles are guaranteed to
388
+ // fit inside u32 by the Component Model ABI
389
+ ErrorContext :: from_handle ( u32:: try_from ( handle) . unwrap ( ) )
390
+ }
391
+ }
392
+ }
393
+
369
394
#[ doc( hidden) ]
370
395
pub fn from_handle ( handle : u32 ) -> Self {
371
396
Self { handle }
@@ -380,49 +405,52 @@ impl ErrorContext {
380
405
pub fn debug_message ( & self ) -> String {
381
406
#[ cfg( not( target_arch = "wasm32" ) ) ]
382
407
{
383
- _ = self ;
384
- unreachable ! ( ) ;
408
+ String :: from ( "<no debug message on native hosts>" )
385
409
}
386
410
387
411
#[ cfg( target_arch = "wasm32" ) ]
388
412
{
413
+ #[ repr( C ) ]
414
+ struct RetPtr {
415
+ ptr : * mut u8 ,
416
+ len : usize ,
417
+ }
389
418
#[ link( wasm_import_module = "$root" ) ]
390
419
extern "C" {
391
420
#[ link_name = "[error-context-debug-message;encoding=utf8;realloc=cabi_realloc]" ]
392
- fn error_context_debug_message ( _: u32 , _: * mut u8 ) ;
421
+ fn error_context_debug_message ( _: u32 , _: & mut RetPtr ) ;
393
422
}
394
423
395
424
unsafe {
396
- let mut ret = [ 0u32 ; 2 ] ;
397
- error_context_debug_message ( self . handle , ret. as_mut_ptr ( ) as * mut _ ) ;
398
- let len = usize:: try_from ( ret[ 1 ] ) . unwrap ( ) ;
399
- String :: from_raw_parts ( usize:: try_from ( ret[ 0 ] ) . unwrap ( ) as * mut _ , len, len)
425
+ let mut ret = RetPtr {
426
+ ptr : ptr:: null_mut ( ) ,
427
+ len : 0 ,
428
+ } ;
429
+ error_context_debug_message ( self . handle , & mut ret) ;
430
+ String :: from_raw_parts ( ret. ptr , ret. len , ret. len )
400
431
}
401
432
}
402
433
}
403
434
}
404
435
405
436
impl Debug for ErrorContext {
406
437
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
407
- f. debug_struct ( "ErrorContext" ) . finish ( )
438
+ f. debug_struct ( "ErrorContext" )
439
+ . field ( "debug_message" , & self . debug_message ( ) )
440
+ . finish ( )
408
441
}
409
442
}
410
443
411
444
impl Display for ErrorContext {
412
445
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
413
- write ! ( f , "Error" )
446
+ Display :: fmt ( & self . debug_message ( ) , f )
414
447
}
415
448
}
416
449
417
450
impl std:: error:: Error for ErrorContext { }
418
451
419
452
impl Drop for ErrorContext {
420
453
fn drop ( & mut self ) {
421
- #[ cfg( not( target_arch = "wasm32" ) ) ]
422
- {
423
- unreachable ! ( ) ;
424
- }
425
-
426
454
#[ cfg( target_arch = "wasm32" ) ]
427
455
{
428
456
#[ link( wasm_import_module = "$root" ) ]
@@ -539,28 +567,3 @@ pub fn task_backpressure(enabled: bool) {
539
567
}
540
568
}
541
569
}
542
-
543
- /// Call the `error-context.new` canonical built-in function.
544
- pub fn error_context_new ( debug_message : & str ) -> ErrorContext {
545
- #[ cfg( not( target_arch = "wasm32" ) ) ]
546
- {
547
- _ = debug_message;
548
- unreachable ! ( ) ;
549
- }
550
-
551
- #[ cfg( target_arch = "wasm32" ) ]
552
- {
553
- #[ link( wasm_import_module = "$root" ) ]
554
- extern "C" {
555
- #[ link_name = "[error-context-new;encoding=utf8]" ]
556
- fn context_new ( _: * const u8 , _: usize ) -> i32 ;
557
- }
558
-
559
- unsafe {
560
- let handle = context_new ( debug_message. as_ptr ( ) , debug_message. len ( ) ) ;
561
- // SAFETY: Handles (including error context handles are guaranteed to
562
- // fit inside u32 by the Component Model ABI
563
- ErrorContext :: from_handle ( u32:: try_from ( handle) . unwrap ( ) )
564
- }
565
- }
566
- }
0 commit comments