view dwt/internal/objc/runtime.d @ 36:db5a898b2119

Fixed a lot of compile errors
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Tue, 07 Oct 2008 12:56:18 +0200
parents fba856099f87
children 642f460a0908
line wrap: on
line source

/**
 * Copyright: Copyright (c) 2008 Jacob Carlborg. All rights reserved.
 * Authors: Jacob Carlborg
 * Version: Initial created: 2008
 * License: $(LINK2 http://opensource.org/licenses/bsd-license.php, BSD Style)
 * 
 */
module dwt.internal.objc.runtime;

import tango.stdc.stringz : fromStringz, toStringz;

import dwt.dwthelper.utils : String;
import dwt.internal.cocoa.NSPoint;
import dwt.internal.cocoa.NSRange;
import dwt.internal.cocoa.NSSize;
import dwt.internal.cocoa.NSRect;
static import dwt.internal.objc.bindings;



alias objc_ivar* Ivar;
alias objc_method* Method;
alias objc_object Protocol;

alias char* SEL;
alias objc_class* Class;
alias objc_object* id;

alias extern (C) id function(id, SEL, ...) IMP;

struct objc_object
{
    Class isa;
}

struct objc_super
{
    id receiver;
    Class clazz;
    
    // for dwt compatibility
    alias clazz cls;
}

struct objc_class
{
    Class isa;
    Class super_class;
    const char* name;
    int versionn;
    int info;
    int instance_size;
    objc_ivar_list* ivars;
    objc_method_list** methodLists;
    objc_cache* cache;
    objc_protocol_list* protocols;
}

struct objc_ivar
{
    char* ivar_name;
    char* ivar_type;
    int ivar_offset;

    version (X86_64)
        int space;
}

struct objc_ivar_list
{
    int ivar_count;

    version (X86_64)
        int space;

    /* variable length structure */
    objc_ivar ivar_list[1];
}

struct objc_method
{
    SEL method_name;
    char* method_types;
    IMP method_imp;
}

struct objc_method_list
{
    objc_method_list* obsolete;

    int method_count;

    version (X86_64)
        int space;

    /* variable length structure */
    objc_method method_list[1];
}

struct objc_cache
{
    uint mask /* total = mask + 1 */;
    uint occupied;
    Method buckets[1];
}

struct objc_protocol_list
{
    objc_protocol_list* next;
    long count;
    Protocol* list[1];
}



alias dwt.internal.objc.bindings.objc_registerClassPair objc_registerClassPair;

bool class_addIvar (Class cls, String name, size_t size, byte alignment, String types)
{
    return dwt.internal.objc.bindings.class_addIvar(cls, name.ptr, size, alignment, types.ptr);
}

bool class_addMethod (Class cls, String name, IMP imp, String types)
{
    return dwt.internal.objc.bindings.class_addMethod(cls, name.ptr, imp, types.ptr);
}

Class objc_allocateClassPair (Class superclass, String name, size_t extraBytes)
{
    return dwt.internal.objc.bindings.objc_allocateClassPair(superclass, name.ptr, extraBytes);
}

id objc_getClass (String name)
{
    return dwt.internal.objc.bindings.objc_getClass(name.ptr);
}

id objc_lookUpClass (String name)
{
    return dwt.internal.objc.bindings.objc_lookUpClass(name.ptr);
}

String object_getClassName (id obj)
{
    return fromStringz(dwt.internal.objc.bindings.object_getClassName(obj));
}

Ivar object_getInstanceVariable (id obj, String name, void** outValue)
{
    return dwt.internal.objc.bindings.object_getInstanceVariable(obj, name.ptr, outValue);
}

Ivar object_setInstanceVariable (id obj, String name, void* value)
{
    return dwt.internal.objc.bindings.object_setInstanceVariable(obj, name.ptr, value);
}

String sel_registerName (String str)
{
    return fromStringz(dwt.internal.objc.bindings.sel_registerName(str.ptr));
}

id objc_msgSend (ARGS...)(id theReceiver, String theSelector, ARGS args)
{
    return dwt.internal.objc.bindings.objc_msgSend(theReceiver, theSelector.ptr, args);
}

/// dummy function for now
int objc_msgSend_struct (T, ARGS...)(T* result, id theReceiver, String theSelector, ARGS args)
{
    //result = *cast(T) dwt.internal.objc.bindings.objc_msgSend(theReceiver, theSelector.ptr, args);
    return 0;
}

void objc_msgSend_stret (T, ARGS...)(T* stretAddr, id theReceiver, String theSelector, ARGS args)
{
    dwt.internal.objc.bindings.objc_msgSend_stret(stretAddr, theReceiver, theSelector.ptr, args);
}

id objc_msgSendSuper (ARGS...)(objc_super* superr, String op, ARGS args)
{
    return dwt.internal.objc.bindings.objc_msgSendSuper(superr, op.ptr, args);
}

version (X86)
{
    double objc_msgSend_fpret(ARGS...)(id self, String op, ARGS args)
    {
        return dwt.internal.objc.bindings.objc_msgSend_fpret(self, op.ptr, args);
    }
}


// os_custom
extern (C):
alias void function (id, SEL, NSRect) funcPtr;
static IMP drawRect_1CALLBACKK;


private void drawRect(id obj, SEL sel, NSRect rect)
{
    return cast(funcPtr) drawRect_1CALLBACKK(obj, sel, &rect);
}

funcPtr drawRect_1CALLBACK (IMP func)
{
    drawRect_1CALLBACKK = func;
    return &drawRect;
}