| 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 3929 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3940 | 3940 |
| 3941 // Now invoke the closure and check the result (should be an exception). | 3941 // Now invoke the closure and check the result (should be an exception). |
| 3942 dart_arguments[0] = Dart_NewInteger(1); | 3942 dart_arguments[0] = Dart_NewInteger(1); |
| 3943 result = Dart_InvokeClosure(retobj, 1, dart_arguments); | 3943 result = Dart_InvokeClosure(retobj, 1, dart_arguments); |
| 3944 EXPECT(Dart_IsError(result)); | 3944 EXPECT(Dart_IsError(result)); |
| 3945 EXPECT(Dart_ErrorHasException(result)); | 3945 EXPECT(Dart_ErrorHasException(result)); |
| 3946 } | 3946 } |
| 3947 | 3947 |
| 3948 | 3948 |
| 3949 void ExceptionNative(Dart_NativeArguments args) { | 3949 void ExceptionNative(Dart_NativeArguments args) { |
| 3950 Dart_Handle param = Dart_GetNativeArgument(args, 0); | 3950 Dart_EnterScope(); |
| 3951 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 3951 Dart_ThrowException(Dart_NewString("Hello from ExceptionNative!")); |
| 3952 Dart_ThrowException(param); | |
| 3953 UNREACHABLE(); | 3952 UNREACHABLE(); |
| 3954 } | 3953 } |
| 3955 | 3954 |
| 3956 | 3955 |
| 3957 static Dart_NativeFunction native_lookup(Dart_Handle name, int argument_count) { | 3956 static Dart_NativeFunction native_lookup(Dart_Handle name, int argument_count) { |
| 3958 return reinterpret_cast<Dart_NativeFunction>(&ExceptionNative); | 3957 return reinterpret_cast<Dart_NativeFunction>(&ExceptionNative); |
| 3959 } | 3958 } |
| 3960 | 3959 |
| 3961 | 3960 |
| 3962 TEST_CASE(ThrowException) { | 3961 TEST_CASE(ThrowException) { |
| 3963 const char* kScriptChars = | 3962 const char* kScriptChars = |
| 3964 "class ThrowException {\n" | 3963 "int test() native \"ThrowException_native\";"; |
| 3965 " ThrowException(int i) : fld1 = i {}\n" | |
| 3966 " int thrower(int i) native \"ThrowException_native\";" | |
| 3967 " int test() {\n" | |
| 3968 " try { thrower(10); } catch(var a) { return 5; } return 10;\n" | |
| 3969 " }\n" | |
| 3970 " int fld1;\n" | |
| 3971 "}\n" | |
| 3972 "ThrowException testMain() {\n" | |
| 3973 " ThrowException obj = new ThrowException(10);\n" | |
| 3974 " return obj;\n" | |
| 3975 "}\n"; | |
| 3976 Dart_Handle result; | 3964 Dart_Handle result; |
| 3977 Isolate* isolate = Isolate::Current(); | 3965 Isolate* isolate = Isolate::Current(); |
| 3978 EXPECT(isolate != NULL); | 3966 EXPECT(isolate != NULL); |
| 3979 ApiState* state = isolate->api_state(); | 3967 ApiState* state = isolate->api_state(); |
| 3980 EXPECT(state != NULL); | 3968 EXPECT(state != NULL); |
| 3981 intptr_t size = state->ZoneSizeInBytes(); | 3969 intptr_t size = state->ZoneSizeInBytes(); |
| 3982 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 3970 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 3983 | 3971 |
| 3984 // Load up a test script which extends the native wrapper class. | 3972 // Load up a test script which extends the native wrapper class. |
| 3985 Dart_Handle lib = TestCase::LoadTestScript( | 3973 Dart_Handle lib = TestCase::LoadTestScript( |
| 3986 kScriptChars, | 3974 kScriptChars, |
| 3987 reinterpret_cast<Dart_NativeEntryResolver>(native_lookup)); | 3975 reinterpret_cast<Dart_NativeEntryResolver>(native_lookup)); |
| 3988 | 3976 |
| 3989 // Invoke a function which returns an object of type ThrowException. | 3977 // Throwing an exception here should result in an error. |
| 3990 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL); | 3978 result = Dart_ThrowException(Dart_NewString("This doesn't work")); |
| 3991 EXPECT_VALID(retobj); | 3979 EXPECT_ERROR(result, "No Dart frames on stack, cannot throw exception"); |
| 3980 EXPECT(!Dart_ErrorHasException(result)); |
| 3992 | 3981 |
| 3993 // Throwing an exception here should result in an error. | 3982 // Invoke 'test' and check for an uncaught exception. |
| 3994 result = Dart_ThrowException(retobj); | 3983 result = Dart_Invoke(lib, Dart_NewString("test"), 0, NULL); |
| 3995 EXPECT(Dart_IsError(result)); | 3984 EXPECT_ERROR(result, "Hello from ExceptionNative!"); |
| 3996 | 3985 EXPECT(Dart_ErrorHasException(result)); |
| 3997 // Now invoke 'test' which indirectly invokes a natve method where | |
| 3998 // it is ok to throw an exception, check the result which would | |
| 3999 // indicate if an exception was thrown or not. | |
| 4000 result = Dart_Invoke(retobj, Dart_NewString("test"), 0, NULL); | |
| 4001 EXPECT_VALID(result); | |
| 4002 EXPECT(Dart_IsInteger(result)); | |
| 4003 int64_t value = 0; | |
| 4004 result = Dart_IntegerToInt64(result, &value); | |
| 4005 EXPECT_EQ(5, value); | |
| 4006 | 3986 |
| 4007 Dart_ExitScope(); // Exit the Dart API scope. | 3987 Dart_ExitScope(); // Exit the Dart API scope. |
| 4008 EXPECT_EQ(size, state->ZoneSizeInBytes()); | 3988 EXPECT_EQ(size, state->ZoneSizeInBytes()); |
| 4009 } | 3989 } |
| 4010 | 3990 |
| 4011 | 3991 |
| 4012 void NativeArgumentCounter(Dart_NativeArguments args) { | 3992 void NativeArgumentCounter(Dart_NativeArguments args) { |
| 4013 Dart_EnterScope(); | 3993 Dart_EnterScope(); |
| 4014 int count = Dart_GetNativeArgumentCount(args); | 3994 int count = Dart_GetNativeArgumentCount(args); |
| 4015 Dart_SetReturnValue(args, Dart_NewInteger(count)); | 3995 Dart_SetReturnValue(args, Dart_NewInteger(count)); |
| (...skipping 2517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6533 EXPECT(Dart_IsString(str)); | 6513 EXPECT(Dart_IsString(str)); |
| 6534 len = -1; | 6514 len = -1; |
| 6535 EXPECT_VALID(Dart_StringLength(str, &len)); | 6515 EXPECT_VALID(Dart_StringLength(str, &len)); |
| 6536 EXPECT_EQ(0, len); | 6516 EXPECT_EQ(0, len); |
| 6537 } | 6517 } |
| 6538 | 6518 |
| 6539 | 6519 |
| 6540 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 6520 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 6541 | 6521 |
| 6542 } // namespace dart | 6522 } // namespace dart |
| OLD | NEW |