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"
12 using v8::HandleScope
;
15 using v8::ObjectTemplate
;
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
) {
40 CHECK(false) << try_catch
.GetStackTrace();
43 Runner::Runner(RunnerDelegate
* delegate
, Isolate
* isolate
)
44 : ContextHolder(isolate
),
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));
53 PerContextData::From(context
)->set_runner(this);
55 v8::Context::Scope
scope(context
);
56 delegate_
->DidCreateContext(this);
62 void Runner::Run(const std::string
& source
, const std::string
& resource_name
) {
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
);
74 void Runner::Run(v8::Handle
<Script
> script
) {
76 delegate_
->WillRunScript(this);
80 delegate_
->DidRunScript(this);
81 if (try_catch
.HasCaught()) {
82 delegate_
->UnhandledException(this, try_catch
);
86 v8::Handle
<v8::Value
> Runner::Call(v8::Handle
<v8::Function
> function
,
87 v8::Handle
<v8::Value
> receiver
,
89 v8::Handle
<v8::Value
> argv
[]) {
91 delegate_
->WillRunScript(this);
93 v8::Handle
<v8::Value
> result
= function
->Call(receiver
, argc
, argv
);
95 delegate_
->DidRunScript(this);
96 if (try_catch
.HasCaught()) {
97 delegate_
->UnhandledException(this, try_catch
);
103 Runner::Scope::Scope(Runner
* runner
)
104 : isolate_scope_(runner
->isolate()),
105 handle_scope_(runner
->isolate()),
106 scope_(runner
->context()) {
109 Runner::Scope::~Scope() {