/
Program.cs
467 lines (398 loc) · 18.5 KB
/
Program.cs
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System.Xml;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocGenerator;
namespace EmbindGenerator
{
class Program
{
static CodeStructure cs = new CodeStructure();
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: EmbindGenerator <directory_to_doxygen_xml_docs> [class1 [class2 [class3 ... [classN]]]]");
return;
}
cs.LoadSymbolsFromDirectory(args[0]);
List<string> knownSymbolNames = new List<string>();
knownSymbolNames.Add(""); // Typeless 'types', e.g. return value of ctor is parsed to an empty string.
knownSymbolNames.Add("void");
knownSymbolNames.Add("bool");
knownSymbolNames.Add("char");
knownSymbolNames.Add("signed char");
knownSymbolNames.Add("unsigned char");
knownSymbolNames.Add("short");
knownSymbolNames.Add("signed short");
knownSymbolNames.Add("unsigned short");
knownSymbolNames.Add("int");
knownSymbolNames.Add("signed int");
knownSymbolNames.Add("unsigned int");
knownSymbolNames.Add("long");
knownSymbolNames.Add("signed long");
knownSymbolNames.Add("unsigned long");
knownSymbolNames.Add("float");
knownSymbolNames.Add("double");
knownSymbolNames.Add("unsigned int");
knownSymbolNames.Add("std::string");
knownSymbolNames.Add("emscripten::val");
for (int i = 1; i < args.Length; ++i)
knownSymbolNames.Add(args[i]);
string t =
"#ifdef EMSCRIPTEN\n\n" +
"#include <emscripten/bind.h>\n" +
"using namespace emscripten;\n\n" +
"#include \"embind_prologue.h\" // Implement this file and all required code and header files to compile this file here.\n\n";
tw.Write(t);
js_tw.WriteLine("function RegisterFunctionSelectors() {");
for (int i = 1; i < args.Length; ++i)
GenerateCtorFunctions(args[i], knownSymbolNames);
tw.Write("EMSCRIPTEN_BINDINGS(bindings) /* todo: if we create a separate bindings file for each, generate a unique name here. */ {\n\n");
for (int i = 1; i < args.Length; ++i)
WriteForwardDeclaration(args[i]);
for (int i = 1; i < args.Length; ++i)
GenerateEmbindFile(args[i], knownSymbolNames);
tw.Write("\n}\n\n");
tw.Write("#endif\n");
tw.Flush();
tw.Close();
Console.WriteLine("Writing embind_symbols.cpp done.");
GenerateTypeIdAssignments();
js_tw.WriteLine("}");
js_tw.WriteLine("window['RegisterFunctionSelectors'] = RegisterFunctionSelectors;");
GenerateIsOfTypeFunctions();
js_tw.Flush();
js_tw.Close();
Console.WriteLine("Writing embind_symbols.js done.");
}
static TextWriter tw = new StreamWriter("embind_symbols.cpp");
static TextWriter js_tw = new StreamWriter("embind_symbols.js");
static void WriteForwardDeclaration(string className)
{
// string t = "auto " + className + "_class = class_<" + className + ">(\"" + className + "\");\n\n";
// tw.Write(t);
}
static List<string> generatedFunctionSelectors = new List<string>();
static List<Symbol> FetchFunctionOverloads(Symbol function, List<string> knownSymbolNames)
{
List<Symbol> functionOverloads = new List<Symbol>();
foreach (Symbol s in function.parent.children)
if (s.name == function.name && IsGoodSymbol(s, knownSymbolNames))
functionOverloads.Add(s);
functionOverloads.Sort(delegate(Symbol left, Symbol right)
{
return right.parameters.Count - left.parameters.Count;
/*
if (left.parameters.Count > right.parameters.Count)
return true;
else if (left.parameters.Count < right.parameters.Count)
return false;
return left.parameters.cou
*/
});
return functionOverloads;
}
/// <summary>
/// Lists the types for which we need to generate type identifier functions for.
/// </summary>
static List<string> isOfTypeFunctionsRequired = new List<string>();
static void GenerateTypeIdAssignments()
{
int typeIdCounter = 1;
foreach (string type in isOfTypeFunctionsRequired)
{
if (type == "float" || type == "int")
continue;
js_tw.WriteLine("\tModule." + type + ".prototype.TypeId = " + typeIdCounter + " /* Magic automatically generated TypeId number for "
+ type + " */");
++typeIdCounter;
}
}
static void GenerateIsOfTypeFunctions()
{
js_tw.WriteLine("function isNumber(value) {");
js_tw.WriteLine("\tif ((undefined === value) || (null === value)) {");
js_tw.WriteLine("\t\treturn false;");
js_tw.WriteLine("\t}");
js_tw.WriteLine("\tif (typeof value == 'number') {");
js_tw.WriteLine("\t\treturn true;");
js_tw.WriteLine("\t}");
js_tw.WriteLine("\treturn !isNaN(value - 0);");
js_tw.WriteLine("}");
js_tw.WriteLine("");
int typeIdCounter = 1;
foreach (string type in isOfTypeFunctionsRequired)
{
if (type == "float" || type == "int")
{
js_tw.WriteLine("function IsOfType_" + type + "(obj) { return isNumber(obj); }");
}
else
{
// js_tw.WriteLine(type + ".prototype.TypeId = " + typeIdCounter + " /* Magic automatically generated TypeId number for "
// + type + " */");
js_tw.WriteLine("function IsOfType_" + type + "(obj) { return obj != undefined && obj != null && obj.TypeId == "
+ typeIdCounter + " /* Magic automatically generated TypeId number for " + type + " */; }");
++typeIdCounter;
}
// js_tw.WriteLine("");
}
}
static void GenerateFunctionSelector(List<Symbol> functionOverloads)
{
functionOverloads.Sort(delegate(Symbol left, Symbol right)
{
return right.parameters.Count - left.parameters.Count;
});
Symbol function = functionOverloads.First();
if (generatedFunctionSelectors.Contains(function.parent.name + "::" + function.name))
return;
generatedFunctionSelectors.Add(function.parent.name + "::" + function.name);
// if (function.name == function.parent.name)
// return; // TODO: Add support for ctors.
if (functionOverloads.First().parameters.Count == 0)
return;
bool isCtor = (function.name == function.parent.name);
string prototype;
if (isCtor)
prototype = function.name + "_ = function("; ///\todo Add support for REAL ctors.
else
prototype = function.parent.name + ".prototype." + function.name + " = function(";
string paramList = "";
for (int i = 0; i < functionOverloads.First().parameters.Count; ++i)
{
if (i != 0)
paramList += ", ";
paramList += "arg" + (i + 1);
}
prototype += paramList + ") {";
js_tw.WriteLine(prototype);
for(int i = 0; i < functionOverloads.Count; ++i)
{
Symbol thisFunc = functionOverloads[i];
Symbol nextFunc = (i == functionOverloads.Count - 1) ? null : functionOverloads[i + 1];
js_tw.Write("\t");
if (i != 0)
js_tw.Write("else ");
if (nextFunc != null)
{
if (thisFunc.parameters.Count != nextFunc.parameters.Count)
js_tw.WriteLine("if (arg" + thisFunc.parameters.Count + " != undefined)");
else
{
js_tw.Write("if (");
for (int j = 0; j < thisFunc.parameters.Count; ++j)
{
if (j != 0)
js_tw.Write(" && ");
js_tw.Write("IsOfType_" + thisFunc.parameters[j].BasicType() + "(arg" + (j + 1) + ")");
if (!isOfTypeFunctionsRequired.Contains(thisFunc.parameters[j].BasicType()))
isOfTypeFunctionsRequired.Add(thisFunc.parameters[j].BasicType());
}
js_tw.WriteLine(")");
}
}
else
js_tw.WriteLine("");
js_tw.Write("\t\t" + ((thisFunc.type != "void") ? "return " : ""));
js_tw.Write((isCtor ? "Module." : "this.") + function.name);
foreach (Parameter p in thisFunc.parameters)
js_tw.Write("_" + p.BasicType().Replace(':', '_').Replace('<', '_').Replace('>', '_'));
paramList = "";
for (int j = 0; j < thisFunc.parameters.Count; ++j)
{
if (j != 0)
paramList += ", ";
paramList += "arg" + (j + 1);
}
js_tw.WriteLine("(" + paramList + ");");
}
js_tw.WriteLine("}");
}
static bool IsGoodSymbol(Symbol s, List<string> knownSymbolNames)
{
if (s.attributes.Contains("noembind"))
return false;
if (!knownSymbolNames.Contains(s.type))
return false;
foreach(Parameter p in s.parameters)
if (!knownSymbolNames.Contains(p.BasicType()))
return false;
return true;
}
static void GenerateEmbindFile(string className, List<string> knownSymbolNames)
{
if (!cs.symbolsByName.ContainsKey(className))
{
Console.WriteLine("Error: Cannot generate bindings for class '" + className + "', XML docs for that class don't exist!");
return;
}
string t =
"//#include <emscripten/bind.h>\n" +
"//using namespace emscripten;\n\n" +
"//#include \"embind_prologue.h\" // Implement this file and all required code and header files to compile this file here.\n\n" +
"//EMSCRIPTEN_BINDINGS(" + className + ") { \n\n";
bool hasCtorExposed = false; // Embind only supports exposing one ctor, so pick the first one.
t += "class_<"+className+">(\""+className+"\")\n";
// t += className + "_class\n";
Symbol s = cs.symbolsByName[className];
foreach(Symbol f in s.children)
{
if (f.visibilityLevel != VisibilityLevel.Public)
continue; // Only public functions and members are exported.
List<Symbol> functionOverloads = FetchFunctionOverloads(f, knownSymbolNames);
bool isGoodSymbol = !f.attributes.Contains("noembind"); // If true, this symbol is exposed. If false, this symbol is not enabled for JS.
string reason = f.attributes.Contains("noembind") ? "(ignoring since [noembind] specified)" : "";
if (f.kind == "function" && !f.name.StartsWith("operator"))
{
bool isCtor = f.name == className;
if (isGoodSymbol && !knownSymbolNames.Contains(f.type))
{
isGoodSymbol = false;
reason += "(" + f.type + " is not known to embind)";
}
string targetFunctionName = f.name; // The JS side name with which this function will be exposed.
bool hasOverloads = (functionOverloads.Count > 1);
string funcPtrType = f.type + "(" + (f.isStatic ? "" : ( className + "::")) + "*)(";
bool first = true;
string paramList = "";
foreach(Parameter p in f.parameters)
{
if (!first) paramList += ",";
paramList += p.type;
if (isGoodSymbol && !knownSymbolNames.Contains(p.BasicType()))
{
isGoodSymbol = false;
reason += "(" + p.BasicType() + " is not known to embind)";
}
if (hasOverloads)
{
targetFunctionName += "_" + p.BasicType().Replace(':', '_').Replace('<', '_').Replace('>', '_');
}
first = false;
}
funcPtrType += paramList + ")";
if (f.isConst)
funcPtrType += " const";
if (isGoodSymbol && f.name == className && hasCtorExposed) ///\todo Remove this line once multiple ctors is supported!
{
isGoodSymbol = false;
reason = "(Multiple constructors not yet supported by embind!)";
}
if (!isGoodSymbol)
t += "// /*" + reason + "*/ ";
t += "\t";
if (isCtor)
{
t += ".constructor<" + paramList + ">()\n";
if (isGoodSymbol)
hasCtorExposed = true;
}
else
{
if (f.isStatic)
t += ".class_function(";
else
t += ".function(";
t += "\"" + targetFunctionName + "\", (" + funcPtrType + ")&" + className + "::" + f.name + ")\n";
}
if (hasOverloads && isGoodSymbol)
GenerateFunctionSelector(functionOverloads);
}
else if (f.kind == "variable" && f.visibilityLevel == VisibilityLevel.Public)
{
if (!knownSymbolNames.Contains(f.type))
t += "// /* " + f.type + " is not known to embind. */ ";
else if (f.IsArray())
t += "// /* Exposing array types as fields are not supported by embind. */ ";
else if (f.isStatic)
t += "// /* Exposting static class variables not yet implemented (are they supported?) */ ";
t += "\t";
t += ".property(\"" + f.name + "\", &" + className + "::" + f.name + ")\n";
}
}
t += "\t;\n";
t += "\n//}\n";
RegisterCtorFunctions(className, knownSymbolNames);
// TextWriter tw = new StreamWriter(className + "_embind.cpp");
// TextWriter tw = new StreamWriter(className + "_embind.h");
tw.Write(t);
// tw.Flush();
// tw.Close();
}
static void GenerateCtorFunctions(string className, List<string> knownGoodSymbols)
{
Symbol s = cs.symbolsByName[className];
List<Symbol> ctors = new List<Symbol>();
foreach (Symbol f in s.children)
{
if (f.name == s.name)
{
bool isGoodCtor = true;
foreach (Parameter p in f.parameters)
if (!knownGoodSymbols.Contains(p.BasicType()))
{
isGoodCtor = false;
break;
}
if (f.parameters.Count == 0) // 0-parameter ctors are created with 'new type();'
isGoodCtor = false;
if (!isGoodCtor)
continue;
ctors.Add(f);
tw.Write(className + " " + className);
foreach (Parameter p in f.parameters)
{
tw.Write("_");
tw.Write(p.BasicType());
}
if (f.parameters.Count == 0)
tw.Write("_");
tw.WriteLine(f.ArgStringWithTypes() + " { return " + className + f.ArgStringWithoutTypes() + "; }");
}
}
tw.WriteLine("");
// js_tw.WriteLine(className + " = Module." + className +";");
js_tw.WriteLine("window['" + className + "'] = Module." + className + ";");
if (ctors.Count > 1)
{
GenerateFunctionSelector(ctors);
js_tw.WriteLine("window['" + className + "_'] = " + className + "_;");
}
}
static void RegisterCtorFunctions(string className, List<string> knownGoodSymbols)
{
Symbol s = cs.symbolsByName[className];
foreach (Symbol f in s.children)
{
if (f.name == s.name)
{
bool isGoodCtor = true;
foreach (Parameter p in f.parameters)
if (!knownGoodSymbols.Contains(p.BasicType()))
{
isGoodCtor = false;
break;
}
if (f.parameters.Count == 0) // 0-parameter ctors are created with 'new type();'
isGoodCtor = false;
if (!isGoodCtor)
continue;
string t = "";
t += className;
foreach (Parameter p in f.parameters)
{
t += "_";
t += p.BasicType();
}
tw.WriteLine("function(\"" + t + "\", &" + t + ");");
}
}
}
}
}