OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/runner.h" | 5 #include "gin/runner.h" |
6 | 6 |
7 #include "gin/converter.h" | 7 #include "gin/converter.h" |
8 | 8 |
9 using v8::Context; | 9 using v8::Context; |
10 using v8::Function; | |
11 using v8::Handle; | 10 using v8::Handle; |
12 using v8::HandleScope; | 11 using v8::HandleScope; |
13 using v8::Isolate; | 12 using v8::Isolate; |
14 using v8::Local; | |
15 using v8::Object; | 13 using v8::Object; |
| 14 using v8::ObjectTemplate; |
16 using v8::Script; | 15 using v8::Script; |
17 using v8::String; | |
18 using v8::Value; | |
19 | 16 |
20 namespace gin { | 17 namespace gin { |
21 | 18 |
| 19 RunnerDelegate::RunnerDelegate() { |
| 20 } |
| 21 |
22 RunnerDelegate::~RunnerDelegate() { | 22 RunnerDelegate::~RunnerDelegate() { |
23 } | 23 } |
24 | 24 |
| 25 Handle<ObjectTemplate> RunnerDelegate::GetGlobalTemplate(Runner* runner) { |
| 26 return Handle<ObjectTemplate>(); |
| 27 } |
| 28 |
| 29 void RunnerDelegate::DidCreateContext(Runner* runner) { |
| 30 } |
| 31 |
25 Runner::Runner(RunnerDelegate* delegate, Isolate* isolate) | 32 Runner::Runner(RunnerDelegate* delegate, Isolate* isolate) |
26 : delegate_(delegate), | 33 : ContextHolder(isolate), |
27 isolate_(isolate) { | 34 delegate_(delegate) { |
28 HandleScope handle_scope(isolate_); | 35 HandleScope handle_scope(isolate); |
29 context_.Reset(isolate_, Context::New(isolate_)); | 36 SetContext(Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this))); |
| 37 |
| 38 v8::Context::Scope scope(context()); |
| 39 delegate_->DidCreateContext(this); |
30 } | 40 } |
31 | 41 |
32 Runner::~Runner() { | 42 Runner::~Runner() { |
33 // TODO(abarth): Figure out how to set kResetInDestructor to true. | 43 } |
34 context_.Reset(); | 44 |
| 45 void Runner::Run(const std::string& script) { |
| 46 Run(Script::New(StringToV8(isolate(), script))); |
35 } | 47 } |
36 | 48 |
37 void Runner::Run(Handle<Script> script) { | 49 void Runner::Run(Handle<Script> script) { |
38 script->Run(); | 50 script->Run(); |
39 Handle<Function> main = GetMain(); | |
40 if (main.IsEmpty()) | |
41 return; | |
42 Handle<Value> argv[] = { delegate_->CreateRootObject(this) }; | |
43 main->Call(global(), 1, argv); | |
44 } | |
45 | |
46 v8::Handle<v8::Function> Runner::GetMain() { | |
47 Handle<Value> property = global()->Get(StringToSymbol(isolate_, "main")); | |
48 if (property.IsEmpty()) | |
49 return v8::Handle<v8::Function>(); | |
50 Handle<Function> main; | |
51 if (!ConvertFromV8(property, &main)) | |
52 return v8::Handle<v8::Function>(); | |
53 return main; | |
54 } | 51 } |
55 | 52 |
56 Runner::Scope::Scope(Runner* runner) | 53 Runner::Scope::Scope(Runner* runner) |
57 : handle_scope_(runner->isolate_), | 54 : handle_scope_(runner->isolate()), |
58 scope_(Local<Context>::New(runner->isolate_, runner->context_)) { | 55 scope_(runner->context()) { |
59 } | 56 } |
60 | 57 |
61 Runner::Scope::~Scope() { | 58 Runner::Scope::~Scope() { |
62 } | 59 } |
63 | 60 |
64 } // namespace gin | 61 } // namespace gin |
OLD | NEW |