Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(641)

Unified Diff: runtime/lib/byte_buffer.cc

Issue 8429027: Add an external array type to support typed arrays. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments from patch set 1 Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/lib/byte_buffer.dart » ('j') | runtime/lib/byte_buffer.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/byte_buffer.cc
diff --git a/runtime/lib/byte_buffer.cc b/runtime/lib/byte_buffer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a76113b821ec97cdae421358b1e2ceb05cb838d7
--- /dev/null
+++ b/runtime/lib/byte_buffer.cc
@@ -0,0 +1,226 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#include "vm/bootstrap_natives.h"
+
+#include "vm/assembler.h"
+#include "vm/assert.h"
+#include "vm/bigint_operations.h"
+#include "vm/exceptions.h"
+#include "vm/native_entry.h"
+#include "vm/object.h"
+
+namespace dart {
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_allocate, 2) {
+ const Instance& length_instance = Instance::CheckedHandle(arguments->At(1));
+ if (!length_instance.IsSmi()) {
+ GrowableArray<const Object*> args;
+ args.Add(&length_instance);
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ Smi& length = Smi::Handle();
+ length ^= length_instance.raw();
+ if (length.IsNull() || (length.Value() < 0)) {
Ivan Posva 2011/11/10 18:06:41 Shouldn't length.IsNull already be caught by the I
cshapiro 2011/11/11 00:37:53 I suppose so. Fixed.
+ GrowableArray<const Object*> args;
+ args.Add(&length);
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ uint8_t* data = new uint8_t[length.Value()];
+ const ByteBuffer& new_array =
+ ByteBuffer::Handle(ByteBuffer::New(data, length.Value()));
+ arguments->SetReturn(new_array);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getLength, 1) {
+ const ByteBuffer& array = ByteBuffer::CheckedHandle(arguments->At(0));
+ if (array.IsNull()) {
+ // TODO(asiva): Need to handle error cases.
+ UNIMPLEMENTED();
+ return;
+ }
+ const Smi& length = Smi::Handle(Smi::New(array.Length()));
+ arguments->SetReturn(length);
+}
+
+
+template<typename T, typename C>
+static void GetIndexed(NativeArguments* arguments) {
+ const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0));
+ const Instance& index_instance = Instance::CheckedHandle(arguments->At(1));
+ if (!index_instance.IsSmi()) {
+ GrowableArray<const Object*> args;
+ args.Add(&index_instance);
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ Smi& index = Smi::Handle();
+ index ^= index_instance.raw();
+ if (buffer.IsNull() || index.IsNull()) {
+ // TODO(asiva): Need to handle error cases.
+ UNIMPLEMENTED();
+ return;
+ }
+ if ((index.Value() < 0) || (index.Value() >= buffer.Length())) {
+ GrowableArray<const Object*> arguments;
+ arguments.Add(&index);
+ Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments);
+ }
+ arguments->SetReturn(C::Handle(C::New(buffer.At<T>(index.Value()))));
+}
+
+
+class IntegerValue {
+ public:
+ typedef Integer ValueType;
+ static bool Is(const Instance& obj) { return obj.IsInteger(); }
+ static int64_t ValueOf(const Integer& val) { return val.AsInt64Value(); }
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(IntegerValue);
+};
+
+
+class DoubleValue {
+ public:
+ typedef Double ValueType;
+ static bool Is(const Instance& obj) { return obj.IsDouble(); }
+ static double ValueOf(const Double& val) { return val.value(); }
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleValue);
+};
+
+
+template<typename T, typename C>
+static void SetIndexed(const NativeArguments* arguments) {
+ const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0));
+ const Instance& index_instance = Instance::CheckedHandle(arguments->At(1));
+ if (!index_instance.IsSmi()) {
+ GrowableArray<const Object*> args;
+ args.Add(&index_instance);
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ Smi& index = Smi::Handle();
+ index ^= index_instance.raw();
+ const Instance& value_instance = Instance::CheckedHandle(arguments->At(2));
+ if (buffer.IsNull() || index.IsNull()) {
+ // TODO(asiva): Need to handle error cases.
+ UNIMPLEMENTED();
+ return;
+ }
+ if ((index.Value() < 0) || (index.Value() >= buffer.Length())) {
+ GrowableArray<const Object*> arguments;
+ arguments.Add(&index);
+ Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments);
+ }
+ if (!C::Is(value_instance)) {
+ GrowableArray<const Object*> args;
+ args.Add(&value_instance);
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ typename C::ValueType& value = C::ValueType::Handle();
+ value ^= value_instance.raw();
+ buffer.SetAt<T>(index.Value(), C::ValueOf(value));
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getInt8, 2) {
+ GetIndexed<int8_t, Smi>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setInt8, 3) {
+ SetIndexed<int8_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getUint8, 2) {
+ GetIndexed<uint8_t, Smi>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setUint8, 3) {
+ SetIndexed<uint8_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getInt16, 2) {
+ GetIndexed<int16_t, Smi>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setInt16, 3) {
+ SetIndexed<int16_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getUint16, 2) {
+ GetIndexed<uint16_t, Smi>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setUint16, 3) {
+ SetIndexed<uint16_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getInt32, 2) {
+ GetIndexed<int32_t, Integer>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setInt32, 3) {
+ SetIndexed<int32_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getUint32, 2) {
+ GetIndexed<uint32_t, Integer>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setUint32, 3) {
+ SetIndexed<uint32_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getInt64, 2) {
+ GetIndexed<int64_t, Integer>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setInt64, 3) {
+ SetIndexed<int64_t, IntegerValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getUint64, 2) {
+ UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation.
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setUint64, 3) {
+ UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation.
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat32, 2) {
+ GetIndexed<float, Double>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat32, 3) {
+ SetIndexed<float, DoubleValue>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat64, 2) {
+ GetIndexed<double, Double>(arguments);
+}
+
+
+DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat64, 3) {
+ SetIndexed<double, DoubleValue>(arguments);
+}
+
+} // namespace dart
« no previous file with comments | « no previous file | runtime/lib/byte_buffer.dart » ('j') | runtime/lib/byte_buffer.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698