Skip to content

Commit

Permalink
library/system: Move get_working_directory() here, and rename `uuid…
Browse files Browse the repository at this point in the history
…()` to `generate_uuid()`
  • Loading branch information
lhmouse committed Dec 4, 2023
1 parent f3ef413 commit bfa8f8a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
28 changes: 0 additions & 28 deletions asteria/library/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,6 @@ do_write_loop(int fd, const void* data, size_t size, const V_string& path)

} // namespace

V_string
std_filesystem_get_working_directory()
{
// Pass a null pointer to request dynamic allocation.
// Note this behavior is an extension that exists almost everywhere.
unique_ptr<char, void (void*)> cwd(::free);
cwd.reset(::getcwd(nullptr, 0));
if(!cwd)
ASTERIA_THROW((
"Could not get current working directory",
"[`getcwd()` failed: ${errno:full}]"));

V_string str(cwd.get());
return str;
}

V_string
std_filesystem_get_real_path(V_string path)
{
Expand Down Expand Up @@ -673,18 +657,6 @@ std_filesystem_remove_file(V_string path)
void
create_bindings_filesystem(V_object& result, API_Version /*version*/)
{
result.insert_or_assign(sref("get_working_directory"),
ASTERIA_BINDING(
"std.filesystem.get_working_directory", "",
Argument_Reader&& reader)
{
reader.start_overload();
if(reader.end_overload())
return (Value) std_filesystem_get_working_directory();

reader.throw_no_matching_function_call();
});

result.insert_or_assign(sref("get_real_path"),
ASTERIA_BINDING(
"std.filesystem.get_real_path", "path",
Expand Down
4 changes: 0 additions & 4 deletions asteria/library/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include "../fwd.hpp"
namespace asteria {

// `std.filesystem.get_working_directory`
V_string
std_filesystem_get_working_directory();

// `std.filesystem.get_real_path`
V_string
std_filesystem_get_real_path(V_string path);
Expand Down
36 changes: 32 additions & 4 deletions asteria/library/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ do_conf_parse_value_nonrecursive(Token_Stream& tstrm)

} // namespace

V_string
std_system_get_working_directory()
{
// Pass a null pointer to request dynamic allocation.
// Note this behavior is an extension that exists almost everywhere.
unique_ptr<char, void (void*)> cwd(::free);
cwd.reset(::getcwd(nullptr, 0));
if(!cwd)
ASTERIA_THROW((
"Could not get current working directory",
"[`getcwd()` failed: ${errno:full}]"));

V_string str(cwd.get());
return str;
}

optV_string
std_system_get_environment_variable(V_string name)
{
Expand Down Expand Up @@ -287,7 +303,7 @@ std_system_get_properties()
}

V_string
std_system_uuid(Global_Context& global)
std_system_generate_uuid(Global_Context& global)
{
// Canonical form: `xxxxxxxx-xxxx-Myyy-Nzzz-wwwwwwwwwwww`
// * x: number of 1/30518 seconds since UNIX Epoch
Expand Down Expand Up @@ -531,6 +547,18 @@ std_system_load_conf(V_string path)
void
create_bindings_system(V_object& result, API_Version /*version*/)
{
result.insert_or_assign(sref("get_working_directory"),
ASTERIA_BINDING(
"std.system.get_working_directory", "",
Argument_Reader&& reader)
{
reader.start_overload();
if(reader.end_overload())
return (Value) std_system_get_working_directory();

reader.throw_no_matching_function_call();
});

result.insert_or_assign(sref("get_environment_variable"),
ASTERIA_BINDING(
"std.system.get_environment_variable", "name",
Expand Down Expand Up @@ -570,14 +598,14 @@ create_bindings_system(V_object& result, API_Version /*version*/)
reader.throw_no_matching_function_call();
});

result.insert_or_assign(sref("uuid"),
result.insert_or_assign(sref("generate_uuid"),
ASTERIA_BINDING(
"std.system.uuid", "",
"std.system.generate_uuid", "",
Global_Context& global, Argument_Reader&& reader)
{
reader.start_overload();
if(reader.end_overload())
return (Value) std_system_uuid(global);
return (Value) std_system_generate_uuid(global);

reader.throw_no_matching_function_call();
});
Expand Down
8 changes: 6 additions & 2 deletions asteria/library/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "../fwd.hpp"
namespace asteria {

// `std.system.get_working_directory`
V_string
std_system_get_working_directory();

// `std.system.get_environment_variable`
optV_string
std_system_get_environment_variable(V_string name);
Expand All @@ -19,9 +23,9 @@ std_system_get_environment_variables();
V_object
std_system_get_properties();

// `std.system.uuid`
// `std.system.generate_uuid`
V_string
std_system_uuid(Global_Context& global);
std_system_generate_uuid(Global_Context& global);

// `std.system.get_pid()`
V_integer
Expand Down
14 changes: 7 additions & 7 deletions doc/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ variable. Individual components are categorized into sub-objects.

## `std.system`

### `std.system.get_working_directory()`

* Gets the absolute path of the current working directory.

* Returns the path as a string.

### `std.system.get_environment_variable(name)`

* Retrieves an environment variable with `name`.
Expand All @@ -84,7 +90,7 @@ variable. Individual components are categorized into sub-objects.
* `arch` string: name of the CPU architecture
* `nprocs` integer: number of active CPU cores

### `std.system.uuid()`
### `std.system.generate_uuid()`

* Generates a UUID of the form `xxxxxxxx-xxxx-Myyy-Nzzz-wwwwwwwwwwww`, where

Expand Down Expand Up @@ -1636,12 +1642,6 @@ variable. Individual components are categorized into sub-objects.

## `std.filesystem`

### `std.filesystem.get_working_directory()`

* Gets the absolute path of the current working directory.

* Returns the path as a string.

### `std.filesystem.get_real_path(path)`

* Resolves `path` to an absolute one. The result is a canonical path that
Expand Down

0 comments on commit bfa8f8a

Please sign in to comment.