From d74c5060447c01044e4122d9bcda9482bda2a351 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 11 Nov 2010 14:26:41 -0800 Subject: [PATCH] Support encoding --- src/node_buffer.cc | 8 +++++--- src/node_buffer.h | 4 +++- src/node_io_watcher.cc | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 8d7fc6dc91..d838577a15 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -82,7 +82,8 @@ static size_t ByteLength (Handle string, enum encoding enc) { } -Handle Buffer::New(Handle string) { +Handle Buffer::New(Handle string, + Handle encoding) { HandleScope scope; // get Buffer from global scope. @@ -91,8 +92,9 @@ Handle Buffer::New(Handle string) { assert(bv->IsFunction()); Local b = Local::Cast(bv); - Local argv[1] = { Local::New(string) }; - Local instance = b->NewInstance(1, argv); + Local argv[2] = { Local::New(string), + Local::New(encoding) }; + Local instance = b->NewInstance(2, argv); return scope.Close(instance); } diff --git a/src/node_buffer.h b/src/node_buffer.h index 909be452f7..efc982ecf7 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -25,7 +25,9 @@ class Buffer : public ObjectWrap { typedef void (*free_callback)(char *data, void *hint); // C++ API for constructing fast buffer - static v8::Handle New(v8::Handle string); + static v8::Handle New( + v8::Handle string, + v8::Handle encoding = v8::Handle()); static void Initialize(v8::Handle target); static Buffer* New(size_t length); // public constructor diff --git a/src/node_io_watcher.cc b/src/node_io_watcher.cc index 98c2403eb2..298a35b9f2 100644 --- a/src/node_io_watcher.cc +++ b/src/node_io_watcher.cc @@ -31,6 +31,7 @@ static Persistent prev_sym; static Persistent ondrain_sym; static Persistent onerror_sym; static Persistent data_sym; +static Persistent encoding_sym; static Persistent offset_sym; static Persistent fd_sym; static Persistent is_unix_socket_sym; @@ -65,6 +66,7 @@ void IOWatcher::Initialize(Handle target) { fd_sym = NODE_PSYMBOL("fd"); is_unix_socket_sym = NODE_PSYMBOL("isUnixSocket"); data_sym = NODE_PSYMBOL("data"); + encoding_sym = NODE_PSYMBOL("encoding"); ev_prepare_init(&dumper, IOWatcher::Dump); @@ -321,9 +323,9 @@ void IOWatcher::Dump(EV_P_ ev_prepare *w, int revents) { if (data_v->IsString()) { // TODO: insert v8::String::Pointers() hack here. - // TODO: handle different encodings. Local s = data_v->ToString(); - buf_object = Local::New(Buffer::New(s)); + Local e = bucket->Get(encoding_sym); + buf_object = Local::New(Buffer::New(s, e)); bucket->Set(data_sym, buf_object); } else if (Buffer::HasInstance(data_v)) { buf_object = data_v->ToObject();