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