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#ifndef RUNTIME_BIN_NAMESPACE_H_
6#define RUNTIME_BIN_NAMESPACE_H_
7
8#include "bin/builtin.h"
9#include "bin/dartutils.h"
10#include "bin/reference_counting.h"
11#include "platform/syslog.h"
12
13namespace dart {
14namespace bin {
15
16class NamespaceImpl;
17
18class Namespace : public ReferenceCounted<Namespace> {
19 public:
20 // Assumes namespc is a value that can be directly used as namespc_.
21 static Namespace* Create(intptr_t namespc);
22
23 // Uses path to compute a value that can be used as namespc_.
24 static Namespace* Create(const char* path);
25
26 // Gives a safe default value for namespc_ for the standalone Dart VM.
27 static intptr_t Default();
28
29 // Tells whether the given namespace is the default namespace.
30 static bool IsDefault(Namespace* namespc);
31
32 // Returns the native namespace wrapper if the argument at the supplied index
33 // is a _NamespaceImpl object. If it is not, calls Dart_PropagateError().
34 static Namespace* GetNamespace(Dart_NativeArguments args, intptr_t index);
35
36 // Get and set the current working directory through the namespace if there
37 // is one.
38 static const char* GetCurrent(Namespace* namespc);
39 static bool SetCurrent(Namespace* namespc, const char* path);
40
41 NamespaceImpl* namespc() const { return namespc_; }
42
43 private:
44 // When namespc_ has this value, it indicates that there is currently
45 // no namespace for resolving absolute paths.
46 static const intptr_t kNone = 0;
47
48 explicit Namespace(NamespaceImpl* namespc)
49 : ReferenceCounted(), namespc_(namespc) {}
50
51 ~Namespace();
52
53 // When the native argument at |index| is a _NamespaceImpl object,
54 // write the valueof its native field into |namespc|.
55 static Dart_Handle GetNativeNamespaceArgument(Dart_NativeArguments args,
56 intptr_t index,
57 Namespace** namespc);
58
59 // Given a namespace and a path, computes the information needed to access the
60 // path relative to the namespace. This can include massaging the path and
61 // returning a platform specific value in dirfd that together are used to
62 // access the path.
63 static void ResolvePath(Namespace* namespc,
64 const char* path,
65 intptr_t* dirfd,
66 const char** resolved_path);
67
68 NamespaceImpl* namespc_;
69 // TODO(zra): When Isolate-specific cwds are added, we'll need some more
70 // fields here to track them.
71
72 friend class NamespaceScope;
73 friend class ReferenceCounted<Namespace>;
74 DISALLOW_COPY_AND_ASSIGN(Namespace);
75};
76
77class NamespaceScope {
78 public:
79 NamespaceScope(Namespace* namespc, const char* path);
80 ~NamespaceScope();
81
82 intptr_t fd() const { return fd_; }
83 const char* path() const { return path_; }
84
85 private:
86 intptr_t fd_;
87 const char* path_;
88
89 DISALLOW_ALLOCATION();
90 DISALLOW_COPY_AND_ASSIGN(NamespaceScope);
91};
92
93} // namespace bin
94} // namespace dart
95
96#endif // RUNTIME_BIN_NAMESPACE_H_
97