Commit 13f28296e4b4782565f52674dd8be5afbc1f101b
Merge pull request #7 from ewasm/memory-refactor
Refactor Interface to have getMemory/setMemoryAlex Beregszaszi authored on 8/3/2016, 10:41:22 PM
GitHub committed on 8/3/2016, 10:41:22 PM
Parent: 63c9d288b4797cd12ec12e080e84ed2559b14e63
Parent: 2f9c40297f9473f99e508eb777b8108347a093bd
Files changed
interface.js | changed |
interface.js | ||
---|---|---|
@@ -90,11 +90,9 @@ | ||
90 | 90 | * the given offset. |
91 | 91 | * @param {integer} offset |
92 | 92 | */ |
93 | 93 | 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) | |
97 | 95 | } |
98 | 96 | |
99 | 97 | /** |
100 | 98 | * Gets balance of the given account and loads it into memory at the given |
@@ -102,13 +100,12 @@ | ||
102 | 100 | * @param {integer} addressOffset the memory offset to laod the address |
103 | 101 | * @param {integer} resultOffset |
104 | 102 | */ |
105 | 103 | 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) | |
108 | 105 | // call the parent contract and ask for the balance of one of its child contracts |
109 | 106 | const balance = this.environment.parent.environment.getBalance(address) |
110 | - memory.set(balance) | |
107 | + this.setMemory(offset, constants.MAX_BAL_BYTES, balance) | |
111 | 108 | } |
112 | 109 | |
113 | 110 | /** |
114 | 111 | * Gets the execution's origination address and loads it into memory at the |
@@ -116,33 +113,27 @@ | ||
116 | 113 | * account with non-empty associated code. |
117 | 114 | * @param {integer} offset |
118 | 115 | */ |
119 | 116 | 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) | |
123 | 118 | } |
124 | 119 | |
125 | 120 | /** |
126 | 121 | * Gets caller address and loads it into memory at the given offset. This is |
127 | 122 | * the address of the account that is directly responsible for this execution. |
128 | 123 | * @param {integer} offset |
129 | 124 | */ |
130 | 125 | 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) | |
134 | 127 | } |
135 | 128 | |
136 | 129 | /** |
137 | 130 | * Gets the deposited value by the instruction/transaction responsible for |
138 | 131 | * this execution and loads it into memory at the given location. |
139 | 132 | * @param {integer} offset |
140 | 133 | */ |
141 | 134 | 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) | |
145 | 136 | } |
146 | 137 | |
147 | 138 | /** |
148 | 139 | * Get size of input data in current environment. This pertains to the input |
@@ -161,10 +152,9 @@ | ||
161 | 152 | * @param {integer} length the length of data to copy |
162 | 153 | */ |
163 | 154 | callDataCopy (offset, dataOffset, length) { |
164 | 155 | 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) | |
167 | 157 | } |
168 | 158 | |
169 | 159 | /** |
170 | 160 | * Gets the size of code running in current environment. |
@@ -181,19 +171,18 @@ | ||
181 | 171 | * @param {integer} length the length of code to copy |
182 | 172 | */ |
183 | 173 | codeCopy (offset, codeOffset, length) { |
184 | 174 | 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) | |
187 | 176 | } |
188 | 177 | |
189 | 178 | /** |
190 | 179 | * Get size of an account’s code. |
191 | 180 | * @param {integer} addressOffset the offset in memory to load the address from |
192 | 181 | * @return {integer} |
193 | 182 | */ |
194 | 183 | 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) | |
196 | 185 | const code = this.environment.getCode(address) |
197 | 186 | return code.byteLength |
198 | 187 | } |
199 | 188 | |
@@ -204,13 +193,12 @@ | ||
204 | 193 | * @param {integer} codeOffset the code offset |
205 | 194 | * @param {integer} length the length of code to copy |
206 | 195 | */ |
207 | 196 | 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) | |
209 | 198 | let code = this.environment.getCode(address) |
210 | 199 | 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) | |
213 | 201 | } |
214 | 202 | |
215 | 203 | /** |
216 | 204 | * Gets price of gas in current environment. |
@@ -226,19 +214,17 @@ | ||
226 | 214 | * @param {integer} offset the offset to load the hash into |
227 | 215 | */ |
228 | 216 | blockHash (number, offset) { |
229 | 217 | 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) | |
232 | 219 | } |
233 | 220 | |
234 | 221 | /** |
235 | 222 | * Gets the block’s beneficiary address and loads into memory. |
236 | 223 | * @param offset |
237 | 224 | */ |
238 | 225 | 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) | |
241 | 227 | } |
242 | 228 | |
243 | 229 | /** |
244 | 230 | * Get the block’s timestamp. |
@@ -278,9 +264,9 @@ | ||
278 | 264 | * @param {integer} length the data length |
279 | 265 | * TODO: replace with variadic |
280 | 266 | */ |
281 | 267 | 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) | |
283 | 269 | this.environment.logs.push({ |
284 | 270 | data: data, |
285 | 271 | topics: [topic1, topic2, topic3, topic4, topic5] |
286 | 272 | }) |
@@ -292,10 +278,10 @@ | ||
292 | 278 | * @param {integer} dataOffset the offset to load the code for the new contract from |
293 | 279 | * @param {integer} length the data length |
294 | 280 | */ |
295 | 281 | 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) | |
298 | 284 | const result = this.environment.create(value, data) |
299 | 285 | return result |
300 | 286 | } |
301 | 287 | |
@@ -315,15 +301,14 @@ | ||
315 | 301 | if (gas === undefined) { |
316 | 302 | gas = this.gasLeft() |
317 | 303 | } |
318 | 304 | // 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) | |
322 | 308 | // Run the call |
323 | 309 | 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) | |
326 | 311 | |
327 | 312 | return errorCode |
328 | 313 | } |
329 | 314 | |
@@ -339,13 +324,12 @@ | ||
339 | 324 | * @param {integer} resultLength |
340 | 325 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
341 | 326 | */ |
342 | 327 | 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) | |
345 | 330 | 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) | |
348 | 332 | |
349 | 333 | return errorCode |
350 | 334 | } |
351 | 335 | |
@@ -355,10 +339,10 @@ | ||
355 | 339 | * @param {interger} pathOffest the memory offset to load the the path from |
356 | 340 | * @param {interger} valueOffset the memory offset to load the value from |
357 | 341 | */ |
358 | 342 | 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) | |
361 | 345 | const oldValue = this.environment.state.get(path) |
362 | 346 | const valIsZero = value.every((i) => i === 0) |
363 | 347 | |
364 | 348 | // write |
@@ -380,32 +364,39 @@ | ||
380 | 364 | * @param {interger} pathOffest the memory offset to load the the path from |
381 | 365 | * @param {interger} resultOffset the memory offset to load the value from |
382 | 366 | */ |
383 | 367 | 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') | |
385 | 369 | 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) | |
388 | 371 | } |
389 | 372 | |
390 | 373 | /** |
391 | 374 | * Halt execution returning output data. |
392 | 375 | * @param {integer} offset the offset of the output data. |
393 | 376 | * @param {integer} length the length of the output data. |
394 | 377 | */ |
395 | 378 | return (offset, length) { |
396 | - this.environment.returnValue = new Uint8Array(this.module.exports.memory, offset, length) | |
379 | + this.environment.returnValue = this.getMemory(offset, length) | |
397 | 380 | } |
398 | 381 | |
399 | 382 | /** |
400 | 383 | * Halt execution and register account for later deletion giving the remaining |
401 | 384 | * balance to an address path |
402 | 385 | * @param {integer} offset the offset to load the address from |
403 | 386 | */ |
404 | 387 | 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) | |
407 | 389 | } |
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 | + } | |
408 | 399 | } |
409 | 400 | |
410 | 401 | // |
411 | 402 | // Polyfill required unless this is sorted: https://bugs.chromium.org/p/chromium/issues/detail?id=633895 |
Built with git-ssb-web