Update V8 to version 4.6.72.
[chromium-blink-merge.git] / gin / shell_runner.cc
blob0e534e4af027ca1cacb0e950ee0d6167dae5253e
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
3 // found in the LICENSE file.
5 #include "gin/shell_runner.h"
7 #include "gin/converter.h"
8 #include "gin/modules/module_registry.h"
9 #include "gin/per_context_data.h"
10 #include "gin/public/context_holder.h"
11 #include "gin/try_catch.h"
13 using v8::Context;
14 using v8::HandleScope;
15 using v8::Isolate;
16 using v8::Object;
17 using v8::ObjectTemplate;
18 using v8::Script;
20 namespace gin {
22 ShellRunnerDelegate::ShellRunnerDelegate() {
25 ShellRunnerDelegate::~ShellRunnerDelegate() {
28 v8::Local<ObjectTemplate> ShellRunnerDelegate::GetGlobalTemplate(
29 ShellRunner* runner,
30 v8::Isolate* isolate) {
31 return v8::Local<ObjectTemplate>();
34 void ShellRunnerDelegate::DidCreateContext(ShellRunner* runner) {
37 void ShellRunnerDelegate::WillRunScript(ShellRunner* runner) {
40 void ShellRunnerDelegate::DidRunScript(ShellRunner* runner) {
43 void ShellRunnerDelegate::UnhandledException(ShellRunner* runner,
44 TryCatch& try_catch) {
45 CHECK(false) << try_catch.GetStackTrace();
48 ShellRunner::ShellRunner(ShellRunnerDelegate* delegate, Isolate* isolate)
49 : delegate_(delegate) {
50 v8::Isolate::Scope isolate_scope(isolate);
51 HandleScope handle_scope(isolate);
52 v8::Local<v8::Context> context =
53 Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this, isolate));
55 context_holder_.reset(new ContextHolder(isolate));
56 context_holder_->SetContext(context);
57 PerContextData::From(context)->set_runner(this);
59 v8::Context::Scope scope(context);
60 delegate_->DidCreateContext(this);
63 ShellRunner::~ShellRunner() {
66 void ShellRunner::Run(const std::string& source,
67 const std::string& resource_name) {
68 v8::Isolate* isolate = GetContextHolder()->isolate();
69 TryCatch try_catch(isolate);
70 v8::ScriptOrigin origin(StringToV8(isolate, resource_name));
71 auto maybe_script = Script::Compile(GetContextHolder()->context(),
72 StringToV8(isolate, source), &origin);
73 v8::Local<Script> script;
74 if (!maybe_script.ToLocal(&script)) {
75 delegate_->UnhandledException(this, try_catch);
76 return;
79 Run(script);
82 v8::Local<v8::Value> ShellRunner::Call(v8::Local<v8::Function> function,
83 v8::Local<v8::Value> receiver,
84 int argc,
85 v8::Local<v8::Value> argv[]) {
86 TryCatch try_catch(GetContextHolder()->isolate());
87 delegate_->WillRunScript(this);
89 auto maybe_result =
90 function->Call(GetContextHolder()->context(), receiver, argc, argv);
92 delegate_->DidRunScript(this);
93 v8::Local<v8::Value> result;
94 if (!maybe_result.ToLocal(&result))
95 delegate_->UnhandledException(this, try_catch);
97 return result;
100 ContextHolder* ShellRunner::GetContextHolder() {
101 return context_holder_.get();
104 void ShellRunner::Run(v8::Local<Script> script) {
105 TryCatch try_catch(GetContextHolder()->isolate());
106 delegate_->WillRunScript(this);
108 auto maybe = script->Run(GetContextHolder()->context());
110 delegate_->DidRunScript(this);
111 v8::Local<v8::Value> result;
112 if (!maybe.ToLocal(&result)) {
113 delegate_->UnhandledException(this, try_catch);
117 } // namespace gin