]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/CommonSlowPaths.h
c41ced7ee6dfe7ab9cbb253c6e16f5d71b4a748a
[apple/javascriptcore.git] / runtime / CommonSlowPaths.h
1 /*
2 * Copyright (C) 2011, 2012 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 * 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.
12 *
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.
24 */
25
26 #ifndef CommonSlowPaths_h
27 #define CommonSlowPaths_h
28
29 #include "CodeBlock.h"
30 #include "CodeSpecializationKind.h"
31 #include "ExceptionHelpers.h"
32 #include "JSArray.h"
33
34 namespace JSC {
35
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).
42
43 namespace CommonSlowPaths {
44
45 ALWAYS_INLINE ExecState* arityCheckFor(ExecState* exec, RegisterFile* registerFile, CodeSpecializationKind kind)
46 {
47 JSFunction* callee = jsCast<JSFunction*>(exec->callee());
48 ASSERT(!callee->isHostFunction());
49 CodeBlock* newCodeBlock = &callee->jsExecutable()->generatedBytecodeFor(kind);
50 int argumentCountIncludingThis = exec->argumentCountIncludingThis();
51
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))
54 return 0;
55
56 ASSERT(argumentCountIncludingThis < newCodeBlock->numParameters());
57
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;
62
63 int i;
64 int end = -ExecState::offsetFor(argumentCountIncludingThis);
65 for (i = -1; i >= end; --i)
66 dst[i] = src[i];
67
68 end -= delta;
69 for ( ; i >= end; --i)
70 dst[i] = jsUndefined();
71
72 ExecState* newExec = ExecState::create(dst);
73 ASSERT((void*)newExec <= registerFile->end());
74 return newExec;
75 }
76
77 ALWAYS_INLINE bool opInstanceOfSlow(ExecState* exec, JSValue value, JSValue baseVal, JSValue proto)
78 {
79 ASSERT(!value.isCell() || !baseVal.isCell() || !proto.isCell()
80 || !value.isObject() || !baseVal.isObject() || !proto.isObject()
81 || !asObject(baseVal)->structure()->typeInfo().implementsDefaultHasInstance());
82
83
84 // ECMA-262 15.3.5.3:
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);
89 return false;
90 }
91 ASSERT(typeInfo.type() != UnspecifiedType);
92
93 if (!typeInfo.overridesHasInstance() && !value.isObject())
94 return false;
95
96 return asObject(baseVal)->methodTable()->hasInstance(asObject(baseVal), exec, value, proto);
97 }
98
99 inline bool opIn(ExecState* exec, JSValue propName, JSValue baseVal)
100 {
101 if (!baseVal.isObject()) {
102 exec->globalData().exception = createInvalidParamError(exec, "in", baseVal);
103 return false;
104 }
105
106 JSObject* baseObj = asObject(baseVal);
107
108 uint32_t i;
109 if (propName.getUInt32(i))
110 return baseObj->hasProperty(exec, i);
111
112 Identifier property(exec, propName.toString(exec)->value(exec));
113 if (exec->globalData().exception)
114 return false;
115 return baseObj->hasProperty(exec, property);
116 }
117
118 ALWAYS_INLINE JSValue opResolve(ExecState* exec, Identifier& ident)
119 {
120 ScopeChainNode* scopeChain = exec->scopeChain();
121
122 ScopeChainIterator iter = scopeChain->begin();
123 ScopeChainIterator end = scopeChain->end();
124 ASSERT(iter != end);
125
126 do {
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);
132
133 exec->globalData().exception = createUndefinedVariableError(exec, ident);
134 return JSValue();
135 }
136
137 ALWAYS_INLINE JSValue opResolveSkip(ExecState* exec, Identifier& ident, int skip)
138 {
139 ScopeChainNode* scopeChain = exec->scopeChain();
140
141 ScopeChainIterator iter = scopeChain->begin();
142 ScopeChainIterator end = scopeChain->end();
143 ASSERT(iter != 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())
149 ++iter;
150 }
151 while (skip--) {
152 ++iter;
153 ASSERT(iter != end);
154 }
155 do {
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);
161
162 exec->globalData().exception = createUndefinedVariableError(exec, ident);
163 return JSValue();
164 }
165
166 ALWAYS_INLINE JSValue opResolveWithBase(ExecState* exec, Identifier& ident, Register& baseSlot)
167 {
168 ScopeChainNode* scopeChain = exec->scopeChain();
169
170 ScopeChainIterator iter = scopeChain->begin();
171 ScopeChainIterator end = scopeChain->end();
172
173 // FIXME: add scopeDepthIsZero optimization
174
175 ASSERT(iter != end);
176
177 JSObject* base;
178 do {
179 base = iter->get();
180 PropertySlot slot(base);
181 if (base->getPropertySlot(exec, ident, slot)) {
182 JSValue result = slot.getValue(exec, ident);
183 if (exec->globalData().exception)
184 return JSValue();
185
186 baseSlot = JSValue(base);
187 return result;
188 }
189 ++iter;
190 } while (iter != end);
191
192 exec->globalData().exception = createUndefinedVariableError(exec, ident);
193 return JSValue();
194 }
195
196 ALWAYS_INLINE JSValue opResolveWithThis(ExecState* exec, Identifier& ident, Register& baseSlot)
197 {
198 ScopeChainNode* scopeChain = exec->scopeChain();
199
200 ScopeChainIterator iter = scopeChain->begin();
201 ScopeChainIterator end = scopeChain->end();
202
203 // FIXME: add scopeDepthIsZero optimization
204
205 ASSERT(iter != end);
206
207 JSObject* base;
208 do {
209 base = iter->get();
210 ++iter;
211 PropertySlot slot(base);
212 if (base->getPropertySlot(exec, ident, slot)) {
213 JSValue result = slot.getValue(exec, ident);
214 if (exec->globalData().exception)
215 return JSValue();
216
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();
224 else
225 baseSlot = JSValue(base);
226 return result;
227 }
228 } while (iter != end);
229
230 exec->globalData().exception = createUndefinedVariableError(exec, ident);
231 return JSValue();
232 }
233
234 } } // namespace JSC::CommonSlowPaths
235
236 #endif // CommonSlowPaths_h
237