forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
106 lines (96 loc) · 3.35 KB
/
common.js
File metadata and controls
106 lines (96 loc) · 3.35 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
#!/bin/false
// common functions for parsing the JSON comments out of
// the C wrapper files (like common.py)
var fs = require('fs');
var path = require('path');
/// Object representing each builtin
function Builtin(j) {
// remove arrays of description - folw it down with newlines
if ("description" in j)
if (j.description instanceof Array)
j.description = j.description.join("\n\n");
for (var k in j)
this[k] = j[k];
}
Builtin.prototype.getDescription = function() {
var d = "";
if ("description" in this)
d = this.description;
if (d instanceof Array)
d = d.join("\n\n");
return d;
};
Builtin.prototype.getURL = function() {
if (this.type == "class")
anchor = this.class;
else if ("class" in this)
anchor = "l_"+this.class+"_"+this.name;
else
anchor = "l__global_"+this.name;
return "http://www.espruino.com/Reference#"+anchor;
};
/// Tern types - for tern.js json format
function getBasicTernType(t) {
if (["int","float","int32"].indexOf(t)>=0) return "number";
if (t=="pin") return "+Pin";
if (t=="bool") return "bool";
if (t=="JsVarArray") return "?"; // TODO: not right. Should be variable arg count
return "?";
}
/// Get full Tern type - for tern.js json format
Builtin.prototype.getTernType = function() {
if (["class","library"].indexOf(this.type)>=0) {
return "fn()";
} else if (["function","method","staticmethod","constructor"].indexOf(this.type)>=0) {
// it's a function
var args = [];
if ("params" in this)
args = this.params.map(function (p) {
if (p[0]=="pin" && p[1]=="JsVar")
return "pin: +Pin"; // hack because digitalRead/Write can also take arrays/objects (but most use cases are Pins)
return p[0]+": "+getBasicTernType(p[1]);
});
var ret = "";
if ("return_object" in this)
ret = " -> +"+this.return_object
else if ("return" in this)
ret = " -> "+getBasicTernType(this.return[0]);
return "fn("+args.join("\, ")+")"+ret;
} else {
return getBasicTernType(this.return[0]);
}
};
/// Get any files that we think might contain 'JSON' tags
exports.getWrapperFiles = function (callback) {
var BASEDIR = path.resolve(__dirname, "..");
require('child_process').exec("find "+BASEDIR+" -name jswrap*.c", function(error, stdout, stderr) {
callback(stdout.toString().trim().split("\n"));
});
}
/// Extract the /*JSON ... */ comments from a file and parse them
exports.readWrapperFile = function(filename) {
var contents = fs.readFileSync(filename).toString();
var builtins = [];
var comments = contents.match( /\/\*JSON(?:(?!\*\/).|[\n\r])*\*\//g );
if (comments) comments.forEach(function(comment) {
comment = comment.slice(6,-2); // pull off /*JSON ... */ bit
var endOfJson = comment.indexOf("\n}")+2;
var json = comment.substr(0,endOfJson);
var description = comment.substr(endOfJson).trim();
var j = new Builtin(JSON.parse(json));
if (description.length) j.description = description;
j.implementation = filename;
builtins.push(j);
});
return builtins;
}
/// Extract all parsed /*JSON ... */ comments from all files
exports.readAllWrapperFiles = function(callback) {
exports.getWrapperFiles(function(files) {
var builtins = [];
files.forEach(function(filename) {
builtins = builtins.concat(exports.readWrapperFile(filename));
});
callback(builtins);
});
}