-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyJsHelper.h
executable file
·86 lines (77 loc) · 2.22 KB
/
MyJsHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Jsvalue_helper.h
*
* Created on: 2018年3月31日
* Author: tom
*/
#ifndef MYJSHELPER_H_
#define MYJSHELPER_H_
#include <webkit2/webkit2.h>
#include <JavaScriptCore/JavaScript.h>
gchar * my_js_value_to_gchar(JSContextRef ctx,JSValueRef value){
if(!JSValueIsString(ctx,value))return NULL;
JSStringRef js_str=JSValueToStringCopy(ctx,value,NULL);
gsize size=JSStringGetMaximumUTF8CStringSize(js_str);
gchar *str=g_malloc(size);
JSStringGetUTF8CString(js_str,str,size);
JSStringRelease(js_str);
return str;
};
JSValueRef my_js_evaluate(JSGlobalContextRef ctx,JSObjectRef this_obj,gchar *script){
if(script==NULL)return NULL;
JSStringRef js_str=JSStringCreateWithUTF8CString(script);
JSValueRef js_res=JSEvaluateScript(ctx,js_str,this_obj,NULL,1,NULL);
JSStringRelease(js_str);
return js_res;
}
JSValueRef *my_js_obj_method(JSGlobalContextRef ctx,JSObjectRef object,gchar *method){
JSValueRef func,result;
JSStringRef str;
JSObjectRef func_obj;
str=JSStringCreateWithUTF8CString(method);
func=JSObjectGetProperty(ctx,object,str,NULL);
JSStringRelease(str);
func_obj=JSValueToObject(ctx,func,NULL);
result =JSObjectCallAsFunction(ctx,func_obj,object,0,NULL,NULL);
return result;
}
JSValueRef *my_js_obj_method_args(JSGlobalContextRef ctx,JSObjectRef object,gchar *method,...){
JSValueRef func,result,*args;
JSStringRef str;
JSObjectRef func_obj;
gchar *arg=NULL;
guint n=0,i;
va_list p;
va_start(p,method);
arg=va_arg(p,gchar*);
while(arg!=NULL){
n++;
arg=va_arg(p,gchar*);
}
args=g_malloc(n*sizeof(JSValueRef));
i=0;
va_start(p,method);
while(i<n){
arg=va_arg(p,gchar*);
str=JSStringCreateWithUTF8CString(arg);
args[i]=JSValueMakeString(ctx,str);
JSStringRelease(str);
i++;
}
str=JSStringCreateWithUTF8CString(method);
func=JSObjectGetProperty(ctx,object,str,NULL);
JSStringRelease(str);
func_obj=JSValueToObject(ctx,func,NULL);
result =JSObjectCallAsFunction(ctx,func_obj,object,n,args,NULL);
va_end(p);
g_free(args);
return result;
}
JSValueRef *my_js_obj_get_prop(JSGlobalContextRef ctx,JSObjectRef obj,gchar *prop){
JSValueRef value;
JSStringRef str=JSStringCreateWithUTF8CString(prop);
value=JSObjectGetProperty(ctx,obj,str,NULL);
JSStringRelease(str);
return value;
}
#endif /* MYJSHELPER_H_ */