Commit ccd5cf6291ba56bba5cd804c94c57da6b7801c40
Merge pull request #8 from ewasm/debug-interface
Proper debug interfaceAlex Beregszaszi authored on 8/4/2016, 12:25:44 AM
GitHub committed on 8/4/2016, 12:25:44 AM
Parent: 0c67dbd7d1ebd5df858985a411d801af32110ab8
Parent: 0c2b4bb4899d91f3248845ca6992f290844bbba2
Files changed
debugInterface.js | changed |
index.js | changed |
interface.js | changed |
tests/interfaceRunner.js | changed |
debugInterface.js | ||
---|---|---|
@@ -1,16 +1,35 @@ | ||
1 | 1 | /** |
2 | 2 | * Debug Interface |
3 | 3 | * This expose some functions that can help with debugging wast |
4 | 4 | */ |
5 | -const Interface = require('./interface.js') | |
6 | -let MOD | |
7 | 5 | |
8 | -module.exports = class DebugInterface extends Interface { | |
9 | - debugPrint (a, b) { | |
10 | - console.log(a) | |
6 | +// This is ASCII only | |
7 | +function Uint8ArrayToString (input) { | |
8 | + return String.fromCharCode.apply(null, input) | |
9 | +} | |
10 | + | |
11 | +function Uint8ArrayToHexString (input) { | |
12 | + var ret = '' | |
13 | + for (var i = 0; i < input.length; i++) { | |
14 | + ret += input[i].toString(16) | |
11 | 15 | } |
16 | + return ret | |
17 | +} | |
12 | 18 | |
13 | - memPrint () { | |
14 | - console.log((new Uint8Array(MOD.exports.memory)).toString()) | |
19 | +module.exports = class DebugInterface { | |
20 | + setModule (mod) { | |
21 | + this.module = mod | |
15 | 22 | } |
23 | + | |
24 | + get exportTable () { | |
25 | + return { | |
26 | + 'print': function (offset, length) { | |
27 | + console.log(`<DEBUG(str): ${Uint8ArrayToString(new Uint8Array(this.module.exports.memory, offset, length))}>`) | |
28 | + }.bind(this), | |
29 | + | |
30 | + 'printHex': function (offset, length) { | |
31 | + console.log(`<DEBUG(hex): ${Uint8ArrayToHexString(new Uint8Array(this.module.exports.memory, offset, length))}>`) | |
32 | + }.bind(this) | |
33 | + } | |
34 | + } | |
16 | 35 | } |
index.js | ||
---|---|---|
@@ -13,25 +13,33 @@ | ||
13 | 13 | */ |
14 | 14 | |
15 | 15 | // The Kernel Exposes this Interface to VM instances it makes |
16 | 16 | const Interface = require('./interface.js') |
17 | + | |
17 | 18 | // The Kernel Stores all of its state in the Environment. The Interface is used |
18 | 19 | // to by the VM to retrive infromation from the Environment. |
19 | 20 | const Environment = require('./environment.js') |
20 | 21 | |
22 | +const DebugInterface = require('./debugInterface.js') | |
23 | + | |
21 | 24 | module.exports = class Kernal { |
22 | 25 | // runs some code in the VM |
23 | 26 | constructor (environment = new Environment()) { |
24 | 27 | this.environment = environment |
25 | 28 | } |
26 | 29 | |
27 | 30 | // handles running code. |
28 | 31 | static codeHandler (code, ethInterface = new Interface(new Environment())) { |
32 | + const debugInterface = new DebugInterface() | |
33 | + | |
29 | 34 | const instance = Wasm.instantiateModule(code, { |
30 | - 'ethereum': ethInterface.exportTable | |
35 | + 'ethereum': ethInterface.exportTable, | |
36 | + 'debug': debugInterface.exportTable | |
31 | 37 | }) |
32 | 38 | |
33 | 39 | ethInterface.setModule(instance) |
40 | + debugInterface.setModule(instance) | |
41 | + | |
34 | 42 | if (instance.exports.main) { |
35 | 43 | instance.exports.main() |
36 | 44 | } |
37 | 45 | return instance |
interface.js | ||
---|---|---|
@@ -5,16 +5,8 @@ | ||
5 | 5 | const constants = require('./constants.js') |
6 | 6 | |
7 | 7 | // The interface exposed to the WebAessembly Core |
8 | 8 | module.exports = class Interface { |
9 | - print (a) { | |
10 | - console.log(a) | |
11 | - } | |
12 | - | |
13 | - memPrint () { | |
14 | - console.log((new Uint8Array(this.module.exports.memory)).toString()) | |
15 | - } | |
16 | - | |
17 | 9 | constructor (environment) { |
18 | 10 | this.environment = environment |
19 | 11 | } |
20 | 12 |
tests/interfaceRunner.js | ||
---|---|---|
@@ -5,8 +5,9 @@ | ||
5 | 5 | |
6 | 6 | const Kernel = require('../index.js') |
7 | 7 | const Environment = require('../environment.js') |
8 | 8 | const Interface = require('../interface.js') |
9 | +const DebugInterface = require('../debugInterface.js') | |
9 | 10 | const dir = __dirname + '/interface' |
10 | 11 | // get the test names |
11 | 12 | let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast')) |
12 | 13 | // tests = ['balance.wast'] |
@@ -24,12 +25,17 @@ | ||
24 | 25 | const environment = new Environment(envData) |
25 | 26 | environment.parent = ethereum |
26 | 27 | const testContract = new Kernel(environment) |
27 | 28 | const ethInterface = new Interface(environment, testContract) |
29 | + const debugInterface = new DebugInterface() | |
28 | 30 | |
29 | 31 | try { |
30 | - const mod = Wasm.instantiateModule(buffer, { 'ethereum': ethInterface.exportTable }) | |
32 | + const mod = Wasm.instantiateModule(buffer, { | |
33 | + 'ethereum': ethInterface.exportTable, | |
34 | + 'debug': debugInterface.exportTable | |
35 | + }) | |
31 | 36 | ethInterface.setModule(mod) |
37 | + debugInterface.setModule(mod) | |
32 | 38 | mod.exports.test() |
33 | 39 | } catch (e) { |
34 | 40 | t.fail() |
35 | 41 | console.error('FAIL') |
Built with git-ssb-web