From b79ce93010d0cc80a9345f646e562326de4588e5 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 23 Jul 2018 14:11:41 -0400 Subject: Allow deno_buf with null alloc_ptr to be memcpy'd This is a temporary hack to allow for easier restructuring of the serialization code as we move Flatbuffer stuff from C++ to Rust. --- src/binding.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/binding.cc') diff --git a/src/binding.cc b/src/binding.cc index 60325df1e..7b5da2d7f 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -120,12 +120,21 @@ void Print(const v8::FunctionCallbackInfo& args) { } static v8::Local ImportBuf(v8::Isolate* isolate, deno_buf buf) { - auto ab = v8::ArrayBuffer::New( - isolate, reinterpret_cast(buf.alloc_ptr), buf.alloc_len, - v8::ArrayBufferCreationMode::kInternalized); - auto view = - v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len); - return view; + if (buf.alloc_ptr == nullptr) { + // If alloc_ptr isn't set, we memcpy. + // This is currently used for flatbuffers created in Rust. + auto ab = v8::ArrayBuffer::New(isolate, buf.data_len); + memcpy(ab->GetContents().Data(), buf.data_ptr, buf.data_len); + auto view = v8::Uint8Array::New(ab, 0, buf.data_len); + return view; + } else { + auto ab = v8::ArrayBuffer::New( + isolate, reinterpret_cast(buf.alloc_ptr), buf.alloc_len, + v8::ArrayBufferCreationMode::kInternalized); + auto view = + v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len); + return view; + } } static deno_buf ExportBuf(v8::Isolate* isolate, -- cgit v1.2.3