1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // The infrastructure used for (localized) message reporting in V8.
7 // Note: there's a big unresolved issue about ownership of the data
8 // structures used by this framework.
10 #ifndef V8_MESSAGES_H_
11 #define V8_MESSAGES_H_
13 #include "src/base/smart-pointers.h"
14 #include "src/handles.h"
20 // Forward declarations.
21 class JSMessageObject;
25 class MessageLocation {
27 MessageLocation(Handle<Script> script, int start_pos, int end_pos,
28 Handle<JSFunction> function = Handle<JSFunction>())
30 start_pos_(start_pos),
32 function_(function) {}
33 MessageLocation() : start_pos_(-1), end_pos_(-1) { }
35 Handle<Script> script() const { return script_; }
36 int start_pos() const { return start_pos_; }
37 int end_pos() const { return end_pos_; }
38 Handle<JSFunction> function() const { return function_; }
41 Handle<Script> script_;
44 Handle<JSFunction> function_;
50 CallSite(Isolate* isolate, Handle<JSObject> call_site_obj);
52 Handle<Object> GetFileName();
53 Handle<Object> GetFunctionName();
54 Handle<Object> GetScriptNameOrSourceUrl();
55 Handle<Object> GetMethodName();
56 // Return 1-based line number, including line offset.
58 // Return 1-based column number, including column offset if first line.
59 int GetColumnNumber();
67 Handle<Object> receiver_;
68 Handle<JSFunction> fun_;
73 #define MESSAGE_TEMPLATES(T) \
76 T(CyclicProto, "Cyclic __proto__ value") \
77 T(Debugger, "Debugger: %") \
78 T(DebuggerLoading, "Error loading debugger") \
79 T(DefaultOptionsMissing, "Internal % error. Default options are missing.") \
80 T(UncaughtException, "Uncaught %") \
81 T(Unsupported, "Not supported") \
82 T(WrongServiceType, "Internal error, wrong service type: %") \
83 T(WrongValueType, "Internal error. Wrong value type.") \
86 "Function.prototype.apply was called on %, which is a % and not a " \
88 T(ArrayFunctionsOnFrozen, "Cannot modify frozen array elements") \
89 T(ArrayFunctionsOnSealed, "Cannot add/remove sealed array elements") \
90 T(ArrayNotSubclassable, "Subclassing Arrays is not currently supported.") \
91 T(CalledNonCallable, "% is not a function") \
92 T(CalledOnNonObject, "% called on non-object") \
93 T(CalledOnNullOrUndefined, "% called on null or undefined") \
94 T(CannotConvertToPrimitive, "Cannot convert object to primitive value") \
95 T(CannotPreventExtExternalArray, \
96 "Cannot prevent extension of an object with external array elements") \
97 T(CircularStructure, "Converting circular structure to JSON") \
98 T(ConstAssign, "Assignment to constant variable.") \
99 T(ConstructorNonCallable, \
100 "Class constructors cannot be invoked without 'new'") \
101 T(ConstructorNotFunction, "Constructor % requires 'new'") \
102 T(CurrencyCode, "Currency code is required with currency style.") \
103 T(DataViewNotArrayBuffer, \
104 "First argument to DataView constructor must be an ArrayBuffer") \
105 T(DateType, "this is not a Date object.") \
106 T(DebuggerFrame, "Debugger: Invalid frame index.") \
107 T(DebuggerType, "Debugger: Parameters have wrong types.") \
108 T(DefineDisallowed, "Cannot define property:%, object is not extensible.") \
109 T(DuplicateTemplateProperty, "Object template has duplicate property '%'") \
110 T(ExtendsValueGenerator, \
111 "Class extends value % may not be a generator function") \
112 T(ExtendsValueNotFunction, \
113 "Class extends value % is not a function or null") \
114 T(FirstArgumentNotRegExp, \
115 "First argument to % must not be a regular expression") \
116 T(FlagsGetterNonObject, \
117 "RegExp.prototype.flags getter called on non-object %") \
118 T(FunctionBind, "Bind must be called on a function") \
119 T(GeneratorRunning, "Generator is already running") \
120 T(IllegalInvocation, "Illegal invocation") \
121 T(IncompatibleMethodReceiver, "Method % called on incompatible receiver %") \
122 T(InstanceofFunctionExpected, \
123 "Expecting a function in instanceof check, but got %") \
124 T(InstanceofNonobjectProto, \
125 "Function has non-object prototype '%' in instanceof check") \
126 T(InvalidArgument, "invalid_argument") \
127 T(InvalidInOperatorUse, "Cannot use 'in' operator to search for '%' in %") \
128 T(IteratorResultNotAnObject, "Iterator result % is not an object") \
129 T(IteratorValueNotAnObject, "Iterator value % is not an entry object") \
130 T(LanguageID, "Language ID should be string or object.") \
131 T(MethodCalledOnWrongObject, \
132 "Method % called on a non-object or on a wrong type of object.") \
133 T(MethodInvokedOnNullOrUndefined, \
134 "Method invoked on undefined or null value.") \
135 T(MethodInvokedOnWrongType, "Method invoked on an object that is not %.") \
136 T(NoAccess, "no access") \
137 T(NonCoercible, "Cannot match against 'undefined' or 'null'.") \
138 T(NonExtensibleProto, "% is not extensible") \
139 T(NonObjectPropertyLoad, "Cannot read property '%' of %") \
140 T(NonObjectPropertyStore, "Cannot set property '%' of %") \
141 T(NoSetterInCallback, "Cannot set property % of % which has only a getter") \
142 T(NotAnIterator, "% is not an iterator") \
143 T(NotAPromise, "% is not a promise") \
144 T(NotConstructor, "% is not a constructor") \
145 T(NotDateObject, "this is not a Date object.") \
146 T(NotIntlObject, "% is not an i18n object.") \
147 T(NotGeneric, "% is not generic") \
148 T(NotIterable, "% is not iterable") \
149 T(NotTypedArray, "this is not a typed array.") \
150 T(NotSharedTypedArray, "% is not a shared typed array.") \
151 T(NotIntegerSharedTypedArray, "% is not an integer shared typed array.") \
152 T(NotInt32SharedTypedArray, "% is not an int32 shared typed array.") \
153 T(ObjectGetterExpectingFunction, \
154 "Object.prototype.__defineGetter__: Expecting function") \
155 T(ObjectGetterCallable, "Getter must be a function: %") \
156 T(ObjectNotExtensible, "Can't add property %, object is not extensible") \
157 T(ObjectSetterExpectingFunction, \
158 "Object.prototype.__defineSetter__: Expecting function") \
159 T(ObjectSetterCallable, "Setter must be a function: %") \
160 T(ObserveCallbackFrozen, \
161 "Object.observe cannot deliver to a frozen function object") \
162 T(ObserveGlobalProxy, "% cannot be called on the global proxy object") \
163 T(ObserveInvalidAccept, \
164 "Third argument to Object.observe must be an array of strings.") \
165 T(ObserveNonFunction, "Object.% cannot deliver to non-function") \
166 T(ObserveNonObject, "Object.% cannot % non-object") \
167 T(ObserveNotifyNonNotifier, "notify called on non-notifier object") \
168 T(ObservePerformNonFunction, "Cannot perform non-function") \
169 T(ObservePerformNonString, "Invalid non-string changeType") \
170 T(ObserveTypeNonString, \
171 "Invalid changeRecord with non-string 'type' property") \
172 T(OrdinaryFunctionCalledAsConstructor, \
173 "Function object that's not a constructor was created with new") \
174 T(PromiseCyclic, "Chaining cycle detected for promise %") \
175 T(PropertyDescObject, "Property description must be an object: %") \
176 T(PropertyNotFunction, "Property '%' of object % is not a function") \
177 T(ProtoObjectOrNull, "Object prototype may only be an Object or null: %") \
178 T(PrototypeParentNotAnObject, \
179 "Class extends value does not have valid prototype property %") \
180 T(ProxyHandlerDeleteFailed, \
181 "Proxy handler % did not return a boolean value from 'delete' trap") \
182 T(ProxyHandlerNonObject, "Proxy.% called with non-object as handler") \
183 T(ProxyHandlerReturned, "Proxy handler % returned % from '%' trap") \
184 T(ProxyHandlerTrapMissing, "Proxy handler % has no '%' trap") \
185 T(ProxyHandlerTrapMustBeCallable, \
186 "Proxy handler %0 has non-callable '%' trap") \
187 T(ProxyNonObjectPropNames, "Trap '%' returned non-object %") \
188 T(ProxyProtoNonObject, "Proxy.create called with no-object as prototype") \
189 T(ProxyPropNotConfigurable, \
190 "Proxy handler % returned non-configurable descriptor for property '%' " \
192 T(ProxyRepeatedPropName, "Trap '%' returned repeated property name '%'") \
193 T(ProxyTrapFunctionExpected, \
194 "Proxy.createFunction called with non-function for '%' trap") \
195 T(RedefineDisallowed, "Cannot redefine property: %") \
196 T(RedefineExternalArray, \
197 "Cannot redefine a property of an object with external array elements") \
198 T(ReduceNoInitial, "Reduce of empty array with no initial value") \
200 "Cannot supply flags when constructing one RegExp from another") \
201 T(ReinitializeIntl, "Trying to re-initialize % object.") \
202 T(ResolvedOptionsCalledOnNonObject, \
203 "resolvedOptions method called on a non-object or on a object that is " \
205 T(ResolverNotAFunction, "Promise resolver % is not a function") \
206 T(RestrictedFunctionProperties, \
207 "'caller' and 'arguments' are restricted function properties and cannot " \
208 "be accessed in this context.") \
209 T(StaticPrototype, "Classes may not have static property named prototype") \
210 T(StrictCannotAssign, "Cannot assign to read only '% in strict mode") \
211 T(StrictDeleteProperty, "Cannot delete property '%' of %") \
212 T(StrictPoisonPill, \
213 "'caller', 'callee', and 'arguments' properties may not be accessed on " \
214 "strict mode functions or the arguments objects for calls to them") \
215 T(StrictReadOnlyProperty, "Cannot assign to read only property '%' of %") \
217 "In strong mode, calling a function with too few arguments is deprecated") \
218 T(StrongDeleteProperty, \
219 "Deleting property '%' of strong object '%' is deprecated") \
220 T(StrongExtendNull, "In strong mode, classes extending null are deprecated") \
221 T(StrongImplicitConversion, \
222 "In strong mode, implicit conversions are deprecated") \
223 T(StrongRedefineDisallowed, \
224 "On strong object %, redefining writable, non-configurable property '%' " \
225 "to be non-writable is deprecated") \
227 "On strong object %, redefining the internal prototype is deprecated") \
228 T(SymbolKeyFor, "% is not a symbol") \
229 T(SymbolToNumber, "Cannot convert a Symbol value to a number") \
230 T(SymbolToString, "Cannot convert a Symbol value to a string") \
231 T(SimdToNumber, "Cannot convert a SIMD value to a number") \
232 T(UndefinedOrNullToObject, "Cannot convert undefined or null to object") \
233 T(ValueAndAccessor, \
234 "Invalid property. A property cannot both have accessors and be " \
235 "writable or have a value, %") \
236 T(VarRedeclaration, "Identifier '%' has already been declared") \
237 T(WithExpression, "% has no properties") \
238 T(WrongArgs, "%: Arguments list has wrong type") \
239 /* ReferenceError */ \
240 T(NonMethod, "'super' is referenced from non-method") \
241 T(NotDefined, "% is not defined") \
242 T(StrongSuperCallMissing, \
243 "In strong mode, invoking the super constructor in a subclass is " \
245 T(StrongUnboundGlobal, \
246 "In strong mode, using an undeclared global variable '%' is not allowed") \
247 T(UnsupportedSuper, "Unsupported reference to 'super'") \
249 T(DateRange, "Provided date is not in valid range.") \
250 T(ExpectedLocation, "Expected Area/Location for time zone, got %") \
251 T(InvalidArrayBufferLength, "Invalid array buffer length") \
252 T(InvalidArrayLength, "Invalid array length") \
253 T(InvalidCodePoint, "Invalid code point %") \
254 T(InvalidCountValue, "Invalid count value") \
255 T(InvalidCurrencyCode, "Invalid currency code: %") \
256 T(InvalidDataViewAccessorOffset, \
257 "Offset is outside the bounds of the DataView") \
258 T(InvalidDataViewLength, "Invalid data view length") \
259 T(InvalidDataViewOffset, "Start offset is outside the bounds of the buffer") \
260 T(InvalidLanguageTag, "Invalid language tag: %") \
261 T(InvalidWeakMapKey, "Invalid value used as weak map key") \
262 T(InvalidWeakSetValue, "Invalid value used in weak set") \
263 T(InvalidStringLength, "Invalid string length") \
264 T(InvalidTimeValue, "Invalid time value") \
265 T(InvalidTypedArrayAlignment, "% of % should be a multiple of %") \
266 T(InvalidTypedArrayLength, "Invalid typed array length") \
267 T(InvalidTypedArrayOffset, "Start offset is too large:") \
268 T(LocaleMatcher, "Illegal value for localeMatcher:%") \
269 T(NormalizationForm, "The normalization form should be one of %.") \
270 T(NumberFormatRange, "% argument must be between 0 and 20") \
271 T(PropertyValueOutOfRange, "% value is out of range.") \
272 T(StackOverflow, "Maximum call stack size exceeded") \
273 T(ToPrecisionFormatRange, "toPrecision() argument must be between 1 and 21") \
274 T(ToRadixFormatRange, "toString() radix argument must be between 2 and 36") \
275 T(TypedArraySetNegativeOffset, "Start offset is negative") \
276 T(TypedArraySetSourceTooLarge, "Source is too large") \
277 T(UnsupportedTimeZone, "Unsupported time zone specified %") \
278 T(ValueOutOfRange, "Value % out of range for % options property %") \
280 T(BadGetterArity, "Getter must not have any formal parameters.") \
281 T(BadSetterArity, "Setter must have exactly one formal parameter.") \
282 T(ConstructorIsAccessor, "Class constructor may not be an accessor") \
283 T(ConstructorIsGenerator, "Class constructor may not be a generator") \
284 T(DerivedConstructorReturn, \
285 "Derived constructors may only return object or undefined") \
286 T(DuplicateConstructor, "A class may only have one constructor") \
287 T(DuplicateExport, "Duplicate export of '%'") \
289 "Duplicate __proto__ fields are not allowed in object literals") \
290 T(ForInLoopInitializer, \
291 "for-in loop variable declaration may not have an initializer.") \
292 T(ForInOfLoopMultiBindings, \
293 "Invalid left-hand side in % loop: Must have a single binding.") \
294 T(ForOfLoopInitializer, \
295 "for-of loop variable declaration may not have an initializer.") \
296 T(IllegalAccess, "Illegal access") \
297 T(IllegalBreak, "Illegal break statement") \
298 T(IllegalContinue, "Illegal continue statement") \
299 T(IllegalLanguageModeDirective, \
300 "Illegal '%' directive in function with non-simple parameter list") \
301 T(IllegalReturn, "Illegal return statement") \
302 T(InvalidLhsInAssignment, "Invalid left-hand side in assignment") \
303 T(InvalidLhsInFor, "Invalid left-hand side in for-loop") \
304 T(InvalidLhsInPostfixOp, \
305 "Invalid left-hand side expression in postfix operation") \
306 T(InvalidLhsInPrefixOp, \
307 "Invalid left-hand side expression in prefix operation") \
308 T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \
309 T(LabelRedeclaration, "Label '%' has already been declared") \
310 T(MalformedArrowFunParamList, "Malformed arrow function parameter list") \
311 T(MalformedRegExp, "Invalid regular expression: /%/: %") \
312 T(MalformedRegExpFlags, "Invalid regular expression flags") \
313 T(ModuleExportUndefined, "Export '%' is not defined in module") \
314 T(MultipleDefaultsInSwitch, \
315 "More than one default clause in switch statement") \
316 T(NewlineAfterThrow, "Illegal newline after throw") \
317 T(NoCatchOrFinally, "Missing catch or finally after try") \
318 T(NotIsvar, "builtin %%IS_VAR: not a variable") \
319 T(ParamAfterRest, "Rest parameter must be last formal parameter") \
320 T(BadSetterRestParameter, \
321 "Setter function argument must not be a rest parameter") \
322 T(ParamDupe, "Duplicate parameter name not allowed in this context") \
323 T(ParenthesisInArgString, "Function arg string contains parenthesis") \
324 T(SingleFunctionLiteral, "Single function literal required") \
326 "Block-scoped declarations (let, const, function, class) not yet " \
327 "supported outside strict mode") \
328 T(StrictDelete, "Delete of an unqualified identifier in strict mode.") \
329 T(StrictEvalArguments, "Unexpected eval or arguments in strict mode") \
331 "In strict mode code, functions can only be declared at top level or " \
332 "immediately within another function.") \
333 T(StrictOctalLiteral, "Octal literals are not allowed in strict mode.") \
334 T(StrictWith, "Strict mode code may not include a with statement") \
336 "In strong mode, 'arguments' is deprecated, use '...args' instead") \
337 T(StrongConstructorReturnMisplaced, \
338 "In strong mode, returning from a constructor before its super " \
339 "constructor invocation or all assignments to 'this' is deprecated") \
340 T(StrongConstructorReturnValue, \
341 "In strong mode, returning a value from a constructor is deprecated") \
342 T(StrongConstructorSuper, \
343 "In strong mode, 'super' can only be used to invoke the super " \
344 "constructor, and cannot be nested inside another statement or " \
346 T(StrongConstructorThis, \
347 "In strong mode, 'this' can only be used to initialize properties, and " \
348 "cannot be nested inside another statement or expression") \
350 "In strong mode, 'delete' is deprecated, use maps or sets instead") \
351 T(StrongDirectEval, "In strong mode, direct calls to eval are deprecated") \
353 "In strong mode, arrays with holes are deprecated, use maps instead") \
355 "In strong mode, empty sub-statements are deprecated, make them explicit " \
356 "with '{}' instead") \
358 "In strong mode, '==' and '!=' are deprecated, use '===' and '!==' " \
361 "In strong mode, 'for'-'in' loops are deprecated, use 'for'-'of' instead") \
362 T(StrongPropertyAccess, \
363 "In strong mode, accessing missing property '%' of % is deprecated") \
364 T(StrongSuperCallDuplicate, \
365 "In strong mode, invoking the super constructor multiple times is " \
367 T(StrongSuperCallMisplaced, \
368 "In strong mode, the super constructor must be invoked before any " \
369 "assignment to 'this'") \
370 T(StrongSwitchFallthrough, \
371 "In strong mode, switch fall-through is deprecated, terminate each case " \
372 "with 'break', 'continue', 'return' or 'throw'") \
374 "In strong mode, binding or assigning to 'undefined' is deprecated") \
375 T(StrongUseBeforeDeclaration, \
376 "In strong mode, declaring variable '%' before its use is required") \
378 "In strong mode, 'var' is deprecated, use 'let' or 'const' instead") \
379 T(TemplateOctalLiteral, \
380 "Octal literals are not allowed in template strings.") \
381 T(ThisFormalParameter, "'this' is not a valid formal parameter name") \
382 T(TooManyArguments, \
383 "Too many arguments in function call (only 65535 allowed)") \
384 T(TooManyParameters, \
385 "Too many parameters in function definition (only 65535 allowed)") \
386 T(TooManyVariables, "Too many variables declared (only 4194303 allowed)") \
387 T(UnexpectedEOS, "Unexpected end of input") \
388 T(UnexpectedReserved, "Unexpected reserved word") \
389 T(UnexpectedStrictReserved, "Unexpected strict mode reserved word") \
390 T(UnexpectedSuper, "'super' keyword unexpected here") \
391 T(UnexpectedNewTarget, "new.target expression is not allowed here") \
392 T(UnexpectedTemplateString, "Unexpected template string") \
393 T(UnexpectedToken, "Unexpected token %") \
394 T(UnexpectedTokenIdentifier, "Unexpected identifier") \
395 T(UnexpectedTokenNumber, "Unexpected number") \
396 T(UnexpectedTokenString, "Unexpected string") \
397 T(UnexpectedTokenRegExp, "Unexpected regular expression") \
398 T(UnknownLabel, "Undefined label '%'") \
399 T(UnterminatedArgList, "missing ) after argument list") \
400 T(UnterminatedRegExp, "Invalid regular expression: missing /") \
401 T(UnterminatedTemplate, "Unterminated template literal") \
402 T(UnterminatedTemplateExpr, "Missing } in template expression") \
404 T(CodeGenFromStrings, "%") \
406 T(URIMalformed, "URI malformed")
408 class MessageTemplate {
411 #define TEMPLATE(NAME, STRING) k##NAME,
412 MESSAGE_TEMPLATES(TEMPLATE)
417 static const char* TemplateString(int template_index);
419 static MaybeHandle<String> FormatMessage(int template_index,
422 Handle<String> arg2);
424 static Handle<String> FormatMessage(Isolate* isolate, int template_index,
429 // A message handler is a convenience interface for accessing the list
430 // of message listeners registered in an environment
431 class MessageHandler {
433 // Returns a message object for the API to use.
434 static Handle<JSMessageObject> MakeMessageObject(
435 Isolate* isolate, MessageTemplate::Template type,
436 MessageLocation* location, Handle<Object> argument,
437 Handle<JSArray> stack_frames);
439 // Report a formatted message (needs JS allocation).
440 static void ReportMessage(Isolate* isolate, MessageLocation* loc,
441 Handle<JSMessageObject> message);
443 static void DefaultMessageReport(Isolate* isolate, const MessageLocation* loc,
444 Handle<Object> message_obj);
445 static Handle<String> GetMessage(Isolate* isolate, Handle<Object> data);
446 static base::SmartArrayPointer<char> GetLocalizedMessage(Isolate* isolate,
447 Handle<Object> data);
451 class ErrorToStringHelper {
453 ErrorToStringHelper() : visited_(0) {}
455 MUST_USE_RESULT MaybeHandle<String> Stringify(Isolate* isolate,
456 Handle<JSObject> error);
461 VisitedScope(ErrorToStringHelper* helper, Handle<JSObject> error)
462 : helper_(helper), has_visited_(false) {
463 for (const Handle<JSObject>& visited : helper->visited_) {
464 if (visited.is_identical_to(error)) {
469 helper->visited_.Add(error);
471 ~VisitedScope() { helper_->visited_.RemoveLast(); }
472 bool has_visited() { return has_visited_; }
475 ErrorToStringHelper* helper_;
479 static bool ShadowsInternalError(Isolate* isolate,
480 LookupIterator* property_lookup,
481 LookupIterator* internal_error_lookup);
483 static MUST_USE_RESULT MaybeHandle<String> GetStringifiedProperty(
484 Isolate* isolate, LookupIterator* property_lookup,
485 Handle<String> default_value);
487 List<Handle<JSObject> > visited_;
489 } } // namespace v8::internal
491 #endif // V8_MESSAGES_H_