Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 279 lines (237 sloc) 6.251 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*
* MacRuby ObjC helpers.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012, The MacRuby Team. All rights reserved.
* Copyright (C) 2007-2011, Apple Inc. All rights reserved.
*/

#ifndef __OBJC_H_
#define __OBJC_H_

#if defined(__cplusplus)
extern "C" {
#endif

#include "bs.h"

static inline const char *
rb_get_bs_method_type(bs_element_method_t *bs_method, int arg)
{
    if (bs_method != NULL) {
if (arg == -1) {
if (bs_method->retval != NULL) {
return bs_method->retval->type;
}
}
else {
unsigned int i;
for (i = 0; i < bs_method->args_count; i++) {
if (bs_method->args[i].index == arg) {
return bs_method->args[i].type;
}
}
}
    }
    return NULL;
}

bool rb_objc_get_types(VALUE recv, Class klass, SEL sel, Method m,
bs_element_method_t *bs_method, char *buf, size_t buflen);

bool rb_objc_supports_forwarding(VALUE recv, SEL sel);

void rb_objc_define_kvo_setter(VALUE klass, ID mid);

static inline IMP
rb_objc_install_method(Class klass, SEL sel, IMP imp)
{
    Method method = class_getInstanceMethod(klass, sel);
    if (method == NULL) {
printf("method %s not found on class %p - aborting\n",
sel_getName(sel), klass);
abort();
    }
    IMP old = method_getImplementation(method);
    class_replaceMethod(klass, sel, imp, method_getTypeEncoding(method));
    return old;
}

static inline IMP
rb_objc_install_method2(Class klass, const char *selname, IMP imp)
{
    return rb_objc_install_method(klass, sel_registerName(selname), imp);
}

static inline bool
rb_objc_is_kind_of(id object, Class klass)
{
    Class cls;
    for (cls = *(Class *)object; cls != NULL; cls = class_getSuperclass(cls)) {
if (cls == klass) {
return true;
}
    }
    return false;
}

id rb_rb2oc_exception(VALUE exc);
VALUE rb_oc2rb_exception(id exc, bool *created);

size_t rb_objc_type_size(const char *type);

static inline int
SubtypeUntil(const char *type, char end)
{
    int level = 0;
    const char *head = type;

    while (*type)
    {
if (!*type || (!level && (*type == end)))
return (int)(type - head);

switch (*type)
{
case ']': case '}': case ')': case '>': level--; break;
case '[': case '{': case '(': case '<': level += 1; break;
}

type += 1;
    }

    rb_bug ("Object: SubtypeUntil: end of type encountered prematurely\n");
    return 0;
}

static inline const char *
SkipStackSize(const char *type)
{
    while ((*type >= '0') && (*type <= '9')) {
type += 1;
    }
    return type;
}

static inline const char *
SkipTypeModifiers(const char *type)
{
    while (true) {
switch (*type) {
case _C_CONST:
case 'O': /* bycopy */
case 'n': /* in */
case 'o': /* out */
case 'N': /* inout */
case 'V': /* oneway */
type++;
break;

default:
return type;
}
    }
}

static inline const char *
SkipFirstType(const char *type)
{
    while (1) {
        switch (*type++) {
case _C_CONST:
case _C_PTR:
            case 'O': /* bycopy */
            case 'n': /* in */
            case 'o': /* out */
            case 'N': /* inout */
            case 'V': /* oneway */
                break;

case _C_ID:
if (*type == _C_UNDEF) {
type++; /* Blocks */
}
return type;

                /* arrays */
            case _C_ARY_B:
                return type + SubtypeUntil (type, _C_ARY_E) + 1;

                /* structures */
            case _C_STRUCT_B:
                return type + SubtypeUntil (type, _C_STRUCT_E) + 1;

                /* unions */
            case _C_UNION_B:
                return type + SubtypeUntil (type, _C_UNION_E) + 1;

                /* lambdas */
            case _MR_C_LAMBDA_B:
                return type + SubtypeUntil (type, _MR_C_LAMBDA_E) + 1;

                /* basic types */
            default:
                return type;
        }
    }
}

static inline const char *
GetFirstType(const char *p, char *buf, size_t buflen)
{
    const char *p2 = SkipFirstType(p);
    const size_t len = p2 - p;
    assert(len < buflen);
    strncpy(buf, p, len);
    buf[len] = '\0';
    return SkipStackSize(p2);
}

static inline unsigned int
TypeArity(const char *type)
{
    unsigned int arity = 0;
    while (*type != '\0') {
type = SkipFirstType(type);
type = SkipStackSize(type);
arity++;
    }
    return arity;
}

// We do not use method_getNumberOfArguments since it's broken on
// SnowLeopard for signatures containing Block objects.
static inline unsigned int
rb_method_getNumberOfArguments(Method m)
{
    const unsigned int arity = TypeArity(method_getTypeEncoding(m));
    assert(arity >= 2);
    return arity - 1; // Skip return type.
}

id rb_objc_numeric2nsnumber(VALUE obj);
VALUE rb_objc_convert_immediate(id obj);

static inline id
rb_rval_to_ocid(VALUE obj)
{
    if (SPECIAL_CONST_P(obj)) {
        if (obj == Qtrue) {
            return (id)kCFBooleanTrue;
        }
        if (obj == Qfalse) {
            return (id)kCFBooleanFalse;
        }
        if (obj == Qnil) {
            return (id)kCFNull;
        }
if (IMMEDIATE_P(obj)) {
return rb_objc_numeric2nsnumber(obj);
}
    }
    return (id)obj;
}

static inline VALUE
rb_ocid_to_rval(id obj)
{
    if (obj == (id)kCFBooleanTrue) {
return Qtrue;
    }
    if (obj == (id)kCFBooleanFalse) {
return Qfalse;
    }
    if (obj == (id)kCFNull || obj == nil) {
return Qnil;
    }
    return rb_objc_convert_immediate(obj);
}

#define RB2OC(obj) (rb_rval_to_ocid((VALUE)obj))
#define OC2RB(obj) (rb_ocid_to_rval((id)obj))

void rb_objc_exception_raise(const char *name, const char *message);

bool rb_objc_ignored_sel(SEL sel);
bool rb_objc_isEqual(VALUE x, VALUE y);
void rb_objc_force_class_initialize(Class klass);
void rb_objc_fix_relocatable_load_path(void);
void rb_objc_load_loaded_frameworks_bridgesupport(void);
void rb_objc_install_NSObject_special_methods(Class k);

extern bool rb_objc_enable_ivar_set_kvo_notifications;

#if !defined(MACRUBY_STATIC)
void rb_vm_parse_bs_full_file(const char *path,
void (*add_stub_types_cb)(SEL, const char *, bool, void *),
void *ctx);
#endif

#define SINCE_EPOCH 978307200.0
#define CF_REFERENCE_DATE SINCE_EPOCH

#if defined(__cplusplus)
}
#endif

#endif /* __OBJC_H_ */
Something went wrong with that request. Please try again.