[Pkg-javascript-commits] [node-expat] 01/371: barely works, finally

Jonas Smedegaard dr at jones.dk
Sun Feb 28 09:59:39 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 454115778412f41e15c462a3df27defc724e37e0
Author: Astro <astro at spaceboyz.net>
Date:   Sat May 22 14:27:55 2010 +0200

    barely works, finally
---
 bench.js | 40 ++++++++++++++++++++++++++
 expat.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test.js  |  7 +++++
 wscript  | 16 +++++++++++
 4 files changed, 161 insertions(+)

diff --git a/bench.js b/bench.js
new file mode 100644
index 0000000..1d61c89
--- /dev/null
+++ b/bench.js
@@ -0,0 +1,40 @@
+var sys = require('sys');
+var node_xml = require("node-xml");
+var libxml = require("libxmljs");
+var expat = require('./build/default/expat');
+
+function NodeXmlParser() {
+    var parser = new node_xml.SaxParser(function(cb) { });
+    this.parse = function(s) {
+	parser.parseString(s);
+    };
+}
+function LibXmlJsParser() {
+    var parser = new libxml.SaxPushParser(function(cb) { });
+    this.parse = function(s) {
+	parser.push(s, false);
+    };
+}
+function ExpatParser() {
+    var parser = new expat.Parser();
+    this.parse = function(s) {
+	parser.parse(s, false);
+    };
+}
+
+//var p = new NodeXmlParser();
+//var p = new LibXmlJsParser();
+var p = new ExpatParser();
+p.parse("<r>");
+var nEl = 0;
+function d() {
+    p.parse("<foo bar='baz'>quux</foo>");
+    nEl++;
+    setTimeout(d, 0);
+}
+d();
+
+setInterval(function() {
+    sys.puts(nEl + " el/s");
+    nEl = 0;
+}, 1000);
\ No newline at end of file
diff --git a/expat.cc b/expat.cc
new file mode 100644
index 0000000..c66c596
--- /dev/null
+++ b/expat.cc
@@ -0,0 +1,98 @@
+#include <node.h>
+#include <node_events.h>
+extern "C" {
+#include <expat.h>
+}
+
+using namespace v8;
+using namespace node;
+
+class Parser : public EventEmitter {
+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);
+
+    target->Set(String::NewSymbol("Parser"), t->GetFunction());
+    printf("initialized %i\n", t->GetFunction()->IsFunction());
+  }
+
+protected:
+  static Handle<Value> New(const Arguments& args)
+  {
+    HandleScope scope;
+    printf("new\n");
+
+    Parser *parser = new Parser();
+    parser->Wrap(args.This());
+    
+    printf("newed\n");
+    return args.This();
+  }
+
+  Parser()
+    : EventEmitter()
+  {
+    parser = XML_ParserCreate(/*encoding*/ "UTF-8");
+    assert(parser != NULL);
+  }
+
+  ~Parser()
+  {
+    //assert(parser == NULL);
+  }
+
+  static Handle<Value> Parse(const Arguments& args)
+  {
+    Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+    HandleScope scope;
+    Local<String> str;
+    int isFinal = 0;
+
+    /* Argument 1: buf :: String */
+    if (args.Length() >= 1 && args[0]->IsString())
+      {
+        str = args[0]->ToString();
+      }
+    else
+      return scope.Close(False());
+
+    /* Argument 2: isFinal :: Bool */
+    if (args.Length() >= 2)
+      {
+        isFinal = args[1]->IsTrue();
+      }
+
+    if (parser->parse(**str, isFinal))
+      return scope.Close(False());
+    else
+      return scope.Close(True());
+  }
+
+  bool parse(String &str, int isFinal)
+  {
+    int len = str.Utf8Length();
+    void *buf = XML_GetBuffer(parser, len);
+    assert(buf != NULL);
+    assert(str.WriteUtf8(static_cast<char *>(buf), len) == len);
+    assert(XML_ParseBuffer(parser, len, isFinal) != 0);
+
+    return true;
+  }
+
+private:
+  XML_Parser parser;
+};
+
+
+extern "C" void init(Handle<Object> target)
+{
+  HandleScope scope;
+  Parser::Initialize(target);
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..a6c0589
--- /dev/null
+++ b/test.js
@@ -0,0 +1,7 @@
+var sys = require('sys');
+var expat = require('./build/default/expat');
+
+sys.puts("newing");
+var p = new expat.Parser();
+sys.puts("newed");
+p.parse("<foo bar='baz'/>");
diff --git a/wscript b/wscript
new file mode 100644
index 0000000..040813e
--- /dev/null
+++ b/wscript
@@ -0,0 +1,16 @@
+srcdir = '.'
+blddir = 'build'
+VERSION = '0.0.1'
+
+def set_options(opt):
+  opt.tool_options('compiler_cxx')
+
+def configure(conf):
+  conf.check_tool('compiler_cxx')
+  conf.check_tool('node_addon')
+
+def build(bld):
+  obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
+  obj.target = 'expat'
+  obj.source = 'expat.cc'
+  obj.lib = 'expat'

-- 
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