Phasor 3.1.1
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
4namespace Phasor
5{
6
7char **StdLib::argv = nullptr;
8int StdLib::argc = 0;
9char **StdLib::envp = nullptr;
10
11#ifndef __EMSCRIPTEN__
12#ifndef SANDBOXED
13int StdLib::dupenv(std::string &out, const char *name, char *const argp[])
14{
15 if (!name || !argp)
16 {
17 return 1;
18 }
19
20 const size_t key_len = strlen(name);
21
22 const char *val = NULL;
23 for (size_t i = 0; argp[i]; i++)
24 {
25 const char *entry = argp[i];
26 if (strncmp(entry, name, key_len) == 0 && entry[key_len] == '=')
27 {
28 val = entry + key_len + 1;
29 break;
30 }
31 }
32 if (!val)
33 {
34 out.clear();
35 return 2;
36 }
37
38 out = std::string(val);
39 return 0;
40}
41#endif
42#endif
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
59Value StdLib::std_import(const std::vector<Value> &args, VM *vm)
60{
61#ifdef __EMSCRIPTEN__
62 return false;
63#else
64 checkArgCount(args, 1, "using", true);
65 for (const auto &arg : args)
66 {
67 if (arg.getType() != ValueType::String)
68 {
69 throw std::runtime_error("All arguments to 'using' must be strings");
70 return false;
71 }
72 auto moduleName = arg.asString();
73
74 if (moduleName == "stdio") [[likely]]
76 else if (moduleName == "stdsys") [[likely]]
78 else if (moduleName == "stdmath")
80 else if (moduleName == "stdstr")
82 else if (moduleName == "stdtype")
84#ifndef SANDBOXED
85 else if (moduleName == "stdfile")
87#endif
88 else if (moduleName == "std*") [[unlikely]]
89 {
95#ifndef SANDBOXED
97#endif
98 }
99 else
100 {
101 throw std::runtime_error("Unknown standard library module: " + moduleName);
102 return false;
103 }
104 }
105 return true;
106#endif
107}
108
109#ifndef SANDBOXED
110Value StdLib::std_assert(const std::vector<Value>& args, VM* vm)
111{
112 checkArgCount(args, 1, "assert");
113
114#ifdef TRACING
115 #ifndef NDEBUG
116 vm->log(std::format("StdLib::{}({:T})\n", __func__, args[0]));
117 #else
118 vm->log(std::format("StdLib::{}({:T}): Assertion skipped (NDEBUG)\n", __func__, args[0]));
119 #endif
120 vm->flush();
121#endif
122
123#ifndef NDEBUG
124 if (!args[0].isTruthy())
125 {
126 vm->logerr(std::format("StdLib::{}({:T}): Assertion failed!\n", __func__, args[0]));
127 vm->flusherr();
128 }
129 assert(args[0].isTruthy());
130#endif
131 return Value();
132}
133#endif
134
135} // namespace Phasor
static void registerStringFunctions(VM *vm)
Definition string.cpp:7
static void registerFileFunctions(VM *vm)
Definition file.cpp:10
static char ** argv
Command line arguments.
Definition StdLib.hpp:40
static void registerIOFunctions(VM *vm)
Definition io.cpp:13
static void registerTypeConvFunctions(VM *vm)
Definition typeconv.cpp:6
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:6
static int argc
Number of command line arguments.
Definition StdLib.hpp:41
static Value std_import(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:59
static void registerSysFunctions(VM *vm)
Definition system.cpp:20
static Value std_assert(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:110
static char ** envp
Environment variables.
Definition StdLib.hpp:42
static int dupenv(std::string &out, const char *name, char *const argp[])
Definition StdLib.cpp:13
Virtual Machine.
Definition VM.hpp:30
void logerr(const Value &msg)
Log a Value to stderr.
Definition Utility.cpp:182
void log(const Value &msg)
Log a Value to stdout.
Definition Utility.cpp:176
void flush()
Flush stdout.
Definition Utility.cpp:188
void flusherr()
Flush stderr.
Definition Utility.cpp:193
A value in the Phasor VM.
Definition Value.hpp:67
The Phasor Programming Language and Runtime.
Definition AST.hpp:11