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