[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame^] | 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 | |
| 9 | using v8::Context; |
| 10 | using v8::Function; |
| 11 | using v8::Handle; |
| 12 | using v8::HandleScope; |
| 13 | using v8::Isolate; |
| 14 | using v8::Local; |
| 15 | using v8::Object; |
| 16 | using v8::Script; |
| 17 | using v8::String; |
| 18 | using v8::Value; |
| 19 | |
| 20 | namespace gin { |
| 21 | |
| 22 | RunnerDelegate::~RunnerDelegate() { |
| 23 | } |
| 24 | |
| 25 | Runner::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 | |
| 32 | Runner::~Runner() { |
| 33 | // TODO(abarth): Figure out how to set kResetInDestructor to true. |
| 34 | context_.Reset(); |
| 35 | } |
| 36 | |
| 37 | void 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 | |
| 46 | v8::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 | |
| 56 | Runner::Scope::Scope(Runner* runner) |
| 57 | : handle_scope_(runner->isolate_), |
| 58 | scope_(Local<Context>::New(runner->isolate_, runner->context_)) { |
| 59 | } |
| 60 | |
| 61 | Runner::Scope::~Scope() { |
| 62 | } |
| 63 | |
| 64 | } // namespace gin |