Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Unified Diff: mojo/public/js/bindings/unicode.js

Issue 294123008: Mojo: Reimplement unicode.js in JavaScript, remove native implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/js/bindings/constants.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/js/bindings/unicode.js
diff --git a/mojo/public/js/bindings/unicode.js b/mojo/public/js/bindings/unicode.js
index a42c710be404d2cfde3b4e920ffe378c16eade15..ba0f00f95bae99b20f2db4e2c9d9013d96517149 100644
--- a/mojo/public/js/bindings/unicode.js
+++ b/mojo/public/js/bindings/unicode.js
@@ -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;
+});
« no previous file with comments | « mojo/public/js/bindings/constants.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698