From b50d51ef5b5ebdf250eefc387ed25c7aaae557f4 Mon Sep 17 00:00:00 2001 From: Dean McNamee Date: Tue, 18 Dec 2012 20:03:41 +0100 Subject: [PATCH] typed arrays: replace switch with templates Convert TypedArray's TypeName implementation to template specialization instead of a switch() statement. --- src/v8_typed_array.cc | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/v8_typed_array.cc b/src/v8_typed_array.cc index 9b1aa442b7b..a0a5e8b444b 100644 --- a/src/v8_typed_array.cc +++ b/src/v8_typed_array.cc @@ -172,6 +172,30 @@ static bool checkAlignment(size_t val, unsigned int bytes) { return (val & (bytes - 1)) == 0; // Handles bytes == 0. } +template +struct TEANameTrait { + static const char* const name; +}; + +template <> const char* const + TEANameTrait::name = "Int8Array"; +template <> const char* const + TEANameTrait::name = "Uint8Array"; +template <> const char* const + TEANameTrait::name = "Uint8ClampedArray"; +template <> const char* const + TEANameTrait::name = "Int16Array"; +template <> const char* const + TEANameTrait::name = "Uint16Array"; +template <> const char* const + TEANameTrait::name = "Int32Array"; +template <> const char* const + TEANameTrait::name = "Uint32Array"; +template <> const char* const + TEANameTrait::name = "Float32Array"; +template <> const char* const + TEANameTrait::name = "Float64Array"; + template class TypedArray { public: @@ -183,7 +207,7 @@ class TypedArray { v8::HandleScope scope; ft_cache = v8::Persistent::New( v8::FunctionTemplate::New(&TypedArray::V8New)); - ft_cache->SetClassName(v8::String::New(TypeName())); + ft_cache->SetClassName(v8::String::New(TEANameTrait::name)); v8::Local instance = ft_cache->InstanceTemplate(); instance->SetInternalFieldCount(0); @@ -441,23 +465,6 @@ class TypedArray { return TypedArray::GetTemplate()-> GetFunction()->NewInstance(3, argv); } - - static const char* TypeName() { - switch (TEAType) { - case v8::kExternalByteArray: return "Int8Array"; - case v8::kExternalUnsignedByteArray: return "Uint8Array"; - case v8::kExternalShortArray: return "Int16Array"; - case v8::kExternalUnsignedShortArray: return "Uint16Array"; - case v8::kExternalIntArray: return "Int32Array"; - case v8::kExternalUnsignedIntArray: return "Uint32Array"; - case v8::kExternalFloatArray: return "Float32Array"; - case v8::kExternalDoubleArray: return "Float64Array"; - case v8::kExternalPixelArray: return "Uint8ClampedArray"; - } - abort(); - // Please the compiler - return ""; - } }; class Int8Array : public TypedArray<1, v8::kExternalByteArray> { };