Flutter Engine
The Flutter Engine
namespace.cc
Go to the documentation of this file.
1// Copyright (c) 2017, 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#if !defined(DART_IO_DISABLED)
6
7#include "bin/namespace.h"
8
9#include "bin/builtin.h"
10#include "bin/dartutils.h"
11#include "include/dart_api.h"
12#include "platform/globals.h"
13
14namespace dart {
15namespace bin {
16
17static constexpr int kNamespaceNativeFieldIndex = 0;
18
19static void ReleaseNamespace(void* isolate_callback_data, void* peer) {
20 Namespace* namespc = reinterpret_cast<Namespace*>(peer);
21 ASSERT(namespc != nullptr);
22 namespc->Release();
23}
24
25#if defined(DEBUG)
26static bool IsNamespace(Dart_Handle namespc_obj) {
27 Dart_Handle namespc_type =
28 DartUtils::GetDartType("dart:io", "_NamespaceImpl");
29 ASSERT(!Dart_IsError(namespc_type));
30 bool isinstance = false;
32 Dart_ObjectIsType(namespc_obj, namespc_type, &isinstance);
34 return isinstance;
35}
36#endif
37
39 Dart_Handle namespc_obj = Dart_GetNativeArgument(args, 0);
40 if (Dart_IsError(namespc_obj)) {
41 Dart_PropagateError(namespc_obj);
42 }
43 DEBUG_ASSERT(IsNamespace(namespc_obj));
44
45 // Allocate a native wrapper for the platform namespc bits.
46 Namespace* namespc = nullptr;
48 Dart_Handle native_namespc = Dart_GetNativeArgument(args, 1);
49 if (Dart_IsInteger(native_namespc)) {
50 int64_t namespc_val;
51 result = Dart_IntegerToInt64(native_namespc, &namespc_val);
52 if (Dart_IsError(result)) {
54 }
55 namespc = Namespace::Create(namespc_val);
56 } else if (Dart_IsString(native_namespc)) {
57 const char* namespc_path;
58 result = Dart_StringToCString(native_namespc, &namespc_path);
59 if (Dart_IsError(result)) {
61 }
62 namespc = Namespace::Create(namespc_path);
63 } else {
64 // Propagate a type error.
66 DartUtils::NewDartArgumentError("Argument must be an int or a String"));
67 }
68
69 // We were unable to create a native Namespace wrapper object due to some
70 // OS-level error.
71 if (namespc == nullptr) {
73 }
74
75 // Set the Dart objects native field to the native wrapper.
77 reinterpret_cast<intptr_t>(namespc));
78 if (Dart_IsError(result)) {
79 namespc->Release();
81 }
82
83 // Set up a finalizer for the Dart object so that we can do any necessary
84 // platform-specific cleanup for the namespc.
85 Dart_NewFinalizableHandle(namespc_obj, reinterpret_cast<void*>(namespc),
86 sizeof(*namespc), ReleaseNamespace);
87 Dart_SetReturnValue(args, namespc_obj);
88}
89
92}
93
96 ASSERT(namespc != nullptr);
97 namespc->Retain();
98 Dart_SetIntegerReturnValue(args, reinterpret_cast<intptr_t>(namespc));
99}
100
103 Dart_Handle status =
104 Namespace::GetNativeNamespaceArgument(args, index, &namespc);
105 if (Dart_IsError(status)) {
106 Dart_PropagateError(status);
107 }
108 return namespc;
109}
110
112 return (namespc == nullptr) || (namespc->namespc() == nullptr);
113}
114
115Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args,
116 intptr_t index,
117 Namespace** namespc) {
118 Dart_Handle namespc_obj = Dart_GetNativeArgument(args, index);
119 if (Dart_IsError(namespc_obj)) {
120 return namespc_obj;
121 }
122 DEBUG_ASSERT(IsNamespace(namespc_obj));
123
126 reinterpret_cast<intptr_t*>(namespc));
127 if (Dart_IsError(result)) {
128 return result;
129 }
130 if (*namespc == nullptr) {
132 DartUtils::NewInternalError("No native peer"));
133 }
134 return Dart_Null();
135}
136
137} // namespace bin
138} // namespace dart
139
140#endif // !defined(DART_IO_DISABLED)
#define DEBUG_ASSERT(cond)
Definition: assert.h:321
#define FUNCTION_NAME(name)
Definition: builtin.h:19
static Dart_Handle NewDartOSError()
Definition: dartutils.cc:702
static Dart_Handle NewDartArgumentError(const char *message)
Definition: dartutils.cc:746
static Dart_Handle NewInternalError(const char *message)
Definition: dartutils.cc:781
static Dart_Handle GetDartType(const char *library_url, const char *class_name)
Definition: dartutils.cc:696
static bool IsDefault(Namespace *namespc)
Definition: namespace.cc:111
static Namespace * Create(intptr_t namespc)
static Namespace * GetNamespace(Dart_NativeArguments args, intptr_t index)
Definition: namespace.cc:101
NamespaceImpl * namespc() const
Definition: namespace.h:41
static intptr_t Default()
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
#define ASSERT(E)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
static void ReleaseNamespace(void *isolate_callback_data, void *peer)
Definition: namespace.cc:19
static constexpr int kNamespaceNativeFieldIndex
Definition: namespace.cc:17
void FUNCTION_NAME() Namespace_GetDefault(Dart_NativeArguments args)
Definition: namespace.cc:90
void FUNCTION_NAME() Namespace_Create(Dart_NativeArguments args)
Definition: namespace.cc:38
void FUNCTION_NAME() Namespace_GetPointer(Dart_NativeArguments args)
Definition: namespace.cc:94
Definition: dart_vm.cc:33
DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj, int index, intptr_t *value)
DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception)
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT Dart_FinalizableHandle Dart_NewFinalizableHandle(Dart_Handle object, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, Dart_Handle retval)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj, int index, intptr_t value)
DART_EXPORT void Dart_SetIntegerReturnValue(Dart_NativeArguments args, int64_t retval)
DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object, Dart_Handle type, bool *value)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
DART_EXPORT bool Dart_IsInteger(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_Null()
DART_EXPORT bool Dart_IsString(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, const char **cstr)
DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer, int64_t *value)