summaryrefslogtreecommitdiffstats
path: root/gin
diff options
context:
space:
mode:
authorabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-19 05:17:12 +0000
committerabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-19 05:17:12 +0000
commit858eeea0a7a3e3271351ca127734574150af1185 (patch)
tree1e84210476d1d5ef365737cda1786d9564edff58 /gin
parentb61ee040ecfd5e983137a114aaa3445ff4dbec4e (diff)
downloadchromium_src-858eeea0a7a3e3271351ca127734574150af1185.zip
chromium_src-858eeea0a7a3e3271351ca127734574150af1185.tar.gz
chromium_src-858eeea0a7a3e3271351ca127734574150af1185.tar.bz2
Add console.log support to gin_shell
This CL adds a basic console module to gin_shell so that we can print to stdout with a JavaScript program like the following: define(["console"], function(console) { console.log("Hello,", "world"); }); BUG=317398 Review URL: https://codereview.chromium.org/75273007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@235940 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin')
-rw-r--r--gin/arguments.h16
-rw-r--r--gin/gin.gyp2
-rw-r--r--gin/modules/console.cc49
-rw-r--r--gin/modules/console.h20
-rw-r--r--gin/shell/gin_main.cc5
-rw-r--r--gin/test/gtest.cc13
6 files changed, 98 insertions, 7 deletions
diff --git a/gin/arguments.h b/gin/arguments.h
index 1882640..1d59d34 100644
--- a/gin/arguments.h
+++ b/gin/arguments.h
@@ -31,6 +31,22 @@ class Arguments {
}
template<typename T>
+ bool GetRemaining(std::vector<T>* out) {
+ if (next_ >= info_.Length()) {
+ insufficient_arguments_ = true;
+ return false;
+ }
+ int remaining = info_.Length() - next_;
+ out->resize(remaining);
+ for (int i = 0; i < remaining; ++i) {
+ v8::Handle<v8::Value> val = info_[next_++];
+ if (!ConvertFromV8(val, &out->at(i)))
+ return false;
+ }
+ return true;
+ }
+
+ template<typename T>
void Return(T val) {
info_.GetReturnValue().Set(ConvertToV8(isolate_, val));
}
diff --git a/gin/gin.gyp b/gin/gin.gyp
index 7bdf8ab..7546cfb 100644
--- a/gin/gin.gyp
+++ b/gin/gin.gyp
@@ -19,6 +19,8 @@
'../v8/tools/gyp/v8.gyp:v8',
],
'sources': [
+ 'modules/console.cc',
+ 'modules/console.h',
'modules/file_module_provider.cc',
'modules/file_module_provider.h',
'modules/module_registry.cc',
diff --git a/gin/modules/console.cc b/gin/modules/console.cc
new file mode 100644
index 0000000..6f4afbb
--- /dev/null
+++ b/gin/modules/console.cc
@@ -0,0 +1,49 @@
+// 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/modules/console.h"
+
+#include <iostream>
+
+#include "base/strings/string_util.h"
+#include "gin/arguments.h"
+#include "gin/converter.h"
+#include "gin/per_isolate_data.h"
+#include "gin/wrapper_info.h"
+
+using v8::ObjectTemplate;
+
+namespace gin {
+
+namespace {
+
+void Log(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ Arguments args(info);
+
+ std::vector<std::string> messages;
+ if (!args.GetRemaining(&messages))
+ return args.ThrowTypeError("Expected strings.");
+
+ std::cout << JoinString(messages, ' ') << std::endl;
+}
+
+WrapperInfo g_wrapper_info = {};
+
+} // namespace
+
+const char Console::kModuleName[] = "console";
+
+v8::Local<ObjectTemplate> Console::GetTemplate(v8::Isolate* isolate) {
+ PerIsolateData* data = PerIsolateData::From(isolate);
+ v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info);
+ if (templ.IsEmpty()) {
+ templ = ObjectTemplate::New();
+ templ->Set(StringToSymbol(isolate, "log"),
+ v8::FunctionTemplate::New(Log));
+ data->SetObjectTemplate(&g_wrapper_info, templ);
+ }
+ return templ;
+}
+
+} // namespace gin
diff --git a/gin/modules/console.h b/gin/modules/console.h
new file mode 100644
index 0000000..139a6c9
--- /dev/null
+++ b/gin/modules/console.h
@@ -0,0 +1,20 @@
+// 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_MODULES_CONSOLE_H_
+#define GIN_MODULES_CONSOLE_H_
+
+#include "v8/include/v8.h"
+
+namespace gin {
+
+class Console {
+ public:
+ static const char kModuleName[];
+ static v8::Local<v8::ObjectTemplate> GetTemplate(v8::Isolate* isolate);
+};
+
+} // namespace gin
+
+#endif // GIN_MODULES_CONSOLE_H_
diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc
index 5d2831c..ca984f7 100644
--- a/gin/shell/gin_main.cc
+++ b/gin/shell/gin_main.cc
@@ -8,6 +8,7 @@
#include "base/file_util.h"
#include "base/message_loop/message_loop.h"
#include "gin/initialize.h"
+#include "gin/modules/console.h"
#include "gin/modules/module_runner_delegate.h"
#include "gin/test/file_runner.h"
#include "gin/try_catch.h"
@@ -37,7 +38,9 @@ base::FilePath GetModuleBase() {
class ShellRunnerDelegate : public ModuleRunnerDelegate {
public:
- ShellRunnerDelegate() : ModuleRunnerDelegate(GetModuleBase()) {}
+ ShellRunnerDelegate() : ModuleRunnerDelegate(GetModuleBase()) {
+ AddBuiltinModule(Console::kModuleName, Console::GetTemplate);
+ }
virtual void UnhandledException(Runner* runner,
TryCatch& try_catch) OVERRIDE {
diff --git a/gin/test/gtest.cc b/gin/test/gtest.cc
index d600418..c33a83d 100644
--- a/gin/test/gtest.cc
+++ b/gin/test/gtest.cc
@@ -10,6 +10,8 @@
#include "gin/wrapper_info.h"
#include "testing/gtest/include/gtest/gtest.h"
+using v8::ObjectTemplate;
+
namespace gin {
namespace {
@@ -52,25 +54,24 @@ void ExpectEqual(const v8::FunctionCallbackInfo<v8::Value>& info) {
EXPECT_TRUE(info[0]->StrictEquals(info[1])) << description;
}
-WrapperInfo g_gtest_wrapper_info = {};
+WrapperInfo g_wrapper_info = {};
} // namespace
const char GTest::kModuleName[] = "gtest";
-v8::Local<v8::ObjectTemplate> GTest::GetTemplate(v8::Isolate* isolate) {
+v8::Local<ObjectTemplate> GTest::GetTemplate(v8::Isolate* isolate) {
PerIsolateData* data = PerIsolateData::From(isolate);
- v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(
- &g_gtest_wrapper_info);
+ v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info);
if (templ.IsEmpty()) {
- templ = v8::ObjectTemplate::New();
+ templ = ObjectTemplate::New();
templ->Set(StringToSymbol(isolate, "expectTrue"),
v8::FunctionTemplate::New(ExpectTrue));
templ->Set(StringToSymbol(isolate, "expectFalse"),
v8::FunctionTemplate::New(ExpectFalse));
templ->Set(StringToSymbol(isolate, "expectEqual"),
v8::FunctionTemplate::New(ExpectEqual));
- data->SetObjectTemplate(&g_gtest_wrapper_info, templ);
+ data->SetObjectTemplate(&g_wrapper_info, templ);
}
return templ;
}