Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 0.59
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 368 lines (343 sloc) 8.275 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
#define PERL_POLLUTE

#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif

/* Why isn't in_eval defined with PERL_POLLUTE? */
#if defined(na) && !defined(in_eval)
#define in_eval PL_in_eval
#endif

/* First i such that ST(i) is a func arg */
#define DYNALIB_ARGSTART 3

#ifndef DYNALIB_NUM_CALLBACKS
#define DYNALIB_NUM_CALLBACKS 0
#endif

#ifdef DYNALIB_USE_cdecl
#define DYNALIB_DECL "cdecl"
#include "cdecl.c"
#endif
#ifdef DYNALIB_USE_sparc
#define DYNALIB_DECL "sparc"
#include "sparc.c"
#endif
#ifdef DYNALIB_USE_alpha
#define DYNALIB_DECL "alpha"
#include "alpha.c"
#endif
#ifdef DYNALIB_USE_hack30
#define DYNALIB_DECL "hack30"
#include "hack30.c"
#endif

typedef long (*cb_callback) _((void * a, ...));
typedef struct {
    SV *coderef;
    char *ret_type;
    char *arg_type;
    cb_callback func;
} cb_entry;

static long cb_call_sub _((int index, void * first, va_list ap));

#include "cbfunc.c"

static AV *cb_av_config;

static SV *
cb_init(arr_ref)
    SV *arr_ref;
{
    SV *elts[DYNALIB_NUM_CALLBACKS];
    int i;
    cb_entry entry;

    entry.coderef = NULL;
    entry.arg_type = "";
    entry.ret_type = "";
    for (i = 0; i < DYNALIB_NUM_CALLBACKS; i++) {
entry.func = cb_arr[i];
elts[i] = newSVpv((char *) &entry, sizeof entry);
    }
    cb_av_config = av_make(DYNALIB_NUM_CALLBACKS, elts);
    return (SV *) cb_av_config;
}

