diff --git a/bin/find.js b/bin/find.js index 7730ba5..6cff92c 100755 --- a/bin/find.js +++ b/bin/find.js @@ -10,7 +10,7 @@ const dir = './tmp/'; fs.readdir(dir).then(files => { const tt0 = Date.now(); - async.eachLimit(files, 1, (fileName, callback) => { + async.eachLimit(files, 2, (fileName, callback) => { let start = 0; let stop = 0; @@ -18,29 +18,31 @@ fs.readdir(dir).then(files => { let inst = vcd(); - // inst.on('$enddefinitions', () => { + inst.on('$enddefinitions', () => { // console.log(res); // console.log(inst.info); - // }); - - inst.onTrigger('D1', time => { - if (time > 10) { - if (start == 0) { - start = time; - } else { - stop = time; + inst.onTrigger('D1', time => { + if (time > 10) { + if (start == 0) { + start = time; + } else { + stop = time; + } } - } + }); }); - const s = fs.createReadStream(dir + fileName); - - s.on('data', inst.execute); - - s.on('end', () => { + inst.on('finish', () => { console.log(fileName, (stop - start), ((Date.now() - t0) / 1000 + 's')); callback(); }); + + inst.on('error', err => { + console.log(err); + }); + + fs.createReadStream(dir + fileName).pipe(inst); + }, () => { console.log('Total time: ' + (Date.now() - tt0) / 1000 + 's'); }); diff --git a/index.js b/index.js index e9a3d3e..d17b84d 100644 --- a/index.js +++ b/index.js @@ -1,38 +1,37 @@ 'use strict'; +const stream = require('stream'); const EventEmitter = require('events').EventEmitter; const pkg = require('./package.json'); let lib = require('bindings')('vcd.node'); -const version = pkg.version; - module.exports = () => { - const lifee = new EventEmitter(); - const triee = new EventEmitter(); - - const lifemit = lifee.emit.bind(lifee); - const triemit = triee.emit.bind(triee); const info = {path: []}; + const s = new stream.Writable(); + + // const lifee = new EventEmitter(); + const lifemit = s.emit.bind(s); + + const triee = new EventEmitter(); + const triemit = triee.emit.bind(triee); + const cxt = lib.init(lifemit, triemit, info); - const execute = chunk => lib.execute(cxt, lifemit, triemit, info, chunk); - - const on = (id, fn) => { - lifee.on(id, fn); + s._write = function (chunk, encoding, callback) { + lib.execute(cxt, lifemit, triemit, info, chunk); + callback(); }; - const onTrigger = (id, fn) => { + s.onTrigger = (id, fn) => { lib.setTrigger(cxt, id); triee.on(id, fn); }; - return { - version, - info, - execute, - on, - onTrigger - }; + s.version = pkg.version; + + s.info = info; + + return s; }; diff --git a/test/basic.js b/test/basic.js index f4be5b0..7bda9b4 100644 --- a/test/basic.js +++ b/test/basic.js @@ -17,7 +17,7 @@ describe('basic', () => { it('fail: foo bar', done => { const inst = vcd(); - expect(inst.execute(Buffer.from(' foo bar ???'))).to.eq(1); + expect(inst.write(Buffer.from(' foo bar ???'))).to.eq(true); expect(inst.info).to.deep.eq({ path: [], status: 'declaration' @@ -27,9 +27,9 @@ describe('basic', () => { it('$comment', done => { const inst = vcd(); - expect(inst.execute(Buffer.from( + expect(inst.write(Buffer.from( ' \n $comment some text $end $comment more text $end ???' - ))).to.eq(1); + ))).to.eq(true); expect(inst.info).to.deep.eq({ path: [], status: 'declaration' @@ -39,7 +39,7 @@ describe('basic', () => { it('$version', done => { const inst = vcd(); - expect(inst.execute(Buffer.from(` + expect(inst.write(` $version Generated by VerilatedVcd $end $date Wed Sep 18 22:59:07 2019 $end @@ -54,9 +54,9 @@ $timescale 1ns $end $enddefinitions $end ` - ))).to.eq(0); +)).to.eq(true); - expect(inst.execute(Buffer.from(` + expect(inst.write(` #1 0"}G @@ -72,7 +72,7 @@ b0000000011110000 {u #303 b0000000000001111 {u ` -))).to.eq(0); +)).to.eq(true); expect(inst.info).to.deep.eq({ path: [],