Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <atomic>
3#include <chrono>
4#include <thread>
5#if defined(_MSC_VER)
6#include <vcruntime_startup.h>
7#endif
8
9#include "core/system.h"
10
11#if defined(_WIN32)
12#include <windows.h>
13#else
14#include <unistd.h>
15#endif
16
17namespace Phasor
18{
19
41
42Value StdLib::sys_time(const std::vector<Value> &args, VM *vm)
43{
44 checkArgCount(args, 0, "time");
45 auto now = std::chrono::steady_clock::now();
46 auto duration = now.time_since_epoch();
47 double millis = std::chrono::duration<double, std::milli>(duration).count();
48 return millis;
49}
50
51Value StdLib::sys_time_formatted(const std::vector<Value> &args, VM *vm)
52{
53 checkArgCount(args, 1, "timef");
54 std::string format = args[0].asString();
55
56 auto now = std::chrono::system_clock::now();
57 std::time_t t = std::chrono::system_clock::to_time_t(now);
58
59 std::tm tm{};
60#if defined(_WIN32)
61 localtime_s(&tm, &t);
62#else
63 localtime_r(&t, &tm);
64#endif
65
66 char buffer[256];
67 if (std::strftime(buffer, sizeof(buffer), format.c_str(), &tm) == 0)
68 {
69 return Value(" ");
70 }
71
72 return std::string(buffer);
73}
74
75Value StdLib::sys_sleep(const std::vector<Value> &args, VM *vm)
76{
77 checkArgCount(args, 1, "sleep");
78 int64_t ms = args[0].asInt();
79 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
80 return Value(" ");
81}
82
83#ifndef SANDBOXED
84Value StdLib::sys_os(const std::vector<Value> &args, VM *vm)
85{
86 checkArgCount(args, 0, "sys_os");
87#if defined(_WIN32)
88 return Value("win32");
89#elif defined(__linux__)
90 return Value("Linux");
91#elif defined(__APPLE__)
92 return Value("Darwin");
93#elif defined(__FreeBSD__)
94 return Value("FreeBSD");
95#elif defined(__unix__)
96 return Value("UNIX");
97#else
98 return Value("Unknown");
99#endif
100 return false;
101}
102
103Value StdLib::sys_env(const std::vector<Value> &args, VM *vm)
104{
105 checkArgCount(args, 1, "sys_env");
106 std::string key = args[0].asString();
107 std::string value;
108 dupenv(value, key.c_str(), envp);
109 return value;
110}
111
112Value StdLib::sys_argv(const std::vector<Value> &args, VM *vm)
113{
114 if (args.size() == 0)
115 {
116 auto l_argv = std::make_shared<Value::StructInstance>();
117
118 for (size_t i = 0; i < static_cast<size_t>(argc); i++)
119 {
120 l_argv->fields["arg" + std::to_string(i)] = Value(argv[i]);
121 }
122
123 return Value(l_argv);
124 }
125
126 checkArgCount(args, 1, "sys_argv");
127 int64_t index = args[0].asInt();
128 if (argv != nullptr)
129 return argv[index];
130 else
131 return Value();
132}
133
134Value StdLib::sys_argc(const std::vector<Value> &args, VM *vm)
135{
136 checkArgCount(args, 0, "sys_argc");
137 return argc;
138}
139
140Value StdLib::system_get_free_memory(const std::vector<Value> &args, VM *vm)
141{
142 checkArgCount(args, 0, "sys_get_memory");
143 return static_cast<int64_t>(PHASORstd_sys_getAvailableMemory());
144}
145
146Value StdLib::sys_wait_for_input(const std::vector<Value> &args, VM *vm)
147{
148 checkArgCount(args, 0, "wait_for_input");
149 io_gets({}, vm);
150 return Value("");
151}
152
153Value StdLib::sys_shell(const std::vector<Value> &args, VM *vm)
154{
155 checkArgCount(args, 1, "sys_shell");
156 return vm->regRun(OpCode::SYSTEM_R, args[0]);
157}
158
159Value StdLib::sys_fork(const std::vector<Value> &args, VM *vm)
160{
161 checkArgCount(args, 1, "sys_fork", true);
162 const char *executable = args[0].c_str();
163 int argc = (int)args.size() - 1;
164 std::vector<char *> v_argv(argc);
165 for (int i = 0; i < argc; ++i)
166 {
167 v_argv[i] = const_cast<char *>(args[i + 1].c_str());
168 }
169 return PHASORstd_sys_run(executable, argc, v_argv.data());
170}
171
172Value StdLib::sys_fork_detached(const std::vector<Value> &args, VM *vm)
173{
174 checkArgCount(args, 1, "sys_fork_detached", true);
175 const char *executable = args[0].c_str();
176 int argc = (int)args.size() - 1;
177 std::vector<char *> v_argv(argc);
178 for (int i = 0; i < argc; ++i)
179 {
180 v_argv[i] = const_cast<char *>(args[i + 1].c_str());
181 }
182 return PHASORstd_sys_run_detached(executable, argc, v_argv.data());
183}
184
185Value StdLib::sys_crash(const std::vector<Value> &args, VM *vm)
186{
187 checkArgCount(args, 1, "error", true);
188 vm->reset();
189 vm->setStatus(-1);
190 throw std::runtime_error(args[0].asString());
191}
192
193Value StdLib::sys_reset(const std::vector<Value> &args, VM *vm)
194{
195 checkArgCount(args, 0, "reset");
196 vm->reset();
197 return Value();
198}
199
200Value StdLib::sys_pid(const std::vector<Value> &args, VM *vm)
201{
202 checkArgCount(args, 0, "sys_pid");
203#if defined(_WIN32)
204 return static_cast<int64_t>(GetCurrentProcessId());
205#else
206 return static_cast<int64_t>(getpid());
207#endif
208}
209
210#endif
211
212Value StdLib::sys_shutdown(const std::vector<Value> &args, VM *vm)
213{
214 checkArgCount(args, 1, "shutdown");
215 int ret = static_cast<int>(args[0].asInt());
216 vm->setStatus(ret);
217 throw VM::Halt();
218}
219
220} // namespace Phasor
static Value sys_env(const std::vector< Value > &args, VM *vm)
Get the current environment variables.
Definition system.cpp:103
static Value sys_fork_detached(const std::vector< Value > &args, VM *vm)
Run a native program detached.
Definition system.cpp:172
static Value sys_reset(const std::vector< Value > &args, VM *vm)
Reset the VM.
Definition system.cpp:193
static char ** argv
Command line arguments.
Definition StdLib.hpp:40
static Value sys_time(const std::vector< Value > &args, VM *vm)
Current time.
Definition system.cpp:42
static Value sys_argc(const std::vector< Value > &args, VM *vm)
Get the current number of command line arguments.
Definition system.cpp:134
static Value sys_crash(const std::vector< Value > &args, VM *vm)
Crash the VM / Program.
Definition system.cpp:185
static Value system_get_free_memory(const std::vector< Value > &args, VM *vm)
Get current free memory.
Definition system.cpp:140
static Value sys_os(const std::vector< Value > &args, VM *vm)
Get the current OS.
Definition system.cpp:84
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static Value sys_argv(const std::vector< Value > &args, VM *vm)
Get the current command line arguments.
Definition system.cpp:112
static int argc
Number of command line arguments.
Definition StdLib.hpp:41
static Value sys_wait_for_input(const std::vector< Value > &args, VM *vm)
Wait for input.
Definition system.cpp:146
static void registerSysFunctions(VM *vm)
Definition system.cpp:20
static Value sys_shutdown(const std::vector< Value > &args, VM *vm)
Shutdown the VM.
Definition system.cpp:212
static Value sys_shell(const std::vector< Value > &args, VM *vm)
Run a shell command.
Definition system.cpp:153
static Value sys_pid(const std::vector< Value > &args, VM *vm)
Get the current process ID.
Definition system.cpp:200
static Value sys_sleep(const std::vector< Value > &args, VM *vm)
Sleep for a specified amount of time.
Definition system.cpp:75
static char ** envp
Environment variables.
Definition StdLib.hpp:42
static Value sys_time_formatted(const std::vector< Value > &args, VM *vm)
Current time formatted.
Definition system.cpp:51
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
Definition io.cpp:121
static int dupenv(std::string &out, const char *name, char *const argp[])
Definition StdLib.cpp:13
static Value sys_fork(const std::vector< Value > &args, VM *vm)
Run a native program.
Definition system.cpp:159
Throws when the HALT opcode is reached.
Definition VM.hpp:75
Virtual Machine.
Definition VM.hpp:30
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
void setStatus(int newStatus)
Set VM exit code.
Definition VM.hpp:218
Value regRun(OpCode opcode, Args &&...args)
Run an opcode with arguments pre-loaded into registers.
Definition VM.hpp:227
void reset(const bool &resetStack=true, const bool &resetFunctions=true, const bool &resetVariables=true)
Reset the virtual machine.
Definition Utility.cpp:88
A value in the Phasor VM.
Definition Value.hpp:67
The Phasor Programming Language and Runtime.
Definition AST.hpp:11
@ SYSTEM_R
Run an operating system shell command: system(R[rA]).
Definition ISA.hpp:157
int PHASORstd_sys_run_detached(const char *name, int argc, char **argv)
Definition system.c:90
size_t PHASORstd_sys_getAvailableMemory()
Definition system.c:3
int PHASORstd_sys_run(const char *name, int argc, char **argv)
Definition system.c:56