git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 4026b89455deff8f73612b2be7855c5bf6aaa296

Merge branch 'shim' into testing

wanderer committed on 9/13/2016, 8:10:11 PM
Parent: be42af81974876ce832fa149178075c8fca67321
Parent: 32fabfab3c71c9d5e49fd11bdc60938c5db6ca43

Files changed

index.jschanged
interface.jschanged
wasm/interface.wasmchanged
wasm/interface.wastchanged
index.jsView
@@ -59,8 +59,9 @@
5959 }
6060 // add shims
6161 imports.ethereum.useGas = ethInterface.shims.exports.useGas
6262 imports.ethereum.getGasLeft = ethInterface.shims.exports.getGasLeft
63+ imports.ethereum.call = ethInterface.shims.exports.call
6364
6465 const instance = WebAssembly.Instance(module, imports)
6566
6667 ethInterface.setModule(instance)
interface.jsView
@@ -20,9 +20,10 @@
2020 this.shims = WebAssembly.Instance(shimMod, {
2121 'interface': {
2222 'useGas': this._useGas.bind(this),
2323 'getGasLeftHigh': this._getGasLeftHigh.bind(this),
24- 'getGasLeftLow': this._getGasLeftLow.bind(this)
24+ 'getGasLeftLow': this._getGasLeftLow.bind(this),
25+ 'call': this._getGasLeftLow.bind(this)
2526 }
2627 })
2728 }
2829
@@ -49,9 +50,8 @@
4950 'getBlockDifficulty',
5051 'getBlockGasLimit',
5152 'log',
5253 'create',
53- 'call',
5454 'callCode',
5555 'callDelegate',
5656 'storageStore',
5757 'storageLoad',
@@ -73,21 +73,9 @@
7373 * Subtracts an amount to the gas counter
7474 * @param {integer} amount the amount to subtract to the gas counter
7575 */
7676 _useGas (high, low) {
77- if (high < 0) {
78- // convert from a 32-bit two's compliment
79- high = 0x100000000 - high
80- }
81-
82- if (low < 0) {
83- // convert from a 32-bit two's compliment
84- low = 0x100000000 - low
85- }
86-
87- // JS only bitshift 32bits, so instead of high << 32 we have high * 2 ^ 32
88- const amount = (high * 4294967296) + low
89- this.takeGas(amount)
77+ this.takeGas(from64bit(high, low))
9078 }
9179
9280 /**
9381 * Returns the current amount of gas
@@ -413,35 +401,30 @@
413401 * @param {integer} resultLength
414402 * @param {integer} gas
415403 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
416404 */
417- call (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) {
405+ _call (gasHigh, gasLow, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) {
406+ const gas = from64bit(gasHigh, gasLow)
407+ this.takeGas(40 + gas)
408+
418409 // Load the params from mem
419410 const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
420411 const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
412+ const data = this.getMemory(dataOffset, dataLength).slice(0)
421413
422- this.takeGas(40)
423-
424- // const data = this.getMemory(dataOffset, dataLength).slice(0)
425-
426- // // Special case for calling into empty account
427- // if (!this.environment.isAccountPresent(address)) {
428- // this.takeGas(25000)
429- // }
430- if (address.lt(new U256(4))) {
414+ // Special case for calling into empty account
415+ if (!this.environment.isAccountPresent(address)) {
431416 this.takeGas(25000)
432417 }
433- // // Special case for non-zero value
418+
419+ // Special case for non-zero value
434420 if (!value.isZero()) {
435- this.takeGas(9000 - 2300 + gas)
436- this.takeGas(-gas)
421+ this.takeGas(9000)
437422 }
438423
439- // const [errorCode, result] = this.environment.call(gas, address, value, data)
440- // this.setMemory(resultOffset, resultLength, result)
441- // return errorCode
442- //
443- return 1
424+ const [errorCode, result] = this.environment.call(gas, address, value, data)
425+ this.setMemory(resultOffset, resultLength, result)
426+ return errorCode
444427 }
445428
446429 /**
447430 * Message-call into this account with an alternative account’s code.
@@ -454,31 +437,18 @@
454437 * @param {integer} gas
455438 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
456439 */
457440 callCode (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) {
441+ // FIXME: count properly
442+ this.takeGas(40)
443+
458444 // Load the params from mem
459445 const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
460446 const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
461-
462- this.takeGas(40)
463-
464- // const data = this.getMemory(dataOffset, dataLength).slice(0)
465-
466- // // Special case for calling into empty account
467- // if (!this.environment.isAccountPresent(address)) {
468- // this.takeGas(25000)
469- // }
470- // // Special case for non-zero value
471- if (!value.isZero()) {
472- this.takeGas(9000 - 2300 + gas)
473- this.takeGas(-gas)
474- }
475-
476- // const [errorCode, result] = this.environment.call(gas, address, value, data)
477- // this.setMemory(resultOffset, resultLength, result)
478- // return errorCode
479- //
480- return 1
447+ const data = this.getMemory(dataOffset, dataLength).slice(0)
448+ const [errorCode, result] = this.environment.callCode(gas, address, value, data)
449+ this.setMemory(resultOffset, resultLength, result)
450+ return errorCode
481451 }
482452
483453 /**
484454 * Message-call into this account with an alternative account’s code, but
@@ -586,4 +556,18 @@
586556 }
587557 this.environment.gasLeft -= amount
588558 }
589559 }
560+
561+// converts a 64 bit number to a JS number
562+function from64bit (high, low) {
563+ if (high < 0) {
564+ // convert from a 32-bit two's compliment
565+ high = 0x100000000 - high
566+ }
567+ if (low < 0) {
568+ // convert from a 32-bit two's compliment
569+ low = 0x100000000 - low
570+ }
571+ // JS only bitshift 32bits, so instead of high << 32 we have high * 2 ^ 32
572+ return (high * 4294967296) + low
573+}
wasm/interface.wasmView
@@ -1,2 +1,2 @@
1-asm type@@@@importF interfaceuseGas interfacegetGasLeftHigh interface getGasLeftLowfunctionexportuseGas
2-getGasLeftcode&  f��� e�[ 
1+asm type)@@@@@@importV interfaceuseGas interfacegetGasLeftHigh interface getGasLeftLow interfacecallfunctionexportuseGas
2+getGasLeftcallcode@  f��� e�[  f��
wasm/interface.wastView
@@ -21,5 +21,25 @@
2121 (i64.shl (i64.extend_u/i32 (call_import $getGasLeftHigh)) (i64.const 32))
2222 (i64.extend_u/i32 (call_import $getGasLeftLow))))
2323 )
2424 (export "getGasLeft" $getGasLeft)
25+
26+ ;; call
27+ ;; (import $call "ethereum" "call" (param i32 i32 i32 i32 i32 i32 i32) (result i32))
28+ (import $call "interface" "call" (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32))
29+ (func $callShim
30+ (param i64 i32 i32 i32 i32 i32 i32)
31+ (result i32)
32+ (call_import $call
33+ (i32.wrap/i64
34+ (i64.shr_u (get_local 0) (i64.const 32)))
35+ (i32.wrap/i64 (get_local 0))
36+ (get_local 1)
37+ (get_local 2)
38+ (get_local 3)
39+ (get_local 4)
40+ (get_local 5)
41+ (get_local 6)
42+ )
43+ )
44+ (export "call" $callShim)
2545 )

Built with git-ssb-web