-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
298 lines (240 loc) · 7.06 KB
/
index.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
var fs = require('fs');
var path = require('path');
var PEG = require('pegjs');
var fireTs = require('fire-ts');
var util = require('util');
var extend = require('extend');
var setParser = PEG.buildParser(fs.readFileSync(path.join(__dirname, 'set.peg'), 'utf8'));
//console.log(setParser);
function nameForFunction(func) {
return func.name;
}
function produceFunction(func) {
var name = nameForFunction(func);
var out = "";
out += "static inline NAN_METHOD(" + name + ") {\n";
out += "}\n\n";
return out;
}
function generateInit(baseName) {
var out = "";
out += "static inline void init_" + baseName +"(v8::Handle<v8::Object>& exports) {\n"
out += "}\n";
return out;
}
// function typeString(obj, options) {
// var str = obj.type.name;
// options = options || {};
// if(obj.type.pointer && options !== '*')
// str += obj.type.pointer;
// if(options.param) {
// str += ' ' + options.param;
// }
// return str;
//}
var typeString = require('nid-parser').typeString;
function typeIdentifier(type, options) {
var str = ((typeof type === 'string') ? type : typeString(type, options));
str = str.replace(/ /g, '_');
str = str.replace(/,/g, '_');
str = str.replace(/\(\*\)/g, '_funcptr_');
str = str.replace(/\(/g, '_');
str = str.replace(/\)/g, '_');
str = str.replace(/::/g, '__');
str = str.replace(/\*/g, ' ptr');
return str.split(/\s+/).join('_');
}
console.log = console.warn;
var processedTypes = {};
var existingProcessedTypes = {
'const_char_ptr': true,
'char_ptr': true,
'void_ptr': true
};
var namespaceTokens = [];
function processType(obj, options) {
console.log("Pre process type", obj);
if(! obj.type.pointer && !(obj.functionPointer || obj.type.functionPointer )) {
console.log("Skipping type", obj);
return;
}
var typeIdent = typeIdentifier(obj, options);
console.log("Process type ident", typeIdent);
if(existingProcessedTypes[typeIdent]) {
console.log("Skipping due to already existing");
return;
}
console.log("Type ident", typeIdent, processedTypes);
if(!processedTypes[typeIdent]) {
console.log("Adding type", typeIdent);
obj.ifdefIdentifier = 'CBIND_HELPER_' + typeIdent.toUpperCase();
}
else {
console.log("Type exists", typeIdent);
return;
}
processedTypes[typeIdent] = obj;
console.log("########ADDED", typeIdent);
}
function processFunction(func) {
if(func.attributes.macro)
func.accessCode = func.name;
else
func.accessCode = namespaceTokens.concat([func.name]).join('::');
if(func.type.functionPointer) {
console.log("Processing return function pointer type", func.type);
processFunction(func.type);
console.log("End of processing return function pointer type");
}
else {
processType(func);
if(func.parameters) {
func.parameters.forEach(function(parameter) {
processType(parameter);
});
}
}
}
function processVariable(_variable) {
console.log(">>>>>>>>>>>>>>>>> ADDING VARIABLE", _variable);
if(_variable.functionPointer) {
processType(_variable, {forceParams: true});
}
else {
processType(_variable);
}
if(_variable.attributes.macro)
_variable.accessCode = _variable.name;
else
_variable.accessCode = namespaceTokens.concat([_variable.name]).join('::');
}
function processClass(_class) {
processType({type: {
name: _class.name,
pointer: '*',
nonPrimitive: true
}});
}
function processNamespace(namespace) {
namespaceTokens.push(namespace.name);
namespace.declarations.forEach(function(declaration) {
if(declaration.namespace)
processNamespace(declaration.namespace);
else if(declaration.function) {
processFunction(declaration.function);
}
else if(declaration.struct || declaration['class']) {
processClass(declaration.struct || declaration['class']);
}
else if(declaration.variable) {
processVariable(declaration.variable);
}
});
namespaceTokens.pop();
}
function parseSet(setString, name2param)
{
setParser.name2param = name2param;
return setParser.parse(setString);
}
function processNidFile(file) {
console.warn("Processing NID file:", file);
var defs = fs.readFileSync(file);
var base = path.basename(file);
var baseName = path.basename(file, '.nid');
var outHeader = path.join('build', 'cbind_' + baseName + '.h');
var outGypi = path.join('build', 'cbind.gypi');
var parser;
try {
parser = require('nid-parser');
} catch(err) {
console.log(err);
process.exit(1);
}
function prepareForParse(str) {
return str.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/gm, function(match) {
return new Array(match.length + 1).join(' ');
});
}
var nid;
try {
nid = parser.parse(prepareForParse(fs.readFileSync(file, 'utf8')));
}
catch(err) {
console.log(err.line + ':' + err.offset, ':', err.message);
process.exit(1);
}
var tplCache = {};
var indent = 1;
function processInclude(tplName, data) {
console.log(">>>>>>>>>>>>>> PROCESS INCLUDE", tplName);
++indent;
var indentStr = Array(indent).join(' ');
var filename = path.join(__dirname, 'templates', tplName);
if(!tplCache[tplName]) {
tplCache[tplName] = fireTs.parseSync(filename, {
indent: ' '
});
// console.log(tplCache[tplName]);
}
--indent;
return tplCache[tplName](data, {
render: function(f, f2, f3) {
return processInclude(f, f2, f3).split(/\n/).map(function(line) {
return indentStr + line;
}).join('\n');
}
});
}
processNamespace(nid);
processedTypes = Object.keys(processedTypes).map(function(key) {
return processedTypes[key];
});
console.warn(util.inspect(nid, {depth: null}));
var headerContent = "";
try {
headerContent = processInclude('header.fts', {
baseName: baseName,
declarations: nid.declarations,
processedTypes: processedTypes,
data: nid,
upcase: function(str) {
return str.toUpperCase();
},
typeString: typeString,
typeIdentifier: typeIdentifier,
parseSet: parseSet
});
console.warn(headerContent, {depth: null});
} catch(err) {
console.log(err.stack);
}
var buildDir = path.resolve('build');
['helpers.h', 'type_literal.h'].forEach(function(header) {
fs.createReadStream(path.join(__dirname, header)).pipe(
fs.createWriteStream(path.join(buildDir, header))
);
});
//fs.writeFileSync(outHeader, '');
fs.writeFile(outHeader, headerContent);
}
var globExpand = require('glob-expand');
module.exports = function() {
var buildDir = path.resolve('build');
var args = Array.prototype.slice.apply(arguments);
//process.stdout.write("DUPA\n");
var files = [];
args.forEach(function(arg) {
//process.stdout.write(globExpand(arg)+'\n');
files = files.concat(globExpand(arg).toString('utf8').split(/,/));
});
//proess.stdout.write(files.join(',') + "\n");
files.map(processNidFile);
process.stdout.write(buildDir+'\n');
}
var defs = {};
try {
defs = require('./build/Release/cbind_core.node')
} catch(err) {
}
module.exports = extend(module.exports, defs);