[Pkg-javascript-commits] [node-expat] 85/371: wrap parser because EventEmitter is pure-JS as of node 0.5.x
Jonas Smedegaard
dr at jones.dk
Sun Feb 28 09:59:48 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-expat.
commit 2f24b623e8cc5dae22f79bf51501382803e5bb73
Author: Astro <astro at spaceboyz.net>
Date: Tue Aug 16 13:47:22 2011 +0200
wrap parser because EventEmitter is pure-JS as of node 0.5.x
should fix GitHub issue #19
---
bench.js | 2 +-
lib/node-expat.js | 38 +++++++++++++++++++++++++++++++++
node-expat.cc | 63 ++++++++++++++++++++++++++++++++++---------------------
package.json | 2 +-
test.js | 2 +-
5 files changed, 80 insertions(+), 27 deletions(-)
diff --git a/bench.js b/bench.js
index 4db66f0..ce282a1 100644
--- a/bench.js
+++ b/bench.js
@@ -1,7 +1,7 @@
var sys = require('sys');
var node_xml = require("node-xml");
var libxml = require("libxmljs");
-var expat = require('./build/default/node-expat');
+var expat = require('./lib/node-expat');
function NodeXmlParser() {
var parser = new node_xml.SaxParser(function(cb) { });
diff --git a/lib/node-expat.js b/lib/node-expat.js
new file mode 100644
index 0000000..cd7f91a
--- /dev/null
+++ b/lib/node-expat.js
@@ -0,0 +1,38 @@
+var EventEmitter = require('events').EventEmitter;
+var util = require('util');
+var expat = require('../build/default/node-expat');
+
+/**
+ * Simple wrapper because EventEmitter has turned pure-JS as of node
+ * 0.5.x.
+ */
+exports.Parser = function(encoding) {
+ this.parser = new expat.Parser(encoding);
+
+ var that = this;
+ this.parser.emit = function() {
+ that.emit.apply(that, arguments);
+ };
+};
+util.inherits(exports.Parser, EventEmitter);
+
+exports.Parser.prototype.parse = function(buf, isFinal) {
+ return this.parser.parse(buf, isFinal);
+};
+
+exports.Parser.prototype.setEncoding = function(encoding) {
+ return this.parser.setEncoding(encoding);
+};
+
+exports.Parser.prototype.getError = function() {
+ return this.parser.getError();
+};
+exports.Parser.prototype.stop = function() {
+ return this.parser.stop();
+};
+exports.Parser.prototype.pause = function() {
+ return this.stop();
+};
+exports.Parser.prototype.resume = function() {
+ return this.parser.resume();
+};
diff --git a/node-expat.cc b/node-expat.cc
index 05d87db..6243f07 100644
--- a/node-expat.cc
+++ b/node-expat.cc
@@ -1,6 +1,6 @@
#include <node.h>
#include <node_version.h>
-#include <node_events.h>
+#include <node_object_wrap.h>
#include <node_buffer.h>
extern "C" {
#include <expat.h>
@@ -12,23 +12,22 @@ using namespace node;
static Persistent<String> sym_startElement, sym_endElement,
sym_startCdata, sym_endCdata,
sym_text, sym_processingInstruction,
- sym_comment, sym_xmlDecl, sym_entityDecl;
+ sym_comment, sym_xmlDecl, sym_entityDecl,
+ sym_emit;
-class Parser : public EventEmitter {
+class Parser : public ObjectWrap {
public:
static void Initialize(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
- t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "parse", Parse);
NODE_SET_PROTOTYPE_METHOD(t, "setEncoding", SetEncoding);
NODE_SET_PROTOTYPE_METHOD(t, "getError", GetError);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
- NODE_SET_PROTOTYPE_METHOD(t, "pause", Stop); // Alias for node stream conventions
NODE_SET_PROTOTYPE_METHOD(t, "resume", Resume);
target->Set(String::NewSymbol("Parser"), t->GetFunction());
@@ -42,6 +41,7 @@ public:
sym_comment = NODE_PSYMBOL("comment");
sym_xmlDecl = NODE_PSYMBOL("xmlDecl");
sym_entityDecl = NODE_PSYMBOL("entityDecl");
+ sym_emit = NODE_PSYMBOL("emit");
}
protected:
@@ -65,7 +65,7 @@ protected:
}
Parser(const XML_Char *encoding)
- : EventEmitter()
+ : ObjectWrap()
{
parser = XML_ParserCreate(encoding);
assert(parser != NULL);
@@ -260,8 +260,10 @@ private:
attr->Set(String::New(atts1[0]), String::New(atts1[1]));
/* Trigger event */
- Handle<Value> argv[2] = { String::New(name), attr };
- parser->Emit(sym_startElement, 2, argv);
+ Handle<Value> argv[3] = { sym_startElement,
+ String::New(name),
+ attr };
+ parser->Emit(3, argv);
}
static void EndElement(void *userData,
@@ -270,8 +272,8 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[1] = { String::New(name) };
- parser->Emit(sym_endElement, 1, argv);
+ Handle<Value> argv[2] = { sym_endElement, String::New(name) };
+ parser->Emit(2, argv);
}
static void StartCdata(void *userData)
@@ -279,8 +281,8 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[0] = {};
- parser->Emit(sym_startCdata, 0, argv);
+ Handle<Value> argv[1] = { sym_startCdata };
+ parser->Emit(1, argv);
}
static void EndCdata(void *userData)
@@ -288,8 +290,8 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[0] = {};
- parser->Emit(sym_endCdata, 0, argv);
+ Handle<Value> argv[1] = { sym_endCdata };
+ parser->Emit(1, argv);
}
static void Text(void *userData,
@@ -298,8 +300,9 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[1] = { String::New(s, len) };
- parser->Emit(sym_text, 1, argv);
+ Handle<Value> argv[2] = { sym_text,
+ String::New(s, len) };
+ parser->Emit(2, argv);
}
static void ProcessingInstruction(void *userData,
@@ -308,8 +311,10 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[2] = { String::New(target), String::New(data) };
- parser->Emit(sym_processingInstruction, 2, argv);
+ Handle<Value> argv[3] = { sym_processingInstruction,
+ String::New(target),
+ String::New(data) };
+ parser->Emit(3, argv);
}
static void Comment(void *userData,
@@ -318,8 +323,8 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[1] = { String::New(data) };
- parser->Emit(sym_comment, 1, argv);
+ Handle<Value> argv[2] = { sym_comment, String::New(data) };
+ parser->Emit(2, argv);
}
static void XmlDecl(void *userData,
@@ -329,10 +334,11 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[3] = { version ? String::New(version) : Null(),
+ Handle<Value> argv[4] = { sym_xmlDecl,
+ version ? String::New(version) : Null(),
encoding ? String::New(encoding) : Null(),
Boolean::New(standalone) };
- parser->Emit(sym_xmlDecl, 3, argv);
+ parser->Emit(4, argv);
}
static void EntityDecl(void *userData, const XML_Char *entityName, int is_parameter_entity,
@@ -342,7 +348,8 @@ private:
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
- Handle<Value> argv[7] = { entityName ? String::New(entityName) : Null(),
+ Handle<Value> argv[8] = { sym_entityDecl,
+ entityName ? String::New(entityName) : Null(),
Boolean::New(is_parameter_entity),
value ? String::New(value) : Null(),
base ? String::New(base) : Null(),
@@ -350,7 +357,15 @@ private:
publicId ? String::New(publicId) : Null(),
notationName ? String::New(notationName) : Null(),
};
- parser->Emit(sym_entityDecl, 7, argv);
+ parser->Emit(8, argv);
+ }
+
+ void Emit(int argc, Handle<Value> argv[])
+ {
+ HandleScope scope;
+
+ Local<Function> emit = Local<Function>::Cast(handle_->Get(sym_emit));
+ emit->Call(handle_, argc, argv);
}
};
diff --git a/package.json b/package.json
index bce2963..4e3a683 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{ "name": "node-expat"
,"version": "1.3.2"
-,"main": "./build/default/node-expat"
+,"main": "./lib/node-expat"
,"description": "NodeJS binding for fast XML parsing."
,"scripts" : { "install": "node-waf configure build"
,"update": "node-waf build"
diff --git a/test.js b/test.js
index 5472e81..7def9b0 100644
--- a/test.js
+++ b/test.js
@@ -1,5 +1,5 @@
var sys = require('sys');
-var expat = require('./build/default/node-expat');
+var expat = require('./lib/node-expat');
var Buffer = require('buffer').Buffer;
var vows = require('vows');
var assert = require('assert');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-expat.git
More information about the Pkg-javascript-commits
mailing list