-
Notifications
You must be signed in to change notification settings - Fork 4
/
genvalacef.py
243 lines (218 loc) · 9.03 KB
/
genvalacef.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os
import sys
from valacefgen.cparser import Parser, Naming
from valacefgen.types import Repository, Function
from valacefgen.utils import TypeInfo
try:
CEF_INCLUDE_DIR = sys.argv[1]
except IndexError:
CEF_INCLUDE_DIR = "/app/include/cef/include"
try:
TOP = sys.argv[2] or '.'
except IndexError:
TOP = '.'
try:
OUT = sys.argv[3] or '.'
except IndexError:
OUT = 'build'
header_files = [
('%s/overrides/cef_primitives.h' % TOP, 'capi/cef_base_capi.h'),
('%s/overrides/cef_base.h' % TOP, 'capi/cef_base_capi.h'),
('%s/overrides/cef_string.h' % TOP, 'capi/cef_base_capi.h'),
('%s/overrides/cef_error_codes.h' % TOP, 'internal/cef_types.h'),
'internal/cef_types_linux.h',
'internal/cef_types.h',
'internal/cef_string_list.h',
'capi/cef_app_capi.h',
'capi/cef_base_capi.h',
'internal/cef_time.h',
'capi/cef_audio_handler_capi.h',
'capi/cef_command_line_capi.h',
'capi/cef_browser_process_handler_capi.h',
'capi/cef_render_process_handler_capi.h',
'capi/cef_resource_bundle_handler_capi.h',
'capi/cef_resource_handler_capi.h',
'capi/cef_resource_request_handler_capi.h',
'capi/cef_request_callback_capi.h',
'capi/cef_urlrequest_capi.h',
'capi/cef_scheme_capi.h',
'capi/cef_request_capi.h',
'capi/cef_browser_capi.h',
'capi/cef_path_util_capi.h',
'capi/cef_client_capi.h',
'capi/cef_dialog_handler_capi.h',
'capi/cef_keyboard_handler_capi.h',
'capi/cef_process_message_capi.h',
'capi/cef_life_span_handler_capi.h',
'capi/cef_load_handler_capi.h',
'capi/cef_drag_handler_capi.h',
'capi/cef_focus_handler_capi.h',
'capi/cef_context_menu_handler_capi.h',
'capi/cef_render_handler_capi.h',
'capi/cef_jsdialog_handler_capi.h',
'capi/cef_request_handler_capi.h',
'capi/cef_download_handler_capi.h',
'capi/cef_find_handler_capi.h',
'capi/cef_display_handler_capi.h',
'capi/cef_frame_capi.h',
'capi/cef_menu_model_capi.h',
'capi/cef_menu_model_delegate_capi.h',
'capi/cef_download_item_capi.h',
'capi/cef_drag_data_capi.h',
'capi/cef_image_capi.h',
'capi/cef_string_visitor_capi.h',
'capi/cef_dom_capi.h',
'capi/cef_v8_capi.h',
'capi/cef_stream_capi.h',
'capi/cef_values_capi.h',
'capi/cef_accessibility_handler_capi.h',
'capi/cef_response_capi.h',
'capi/cef_task_capi.h',
'capi/cef_response_filter_capi.h',
'capi/cef_ssl_info_capi.h',
'capi/cef_auth_callback_capi.h',
'capi/cef_x509_certificate_capi.h',
'capi/cef_request_context_capi.h',
'capi/cef_request_context_handler_capi.h',
'capi/cef_cookie_capi.h',
'capi/cef_web_plugin_capi.h',
'capi/cef_callback_capi.h',
'capi/cef_print_handler_capi.h',
'capi/cef_print_settings_capi.h',
'capi/cef_navigation_entry_capi.h',
'capi/cef_ssl_status_capi.h',
'capi/cef_extension_capi.h',
'capi/cef_extension_handler_capi.h',
]
ignore = {
'XEvent',
'XDisplay',
'cef_get_xdisplay',
'TID_FILE',
}
base_structs = {
"cef_base_scoped_t",
}
base_classes = {
"cef_base_ref_counted_t",
}
class Overrides:
def param__cef_string_utf8_to_utf16__src(self, info: TypeInfo):
info.c_type = 'string'
def param__cef_string_utf8_to_utf16__output(self, info: TypeInfo):
info.ref = True
def param__cef_string_utf16_to_utf8_output(self, info: TypeInfo):
info.ref = True
parser = Parser(Naming('Cef'), Repository('Cef', Overrides()), ignore, base_structs, base_classes)
for entry in header_files:
if isinstance(entry, str):
c_include_path = entry
path = os.path.join(CEF_INCLUDE_DIR, entry)
else:
path, c_include_path = entry
parser.parse_header(path, c_include_path)
repo = parser.repo
ref_func = Function('cef_base_ref_counted_ref', 'ref', "valacef_api.h")
unref_func = Function('cef_base_ref_counted_unref', 'unref', "valacef_api.h")
base_refcounted = repo.structs['cef_base_ref_counted_t']
base_refcounted.add_method(ref_func)
base_refcounted.add_method(unref_func)
base_refcounted.set_ref_counting(ref_func.c_name, unref_func.c_name)
ref_func = Function('cef_base_ref_counted_ref', 'ref', "capi/cef_base_capi.h",
params=[("cef_base_ref_counted_t*", "self")],
body=['self->add_ref(self);', 'return self;'],
ret_type="cef_base_ref_counted_t*")
unref_func = Function('cef_base_ref_counted_unref', 'unref', "capi/cef_base_capi.h",
params=[("cef_base_ref_counted_t*", "self")],
body=['self->release(self);'])
parser.add_c_glue(ref_func, unref_func)
add_ref_func = Function(
'cef_base_ref_counted_add_ref', 'base_ref_counted_add_ref', 'capi/cef_base_capi.h;stdio.h',
params=[('void*', 'self_ptr')],
body=[
'cef_base_ref_counted_t* self = (cef_base_ref_counted_t*) self_ptr;',
'char* pointer = (char*) self + (self->size - (sizeof(int) > sizeof(void*) ? sizeof(int) : sizeof(void*)));',
'volatile int* ref_count = (volatile int*) pointer;',
'// printf("%p++ (%d) size: %d\\n", self, *ref_count + 1, (int) self->size);',
'g_atomic_int_inc(ref_count);',
])
release_ref_func = Function(
'cef_base_ref_counted_release_ref', 'base_ref_counted_release_ref', 'stdlib.h;capi/cef_base_capi.h;stdio.h', 'int',
params=[('void*', 'self_ptr')],
body=[
'gboolean is_dead = FALSE;'
'cef_base_ref_counted_t* self = (cef_base_ref_counted_t*) self_ptr;',
'char* pointer = (char*) self + (self->size - (sizeof(int) > sizeof(void*) ? sizeof(int) : sizeof(void*)));',
'volatile int* ref_count = (volatile int*) pointer;',
'// printf("%p-- (%d) size: %d\\n", self, *ref_count - 1, (int) self->size);',
'is_dead = g_atomic_int_dec_and_test(ref_count);',
'if (is_dead) {',
' // printf("%p dealloc!\\n", self);',
' GData** priv_data = (GData**)(pointer - sizeof(void*));',
' g_datalist_clear(priv_data);',
' free(self_ptr);',
'}',
'return is_dead;'
])
has_one_ref_func = Function(
'cef_base_ref_counted_has_one_ref', 'base_ref_counted_has_one_ref', 'capi/cef_base_capi.h;stdio.h', 'int',
params=[('void*', 'self_ptr')],
body=[
'cef_base_ref_counted_t* self = (cef_base_ref_counted_t*) self_ptr;',
'char* pointer = (char*) self + (self->size - (sizeof(int) > sizeof(void*) ? sizeof(int) : sizeof(void*)));',
'volatile int* ref_count = (volatile int*) pointer;',
'// printf("%p?? %d size: %d\\n", self, *ref_count, (int) self->size);',
'return g_atomic_int_get(ref_count) == 1;',
])
init_refcounting_func = Function(
'cef_base_ref_counted_init_ref_counting', 'init_refcounting', 'capi/cef_base_capi.h;stdio.h',
params=[('void*', 'self_ptr'), ('size_t', 'base_size'), ('size_t', 'derived_size')],
body=[
'cef_base_ref_counted_t* self = (cef_base_ref_counted_t*) self_ptr;',
'self->size = derived_size;',
'self->add_ref = %s;' % add_ref_func.c_name,
'self->release = %s;' % release_ref_func.c_name,
'self->has_one_ref = %s;' % has_one_ref_func.c_name,
'g_assert(base_size + (sizeof(int) > sizeof(void*) ? sizeof(int) : sizeof(void*)) + sizeof(void*) == '
'derived_size);',
'char* pointer = (char*) self + (self->size - (sizeof(int) > sizeof(void*) ? sizeof(int) : sizeof(void*)));',
'volatile int* ref_count = (volatile int*) pointer;',
'g_atomic_int_set(ref_count, 1);',
'// printf("%p=%d size: %d\\n", self, *ref_count, (int) self->size);',
])
parser.add_c_glue(add_ref_func, release_ref_func, has_one_ref_func, init_refcounting_func)
utf16_to_utf8_func = Function(
'cef_utf16_string_to_vala_string', 'get_string', 'capi/cef_base_capi.h;stdio.h', 'char*',
params=[('cef_string_t*', 'utf16_str')],
body=[
'if (utf16_str == NULL) return NULL;',
'cef_string_utf8_t utf8_str = {};',
'cef_string_utf16_to_utf8(utf16_str->str, utf16_str->length, &utf8_str);',
'return utf8_str.str;',
])
parser.add_c_glue(utf16_to_utf8_func)
utf16_to_utf8_func = Function(
'cef_utf16_string_to_vala_string', 'get_string', 'valacef_api.h', 'char*',
params=[('cef_string_t*', 'utf16_str')])
repo.add_function(utf16_to_utf8_func)
utf8_to_utf16_func = Function(
'cef_utf16_string_from_vala_string', 'set_string', 'string.h;capi/cef_base_capi.h;stdio.h',
params=[('cef_string_t*', 'utf16_str'), ('char*', 'str')],
body=[
'cef_string_utf8_to_utf16(str, strlen(str), utf16_str);',
])
parser.add_c_glue(utf8_to_utf16_func)
utf8_to_utf16_func = Function(
'cef_utf16_string_from_vala_string', 'set_string', 'valacef_api.h',
params=[('cef_string_t*', 'utf16_str'), ('char*', 'str')])
repo.add_function(utf8_to_utf16_func)
vapi, vala, c_header, c_glue = parser.finish()
os.makedirs(OUT, exist_ok=True)
with open(OUT + "/valacef_api.vapi", "wt") as f:
f.write(vapi)
with open(OUT + "/cef.vala", "wt") as f:
f.write(vala)
with open(OUT + "/valacef_api.c", "wt") as f:
f.write(c_glue)
with open(OUT + "/valacef_api.h", "wt") as f:
f.write(c_header)