| 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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
| 6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 7 #include "platform/json.h" | 7 #include "platform/json.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.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 7433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7444 | 7444 |
| 7445 Dart_Handle source = NewString(kLoadSecond); | 7445 Dart_Handle source = NewString(kLoadSecond); |
| 7446 Dart_Handle url = NewString(TestCase::url()); | 7446 Dart_Handle url = NewString(TestCase::url()); |
| 7447 Dart_LoadSource(TestCase::lib(), url, source); | 7447 Dart_LoadSource(TestCase::lib(), url, source); |
| 7448 | 7448 |
| 7449 dart_args[0] = Dart_NewInteger(1); | 7449 dart_args[0] = Dart_NewInteger(1); |
| 7450 result = Dart_Invoke(lib1, NewString("start"), 1, dart_args); | 7450 result = Dart_Invoke(lib1, NewString("start"), 1, dart_args); |
| 7451 EXPECT_VALID(result); | 7451 EXPECT_VALID(result); |
| 7452 } | 7452 } |
| 7453 | 7453 |
| 7454 |
| 7455 // Test external strings and optimized code. |
| 7456 static void ExternalStringDeoptimize_Finalize(void* peer) { |
| 7457 delete[] reinterpret_cast<char*>(peer); |
| 7458 } |
| 7459 |
| 7460 |
| 7461 static void A_change_str_native(Dart_NativeArguments args) { |
| 7462 Dart_EnterScope(); |
| 7463 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
| 7464 EXPECT(Dart_IsString(str)); |
| 7465 intptr_t size = 0; |
| 7466 EXPECT_VALID(Dart_StringStorageSize(str, &size)); |
| 7467 char* str_data = new char[size]; |
| 7468 Dart_Handle result = |
| 7469 Dart_MakeExternalString(str, |
| 7470 str_data, |
| 7471 size, |
| 7472 str_data, |
| 7473 &ExternalStringDeoptimize_Finalize); |
| 7474 EXPECT_VALID(result); |
| 7475 Dart_ExitScope(); |
| 7476 } |
| 7477 |
| 7478 |
| 7479 static Dart_NativeFunction ExternalStringDeoptimize_native_lookup( |
| 7480 Dart_Handle name, int argument_count) { |
| 7481 return reinterpret_cast<Dart_NativeFunction>(&A_change_str_native); |
| 7482 } |
| 7483 |
| 7484 |
| 7485 TEST_CASE(ExternalStringDeoptimize) { |
| 7486 const char* kScriptChars = |
| 7487 "String str = 'A';\n" |
| 7488 "class A {\n" |
| 7489 " static change_str(String s) native 'A_change_str';\n" |
| 7490 "}\n" |
| 7491 "sum_chars(String s, bool b) {\n" |
| 7492 " var result = 0;\n" |
| 7493 " for (var i = 0; i < s.length; i++) {\n" |
| 7494 " if (b && i == 0) A.change_str(str);\n" |
| 7495 " result += s.charCodeAt(i);" |
| 7496 " }\n" |
| 7497 " return result;\n" |
| 7498 "}\n" |
| 7499 "main() {\n" |
| 7500 " str = '$str$str';\n" |
| 7501 " for (var i = 0; i < 2000; i++) sum_chars(str, false);\n" |
| 7502 " var x = sum_chars(str, false);\n" |
| 7503 " var y = sum_chars(str, true);\n" |
| 7504 " return x + y;\n" |
| 7505 "}\n"; |
| 7506 Dart_Handle lib = |
| 7507 TestCase::LoadTestScript(kScriptChars, |
| 7508 &ExternalStringDeoptimize_native_lookup); |
| 7509 Dart_Handle result = Dart_Invoke(lib, |
| 7510 NewString("main"), |
| 7511 0, |
| 7512 NULL); |
| 7513 int64_t value = 0; |
| 7514 result = Dart_IntegerToInt64(result, &value); |
| 7515 EXPECT_VALID(result); |
| 7516 EXPECT_EQ(260, value); |
| 7517 } |
| 7518 |
| 7454 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 7519 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 7455 | 7520 |
| 7456 } // namespace dart | 7521 } // namespace dart |
| OLD | NEW |