#if DYNALIB_NUM_CALLBACKS
/*
* With apologies to pp.c
*/
static long
cb_call_sub(index, first, ap)
    int index;
    void * first;
    va_list ap;
{
    dSP;
    I32 nret;
    int i;
    long result;
    STRLEN old_err_len, new_err_len;
    char *arg_type;
    cb_entry *config;
    SV *sv;
#ifdef HAS_QUAD
    Quad_t aquad;
    unsigned Quad_t auquad;
#endif
    static char *first_msg = "Can't use '%c' as first argument type in callback";

    config = (cb_entry *) SvPV(*av_fetch(cb_av_config, index, 0), na);
    ENTER;
    SAVETMPS;
    PUSHMARK(sp);
    arg_type = config->arg_type;
    if (*arg_type != '\0') {
switch (*arg_type) {
case 'i' :
XPUSHs(sv_2mortal(newSViv((IV) (int) first)));
break;
case 'l' :
XPUSHs(sv_2mortal(newSViv((IV) (I32) first)));
break;
case 's' :
/* the cast to (long) is just to avoid compiler warnings */
XPUSHs(sv_2mortal(newSViv((IV) (I16) (long) first)));
break;
case 'c' :
XPUSHs(sv_2mortal(newSViv((IV) (char) (long) first)));
break;
case 'I' :
sv = newSV(0);
sv_setuv(sv, PTR2UV(first));
XPUSHs(sv_2mortal(sv));
break;
case 'L' :
sv = newSV(0);
sv_setuv(sv, PTR2UV(first));
XPUSHs(sv_2mortal(sv));
break;
case 'S' :
XPUSHs(sv_2mortal(newSViv((IV) (U16) (long) first)));
break;
case 'C' :
XPUSHs(sv_2mortal(newSViv((IV) (unsigned char) (long) first)));
break;
#ifdef HAS_QUAD
case 'q' :
if (sizeof (Quad_t) <= sizeof first)
XPUSHs(sv_2mortal(newSViv(PTR2IV((Quad_t) first))));
else
croak(first_msg, *arg_type);
break;
case 'Q' :
if (sizeof (unsigned Quad_t) <= sizeof first) {
sv = newSV(0);
sv_setuv(sv, PTR2UV((unsigned Quad_t) first));
XPUSHs(sv_2mortal(sv));
}
else
croak(first_msg, *arg_type);
break;
#endif
case 'P' :
++ arg_type;
XPUSHs(sv_2mortal(newSVpv((char *) first,
(int) strtol(arg_type, &arg_type, 10))));
-- arg_type;
break;
case 'p' :
XPUSHs(sv_2mortal(newSVpv((char *) first, 0)));
break;
default :
croak(first_msg, *arg_type);
}
++ arg_type;
while (*arg_type != '\0') {
switch (*arg_type) {
case 'i' :
XPUSHs(sv_2mortal(newSViv((IV) va_arg(ap, int))));
break;
case 'l' :
XPUSHs(sv_2mortal(newSViv((IV) va_arg(ap, I32))));
break;
case 's' :
XPUSHs(sv_2mortal(newSViv((IV) va_arg(ap, int))));
break;
case 'c' :
XPUSHs(sv_2mortal(newSViv((IV) va_arg(ap, int))));
break;
case 'I' :
sv = newSV(0);
sv_setuv(sv, va_arg(ap, UV));
XPUSHs(sv_2mortal(sv));
break;
case 'L' :
sv = newSV(0);
sv_setuv(sv, va_arg(ap, U32));
XPUSHs(sv_2mortal(sv));
break;
case 'S' :
XPUSHs(sv_2mortal(newSViv((IV) va_arg(ap, int))));
break;
case 'C' :
XPUSHs(sv_2mortal(newSViv((IV) va_arg(ap, int))));
break;
#ifdef HAS_QUAD
case 'q' :
aquad = va_arg(ap, Quad_t);
sv = newSV(0);
if (aquad >= IV_MIN && aquad <= IV_MAX)
sv_setiv(sv, (IV)aquad);
else
sv_setnv(sv, (double)aquad);
XPUSHs(sv_2mortal(sv));
break;
case 'Q' :
auquad = va_arg(ap, unsigned Quad_t);
sv = newSV(0);
if (aquad <= UV_MAX)
sv_setuv(sv, (UV)auquad);
else
sv_setnv(sv, (double)auquad);
XPUSHs(sv_2mortal(sv));
break;
#endif
case 'f' :
XPUSHs(sv_2mortal(newSVnv((double) va_arg(ap, double))));
break;
case 'd' :
XPUSHs(sv_2mortal(newSVnv(va_arg(ap, double))));
break;
case 'P' :
++ arg_type;
XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *),
(int) strtol(arg_type, &arg_type, 10))));
-- arg_type;
break;
case 'p' :
XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
break;
default :
croak("Can't use '%c' as argument type in callback", *arg_type);
}
++ arg_type;
}
    }
    PUTBACK;

    if (in_eval) {
/*
* XXX The whole issue of G_KEEPERR and `eval's is very confusing
* to me. For example, we should be able to tell whether or not we
* are in cleanup code that follows a die. We can't tell just by
* looking at $@, since it may be left over from a previous eval.
*
* If we're not in cleanup, we should clear $@/errgv before we call the
* sub. The way this code works now, any error string left over from a
* completed eval is wrongly included in our croak message.
*
* It can also produce weirdness when used with Carp::confess.
*/
SvPV(GvSV(errgv), old_err_len);
nret = perl_call_sv(config->coderef, G_SCALAR | G_EVAL | G_KEEPERR);
SPAGAIN;
SvPV(GvSV(errgv), new_err_len);
if (new_err_len > old_err_len) {
char *msg = SvPV(GvSV(errgv),na);
static char prefix[] = "\t(in cleanup) "; /* from pp_ctl.c */

if (old_err_len == 0 && strnEQ(msg, prefix, (sizeof prefix) - 1)) {
msg += (sizeof prefix) - 1;
croak("In callback: %s", msg);
}
else {
croak("%s", msg);
}
}
    }
    else {
nret = perl_call_sv(config->coderef, G_SCALAR);
SPAGAIN;
    }
    if (nret != 1) {
/* don't know if this can ever happen... */
croak("Call to callback failed\n");
    }
    switch (*(config->ret_type)) {
    case '\0' :
    case 'i' :
result = (long) (int) POPi;
break;
    case 'I' :
result = (long) (unsigned int) POPu;
break;
#if defined(HAS_QUAD) && LONGSIZE >= 8
    case 'q' :
result = (long) (Quad_t) POPi;
break;
#endif
/*
* Returning a pointer is impossible to do safely, it seems.
* case 'p' :
* result = (long) POPp;
* break;
*/
    default :
croak("Can't use '%s' as return type in callback", config->ret_type);
    }
    PUTBACK;
    FREETMPS;
    LEAVE;
    return result;
}
#endif /* DYNALIB_NUM_CALLBACKS != 0 */

static char *
constant(name)
    char *name;
{
    errno = 0;
    switch (*name) {
    case 'D' :
if (strEQ(name, "DYNALIB_DEFAULT_CONV")) {
return DYNALIB_DEFAULT_CONV;
}
break;
    case 'P' :
if (strEQ(name, "PTR_TYPE")) {
if (sizeof (void *) == sizeof (int))
/* XXX Are pointers signed? */
return "i";
#ifdef HAS_QUAD
if (sizeof (void *) == sizeof (Quad_t))
return "q";
#endif
if (sizeof (void *) == sizeof (I32))
return "l";
if (sizeof (void *) == sizeof (I16))
return "s";
croak("Can't find an integer type that's the same size as pointers");
}
break;
    }
    errno = EINVAL;
    return 0;
}


MODULE = C::DynaLib PACKAGE = C::DynaLib

char *
constant(name)
char * name

INCLUDE: conv.xsi

void
Poke(dest, data)
# Well, you could do the same thing by loading memcpy().
void * dest
SV * data
CODE:
{
STRLEN len;
char *source;
if (SvPOK(data)) {
source = SvPV(data, len);
Copy(source, dest, len, char);
}
}


BOOT:
/* $C::DynaLib::decl = cdecl or hack30 or ...; */
sv_setsv(get_sv("C::DynaLib::decl", 1), newSVpv(DYNALIB_DECL, 0));

/* Setup the callback config array. */
sv_setsv(SvRV(ST(2)), newRV((SV*) sv_2mortal(cb_init(ST(2)))));
Something went wrong with that request. Please try again.