Phasor 3.3.0
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 try
23 {
24#ifdef _WIN32
25 // Load phasor-runtime.dll
26 HMODULE hRuntime = LoadLibraryA("phasorrt.dll");
27 if (!hRuntime)
28 {
29 MessageBoxA(nullptr, "Could not load phasorrt.dll",
30 (std::string(moduleName) + " | Phasor Application - Error").c_str(), MB_OK | MB_ICONERROR);
31 std::filesystem::remove(tempFile);
32 return 1;
33 }
34
35 // Get the exec function
36 typedef int(CALLBACK * ExecFunc)(void *, const unsigned char[], size_t, const char *, int, const char **);
37 ExecFunc execFunc = (ExecFunc)GetProcAddress(hRuntime, "exec");
38 if (!execFunc)
39 {
40 MessageBoxA(nullptr, "Could not find exec function in DLL",
41 (std::string(moduleName) + " | Phasor Application - Error").c_str(), MB_OK | MB_ICONERROR);
42 FreeLibrary(hRuntime);
43 std::filesystem::remove(tempFile);
44 return 1;
45 }
46
47 // Execute the bytecode
48 exitCode =
49 execFunc(nullptr, embeddedBytecode, embeddedBytecodeSize, moduleName.c_str(), argc, (const char **)argv);
50
51 // Cleanup
52 FreeLibrary(hRuntime);
53#else
54 // Load phasor-runtime.so
55 void *hRuntime = dlopen("libphasorrt.so", RTLD_LAZY);
56 if (!hRuntime)
57 {
58 std::cerr << "Error: Could not load libphasorrt.so: " << dlerror() << "\n";
59 std::filesystem::remove(tempFile);
60 return 1;
61 }
62
63 // Get the exec function
64 typedef int (*ExecFunc)(void *, const unsigned char[], size_t, const char *, int, const char **);
65 ExecFunc execFunc = (ExecFunc)dlsym(hRuntime, "exec");
66 if (!execFunc)
67 {
68 std::cerr << "Error: Could not find exec function in library\n";
69 dlclose(hRuntime);
70 std::filesystem::remove(tempFile);
71 return 1;
72 }
73
74 // Execute the bytecode
75 int exitCode =
76 execFunc(nullptr, embeddedBytecode, embeddedBytecodeSize, moduleName.c_str(), argc, (const char **)argv);
77
78 // Cleanup
79 dlclose(hRuntime);
80
81 return exitCode;
82#endif
83 }
84 catch (const std::exception &e)
85 {
86 std::cerr << "Runtime Error: " << e.what() << "\n";
87 }
88 return 1;
89}
int main()
Definition LSP_main.cpp:130