Revert of https://codereview.chromium.org/94133003/
[chromium-blink-merge.git] / gin / runner.cc
blob37e16a09c748ae1c33bf963d22729e4061b99860
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
3 // found in the LICENSE file.
5 #include "gin/runner.h"
7 #include "gin/converter.h"
8 #include "gin/per_context_data.h"
9 #include "gin/try_catch.h"
11 using v8::Context;
12 using v8::HandleScope;
13 using v8::Isolate;
14 using v8::Object;
15 using v8::ObjectTemplate;
16 using v8::Script;
18 namespace gin {
20 RunnerDelegate::RunnerDelegate() {
23 RunnerDelegate::~RunnerDelegate() {
26 v8::Handle<ObjectTemplate> RunnerDelegate::GetGlobalTemplate(Runner* runner) {
27 return v8::Handle<ObjectTemplate>();
30 void RunnerDelegate::DidCreateContext(Runner* runner) {
33 void RunnerDelegate::WillRunScript(Runner* runner) {
36 void RunnerDelegate::DidRunScript(Runner* runner) {
39 void RunnerDelegate::UnhandledException(Runner* runner, TryCatch& try_catch) {
42 Runner::Runner(RunnerDelegate* delegate, Isolate* isolate)
43 : ContextHolder(isolate),
44 delegate_(delegate),
45 weak_factory_(this) {
46 v8::Isolate::Scope isolate_scope(isolate);
47 HandleScope handle_scope(isolate);
48 v8::Handle<v8::Context> context =
49 Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this));
51 SetContext(context);
52 PerContextData::From(context)->set_runner(this);
54 v8::Context::Scope scope(context);
55 delegate_->DidCreateContext(this);
58 Runner::~Runner() {
61 void Runner::Run(const std::string& source, const std::string& resource_name) {
62 Run(Script::New(StringToV8(isolate(), source),
63 StringToV8(isolate(), resource_name)));
66 void Runner::Run(v8::Handle<Script> script) {
67 TryCatch try_catch;
68 delegate_->WillRunScript(this);
70 script->Run();
72 delegate_->DidRunScript(this);
73 if (try_catch.HasCaught())
74 delegate_->UnhandledException(this, try_catch);
77 v8::Handle<v8::Value> Runner::Call(v8::Handle<v8::Function> function,
78 v8::Handle<v8::Value> receiver,
79 int argc,
80 v8::Handle<v8::Value> argv[]) {
81 TryCatch try_catch;
82 delegate_->WillRunScript(this);
84 v8::Handle<v8::Value> result = function->Call(receiver, argc, argv);
86 delegate_->DidRunScript(this);
87 if (try_catch.HasCaught())
88 delegate_->UnhandledException(this, try_catch);
90 return result;
93 Runner::Scope::Scope(Runner* runner)
94 : isolate_scope_(runner->isolate()),
95 handle_scope_(runner->isolate()),
96 scope_(runner->context()) {
99 Runner::Scope::~Scope() {
102 } // namespace gin