Skip to content

Commit 0693d22

Browse files
committed
src: enable native v8 typed arrays
This commit removes our homegrown typed arrays implementation and enables V8's built-in typed arrays implementation.
1 parent c56a96c commit 0693d22

File tree

4 files changed

+34
-349
lines changed

4 files changed

+34
-349
lines changed

doc/api/buffer.markdown

-11
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ encoding method. Here are the different string encodings.
3737

3838
* `'hex'` - Encode each byte as two hexadecimal characters.
3939

40-
A `Buffer` object can also be used with typed arrays. The buffer object is
41-
cloned to an `ArrayBuffer` that is used as the backing store for the typed
42-
array. The memory of the buffer and the `ArrayBuffer` is not shared.
43-
44-
NOTE: Node.js v0.8 simply retained a reference to the buffer in `array.buffer`
45-
instead of cloning it.
46-
47-
While more efficient, it introduces subtle incompatibilities with the typed
48-
arrays specification. `ArrayBuffer#slice()` makes a copy of the slice while
49-
`Buffer#slice()` creates a view.
50-
5140
## Class: Buffer
5241

5342
The Buffer class is a global type for dealing with binary data directly.

src/node.cc

+34
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ extern char **environ;
9191
namespace node {
9292

9393
using v8::Array;
94+
using v8::ArrayBuffer;
9495
using v8::Boolean;
9596
using v8::Context;
9697
using v8::Exception;
@@ -204,6 +205,35 @@ static uv_async_t emit_debug_enabled_async;
204205
Isolate* node_isolate = NULL;
205206

206207

208+
class ArrayBufferAllocator : public ArrayBuffer::Allocator {
209+
public:
210+
// Impose an upper limit to avoid out of memory errors that bring down
211+
// the process.
212+
static const size_t kMaxLength = 0x3fffffff;
213+
static ArrayBufferAllocator the_singleton;
214+
virtual ~ArrayBufferAllocator() {}
215+
virtual void* Allocate(size_t length);
216+
virtual void Free(void* data);
217+
private:
218+
ArrayBufferAllocator() {}
219+
ArrayBufferAllocator(const ArrayBufferAllocator&);
220+
void operator=(const ArrayBufferAllocator&);
221+
};
222+
223+
ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
224+
225+
226+
void* ArrayBufferAllocator::Allocate(size_t length) {
227+
if (length > kMaxLength) return NULL;
228+
return new char[length];
229+
}
230+
231+
232+
void ArrayBufferAllocator::Free(void* data) {
233+
delete[] static_cast<char*>(data);
234+
}
235+
236+
207237
static void CheckImmediate(uv_check_t* handle, int status) {
208238
assert(handle == &check_immediate_watcher);
209239
assert(status == 0);
@@ -2924,6 +2954,10 @@ char** Init(int argc, char *argv[]) {
29242954
}
29252955
V8::SetFlagsFromCommandLine(&v8argc, v8argv, false);
29262956

2957+
const char typed_arrays_flag[] = "--harmony_typed_arrays";
2958+
V8::SetFlagsFromString(typed_arrays_flag, sizeof(typed_arrays_flag) - 1);
2959+
V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
2960+
29272961
// Fetch a reference to the main isolate, so we have a reference to it
29282962
// even when we need it to access it from another (debugger) thread.
29292963
node_isolate = Isolate::GetCurrent();

test/simple/test-arraybuffer-slice.js

-89
This file was deleted.

0 commit comments

Comments
 (0)