2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef CommonSlowPaths_h
27 #define CommonSlowPaths_h
29 #include "CodeBlock.h"
30 #include "CodeSpecializationKind.h"
31 #include "ExceptionHelpers.h"
36 // The purpose of this namespace is to include slow paths that are shared
37 // between the interpreter and baseline JIT. They are written to be agnostic
38 // with respect to the slow-path calling convention, but they do rely on the
39 // JS code being executed more-or-less directly from bytecode (so the call
40 // frame layout is unmodified, making it potentially awkward to use these
41 // from any optimizing JIT, like the DFG).
43 namespace CommonSlowPaths
{
45 ALWAYS_INLINE ExecState
* arityCheckFor(ExecState
* exec
, RegisterFile
* registerFile
, CodeSpecializationKind kind
)
47 JSFunction
* callee
= jsCast
<JSFunction
*>(exec
->callee());
48 ASSERT(!callee
->isHostFunction());
49 CodeBlock
* newCodeBlock
= &callee
->jsExecutable()->generatedBytecodeFor(kind
);
50 int argumentCountIncludingThis
= exec
->argumentCountIncludingThis();
52 // This ensures enough space for the worst case scenario of zero arguments passed by the caller.
53 if (!registerFile
->grow(exec
->registers() + newCodeBlock
->numParameters() + newCodeBlock
->m_numCalleeRegisters
))
56 ASSERT(argumentCountIncludingThis
< newCodeBlock
->numParameters());
58 // Too few arguments -- copy call frame and arguments, then fill in missing arguments with undefined.
59 size_t delta
= newCodeBlock
->numParameters() - argumentCountIncludingThis
;
60 Register
* src
= exec
->registers();
61 Register
* dst
= exec
->registers() + delta
;
64 int end
= -ExecState::offsetFor(argumentCountIncludingThis
);
65 for (i
= -1; i
>= end
; --i
)
69 for ( ; i
>= end
; --i
)
70 dst
[i
] = jsUndefined();
72 ExecState
* newExec
= ExecState::create(dst
);
73 ASSERT((void*)newExec
<= registerFile
->end());
77 ALWAYS_INLINE
bool opInstanceOfSlow(ExecState
* exec
, JSValue value
, JSValue baseVal
, JSValue proto
)
79 ASSERT(!value
.isCell() || !baseVal
.isCell() || !proto
.isCell()
80 || !value
.isObject() || !baseVal
.isObject() || !proto
.isObject()
81 || !asObject(baseVal
)->structure()->typeInfo().implementsDefaultHasInstance());
85 // Throw an exception either if baseVal is not an object, or if it does not implement 'HasInstance' (i.e. is a function).
86 TypeInfo
typeInfo(UnspecifiedType
);
87 if (!baseVal
.isObject() || !(typeInfo
= asObject(baseVal
)->structure()->typeInfo()).implementsHasInstance()) {
88 exec
->globalData().exception
= createInvalidParamError(exec
, "instanceof", baseVal
);
91 ASSERT(typeInfo
.type() != UnspecifiedType
);
93 if (!typeInfo
.overridesHasInstance() && !value
.isObject())
96 return asObject(baseVal
)->methodTable()->hasInstance(asObject(baseVal
), exec
, value
, proto
);
99 inline bool opIn(ExecState
* exec
, JSValue propName
, JSValue baseVal
)
101 if (!baseVal
.isObject()) {
102 exec
->globalData().exception
= createInvalidParamError(exec
, "in", baseVal
);
106 JSObject
* baseObj
= asObject(baseVal
);
109 if (propName
.getUInt32(i
))
110 return baseObj
->hasProperty(exec
, i
);
112 Identifier
property(exec
, propName
.toString(exec
)->value(exec
));
113 if (exec
->globalData().exception
)
115 return baseObj
->hasProperty(exec
, property
);
118 ALWAYS_INLINE JSValue
opResolve(ExecState
* exec
, Identifier
& ident
)
120 ScopeChainNode
* scopeChain
= exec
->scopeChain();
122 ScopeChainIterator iter
= scopeChain
->begin();
123 ScopeChainIterator end
= scopeChain
->end();
127 JSObject
* o
= iter
->get();
128 PropertySlot
slot(o
);
129 if (o
->getPropertySlot(exec
, ident
, slot
))
130 return slot
.getValue(exec
, ident
);
131 } while (++iter
!= end
);
133 exec
->globalData().exception
= createUndefinedVariableError(exec
, ident
);
137 ALWAYS_INLINE JSValue
opResolveSkip(ExecState
* exec
, Identifier
& ident
, int skip
)
139 ScopeChainNode
* scopeChain
= exec
->scopeChain();
141 ScopeChainIterator iter
= scopeChain
->begin();
142 ScopeChainIterator end
= scopeChain
->end();
144 CodeBlock
* codeBlock
= exec
->codeBlock();
145 bool checkTopLevel
= codeBlock
->codeType() == FunctionCode
&& codeBlock
->needsFullScopeChain();
146 ASSERT(skip
|| !checkTopLevel
);
147 if (checkTopLevel
&& skip
--) {
148 if (exec
->uncheckedR(codeBlock
->activationRegister()).jsValue())
156 JSObject
* o
= iter
->get();
157 PropertySlot
slot(o
);
158 if (o
->getPropertySlot(exec
, ident
, slot
))
159 return slot
.getValue(exec
, ident
);
160 } while (++iter
!= end
);
162 exec
->globalData().exception
= createUndefinedVariableError(exec
, ident
);
166 ALWAYS_INLINE JSValue
opResolveWithBase(ExecState
* exec
, Identifier
& ident
, Register
& baseSlot
)
168 ScopeChainNode
* scopeChain
= exec
->scopeChain();
170 ScopeChainIterator iter
= scopeChain
->begin();
171 ScopeChainIterator end
= scopeChain
->end();
173 // FIXME: add scopeDepthIsZero optimization
180 PropertySlot
slot(base
);
181 if (base
->getPropertySlot(exec
, ident
, slot
)) {
182 JSValue result
= slot
.getValue(exec
, ident
);
183 if (exec
->globalData().exception
)
186 baseSlot
= JSValue(base
);
190 } while (iter
!= end
);
192 exec
->globalData().exception
= createUndefinedVariableError(exec
, ident
);
196 ALWAYS_INLINE JSValue
opResolveWithThis(ExecState
* exec
, Identifier
& ident
, Register
& baseSlot
)
198 ScopeChainNode
* scopeChain
= exec
->scopeChain();
200 ScopeChainIterator iter
= scopeChain
->begin();
201 ScopeChainIterator end
= scopeChain
->end();
203 // FIXME: add scopeDepthIsZero optimization
211 PropertySlot
slot(base
);
212 if (base
->getPropertySlot(exec
, ident
, slot
)) {
213 JSValue result
= slot
.getValue(exec
, ident
);
214 if (exec
->globalData().exception
)
217 // All entries on the scope chain should be EnvironmentRecords (activations etc),
218 // other then 'with' object, which are directly referenced from the scope chain,
219 // and the global object. If we hit either an EnvironmentRecord or a global
220 // object at the end of the scope chain, this is undefined. If we hit a non-
221 // EnvironmentRecord within the scope chain, pass the base as the this value.
222 if (iter
== end
|| base
->structure()->typeInfo().isEnvironmentRecord())
223 baseSlot
= jsUndefined();
225 baseSlot
= JSValue(base
);
228 } while (iter
!= end
);
230 exec
->globalData().exception
= createUndefinedVariableError(exec
, ident
);
234 } } // namespace JSC::CommonSlowPaths
236 #endif // CommonSlowPaths_h