Support converting Buffer to Value

This commit is contained in:
Cheng Zhao 2015-08-12 15:39:33 +08:00
parent d2681d2ba1
commit ebb1ddc0df
3 changed files with 16 additions and 3 deletions

View file

@ -224,8 +224,6 @@
# Defined in Chromium but not exposed in its gyp file.
'V8_USE_EXTERNAL_STARTUP_DATA',
'ENABLE_PLUGINS',
# Needed by Node.
'NODE_WANT_INTERNALS=1',
],
'sources': [
'<@(lib_sources)',

View file

@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "vendor/node/src/node_buffer.h"
namespace atom {
@ -258,6 +259,10 @@ base::Value* V8ValueConverter::FromV8ValueImpl(
return FromV8Object(val->ToObject(), state, isolate);
}
if (node::Buffer::HasInstance(val)) {
return FromNodeBuffer(val, state, isolate);
}
if (val->IsObject()) {
return FromV8Object(val->ToObject(), state, isolate);
}
@ -305,6 +310,14 @@ base::Value* V8ValueConverter::FromV8Array(
return result;
}
base::Value* V8ValueConverter::FromNodeBuffer(
v8::Local<v8::Value> value,
FromV8ValueState* state,
v8::Isolate* isolate) const {
return base::BinaryValue::CreateWithCopiedBuffer(
node::Buffer::Data(value), node::Buffer::Length(value));
}
base::Value* V8ValueConverter::FromV8Object(
v8::Local<v8::Object> val,
FromV8ValueState* state,

View file

@ -48,7 +48,9 @@ class V8ValueConverter {
base::Value* FromV8Array(v8::Local<v8::Array> array,
FromV8ValueState* state,
v8::Isolate* isolate) const;
base::Value* FromNodeBuffer(v8::Local<v8::Value> value,
FromV8ValueState* state,
v8::Isolate* isolate) const;
base::Value* FromV8Object(v8::Local<v8::Object> object,
FromV8ValueState* state,
v8::Isolate* isolate) const;