Repository navigation
Expand file tree
/
Copy pathhost.c
More file actions
60 lines (52 loc) · 1.59 KB
/
Copy pathhost.c
File metadata and controls
60 lines (52 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <dlfcn.h>
#include <libgen.h>
#include <limits.h>
#include <mach-o/dyld.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*LuaObjCMain)(int argc, char *argv[]);
static void framework_path(char *destination, size_t capacity) {
const char *override = getenv("LUA_OBJC_APPKIT_MODULE");
if (override && override[0] != '\0') {
snprintf(destination, capacity, "%s", override);
return;
}
char executable[PATH_MAX];
uint32_t length = (uint32_t)sizeof(executable);
if (_NSGetExecutablePath(executable, &length) != 0) {
snprintf(destination, capacity, "build/AppKit.dylib");
return;
}
char resolved[PATH_MAX];
if (!realpath(executable, resolved)) {
snprintf(resolved, sizeof(resolved), "%s", executable);
}
char directoryBuffer[PATH_MAX];
snprintf(directoryBuffer, sizeof(directoryBuffer), "%s", resolved);
snprintf(destination, capacity, "%s/build/AppKit.dylib",
dirname(directoryBuffer));
}
int main(int argc, char *argv[]) {
char path[PATH_MAX];
framework_path(path, sizeof(path));
/*
* RTLD_GLOBAL lets Lua-loaded companion modules resolve Lua symbols and
* the private AppKitNative entry point from the framework runtime.
*/
void *framework = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
if (!framework) {
fprintf(stderr, "lua-objc: cannot load %s: %s\n", path, dlerror());
return 1;
}
LuaObjCMain run = (LuaObjCMain)dlsym(framework, "lua_objc_main");
if (!run) {
fprintf(stderr, "lua-objc: AppKit module has no lua_objc_main: %s\n",
dlerror());
dlclose(framework);
return 1;
}
int status = run(argc, argv);
dlclose(framework);
return status;
}