OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 namespace v8 { | 36 namespace v8 { |
37 namespace internal { | 37 namespace internal { |
38 | 38 |
39 class ScriptDataImpl; | 39 class ScriptDataImpl; |
40 | 40 |
41 // CompilationInfo encapsulates some information known at compile time. It | 41 // CompilationInfo encapsulates some information known at compile time. It |
42 // is constructed based on the resources available at compile-time. | 42 // is constructed based on the resources available at compile-time. |
43 class CompilationInfo BASE_EMBEDDED { | 43 class CompilationInfo BASE_EMBEDDED { |
44 public: | 44 public: |
45 virtual ~CompilationInfo() {} | 45 explicit CompilationInfo(Handle<Script> script); |
| 46 explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info); |
| 47 explicit CompilationInfo(Handle<JSFunction> closure); |
46 | 48 |
47 // Dispatched behavior. | 49 bool is_lazy() const { return (flags_ & IsLazy::mask()) != 0; } |
48 virtual Handle<SharedFunctionInfo> shared_info() const = 0; | 50 bool is_eval() const { return (flags_ & IsEval::mask()) != 0; } |
| 51 bool is_global() const { return (flags_ & IsGlobal::mask()) != 0; } |
| 52 bool is_json() const { return (flags_ & IsJson::mask()) != 0; } |
| 53 bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; } |
| 54 FunctionLiteral* function() const { return function_; } |
| 55 Scope* scope() const { return function_->scope(); } |
| 56 Handle<JSFunction> closure() const { return closure_; } |
| 57 Handle<SharedFunctionInfo> shared_info() const { return shared_info_; } |
| 58 Handle<Script> script() const { return script_; } |
| 59 v8::Extension* extension() const { return extension_; } |
| 60 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
49 | 61 |
50 virtual Handle<Script> script() const { | 62 void MarkAsEval() { |
51 return Handle<Script>(Script::cast(shared_info()->script())); | 63 ASSERT(!is_lazy()); |
| 64 flags_ |= IsEval::encode(true); |
| 65 } |
| 66 void MarkAsGlobal() { |
| 67 ASSERT(!is_lazy()); |
| 68 flags_ |= IsGlobal::encode(true); |
| 69 } |
| 70 void MarkAsJson() { |
| 71 ASSERT(!is_lazy()); |
| 72 flags_ |= IsJson::encode(true); |
| 73 } |
| 74 void MarkAsInLoop() { |
| 75 ASSERT(is_lazy()); |
| 76 flags_ |= IsInLoop::encode(true); |
| 77 } |
| 78 void SetFunction(FunctionLiteral* literal) { |
| 79 ASSERT(function_ == NULL); |
| 80 function_ = literal; |
| 81 } |
| 82 void SetExtension(v8::Extension* extension) { |
| 83 ASSERT(!is_lazy()); |
| 84 extension_ = extension; |
| 85 } |
| 86 void SetPreParseData(ScriptDataImpl* pre_parse_data) { |
| 87 ASSERT(!is_lazy()); |
| 88 pre_parse_data_ = pre_parse_data; |
52 } | 89 } |
53 | 90 |
54 virtual Handle<JSFunction> closure() const { | 91 private: |
55 return Handle<JSFunction>::null(); | 92 // Flags using template class BitField<type, start, length>. All are |
56 } | 93 // false by default. |
| 94 // |
| 95 // Compilation is either eager or lazy. |
| 96 class IsLazy: public BitField<bool, 0, 1> {}; |
| 97 // Flags that can be set for eager compilation. |
| 98 class IsEval: public BitField<bool, 1, 1> {}; |
| 99 class IsGlobal: public BitField<bool, 2, 1> {}; |
| 100 class IsJson: public BitField<bool, 3, 1> {}; |
| 101 // Flags that can be set for lazy compilation. |
| 102 class IsInLoop: public BitField<bool, 4, 1> {}; |
57 | 103 |
58 virtual bool is_eval() const { return false; } | 104 unsigned flags_; |
59 | 105 |
60 virtual int loop_nesting() const { return 0; } | 106 // Fields filled in by the compilation pipeline. |
| 107 FunctionLiteral* function_; |
| 108 Scope* scope_; |
61 | 109 |
62 virtual bool has_global_object() const { return false; } | 110 // Possible initial inputs to the compilation process. |
63 virtual GlobalObject* global_object() const { return NULL; } | 111 Handle<JSFunction> closure_; |
| 112 Handle<SharedFunctionInfo> shared_info_; |
| 113 Handle<Script> script_; |
64 | 114 |
65 // There should always be a function literal, but it may be set after | 115 // Fields possibly needed for eager compilation, NULL by default. |
66 // construction (for lazy compilation). | 116 v8::Extension* extension_; |
67 FunctionLiteral* function() { return function_; } | 117 ScriptDataImpl* pre_parse_data_; |
68 void set_function(FunctionLiteral* literal) { function_ = literal; } | |
69 | |
70 // Derived accessors. | |
71 Scope* scope() { return function()->scope(); } | |
72 | |
73 protected: | |
74 CompilationInfo() : function_(NULL) {} | |
75 | |
76 private: | |
77 FunctionLiteral* function_; | |
78 | 118 |
79 DISALLOW_COPY_AND_ASSIGN(CompilationInfo); | 119 DISALLOW_COPY_AND_ASSIGN(CompilationInfo); |
80 }; | 120 }; |
81 | 121 |
82 | 122 |
83 class EagerCompilationInfo: public CompilationInfo { | |
84 public: | |
85 EagerCompilationInfo(Handle<Script> script, bool is_eval) | |
86 : script_(script), is_eval_(is_eval) { | |
87 ASSERT(!script.is_null()); | |
88 } | |
89 | |
90 // Overridden functions from the base class. | |
91 virtual Handle<SharedFunctionInfo> shared_info() const { | |
92 return Handle<SharedFunctionInfo>::null(); | |
93 } | |
94 | |
95 virtual Handle<Script> script() const { return script_; } | |
96 | |
97 virtual bool is_eval() const { return is_eval_; } | |
98 | |
99 private: | |
100 Handle<Script> script_; | |
101 bool is_eval_; | |
102 }; | |
103 | |
104 | |
105 class LazySharedCompilationInfo: public CompilationInfo { | |
106 public: | |
107 explicit LazySharedCompilationInfo(Handle<SharedFunctionInfo> shared_info) | |
108 : shared_info_(shared_info) { | |
109 ASSERT(!shared_info.is_null()); | |
110 } | |
111 | |
112 // Overridden functions from the base class. | |
113 virtual Handle<SharedFunctionInfo> shared_info() const { | |
114 return shared_info_; | |
115 } | |
116 | |
117 private: | |
118 Handle<SharedFunctionInfo> shared_info_; | |
119 }; | |
120 | |
121 | |
122 class LazyFunctionCompilationInfo: public CompilationInfo { | |
123 public: | |
124 LazyFunctionCompilationInfo(Handle<JSFunction> closure, | |
125 int loop_nesting) | |
126 : closure_(closure), loop_nesting_(loop_nesting) { | |
127 ASSERT(!closure.is_null()); | |
128 } | |
129 | |
130 // Overridden functions from the base class. | |
131 virtual Handle<SharedFunctionInfo> shared_info() const { | |
132 return Handle<SharedFunctionInfo>(closure_->shared()); | |
133 } | |
134 | |
135 virtual int loop_nesting() const { return loop_nesting_; } | |
136 | |
137 virtual bool has_global_object() const { | |
138 return closure_->context()->global() != NULL; | |
139 } | |
140 | |
141 virtual GlobalObject* global_object() const { | |
142 return closure_->context()->global(); | |
143 } | |
144 | |
145 private: | |
146 Handle<JSFunction> closure_; | |
147 int loop_nesting_; | |
148 }; | |
149 | |
150 | |
151 // The V8 compiler | 123 // The V8 compiler |
152 // | 124 // |
153 // General strategy: Source code is translated into an anonymous function w/o | 125 // General strategy: Source code is translated into an anonymous function w/o |
154 // parameters which then can be executed. If the source code contains other | 126 // parameters which then can be executed. If the source code contains other |
155 // functions, they will be compiled and allocated as part of the compilation | 127 // functions, they will be compiled and allocated as part of the compilation |
156 // of the source code. | 128 // of the source code. |
157 | 129 |
158 // Please note this interface returns shared function infos. | 130 // Please note this interface returns shared function infos. |
159 // This means you need to call Factory::NewFunctionFromSharedFunctionInfo | 131 // This means you need to call Factory::NewFunctionFromSharedFunctionInfo |
160 // before you have a real function with a context. | 132 // before you have a real function with a context. |
161 | 133 |
162 class Compiler : public AllStatic { | 134 class Compiler : public AllStatic { |
163 public: | 135 public: |
164 enum ValidationState { VALIDATE_JSON, DONT_VALIDATE_JSON }; | 136 enum ValidationState { DONT_VALIDATE_JSON, VALIDATE_JSON }; |
165 | 137 |
166 // All routines return a JSFunction. | 138 // All routines return a JSFunction. |
167 // If an error occurs an exception is raised and | 139 // If an error occurs an exception is raised and |
168 // the return handle contains NULL. | 140 // the return handle contains NULL. |
169 | 141 |
170 // Compile a String source within a context. | 142 // Compile a String source within a context. |
171 static Handle<SharedFunctionInfo> Compile(Handle<String> source, | 143 static Handle<SharedFunctionInfo> Compile(Handle<String> source, |
172 Handle<Object> script_name, | 144 Handle<Object> script_name, |
173 int line_offset, | 145 int line_offset, |
174 int column_offset, | 146 int column_offset, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 FrameElement::ClearConstantList(); | 201 FrameElement::ClearConstantList(); |
230 Result::ClearConstantList(); | 202 Result::ClearConstantList(); |
231 } | 203 } |
232 } | 204 } |
233 }; | 205 }; |
234 | 206 |
235 | 207 |
236 } } // namespace v8::internal | 208 } } // namespace v8::internal |
237 | 209 |
238 #endif // V8_COMPILER_H_ | 210 #endif // V8_COMPILER_H_ |
OLD | NEW |