Flutter Engine
The Flutter Engine
exceptions_test.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "include/dart_api.h"
6#include "platform/assert.h"
7#include "vm/dart_api_impl.h"
8#include "vm/unit_test.h"
9
10namespace dart {
11
12#define FUNCTION_NAME(name) UnhandledExcp_##name
13#define REGISTER_FUNCTION(name, count) {"" #name, FUNCTION_NAME(name), count},
14
16 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
17 TransitionNativeToVM transition(arguments->thread());
18 Zone* zone = arguments->thread()->zone();
19 const Instance& expected =
20 Instance::CheckedHandle(zone, arguments->NativeArgAt(0));
21 const Instance& actual =
22 Instance::CheckedHandle(zone, arguments->NativeArgAt(1));
23 if (!expected.CanonicalizeEquals(actual)) {
24 OS::PrintErr("expected: '%s' actual: '%s'\n", expected.ToCString(),
25 actual.ToCString());
26 FATAL("Unhandled_equals fails.\n");
27 }
28}
29
31 // Invoke the specified entry point.
33 Dart_Handle result = Dart_Invoke(cls, NewString("method2"), 0, nullptr);
36 return;
37}
38
40 // Invoke the specified entry point.
42 Dart_Handle result = Dart_Invoke(cls, NewString("method2"), 0, nullptr);
46 ASSERT(!Dart_IsError(exception));
47 Dart_ThrowException(exception);
49 return;
50}
51
52// List all native functions implemented in the vm or core boot strap dart
53// libraries so that we can resolve the native function to it's entry
54// point.
55#define UNHANDLED_NATIVE_LIST(V) \
56 V(Unhandled_equals, 2) \
57 V(Unhandled_invoke, 0) \
58 V(Unhandled_invoke2, 0)
59
60static struct NativeEntries {
61 const char* name_;
65
68 bool* auto_setup_scope) {
69 ASSERT(auto_setup_scope != nullptr);
70 *auto_setup_scope = true;
73 ASSERT(obj.IsString());
74 const char* function_name = obj.ToCString();
75 ASSERT(function_name != nullptr);
76 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries);
77 for (int i = 0; i < num_entries; i++) {
78 struct NativeEntries* entry = &(BuiltinEntries[i]);
79 if ((strcmp(function_name, entry->name_) == 0) &&
80 (argument_count == entry->argument_count_)) {
81 return reinterpret_cast<Dart_NativeFunction>(entry->function_);
82 }
83 }
84 return nullptr;
85}
86
87// Unit test case to verify unhandled exceptions.
88TEST_CASE(UnhandledExceptions) {
89 const char* kScriptChars =
90 R"(
91 class UnhandledExceptions {
92 @pragma('vm:external-name', 'Unhandled_equals')
93 external static equals(var obj1, var obj2);
94
95 @pragma('vm:external-name', 'Unhandled_invoke')
96 external static invoke();
97
98 @pragma('vm:external-name', 'Unhandled_invoke2')
99 external static invoke2();
100 }
101 class Second {
102 Second() { }
103 static int method1(int param) {
104 UnhandledExceptions.invoke();
105 return 2;
106 }
107 static int method2() {
108 throw new Second();
109 }
110 static int method3(int param) {
111 try {
112 UnhandledExceptions.invoke2();
113 } on Second catch (e) {
114 return 3;
115 }
116 return 2;
117 }
118 }
119 testMain() {
120 UnhandledExceptions.equals(2, Second.method1(1));
121 UnhandledExceptions.equals(3, Second.method3(1));
122 }
123 )";
125 EXPECT_VALID(Dart_Invoke(lib, NewString("testMain"), 0, nullptr));
126}
127
128} // namespace dart
#define UNREACHABLE()
Definition: assert.h:248
static ObjectPtr UnwrapHandle(Dart_Handle object)
virtual bool CanonicalizeEquals(const Instance &other) const
Definition: object.cc:20259
Thread * thread() const
ObjectPtr NativeArgAt(int index) const
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
virtual const char * ToCString() const
Definition: object.h:366
static Object & Handle()
Definition: object.h:407
static Dart_Handle lib()
Definition: unit_test.cc:643
static Dart_Handle LoadTestScript(const char *script, Dart_NativeEntryResolver resolver, const char *lib_uri=RESOLVED_USER_TEST_URI, bool finalize=true, bool allow_compile_errors=false)
Definition: unit_test.cc:436
Zone * zone() const
Definition: thread_state.h:37
static Thread * Current()
Definition: thread.h:362
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
#define ASSERT(E)
#define UNHANDLED_NATIVE_LIST(V)
#define REGISTER_FUNCTION(name, count)
#define FUNCTION_NAME(name)
#define FATAL(error)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
int argument_count
Definition: fuchsia.cc:52
Definition: dart_vm.cc:33
const char *const name
DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target, Dart_Handle name, int number_of_arguments, Dart_Handle *arguments)
void FUNCTION_NAME() Unhandled_equals(Dart_NativeArguments args)
DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle class_name)
void FUNCTION_NAME() Unhandled_invoke(Dart_NativeArguments args)
static Dart_NativeFunction native_lookup(Dart_Handle name, int argument_count, bool *auto_setup_scope)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
static struct dart::NativeEntries BuiltinEntries[]
TEST_CASE(DirectoryCurrent)
Dart_Handle NewString(const char *str)
void FUNCTION_NAME() Unhandled_invoke2(Dart_NativeArguments args)
DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
const char *const function_name
Dart_NativeFunction function_
#define EXPECT_VALID(handle)
Definition: unit_test.h:643