@@ -258,6 +258,7 @@ fn native_callable_export_arity_reference(module: &str, prop: &str) -> Option<u3
258258 Some ( 1 )
259259 }
260260 ( "tls" , "checkServerIdentity" ) => Some ( 2 ) ,
261+ ( "tls" , "convertALPNProtocols" ) => Some ( 2 ) ,
261262 ( "tls" , "SecureContext" ) => Some ( 1 ) ,
262263 // #3726: `crypto.Cipheriv` / `crypto.Decipheriv` constructor exports —
263264 // `(cipher, key, iv, options)` arity matches Node's length 4.
@@ -1396,6 +1397,135 @@ fn attach_tls_secure_context_prototype(constructor_value: f64) {
13961397 crate :: tls:: attach_secure_context_constructor_prototype ( constructor_value) ;
13971398}
13981399
1400+ const TLS_SOCKET_PROTOTYPE_METHODS : & [ ( & str , u32 ) ] = & [
1401+ ( "setKeyCert" , 1 ) ,
1402+ ( "getSharedSigalgs" , 0 ) ,
1403+ ( "getX509Certificate" , 0 ) ,
1404+ ( "getPeerX509Certificate" , 1 ) ,
1405+ ] ;
1406+
1407+ thread_local ! {
1408+ static TLS_DERIVED_PROTOTYPES : RefCell <Vec <( u64 , u8 ) >> = const { RefCell :: new( Vec :: new( ) ) } ;
1409+ }
1410+
1411+ const TLS_PARENT_EVENT_EMITTER : u8 = 1 ;
1412+ const TLS_PARENT_DUPLEX : u8 = 2 ;
1413+
1414+ pub ( crate ) fn scan_tls_derived_prototype_roots_mut (
1415+ visitor : & mut crate :: gc:: RuntimeRootVisitor < ' _ > ,
1416+ ) {
1417+ TLS_DERIVED_PROTOTYPES . with ( |prototypes| {
1418+ for ( bits, _) in prototypes. borrow_mut ( ) . iter_mut ( ) {
1419+ visitor. visit_nanbox_u64_slot ( bits) ;
1420+ }
1421+ } ) ;
1422+ }
1423+
1424+ extern "C" fn tls_prototype_method_thunk (
1425+ closure : * const crate :: closure:: ClosureHeader ,
1426+ rest : f64 ,
1427+ ) -> f64 {
1428+ unsafe {
1429+ let name_ptr = crate :: closure:: js_closure_get_capture_ptr ( closure, 0 ) as * const i8 ;
1430+ let name_len = crate :: closure:: js_closure_get_capture_ptr ( closure, 1 ) as usize ;
1431+ let receiver = crate :: object:: js_implicit_this_get ( ) ;
1432+ let args_array = crate :: value:: js_nanbox_get_pointer ( rest) ;
1433+ crate :: object:: js_native_call_method_apply ( receiver, name_ptr, name_len, args_array)
1434+ }
1435+ }
1436+
1437+ fn attach_tls_constructor_prototype ( constructor_value : f64 , constructor_name : & str ) {
1438+ let methods = if constructor_name == "TLSSocket" {
1439+ TLS_SOCKET_PROTOTYPE_METHODS
1440+ } else {
1441+ & [ ]
1442+ } ;
1443+ let constructor_js = JSValue :: from_bits ( constructor_value. to_bits ( ) ) ;
1444+ if !constructor_js. is_pointer ( ) {
1445+ return ;
1446+ }
1447+ let constructor = constructor_js. as_pointer :: < crate :: closure:: ClosureHeader > ( ) as usize ;
1448+ if constructor == 0 {
1449+ return ;
1450+ }
1451+
1452+ let prototype = js_object_alloc ( 0 , 0 ) ;
1453+ if prototype. is_null ( ) {
1454+ return ;
1455+ }
1456+ let constructor_key =
1457+ crate :: string:: js_string_from_bytes ( b"constructor" . as_ptr ( ) , "constructor" . len ( ) as u32 ) ;
1458+ js_object_set_field_by_name ( prototype, constructor_key, constructor_value) ;
1459+ super :: super :: set_builtin_property_attrs (
1460+ prototype as usize ,
1461+ "constructor" . to_string ( ) ,
1462+ super :: super :: PropertyAttrs :: new ( true , false , true ) ,
1463+ ) ;
1464+
1465+ let thunk = tls_prototype_method_thunk as * const u8 ;
1466+ crate :: closure:: js_register_closure_rest ( thunk, 0 ) ;
1467+ for & ( name, length) in methods {
1468+ let method = crate :: closure:: js_closure_alloc ( thunk, 2 ) ;
1469+ if method. is_null ( ) {
1470+ continue ;
1471+ }
1472+ crate :: closure:: js_closure_set_capture_ptr ( method, 0 , name. as_ptr ( ) as i64 ) ;
1473+ crate :: closure:: js_closure_set_capture_ptr ( method, 1 , name. len ( ) as i64 ) ;
1474+ set_bound_native_closure_name ( method, name) ;
1475+ set_builtin_closure_length ( method as usize , length) ;
1476+ let key = crate :: string:: js_string_from_bytes ( name. as_ptr ( ) , name. len ( ) as u32 ) ;
1477+ js_object_set_field_by_name (
1478+ prototype,
1479+ key,
1480+ crate :: value:: js_nanbox_pointer ( method as i64 ) ,
1481+ ) ;
1482+ super :: super :: set_builtin_property_attrs (
1483+ prototype as usize ,
1484+ name. to_string ( ) ,
1485+ super :: super :: PropertyAttrs :: new ( true , false , true ) ,
1486+ ) ;
1487+ }
1488+
1489+ crate :: closure:: closure_set_dynamic_prop (
1490+ constructor,
1491+ "prototype" ,
1492+ crate :: value:: js_nanbox_pointer ( prototype as i64 ) ,
1493+ ) ;
1494+ let parent_kind = match constructor_name {
1495+ "Server" => TLS_PARENT_EVENT_EMITTER ,
1496+ "TLSSocket" => TLS_PARENT_DUPLEX ,
1497+ _ => 0 ,
1498+ } ;
1499+ if parent_kind != 0 {
1500+ let bits = crate :: value:: js_nanbox_pointer ( prototype as i64 ) . to_bits ( ) ;
1501+ TLS_DERIVED_PROTOTYPES . with ( |prototypes| {
1502+ let mut prototypes = prototypes. borrow_mut ( ) ;
1503+ if !prototypes. iter ( ) . any ( |( existing, _) | * existing == bits) {
1504+ prototypes. push ( ( bits, parent_kind) ) ;
1505+ }
1506+ } ) ;
1507+ }
1508+ super :: super :: set_builtin_property_attrs (
1509+ constructor,
1510+ "prototype" . to_string ( ) ,
1511+ super :: super :: PropertyAttrs :: new ( false , false , false ) ,
1512+ ) ;
1513+ }
1514+
1515+ pub ( crate ) fn tls_constructor_prototype_is_instance_of ( value : f64 , parent_name : & str ) -> bool {
1516+ let parent_kind = match parent_name {
1517+ "EventEmitter" => TLS_PARENT_EVENT_EMITTER ,
1518+ "Duplex" => TLS_PARENT_DUPLEX ,
1519+ _ => return false ,
1520+ } ;
1521+ TLS_DERIVED_PROTOTYPES . with ( |prototypes| {
1522+ prototypes
1523+ . borrow ( )
1524+ . iter ( )
1525+ . any ( |( bits, kind) | * bits == value. to_bits ( ) && * kind == parent_kind)
1526+ } )
1527+ }
1528+
13991529pub ( crate ) unsafe fn bound_native_callable_module_and_method (
14001530 value : f64 ,
14011531) -> Option < ( String , String ) > {
@@ -1574,6 +1704,8 @@ pub(crate) unsafe fn nm_attach_tls(
15741704) -> f64 {
15751705 if property_name == "SecureContext" {
15761706 attach_tls_secure_context_prototype ( value) ;
1707+ } else if matches ! ( property_name, "Server" | "TLSSocket" ) {
1708+ attach_tls_constructor_prototype ( value, property_name) ;
15771709 }
15781710 value
15791711}
@@ -2103,6 +2235,7 @@ static CALLABLE_EXPORT_ARITY_TABLE: &[(&str, &[(&str, u32)])] = &[
21032235 ( "TLSSocket" , 2 ) ,
21042236 ( "checkServerIdentity" , 2 ) ,
21052237 ( "connect" , 4 ) ,
2238+ ( "convertALPNProtocols" , 2 ) ,
21062239 ( "createSecureContext" , 1 ) ,
21072240 ( "createServer" , 2 ) ,
21082241 ( "getCACertificates" , 1 ) ,
@@ -2220,4 +2353,50 @@ mod callable_export_arity_table_tests {
22202353 }
22212354 }
22222355 }
2356+
2357+ #[ test]
2358+ fn tls_constructor_prototypes_match_node_parent_classes ( ) {
2359+ let server = bound_native_callable_export_value ( "tls" , "Server" ) ;
2360+ let server_addr = ( server. to_bits ( ) & crate :: value:: POINTER_MASK ) as usize ;
2361+ let server_proto = crate :: closure:: closure_get_dynamic_prop ( server_addr, "prototype" ) ;
2362+ assert ! ( tls_constructor_prototype_is_instance_of(
2363+ server_proto,
2364+ "EventEmitter"
2365+ ) ) ;
2366+ assert_eq ! (
2367+ crate :: object:: js_instanceof( server_proto, 0xFFFF_0076 ) . to_bits( ) ,
2368+ crate :: value:: TAG_TRUE
2369+ ) ;
2370+ let event_emitter = bound_native_callable_export_value ( "events" , "EventEmitter" ) ;
2371+ assert_eq ! (
2372+ crate :: object:: js_instanceof_dynamic( server_proto, event_emitter) . to_bits( ) ,
2373+ crate :: value:: TAG_TRUE
2374+ ) ;
2375+
2376+ let socket = bound_native_callable_export_value ( "tls" , "TLSSocket" ) ;
2377+ let socket_addr = ( socket. to_bits ( ) & crate :: value:: POINTER_MASK ) as usize ;
2378+ let socket_proto = crate :: closure:: closure_get_dynamic_prop ( socket_addr, "prototype" ) ;
2379+ assert ! ( tls_constructor_prototype_is_instance_of(
2380+ socket_proto,
2381+ "Duplex"
2382+ ) ) ;
2383+ let socket_proto_obj =
2384+ JSValue :: from_bits ( socket_proto. to_bits ( ) ) . as_pointer :: < ObjectHeader > ( ) ;
2385+ for & ( name, length) in TLS_SOCKET_PROTOTYPE_METHODS {
2386+ let key = crate :: string:: js_string_from_bytes ( name. as_ptr ( ) , name. len ( ) as u32 ) ;
2387+ let method = crate :: object:: js_object_get_field_by_name ( socket_proto_obj, key) ;
2388+ let method_addr = method. as_pointer :: < crate :: closure:: ClosureHeader > ( ) as usize ;
2389+ assert ! ( crate :: closure:: is_closure_ptr( method_addr) , "{name}" ) ;
2390+ assert_eq ! ( builtin_closure_length( method_addr) , Some ( length) , "{name}" ) ;
2391+ }
2392+ assert_eq ! (
2393+ crate :: object:: js_instanceof( socket_proto, 0xFFFF_0073 ) . to_bits( ) ,
2394+ crate :: value:: TAG_TRUE
2395+ ) ;
2396+ let duplex = bound_native_callable_export_value ( "stream" , "Duplex" ) ;
2397+ assert_eq ! (
2398+ crate :: object:: js_instanceof_dynamic( socket_proto, duplex) . to_bits( ) ,
2399+ crate :: value:: TAG_TRUE
2400+ ) ;
2401+ }
22232402}
0 commit comments