]>
Commit | Line | Data |
---|---|---|
f9bf01c6 | 1 | /* |
14957cd0 | 2 | * Copyright (C) 2009, 2010 Apple Inc. All rights reserved. |
f9bf01c6 A |
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 "JIT.h" | |
32 | #include "Parser.h" | |
14957cd0 | 33 | #include "UStringBuilder.h" |
f9bf01c6 A |
34 | #include "Vector.h" |
35 | ||
14957cd0 A |
36 | #if ENABLE(DFG_JIT) |
37 | #include "DFGByteCodeParser.h" | |
38 | #include "DFGJITCompiler.h" | |
39 | #endif | |
40 | ||
f9bf01c6 A |
41 | namespace JSC { |
42 | ||
14957cd0 A |
43 | const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0 }; |
44 | ||
f9bf01c6 | 45 | #if ENABLE(JIT) |
14957cd0 A |
46 | class ExecutableFinalizer : public WeakHandleOwner { |
47 | virtual void finalize(Handle<Unknown> handle, void*) | |
48 | { | |
49 | Weak<ExecutableBase> executable(Weak<ExecutableBase>::Adopt, handle); | |
50 | executable->clearExecutableCode(); | |
51 | } | |
52 | }; | |
53 | ||
54 | WeakHandleOwner* ExecutableBase::executableFinalizer() | |
f9bf01c6 | 55 | { |
14957cd0 A |
56 | DEFINE_STATIC_LOCAL(ExecutableFinalizer, finalizer, ()); |
57 | return &finalizer; | |
f9bf01c6 A |
58 | } |
59 | #endif | |
14957cd0 A |
60 | |
61 | const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0 }; | |
f9bf01c6 | 62 | |
14957cd0 A |
63 | NativeExecutable::~NativeExecutable() |
64 | { | |
65 | } | |
66 | ||
67 | const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0 }; | |
68 | ||
69 | const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0 }; | |
70 | ||
71 | EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext) | |
72 | : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext) | |
f9bf01c6 A |
73 | { |
74 | } | |
75 | ||
76 | EvalExecutable::~EvalExecutable() | |
77 | { | |
14957cd0 A |
78 | } |
79 | ||
80 | const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0 }; | |
81 | ||
82 | ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source) | |
83 | : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false) | |
84 | { | |
f9bf01c6 A |
85 | } |
86 | ||
87 | ProgramExecutable::~ProgramExecutable() | |
88 | { | |
f9bf01c6 A |
89 | } |
90 | ||
14957cd0 A |
91 | const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0 }; |
92 | ||
93 | FunctionExecutable::FunctionExecutable(JSGlobalData* globalData, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext, int firstLine, int lastLine) | |
94 | : ScriptExecutable(globalData->functionExecutableStructure.get(), globalData, source, inStrictContext) | |
95 | , m_numCapturedVariables(0) | |
96 | , m_forceUsesArguments(forceUsesArguments) | |
97 | , m_parameters(parameters) | |
98 | , m_name(name) | |
99 | , m_symbolTable(0) | |
f9bf01c6 | 100 | { |
14957cd0 A |
101 | m_firstLine = firstLine; |
102 | m_lastLine = lastLine; | |
f9bf01c6 A |
103 | } |
104 | ||
14957cd0 A |
105 | FunctionExecutable::FunctionExecutable(ExecState* exec, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext, int firstLine, int lastLine) |
106 | : ScriptExecutable(exec->globalData().functionExecutableStructure.get(), exec, source, inStrictContext) | |
107 | , m_numCapturedVariables(0) | |
108 | , m_forceUsesArguments(forceUsesArguments) | |
109 | , m_parameters(parameters) | |
110 | , m_name(name) | |
111 | , m_symbolTable(0) | |
f9bf01c6 | 112 | { |
14957cd0 A |
113 | m_firstLine = firstLine; |
114 | m_lastLine = lastLine; | |
115 | } | |
f9bf01c6 | 116 | |
14957cd0 A |
117 | |
118 | JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode) | |
119 | { | |
120 | JSObject* exception = 0; | |
121 | JSGlobalData* globalData = &exec->globalData(); | |
122 | JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject(); | |
123 | if (!lexicalGlobalObject->isEvalEnabled()) | |
124 | return throwError(exec, createEvalError(exec, "Eval is disabled")); | |
125 | RefPtr<EvalNode> evalNode = globalData->parser->parse<EvalNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, &exception); | |
126 | if (!evalNode) { | |
127 | ASSERT(exception); | |
128 | return exception; | |
129 | } | |
130 | recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine()); | |
131 | ||
132 | JSGlobalObject* globalObject = scopeChainNode->globalObject.get(); | |
f9bf01c6 A |
133 | |
134 | ASSERT(!m_evalCodeBlock); | |
14957cd0 A |
135 | m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth())); |
136 | OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get()))); | |
137 | if ((exception = generator->generate())) { | |
138 | m_evalCodeBlock.clear(); | |
139 | evalNode->destroyData(); | |
140 | return exception; | |
141 | } | |
142 | ||
f9bf01c6 | 143 | evalNode->destroyData(); |
14957cd0 A |
144 | |
145 | #if ENABLE(JIT) | |
146 | if (exec->globalData().canUseJIT()) { | |
147 | m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_evalCodeBlock.get()); | |
148 | #if !ENABLE(OPCODE_SAMPLING) | |
149 | if (!BytecodeGenerator::dumpsGeneratedCode()) | |
150 | m_evalCodeBlock->discardBytecode(); | |
151 | #endif | |
152 | } | |
153 | #endif | |
154 | ||
155 | #if ENABLE(JIT) | |
156 | #if ENABLE(INTERPRETER) | |
157 | if (!m_jitCodeForCall) | |
158 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock)); | |
159 | else | |
160 | #endif | |
161 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size()); | |
162 | #else | |
163 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock)); | |
164 | #endif | |
165 | ||
f9bf01c6 A |
166 | return 0; |
167 | } | |
168 | ||
14957cd0 | 169 | void EvalExecutable::visitChildren(SlotVisitor& visitor) |
f9bf01c6 | 170 | { |
14957cd0 A |
171 | ASSERT_GC_OBJECT_INHERITS(this, &s_info); |
172 | COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag); | |
173 | ASSERT(structure()->typeInfo().overridesVisitChildren()); | |
174 | ScriptExecutable::visitChildren(visitor); | |
175 | if (m_evalCodeBlock) | |
176 | m_evalCodeBlock->visitAggregate(visitor); | |
f9bf01c6 A |
177 | } |
178 | ||
14957cd0 | 179 | void EvalExecutable::unlinkCalls() |
f9bf01c6 | 180 | { |
14957cd0 A |
181 | #if ENABLE(JIT) |
182 | if (!m_jitCodeForCall) | |
183 | return; | |
184 | ASSERT(m_evalCodeBlock); | |
185 | m_evalCodeBlock->unlinkCalls(); | |
186 | #endif | |
187 | } | |
f9bf01c6 | 188 | |
14957cd0 A |
189 | JSObject* ProgramExecutable::checkSyntax(ExecState* exec) |
190 | { | |
191 | JSObject* exception = 0; | |
192 | JSGlobalData* globalData = &exec->globalData(); | |
193 | JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject(); | |
194 | RefPtr<ProgramNode> programNode = globalData->parser->parse<ProgramNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, JSParseNormal, &exception); | |
195 | if (programNode) | |
196 | return 0; | |
197 | ASSERT(exception); | |
198 | return exception; | |
f9bf01c6 A |
199 | } |
200 | ||
14957cd0 | 201 | JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode) |
f9bf01c6 | 202 | { |
14957cd0 | 203 | ASSERT(!m_programCodeBlock); |
f9bf01c6 | 204 | |
14957cd0 A |
205 | JSObject* exception = 0; |
206 | JSGlobalData* globalData = &exec->globalData(); | |
207 | JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject(); | |
208 | RefPtr<ProgramNode> programNode = globalData->parser->parse<ProgramNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, &exception); | |
209 | if (!programNode) { | |
210 | ASSERT(exception); | |
211 | return exception; | |
212 | } | |
213 | recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine()); | |
f9bf01c6 | 214 | |
14957cd0 A |
215 | JSGlobalObject* globalObject = scopeChainNode->globalObject.get(); |
216 | ||
217 | m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider())); | |
218 | OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get()))); | |
219 | if ((exception = generator->generate())) { | |
220 | m_programCodeBlock.clear(); | |
221 | programNode->destroyData(); | |
222 | return exception; | |
223 | } | |
f9bf01c6 | 224 | |
14957cd0 | 225 | programNode->destroyData(); |
f9bf01c6 A |
226 | |
227 | #if ENABLE(JIT) | |
14957cd0 A |
228 | if (exec->globalData().canUseJIT()) { |
229 | m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_programCodeBlock.get()); | |
230 | #if !ENABLE(OPCODE_SAMPLING) | |
231 | if (!BytecodeGenerator::dumpsGeneratedCode()) | |
232 | m_programCodeBlock->discardBytecode(); | |
233 | #endif | |
234 | } | |
235 | #endif | |
f9bf01c6 | 236 | |
14957cd0 | 237 | #if ENABLE(JIT) |
4e4e5a6f | 238 | #if ENABLE(INTERPRETER) |
14957cd0 A |
239 | if (!m_jitCodeForCall) |
240 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock)); | |
241 | else | |
4e4e5a6f | 242 | #endif |
14957cd0 A |
243 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size()); |
244 | #else | |
245 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock)); | |
f9bf01c6 | 246 | #endif |
14957cd0 A |
247 | |
248 | return 0; | |
f9bf01c6 A |
249 | } |
250 | ||
14957cd0 | 251 | void ProgramExecutable::unlinkCalls() |
f9bf01c6 | 252 | { |
14957cd0 A |
253 | #if ENABLE(JIT) |
254 | if (!m_jitCodeForCall) | |
255 | return; | |
256 | ASSERT(m_programCodeBlock); | |
257 | m_programCodeBlock->unlinkCalls(); | |
f9bf01c6 A |
258 | #endif |
259 | } | |
260 | ||
14957cd0 A |
261 | #if ENABLE(JIT) |
262 | static bool tryDFGCompile(JSGlobalData* globalData, CodeBlock* codeBlock, JITCode& jitCode, MacroAssemblerCodePtr& jitCodeWithArityCheck) | |
f9bf01c6 | 263 | { |
14957cd0 A |
264 | #if ENABLE(DFG_JIT) |
265 | #if ENABLE(DFG_JIT_RESTRICTIONS) | |
266 | // FIXME: No flow control yet supported, don't bother scanning the bytecode if there are any jump targets. | |
267 | // FIXME: temporarily disable property accesses until we fix regressions. | |
268 | if (codeBlock->numberOfJumpTargets() || codeBlock->numberOfStructureStubInfos()) | |
269 | return false; | |
4e4e5a6f | 270 | #endif |
f9bf01c6 | 271 | |
14957cd0 A |
272 | DFG::Graph dfg(codeBlock->m_numParameters, codeBlock->m_numVars); |
273 | if (!parse(dfg, globalData, codeBlock)) | |
274 | return false; | |
275 | ||
276 | DFG::JITCompiler dataFlowJIT(globalData, dfg, codeBlock); | |
277 | dataFlowJIT.compileFunction(jitCode, jitCodeWithArityCheck); | |
278 | return true; | |
279 | #else | |
280 | UNUSED_PARAM(globalData); | |
281 | UNUSED_PARAM(codeBlock); | |
282 | UNUSED_PARAM(jitCode); | |
283 | UNUSED_PARAM(jitCodeWithArityCheck); | |
284 | return false; | |
f9bf01c6 A |
285 | #endif |
286 | } | |
f9bf01c6 A |
287 | #endif |
288 | ||
14957cd0 | 289 | void ProgramExecutable::visitChildren(SlotVisitor& visitor) |
f9bf01c6 | 290 | { |
14957cd0 A |
291 | ASSERT_GC_OBJECT_INHERITS(this, &s_info); |
292 | COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag); | |
293 | ASSERT(structure()->typeInfo().overridesVisitChildren()); | |
294 | ScriptExecutable::visitChildren(visitor); | |
295 | if (m_programCodeBlock) | |
296 | m_programCodeBlock->visitAggregate(visitor); | |
f9bf01c6 A |
297 | } |
298 | ||
14957cd0 | 299 | JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode) |
f9bf01c6 | 300 | { |
14957cd0 A |
301 | JSObject* exception = 0; |
302 | JSGlobalData* globalData = scopeChainNode->globalData; | |
303 | RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(exec->lexicalGlobalObject(), 0, 0, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, &exception); | |
304 | if (!body) { | |
305 | ASSERT(exception); | |
306 | return exception; | |
307 | } | |
f9bf01c6 | 308 | if (m_forceUsesArguments) |
14957cd0 A |
309 | body->setUsesArguments(); |
310 | body->finishParsing(m_parameters, m_name); | |
311 | recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine()); | |
f9bf01c6 | 312 | |
14957cd0 | 313 | JSGlobalObject* globalObject = scopeChainNode->globalObject.get(); |
f9bf01c6 | 314 | |
14957cd0 A |
315 | ASSERT(!m_codeBlockForCall); |
316 | m_codeBlockForCall = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), false)); | |
317 | OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, m_codeBlockForCall->symbolTable(), m_codeBlockForCall.get()))); | |
318 | if ((exception = generator->generate())) { | |
319 | m_codeBlockForCall.clear(); | |
320 | body->destroyData(); | |
321 | return exception; | |
322 | } | |
f9bf01c6 | 323 | |
14957cd0 A |
324 | m_numParametersForCall = m_codeBlockForCall->m_numParameters; |
325 | ASSERT(m_numParametersForCall); | |
326 | m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars; | |
327 | m_symbolTable = m_codeBlockForCall->sharedSymbolTable(); | |
f9bf01c6 | 328 | |
14957cd0 | 329 | body->destroyData(); |
f9bf01c6 A |
330 | |
331 | #if ENABLE(JIT) | |
14957cd0 A |
332 | if (exec->globalData().canUseJIT()) { |
333 | bool dfgCompiled = tryDFGCompile(&exec->globalData(), m_codeBlockForCall.get(), m_jitCodeForCall, m_jitCodeForCallWithArityCheck); | |
334 | if (!dfgCompiled) | |
335 | m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_codeBlockForCall.get(), &m_jitCodeForCallWithArityCheck); | |
336 | ||
337 | #if !ENABLE(OPCODE_SAMPLING) | |
338 | if (!BytecodeGenerator::dumpsGeneratedCode()) | |
339 | m_codeBlockForCall->discardBytecode(); | |
4e4e5a6f | 340 | #endif |
4e4e5a6f | 341 | } |
f9bf01c6 A |
342 | #endif |
343 | ||
14957cd0 A |
344 | #if ENABLE(JIT) |
345 | #if ENABLE(INTERPRETER) | |
346 | if (!m_jitCodeForCall) | |
347 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall)); | |
348 | else | |
349 | #endif | |
350 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size()); | |
351 | #else | |
352 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall)); | |
353 | #endif | |
f9bf01c6 | 354 | |
14957cd0 | 355 | return 0; |
f9bf01c6 A |
356 | } |
357 | ||
14957cd0 | 358 | JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode) |
f9bf01c6 | 359 | { |
14957cd0 A |
360 | JSObject* exception = 0; |
361 | JSGlobalData* globalData = scopeChainNode->globalData; | |
362 | RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(exec->lexicalGlobalObject(), 0, 0, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, &exception); | |
363 | if (!body) { | |
364 | ASSERT(exception); | |
365 | return exception; | |
366 | } | |
367 | if (m_forceUsesArguments) | |
368 | body->setUsesArguments(); | |
369 | body->finishParsing(m_parameters, m_name); | |
370 | recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine()); | |
f9bf01c6 | 371 | |
14957cd0 | 372 | JSGlobalObject* globalObject = scopeChainNode->globalObject.get(); |
f9bf01c6 | 373 | |
14957cd0 A |
374 | ASSERT(!m_codeBlockForConstruct); |
375 | m_codeBlockForConstruct = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), true)); | |
376 | OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, m_codeBlockForConstruct->symbolTable(), m_codeBlockForConstruct.get()))); | |
377 | if ((exception = generator->generate())) { | |
378 | m_codeBlockForConstruct.clear(); | |
379 | body->destroyData(); | |
380 | return exception; | |
381 | } | |
f9bf01c6 | 382 | |
14957cd0 A |
383 | m_numParametersForConstruct = m_codeBlockForConstruct->m_numParameters; |
384 | ASSERT(m_numParametersForConstruct); | |
385 | m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars; | |
386 | m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable(); | |
f9bf01c6 | 387 | |
14957cd0 | 388 | body->destroyData(); |
f9bf01c6 A |
389 | |
390 | #if ENABLE(JIT) | |
14957cd0 A |
391 | if (exec->globalData().canUseJIT()) { |
392 | m_jitCodeForConstruct = JIT::compile(scopeChainNode->globalData, m_codeBlockForConstruct.get(), &m_jitCodeForConstructWithArityCheck); | |
393 | #if !ENABLE(OPCODE_SAMPLING) | |
394 | if (!BytecodeGenerator::dumpsGeneratedCode()) | |
395 | m_codeBlockForConstruct->discardBytecode(); | |
4e4e5a6f | 396 | #endif |
4e4e5a6f | 397 | } |
f9bf01c6 A |
398 | #endif |
399 | ||
14957cd0 A |
400 | #if ENABLE(JIT) |
401 | #if ENABLE(INTERPRETER) | |
402 | if (!m_jitCodeForConstruct) | |
403 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct)); | |
404 | else | |
405 | #endif | |
406 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size()); | |
407 | #else | |
408 | Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct)); | |
409 | #endif | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | void FunctionExecutable::visitChildren(SlotVisitor& visitor) | |
415 | { | |
416 | ASSERT_GC_OBJECT_INHERITS(this, &s_info); | |
417 | COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag); | |
418 | ASSERT(structure()->typeInfo().overridesVisitChildren()); | |
419 | ScriptExecutable::visitChildren(visitor); | |
420 | if (m_codeBlockForCall) | |
421 | m_codeBlockForCall->visitAggregate(visitor); | |
422 | if (m_codeBlockForConstruct) | |
423 | m_codeBlockForConstruct->visitAggregate(visitor); | |
f9bf01c6 A |
424 | } |
425 | ||
14957cd0 | 426 | void FunctionExecutable::discardCode() |
f9bf01c6 | 427 | { |
f9bf01c6 | 428 | #if ENABLE(JIT) |
14957cd0 A |
429 | // These first two checks are to handle the rare case where |
430 | // we are trying to evict code for a function during its | |
431 | // codegen. | |
432 | if (!m_jitCodeForCall && m_codeBlockForCall) | |
433 | return; | |
434 | if (!m_jitCodeForConstruct && m_codeBlockForConstruct) | |
435 | return; | |
436 | m_jitCodeForCall = JITCode(); | |
437 | m_jitCodeForConstruct = JITCode(); | |
438 | m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr(); | |
439 | m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr(); | |
f9bf01c6 | 440 | #endif |
14957cd0 A |
441 | if (m_codeBlockForCall) |
442 | m_codeBlockForCall->clearEvalCache(); | |
443 | m_codeBlockForCall.clear(); | |
444 | if (m_codeBlockForConstruct) | |
445 | m_codeBlockForConstruct->clearEvalCache(); | |
446 | m_codeBlockForConstruct.clear(); | |
447 | m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED; | |
448 | m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED; | |
449 | ||
f9bf01c6 A |
450 | } |
451 | ||
14957cd0 | 452 | void FunctionExecutable::unlinkCalls() |
f9bf01c6 | 453 | { |
14957cd0 A |
454 | #if ENABLE(JIT) |
455 | if (!!m_jitCodeForCall) { | |
456 | ASSERT(m_codeBlockForCall); | |
457 | m_codeBlockForCall->unlinkCalls(); | |
458 | } | |
459 | if (!!m_jitCodeForConstruct) { | |
460 | ASSERT(m_codeBlockForConstruct); | |
461 | m_codeBlockForConstruct->unlinkCalls(); | |
462 | } | |
463 | #endif | |
464 | } | |
465 | ||
466 | FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception) | |
467 | { | |
468 | JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject(); | |
469 | RefPtr<ProgramNode> program = exec->globalData().parser->parse<ProgramNode>(lexicalGlobalObject, debugger, exec, source, 0, JSParseNormal, exception); | |
470 | if (!program) { | |
471 | ASSERT(*exception); | |
f9bf01c6 | 472 | return 0; |
14957cd0 | 473 | } |
f9bf01c6 | 474 | |
14957cd0 | 475 | // Uses of this function that would not result in a single function expression are invalid. |
f9bf01c6 A |
476 | StatementNode* exprStatement = program->singleStatement(); |
477 | ASSERT(exprStatement); | |
478 | ASSERT(exprStatement->isExprStatement()); | |
f9bf01c6 A |
479 | ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr(); |
480 | ASSERT(funcExpr); | |
481 | ASSERT(funcExpr->isFuncExprNode()); | |
f9bf01c6 A |
482 | FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body(); |
483 | ASSERT(body); | |
14957cd0 A |
484 | |
485 | return FunctionExecutable::create(&exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine()); | |
f9bf01c6 A |
486 | } |
487 | ||
488 | UString FunctionExecutable::paramString() const | |
489 | { | |
490 | FunctionParameters& parameters = *m_parameters; | |
14957cd0 | 491 | UStringBuilder builder; |
f9bf01c6 A |
492 | for (size_t pos = 0; pos < parameters.size(); ++pos) { |
493 | if (!builder.isEmpty()) | |
494 | builder.append(", "); | |
495 | builder.append(parameters[pos].ustring()); | |
496 | } | |
14957cd0 | 497 | return builder.toUString(); |
f9bf01c6 A |
498 | } |
499 | ||
14957cd0 | 500 | } |