Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
NativeRuntime.cpp
Go to the documentation of this file.
1#include "NativeRuntime.hpp"
4#include "../../AST/AST.hpp"
11#include <filesystem>
12#include <iostream>
13#include <nativeerror.h>
14
15namespace Phasor
16{
17
18NativeRuntime::NativeRuntime(const std::vector<uint8_t> &bytecodeData, const int argc, const char **argv)
19 : m_bytecodeData(bytecodeData), m_argc(argc), m_argv(const_cast<char **>(argv))
20{
21 BytecodeDeserializer deserializer;
23 m_vm = std::make_unique<VM>();
24}
25
26NativeRuntime::NativeRuntime(const Phasor::Bytecode &bytecode, const int argc, const char **argv)
27 : m_bytecode(bytecode), m_argc(argc), m_argv(const_cast<char **>(argv))
28{
29 m_vm = std::make_unique<VM>();
30}
31
32NativeRuntime::NativeRuntime(const std::string &script, const int argc, const char **argv)
33 : m_script(script), m_argc(argc), m_argv(const_cast<char **>(argv))
34{
35 Lexer lexer(m_script);
36 auto tokens = lexer.tokenize();
37
38 Parser parser(tokens);
39 auto program = parser.parse();
40
41 CodeGenerator codegen;
42 m_bytecode = codegen.generate(*program);
43 m_vm = std::make_unique<VM>();
44}
45
46NativeRuntime::NativeRuntime(const Phasor::VM &vm, const std::string &script, const int argc, const char **argv)
47 : m_vm(const_cast<VM *>(&vm), [](VM *) {}), m_script(script), m_argc(argc), m_argv(const_cast<char **>(argv))
48{
49 Lexer lexer(m_script);
50 Parser parser(lexer.tokenize());
51 CodeGenerator codegen;
52 m_bytecode = codegen.generate(*parser.parse());
53}
54
55NativeRuntime::NativeRuntime(Phasor::VM *vm, const std::vector<uint8_t> &bytecodeData, const int argc,
56 const char **argv)
57 : m_bytecodeData(bytecodeData), m_argc(argc), m_argv(const_cast<char **>(argv))
58{
59 if (vm)
60 m_vm = std::shared_ptr<VM>(vm, [](VM *) {}); // non-owning, caller manages lifetime
61 else
62 m_vm = std::make_shared<VM>(); // owning, we manage lifetime
63
64 BytecodeDeserializer deserializer;
66}
67
69{
70 m_vm.reset();
71 m_vm = nullptr;
72}
73
74void NativeRuntime::addNativeFunction(const std::string &name, void *function)
75{
76 using RawFunctionPtr = Value (*)(const std::vector<Value> &, VM *);
77 RawFunctionPtr rawPtr = reinterpret_cast<RawFunctionPtr>(function);
78 NativeFunction nativeFunction = rawPtr;
79 m_vm->registerNativeFunction(name, nativeFunction);
80}
81
82int NativeRuntime::eval(VM *vm, const std::string &script)
83{
84 Lexer lexer(script);
85 auto tokens = lexer.tokenize();
86
87 Parser parser(tokens);
88 auto program = parser.parse();
89
90 CodeGenerator codegen;
91 auto bytecode = codegen.generate(*program);
92
93 return vm->run(bytecode);
94}
95
97{
98
99 try
100 {
104#if defined(_WIN32)
105 m_vm->initFFI("plugins");
106#elif defined(__APPLE__)
107 m_vm->initFFI("/Library/Application Support/org.Phasor.Phasor/plugins");
108#elif defined(__linux__)
109 m_vm->initFFI("/usr/lib/phasor/plugins/");
110#endif
111 m_vm->setImportHandler([](const std::filesystem::path &path) {
112 throw std::runtime_error("Imports not supported in pure binary runtime yet: " + path.string());
113 });
114
115 int status = m_vm->run(m_bytecode);
116
117 if (status != 0)
118 {
119 m_vm->reset(true, false, false);
120 m_vm->resetStatus();
121 }
122
123 return status;
124 }
125 catch (const std::exception &)
126 {
127 throw;
128 }
129 return 0;
130}
131
132int NativeRuntime::runFunctionInt(std::string functionName)
133{
134 try
135 {
139#if defined(_WIN32)
140 m_vm->initFFI("plugins");
141#elif defined(__APPLE__)
142 m_vm->initFFI("/Library/Application Support/org.Phasor.Phasor/plugins");
143#elif defined(__linux__)
144 m_vm->initFFI("/usr/lib/phasor/plugins/");
145#endif
146 m_vm->setImportHandler([](const std::filesystem::path &path) {
147 throw std::runtime_error("Imports not supported in pure binary runtime yet: " + path.string());
148 });
149
150 Value ret = m_vm->runFunction(functionName, m_bytecode);
151
152 return static_cast<int>(ret.asInt());
153 }
154 catch (const std::exception &)
155 {
156 throw;
157 }
158 return 0;
159}
160
161std::optional<std::string> NativeRuntime::runFunctionString(std::string functionName)
162{
163 try
164 {
168#if defined(_WIN32)
169 m_vm->initFFI("plugins");
170#elif defined(__APPLE__)
171 m_vm->initFFI("/Library/Application Support/org.Phasor.Phasor/plugins");
172#elif defined(__linux__)
173 m_vm->initFFI("/usr/lib/phasor/plugins/");
174#endif
175 m_vm->setImportHandler([](const std::filesystem::path &path) {
176 throw std::runtime_error("Imports not supported in pure binary runtime yet: " + path.string());
177 });
178
179 Value ret = m_vm->runFunction(functionName, m_bytecode);
180
181 return ret.asString();
182 }
183 catch (const std::exception &)
184 {
185 throw;
186 }
187 return std::nullopt;
188}
189
190} // namespace Phasor
Bytecode binary format deserializer.
Bytecode deserialize(const std::vector< uint8_t > &data)
Deserialize bytecode from binary buffer.
Code generator for Phasor VM.
Definition CodeGen.hpp:108
Bytecode generate(const AST::Program &program, const std::unordered_map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
Definition CodeGen.cpp:8
Lexer.
Definition Lexer.hpp:13
std::vector< Token > tokenize()
Definition Lexer.cpp:27
void addNativeFunction(const std::string &name, void *function)
NativeRuntime(const std::vector< uint8_t > &bytecodeData, const int argc, const char **argv)
int runFunctionInt(std::string functionName)
std::shared_ptr< Phasor::VM > m_vm
static int eval(VM *vm, const std::string &script)
std::optional< std::string > runFunctionString(std::string functionName)
std::vector< uint8_t > m_bytecodeData
Parser.
Definition Parser.hpp:13
std::unique_ptr< AST::Program > parse()
Definition Parser.cpp:74
static char ** argv
Command line arguments.
Definition StdLib.hpp:47
static void registerFunctions(VM &vm)
Definition StdLib.hpp:35
static int argc
Number of command line arguments.
Definition StdLib.hpp:48
Virtual Machine.
Definition VM.hpp:33
int run(const Bytecode &bytecode, const size_t startPC=0)
Run the virtual machine Exits -1 on uncaught exception.
Definition Utility.cpp:88
A value in the Phasor VM.
Definition Value.hpp:58
int64_t asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:159
std::string asString() const noexcept
Get the value as a string.
Definition Value.hpp:185
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Definition StdLib.hpp:24
Complete bytecode structure.
Definition CodeGen.hpp:50