Inline String.fromCharCode in hydrogen.
authoryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 17 Apr 2013 16:37:11 +0000 (16:37 +0000)
committeryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 17 Apr 2013 16:37:11 +0000 (16:37 +0000)
BUG=

Review URL: https://chromiumcodereview.appspot.com/14296009

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14315 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/hydrogen.cc
src/runtime.cc
test/mjsunit/string-fromcharcode.js

index a628ec0..09e3fe9 100644 (file)
@@ -8557,6 +8557,18 @@ bool HOptimizedGraphBuilder::TryInlineBuiltinMethodCall(
         return true;
       }
       break;
+    case kStringFromCharCode:
+      if (argument_count == 2 && check_type == RECEIVER_MAP_CHECK) {
+        AddCheckConstantFunction(expr->holder(), receiver, receiver_map);
+        HValue* argument = Pop();
+        HValue* context = environment()->LookupContext();
+        Drop(1);  // Receiver.
+        HInstruction* result =
+            HStringCharFromCode::New(zone(), context, argument);
+        ast_context()->ReturnInstruction(result, expr->id());
+        return true;
+      }
+      break;
     case kMathExp:
       if (!FLAG_fast_math) break;
       // Fall through if FLAG_fast_math.
index 7e7d6d5..3fd5f6d 100644 (file)
@@ -2417,11 +2417,9 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSGeneratorObject) {
 
 MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate,
                                                  Object* char_code) {
-  uint32_t code;
-  if (char_code->ToArrayIndex(&code)) {
-    if (code <= 0xffff) {
-      return isolate->heap()->LookupSingleCharacterStringFromCode(code);
-    }
+  if (char_code->IsNumber()) {
+    return isolate->heap()->LookupSingleCharacterStringFromCode(
+        NumberToUint32(char_code) & 0xffff);
   }
   return isolate->heap()->empty_string();
 }
index 1986dda..631c043 100644 (file)
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+// Flags: --allow-natives-syntax
+
 // Test String.fromCharCode.
 
+// Test char codes larger than 0xffff.
+var expected = "";
+for (var i = 100; i < 500; i++) {
+  expected += String.fromCharCode(i);
+}
+
+function testCharCodeTruncation() {
+  var result = "";
+  for (var i = 0x100000 + 100; i < 0x100000 + 500; i++) {
+    result += String.fromCharCode(i);
+  }
+  assertEquals(String.fromCharCode(0xFFFF), String.fromCharCode(0xFFFFFFFF));
+  return result;
+}
+
+assertEquals(expected, testCharCodeTruncation());
+assertEquals(expected, testCharCodeTruncation());
+%OptimizeFunctionOnNextCall(testCharCodeTruncation);
+assertEquals(expected, testCharCodeTruncation());
 
 // Test various receivers and arguments passed to String.fromCharCode.