Mojo: Reimplement unicode.js in JavaScript, remove native implementation.

This version is likely less efficient, but doesn't require gin.

BUG=372065

Review URL: https://codereview.chromium.org/294123008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275342 0039d316-1c4b-4281-b951-d872f2087c98
parent fb1464a0
......@@ -35,6 +35,7 @@ WebUIDataSource* WebUIDataSource::AddMojoDataSource(
{ mojo::kConnectionModuleName, IDR_MOJO_CONNECTION_JS },
{ mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS },
{ mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS },
{ mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(resources); ++i)
mojo_source->AddResourcePath(resources[i].path, resources[i].id);
......
......@@ -39,6 +39,7 @@ bool GetResource(const std::string& id,
if (id == mojo::kCodecModuleName ||
id == mojo::kConnectionModuleName ||
id == mojo::kConnectorModuleName ||
id == mojo::kUnicodeModuleName ||
id == mojo::kRouterModuleName)
return false;
......
......@@ -43,6 +43,7 @@
<include name="IDR_MOJO_CONNECTION_JS" file="../mojo/public/js/bindings/connection.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_MOJO_CONNECTOR_JS" file="../mojo/public/js/bindings/connector.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_MOJO_ROUTER_JS" file="../mojo/public/js/bindings/router.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_MOJO_UNICODE_JS" file="../mojo/public/js/bindings/unicode.js" flattenhtml="true" type="BINDATA" />
</if>
</includes>
</release>
......
......@@ -9,7 +9,6 @@
#include "gin/public/context_holder.h"
#include "mojo/bindings/js/core.h"
#include "mojo/bindings/js/support.h"
#include "mojo/bindings/js/unicode.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"
......@@ -48,10 +47,6 @@ void WebUIRunner::RegisterBuiltinModules() {
mojo::js::Support::kModuleName,
mojo::js::Support::GetModule(
context_holder_->isolate()));
registry->AddBuiltinModule(context_holder_->isolate(),
mojo::js::Unicode::kModuleName,
mojo::js::Unicode::GetModule(
context_holder_->isolate()));
}
void WebUIRunner::Run(const std::string& source,
......
......@@ -17,7 +17,6 @@
#include "mojo/bindings/js/core.h"
#include "mojo/bindings/js/handle.h"
#include "mojo/bindings/js/support.h"
#include "mojo/bindings/js/unicode.h"
namespace mojo {
namespace apps {
......@@ -54,7 +53,6 @@ MojoRunnerDelegate::MojoRunnerDelegate()
AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule);
AddBuiltinModule(js::Core::kModuleName, js::Core::GetModule);
AddBuiltinModule(js::Support::kModuleName, js::Support::GetModule);
AddBuiltinModule(js::Unicode::kModuleName, js::Unicode::GetModule);
AddBuiltinModule(js::gl::kModuleName, js::gl::GetModule);
AddBuiltinModule(MonotonicClock::kModuleName, MonotonicClock::GetModule);
AddBuiltinModule(Threading::kModuleName, Threading::GetModule);
......
......@@ -13,7 +13,6 @@
#include "mojo/apps/js/bindings/monotonic_clock.h"
#include "mojo/apps/js/bindings/threading.h"
#include "mojo/bindings/js/core.h"
#include "mojo/bindings/js/unicode.h"
#include "mojo/common/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -26,7 +25,6 @@ class TestRunnerDelegate : public gin::FileRunnerDelegate {
TestRunnerDelegate() {
AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
AddBuiltinModule(Core::kModuleName, Core::GetModule);
AddBuiltinModule(Unicode::kModuleName, Unicode::GetModule);
AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule);
AddBuiltinModule(apps::MonotonicClock::kModuleName,
apps::MonotonicClock::GetModule);
......
......@@ -11,7 +11,6 @@
#include "gin/test/file_runner.h"
#include "gin/test/gtest.h"
#include "mojo/bindings/js/core.h"
#include "mojo/bindings/js/unicode.h"
#include "mojo/common/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -24,7 +23,6 @@ class TestRunnerDelegate : public gin::FileRunnerDelegate {
TestRunnerDelegate() {
AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
AddBuiltinModule(Core::kModuleName, Core::GetModule);
AddBuiltinModule(Unicode::kModuleName, Unicode::GetModule);
}
private:
......
// Copyright 2014 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 "mojo/bindings/js/unicode.h"
#include "gin/arguments.h"
#include "gin/array_buffer.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
#include "gin/public/wrapper_info.h"
namespace mojo {
namespace js {
namespace {
v8::Handle<v8::Value> DecodeUtf8String(const gin::Arguments& args,
const gin::ArrayBufferView& buffer) {
assert(static_cast<int>(buffer.num_bytes()) >= 0);
return v8::String::NewFromUtf8(args.isolate(),
reinterpret_cast<char*>(buffer.bytes()),
v8::String::kNormalString,
static_cast<int>(buffer.num_bytes()));
}
int EncodeUtf8String(const gin::Arguments& args,
v8::Handle<v8::Value> str_value,
const gin::ArrayBufferView& buffer) {
assert(static_cast<int>(buffer.num_bytes()) >= 0);
v8::Handle<v8::String> str = str_value->ToString();
int num_bytes = str->WriteUtf8(
reinterpret_cast<char*>(buffer.bytes()),
static_cast<int>(buffer.num_bytes()),
NULL,
v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8);
return num_bytes;
}
int Utf8Length(v8::Handle<v8::Value> str_value) {
return str_value->ToString()->Utf8Length();
}
gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
} // namespace
const char Unicode::kModuleName[] = "mojo/public/js/bindings/unicode";
v8::Local<v8::Value> Unicode::GetModule(v8::Isolate* isolate) {
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(
&g_wrapper_info);
if (templ.IsEmpty()) {
templ = gin::ObjectTemplateBuilder(isolate)
.SetMethod("decodeUtf8String", DecodeUtf8String)
.SetMethod("encodeUtf8String", EncodeUtf8String)
.SetMethod("utf8Length", Utf8Length)
.Build();
data->SetObjectTemplate(&g_wrapper_info, templ);
}
return templ->NewInstance();
}
} // namespace js
} // namespace mojo
// Copyright 2014 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 MOJO_BINDINGS_JS_UNICODE_H_
#define MOJO_BINDINGS_JS_UNICODE_H_
#include "v8/include/v8.h"
namespace mojo {
namespace js {
class Unicode {
public:
static const char kModuleName[];
static v8::Local<v8::Value> GetModule(v8::Isolate* isolate);
};
} // namespace js
} // namespace mojo
#endif // MOJO_BINDINGS_JS_UNICODE_H_
......@@ -643,8 +643,6 @@
'bindings/js/handle.h',
'bindings/js/support.cc',
'bindings/js/support.h',
'bindings/js/unicode.cc',
'bindings/js/unicode.h',
'bindings/js/waiting_callback.cc',
'bindings/js/waiting_callback.h',
],
......
......@@ -9,6 +9,7 @@ namespace mojo {
const char kCodecModuleName[] = "mojo/public/js/bindings/codec";
const char kConnectionModuleName[] = "mojo/public/js/bindings/connection";
const char kConnectorModuleName[] = "mojo/public/js/bindings/connector";
const char kUnicodeModuleName[] = "mojo/public/js/bindings/unicode";
const char kRouterModuleName[] = "mojo/public/js/bindings/router";
} // namespace mojo
......@@ -11,6 +11,7 @@ namespace mojo {
extern const char kCodecModuleName[];
extern const char kConnectionModuleName[];
extern const char kConnectorModuleName[];
extern const char kUnicodeModuleName[];
extern const char kRouterModuleName[];
} // namespace mojo
......
......@@ -2,32 +2,50 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Module "mojo/public/js/bindings/unicode"
//
// Note: This file is for documentation purposes only. The code here is not
// actually executed. The real module is implemented natively in Mojo.
while (1);
/**
* Decodes the UTF8 string from the given buffer.
* @param {ArrayBufferView} buffer The buffer containing UTF8 string data.
* @return {string} The corresponding JavaScript string.
* Defines functions for translating between JavaScript strings and UTF8 strings
* stored in ArrayBuffers. There is much room for optimization in this code if
* it proves necessary.
*/
function decodeUtf8String(buffer) { [native code] }
define("mojo/public/js/bindings/unicode", function() {
/**
* Decodes the UTF8 string from the given buffer.
* @param {ArrayBufferView} buffer The buffer containing UTF8 string data.
* @return {string} The corresponding JavaScript string.
*/
function decodeUtf8String(buffer) {
return decodeURIComponent(escape(String.fromCharCode.apply(null, buffer)));
}
/**
* Encodes the given JavaScript string into UTF8.
* @param {string} str The string to encode.
* @param {ArrayBufferView} outputBuffer The buffer to contain the result.
* Should be pre-allocated to hold enough space. Use |utf8Length| to determine
* how much space is required.
* @return {number} The number of bytes written to |outputBuffer|.
*/
function encodeUtf8String(str, outputBuffer) { [native code] }
/**
* Encodes the given JavaScript string into UTF8.
* @param {string} str The string to encode.
* @param {ArrayBufferView} outputBuffer The buffer to contain the result.
* Should be pre-allocated to hold enough space. Use |utf8Length| to determine
* how much space is required.
* @return {number} The number of bytes written to |outputBuffer|.
*/
function encodeUtf8String(str, outputBuffer) {
var utf8String = unescape(encodeURIComponent(str));
if (outputBuffer.length < utf8String.length)
throw new Error("Buffer too small for encodeUtf8String");
for (var i = 0; i < outputBuffer.length && i < utf8String.length; i++)
outputBuffer[i] = utf8String.charCodeAt(i);
return i;
}
/**
* Returns the number of bytes that a UTF8 encoding of the JavaScript string
* |str| would occupy.
*/
function utf8Length(str) { [native code] }
/**
* Returns the number of bytes that a UTF8 encoding of the JavaScript string
* |str| would occupy.
*/
function utf8Length(str) {
var utf8String = unescape(encodeURIComponent(str));
return utf8String.length;
}
var exports = {};
exports.decodeUtf8String = decodeUtf8String;
exports.encodeUtf8String = encodeUtf8String;
exports.utf8Length = utf8Length;
return exports;
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment