From a5de69f4f88842eddd298e26e435acddf7660874 Mon Sep 17 00:00:00 2001 From: yangguo Date: Wed, 6 May 2015 00:51:56 -0700 Subject: [PATCH] Migrate error messages, part 8. R=mvstanton@chromium.org Review URL: https://codereview.chromium.org/1118273004 Cr-Commit-Position: refs/heads/master@{#28250} --- src/accessors.cc | 5 +- src/arraybuffer.js | 2 +- src/elements.cc | 4 +- src/factory.cc | 7 +++ src/factory.h | 8 ++- src/messages.h | 24 ++++++++- src/messages.js | 24 --------- src/promise.js | 10 ++-- src/proxy.js | 11 ++--- src/runtime.js | 4 +- src/runtime/runtime-array.cc | 4 +- src/runtime/runtime-typedarray.cc | 82 ++++++++++++++----------------- src/typedarray.js | 56 +++++++++------------ test/mjsunit/messages.js | 30 +++++++++++ 14 files changed, 147 insertions(+), 124 deletions(-) diff --git a/src/accessors.cc b/src/accessors.cc index c2518b625c..db0ec328ef 100644 --- a/src/accessors.cc +++ b/src/accessors.cc @@ -13,6 +13,7 @@ #include "src/frames-inl.h" #include "src/isolate.h" #include "src/list-inl.h" +#include "src/messages.h" #include "src/property-details.h" #include "src/prototype.h" @@ -260,8 +261,8 @@ void Accessors::ArrayLengthSetter( return; } - Handle exception = isolate->factory()->NewRangeError( - "invalid_array_length", HandleVector(NULL, 0)); + Handle exception = + isolate->factory()->NewRangeError(MessageTemplate::kInvalidArrayLength); isolate->ScheduleThrow(*exception); } diff --git a/src/arraybuffer.js b/src/arraybuffer.js index 78f3102aa8..11b3c46bc7 100644 --- a/src/arraybuffer.js +++ b/src/arraybuffer.js @@ -15,7 +15,7 @@ var GlobalObject = global.Object; function ArrayBufferConstructor(length) { // length = 1 if (%_IsConstructCall()) { - var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length'); + var byteLength = ToPositiveInteger(length, kInvalidArrayBufferLength); %ArrayBufferInitialize(this, byteLength); } else { throw MakeTypeError(kConstructorNotFunction, "ArrayBuffer"); diff --git a/src/elements.cc b/src/elements.cc index ac63b75888..ce32cb2898 100644 --- a/src/elements.cc +++ b/src/elements.cc @@ -7,6 +7,7 @@ #include "src/arguments.h" #include "src/conversions.h" #include "src/elements.h" +#include "src/messages.h" #include "src/objects.h" #include "src/utils.h" @@ -141,8 +142,7 @@ static bool HasKey(Handle array, Handle key_handle) { MUST_USE_RESULT static MaybeHandle ThrowArrayLengthRangeError(Isolate* isolate) { - THROW_NEW_ERROR(isolate, NewRangeError("invalid_array_length", - HandleVector(NULL, 0)), + THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kInvalidArrayLength), Object); } diff --git a/src/factory.cc b/src/factory.cc index 3632989675..332dc5e39d 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -1193,6 +1193,13 @@ Handle Factory::NewTypeError(MessageTemplate::Template template_index, } +Handle Factory::NewRangeError(MessageTemplate::Template template_index, + Handle arg0, Handle arg1, + Handle arg2) { + return NewError("MakeRangeError", template_index, arg0, arg1, arg2); +} + + Handle Factory::NewEvalError(MessageTemplate::Template template_index, Handle arg0, Handle arg1, Handle arg2) { diff --git a/src/factory.h b/src/factory.h index 0ab312b2b7..8c9b5b56ac 100644 --- a/src/factory.h +++ b/src/factory.h @@ -556,8 +556,7 @@ class Factory final { Handle NewRangeError(Handle message); Handle NewInvalidStringLengthError() { - return NewRangeError("invalid_string_length", - HandleVector(NULL, 0)); + return NewRangeError(MessageTemplate::kInvalidStringLength); } Handle NewSyntaxError(const char* message, Handle args); @@ -586,6 +585,11 @@ class Factory final { Handle arg1 = Handle(), Handle arg2 = Handle()); + Handle NewRangeError(MessageTemplate::Template template_index, + Handle arg0 = Handle(), + Handle arg1 = Handle(), + Handle arg2 = Handle()); + Handle NewEvalError(MessageTemplate::Template template_index, Handle arg0 = Handle(), Handle arg1 = Handle(), diff --git a/src/messages.h b/src/messages.h index 7e18beb967..cefd491905 100644 --- a/src/messages.h +++ b/src/messages.h @@ -132,6 +132,8 @@ class CallSite { T(CannotConvertToPrimitive, "Cannot convert object to primitive value") \ T(ConstructorNotFunction, "Constructor % requires 'new'") \ T(CurrencyCode, "Currency code is required with currency style.") \ + T(DataViewNotArrayBuffer, \ + "First argument to DataView constructor must be an ArrayBuffer") \ T(DateType, "this is not a Date object.") \ T(DefineDisallowed, "Cannot define property:%, object is not extensible.") \ T(FirstArgumentNotRegExp, \ @@ -145,6 +147,7 @@ class CallSite { "Expecting a function in instanceof check, but got %") \ T(InstanceofNonobjectProto, \ "Function has non-object prototype '%' in instanceof check") \ + T(InvalidArgument, "invalid_argument") \ T(InvalidInOperatorUse, "Cannot use 'in' operator to search for '%' in %") \ T(IteratorResultNotAnObject, "Iterator result % is not an object") \ T(IteratorValueNotAnObject, "Iterator value % is not an entry object") \ @@ -155,6 +158,7 @@ class CallSite { "Method invoked on undefined or null value.") \ T(MethodInvokedOnWrongType, "Method invoked on an object that is not %.") \ T(NotAnIterator, "% is not an iterator") \ + T(NotAPromise, "% is not a promise") \ T(NotConstructor, "% is not a constructor") \ T(NotGeneric, "% is not generic") \ T(NotIterable, "% is not iterable") \ @@ -179,24 +183,30 @@ class CallSite { "Invalid changeRecord with non-string 'type' property") \ T(OrdinaryFunctionCalledAsConstructor, \ "Function object that's not a constructor was created with new") \ + T(PromiseCyclic, "Chaining cycle detected for promise %") \ T(PropertyDescObject, "Property description must be an object: %") \ T(PropertyNotFunction, "Property '%' of object % is not a function") \ T(ProtoObjectOrNull, "Object prototype may only be an Object or null: %") \ + T(ProxyHandlerNonObject, "Proxy.% called with non-object as handler") \ T(ProxyHandlerReturned, "Proxy handler % returned % from '%' trap") \ T(ProxyHandlerTrapMissing, "Proxy handler % has no '%' trap") \ T(ProxyHandlerTrapMustBeCallable, \ "Proxy handler %0 has non-callable '%' trap") \ T(ProxyNonObjectPropNames, "Trap '%' returned non-object %") \ - T(ProxyRepeatedPropName, "Trap '%' returned repeated property name '%'") \ + T(ProxyProtoNonObject, "Proxy.create called with no-object as prototype") \ T(ProxyPropNotConfigurable, \ "Proxy handler % returned non-configurable descriptor for property '%' " \ "from '%' trap") \ + T(ProxyRepeatedPropName, "Trap '%' returned repeated property name '%'") \ + T(ProxyTrapFunctionExpected, \ + "Proxy.createFunction called with non-function for '%' trap") \ T(RedefineDisallowed, "Cannot redefine property: %") \ T(ReduceNoInitial, "Reduce of empty array with no initial value") \ T(ReinitializeIntl, "Trying to re-initialize % object.") \ T(ResolvedOptionsCalledOnNonObject, \ "resolvedOptions method called on a non-object or on a object that is " \ "not Intl.%.") \ + T(ResolverNotAFunction, "Promise resolver % is not a function") \ T(SymbolToPrimitive, \ "Cannot convert a Symbol wrapper object to a primitive value") \ T(SymbolToNumber, "Cannot convert a Symbol value to a number") \ @@ -211,11 +221,21 @@ class CallSite { T(ArrayLengthOutOfRange, "defineProperty() array length out of range") \ T(DateRange, "Provided date is not in valid range.") \ T(ExpectedLocation, "Expected Area/Location for time zone, got %") \ + T(InvalidArrayBufferLength, "Invalid array buffer length") \ + T(InvalidArrayLength, "Invalid array length") \ T(InvalidCodePoint, "Invalid code point %") \ T(InvalidCountValue, "Invalid count value") \ T(InvalidCurrencyCode, "Invalid currency code: %") \ + T(InvalidDataViewAccessorOffset, \ + "Offset is outside the bounds of the DataView") \ + T(InvalidDataViewLength, "Invalid data view length") \ + T(InvalidDataViewOffset, "Start offset is outside the bounds of the buffer") \ T(InvalidLanguageTag, "Invalid language tag: %") \ + T(InvalidStringLength, "Invalid string length") \ T(InvalidTimeValue, "Invalid time value") \ + T(InvalidTypedArrayAlignment, "% of % should be a multiple of %") \ + T(InvalidTypedArrayLength, "Invalid typed array length") \ + T(InvalidTypedArrayOffset, "Start offset is too large:") \ T(LocaleMatcher, "Illegal value for localeMatcher:%") \ T(NormalizationForm, "The normalization form should be one of %.") \ T(NumberFormatRange, "% argument must be between 0 and 20") \ @@ -223,6 +243,8 @@ class CallSite { T(StackOverflow, "Maximum call stack size exceeded") \ T(ToPrecisionFormatRange, "toPrecision() argument must be between 1 and 21") \ T(ToRadixFormatRange, "toString() radix argument must be between 2 and 36") \ + T(TypedArraySetNegativeOffset, "Start offset is negative") \ + T(TypedArraySetSourceTooLarge, "Source is too large") \ T(UnsupportedTimeZone, "Unsupported time zone specified %") \ T(ValueOutOfRange, "Value % out of range for % options property %") \ /* SyntaxError */ \ diff --git a/src/messages.js b/src/messages.js index 20b7cb3d4e..60c703db07 100644 --- a/src/messages.js +++ b/src/messages.js @@ -85,34 +85,10 @@ var kMessages = { value_and_accessor: ["Invalid property. A property cannot both have accessors and be writable or have a value, ", "%0"], proto_object_or_null: ["Object prototype may only be an Object or null: ", "%0"], non_extensible_proto: ["%0", " is not extensible"], - handler_non_object: ["Proxy.", "%0", " called with non-object as handler"], - proto_non_object: ["Proxy.", "%0", " called with non-object as prototype"], - trap_function_expected: ["Proxy.", "%0", " called with non-function for '", "%1", "' trap"], - proxy_repeated_prop_name: ["Trap '", "%1", "' returned repeated property name '", "%2", "'"], invalid_weakmap_key: ["Invalid value used as weak map key"], invalid_weakset_value: ["Invalid value used in weak set"], not_date_object: ["this is not a Date object."], - invalid_argument: ["invalid_argument"], - data_view_not_array_buffer: ["First argument to DataView constructor must be an ArrayBuffer"], not_a_symbol: ["%0", " is not a symbol"], - not_a_promise: ["%0", " is not a promise"], - resolver_not_a_function: ["Promise resolver ", "%0", " is not a function"], - promise_cyclic: ["Chaining cycle detected for promise ", "%0"], - // RangeError - invalid_array_length: ["Invalid array length"], - invalid_array_buffer_length: ["Invalid array buffer length"], - invalid_string_length: ["Invalid string length"], - invalid_typed_array_offset: ["Start offset is too large:"], - invalid_typed_array_length: ["Invalid typed array length"], - invalid_typed_array_alignment: ["%0", " of ", "%1", " should be a multiple of ", "%2"], - typed_array_set_source_too_large: - ["Source is too large"], - typed_array_set_negative_offset: - ["Start offset is negative"], - invalid_data_view_offset: ["Start offset is outside the bounds of the buffer"], - invalid_data_view_length: ["Invalid data view length"], - invalid_data_view_accessor_offset: - ["Offset is outside the bounds of the DataView"], // ReferenceError invalid_lhs_in_assignment: ["Invalid left-hand side in assignment"], invalid_lhs_in_for: ["Invalid left-hand side in for-loop"], diff --git a/src/promise.js b/src/promise.js index b798fb9faf..630de4c85a 100644 --- a/src/promise.js +++ b/src/promise.js @@ -31,9 +31,9 @@ var lastMicrotaskId = 0; var GlobalPromise = function Promise(resolver) { if (resolver === promiseRaw) return; - if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); + if (!%_IsConstructCall()) throw MakeTypeError(kNotAPromise, this); if (!IS_SPEC_FUNCTION(resolver)) - throw MakeTypeError('resolver_not_a_function', [resolver]); + throw MakeTypeError(kResolverNotAFunction, resolver); var promise = PromiseInit(this); try { %DebugPushPromise(promise, Promise); @@ -106,7 +106,7 @@ function PromiseHandle(value, handler, deferred) { DEBUG_PREPARE_STEP_IN_IF_STEPPING(handler); var result = handler(value); if (result === deferred.promise) - throw MakeTypeError('promise_cyclic', [result]); + throw MakeTypeError(kPromiseCyclic, result); else if (IsPromise(result)) %_CallFunction(result, deferred.resolve, deferred.reject, PromiseChain); else @@ -224,7 +224,7 @@ function PromiseChain(onResolve, onReject) { // a.k.a. flatMap var deferred = %_CallFunction(this.constructor, PromiseDeferred); switch (GET_PRIVATE(this, promiseStatus)) { case UNDEFINED: - throw MakeTypeError('not_a_promise', [this]); + throw MakeTypeError(kNotAPromise, this); case 0: // Pending GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); @@ -272,7 +272,7 @@ function PromiseThen(onResolve, onReject) { x = PromiseCoerce(constructor, x); if (x === that) { DEBUG_PREPARE_STEP_IN_IF_STEPPING(onReject); - return onReject(MakeTypeError('promise_cyclic', [x])); + return onReject(MakeTypeError(kPromiseCyclic, x)); } else if (IsPromise(x)) { return x.then(onResolve, onReject); } else { diff --git a/src/proxy.js b/src/proxy.js index 44939f6920..b2be6d2076 100644 --- a/src/proxy.js +++ b/src/proxy.js @@ -23,19 +23,19 @@ var GlobalObject = global.Object; function ProxyCreate(handler, proto) { if (!IS_SPEC_OBJECT(handler)) - throw MakeTypeError("handler_non_object", ["create"]) + throw MakeTypeError(kProxyHandlerNonObject, "create") if (IS_UNDEFINED(proto)) proto = null else if (!(IS_SPEC_OBJECT(proto) || IS_NULL(proto))) - throw MakeTypeError("proto_non_object", ["create"]) + throw MakeTypeError(kProxyProtoNonObject) return %CreateJSProxy(handler, proto) } function ProxyCreateFunction(handler, callTrap, constructTrap) { if (!IS_SPEC_OBJECT(handler)) - throw MakeTypeError("handler_non_object", ["create"]) + throw MakeTypeError(kProxyHandlerNonObject, "createFunction") if (!IS_SPEC_FUNCTION(callTrap)) - throw MakeTypeError("trap_function_expected", ["createFunction", "call"]) + throw MakeTypeError(kProxyTrapFunctionExpected, "call") if (IS_UNDEFINED(constructTrap)) { constructTrap = DerivedConstructTrap(callTrap) } else if (IS_SPEC_FUNCTION(constructTrap)) { @@ -45,8 +45,7 @@ function ProxyCreateFunction(handler, callTrap, constructTrap) { return %Apply(construct, UNDEFINED, arguments, 0, %_ArgumentsLength()); } } else { - throw MakeTypeError("trap_function_expected", - ["createFunction", "construct"]) + throw MakeTypeError(kProxyTrapFunctionExpected, "construct") } return %CreateJSFunctionProxy( handler, callTrap, constructTrap, GlobalFunction.prototype) diff --git a/src/runtime.js b/src/runtime.js index 9d9fe27335..b425a2c186 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -876,9 +876,9 @@ function DefaultString(x) { throw %MakeTypeError(kCannotConvertToPrimitive); } -function ToPositiveInteger(x, rangeErrorName) { +function ToPositiveInteger(x, rangeErrorIndex) { var i = TO_INTEGER_MAP_MINUS_ZERO(x); - if (i < 0) throw MakeRangeError(rangeErrorName); + if (i < 0) throw MakeRangeError(rangeErrorIndex); return i; } diff --git a/src/runtime/runtime-array.cc b/src/runtime/runtime-array.cc index 8391f45158..95670d5950 100644 --- a/src/runtime/runtime-array.cc +++ b/src/runtime/runtime-array.cc @@ -5,6 +5,7 @@ #include "src/v8.h" #include "src/arguments.h" +#include "src/messages.h" #include "src/runtime/runtime-utils.h" namespace v8 { @@ -906,8 +907,7 @@ RUNTIME_FUNCTION(Runtime_ArrayConcat) { if (visitor.exceeds_array_limit()) { THROW_NEW_ERROR_RETURN_FAILURE( - isolate, - NewRangeError("invalid_array_length", HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kInvalidArrayLength)); } return *visitor.ToArray(); } diff --git a/src/runtime/runtime-typedarray.cc b/src/runtime/runtime-typedarray.cc index 26929565c6..dfc1bab8d3 100644 --- a/src/runtime/runtime-typedarray.cc +++ b/src/runtime/runtime-typedarray.cc @@ -80,14 +80,12 @@ RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { size_t allocated_length = 0; if (!TryNumberToSize(isolate, *byteLength, &allocated_length)) { THROW_NEW_ERROR_RETURN_FAILURE( - isolate, NewRangeError("invalid_array_buffer_length", - HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); } if (!Runtime::SetupArrayBufferAllocatingData(isolate, holder, allocated_length)) { THROW_NEW_ERROR_RETURN_FAILURE( - isolate, NewRangeError("invalid_array_buffer_length", - HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); } return *holder; } @@ -214,8 +212,7 @@ RUNTIME_FUNCTION(Runtime_TypedArrayInitialize) { if (length > static_cast(Smi::kMaxValue)) { THROW_NEW_ERROR_RETURN_FAILURE( - isolate, NewRangeError("invalid_typed_array_length", - HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayLength)); } // All checks are done, now we can modify objects. @@ -291,8 +288,7 @@ RUNTIME_FUNCTION(Runtime_TypedArrayInitializeFromArrayLike) { if ((length > static_cast(Smi::kMaxValue)) || (length > (kMaxInt / element_size))) { THROW_NEW_ERROR_RETURN_FAILURE( - isolate, NewRangeError("invalid_typed_array_length", - HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayLength)); } size_t byte_length = length * element_size; @@ -321,8 +317,7 @@ RUNTIME_FUNCTION(Runtime_TypedArrayInitializeFromArrayLike) { if (!Runtime::SetupArrayBufferAllocatingData(isolate, buffer, byte_length, false)) { THROW_NEW_ERROR_RETURN_FAILURE( - isolate, NewRangeError("invalid_array_buffer_length", - HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); } holder->set_buffer(*buffer); @@ -421,8 +416,7 @@ RUNTIME_FUNCTION(Runtime_TypedArraySetFastCases) { if (offset > target_length || offset + source_length > target_length || offset + source_length < offset) { // overflow THROW_NEW_ERROR_RETURN_FAILURE( - isolate, NewRangeError("typed_array_set_source_too_large", - HandleVector(NULL, 0))); + isolate, NewRangeError(MessageTemplate::kTypedArraySetSourceTooLarge)); } size_t target_offset = NumberToSize(isolate, target->byte_offset()); @@ -614,22 +608,22 @@ static bool DataViewSetValue(Isolate* isolate, Handle data_view, } -#define DATA_VIEW_GETTER(TypeName, Type, Converter) \ - RUNTIME_FUNCTION(Runtime_DataViewGet##TypeName) { \ - HandleScope scope(isolate); \ - DCHECK(args.length() == 3); \ - CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \ - CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset, 1); \ - CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 2); \ - Type result; \ - if (DataViewGetValue(isolate, holder, offset, is_little_endian, \ - &result)) { \ - return *isolate->factory()->Converter(result); \ - } else { \ - THROW_NEW_ERROR_RETURN_FAILURE( \ - isolate, NewRangeError("invalid_data_view_accessor_offset", \ - HandleVector(NULL, 0))); \ - } \ +#define DATA_VIEW_GETTER(TypeName, Type, Converter) \ + RUNTIME_FUNCTION(Runtime_DataViewGet##TypeName) { \ + HandleScope scope(isolate); \ + DCHECK(args.length() == 3); \ + CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \ + CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset, 1); \ + CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 2); \ + Type result; \ + if (DataViewGetValue(isolate, holder, offset, is_little_endian, \ + &result)) { \ + return *isolate->factory()->Converter(result); \ + } else { \ + THROW_NEW_ERROR_RETURN_FAILURE( \ + isolate, \ + NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset)); \ + } \ } DATA_VIEW_GETTER(Uint8, uint8_t, NewNumberFromUint) @@ -696,22 +690,22 @@ double DataViewConvertValue(double value) { } -#define DATA_VIEW_SETTER(TypeName, Type) \ - RUNTIME_FUNCTION(Runtime_DataViewSet##TypeName) { \ - HandleScope scope(isolate); \ - DCHECK(args.length() == 4); \ - CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \ - CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset, 1); \ - CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); \ - CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 3); \ - Type v = DataViewConvertValue(value->Number()); \ - if (DataViewSetValue(isolate, holder, offset, is_little_endian, v)) { \ - return isolate->heap()->undefined_value(); \ - } else { \ - THROW_NEW_ERROR_RETURN_FAILURE( \ - isolate, NewRangeError("invalid_data_view_accessor_offset", \ - HandleVector(NULL, 0))); \ - } \ +#define DATA_VIEW_SETTER(TypeName, Type) \ + RUNTIME_FUNCTION(Runtime_DataViewSet##TypeName) { \ + HandleScope scope(isolate); \ + DCHECK(args.length() == 4); \ + CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \ + CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset, 1); \ + CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); \ + CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 3); \ + Type v = DataViewConvertValue(value->Number()); \ + if (DataViewSetValue(isolate, holder, offset, is_little_endian, v)) { \ + return isolate->heap()->undefined_value(); \ + } else { \ + THROW_NEW_ERROR_RETURN_FAILURE( \ + isolate, \ + NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset)); \ + } \ } DATA_VIEW_SETTER(Uint8, uint8_t) diff --git a/src/typedarray.js b/src/typedarray.js index 8a16a25a39..9f0f03e585 100644 --- a/src/typedarray.js +++ b/src/typedarray.js @@ -38,10 +38,10 @@ macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { if (!IS_UNDEFINED(byteOffset)) { byteOffset = - ToPositiveInteger(byteOffset, "invalid_typed_array_length"); + ToPositiveInteger(byteOffset, kInvalidTypedArrayLength); } if (!IS_UNDEFINED(length)) { - length = ToPositiveInteger(length, "invalid_typed_array_length"); + length = ToPositiveInteger(length, kInvalidTypedArrayLength); } var bufferByteLength = %_ArrayBufferGetByteLength(buffer); @@ -52,11 +52,11 @@ function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { offset = byteOffset; if (offset % ELEMENT_SIZE !== 0) { - throw MakeRangeError("invalid_typed_array_alignment", - ["start offset", "NAME", ELEMENT_SIZE]); + throw MakeRangeError(kInvalidTypedArrayAlignment, + "start offset", "NAME", ELEMENT_SIZE); } if (offset > bufferByteLength) { - throw MakeRangeError("invalid_typed_array_offset"); + throw MakeRangeError(kInvalidTypedArrayOffset); } } @@ -64,8 +64,8 @@ function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { var newLength; if (IS_UNDEFINED(length)) { if (bufferByteLength % ELEMENT_SIZE !== 0) { - throw MakeRangeError("invalid_typed_array_alignment", - ["byte length", "NAME", ELEMENT_SIZE]); + throw MakeRangeError(kInvalidTypedArrayAlignment, + "byte length", "NAME", ELEMENT_SIZE); } newByteLength = bufferByteLength - offset; newLength = newByteLength / ELEMENT_SIZE; @@ -75,16 +75,16 @@ function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { } if ((offset + newByteLength > bufferByteLength) || (newLength > %_MaxSmi())) { - throw MakeRangeError("invalid_typed_array_length"); + throw MakeRangeError(kInvalidTypedArrayLength); } %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); } function NAMEConstructByLength(obj, length) { var l = IS_UNDEFINED(length) ? - 0 : ToPositiveInteger(length, "invalid_typed_array_length"); + 0 : ToPositiveInteger(length, kInvalidTypedArrayLength); if (l > %_MaxSmi()) { - throw MakeRangeError("invalid_typed_array_length"); + throw MakeRangeError(kInvalidTypedArrayLength); } var byteLength = l * ELEMENT_SIZE; if (byteLength > %_TypedArrayMaxSizeInHeap()) { @@ -97,10 +97,10 @@ function NAMEConstructByLength(obj, length) { function NAMEConstructByArrayLike(obj, arrayLike) { var length = arrayLike.length; - var l = ToPositiveInteger(length, "invalid_typed_array_length"); + var l = ToPositiveInteger(length, kInvalidTypedArrayLength); if (l > %_MaxSmi()) { - throw MakeRangeError("invalid_typed_array_length"); + throw MakeRangeError(kInvalidTypedArrayLength); } if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { for (var i = 0; i < l; i++) { @@ -257,12 +257,10 @@ function TypedArraySetFromOverlappingTypedArray(target, source, offset) { function TypedArraySet(obj, offset) { var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset); - if (intOffset < 0) { - throw MakeTypeError("typed_array_set_negative_offset"); - } + if (intOffset < 0) throw MakeTypeError(kTypedArraySetNegativeOffset); if (intOffset > %_MaxSmi()) { - throw MakeRangeError("typed_array_set_source_too_large"); + throw MakeRangeError(kTypedArraySetSourceTooLarge); } switch (%TypedArraySetFastCases(this, obj, intOffset)) { // These numbers should be synchronized with runtime.cc. @@ -282,12 +280,12 @@ function TypedArraySet(obj, offset) { // instead of silently ignoring the call, so that // the user knows (s)he did something wrong. // (Consistent with Firefox and Blink/WebKit) - throw MakeTypeError("invalid_argument"); + throw MakeTypeError(kInvalidArgument); } return; } if (intOffset + l > this.length) { - throw MakeRangeError("typed_array_set_source_too_large"); + throw MakeRangeError(kTypedArraySetSourceTooLarge); } TypedArraySetFromArrayLike(this, obj, l, intOffset); return; @@ -335,11 +333,9 @@ TYPED_ARRAYS(SETUP_TYPED_ARRAY) function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 if (%_IsConstructCall()) { - if (!IS_ARRAYBUFFER(buffer)) { - throw MakeTypeError('data_view_not_array_buffer', []); - } + if (!IS_ARRAYBUFFER(buffer)) throw MakeTypeError(kDataViewNotArrayBuffer); if (!IS_UNDEFINED(byteOffset)) { - byteOffset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); + byteOffset = ToPositiveInteger(byteOffset, kInvalidDataViewOffset); } if (!IS_UNDEFINED(byteLength)) { byteLength = TO_INTEGER(byteLength); @@ -348,15 +344,13 @@ function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); var offset = IS_UNDEFINED(byteOffset) ? 0 : byteOffset; - if (offset > bufferByteLength) { - throw MakeRangeError('invalid_data_view_offset'); - } + if (offset > bufferByteLength) throw MakeRangeError(kInvalidDataViewOffset); var length = IS_UNDEFINED(byteLength) ? bufferByteLength - offset : byteLength; if (length < 0 || offset + length > bufferByteLength) { - throw new MakeRangeError('invalid_data_view_length'); + throw new MakeRangeError(kInvalidDataViewLength); } %_DataViewInitialize(this, buffer, offset, length); } else { @@ -399,7 +393,7 @@ macro DATA_VIEW_TYPES(FUNCTION) endmacro function ToPositiveDataViewOffset(offset) { - return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset'); + return ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); } @@ -409,9 +403,7 @@ function DataViewGetTYPENAMEJS(offset, little_endian) { throw MakeTypeError(kIncompatibleMethodReceiver, 'DataView.getTYPENAME', this); } - if (%_ArgumentsLength() < 1) { - throw MakeTypeError('invalid_argument'); - } + if (%_ArgumentsLength() < 1) throw MakeTypeError(kInvalidArgument); return %DataViewGetTYPENAME(this, ToPositiveDataViewOffset(offset), !!little_endian); @@ -422,9 +414,7 @@ function DataViewSetTYPENAMEJS(offset, value, little_endian) { throw MakeTypeError(kIncompatibleMethodReceiver, 'DataView.setTYPENAME', this); } - if (%_ArgumentsLength() < 2) { - throw MakeTypeError('invalid_argument'); - } + if (%_ArgumentsLength() < 2) throw MakeTypeError(kInvalidArgument); %DataViewSetTYPENAME(this, ToPositiveDataViewOffset(offset), TO_NUMBER_INLINE(value), diff --git a/test/mjsunit/messages.js b/test/mjsunit/messages.js index 3eae74cd35..5a63e73369 100644 --- a/test/mjsunit/messages.js +++ b/test/mjsunit/messages.js @@ -72,6 +72,11 @@ test(function() { Uint16Array(1); }, "Constructor Uint16Array requires 'new'", TypeError); +// kDataViewNotArrayBuffer +test(function() { + new DataView(1); +}, "First argument to DataView constructor must be an ArrayBuffer", TypeError); + // kDateType test(function() { Date.prototype.setYear.call({}, 1); @@ -145,6 +150,11 @@ test(function() { new Map([1]); }, "Iterator value 1 is not an entry object", TypeError); +// kNotAPromise +test(function() { + Promise.prototype.chain.call(1); +}, "1 is not a promise", TypeError); + // kNotConstructor test(function() { new Symbol(); @@ -233,6 +243,11 @@ test(function() { [].reduce(function() {}); }, "Reduce of empty array with no initial value", TypeError); +// kResolverNotAFunction +test(function() { + new Promise(1); +}, "Promise resolver 1 is not a function", TypeError); + // kSymbolToPrimitive test(function() { 1 + Object(Symbol()); @@ -293,6 +308,16 @@ test(function() { Object.defineProperty([], "length", { value: 1E100 }); }, "defineProperty() array length out of range", RangeError); +// kInvalidArrayBufferLength +test(function() { + new ArrayBuffer(-1); +}, "Invalid array buffer length", RangeError); + +// kInvalidArrayLength +test(function() { + [].length = -1; +}, "Invalid array length", RangeError); + // kInvalidCodePoint test(function() { String.fromCodePoint(-1); @@ -303,6 +328,11 @@ test(function() { "a".repeat(-1); }, "Invalid count value", RangeError); +// kInvalidArrayBufferLength +test(function() { + new Uint16Array(-1); +}, "Invalid typed array length", RangeError); + // kNormalizationForm test(function() { "".normalize("ABC");