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"
33 #include "NameInstance.h"
37 // The purpose of this namespace is to include slow paths that are shared
38 // between the interpreter and baseline JIT. They are written to be agnostic
39 // with respect to the slow-path calling convention, but they do rely on the
40 // JS code being executed more-or-less directly from bytecode (so the call
41 // frame layout is unmodified, making it potentially awkward to use these
42 // from any optimizing JIT, like the DFG).
44 namespace CommonSlowPaths {
46 ALWAYS_INLINE ExecState* arityCheckFor(ExecState* exec, RegisterFile* registerFile, CodeSpecializationKind kind)
48 JSFunction* callee = jsCast<JSFunction*>(exec->callee());
49 ASSERT(!callee->isHostFunction());
50 CodeBlock* newCodeBlock = &callee->jsExecutable()->generatedBytecodeFor(kind);
51 int argumentCountIncludingThis = exec->argumentCountIncludingThis();
53 // This ensures enough space for the worst case scenario of zero arguments passed by the caller.
54 if (!registerFile->grow(exec->registers() + newCodeBlock->numParameters() + newCodeBlock->m_numCalleeRegisters))
57 ASSERT(argumentCountIncludingThis < newCodeBlock->numParameters());
59 // Too few arguments -- copy call frame and arguments, then fill in missing arguments with undefined.
60 size_t delta = newCodeBlock->numParameters() - argumentCountIncludingThis;
61 Register* src = exec->registers();
62 Register* dst = exec->registers() + delta;
65 int end = -ExecState::offsetFor(argumentCountIncludingThis);
66 for (i = -1; i >= end; --i)
70 for ( ; i >= end; --i)
71 dst[i] = jsUndefined();
73 ExecState* newExec = ExecState::create(dst);
74 ASSERT((void*)newExec <= registerFile->end());
78 ALWAYS_INLINE bool opInstanceOfSlow(ExecState* exec, JSValue value, JSValue baseVal, JSValue proto)
80 ASSERT(!value.isCell() || !baseVal.isCell() || !proto.isCell()
81 || !value.isObject() || !baseVal.isObject() || !proto.isObject()
82 || !asObject(baseVal)->structure()->typeInfo().implementsDefaultHasInstance());
86 // Throw an exception either if baseVal is not an object, or if it does not implement 'HasInstance' (i.e. is a function).
87 TypeInfo typeInfo(UnspecifiedType);
88 if (!baseVal.isObject() || !(typeInfo = asObject(baseVal)->structure()->typeInfo()).implementsHasInstance()) {
89 exec->globalData().exception = createInvalidParamError(exec, "instanceof", baseVal);
92 ASSERT(typeInfo.type() != UnspecifiedType);
94 if (!typeInfo.overridesHasInstance() && !value.isObject())
97 return asObject(baseVal)->methodTable()->hasInstance(asObject(baseVal), exec, value, proto);
100 inline bool opIn(ExecState* exec, JSValue propName, JSValue baseVal)
102 if (!baseVal.isObject()) {
103 exec->globalData().exception = createInvalidParamError(exec, "in", baseVal);
107 JSObject* baseObj = asObject(baseVal);
110 if (propName.getUInt32(i))
111 return baseObj->hasProperty(exec, i);
113 if (isName(propName))
114 return baseObj->hasProperty(exec, jsCast<NameInstance*>(propName.asCell())->privateName());
116 Identifier property(exec, propName.toString(exec)->value(exec));
117 if (exec->globalData().exception)
119 return baseObj->hasProperty(exec, property);
122 ALWAYS_INLINE JSValue opResolve(ExecState* exec, Identifier& ident)
124 ScopeChainNode* scopeChain = exec->scopeChain();
126 ScopeChainIterator iter = scopeChain->begin();
127 ScopeChainIterator end = scopeChain->end();
131 JSObject* o = iter->get();
132 PropertySlot slot(o);
133 if (o->getPropertySlot(exec, ident, slot))
134 return slot.getValue(exec, ident);
135 } while (++iter != end);
137 exec->globalData().exception = createUndefinedVariableError(exec, ident);
141 ALWAYS_INLINE JSValue opResolveSkip(ExecState* exec, Identifier& ident, int skip)
143 ScopeChainNode* scopeChain = exec->scopeChain();
145 ScopeChainIterator iter = scopeChain->begin();
146 ScopeChainIterator end = scopeChain->end();
148 CodeBlock* codeBlock = exec->codeBlock();
149 bool checkTopLevel = codeBlock->codeType() == FunctionCode && codeBlock->needsFullScopeChain();
150 ASSERT(skip || !checkTopLevel);
151 if (checkTopLevel && skip--) {
152 if (exec->uncheckedR(codeBlock->activationRegister()).jsValue())
160 JSObject* o = iter->get();
161 PropertySlot slot(o);
162 if (o->getPropertySlot(exec, ident, slot))
163 return slot.getValue(exec, ident);
164 } while (++iter != end);
166 exec->globalData().exception = createUndefinedVariableError(exec, ident);
170 ALWAYS_INLINE JSValue opResolveWithBase(ExecState* exec, Identifier& ident, Register& baseSlot)
172 ScopeChainNode* scopeChain = exec->scopeChain();
174 ScopeChainIterator iter = scopeChain->begin();
175 ScopeChainIterator end = scopeChain->end();
177 // FIXME: add scopeDepthIsZero optimization
184 PropertySlot slot(base);
185 if (base->getPropertySlot(exec, ident, slot)) {
186 JSValue result = slot.getValue(exec, ident);
187 if (exec->globalData().exception)
190 baseSlot = JSValue(base);
194 } while (iter != end);
196 exec->globalData().exception = createUndefinedVariableError(exec, ident);
200 ALWAYS_INLINE JSValue opResolveWithThis(ExecState* exec, Identifier& ident, Register& baseSlot)
202 ScopeChainNode* scopeChain = exec->scopeChain();
204 ScopeChainIterator iter = scopeChain->begin();
205 ScopeChainIterator end = scopeChain->end();
207 // FIXME: add scopeDepthIsZero optimization
215 PropertySlot slot(base);
216 if (base->getPropertySlot(exec, ident, slot)) {
217 JSValue result = slot.getValue(exec, ident);
218 if (exec->globalData().exception)
221 // All entries on the scope chain should be EnvironmentRecords (activations etc),
222 // other then 'with' object, which are directly referenced from the scope chain,
223 // and the global object. If we hit either an EnvironmentRecord or a global
224 // object at the end of the scope chain, this is undefined. If we hit a non-
225 // EnvironmentRecord within the scope chain, pass the base as the this value.
226 if (iter == end || base->structure()->typeInfo().isEnvironmentRecord())
227 baseSlot = jsUndefined();
229 baseSlot = JSValue(base);
232 } while (iter != end);
234 exec->globalData().exception = createUndefinedVariableError(exec, ident);
238 } } // namespace JSC::CommonSlowPaths
240 #endif // CommonSlowPaths_h