Commit faee2f5886f2e3201e3ae568539673700c84ecb3
Merge branch 'shim' into testing
wanderer committed on 9/13/2016, 5:33:01 PMParent: 8acbfdf6688be5aabb51397bfe951bb0997b1c8b
Parent: 935cf8fa5bf1fa8accb03404084eb4c6f1a187aa
Files changed
index.js | changed |
interface.js | changed |
wasm/interface.wasm | added |
wasm/interface.wast | added |
index.js | ||
---|---|---|
@@ -44,9 +44,9 @@ | ||
44 | 44 | // otherwise execution succeeded |
45 | 45 | codeHandler (code, ethInterface = new Interface(new Environment())) { |
46 | 46 | const debugInterface = new DebugInterface(ethInterface.environment) |
47 | 47 | const module = WebAssembly.Module(code) |
48 | - const instance = WebAssembly.Instance(module, { | |
48 | + const imports = { | |
49 | 49 | 'ethereum': ethInterface.exportTable, |
50 | 50 | 'debug': debugInterface.exportTable, |
51 | 51 | |
52 | 52 | // export this for Rust |
@@ -55,9 +55,12 @@ | ||
55 | 55 | |
56 | 56 | // export this for Binaryen |
57 | 57 | // FIXME: remove once C has proper imports, see https://github.com/ethereum/evm2.0-design/issues/16 |
58 | 58 | 'env': ethInterface.exportTable |
59 | - }) | |
59 | + } | |
60 | + // add shims | |
61 | + imports.ethereum.useGas = ethInterface.shims.exports.useGas | |
62 | + const instance = WebAssembly.Instance(module, imports) | |
60 | 63 | |
61 | 64 | ethInterface.setModule(instance) |
62 | 65 | debugInterface.setModule(instance) |
63 | 66 |
interface.js | ||
---|---|---|
@@ -3,8 +3,10 @@ | ||
3 | 3 | * enables to interact with the Ethereum Environment |
4 | 4 | */ |
5 | 5 | const Address = require('./address.js') |
6 | 6 | const U256 = require('./u256.js') |
7 | +const fs = require('fs') | |
8 | +const path = require('path') | |
7 | 9 | |
8 | 10 | const U128_SIZE_BYTES = 16 |
9 | 11 | const ADDRESS_SIZE_BYTES = 20 |
10 | 12 | const U256_SIZE_BYTES = 32 |
@@ -12,14 +14,20 @@ | ||
12 | 14 | // The interface exposed to the WebAessembly Core |
13 | 15 | module.exports = class Interface { |
14 | 16 | constructor (environment) { |
15 | 17 | this.environment = environment |
18 | + const shimBin = fs.readFileSync(path.join(__dirname, '/wasm/interface.wasm')) | |
19 | + const shimMod = WebAssembly.Module(shimBin) | |
20 | + this.shims = WebAssembly.Instance(shimMod, { | |
21 | + 'interface': { | |
22 | + 'useGas': this._useGas.bind(this) | |
23 | + } | |
24 | + }) | |
16 | 25 | } |
17 | 26 | |
18 | 27 | get exportTable () { |
19 | 28 | let exportMethods = [ |
20 | 29 | // include all the public methods according to the Ethereum Environment Interface (EEI) r1 |
21 | - 'useGas', | |
22 | 30 | 'getGasLeft', |
23 | 31 | 'getAddress', |
24 | 32 | 'getBalance', |
25 | 33 | 'getTxOrigin', |
@@ -63,14 +71,21 @@ | ||
63 | 71 | /** |
64 | 72 | * Subtracts an amount to the gas counter |
65 | 73 | * @param {integer} amount the amount to subtract to the gas counter |
66 | 74 | */ |
67 | - useGas (amount) { | |
68 | - if (amount < 0) { | |
75 | + _useGas (high, low) { | |
76 | + if (high < 0) { | |
69 | 77 | // convert from a 32-bit two's compliment |
70 | - amount = 0x100000000 - amount | |
78 | + high = 0x100000000 - high | |
71 | 79 | } |
72 | 80 | |
81 | + if (low < 0) { | |
82 | + // convert from a 32-bit two's compliment | |
83 | + low = 0x100000000 - low | |
84 | + } | |
85 | + | |
86 | + // JS only bitshift 32bits, so instead of high << 32 we have high * 2 ^ 32 | |
87 | + const amount = (high * 4294967296) + low | |
73 | 88 | this.takeGas(amount) |
74 | 89 | } |
75 | 90 | |
76 | 91 | /** |
@@ -563,34 +578,4 @@ | ||
563 | 578 | } |
564 | 579 | this.environment.gasLeft -= amount |
565 | 580 | } |
566 | 581 | } |
567 | - | |
568 | -// | |
569 | -// Polyfill required unless this is sorted: https://bugs.chromium.org/p/chromium/issues/detail?id=633895 | |
570 | -// | |
571 | -// Polyfill from: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind | |
572 | -// | |
573 | -Function.prototype.bind = function (oThis) { // eslint-disable-line | |
574 | - if (typeof this !== 'function') { | |
575 | - // closest thing possible to the ECMAScript 5 | |
576 | - // internal IsCallable function | |
577 | - throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable') | |
578 | - } | |
579 | - | |
580 | - var aArgs = Array.prototype.slice.call(arguments, 1) | |
581 | - var fToBind = this | |
582 | - var fNOP = function () {} | |
583 | - var fBound = function () { | |
584 | - return fToBind.apply(this instanceof fNOP ? this : oThis, | |
585 | - aArgs.concat(Array.prototype.slice.call(arguments))) | |
586 | - } | |
587 | - | |
588 | - if (this.prototype) { | |
589 | - // Function.prototype doesn't have a prototype property | |
590 | - fNOP.prototype = this.prototype | |
591 | - } | |
592 | - | |
593 | - fBound.prototype = new fNOP() // eslint-disable-line new-cap | |
594 | - | |
595 | - return fBound | |
596 | -} |
wasm/interface.wasm | ||
---|---|---|
@@ -1,0 +1,2 @@ | ||
1 | + asm type | |
2 | +@ @ import interfaceuseGasfunctionexport useGascode f� � |
wasm/interface.wast | ||
---|---|---|
@@ -1,0 +1,11 @@ | ||
1 | +(module | |
2 | + (import $useGas "interface" "useGas" (param i32 i32)) | |
3 | + (func $useGasShim | |
4 | + (param $amount i64) | |
5 | + (call_import $useGas | |
6 | + (i32.wrap/i64 | |
7 | + (i64.shr_u (get_local $amount) (i64.const 32))) | |
8 | + (i32.wrap/i64 (get_local $amount))) | |
9 | + ) | |
10 | + (export "useGas" $useGasShim) | |
11 | +) |
Built with git-ssb-web