OLD | NEW |
(Empty) | |
| 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 "mojo/public/bindings/js/v8_runner.h" |
| 6 |
| 7 #include "mojo/public/bindings/js/v8_per_isolate_data.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace js { |
| 11 |
| 12 Runner::Runner(v8::Isolate* isolate) |
| 13 : isolate_(isolate) { |
| 14 v8::HandleScope handle_scope(isolate_); |
| 15 context_.Reset(isolate_, v8::Context::New(isolate_)); |
| 16 } |
| 17 |
| 18 Runner::~Runner() { |
| 19 // TODO(abarth): Figure out how to set kResetInDestructor to true. |
| 20 context_.Reset(); |
| 21 } |
| 22 |
| 23 void Runner::Run(v8::Handle<v8::Script> script) { |
| 24 V8PerIsolateData* data = V8PerIsolateData::From(isolate_); |
| 25 v8::Handle<v8::Object> mojo = data->mojo_template()->NewInstance(); |
| 26 |
| 27 script->Run(); |
| 28 |
| 29 v8::Handle<v8::Context> context = v8::Local<v8::Context>::New(isolate_, |
| 30 context_); |
| 31 v8::Handle<v8::Object> global = context->Global(); |
| 32 v8::Handle<v8::Value> main_value = global->Get(v8::String::New("main")); |
| 33 if (main_value.IsEmpty() || !main_value->IsFunction()) |
| 34 return; |
| 35 v8::Handle<v8::Function> main = v8::Handle<v8::Function>::Cast(main_value); |
| 36 |
| 37 v8::Handle<v8::Value> argv[] = { mojo }; |
| 38 main->Call(global, 1, argv); |
| 39 } |
| 40 |
| 41 Runner::Scope::Scope(Runner* runner) |
| 42 : handle_scope_(runner->isolate_), |
| 43 scope_(v8::Local<v8::Context>::New(runner->isolate_, runner->context_)) { |
| 44 } |
| 45 |
| 46 Runner::Scope::~Scope() { |
| 47 } |
| 48 |
| 49 } // namespace js |
| 50 } // namespace mojo |
OLD | NEW |