1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
|
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013 Apple Inc. All rights reserved.
* Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
* Copyright (C) 2013 Michael Pruett <michael@68k.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "JSDOMBinding.h"
#include "CachedScript.h"
#include "DOMConstructorWithDocument.h"
#include "DOMStringList.h"
#include "ExceptionCode.h"
#include "ExceptionCodeDescription.h"
#include "ExceptionHeaders.h"
#include "ExceptionInterfaces.h"
#include "Frame.h"
#include "HTMLParserIdioms.h"
#include "IDBDatabaseException.h"
#include "JSDOMWindowCustom.h"
#include "JSExceptionBase.h"
#include "SecurityOrigin.h"
#include <bytecode/CodeBlock.h>
#include <inspector/ScriptCallStack.h>
#include <inspector/ScriptCallStackFactory.h>
#include <interpreter/Interpreter.h>
#include <runtime/DateInstance.h>
#include <runtime/Error.h>
#include <runtime/ErrorHandlingScope.h>
#include <runtime/Exception.h>
#include <runtime/ExceptionHelpers.h>
#include <runtime/JSFunction.h>
#include <stdarg.h>
#include <wtf/MathExtras.h>
using namespace JSC;
using namespace Inspector;
namespace WebCore {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DOMConstructorObject);
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DOMConstructorWithDocument);
void addImpureProperty(const AtomicString& propertyName)
{
JSDOMWindow::commonVM().addImpureProperty(propertyName);
}
JSValue jsStringOrNull(ExecState* exec, const String& s)
{
if (s.isNull())
return jsNull();
return jsStringWithCache(exec, s);
}
JSValue jsOwnedStringOrNull(ExecState* exec, const String& s)
{
if (s.isNull())
return jsNull();
return jsOwnedString(exec, s);
}
JSValue jsStringOrUndefined(ExecState* exec, const String& s)
{
if (s.isNull())
return jsUndefined();
return jsStringWithCache(exec, s);
}
JSValue jsString(ExecState* exec, const URL& url)
{
return jsStringWithCache(exec, url.string());
}
JSValue jsStringOrNull(ExecState* exec, const URL& url)
{
if (url.isNull())
return jsNull();
return jsStringWithCache(exec, url.string());
}
JSValue jsStringOrUndefined(ExecState* exec, const URL& url)
{
if (url.isNull())
return jsUndefined();
return jsStringWithCache(exec, url.string());
}
String valueToStringWithNullCheck(ExecState* exec, JSValue value)
{
if (value.isNull())
return String();
return value.toString(exec)->value(exec);
}
String valueToStringWithUndefinedOrNullCheck(ExecState* exec, JSValue value)
{
if (value.isUndefinedOrNull())
return String();
return value.toString(exec)->value(exec);
}
JSValue jsDateOrNaN(ExecState* exec, double value)
{
if (std::isnan(value))
return jsDoubleNumber(value);
return jsDateOrNull(exec, value);
}
JSValue jsDateOrNull(ExecState* exec, double value)
{
if (!std::isfinite(value))
return jsNull();
return DateInstance::create(exec->vm(), exec->lexicalGlobalObject()->dateStructure(), value);
}
double valueToDate(ExecState* exec, JSValue value)
{
if (value.isNumber())
return value.asNumber();
if (!value.inherits(DateInstance::info()))
return std::numeric_limits<double>::quiet_NaN();
return static_cast<DateInstance*>(value.toObject(exec))->internalNumber();
}
JSC::JSValue jsArray(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, PassRefPtr<DOMStringList> stringList)
{
JSC::MarkedArgumentBuffer list;
if (stringList) {
for (unsigned i = 0; i < stringList->length(); ++i)
list.append(jsStringWithCache(exec, stringList->item(i)));
}
return JSC::constructArray(exec, 0, globalObject, list);
}
void reportException(ExecState* exec, JSValue exceptionValue, CachedScript* cachedScript)
{
RELEASE_ASSERT(exec->vm().currentThreadIsHoldingAPILock());
Exception* exception = jsDynamicCast<Exception*>(exceptionValue);
if (!exception) {
exception = exec->lastException();
if (!exception)
exception = Exception::create(exec->vm(), exceptionValue, Exception::DoNotCaptureStack);
}
reportException(exec, exception, cachedScript);
}
void reportException(ExecState* exec, Exception* exception, CachedScript* cachedScript, ExceptionDetails* exceptionDetails)
{
RELEASE_ASSERT(exec->vm().currentThreadIsHoldingAPILock());
if (isTerminatedExecutionException(exception))
return;
ErrorHandlingScope errorScope(exec->vm());
RefPtr<ScriptCallStack> callStack(createScriptCallStackFromException(exec, exception, ScriptCallStack::maxCallStackSizeToCapture));
exec->clearException();
exec->clearLastException();
JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
if (JSDOMWindow* window = jsDynamicCast<JSDOMWindow*>(globalObject)) {
if (!window->wrapped().isCurrentlyDisplayedInFrame())
return;
}
int lineNumber = 0;
int columnNumber = 0;
String exceptionSourceURL;
if (const ScriptCallFrame* callFrame = callStack->firstNonNativeCallFrame()) {
lineNumber = callFrame->lineNumber();
columnNumber = callFrame->columnNumber();
exceptionSourceURL = callFrame->sourceURL();
}
String errorMessage;
if (ExceptionBase* exceptionBase = toExceptionBase(exception->value()))
errorMessage = exceptionBase->message() + ": " + exceptionBase->description();
else {
// FIXME: <http://webkit.org/b/115087> Web Inspector: WebCore::reportException should not evaluate JavaScript handling exceptions
// If this is a custon exception object, call toString on it to try and get a nice string representation for the exception.
errorMessage = exception->value().toString(exec)->value(exec);
// We need to clear any new exception that may be thrown in the toString() call above.
// reportException() is not supposed to be making new exceptions.
exec->clearException();
exec->clearLastException();
}
ScriptExecutionContext* scriptExecutionContext = globalObject->scriptExecutionContext();
scriptExecutionContext->reportException(errorMessage, lineNumber, columnNumber, exceptionSourceURL, callStack->size() ? callStack : 0, cachedScript);
if (exceptionDetails) {
exceptionDetails->message = errorMessage;
exceptionDetails->lineNumber = lineNumber;
exceptionDetails->columnNumber = columnNumber;
exceptionDetails->sourceURL = exceptionSourceURL;
}
}
void reportCurrentException(ExecState* exec)
{
Exception* exception = exec->exception();
exec->clearException();
reportException(exec, exception);
}
#define TRY_TO_CREATE_EXCEPTION(interfaceName) \
case interfaceName##Type: \
errorObject = toJS(exec, globalObject, interfaceName::create(description)); \
break;
static JSValue createDOMException(ExecState* exec, ExceptionCode ec, const String* message)
{
if (!ec)
return jsUndefined();
// FIXME: Handle other WebIDL exception types.
if (ec == TypeError) {
if (!message || message->isEmpty())
return createTypeError(exec);
return createTypeError(exec, *message);
}
if (ec == RangeError) {
if (!message || message->isEmpty())
return createRangeError(exec, ASCIILiteral("Bad value"));
return createRangeError(exec, *message);
}
// FIXME: All callers to setDOMException need to pass in the right global object
// for now, we're going to assume the lexicalGlobalObject. Which is wrong in cases like this:
// frames[0].document.createElement(null, null); // throws an exception which should have the subframes prototypes.
JSDOMGlobalObject* globalObject = deprecatedGlobalObjectForPrototype(exec);
ExceptionCodeDescription description(ec);
CString messageCString;
if (message)
messageCString = message->utf8();
if (message && !message->isEmpty()) {
// It is safe to do this because the char* contents of the CString are copied into a new WTF::String before the CString is destroyed.
description.description = messageCString.data();
}
JSValue errorObject;
switch (description.type) {
DOM_EXCEPTION_INTERFACES_FOR_EACH(TRY_TO_CREATE_EXCEPTION)
#if ENABLE(INDEXED_DATABASE)
case IDBDatabaseExceptionType:
errorObject = toJS(exec, globalObject, DOMCoreException::createWithDescriptionAsMessage(description));
#endif
}
ASSERT(errorObject);
addErrorInfo(exec, asObject(errorObject), true);
return errorObject;
}
static JSValue createDOMException(ExecState* exec, ExceptionCode ec, const String& message)
{
return createDOMException(exec, ec, &message);
}
JSValue createDOMException(ExecState* exec, ExceptionCode ec)
{
return createDOMException(exec, ec, nullptr);
}
void setDOMException(ExecState* exec, ExceptionCode ec)
{
if (!ec || exec->hadException())
return;
exec->vm().throwException(exec, createDOMException(exec, ec));
}
void setDOMException(JSC::ExecState* exec, const ExceptionCodeWithMessage& ec)
{
if (!ec.code || exec->hadException())
return;
exec->vm().throwException(exec, createDOMException(exec, ec.code, ec.message));
}
#undef TRY_TO_CREATE_EXCEPTION
bool shouldAllowAccessToNode(ExecState* exec, Node* node)
{
return BindingSecurity::shouldAllowAccessToNode(exec, node);
}
bool shouldAllowAccessToFrame(ExecState* exec, Frame* target)
{
return BindingSecurity::shouldAllowAccessToFrame(exec, target);
}
bool shouldAllowAccessToFrame(ExecState* exec, Frame* frame, String& message)
{
if (!frame)
return false;
if (BindingSecurity::shouldAllowAccessToFrame(exec, frame, DoNotReportSecurityError))
return true;
message = frame->document()->domWindow()->crossDomainAccessErrorMessage(activeDOMWindow(exec));
return false;
}
bool shouldAllowAccessToDOMWindow(ExecState* exec, DOMWindow& target, String& message)
{
if (BindingSecurity::shouldAllowAccessToDOMWindow(exec, target, DoNotReportSecurityError))
return true;
message = target.crossDomainAccessErrorMessage(activeDOMWindow(exec));
return false;
}
void printErrorMessageForFrame(Frame* frame, const String& message)
{
if (!frame)
return;
frame->document()->domWindow()->printErrorMessage(message);
}
Structure* getCachedDOMStructure(JSDOMGlobalObject& globalObject, const ClassInfo* classInfo)
{
JSDOMStructureMap& structures = globalObject.structures();
return structures.get(classInfo).get();
}
Structure* cacheDOMStructure(JSDOMGlobalObject& globalObject, Structure* structure, const ClassInfo* classInfo)
{
JSDOMStructureMap& structures = globalObject.structures();
ASSERT(!structures.contains(classInfo));
return structures.set(classInfo, WriteBarrier<Structure>(globalObject.vm(), &globalObject, structure)).iterator->value.get();
}
static const int32_t kMaxInt32 = 0x7fffffff;
static const int32_t kMinInt32 = -kMaxInt32 - 1;
static const uint32_t kMaxUInt32 = 0xffffffffU;
static const int64_t kJSMaxInteger = 0x20000000000000LL - 1; // 2^53 - 1, largest integer exactly representable in ECMAScript.
static String rangeErrorString(double value, double min, double max)
{
return makeString("Value ", String::numberToStringECMAScript(value), " is outside the range [", String::numberToStringECMAScript(min), ", ", String::numberToStringECMAScript(max), "]");
}
static double enforceRange(ExecState* exec, double x, double minimum, double maximum)
{
if (std::isnan(x) || std::isinf(x)) {
exec->vm().throwException(exec, createTypeError(exec, rangeErrorString(x, minimum, maximum)));
return 0;
}
x = trunc(x);
if (x < minimum || x > maximum) {
exec->vm().throwException(exec, createTypeError(exec, rangeErrorString(x, minimum, maximum)));
return 0;
}
return x;
}
template <typename T>
struct IntTypeLimits {
};
template <>
struct IntTypeLimits<int8_t> {
static const int8_t minValue = -128;
static const int8_t maxValue = 127;
static const unsigned numberOfValues = 256; // 2^8
};
template <>
struct IntTypeLimits<uint8_t> {
static const uint8_t maxValue = 255;
static const unsigned numberOfValues = 256; // 2^8
};
template <>
struct IntTypeLimits<int16_t> {
static const short minValue = -32768;
static const short maxValue = 32767;
static const unsigned numberOfValues = 65536; // 2^16
};
template <>
struct IntTypeLimits<uint16_t> {
static const unsigned short maxValue = 65535;
static const unsigned numberOfValues = 65536; // 2^16
};
template <typename T>
static inline T toSmallerInt(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
typedef IntTypeLimits<T> LimitsTrait;
// Fast path if the value is already a 32-bit signed integer in the right range.
if (value.isInt32()) {
int32_t d = value.asInt32();
if (d >= LimitsTrait::minValue && d <= LimitsTrait::maxValue)
return static_cast<T>(d);
if (configuration == EnforceRange) {
throwTypeError(exec);
return 0;
}
d %= LimitsTrait::numberOfValues;
return static_cast<T>(d > LimitsTrait::maxValue ? d - LimitsTrait::numberOfValues : d);
}
double x = value.toNumber(exec);
if (exec->hadException())
return 0;
if (configuration == EnforceRange)
return enforceRange(exec, x, LimitsTrait::minValue, LimitsTrait::maxValue);
if (std::isnan(x) || std::isinf(x) || !x)
return 0;
x = x < 0 ? -floor(fabs(x)) : floor(fabs(x));
x = fmod(x, LimitsTrait::numberOfValues);
return static_cast<T>(x > LimitsTrait::maxValue ? x - LimitsTrait::numberOfValues : x);
}
template <typename T>
static inline T toSmallerUInt(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
typedef IntTypeLimits<T> LimitsTrait;
// Fast path if the value is already a 32-bit unsigned integer in the right range.
if (value.isUInt32()) {
uint32_t d = value.asUInt32();
if (d <= LimitsTrait::maxValue)
return static_cast<T>(d);
if (configuration == EnforceRange) {
throwTypeError(exec);
return 0;
}
return static_cast<T>(d);
}
double x = value.toNumber(exec);
if (exec->hadException())
return 0;
if (configuration == EnforceRange)
return enforceRange(exec, x, 0, LimitsTrait::maxValue);
if (std::isnan(x) || std::isinf(x) || !x)
return 0;
x = x < 0 ? -floor(fabs(x)) : floor(fabs(x));
return static_cast<T>(fmod(x, LimitsTrait::numberOfValues));
}
// http://www.w3.org/TR/WebIDL/#es-byte
int8_t toInt8(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
return toSmallerInt<int8_t>(exec, value, configuration);
}
// http://www.w3.org/TR/WebIDL/#es-octet
uint8_t toUInt8(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
return toSmallerUInt<uint8_t>(exec, value, configuration);
}
// http://www.w3.org/TR/WebIDL/#es-short
int16_t toInt16(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
return toSmallerInt<int16_t>(exec, value, configuration);
}
// http://www.w3.org/TR/WebIDL/#es-unsigned-short
uint16_t toUInt16(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
return toSmallerUInt<uint16_t>(exec, value, configuration);
}
// http://www.w3.org/TR/WebIDL/#es-long
int32_t toInt32EnforceRange(ExecState* exec, JSValue value)
{
if (value.isInt32())
return value.asInt32();
double x = value.toNumber(exec);
if (exec->hadException())
return 0;
return enforceRange(exec, x, kMinInt32, kMaxInt32);
}
// http://www.w3.org/TR/WebIDL/#es-unsigned-long
uint32_t toUInt32EnforceRange(ExecState* exec, JSValue value)
{
if (value.isUInt32())
return value.asUInt32();
double x = value.toNumber(exec);
if (exec->hadException())
return 0;
return enforceRange(exec, x, 0, kMaxUInt32);
}
// http://www.w3.org/TR/WebIDL/#es-long-long
int64_t toInt64(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
if (value.isInt32())
return value.asInt32();
double x = value.toNumber(exec);
if (exec->hadException())
return 0;
if (configuration == EnforceRange)
return enforceRange(exec, x, -kJSMaxInteger, kJSMaxInteger);
// Map NaNs and +/-Infinity to 0; convert finite values modulo 2^64.
unsigned long long n;
doubleToInteger(x, n);
return n;
}
// http://www.w3.org/TR/WebIDL/#es-unsigned-long-long
uint64_t toUInt64(ExecState* exec, JSValue value, IntegerConversionConfiguration configuration)
{
if (value.isUInt32())
return value.asUInt32();
double x = value.toNumber(exec);
if (exec->hadException())
return 0;
if (configuration == EnforceRange)
return enforceRange(exec, x, 0, kJSMaxInteger);
// Map NaNs and +/-Infinity to 0; convert finite values modulo 2^64.
unsigned long long n;
doubleToInteger(x, n);
return n;
}
class GetCallerGlobalObjectFunctor {
public:
GetCallerGlobalObjectFunctor() = default;
StackVisitor::Status operator()(StackVisitor& visitor) const
{
if (!m_hasSkippedFirstFrame) {
m_hasSkippedFirstFrame = true;
return StackVisitor::Continue;
}
if (auto* codeBlock = visitor->codeBlock())
m_globalObject = codeBlock->globalObject();
else {
ASSERT(visitor->callee());
m_globalObject = visitor->callee()->globalObject();
}
return StackVisitor::Done;
}
JSGlobalObject* globalObject() const { return m_globalObject; }
private:
mutable bool m_hasSkippedFirstFrame { false };
mutable JSGlobalObject* m_globalObject { nullptr };
};
DOMWindow& callerDOMWindow(ExecState* exec)
{
GetCallerGlobalObjectFunctor iter;
exec->iterate(iter);
return iter.globalObject() ? asJSDOMWindow(iter.globalObject())->wrapped() : firstDOMWindow(exec);
}
DOMWindow& activeDOMWindow(ExecState* exec)
{
return asJSDOMWindow(exec->lexicalGlobalObject())->wrapped();
}
DOMWindow& firstDOMWindow(ExecState* exec)
{
return asJSDOMWindow(exec->vmEntryGlobalObject())->wrapped();
}
static inline bool canAccessDocument(JSC::ExecState* state, Document* targetDocument, SecurityReportingOption reportingOption = ReportSecurityError)
{
if (!targetDocument)
return false;
DOMWindow& active = activeDOMWindow(state);
if (active.document()->securityOrigin()->canAccess(targetDocument->securityOrigin()))
return true;
if (reportingOption == ReportSecurityError)
printErrorMessageForFrame(targetDocument->frame(), targetDocument->domWindow()->crossDomainAccessErrorMessage(active));
return false;
}
bool BindingSecurity::shouldAllowAccessToDOMWindow(JSC::ExecState* state, DOMWindow& target, SecurityReportingOption reportingOption)
{
return canAccessDocument(state, target.document(), reportingOption);
}
bool BindingSecurity::shouldAllowAccessToFrame(JSC::ExecState* state, Frame* target, SecurityReportingOption reportingOption)
{
return target && canAccessDocument(state, target->document(), reportingOption);
}
bool BindingSecurity::shouldAllowAccessToNode(JSC::ExecState* state, Node* target)
{
return target && canAccessDocument(state, &target->document());
}
static EncodedJSValue throwTypeError(JSC::ExecState& state, const String& errorMessage)
{
return throwVMError(&state, createTypeError(&state, errorMessage));
}
static void appendArgumentMustBe(StringBuilder& builder, unsigned argumentIndex, const char* argumentName, const char* interfaceName, const char* functionName)
{
builder.appendLiteral("Argument ");
builder.appendNumber(argumentIndex + 1);
builder.appendLiteral(" ('");
builder.append(argumentName);
builder.appendLiteral("') to ");
if (!functionName) {
builder.appendLiteral("the ");
builder.append(interfaceName);
builder.appendLiteral(" constructor");
} else {
builder.append(interfaceName);
builder.append('.');
builder.append(functionName);
}
builder.appendLiteral(" must be ");
}
JSC::EncodedJSValue reportDeprecatedGetterError(JSC::ExecState& state, const char* interfaceName, const char* attributeName)
{
auto& context = *jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject())->scriptExecutionContext();
context.addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("Deprecated attempt to access property '", attributeName, "' on a non-", interfaceName, " object."));
return JSValue::encode(jsUndefined());
}
void reportDeprecatedSetterError(JSC::ExecState& state, const char* interfaceName, const char* attributeName)
{
auto& context = *jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject())->scriptExecutionContext();
context.addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("Deprecated attempt to set property '", attributeName, "' on a non-", interfaceName, " object."));
}
JSC::EncodedJSValue throwArgumentMustBeEnumError(JSC::ExecState& state, unsigned argumentIndex, const char* argumentName, const char* functionInterfaceName, const char* functionName, const char* expectedValues)
{
StringBuilder builder;
appendArgumentMustBe(builder, argumentIndex, argumentName, functionInterfaceName, functionName);
builder.appendLiteral("one of: ");
builder.append(expectedValues);
return throwTypeError(state, builder.toString());
}
JSC::EncodedJSValue throwArgumentMustBeFunctionError(JSC::ExecState& state, unsigned argumentIndex, const char* argumentName, const char* interfaceName, const char* functionName)
{
StringBuilder builder;
appendArgumentMustBe(builder, argumentIndex, argumentName, interfaceName, functionName);
builder.appendLiteral("a function");
return throwTypeError(state, builder.toString());
}
JSC::EncodedJSValue throwArgumentTypeError(JSC::ExecState& state, unsigned argumentIndex, const char* argumentName, const char* functionInterfaceName, const char* functionName, const char* expectedType)
{
StringBuilder builder;
appendArgumentMustBe(builder, argumentIndex, argumentName, functionInterfaceName, functionName);
builder.appendLiteral("an instance of ");
builder.append(expectedType);
return throwTypeError(state, builder.toString());
}
void throwArrayElementTypeError(JSC::ExecState& state)
{
throwTypeError(state, "Invalid Array element type");
}
void throwAttributeTypeError(JSC::ExecState& state, const char* interfaceName, const char* attributeName, const char* expectedType)
{
throwTypeError(state, makeString("The ", interfaceName, '.', attributeName, " attribute must be an instance of ", expectedType));
}
JSC::EncodedJSValue throwConstructorDocumentUnavailableError(JSC::ExecState& state, const char* interfaceName)
{
// FIXME: This is confusing exception wording. Can we reword to be clearer and more specific?
return throwVMError(&state, createReferenceError(&state, makeString(interfaceName, " constructor associated document is unavailable")));
}
JSC::EncodedJSValue throwGetterTypeError(JSC::ExecState& state, const char* interfaceName, const char* attributeName)
{
return throwTypeError(state, makeString("The ", interfaceName, '.', attributeName, " getter can only be used on instances of ", interfaceName));
}
void throwSequenceTypeError(JSC::ExecState& state)
{
throwTypeError(state, "Value is not a sequence");
}
void throwSetterTypeError(JSC::ExecState& state, const char* interfaceName, const char* attributeName)
{
throwTypeError(state, makeString("The ", interfaceName, '.', attributeName, " setter can only be used on instances of ", interfaceName));
}
EncodedJSValue throwThisTypeError(JSC::ExecState& state, const char* interfaceName, const char* functionName)
{
return throwTypeError(state, makeString("Can only call ", interfaceName, '.', functionName, " on instances of ", interfaceName));
}
void callFunctionWithCurrentArguments(JSC::ExecState& state, JSC::JSObject& thisObject, JSC::JSFunction& function)
{
JSC::CallData callData;
JSC::CallType callType = JSC::getCallData(&function, callData);
ASSERT(callType != CallTypeNone);
JSC::MarkedArgumentBuffer arguments;
for (unsigned i = 0; i < state.argumentCount(); ++i)
arguments.append(state.uncheckedArgument(i));
JSC::call(&state, &function, callType, callData, &thisObject, arguments);
}
void DOMConstructorJSBuiltinObject::visitChildren(JSC::JSCell* cell, JSC::SlotVisitor& visitor)
{
auto* thisObject = jsCast<DOMConstructorJSBuiltinObject*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(&thisObject->m_initializeFunction);
}
static EncodedJSValue JSC_HOST_CALL callThrowTypeError(ExecState* exec)
{
throwTypeError(exec, ASCIILiteral("Constructor requires 'new' operator"));
return JSValue::encode(jsNull());
}
CallType DOMConstructorObject::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callThrowTypeError;
return CallTypeHost;
}
} // namespace WebCore
|