Use v8::ArrayBuffer::Allocator inside Node

This commit is contained in:
Cheng Zhao 2017-05-14 19:25:07 +09:00
parent 9ce3a8c3a7
commit f4420b449f
11 changed files with 5 additions and 145 deletions

View file

@ -10,40 +10,13 @@
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "gin/array_buffer.h"
#include "gin/v8_initializer.h" #include "gin/v8_initializer.h"
#if defined(OS_WIN)
#include "atom/node/osfhandle.h"
#endif
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
namespace atom { namespace atom {
void* ArrayBufferAllocator::Allocate(size_t length) {
#if defined(OS_WIN)
return node::ArrayBufferCalloc(length);
#else
return calloc(1, length);
#endif
}
void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
#if defined(OS_WIN)
return node::ArrayBufferMalloc(length);
#else
return malloc(length);
#endif
}
void ArrayBufferAllocator::Free(void* data, size_t length) {
#if defined(OS_WIN)
node::ArrayBufferFree(data, length);
#else
free(data);
#endif
}
JavascriptEnvironment::JavascriptEnvironment() JavascriptEnvironment::JavascriptEnvironment()
: initialized_(Initialize()), : initialized_(Initialize()),
isolate_holder_(base::ThreadTaskRunnerHandle::Get()), isolate_holder_(base::ThreadTaskRunnerHandle::Get()),
@ -73,7 +46,7 @@ bool JavascriptEnvironment::Initialize() {
gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode, gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode,
gin::IsolateHolder::kStableV8Extras, gin::IsolateHolder::kStableV8Extras,
&allocator_); gin::ArrayBufferAllocator::SharedInstance());
return true; return true;
} }

View file

