OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 * | 7 * |
8 */ | 8 */ |
9 #include "Global.h" | 9 #include "Global.h" |
10 | 10 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 v8::FunctionTemplate::New(Global::Print)); | 183 v8::FunctionTemplate::New(Global::Print)); |
184 global->Set(v8::String::NewFromUtf8(fIsolate, "setTimeout"), | 184 global->Set(v8::String::NewFromUtf8(fIsolate, "setTimeout"), |
185 v8::FunctionTemplate::New(Global::SetTimeout)); | 185 v8::FunctionTemplate::New(Global::SetTimeout)); |
186 global->Set(v8::String::NewFromUtf8(fIsolate, "inval"), | 186 global->Set(v8::String::NewFromUtf8(fIsolate, "inval"), |
187 v8::FunctionTemplate::New(Global::Inval)); | 187 v8::FunctionTemplate::New(Global::Inval)); |
188 | 188 |
189 | 189 |
190 return Context::New(fIsolate, NULL, global); | 190 return Context::New(fIsolate, NULL, global); |
191 } | 191 } |
192 | 192 |
| 193 void Global::initialize() { |
| 194 // Create a stack-allocated handle scope. |
| 195 HandleScope handleScope(fIsolate); |
| 196 |
| 197 // Create a new context. |
| 198 Handle<Context> context = this->createRootContext(); |
| 199 |
| 200 // Make the context persistent. |
| 201 fContext.Reset(fIsolate, context); |
| 202 } |
| 203 |
193 | 204 |
194 // Creates the root context, parses the script into it, then stores the | 205 // Creates the root context, parses the script into it, then stores the |
195 // context in a global. | 206 // context in a global. |
196 // | 207 // |
197 // TODO(jcgregorio) Currently only handles one script. Need to move | 208 // TODO(jcgregorio) Currently only handles one script. Need to move |
198 // createRootContext to another call that's only done once. | 209 // createRootContext to another call that's only done once. |
199 bool Global::parseScript(const char script[]) { | 210 bool Global::parseScript(const char script[]) { |
200 | 211 |
201 // Create a stack-allocated handle scope. | 212 // Create a stack-allocated handle scope. |
202 HandleScope handleScope(fIsolate); | 213 HandleScope handleScope(fIsolate); |
203 | 214 |
204 printf("Before create context\n"); | 215 // Get the global context. |
205 | 216 Handle<Context> context = this->getContext(); |
206 // Create a new context. | |
207 Handle<Context> context = this->createRootContext(); | |
208 | 217 |
209 // Enter the scope so all operations take place in the scope. | 218 // Enter the scope so all operations take place in the scope. |
210 Context::Scope contextScope(context); | 219 Context::Scope contextScope(context); |
211 | 220 |
212 v8::TryCatch tryCatch; | 221 v8::TryCatch tryCatch; |
213 | 222 |
214 // Compile the source code. | 223 // Compile the source code. |
215 Handle<String> source = String::NewFromUtf8(fIsolate, script); | 224 Handle<String> source = String::NewFromUtf8(fIsolate, script); |
216 printf("Before Compile\n"); | |
217 Handle<Script> compiledScript = Script::Compile(source); | 225 Handle<Script> compiledScript = Script::Compile(source); |
218 printf("After Compile\n"); | |
219 | 226 |
220 if (compiledScript.IsEmpty()) { | 227 if (compiledScript.IsEmpty()) { |
221 // Print errors that happened during compilation. | 228 // Print errors that happened during compilation. |
222 this->reportException(&tryCatch); | 229 this->reportException(&tryCatch); |
223 return false; | 230 return false; |
224 } | 231 } |
225 printf("After Exception.\n"); | |
226 | 232 |
227 // Try running it now to create the onDraw function. | 233 // Try running it now to create the onDraw function. |
228 Handle<Value> result = compiledScript->Run(); | 234 Handle<Value> result = compiledScript->Run(); |
229 | 235 |
230 // Handle any exceptions or output. | 236 // Handle any exceptions or output. |
231 if (result.IsEmpty()) { | 237 if (result.IsEmpty()) { |
232 SkASSERT(tryCatch.HasCaught()); | 238 SkASSERT(tryCatch.HasCaught()); |
233 // Print errors that happened during execution. | 239 // Print errors that happened during execution. |
234 this->reportException(&tryCatch); | 240 this->reportException(&tryCatch); |
235 return false; | 241 return false; |
236 } | 242 } |
237 | 243 |
238 // Also make the context persistent. | |
239 fContext.Reset(fIsolate, context); | |
240 return true; | 244 return true; |
241 } | 245 } |
OLD | NEW |