blob: 19499de71ba79a48ad43d4388c7cc9452b210a57 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_COMPILER_H_
29#define V8_COMPILER_H_
30
31#include "frame-element.h"
32#include "parser.h"
33#include "zone.h"
34
35namespace v8 {
36namespace internal {
37
Leon Clarke4515c472010-02-03 11:58:03 +000038// CompilationInfo encapsulates some information known at compile time.
39class CompilationInfo BASE_EMBEDDED {
40 public:
41 CompilationInfo(Handle<SharedFunctionInfo> shared_info,
42 Handle<Object> receiver,
43 int loop_nesting)
44 : shared_info_(shared_info),
45 receiver_(receiver),
46 loop_nesting_(loop_nesting),
47 has_this_properties_(false),
48 has_globals_(false) {
49 }
50
51 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
52
53 bool has_receiver() { return !receiver_.is_null(); }
54 Handle<Object> receiver() { return receiver_; }
55
56 int loop_nesting() { return loop_nesting_; }
57
58 bool has_this_properties() { return has_this_properties_; }
59 void set_has_this_properties(bool flag) { has_this_properties_ = flag; }
60
61 bool has_globals() { return has_globals_; }
62 void set_has_globals(bool flag) { has_globals_ = flag; }
63
64 private:
65 Handle<SharedFunctionInfo> shared_info_;
66 Handle<Object> receiver_;
67 int loop_nesting_;
68 bool has_this_properties_;
69 bool has_globals_;
70};
71
72
Steve Blocka7e24c12009-10-30 11:49:00 +000073// The V8 compiler
74//
75// General strategy: Source code is translated into an anonymous function w/o
76// parameters which then can be executed. If the source code contains other
77// functions, they will be compiled and allocated as part of the compilation
78// of the source code.
79
80// Please note this interface returns function boilerplates.
81// This means you need to call Factory::NewFunctionFromBoilerplate
82// before you have a real function with context.
83
84class Compiler : public AllStatic {
85 public:
86 enum ValidationState { VALIDATE_JSON, DONT_VALIDATE_JSON };
87
88 // All routines return a JSFunction.
89 // If an error occurs an exception is raised and
90 // the return handle contains NULL.
91
92 // Compile a String source within a context.
93 static Handle<JSFunction> Compile(Handle<String> source,
94 Handle<Object> script_name,
95 int line_offset, int column_offset,
96 v8::Extension* extension,
97 ScriptDataImpl* script_Data);
98
99 // Compile a String source within a context for Eval.
100 static Handle<JSFunction> CompileEval(Handle<String> source,
101 Handle<Context> context,
102 bool is_global,
103 ValidationState validation);
104
105 // Compile from function info (used for lazy compilation). Returns
106 // true on success and false if the compilation resulted in a stack
107 // overflow.
Leon Clarke4515c472010-02-03 11:58:03 +0000108 static bool CompileLazy(CompilationInfo* info);
Steve Blockd0582a62009-12-15 09:54:21 +0000109
110 // Compile a function boilerplate object (the function is possibly
111 // lazily compiled). Called recursively from a backend code
112 // generator 'caller' to build the boilerplate.
113 static Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node,
114 Handle<Script> script,
115 AstVisitor* caller);
116
117 // Set the function info for a newly compiled function.
118 static void SetFunctionInfo(Handle<JSFunction> fun,
119 FunctionLiteral* lit,
120 bool is_toplevel,
121 Handle<Script> script);
Steve Blocka7e24c12009-10-30 11:49:00 +0000122};
123
124
125// During compilation we need a global list of handles to constants
126// for frame elements. When the zone gets deleted, we make sure to
127// clear this list of handles as well.
128class CompilationZoneScope : public ZoneScope {
129 public:
130 explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { }
131 virtual ~CompilationZoneScope() {
132 if (ShouldDeleteOnExit()) {
133 FrameElement::ClearConstantList();
134 Result::ClearConstantList();
135 }
136 }
137};
138
139
140} } // namespace v8::internal
141
142#endif // V8_COMPILER_H_