-
Notifications
You must be signed in to change notification settings - Fork 19
/
os-binder-generator.os
218 lines (190 loc) · 6.07 KB
/
os-binder-generator.os
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
var header = <<<===END==='
/*
AUTO-GENERATED FILE. DO NOT MODIFY.
Note: this header is a header template
and must NOT have multiple-inclusion protection.
*/
/******************************************************************************
* Copyright (C) 2012-2014 Evgeniy Golovin (evgeniy.golovin@unitpoint.ru)
*
* Please feel free to contact me at anytime,
* my email is evgeniy.golovin@unitpoint.ru, skype: egolovin
*
* Latest source code: https://github.com/unitpoint/objectscript
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
===END===
var MAX_ARGS = 11
function Buffer.__lshift(b){
this.append(b)
return this
}
var content = Buffer(header) << <<<===END==='
#if OS_BIND_FUNC_NUM_ARGS == 0
#define OS_BIND_FUNC_PARMS_COMMA
#define OS_BIND_FUNC_TEMPLATE_PARMS
#define OS_BIND_FUNC_PARMS
#define OS_BIND_FUNC_ARGS
#define OS_BIND_FUNC_GET_ARGS
===END===
function file_get_contents(filename)
{
var f = File(filename, "rb")
var content = f.read()
f.close()
return content
}
function file_put_contents(filename, content)
{
var f = File(filename, "wb")
f.write(content)
f.close()
}
function get_template_parms(i)
{
var items = []
for(var j = 1; j <= i; j++){
items[] = "class ARG_TYPE_${j}"
}
return items.join(", ")
}
function get_parms(i)
{
var items = []
for(var j = 1; j <= i; j++){
items[] = "ARG_TYPE_${j}"
}
return items.join(", ")
}
function get_args(i)
{
var items = []
for(var j = 1; j <= i; j++){
items[] = "arg${j}"
}
return items.join(", ")
}
function get_get_args(i)
{
var items = []
for(var j = 1; j <= i; j++){
items[] = "OS_GET_TEMPLATE_ARG(${j}, ARG_TYPE_${j})"
}
return " \\\n\tint cur_param_offs = -params; \\\n\t"..items.join("; \\\n\t")
}
for(var i = 1; i <= MAX_ARGS; i++){
content
<< "#elif OS_BIND_FUNC_NUM_ARGS == ${i}\n\n"
<< "#define OS_BIND_FUNC_PARMS_COMMA ,\n"
<< "#define OS_BIND_FUNC_TEMPLATE_PARMS ${get_template_parms(i)}\n"
<< "#define OS_BIND_FUNC_PARMS ${get_parms(i)}\n"
<< "#define OS_BIND_FUNC_ARGS ${get_args(i)}\n"
<< "#define OS_BIND_FUNC_GET_ARGS ${get_get_args(i)}\n\n"
}
content << "#endif\n\n"
var cc_types = ["cdecl", "stdcall", "fastcall", "thiscall"]
for(var _, class_name in [
"OS_FunctionClassImpConst",
"OS_FunctionClassImp",
"OS_FunctionImp",
]){
for(var i, cc in cc_types){
content
<< (i > 0 ? "#elif defined " : "#ifdef ")
<< "OS_BIND_FUNC_${cc.upper()}\n\n"
<< "#ifdef __GNUC__\n"
<< "#define OS_BIND_FUNC_CC\n"
<< "#define OS_BIND_FUNC_CC_GNUC __attribute__((${cc}))\n"
<< "#else\n"
<< "#define OS_BIND_FUNC_CC __${cc}\n"
<< "#define OS_BIND_FUNC_CC_GNUC\n"
<< "#endif\n\n"
for(var j = 0; j <= MAX_ARGS; j++){
content
<< (j > 0 ? "#elif " : "#if ") << "OS_BIND_FUNC_NUM_ARGS == ${j}\n"
<< "#define OS_BIND_FUNC_CLASS_NAME ${class_name}${j} ## _${cc}\n"
<< "#define OS_BIND_FUNC_RUN_CLASS_NAME ${class_name}${j}_run ## _${cc}\n"
}
content << "#endif\n\n"
}
if(true){
content
<< "#else\n\n"
<< "#define OS_BIND_FUNC_CC\n"
<< "#define OS_BIND_FUNC_CC_GNUC\n\n"
for(var j = 0; j <= MAX_ARGS; j++){
content
<< (j > 0 ? "#elif " : "#if ") << "OS_BIND_FUNC_NUM_ARGS == ${j}\n"
<< "#define OS_BIND_FUNC_CLASS_NAME ${class_name}${j}\n"
<< "#define OS_BIND_FUNC_RUN_CLASS_NAME ${class_name}${j}_run\n"
}
content << "#endif\n\n"
}
content << "#endif\n\n"
if(class_name == "OS_FunctionClassImpConst"){
impl = file_get_contents("os-binder-FunctionClassImp.tpl")
.trim()
.replace("{const}", "const")
}else if(class_name == "OS_FunctionClassImp"){
impl = file_get_contents("os-binder-FunctionClassImp.tpl")
.trim()
.replace("{const}", "")
}else{
impl = "#if OS_BIND_FUNC_NUM_ARGS > 0\n\n"
.. file_get_contents("os-binder-FunctionImp.tpl").trim()
.. "\n\n#else\n\n"
.. file_get_contents("os-binder-FunctionImpVoid.tpl").trim()
.. "\n\n#endif\n\n"
}
content
<< "${impl}\n\n"
<< "#undef OS_BIND_FUNC_CLASS_NAME\n"
<< "#undef OS_BIND_FUNC_RUN_CLASS_NAME\n"
<< "#undef OS_BIND_FUNC_CC\n\n"
<< "#undef OS_BIND_FUNC_CC_GNUC\n\n"
}
content
<< "#undef OS_BIND_FUNC_PARMS_COMMA\n"
<< "#undef OS_BIND_FUNC_TEMPLATE_PARMS\n"
<< "#undef OS_BIND_FUNC_PARMS\n"
<< "#undef OS_BIND_FUNC_ARGS\n"
<< "#undef OS_BIND_FUNC_GET_ARGS\n"
file_put_contents("os-binder-function.h", content)
content.clear() << header
<< "#if defined __GNUC__ || !(defined(__i386__) || defined(__i386) || defined(__X86__))\n"
<< "#include \"os-binder-function.h\"\n"
<< "#else\n\n"
for(var _, cc in cc_types){
content
<< "#define OS_BIND_FUNC_${cc.upper()}\n"
<< "#include \"os-binder-function.h\"\n"
<< "#undef OS_BIND_FUNC_${cc.upper()}\n\n"
}
content << "#endif\n"
file_put_contents("os-binder-cc-functions.h", content)
content.clear() << header
for(var i = 0; i <= MAX_ARGS; i++){
content
<< "#define OS_BIND_FUNC_NUM_ARGS ${i}\n"
<< "#include \"os-binder-cc-functions.h\"\n"
<< "#undef OS_BIND_FUNC_NUM_ARGS\n\n"
}
file_put_contents("os-binder-arg-cc-functions.h", content)