-
Notifications
You must be signed in to change notification settings - Fork 587
/
Copy pathsimshim.ts
219 lines (193 loc) · 8.08 KB
/
simshim.ts
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
/// <reference path='../built/pxtlib.d.ts' />
namespace pxt {
export function simshim(prog: ts.Program, pathParse: any): pxt.Map<string> {
let SK = ts.SyntaxKind
let checker = prog.getTypeChecker()
let mainWr = cpp.nsWriter("declare namespace")
let currNs = ""
let currMod: ts.ModuleDeclaration
for (let src of prog.getSourceFiles()) {
if (pathParse) {
let pp = pathParse(src.fileName);
pxt.debug("SimShim[1]: " + pp.dir)
if (!U.endsWith(pp.dir, "/sim") && !U.startsWith(src.fileName, "sim/"))
continue;
} else if (!U.startsWith(src.fileName, "sim/"))
continue;
pxt.debug("SimShim[2]: " + src.fileName)
for (let stmt of src.statements) {
let mod = stmt as ts.ModuleDeclaration
if (stmt.kind == SK.ModuleDeclaration && mod.name.text == "pxsim") {
currMod = mod
doStmt(mod.body as ts.ModuleBlock)
}
}
}
let res: pxt.Map<string> = {}
res[appTarget.corepkg] = mainWr.finish()
return res
function typeOf(node: ts.Node) {
let r: ts.Type;
if (ts.isExpression(node))
r = checker.getContextualType(<ts.Expression>node)
if (!r) r = checker.getTypeAtLocation(node);
return r
}
/*
let doSymbol = (sym: ts.Symbol) => {
if (sym.getFlags() & ts.SymbolFlags.HasExports) {
typechecker.getExportsOfModule(sym).forEach(doSymbol)
}
decls[pxtc.getFullName(typechecker, sym)] = sym
}
*/
function emitModuleDeclaration(mod: ts.ModuleDeclaration) {
let prevNs = currNs
if (currNs) currNs += "."
currNs += mod.name.text
doStmt(mod.body as ts.ModuleBlock)
currNs = prevNs
}
function mapType(tp: ts.Type) {
let fn = checker.typeToString(tp, currMod, ts.TypeFormatFlags.UseFullyQualifiedType)
fn = fn.replace(/^pxsim\./, "")
switch (fn) {
case "RefAction": return "() => void";
case "RefBuffer": return "Buffer";
default:
return fn
}
}
function promiseElementType(tp: ts.Type) {
if (pxtc.isObjectType(tp) && (tp.objectFlags & ts.ObjectFlags.Reference) && tp.symbol.name == "Promise") {
return (tp as ts.TypeReference).typeArguments[0]
}
return null
}
function emitClassDeclaration(cl: ts.ClassDeclaration) {
let cmts = getExportComments(cl)
if (!cmts) return
mainWr.setNs(currNs)
mainWr.write(cmts)
let prevNs = currNs
if (currNs) currNs += "."
currNs += cl.name.text
let decl = prevNs ? "" : "declare"
let ext = ""
if (cl.heritageClauses)
for (let h of cl.heritageClauses) {
if (h.token == SK.ExtendsKeyword) {
ext = " extends " + mapType(typeOf(h.types[0]))
}
}
mainWr.write(`${decl} class ${cl.name.text}${ext} {`)
mainWr.incrIndent()
for (let mem of cl.members) {
switch (mem.kind) {
case SK.MethodDeclaration:
emitFunctionDeclaration(mem as ts.MethodDeclaration)
break
case SK.PropertyDeclaration:
emitPropertyDeclaration(mem as ts.PropertyDeclaration)
break
case SK.Constructor:
emitConstructorDeclaration(mem as ts.ConstructorDeclaration)
break
case SK.GetAccessor:
let hasSetter = cl.members.some(m => m.kind == SK.SetAccessor && m.name.getText() == mem.name.getText())
emitFunctionDeclaration(mem as ts.GetAccessorDeclaration, hasSetter)
break
default:
break;
}
}
currNs = prevNs
mainWr.decrIndent()
mainWr.write(`}`)
}
function getExportComments(n: ts.Node) {
let cmts = pxtc.getComments(n)
if (!/^\s*\/\/%/m.test(cmts)) return null
return cmts
}
function emitPropertyDeclaration(fn: ts.PropertyDeclaration) {
let cmts = getExportComments(fn)
if (!cmts) return
let nm = fn.name.getText()
let attrs = "//% shim=." + nm
let tp = checker.getTypeAtLocation(fn)
mainWr.write(cmts)
mainWr.write(attrs)
mainWr.write(`public ${nm}: ${mapType(tp)};`)
mainWr.write("")
}
function emitConstructorDeclaration(fn: ts.ConstructorDeclaration) {
let cmts = getExportComments(fn)
if (!cmts) return
let tp = checker.getTypeAtLocation(fn)
let args = fn.parameters.map(p => p.name.getText() + ": " + mapType(typeOf(p)))
mainWr.write(cmts)
mainWr.write(`//% shim="new ${currNs}"`)
mainWr.write(`constructor(${args.join(", ")});`)
mainWr.write("")
}
function emitFunctionDeclaration(fn: ts.FunctionLikeDeclaration, hasSetter = false) {
let cmts = getExportComments(fn)
if (!cmts) return
let fnname = fn.name.getText()
let isMethod = fn.kind == SK.MethodDeclaration || fn.kind == SK.GetAccessor || fn.kind == SK.SetAccessor
let attrs = "//% shim=" + (isMethod ? "." + fnname : currNs + "::" + fnname)
let sig = checker.getSignatureFromDeclaration(fn)
let rettp = checker.getReturnTypeOfSignature(sig)
let asyncName = /Async$/.test(fnname)
let prom = promiseElementType(rettp)
if (prom) {
attrs += " promise"
rettp = prom
if (!asyncName)
U.userError(`${currNs}::${fnname} should be called ${fnname}Async`)
} else if (asyncName) {
U.userError(`${currNs}::${fnname} doesn't return a promise`)
}
pxt.debug("emitFun: " + fnname)
let args = fn.parameters.map(p => {
return `${p.name.getText()}${p.questionToken ? "?" : ""}: ${mapType(typeOf(p))}`
})
let localname = fnname.replace(/Async$/, "")
let defkw = isMethod ? "public" : "function"
let allArgs = `(${args.join(", ")})`
if (fn.kind == SK.GetAccessor) {
defkw = hasSetter ? "public" : "readonly"
allArgs = ""
attrs += " property"
}
if (!isMethod)
mainWr.setNs(currNs)
mainWr.write(cmts)
mainWr.write(attrs)
mainWr.write(`${defkw} ${localname}${allArgs}: ${mapType(rettp)};`)
mainWr.write("")
}
function doStmt(stmt: ts.Statement) {
switch (stmt.kind) {
case SK.ModuleDeclaration:
return emitModuleDeclaration(stmt as ts.ModuleDeclaration)
case SK.ModuleBlock:
return (stmt as ts.ModuleBlock).statements.forEach(doStmt)
case SK.FunctionDeclaration:
return emitFunctionDeclaration(stmt as ts.FunctionDeclaration)
case SK.ClassDeclaration:
return emitClassDeclaration(stmt as ts.ClassDeclaration)
}
//pxt.log("SKIP", pxtc.stringKind(stmt))
//let mod = stmt as ts.ModuleDeclaration
//if (mod.name) pxt.log(mod.name.text)
/*
if (mod.name) {
let sym = typechecker.getSymbolAtLocation(mod.name)
if (sym) doSymbol(sym)
}
*/
}
}
}