Commit 14d46f52 authored by yangguo's avatar yangguo Committed by Commit bot

Correctly wrap uri.js into a function.

R=jkummerow@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#27712}
parent 9e3e0aaa
......@@ -2,24 +2,22 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
"use strict";
// This file relies on the fact that the following declaration has been made
// in runtime.js:
// var $Array = global.Array;
// -------------------------------------------------------------------
// This file contains support for URI manipulations written in
// JavaScript.
(function() {
// -------------------------------------------------------------------
// Define internal helper functions.
"use strict";
%CheckIsBootstrapping();
function HexValueOf(code) {
var GlobalObject = global.Object;
var GlobalArray = global.Array;
// -------------------------------------------------------------------
// Define internal helper functions.
function HexValueOf(code) {
// 0-9
if (code >= 48 && code <= 57) return code - 48;
// A-F
......@@ -28,10 +26,10 @@
if (code >= 97 && code <= 102) return code - 87;
return -1;
}
}
// Does the char code correspond to an alpha-numeric char.
function isAlphaNumeric(cc) {
// Does the char code correspond to an alpha-numeric char.
function isAlphaNumeric(cc) {
// a - z
if (97 <= cc && cc <= 122) return true;
// A - Z
......@@ -40,19 +38,19 @@
if (48 <= cc && cc <= 57) return true;
return false;
}
}
//Lazily initialized.
var hexCharCodeArray = 0;
//Lazily initialized.
var hexCharCodeArray = 0;
function URIAddEncodedOctetToBuffer(octet, result, index) {
function URIAddEncodedOctetToBuffer(octet, result, index) {
result[index++] = 37; // Char code of '%'.
result[index++] = hexCharCodeArray[octet >> 4];
result[index++] = hexCharCodeArray[octet & 0x0F];
return index;
}
}
function URIEncodeOctets(octets, result, index) {
function URIEncodeOctets(octets, result, index) {
if (hexCharCodeArray === 0) {
hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
65, 66, 67, 68, 69, 70];
......@@ -62,13 +60,13 @@
if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index);
if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index);
return index;
}
}
function URIEncodeSingle(cc, result, index) {
function URIEncodeSingle(cc, result, index) {
var x = (cc >> 12) & 0xF;
var y = (cc >> 6) & 63;
var z = cc & 63;
var octets = new $Array(3);
var octets = new GlobalArray(3);
if (cc <= 0x007F) {
octets[0] = cc;
} else if (cc <= 0x07FF) {
......@@ -80,34 +78,34 @@
octets[2] = z + 128;
}
return URIEncodeOctets(octets, result, index);
}
}
function URIEncodePair(cc1 , cc2, result, index) {
function URIEncodePair(cc1 , cc2, result, index) {
var u = ((cc1 >> 6) & 0xF) + 1;
var w = (cc1 >> 2) & 0xF;
var x = cc1 & 3;
var y = (cc2 >> 6) & 0xF;
var z = cc2 & 63;
var octets = new $Array(4);
var octets = new GlobalArray(4);
octets[0] = (u >> 2) + 240;
octets[1] = (((u & 3) << 4) | w) + 128;
octets[2] = ((x << 4) | y) + 128;
octets[3] = z + 128;
return URIEncodeOctets(octets, result, index);
}
}
function URIHexCharsToCharCode(highChar, lowChar) {
function URIHexCharsToCharCode(highChar, lowChar) {
var highCode = HexValueOf(highChar);
var lowCode = HexValueOf(lowChar);
if (highCode == -1 || lowCode == -1) {
throw new $URIError("URI malformed");
}
return (highCode << 4) | lowCode;
}
}
// Callers must ensure that |result| is a sufficiently long sequential
// two-byte string!
function URIDecodeOctets(octets, result, index) {
// Callers must ensure that |result| is a sufficiently long sequential
// two-byte string!
function URIDecodeOctets(octets, result, index) {
var value;
var o0 = octets[0];
if (o0 < 0x80) {
......@@ -178,10 +176,10 @@
%_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result);
}
return index;
}
}
// ECMA-262, section 15.1.3
function Encode(uri, unescape) {
// ECMA-262, section 15.1.3
function Encode(uri, unescape) {
var uriLength = uri.length;
var array = new InternalArray(uriLength);
var index = 0;
......@@ -208,10 +206,10 @@
%_OneByteSeqStringSetChar(i, array[i], result);
}
return result;
}
}
// ECMA-262, section 15.1.3
function Decode(uri, reserved) {
// ECMA-262, section 15.1.3
function Decode(uri, reserved) {
var uriLength = uri.length;
var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING);
var index = 0;
......@@ -254,7 +252,7 @@
var n = 0;
while (((cc << ++n) & 0x80) != 0) { }
if (n == 1 || n > 4) throw new $URIError("URI malformed");
var octets = new $Array(n);
var octets = new GlobalArray(n);
octets[0] = cc;
if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed");
for (var i = 1; i < n; i++) {
......@@ -277,25 +275,25 @@
two_byte = %TruncateString(two_byte, index);
return one_byte + two_byte;
}
}
// -------------------------------------------------------------------
// Define exported functions.
// -------------------------------------------------------------------
// Define exported functions.
// ECMA-262 - B.2.1.
function URIEscapeJS(str) {
// ECMA-262 - B.2.1.
function URIEscapeJS(str) {
var s = ToString(str);
return %URIEscape(s);
}
}
// ECMA-262 - B.2.2.
function URIUnescapeJS(str) {
// ECMA-262 - B.2.2.
function URIUnescapeJS(str) {
var s = ToString(str);
return %URIUnescape(s);
}
}
// ECMA-262 - 15.1.3.1.
function URIDecode(uri) {
// ECMA-262 - 15.1.3.1.
function URIDecode(uri) {
var reservedPredicate = function(cc) {
// #$
if (35 <= cc && cc <= 36) return true;
......@@ -316,17 +314,17 @@
};
var string = ToString(uri);
return Decode(string, reservedPredicate);
}
}
// ECMA-262 - 15.1.3.2.
function URIDecodeComponent(component) {
// ECMA-262 - 15.1.3.2.
function URIDecodeComponent(component) {
var reservedPredicate = function(cc) { return false; };
var string = ToString(component);
return Decode(string, reservedPredicate);
}
}
// ECMA-262 - 15.1.3.3.
function URIEncode(uri) {
// ECMA-262 - 15.1.3.3.
function URIEncode(uri) {
var unescapePredicate = function(cc) {
if (isAlphaNumeric(cc)) return true;
// !
......@@ -350,10 +348,10 @@
};
var string = ToString(uri);
return Encode(string, unescapePredicate);
}
}
// ECMA-262 - 15.1.3.4
function URIEncodeComponent(component) {
// ECMA-262 - 15.1.3.4
function URIEncodeComponent(component) {
var unescapePredicate = function(cc) {
if (isAlphaNumeric(cc)) return true;
// !
......@@ -371,22 +369,20 @@
};
var string = ToString(component);
return Encode(string, unescapePredicate);
}
// -------------------------------------------------------------------
// Install exported functions.
}
%CheckIsBootstrapping();
// -------------------------------------------------------------------
// Install exported functions.
// Set up non-enumerable URI functions on the global object and set
// their names.
InstallFunctions(global, DONT_ENUM, $Array(
// Set up non-enumerable URI functions on the global object and set
// their names.
InstallFunctions(global, DONT_ENUM, GlobalArray(
"escape", URIEscapeJS,
"unescape", URIUnescapeJS,
"decodeURI", URIDecode,
"decodeURIComponent", URIDecodeComponent,
"encodeURI", URIEncode,
"encodeURIComponent", URIEncodeComponent
));
));
})();
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