| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/builtin.h" | 5 #include "bin/builtin.h" |
| 6 #include "include/dart_api.h" | 6 #include "include/dart_api.h" |
| 7 #include "include/dart_mirrors_api.h" | 7 #include "include/dart_mirrors_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
| 10 #include "platform/json.h" | 10 #include "platform/json.h" |
| (...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 EXPECT_VALID(utf8_str); | 636 EXPECT_VALID(utf8_str); |
| 637 EXPECT(Dart_IsString(utf8_str)); | 637 EXPECT(Dart_IsString(utf8_str)); |
| 638 | 638 |
| 639 uint8_t invalid[] = { 0xE4, 0xBA }; // underflow. | 639 uint8_t invalid[] = { 0xE4, 0xBA }; // underflow. |
| 640 Dart_Handle invalid_str = Dart_NewStringFromUTF8(invalid, | 640 Dart_Handle invalid_str = Dart_NewStringFromUTF8(invalid, |
| 641 ARRAY_SIZE(invalid)); | 641 ARRAY_SIZE(invalid)); |
| 642 EXPECT(Dart_IsError(invalid_str)); | 642 EXPECT(Dart_IsError(invalid_str)); |
| 643 } | 643 } |
| 644 | 644 |
| 645 | 645 |
| 646 TEST_CASE(ExternalStringGetPeer) { | |
| 647 Dart_Handle result; | |
| 648 | |
| 649 uint8_t data8[] = { 'o', 'n', 'e', 0xFF }; | |
| 650 int peer_data = 123; | |
| 651 void* peer = NULL; | |
| 652 | |
| 653 // Success. | |
| 654 Dart_Handle ext8 = Dart_NewExternalLatin1String(data8, ARRAY_SIZE(data8), | |
| 655 &peer_data, NULL); | |
| 656 EXPECT_VALID(ext8); | |
| 657 | |
| 658 result = Dart_ExternalStringGetPeer(ext8, &peer); | |
| 659 EXPECT_VALID(result); | |
| 660 EXPECT_EQ(&peer_data, peer); | |
| 661 | |
| 662 // NULL peer. | |
| 663 result = Dart_ExternalStringGetPeer(ext8, NULL); | |
| 664 EXPECT(Dart_IsError(result)); | |
| 665 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'peer' to be " | |
| 666 "non-null.", Dart_GetError(result)); | |
| 667 | |
| 668 // String is not external. | |
| 669 peer = NULL; | |
| 670 uint8_t utf8_data8[] = { 'o', 'n', 'e', 0x7F }; | |
| 671 Dart_Handle str8 = Dart_NewStringFromUTF8(utf8_data8, ARRAY_SIZE(data8)); | |
| 672 EXPECT_VALID(str8); | |
| 673 result = Dart_ExternalStringGetPeer(str8, &peer); | |
| 674 EXPECT(Dart_IsError(result)); | |
| 675 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'object' to be " | |
| 676 "an external String.", Dart_GetError(result)); | |
| 677 EXPECT(peer == NULL); | |
| 678 | |
| 679 // Not a String. | |
| 680 peer = NULL; | |
| 681 result = Dart_ExternalStringGetPeer(Dart_True(), &peer); | |
| 682 EXPECT(Dart_IsError(result)); | |
| 683 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'object' to be " | |
| 684 "of type String.", Dart_GetError(result)); | |
| 685 EXPECT(peer == NULL); | |
| 686 } | |
| 687 | |
| 688 | |
| 689 static void ExternalStringCallbackFinalizer(void* peer) { | 646 static void ExternalStringCallbackFinalizer(void* peer) { |
| 690 *static_cast<int*>(peer) *= 2; | 647 *static_cast<int*>(peer) *= 2; |
| 691 } | 648 } |
| 692 | 649 |
| 693 | 650 |
| 694 TEST_CASE(ExternalStringCallback) { | 651 TEST_CASE(ExternalStringCallback) { |
| 695 int peer8 = 40; | 652 int peer8 = 40; |
| 696 int peer16 = 41; | 653 int peer16 = 41; |
| 697 | 654 |
| 698 { | 655 { |
| 699 Dart_EnterScope(); | 656 Dart_EnterScope(); |
| 700 | 657 |
| 701 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; | 658 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 702 Dart_Handle obj8 = Dart_NewExternalLatin1String( | 659 Dart_Handle obj8 = Dart_NewExternalLatin1String( |
| 703 data8, | 660 data8, |
| 704 ARRAY_SIZE(data8), | 661 ARRAY_SIZE(data8), |
| 705 &peer8, | 662 &peer8, |
| 706 ExternalStringCallbackFinalizer); | 663 ExternalStringCallbackFinalizer); |
| 707 EXPECT_VALID(obj8); | 664 EXPECT_VALID(obj8); |
| 708 void* api_peer8 = NULL; | |
| 709 EXPECT_VALID(Dart_ExternalStringGetPeer(obj8, &api_peer8)); | |
| 710 EXPECT_EQ(api_peer8, &peer8); | |
| 711 | 665 |
| 712 uint16_t data16[] = { 'h', 'e', 'l', 'l', 'o' }; | 666 uint16_t data16[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 713 Dart_Handle obj16 = Dart_NewExternalUTF16String( | 667 Dart_Handle obj16 = Dart_NewExternalUTF16String( |
| 714 data16, | 668 data16, |
| 715 ARRAY_SIZE(data16), | 669 ARRAY_SIZE(data16), |
| 716 &peer16, | 670 &peer16, |
| 717 ExternalStringCallbackFinalizer); | 671 ExternalStringCallbackFinalizer); |
| 718 EXPECT_VALID(obj16); | 672 EXPECT_VALID(obj16); |
| 719 void* api_peer16 = NULL; | |
| 720 EXPECT_VALID(Dart_ExternalStringGetPeer(obj16, &api_peer16)); | |
| 721 EXPECT_EQ(api_peer16, &peer16); | |
| 722 | 673 |
| 723 Dart_ExitScope(); | 674 Dart_ExitScope(); |
| 724 } | 675 } |
| 725 | 676 |
| 726 EXPECT_EQ(40, peer8); | 677 EXPECT_EQ(40, peer8); |
| 727 EXPECT_EQ(41, peer16); | 678 EXPECT_EQ(41, peer16); |
| 728 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); | 679 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
| 729 EXPECT_EQ(40, peer8); | 680 EXPECT_EQ(40, peer8); |
| 730 EXPECT_EQ(41, peer16); | 681 EXPECT_EQ(41, peer16); |
| 731 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); | 682 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
| (...skipping 6201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6933 | 6884 |
| 6934 // Test API call to make strings external. | 6885 // Test API call to make strings external. |
| 6935 static void MakeExternalCback(void* peer) { | 6886 static void MakeExternalCback(void* peer) { |
| 6936 *static_cast<int*>(peer) *= 2; | 6887 *static_cast<int*>(peer) *= 2; |
| 6937 } | 6888 } |
| 6938 | 6889 |
| 6939 | 6890 |
| 6940 TEST_CASE(MakeExternalString) { | 6891 TEST_CASE(MakeExternalString) { |
| 6941 int peer8 = 40; | 6892 int peer8 = 40; |
| 6942 int peer16 = 41; | 6893 int peer16 = 41; |
| 6894 int canonical_str_peer = 42; |
| 6943 intptr_t length = 0; | 6895 intptr_t length = 0; |
| 6944 intptr_t expected_length = 0; | 6896 intptr_t expected_length = 0; |
| 6945 { | 6897 { |
| 6946 Dart_EnterScope(); | 6898 Dart_EnterScope(); |
| 6947 | 6899 |
| 6948 // First test some negative conditions. | 6900 // First test some negative conditions. |
| 6949 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; | 6901 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 6950 const char* err = "string"; | 6902 const char* err = "string"; |
| 6951 Dart_Handle err_str = NewString(err); | 6903 Dart_Handle err_str = NewString(err); |
| 6952 Dart_Handle ext_err_str = Dart_NewExternalLatin1String( | 6904 Dart_Handle ext_err_str = Dart_NewExternalLatin1String( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6994 NULL); | 6946 NULL); |
| 6995 EXPECT(Dart_IsString(str)); | 6947 EXPECT(Dart_IsString(str)); |
| 6996 EXPECT(Dart_IsString(empty_str)); | 6948 EXPECT(Dart_IsString(empty_str)); |
| 6997 EXPECT(Dart_IsStringLatin1(str)); | 6949 EXPECT(Dart_IsStringLatin1(str)); |
| 6998 EXPECT(Dart_IsStringLatin1(empty_str)); | 6950 EXPECT(Dart_IsStringLatin1(empty_str)); |
| 6999 EXPECT(Dart_IsExternalString(str)); | 6951 EXPECT(Dart_IsExternalString(str)); |
| 7000 EXPECT(Dart_IsExternalString(empty_str)); | 6952 EXPECT(Dart_IsExternalString(empty_str)); |
| 7001 EXPECT_VALID(Dart_StringLength(str, &length)); | 6953 EXPECT_VALID(Dart_StringLength(str, &length)); |
| 7002 EXPECT_EQ(0, length); | 6954 EXPECT_EQ(0, length); |
| 7003 | 6955 |
| 6956 // Test with single character canonical string, it should not become |
| 6957 // external string but the peer should be setup for it. |
| 6958 Isolate* isolate = Isolate::Current(); |
| 6959 Dart_Handle canonical_str = Api::NewHandle(isolate, Symbols::New("*")); |
| 6960 EXPECT(Dart_IsString(canonical_str)); |
| 6961 EXPECT(!Dart_IsExternalString(canonical_str)); |
| 6962 uint8_t ext_canonical_str[kLength]; |
| 6963 str = Dart_MakeExternalString(canonical_str, |
| 6964 ext_canonical_str, |
| 6965 kLength, |
| 6966 &canonical_str_peer, |
| 6967 MakeExternalCback); |
| 6968 EXPECT(Dart_IsString(str)); |
| 6969 EXPECT(!Dart_IsExternalString(canonical_str)); |
| 6970 EXPECT_EQ(canonical_str, str); |
| 6971 EXPECT(Dart_IsString(canonical_str)); |
| 6972 EXPECT(!Dart_IsExternalString(canonical_str)); |
| 6973 |
| 7004 // Test with a one byte ascii string. | 6974 // Test with a one byte ascii string. |
| 7005 const char* ascii = "string"; | 6975 const char* ascii = "string"; |
| 7006 expected_length = strlen(ascii); | 6976 expected_length = strlen(ascii); |
| 7007 Dart_Handle ascii_str = NewString(ascii); | 6977 Dart_Handle ascii_str = NewString(ascii); |
| 7008 EXPECT_VALID(ascii_str); | 6978 EXPECT_VALID(ascii_str); |
| 7009 EXPECT(Dart_IsString(ascii_str)); | 6979 EXPECT(Dart_IsString(ascii_str)); |
| 7010 EXPECT(Dart_IsStringLatin1(ascii_str)); | 6980 EXPECT(Dart_IsStringLatin1(ascii_str)); |
| 7011 EXPECT(!Dart_IsExternalString(ascii_str)); | 6981 EXPECT(!Dart_IsExternalString(ascii_str)); |
| 7012 EXPECT_VALID(Dart_StringLength(ascii_str, &length)); | 6982 EXPECT_VALID(Dart_StringLength(ascii_str, &length)); |
| 7013 EXPECT_EQ(expected_length, length); | 6983 EXPECT_EQ(expected_length, length); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7064 EXPECT_EQ(expected_length, length); | 7034 EXPECT_EQ(expected_length, length); |
| 7065 EXPECT(Dart_IdentityEquals(str, utf16_str)); | 7035 EXPECT(Dart_IdentityEquals(str, utf16_str)); |
| 7066 for (intptr_t i = 0; i < length; i++) { | 7036 for (intptr_t i = 0; i < length; i++) { |
| 7067 EXPECT_EQ(0x4e8c, ext_utf16_str[i]); | 7037 EXPECT_EQ(0x4e8c, ext_utf16_str[i]); |
| 7068 } | 7038 } |
| 7069 | 7039 |
| 7070 Dart_ExitScope(); | 7040 Dart_ExitScope(); |
| 7071 } | 7041 } |
| 7072 EXPECT_EQ(40, peer8); | 7042 EXPECT_EQ(40, peer8); |
| 7073 EXPECT_EQ(41, peer16); | 7043 EXPECT_EQ(41, peer16); |
| 7044 EXPECT_EQ(42, canonical_str_peer); |
| 7074 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); | 7045 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
| 7075 EXPECT_EQ(80, peer8); | 7046 EXPECT_EQ(80, peer8); |
| 7076 EXPECT_EQ(82, peer16); | 7047 EXPECT_EQ(82, peer16); |
| 7048 EXPECT_EQ(42, canonical_str_peer); // "*" Symbol is not removed on GC. |
| 7077 } | 7049 } |
| 7078 | 7050 |
| 7079 | 7051 |
| 7080 TEST_CASE(ExternalizeConstantStrings) { | 7052 TEST_CASE(ExternalizeConstantStrings) { |
| 7081 const char* kScriptChars = | 7053 const char* kScriptChars = |
| 7082 "String testMain() {\n" | 7054 "String testMain() {\n" |
| 7083 " return 'constant string';\n" | 7055 " return 'constant string';\n" |
| 7084 "}\n"; | 7056 "}\n"; |
| 7085 | 7057 |
| 7086 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 7058 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7309 NewString("main"), | 7281 NewString("main"), |
| 7310 0, | 7282 0, |
| 7311 NULL); | 7283 NULL); |
| 7312 int64_t value = 0; | 7284 int64_t value = 0; |
| 7313 result = Dart_IntegerToInt64(result, &value); | 7285 result = Dart_IntegerToInt64(result, &value); |
| 7314 EXPECT_VALID(result); | 7286 EXPECT_VALID(result); |
| 7315 EXPECT_EQ(8, value); | 7287 EXPECT_EQ(8, value); |
| 7316 } | 7288 } |
| 7317 | 7289 |
| 7318 } // namespace dart | 7290 } // namespace dart |
| OLD | NEW |