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