| 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 "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 | 6 |
| 7 #include "vm/assembler.h" | 7 #include "vm/assembler.h" |
| 8 #include "vm/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
| 9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| (...skipping 4093 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4104 cls = Object::dynamic_class(); | 4104 cls = Object::dynamic_class(); |
| 4105 array = cls.fields(); | 4105 array = cls.fields(); |
| 4106 EXPECT(!array.IsNull()); | 4106 EXPECT(!array.IsNull()); |
| 4107 EXPECT(array.IsArray()); | 4107 EXPECT(array.IsArray()); |
| 4108 array = cls.functions(); | 4108 array = cls.functions(); |
| 4109 EXPECT(!array.IsNull()); | 4109 EXPECT(!array.IsNull()); |
| 4110 EXPECT(array.IsArray()); | 4110 EXPECT(array.IsArray()); |
| 4111 } | 4111 } |
| 4112 | 4112 |
| 4113 | 4113 |
| 4114 TEST_CASE(ToUserCString) { | 4114 TEST_CASE(ToCStringTruncated) { |
| 4115 const char* kScriptChars = | 4115 const char* kScriptChars = |
| 4116 "var simple = 'simple';\n" | 4116 "var ascii = 'Hello, World!';\n" |
| 4117 "var escapes = 'stuff\\n\\r\\f\\b\\t\\v\\'\"\\$stuff';\n" | 4117 "var unicode = '\\u00CE\\u00F1\\u0163\\u00E9r\\u00F1\\u00E5\\u0163" |
| 4118 "var uescapes = 'stuff\\u0001\\u0002stuff';\n" | 4118 "\\u00EE\\u00F6\\u00F1\\u00E5\\u013C\\u00EE\\u017E\\u00E5\\u0163" |
| 4119 "var toolong = " | 4119 "\\u00EE\\u1EDD\\u00F1';\n" |
| 4120 "'01234567890123456789012345678901234567890123456789howdy';\n" | 4120 "var surrogates ='\\u{1D11E}\\u{1D11E}\\u{1D11E}\\u{1D11E}"\ |
| 4121 "var toolong2 = " | 4121 "\\u{1D11E}';\n"; |
| 4122 "'0123456789012345678901234567890123\\t567890123456789howdy';\n" | 4122 |
| 4123 "var toolong3 = " | |
| 4124 "'012345678901234567890123456789\\u0001567890123456789howdy';\n"; | |
| 4125 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 4123 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 4126 EXPECT_VALID(lib); | 4124 EXPECT_VALID(lib); |
| 4127 | 4125 |
| 4128 String& obj = String::Handle(); | 4126 String& obj = String::Handle(); |
| 4129 Dart_Handle result; | 4127 Dart_Handle result; |
| 4128 bool did_truncate; |
| 4129 intptr_t length; |
| 4130 | 4130 |
| 4131 // Simple string. | 4131 result = Dart_GetField(lib, NewString("ascii")); |
| 4132 result = Dart_GetField(lib, NewString("simple")); | |
| 4133 EXPECT_VALID(result); | 4132 EXPECT_VALID(result); |
| 4134 obj ^= Api::UnwrapHandle(result); | 4133 obj ^= Api::UnwrapHandle(result); |
| 4135 EXPECT_STREQ("\"simple\"", obj.ToUserCString(40)); | 4134 EXPECT_STREQ("Hello, World!", |
| 4135 obj.ToCStringTruncated(100, &did_truncate, &length)); |
| 4136 EXPECT(!did_truncate); |
| 4137 EXPECT_EQ(13, length); |
| 4138 EXPECT_STREQ("Hel", obj.ToCStringTruncated(3, &did_truncate, &length)); |
| 4139 EXPECT(did_truncate); |
| 4140 EXPECT_EQ(3, length); |
| 4136 | 4141 |
| 4137 // Escaped chars. | 4142 result = Dart_GetField(lib, NewString("unicode")); |
| 4138 result = Dart_GetField(lib, NewString("escapes")); | |
| 4139 EXPECT_VALID(result); | 4143 EXPECT_VALID(result); |
| 4140 obj ^= Api::UnwrapHandle(result); | 4144 obj ^= Api::UnwrapHandle(result); |
| 4141 EXPECT_STREQ("\"stuff\\n\\r\\f\\b\\t\\v'\\\"\\$stuff\"", | 4145 EXPECT_STREQ("\u00CE\u00F1\u0163\u00E9r\u00F1\u00E5\u0163" |
| 4142 obj.ToUserCString(40)); | 4146 "\u00EE\u00F6\u00F1\u00E5\u013C\u00EE\u017E\u00E5\u0163" |
| 4147 "\u00EE\u1EDD\u00F1", |
| 4148 obj.ToCStringTruncated(100, &did_truncate, &length)); |
| 4149 EXPECT(!did_truncate); |
| 4150 EXPECT_EQ(40, length); |
| 4151 EXPECT_STREQ("\u00CE\u00F1\u0163", |
| 4152 obj.ToCStringTruncated(3, &did_truncate, &length)); |
| 4153 EXPECT(did_truncate); |
| 4154 EXPECT_EQ(6, length); |
| 4143 | 4155 |
| 4144 // U-escaped chars. | 4156 result = Dart_GetField(lib, NewString("surrogates")); |
| 4145 result = Dart_GetField(lib, NewString("uescapes")); | |
| 4146 EXPECT_VALID(result); | 4157 EXPECT_VALID(result); |
| 4147 obj ^= Api::UnwrapHandle(result); | 4158 obj ^= Api::UnwrapHandle(result); |
| 4148 EXPECT_STREQ("\"stuff\\u0001\\u0002stuff\"", obj.ToUserCString(40)); | 4159 EXPECT_STREQ("\U0001D11E\U0001D11E\U0001D11E\U0001D11E\U0001D11E", |
| 4149 | 4160 obj.ToCStringTruncated(100, &did_truncate, &length)); |
| 4150 // Truncation. | 4161 EXPECT(!did_truncate); |
| 4151 result = Dart_GetField(lib, NewString("toolong")); | 4162 EXPECT_EQ(20, length); |
| 4152 EXPECT_VALID(result); | 4163 EXPECT_STREQ("\U0001D11E", |
| 4153 obj ^= Api::UnwrapHandle(result); | 4164 obj.ToCStringTruncated(3, &did_truncate, &length)); |
| 4154 EXPECT_STREQ("\"01234567890123456789012345678901234\"...", | 4165 EXPECT(did_truncate); |
| 4155 obj.ToUserCString(40)); | 4166 EXPECT_EQ(4, length); // 3 code units would be in the middle of a surrogate |
| 4156 | 4167 // pair, so it gets rounded down 2 code units. |
| 4157 // Truncation, shorter. | 4168 EXPECT_STREQ("\U0001D11E\U0001D11E", |
| 4158 result = Dart_GetField(lib, NewString("toolong")); | 4169 obj.ToCStringTruncated(4, &did_truncate, &length)); |
| 4159 EXPECT_VALID(result); | 4170 EXPECT(did_truncate); |
| 4160 obj ^= Api::UnwrapHandle(result); | 4171 EXPECT_EQ(8, length); |
| 4161 EXPECT_STREQ("\"01234\"...", obj.ToUserCString(10)); | |
| 4162 | |
| 4163 // Truncation, limit is in escape. | |
| 4164 result = Dart_GetField(lib, NewString("toolong2")); | |
| 4165 EXPECT_VALID(result); | |
| 4166 obj ^= Api::UnwrapHandle(result); | |
| 4167 EXPECT_STREQ("\"0123456789012345678901234567890123\"...", | |
| 4168 obj.ToUserCString(40)); | |
| 4169 | |
| 4170 // Truncation, limit is in u-escape | |
| 4171 result = Dart_GetField(lib, NewString("toolong3")); | |
| 4172 EXPECT_VALID(result); | |
| 4173 obj ^= Api::UnwrapHandle(result); | |
| 4174 EXPECT_STREQ("\"012345678901234567890123456789\"...", | |
| 4175 obj.ToUserCString(40)); | |
| 4176 } | 4172 } |
| 4177 | 4173 |
| 4178 | |
| 4179 class ObjectAccumulator : public ObjectVisitor { | 4174 class ObjectAccumulator : public ObjectVisitor { |
| 4180 public: | 4175 public: |
| 4181 explicit ObjectAccumulator(GrowableArray<Object*>* objects) | 4176 explicit ObjectAccumulator(GrowableArray<Object*>* objects) |
| 4182 : ObjectVisitor(Isolate::Current()), objects_(objects) {} | 4177 : ObjectVisitor(Isolate::Current()), objects_(objects) {} |
| 4183 virtual ~ObjectAccumulator() { } | 4178 virtual ~ObjectAccumulator() { } |
| 4184 virtual void VisitObject(RawObject* obj) { | 4179 virtual void VisitObject(RawObject* obj) { |
| 4185 // Free-list elements cannot even be wrapped in handles. | 4180 // Free-list elements cannot even be wrapped in handles. |
| 4186 if (obj->IsFreeListElement()) { | 4181 if (obj->IsFreeListElement()) { |
| 4187 return; | 4182 return; |
| 4188 } | 4183 } |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4365 JSONStream js; | 4360 JSONStream js; |
| 4366 const String& str = String::Handle(String::New("dw")); | 4361 const String& str = String::Handle(String::New("dw")); |
| 4367 str.PrintJSON(&js, true); | 4362 str.PrintJSON(&js, true); |
| 4368 elideSubstring("classes", js.ToCString(), buffer); | 4363 elideSubstring("classes", js.ToCString(), buffer); |
| 4369 elideSubstring("objects", buffer, buffer); | 4364 elideSubstring("objects", buffer, buffer); |
| 4370 elideSubstring("_OneByteString@", buffer, buffer); | 4365 elideSubstring("_OneByteString@", buffer, buffer); |
| 4371 EXPECT_STREQ( | 4366 EXPECT_STREQ( |
| 4372 "{\"type\":\"@String\"," | 4367 "{\"type\":\"@String\"," |
| 4373 "\"class\":{\"type\":\"@Class\",\"id\":\"\"," | 4368 "\"class\":{\"type\":\"@Class\",\"id\":\"\"," |
| 4374 "\"name\":\"_OneByteString\",\"_vmName\":\"\"}," | 4369 "\"name\":\"_OneByteString\",\"_vmName\":\"\"}," |
| 4375 "\"id\":\"\",\"valueAsString\":\"\\\"dw\\\"\"}", | 4370 "\"id\":\"\",\"valueAsString\":\"dw\"}", |
| 4376 buffer); | 4371 buffer); |
| 4377 } | 4372 } |
| 4378 // Array reference | 4373 // Array reference |
| 4379 { | 4374 { |
| 4380 JSONStream js; | 4375 JSONStream js; |
| 4381 const Array& array = Array::Handle(Array::New(0)); | 4376 const Array& array = Array::Handle(Array::New(0)); |
| 4382 array.PrintJSON(&js, true); | 4377 array.PrintJSON(&js, true); |
| 4383 elideSubstring("classes", js.ToCString(), buffer); | 4378 elideSubstring("classes", js.ToCString(), buffer); |
| 4384 elideSubstring("objects", buffer, buffer); | 4379 elideSubstring("objects", buffer, buffer); |
| 4385 elideSubstring("_List@", buffer, buffer); | 4380 elideSubstring("_List@", buffer, buffer); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4538 EXPECT_VALID(h_result); | 4533 EXPECT_VALID(h_result); |
| 4539 Integer& result = Integer::Handle(); | 4534 Integer& result = Integer::Handle(); |
| 4540 result ^= Api::UnwrapHandle(h_result); | 4535 result ^= Api::UnwrapHandle(h_result); |
| 4541 String& foo = String::Handle(String::New("foo")); | 4536 String& foo = String::Handle(String::New("foo")); |
| 4542 Integer& expected = Integer::Handle(); | 4537 Integer& expected = Integer::Handle(); |
| 4543 expected ^= foo.HashCode(); | 4538 expected ^= foo.HashCode(); |
| 4544 EXPECT(result.IsIdenticalTo(expected)); | 4539 EXPECT(result.IsIdenticalTo(expected)); |
| 4545 } | 4540 } |
| 4546 | 4541 |
| 4547 } // namespace dart | 4542 } // namespace dart |
| OLD | NEW |