2012-01-16 12:28:10 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-10-05 05:20:07 +00:00
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "include/dart_api.h"
|
2012-01-16 12:28:10 +00:00
|
|
|
#include "platform/assert.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
#include "vm/dart_api_impl.h"
|
|
|
|
#include "vm/unit_test.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
|
|
|
#define FUNCTION_NAME(name) UnhandledExcp_##name
|
2016-11-08 21:54:47 +00:00
|
|
|
#define REGISTER_FUNCTION(name, count) {"" #name, FUNCTION_NAME(name), count},
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
void FUNCTION_NAME(Unhandled_equals)(Dart_NativeArguments args) {
|
|
|
|
NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
|
2018-11-08 02:02:39 +00:00
|
|
|
TransitionNativeToVM transition(arguments->thread());
|
|
|
|
Zone* zone = arguments->thread()->zone();
|
|
|
|
const Instance& expected =
|
|
|
|
Instance::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
|
|
const Instance& actual =
|
|
|
|
Instance::CheckedHandle(zone, arguments->NativeArgAt(1));
|
2014-05-12 20:53:23 +00:00
|
|
|
if (!expected.CanonicalizeEquals(actual)) {
|
2018-06-13 19:51:40 +00:00
|
|
|
OS::PrintErr("expected: '%s' actual: '%s'\n", expected.ToCString(),
|
|
|
|
actual.ToCString());
|
2011-10-05 05:20:07 +00:00
|
|
|
FATAL("Unhandled_equals fails.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUNCTION_NAME(Unhandled_invoke)(Dart_NativeArguments args) {
|
|
|
|
// Invoke the specified entry point.
|
2012-10-31 17:56:46 +00:00
|
|
|
Dart_Handle cls = Dart_GetClass(TestCase::lib(), NewString("Second"));
|
2016-11-08 21:54:47 +00:00
|
|
|
Dart_Handle result = Dart_Invoke(cls, NewString("method2"), 0, NULL);
|
2011-11-11 19:31:04 +00:00
|
|
|
ASSERT(Dart_IsError(result));
|
|
|
|
ASSERT(Dart_ErrorHasException(result));
|
2011-10-05 05:20:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUNCTION_NAME(Unhandled_invoke2)(Dart_NativeArguments args) {
|
|
|
|
// Invoke the specified entry point.
|
2012-10-31 17:56:46 +00:00
|
|
|
Dart_Handle cls = Dart_GetClass(TestCase::lib(), NewString("Second"));
|
2016-11-08 21:54:47 +00:00
|
|
|
Dart_Handle result = Dart_Invoke(cls, NewString("method2"), 0, NULL);
|
2011-11-11 19:31:04 +00:00
|
|
|
ASSERT(Dart_IsError(result));
|
|
|
|
ASSERT(Dart_ErrorHasException(result));
|
|
|
|
Dart_Handle exception = Dart_ErrorGetException(result);
|
|
|
|
ASSERT(!Dart_IsError(exception));
|
2011-10-05 05:20:07 +00:00
|
|
|
Dart_ThrowException(exception);
|
|
|
|
UNREACHABLE();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// List all native functions implemented in the vm or core boot strap dart
|
|
|
|
// libraries so that we can resolve the native function to it's entry
|
|
|
|
// point.
|
|
|
|
#define UNHANDLED_NATIVE_LIST(V) \
|
|
|
|
V(Unhandled_equals, 2) \
|
|
|
|
V(Unhandled_invoke, 0) \
|
2016-11-08 21:54:47 +00:00
|
|
|
V(Unhandled_invoke2, 0)
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
static struct NativeEntries {
|
|
|
|
const char* name_;
|
|
|
|
Dart_NativeFunction function_;
|
|
|
|
int argument_count_;
|
2016-11-08 21:54:47 +00:00
|
|
|
} BuiltinEntries[] = {UNHANDLED_NATIVE_LIST(REGISTER_FUNCTION)};
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
static Dart_NativeFunction native_lookup(Dart_Handle name,
|
2013-12-19 17:47:56 +00:00
|
|
|
int argument_count,
|
|
|
|
bool* auto_setup_scope) {
|
|
|
|
ASSERT(auto_setup_scope != NULL);
|
2016-02-01 18:57:34 +00:00
|
|
|
*auto_setup_scope = true;
|
2018-10-31 19:51:52 +00:00
|
|
|
TransitionNativeToVM transition(Thread::Current());
|
2011-10-05 05:20:07 +00:00
|
|
|
const Object& obj = Object::Handle(Api::UnwrapHandle(name));
|
|
|
|
ASSERT(obj.IsString());
|
|
|
|
const char* function_name = obj.ToCString();
|
|
|
|
ASSERT(function_name != NULL);
|
|
|
|
int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries);
|
|
|
|
for (int i = 0; i < num_entries; i++) {
|
|
|
|
struct NativeEntries* entry = &(BuiltinEntries[i]);
|
2019-09-05 21:41:42 +00:00
|
|
|
if ((strcmp(function_name, entry->name_) == 0) &&
|
2012-07-10 23:59:09 +00:00
|
|
|
(argument_count == entry->argument_count_)) {
|
2011-10-05 05:20:07 +00:00
|
|
|
return reinterpret_cast<Dart_NativeFunction>(entry->function_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unit test case to verify unhandled exceptions.
|
|
|
|
TEST_CASE(UnhandledExceptions) {
|
|
|
|
const char* kScriptChars =
|
|
|
|
"class UnhandledExceptions {\n"
|
|
|
|
" static equals(var obj1, var obj2) native \"Unhandled_equals\";"
|
|
|
|
" static invoke() native \"Unhandled_invoke\";\n"
|
|
|
|
" static invoke2() native \"Unhandled_invoke2\";\n"
|
|
|
|
"}\n"
|
|
|
|
"class Second {\n"
|
|
|
|
" Second() { }\n"
|
|
|
|
" static int method1(int param) {\n"
|
|
|
|
" UnhandledExceptions.invoke();\n"
|
|
|
|
" return 2;\n"
|
|
|
|
" }\n"
|
|
|
|
" static int method2() {\n"
|
|
|
|
" throw new Second();\n"
|
|
|
|
" }\n"
|
|
|
|
" static int method3(int param) {\n"
|
|
|
|
" try {\n"
|
|
|
|
" UnhandledExceptions.invoke2();\n"
|
2012-08-30 11:14:39 +00:00
|
|
|
" } on Second catch (e) {\n"
|
2011-10-05 05:20:07 +00:00
|
|
|
" return 3;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 2;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n"
|
2012-04-19 21:04:15 +00:00
|
|
|
"testMain() {\n"
|
|
|
|
" UnhandledExceptions.equals(2, Second.method1(1));\n"
|
|
|
|
" UnhandledExceptions.equals(3, Second.method3(1));\n"
|
2011-10-05 05:20:07 +00:00
|
|
|
"}";
|
2020-06-02 21:29:03 +00:00
|
|
|
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, native_lookup);
|
2012-10-31 17:56:46 +00:00
|
|
|
EXPECT_VALID(Dart_Invoke(lib, NewString("testMain"), 0, NULL));
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dart
|