source: webkit/trunk/JavaScriptCore/runtime/ExceptionHelpers.cpp@ 60075

Last change on this file since 60075 was 60075, checked in by [email protected], 15 years ago

https://bugs.webkit.org/show_bug.cgi?id=39583
Move creation of 'this' object from caller to callee in construction.

Reviewed by Sam Weinig.

Presently the caller of a constructor is responsible for providing a this
object. Instead, move the object creation into a new op_create_this opcode,
planted in the head of the contructor bytecode for a function. Since the
prototype for the object is provided by performing a get_by_id on the callee,
also add a new get_callee opcode (this is used to get the callee JSFunction
into a register so that a normal get_by_id can be used).

Currently the caller is also responsible for detecting when op_construct is
performed on a JSFunction representing a host function, in which case an
exception is thrown – and this check currently takes place when constructing
the this object. Instead, mirroring the recent changes for non-host functions,
add a parallel code-path for native constructors to follow, with a thunk for
invoking native constructors provided by JITStubs, and a constructor-specific
NativeFunction on NativeExecutable. Provide an implementation of a host
constructor which will throw an exception.

  • bytecode/CodeBlock.cpp:

(JSC::CodeBlock::dump):
(JSC::CodeBlock::getByIdExceptionInfoForBytecodeOffset):

  • bytecode/CodeBlock.h:
  • bytecode/Opcode.h:
  • bytecompiler/BytecodeGenerator.cpp:

(JSC::BytecodeGenerator::BytecodeGenerator):
(JSC::BytecodeGenerator::emitConstruct):

  • bytecompiler/BytecodeGenerator.h:

(JSC::BytecodeGenerator::emitGetByIdExceptionInfo):

  • interpreter/Interpreter.cpp:

(JSC::Interpreter::privateExecute):

  • jit/JIT.cpp:

(JSC::JIT::privateCompileMainPass):

  • jit/JIT.h:
  • jit/JITCall.cpp:

(JSC::JIT::compileOpCall):
(JSC::JIT::compileOpCallSlowCase):

  • jit/JITCall32_64.cpp:

(JSC::JIT::compileOpCall):
(JSC::JIT::compileOpCallSlowCase):

  • jit/JITOpcodes.cpp:

(JSC::JIT::privateCompileCTIMachineTrampolines):
(JSC::JIT::privateCompileCTINativeCall):
(JSC::JIT::emit_op_neq_null):
(JSC::JIT::emit_op_convert_this):
(JSC::JIT::emit_op_get_callee):
(JSC::JIT::emit_op_create_this):

  • jit/JITOpcodes32_64.cpp:

(JSC::JIT::privateCompileCTIMachineTrampolines):
(JSC::JIT::privateCompileCTINativeCall):
(JSC::JIT::emit_op_get_callee):
(JSC::JIT::emit_op_create_this):

  • jit/JITStubs.cpp:

(JSC::DEFINE_STUB_FUNCTION):
(JSC::JITThunks::hostFunctionStub):

  • jit/JITStubs.h:

(JSC::JITThunks::ctiNativeConstruct):
(JSC::):

  • runtime/ExceptionHelpers.cpp:

(JSC::createNotAnObjectError):

  • runtime/Executable.h:

(JSC::NativeExecutable::create):
(JSC::NativeExecutable::NativeExecutable):

  • runtime/JSFunction.cpp:

(JSC::callHostFunctionAsConstructor):

  • runtime/JSFunction.h:
