fix anonymous function

pull/7/head
yann300 8 years ago
parent efa965b53c
commit 7d944721f0
  1. 2
      src/code/disassembler.js
  2. 17
      src/lib/eventManager.js
  3. 4
      src/ui/CodeListView.js
  4. 6
      src/ui/StepDetail.js
  5. 2
      src/ui/TxBrowser.js
  6. 35
      test/eventManager.js
  7. 1
      test/tests.js

@ -7,7 +7,7 @@ var createExpressions = function (instructions) {
var expressions = []
var labels = 0
for (var i = 0; i < instructions.length; i++) {
let expr = instructions[i]
var expr = instructions[i]
expr.functional = false
if (expr.name === 'JUMPDEST') {
expr.label = 'label' + (++labels)

@ -2,6 +2,7 @@
function eventManager () {
this.registered = {}
this.anonymous = {}
}
/*
@ -13,15 +14,16 @@ function eventManager () {
* @param {Func} func - function of the listenners that will be executed
*/
eventManager.prototype.unregister = function (eventName, obj, func) {
if (!this.registered[eventName]) {
return
}
if (obj instanceof Function) {
func = obj
obj = {}
obj = this.anonymous
}
for (var reg in this.registered[eventName]) {
if (this.registered[eventName][reg] &&
this.registered[eventName][reg].obj === obj && (!func || this.registered[eventName][reg].func === func)) {
if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) {
this.registered[eventName].splice(reg, 1)
return
}
}
}
@ -40,7 +42,7 @@ eventManager.prototype.register = function (eventName, obj, func) {
}
if (obj instanceof Function) {
func = obj
obj = {}
obj = this.anonymous
}
this.registered[eventName].push({
obj: obj,
@ -56,9 +58,12 @@ eventManager.prototype.register = function (eventName, obj, func) {
* @param {Array}j - argument that will be passed to the exectued function.
*/
eventManager.prototype.trigger = function (eventName, args) {
if (!this.registered[eventName]) {
return
}
for (var listener in this.registered[eventName]) {
var l = this.registered[eventName][listener]
l.func.apply(l.obj, args)
l.func.apply(l.obj === this.anonymous ? {} : l.obj, args)
}
}

@ -21,8 +21,8 @@ CodeListView.prototype.render = function () {
CodeListView.prototype.init = function () {
var self = this
this.codeManager.register('changed', this, this.changed)
this.parent.register('traceUnloaded', this, function () {
this.codeManager.event.register('changed', this, this.changed)
this.parent.event.register('traceUnloaded', this, function () {
self.changed([], '', -1)
})
}

@ -19,17 +19,17 @@ StepDetail.prototype.render = function () {
StepDetail.prototype.init = function () {
var self = this
this.parent.register('traceUnloaded', this, function () {
this.parent.event.register('traceUnloaded', this, function () {
self.detail = initDetail()
self.basicPanel.update(self.detail)
})
this.parent.register('newTraceLoaded', this, function () {
this.parent.event.register('newTraceLoaded', this, function () {
self.detail = initDetail()
self.basicPanel.update(self.detail)
})
this.parent.register('indexChanged', this, function (index) {
this.parent.event.register('indexChanged', this, function (index) {
if (index < 0) return
self.detail['vm trace step'] = index

@ -74,7 +74,7 @@ TxBrowser.prototype.update = function (error, tx) {
info.from = tx.from
info.to = tx.to
info.hash = tx.hash
this.trigger('newTraceRequested', [this.blockNumber, this.txNumber, tx])
this.event.trigger('newTraceRequested', [this.blockNumber, this.txNumber, tx])
} else {
var mes = '<not found>'
info.from = mes

@ -0,0 +1,35 @@
'use strict'
var tape = require('tape')
var EventManager = require('../src/lib/eventManager')
tape('eventManager', function (t) {
t.test('eventManager', function (st) {
var events = new EventManager()
var listenner = {}
var trace = ''
listenner.listen = function (data1) {
trace += data1
}
var registeredFunction = function (data) {
trace += data
}
events.register('event1', listenner, listenner.listen)
events.register('event2', registeredFunction)
events.trigger('event1', ['event1'])
events.trigger('event2', ['event2'])
st.equal(trace, 'event1event2')
events.unregister('event1', listenner.listen)
st.equal(events.registered['event1'].length, 1)
st.equal(events.registered['event2'].length, 1)
events.unregister('event1', listenner, listenner.listen)
st.equal(events.registered['event1'].length, 0)
st.equal(events.registered['event2'].length, 1)
events.unregister('event2', registeredFunction)
st.equal(events.registered['event1'].length, 0)
st.equal(events.registered['event2'].length, 0)
st.end()
})
})

@ -5,3 +5,4 @@ require('./codeManager.js')
require('./util.js')
require('./astwalker.js')
require('./disassembler.js')
require('./eventManager.js')

Loading…
Cancel
Save