Commit 742ae613 authored by yangguo's avatar yangguo Committed by Commit bot

Do not switch to two-byte string in String.fromCharCode if avoidable.

R=bmeurer@chromium.org
BUG=v8:4536
LOG=N

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

Cr-Commit-Position: refs/heads/master@{#31872}
parent 7c3396d0
...@@ -773,17 +773,12 @@ function StringTrimRight() { ...@@ -773,17 +773,12 @@ function StringTrimRight() {
// ECMA-262, section 15.5.3.2 // ECMA-262, section 15.5.3.2
function StringFromCharCode(code) { function StringFromCharCode(code) {
var n = %_ArgumentsLength(); var n = %_ArgumentsLength();
if (n == 1) { if (n == 1) return %_StringCharFromCode(code & 0xffff);
if (!%_IsSmi(code)) code = TO_NUMBER(code);
return %_StringCharFromCode(code & 0xffff);
}
var one_byte = %NewString(n, NEW_ONE_BYTE_STRING); var one_byte = %NewString(n, NEW_ONE_BYTE_STRING);
var i; var i;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
var code = %_Arguments(i); code = %_Arguments(i) & 0xffff;
if (!%_IsSmi(code)) code = TO_NUMBER(code) & 0xffff;
if (code < 0) code = code & 0xffff;
if (code > 0xff) break; if (code > 0xff) break;
%_OneByteSeqStringSetChar(i, code, one_byte); %_OneByteSeqStringSetChar(i, code, one_byte);
} }
...@@ -791,9 +786,10 @@ function StringFromCharCode(code) { ...@@ -791,9 +786,10 @@ function StringFromCharCode(code) {
one_byte = %TruncateString(one_byte, i); one_byte = %TruncateString(one_byte, i);
var two_byte = %NewString(n - i, NEW_TWO_BYTE_STRING); var two_byte = %NewString(n - i, NEW_TWO_BYTE_STRING);
for (var j = 0; i < n; i++, j++) { %_TwoByteSeqStringSetChar(0, code, two_byte);
var code = %_Arguments(i); i++;
if (!%_IsSmi(code)) code = TO_NUMBER(code) & 0xffff; for (var j = 1; i < n; i++, j++) {
code = %_Arguments(i) & 0xffff;
%_TwoByteSeqStringSetChar(j, code, two_byte); %_TwoByteSeqStringSetChar(j, code, two_byte);
} }
return one_byte + two_byte; return one_byte + two_byte;
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var counter = 0;
var a = { valueOf: function() { counter++; return 0x100; } };
assertEquals("A\u0100\u0100\u0100", String.fromCharCode(65, a, a, a));
assertEquals(3, counter);
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