diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-24 19:10:11 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-24 19:10:11 +0000 |
commit | ec95fdfdde2c5c0ca1b953143dd6f4a9144025fa (patch) | |
tree | 112b98f4ca12105ce9c32376d9268bc03af6e723 /gin | |
parent | 8b4e19b1b01a00c12d62a778a92981ef18d10073 (diff) | |
download | chromium_src-ec95fdfdde2c5c0ca1b953143dd6f4a9144025fa.zip chromium_src-ec95fdfdde2c5c0ca1b953143dd6f4a9144025fa.tar.gz chromium_src-ec95fdfdde2c5c0ca1b953143dd6f4a9144025fa.tar.bz2 |
[Mojo] Improve JavaScript API for MojoReadMessage
Originally, I tried to make the JavaScript bindings for the core Mojo APIs a
literal translation of the C APIs, but that makes MojoReadMessage a bit awkward
because C needs to worry much more about buffer allocation than JavaScript. In
building towards using the hello_world_service, I found that it was much easier
to push the MojoReadMessage pattern from connector.cc into C++ and make the
JavaScript API a bit more idiomatic by just returning an ArrayBuffer for the
message and an Array of handles.
BUG=317398
Review URL: https://codereview.chromium.org/83143002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236999 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin')
-rw-r--r-- | gin/converter.cc | 14 | ||||
-rw-r--r-- | gin/converter.h | 8 | ||||
-rw-r--r-- | gin/modules/module_registry.cc | 26 |
3 files changed, 32 insertions, 16 deletions
diff --git a/gin/converter.cc b/gin/converter.cc index 5fda673..e60e3a0 100644 --- a/gin/converter.cc +++ b/gin/converter.cc @@ -6,6 +6,7 @@ #include "v8/include/v8.h" +using v8::ArrayBuffer; using v8::Boolean; using v8::External; using v8::Function; @@ -125,6 +126,19 @@ bool Converter<Handle<Object> >::FromV8(Handle<Value> val, return true; } +Handle<Value> Converter<Handle<ArrayBuffer> >::ToV8(v8::Isolate* isolate, + Handle<ArrayBuffer> val) { + return val.As<Value>(); +} + +bool Converter<Handle<ArrayBuffer> >::FromV8(Handle<Value> val, + Handle<ArrayBuffer>* out) { + if (!val->IsArrayBuffer()) + return false; + *out = Handle<ArrayBuffer>::Cast(val); + return true; +} + Handle<Value> Converter<Handle<External> >::ToV8(v8::Isolate* isolate, Handle<External> val) { return val.As<Value>(); diff --git a/gin/converter.h b/gin/converter.h index 33af452..2279319 100644 --- a/gin/converter.h +++ b/gin/converter.h @@ -88,6 +88,14 @@ struct Converter<v8::Handle<v8::Object> > { }; template<> +struct Converter<v8::Handle<v8::ArrayBuffer> > { + static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, + v8::Handle<v8::ArrayBuffer> val); + static bool FromV8(v8::Handle<v8::Value> val, + v8::Handle<v8::ArrayBuffer>* out); +}; + +template<> struct Converter<v8::Handle<v8::External> > { static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, v8::Handle<v8::External> val); diff --git a/gin/modules/module_registry.cc b/gin/modules/module_registry.cc index 8baa482..afb07d3 100644 --- a/gin/modules/module_registry.cc +++ b/gin/modules/module_registry.cc @@ -12,6 +12,7 @@ #include "gin/converter.h" #include "gin/per_isolate_data.h" #include "gin/public/wrapper_info.h" +#include "gin/try_catch.h" using v8::Context; using v8::External; @@ -91,19 +92,6 @@ Handle<String> GetHiddenValueKey(Isolate* isolate) { return StringToSymbol(isolate, "::gin::ModuleRegistry"); } -std::string GetImplicitModuleName(const std::string& explicit_name) { - if (!explicit_name.empty()) - return explicit_name; - std::string implicit_name; - Handle<StackTrace> trace = StackTrace::CurrentStackTrace(1); - if (!trace->GetFrameCount()) - return implicit_name; - Handle<String> script_name = trace->GetFrame(0)->GetScriptName(); - if (!script_name.IsEmpty()) - ConvertFromV8(script_name, &implicit_name); - return implicit_name; -} - } // namespace ModuleRegistry::ModuleRegistry(Isolate* isolate) @@ -196,11 +184,17 @@ void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { Handle<Function> factory; if (ConvertFromV8(module, &factory)) { Handle<Object> global = isolate->GetCurrentContext()->Global(); - module = factory->Call(global, argc, argv.data()); - // TODO(abarth): What should we do with exceptions? + { + 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? + } + if (pending->id.empty()) + ConvertFromV8(factory->GetScriptOrigin().ResourceName(), &pending->id); } - RegisterModule(isolate, GetImplicitModuleName(pending->id), module); + RegisterModule(isolate, pending->id, module); } bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |