diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-19 05:17:12 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-19 05:17:12 +0000 |
commit | 858eeea0a7a3e3271351ca127734574150af1185 (patch) | |
tree | 1e84210476d1d5ef365737cda1786d9564edff58 /gin/modules | |
parent | b61ee040ecfd5e983137a114aaa3445ff4dbec4e (diff) | |
download | chromium_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/modules')
-rw-r--r-- | gin/modules/console.cc | 49 | ||||
-rw-r--r-- | gin/modules/console.h | 20 |
2 files changed, 69 insertions, 0 deletions
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_ |