From 1b44ddae31112caddb8f9eef88874ecdf80a279d Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 27 Jul 2015 14:44:07 +0200 Subject: [PATCH] Dissolve using Generators. --- example-complex.js | 103 ++++--- example-loop.js | 35 ++- example.js | 44 ++- index.js | 310 +++++-------------- test/integer.js | 728 ++++++++++++++++++++++++++------------------- test/loop.js | 83 ++---- test/tap.js | 63 ---- 7 files changed, 625 insertions(+), 741 deletions(-) delete mode 100644 test/tap.js diff --git a/example-complex.js b/example-complex.js index 17847d6..df78378 100755 --- a/example-complex.js +++ b/example-complex.js @@ -1,58 +1,83 @@ #!/usr/bin/env node +"use strict"; var Dissolve = require("./index"), util = require("util"); -function Parser() { - Dissolve.call(this); - - this.loop(function(end) { - this.uint8("pid").tap(function() { - switch (this.vars.pid) { - case 0x00: this.uint32be("token"); break; - case 0x01: this.uint32be("eid").mcstring16("level_type").uint8("game_mode").uint8("dimension").uint8("difficulty").uint8("junk").uint8("max_players"); break; - case 0x02: this.uint8("protocol_version").mcstring16("username").mcstring16("server_host").uint32be("server_port"); break; - case 0x03: this.mcstring16("message"); break; - case 0x04: this.uint64be("time"); break; - case 0xfe: break; - } - }).tap(function() { - this.push(this.vars); - this.vars = {}; - }); - }); -} -util.inherits(Parser, Dissolve); +class Parser extends Dissolve { + *parser() { + var data; + while (true) { + data = { + pid: yield this.uint8() + }; + + switch (data.pid) { + case 0x00: + data.token = yield this.uint32be(); + break; + + case 0x01: + data.eid = yield this.uint32be(); + data.level_type = yield *this.mcstring16(); + data.game_mode = yield this.uint8(); + data.dimension = yield this.uint8(); + data.difficulty = yield this.uint8(); + data.junk = yield this.uint8(); + data.max_players = yield this.uint8(); + break; -Parser.prototype.mcstring16 = function string16(name) { - var len = [name, "len"].join("_"); + case 0x02: + data.protocol_version = yield this.uint8(); + data.username = yield *this.mcstring16(); + data.server_host = yield *this.mcstring16(); + data.server_port = yield this.uint32be(); + break; - return this.uint16be(len).tap(function() { - this.buffer(name, this.vars[len] * 2).tap(function() { - delete this.vars[len]; + case 0x03: + data.message = yield *this.mcstring16(); + break; - for (var i=0;i