Chromium Code Reviews| 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); | |
|
siva
2017/08/29 16:47:39
ASSERT(namespc != NULL);
or
if (namespc != NULL)
zra
2017/08/29 20:22:51
Done.
| |
| 23 namespc->Release(); | |
| 24 } | |
| 25 | |
| 26 static void AssertIsNamespace(Dart_Handle namespc_obj) { | |
|
siva
2017/08/29 16:47:39
Why not change this into
#if defined(DEBUG)
static
zra
2017/08/29 20:22:51
Done.
| |
| 27 #if defined(DEBUG) | |
| 28 Dart_Handle namespc_type = | |
| 29 DartUtils::GetDartType("dart:io", "_NamespaceImpl"); | |
| 30 ASSERT(!Dart_IsError(namespc_type)); | |
| 31 bool isinstance = false; | |
| 32 Dart_Handle result = | |
| 33 Dart_ObjectIsType(namespc_obj, namespc_type, &isinstance); | |
| 34 ASSERT(!Dart_IsError(result)); | |
| 35 ASSERT(isinstance); | |
| 36 #endif | |
| 37 } | |
| 38 | |
| 39 void FUNCTION_NAME(Namespace_Create)(Dart_NativeArguments args) { | |
| 40 Dart_Handle namespc_obj = Dart_GetNativeArgument(args, 0); | |
| 41 if (Dart_IsError(namespc_obj)) { | |
| 42 Dart_PropagateError(namespc_obj); | |
| 43 } | |
| 44 AssertIsNamespace(namespc_obj); | |
| 45 | |
| 46 // Allocate a native wrapper for the platform namespc bits. | |
| 47 Namespace* namespc = NULL; | |
| 48 Dart_Handle result; | |
| 49 Dart_Handle native_namespc = Dart_GetNativeArgument(args, 1); | |
| 50 if (Dart_IsInteger(native_namespc)) { | |
| 51 int64_t namespc_val; | |
| 52 result = Dart_IntegerToInt64(native_namespc, &namespc_val); | |
| 53 if (Dart_IsError(result)) { | |
| 54 Dart_PropagateError(result); | |
| 55 } | |
| 56 namespc = Namespace::Create(namespc_val); | |
| 57 } else if (Dart_IsString(native_namespc)) { | |
| 58 const char* namespc_path; | |
| 59 result = Dart_StringToCString(native_namespc, &namespc_path); | |
| 60 if (Dart_IsError(result)) { | |
| 61 Dart_PropagateError(result); | |
| 62 } | |
| 63 namespc = Namespace::Create(namespc_path); | |
| 64 } else { | |
| 65 // Propagate a type error. | |
| 66 Dart_ThrowException( | |
| 67 DartUtils::NewDartArgumentError("Argument must be an int or a String")); | |
| 68 } | |
| 69 | |
| 70 // We were unable to create a native Namespace wrapper object due to some | |
| 71 // OS-level error. | |
| 72 if (namespc == NULL) { | |
| 73 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 74 } | |
| 75 | |
| 76 // Set the Dart objects native field to the native wrapper. | |
| 77 result = Dart_SetNativeInstanceField(namespc_obj, kNamespaceNativeFieldIndex, | |
| 78 reinterpret_cast<intptr_t>(namespc)); | |
| 79 if (Dart_IsError(result)) { | |
| 80 namespc->Release(); | |
| 81 Dart_PropagateError(result); | |
| 82 } | |
| 83 | |
| 84 // Set up a finalizer for the Dart object so that we can do any necessary | |
| 85 // platform-specific cleanup for the namespc. | |
| 86 Dart_NewWeakPersistentHandle(namespc_obj, reinterpret_cast<void*>(namespc), | |
| 87 sizeof(*namespc), ReleaseNamespace); | |
| 88 Dart_SetReturnValue(args, namespc_obj); | |
| 89 } | |
| 90 | |
| 91 void FUNCTION_NAME(Namespace_GetDefault)(Dart_NativeArguments args) { | |
| 92 Dart_SetIntegerReturnValue(args, Namespace::Default()); | |
| 93 } | |
| 94 | |
| 95 void FUNCTION_NAME(Namespace_GetPointer)(Dart_NativeArguments args) { | |
| 96 Namespace* namespc = Namespace::GetNamespace(args, 0); | |
| 97 ASSERT(namespc != NULL); | |
| 98 namespc->Retain(); | |
| 99 const intptr_t namespc_pointer = reinterpret_cast<intptr_t>(namespc); | |
| 100 Dart_SetIntegerReturnValue(args, namespc_pointer); | |
|
siva
2017/08/29 16:47:39
Why not just
Dart_SetIntegerReturnValue(args, rein
zra
2017/08/29 20:22:51
Done.
| |
| 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 AssertIsNamespace(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 |