Phasor 3.1.1
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#ifdef _WIN32
38 DLLEXPORT void exec(const unsigned char embeddedBytecode[], size_t embeddedBytecodeSize, const char *moduleName,
39 const void *nativeFunctionsVector, const int argc, const char **argv)
40#else
41 DLLEXPORT void exec(const unsigned char embeddedBytecode[], size_t embeddedBytecodeSize, const char *,
42 const void *nativeFunctionsVector, const int argc, const char **argv)
43#endif
44 {
45 try
46 {
47 std::vector<uint8_t> bytecodeData(embeddedBytecode, embeddedBytecode + embeddedBytecodeSize);
48 Phasor::NativeRuntime NativeRT(bytecodeData, argc, argv);
49
50 if (nativeFunctionsVector != nullptr)
51 {
52 const void *ptr = nativeFunctionsVector;
53 const auto *funcVector = static_cast<const std::vector<std::pair<std::string, void *>> *>(ptr);
54 for (const auto &nativeFunction : *funcVector)
55 {
56 NativeRT.addNativeFunction(nativeFunction.first, nativeFunction.second);
57 }
58 }
59
60 NativeRT.run();
61 }
62 catch (const std::exception &e)
63 {
64#ifdef _WIN32
65 MessageBoxA(nullptr, e.what(), (std::string(moduleName) + " | Phasor Runtime - Error").c_str(),
66 MB_OK | MB_ICONERROR);
67#else
68 std::cerr << "Error: " << e.what() << "\n\a";
69#endif
70 }
71 }
72#ifdef _WIN32
73 DLLEXPORT void jitExec(const char *script, const char *moduleName, const void *nativeFunctionsVector)
74#else
75 DLLEXPORT void jitExec(const char *script, const char *, const void *nativeFunctionsVector)
76#endif
77 {
78 try
79 {
80 Phasor::NativeRuntime NativeRT(script, 0, nullptr);
81
82 if (nativeFunctionsVector != nullptr)
83 {
84 const void *ptr = nativeFunctionsVector;
85 const auto *funcVector = static_cast<const std::vector<std::pair<std::string, void *>> *>(ptr);
86 for (const auto &nativeFunction : *funcVector)
87 {
88 NativeRT.addNativeFunction(nativeFunction.first, nativeFunction.second);
89 }
90 }
91
92 NativeRT.run();
93 }
94 catch (const std::exception &e)
95 {
96#ifdef _WIN32
97 MessageBoxA(nullptr, e.what(), (std::string(moduleName) + " | Phasor Runtime - Error").c_str(),
98 MB_OK | MB_ICONERROR);
99#else
100 std::cerr << "Error: " << e.what() << "\n\a";
101#endif
102 }
103 }
104
105 DLLEXPORT void executeScript(void *vm, const char *script)
106 {
107 if (vm == nullptr)
108 {
109 return;
110 }
111
112 Phasor::VM *phasorVM = (Phasor::VM *)vm;
113 Phasor::NativeRuntime::eval(phasorVM, script);
114 }
115}
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 exec(const unsigned char embeddedBytecode[], size_t embeddedBytecodeSize, const char *, const void *nativeFunctionsVector, const int argc, const char **argv)
Executes pre-compiled Phasor bytecode.
DLLEXPORT void jitExec(const char *script, const char *, const void *nativeFunctionsVector)
Executes a Phasor script using Just-In-Time (JIT) compilation.
CLI wrapper for running Phasor scripts and bytecode in-process.
void addNativeFunction(const std::string &name, void *function)
static int eval(VM *vm, const std::string &script)
Virtual Machine.
Definition VM.hpp:30