@@ -366,6 +366,31 @@ pub struct ErrorContext {
366366}
367367
368368impl 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+
369394 #[ doc( hidden) ]
370395 pub fn from_handle ( handle : u32 ) -> Self {
371396 Self { handle }
@@ -380,49 +405,52 @@ impl ErrorContext {
380405 pub fn debug_message ( & self ) -> String {
381406 #[ cfg( not( target_arch = "wasm32" ) ) ]
382407 {
383- _ = self ;
384- unreachable ! ( ) ;
408+ String :: from ( "<no debug message on native hosts>" )
385409 }
386410
387411 #[ cfg( target_arch = "wasm32" ) ]
388412 {
413+ #[ repr( C ) ]
414+ struct RetPtr {
415+ ptr : * mut u8 ,
416+ len : usize ,
417+ }
389418 #[ link( wasm_import_module = "$root" ) ]
390419 extern "C" {
391420 #[ 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 ) ;
393422 }
394423
395424 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 )
400431 }
401432 }
402433 }
403434}
404435
405436impl Debug for ErrorContext {
406437 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 ( )
408441 }
409442}
410443
411444impl Display for ErrorContext {
412445 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
413- write ! ( f , "Error" )
446+ Display :: fmt ( & self . debug_message ( ) , f )
414447 }
415448}
416449
417450impl std:: error:: Error for ErrorContext { }
418451
419452impl Drop for ErrorContext {
420453 fn drop ( & mut self ) {
421- #[ cfg( not( target_arch = "wasm32" ) ) ]
422- {
423- unreachable ! ( ) ;
424- }
425-
426454 #[ cfg( target_arch = "wasm32" ) ]
427455 {
428456 #[ link( wasm_import_module = "$root" ) ]
@@ -539,28 +567,3 @@ pub fn task_backpressure(enabled: bool) {
539567 }
540568 }
541569}
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