summaryrefslogtreecommitdiffstats
path: root/gin/converter.h
diff options
context:
space:
mode:
authorabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-10 05:00:50 +0000
committerabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-10 05:00:50 +0000
commita22998a223ecdcd926d152baea8a8703bcb57d61 (patch)
treeccb6b71bcc50b61c6671d955fbc409802f0c8d95 /gin/converter.h
parent1cde19eb7e17b16645fb75a50f3250218d53cd17 (diff)
downloadchromium_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/converter.h')
-rw-r--r--gin/converter.h121
1 files changed, 121 insertions, 0 deletions
diff --git a/gin/converter.h b/gin/converter.h
new file mode 100644
index 0000000..eb84be2
--- /dev/null
+++ b/gin/converter.h
@@ -0,0 +1,121 @@
+// 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.
+
+#ifndef GIN_CONVERTER_H_
+#define GIN_CONVERTER_H_
+
+#include <string>
+#include <vector>
+
+#include "v8/include/v8.h"
+
+namespace gin {
+
+template<typename T>
+struct Converter {};
+
+template<>
+struct Converter<bool> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ bool val);
+ static bool FromV8(v8::Handle<v8::Value> val,
+ bool* out);
+};
+
+template<>
+struct Converter<int32_t> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ int32_t val);
+ static bool FromV8(v8::Handle<v8::Value> val,
+ int32_t* out);
+};
+
+template<>
+struct Converter<uint32_t> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ uint32_t val);
+ static bool FromV8(v8::Handle<v8::Value> val,
+ uint32_t* out);
+};
+
+template<>
+struct Converter<double> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ double val);
+ static bool FromV8(v8::Handle<v8::Value> val,
+ double* out);
+};
+
+template<>
+struct Converter<std::string> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ const std::string& val);
+ static bool FromV8(v8::Handle<v8::Value> val,
+ std::string* out);
+};
+
+template<>
+struct Converter<v8::Handle<v8::Function> > {
+ static bool FromV8(v8::Handle<v8::Value> val,
+ v8::Handle<v8::Function>* out);
+};
+
+template<>
+struct Converter<v8::Handle<v8::Object> > {
+ static v8::Handle<v8::Value> ToV8(v8::Handle<v8::Object> val);
+ static bool FromV8(v8::Handle<v8::Value> val,
+ v8::Handle<v8::Object>* out);
+};
+
+template<typename T>
+struct Converter<std::vector<T> > {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ const std::vector<T>& val) {
+ v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size())));
+ for (size_t i = 0; i < val.size(); ++i) {
+ result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
+ }
+ return result;
+ }
+
+ static bool FromV8(v8::Handle<v8::Value> val,
+ std::vector<T>* out) {
+ if (!val->IsArray())
+ return false;
+
+ std::vector<T> result;
+ v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
+ uint32_t length = array->Length();
+ for (uint32_t i = 0; i < length; ++i) {
+ T item;
+ if (!Converter<T>::FromV8(array->Get(i), &item))
+ return false;
+ result.push_back(item);
+ }
+
+ out->swap(result);
+ return true;
+ }
+};
+
+// Convenience functions that deduce T.
+template<typename T>
+v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
+ T input) {
+ return Converter<T>::ToV8(isolate, input);
+}
+
+inline v8::Handle<v8::String> StringToV8(
+ v8::Isolate* isolate, std::string input) {
+ return ConvertToV8(isolate, input).As<v8::String>();
+}
+
+template<typename T>
+bool ConvertFromV8(v8::Handle<v8::Value> input, T* result) {
+ return Converter<T>::FromV8(input, result);
+}
+
+} // namespace gin
+
+#endif // GIN_CONVERTER_H_