1 module bindbc.cocoa.dynloadruntime;
2 
3 import bindbc.cocoa.runtime;
4 
5 import bindbc.loader;
6 
7 private {
8     SharedLib lib;
9     RTSupport loadedVersion;
10 }
11 
12 enum RTSupport {
13     noLibrary,
14     badLibrary,
15     rtLibrary
16 }
17 
18 void unloadRT()
19 {
20     if(lib != invalidHandle) {
21         lib.unload();
22     }
23 }
24 
25 RTSupport loadedRTVersion() { return loadedVersion; }
26 
27 bool isRTLoaded() { return lib != invalidHandle; }
28 
29 RTSupport loadRT(const(char)* libName)
30 {
31     // If the library isn't yet loaded, load it now.
32     if(lib == invalidHandle) {
33         lib = load(libName);
34         if(lib == invalidHandle) {
35             return RTSupport.noLibrary;
36         }
37     }
38 
39     auto errCount = errorCount();
40 
41     lib.bindSymbol(cast(void**)&objc_registerClassPair, "objc_registerClassPair");
42     lib.bindSymbol(cast(void**)&varclass_addIvar, "class_addIvar");
43     lib.bindSymbol(cast(void**)&varclass_addMethod, "class_addMethod");
44     lib.bindSymbol(cast(void**)&varobjc_allocateClassPair, "objc_allocateClassPair");
45     lib.bindSymbol(cast(void**)&objc_disposeClassPair, "objc_disposeClassPair");
46     lib.bindSymbol(cast(void**)&varobjc_getClass, "objc_getClass");
47     lib.bindSymbol(cast(void**)&objc_getMetaClass, "objc_getMetaClass");
48     lib.bindSymbol(cast(void**)&varobjc_lookUpClass, "objc_lookUpClass");
49 
50     lib.bindSymbol(cast(void**)&objc_msgSend, "objc_msgSend");
51     lib.bindSymbol(cast(void**)&objc_msgSendSuper, "objc_msgSendSuper");
52     version(AnyX86) lib.bindSymbol(cast(void**)&objc_msgSend_stret, "objc_msgSend_stret");
53     version(X86) lib.bindSymbol(cast(void**)&objc_msgSend_fpret, "objc_msgSend_fpret");
54 
55     lib.bindSymbol(cast(void**)&varobject_getClassName, "object_getClassName");
56     lib.bindSymbol(cast(void**)&object_getInstanceVariable, "object_getInstanceVariable");
57     lib.bindSymbol(cast(void**)&object_setInstanceVariable, "object_setInstanceVariable");
58     lib.bindSymbol(cast(void**)&varsel_registerName, "sel_registerName");
59 
60     lib.bindSymbol(cast(void**)&varclass_getInstanceMethod, "class_getInstanceMethod");
61     lib.bindSymbol(cast(void**)&method_setImplementation, "method_setImplementation");
62 
63     lib.bindSymbol(cast(void**)&class_addProtocol, "class_addProtocol");
64     lib.bindSymbol(cast(void**)&objc_getProtocol, "objc_getProtocol");
65     lib.bindSymbol(cast(void**)&objc_allocateProtocol, "objc_allocateProtocol"); // min 10.7
66     lib.bindSymbol(cast(void**)&objc_registerProtocol, "objc_registerProtocol"); // min 10.7
67     lib.bindSymbol(cast(void**)&class_conformsToProtocol, "class_conformsToProtocol"); // min 10.5
68     lib.bindSymbol(cast(void**)&protocol_addMethodDescription, "protocol_addMethodDescription"); // min 10.7
69     
70     if(errorCount() != errCount) loadedVersion = RTSupport.badLibrary;
71     else loadedVersion = RTSupport.rtLibrary;
72 
73     return loadedVersion;
74 }