Flutter Engine
The Flutter Engine
native_type_vm_test.cc
Go to the documentation of this file.
1// Copyright (c) 2020, 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
6
8#include "vm/unit_test.h"
9
10namespace dart {
11namespace compiler {
12namespace ffi {
13
14ISOLATE_UNIT_TEST_CASE(Ffi_NativeType_Primitive_FromAbstractType) {
15 Zone* Z = thread->zone();
16
17 const auto& ffi_library = Library::Handle(Library::FfiLibrary());
18 const auto& int8_class = Class::Handle(GetClass(ffi_library, "Int8"));
19 const auto& int8_type = Type::Handle(int8_class.DeclarationType());
20 const char* error = nullptr;
21 const auto& native_type = *NativeType::FromAbstractType(Z, int8_type, &error);
22 EXPECT_NULLPTR(error);
23
24 EXPECT_EQ(1, native_type.SizeInBytes());
25 EXPECT_STREQ("int8", native_type.ToCString());
26 EXPECT(native_type.IsInt());
27 EXPECT(native_type.IsPrimitive());
28}
29
30// In all calling conventions `bool` behaves as `uint8_t` when it comes to:
31// - size
32// - alignment in structs
33// - alignment on stack
34// - upper bytes in cpu registers.
35ISOLATE_UNIT_TEST_CASE(Ffi_NativeType_Bool_FromAbstractType) {
36 Zone* Z = thread->zone();
37
38 const auto& ffi_library = Library::Handle(Library::FfiLibrary());
39 const auto& bool_class = Class::Handle(GetClass(ffi_library, "Bool"));
40 const auto& bool_type = Type::Handle(bool_class.DeclarationType());
41 const char* error = nullptr;
42 const auto& bool_native_type =
44 EXPECT_NULLPTR(error);
45
46 const auto& uint8_native_type = *new (Z) NativePrimitiveType(kUint8);
47
48 EXPECT(bool_native_type.Equals(uint8_native_type));
49 EXPECT_EQ(1, bool_native_type.SizeInBytes());
50 EXPECT_STREQ("uint8", bool_native_type.ToCString());
51 EXPECT(bool_native_type.IsInt());
52 EXPECT(bool_native_type.IsPrimitive());
53}
54
55// Test that we construct `NativeType` correctly from `Type`.
56ISOLATE_UNIT_TEST_CASE(Ffi_NativeType_Struct_FromAbstractType) {
57 Zone* Z = thread->zone();
58
59 const char* kScript =
60 R"(
61 import 'dart:ffi';
62
63 final class MyStruct extends Struct {
64 @Int8()
65 external int a0;
66
67 external Pointer<Int8> a1;
68 }
69 )";
70
71 const auto& root_library = Library::Handle(LoadTestScript(kScript));
72 const auto& struct_class = Class::Handle(GetClass(root_library, "MyStruct"));
73 const auto& struct_type = Type::Handle(struct_class.DeclarationType());
74
75 const char* error = nullptr;
76 const auto& native_type =
78 EXPECT_NULLPTR(error);
79
80 EXPECT_EQ(2, native_type.members().length());
81
82 const auto& int8_type = *new (Z) NativePrimitiveType(kInt8);
83 EXPECT(int8_type.Equals(*native_type.members()[0]));
84
86 native_type.members()[1]->SizeInBytes());
87}
88
89} // namespace ffi
90} // namespace compiler
91} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
#define Z
static LibraryPtr FfiLibrary()
Definition: object.cc:14799
static Object & Handle()
Definition: object.h:407
const NativeCompoundType & AsCompound() const
Definition: native_type.cc:56
static const NativeType * FromAbstractType(Zone *zone, const AbstractType &type, const char **error)
Definition: native_type.cc:549
const uint8_t uint32_t uint32_t GError ** error
ISOLATE_UNIT_TEST_CASE(Ffi_NativeType_Primitive_FromAbstractType)
static constexpr intptr_t kWordSize
Definition: runtime_api.h:274
Definition: dart_vm.cc:33
LibraryPtr LoadTestScript(const char *script, Dart_NativeEntryResolver resolver, const char *lib_uri)
ClassPtr GetClass(const Library &lib, const char *name)