This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathultralight.c
158 lines (136 loc) · 4.33 KB
/
ultralight.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <ultralight.h>
char * printParams(JSContextRef ctx, JSValueRef *params, size_t count) {
if (count == 0) {
return "";
}
size_t destLen = 0;
char delimeter[4] = "\t\t\t";
int i;
for (i = 0; i < count; i++) {
if (JSValueIsString(ctx, *params)) {
JSStringRef strOut = JSValueToStringCopy(ctx, (params)[i], NULL);
destLen += (JSStringGetLength(strOut)+1);
if (i != (count-1)) {
destLen += sizeof(delimeter);
}
}
}
char output[destLen];
for (i = 0; i < count; i++) {
if (JSValueIsString(ctx, *params)) {
JSStringRef strOut = JSValueToStringCopy(ctx, (params)[i], NULL);
int length = (JSStringGetLength(strOut)+1);
char value[length];
JSStringGetUTF8CString(strOut, value, length);
if (i == 0) {
sprintf(output, "%s", value);
} else {
sprintf(output + strlen(output), "%s", value);
}
if (i != (count-1)) {
sprintf(output + strlen(output), "%s", delimeter);
}
} else {
printf("Not a string, have %d\n", (JSType)JSValueGetType(ctx, *params));
}
}
char * out = malloc(destLen * sizeof(char));
for (i = 0; i < destLen; i++) {
out[i] = output[i];
}
return out;
}
void setAppUpdateCallback(ULApp app) {
ulAppSetUpdateCallback(app, appUpdateFunction, NULL);
}
void setViewChangeTitleCallback(ULView view) {
ulViewSetChangeTitleCallback(view, viewChangeTitleFunction, NULL);
}
void setViewChangeURLCallback(ULView view) {
ulViewSetChangeURLCallback(view, viewChangeURLFunction, NULL);
}
void setViewChangeTooltipCallback(ULView view) {
ulViewSetChangeTooltipCallback(view, viewChangeTooltipFunction, NULL);
}
void setViewChangeCursorCallback(ULView view) {
ulViewSetChangeCursorCallback(view, viewChangeCursorFunction, NULL);
}
void setViewAddConsoleMessageCallback(ULView view) {
ulViewSetAddConsoleMessageCallback(view, viewAddConsoleMessageFunction, NULL);
}
void setViewBeginLoadingCallback(ULView view) {
ulViewSetBeginLoadingCallback(view, viewBeginLoadingFunction, NULL);
}
void setViewFinishLoadingCallback(ULView view) {
ulViewSetFinishLoadingCallback(view, viewFinishLoadingFunction, NULL);
}
void setViewUpdateHistoryCallback(ULView view) {
ulViewSetUpdateHistoryCallback(view, viewUpdateHistoryFunction, NULL);
}
void setViewDOMReadyCallback(ULView view) {
ulViewSetDOMReadyCallback(view, viewDOMReadyFunction, NULL);
}
void setWinCloseCallback(ULWindow win) {
ulWindowSetCloseCallback(win, winCloseFunction, NULL);
}
void setWinResizeCallback(ULWindow win) {
ulWindowSetResizeCallback(win, winResizeFunction, NULL);
}
char * strconv(ULString str) {
if (ulStringGetLength(str) == 0) {
return NULL;
}
size_t len = ulStringGetLength(str);
ULChar16 *val = ulStringGetData(str);
char string[len];
int i;
for (i = 0; i < len; i++) {
if (i == 0) {
sprintf(string, "%c", (char)*val & 0x00FF);
} else {
sprintf(string + strlen(string), "%c", (char)*val & 0x00FF);
}
val++;
}
// Required to be accessible from Go
char *num = malloc(len);
for (i = 0; i < len; i++) {
num[i] = string[i];
}
return num;
}
char * evaluateScript(ULView view, ULString script) {
const JSStringRef values = JSValueToStringCopy(ulViewGetJSContext(view), ulViewEvaluateScript(view, script), NULL);
int length = (JSStringGetLength(values)+1);
char value[length];
JSStringGetUTF8CString(values, value, sizeof(value));
// Required to be accessible from Go
char *out = malloc(length * sizeof(char));
int i;
for (i = 0; i < length; i++) {
out[i] = value[i];
}
return out;
}
JSObjectRef bindScript(ULView view, char* name) {
JSContextRef ctx = ulViewGetJSContext(view);
JSObjectRef func = JSObjectMakeFunctionWithCallback(ctx, NULL, (JSObjectCallAsFunctionCallback)objFunctionCallback);
JSObjectSetProperty(ctx, JSContextGetGlobalObject(ctx), JSStringCreateWithUTF8CString(name), func, 0, NULL);
return func;
}
JSValueRef makeJSValueString(ULView view, char * val) {
return JSValueMakeString(ulViewGetJSContext(view), JSStringCreateWithUTF8CString(val));
}
JSValueRef makeJSValueBool(ULView view, bool val) {
return JSValueMakeBoolean(ulViewGetJSContext(view), val);
}
JSValueRef makeJSValueNum(ULView view, double val) {
return JSValueMakeNumber(ulViewGetJSContext(view), val);
}
JSValueRef makeJSValueJSON(ULView view, char * val) {
JSValueRef res = JSValueMakeFromJSONString(ulViewGetJSContext(view), JSStringCreateWithUTF8CString(val));
if (res == NULL) {
return NULL;
}
return res;
}