Commit 184bf886b55ace71c3b15786ecd494c3973ca8d6
Remove ENV/MOD workaround
Alex Beregszaszi committed on 8/3/2016, 6:25:35 PMParent: 85f7c3ade0ebc8c21a6734cf20824c455a7e572b
Files changed
interface.js | changed |
interface.js | ||
---|---|---|
@@ -2,25 +2,21 @@ | ||
2 | 2 | * This is the Ethereum interface that is exposed to the WASM instance which |
3 | 3 | * enables to interact with the Ethereum Environment |
4 | 4 | */ |
5 | 5 | const constants = require('./constants.js') |
6 | -// const Graph = require('generic-digraph') | |
7 | -// function.bind is not working corretly whith Wasm imports. So instead create | |
8 | -// a global for now. TODO REMOVE | |
9 | -let ENV | |
10 | -let MOD | |
6 | + | |
11 | 7 | // The interface exposed to the WebAessembly Core |
12 | 8 | module.exports = class Interface { |
13 | 9 | print (a) { |
14 | 10 | console.log(a) |
15 | 11 | } |
16 | 12 | |
17 | 13 | memPrint () { |
18 | - console.log((new Uint8Array(MOD.exports.memory)).toString()) | |
14 | + console.log((new Uint8Array(this.module.exports.memory)).toString()) | |
19 | 15 | } |
20 | 16 | |
21 | 17 | constructor (environment) { |
22 | - ENV = this.environment = environment | |
18 | + this.environment = environment | |
23 | 19 | } |
24 | 20 | |
25 | 21 | get exportTable () { |
26 | 22 | let exportMethods = [ |
@@ -63,41 +59,41 @@ | ||
63 | 59 | } |
64 | 60 | |
65 | 61 | // FIXME: this shouldn't be needed |
66 | 62 | get env () { |
67 | - return ENV | |
63 | + return this.environment | |
68 | 64 | } |
69 | 65 | |
70 | 66 | setModule (mod) { |
71 | - this.module = MOD = mod | |
67 | + this.module = mod | |
72 | 68 | } |
73 | 69 | |
74 | 70 | /** |
75 | 71 | * Subtracts an amount to the gas counter |
76 | 72 | * @param {integer} amount the amount to subtract to the gas counter |
77 | 73 | */ |
78 | 74 | useGas (amount) { |
79 | 75 | if (amount > 0) { |
80 | - ENV.gasLimit -= amount | |
76 | + this.environment.gasLimit -= amount | |
81 | 77 | } |
82 | 78 | } |
83 | 79 | |
84 | 80 | /** |
85 | 81 | * Returns the current amount of gas |
86 | 82 | * @return {integer} |
87 | 83 | */ |
88 | 84 | gas () { |
89 | - return ENV.gasLimit | |
85 | + return this.environment.gasLimit | |
90 | 86 | } |
91 | 87 | |
92 | 88 | /** |
93 | 89 | * Gets address of currently executing account and loads it into memory at |
94 | 90 | * the given offset. |
95 | 91 | * @param {integer} offset |
96 | 92 | */ |
97 | 93 | address (offset) { |
98 | - const address = ENV.address | |
99 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
94 | + const address = this.environment.address | |
95 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
100 | 96 | memory.set(address) |
101 | 97 | } |
102 | 98 | |
103 | 99 | /** |
@@ -106,12 +102,12 @@ | ||
106 | 102 | * @param {integer} addressOffset the memory offset to laod the address |
107 | 103 | * @param {integer} resultOffset |
108 | 104 | */ |
109 | 105 | balance (addressOffset, offset) { |
110 | - const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
111 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.MAX_BAL_BYTES) | |
106 | + const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
107 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.MAX_BAL_BYTES) | |
112 | 108 | // call the parent contract and ask for the balance of one of its child contracts |
113 | - const balance = ENV.parent.environment.getBalance(address) | |
109 | + const balance = this.environment.parent.environment.getBalance(address) | |
114 | 110 | memory.set(balance) |
115 | 111 | } |
116 | 112 | |
117 | 113 | /** |
@@ -120,10 +116,10 @@ | ||
120 | 116 | * account with non-empty associated code. |
121 | 117 | * @param {integer} offset |
122 | 118 | */ |
123 | 119 | origin (offset) { |
124 | - const origin = ENV.origin | |
125 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
120 | + const origin = this.environment.origin | |
121 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
126 | 122 | memory.set(origin) |
127 | 123 | } |
128 | 124 | |
129 | 125 | /** |
@@ -131,10 +127,10 @@ | ||
131 | 127 | * the address of the account that is directly responsible for this execution. |
132 | 128 | * @param {integer} offset |
133 | 129 | */ |
134 | 130 | caller (offset) { |
135 | - const caller = ENV.caller | |
136 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
131 | + const caller = this.environment.caller | |
132 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
137 | 133 | memory.set(caller) |
138 | 134 | } |
139 | 135 | |
140 | 136 | /** |
@@ -142,10 +138,10 @@ | ||
142 | 138 | * this execution and loads it into memory at the given location. |
143 | 139 | * @param {integer} offset |
144 | 140 | */ |
145 | 141 | callValue (offset) { |
146 | - const callValue = ENV.callValue | |
147 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.MAX_BAL_BYTES) | |
142 | + const callValue = this.environment.callValue | |
143 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.MAX_BAL_BYTES) | |
148 | 144 | memory.set(callValue) |
149 | 145 | } |
150 | 146 | |
151 | 147 | /** |
@@ -153,9 +149,9 @@ | ||
153 | 149 | * data passed with the message call instruction or transaction. |
154 | 150 | * @return {integer} |
155 | 151 | */ |
156 | 152 | callDataSize () { |
157 | - return ENV.callData.byteLength | |
153 | + return this.environment.callData.byteLength | |
158 | 154 | } |
159 | 155 | |
160 | 156 | /** |
161 | 157 | * Copys the input data in current environment to memory. This pertains to |
@@ -164,19 +160,19 @@ | ||
164 | 160 | * @param {integer} dataOffset the offset in the input data |
165 | 161 | * @param {integer} length the length of data to copy |
166 | 162 | */ |
167 | 163 | callDataCopy (offset, dataOffset, length) { |
168 | - const callData = new Uint8Array(ENV.callData, offset, length) | |
169 | - const memory = new Uint8Array(MOD.exports.memory, offset, length) | |
164 | + const callData = new Uint8Array(this.environment.callData, offset, length) | |
165 | + const memory = new Uint8Array(this.module.exports.memory, offset, length) | |
170 | 166 | memory.set(callData) |
171 | 167 | } |
172 | 168 | |
173 | 169 | /** |
174 | 170 | * Gets the size of code running in current environment. |
175 | 171 | * @return {interger} |
176 | 172 | */ |
177 | 173 | codeSize () { |
178 | - return ENV.code.byteLength | |
174 | + return this.environment.code.byteLength | |
179 | 175 | } |
180 | 176 | |
181 | 177 | /** |
182 | 178 | * Copys the code running in current environment to memory. |
@@ -184,10 +180,10 @@ | ||
184 | 180 | * @param {integer} codeOffset the code offset |
185 | 181 | * @param {integer} length the length of code to copy |
186 | 182 | */ |
187 | 183 | codeCopy (offset, codeOffset, length) { |
188 | - const code = new Uint8Array(ENV.code, codeOffset, length) | |
189 | - const memory = new Uint8Array(MOD.exports.memory, offset, length) | |
184 | + const code = new Uint8Array(this.environment.code, codeOffset, length) | |
185 | + const memory = new Uint8Array(this.module.exports.memory, offset, length) | |
190 | 186 | memory.set(code) |
191 | 187 | } |
192 | 188 | |
193 | 189 | /** |
@@ -195,9 +191,9 @@ | ||
195 | 191 | * @param {integer} addressOffset the offset in memory to load the address from |
196 | 192 | * @return {integer} |
197 | 193 | */ |
198 | 194 | extCodeSize (addressOffset) { |
199 | - const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
195 | + const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
200 | 196 | const code = this.environment.getCode(address) |
201 | 197 | return code.byteLength |
202 | 198 | } |
203 | 199 | |
@@ -208,21 +204,21 @@ | ||
208 | 204 | * @param {integer} codeOffset the code offset |
209 | 205 | * @param {integer} length the length of code to copy |
210 | 206 | */ |
211 | 207 | extCodeCopy (addressOffset, offset, codeOffset, length) { |
212 | - const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
208 | + const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
213 | 209 | let code = this.environment.getCode(address) |
214 | 210 | code = new Uint8Array(code, codeOffset, length) |
215 | - const memory = new Uint8Array(MOD.exports.memory, offset, length) | |
211 | + const memory = new Uint8Array(this.module.exports.memory, offset, length) | |
216 | 212 | memory.set(code) |
217 | 213 | } |
218 | 214 | |
219 | 215 | /** |
220 | 216 | * Gets price of gas in current environment. |
221 | 217 | * @return {integer} |
222 | 218 | */ |
223 | 219 | gasPrice () { |
224 | - return ENV.gasPrice | |
220 | + return this.environment.gasPrice | |
225 | 221 | } |
226 | 222 | |
227 | 223 | /** |
228 | 224 | * Gets the hash of one of the 256 most recent complete blocks. |
@@ -230,51 +226,51 @@ | ||
230 | 226 | * @param {integer} offset the offset to load the hash into |
231 | 227 | */ |
232 | 228 | blockHash (number, offset) { |
233 | 229 | const hash = this.environment.getBlockHash(number) |
234 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
230 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
235 | 231 | memory.set(hash) |
236 | 232 | } |
237 | 233 | |
238 | 234 | /** |
239 | 235 | * Gets the block’s beneficiary address and loads into memory. |
240 | 236 | * @param offset |
241 | 237 | */ |
242 | 238 | coinbase (offset) { |
243 | - const memory = new Uint8Array(MOD.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
244 | - memory.set(ENV.coinbase) | |
239 | + const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES) | |
240 | + memory.set(this.environment.coinbase) | |
245 | 241 | } |
246 | 242 | |
247 | 243 | /** |
248 | 244 | * Get the block’s timestamp. |
249 | 245 | * @return {integer} |
250 | 246 | */ |
251 | 247 | timestamp () { |
252 | - return ENV.timestamp | |
248 | + return this.environment.timestamp | |
253 | 249 | } |
254 | 250 | |
255 | 251 | /** |
256 | 252 | * Get the block’s number. |
257 | 253 | * @return {integer} |
258 | 254 | */ |
259 | 255 | number () { |
260 | - return ENV.number | |
256 | + return this.environment.number | |
261 | 257 | } |
262 | 258 | |
263 | 259 | /** |
264 | 260 | * Get the block’s difficulty. |
265 | 261 | * @return {integer} |
266 | 262 | */ |
267 | 263 | difficulty () { |
268 | - return ENV.difficulty | |
264 | + return this.environment.difficulty | |
269 | 265 | } |
270 | 266 | |
271 | 267 | /** |
272 | 268 | * Get the block’s gas limit. |
273 | 269 | * @return {integer} |
274 | 270 | */ |
275 | 271 | gasLimit () { |
276 | - return ENV.gasLimit | |
272 | + return this.environment.gasLimit | |
277 | 273 | } |
278 | 274 | |
279 | 275 | /** |
280 | 276 | * Creates a new log in the current environment |
@@ -282,10 +278,10 @@ | ||
282 | 278 | * @param {integer} length the data length |
283 | 279 | * TODO: replace with variadic |
284 | 280 | */ |
285 | 281 | log (dataOffset, length, topic1, topic2, topic3, topic4, topic5) { |
286 | - const data = new Uint8Array(MOD.exports.memory, dataOffset, length) | |
287 | - ENV.logs.push({ | |
282 | + const data = new Uint8Array(this.module.exports.memory, dataOffset, length) | |
283 | + this.environment.logs.push({ | |
288 | 284 | data: data, |
289 | 285 | topics: [topic1, topic2, topic3, topic4, topic5] |
290 | 286 | }) |
291 | 287 | } |
@@ -296,11 +292,11 @@ | ||
296 | 292 | * @param {integer} dataOffset the offset to load the code for the new contract from |
297 | 293 | * @param {integer} length the data length |
298 | 294 | */ |
299 | 295 | create (valueOffset, dataOffset, length) { |
300 | - const value = new Uint8Array(MOD.exports.memory, valueOffset, constants.MAX_BAL_BYTES) | |
301 | - const data = new Uint8Array(MOD.exports.memory, dataOffset, length) | |
302 | - const result = ENV.create(value, data) | |
296 | + const value = new Uint8Array(this.module.exports.memory, valueOffset, constants.MAX_BAL_BYTES) | |
297 | + const data = new Uint8Array(this.module.exports.memory, dataOffset, length) | |
298 | + const result = this.environment.create(value, data) | |
303 | 299 | return result |
304 | 300 | } |
305 | 301 | |
306 | 302 | /** |
@@ -319,14 +315,14 @@ | ||
319 | 315 | if (gas === undefined) { |
320 | 316 | gas = this.gasLeft() |
321 | 317 | } |
322 | 318 | // Load the params from mem |
323 | - const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
324 | - const value = new Uint8Array(MOD.exports.memory, valueOffset, constants.MAX_BAL_BYTES) | |
325 | - const data = new Uint8Array(MOD.exports.memory, dataOffset, dataLength) | |
319 | + const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
320 | + const value = new Uint8Array(this.module.exports.memory, valueOffset, constants.MAX_BAL_BYTES) | |
321 | + const data = new Uint8Array(this.module.exports.memory, dataOffset, dataLength) | |
326 | 322 | // Run the call |
327 | - const [result, errorCode] = ENV.call(gas, address, value, data) | |
328 | - const memory = new Uint8Array(MOD.exports.memory, resultOffset, resultLength) | |
323 | + const [result, errorCode] = this.environment.call(gas, address, value, data) | |
324 | + const memory = new Uint8Array(this.module.exports.memory, resultOffset, resultLength) | |
329 | 325 | memory.set(result) |
330 | 326 | |
331 | 327 | return errorCode |
332 | 328 | } |
@@ -343,12 +339,12 @@ | ||
343 | 339 | * @param {integer} resultLength |
344 | 340 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
345 | 341 | */ |
346 | 342 | callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) { |
347 | - const data = new Uint8Array(MOD.exports.memory, dataOffset, dataLength) | |
348 | - const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
343 | + const data = new Uint8Array(this.module.exports.memory, dataOffset, dataLength) | |
344 | + const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
349 | 345 | const [result, errorCode] = this.environment.callDelegate(gas, address, data) |
350 | - const memory = new Uint8Array(MOD.exports.memory, resultOffset, resultLength) | |
346 | + const memory = new Uint8Array(this.module.exports.memory, resultOffset, resultLength) | |
351 | 347 | memory.set(result) |
352 | 348 | |
353 | 349 | return errorCode |
354 | 350 | } |
@@ -359,24 +355,24 @@ | ||
359 | 355 | * @param {interger} pathOffest the memory offset to load the the path from |
360 | 356 | * @param {interger} valueOffset the memory offset to load the value from |
361 | 357 | */ |
362 | 358 | sstore (pathOffest, valueOffset) { |
363 | - const path = new Buffer(MOD.exports.memory, pathOffest, 32).toString('hex') | |
364 | - const value = new Uint8Array(MOD.exports.memory, valueOffset, 32) | |
365 | - const oldValue = ENV.state.get(path) | |
359 | + const path = new Buffer(this.module.exports.memory, pathOffest, 32).toString('hex') | |
360 | + const value = new Uint8Array(this.module.exports.memory, valueOffset, 32) | |
361 | + const oldValue = this.environment.state.get(path) | |
366 | 362 | const valIsZero = value.every((i) => i === 0) |
367 | 363 | |
368 | 364 | // write |
369 | 365 | if (!valIsZero && !oldValue) { |
370 | - ENV.gasLimit -= 15000 | |
366 | + this.environment.gasLimit -= 15000 | |
371 | 367 | } |
372 | 368 | |
373 | 369 | // delete |
374 | 370 | if (valIsZero && oldValue) { |
375 | - ENV.gasRefund += 15000 | |
376 | - ENV.state.delete(path) | |
371 | + this.environment.gasRefund += 15000 | |
372 | + this.environment.state.delete(path) | |
377 | 373 | } else { |
378 | - ENV.state.set(path, value) | |
374 | + this.environment.state.set(path, value) | |
379 | 375 | } |
380 | 376 | } |
381 | 377 | |
382 | 378 | /** |
@@ -384,11 +380,11 @@ | ||
384 | 380 | * @param {interger} pathOffest the memory offset to load the the path from |
385 | 381 | * @param {interger} resultOffset the memory offset to load the value from |
386 | 382 | */ |
387 | 383 | sload (pathOffest, resultOffset) { |
388 | - const path = new Buffer(MOD.exports.memory, pathOffest, 32).toString('hex') | |
389 | - const result = ENV.state.get(path) | |
390 | - const memory = new Uint8Array(MOD.exports.memory, resultOffset, 32) | |
384 | + const path = new Buffer(this.module.exports.memory, pathOffest, 32).toString('hex') | |
385 | + const result = this.environment.state.get(path) | |
386 | + const memory = new Uint8Array(this.module.exports.memory, resultOffset, 32) | |
391 | 387 | memory.set(result) |
392 | 388 | } |
393 | 389 | |
394 | 390 | /** |
@@ -396,18 +392,18 @@ | ||
396 | 392 | * @param {integer} offset the offset of the output data. |
397 | 393 | * @param {integer} length the length of the output data. |
398 | 394 | */ |
399 | 395 | return (offset, length) { |
400 | - ENV.returnValue = new Uint8Array(MOD.exports.memory, offset, length) | |
396 | + this.environment.returnValue = new Uint8Array(this.module.exports.memory, offset, length) | |
401 | 397 | } |
402 | 398 | |
403 | 399 | /** |
404 | 400 | * Halt execution and register account for later deletion giving the remaining |
405 | 401 | * balance to an address path |
406 | 402 | * @param {integer} offset the offset to load the address from |
407 | 403 | */ |
408 | 404 | suicide (addressOffset) { |
409 | - const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
405 | + const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES) | |
410 | 406 | this.environment.suicideAddress = address |
411 | 407 | } |
412 | 408 | } |
413 | 409 |
Built with git-ssb-web