git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit d48321af0dae8c0c5f6959e9c51e6ff1331b5a1b

Implement a proper debugInterface with print and printHex

Alex Beregszaszi committed on 8/4/2016, 12:13:36 AM
Parent: 0c67dbd7d1ebd5df858985a411d801af32110ab8

Files changed

debugInterface.jschanged
index.jschanged
tests/interfaceRunner.jschanged
debugInterface.jsView
@@ -1,16 +1,35 @@
11 /**
22 * Debug Interface
33 * This expose some functions that can help with debugging wast
44 */
5-const Interface = require('./interface.js')
6-let MOD
75
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)
1115 }
16+ return ret
17+}
1218
13- memPrint () {
14- console.log((new Uint8Array(MOD.exports.memory)).toString())
19+module.exports = class DebugInterface {
20+ setModule (mod) {
21+ this.module = mod
1522 }
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+ }
1635 }
index.jsView
@@ -13,25 +13,33 @@
1313 */
1414
1515 // The Kernel Exposes this Interface to VM instances it makes
1616 const Interface = require('./interface.js')
17+
1718 // The Kernel Stores all of its state in the Environment. The Interface is used
1819 // to by the VM to retrive infromation from the Environment.
1920 const Environment = require('./environment.js')
2021
22+const DebugInterface = require('./debugInterface.js')
23+
2124 module.exports = class Kernal {
2225 // runs some code in the VM
2326 constructor (environment = new Environment()) {
2427 this.environment = environment
2528 }
2629
2730 // handles running code.
2831 static codeHandler (code, ethInterface = new Interface(new Environment())) {
32+ const debugInterface = new DebugInterface()
33+
2934 const instance = Wasm.instantiateModule(code, {
30- 'ethereum': ethInterface.exportTable
35+ 'ethereum': ethInterface.exportTable,
36+ 'debug': debugInterface.exportTable
3137 })
3238
3339 ethInterface.setModule(instance)
40+ debugInterface.setModule(instance)
41+
3442 if (instance.exports.main) {
3543 instance.exports.main()
3644 }
3745 return instance
tests/interfaceRunner.jsView
@@ -5,8 +5,9 @@
55
66 const Kernel = require('../index.js')
77 const Environment = require('../environment.js')
88 const Interface = require('../interface.js')
9+const DebugInterface = require('../debugInterface.js')
910 const dir = __dirname + '/interface'
1011 // get the test names
1112 let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
1213 // tests = ['balance.wast']
@@ -24,12 +25,17 @@
2425 const environment = new Environment(envData)
2526 environment.parent = ethereum
2627 const testContract = new Kernel(environment)
2728 const ethInterface = new Interface(environment, testContract)
29+ const debugInterface = new DebugInterface()
2830
2931 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+ })
3136 ethInterface.setModule(mod)
37+ debugInterface.setModule(mod)
3238 mod.exports.test()
3339 } catch (e) {
3440 t.fail()
3541 console.error('FAIL')

Built with git-ssb-web