Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
NativeRuntime_library.cpp
Go to the documentation of this file.
2
3#ifdef _WIN32
4#include <windows.h>
5#ifdef _SHARED
6#define DLLEXPORT __declspec(dllexport)
7#endif
8#else
9#include <iostream>
10#ifdef _SHARED
11#define DLLEXPORT __attribute__((visibility("default")))
12#endif
13#endif
14#ifndef _SHARED
15#define DLLEXPORT
16#endif
17
18#include <vector>
19#include <cstring>
20#include <sstream>
21
22std::vector<std::string> splits(const std::string &input)
23{
24 std::vector<std::string> parts;
25
26 std::istringstream iss(input);
27 std::string word;
28 while (iss >> word)
29 {
30 parts.push_back(word);
31 }
32 return parts;
33}
34
35extern "C"
36{
37 DLLEXPORT void exec(const unsigned char embeddedBytecode[], size_t embeddedBytecodeSize, const char *moduleName,
38 const void *nativeFunctionsVector, const int argc, const char** argv)
39 {
40 try
41 {
42 std::vector<uint8_t> bytecodeData(embeddedBytecode, embeddedBytecode + embeddedBytecodeSize);
43 Phasor::NativeRuntime NativeRT(bytecodeData, argc, argv);
44
45 if (nativeFunctionsVector != nullptr)
46 {
47 const void* ptr = nativeFunctionsVector;
48 const auto* funcVector = static_cast<const std::vector<std::pair<std::string, void*>>*>(ptr);
49 for (const auto &nativeFunction : *funcVector)
50 {
51 NativeRT.addNativeFunction(nativeFunction.first, nativeFunction.second);
52 }
53 }
54
55 NativeRT.run();
56 }
57 catch (const std::exception &e)
58 {
59#ifdef _WIN32
60 MessageBoxA(nullptr, e.what(), (std::string(moduleName) + " | Phasor Runtime - Error").c_str(),
61 MB_OK | MB_ICONERROR);
62#else
63 std::cerr << "Error: " << e.what() << "\n\a";
64#endif
65 }
66 }
67
68 DLLEXPORT void jitExec(const char *script, const char *moduleName, const void *nativeFunctionsVector)
69 {
70 try
71 {
72 Phasor::NativeRuntime NativeRT(script, 0, nullptr);
73
74 if (nativeFunctionsVector != nullptr)
75 {
76 const void* ptr = nativeFunctionsVector;
77 const auto* funcVector = static_cast<const std::vector<std::pair<std::string, void*>>*>(ptr);
78 for (const auto &nativeFunction : *funcVector)
79 {
80 NativeRT.addNativeFunction(nativeFunction.first, nativeFunction.second);
81 }
82 }
83
84 NativeRT.run();
85 }
86 catch (const std::exception &e)
87 {
88#ifdef _WIN32
89 MessageBoxA(nullptr, e.what(), (std::string(moduleName) + " | Phasor Runtime - Error").c_str(),
90 MB_OK | MB_ICONERROR);
91#else
92 std::cerr << "Error: " << e.what() << "\n\a";
93#endif
94 }
95 }
96
97 DLLEXPORT void executeScript(void *vm, const char *script)
98 {
99 if (vm == nullptr)
100 {
101 return;
102 }
103
104 Phasor::VM *phasorVM = (Phasor::VM *)vm;
105 Phasor::NativeRuntime::eval(phasorVM, script);
106 }
107}
std::vector< std::string > splits(const std::string &input)
DLLEXPORT void executeScript(void *vm, const char *script)
Executes a Phasor script within an existing VM instance.
#define DLLEXPORT
DLLEXPORT void jitExec(const char *script, const char *moduleName, const void *nativeFunctionsVector)
Executes a Phasor script using Just-In-Time (JIT) compilation.
DLLEXPORT void exec(const unsigned char embeddedBytecode[], size_t embeddedBytecodeSize, const char *moduleName, const void *nativeFunctionsVector, const int argc, const char **argv)
Executes pre-compiled Phasor bytecode.
CLI wrapper for running Phasor scripts and bytecode in-process.
void addNativeFunction(const std::string &name, void *function)
static void eval(VM *vm, const std::string &script)
Virtual Machine.
Definition VM.hpp:18