@ -14,14 +14,6 @@ class Environment;
namespace atom { namespace atom {
// ArrayBuffer's allocator, used on Chromium's side.
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
void* Allocate(size_t length) override;
void* AllocateUninitialized(size_t length) override;
void Free(void* data, size_t length) override;
};
// Manage the V8 isolate and context automatically. // Manage the V8 isolate and context automatically.
class JavascriptEnvironment { class JavascriptEnvironment {
public: public:
@ -39,7 +31,6 @@ class JavascriptEnvironment {
bool Initialize(); bool Initialize();
bool initialized_; bool initialized_;
ArrayBufferAllocator allocator_;
gin::IsolateHolder isolate_holder_; gin::IsolateHolder isolate_holder_;
v8::Isolate* isolate_; v8::Isolate* isolate_;
v8::Isolate::Scope isolate_scope_; v8::Isolate::Scope isolate_scope_;

View file

@ -27,7 +27,6 @@
#include "third_party/icu/source/i18n/unicode/uregex.h" #include "third_party/icu/source/i18n/unicode/uregex.h"
#include "third_party/icu/source/i18n/unicode/uspoof.h" #include "third_party/icu/source/i18n/unicode/uspoof.h"
#include "third_party/icu/source/i18n/unicode/usearch.h" #include "third_party/icu/source/i18n/unicode/usearch.h"
#include "util-inl.h"
#include "v8-profiler.h" #include "v8-profiler.h"
#include "v8-inspector.h" #include "v8-inspector.h"
@ -41,18 +40,6 @@ int close(int fd) {
return _close(fd); return _close(fd);
} }
void* ArrayBufferCalloc(size_t length) {
return UncheckedCalloc(length);
}
void* ArrayBufferMalloc(size_t length) {
return UncheckedMalloc(length);
}
void ArrayBufferFree(void* data, size_t length) {
return ::free(data);
}
void ReferenceSymbols() { void ReferenceSymbols() {
// Following symbols are used by electron.exe but got stripped by compiler, // Following symbols are used by electron.exe but got stripped by compiler,
// by using the symbols we can force compiler to keep the objects in node.dll, // by using the symbols we can force compiler to keep the objects in node.dll,

View file

@ -21,12 +21,6 @@ namespace node {
__declspec(dllexport) int open_osfhandle(intptr_t osfhandle, int flags); __declspec(dllexport) int open_osfhandle(intptr_t osfhandle, int flags);
__declspec(dllexport) int close(int fd); __declspec(dllexport) int close(int fd);
// Memory allocation functions from Node's module, used by ArrayBuffer allocator
// to make sure memories are allocated and freed with the same allocator.
__declspec(dllexport) void* ArrayBufferCalloc(size_t length);
__declspec(dllexport) void* ArrayBufferMalloc(size_t length);
__declspec(dllexport) void ArrayBufferFree(void* data, size_t length);
// A trick to force referencing symbols. // A trick to force referencing symbols.
__declspec(dllexport) void ReferenceSymbols(); __declspec(dllexport) void ReferenceSymbols();

View file

@ -16,7 +16,6 @@
#include "atom/renderer/api/atom_api_renderer_ipc.h" #include "atom/renderer/api/atom_api_renderer_ipc.h"
#include "atom/renderer/atom_render_frame_observer.h" #include "atom/renderer/atom_render_frame_observer.h"
#include "atom/renderer/atom_render_view_observer.h" #include "atom/renderer/atom_render_view_observer.h"
#include "atom/renderer/node_array_buffer_bridge.h"
#include "atom/renderer/web_worker_observer.h" #include "atom/renderer/web_worker_observer.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_frame.h"
@ -51,7 +50,6 @@ AtomRendererClient::~AtomRendererClient() {
} }
void AtomRendererClient::RenderThreadStarted() { void AtomRendererClient::RenderThreadStarted() {
OverrideNodeArrayBuffer();
RendererClientBase::RenderThreadStarted(); RendererClientBase::RenderThreadStarted();
} }

View file

@ -1,66 +0,0 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/renderer/node_array_buffer_bridge.h"
#include "atom/common/node_includes.h"
#include "base/macros.h"
#include "native_mate/converter.h"
#include "third_party/WebKit/public/web/WebArrayBuffer.h"
#include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
namespace atom {
namespace {
// global.Uint8Array;
v8::Local<v8::Function> GetUint8ArrayConstructor(
v8::Isolate* isolate, v8::Local<v8::Context> context) {
v8::Local<v8::Value> constructor = context->Global()->Get(
mate::StringToV8(isolate, "Uint8Array"));
return v8::Local<v8::Function>::Cast(constructor);
}
// new ArrayBuffer(size);
v8::Local<v8::ArrayBuffer> BlinkArrayBufferNew(
v8::Isolate* isolate, size_t size) {
blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(size, 1);
return v8::Local<v8::ArrayBuffer>::Cast(
blink::WebArrayBufferConverter::toV8Value(
&buffer, isolate->GetCurrentContext()->Global(), isolate));
}
// new ArrayBuffer(data, size);
v8::Local<v8::ArrayBuffer> BlinkArrayBufferNewWith(
v8::Isolate* isolate, void* data, size_t size) {
blink::WebArrayBuffer buffer = blink::WebArrayBuffer::createExternal(
data, size);
return v8::Local<v8::ArrayBuffer>::Cast(
blink::WebArrayBufferConverter::toV8Value(
&buffer, isolate->GetCurrentContext()->Global(), isolate));
}
// new Uint8Array(array_buffer, offset, size);
v8::Local<v8::Uint8Array> BlinkUint8ArrayNew(
v8::Local<v8::ArrayBuffer> ab, size_t offset, size_t size) {
// Use the DOM's Uint8Array constructor to create Uint8Array.
v8::Local<v8::Context> context = ab->CreationContext();
v8::Isolate* isolate = context->GetIsolate();
v8::Local<v8::Function> constructor =
GetUint8ArrayConstructor(isolate, context);
v8::Local<v8::Value> args[] = {
ab, mate::ConvertToV8(isolate, offset), mate::ConvertToV8(isolate, size)
};
return v8::Local<v8::Uint8Array>::Cast(constructor->NewInstance(
context, node::arraysize(args), args).ToLocalChecked());
}
} // namespace
void OverrideNodeArrayBuffer() {
node::Buffer::SetArrayBufferCreator(
BlinkArrayBufferNew, BlinkArrayBufferNewWith, BlinkUint8ArrayNew);
}
} // namespace atom

View file

@ -1,15 +0,0 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_RENDERER_NODE_ARRAY_BUFFER_BRIDGE_H_
#define ATOM_RENDERER_NODE_ARRAY_BUFFER_BRIDGE_H_
namespace atom {
// Override Node's ArrayBuffer with DOM's ArrayBuffer.
void OverrideNodeArrayBuffer();
} // namespace atom
#endif // ATOM_RENDERER_NODE_ARRAY_BUFFER_BRIDGE_H_

View file

@ -491,8 +491,6 @@
'atom/renderer/atom_sandboxed_renderer_client.h', 'atom/renderer/atom_sandboxed_renderer_client.h',
'atom/renderer/guest_view_container.cc', 'atom/renderer/guest_view_container.cc',
'atom/renderer/guest_view_container.h', 'atom/renderer/guest_view_container.h',
'atom/renderer/node_array_buffer_bridge.cc',
'atom/renderer/node_array_buffer_bridge.h',
'atom/renderer/preferences_manager.cc', 'atom/renderer/preferences_manager.cc',
'atom/renderer/preferences_manager.h', 'atom/renderer/preferences_manager.h',
'atom/renderer/renderer_client_base.cc', 'atom/renderer/renderer_client_base.cc',

View file

@ -9,7 +9,7 @@ import sys
BASE_URL = os.getenv('LIBCHROMIUMCONTENT_MIRROR') or \ BASE_URL = os.getenv('LIBCHROMIUMCONTENT_MIRROR') or \
'https://s3.amazonaws.com/github-janky-artifacts/libchromiumcontent' 'https://s3.amazonaws.com/github-janky-artifacts/libchromiumcontent'
LIBCHROMIUMCONTENT_COMMIT = os.getenv('LIBCHROMIUMCONTENT_COMMIT') or \ LIBCHROMIUMCONTENT_COMMIT = os.getenv('LIBCHROMIUMCONTENT_COMMIT') or \
'ecc5298428a02f8acb9af285c75e1491715ae4dd' '549af32d00045c7b9dbb2760c090586c8d3a2638'
PLATFORM = { PLATFORM = {
'cygwin': 'win32', 'cygwin': 'win32',

@ -1 +1 @@
Subproject commit ecc5298428a02f8acb9af285c75e1491715ae4dd Subproject commit 549af32d00045c7b9dbb2760c090586c8d3a2638

2
vendor/node vendored

@ -1 +1 @@
Subproject commit 3cdf4532cf3da20af37e57cbbd59eb860be233aa Subproject commit dfa72e2c73e0442d27746e0f8716d0427f7f9b27