|
From: <cod...@go...> - 2008-12-06 12:05:34
|
Author: jam...@us...
Date: Sat Dec 6 04:04:52 2008
New Revision: 360
Modified:
branches/objc2/hoc/HOC/HOC/NewClass.hs
branches/objc2/hoc/HOC_cbits/Exceptions.m
branches/objc2/hoc/HOC_cbits/Methods.h
branches/objc2/hoc/HOC_cbits/Methods.m
branches/objc2/hoc/HOC_cbits/NewClass.h
branches/objc2/hoc/HOC_cbits/NewClass.m
Log:
Confined all usage of objc_method_list and objc_method structs to Methods.h
and Methods.m in cbits.
On the Haskell side, MethodList is now a ForeignPtr rather than a Ptr.
Small changes were made to properly
handle that as well.
Modified: branches/objc2/hoc/HOC/HOC/NewClass.hs
==============================================================================
--- branches/objc2/hoc/HOC/HOC/NewClass.hs (original)
+++ branches/objc2/hoc/HOC/HOC/NewClass.hs Sat Dec 6 04:04:52 2008
@@ -28,27 +28,29 @@
type IMP = FFICif -> Ptr () -> Ptr (Ptr ()) -> IO (Ptr ObjCObject)
foreign import ccall "wrapper" wrapIMP :: IMP -> IO (FunPtr IMP)
-newtype MethodList = MethodList (Ptr MethodList)
+newtype MethodList = MethodList (ForeignPtr MethodList)
newtype IvarList = IvarList (ForeignPtr IvarList)
foreign import ccall "NewClass.h newClass"
c_newClass :: Ptr ObjCObject -> CString
-> Ptr IvarList
- -> MethodList -> MethodList
+ -> Ptr MethodList -> Ptr MethodList
-> IO ()
newClass :: Ptr ObjCObject -> CString
-> IvarList
-> MethodList -> MethodList
-> IO ()
-newClass sc name (IvarList ivars) ms cms =
- withForeignPtr ivars $ \ivars -> do
- c_newClass sc name ivars ms cms
+newClass sc name (IvarList ivars) (MethodList ms) (MethodList cms) =
+ withForeignPtr ivars $ \ivars ->
+ withForeignPtr ms $ \ms ->
+ withForeignPtr cms $ \cms -> do
+ c_newClass sc name ivars ms cms
foreign import ccall "NewClass.h makeMethodList"
- makeMethodList :: Int -> IO MethodList
+ rawMakeMethodList :: Int -> IO (Ptr MethodList)
foreign import ccall "NewClass.h setMethodInList"
- rawSetMethodInList :: MethodList -> Int
+ rawSetMethodInList :: Ptr MethodList -> Int
-> SEL -> CString
-> FFICif -> FunPtr IMP
-> IO ()
@@ -72,10 +74,17 @@
withForeignPtr ivars $ \ivars -> do
c_setIvarInList ivars n name ty sz align
-setMethodInList methodList idx sel typ cif imp = do
- typC <- newCString typ
- thunk <- wrapIMP imp
- rawSetMethodInList methodList idx sel typC cif thunk
+makeMethodList :: Int -> IO MethodList
+makeMethodList n = do
+ methods <- rawMakeMethodList n
+ methods <- newForeignPtr freePtr methods
+ return (MethodList methods)
+
+setMethodInList (MethodList methodList) idx sel typ cif imp =
+ withForeignPtr methodList $ \methodList -> do
+ typC <- newCString typ
+ thunk <- wrapIMP imp
+ rawSetMethodInList methodList idx sel typC cif thunk
makeDefaultIvarList = do
list <- makeIvarList 1
@@ -98,33 +107,11 @@
getHaskellDataCif = getCifForSelector (undefined :: Class () -> ID () ->
IO (ID ()))
-- actually -> IO (Ptr
()) ...
-setHaskellRetainMethod methodList idx = do
- typC <- newCString "@@:"
- thunk <- wrapIMP haskellObject_retain_IMP
- rawSetMethodInList methodList
- idx
- retainSelector
- typC
- retainCif
- thunk
+setHaskellRetainMethod methodList idx =
+ setMethodInList methodList idx retainSelector "@@:" retainCif
haskellObject_retain_IMP
-setHaskellReleaseMethod methodList idx = do
- typC <- newCString "v@:"
- thunk <- wrapIMP haskellObject_release_IMP
- rawSetMethodInList methodList
- idx
- releaseSelector
- typC
- releaseCif
- thunk
-
-setHaskellDataMethod methodList idx super mbDat = do
- typC <- newCString "^v@:#"
- thunk <- wrapIMP (getHaskellData_IMP super mbDat)
- rawSetMethodInList methodList
- idx
- getHaskellDataSelector
- typC
- getHaskellDataCif
- thunk
+setHaskellReleaseMethod methodList idx =
+ setMethodInList methodList idx releaseSelector "v@:" releaseCif
haskellObject_release_IMP
+setHaskellDataMethod methodList idx super mbDat =
+ setMethodInList methodList idx getHaskellDataSelector "^v@:#"
getHaskellDataCif (getHaskellData_IMP super mbDat)
Modified: branches/objc2/hoc/HOC_cbits/Exceptions.m
==============================================================================
--- branches/objc2/hoc/HOC_cbits/Exceptions.m (original)
+++ branches/objc2/hoc/HOC_cbits/Exceptions.m Sat Dec 6 04:04:52 2008
@@ -40,20 +40,14 @@
{
if(!excWrapperInited)
{
- struct objc_method_list *methods = makeMethodList(1);
- struct objc_method_list *class_methods = makeMethodList(0);
+ struct hoc_method_list *methods = makeMethodList(1);
+ struct hoc_method_list *class_methods = makeMethodList(0);
struct hoc_ivar_list *ivars = makeIvarList(1);
struct objc_ivar *stablePtrIvar;
selDealloc = getSelectorForName("dealloc");
-#ifdef GNUSTEP
- methods->method_list[0].method_name = (SEL)"dealloc";
-#else
- methods->method_list[0].method_name = selDealloc;
-#endif
- methods->method_list[0].method_types = "v@:";
- methods->method_list[0].method_imp = (IMP) &exc_dealloc;
+ setMethodInListWithIMP(methods, 0, selDealloc, "v@:", (IMP)
&exc_dealloc);
setIvarInList(ivars, 0, hsExceptionIvarName, "^v", sizeof(void *),
IVAR_PTR_ALIGN);
Modified: branches/objc2/hoc/HOC_cbits/Methods.h
==============================================================================
--- branches/objc2/hoc/HOC_cbits/Methods.h (original)
+++ branches/objc2/hoc/HOC_cbits/Methods.h Sat Dec 6 04:04:52 2008
@@ -18,12 +18,36 @@
void **args
);
-struct objc_method_list * makeMethodList(int n);
+struct hoc_method {
+ SEL method_name;
+ char *method_types;
+ IMP method_imp;
+};
+
+struct hoc_method_list {
+ int method_count;
+
+ /* variable length structure */
+ struct hoc_method method_list[1];
+};
+
+struct hoc_method_list * makeMethodList(int n);
+
+void setMethodInListWithIMP(
+ struct hoc_method_list *list,
+ int i,
+ SEL sel,
+ char *types, /* never deallocate this */
+ IMP imp /* never deallocate this */
+ );
+
void setMethodInList(
- struct objc_method_list *list,
+ struct hoc_method_list *list,
int i,
SEL sel,
char *types, /* never deallocate this */
ffi_cif *cif, /* never deallocate this */
haskellIMP imp
);
+
+struct objc_method_list * convertMethodList(struct hoc_method_list * list);
\ No newline at end of file
Modified: branches/objc2/hoc/HOC_cbits/Methods.m
==============================================================================
--- branches/objc2/hoc/HOC_cbits/Methods.m (original)
+++ branches/objc2/hoc/HOC_cbits/Methods.m Sat Dec 6 04:04:52 2008
@@ -22,23 +22,58 @@
return closure;
}
-struct objc_method_list * makeMethodList(int n)
+struct hoc_method_list * makeMethodList(int n)
{
- struct objc_method_list *list =
- calloc(1, sizeof(struct objc_method_list)
+ struct hoc_method_list *list =
+ calloc(1, sizeof(struct hoc_method_list)
+ (n-1) * sizeof(struct objc_method));
list->method_count = n;
return list;
}
void setMethodInList(
- struct objc_method_list *list,
+ struct hoc_method_list *list,
int i,
SEL sel,
char *types,
ffi_cif *cif,
haskellIMP imp
)
+{
+ setMethodInListWithIMP(list, i, sel, types, (IMP) newIMP(cif, imp) );
+}
+
+void setMethodInListWithIMP(
+ struct hoc_method_list *list,
+ int i,
+ SEL sel,
+ char *types,
+ IMP imp
+ )
+{
+ list->method_list[i].method_name = sel;
+ list->method_list[i].method_types = types;
+ list->method_list[i].method_imp = imp;
+}
+
+/* Was previously makeMethodList */
+static struct objc_method_list * makeObjcMethodList(int n)
+{
+ struct objc_method_list *list =
+ calloc(1, sizeof(struct objc_method_list)
+ + (n-1) * sizeof(struct objc_method));
+ list->method_count = n;
+ return list;
+}
+
+/* Was previously setMethodInList */
+static void setObjCMethodInList(
+ struct objc_method_list *list,
+ int i,
+ SEL sel,
+ char *types,
+ IMP imp
+ )
{
#ifdef GNUSTEP
list->method_list[i].method_name = (SEL) sel_get_name(sel);
@@ -46,5 +81,20 @@
list->method_list[i].method_name = sel;
#endif
list->method_list[i].method_types = types;
- list->method_list[i].method_imp = (IMP) newIMP(cif, imp);
+ list->method_list[i].method_imp = imp;
}
+
+struct objc_method_list *
+convertMethodList(struct hoc_method_list * list) {
+ struct objc_method_list * newList =
makeObjcMethodList(list->method_count);
+ int i;
+
+ for(i = 0; i < list->method_count; i++)
+ {
+ struct hoc_method * method = &list->method_list[i];
+
+ setObjCMethodInList(newList, i, method->method_name,
method->method_types, method->method_imp);
+ }
+
+ return newList;
+}
\ No newline at end of file
Modified: branches/objc2/hoc/HOC_cbits/NewClass.h
==============================================================================
--- branches/objc2/hoc/HOC_cbits/NewClass.h (original)
+++ branches/objc2/hoc/HOC_cbits/NewClass.h Sat Dec 6 04:04:52 2008
@@ -5,10 +5,11 @@
#endif
struct hoc_ivar_list;
+struct hoc_method_list;
void newClass(struct objc_class * super_class,
const char * name, /* never
deallocate this */
struct hoc_ivar_list *ivars,
- struct objc_method_list *methods, /* never deallocate this */
- struct objc_method_list *class_methods); /* never deallocate this */
+ struct hoc_method_list *methods,
+ struct hoc_method_list *class_methods);
Modified: branches/objc2/hoc/HOC_cbits/NewClass.m
==============================================================================
--- branches/objc2/hoc/HOC_cbits/NewClass.m (original)
+++ branches/objc2/hoc/HOC_cbits/NewClass.m Sat Dec 6 04:04:52 2008
@@ -3,6 +3,7 @@
#include <assert.h>
#include "Class.h"
#include "Ivars.h"
+#include "Methods.h"
#include "NewClass.h"
#ifdef GNUSTEP
@@ -27,8 +28,8 @@
void newClass(struct objc_class * super_class,
const char * name,
struct hoc_ivar_list *ivars,
- struct objc_method_list *methods,
- struct objc_method_list *class_methods)
+ struct hoc_method_list *methods,
+ struct hoc_method_list *class_methods)
{
struct objc_class * meta_class;
struct objc_class * new_class;
@@ -81,8 +82,8 @@
__objc_resolve_class_links();
}
- class_add_method_list(new_class, methods);
- class_add_method_list(meta_class, class_methods);
+ class_add_method_list(new_class, convertMethodList(methods));
+ class_add_method_list(meta_class, convertMethodList(class_methods));
#else
new_class->methodLists = calloc( 1, sizeof(struct objc_method_list *) );
meta_class->methodLists = calloc( 1, sizeof(struct objc_method_list *) );
@@ -95,8 +96,8 @@
objc_addClass( new_class );
- class_addMethods(new_class, methods);
- class_addMethods(meta_class, class_methods);
+ class_addMethods(new_class, convertMethodList(methods));
+ class_addMethods(meta_class, convertMethodList(class_methods));
#endif
}
|