Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
NativeRuntime_dynamic_main.cpp
Go to the documentation of this file.
1#include <cstdint>
2#include <cstdio>
3#include <cstdlib>
4#include <iostream>
5#include <fstream>
6#include <filesystem>
7#include <string>
8#include <vector>
9#include <nativeerror.h>
10
11#ifdef _WIN32
12#include <windows.h>
13#include <process.h>
14#pragma comment(lib, "User32.lib")
15#else
16#include <dlfcn.h>
17#include <unistd.h>
18#endif
19
20int main(int argc, char *argv[])
21{
22 std::string tempFile;
23 int exitCode = 1;
24
25 try
26 {
27#ifdef _WIN32
28 // Load phasor-runtime.dll
29 HMODULE hRuntime = LoadLibraryA("phasorrt.dll");
30 if (!hRuntime)
31 {
32 MessageBoxA(nullptr, "Could not load phasorrt.dll",
33 (std::string(moduleName) + " | Phasor Application - Error").c_str(), MB_OK | MB_ICONERROR);
34 std::filesystem::remove(tempFile);
35 return 1;
36 }
37
38 // Get the exec function
39 typedef void(CALLBACK * ExecFunc)(const unsigned char[], size_t, const char *, const void *);
40 ExecFunc execFunc = (ExecFunc)GetProcAddress(hRuntime, "exec");
41 if (!execFunc)
42 {
43 MessageBoxA(nullptr, "Could not find exec function in DLL",
44 (std::string(moduleName) + " | Phasor Application - Error").c_str(), MB_OK | MB_ICONERROR);
45 FreeLibrary(hRuntime);
46 std::filesystem::remove(tempFile);
47 return 1;
48 }
49
50 // Execute the bytecode
51 execFunc(embeddedBytecode, embeddedBytecodeSize, moduleName.c_str(), nullptr);
52 exitCode = 0;
53
54 // Cleanup
55 FreeLibrary(hRuntime);
56#else
57 // Load phasor-runtime.so
58 void *hRuntime = dlopen("libphasorrt.so", RTLD_LAZY);
59 if (!hRuntime)
60 {
61 std::cerr << "Error: Could not load libphasorrt.so: " << dlerror() << "\n";
62 std::filesystem::remove(tempFile);
63 return 1;
64 }
65
66 // Get the exec function
67 typedef void (*ExecFunc)(const char *, size_t, const char *, const void *);
68 ExecFunc execFunc = (ExecFunc)dlsym(hRuntime, "exec");
69 if (!execFunc)
70 {
71 std::cerr << "Error: Could not find exec function in library\n";
72 dlclose(hRuntime);
73 std::filesystem::remove(tempFile);
74 return 1;
75 }
76
77 // Execute the bytecode
78 execFunc(tempFile.c_str(), tempFile.size(), moduleName.c_str(), nullptr);
79 exitCode = 0;
80
81 // Cleanup
82 dlclose(hRuntime);
83#endif
84
85 // Remove temporary file
86 std::filesystem::remove(tempFile);
87 }
88 catch (const std::exception &e)
89 {
90 std::cerr << "Runtime Error: " << e.what() << "\n";
91 if (!tempFile.empty())
92 {
93 std::filesystem::remove(tempFile);
94 }
95 return 1;
96 }
97
98 return exitCode;
99}
int main()
Definition LSP_main.cpp:121