diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-25 02:19:34 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-25 02:19:34 +0000 |
commit | 0d72f004efa9898b428b9b289a8d63d2427e2828 (patch) | |
tree | f749189620fba034c4e09aa5e64c537eba778e61 /gin | |
parent | 6a36eff422ce0dfaf8bcb75585863c38817d219d (diff) | |
download | chromium_src-0d72f004efa9898b428b9b289a8d63d2427e2828.zip chromium_src-0d72f004efa9898b428b9b289a8d63d2427e2828.tar.gz chromium_src-0d72f004efa9898b428b9b289a8d63d2427e2828.tar.bz2 |
[Mojo] Almost connect mojo_js with hello_world_service
This CL connects mojo_js with hello_world_service. After this
CL, the JavaScript and C++ implementations have reached
parity.
BUG=317398
Review URL: https://codereview.chromium.org/82953004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237018 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin')
-rw-r--r-- | gin/modules/module_registry.cc | 47 | ||||
-rw-r--r-- | gin/modules/module_runner_delegate.cc | 3 | ||||
-rw-r--r-- | gin/modules/module_runner_delegate.h | 3 | ||||
-rw-r--r-- | gin/per_context_data.cc | 3 | ||||
-rw-r--r-- | gin/per_context_data.h | 6 | ||||
-rw-r--r-- | gin/per_isolate_data.cc | 1 | ||||
-rw-r--r-- | gin/runner.cc | 35 | ||||
-rw-r--r-- | gin/runner.h | 9 | ||||
-rw-r--r-- | gin/wrappable.cc | 6 | ||||
-rw-r--r-- | gin/wrappable.h | 2 |
10 files changed, 75 insertions, 40 deletions
diff --git a/gin/modules/module_registry.cc b/gin/modules/module_registry.cc index afb07d3..d868dec 100644 --- a/gin/modules/module_registry.cc +++ b/gin/modules/module_registry.cc @@ -12,13 +12,12 @@ #include "gin/converter.h" #include "gin/per_isolate_data.h" #include "gin/public/wrapper_info.h" -#include "gin/try_catch.h" +#include "gin/runner.h" using v8::Context; using v8::External; using v8::Function; using v8::FunctionTemplate; -using v8::Handle; using v8::Isolate; using v8::Local; using v8::Object; @@ -56,7 +55,7 @@ void Define(const v8::FunctionCallbackInfo<Value>& info) { std::string id; std::vector<std::string> dependencies; - Handle<Value> factory; + v8::Handle<Value> factory; if (args.PeekNext()->IsString()) args.GetNext(&id); @@ -88,7 +87,7 @@ Local<FunctionTemplate> GetDefineTemplate(Isolate* isolate) { return templ; } -Handle<String> GetHiddenValueKey(Isolate* isolate) { +v8::Handle<String> GetHiddenValueKey(Isolate* isolate) { return StringToSymbol(isolate, "::gin::ModuleRegistry"); } @@ -103,15 +102,15 @@ ModuleRegistry::~ModuleRegistry() { } void ModuleRegistry::RegisterGlobals(Isolate* isolate, - Handle<ObjectTemplate> templ) { + v8::Handle<ObjectTemplate> templ) { templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate)); } -ModuleRegistry* ModuleRegistry::From(Handle<Context> context) { +ModuleRegistry* ModuleRegistry::From(v8::Handle<Context> context) { Isolate* isolate = context->GetIsolate(); - Handle<String> key = GetHiddenValueKey(isolate); - Handle<Value> value = context->Global()->GetHiddenValue(key); - Handle<External> external; + v8::Handle<String> key = GetHiddenValueKey(isolate); + v8::Handle<Value> value = context->Global()->GetHiddenValue(key); + v8::Handle<External> external; if (value.IsEmpty() || !ConvertFromV8(value, &external)) { PerContextData* data = PerContextData::From(context); if (!data) @@ -126,7 +125,7 @@ ModuleRegistry* ModuleRegistry::From(Handle<Context> context) { void ModuleRegistry::AddBuiltinModule(Isolate* isolate, const std::string& id, - Handle<ObjectTemplate> templ) { + v8::Handle<ObjectTemplate> templ) { DCHECK(!id.empty()); RegisterModule(isolate, id, templ->NewInstance()); } @@ -138,19 +137,19 @@ void ModuleRegistry::AddPendingModule(Isolate* isolate, void ModuleRegistry::RegisterModule(Isolate* isolate, const std::string& id, - Handle<Value> module) { + v8::Handle<Value> module) { if (id.empty() || module.IsEmpty()) return; unsatisfied_dependencies_.erase(id); available_modules_.insert(id); - Handle<Object> modules = Local<Object>::New(isolate, modules_); + v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); modules->Set(StringToSymbol(isolate, id), module); } -void ModuleRegistry::Detach(Handle<Context> context) { +void ModuleRegistry::Detach(v8::Handle<Context> context) { context->Global()->SetHiddenValue(GetHiddenValueKey(context->GetIsolate()), - Handle<Value>()); + v8::Handle<Value>()); } bool ModuleRegistry::CheckDependencies(PendingModule* pending) { @@ -170,26 +169,22 @@ void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { if (!pending->id.empty() && available_modules_.count(pending->id)) return; // We've already loaded this module. - Handle<Object> modules = Local<Object>::New(isolate, modules_); + v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); - std::vector<Handle<Value> > argv(argc); + std::vector<v8::Handle<Value> > argv(argc); for (uint32_t i = 0; i < argc; ++i) { - Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); + v8::Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); DCHECK(modules->HasOwnProperty(key)); argv[i] = modules->Get(key); } - Handle<Value> module = Local<Value>::New(isolate, pending->factory); + v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory); - Handle<Function> factory; + v8::Handle<Function> factory; if (ConvertFromV8(module, &factory)) { - Handle<Object> global = isolate->GetCurrentContext()->Global(); - { - gin::TryCatch try_catch; - module = factory->Call(global, argc, argv.data()); - if (try_catch.HasCaught()) - return; // TODO(abarth): What should we do with the exception? - } + PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); + Runner* runner = data->runner(); + module = runner->Call(factory, runner->global(), argc, argv.data()); if (pending->id.empty()) ConvertFromV8(factory->GetScriptOrigin().ResourceName(), &pending->id); } diff --git a/gin/modules/module_runner_delegate.cc b/gin/modules/module_runner_delegate.cc index 3f2422c..9e0b762 100644 --- a/gin/modules/module_runner_delegate.cc +++ b/gin/modules/module_runner_delegate.cc @@ -41,8 +41,7 @@ void ModuleRunnerDelegate::DidCreateContext(Runner* runner) { } } -void ModuleRunnerDelegate::DidRunScript(Runner* runner, - v8::Handle<v8::Script> script) { +void ModuleRunnerDelegate::DidRunScript(Runner* runner) { ModuleRegistry* registry = ModuleRegistry::From(runner->context()); registry->AttemptToLoadMoreModules(runner->isolate()); module_provider_.AttempToLoadModules( diff --git a/gin/modules/module_runner_delegate.h b/gin/modules/module_runner_delegate.h index a66695f..efc9761 100644 --- a/gin/modules/module_runner_delegate.h +++ b/gin/modules/module_runner_delegate.h @@ -31,8 +31,7 @@ class ModuleRunnerDelegate : public RunnerDelegate { virtual v8::Handle<v8::ObjectTemplate> GetGlobalTemplate( Runner* runner) OVERRIDE; virtual void DidCreateContext(Runner* runner) OVERRIDE; - virtual void DidRunScript(Runner* runner, - v8::Handle<v8::Script> script) OVERRIDE; + virtual void DidRunScript(Runner* runner) OVERRIDE; BuiltinModuleMap builtin_modules_; FileModuleProvider module_provider_; diff --git a/gin/per_context_data.cc b/gin/per_context_data.cc index 4a76616..666c414 100644 --- a/gin/per_context_data.cc +++ b/gin/per_context_data.cc @@ -15,7 +15,8 @@ ContextSupplement::ContextSupplement() { ContextSupplement::~ContextSupplement() { } -PerContextData::PerContextData(v8::Handle<v8::Context> context) { +PerContextData::PerContextData(v8::Handle<v8::Context> context) + : runner_(NULL) { context->SetAlignedPointerInEmbedderData(kEncodedValueIndex, this); } diff --git a/gin/per_context_data.h b/gin/per_context_data.h index 0f10485..b89c5a6 100644 --- a/gin/per_context_data.h +++ b/gin/per_context_data.h @@ -12,6 +12,8 @@ namespace gin { +class Runner; + class ContextSupplement { public: ContextSupplement(); @@ -32,11 +34,15 @@ class PerContextData { static PerContextData* From(v8::Handle<v8::Context>); void Detach(v8::Handle<v8::Context> context); + void set_runner(Runner* runner) { runner_ = runner; } + Runner* runner() const { return runner_; } + void AddSupplement(scoped_ptr<ContextSupplement> supplement); private: typedef ScopedVector<ContextSupplement> SuplementVector; + Runner* runner_; SuplementVector supplements_; DISALLOW_COPY_AND_ASSIGN(PerContextData); diff --git a/gin/per_isolate_data.cc b/gin/per_isolate_data.cc index 844f1e1..6c2397b 100644 --- a/gin/per_isolate_data.cc +++ b/gin/per_isolate_data.cc @@ -6,7 +6,6 @@ #include "gin/public/gin_embedders.h" using v8::Eternal; -using v8::Handle; using v8::Isolate; using v8::Local; using v8::Object; diff --git a/gin/runner.cc b/gin/runner.cc index f45d6a0..0cd7269 100644 --- a/gin/runner.cc +++ b/gin/runner.cc @@ -5,6 +5,7 @@ #include "gin/runner.h" #include "gin/converter.h" +#include "gin/per_context_data.h" #include "gin/try_catch.h" using v8::Context; @@ -29,10 +30,10 @@ v8::Handle<ObjectTemplate> RunnerDelegate::GetGlobalTemplate(Runner* runner) { void RunnerDelegate::DidCreateContext(Runner* runner) { } -void RunnerDelegate::WillRunScript(Runner* runner, v8::Handle<Script> script) { +void RunnerDelegate::WillRunScript(Runner* runner) { } -void RunnerDelegate::DidRunScript(Runner* runner, v8::Handle<Script> script) { +void RunnerDelegate::DidRunScript(Runner* runner) { } void RunnerDelegate::UnhandledException(Runner* runner, TryCatch& try_catch) { @@ -44,9 +45,13 @@ Runner::Runner(RunnerDelegate* delegate, Isolate* isolate) weak_factory_(this) { v8::Isolate::Scope isolate_scope(isolate); HandleScope handle_scope(isolate); - SetContext(Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this))); + v8::Handle<v8::Context> context = + Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this)); - v8::Context::Scope scope(context()); + SetContext(context); + PerContextData::From(context)->set_runner(this); + + v8::Context::Scope scope(context); delegate_->DidCreateContext(this); } @@ -59,13 +64,31 @@ void Runner::Run(const std::string& script) { void Runner::Run(v8::Handle<Script> script) { TryCatch try_catch; - delegate_->WillRunScript(this, script); + delegate_->WillRunScript(this); + script->Run(); - delegate_->DidRunScript(this, script); + + delegate_->DidRunScript(this); if (try_catch.HasCaught()) delegate_->UnhandledException(this, try_catch); } +v8::Handle<v8::Value> Runner::Call(v8::Handle<v8::Function> function, + v8::Handle<v8::Value> receiver, + int argc, + v8::Handle<v8::Value> argv[]) { + TryCatch try_catch; + delegate_->WillRunScript(this); + + v8::Handle<v8::Value> result = function->Call(receiver, argc, argv); + + delegate_->DidRunScript(this); + if (try_catch.HasCaught()) + delegate_->UnhandledException(this, try_catch); + + return result; +} + Runner::Scope::Scope(Runner* runner) : isolate_scope_(runner->isolate()), handle_scope_(runner->isolate()), diff --git a/gin/runner.h b/gin/runner.h index e664c3d..614b60d 100644 --- a/gin/runner.h +++ b/gin/runner.h @@ -23,8 +23,8 @@ class RunnerDelegate { // Returns the template for the global object. virtual v8::Handle<v8::ObjectTemplate> GetGlobalTemplate(Runner* runner); virtual void DidCreateContext(Runner* runner); - virtual void WillRunScript(Runner* runner, v8::Handle<v8::Script> script); - virtual void DidRunScript(Runner* runner, v8::Handle<v8::Script> script); + virtual void WillRunScript(Runner* runner); + virtual void DidRunScript(Runner* runner); virtual void UnhandledException(Runner* runner, TryCatch& try_catch); }; @@ -36,6 +36,11 @@ class Runner : public ContextHolder { void Run(const std::string& script); void Run(v8::Handle<v8::Script> script); + v8::Handle<v8::Value> Call(v8::Handle<v8::Function> function, + v8::Handle<v8::Value> receiver, + int argc, + v8::Handle<v8::Value> argv[]); + v8::Handle<v8::Object> global() const { return context()->Global(); } diff --git a/gin/wrappable.cc b/gin/wrappable.cc index 980349a..943927b 100644 --- a/gin/wrappable.cc +++ b/gin/wrappable.cc @@ -16,6 +16,12 @@ Wrappable::~Wrappable() { wrapper_.Reset(); } +v8::Handle<v8::Object> Wrappable::GetWrapper(v8::Isolate* isolate) { + v8::Handle<v8::Value> wrapper = ConvertToV8(isolate, this); + DCHECK(wrapper->IsObject()); + return v8::Handle<v8::Object>::Cast(wrapper); +} + void Wrappable::WeakCallback( const v8::WeakCallbackData<v8::Object, Wrappable>& data) { Wrappable* wrappable = data.GetParameter(); diff --git a/gin/wrappable.h b/gin/wrappable.h index 66c2389..79b7144 100644 --- a/gin/wrappable.h +++ b/gin/wrappable.h @@ -15,6 +15,8 @@ class Wrappable : public base::RefCounted<Wrappable> { public: virtual WrapperInfo* GetWrapperInfo() = 0; + v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate); + protected: Wrappable(); virtual ~Wrappable(); |