OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/shell_runner.h" | 5 #include "gin/shell_runner.h" |
6 | 6 |
7 #include "gin/converter.h" | 7 #include "gin/converter.h" |
8 #include "gin/modules/module_registry.h" | 8 #include "gin/modules/module_registry.h" |
9 #include "gin/per_context_data.h" | 9 #include "gin/per_context_data.h" |
10 #include "gin/public/context_holder.h" | 10 #include "gin/public/context_holder.h" |
11 #include "gin/try_catch.h" | 11 #include "gin/try_catch.h" |
12 | 12 |
13 using v8::Context; | 13 using v8::Context; |
14 using v8::HandleScope; | 14 using v8::HandleScope; |
15 using v8::Isolate; | 15 using v8::Isolate; |
16 using v8::Object; | 16 using v8::Object; |
17 using v8::ObjectTemplate; | 17 using v8::ObjectTemplate; |
18 using v8::Script; | 18 using v8::Script; |
19 | 19 |
20 namespace gin { | 20 namespace gin { |
21 | 21 |
22 ShellRunnerDelegate::ShellRunnerDelegate() { | 22 ShellRunnerDelegate::ShellRunnerDelegate() { |
23 } | 23 } |
24 | 24 |
25 ShellRunnerDelegate::~ShellRunnerDelegate() { | 25 ShellRunnerDelegate::~ShellRunnerDelegate() { |
26 } | 26 } |
27 | 27 |
28 v8::Handle<ObjectTemplate> ShellRunnerDelegate::GetGlobalTemplate( | 28 v8::Local<ObjectTemplate> ShellRunnerDelegate::GetGlobalTemplate( |
29 ShellRunner* runner, | 29 ShellRunner* runner, |
30 v8::Isolate* isolate) { | 30 v8::Isolate* isolate) { |
31 return v8::Handle<ObjectTemplate>(); | 31 return v8::Local<ObjectTemplate>(); |
32 } | 32 } |
33 | 33 |
34 void ShellRunnerDelegate::DidCreateContext(ShellRunner* runner) { | 34 void ShellRunnerDelegate::DidCreateContext(ShellRunner* runner) { |
35 } | 35 } |
36 | 36 |
37 void ShellRunnerDelegate::WillRunScript(ShellRunner* runner) { | 37 void ShellRunnerDelegate::WillRunScript(ShellRunner* runner) { |
38 } | 38 } |
39 | 39 |
40 void ShellRunnerDelegate::DidRunScript(ShellRunner* runner) { | 40 void ShellRunnerDelegate::DidRunScript(ShellRunner* runner) { |
41 } | 41 } |
42 | 42 |
43 void ShellRunnerDelegate::UnhandledException(ShellRunner* runner, | 43 void ShellRunnerDelegate::UnhandledException(ShellRunner* runner, |
44 TryCatch& try_catch) { | 44 TryCatch& try_catch) { |
45 CHECK(false) << try_catch.GetStackTrace(); | 45 CHECK(false) << try_catch.GetStackTrace(); |
46 } | 46 } |
47 | 47 |
48 ShellRunner::ShellRunner(ShellRunnerDelegate* delegate, Isolate* isolate) | 48 ShellRunner::ShellRunner(ShellRunnerDelegate* delegate, Isolate* isolate) |
49 : delegate_(delegate) { | 49 : delegate_(delegate) { |
50 v8::Isolate::Scope isolate_scope(isolate); | 50 v8::Isolate::Scope isolate_scope(isolate); |
51 HandleScope handle_scope(isolate); | 51 HandleScope handle_scope(isolate); |
52 v8::Handle<v8::Context> context = | 52 v8::Local<v8::Context> context = |
53 Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this, isolate)); | 53 Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this, isolate)); |
54 | 54 |
55 context_holder_.reset(new ContextHolder(isolate)); | 55 context_holder_.reset(new ContextHolder(isolate)); |
56 context_holder_->SetContext(context); | 56 context_holder_->SetContext(context); |
57 PerContextData::From(context)->set_runner(this); | 57 PerContextData::From(context)->set_runner(this); |
58 | 58 |
59 v8::Context::Scope scope(context); | 59 v8::Context::Scope scope(context); |
60 delegate_->DidCreateContext(this); | 60 delegate_->DidCreateContext(this); |
61 } | 61 } |
62 | 62 |
63 ShellRunner::~ShellRunner() { | 63 ShellRunner::~ShellRunner() { |
64 } | 64 } |
65 | 65 |
66 void ShellRunner::Run(const std::string& source, | 66 void ShellRunner::Run(const std::string& source, |
67 const std::string& resource_name) { | 67 const std::string& resource_name) { |
68 TryCatch try_catch; | 68 TryCatch try_catch; |
69 v8::Isolate* isolate = GetContextHolder()->isolate(); | 69 v8::Isolate* isolate = GetContextHolder()->isolate(); |
70 v8::Handle<Script> script = Script::Compile( | 70 v8::Local<Script> script = Script::Compile( |
71 StringToV8(isolate, source), StringToV8(isolate, resource_name)); | 71 StringToV8(isolate, source), StringToV8(isolate, resource_name)); |
72 if (try_catch.HasCaught()) { | 72 if (try_catch.HasCaught()) { |
73 delegate_->UnhandledException(this, try_catch); | 73 delegate_->UnhandledException(this, try_catch); |
74 return; | 74 return; |
75 } | 75 } |
76 | 76 |
77 Run(script); | 77 Run(script); |
78 } | 78 } |
79 | 79 |
80 v8::Handle<v8::Value> ShellRunner::Call(v8::Handle<v8::Function> function, | 80 v8::Local<v8::Value> ShellRunner::Call(v8::Local<v8::Function> function, |
81 v8::Handle<v8::Value> receiver, | 81 v8::Local<v8::Value> receiver, |
82 int argc, | 82 int argc, |
83 v8::Handle<v8::Value> argv[]) { | 83 v8::Local<v8::Value> argv[]) { |
84 TryCatch try_catch; | 84 TryCatch try_catch; |
85 delegate_->WillRunScript(this); | 85 delegate_->WillRunScript(this); |
86 | 86 |
87 v8::Handle<v8::Value> result = function->Call(receiver, argc, argv); | 87 v8::Local<v8::Value> result = function->Call(receiver, argc, argv); |
88 | 88 |
89 delegate_->DidRunScript(this); | 89 delegate_->DidRunScript(this); |
90 if (try_catch.HasCaught()) | 90 if (try_catch.HasCaught()) |
91 delegate_->UnhandledException(this, try_catch); | 91 delegate_->UnhandledException(this, try_catch); |
92 | 92 |
93 return result; | 93 return result; |
94 } | 94 } |
95 | 95 |
96 ContextHolder* ShellRunner::GetContextHolder() { | 96 ContextHolder* ShellRunner::GetContextHolder() { |
97 return context_holder_.get(); | 97 return context_holder_.get(); |
98 } | 98 } |
99 | 99 |
100 void ShellRunner::Run(v8::Handle<Script> script) { | 100 void ShellRunner::Run(v8::Local<Script> script) { |
101 TryCatch try_catch; | 101 TryCatch try_catch; |
102 delegate_->WillRunScript(this); | 102 delegate_->WillRunScript(this); |
103 | 103 |
104 script->Run(); | 104 script->Run(); |
105 | 105 |
106 delegate_->DidRunScript(this); | 106 delegate_->DidRunScript(this); |
107 if (try_catch.HasCaught()) { | 107 if (try_catch.HasCaught()) { |
108 delegate_->UnhandledException(this, try_catch); | 108 delegate_->UnhandledException(this, try_catch); |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 } // namespace gin | 112 } // namespace gin |
OLD | NEW |