| 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 6186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6918 | 6869 |
| 6919 // Test API call to make strings external. | 6870 // Test API call to make strings external. |
| 6920 static void MakeExternalCback(void* peer) { | 6871 static void MakeExternalCback(void* peer) { |
| 6921 *static_cast<int*>(peer) *= 2; | 6872 *static_cast<int*>(peer) *= 2; |
| 6922 } | 6873 } |
| 6923 | 6874 |
| 6924 | 6875 |
| 6925 TEST_CASE(MakeExternalString) { | 6876 TEST_CASE(MakeExternalString) { |
| 6926 int peer8 = 40; | 6877 int peer8 = 40; |
| 6927 int peer16 = 41; | 6878 int peer16 = 41; |
| 6879 int canonical_str_peer = 42; |
| 6928 intptr_t length = 0; | 6880 intptr_t length = 0; |
| 6929 intptr_t expected_length = 0; | 6881 intptr_t expected_length = 0; |
| 6930 { | 6882 { |
| 6931 Dart_EnterScope(); | 6883 Dart_EnterScope(); |
| 6932 | 6884 |
| 6933 // First test some negative conditions. | 6885 // First test some negative conditions. |
| 6934 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; | 6886 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 6935 const char* err = "string"; | 6887 const char* err = "string"; |
| 6936 Dart_Handle err_str = NewString(err); | 6888 Dart_Handle err_str = NewString(err); |
| 6937 Dart_Handle ext_err_str = Dart_NewExternalLatin1String( | 6889 Dart_Handle ext_err_str = Dart_NewExternalLatin1String( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6979 NULL); | 6931 NULL); |
| 6980 EXPECT(Dart_IsString(str)); | 6932 EXPECT(Dart_IsString(str)); |
| 6981 EXPECT(Dart_IsString(empty_str)); | 6933 EXPECT(Dart_IsString(empty_str)); |
| 6982 EXPECT(Dart_IsStringLatin1(str)); | 6934 EXPECT(Dart_IsStringLatin1(str)); |
| 6983 EXPECT(Dart_IsStringLatin1(empty_str)); | 6935 EXPECT(Dart_IsStringLatin1(empty_str)); |
| 6984 EXPECT(Dart_IsExternalString(str)); | 6936 EXPECT(Dart_IsExternalString(str)); |
| 6985 EXPECT(Dart_IsExternalString(empty_str)); | 6937 EXPECT(Dart_IsExternalString(empty_str)); |
| 6986 EXPECT_VALID(Dart_StringLength(str, &length)); | 6938 EXPECT_VALID(Dart_StringLength(str, &length)); |
| 6987 EXPECT_EQ(0, length); | 6939 EXPECT_EQ(0, length); |
| 6988 | 6940 |
| 6941 // Test with single character canonical string, it should not become |
| 6942 // external string but the peer should be setup for it. |
| 6943 Isolate* isolate = Isolate::Current(); |
| 6944 Dart_Handle canonical_str = Api::NewHandle(isolate, Symbols::New("*")); |
| 6945 EXPECT(Dart_IsString(canonical_str)); |
| 6946 EXPECT(!Dart_IsExternalString(canonical_str)); |
| 6947 uint8_t ext_canonical_str[kLength]; |
| 6948 str = Dart_MakeExternalString(canonical_str, |
| 6949 ext_canonical_str, |
| 6950 kLength, |
| 6951 &canonical_str_peer, |
| 6952 MakeExternalCback); |
| 6953 EXPECT(Dart_IsString(str)); |
| 6954 EXPECT(!Dart_IsExternalString(canonical_str)); |
| 6955 EXPECT_EQ(canonical_str, str); |
| 6956 EXPECT(Dart_IsString(canonical_str)); |
| 6957 EXPECT(!Dart_IsExternalString(canonical_str)); |
| 6958 |
| 6989 // Test with a one byte ascii string. | 6959 // Test with a one byte ascii string. |
| 6990 const char* ascii = "string"; | 6960 const char* ascii = "string"; |
| 6991 expected_length = strlen(ascii); | 6961 expected_length = strlen(ascii); |
| 6992 Dart_Handle ascii_str = NewString(ascii); | 6962 Dart_Handle ascii_str = NewString(ascii); |
| 6993 EXPECT_VALID(ascii_str); | 6963 EXPECT_VALID(ascii_str); |
| 6994 EXPECT(Dart_IsString(ascii_str)); | 6964 EXPECT(Dart_IsString(ascii_str)); |
| 6995 EXPECT(Dart_IsStringLatin1(ascii_str)); | 6965 EXPECT(Dart_IsStringLatin1(ascii_str)); |
| 6996 EXPECT(!Dart_IsExternalString(ascii_str)); | 6966 EXPECT(!Dart_IsExternalString(ascii_str)); |
| 6997 EXPECT_VALID(Dart_StringLength(ascii_str, &length)); | 6967 EXPECT_VALID(Dart_StringLength(ascii_str, &length)); |
| 6998 EXPECT_EQ(expected_length, length); | 6968 EXPECT_EQ(expected_length, length); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7049 EXPECT_EQ(expected_length, length); | 7019 EXPECT_EQ(expected_length, length); |
| 7050 EXPECT(Dart_IdentityEquals(str, utf16_str)); | 7020 EXPECT(Dart_IdentityEquals(str, utf16_str)); |
| 7051 for (intptr_t i = 0; i < length; i++) { | 7021 for (intptr_t i = 0; i < length; i++) { |
| 7052 EXPECT_EQ(0x4e8c, ext_utf16_str[i]); | 7022 EXPECT_EQ(0x4e8c, ext_utf16_str[i]); |
| 7053 } | 7023 } |
| 7054 | 7024 |
| 7055 Dart_ExitScope(); | 7025 Dart_ExitScope(); |
| 7056 } | 7026 } |
| 7057 EXPECT_EQ(40, peer8); | 7027 EXPECT_EQ(40, peer8); |
| 7058 EXPECT_EQ(41, peer16); | 7028 EXPECT_EQ(41, peer16); |
| 7029 EXPECT_EQ(42, canonical_str_peer); |
| 7059 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); | 7030 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
| 7060 EXPECT_EQ(80, peer8); | 7031 EXPECT_EQ(80, peer8); |
| 7061 EXPECT_EQ(82, peer16); | 7032 EXPECT_EQ(82, peer16); |
| 7033 EXPECT_EQ(42, canonical_str_peer); // "*" Symbol is not removed on GC. |
| 7062 } | 7034 } |
| 7063 | 7035 |
| 7064 | 7036 |
| 7065 TEST_CASE(ExternalizeConstantStrings) { | 7037 TEST_CASE(ExternalizeConstantStrings) { |
| 7066 const char* kScriptChars = | 7038 const char* kScriptChars = |
| 7067 "String testMain() {\n" | 7039 "String testMain() {\n" |
| 7068 " return 'constant string';\n" | 7040 " return 'constant string';\n" |
| 7069 "}\n"; | 7041 "}\n"; |
| 7070 | 7042 |
| 7071 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 7043 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7294 NewString("main"), | 7266 NewString("main"), |
| 7295 0, | 7267 0, |
| 7296 NULL); | 7268 NULL); |
| 7297 int64_t value = 0; | 7269 int64_t value = 0; |
| 7298 result = Dart_IntegerToInt64(result, &value); | 7270 result = Dart_IntegerToInt64(result, &value); |
| 7299 EXPECT_VALID(result); | 7271 EXPECT_VALID(result); |
| 7300 EXPECT_EQ(8, value); | 7272 EXPECT_EQ(8, value); |
| 7301 } | 7273 } |
| 7302 | 7274 |
| 7303 } // namespace dart | 7275 } // namespace dart |
| OLD | NEW |