1+ #include "../lib/imports.h"
2+ #include "../utils.h"
3+ #include "aot.h"
4+
5+ JSValue getAOTObject (JSContext * ctx , const char * type , int count , void * va_list ){
6+ JSValue array = JS_NewArray (ctx );
7+
8+ int offset = ((int * )va_list )[6 ];
9+
10+ const char * * list = va_list + offset ;
11+ const char * * overflow_list = ((const char * * * )va_list )[0 ];
12+
13+ int c = - offset / 8 ;
14+
15+ int first_count = count ;
16+ if (first_count > c ) first_count = c ;
17+
18+ for (uint32_t i = 0 ; i < first_count ; ++ i ){
19+ const char * str = list [i ];
20+ JS_DefinePropertyValueUint32 (ctx , array , i , JS_NewStringLen (ctx , str , strlen (str )), 7 );
21+ }
22+
23+ if (count > 5 ){
24+ for (uint32_t i = 0 ; i < count - c ; ++ i ){
25+ const char * str = overflow_list [i ];
26+ JS_DefinePropertyValueUint32 (ctx , array , i + c , JS_NewStringLen (ctx , str , strlen (str )), 7 );
27+ }
28+ }
29+
30+ JSValue aot_object = get_aot_object (ctx , count , va_list );
31+
32+ JSValue object = JS_NewObject (ctx );
33+
34+ JS_SetPropertyStr (ctx , object , "type" , JS_NewStringLen (ctx , type , strlen (type )));
35+ JS_SetPropertyStr (ctx , object , "object" , aot_object );
36+
37+ JSValue joinstr = JS_NewStringLen (ctx , "." , 1 );
38+ JSValue path = js_array_join (ctx , array , 1 , & joinstr , 0 );
39+
40+ JS_SetPropertyStr (ctx , object , "path" , path );
41+
42+ emitChowloaderEventValue (ctx , "aot_object" , object );
43+
44+ return aot_object ;
45+ }
46+
47+ JSValue hookJSAOT (JSContext * ctx , int count , void * va_list ){
48+ return getAOTObject (ctx , "jsaot" , count , va_list );
49+ }
50+
51+ JSValue hookJSVAL (JSContext * ctx , int count , void * va_list ){
52+ return getAOTObject (ctx , "jsval" , count , va_list );
53+ }
54+
55+ JSValue hookJSVARREF (JSContext * ctx , int count , void * va_list ){
56+ return getAOTObject (ctx , "jsval" , count , va_list );
57+ }
58+
59+ void initAOT (JSContext * ctx ){
60+ emitChowloaderEvent (ctx , "omori_loaded" );
61+ init_aot (ctx );
62+ emitChowloaderEvent (ctx , "aot_loaded" );
63+ }
64+
65+ // AOT Patching
66+
67+ JSValue createAOTObject (JSContext * ctx ){
68+ JSValue aot = JS_NewObject (ctx );
69+
70+ JSValue _findJSVALNative = JS_NewCFunction2 (ctx , & findJSVALNative , "find" , 1 , JS_CFUNC_generic , 0 );
71+ JS_SetPropertyStr (ctx , aot , "find" , _findJSVALNative );
72+
73+ JSValue _patchJSVALNative = JS_NewCFunction2 (ctx , & patchJSVALNative , "patch" , 1 , JS_CFUNC_generic , 0 );
74+ JS_SetPropertyStr (ctx , aot , "patch" , _patchJSVALNative );
75+
76+ return aot ;
77+ }
78+
79+ JSValue JSVALOffset = 0 ;
80+
81+ JSValue findJSVALNative (JSContext * ctx , JSValueConst this_val , int argc , JSValueConst * argv ){
82+ JSValue * offset = & JSVALOffset ;
83+
84+ for (size_t i = 0 ; i < JS_VALUE_GET_UINT (argv [0 ]); i ++ ){
85+ if (offset [i ] == argv [1 ]){
86+ return JS_MKVAL (JS_TAG_INT , (uintptr_t )(offset + i ));
87+ }
88+ }
89+
90+ return JS_MKVAL (JS_TAG_INT , 0xFFFFFFFF );
91+ }
92+
93+ JSValue patchJSVALNative (JSContext * ctx , JSValueConst this_val , int argc , JSValueConst * argv ){
94+ JSValue * val = (JSValue * )JS_VALUE_GET_UINT (argv [0 ]);
95+ val [0 ] = argv [1 ];
96+ return JS_TRUE ;
97+ }
0 commit comments