]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Executable.h
e795fbfc3123e3f72b25f7aa320832b5d2a22520
[apple/javascriptcore.git] / runtime / Executable.h
1 /*
2 * Copyright (C) 2009, 2010 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 Executable_h
27 #define Executable_h
28
29 #include "CallData.h"
30 #include "JSFunction.h"
31 #include "Interpreter.h"
32 #include "Nodes.h"
33 #include "SamplingTool.h"
34 #include <wtf/PassOwnPtr.h>
35
36 namespace JSC {
37
38 class CodeBlock;
39 class Debugger;
40 class EvalCodeBlock;
41 class FunctionCodeBlock;
42 class ProgramCodeBlock;
43 class ScopeChainNode;
44
45 struct ExceptionInfo;
46
47 class ExecutableBase : public JSCell {
48 friend class JIT;
49
50 protected:
51 static const int NUM_PARAMETERS_IS_HOST = 0;
52 static const int NUM_PARAMETERS_NOT_COMPILED = -1;
53
54 public:
55 ExecutableBase(JSGlobalData& globalData, Structure* structure, int numParameters)
56 : JSCell(globalData, structure)
57 , m_numParametersForCall(numParameters)
58 , m_numParametersForConstruct(numParameters)
59 {
60 #if ENABLE(JIT)
61 Weak<ExecutableBase> finalizer(globalData, this, executableFinalizer());
62 finalizer.leakHandle();
63 #endif
64 }
65
66 bool isHostFunction() const
67 {
68 ASSERT((m_numParametersForCall == NUM_PARAMETERS_IS_HOST) == (m_numParametersForConstruct == NUM_PARAMETERS_IS_HOST));
69 return m_numParametersForCall == NUM_PARAMETERS_IS_HOST;
70 }
71
72 static Structure* createStructure(JSGlobalData& globalData, JSValue proto) { return Structure::create(globalData, proto, TypeInfo(CompoundType, StructureFlags), AnonymousSlotCount, &s_info); }
73
74 static const ClassInfo s_info;
75
76 protected:
77 static const unsigned StructureFlags = 0;
78 int m_numParametersForCall;
79 int m_numParametersForConstruct;
80
81 #if ENABLE(JIT)
82 public:
83 JITCode& generatedJITCodeForCall()
84 {
85 ASSERT(m_jitCodeForCall);
86 return m_jitCodeForCall;
87 }
88
89 JITCode& generatedJITCodeForConstruct()
90 {
91 ASSERT(m_jitCodeForConstruct);
92 return m_jitCodeForConstruct;
93 }
94
95 void clearExecutableCode()
96 {
97 m_jitCodeForCall.clear();
98 m_jitCodeForConstruct.clear();
99 }
100
101 protected:
102 JITCode m_jitCodeForCall;
103 JITCode m_jitCodeForConstruct;
104 MacroAssemblerCodePtr m_jitCodeForCallWithArityCheck;
105 MacroAssemblerCodePtr m_jitCodeForConstructWithArityCheck;
106
107 private:
108 static WeakHandleOwner* executableFinalizer();
109 #endif
110 };
111
112 class NativeExecutable : public ExecutableBase {
113 friend class JIT;
114 public:
115 #if ENABLE(JIT)
116 static NativeExecutable* create(JSGlobalData& globalData, MacroAssemblerCodePtr callThunk, NativeFunction function, MacroAssemblerCodePtr constructThunk, NativeFunction constructor)
117 {
118 if (!callThunk)
119 return new (&globalData) NativeExecutable(globalData, JITCode(), function, JITCode(), constructor);
120 return new (&globalData) NativeExecutable(globalData, JITCode::HostFunction(callThunk), function, JITCode::HostFunction(constructThunk), constructor);
121 }
122 #else
123 static NativeExecutable* create(JSGlobalData& globalData, NativeFunction function, NativeFunction constructor)
124 {
125 return new (&globalData) NativeExecutable(globalData, function, constructor);
126 }
127 #endif
128
129 ~NativeExecutable();
130
131 NativeFunction function() { return m_function; }
132
133 static Structure* createStructure(JSGlobalData& globalData, JSValue proto) { return Structure::create(globalData, proto, TypeInfo(LeafType, StructureFlags), AnonymousSlotCount, &s_info); }
134
135 static const ClassInfo s_info;
136
137 private:
138 #if ENABLE(JIT)
139 NativeExecutable(JSGlobalData& globalData, JITCode callThunk, NativeFunction function, JITCode constructThunk, NativeFunction constructor)
140 : ExecutableBase(globalData, globalData.nativeExecutableStructure.get(), NUM_PARAMETERS_IS_HOST)
141 , m_function(function)
142 , m_constructor(constructor)
143 {
144 m_jitCodeForCall = callThunk;
145 m_jitCodeForConstruct = constructThunk;
146 m_jitCodeForCallWithArityCheck = callThunk.addressForCall();
147 m_jitCodeForConstructWithArityCheck = constructThunk.addressForCall();
148 }
149 #else
150 NativeExecutable(JSGlobalData& globalData, NativeFunction function, NativeFunction constructor)
151 : ExecutableBase(globalData, globalData.nativeExecutableStructure.get(), NUM_PARAMETERS_IS_HOST)
152 , m_function(function)
153 , m_constructor(constructor)
154 {
155 }
156 #endif
157
158 NativeFunction m_function;
159 // Probably should be a NativeConstructor, but this will currently require rewriting the JIT
160 // trampoline. It may be easier to make NativeFunction be passed 'this' as a part of the ArgList.
161 NativeFunction m_constructor;
162 };
163
164 class ScriptExecutable : public ExecutableBase {
165 public:
166 ScriptExecutable(Structure* structure, JSGlobalData* globalData, const SourceCode& source, bool isInStrictContext)
167 : ExecutableBase(*globalData, structure, NUM_PARAMETERS_NOT_COMPILED)
168 , m_source(source)
169 , m_features(isInStrictContext ? StrictModeFeature : 0)
170 {
171 #if ENABLE(CODEBLOCK_SAMPLING)
172 relaxAdoptionRequirement();
173 if (SamplingTool* sampler = globalData->interpreter->sampler())
174 sampler->notifyOfScope(this);
175 #else
176 UNUSED_PARAM(globalData);
177 #endif
178 }
179
180 ScriptExecutable(Structure* structure, ExecState* exec, const SourceCode& source, bool isInStrictContext)
181 : ExecutableBase(exec->globalData(), structure, NUM_PARAMETERS_NOT_COMPILED)
182 , m_source(source)
183 , m_features(isInStrictContext ? StrictModeFeature : 0)
184 {
185 #if ENABLE(CODEBLOCK_SAMPLING)
186 relaxAdoptionRequirement();
187 if (SamplingTool* sampler = exec->globalData().interpreter->sampler())
188 sampler->notifyOfScope(this);
189 #else
190 UNUSED_PARAM(exec);
191 #endif
192 }
193
194 const SourceCode& source() { return m_source; }
195 intptr_t sourceID() const { return m_source.provider()->asID(); }
196 const UString& sourceURL() const { return m_source.provider()->url(); }
197 int lineNo() const { return m_firstLine; }
198 int lastLine() const { return m_lastLine; }
199
200 bool usesEval() const { return m_features & EvalFeature; }
201 bool usesArguments() const { return m_features & ArgumentsFeature; }
202 bool needsActivation() const { return m_hasCapturedVariables || m_features & (EvalFeature | WithFeature | CatchFeature); }
203 bool isStrictMode() const { return m_features & StrictModeFeature; }
204
205 virtual void unlinkCalls() = 0;
206
207 static const ClassInfo s_info;
208 protected:
209 void recordParse(CodeFeatures features, bool hasCapturedVariables, int firstLine, int lastLine)
210 {
211 m_features = features;
212 m_hasCapturedVariables = hasCapturedVariables;
213 m_firstLine = firstLine;
214 m_lastLine = lastLine;
215 }
216
217 SourceCode m_source;
218 CodeFeatures m_features;
219 bool m_hasCapturedVariables;
220 int m_firstLine;
221 int m_lastLine;
222 };
223
224 class EvalExecutable : public ScriptExecutable {
225 public:
226
227 ~EvalExecutable();
228
229 JSObject* compile(ExecState* exec, ScopeChainNode* scopeChainNode)
230 {
231 ASSERT(exec->globalData().dynamicGlobalObject);
232 JSObject* error = 0;
233 if (!m_evalCodeBlock)
234 error = compileInternal(exec, scopeChainNode);
235 ASSERT(!error == !!m_evalCodeBlock);
236 return error;
237 }
238
239 EvalCodeBlock& generatedBytecode()
240 {
241 ASSERT(m_evalCodeBlock);
242 return *m_evalCodeBlock;
243 }
244
245 static EvalExecutable* create(ExecState* exec, const SourceCode& source, bool isInStrictContext) { return new (exec) EvalExecutable(exec, source, isInStrictContext); }
246
247 #if ENABLE(JIT)
248 JITCode& generatedJITCode()
249 {
250 return generatedJITCodeForCall();
251 }
252 #endif
253 static Structure* createStructure(JSGlobalData& globalData, JSValue proto)
254 {
255 return Structure::create(globalData, proto, TypeInfo(CompoundType, StructureFlags), AnonymousSlotCount, &s_info);
256 }
257
258 static const ClassInfo s_info;
259 private:
260 static const unsigned StructureFlags = OverridesVisitChildren | ScriptExecutable::StructureFlags;
261 EvalExecutable(ExecState*, const SourceCode&, bool);
262
263 JSObject* compileInternal(ExecState*, ScopeChainNode*);
264 virtual void visitChildren(SlotVisitor&);
265 void unlinkCalls();
266
267 OwnPtr<EvalCodeBlock> m_evalCodeBlock;
268 };
269
270 class ProgramExecutable : public ScriptExecutable {
271 public:
272 static ProgramExecutable* create(ExecState* exec, const SourceCode& source)
273 {
274 return new (exec) ProgramExecutable(exec, source);
275 }
276
277 ~ProgramExecutable();
278
279 JSObject* compile(ExecState* exec, ScopeChainNode* scopeChainNode)
280 {
281 ASSERT(exec->globalData().dynamicGlobalObject);
282 JSObject* error = 0;
283 if (!m_programCodeBlock)
284 error = compileInternal(exec, scopeChainNode);
285 ASSERT(!error == !!m_programCodeBlock);
286 return error;
287 }
288
289 ProgramCodeBlock& generatedBytecode()
290 {
291 ASSERT(m_programCodeBlock);
292 return *m_programCodeBlock;
293 }
294
295 JSObject* checkSyntax(ExecState*);
296
297 #if ENABLE(JIT)
298 JITCode& generatedJITCode()
299 {
300 return generatedJITCodeForCall();
301 }
302 #endif
303
304 static Structure* createStructure(JSGlobalData& globalData, JSValue proto)
305 {
306 return Structure::create(globalData, proto, TypeInfo(CompoundType, StructureFlags), AnonymousSlotCount, &s_info);
307 }
308
309 static const ClassInfo s_info;
310
311 private:
312 static const unsigned StructureFlags = OverridesVisitChildren | ScriptExecutable::StructureFlags;
313 ProgramExecutable(ExecState*, const SourceCode&);
314
315 JSObject* compileInternal(ExecState*, ScopeChainNode*);
316 virtual void visitChildren(SlotVisitor&);
317 void unlinkCalls();
318
319 OwnPtr<ProgramCodeBlock> m_programCodeBlock;
320 };
321
322 class FunctionExecutable : public ScriptExecutable {
323 friend class JIT;
324 public:
325 static FunctionExecutable* create(ExecState* exec, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool isInStrictContext, int firstLine, int lastLine)
326 {
327 return new (exec) FunctionExecutable(exec, name, source, forceUsesArguments, parameters, isInStrictContext, firstLine, lastLine);
328 }
329
330 static FunctionExecutable* create(JSGlobalData* globalData, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool isInStrictContext, int firstLine, int lastLine)
331 {
332 return new (globalData) FunctionExecutable(globalData, name, source, forceUsesArguments, parameters, isInStrictContext, firstLine, lastLine);
333 }
334
335 JSFunction* make(ExecState* exec, ScopeChainNode* scopeChain)
336 {
337 return new (exec) JSFunction(exec, this, scopeChain);
338 }
339
340 // Returns either call or construct bytecode. This can be appropriate
341 // for answering questions that that don't vary between call and construct --
342 // for example, argumentsRegister().
343 FunctionCodeBlock& generatedBytecode()
344 {
345 if (m_codeBlockForCall)
346 return *m_codeBlockForCall;
347 ASSERT(m_codeBlockForConstruct);
348 return *m_codeBlockForConstruct;
349 }
350
351 JSObject* compileForCall(ExecState* exec, ScopeChainNode* scopeChainNode)
352 {
353 ASSERT(exec->globalData().dynamicGlobalObject);
354 JSObject* error = 0;
355 if (!m_codeBlockForCall)
356 error = compileForCallInternal(exec, scopeChainNode);
357 ASSERT(!error == !!m_codeBlockForCall);
358 return error;
359 }
360
361 bool isGeneratedForCall() const
362 {
363 return m_codeBlockForCall;
364 }
365
366 FunctionCodeBlock& generatedBytecodeForCall()
367 {
368 ASSERT(m_codeBlockForCall);
369 return *m_codeBlockForCall;
370 }
371
372 JSObject* compileForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode)
373 {
374 ASSERT(exec->globalData().dynamicGlobalObject);
375 JSObject* error = 0;
376 if (!m_codeBlockForConstruct)
377 error = compileForConstructInternal(exec, scopeChainNode);
378 ASSERT(!error == !!m_codeBlockForConstruct);
379 return error;
380 }
381
382 bool isGeneratedForConstruct() const
383 {
384 return m_codeBlockForConstruct;
385 }
386
387 FunctionCodeBlock& generatedBytecodeForConstruct()
388 {
389 ASSERT(m_codeBlockForConstruct);
390 return *m_codeBlockForConstruct;
391 }
392
393 const Identifier& name() { return m_name; }
394 size_t parameterCount() const { return m_parameters->size(); }
395 unsigned capturedVariableCount() const { return m_numCapturedVariables; }
396 UString paramString() const;
397 SharedSymbolTable* symbolTable() const { return m_symbolTable; }
398
399 void discardCode();
400 void visitChildren(SlotVisitor&);
401 static FunctionExecutable* fromGlobalCode(const Identifier&, ExecState*, Debugger*, const SourceCode&, JSObject** exception);
402 static Structure* createStructure(JSGlobalData& globalData, JSValue proto)
403 {
404 return Structure::create(globalData, proto, TypeInfo(CompoundType, StructureFlags), AnonymousSlotCount, &s_info);
405 }
406
407 static const ClassInfo s_info;
408
409 private:
410 FunctionExecutable(JSGlobalData*, const Identifier& name, const SourceCode&, bool forceUsesArguments, FunctionParameters*, bool, int firstLine, int lastLine);
411 FunctionExecutable(ExecState*, const Identifier& name, const SourceCode&, bool forceUsesArguments, FunctionParameters*, bool, int firstLine, int lastLine);
412
413 JSObject* compileForCallInternal(ExecState*, ScopeChainNode*);
414 JSObject* compileForConstructInternal(ExecState*, ScopeChainNode*);
415
416 static const unsigned StructureFlags = OverridesVisitChildren | ScriptExecutable::StructureFlags;
417 unsigned m_numCapturedVariables : 31;
418 bool m_forceUsesArguments : 1;
419 void unlinkCalls();
420
421 RefPtr<FunctionParameters> m_parameters;
422 OwnPtr<FunctionCodeBlock> m_codeBlockForCall;
423 OwnPtr<FunctionCodeBlock> m_codeBlockForConstruct;
424 Identifier m_name;
425 SharedSymbolTable* m_symbolTable;
426
427 #if ENABLE(JIT)
428 public:
429 MacroAssemblerCodePtr generatedJITCodeForCallWithArityCheck()
430 {
431 ASSERT(m_jitCodeForCall);
432 ASSERT(m_jitCodeForCallWithArityCheck);
433 return m_jitCodeForCallWithArityCheck;
434 }
435
436 MacroAssemblerCodePtr generatedJITCodeForConstructWithArityCheck()
437 {
438 ASSERT(m_jitCodeForConstruct);
439 ASSERT(m_jitCodeForConstructWithArityCheck);
440 return m_jitCodeForConstructWithArityCheck;
441 }
442 #endif
443 };
444
445 inline FunctionExecutable* JSFunction::jsExecutable() const
446 {
447 ASSERT(!isHostFunctionNonInline());
448 return static_cast<FunctionExecutable*>(m_executable.get());
449 }
450
451 inline bool JSFunction::isHostFunction() const
452 {
453 ASSERT(m_executable);
454 return m_executable->isHostFunction();
455 }
456
457 inline NativeFunction JSFunction::nativeFunction()
458 {
459 ASSERT(isHostFunction());
460 return static_cast<NativeExecutable*>(m_executable.get())->function();
461 }
462 }
463
464 #endif