git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 184bf886b55ace71c3b15786ecd494c3973ca8d6

Remove ENV/MOD workaround

Alex Beregszaszi committed on 8/3/2016, 6:25:35 PM
Parent: 85f7c3ade0ebc8c21a6734cf20824c455a7e572b

Files changed

interface.jschanged
interface.jsView
@@ -2,25 +2,21 @@
22 * This is the Ethereum interface that is exposed to the WASM instance which
33 * enables to interact with the Ethereum Environment
44 */
55 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+
117 // The interface exposed to the WebAessembly Core
128 module.exports = class Interface {
139 print (a) {
1410 console.log(a)
1511 }
1612
1713 memPrint () {
18- console.log((new Uint8Array(MOD.exports.memory)).toString())
14+ console.log((new Uint8Array(this.module.exports.memory)).toString())
1915 }
2016
2117 constructor (environment) {
22- ENV = this.environment = environment
18+ this.environment = environment
2319 }
2420
2521 get exportTable () {
2622 let exportMethods = [
@@ -63,41 +59,41 @@
6359 }
6460
6561 // FIXME: this shouldn't be needed
6662 get env () {
67- return ENV
63+ return this.environment
6864 }
6965
7066 setModule (mod) {
71- this.module = MOD = mod
67+ this.module = mod
7268 }
7369
7470 /**
7571 * Subtracts an amount to the gas counter
7672 * @param {integer} amount the amount to subtract to the gas counter
7773 */
7874 useGas (amount) {
7975 if (amount > 0) {
80- ENV.gasLimit -= amount
76+ this.environment.gasLimit -= amount
8177 }
8278 }
8379
8480 /**
8581 * Returns the current amount of gas
8682 * @return {integer}
8783 */
8884 gas () {
89- return ENV.gasLimit
85+ return this.environment.gasLimit
9086 }
9187
9288 /**
9389 * Gets address of currently executing account and loads it into memory at
9490 * the given offset.
9591 * @param {integer} offset
9692 */
9793 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)
10096 memory.set(address)
10197 }
10298
10399 /**
@@ -106,12 +102,12 @@
106102 * @param {integer} addressOffset the memory offset to laod the address
107103 * @param {integer} resultOffset
108104 */
109105 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)
112108 // 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)
114110 memory.set(balance)
115111 }
116112
117113 /**
@@ -120,10 +116,10 @@
120116 * account with non-empty associated code.
121117 * @param {integer} offset
122118 */
123119 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)
126122 memory.set(origin)
127123 }
128124
129125 /**
@@ -131,10 +127,10 @@
131127 * the address of the account that is directly responsible for this execution.
132128 * @param {integer} offset
133129 */
134130 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)
137133 memory.set(caller)
138134 }
139135
140136 /**
@@ -142,10 +138,10 @@
142138 * this execution and loads it into memory at the given location.
143139 * @param {integer} offset
144140 */
145141 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)
148144 memory.set(callValue)
149145 }
150146
151147 /**
@@ -153,9 +149,9 @@
153149 * data passed with the message call instruction or transaction.
154150 * @return {integer}
155151 */
156152 callDataSize () {
157- return ENV.callData.byteLength
153+ return this.environment.callData.byteLength
158154 }
159155
160156 /**
161157 * Copys the input data in current environment to memory. This pertains to
@@ -164,19 +160,19 @@
164160 * @param {integer} dataOffset the offset in the input data
165161 * @param {integer} length the length of data to copy
166162 */
167163 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)
170166 memory.set(callData)
171167 }
172168
173169 /**
174170 * Gets the size of code running in current environment.
175171 * @return {interger}
176172 */
177173 codeSize () {
178- return ENV.code.byteLength
174+ return this.environment.code.byteLength
179175 }
180176
181177 /**
182178 * Copys the code running in current environment to memory.
@@ -184,10 +180,10 @@
184180 * @param {integer} codeOffset the code offset
185181 * @param {integer} length the length of code to copy
186182 */
187183 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)
190186 memory.set(code)
191187 }
192188
193189 /**
@@ -195,9 +191,9 @@
195191 * @param {integer} addressOffset the offset in memory to load the address from
196192 * @return {integer}
197193 */
198194 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)
200196 const code = this.environment.getCode(address)
201197 return code.byteLength
202198 }
203199
@@ -208,21 +204,21 @@
208204 * @param {integer} codeOffset the code offset
209205 * @param {integer} length the length of code to copy
210206 */
211207 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)
213209 let code = this.environment.getCode(address)
214210 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)
216212 memory.set(code)
217213 }
218214
219215 /**
220216 * Gets price of gas in current environment.
221217 * @return {integer}
222218 */
223219 gasPrice () {
224- return ENV.gasPrice
220+ return this.environment.gasPrice
225221 }
226222
227223 /**
228224 * Gets the hash of one of the 256 most recent complete blocks.
@@ -230,51 +226,51 @@
230226 * @param {integer} offset the offset to load the hash into
231227 */
232228 blockHash (number, offset) {
233229 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)
235231 memory.set(hash)
236232 }
237233
238234 /**
239235 * Gets the block’s beneficiary address and loads into memory.
240236 * @param offset
241237 */
242238 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)
245241 }
246242
247243 /**
248244 * Get the block’s timestamp.
249245 * @return {integer}
250246 */
251247 timestamp () {
252- return ENV.timestamp
248+ return this.environment.timestamp
253249 }
254250
255251 /**
256252 * Get the block’s number.
257253 * @return {integer}
258254 */
259255 number () {
260- return ENV.number
256+ return this.environment.number
261257 }
262258
263259 /**
264260 * Get the block’s difficulty.
265261 * @return {integer}
266262 */
267263 difficulty () {
268- return ENV.difficulty
264+ return this.environment.difficulty
269265 }
270266
271267 /**
272268 * Get the block’s gas limit.
273269 * @return {integer}
274270 */
275271 gasLimit () {
276- return ENV.gasLimit
272+ return this.environment.gasLimit
277273 }
278274
279275 /**
280276 * Creates a new log in the current environment
@@ -282,10 +278,10 @@
282278 * @param {integer} length the data length
283279 * TODO: replace with variadic
284280 */
285281 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({
288284 data: data,
289285 topics: [topic1, topic2, topic3, topic4, topic5]
290286 })
291287 }
@@ -296,11 +292,11 @@
296292 * @param {integer} dataOffset the offset to load the code for the new contract from
297293 * @param {integer} length the data length
298294 */
299295 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)
303299 return result
304300 }
305301
306302 /**
@@ -319,14 +315,14 @@
319315 if (gas === undefined) {
320316 gas = this.gasLeft()
321317 }
322318 // 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)
326322 // 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)
329325 memory.set(result)
330326
331327 return errorCode
332328 }
@@ -343,12 +339,12 @@
343339 * @param {integer} resultLength
344340 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
345341 */
346342 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)
349345 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)
351347 memory.set(result)
352348
353349 return errorCode
354350 }
@@ -359,24 +355,24 @@
359355 * @param {interger} pathOffest the memory offset to load the the path from
360356 * @param {interger} valueOffset the memory offset to load the value from
361357 */
362358 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)
366362 const valIsZero = value.every((i) => i === 0)
367363
368364 // write
369365 if (!valIsZero && !oldValue) {
370- ENV.gasLimit -= 15000
366+ this.environment.gasLimit -= 15000
371367 }
372368
373369 // delete
374370 if (valIsZero && oldValue) {
375- ENV.gasRefund += 15000
376- ENV.state.delete(path)
371+ this.environment.gasRefund += 15000
372+ this.environment.state.delete(path)
377373 } else {
378- ENV.state.set(path, value)
374+ this.environment.state.set(path, value)
379375 }
380376 }
381377
382378 /**
@@ -384,11 +380,11 @@
384380 * @param {interger} pathOffest the memory offset to load the the path from
385381 * @param {interger} resultOffset the memory offset to load the value from
386382 */
387383 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)
391387 memory.set(result)
392388 }
393389
394390 /**
@@ -396,18 +392,18 @@
396392 * @param {integer} offset the offset of the output data.
397393 * @param {integer} length the length of the output data.
398394 */
399395 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)
401397 }
402398
403399 /**
404400 * Halt execution and register account for later deletion giving the remaining
405401 * balance to an address path
406402 * @param {integer} offset the offset to load the address from
407403 */
408404 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)
410406 this.environment.suicideAddress = address
411407 }
412408 }
413409

Built with git-ssb-web