Slides

unix > swig -python example.i unix > gcc -c -fpic ... unix > python. Python 2.0 (#6, Feb 21 2001, 13:29:45) ...... "contains(/foo/bar[1], 'test 1')". /* Register a new ...
764KB Sizes 3 Downloads 333 Views
binding C libraries

A journey

why?

how?

Perl 5 XS

#include #include "EXTERN.h" "EXTERN.h" #include #include "perl.h" "perl.h" #include #include "XSUB.h" "XSUB.h" #include #include "ppport.h" "ppport.h" #include #include <SDL.h> <SDL.h> MODULE MODULE == SDL::Rect SDL::Rect

PACKAGE PACKAGE == SDL::Rect SDL::Rect PREFIX PREFIX == rect_ rect_

SDL_Rect SDL_Rect ** rect_new rect_new (CLASS, (CLASS, x, x, y, y, w, w, h) h) char char** CLASS CLASS Sint16 Sint16 x, x, yy Uint16 Uint16 w, w, hh CODE: CODE: RETVAL RETVAL == (SDL_Rect (SDL_Rect *)safemalloc(sizeof(SDL_Rect)); *)safemalloc(sizeof(SDL_Rect)); RETVAL->x RETVAL->x == x; x; RETVAL->y RETVAL->y == y; y; RETVAL->w RETVAL->w == w; w; RETVAL->h RETVAL->h == h; h; OUTPUT: OUTPUT: RETVAL RETVAL

Sint16 Sint16 rect_x rect_x (( rect, rect, ... ... )) SDL_Rect SDL_Rect *rect *rect CODE: CODE: if if (items (items >> 11)) rect->x rect->x == SvIV(ST( SvIV(ST(11)); )); RETVAL RETVAL == rect->x; rect->x; OUTPUT: OUTPUT: RETVAL RETVAL Sint16 Sint16 rect_y rect_y (( rect, rect, ... ... )) SDL_Rect SDL_Rect *rect *rect CODE: CODE: if if (items (items >> 11)) rect->y rect->y == SvIV(ST( SvIV(ST(11)); )); RETVAL RETVAL == rect->y; rect->y; OUTPUT: OUTPUT: RETVAL RETVAL

SV SV ** createDocument( createDocument( CLASS, CLASS, version= version="1.0" "1.0",, encoding=NULL encoding=NULL )) char char ** version version char char ** encoding encoding ALIAS: ALIAS: XML::LibXML::Document::new XML::LibXML::Document::new == 11 PREINIT: PREINIT: xmlDocPtr xmlDocPtr doc=NULL; doc=NULL; CODE: CODE: PERL_UNUSED_VAR(ix); PERL_UNUSED_VAR(ix); doc doc == xmlNewDoc((const xmlNewDoc((const xmlChar*)version); xmlChar*)version); if if (encoding (encoding && && *encoding *encoding != != 00)) {{ doc->encoding doc->encoding == (xmlChar (xmlChar *)xmlStrdup((xmlChar *)xmlStrdup((xmlChar *)encoding); *)encoding); }} RETVAL RETVAL == PmmNodeToSv(INT2PTR(xmlNodePtr,doc),NULL); PmmNodeToSv(INT2PTR(xmlNodePtr,doc),NULL); OUTPUT: OUTPUT: RETVAL RETVAL

void void lmx_add( lmx_add( manager, manager, bag bag )) SDLx_LayerManager SDLx_LayerManager *manager *manager SV* SV* bag bag CODE: CODE: if if(( sv_isobject(bag) sv_isobject(bag) && && (SvTYPE(SvRV(bag)) (SvTYPE(SvRV(bag)) == == SVt_PVMG) SVt_PVMG) )) {{ SDLx_Layer SDLx_Layer *layer *layer == (SDLx_Layer (SDLx_Layer *)bag2obj(bag); *)bag2obj(bag); layer->index == av_len( layer->index av_len( manager->layers manager->layers )) ++ 11;; layer->manager == manager; layer->manager manager; layer->touched == 11;; layer->touched av_push( av_push( manager->layers, manager->layers, bag); bag); SvREFCNT_inc(bag); SvREFCNT_inc(bag); }}

Perl 5 XS C support

yes

C++ support

no

Compiler needed

yes

pro

- mature - no runtime penalty

contra

- very good C knowledge and a C compiler needed

?

?

?

Perl 5 XSpp

%{ %{ #include #include %} %} %module{Box2D}; %module{Box2D}; %name{Box2D::b2Shape} %name{Box2D::b2Shape} class class b2Shape b2Shape {{ %{ %{ void void b2Shape::ComputeAABB( b2Shape::ComputeAABB( aabb, aabb, xf xf )) b2AABB* b2AABB* aabb aabb b2Transform* b2Transform* xf xf CODE: CODE: THIS->ComputeAABB( THIS->ComputeAABB( aabb, aabb, *xf *xf );); %} %} };};

int16 int16 uint16 uint16 int32 int32 uint32 uint32 float32 float32 b2Vec2 b2Vec2 ** b2Mat22 b2Mat22 ** b2World b2World ** b2Body b2Body ** b2BodyDef b2BodyDef ** b2Shape b2Shape ** b2PolygonShape b2PolygonShape ** b2CircleShape b2CircleShape ** b2Filter b2Filter ** b2FixtureDef b2FixtureDef ** b2Fixture b2Fixture ** b2Transform b2Transform ** b2Joint b2Joint ** b2JointDef b2JointDef ** b2DistanceJoint b2DistanceJoint ** b2DistanceJointDef b2DistanceJointDef **

T_IV T_IV T_IV T_IV T_IV T_IV T_IV T_IV T_NV T_NV O_OBJECT O_OBJECT O_OBJECT O_OBJECT O_OBJECT O_OBJECT O_OBJECT O_OBJECT O_OBJ