-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2722 from atom/fix-array-buffer
Support creating externalized ArrayBuffer for node::Buffer
- Loading branch information
Showing
6 changed files
with
88 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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 "base/basictypes.h" | ||
#include "atom/common/node_includes.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, arraysize(args), args).ToLocalChecked()); | ||
} | ||
|
||
} // namespace | ||
|
||
void OverrideNodeArrayBuffer() { | ||
node::Buffer::SetArrayBufferCreator( | ||
BlinkArrayBufferNew, BlinkArrayBufferNewWith, BlinkUint8ArrayNew); | ||
} | ||
|
||
} // namespace atom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters