From 1c10438fc37d9b0e9b205f47200b7df3784a225b Mon Sep 17 00:00:00 2001
From: "Nicolas \"Pixel\" Noble" <pixel@nobis-crew.org>
Date: Tue, 8 Jun 2010 18:45:42 +0200
Subject: Fixing a few things for the apple dll loader - mainly disabling the
 memory loader and fallbacking on the linux 'from-disk' loader.

---
 src/lua-plugin.cc | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/lua-plugin.cc b/src/lua-plugin.cc
index 1e4ebc7..3bc7e83 100644
--- a/src/lua-plugin.cc
+++ b/src/lua-plugin.cc
@@ -90,7 +90,8 @@ void LuaLoadPlugin(const String & fname, Lua * L) throw (GeneralException) {
     init_ptr(L);
 }
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) and 0
+// This actually requires bundles, and not dylibs... disabling it for now.
 #include <mach/vm_map.h>
 #include <mach-o/dyld.h>
 #include <mach/mach_init.h>
@@ -99,16 +100,25 @@ void LuaLoadPlugin(Handle * h, Lua * L) throw (GeneralException) {
     Byte * buffer;
     NSObjectFileImage image;
 
-    kern_return_t result = vm_allocate(mach_task_self(), (vm_address_t *) &buffer, h->GetSize(), VM_FLAGS_ANYWHERE);
+    if (vm_allocate(mach_task_self(), (vm_address_t *) &buffer, h->GetSize(), VM_FLAGS_ANYWHERE) != KERN_SUCCESS) {
+        throw GeneralException("Can't allocate kernel memory to load library " + h->GetName());
+    }
     h->read(buffer, h->GetSize());
     
-    if (NSCreateObjectFileImageFromMemory(buffer, h->GetSize(), &image) != NSObjectFileImageSuccess)
-        throw("Can't load library " + h->GetName() + " from memory.");
+    if (NSCreateObjectFileImageFromMemory(buffer, h->GetSize(), &image) != NSObjectFileImageSuccess) {
+        vm_deallocate(mach_task_self(), (vm_address_t) buffer, h->GetSize());
+        throw GeneralException("Can't load library " + h->GetName() + " from memory.");
+    }
         
     NSModule module;
+    
+    int o = 0;
+    if (h->GetName()[0] == '@') o = 1;
 
-    if (!(module = NSLinkModule(image, h->GetName().to_charp(), NSLINKMODULE_OPTION_PRIVATE)))
-        throw("Can't link library " + h->GetName() + " from memory.");
+    if (!(module = NSLinkModule(image, h->GetName().to_charp() + o, NSLINKMODULE_OPTION_PRIVATE))) {
+        NSDestroyObjectFileImage(image);
+        throw GeneralException("Can't link library " + h->GetName() + " from memory.");
+    }
 
     NSSymbol symbol = NSLookupSymbolInModule(module, "init_plugin");
     init_ptr_t init_ptr = (init_ptr_t) NSAddressOfSymbol(symbol);
-- 
cgit v1.2.3