From 025116f8d01e2ff07af79d3f8bfaf9cd1b734740 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 19 Mar 2010 20:50:29 -0700 Subject: [PATCH] Move Buffer into own module --- lib/buffer.js | 20 ++++++++++++++++++++ lib/net.js | 2 +- src/node.cc | 11 ++++++++++- test/disabled/test-net-fd-passing.js | 4 ---- test/fixtures/net-fd-passing-receiver.js | 5 ----- test/simple/test-buffer.js | 5 +++-- test/simple/test-http-parser.js | 3 ++- test/simple/test-net-pingpong.js | 4 ---- 8 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 lib/buffer.js diff --git a/lib/buffer.js b/lib/buffer.js new file mode 100644 index 0000000000..6b570eed04 --- /dev/null +++ b/lib/buffer.js @@ -0,0 +1,20 @@ +var Buffer = process.binding('buffer').Buffer; + +exports.Buffer = Buffer; + +Buffer.prototype.toString = function () { + return this.utf8Slice(0, this.length); +}; + +Buffer.prototype.toJSON = function () { + return this.utf8Slice(0, this.length); + /* + var s = ""; + for (var i = 0; i < this.length; i++) { + s += this[i].toString(16) + " "; + } + return s; + */ +}; + + diff --git a/lib/net.js b/lib/net.js index 72618e1496..b5951acb32 100644 --- a/lib/net.js +++ b/lib/net.js @@ -18,7 +18,7 @@ var binding = process.binding('net'); // represent buffer.used) that can be seeked around would be easier. I'm not // yet convinced that every use-case can be fit into that abstraction, so // waiting to implement it until I get more experience with this. -var Buffer = process.Buffer; +var Buffer = require('buffer').Buffer; var IOWatcher = process.IOWatcher; var assert = process.assert; diff --git a/src/node.cc b/src/node.cc index 3fb9f80436..5f98e7de7a 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1176,6 +1176,15 @@ static Handle Binding(const Arguments& args) { binding_cache->Set(module, exports); } + } else if (!strcmp(*module_v, "buffer")) { + if (binding_cache->Has(module)) { + exports = binding_cache->Get(module)->ToObject(); + } else { + exports = Object::New(); + Buffer::Initialize(exports); + binding_cache->Set(module, exports); + } + } else if (!strcmp(*module_v, "natives")) { if (binding_cache->Has(module)) { exports = binding_cache->Get(module)->ToObject(); @@ -1184,6 +1193,7 @@ static Handle Binding(const Arguments& args) { // Explicitly define native sources. // TODO DRY/automate this? exports->Set(String::New("assert"), String::New(native_assert)); + exports->Set(String::New("buffer"), String::New(native_buffer)); exports->Set(String::New("child_process"),String::New(native_child_process)); exports->Set(String::New("dns"), String::New(native_dns)); exports->Set(String::New("events"), String::New(native_events)); @@ -1301,7 +1311,6 @@ static void Load(int argc, char *argv[]) { // Initialize the C++ modules..................filename of module - Buffer::Initialize(process); // buffer.cc IOWatcher::Initialize(process); // io_watcher.cc IdleWatcher::Initialize(process); // idle_watcher.cc Timer::Initialize(process); // timer.cc diff --git a/test/disabled/test-net-fd-passing.js b/test/disabled/test-net-fd-passing.js index 1cf577bf6a..3d16ff62d3 100644 --- a/test/disabled/test-net-fd-passing.js +++ b/test/disabled/test-net-fd-passing.js @@ -1,10 +1,6 @@ process.mixin(require("../common")); net = require("net"); -process.Buffer.prototype.toString = function () { - return this.utf8Slice(0, this.length); -}; - var tests_run = 0; function fdPassingTest(path, port) { diff --git a/test/fixtures/net-fd-passing-receiver.js b/test/fixtures/net-fd-passing-receiver.js index be29a28723..bcaf9e116c 100644 --- a/test/fixtures/net-fd-passing-receiver.js +++ b/test/fixtures/net-fd-passing-receiver.js @@ -1,11 +1,6 @@ process.mixin(require("../common")); net = require("net"); -process.Buffer.prototype.toString = function () { - return this.utf8Slice(0, this.length); -}; - - path = process.ARGV[2]; greeting = process.ARGV[3]; diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index a069face9b..d175c4dfd0 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -1,8 +1,9 @@ require("../common"); assert = require("assert"); +var Buffer = require('buffer').Buffer; -var b = new process.Buffer(1024); +var b = new Buffer(1024); puts("b.length == " + b.length); assert.equal(1024, b.length); @@ -52,7 +53,7 @@ for (var j = 0; j < 100; j++) { // unpack -var b = new process.Buffer(10); +var b = new Buffer(10); b[0] = 0x00; b[1] = 0x01; b[2] = 0x03; diff --git a/test/simple/test-http-parser.js b/test/simple/test-http-parser.js index 6ad6510055..e524cd009d 100644 --- a/test/simple/test-http-parser.js +++ b/test/simple/test-http-parser.js @@ -9,7 +9,8 @@ var HTTPParser = process.binding('http_parser').HTTPParser; var parser = new HTTPParser("request"); -var buffer = new process.Buffer(1024); +var Buffer = require('buffer').Buffer; +var buffer = new Buffer(1024); var request = "GET /hello HTTP/1.1\r\n\r\n"; diff --git a/test/simple/test-net-pingpong.js b/test/simple/test-net-pingpong.js index 860b2007c3..ed8cfebf87 100644 --- a/test/simple/test-net-pingpong.js +++ b/test/simple/test-net-pingpong.js @@ -2,10 +2,6 @@ require("../common"); net = require("net"); -process.Buffer.prototype.toString = function () { - return this.utf8Slice(0, this.length); -}; - var tests_run = 0; function pingPongTest (port, host) {