-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateBindings.ts
349 lines (289 loc) · 10 KB
/
GenerateBindings.ts
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import * as fs from "fs";
import ConvertFunctionReturnType from "./FunctionReturnType";
import wrappedFunctions from "./GenerateWrappedFunction";
import convert_info_arg from "./InfoArgs";
import { RaylibAPI, RaylibFunction, RaylibStruct } from "./JSONInterface";
import convert_napi_object_property from "./ObjectProperty";
let wrapped_functions = `
Napi::Object WrapUpdateCamera3D(const Napi::CallbackInfo& info) {
Camera3D params = Camera3DFromNAPIObject(info[0].As<Napi::Object>());
UpdateCamera(
¶ms
);
Napi::Object out = Napi::Object::New(info.Env());
out.Set("position", Vector3ToNAPIObject(info.Env(), params.position));
out.Set("target", Vector3ToNAPIObject(info.Env(), params.target));
out.Set("up", Vector3ToNAPIObject(info.Env(), params.up));
out.Set("fovy", params.fovy);
out.Set("projection", params.projection);
return out;
}
void SetShaderFloat(const Napi::CallbackInfo& info) {
float value = info[2].As<Napi::Number>();
SetShaderValueV(
ShaderFromNAPIObject(info[0].As<Napi::Object>()),
info[1].As<Napi::Number>(),
&value,
SHADER_UNIFORM_FLOAT,
1
);
}
void SetShaderInt(const Napi::CallbackInfo& info) {
int value = info[2].As<Napi::Number>();
SetShaderValueV(
ShaderFromNAPIObject(info[0].As<Napi::Object>()),
info[1].As<Napi::Number>(),
&value,
SHADER_UNIFORM_INT,
1
);
}
void SetShaderVector2(const Napi::CallbackInfo& info) {
Vector2 value = Vector2FromNAPIObject(info[2].As<Napi::Object>());
const float fvalue[2] = { value.x, value.y };
SetShaderValueV(
ShaderFromNAPIObject(info[0].As<Napi::Object>()),
info[1].As<Napi::Number>(),
&fvalue,
SHADER_UNIFORM_VEC2,
1
);
}
void SetShaderVector3(const Napi::CallbackInfo& info) {
Vector3 value = Vector3FromNAPIObject(info[2].As<Napi::Object>());
const float fvalue[3] = { value.x, value.y, value.z };
SetShaderValueV(
ShaderFromNAPIObject(info[0].As<Napi::Object>()),
info[1].As<Napi::Number>(),
&fvalue,
SHADER_UNIFORM_VEC3,
1
);
}
void SetShaderVector4(const Napi::CallbackInfo& info) {
Vector4 value = Vector4FromNAPIObject(info[2].As<Napi::Object>());
const float fvalue[4] = { value.x, value.y, value.z, value.w };
SetShaderValueV(
ShaderFromNAPIObject(info[0].As<Napi::Object>()),
info[1].As<Napi::Number>(),
&fvalue,
SHADER_UNIFORM_VEC4,
1
);
}
void SetShaderSampler2D(const Napi::CallbackInfo& info) {
Texture value = TextureFromNAPIObject(info[2].As<Napi::Object>());
SetShaderValueV(
ShaderFromNAPIObject(info[0].As<Napi::Object>()),
info[1].As<Napi::Number>(),
&value,
SHADER_UNIFORM_SAMPLER2D,
1
);
}
`
let wrapped_function_bindings = `
exports.Set(
Napi::String::New(env, "SetShaderFloat"),
Napi::Function::New(env, SetShaderFloat)
);
exports.Set(
Napi::String::New(env, "SetShaderInt"),
Napi::Function::New(env, SetShaderInt)
);
exports.Set(
Napi::String::New(env, "SetShaderVector2"),
Napi::Function::New(env, SetShaderVector2)
);
exports.Set(
Napi::String::New(env, "SetShaderVector3"),
Napi::Function::New(env, SetShaderVector3)
);
exports.Set(
Napi::String::New(env, "SetShaderVector4"),
Napi::Function::New(env, SetShaderVector4)
);
exports.Set(
Napi::String::New(env, "SetShaderSampler2D"),
Napi::Function::New(env, SetShaderSampler2D)
);
exports.Set(
Napi::String::New(env, "UpdateCamera3D"),
Napi::Function::New(env, WrapUpdateCamera3D)
);
`
let tab = ` `
let br = "\n"
let constructors = [
'Vector2',
'Vector3',
'Vector4',
'Matrix',
'Color',
'Rectangle',
'NPatchInfo',
'Camera2D',
'Camera3D',
'Transform',
'Ray'
]
function GenerateStructConverters(struct: RaylibStruct) {
if (struct.name == 'CharInfo') struct.name = 'GlyphInfo'
// Generate internally used Cpp deserializer for this struct
let from_napi_object = ''
from_napi_object += `${struct.name} ${struct.name}FromNAPIObject(Napi::Object obj) {` + br
from_napi_object += tab + `${struct.name} out = ${struct.name}();` + br
for (let property of struct.fields) {
from_napi_object += tab + `out.${property.name} = ` + convert_napi_object_property(property.name, property.type) + br
}
from_napi_object += tab + 'return out;' + br
from_napi_object += '}' + br + br
// Generate exported constructor function for this struct
let to_napi_object = ''
if (true) {
let constructor_name = `Bind${struct.name}`
to_napi_object += `Napi::Object ${constructor_name}(const Napi::CallbackInfo& info) {` + br
//to_napi_object += tab + `${struct.name} object = ${struct.name}();` + br
to_napi_object += tab + 'Napi::Object out = Napi::Object::New(info.Env());' + br
for (let index = 0; index < struct.fields.length; index++) {
const field = struct.fields[index]
//to_napi_object += tab + `object.${field.name} = ` + convert_info_arg(index, field.type) + br
to_napi_object += tab + `out.Set("${field.name}", info[${index}].As<Napi::Object>());` + br
}
to_napi_object += tab + 'return out;' + br
to_napi_object += '}' + br + br
}
let napi_object_constructor = ''
napi_object_constructor += `Napi::Object ${struct.name}ToNAPIObject(napi_env env, ${struct.name} object) {` + br
napi_object_constructor += tab + 'Napi::Object out = Napi::Object::New(env);' + br
for (let index = 0; index < struct.fields.length; index++) {
const field = struct.fields[index]
if (field.type[0].toUpperCase() == field.type[0]) {
if (field.type.endsWith('*')) {
napi_object_constructor += tab + `out.Set("${field.name}", (int64_t)object.${field.name});` + br
} else {
napi_object_constructor += tab + `out.Set("${field.name}", ${field.type}ToNAPIObject(env, object.${field.name}));` + br
}
} else if (field.type.endsWith('*')) {
napi_object_constructor += tab + `out.Set("${field.name}", (int64_t)object.${field.name});` + br
} else {
napi_object_constructor += tab + `out.Set("${field.name}", object.${field.name});` + br
}
}
napi_object_constructor += tab + 'return out;' + br
napi_object_constructor += '}' + br + br
return from_napi_object + to_napi_object + napi_object_constructor
}
function GenerateFunctionBinding(func: RaylibFunction, structs: RaylibStruct[], hidden_functions: string[]) {
if (hidden_functions.includes(func.name)) return ''
let function_bind = ''
let returnType = ConvertFunctionReturnType(func.returnType)
function_bind += returnType + ` Bind${func.name}(const Napi::CallbackInfo& info) {` + br
let function_call = func.name + '(' + br
if (func.returnType.endsWith('*') && returnType == 'Napi::Number') {
function_call = '(int64_t)' + function_call
}
let index = -1
for (let key in func.params) { if (func.params.hasOwnProperty(key)) {
if (index < 0) index = 0
let type = func.params[key] as any as string
if (type) {
let arg = convert_info_arg(index, type)
function_call += tab + tab + arg.slice(0, arg.length-1) + ',' + br
}
index++
}}
// not all funcs take args - fixes format
if (index >= 0) {
function_call = function_call.slice(0, function_call.length - 2) + br
} else {
function_call = function_call.slice(0, function_call.length - 1)
}
function_call += tab + ')'
if (returnType == 'void' || returnType == 'void *') {
function_bind += tab + function_call + ';' + br + '}'
} else if (returnType.includes('Object')) {
// This is when we are returning a raylib struct
function_bind += tab + func.returnType + ' obj = ' + function_call + ';' + br
function_bind += tab + 'Napi::Object out = Napi::Object::New(info.Env());' + br
let func_struct: RaylibStruct = {} as RaylibStruct
for (let struct of structs) {
if (struct.name == func.returnType)
func_struct = struct
}
if (!func_struct.fields)
throw new Error("STRUCT SEARCH ERROR")
for (let field of func_struct.fields) {
let field_type = ''
let field_struct: RaylibStruct
for (let struct of structs) {
if (struct.name == field.type) {
field_type = struct.name
field_struct = struct
}
}
if (field_type == '') {
if (field.type.endsWith('*')) {
function_bind += tab + `out.Set("${field.name}", (int64_t)obj.${field.name});` + br
} else {
function_bind += tab + `out.Set("${field.name}", obj.${field.name});` + br
}
} else {
if (field_type.endsWith('*')) {
function_bind += tab + `out.Set("${field.name}", ${field_type}ToNAPIObject(info.Env(), (int64_t)obj.${field.name}));` + br
} else {
function_bind += tab + `out.Set("${field.name}", ${field_type}ToNAPIObject(info.Env(), obj.${field.name}));` + br
}
}
}
function_bind += tab + 'return out;' + br + '}' + br
} else {
function_bind += tab + 'return ' + returnType + '::New(info.Env(), ' + br
function_bind += tab + tab + function_call + br + ');' + br + '}'
}
return function_bind + br + br
}
export function GenerateCppBindings(api: RaylibAPI, output_path: string, hidden_functions: string[]) {
let auto_wrapped = wrappedFunctions(api.functions, api.structs)
let binding_text = ''
binding_text += `#include <napi.h>`+ br
binding_text += `#include "./raylib/include/raylib.h"`+ br
binding_text += `using namespace Napi;`+ br + br
for (let struct of api.structs) {
let struct_funcs = GenerateStructConverters(struct)
binding_text += struct_funcs
}
for (let func of api.functions) {
binding_text += GenerateFunctionBinding(func, api.structs, hidden_functions)
}
binding_text += auto_wrapped.funcs
binding_text += wrapped_functions
binding_text += br + 'Napi::Object Init(Napi::Env env, Napi::Object exports) {' + br
for (let struct of api.structs) {
if (constructors.includes(struct.name)) {
binding_text += `
exports.Set(
Napi::String::New(env, "${struct.name}"),
Napi::Function::New(env, Bind${struct.name})
);
`
}
}
for (let func of api.functions) {
if (hidden_functions.includes(func.name)) {
continue
}
binding_text += `
exports.Set(
Napi::String::New(env, "${func.name}"),
Napi::Function::New(env, Bind${func.name})
);
`
}
binding_text += wrapped_function_bindings
binding_text += auto_wrapped.bindings
binding_text += br + tab + 'return exports;'
binding_text += br + '}' + br + br
binding_text += 'NODE_API_MODULE(addon, Init);'
fs.writeFileSync(output_path, binding_text)
}