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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
#include "smokeperl.h"
class SmokePerlQt : public SmokePerl {
public:
SmokePerlQt();
virtual ~SmokePerlQt();
void registerSmoke(const char *name, Smoke *smoke);
Smoke *getSmoke(const char *name);
void registerHandlers(TypeHandler *h);
SmokeObject newObject(void *p, const SmokeClass &c);
SmokeObject wrapObject(void *p, const SmokeClass &c);
SmokeObject getObject(void *p);
SmokeObject getObject(SV *sv);
private:
HV *_registered_smoke;
HV *_registered_handlers;
HV *_remembered_pointers;
void rememberPointer(SmokeObject &o, const SmokeClass &c, bool remember, void *lastptr = 0);
void rememberPointer(SmokeObject &o);
void forgetPointer(SmokeObject &o);
SmokeObject createObject(void *p, const SmokeClass &c);
const char *getSmokeName(Smoke *smoke) {
static const char none[] = "";
HE *he;
hv_iterinit(_registered_smoke);
while(he = hv_iternext(_registered_smoke)) {
SV *sv = hv_iterval(_registered_smoke, he);
if((Smoke*)SvIV(sv) == smoke) {
I32 toss;
return hv_iterkey(he, &toss);
}
}
return none;
}
HV *package(const SmokeClass &c) {
// for now, we cheat on the class names by assuming they're all Qt::
if(!strcmp(c.className(), "Qt"))
return gv_stashpv(c.className(), TRUE);
SV *name = newSVpv("Qt::", 0);
sv_catpv(name, c.className() + 1);
HV *stash = gv_stashpv(SvPV_nolen(name), TRUE);
SvREFCNT_dec(name);
return stash;
}
};
Marshall::HandlerFn getMarshallFn(const SmokeType &type);
class VirtualMethodReturnValue : public Marshall {
Smoke *_smoke;
Smoke::Index _method;
Smoke::Stack _stack;
SmokeType _st;
SV *_retval;
public:
const Smoke::Method &method() { return _smoke->methods[_method]; }
SmokeType type() { return _st; }
Marshall::Action action() { return Marshall::FromSV; }
Smoke::StackItem &item() { return _stack[0]; }
SV *var() { return _retval; }
void unsupported() {
croak("Cannot handle '%s' as return-type of virtual method %s::%s",
type().name(),
_smoke->className(method().classId),
_smoke->methodNames[method().name]);
}
Smoke *smoke() { return _smoke; }
void next() {}
bool cleanup() { return false; }
VirtualMethodReturnValue(Smoke *smoke, Smoke::Index meth, Smoke::Stack stack, SV *retval) :
_smoke(smoke), _method(meth), _stack(stack), _retval(retval) {
_st.set(_smoke, method().ret);
Marshall::HandlerFn fn = getMarshallFn(type());
(*fn)(this);
}
};
extern SV *sv_this;
extern void *_current_object;
extern Smoke::Index _current_object_class;
extern int object_count;
extern bool temporary_virtual_function_success;
extern struct mgvtbl vtbl_smoke;
class VirtualMethodCall : public Marshall {
Smoke *_smoke;
Smoke::Index _method;
Smoke::Stack _stack;
GV *_gv;
int _cur;
Smoke::Index *_args;
SV **_sp;
bool _called;
SV *_savethis;
public:
SmokeType type() { return SmokeType(_smoke, _args[_cur]); }
Marshall::Action action() { return Marshall::ToSV; }
Smoke::StackItem &item() { return _stack[_cur + 1]; }
SV *var() { return _sp[_cur]; }
const Smoke::Method &method() { return _smoke->methods[_method]; }
void unsupported() {
croak("Cannot handle '%s' as argument of virtual method %s::%s",
type().name(),
_smoke->className(method().classId),
_smoke->methodNames[method().name]);
}
Smoke *smoke() { return _smoke; }
void callMethod() {
dSP;
if(_called) return;
_called = true;
SP = _sp + method().numArgs - 1;
PUTBACK;
int count = call_sv((SV*)_gv, G_SCALAR);
SPAGAIN;
VirtualMethodReturnValue r(_smoke, _method, _stack, POPs);
PUTBACK;
FREETMPS;
LEAVE;
}
void next() {
int oldcur = _cur;
_cur++;
while(!_called && _cur < method().numArgs) {
Marshall::HandlerFn fn = getMarshallFn(type());
_sp[_cur] = sv_newmortal();
(*fn)(this);
_cur++;
}
callMethod();
_cur = oldcur;
}
bool cleanup() { return false; } // is this right?
VirtualMethodCall(Smoke *smoke, Smoke::Index meth, Smoke::Stack stack, SV *obj, GV *gv) :
_smoke(smoke), _method(meth), _stack(stack), _gv(gv), _cur(-1), _sp(0), _called(false) {
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, method().numArgs);
_savethis = sv_this;
sv_this = newSVsv(obj);
_sp = SP + 1;
for(int i = 0; i < method().numArgs; i++)
_sp[i] = sv_newmortal();
_args = _smoke->argumentList + method().args;
}
~VirtualMethodCall() {
SvREFCNT_dec(sv_this);
sv_this = _savethis;
}
};
class MethodReturnValue : public Marshall {
Smoke *_smoke;
Smoke::Index _method;
SV *_retval;
Smoke::Stack _stack;
public:
MethodReturnValue(Smoke *smoke, Smoke::Index method, Smoke::Stack stack, SV *retval) :
_smoke(smoke), _method(method), _retval(retval), _stack(stack) {
Marshall::HandlerFn fn = getMarshallFn(type());
(*fn)(this);
}
const Smoke::Method &method() { return _smoke->methods[_method]; }
SmokeType type() { return SmokeType(_smoke, method().ret); }
Marshall::Action action() { return Marshall::ToSV; }
Smoke::StackItem &item() { return _stack[0]; }
SV *var() { return _retval; }
void unsupported() {
croak("Cannot handle '%s' as return-type of %s::%s",
type().name(),
_smoke->className(method().classId),
_smoke->methodNames[method().name]);
}
Smoke *smoke() { return _smoke; }
void next() {}
bool cleanup() { return false; }
};
class MethodCall : public Marshall {
int _cur;
Smoke *_smoke;
Smoke::Stack _stack;
Smoke::Index _method;
Smoke::Index *_args;
SV **_sp;
int _items;
SV *_retval;
bool _called;
public:
MethodCall(Smoke *smoke, Smoke::Index method, SV **sp, int items) :
_smoke(smoke), _method(method), _sp(sp), _items(items), _cur(-1), _called(false) {
_args = _smoke->argumentList + _smoke->methods[_method].args;
_items = _smoke->methods[_method].numArgs;
_stack = new Smoke::StackItem[items + 1];
_retval = newSV(0);
}
~MethodCall() {
delete[] _stack;
SvREFCNT_dec(_retval);
}
SmokeType type() { return SmokeType(_smoke, _args[_cur]); }
Marshall::Action action() { return Marshall::FromSV; }
Smoke::StackItem &item() { return _stack[_cur + 1]; }
SV *var() {
if(_cur < 0) return _retval;
SvGETMAGIC(*(_sp + _cur));
return *(_sp + _cur);
}
inline const Smoke::Method &method() { return _smoke->methods[_method]; }
void unsupported() {
croak("Cannot handle '%s' as argument to %s::%s",
type().name(),
_smoke->className(method().classId),
_smoke->methodNames[method().name]);
}
Smoke *smoke() { return _smoke; }
inline void callMethod() {
if(_called) return;
_called = true;
Smoke::ClassFn fn = _smoke->classes[method().classId].classFn;
void *ptr = _smoke->cast(
_current_object,
_current_object_class,
method().classId
);
_items = -1;
(*fn)(method().method, ptr, _stack);
MethodReturnValue r(_smoke, _method, _stack, _retval);
}
void next() {
int oldcur = _cur;
_cur++;
while(!_called && _cur < _items) {
Marshall::HandlerFn fn = getMarshallFn(type());
(*fn)(this);
_cur++;
}
callMethod();
_cur = oldcur;
}
bool cleanup() { return true; }
};
class SmokeBindingQt : public SmokeBinding {
SmokePerlQt *_smokeperl;
public:
SmokeBindingQt(Smoke *s, SmokePerlQt *smokeperl) :
SmokeBinding(s), _smokeperl(smokeperl) {}
void deleted(Smoke::Index classId, void *ptr) {
if(do_debug) printf("%p->~%s()\n", ptr, smoke->className(classId));
object_count--;
if(do_debug) printf("Remaining objects: %d\n", object_count);
SV *obj = getPointerObject(ptr);
smokeperl_object *o = sv_obj_info(obj);
if(!o || !o->ptr) {
return;
}
unmapPointer(o, o->classId, 0);
o->ptr = 0;
}
bool callMethod(Smoke::Index method, void *ptr, Smoke::Stack args, bool isAbstract) {
SV *obj = getPointerObject(ptr);
smokeperl_object *o = sv_obj_info(obj);
if(do_debug) printf("virtual %p->%s::%s() called\n", ptr,
smoke->classes[smoke->methods[method].classId].className,
smoke->methodNames[smoke->methods[method].name]
);
if(!o) {
if(!PL_dirty) // if not in global destruction
warn("Cannot find object for virtual method");
return false;
}
HV *stash = SvSTASH(SvRV(obj));
if(*HvNAME(stash) == ' ')
stash = gv_stashpv(HvNAME(stash) + 1, TRUE);
const char *methodName = smoke->methodNames[smoke->methods[method].name];
GV *gv = gv_fetchmethod_autoload(stash, methodName, 0);
if(!gv) return false;
VirtualMethodCall c(smoke, method, args, obj, gv);
// exception variable, just temporary
temporary_virtual_function_success = true;
c.next();
bool ret = temporary_virtual_function_success;
temporary_virtual_function_success = true;
return ret;
}
char *className(Smoke::Index classId) {
const char *className = smoke->className(classId);
char *buf = new char[strlen(className) + 6];
strcpy(buf, " Qt::");
strcat(buf, className + 1);
return buf;
}
};
SmokePerlQt::SmokePerlQt() {
_registered_smoke = newHV();
_registered_handlers = newHV();
_remembered_pointers = newHV();
}
SmokePerlQt::~SmokePerlQt() {
SvREFCNT_dec((SV*)_registered_smoke);
SvREFCNT_dec((SV*)_registered_handlers);
SvREFCNT_dec((SV*)_remembered_pointers);
}
void SmokePerlQt::registerSmoke(const char *name, Smoke *smoke) {
hv_store(_registered_smoke, name, strlen(name), newSViv((IV)smoke), 0);
// This will also need to handle the per-class initialization
smoke->binding = new SmokeBindingQt(smoke, this);
}
Smoke *SmokePerlQt::getSmoke(const char *name) {
SV **svp = hv_fetch(_registered_smoke, name, strlen(name), 0);
if(svp && SvOK(*svp))
return (Smoke*)SvIV(*svp);
return 0;
}
void SmokePerlQt::registerHandlers(TypeHandler *h) {
while(h->name) {
hv_store(_registered_handlers, h->name, strlen(h->name), newSViv((IV)h->fn), 0);
h++;
}
}
SmokeObject SmokePerlQt::createObject(void *p, const SmokeClass &c) {
HV *hv = newHV();
SV *obj = newRV_noinc((SV*)hv);
Smoke_MAGIC m(p, c);
sv_magic((SV*)hv, (SV*)newAV(), '~', (char*)&m, sizeof(m));
MAGIC *mg = mg_find((SV*)hv, '~');
mg->mg_virtual = &vtbl_smoke;
sv_bless(obj, package(c));
SmokeObject o(obj, (Smoke_MAGIC*)mg->mg_ptr);
SvREFCNT_dec(obj);
if(c.hasVirtual())
rememberPointer(o);
return o;
}
SmokeObject SmokePerlQt::newObject(void *p, const SmokeClass &c) {
SmokeObject o = createObject(p, c);
if(c.isVirtual())
rememberPointer(o);
o.setAllocated(true);
return o;
}
SmokeObject SmokePerlQt::wrapObject(void *p, const SmokeClass &c) {
SmokeObject o = createObject(p, c);
return o;
}
void SmokePerlQt::rememberPointer(SmokeObject &o, const SmokeClass &c, bool remember, void *lastptr) {
void *ptr = o.cast(c);
if(ptr != lastptr) {
SV *keysv = newSViv((IV)o.ptr());
STRLEN klen;
char *key = SvPV(keysv, klen);
if(remember)
hv_store(_remembered_pointers, key, klen,
sv_rvweaken(newSVsv(o.var())), 0);
else
hv_delete(_remembered_pointers, key, klen, G_DISCARD);
SvREFCNT_dec(keysv);
}
for(Smoke::Index *i = c.smoke()->inheritanceList + c.c().parents;
*i;
i++)
rememberPointer(o, SmokeClass(c.smoke(), *i), remember, ptr);
}
void SmokePerlQt::rememberPointer(SmokeObject &o) {
rememberPointer(o, o.c(), true);
}
void SmokePerlQt::forgetPointer(SmokeObject &o) {
rememberPointer(o, o.c(), false);
}
SmokeObject SmokePerlQt::getObject(SV *sv) {
MAGIC *mg = mg_find(SvRV(sv), '~');
Smoke_MAGIC *m = (Smoke_MAGIC*)mg->mg_ptr;
return SmokeObject(sv, m);
}
SmokeObject SmokePerlQt::getObject(void *p) {
SV *keysv = newSViv((IV)p);
STRLEN klen;
char *key = SvPV(keysv, klen);
SV **svp = hv_fetch(_remembered_pointers, key, klen, 0);
if(svp && SvROK(*svp))
return getObject(sv_2mortal(newRV(SvRV(*svp)))); // paranoid copy of a weak ref
return SmokeObject(&PL_sv_undef, 0);
}
|