@@ -5347,6 +5347,40 @@ mod tests {
53475347 assert_eq ! ( context. take_exception( ) . unwrap( ) , Some ( Value :: Int ( 42 ) ) ) ;
53485348 }
53495349
5350+ #[ test]
5351+ fn module_evaluation_caches_error_object_identity_across_contexts ( ) {
5352+ let runtime = Runtime :: new ( ) ;
5353+ let module = {
5354+ let mut compilation_context = runtime. new_context ( ) ;
5355+ compilation_context
5356+ . compile_module ( "throw new Error('cached module error')" )
5357+ . unwrap ( )
5358+ } ;
5359+
5360+ let first_error_id = {
5361+ let mut first_context = runtime. new_context ( ) ;
5362+ assert_eq ! (
5363+ first_context. execute_module( & module) ,
5364+ Err ( RuntimeError :: Exception )
5365+ ) ;
5366+ let Some ( Value :: Object ( error) ) = first_context. take_exception ( ) . unwrap ( ) else {
5367+ panic ! ( "module evaluation did not throw an Error object" ) ;
5368+ } ;
5369+ error. object_id ( )
5370+ } ;
5371+ runtime. run_gc ( ) . unwrap ( ) ;
5372+
5373+ let mut second_context = runtime. new_context ( ) ;
5374+ assert_eq ! (
5375+ second_context. execute_module( & module) ,
5376+ Err ( RuntimeError :: Exception )
5377+ ) ;
5378+ let Some ( Value :: Object ( second_error) ) = second_context. take_exception ( ) . unwrap ( ) else {
5379+ panic ! ( "cached module evaluation did not rethrow an Error object" ) ;
5380+ } ;
5381+ assert_eq ! ( second_error. object_id( ) , first_error_id) ;
5382+ }
5383+
53505384 #[ test]
53515385 fn direct_eval_uses_module_live_cells_without_leaking_eval_var ( ) {
53525386 let runtime = Runtime :: new ( ) ;
@@ -5461,7 +5495,7 @@ mod tests {
54615495 }
54625496
54635497 #[ test]
5464- fn module_handle_roots_compilation_and_first_link_realms ( ) {
5498+ fn cloned_module_handle_roots_compilation_and_first_link_realms ( ) {
54655499 let runtime = Runtime :: new ( ) ;
54665500 let module = {
54675501 let mut context = runtime. new_context ( ) ;
@@ -5470,12 +5504,16 @@ mod tests {
54705504 . unwrap ( )
54715505 } ;
54725506 assert_eq ! ( runtime. heap_counts( ) . context_nodes, 1 ) ;
5507+ let surviving_handle = module. clone ( ) ;
5508+ drop ( module) ;
5509+ runtime. run_gc ( ) . unwrap ( ) ;
5510+ assert_eq ! ( runtime. heap_counts( ) . context_nodes, 1 ) ;
54735511
54745512 {
54755513 let mut link_context = runtime. new_context ( ) ;
54765514 assert_eq ! ( runtime. heap_counts( ) . context_nodes, 2 ) ;
54775515 assert_eq ! (
5478- link_context. execute_module( & module ) . unwrap( ) ,
5516+ link_context. execute_module( & surviving_handle ) . unwrap( ) ,
54795517 Value :: Undefined
54805518 ) ;
54815519 assert_script_true ( & mut link_context, "__rootedModuleRealm === 42" ) ;
@@ -5484,7 +5522,7 @@ mod tests {
54845522 runtime. run_gc ( ) . unwrap ( ) ;
54855523 assert_eq ! ( runtime. heap_counts( ) . context_nodes, 2 ) ;
54865524
5487- drop ( module ) ;
5525+ drop ( surviving_handle ) ;
54885526 runtime. run_gc ( ) . unwrap ( ) ;
54895527 assert_eq ! ( runtime. heap_counts( ) . context_nodes, 0 ) ;
54905528 }
0 commit comments