summaryrefslogtreecommitdiffstats
path: root/gin
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-29 03:21:48 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-29 03:21:48 +0000
commitb520e13a9cddc9089d5ff0bf1c168a0abb6b7ec9 (patch)
treed76d8a9997fe450fab28a52af87000a21f72578d /gin
parentd4d1e4b16a0c7f496b00232314ba9405014f8f36 (diff)
downloadchromium_src-b520e13a9cddc9089d5ff0bf1c168a0abb6b7ec9.zip
chromium_src-b520e13a9cddc9089d5ff0bf1c168a0abb6b7ec9.tar.gz
chromium_src-b520e13a9cddc9089d5ff0bf1c168a0abb6b7ec9.tar.bz2
Implement gin::ObjectTemplateBuilder
BUG= Review URL: https://codereview.chromium.org/93813002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237867 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin')
-rw-r--r--gin/converter.cc13
-rw-r--r--gin/converter.h12
-rw-r--r--gin/function_template.h5
-rw-r--r--gin/function_template.h.pump5
-rw-r--r--gin/gin.gyp2
-rw-r--r--gin/object_template_builder.cc28
-rw-r--r--gin/object_template_builder.h56
-rw-r--r--gin/test/gtest.cc24
8 files changed, 122 insertions, 23 deletions
diff --git a/gin/converter.cc b/gin/converter.cc
index c7a572a..54736f95 100644
--- a/gin/converter.cc
+++ b/gin/converter.cc
@@ -91,12 +91,15 @@ bool Converter<double>::FromV8(Isolate* isolate, Handle<Value> val,
return true;
}
+Handle<Value> Converter<base::StringPiece>::ToV8(
+ Isolate* isolate, const base::StringPiece& val) {
+ return String::NewFromUtf8(isolate, val.data(), String::kNormalString,
+ static_cast<uint32_t>(val.length()));
+}
+
Handle<Value> Converter<std::string>::ToV8(Isolate* isolate,
const std::string& val) {
- return String::NewFromUtf8(isolate,
- val.data(),
- String::kNormalString,
- static_cast<uint32_t>(val.length()));
+ return Converter<base::StringPiece>::ToV8(isolate, val);
}
bool Converter<std::string>::FromV8(Isolate* isolate, Handle<Value> val,
@@ -171,7 +174,7 @@ bool Converter<Handle<Value> >::FromV8(Isolate* isolate, Handle<Value> val,
}
v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
- const std::string& val) {
+ const base::StringPiece& val) {
return String::NewFromUtf8(isolate,
val.data(),
String::kInternalizedString,
diff --git a/gin/converter.h b/gin/converter.h
index 4ff7043..d10f315 100644
--- a/gin/converter.h
+++ b/gin/converter.h
@@ -8,6 +8,7 @@
#include <string>
#include <vector>
+#include "base/strings/string_piece.h"
#include "v8/include/v8.h"
namespace gin {
@@ -72,6 +73,13 @@ struct Converter<double> {
};
template<>
+struct Converter<base::StringPiece> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+ const base::StringPiece& val);
+ // No conversion out is possible because StringPiece does not contain storage.
+};
+
+template<>
struct Converter<std::string> {
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
const std::string& val);
@@ -164,12 +172,12 @@ v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
}
inline v8::Handle<v8::String> StringToV8(v8::Isolate* isolate,
- std::string input) {
+ const base::StringPiece& input) {
return ConvertToV8(isolate, input).As<v8::String>();
}
v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
- const std::string& val);
+ const base::StringPiece& val);
template<typename T>
bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
diff --git a/gin/function_template.h b/gin/function_template.h
index 99165f5..020357d1 100644
--- a/gin/function_template.h
+++ b/gin/function_template.h
@@ -4,6 +4,9 @@
+#ifndef GIN_FUNCTION_TEMPLATE_H_
+#define GIN_FUNCTION_TEMPLATE_H_
+
// 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.
@@ -354,3 +357,5 @@ v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
}
} // namespace gin
+
+#endif // GIN_FUNCTION_TEMPLATE_H_
diff --git a/gin/function_template.h.pump b/gin/function_template.h.pump
index 1f308ce..6b6ffc5 100644
--- a/gin/function_template.h.pump
+++ b/gin/function_template.h.pump
@@ -5,6 +5,9 @@ $$
$$ http://code.google.com/p/googletest/wiki/PumpManual
$$
+#ifndef GIN_FUNCTION_TEMPLATE_H_
+#define GIN_FUNCTION_TEMPLATE_H_
+
$var MAX_ARITY = 4
// Copyright 2013 The Chromium Authors. All rights reserved.
@@ -181,3 +184,5 @@ v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
]]
} // namespace gin
+
+#endif // GIN_FUNCTION_TEMPLATE_H_
diff --git a/gin/gin.gyp b/gin/gin.gyp
index 7c17910..f115604 100644
--- a/gin/gin.gyp
+++ b/gin/gin.gyp
@@ -40,6 +40,8 @@
'modules/module_registry.h',
'modules/module_runner_delegate.cc',
'modules/module_runner_delegate.h',
+ 'object_template_builder.cc',
+ 'object_template_builder.h',
'per_context_data.cc',
'per_context_data.h',
'per_isolate_data.cc',
diff --git a/gin/object_template_builder.cc b/gin/object_template_builder.cc
new file mode 100644
index 0000000..3a148e7
--- /dev/null
+++ b/gin/object_template_builder.cc
@@ -0,0 +1,28 @@
+// 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/object_template_builder.h"
+
+namespace gin {
+
+ObjectTemplateBuilder::ObjectTemplateBuilder(v8::Isolate* isolate)
+ : isolate_(isolate), template_(v8::ObjectTemplate::New(isolate)) {
+}
+
+ObjectTemplateBuilder::~ObjectTemplateBuilder() {
+}
+
+ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl(
+ const base::StringPiece& name, v8::Handle<v8::Data> val) {
+ template_->Set(StringToSymbol(isolate_, name), val);
+ return *this;
+}
+
+v8::Local<v8::ObjectTemplate> ObjectTemplateBuilder::Build() {
+ v8::Local<v8::ObjectTemplate> result = template_;
+ template_.Clear();
+ return result;
+}
+
+} // namespace gin
diff --git a/gin/object_template_builder.h b/gin/object_template_builder.h
new file mode 100644
index 0000000..87e2633
--- /dev/null
+++ b/gin/object_template_builder.h
@@ -0,0 +1,56 @@
+// 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_OBJECT_TEMPLATE_BUILDER_H_
+#define GIN_OBJECT_TEMPLATE_BUILDER_H_
+
+#include "base/bind.h"
+#include "base/strings/string_piece.h"
+#include "gin/converter.h"
+#include "gin/function_template.h"
+#include "v8/include/v8.h"
+
+namespace gin {
+
+// ObjectTemplateBuilder provides a handy interface to creating
+// v8::ObjectTemplate instances with various sorts of properties.
+class ObjectTemplateBuilder {
+ public:
+ explicit ObjectTemplateBuilder(v8::Isolate* isolate);
+ ~ObjectTemplateBuilder();
+
+ // It's against Google C++ style to return a non-const ref, but we take some
+ // poetic license here in order that all calls to Set() can be via the '.'
+ // operator and line up nicely.
+ template<typename T>
+ ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) {
+ return SetImpl(name, ConvertToV8(isolate_, val));
+ }
+
+ template<typename T>
+ ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, T val) {
+ return SetMethod(name, base::Bind(val));
+ }
+
+ template<typename T>
+ ObjectTemplateBuilder& SetMethod(const base::StringPiece& name,
+ const base::Callback<T>& callback) {
+ return SetImpl(name, CreateFunctionTemplate(isolate_, callback));
+ }
+
+ v8::Local<v8::ObjectTemplate> Build();
+
+ private:
+ ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
+ v8::Handle<v8::Data> val);
+
+ v8::Isolate* isolate_;
+
+ // ObjectTemplateBuilder should only be used on the stack.
+ v8::Local<v8::ObjectTemplate> template_;
+};
+
+} // namespace gin
+
+#endif // GIN_OBJECT_TEMPLATE_BUILDER_H_
diff --git a/gin/test/gtest.cc b/gin/test/gtest.cc
index a0cf70c..d5e4d8c 100644
--- a/gin/test/gtest.cc
+++ b/gin/test/gtest.cc
@@ -9,6 +9,7 @@
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/function_template.h"
+#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
#include "gin/public/wrapper_info.h"
#include "gin/wrappable.h"
@@ -18,13 +19,7 @@ namespace gin {
namespace {
-void Fail(const v8::FunctionCallbackInfo<v8::Value>& info) {
- Arguments args(info);
-
- std::string description;
- if (!args.GetNext(&description))
- return args.ThrowError();
-
+void Fail(const std::string& description) {
FAIL() << description;
}
@@ -53,15 +48,12 @@ v8::Local<v8::ObjectTemplate> GTest::GetTemplate(v8::Isolate* isolate) {
v8::Local<v8::ObjectTemplate> templ =
data->GetObjectTemplate(&g_wrapper_info);
if (templ.IsEmpty()) {
- templ = v8::ObjectTemplate::New();
- templ->Set(StringToSymbol(isolate, "fail"),
- v8::FunctionTemplate::New(Fail));
- templ->Set(StringToSymbol(isolate, "expectTrue"),
- CreateFunctionTemplate(isolate, base::Bind(ExpectTrue)));
- templ->Set(StringToSymbol(isolate, "expectFalse"),
- CreateFunctionTemplate(isolate, base::Bind(ExpectFalse)));
- templ->Set(StringToSymbol(isolate, "expectEqual"),
- CreateFunctionTemplate(isolate, base::Bind(ExpectEqual)));
+ templ = ObjectTemplateBuilder(isolate)
+ .SetMethod("fail", Fail)
+ .SetMethod("expectTrue", ExpectTrue)
+ .SetMethod("expectFalse", ExpectFalse)
+ .SetMethod("expectEqual", ExpectEqual)
+ .Build();
data->SetObjectTemplate(&g_wrapper_info, templ);
}
return templ;