• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "gin/runner.h"
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 {
19 
RunnerDelegate()20 RunnerDelegate::RunnerDelegate() {
21 }
22 
~RunnerDelegate()23 RunnerDelegate::~RunnerDelegate() {
24 }
25 
GetGlobalTemplate(Runner * runner)26 v8::Handle<ObjectTemplate> RunnerDelegate::GetGlobalTemplate(Runner* runner) {
27   return v8::Handle<ObjectTemplate>();
28 }
29 
DidCreateContext(Runner * runner)30 void RunnerDelegate::DidCreateContext(Runner* runner) {
31 }
32 
WillRunScript(Runner * runner)33 void RunnerDelegate::WillRunScript(Runner* runner) {
34 }
35 
DidRunScript(Runner * runner)36 void RunnerDelegate::DidRunScript(Runner* runner) {
37 }
38 
UnhandledException(Runner * runner,TryCatch & try_catch)39 void RunnerDelegate::UnhandledException(Runner* runner, TryCatch& try_catch) {
40 }
41 
Runner(RunnerDelegate * delegate,Isolate * isolate)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));
50 
51   SetContext(context);
52   PerContextData::From(context)->set_runner(this);
53 
54   v8::Context::Scope scope(context);
55   delegate_->DidCreateContext(this);
56 }
57 
~Runner()58 Runner::~Runner() {
59 }
60 
Run(const std::string & source,const std::string & resource_name)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)));
64 }
65 
Run(v8::Handle<Script> script)66 void Runner::Run(v8::Handle<Script> script) {
67   TryCatch try_catch;
68   delegate_->WillRunScript(this);
69 
70   script->Run();
71 
72   delegate_->DidRunScript(this);
73   if (try_catch.HasCaught())
74     delegate_->UnhandledException(this, try_catch);
75 }
76 
Call(v8::Handle<v8::Function> function,v8::Handle<v8::Value> receiver,int argc,v8::Handle<v8::Value> argv[])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);
83 
84   v8::Handle<v8::Value> result = function->Call(receiver, argc, argv);
85 
86   delegate_->DidRunScript(this);
87   if (try_catch.HasCaught())
88     delegate_->UnhandledException(this, try_catch);
89 
90   return result;
91 }
92 
Scope(Runner * runner)93 Runner::Scope::Scope(Runner* runner)
94     : isolate_scope_(runner->isolate()),
95       handle_scope_(runner->isolate()),
96       scope_(runner->context()) {
97 }
98 
~Scope()99 Runner::Scope::~Scope() {
100 }
101 
102 }  // namespace gin
103