Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
StdLib.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <cassert>
3#ifdef _WIN32
4#include <stdlib.h>
5#else
6#include <cstdlib>
7#endif
8
9namespace Phasor
10{
11
12char **StdLib::argv = nullptr;
13int StdLib::argc = 0;
14
16{
17 if (!name || name[0] == '\0')
18 {
20 }
21
22#ifdef _WIN32
23 char *buffer = nullptr;
24 size_t len = 0;
25 if (_dupenv_s(&buffer, &len, name) == 0 && buffer != nullptr)
26 {
27 out = buffer;
28 free(buffer);
30 }
31#else
32 const char *val = std::getenv(name);
33 if (val)
34 {
35 out = val;
37 }
38#endif
39
40 out.clear();
42}
43
44void StdLib::checkArgCount(const std::vector<Value> &args, size_t minimumArguments, const std::string &name,
45 bool allowMoreArguments)
46{
47 if (args.size() < minimumArguments)
48 {
49 throw std::runtime_error("Function '" + name + "' expects at least " + std::to_string(minimumArguments) +
50 " arguments, but got " + std::to_string(args.size()));
51 }
52 if (!allowMoreArguments && args.size() > minimumArguments)
53 {
54 throw std::runtime_error("Function '" + name + "' expects exactly " + std::to_string(minimumArguments) +
55 " arguments, but got " + std::to_string(args.size()));
56 }
57}
58
59bool StdLib::std_import(const std::vector<Value> &args, VM *vm)
60{
61 checkArgCount(args, 1, "using", true);
62
63 std::unordered_map<PhsString, std::function<void(Phasor::VM *)>> modules{
64 {"stdio", registerIOFunctions},
65 {"stdsys", registerSysFunctions},
66 {"stdmath", registerMathFunctions},
67 {"stdstr", registerStringFunctions},
68 {"stdtype", registerTypeConvFunctions},
69 {"stdmeta", registerMetaFunctions},
70 {"stdmem", registerMemoryFunctions},
71 {"stdrand", registerRandomFunctions},
72 {"stdarray", registerArrayFunctions},
73#ifndef SANDBOXED
74 {"stdfile", registerFileFunctions},
75#endif
76 {"std*",
77 [](Phasor::VM *vm) {
87#ifndef SANDBOXED
89#endif
90 }},
91 };
92
93 for (const auto &arg : args)
94 {
95 auto it = modules.find(arg.string());
96 if (it != modules.end())
97 {
98 it->second(vm);
99 }
100 else
101 {
102 throw std::runtime_error("Unknown module: " + arg.string());
103 }
104 }
105 return true;
106}
107
108#ifndef SANDBOXED
109Value StdLib::std_assert(const std::vector<Value> &args, VM *vm)
110{
111 checkArgCount(args, 1, "assert", true);
112
113 if (args.size() > 2)
114 { [[unlikely]]
115 throw std::runtime_error("Assert expects 1 or 2 arguments, but got " + std::to_string(args.size()));
116 }
117
118 bool haveMessage = false;
119 const char* message = nullptr;
120
121 if (args.size() == 2)
122 {
123 message = args[1].c_str();
124 haveMessage = true;
125 }
126
127#ifdef TRACING
128#ifndef NDEBUG
129 vm->log(std::format("StdLib::{}({:T})\n", __func__, args[0]));
130#else
131 vm->log(std::format("StdLib::{}({:T}): Assertion skipped (NDEBUG)\n", __func__, args[0]));
132#endif
133 vm->flush();
134#endif
135
136#ifndef NDEBUG
137 if (!args[0].isTruthy())
138 { [[unlikely]]
139 vm->logerr(std::format("StdLib::{}({:T}): Assertion failed!\n", __func__, args[0]));
140 if (haveMessage) vm->logerr(std::format("{}\n", message));
141 vm->flusherr();
142 }
143 if (haveMessage) assert(args[0].isTruthy() && message);
144 else assert(args[0].isTruthy());
145#endif
146 return Value();
147}
148#endif
149
150} // namespace Phasor
void clear() noexcept
static void registerStringFunctions(VM *vm)
Definition string.cpp:8
static void registerMemoryFunctions(VM *vm)
Definition memory.cpp:6
static char ** argv
Command line arguments.
Definition StdLib.hpp:48
static void registerFileFunctions(VM *vm)
Definition file.cpp:11
static void registerIOFunctions(VM *vm)
Definition io.cpp:7
static void registerArrayFunctions(VM *vm)
Definition array.cpp:8
static void registerTypeConvFunctions(VM *vm)
Definition typeconv.cpp:7
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static void registerMathFunctions(VM *vm)
Definition math.cpp:7
static int argc
Number of command line arguments.
Definition StdLib.hpp:49
static void registerSysFunctions(VM *vm)
Definition system.cpp:34
static void registerRandomFunctions(VM *vm)
Definition random.cpp:7
static void registerMetaFunctions(VM *vm)
Definition meta.cpp:8
static Value std_assert(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:109
static dupenv_ret dupenv(PhsString &out, const char *name)
Definition StdLib.cpp:15
static bool std_import(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:59
Virtual Machine.
Definition VM.hpp:36
void logerr(const Value &msg)
Log a Value to stderr.
Definition Utility.cpp:285
void log(const Value &msg)
Log a Value to stdout.
Definition Utility.cpp:279
void flush()
Flush stdout.
Definition Utility.cpp:291
void flusherr()
Flush stderr.
Definition Utility.cpp:296
A value in the Phasor VM.
Definition Value.hpp:59
The Phasor Programming Language and Runtime.
Definition AST.hpp:13