@@ -542,6 +542,38 @@ impl Proxy {
542542 }
543543}
544544
545+ pub fn get_proxy_instance_proxy_and_instance_id_q (
546+ q_ctx : & QuickJsRealmAdapter ,
547+ obj : & JSValueRef ,
548+ ) -> Option < ( Rc < Proxy > , usize ) > {
549+ if !is_proxy_instance_q ( q_ctx, obj) {
550+ None
551+ } else {
552+ let info = get_proxy_instance_info ( obj. borrow_value ( ) ) ;
553+ let cn = info. class_name . as_str ( ) ;
554+ let registry = & * q_ctx. proxy_registry . borrow ( ) ;
555+ if let Some ( proxy) = registry. get ( cn) . cloned ( ) {
556+ Some ( ( proxy, info. id ) )
557+ } else {
558+ None
559+ }
560+ }
561+ }
562+
563+ pub fn is_proxy_instance_q ( q_ctx : & QuickJsRealmAdapter , obj : & JSValueRef ) -> bool {
564+ unsafe { is_proxy_instance ( q_ctx. context , obj) }
565+ }
566+
567+ pub unsafe fn is_proxy_instance ( ctx : * mut q:: JSContext , obj : & JSValueRef ) -> bool {
568+ if !obj. is_object ( ) {
569+ false
570+ } else {
571+ let class_id = PROXY_INSTANCE_CLASS_ID . with ( |rc| * rc. borrow ( ) ) ;
572+ let proxy_class_proto: q:: JSValue = q:: JS_GetClassProto ( ctx, class_id) ;
573+ q:: JS_IsInstanceOf ( ctx, * obj. borrow_value ( ) , proxy_class_proto) != 0
574+ }
575+ }
576+
545577pub fn new_instance2 (
546578 proxy : & Proxy ,
547579 q_ctx : & QuickJsRealmAdapter ,
@@ -686,8 +718,8 @@ unsafe extern "C" fn constructor(
686718
687719pub ( crate ) struct ProxyInstanceInfo {
688720 id : usize ,
689- class_name : String , // todo use unsafe to make these &str?
690- context_id : String , // todo use unsafe to make these &str?
721+ class_name : String , // todo, store all proxies in an autoidmap with a usize as key and store proxy_class_id here instead of string
722+ context_id : String , // todo store all context ids in an autoidmap with a usize as key and store context_id here instead of string
691723}
692724
693725fn get_proxy_instance_info ( val : & q:: JSValue ) -> & ProxyInstanceInfo {
@@ -1219,7 +1251,9 @@ unsafe extern "C" fn proxy_instance_set_prop(
12191251pub mod tests {
12201252 use crate :: facades:: tests:: init_test_rt;
12211253 use crate :: quickjs_utils:: { functions, primitives} ;
1222- use crate :: reflection:: Proxy ;
1254+ use crate :: reflection:: {
1255+ get_proxy_instance_proxy_and_instance_id_q, is_proxy_instance_q, Proxy ,
1256+ } ;
12231257 use hirofa_utils:: js_utils:: JsError ;
12241258 use hirofa_utils:: js_utils:: Script ;
12251259 use log:: trace;
@@ -1278,6 +1312,33 @@ pub mod tests {
12781312 assert ! ( err. contains( "cant run" ) ) ;
12791313 }
12801314
1315+ #[ test]
1316+ pub fn test_proxy_instanceof ( ) {
1317+ log:: info!( "> test_proxy_instanceof" ) ;
1318+
1319+ let rt = init_test_rt ( ) ;
1320+ rt. exe_rt_task_in_event_loop ( |q_js_rt| {
1321+ q_js_rt. gc ( ) ;
1322+ let q_ctx = q_js_rt. get_main_context ( ) ;
1323+ let _ = Proxy :: new ( )
1324+ . constructor ( |_q_ctx, _id, _args| Ok ( ( ) ) )
1325+ . namespace ( vec ! [ "com" , "company" ] )
1326+ . name ( "Test" )
1327+ . install ( q_ctx, true ) ;
1328+ let res = q_ctx
1329+ . eval ( Script :: new ( "test_tostring.es" , "new com.company.Test()" ) )
1330+ . ok ( )
1331+ . expect ( "script failed" ) ;
1332+ assert ! ( is_proxy_instance_q( q_ctx, & res) ) ;
1333+ let info = get_proxy_instance_proxy_and_instance_id_q ( q_ctx, & res)
1334+ . expect ( "could not get info" ) ;
1335+ let id = info. 1 ;
1336+ let p = info. 0 ;
1337+ println ! ( "id={}" , id) ;
1338+ assert_eq ! ( p. get_class_name( ) . as_str( ) , "com.company.Test" ) ;
1339+ } ) ;
1340+ }
1341+
12811342 #[ test]
12821343 pub fn test_to_string ( ) {
12831344 log:: info!( "> test_proxy" ) ;
0 commit comments