-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnode-raylib-bindings.js
406 lines (376 loc) · 11.4 KB
/
node-raylib-bindings.js
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
* Sanitizes a type (primitive or struct) into a string that can be used as part of a function name.
* Converts anything ending with '*' to 'pointer'
* @param {*} name
* @returns
*/
const SanitizeTypeName = name => {
if (name === 'const Vector3') {
return 'Vector3'
}
if (name === 'float[2]') {
return 'pointer'
}
if (name === 'char[32]') {
return 'pointer'
}
if (name === 'float[4]') {
return 'pointer'
}
if (name === 'unsigned int[4]') {
return '(unsigned int) pointer'
}
if (name === 'Matrix[2]') {
return 'pointer'
}
if (name === 'const char *') {
return 'string'
}
if (name.endsWith('*')) {
return 'pointer'
}
if (name.endsWith('Callback')) {
return 'function'
}
if (name === 'Camera') {
return 'Camera3D'
}
if (name === 'Quaternion') {
return 'Vector4'
}
if (name === 'Texture2D') {
return 'Texture'
}
return name.replace(/ /g, '')
}
const TypeUnwrappedLength = (structs, type) => {
if (type === 'Camera') {
type = 'Camera3D'
}
if (type === 'Texture2D') {
type = 'Texture'
}
let properties = 0
for (const struct of structs) {
if (struct.name === type) {
for (const field of struct.fields) {
properties += TypeUnwrappedLength(structs, field.type)
}
}
}
if (properties === 0) { // primitive, not struct. return length 1
return 1
}
return properties
}
const UnwrappedStructProperties = (structs, struct) => {
let length = 0
return struct.fields
.map(field => {
const out = `${field.type.endsWith('*') ? ` (${field.type})` : ''} ${SanitizeTypeName(field.type)}FromValue(info, index + ${length})`
length += TypeUnwrappedLength(structs, field.type)
return out
})
.join(',\n ')
}
const UnwrappedFuncArguments = (structs, func) => {
if (!func.params) { return '' }
let length = 0
return func.params
.map(param => {
const out = `${param.type.endsWith('*') ? ` (${param.type})` : ''} ${SanitizeTypeName(param.type)}FromValue(info, ${length})`
length += TypeUnwrappedLength(structs, param.type)
return out
})
.join(',\n ')
}
/**
*
* @param {*} struct
* @returns
*/
const FromValue = (structs, struct) => `
inline ${struct.name} ${SanitizeTypeName(struct.name)}FromValue(const Napi::CallbackInfo& info, int index) {
return {
${UnwrappedStructProperties(structs, struct)}
};
}`
/**
*
* @param {*} struct
* @returns
*/
const ToValue = (struct) => `
inline Napi::Value ToValue(Napi::Env env, ${struct.name} obj) {
Napi::Object out = Napi::Object::New(env);
${struct.fields
.map(field => `out.Set("${field.name}", ToValue(env, obj.${field.name}));`)
.join('\n ')
}
return out;
}`
/**
*
* @param {*} func
* @returns
*/
const BindFunction = (structs, func) => `
Napi::Value Bind${func.name}(const Napi::CallbackInfo& info) {
return ToValue(info.Env(),
${func.name}(
${UnwrappedFuncArguments(structs, func)}
)
);
}`
/**
*
* @param {*} func
* @returns
*/
const BindVoidFunction = (structs, func) => `
void Bind${func.name}(const Napi::CallbackInfo& info) {
${func.name}(
${UnwrappedFuncArguments(structs, func)}
);
}`
/**
*
* @param {*} func
* @returns
*/
const BindFunctionPassByRef = (structs, func) => {
let returnType = func.params[0].type
returnType = returnType.replace(' *', '')
let length = TypeUnwrappedLength(structs, returnType)
return `
Napi::Value Bind${func.name}(const Napi::CallbackInfo& info) {
${returnType} obj = ${returnType}FromValue(info, 0);
${func.name}(
${func.params.length === 1
? '&obj\n'
: ['&obj'].concat(func.params
?.filter((param, index) => index !== 0)
.map(param => {
const out = `${param.type.endsWith('*') ? ` (${param.type})` : ''} ${SanitizeTypeName(param.type)}FromValue(info, ${length})`
length += TypeUnwrappedLength(structs, param.type)
return out
})
.join(',\n '))
}
);
return ToValue(info.Env(), obj);
}`
}
/**
*
* @param {*} func
* @returns
*/
const ExportFunctionBinding = func => `exports.Set("Bind${func.name}", Napi::Function::New(env, Bind${func.name}));`
module.exports = ({ functions, structs, enums, blocklist, byreflist }) => `
// GENERATED CODE: DO NOT MODIFY
#include <string>
#include <napi.h>
#include <iostream>
#include <cstring>
#include "raylib.h"
#include "raymath.h"
#include "../extras/reasings.h"
#include "../extras/rlgl.h"
#define RAYGUI_IMPLEMENTATION
#include "../extras/raygui.h"
using namespace Napi;
inline Napi::Value ToValue(Napi::Env env, bool value) {
return Napi::Boolean::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, const char* value) {
return Napi::String::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, const std::string& value) {
return Napi::String::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, char* value) {
return Napi::String::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, int value) {
return Napi::Number::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, unsigned int value) {
return Napi::Number::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, long value) {
return Napi::Number::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, double value) {
return Napi::Number::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, float value) {
return Napi::Number::New(env, value);
}
inline Napi::Value ToValue(Napi::Env env, void * value) {
return Napi::Number::New(env, (int64_t) value);
}
inline Napi::Value ToValue(Napi::Env env, unsigned long long value) {
return Napi::BigInt::New(env, (uint64_t) value);
}
inline float floatFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Number>().FloatValue();
}
inline int intFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Number>().Int32Value();
}
inline double doubleFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Number>().DoubleValue();
}
uintptr_t pointerFromValue(const Napi::CallbackInfo& info, int index) {
return (uintptr_t) info[index].As<Napi::Number>().Int64Value();
}
inline unsigned char unsignedcharFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Number>().Uint32Value();
}
inline unsigned int unsignedintFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Number>().Uint32Value();
}
inline unsigned long long unsignedlonglongFromValue(const Napi::CallbackInfo& info, int index) {
return (unsigned long long) info[index].As<Napi::BigInt>().Uint64Value(0);
}
inline bool boolFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Boolean>();
}
inline const char * stringFromValue(const Napi::CallbackInfo& info, int index) {
std::string val = info[index].As<Napi::String>().Utf8Value();
const std::string::size_type size = val.size();
char *buffer = new char[size + 1]; //we need extra char for NUL
memcpy(buffer, val.c_str(), size + 1);
return buffer;
}
inline char charFromValue(const Napi::CallbackInfo& info, int index) {
return info[index].As<Napi::Number>().Uint32Value();
}
// exception for this constructor, which has different input depending on platform
inline rlVertexBuffer rlVertexBufferFromValue(const Napi::CallbackInfo& info, int index) {
return {
intFromValue(info, index + 0),
(float *) pointerFromValue(info, index + 1),
(float *) pointerFromValue(info, index + 2),
(unsigned char *) pointerFromValue(info, index + 3),
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
(unsigned int *) pointerFromValue(info, index + 4), // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
(unsigned short *) pointerFromValue(info, index + 4), // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
#endif
unsignedintFromValue(info, index + 5),
(unsigned int) pointerFromValue(info, index + 6)
};
}
// Convert structs from Napi::Values in info[] arguments
${structs
.filter(({ name }) => !blocklist.includes(name) && name !== 'rlVertexBuffer')
.map(struct => { return FromValue(structs, struct) })
.join('\n')
}
// Convert structs to Napi::Objects for output to JS
${structs
.filter(({ name }) => !blocklist.includes(name))
.map(ToValue)
.join('\n')
}
inline Texture2D Texture2DFromValue(const Napi::CallbackInfo& info, int index) {
return (Texture2D) TextureFromValue(info, index);
}
inline RenderTexture2D RenderTexture2DFromValue(const Napi::CallbackInfo& info, int index) {
return (RenderTexture2D) RenderTextureFromValue(info, index);
}
inline Camera CameraFromValue(const Napi::CallbackInfo& info, int index) {
return Camera3DFromValue(info, index);
}
// Raylib API function bindings
${functions
.filter(({ name }) => !blocklist.includes(name))
.filter(({ name }) => !byreflist.includes(name))
.filter((func) => func.returnType !== 'void')
.map((func) => { return BindFunction(structs, func) })
.join('\n')
}
${functions
.filter(({ name }) => !blocklist.includes(name))
.filter(({ name }) => !byreflist.includes(name))
.filter((func) => func.returnType === 'void')
.map((func) => { return BindVoidFunction(structs, func) })
.join('\n')
}
// By-Reference function bindings
${functions
.filter(({ name }) => !blocklist.includes(name))
.filter(({ name }) => byreflist.includes(name))
.map((func) => { return BindFunctionPassByRef(structs, func) })
.join('\n')
}
// Shader Functions
void BindSetShaderFloat(const Napi::CallbackInfo& info) {
float value = floatFromValue(info, 3);
SetShaderValueV(
ShaderFromValue(info, 0),
intFromValue(info, 2),
&value,
SHADER_UNIFORM_FLOAT,
1
);
}
void BindSetShaderInt(const Napi::CallbackInfo& info) {
int value = intFromValue(info, 3);
SetShaderValueV(
ShaderFromValue(info, 0),
intFromValue(info, 2),
&value,
SHADER_UNIFORM_INT,
1
);
}
void BindSetShaderVec2(const Napi::CallbackInfo& info) {
Vector2 value = Vector2FromValue(info, 3);
SetShaderValueV(
ShaderFromValue(info, 0),
intFromValue(info, 2),
&value,
SHADER_UNIFORM_VEC2,
1
);
}
void BindSetShaderVec3(const Napi::CallbackInfo& info) {
Vector3 value = Vector3FromValue(info, 3);
SetShaderValueV(
ShaderFromValue(info, 0),
intFromValue(info, 2),
&value,
SHADER_UNIFORM_VEC3,
1
);
}
void BindSetShaderVec4(const Napi::CallbackInfo& info) {
Vector4 value = Vector4FromValue(info, 3);
SetShaderValueV(
ShaderFromValue(info, 0),
intFromValue(info, 2),
&value,
SHADER_UNIFORM_VEC4,
1
);
}
// Exported JS Module object
Napi::Object Init(Napi::Env env, Napi::Object exports) {
${functions
.filter(({ name }) => !blocklist.includes(name))
.map(ExportFunctionBinding)
.join('\n ')
}
exports.Set("BindSetShaderFloat", Napi::Function::New(env, BindSetShaderFloat));
exports.Set("BindSetShaderInt", Napi::Function::New(env, BindSetShaderInt));
exports.Set("BindSetShaderVec2", Napi::Function::New(env, BindSetShaderVec2));
exports.Set("BindSetShaderVec3", Napi::Function::New(env, BindSetShaderVec3));
exports.Set("BindSetShaderVec4", Napi::Function::New(env, BindSetShaderVec4));
return exports;
}
NODE_API_MODULE(addon, Init);
`