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(ObserveAccessChecked, "% cannot be called on access-checked objects") \
164 T(ObserveInvalidAccept, \
165 "Third argument to Object.observe must be an array of strings.") \
166 T(ObserveNonFunction, "Object.% cannot deliver to non-function") \
167 T(ObserveNonObject, "Object.% cannot % non-object") \
168 T(ObserveNotifyNonNotifier, "notify called on non-notifier object") \
169 T(ObservePerformNonFunction, "Cannot perform non-function") \
170 T(ObservePerformNonString, "Invalid non-string changeType") \
171 T(ObserveTypeNonString, \
172 "Invalid changeRecord with non-string 'type' property") \
173 T(OrdinaryFunctionCalledAsConstructor, \
174 "Function object that's not a constructor was created with new") \
175 T(PromiseCyclic, "Chaining cycle detected for promise %") \
176 T(PropertyDescObject, "Property description must be an object: %") \
177 T(PropertyNotFunction, "Property '%' of object % is not a function") \
178 T(ProtoObjectOrNull, "Object prototype may only be an Object or null: %") \
179 T(PrototypeParentNotAnObject, \
180 "Class extends value does not have valid prototype property %") \
181 T(ProxyHandlerDeleteFailed, \
182 "Proxy handler % did not return a boolean value from 'delete' trap") \
183 T(ProxyHandlerNonObject, "Proxy.% called with non-object as handler") \
184 T(ProxyHandlerReturned, "Proxy handler % returned % from '%' trap") \
185 T(ProxyHandlerTrapMissing, "Proxy handler % has no '%' trap") \
186 T(ProxyHandlerTrapMustBeCallable, \
187 "Proxy handler %0 has non-callable '%' trap") \
188 T(ProxyNonObjectPropNames, "Trap '%' returned non-object %") \
189 T(ProxyProtoNonObject, "Proxy.create called with no-object as prototype") \
190 T(ProxyPropNotConfigurable, \
191 "Proxy handler % returned non-configurable descriptor for property '%' " \
193 T(ProxyRepeatedPropName, "Trap '%' returned repeated property name '%'") \
194 T(ProxyTrapFunctionExpected, \
195 "Proxy.createFunction called with non-function for '%' trap") \
196 T(RedefineDisallowed, "Cannot redefine property: %") \
197 T(RedefineExternalArray, \
198 "Cannot redefine a property of an object with external array elements") \
199 T(ReduceNoInitial, "Reduce of empty array with no initial value") \
201 "Cannot supply flags when constructing one RegExp from another") \
202 T(ReinitializeIntl, "Trying to re-initialize % object.") \
203 T(ResolvedOptionsCalledOnNonObject, \
204 "resolvedOptions method called on a non-object or on a object that is " \
206 T(ResolverNotAFunction, "Promise resolver % is not a function") \
207 T(RestrictedFunctionProperties, \
208 "'caller' and 'arguments' are restricted function properties and cannot " \
209 "be accessed in this context.") \
210 T(StaticPrototype, "Classes may not have static property named prototype") \
211 T(StrictCannotAssign, "Cannot assign to read only '% in strict mode") \
212 T(StrictDeleteProperty, "Cannot delete property '%' of %") \
213 T(StrictPoisonPill, \
214 "'caller', 'callee', and 'arguments' properties may not be accessed on " \
215 "strict mode functions or the arguments objects for calls to them") \
216 T(StrictReadOnlyProperty, "Cannot assign to read only property '%' of %") \
218 "In strong mode, calling a function with too few arguments is deprecated") \
219 T(StrongDeleteProperty, \
220 "Deleting property '%' of strong object '%' is deprecated") \
221 T(StrongExtendNull, "In strong mode, classes extending null are deprecated") \
222 T(StrongImplicitConversion, \
223 "In strong mode, implicit conversions are deprecated") \
224 T(StrongRedefineDisallowed, \
225 "On strong object %, redefining writable, non-configurable property '%' " \
226 "to be non-writable is deprecated") \
228 "On strong object %, redefining the internal prototype is deprecated") \
229 T(SymbolKeyFor, "% is not a symbol") \
230 T(SymbolToNumber, "Cannot convert a Symbol value to a number") \
231 T(SymbolToString, "Cannot convert a Symbol value to a string") \
232 T(SimdToNumber, "Cannot convert a SIMD value to a number") \
233 T(UndefinedOrNullToObject, "Cannot convert undefined or null to object") \
234 T(ValueAndAccessor, \
235 "Invalid property. A property cannot both have accessors and be " \
236 "writable or have a value, %") \
237 T(VarRedeclaration, "Identifier '%' has already been declared") \
238 T(WithExpression, "% has no properties") \
239 T(WrongArgs, "%: Arguments list has wrong type") \
240 /* ReferenceError */ \
241 T(NonMethod, "'super' is referenced from non-method") \
242 T(NotDefined, "% is not defined") \
243 T(StrongSuperCallMissing, \
244 "In strong mode, invoking the super constructor in a subclass is " \
246 T(StrongUnboundGlobal, \
247 "In strong mode, using an undeclared global variable '%' is not allowed") \
248 T(UnsupportedSuper, "Unsupported reference to 'super'") \
250 T(DateRange, "Provided date is not in valid range.") \
251 T(ExpectedLocation, "Expected Area/Location for time zone, got %") \
252 T(InvalidArrayBufferLength, "Invalid array buffer length") \
253 T(InvalidArrayLength, "Invalid array length") \
254 T(InvalidCodePoint, "Invalid code point %") \
255 T(InvalidCountValue, "Invalid count value") \
256 T(InvalidCurrencyCode, "Invalid currency code: %") \
257 T(InvalidDataViewAccessorOffset, \
258 "Offset is outside the bounds of the DataView") \
259 T(InvalidDataViewLength, "Invalid data view length") \
260 T(InvalidDataViewOffset, "Start offset is outside the bounds of the buffer") \
261 T(InvalidHint, "Invalid hint: %") \
262 T(InvalidLanguageTag, "Invalid language tag: %") \
263 T(InvalidWeakMapKey, "Invalid value used as weak map key") \
264 T(InvalidWeakSetValue, "Invalid value used in weak set") \
265 T(InvalidStringLength, "Invalid string length") \
266 T(InvalidTimeValue, "Invalid time value") \
267 T(InvalidTypedArrayAlignment, "% of % should be a multiple of %") \
268 T(InvalidTypedArrayLength, "Invalid typed array length") \
269 T(InvalidTypedArrayOffset, "Start offset is too large:") \
270 T(LocaleMatcher, "Illegal value for localeMatcher:%") \
271 T(NormalizationForm, "The normalization form should be one of %.") \
272 T(NumberFormatRange, "% argument must be between 0 and 20") \
273 T(PropertyValueOutOfRange, "% value is out of range.") \
274 T(StackOverflow, "Maximum call stack size exceeded") \
275 T(ToPrecisionFormatRange, "toPrecision() argument must be between 1 and 21") \
276 T(ToRadixFormatRange, "toString() radix argument must be between 2 and 36") \
277 T(TypedArraySetNegativeOffset, "Start offset is negative") \
278 T(TypedArraySetSourceTooLarge, "Source is too large") \
279 T(UnsupportedTimeZone, "Unsupported time zone specified %") \
280 T(ValueOutOfRange, "Value % out of range for % options property %") \
282 T(BadGetterArity, "Getter must not have any formal parameters.") \
283 T(BadSetterArity, "Setter must have exactly one formal parameter.") \
284 T(ConstructorIsAccessor, "Class constructor may not be an accessor") \
285 T(ConstructorIsGenerator, "Class constructor may not be a generator") \
286 T(DerivedConstructorReturn, \
287 "Derived constructors may only return object or undefined") \
288 T(DuplicateConstructor, "A class may only have one constructor") \
289 T(DuplicateExport, "Duplicate export of '%'") \
291 "Duplicate __proto__ fields are not allowed in object literals") \
292 T(ForInLoopInitializer, \
293 "for-in loop variable declaration may not have an initializer.") \
294 T(ForInOfLoopMultiBindings, \
295 "Invalid left-hand side in % loop: Must have a single binding.") \
296 T(ForOfLoopInitializer, \
297 "for-of loop variable declaration may not have an initializer.") \
298 T(IllegalAccess, "Illegal access") \
299 T(IllegalBreak, "Illegal break statement") \
300 T(IllegalContinue, "Illegal continue statement") \
301 T(IllegalLanguageModeDirective, \
302 "Illegal '%' directive in function with non-simple parameter list") \
303 T(IllegalReturn, "Illegal return statement") \
304 T(InvalidLhsInAssignment, "Invalid left-hand side in assignment") \
305 T(InvalidLhsInFor, "Invalid left-hand side in for-loop") \
306 T(InvalidLhsInPostfixOp, \
307 "Invalid left-hand side expression in postfix operation") \
308 T(InvalidLhsInPrefixOp, \
309 "Invalid left-hand side expression in prefix operation") \
310 T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \
311 T(LabelRedeclaration, "Label '%' has already been declared") \
312 T(MalformedArrowFunParamList, "Malformed arrow function parameter list") \
313 T(MalformedRegExp, "Invalid regular expression: /%/: %") \
314 T(MalformedRegExpFlags, "Invalid regular expression flags") \
315 T(ModuleExportUndefined, "Export '%' is not defined in module") \
316 T(MultipleDefaultsInSwitch, \
317 "More than one default clause in switch statement") \
318 T(NewlineAfterThrow, "Illegal newline after throw") \
319 T(NoCatchOrFinally, "Missing catch or finally after try") \
320 T(NotIsvar, "builtin %%IS_VAR: not a variable") \
321 T(ParamAfterRest, "Rest parameter must be last formal parameter") \
322 T(BadSetterRestParameter, \
323 "Setter function argument must not be a rest parameter") \
324 T(ParamDupe, "Duplicate parameter name not allowed in this context") \
325 T(ParenthesisInArgString, "Function arg string contains parenthesis") \
326 T(SingleFunctionLiteral, "Single function literal required") \
328 "Block-scoped declarations (let, const, function, class) not yet " \
329 "supported outside strict mode") \
330 T(StrictDelete, "Delete of an unqualified identifier in strict mode.") \
331 T(StrictEvalArguments, "Unexpected eval or arguments in strict mode") \
333 "In strict mode code, functions can only be declared at top level or " \
334 "immediately within another function.") \
335 T(StrictOctalLiteral, "Octal literals are not allowed in strict mode.") \
336 T(StrictWith, "Strict mode code may not include a with statement") \
338 "In strong mode, 'arguments' is deprecated, use '...args' instead") \
339 T(StrongConstructorDirective, \
340 "\"use strong\" directive is disallowed in class constructor body") \
341 T(StrongConstructorReturnMisplaced, \
342 "In strong mode, returning from a constructor before its super " \
343 "constructor invocation or all assignments to 'this' is deprecated") \
344 T(StrongConstructorReturnValue, \
345 "In strong mode, returning a value from a constructor is deprecated") \
346 T(StrongConstructorSuper, \
347 "In strong mode, 'super' can only be used to invoke the super " \
348 "constructor, and cannot be nested inside another statement or " \
350 T(StrongConstructorThis, \
351 "In strong mode, 'this' can only be used to initialize properties, and " \
352 "cannot be nested inside another statement or expression") \
354 "In strong mode, 'delete' is deprecated, use maps or sets instead") \
355 T(StrongDirectEval, "In strong mode, direct calls to eval are deprecated") \
357 "In strong mode, arrays with holes are deprecated, use maps instead") \
359 "In strong mode, empty sub-statements are deprecated, make them explicit " \
360 "with '{}' instead") \
362 "In strong mode, '==' and '!=' are deprecated, use '===' and '!==' " \
365 "In strong mode, 'for'-'in' loops are deprecated, use 'for'-'of' instead") \
366 T(StrongPropertyAccess, \
367 "In strong mode, accessing missing property '%' of % is deprecated") \
368 T(StrongSuperCallDuplicate, \
369 "In strong mode, invoking the super constructor multiple times is " \
371 T(StrongSuperCallMisplaced, \
372 "In strong mode, the super constructor must be invoked before any " \
373 "assignment to 'this'") \
374 T(StrongSwitchFallthrough, \
375 "In strong mode, switch fall-through is deprecated, terminate each case " \
376 "with 'break', 'continue', 'return' or 'throw'") \
378 "In strong mode, binding or assigning to 'undefined' is deprecated") \
379 T(StrongUseBeforeDeclaration, \
380 "In strong mode, declaring variable '%' before its use is required") \
382 "In strong mode, 'var' is deprecated, use 'let' or 'const' instead") \
383 T(TemplateOctalLiteral, \
384 "Octal literals are not allowed in template strings.") \
385 T(ThisFormalParameter, "'this' is not a valid formal parameter name") \
386 T(TooManyArguments, \
387 "Too many arguments in function call (only 65535 allowed)") \
388 T(TooManyParameters, \
389 "Too many parameters in function definition (only 65535 allowed)") \
390 T(TooManyVariables, "Too many variables declared (only 4194303 allowed)") \
391 T(UnexpectedEOS, "Unexpected end of input") \
392 T(UnexpectedReserved, "Unexpected reserved word") \
393 T(UnexpectedStrictReserved, "Unexpected strict mode reserved word") \
394 T(UnexpectedSuper, "'super' keyword unexpected here") \
395 T(UnexpectedNewTarget, "new.target expression is not allowed here") \
396 T(UnexpectedTemplateString, "Unexpected template string") \
397 T(UnexpectedToken, "Unexpected token %") \
398 T(UnexpectedTokenIdentifier, "Unexpected identifier") \
399 T(UnexpectedTokenNumber, "Unexpected number") \
400 T(UnexpectedTokenString, "Unexpected string") \
401 T(UnexpectedTokenRegExp, "Unexpected regular expression") \
402 T(UnknownLabel, "Undefined label '%'") \
403 T(UnterminatedArgList, "missing ) after argument list") \
404 T(UnterminatedRegExp, "Invalid regular expression: missing /") \
405 T(UnterminatedTemplate, "Unterminated template literal") \
406 T(UnterminatedTemplateExpr, "Missing } in template expression") \
408 T(CodeGenFromStrings, "%") \
410 T(URIMalformed, "URI malformed")
412 class MessageTemplate {
415 #define TEMPLATE(NAME, STRING) k##NAME,
416 MESSAGE_TEMPLATES(TEMPLATE)
421 static const char* TemplateString(int template_index);
423 static MaybeHandle<String> FormatMessage(int template_index,
426 Handle<String> arg2);
428 static Handle<String> FormatMessage(Isolate* isolate, int template_index,
433 // A message handler is a convenience interface for accessing the list
434 // of message listeners registered in an environment
435 class MessageHandler {
437 // Returns a message object for the API to use.
438 static Handle<JSMessageObject> MakeMessageObject(
439 Isolate* isolate, MessageTemplate::Template type,
440 MessageLocation* location, Handle<Object> argument,
441 Handle<JSArray> stack_frames);
443 // Report a formatted message (needs JS allocation).
444 static void ReportMessage(Isolate* isolate, MessageLocation* loc,
445 Handle<JSMessageObject> message);
447 static void DefaultMessageReport(Isolate* isolate, const MessageLocation* loc,
448 Handle<Object> message_obj);
449 static Handle<String> GetMessage(Isolate* isolate, Handle<Object> data);
450 static base::SmartArrayPointer<char> GetLocalizedMessage(Isolate* isolate,
451 Handle<Object> data);
455 class ErrorToStringHelper {
457 ErrorToStringHelper() : visited_(0) {}
459 MUST_USE_RESULT MaybeHandle<String> Stringify(Isolate* isolate,
460 Handle<JSObject> error);
465 VisitedScope(ErrorToStringHelper* helper, Handle<JSObject> error)
466 : helper_(helper), has_visited_(false) {
467 for (const Handle<JSObject>& visited : helper->visited_) {
468 if (visited.is_identical_to(error)) {
473 helper->visited_.Add(error);
475 ~VisitedScope() { helper_->visited_.RemoveLast(); }
476 bool has_visited() { return has_visited_; }
479 ErrorToStringHelper* helper_;
483 static bool ShadowsInternalError(Isolate* isolate,
484 LookupIterator* property_lookup,
485 LookupIterator* internal_error_lookup);
487 static MUST_USE_RESULT MaybeHandle<String> GetStringifiedProperty(
488 Isolate* isolate, LookupIterator* property_lookup,
489 Handle<String> default_value);
491 List<Handle<JSObject> > visited_;
493 } } // namespace v8::internal
495 #endif // V8_MESSAGES_H_