-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththermite.js
More file actions
executable file
·173 lines (148 loc) · 4.57 KB
/
thermite.js
File metadata and controls
executable file
·173 lines (148 loc) · 4.57 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
'use strict'
var falafel = require('falafel')
var DiffMatchPatch = require('googlediff')
var survivor = require('survivor')
var functionNodeTypes = ['FunctionExpression', 'FunctionDeclaration']
var blockIDCounter = new Counter('f')
var contextIDCounter = new Counter('c')
var template = ['function $thermiteTemplate() {',
'',
'if(__thermiteMap.$thermiteContextID.$thermiteBlockID.codeVersion >',
' (__thermiteFunctionVersion_$thermiteBlockID || -1)) {',
'',
' __thermiteFunction_$thermiteBlockID =',
' eval(__thermiteMap.$thermiteContextID.$thermiteBlockID.code)',
'',
' __thermiteFunctionVersion_$thermiteBlockID =',
' __thermiteMap.$thermiteContextID.$thermiteBlockID.codeVersion',
'}',
'',
'return __thermiteFunction_$thermiteBlockID.apply(this, arguments)',
'',
'}'].join('\n')
global.__thermiteMap = {}
function Counter(label) {
this.label = label
this.value = 0
}
Counter.prototype.getNextID = function() {
return this.label + (this.value++)
}
function thermiteEval(source, options) {
options = options || {}
var fnTree = {}
var state = {
contextID: contextIDCounter.getNextID(),
lastSource: source,
doLineDiff: options.lineDiff || false,
version: 1
}
var rewritten = forEachNode(source, function(node) {
if(functionNodeTypes.indexOf(node.type) < 0) return
var blockID = blockIDCounter.getNextID()
rewriteNode(state.contextID, blockID, state.version, node)
}).toString()
return {
hotSwap: function(source) { return hotSwap(state, source) },
result: invokeEval(options.eval || eval, rewritten)
}
}
function hotSwap(state, source) {
state.version++
var version = state.version
var contextID = state.contextID
var dmp = new DiffMatchPatch()
var diffs = dmp.diff_main(state.lastSource, source, state.doLineDiff)
var lookup = survivor(diffs, true)
var entries = getEntriesForContext(contextID)
var entriesByRangeString = {}
var persistedBlockIDs = {}
for(var blockID in entries) {
var entry = entries[blockID]
if(entry.deleted) continue
var rangeString = rangeToString(entry.node.range)
entriesByRangeString[rangeString] = entry
}
forEachNode(source, function(node) {
if(functionNodeTypes.indexOf(node.type) < 0) return
var startSurvived = lookup(node.range[0])
var endSurvived = lookup(node.range[1])
var existingEntry = startSurvived
&& endSurvived
&& entriesByRangeString[rangeToString([startSurvived, endSurvived])]
var blockID = existingEntry
? existingEntry.blockID
: blockIDCounter.getNextID()
rewriteNode(contextID, blockID, version, node)
persistedBlockIDs[blockID] = true
})
for(var blockID in entries)
if(!(blockID in persistedBlockIDs))
entries[blockID].deleted = true
state.lastSource = source
}
function getEntriesForContext(contextID) {
return __thermiteMap[contextID] = __thermiteMap[contextID] || {}
}
function invokeEval(evaler, code) {
try {
return evaler(code)
} catch(err) {
console.log('Error evaling code:')
console.log(code)
throw err
}
}
function forEachNode(source, visitor) {
try {
return falafel(source, { ranges: true }, visitor)
} catch(err) {
console.log('Error parsing source:')
console.log(source)
throw err
}
}
function rewriteNode(contextID, blockID, version, node) {
var code = node.source()
var name = ''
// Strip out the name (to handle recursive references)
if(node.id && node.id.name) {
name = node.id.name
code = code.replace(name, '')
node.update(code)
}
var addVersionTo = {
FunctionDeclaration: addVersionToDeclaration,
FunctionExpression: addVersionToExpression
}
var boundTemplate = addVersionTo[node.type](template, name)
.replace(/\$thermiteTemplate/g, name)
.replace(/\$thermiteBlockID/g, blockID)
.replace(/\$thermiteContextID/g, contextID)
node.update(boundTemplate)
var rewritten = '(' + code + ')'
var entries = getEntriesForContext(contextID)
entries[blockID] = {
blockID: blockID,
node: node,
codeVersion: version,
code: rewritten
}
}
function addVersionToDeclaration(template, name) {
return '; '
+ 'var __thermiteFunctionVersion_$thermiteBlockID; '
+ 'var __thermiteFunction_$thermiteBlockID; '
+ template
}
function addVersionToExpression(template, name) {
return '(function ' + name + '() { '
+ 'var __thermiteFunctionVersion_$thermiteBlockID; '
+ 'var __thermiteFunction_$thermiteBlockID; '
+ 'return (' + template + ');'
+ '})()'
}
function rangeToString(range) {
return 'from' + range[0] + 'to' + range[1]
}
exports.eval = thermiteEval