17 if (!name || name[0] ==
'\0')
23 char *buffer =
nullptr;
25 if (_dupenv_s(&buffer, &len, name) == 0 && buffer !=
nullptr)
32 const char *val = std::getenv(name);
45 bool allowMoreArguments)
47 if (args.size() < minimumArguments)
49 throw std::runtime_error(
"Function '" + name +
"' expects at least " + std::to_string(minimumArguments) +
50 " arguments, but got " + std::to_string(args.size()));
52 if (!allowMoreArguments && args.size() > minimumArguments)
54 throw std::runtime_error(
"Function '" + name +
"' expects exactly " + std::to_string(minimumArguments) +
55 " arguments, but got " + std::to_string(args.size()));
93 for (
const auto &arg : args)
95 auto it = modules.find(arg.string());
96 if (it != modules.end())
102 throw std::runtime_error(
"Unknown module: " + arg.string());
115 throw std::runtime_error(
"Assert expects 1 or 2 arguments, but got " + std::to_string(args.size()));
118 bool haveMessage =
false;
119 const char* message =
nullptr;
121 if (args.size() == 2)
123 message = args[1].c_str();
129 vm->
log(std::format(
"StdLib::{}({:T})\n", __func__, args[0]));
131 vm->
log(std::format(
"StdLib::{}({:T}): Assertion skipped (NDEBUG)\n", __func__, args[0]));
137 if (!args[0].isTruthy())
139 vm->
logerr(std::format(
"StdLib::{}({:T}): Assertion failed!\n", __func__, args[0]));
140 if (haveMessage) vm->
logerr(std::format(
"{}\n", message));
143 if (haveMessage) assert(args[0].isTruthy() && message);
144 else assert(args[0].isTruthy());
static void registerStringFunctions(VM *vm)
static void registerMemoryFunctions(VM *vm)
static char ** argv
Command line arguments.
static void registerFileFunctions(VM *vm)
static void registerIOFunctions(VM *vm)
static void registerArrayFunctions(VM *vm)
static void registerTypeConvFunctions(VM *vm)
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static void registerMathFunctions(VM *vm)
static int argc
Number of command line arguments.
static void registerSysFunctions(VM *vm)
static void registerRandomFunctions(VM *vm)
static void registerMetaFunctions(VM *vm)
static Value std_assert(const std::vector< Value > &args, VM *vm)
static dupenv_ret dupenv(PhsString &out, const char *name)
static bool std_import(const std::vector< Value > &args, VM *vm)
void logerr(const Value &msg)
Log a Value to stderr.
void log(const Value &msg)
Log a Value to stdout.
void flush()
Flush stdout.
void flusherr()
Flush stderr.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.