blob: cf9839951f5381a6f6fcb0d186fbf437c7e2cee2 [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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
9using v8::Context;
10using v8::Function;
11using v8::Handle;
12using v8::HandleScope;
13using v8::Isolate;
14using v8::Local;
15using v8::Object;
16using v8::Script;
17using v8::String;
18using v8::Value;
19
20namespace gin {
21
22RunnerDelegate::~RunnerDelegate() {
23}
24
25Runner::Runner(RunnerDelegate* delegate, Isolate* isolate)
26 : delegate_(delegate),
27 isolate_(isolate) {
28 HandleScope handle_scope(isolate_);
29 context_.Reset(isolate_, Context::New(isolate_));
30}
31
32Runner::~Runner() {
33 // TODO(abarth): Figure out how to set kResetInDestructor to true.
34 context_.Reset();
35}
36
37void Runner::Run(Handle<Script> script) {
38 script->Run();
39 Handle<Function> main = GetMain();
40 if (main.IsEmpty())
41 return;
42 Handle<Value> argv[] = { delegate_->CreateRootObject(this) };
43 main->Call(global(), 1, argv);
44}
45
46v8::Handle<v8::Function> Runner::GetMain() {
47 Handle<Value> property = global()->Get(StringToV8(isolate_, "main"));
48 if (property.IsEmpty())
49 return v8::Handle<v8::Function>();
50 Handle<Function> main;
51 if (!ConvertFromV8(property, &main))
52 return v8::Handle<v8::Function>();
53 return main;
54}
55
56Runner::Scope::Scope(Runner* runner)
57 : handle_scope_(runner->isolate_),
58 scope_(Local<Context>::New(runner->isolate_, runner->context_)) {
59}
60
61Runner::Scope::~Scope() {
62}
63
64} // namespace gin