Repository navigation
Expand file tree
/
Copy pathgenerate.js
More file actions
256 lines (230 loc) · 7.38 KB
/
Copy pathgenerate.js
File metadata and controls
256 lines (230 loc) · 7.38 KB
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
// this will generate the wrappers & type-adapters in src/generated
// this file comes from https://github.com/raysan5/raylib/blob/master/parser/raylib_api.json
const defs = require('./raylib_api.json')
// TODO: once array-support works, I should be able to do this:
const { structs, enums, functions } = defs
const { writeFileSync } = require('fs')
const path = require('path')
// use this to keep from wrapping things
const blocklist = [
// error: invalid conversion from ‘void (*)(int, const char*, ...)’ to ‘void (*)()’ [-fpermissive]
'TraceLog',
'TextFormat',
// these appear to not be defined, even though they are in JSON
'SetWindowOpacity',
'GetRenderWidth',
'GetRenderHeight',
'ExportFontAsCode'
]
// thse are aliased types, so you the resolved-type
const typeAliases = {
Quaternion: 'Vector4'
}
// pre-process the data for later analysis
const rSize = /\[([0-9]+)\]/g
for (const struct of structs) {
// take multi-fields (like in Matrix) and make them all distinct fields
let newfields = []
for (const i in struct.fields) {
const field = struct.fields[i]
if (field.name.includes(',')) {
newfields = [...newfields, ...field.name.split(',').map(n => {
return {
...field,
name: n.trim()
}
})]
} else {
newfields.push(field)
}
}
struct.fields = newfields
// find all arrays in structs, and give all fields a size and stripped name for later
for (const field of struct.fields) {
const n = [...field.name.matchAll(rSize)]
if (n.length) {
field.size = parseInt(n[0][1])
field.name = field.name.replace(rSize, '')
} else {
field.size = 1
}
const type = field.type.replace(/[* ]+/g, '')
if (typeAliases[type]) {
field.type = typeAliases[type]
}
}
// TODO: should I also process *-refs to seperate name & the fact it's a ref?
}
// aliases
// structs.push({ ...structs.find(s => s.name === 'Vector4'), name: 'Quaternion' })
// XXX: Since array support isn't complete, just filter out all structs & functions that use them,
// so we get an (incomplete) wrapper that will build.
// THIS IS CURRENTLY NOT WORKING
for (const struct of structs) {
const usesArray = struct.fields.find(f => f.size !== 1)
if (usesArray) {
blocklist.push(struct.name)
}
}
// filter out all functions that use blocked types
for (const f of functions) {
if (blocklist.includes(f.returnType.replace(/[* ]/g, ''))) {
blocklist.push(f.name)
} else {
for (const param of (f.params || [])) {
if (blocklist.includes(param.type.replace(/[* ]/g, ''))) {
blocklist.push(f.name)
break
}
}
}
}
// When Outputting an Napi::Object, you cannot Set() instances of structs - so they need to be converted to Napi::Objects first
const toJSClassConvert = (name, type) => {
if (type.endsWith('*')) { return `out.Set("${name}", (int64_t)input.${name});` }
return type[0].toUpperCase() !== type[0]
? `out.Set("${name}", input.${name});`
: `out.Set("${name}", ToObject(env, input.${name}));`
}
// generate a single adapter for C->JS
const toJS = struct => `
Napi::Object ToObject(Napi::Env& env, const ${struct.name}& input) {
Napi::Object out = Napi::Object::New(env);
${struct.fields.map(({ name, type }) => toJSClassConvert(name, type)).join('\n ')}
return out;
}`
// generate a single adapter for JS->C
const toC = struct => {
const fields = struct.fields.map(field => {
if (field.type.endsWith('*')) {
return `
if (argObject.Has("${field.name}")) {
out.${field.name} = (${field.type})argObject.Get("${field.name}").As<Napi::Number>().Int64Value();
}
`
}
const type = field.type.replace(/[* ]+/g, '')
let fname = `To${type}`
if (type === 'Texture2D') {
fname = 'ToTexture'
}
return `
if (argObject.Has("${field.name}")) {
out.${field.name} = ${fname}(env, argObject.Get("${field.name}"));
}
`
})
return `
${struct.name} To${struct.name}(Napi::Env& env, const Napi::Value& arg) {
Napi::Object argObject(env, arg.As<Napi::Object>());
${struct.name} out;
${fields.join('\n')}
return out;
}
`
}
const templates = {}
templates['node-raylib'] = () => `
#ifndef NODE_RAYLIB_NODE_RAYLIB_H
#define NODE_RAYLIB_NODE_RAYLIB_H
#include <string>
#include <napi.h>
#include "raylib.h"
#include "../lib/AddDefine.h"
#include "../lib/AddFunction.h"
void node_raylib_bindings_defines(Napi::Env& env, Napi::Object& exports) {
${enums
.filter(({ name }) => !blocklist.includes(name))
.map(({ values }) => values.map(({ name }) => `AddDefineInteger(env, exports, "${name}", ${name});`).join('\n '))
.join('\n ')
}
}
void node_raylib_bindings_functions(Napi::Env& env, Napi::Object& exports) {
${functions
.filter(({ name }) => !blocklist.includes(name))
.map(({ name }) => `AddFunction(env, exports, "${name}", &${name});`)
.join('\n ')
}
}
void node_raylib_bindings(Napi::Env& env, Napi::Object& exports) {
node_raylib_bindings_defines(env, exports);
node_raylib_bindings_functions(env, exports);
}
#endif
`
templates.ToValue = () => `
#ifndef NODE_RAYLIB_TOVALUE_H_
#define NODE_RAYLIB_TOVALUE_H_
#include <napi.h>
#include "./ToObject.h"
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);
}
${structs
.filter(({ name }) => !blocklist.includes(name))
.map(({ name }) => `inline Napi::Value ToValue(Napi::Env& env, ${name} value) { return ToObject(env, value); }`)
.join('\n')
}
#endif
`
templates.ToObject = () => `
#ifndef NODE_RAYLIB_TOOBJECT_H_
#define NODE_RAYLIB_TOOBJECT_H_
#include <napi.h>
void Tovoid(Napi::Env& env, Napi::Value value) {
}
float Tofloat(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Number>().FloatValue();
}
unsigned char Tounsignedchar(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Number>().Uint32Value();
}
int Toint(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Number>().Int32Value();
}
unsigned int Tounsignedint(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Number>().Uint32Value();
}
unsigned short Tounsignedshort(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Number>().Uint32Value();
}
char Tochar(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Number>().Uint32Value();
}
bool Tobool(Napi::Env& env, Napi::Value value) {
return value.As<Napi::Boolean>();
}
${structs
.filter(({ name }) => !blocklist.includes(name))
.map((struct) => toJS(struct) + '\n' + toC(struct))
.join('\n ')
}
#endif
`
Object.keys(templates).forEach(file => {
writeFileSync(path.join(__dirname, '..', 'generated', `${file}.h`), templates[file]())
})