Get rid of HeapRootVisitor and make SlotVisitor less painful to use
[WebKit-https.git] / Source / JavaScriptCore / runtime / FunctionExecutable.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2013, 2015-2016 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
28 #include "BatchedTransitionOptimizer.h"
29 #include "CodeBlock.h"
30 #include "Debugger.h"
31 #include "FunctionCodeBlock.h"
32 #include "JIT.h"
33 #include "JSCInlines.h"
34 #include "LLIntEntrypoint.h"
35 #include "Parser.h"
36 #include "TypeProfiler.h"
37 #include "VMInlines.h"
38 #include <wtf/CommaPrinter.h>
39
40 namespace JSC {
41
42 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
43
44 FunctionExecutable::FunctionExecutable(VM& vm, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, unsigned lastLine, unsigned endColumn, Intrinsic intrinsic)
45     : ScriptExecutable(vm.functionExecutableStructure.get(), vm, source, unlinkedExecutable->isInStrictContext(), unlinkedExecutable->derivedContextType(), false, EvalContextType::None, intrinsic)
46     , m_unlinkedExecutable(vm, this, unlinkedExecutable)
47 {
48     RELEASE_ASSERT(!source.isNull());
49     ASSERT(source.length());
50     m_lastLine = lastLine;
51     ASSERT(endColumn != UINT_MAX);
52     m_endColumn = endColumn;
53     m_parametersStartOffset = unlinkedExecutable->parametersStartOffset();
54     m_typeProfilingStartOffset = unlinkedExecutable->typeProfilingStartOffset();
55     m_typeProfilingEndOffset = unlinkedExecutable->typeProfilingEndOffset();
56 }
57
58 void FunctionExecutable::finishCreation(VM& vm)
59 {
60     Base::finishCreation(vm);
61     m_singletonFunction.set(vm, this, InferredValue::create(vm));
62 }
63
64 void FunctionExecutable::destroy(JSCell* cell)
65 {
66     static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
67 }
68
69 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
70 {
71     FunctionCodeBlock* result;
72     if (kind == CodeForCall)
73         result = m_codeBlockForCall.get();
74     else {
75         RELEASE_ASSERT(kind == CodeForConstruct);
76         result = m_codeBlockForConstruct.get();
77     }
78     if (!result)
79         return 0;
80     return static_cast<FunctionCodeBlock*>(result->baselineAlternative());
81 }
82
83 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
84 {
85     FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
86     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
87     ScriptExecutable::visitChildren(thisObject, visitor);
88     if (FunctionCodeBlock* codeBlockForCall = thisObject->m_codeBlockForCall.get())
89         codeBlockForCall->visitWeakly(visitor);
90     if (FunctionCodeBlock* codeBlockForConstruct = thisObject->m_codeBlockForConstruct.get())
91         codeBlockForConstruct->visitWeakly(visitor);
92     visitor.append(thisObject->m_unlinkedExecutable);
93     visitor.append(thisObject->m_singletonFunction);
94 }
95
96 FunctionExecutable* FunctionExecutable::fromGlobalCode(
97     const Identifier& name, ExecState& exec, const SourceCode& source, 
98     JSObject*& exception, int overrideLineNumber)
99 {
100     UnlinkedFunctionExecutable* unlinkedExecutable = 
101         UnlinkedFunctionExecutable::fromGlobalCode(
102             name, exec, source, exception, overrideLineNumber);
103     if (!unlinkedExecutable)
104         return nullptr;
105
106     return unlinkedExecutable->link(exec.vm(), source, overrideLineNumber);
107 }
108
109 } // namespace JSC