Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
src: enable native v8 typed arrays
This commit removes our homegrown typed arrays implementation and
enables V8's built-in typed arrays implementation.
  • Loading branch information
bnoordhuis committed Jul 6, 2013
1 parent c56a96c commit 0693d22
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 349 deletions.
11 changes: 0 additions & 11 deletions doc/api/buffer.markdown
Expand Up @@ -37,17 +37,6 @@ encoding method. Here are the different string encodings.

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

A `Buffer` object can also be used with typed arrays. The buffer object is
cloned to an `ArrayBuffer` that is used as the backing store for the typed
array. The memory of the buffer and the `ArrayBuffer` is not shared.

NOTE: Node.js v0.8 simply retained a reference to the buffer in `array.buffer`
instead of cloning it.

While more efficient, it introduces subtle incompatibilities with the typed
arrays specification. `ArrayBuffer#slice()` makes a copy of the slice while
`Buffer#slice()` creates a view.

## Class: Buffer

The Buffer class is a global type for dealing with binary data directly.
Expand Down
34 changes: 34 additions & 0 deletions src/node.cc
Expand Up @@ -91,6 +91,7 @@ extern char **environ;
namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::Boolean;
using v8::Context;
using v8::Exception;
Expand Down Expand Up @@ -204,6 +205,35 @@ static uv_async_t emit_debug_enabled_async;
Isolate* node_isolate = NULL;


class ArrayBufferAllocator : public ArrayBuffer::Allocator {
public:
// Impose an upper limit to avoid out of memory errors that bring down
// the process.
static const size_t kMaxLength = 0x3fffffff;
static ArrayBufferAllocator the_singleton;
virtual ~ArrayBufferAllocator() {}
virtual void* Allocate(size_t length);
virtual void Free(void* data);
private:
ArrayBufferAllocator() {}
ArrayBufferAllocator(const ArrayBufferAllocator&);
void operator=(const ArrayBufferAllocator&);
};

ArrayBufferAllocator ArrayBufferAllocator::the_singleton;


void* ArrayBufferAllocator::Allocate(size_t length) {
if (length > kMaxLength) return NULL;
return new char[length];
}


void ArrayBufferAllocator::Free(void* data) {
delete[] static_cast<char*>(data);
}


static void CheckImmediate(uv_check_t* handle, int status) {
assert(handle == &check_immediate_watcher);
assert(status == 0);
Expand Down Expand Up @@ -2924,6 +2954,10 @@ char** Init(int argc, char *argv[]) {
}
V8::SetFlagsFromCommandLine(&v8argc, v8argv, false);

const char typed_arrays_flag[] = "--harmony_typed_arrays";
V8::SetFlagsFromString(typed_arrays_flag, sizeof(typed_arrays_flag) - 1);
V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);

// Fetch a reference to the main isolate, so we have a reference to it
// even when we need it to access it from another (debugger) thread.
node_isolate = Isolate::GetCurrent();
Expand Down
89 changes: 0 additions & 89 deletions test/simple/test-arraybuffer-slice.js

This file was deleted.

0 comments on commit 0693d22

Please sign in to comment.