8#include <vcruntime_startup.h>
24#pragma warning(disable: 4996)
44 auto stub = [](
const std::vector<Value> &,
VM *) {
return Value(); };
49 if (!std::getenv(
"PHASOR_NO_ENV")) {
74 auto now = std::chrono::steady_clock::now();
75 auto duration = now.time_since_epoch();
76 double millis = std::chrono::duration<double, std::milli>(duration).count();
83 std::string format = args[0].asString();
85 auto now = std::chrono::system_clock::now();
86 std::time_t t = std::chrono::system_clock::to_time_t(now);
96 if (std::strftime(buffer,
sizeof(buffer), format.c_str(), &tm) == 0)
101 return std::string(buffer);
107 int64_t ms = args[0].asInt();
108 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
115 std::string key = args[0].asString();
126 int64_t index = args[0].asInt();
128 if (
argc > index && index >= 0)
131 throw std::runtime_error(
"sys_argv: Index out of bounds: " + std::to_string(index));
139 return static_cast<int64_t
>(
argc);
145 int ret =
static_cast<int>(args[0].asInt());
157#elif defined(__linux__)
159#elif defined(__APPLE__)
161#elif defined(__FreeBSD__)
163#elif defined(__unix__)
192 const char *executable = args[0].c_str();
193 int argc = (int)args.size() - 1;
194 std::vector<char *> v_argv(
argc);
195 for (
int i = 0; i <
argc; ++i)
197 v_argv[i] =
const_cast<char *
>(args[i + 1].c_str());
205 const char *executable = args[0].c_str();
206 int argc = (int)args.size() - 1;
207 std::vector<char *> v_argv(
argc);
208 for (
int i = 0; i <
argc; ++i)
210 v_argv[i] =
const_cast<char *
>(args[i + 1].c_str());
220 throw std::runtime_error(args[0].asString());
234 return static_cast<int64_t
>(GetCurrentProcessId());
236 return static_cast<int64_t
>(getpid());
244 return _isatty(_fileno(stdin));
246 return isatty(fileno(stdin));
static Value sys_env(const std::vector< Value > &args, VM *vm)
Get the current environment variables.
static Value sys_reset(const std::vector< Value > &args, VM *vm)
Reset the VM.
static char ** argv
Command line arguments.
static Value sys_crash(const std::vector< Value > &args, VM *vm)
Crash the VM / Program.
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static int64_t sys_pid(const std::vector< Value > &args, VM *vm)
Get the current process ID.
static Value sys_argv(const std::vector< Value > &args, VM *vm)
Get the current command line arguments.
static int argc
Number of command line arguments.
static int64_t sys_argc(const std::vector< Value > &args, VM *vm)
Get the current number of command line arguments.
static Value sys_wait_for_input(const std::vector< Value > &args, VM *vm)
Wait for input.
static void registerSysFunctions(VM *vm)
static Value sys_shutdown(const std::vector< Value > &args, VM *vm)
Shutdown the VM.
static int64_t sys_get_free_memory(const std::vector< Value > &args, VM *vm)
Get current free memory.
static Value sys_shell(const std::vector< Value > &args, VM *vm)
Run a shell command.
static Value sys_sleep(const std::vector< Value > &args, VM *vm)
Sleep for a specified amount of time.
static dupenv_ret dupenv(std::string &out, const char *name)
static int64_t sys_fork(const std::vector< Value > &args, VM *vm)
Run a native program.
static std::string sys_os(const std::vector< Value > &args, VM *vm)
Get the current OS.
static Value sys_isatty(const std::vector< Value > &args, VM *vm)
Check if the current output is a terminal.
static int64_t sys_fork_detached(const std::vector< Value > &args, VM *vm)
Run a native program detached.
static Value sys_time_formatted(const std::vector< Value > &args, VM *vm)
Current time formatted.
static double sys_time(const std::vector< Value > &args, VM *vm)
Current time.
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
Throws when the HALT opcode is reached.
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
void setStatus(int newStatus)
Set VM exit code.
Value regRun(OpCode opcode, Args &&...args)
Run an opcode with arguments pre-loaded into registers.
void reset(const bool &resetStack=true, const bool &resetFunctions=true, const bool &resetVariables=true)
Reset the virtual machine.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.
@ SYSTEM_R
Run an operating system shell command: system(R[rA]).
size_t PHASORstd_sys_getAvailableMemory(void)
int PHASORstd_sys_run_detached(const char *name, int argc, char **argv)
int PHASORstd_sys_run(const char *name, int argc, char **argv)
bool prompt_consent(const char(&subsystem)[N1], EConsentVolition volition, const char(&verb)[N2], const char(&noun)[N3], bool default_val=false)
Prompt the user for consent.