Repository navigation
Expand file tree
/
Copy pathsetup.js
More file actions
345 lines (267 loc) · 10.1 KB
/
Copy pathsetup.js
File metadata and controls
345 lines (267 loc) · 10.1 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
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
const fs = require("fs");
const path = require("path");
var version = require("../package.json").libgit2.version;
var descriptor = require("./descriptor.json");
var libgit2 = require("./v" + version + ".json");
var structs = libgit2.types.reduce(function(memo, current) {
return memo[current[0]] = current[1], memo;
}, {});
// Extracted types.
var typeMap = require("./types.json");
// Structs or new types between v0.18 and v0.20.
typeMap.__proto__ = {
"git_filter_mode_t": { cpp: "Number", js: "Number" },
"const git_blame_hunk *": { cpp: "GitBlameHunk", js: "BlameHunk" },
"git_blame_hunk *": { cpp: "GitBlameHunk", js: "BlameHunk" },
"git_filter *": { cpp: "GitFilter", js: "Filter" },
"const git_status_entry *": { cpp: "StatusEntry", js: "StatusEntry" },
"const git_index_name_entry *": {
cpp: "IndexNameEntry", js: "IndexNameEntry" },
"git_time": { cpp: "GitTime", js: "Time", },
"git_vector": { cpp: "Array", js: "Array", },
// unsure
"uint16_t": { cpp: "Integer", js: "Number" },
"git_blame_options": { cpp: "Integer", js: "Number" },
"git_blame_options *": { cpp: "Integer", js: "Number" },
"git_buf *": { cpp: "Buf", js: "Buf" },
"git_branch_iterator *": { cpp: "BranchIterator", js: "Iterator" },
"git_branch_iterator **": { cpp: "BranchIterator", js: "Iterator" },
"git_branch_t *": { cpp: "GitBranch", js: "Branch" },
"const git_commit *[]": { cpp: "Array", js: "Array" },
"git_diff_file": { cpp: "GitDiffFile", js: "DiffFile" },
"git_strarray": { cpp: "Array", js: "Array" },
"git_diff_notify_cb": { cpp: "GitDiffNotifyCb", js: "DiffNotifyCb" },
"unsigned char [20]": { cpp: "Array", js: "Array" },
"git_filter_init_fn": { cpp: "Function", js: "Function" },
"git_filter_shutdown_fn": { cpp: "Function", js: "Function" },
"git_filter_check_fn": { cpp: "Function", js: "Function" },
"git_filter_apply_fn": { cpp: "Function", js: "Function" },
"git_filter_cleanup_fn": { cpp: "Function", js: "Function" },
};
var files = [];
function titleCase(str) {
return str.split(/_|\//).map(function(val, index) {
if (val.length) {
return val[0].toUpperCase() + val.slice(1);
}
return val;
}).join("");
}
function camelCase(str) {
return str.split(/_|\//).map(function(val, index) {
if (val.length) {
return index >= 1 ? val[0].toUpperCase() + val.slice(1) : val;
}
return val;
}).join("");
}
var fileNames = Object.keys(descriptor);
fileNames.forEach(function(fileName, index) {
var file = descriptor[fileName];
var structFile = structs["git_" + fileName];
// Constants.
file.filename = fileName + ".h";
file.ignore = typeof file.ignore == "boolean" ? file.ignore : false;
//FIXME
file.cppClassName = "Git" + titleCase(fileName);
file.jsClassName = file.cppClassName;
// Adjust to find the real the index.
libgit2.files.some(function(currentFile, _index) {
if (currentFile.file === file.filename) {
index = _index;
return true;
}
});
var cFile = libgit2.files[index];
if (file.cType === undefined) {
if (cFile.functions.length) {
file.cType = cFile.functions[0].split("_").slice(0, 2).join("_");
}
}
if (file.cType) {
file.freeFunctionName = "git_" + fileName + "_free";
}
// TODO Obsolete this.
if (file.isStruct) {
// No functions.
cFile = Object.create(file);
cFile.functions = [];
cFile.fields = descriptor[fileName].fields || structFile.fields;
}
// Doesn't actually exist.
if (cFile.functions.indexOf(file.freeFunctionName) === -1) {
delete file.freeFunctionName;
}
var legacyFile = {};
if (file.jsClassName.indexOf("Git") === 0) {
file.jsClassName = file.jsClassName.slice(3);
}
var uniqueTypes = [];
var addType = function(arg) {
if (!arg.type) { return; }
if (arg.type.indexOf("git") === 0) {
var val = arg.type.split(" ")[0].slice(4);
if (uniqueTypes.indexOf(val) === -1 && descriptor[val]) {
uniqueTypes.push(val);
}
}
};
cFile.functions.forEach(function(functionName) {
var funcDescriptor = libgit2.functions[functionName];
var args = funcDescriptor.args || [];
args.forEach(addType);
addType(funcDescriptor.return.type);
});
var deps = file.dependencies;
file.dependencies = uniqueTypes.map(function(file) {
return "../include/" + file + ".h";
});
if (deps) {
file.dependencies.unshift.apply(file.dependencies, deps);
}
// Add to the type's list if it's new.
typeMap["const " + "git_" + fileName + " *"] =
typeMap["const " + "git_" + fileName + " **"] =
typeMap["git_" + fileName + " *"] =
typeMap["git_" + fileName + " **"] =
typeMap["git_" + fileName] = {
cpp: file.cppClassName,
js: file.jsClassName
};
var fields = file.fields || cFile.fields || (structFile ? structFile.fields || [] : []);
// Decorate fields.
file.fields = fields.map(function(field) {
try {
field.cType = field.cType || field.type;
field.cppFunctionName = titleCase(field.name);
field.jsFunctionName = camelCase(field.name);
field.cppClassName = typeMap[field.cType].cpp;
field.jsClassName = typeMap[field.cType].js;
} catch (ex) {
console.error(file.filename, field.cType + " is missing a definition.");
}
return field;
});
// Used to identify a missing function descriptor later on.
var ident = {};
// Cache the existing object, before overwriting.
var functions = file.functions;
var iterateFunctions = Object.keys(file.functions || {});
file.functions = [];
(cFile.functions.length ? cFile.functions : iterateFunctions).forEach(function(functionName, index) {
if (iterateFunctions.indexOf(functionName) === -1) {
return;
}
var funcDescriptor = libgit2.functions[functionName];
var descriptor = {};
var cType = file.cType || "git_" + file.filename.slice(0, -2);
// From the hand maintained file.
var functionDescriptor = functions ? functions[functionName] || ident : ident;
if (!functionName || !funcDescriptor) { return; }
descriptor.cFunctionName = functionName;
descriptor.ignore = functionDescriptor === ident;
if (typeof functionDescriptor.ignore === "boolean") {
descriptor.ignore = functionDescriptor.ignore;
}
var trimmedName = functionName.slice(cType.length + 1);
descriptor.cppFunctionName = functionDescriptor.cppFunctionName || titleCase(trimmedName);
descriptor.jsFunctionName = functionDescriptor.jsFunctionName || camelCase(trimmedName);
// Add to the type's list if it's new.
typeMap["const " + descriptor.cFunctionName + " *"] =
typeMap[descriptor.cFunctionName + " *"] =
typeMap[descriptor.cFunctionName] = {
cpp: descriptor.cppFunctionName,
js: descriptor.jsFunctionName
};
var retVal = descriptor.return = functionDescriptor.return || {};
retVal.cType = funcDescriptor.return.type;
var type = typeMap[retVal.cType];
if (!retVal.cppClassName) {
retVal.cppClassName = type.cpp;
}
if (!retVal.jsClassName) {
retVal.jsClassName = type.js;
}
var args = descriptor.args || [];
var typeDescriptor = args.length ? args[0].type || "" : "";
var isCtor = Boolean(functionDescriptor.isConstructorMethod);
descriptor.isConstructorMethod = isCtor;
// Set the prototype method argument.
descriptor.isPrototypeMethod = !descriptor.isConstructorMethod;
var hasReturn = false;
descriptor.args = funcDescriptor.args.map(function(arg, index) {
var manualArgs = functionDescriptor.args || [];
var isSelf = function() {
if (manualArgs[index] && typeof manualArgs[index].isSelf === "boolean") {
return manualArgs[index].isSelf;
}
if (index === 0) { return false; }
return file.jsClassName === typeMap[arg.type].js;
};
var isOptional = function() {
if (manualArgs[index] && typeof manualArgs[index].isOptional === "boolean") {
return manualArgs[index].isOptional;
}
};
var isReturn = function() {
if (manualArgs[index] && typeof manualArgs[index].isReturn === "boolean") {
return manualArgs[index].isReturn;
}
if (index > 0) { return false; }
if (arg.name === "out" || arg.name.slice(-4) === "_out") {
hasReturn = true;
return true;
}
if ((arg.type || "").replace(/[* ]/g, "") === file.cType) {
hasReturn = true;
return true;
}
return false;
};
if (typeMap[arg.type]) {
var retVal = {
name: arg.name,
cType: arg.type,
cppClassName: typeMap[arg.type].cpp,
jsClassName: typeMap[arg.type].js,
comment: arg.comment,
isReturn: isReturn() || false,
isSelf: isSelf() || false,
shouldAlloc: manualArgs[index] ? manualArgs[index].shouldAlloc || false : false
};
if (manualArgs[index] && manualArgs[index].arrayElementCppClassName) {
retVal.arrayElementCppClassName = manualArgs[index].arrayElementCppClassName;
}
if (manualArgs[index] && manualArgs[index].cType) {
retVal.cType = manualArgs[index].cType;
}
if (manualArgs[index] && manualArgs[index].additionalCast) {
retVal.additionalCast = manualArgs[index].additionalCast;
}
if (isOptional()) {
retVal.isOptional = true;
}
return retVal;
}
throw new Error("Missing typedef for " + arg.type);
});
// By default all methods are synchronous.
descriptor.isAsync = false;
// Determine if retVal isErrorCode.
if (retVal.cType === "int" && hasReturn) {
retVal.isErrorCode = true;
descriptor.isAsync = true;
}
if (typeof functionDescriptor.isAsync === "boolean") {
descriptor.isAsync = functionDescriptor.isAsync;
}
if (Object.keys(functions || {}).indexOf(descriptor.cFunctionName) > -1) {
file.functions.push(descriptor);
}
});
files.push(file);
});
fs.writeFileSync(path.join(__dirname, "types.json"),
JSON.stringify(typeMap, null, 2));
fs.writeFileSync(path.join(__dirname, "idefs.json"),
JSON.stringify(files, null, 2));