]> khamidou — GitHub mirrors - electron.git/commitdiff
Support converting Buffer to Value
authorCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 07:39:33 +0000 (15:39 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 07:39:33 +0000 (15:39 +0800)
atom.gyp
atom/common/native_mate_converters/v8_value_converter.cc
atom/common/native_mate_converters/v8_value_converter.h

index cabb43d4f70a28bbf9ae24aa2573274467b21226..440eceee62d27275ebb7a7d0f6cde89c043408d2 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
         # 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)',
index 0d5a25765470a5770b2036471ff4fa027a43f68a..2270f0e0fd3db4420475644db4ecb6b307ccb82f 100644 (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,
index 3a0f6374ccb4cdc8237e16f957532ee7d4f15459..db108ad9b043397e715b5ca49cbd3c901879adae 100644 (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;