This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupdate-bindings.js
193 lines (173 loc) · 5.95 KB
/
update-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
#!/usr/bin/env node
/**
* This script launch Emscripten's WebIDL binder to generate
* the glue.cpp and glue.js file inside Bindings directory
* using Bindings.idl
*/
var debug = false; //If true, add additional checks in bindings files.
var fs = require('fs');
var exec = require('child_process').exec;
if (!process.env.EMSCRIPTEN) {
console.error('EMSCRIPTEN env. variable is not set');
console.log(
'Please set Emscripten environment by launching `emsdk_env` script'
);
}
var emscriptenPath = process.env.EMSCRIPTEN;
var webIdlBinderPath = emscriptenPath + '/tools/webidl_binder.py';
generateGlueFromBinding(function(err) {
if (err) return fatalError(err);
patchGlueCppFile(function(err) {
if (err) return fatalError(err);
});
});
/**
* Run Embind webidl_binder.py to generate glue.cpp and glue.js
* from Bindings.idl
*/
function generateGlueFromBinding(cb) {
fs.exists(webIdlBinderPath, function(exists) {
if (!exists) {
cb({
message: 'Please check your Emscripten installation',
output: "Can't find " + webIdlBinderPath,
});
return;
}
exec(
'python "' + webIdlBinderPath + '" Bindings/Bindings.idl Bindings/glue',
function(err, stdout, stderr) {
if (err) {
cb({ message: 'Error while running WebIDL binder:', output: err });
}
cb(null);
}
);
});
}
/**
* A few modification needs to be made to glue.cpp because of limitations
* of the IDL language/binder.
*/
function patchGlueCppFile(cb) {
var file = 'Bindings/glue.cpp';
var classesToErase = [
'ArbitraryResourceWorkerJS',
'AbstractFileSystemJS',
'BehaviorJsImplementation',
'ObjectJsImplementation',
'BehaviorSharedDataJsImplementation',
];
var functionsToErase = [
'emscripten_bind_ArbitraryResourceWorkerJS_ExposeImage_1',
'emscripten_bind_ArbitraryResourceWorkerJS_ExposeShader_1',
'emscripten_bind_ArbitraryResourceWorkerJS_ExposeFile_1',
];
fs.readFile(file, function(err, data) {
if (err) cb(err);
var patchedFile = '';
var insideReturnStringFunction = false;
var erasingClass = false;
var erasingFunction = false;
data
.toString()
.split('\n')
.forEach(function(line) {
//When declaring a function returning "[Const, Ref] DOMString"
//or "[Const, Value] DOMString"
//in the IDL file, the return type is const char*. We are using
//std::string in GDevelop and need to call c_str.
if (insideReturnStringFunction) {
//[Const, Value] DOMString
if (line.indexOf('static char*') !== -1) {
line = line.replace('static char*', 'static gd::String');
} else if (line.indexOf(', &temp);') !== -1) {
line = line.replace(', &temp);', ', temp.c_str());');
//[Const, Ref] DOMString
} else {
if (debug) {
//For debugging, use a temporary useless reference
//to check the return type is a reference and not a value.
//Could generate false positive.
line = line
.replace(';', '); return ref.c_str();')
.replace(
'return &',
'gd::String & ref = const_cast<gd::String&>('
);
} else {
line = line.replace(';', '.c_str();').replace('&', '');
}
}
}
//Make sure free functions are called properly.
var freeCallPos = line.indexOf('self->FREE_');
if (freeCallPos !== -1) {
var nameEndPos = line.indexOf('(', freeCallPos);
var name = line.substring(freeCallPos + 11, nameEndPos);
var startOfLine = line.substring(0, freeCallPos);
var endOfLine = line.substring(nameEndPos + 1, line.length);
var hasOtherParamers = endOfLine[0] !== ')';
line =
startOfLine +
name +
'(*self' +
(hasOtherParamers ? ', ' : '') +
endOfLine;
}
//Fix calls to operator [] with pointers
line = line.replace('self->MAP_set', '(*self)MAP_set');
//Simulate copy operator with CLONE_type
var cloneCallPos = line.indexOf('self->CLONE_');
if (cloneCallPos !== -1) {
line = line.replace('self->CLONE_', 'new ');
line = line.replace('()', '(*self)');
}
//Custom function MAPS_keys to get the keys of a map
var mapKeyCallPos = line.indexOf('self->MAP_keys');
if (mapKeyCallPos !== -1) {
line =
'temp.clear(); for(auto it = self->begin(); it != self->end();' +
'++it) { temp.push_back(it->first); } return &temp;';
}
if (line.indexOf('class') === 0) {
for (var i = 0; i < classesToErase.length; ++i) {
if (line.indexOf('class ' + classesToErase[i]) === 0) {
erasingClass = true;
}
}
}
if (line.indexOf('EMSCRIPTEN_KEEPALIVE') !== -1) {
for (var i = 0; i < functionsToErase.length; ++i) {
if (
line.indexOf(
'EMSCRIPTEN_KEEPALIVE ' + functionsToErase[i] + '('
) !== -1
) {
erasingFunction = true;
}
}
}
if (!erasingClass && !erasingFunction) patchedFile += line + '\n';
if (line.indexOf('const char* EMSCRIPTEN_KEEPALIVE') == 0) {
insideReturnStringFunction = true;
} else if (line.indexOf('}') == 0) {
insideReturnStringFunction = false;
}
if (erasingClass && line.indexOf('};') === 0) {
erasingClass = false;
}
if (erasingFunction && line.indexOf('}') === 0) {
erasingFunction = false;
}
});
fs.writeFile(file, patchedFile, function(err) {
cb(err);
});
});
}
function fatalError(error) {
if (error.message) console.error(error.message);
if (error.output) console.log(error.output);
process.exit(1);
}