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
) {
42 Runner::Runner(RunnerDelegate
* delegate
, Isolate
* isolate
)
43 : ContextHolder(isolate
),
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));
52 PerContextData::From(context
)->set_runner(this);
54 v8::Context::Scope
scope(context
);
55 delegate_
->DidCreateContext(this);
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
) {
68 delegate_
->WillRunScript(this);
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
,
80 v8::Handle
<v8::Value
> argv
[]) {
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
);
93 Runner::Scope::Scope(Runner
* runner
)
94 : isolate_scope_(runner
->isolate()),
95 handle_scope_(runner
->isolate()),
96 scope_(runner
->context()) {
99 Runner::Scope::~Scope() {