initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / Executable.cpp
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 #include "config.h"
27 #include "Executable.h"
28
29 #include "BytecodeGenerator.h"
30 #include "CodeBlock.h"
31 #include "DFGDriver.h"
32 #include "JIT.h"
33 #include "Parser.h"
34 #include "UStringBuilder.h"
35 #include "Vector.h"
36
37 namespace JSC {
38
39 const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0 };
40
41 void ExecutableBase::clearCode()
42 {
43 #if ENABLE(JIT)
44     m_jitCodeForCall.clear();
45     m_jitCodeForConstruct.clear();
46     m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
47     m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
48 #endif
49     m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
50     m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
51 }
52
53 class ExecutableFinalizer : public WeakHandleOwner {
54     virtual void finalize(Handle<Unknown> handle, void*)
55     {
56         Weak<ExecutableBase> executable(Weak<ExecutableBase>::Adopt, handle);
57         executable->clearCode();
58     }
59 };
60
61 WeakHandleOwner* ExecutableBase::executableFinalizer()
62 {
63     DEFINE_STATIC_LOCAL(ExecutableFinalizer, finalizer, ());
64     return &finalizer;
65 }
66
67 const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0 };
68
69 NativeExecutable::~NativeExecutable()
70 {
71 }
72
73 const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0 };
74
75 const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0 };
76
77 EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
78     : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext)
79 {
80 }
81
82 EvalExecutable::~EvalExecutable()
83 {
84 }
85
86 const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0 };
87
88 ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
89     : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false)
90 {
91 }
92
93 ProgramExecutable::~ProgramExecutable()
94 {
95 }
96
97 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0 };
98
99 FunctionExecutable::FunctionExecutable(JSGlobalData& globalData, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
100     : ScriptExecutable(globalData.functionExecutableStructure.get(), globalData, source, inStrictContext)
101     , m_numCapturedVariables(0)
102     , m_forceUsesArguments(forceUsesArguments)
103     , m_parameters(parameters)
104     , m_name(name)
105     , m_symbolTable(0)
106 {
107 }
108
109 FunctionExecutable::FunctionExecutable(ExecState* exec, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
110     : ScriptExecutable(exec->globalData().functionExecutableStructure.get(), exec, source, inStrictContext)
111     , m_numCapturedVariables(0)
112     , m_forceUsesArguments(forceUsesArguments)
113     , m_parameters(parameters)
114     , m_name(name)
115     , m_symbolTable(0)
116 {
117 }
118
119 JSObject* EvalExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
120 {
121     ASSERT(exec->globalData().dynamicGlobalObject);
122     ASSERT(!!m_evalCodeBlock);
123     JSObject* error = 0;
124     if (m_evalCodeBlock->getJITType() != JITCode::topTierJIT())
125         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_evalCodeBlock->getJITType()));
126     ASSERT(!!m_evalCodeBlock);
127     return error;
128 }
129
130 JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
131 {
132 #if !ENABLE(JIT)
133     UNUSED_PARAM(jitType);
134 #endif
135     JSObject* exception = 0;
136     JSGlobalData* globalData = &exec->globalData();
137     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
138     if (!lexicalGlobalObject->evalEnabled())
139         return throwError(exec, createEvalError(exec, "Eval is disabled"));
140     RefPtr<EvalNode> evalNode = globalData->parser->parse<EvalNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
141     if (!evalNode) {
142         ASSERT(exception);
143         return exception;
144     }
145     recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
146
147     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
148
149     OwnPtr<CodeBlock> previousCodeBlock = m_evalCodeBlock.release();
150     ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
151     m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth(), previousCodeBlock.release()));
152     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get(), !!m_evalCodeBlock->alternative() ? BytecodeGenerator::OptimizingCompilation : BytecodeGenerator::FirstCompilation)));
153     if ((exception = generator->generate())) {
154         m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
155         evalNode->destroyData();
156         return exception;
157     }
158
159     evalNode->destroyData();
160     m_evalCodeBlock->copyDataFromAlternative();
161
162 #if ENABLE(JIT)
163     if (exec->globalData().canUseJIT()) {
164         bool dfgCompiled = false;
165         if (jitType == JITCode::DFGJIT)
166             dfgCompiled = DFG::tryCompile(exec, m_evalCodeBlock.get(), m_jitCodeForCall);
167         if (dfgCompiled)
168             ASSERT(!m_evalCodeBlock->alternative() || !m_evalCodeBlock->alternative()->hasIncomingCalls());
169         else {
170             if (m_evalCodeBlock->alternative()) {
171                 // There is already an alternative piece of code compiled with a different
172                 // JIT, so we can silently fail.
173                 m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
174                 return 0;
175             }
176             m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_evalCodeBlock.get());
177         }
178 #if !ENABLE(OPCODE_SAMPLING)
179         if (!BytecodeGenerator::dumpsGeneratedCode())
180             m_evalCodeBlock->discardBytecode();
181 #endif
182         m_evalCodeBlock->setJITCode(m_jitCodeForCall, MacroAssemblerCodePtr());
183     }
184 #endif
185
186 #if ENABLE(JIT)
187 #if ENABLE(INTERPRETER)
188     if (!m_jitCodeForCall)
189         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
190     else
191 #endif
192     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
193 #else
194     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
195 #endif
196
197     return 0;
198 }
199
200 void EvalExecutable::visitChildren(SlotVisitor& visitor)
201 {
202     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
203     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
204     ASSERT(structure()->typeInfo().overridesVisitChildren());
205     ScriptExecutable::visitChildren(visitor);
206     if (m_evalCodeBlock)
207         m_evalCodeBlock->visitAggregate(visitor);
208 }
209
210 void EvalExecutable::unlinkCalls()
211 {
212 #if ENABLE(JIT)
213     if (!m_jitCodeForCall)
214         return;
215     ASSERT(m_evalCodeBlock);
216     m_evalCodeBlock->unlinkCalls();
217 #endif
218 }
219
220 void EvalExecutable::clearCode()
221 {
222     if (m_evalCodeBlock) {
223         m_evalCodeBlock->clearEvalCache();
224         m_evalCodeBlock.clear();
225     }
226     Base::clearCode();
227 }
228
229 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
230 {
231     JSObject* exception = 0;
232     JSGlobalData* globalData = &exec->globalData();
233     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
234     RefPtr<ProgramNode> programNode = globalData->parser->parse<ProgramNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, JSParseNormal, &exception);
235     if (programNode)
236         return 0;
237     ASSERT(exception);
238     return exception;
239 }
240
241 JSObject* ProgramExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
242 {
243     ASSERT(exec->globalData().dynamicGlobalObject);
244     ASSERT(!!m_programCodeBlock);
245     JSObject* error = 0;
246     if (m_programCodeBlock->getJITType() != JITCode::topTierJIT())
247         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_programCodeBlock->getJITType()));
248     ASSERT(!!m_programCodeBlock);
249     return error;
250 }
251
252 JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
253 {
254 #if !ENABLE(JIT)
255     UNUSED_PARAM(jitType);
256 #endif
257     JSObject* exception = 0;
258     JSGlobalData* globalData = &exec->globalData();
259     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
260     RefPtr<ProgramNode> programNode = globalData->parser->parse<ProgramNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
261     if (!programNode) {
262         ASSERT(exception);
263         return exception;
264     }
265     recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
266
267     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
268     
269     OwnPtr<CodeBlock> previousCodeBlock = m_programCodeBlock.release();
270     ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
271     m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider(), previousCodeBlock.release()));
272     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get(), !!m_programCodeBlock->alternative() ? BytecodeGenerator::OptimizingCompilation : BytecodeGenerator::FirstCompilation)));
273     if ((exception = generator->generate())) {
274         m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
275         programNode->destroyData();
276         return exception;
277     }
278
279     programNode->destroyData();
280     m_programCodeBlock->copyDataFromAlternative();
281
282 #if ENABLE(JIT)
283     if (exec->globalData().canUseJIT()) {
284         bool dfgCompiled = false;
285         if (jitType == JITCode::DFGJIT)
286             dfgCompiled = DFG::tryCompile(exec, m_programCodeBlock.get(), m_jitCodeForCall);
287         if (dfgCompiled) {
288             if (m_programCodeBlock->alternative())
289                 m_programCodeBlock->alternative()->unlinkIncomingCalls();
290         } else {
291             if (m_programCodeBlock->alternative()) {
292                 m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
293                 return 0;
294             }
295             m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_programCodeBlock.get());
296         }
297 #if !ENABLE(OPCODE_SAMPLING)
298         if (!BytecodeGenerator::dumpsGeneratedCode())
299             m_programCodeBlock->discardBytecode();
300 #endif
301         m_programCodeBlock->setJITCode(m_jitCodeForCall, MacroAssemblerCodePtr());
302     }
303 #endif
304
305 #if ENABLE(JIT)
306 #if ENABLE(INTERPRETER)
307     if (!m_jitCodeForCall)
308         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
309     else
310 #endif
311         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
312 #else
313     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
314 #endif
315
316     return 0;
317 }
318
319 void ProgramExecutable::unlinkCalls()
320 {
321 #if ENABLE(JIT)
322     if (!m_jitCodeForCall)
323         return;
324     ASSERT(m_programCodeBlock);
325     m_programCodeBlock->unlinkCalls();
326 #endif
327 }
328
329 void ProgramExecutable::visitChildren(SlotVisitor& visitor)
330 {
331     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
332     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
333     ASSERT(structure()->typeInfo().overridesVisitChildren());
334     ScriptExecutable::visitChildren(visitor);
335     if (m_programCodeBlock)
336         m_programCodeBlock->visitAggregate(visitor);
337 }
338
339 void ProgramExecutable::clearCode()
340 {
341     if (m_programCodeBlock) {
342         m_programCodeBlock->clearEvalCache();
343         m_programCodeBlock.clear();
344     }
345     Base::clearCode();
346 }
347
348 JSObject* FunctionExecutable::compileOptimizedForCall(ExecState* exec, ScopeChainNode* scopeChainNode, ExecState* calleeArgsExec)
349 {
350     ASSERT(exec->globalData().dynamicGlobalObject);
351     ASSERT(!!m_codeBlockForCall);
352     JSObject* error = 0;
353     if (m_codeBlockForCall->getJITType() != JITCode::topTierJIT())
354         error = compileForCallInternal(exec, scopeChainNode, calleeArgsExec, JITCode::nextTierJIT(m_codeBlockForCall->getJITType()));
355     ASSERT(!!m_codeBlockForCall);
356     return error;
357 }
358
359 JSObject* FunctionExecutable::compileOptimizedForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode)
360 {
361     ASSERT(exec->globalData().dynamicGlobalObject);
362     ASSERT(!!m_codeBlockForConstruct);
363     JSObject* error = 0;
364     if (m_codeBlockForConstruct->getJITType() != JITCode::topTierJIT())
365         error = compileForConstructInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForConstruct->getJITType()));
366     ASSERT(!!m_codeBlockForConstruct);
367     return error;
368 }
369
370 JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode, ExecState* calleeArgsExec, JITCode::JITType jitType)
371 {
372 #if !ENABLE(JIT)
373     UNUSED_PARAM(jitType);
374 #endif
375     JSObject* exception = 0;
376     JSGlobalData* globalData = scopeChainNode->globalData;
377     RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(exec->lexicalGlobalObject(), 0, 0, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
378     if (!body) {
379         ASSERT(exception);
380         return exception;
381     }
382     if (m_forceUsesArguments)
383         body->setUsesArguments();
384     body->finishParsing(m_parameters, m_name);
385     recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
386
387     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
388
389     OwnPtr<CodeBlock> previousCodeBlock = m_codeBlockForCall.release();
390     ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
391     m_codeBlockForCall = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), false, previousCodeBlock.release()));
392     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, m_codeBlockForCall->symbolTable(), m_codeBlockForCall.get(), !!m_codeBlockForCall->alternative() ? BytecodeGenerator::OptimizingCompilation : BytecodeGenerator::FirstCompilation)));
393     if ((exception = generator->generate())) {
394         m_codeBlockForCall = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForCall->releaseAlternative());
395         body->destroyData();
396         return exception;
397     }
398
399     m_numParametersForCall = m_codeBlockForCall->m_numParameters;
400     ASSERT(m_numParametersForCall);
401     m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
402     m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
403
404     body->destroyData();
405     m_codeBlockForCall->copyDataFromAlternative();
406
407 #if ENABLE(JIT)
408     if (exec->globalData().canUseJIT()) {
409         bool dfgCompiled = false;
410         if (jitType == JITCode::DFGJIT)
411             dfgCompiled = DFG::tryCompileFunction(exec, calleeArgsExec, m_codeBlockForCall.get(), m_jitCodeForCall, m_jitCodeForCallWithArityCheck);
412         if (dfgCompiled) {
413             if (m_codeBlockForCall->alternative())
414                 m_codeBlockForCall->alternative()->unlinkIncomingCalls();
415         } else {
416             if (m_codeBlockForCall->alternative()) {
417                 m_codeBlockForCall = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForCall->releaseAlternative());
418                 m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
419                 return 0;
420             }
421             m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_codeBlockForCall.get(), &m_jitCodeForCallWithArityCheck);
422         }
423 #if !ENABLE(OPCODE_SAMPLING)
424         if (!BytecodeGenerator::dumpsGeneratedCode())
425             m_codeBlockForCall->discardBytecode();
426 #endif
427         
428         m_codeBlockForCall->setJITCode(m_jitCodeForCall, m_jitCodeForCallWithArityCheck);
429     }
430 #else
431     UNUSED_PARAM(calleeArgsExec);
432 #endif
433
434 #if ENABLE(JIT)
435 #if ENABLE(INTERPRETER)
436     if (!m_jitCodeForCall)
437         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
438     else
439 #endif
440         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
441 #else
442     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
443 #endif
444
445     return 0;
446 }
447
448 JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
449 {
450     UNUSED_PARAM(jitType);
451     
452     JSObject* exception = 0;
453     JSGlobalData* globalData = scopeChainNode->globalData;
454     RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(exec->lexicalGlobalObject(), 0, 0, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
455     if (!body) {
456         ASSERT(exception);
457         return exception;
458     }
459     if (m_forceUsesArguments)
460         body->setUsesArguments();
461     body->finishParsing(m_parameters, m_name);
462     recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
463
464     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
465
466     OwnPtr<CodeBlock> previousCodeBlock = m_codeBlockForConstruct.release();
467     ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
468     m_codeBlockForConstruct = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), true, previousCodeBlock.release()));
469     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, m_codeBlockForConstruct->symbolTable(), m_codeBlockForConstruct.get(), !!m_codeBlockForConstruct->alternative() ? BytecodeGenerator::OptimizingCompilation : BytecodeGenerator::FirstCompilation)));
470     if ((exception = generator->generate())) {
471         m_codeBlockForConstruct = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForConstruct->releaseAlternative());
472         body->destroyData();
473         return exception;
474     }
475
476     m_numParametersForConstruct = m_codeBlockForConstruct->m_numParameters;
477     ASSERT(m_numParametersForConstruct);
478     m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
479     m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
480
481     body->destroyData();
482     m_codeBlockForConstruct->copyDataFromAlternative();
483
484 #if ENABLE(JIT)
485     if (exec->globalData().canUseJIT()) {
486         bool dfgCompiled = false;
487         // FIXME: Make it possible to compile constructors with DFG.
488         if (dfgCompiled) {
489             if (m_codeBlockForConstruct->alternative())
490                 m_codeBlockForConstruct->alternative()->unlinkIncomingCalls();
491         } else {
492             if (m_codeBlockForConstruct->alternative()) {
493                 m_codeBlockForConstruct = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForConstruct->releaseAlternative());
494                 m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
495                 return 0;
496             }
497             m_jitCodeForConstruct = JIT::compile(scopeChainNode->globalData, m_codeBlockForConstruct.get(), &m_jitCodeForConstructWithArityCheck);
498         }
499 #if !ENABLE(OPCODE_SAMPLING)
500         if (!BytecodeGenerator::dumpsGeneratedCode())
501             m_codeBlockForConstruct->discardBytecode();
502 #endif
503         
504         m_codeBlockForConstruct->setJITCode(m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck);
505     }
506 #endif
507
508 #if ENABLE(JIT)
509 #if ENABLE(INTERPRETER)
510     if (!m_jitCodeForConstruct)
511         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
512     else
513 #endif
514     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
515 #else
516     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
517 #endif
518
519     return 0;
520 }
521
522 void FunctionExecutable::visitChildren(SlotVisitor& visitor)
523 {
524     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
525     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
526     ASSERT(structure()->typeInfo().overridesVisitChildren());
527     ScriptExecutable::visitChildren(visitor);
528     if (m_nameValue)
529         visitor.append(&m_nameValue);
530     if (m_codeBlockForCall)
531         m_codeBlockForCall->visitAggregate(visitor);
532     if (m_codeBlockForConstruct)
533         m_codeBlockForConstruct->visitAggregate(visitor);
534 }
535
536 void FunctionExecutable::discardCode()
537 {
538 #if ENABLE(JIT)
539     // These first two checks are to handle the rare case where
540     // we are trying to evict code for a function during its
541     // codegen.
542     if (!m_jitCodeForCall && m_codeBlockForCall)
543         return;
544     if (!m_jitCodeForConstruct && m_codeBlockForConstruct)
545         return;
546 #endif
547     clearCode();
548 }
549
550 void FunctionExecutable::clearCode()
551 {
552     if (m_codeBlockForCall) {
553         m_codeBlockForCall->clearEvalCache();
554         m_codeBlockForCall.clear();
555     }
556     if (m_codeBlockForConstruct) {
557         m_codeBlockForConstruct->clearEvalCache();
558         m_codeBlockForConstruct.clear();
559     }
560     Base::clearCode();
561 }
562
563 void FunctionExecutable::unlinkCalls()
564 {
565 #if ENABLE(JIT)
566     if (!!m_jitCodeForCall) {
567         ASSERT(m_codeBlockForCall);
568         m_codeBlockForCall->unlinkCalls();
569     }
570     if (!!m_jitCodeForConstruct) {
571         ASSERT(m_codeBlockForConstruct);
572         m_codeBlockForConstruct->unlinkCalls();
573     }
574 #endif
575 }
576
577 FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
578 {
579     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
580     RefPtr<ProgramNode> program = exec->globalData().parser->parse<ProgramNode>(lexicalGlobalObject, debugger, exec, source, 0, JSParseNormal, exception);
581     if (!program) {
582         ASSERT(*exception);
583         return 0;
584     }
585
586     // Uses of this function that would not result in a single function expression are invalid.
587     StatementNode* exprStatement = program->singleStatement();
588     ASSERT(exprStatement);
589     ASSERT(exprStatement->isExprStatement());
590     ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
591     ASSERT(funcExpr);
592     ASSERT(funcExpr->isFuncExprNode());
593     FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
594     ASSERT(body);
595
596     return FunctionExecutable::create(exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
597 }
598
599 UString FunctionExecutable::paramString() const
600 {
601     FunctionParameters& parameters = *m_parameters;
602     UStringBuilder builder;
603     for (size_t pos = 0; pos < parameters.size(); ++pos) {
604         if (!builder.isEmpty())
605             builder.append(", ");
606         builder.append(parameters[pos].ustring());
607     }
608     return builder.toUString();
609 }
610
611 }