-
Notifications
You must be signed in to change notification settings - Fork 47
/
helper.ts
209 lines (186 loc) · 5.62 KB
/
helper.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
import * as estree from 'estree'
import Scope from '../scope'
import evaluate from '../evaluate'
import { define, inherits, runGenerator } from './util'
import { RETURN, SUPER } from './const'
import { BlockStatement } from '../evaluate/statement'
import { FunctionDeclaration, VariableDeclaration, ClassBody } from '../evaluate/declaration'
import { Identifier } from '../evaluate/identifier'
import {
PatternOptions,
ObjectPattern,
ArrayPattern,
RestElement,
AssignmentPattern,
} from '../evaluate/pattern'
export function* hoist(block: estree.Program | estree.BlockStatement, scope: Scope) {
for (let i = 0; i < block.body.length; i++) {
const statement = block.body[i]
if (
statement.type === 'ImportDeclaration'
|| statement.type === 'ExportNamedDeclaration'
|| statement.type === 'ExportDefaultDeclaration'
|| statement.type === 'ExportAllDeclaration'
) {
continue
}
if (statement.type === 'FunctionDeclaration') {
yield* FunctionDeclaration(statement, scope)
// Avoid duplicate declaration
block.body[i] = null
} else {
yield* hoistVarRecursion(statement, scope)
}
}
}
export function* hoistFunc(block: estree.BlockStatement, scope: Scope) {
for (let i = 0; i < block.body.length; i++) {
const statement = block.body[i]
if (statement.type === 'FunctionDeclaration') {
yield* FunctionDeclaration(statement, scope)
// Avoid duplicate declaration
block.body[i] = null
}
}
}
function* hoistVarRecursion(statement: estree.Statement, scope: Scope): IterableIterator<any> {
switch (statement.type) {
case 'VariableDeclaration':
yield* VariableDeclaration(statement, scope, { hoist: true })
break
case 'WhileStatement':
case 'DoWhileStatement':
case 'ForStatement':
case 'ForInStatement':
case 'ForOfStatement':
yield* hoistVarRecursion(statement.body, scope)
break
case 'BlockStatement':
for (const node of statement.body) {
yield* hoistVarRecursion(node, scope)
}
break
case 'SwitchStatement':
for (const eachCase of statement.cases) {
for (const node of eachCase.consequent) {
yield* hoistVarRecursion(node, scope)
}
}
break
case 'TryStatement': {
const tryBlock = statement.block.body
for (const node of tryBlock) {
yield* hoistVarRecursion(node, scope)
}
const catchBlock = statement.handler && statement.handler.body.body
if (catchBlock) {
for (const node of catchBlock) {
yield* hoistVarRecursion(node, scope)
}
}
const finalBlock = statement.finalizer && statement.finalizer.body
if (finalBlock) {
for (const node of finalBlock) {
yield* hoistVarRecursion(node, scope)
}
}
break
}
}
}
export function* pattern(node: estree.Pattern, scope: Scope, options: PatternOptions = {}): IterableIterator<any> {
switch (node.type) {
case 'ObjectPattern':
return yield* ObjectPattern(node, scope, options)
case 'ArrayPattern':
return yield* ArrayPattern(node, scope, options)
case 'RestElement':
return yield* RestElement(node, scope, options)
case 'AssignmentPattern':
return yield* AssignmentPattern(node, scope)
default:
throw new SyntaxError('Unexpected token')
}
}
export interface CtorOptions {
superClass?: (...args: any[]) => any
}
export function* createFunc(
node: estree.FunctionDeclaration | estree.FunctionExpression | estree.ArrowFunctionExpression,
scope: Scope,
options: CtorOptions = {}
) {
const { superClass } = options
const params = node.params
const tmpGenerator = function* (...args: any[]) {
let subScope: Scope
if (node.type !== 'ArrowFunctionExpression') {
subScope = new Scope(scope, true)
subScope.const('this', this)
subScope.let('arguments', arguments)
if (superClass) {
subScope.const(SUPER, superClass)
}
} else {
subScope = new Scope(scope)
}
for (let i = 0; i < params.length; i++) {
const param = params[i]
if (param.type === 'Identifier') {
const name = yield* Identifier(param, scope, { getName: true })
subScope.let(name, args[i])
} else {
yield* pattern(param, scope, { feed: args[i] })
}
}
let result: any
if (node.body.type === 'BlockStatement') {
yield* hoist(node.body, subScope)
result = yield* BlockStatement(node.body, subScope, {
invasived: true,
hoisted: true,
})
} else {
result = yield* evaluate(node.body, subScope)
}
if (result === RETURN) {
return result.RES
}
}
const func: any = node.generator
? tmpGenerator
: function (...args: any[]) { return runGenerator(tmpGenerator.bind(this), ...args) }
if (node.type === 'FunctionDeclaration') {
define(func, 'name', {
value: node.id.name,
configurable: true,
})
}
define(func, 'length', {
value: params.length,
configurable: true,
})
return func
}
export function* createClass(
node: estree.ClassDeclaration | estree.ClassExpression,
scope: Scope,
) {
const superClass = yield* evaluate(node.superClass, scope)
let klass: (...args: any[]) => any = function () { }
for (const method of node.body.body) {
if (method.kind === 'constructor') {
klass = yield* createFunc(method.value, scope, { superClass })
break
}
}
if (superClass) {
inherits(klass, superClass)
}
yield* ClassBody(node.body, scope, { klass })
define(klass, 'name', {
value: node.id.name,
configurable: true,
})
return klass
}