OLD | NEW |
(Empty) | |
| 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 |
| 14 namespace dart { |
| 15 namespace bin { |
| 16 |
| 17 static const int kNamespaceNativeFieldIndex = 0; |
| 18 |
| 19 static void ReleaseNamespace(void* isolate_callback_data, |
| 20 Dart_WeakPersistentHandle handle, |
| 21 void* peer) { |
| 22 Namespace* namespc = reinterpret_cast<Namespace*>(peer); |
| 23 ASSERT(namespc != NULL); |
| 24 namespc->Release(); |
| 25 } |
| 26 |
| 27 #if defined(DEBUG) |
| 28 static bool IsNamespace(Dart_Handle namespc_obj) { |
| 29 Dart_Handle namespc_type = |
| 30 DartUtils::GetDartType("dart:io", "_NamespaceImpl"); |
| 31 ASSERT(!Dart_IsError(namespc_type)); |
| 32 bool isinstance = false; |
| 33 Dart_Handle result = |
| 34 Dart_ObjectIsType(namespc_obj, namespc_type, &isinstance); |
| 35 ASSERT(!Dart_IsError(result)); |
| 36 return isinstance; |
| 37 } |
| 38 #endif |
| 39 |
| 40 void FUNCTION_NAME(Namespace_Create)(Dart_NativeArguments args) { |
| 41 Dart_Handle namespc_obj = Dart_GetNativeArgument(args, 0); |
| 42 if (Dart_IsError(namespc_obj)) { |
| 43 Dart_PropagateError(namespc_obj); |
| 44 } |
| 45 DEBUG_ASSERT(IsNamespace(namespc_obj)); |
| 46 |
| 47 // Allocate a native wrapper for the platform namespc bits. |
| 48 Namespace* namespc = NULL; |
| 49 Dart_Handle result; |
| 50 Dart_Handle native_namespc = Dart_GetNativeArgument(args, 1); |
| 51 if (Dart_IsInteger(native_namespc)) { |
| 52 int64_t namespc_val; |
| 53 result = Dart_IntegerToInt64(native_namespc, &namespc_val); |
| 54 if (Dart_IsError(result)) { |
| 55 Dart_PropagateError(result); |
| 56 } |
| 57 namespc = Namespace::Create(namespc_val); |
| 58 } else if (Dart_IsString(native_namespc)) { |
| 59 const char* namespc_path; |
| 60 result = Dart_StringToCString(native_namespc, &namespc_path); |
| 61 if (Dart_IsError(result)) { |
| 62 Dart_PropagateError(result); |
| 63 } |
| 64 namespc = Namespace::Create(namespc_path); |
| 65 } else { |
| 66 // Propagate a type error. |
| 67 Dart_ThrowException( |
| 68 DartUtils::NewDartArgumentError("Argument must be an int or a String")); |
| 69 } |
| 70 |
| 71 // We were unable to create a native Namespace wrapper object due to some |
| 72 // OS-level error. |
| 73 if (namespc == NULL) { |
| 74 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 75 } |
| 76 |
| 77 // Set the Dart objects native field to the native wrapper. |
| 78 result = Dart_SetNativeInstanceField(namespc_obj, kNamespaceNativeFieldIndex, |
| 79 reinterpret_cast<intptr_t>(namespc)); |
| 80 if (Dart_IsError(result)) { |
| 81 namespc->Release(); |
| 82 Dart_PropagateError(result); |
| 83 } |
| 84 |
| 85 // Set up a finalizer for the Dart object so that we can do any necessary |
| 86 // platform-specific cleanup for the namespc. |
| 87 Dart_NewWeakPersistentHandle(namespc_obj, reinterpret_cast<void*>(namespc), |
| 88 sizeof(*namespc), ReleaseNamespace); |
| 89 Dart_SetReturnValue(args, namespc_obj); |
| 90 } |
| 91 |
| 92 void FUNCTION_NAME(Namespace_GetDefault)(Dart_NativeArguments args) { |
| 93 Dart_SetIntegerReturnValue(args, Namespace::Default()); |
| 94 } |
| 95 |
| 96 void FUNCTION_NAME(Namespace_GetPointer)(Dart_NativeArguments args) { |
| 97 Namespace* namespc = Namespace::GetNamespace(args, 0); |
| 98 ASSERT(namespc != NULL); |
| 99 namespc->Retain(); |
| 100 Dart_SetIntegerReturnValue(args, reinterpret_cast<intptr_t>(namespc)); |
| 101 } |
| 102 |
| 103 Namespace* Namespace::GetNamespace(Dart_NativeArguments args, intptr_t index) { |
| 104 Namespace* namespc; |
| 105 Dart_Handle status = |
| 106 Namespace::GetNativeNamespaceArgument(args, index, &namespc); |
| 107 if (Dart_IsError(status)) { |
| 108 Dart_PropagateError(status); |
| 109 } |
| 110 return namespc; |
| 111 } |
| 112 |
| 113 bool Namespace::IsDefault(Namespace* namespc) { |
| 114 return (namespc == NULL) || (namespc->namespc() == Namespace::Default()); |
| 115 } |
| 116 |
| 117 Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args, |
| 118 intptr_t index, |
| 119 Namespace** namespc) { |
| 120 Dart_Handle namespc_obj = Dart_GetNativeArgument(args, index); |
| 121 if (Dart_IsError(namespc_obj)) { |
| 122 Dart_PropagateError(namespc_obj); |
| 123 } |
| 124 DEBUG_ASSERT(IsNamespace(namespc_obj)); |
| 125 |
| 126 Dart_Handle result = |
| 127 Dart_GetNativeInstanceField(namespc_obj, kNamespaceNativeFieldIndex, |
| 128 reinterpret_cast<intptr_t*>(namespc)); |
| 129 if (Dart_IsError(result)) { |
| 130 return result; |
| 131 } |
| 132 return Dart_Null(); |
| 133 } |
| 134 |
| 135 } // namespace bin |
| 136 } // namespace dart |
| 137 |
| 138 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |