8#include <vcruntime_startup.h>
31#pragma warning(disable: 4996)
50 auto stub = [](
const std::vector<Value> &,
VM *) ->
Value {
return Value(); };
55 if (!std::getenv(
"PHASOR_NO_ENV")) {
93 auto now = std::chrono::steady_clock::now();
94 auto duration = now.time_since_epoch();
95 f64 millis = std::chrono::duration<f64, std::milli>(duration).count();
104 auto now = std::chrono::system_clock::now();
105 std::time_t t = std::chrono::system_clock::to_time_t(now);
109 localtime_s(&tm, &t);
111 localtime_r(&t, &tm);
115 if (std::strftime(buffer,
sizeof(buffer), format.c_str(), &tm) == 0)
126 i64 ms = args[0].asInt();
127 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
151 i64 index = args[0].asInt();
152 if (index < 0 || index >=
argc)
return Value();
159 std::vector<Value> arguments;
160 for (
int i = 0; i <
argc; ++i)
170 int ret =
static_cast<int>(args[0].asInt());
182#elif defined(__linux__)
184#elif defined(__APPLE__)
186#elif defined(__FreeBSD__)
188#elif defined(__unix__)
217 const char *executable = args[0].c_str();
218 int argc = (int)args.size() - 1;
219 std::vector<char *> v_argv(
argc);
220 for (
int i = 0; i <
argc; ++i)
222 v_argv[i] =
const_cast<char *
>(args[i + 1].c_str());
230 const char *executable = args[0].c_str();
231 int argc = (int)args.size() - 1;
232 std::vector<char *> v_argv(
argc);
233 for (
int i = 0; i <
argc; ++i)
235 v_argv[i] =
const_cast<char *
>(args[i + 1].c_str());
245 throw std::runtime_error(args[0].asString());
259 return static_cast<i64>(GetCurrentProcessId());
261 return static_cast<i64>(getpid());
269 return _isatty(_fileno(stdin));
271 return isatty(fileno(stdin));
const char * c_str() const noexcept
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 PhsString sys_os(const std::vector< Value > &args, VM *vm)
Get the current OS.
static i64 sys_pid(const std::vector< Value > &args, VM *vm)
Get the current process ID.
static char ** argv
Command line arguments.
static Value sys_crash(const std::vector< Value > &args, VM *vm)
Crash the VM / Program.
static i64 sys_fork_detached(const std::vector< Value > &args, VM *vm)
Run a native program detached.
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static Value sys_argv(const std::vector< Value > &args, VM *vm)
Get the current command line arguments – deprecated, use sys_args() instead.
static int argc
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 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 Value sys_args(const std::vector< Value > &args, VM *vm)
Get args array.
static i64 sys_argc(const std::vector< Value > &args, VM *vm)
Get the current number of command line arguments – deprecated, use len(sys_args()) instead.
static i64 sys_get_free_memory(const std::vector< Value > &args, VM *vm)
Get current free memory.
static Value sys_isatty(const std::vector< Value > &args, VM *vm)
Check if the current output is a terminal.
static Value sys_time_formatted(const std::vector< Value > &args, VM *vm)
Current time formatted.
static dupenv_ret dupenv(PhsString &out, const char *name)
static f64 sys_time(const std::vector< Value > &args, VM *vm)
Current time.
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
static i64 sys_fork(const std::vector< Value > &args, VM *vm)
Run a native program.
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.
static Value createArray(std::vector< Value > elements={})
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.