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(InvalidHint, "Invalid hint: %") \
261 T(InvalidLanguageTag, "Invalid language tag: %") \
262 T(InvalidWeakMapKey, "Invalid value used as weak map key") \
263 T(InvalidWeakSetValue, "Invalid value used in weak set") \
264 T(InvalidStringLength, "Invalid string length") \
265 T(InvalidTimeValue, "Invalid time value") \
266 T(InvalidTypedArrayAlignment, "% of % should be a multiple of %") \
267 T(InvalidTypedArrayLength, "Invalid typed array length") \
268 T(InvalidTypedArrayOffset, "Start offset is too large:") \
269 T(LocaleMatcher, "Illegal value for localeMatcher:%") \
270 T(NormalizationForm, "The normalization form should be one of %.") \
271 T(NumberFormatRange, "% argument must be between 0 and 20") \
272 T(PropertyValueOutOfRange, "% value is out of range.") \
273 T(StackOverflow, "Maximum call stack size exceeded") \
274 T(ToPrecisionFormatRange, "toPrecision() argument must be between 1 and 21") \
275 T(ToRadixFormatRange, "toString() radix argument must be between 2 and 36") \
276 T(TypedArraySetNegativeOffset, "Start offset is negative") \
277 T(TypedArraySetSourceTooLarge, "Source is too large") \
278 T(UnsupportedTimeZone, "Unsupported time zone specified %") \
279 T(ValueOutOfRange, "Value % out of range for % options property %") \
281 T(BadGetterArity, "Getter must not have any formal parameters.") \
282 T(BadSetterArity, "Setter must have exactly one formal parameter.") \
283 T(ConstructorIsAccessor, "Class constructor may not be an accessor") \
284 T(ConstructorIsGenerator, "Class constructor may not be a generator") \
285 T(DerivedConstructorReturn, \
286 "Derived constructors may only return object or undefined") \
287 T(DuplicateConstructor, "A class may only have one constructor") \
288 T(DuplicateExport, "Duplicate export of '%'") \
290 "Duplicate __proto__ fields are not allowed in object literals") \
291 T(ForInLoopInitializer, \
292 "for-in loop variable declaration may not have an initializer.") \
293 T(ForInOfLoopMultiBindings, \
294 "Invalid left-hand side in % loop: Must have a single binding.") \
295 T(ForOfLoopInitializer, \
296 "for-of loop variable declaration may not have an initializer.") \
297 T(IllegalAccess, "Illegal access") \
298 T(IllegalBreak, "Illegal break statement") \
299 T(IllegalContinue, "Illegal continue statement") \
300 T(IllegalLanguageModeDirective, \
301 "Illegal '%' directive in function with non-simple parameter list") \
302 T(IllegalReturn, "Illegal return statement") \
303 T(InvalidLhsInAssignment, "Invalid left-hand side in assignment") \
304 T(InvalidLhsInFor, "Invalid left-hand side in for-loop") \
305 T(InvalidLhsInPostfixOp, \
306 "Invalid left-hand side expression in postfix operation") \
307 T(InvalidLhsInPrefixOp, \
308 "Invalid left-hand side expression in prefix operation") \
309 T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \
310 T(LabelRedeclaration, "Label '%' has already been declared") \
311 T(MalformedArrowFunParamList, "Malformed arrow function parameter list") \
312 T(MalformedRegExp, "Invalid regular expression: /%/: %") \
313 T(MalformedRegExpFlags, "Invalid regular expression flags") \
314 T(ModuleExportUndefined, "Export '%' is not defined in module") \
315 T(MultipleDefaultsInSwitch, \
316 "More than one default clause in switch statement") \
317 T(NewlineAfterThrow, "Illegal newline after throw") \
318 T(NoCatchOrFinally, "Missing catch or finally after try") \
319 T(NotIsvar, "builtin %%IS_VAR: not a variable") \
320 T(ParamAfterRest, "Rest parameter must be last formal parameter") \
321 T(BadSetterRestParameter, \
322 "Setter function argument must not be a rest parameter") \
323 T(ParamDupe, "Duplicate parameter name not allowed in this context") \
324 T(ParenthesisInArgString, "Function arg string contains parenthesis") \
325 T(SingleFunctionLiteral, "Single function literal required") \
327 "Block-scoped declarations (let, const, function, class) not yet " \
328 "supported outside strict mode") \
329 T(StrictDelete, "Delete of an unqualified identifier in strict mode.") \
330 T(StrictEvalArguments, "Unexpected eval or arguments in strict mode") \
332 "In strict mode code, functions can only be declared at top level or " \
333 "immediately within another function.") \
334 T(StrictOctalLiteral, "Octal literals are not allowed in strict mode.") \
335 T(StrictWith, "Strict mode code may not include a with statement") \
337 "In strong mode, 'arguments' is deprecated, use '...args' instead") \
338 T(StrongConstructorReturnMisplaced, \
339 "In strong mode, returning from a constructor before its super " \
340 "constructor invocation or all assignments to 'this' is deprecated") \
341 T(StrongConstructorReturnValue, \
342 "In strong mode, returning a value from a constructor is deprecated") \
343 T(StrongConstructorSuper, \
344 "In strong mode, 'super' can only be used to invoke the super " \
345 "constructor, and cannot be nested inside another statement or " \
347 T(StrongConstructorThis, \
348 "In strong mode, 'this' can only be used to initialize properties, and " \
349 "cannot be nested inside another statement or expression") \
351 "In strong mode, 'delete' is deprecated, use maps or sets instead") \
352 T(StrongDirectEval, "In strong mode, direct calls to eval are deprecated") \
354 "In strong mode, arrays with holes are deprecated, use maps instead") \
356 "In strong mode, empty sub-statements are deprecated, make them explicit " \
357 "with '{}' instead") \
359 "In strong mode, '==' and '!=' are deprecated, use '===' and '!==' " \
362 "In strong mode, 'for'-'in' loops are deprecated, use 'for'-'of' instead") \
363 T(StrongPropertyAccess, \
364 "In strong mode, accessing missing property '%' of % is deprecated") \
365 T(StrongSuperCallDuplicate, \
366 "In strong mode, invoking the super constructor multiple times is " \
368 T(StrongSuperCallMisplaced, \
369 "In strong mode, the super constructor must be invoked before any " \
370 "assignment to 'this'") \
371 T(StrongSwitchFallthrough, \
372 "In strong mode, switch fall-through is deprecated, terminate each case " \
373 "with 'break', 'continue', 'return' or 'throw'") \
375 "In strong mode, binding or assigning to 'undefined' is deprecated") \
376 T(StrongUseBeforeDeclaration, \
377 "In strong mode, declaring variable '%' before its use is required") \
379 "In strong mode, 'var' is deprecated, use 'let' or 'const' instead") \
380 T(TemplateOctalLiteral, \
381 "Octal literals are not allowed in template strings.") \
382 T(ThisFormalParameter, "'this' is not a valid formal parameter name") \
383 T(TooManyArguments, \
384 "Too many arguments in function call (only 65535 allowed)") \
385 T(TooManyParameters, \
386 "Too many parameters in function definition (only 65535 allowed)") \
387 T(TooManyVariables, "Too many variables declared (only 4194303 allowed)") \
388 T(UnexpectedEOS, "Unexpected end of input") \
389 T(UnexpectedReserved, "Unexpected reserved word") \
390 T(UnexpectedStrictReserved, "Unexpected strict mode reserved word") \
391 T(UnexpectedSuper, "'super' keyword unexpected here") \
392 T(UnexpectedNewTarget, "new.target expression is not allowed here") \
393 T(UnexpectedTemplateString, "Unexpected template string") \
394 T(UnexpectedToken, "Unexpected token %") \
395 T(UnexpectedTokenIdentifier, "Unexpected identifier") \
396 T(UnexpectedTokenNumber, "Unexpected number") \
397 T(UnexpectedTokenString, "Unexpected string") \
398 T(UnexpectedTokenRegExp, "Unexpected regular expression") \
399 T(UnknownLabel, "Undefined label '%'") \
400 T(UnterminatedArgList, "missing ) after argument list") \
401 T(UnterminatedRegExp, "Invalid regular expression: missing /") \
402 T(UnterminatedTemplate, "Unterminated template literal") \
403 T(UnterminatedTemplateExpr, "Missing } in template expression") \
405 T(CodeGenFromStrings, "%") \
407 T(URIMalformed, "URI malformed")
409 class MessageTemplate {
412 #define TEMPLATE(NAME, STRING) k##NAME,
413 MESSAGE_TEMPLATES(TEMPLATE)
418 static const char* TemplateString(int template_index);
420 static MaybeHandle<String> FormatMessage(int template_index,
423 Handle<String> arg2);
425 static Handle<String> FormatMessage(Isolate* isolate, int template_index,
430 // A message handler is a convenience interface for accessing the list
431 // of message listeners registered in an environment
432 class MessageHandler {
434 // Returns a message object for the API to use.
435 static Handle<JSMessageObject> MakeMessageObject(
436 Isolate* isolate, MessageTemplate::Template type,
437 MessageLocation* location, Handle<Object> argument,
438 Handle<JSArray> stack_frames);
440 // Report a formatted message (needs JS allocation).
441 static void ReportMessage(Isolate* isolate, MessageLocation* loc,
442 Handle<JSMessageObject> message);
444 static void DefaultMessageReport(Isolate* isolate, const MessageLocation* loc,
445 Handle<Object> message_obj);
446 static Handle<String> GetMessage(Isolate* isolate, Handle<Object> data);
447 static base::SmartArrayPointer<char> GetLocalizedMessage(Isolate* isolate,
448 Handle<Object> data);
452 class ErrorToStringHelper {
454 ErrorToStringHelper() : visited_(0) {}
456 MUST_USE_RESULT MaybeHandle<String> Stringify(Isolate* isolate,
457 Handle<JSObject> error);
462 VisitedScope(ErrorToStringHelper* helper, Handle<JSObject> error)
463 : helper_(helper), has_visited_(false) {
464 for (const Handle<JSObject>& visited : helper->visited_) {
465 if (visited.is_identical_to(error)) {
470 helper->visited_.Add(error);
472 ~VisitedScope() { helper_->visited_.RemoveLast(); }
473 bool has_visited() { return has_visited_; }
476 ErrorToStringHelper* helper_;
480 static bool ShadowsInternalError(Isolate* isolate,
481 LookupIterator* property_lookup,
482 LookupIterator* internal_error_lookup);
484 static MUST_USE_RESULT MaybeHandle<String> GetStringifiedProperty(
485 Isolate* isolate, LookupIterator* property_lookup,
486 Handle<String> default_value);
488 List<Handle<JSObject> > visited_;
490 } } // namespace v8::internal
492 #endif // V8_MESSAGES_H_