Permalink
Browse files

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...
1 parent c56a96c commit 0693d22f86de01b179343cc568a5609726bef9bb @bnoordhuis bnoordhuis committed Jun 29, 2013
Showing with 34 additions and 349 deletions.
  1. +0 −11 doc/api/buffer.markdown
  2. +34 −0 src/node.cc
  3. +0 −89 test/simple/test-arraybuffer-slice.js
  4. +0 −249 test/simple/test-typed-arrays.js
@@ -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.
View
@@ -91,6 +91,7 @@ extern char **environ;
namespace node {
using v8::Array;
+using v8::ArrayBuffer;
using v8::Boolean;
using v8::Context;
using v8::Exception;
@@ -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);
@@ -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();
@@ -1,89 +0,0 @@
-// Copyright Joyent, Inc. and other Node contributors.
-
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-/*
- * Tests to verify slice functionality of ArrayBuffer.
- * (http://www.khronos.org/registry/typedarray/specs/latest/#5)
- */
-
-var common = require('../common');
-var assert = require('assert');
-
-var buffer = new ArrayBuffer(8);
-var sub;
-var i;
-
-for (var i = 0; i < 8; i++) {
- buffer[i] = i;
-}
-
-// slice a copy of buffer
-sub = buffer.slice(2, 6);
-
-// make sure it copied correctly
-assert.ok(sub instanceof ArrayBuffer);
-assert.equal(sub.byteLength, 4);
-
-for (i = 0; i < 4; i++) {
- assert.equal(sub[i], i+2);
-}
-
-// modifications to the new slice shouldn't affect the original
-sub[0] = 999;
-
-for (i = 0; i < 8; i++) {
- assert.equal(buffer[i], i);
-}
-
-// test optional end param
-sub = buffer.slice(5);
-
-assert.ok(sub instanceof ArrayBuffer);
-assert.equal(sub.byteLength, 3);
-for (i = 0; i < 3; i++) {
- assert.equal(sub[i], i+5);
-}
-
-// test clamping
-sub = buffer.slice(-3, -1);
-
-assert.ok(sub instanceof ArrayBuffer);
-assert.equal(sub.byteLength, 2);
-for (i = 0; i < 2; i++) {
- assert.equal(sub[i], i+5);
-}
-
-// test invalid clamping range
-sub = buffer.slice(-1, -3);
-
-assert.ok(sub instanceof ArrayBuffer);
-assert.equal(sub.byteLength, 0);
-
-// test wrong number of arguments
-var gotError = false;
-
-try {
- sub = buffer.slice();
-} catch (e) {
- gotError = true;
-}
-
-assert.ok(gotError);
Oops, something went wrong.

0 comments on commit 0693d22

Please sign in to comment.