git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit faee2f5886f2e3201e3ae568539673700c84ecb3

Merge branch 'shim' into testing

wanderer committed on 9/13/2016, 5:33:01 PM
Parent: 8acbfdf6688be5aabb51397bfe951bb0997b1c8b
Parent: 935cf8fa5bf1fa8accb03404084eb4c6f1a187aa

Files changed

index.jschanged
interface.jschanged
wasm/interface.wasmadded
wasm/interface.wastadded
index.jsView
@@ -44,9 +44,9 @@
4444 // otherwise execution succeeded
4545 codeHandler (code, ethInterface = new Interface(new Environment())) {
4646 const debugInterface = new DebugInterface(ethInterface.environment)
4747 const module = WebAssembly.Module(code)
48- const instance = WebAssembly.Instance(module, {
48+ const imports = {
4949 'ethereum': ethInterface.exportTable,
5050 'debug': debugInterface.exportTable,
5151
5252 // export this for Rust
@@ -55,9 +55,12 @@
5555
5656 // export this for Binaryen
5757 // FIXME: remove once C has proper imports, see https://github.com/ethereum/evm2.0-design/issues/16
5858 'env': ethInterface.exportTable
59- })
59+ }
60+ // add shims
61+ imports.ethereum.useGas = ethInterface.shims.exports.useGas
62+ const instance = WebAssembly.Instance(module, imports)
6063
6164 ethInterface.setModule(instance)
6265 debugInterface.setModule(instance)
6366
interface.jsView
@@ -3,8 +3,10 @@
33 * enables to interact with the Ethereum Environment
44 */
55 const Address = require('./address.js')
66 const U256 = require('./u256.js')
7+const fs = require('fs')
8+const path = require('path')
79
810 const U128_SIZE_BYTES = 16
911 const ADDRESS_SIZE_BYTES = 20
1012 const U256_SIZE_BYTES = 32
@@ -12,14 +14,20 @@
1214 // The interface exposed to the WebAessembly Core
1315 module.exports = class Interface {
1416 constructor (environment) {
1517 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+ })
1625 }
1726
1827 get exportTable () {
1928 let exportMethods = [
2029 // include all the public methods according to the Ethereum Environment Interface (EEI) r1
21- 'useGas',
2230 'getGasLeft',
2331 'getAddress',
2432 'getBalance',
2533 'getTxOrigin',
@@ -63,14 +71,21 @@
6371 /**
6472 * Subtracts an amount to the gas counter
6573 * @param {integer} amount the amount to subtract to the gas counter
6674 */
67- useGas (amount) {
68- if (amount < 0) {
75+ _useGas (high, low) {
76+ if (high < 0) {
6977 // convert from a 32-bit two's compliment
70- amount = 0x100000000 - amount
78+ high = 0x100000000 - high
7179 }
7280
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
7388 this.takeGas(amount)
7489 }
7590
7691 /**
@@ -563,34 +578,4 @@
563578 }
564579 this.environment.gasLeft -= amount
565580 }
566581 }
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.wasmView
@@ -1,0 +1,2 @@
1+asm type
2+@@import interfaceuseGasfunctionexport useGascode  f��
wasm/interface.wastView
@@ -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