diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-10 05:00:50 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-10 05:00:50 +0000 |
commit | a22998a223ecdcd926d152baea8a8703bcb57d61 (patch) | |
tree | ccb6b71bcc50b61c6671d955fbc409802f0c8d95 /gin/runner.cc | |
parent | 1cde19eb7e17b16645fb75a50f3250218d53cd17 (diff) | |
download | chromium_src-a22998a223ecdcd926d152baea8a8703bcb57d61.zip chromium_src-a22998a223ecdcd926d152baea8a8703bcb57d61.tar.gz chromium_src-a22998a223ecdcd926d152baea8a8703bcb57d61.tar.bz2 |
This CL introduces a lightweight bindings system for V8 called gin
Unlike the extensions V8 bindings, gin is based on ObjectTemplates rather than
on evaluating script. Unlike the Blink V8 bindings, gin isn't tightly coupled to
Blink. In fact, gin's only link-time dependency is V8. We plan to use gin to build
the V8 bindings for Mojo (see https://codereview.chromium.org/59153005/ for
an example of how they will be used).
In the long term, gin could serve as a basis for both the Blink and the extension
system bindings, but we don't have any immediate plans to pursue that use of
this code.
This code is largely inspired by a lightweight bindings system designed by
Aaron Boodman.
Review URL: https://codereview.chromium.org/67763002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234160 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin/runner.cc')
-rw-r--r-- | gin/runner.cc | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/gin/runner.cc b/gin/runner.cc new file mode 100644 index 0000000..cf98399 --- /dev/null +++ b/gin/runner.cc @@ -0,0 +1,64 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "gin/runner.h" + +#include "gin/converter.h" + +using v8::Context; +using v8::Function; +using v8::Handle; +using v8::HandleScope; +using v8::Isolate; +using v8::Local; +using v8::Object; +using v8::Script; +using v8::String; +using v8::Value; + +namespace gin { + +RunnerDelegate::~RunnerDelegate() { +} + +Runner::Runner(RunnerDelegate* delegate, Isolate* isolate) + : delegate_(delegate), + isolate_(isolate) { + HandleScope handle_scope(isolate_); + context_.Reset(isolate_, Context::New(isolate_)); +} + +Runner::~Runner() { + // TODO(abarth): Figure out how to set kResetInDestructor to true. + context_.Reset(); +} + +void Runner::Run(Handle<Script> script) { + script->Run(); + Handle<Function> main = GetMain(); + if (main.IsEmpty()) + return; + Handle<Value> argv[] = { delegate_->CreateRootObject(this) }; + main->Call(global(), 1, argv); +} + +v8::Handle<v8::Function> Runner::GetMain() { + Handle<Value> property = global()->Get(StringToV8(isolate_, "main")); + if (property.IsEmpty()) + return v8::Handle<v8::Function>(); + Handle<Function> main; + if (!ConvertFromV8(property, &main)) + return v8::Handle<v8::Function>(); + return main; +} + +Runner::Scope::Scope(Runner* runner) + : handle_scope_(runner->isolate_), + scope_(Local<Context>::New(runner->isolate_, runner->context_)) { +} + +Runner::Scope::~Scope() { +} + +} // namespace gin |