File size: 11.1 KB
Line 
1/*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "ExceptionHelpers.h"
31
32#include "CodeBlock.h"
33#include "CallFrame.h"
34#include "JSGlobalObjectFunctions.h"
35#include "JSObject.h"
36#include "JSNotAnObject.h"
37#include "Interpreter.h"
38#include "Nodes.h"
39
40namespace JSC {
41
42class InterruptedExecutionError : public JSObject {
43public:
44 InterruptedExecutionError(JSGlobalData* globalData)
45 : JSObject(globalData->interruptedExecutionErrorStructure)
46 {
47 }
48
49 virtual ComplType exceptionType() const { return Interrupted; }
50
51 virtual UString toString(ExecState*) const { return "JavaScript execution exceeded timeout."; }
52};
53
54JSValue createInterruptedExecutionException(JSGlobalData* globalData)
55{
56 return new (globalData) InterruptedExecutionError(globalData);
57}
58
59class TerminatedExecutionError : public JSObject {
60public:
61 TerminatedExecutionError(JSGlobalData* globalData)
62 : JSObject(globalData->terminatedExecutionErrorStructure)
63 {
64 }
65
66 virtual ComplType exceptionType() const { return Terminated; }
67
68 virtual UString toString(ExecState*) const { return "JavaScript execution terminated."; }
69};
70
71JSValue createTerminatedExecutionException(JSGlobalData* globalData)
72{
73 return new (globalData) TerminatedExecutionError(globalData);
74}
75
76static JSValue createError(ExecState* exec, ErrorType e, const char* msg)
77{
78 return Error::create(exec, e, msg, -1, -1, UString());
79}
80
81JSValue createStackOverflowError(ExecState* exec)
82{
83 return createError(exec, RangeError, "Maximum call stack size exceeded.");
84}
85
86JSValue createTypeError(ExecState* exec, const char* message)
87{
88 return createError(exec, TypeError, message);
89}
90
91JSValue createUndefinedVariableError(ExecState* exec, const Identifier& ident, unsigned bytecodeOffset, CodeBlock* codeBlock)
92{
93 int startOffset = 0;
94 int endOffset = 0;
95 int divotPoint = 0;
96 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
97 JSObject* exception = Error::create(exec, ReferenceError, makeString("Can't find variable: ", ident.ustring()), line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
98 exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
99 exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
100 exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
101 return exception;
102}
103
104static UString createErrorMessage(ExecState* exec, CodeBlock* codeBlock, int, int expressionStart, int expressionStop, JSValue value, UString error)
105{
106 if (!expressionStop || expressionStart > codeBlock->source()->length())
107 return makeString(value.toString(exec), " is ", error);
108 if (expressionStart < expressionStop)
109 return makeString("Result of expression '", codeBlock->source()->getRange(expressionStart, expressionStop), "' [", value.toString(exec), "] is ", error, ".");
110
111 // No range information, so give a few characters of context
112 const UChar* data = codeBlock->source()->data();
113 int dataLength = codeBlock->source()->length();
114 int start = expressionStart;
115 int stop = expressionStart;
116 // Get up to 20 characters of context to the left and right of the divot, clamping to the line.
117 // then strip whitespace.
118 while (start > 0 && (expressionStart - start < 20) && data[start - 1] != '\n')
119 start--;
120 while (start < (expressionStart - 1) && isStrWhiteSpace(data[start]))
121 start++;
122 while (stop < dataLength && (stop - expressionStart < 20) && data[stop] != '\n')
123 stop++;
124 while (stop > expressionStart && isStrWhiteSpace(data[stop]))
125 stop--;
126 return makeString("Result of expression near '...", codeBlock->source()->getRange(start, stop), "...' [", value.toString(exec), "] is ", error, ".");
127}
128
129JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
130{
131 int startOffset = 0;
132 int endOffset = 0;
133 int divotPoint = 0;
134 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
135 UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint, divotPoint + endOffset, value, makeString("not a valid argument for '", op, "'"));
136 JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
137 exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
138 exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
139 exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
140 return exception;
141}
142
143JSObject* createNotAConstructorError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
144{
145 int startOffset = 0;
146 int endOffset = 0;
147 int divotPoint = 0;
148 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
149
150 // We're in a "new" expression, so we need to skip over the "new.." part
151 int startPoint = divotPoint - (startOffset ? startOffset - 4 : 0); // -4 for "new "
152 const UChar* data = codeBlock->source()->data();
153 while (startPoint < divotPoint && isStrWhiteSpace(data[startPoint]))
154 startPoint++;
155
156 UString errorMessage = createErrorMessage(exec, codeBlock, line, startPoint, divotPoint, value, "not a constructor");
157 JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
158 exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
159 exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
160 exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
161 return exception;
162}
163
164JSValue createNotAFunctionError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
165{
166 int startOffset = 0;
167 int endOffset = 0;
168 int divotPoint = 0;
169 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
170 UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, value, "not a function");
171 JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
172 exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
173 exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
174 exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
175 return exception;
176}
177
178JSNotAnObjectErrorStub* createNotAnObjectErrorStub(ExecState* exec, bool isNull)
179{
180 return new (exec) JSNotAnObjectErrorStub(exec, isNull);
181}
182
183JSObject* createNotAnObjectError(ExecState* exec, JSNotAnObjectErrorStub* error, unsigned bytecodeOffset, CodeBlock* codeBlock)
184{
185 // Both op_create_this and op_instanceof require a use of op_get_by_id to get
186 // the prototype property from an object. The exception messages for exceptions
187 // thrown by these instances op_get_by_id need to reflect this.
188 OpcodeID followingOpcodeID;
189 if (codeBlock->getByIdExceptionInfoForBytecodeOffset(exec, bytecodeOffset, followingOpcodeID)) {
190 ASSERT(followingOpcodeID == op_create_this || followingOpcodeID == op_instanceof);
191 if (followingOpcodeID == op_create_this)
192 return createNotAConstructorError(exec, error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock);
193 return createInvalidParamError(exec, "instanceof", error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock);
194 }
195
196 int startOffset = 0;
197 int endOffset = 0;
198 int divotPoint = 0;
199 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
200 UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, error->isNull() ? jsNull() : jsUndefined(), "not an object");
201 JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
202 exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
203 exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
204 exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
205 return exception;
206}
207
208JSValue throwOutOfMemoryError(ExecState* exec)
209{
210 return throwError(exec, GeneralError, "Out of memory");
211}
212
213} // namespace JSC
Note: See TracBrowser for help on using the repository browser.