git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 2f9c40297f9473f99e508eb777b8108347a093bd

Refactor Interface to have getMemory/setMemory

Alex Beregszaszi committed on 8/3/2016, 10:17:10 PM
Parent: 63c9d288b4797cd12ec12e080e84ed2559b14e63

Files changed

interface.jschanged
interface.jsView
@@ -90,11 +90,9 @@
9090 * the given offset.
9191 * @param {integer} offset
9292 */
9393 address (offset) {
94- const address = this.environment.address
95- const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES)
96- memory.set(address)
94+ this.setMemory(offset, constants.ADD_SIZE_BYTES, this.environment.address)
9795 }
9896
9997 /**
10098 * Gets balance of the given account and loads it into memory at the given
@@ -102,13 +100,12 @@
102100 * @param {integer} addressOffset the memory offset to laod the address
103101 * @param {integer} resultOffset
104102 */
105103 balance (addressOffset, offset) {
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)
104+ const address = this.getMemory(addressOffset, constants.ADD_SIZE_BYTES)
108105 // call the parent contract and ask for the balance of one of its child contracts
109106 const balance = this.environment.parent.environment.getBalance(address)
110- memory.set(balance)
107+ this.setMemory(offset, constants.MAX_BAL_BYTES, balance)
111108 }
112109
113110 /**
114111 * Gets the execution's origination address and loads it into memory at the
@@ -116,33 +113,27 @@
116113 * account with non-empty associated code.
117114 * @param {integer} offset
118115 */
119116 origin (offset) {
120- const origin = this.environment.origin
121- const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES)
122- memory.set(origin)
117+ this.setMemory(offset, constants.ADD_SIZE_BYTES, this.environment.origin)
123118 }
124119
125120 /**
126121 * Gets caller address and loads it into memory at the given offset. This is
127122 * the address of the account that is directly responsible for this execution.
128123 * @param {integer} offset
129124 */
130125 caller (offset) {
131- const caller = this.environment.caller
132- const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES)
133- memory.set(caller)
126+ this.setMemory(offset, constants.ADD_SIZE_BYTES, this.environment.caller)
134127 }
135128
136129 /**
137130 * Gets the deposited value by the instruction/transaction responsible for
138131 * this execution and loads it into memory at the given location.
139132 * @param {integer} offset
140133 */
141134 callValue (offset) {
142- const callValue = this.environment.callValue
143- const memory = new Uint8Array(this.module.exports.memory, offset, constants.MAX_BAL_BYTES)
144- memory.set(callValue)
135+ this.setMemory(offset, constants.MAX_BAL_BYTES, this.environment.callValue)
145136 }
146137
147138 /**
148139 * Get size of input data in current environment. This pertains to the input
@@ -161,10 +152,9 @@
161152 * @param {integer} length the length of data to copy
162153 */
163154 callDataCopy (offset, dataOffset, length) {
164155 const callData = new Uint8Array(this.environment.callData, offset, length)
165- const memory = new Uint8Array(this.module.exports.memory, offset, length)
166- memory.set(callData)
156+ this.setMemory(offset, length, callData)
167157 }
168158
169159 /**
170160 * Gets the size of code running in current environment.
@@ -181,19 +171,18 @@
181171 * @param {integer} length the length of code to copy
182172 */
183173 codeCopy (offset, codeOffset, length) {
184174 const code = new Uint8Array(this.environment.code, codeOffset, length)
185- const memory = new Uint8Array(this.module.exports.memory, offset, length)
186- memory.set(code)
175+ this.setMemory(offset, length, code)
187176 }
188177
189178 /**
190179 * Get size of an account’s code.
191180 * @param {integer} addressOffset the offset in memory to load the address from
192181 * @return {integer}
193182 */
194183 extCodeSize (addressOffset) {
195- const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES)
184+ const address = this.getMemory(addressOffset, constants.ADD_SIZE_BYTES)
196185 const code = this.environment.getCode(address)
197186 return code.byteLength
198187 }
199188
@@ -204,13 +193,12 @@
204193 * @param {integer} codeOffset the code offset
205194 * @param {integer} length the length of code to copy
206195 */
207196 extCodeCopy (addressOffset, offset, codeOffset, length) {
208- const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES)
197+ const address = this.getMemory(addressOffset, constants.ADD_SIZE_BYTES)
209198 let code = this.environment.getCode(address)
210199 code = new Uint8Array(code, codeOffset, length)
211- const memory = new Uint8Array(this.module.exports.memory, offset, length)
212- memory.set(code)
200+ this.setMemory(offset, length, code)
213201 }
214202
215203 /**
216204 * Gets price of gas in current environment.
@@ -226,19 +214,17 @@
226214 * @param {integer} offset the offset to load the hash into
227215 */
228216 blockHash (number, offset) {
229217 const hash = this.environment.getBlockHash(number)
230- const memory = new Uint8Array(this.module.exports.memory, offset, 32)
231- memory.set(hash)
218+ this.setMemory(offset, 32, hash)
232219 }
233220
234221 /**
235222 * Gets the block’s beneficiary address and loads into memory.
236223 * @param offset
237224 */
238225 coinbase (offset) {
239- const memory = new Uint8Array(this.module.exports.memory, offset, constants.ADD_SIZE_BYTES)
240- memory.set(this.environment.coinbase)
226+ this.setMemory(offset, constants.ADD_SIZE_BYTES, this.environment.coinbase)
241227 }
242228
243229 /**
244230 * Get the block’s timestamp.
@@ -278,9 +264,9 @@
278264 * @param {integer} length the data length
279265 * TODO: replace with variadic
280266 */
281267 log (dataOffset, length, topic1, topic2, topic3, topic4, topic5) {
282- const data = new Uint8Array(this.module.exports.memory, dataOffset, length)
268+ const data = this.getMemory(dataOffset, length)
283269 this.environment.logs.push({
284270 data: data,
285271 topics: [topic1, topic2, topic3, topic4, topic5]
286272 })
@@ -292,10 +278,10 @@
292278 * @param {integer} dataOffset the offset to load the code for the new contract from
293279 * @param {integer} length the data length
294280 */
295281 create (valueOffset, dataOffset, length) {
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)
282+ const value = this.getMemory(valueOffset, constants.MAX_BAL_BYTES)
283+ const data = this.getMemory(dataOffset, length)
298284 const result = this.environment.create(value, data)
299285 return result
300286 }
301287
@@ -315,15 +301,14 @@
315301 if (gas === undefined) {
316302 gas = this.gasLeft()
317303 }
318304 // Load the params from mem
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)
305+ const address = this.getMemory(addressOffset, constants.ADD_SIZE_BYTES)
306+ const value = this.getMemory(valueOffset, constants.MAX_BAL_BYTES)
307+ const data = this.getMemory(dataOffset, dataLength)
322308 // Run the call
323309 const [result, errorCode] = this.environment.call(gas, address, value, data)
324- const memory = new Uint8Array(this.module.exports.memory, resultOffset, resultLength)
325- memory.set(result)
310+ this.setMemory(resultOffset, resultLength, result)
326311
327312 return errorCode
328313 }
329314
@@ -339,13 +324,12 @@
339324 * @param {integer} resultLength
340325 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
341326 */
342327 callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) {
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)
328+ const data = this.getMemory(dataOffset, dataLength)
329+ const address = this.getMemory(addressOffset, constants.ADD_SIZE_BYTES)
345330 const [result, errorCode] = this.environment.callDelegate(gas, address, data)
346- const memory = new Uint8Array(this.module.exports.memory, resultOffset, resultLength)
347- memory.set(result)
331+ this.setMemory(resultOffset, resultLength, result)
348332
349333 return errorCode
350334 }
351335
@@ -355,10 +339,10 @@
355339 * @param {interger} pathOffest the memory offset to load the the path from
356340 * @param {interger} valueOffset the memory offset to load the value from
357341 */
358342 sstore (pathOffest, valueOffset) {
359- const path = new Buffer(this.module.exports.memory, pathOffest, 32).toString('hex')
360- const value = new Uint8Array(this.module.exports.memory, valueOffset, 32)
343+ const path = this.getMemory(pathOffset, 32).toString('hex')
344+ const value = this.getmemory(valueOffset, 32)
361345 const oldValue = this.environment.state.get(path)
362346 const valIsZero = value.every((i) => i === 0)
363347
364348 // write
@@ -380,32 +364,39 @@
380364 * @param {interger} pathOffest the memory offset to load the the path from
381365 * @param {interger} resultOffset the memory offset to load the value from
382366 */
383367 sload (pathOffest, resultOffset) {
384- const path = new Buffer(this.module.exports.memory, pathOffest, 32).toString('hex')
368+ const path = this.getMemory(pathOffset, 32).toString('hex')
385369 const result = this.environment.state.get(path)
386- const memory = new Uint8Array(this.module.exports.memory, resultOffset, 32)
387- memory.set(result)
370+ this.setMemory(resultOffset, 32, result)
388371 }
389372
390373 /**
391374 * Halt execution returning output data.
392375 * @param {integer} offset the offset of the output data.
393376 * @param {integer} length the length of the output data.
394377 */
395378 return (offset, length) {
396- this.environment.returnValue = new Uint8Array(this.module.exports.memory, offset, length)
379+ this.environment.returnValue = this.getMemory(offset, length)
397380 }
398381
399382 /**
400383 * Halt execution and register account for later deletion giving the remaining
401384 * balance to an address path
402385 * @param {integer} offset the offset to load the address from
403386 */
404387 suicide (addressOffset) {
405- const address = new Uint8Array(this.module.exports.memory, addressOffset, constants.ADD_SIZE_BYTES)
406- this.environment.suicideAddress = address
388+ this.environment.suicideAddress = this.getMemory(addressOffset, constants.ADD_SIZE_BYTES)
407389 }
390+
391+ getMemory (offset, length) {
392+ return new Uint8Array(this.module.exports.memory, offset, length)
393+ }
394+
395+ setMemory (offset, length, value) {
396+ const memory = new Uint8Array(this.module.exports.memory, offset, length)
397+ memory.set(value)
398+ }
408399 }
409400
410401 //
411402 // Polyfill required unless this is sorted: https://bugs.chromium.org/p/chromium/issues/detail?id=633895

Built with git-ssb-web