5分で始める xs - tsukuba.xs#1

30
5分で始めるXS Tsukuba.xs Beer Talks #1 xaicron 2010724日土曜日

Upload: yuji-shimada

Post on 05-Dec-2014

1.073 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 5分で始める XS - tsukuba.xs#1

5分で始めるXSTsukuba.xs Beer Talks #1

xaicron

2010年7月24日土曜日

Page 2: 5分で始める XS - tsukuba.xs#1

自己紹介

• @xaicron

• http://blog.livedoor.jp/xaicron/

• XS初心者

2010年7月24日土曜日

Page 3: 5分で始める XS - tsukuba.xs#1

とりあえず

2010年7月24日土曜日

Page 4: 5分で始める XS - tsukuba.xs#1

今日の発表は忘れましょう

2010年7月24日土曜日

Page 5: 5分で始める XS - tsukuba.xs#1

• 構文木を作る

• PL_Cehck Hack

• Perl インタープリターを起動する

• /* Polyglot */

• VMを実装する

2010年7月24日土曜日

Page 6: 5分で始める XS - tsukuba.xs#1

そういうのやらない

2010年7月24日土曜日

Page 7: 5分で始める XS - tsukuba.xs#1

全くXSやったことない人向けの話をします!

2010年7月24日土曜日

Page 8: 5分で始める XS - tsukuba.xs#1

簡単なCのラッパーから始めよう!

2010年7月24日土曜日

Page 9: 5分で始める XS - tsukuba.xs#1

下準備

• make, gcc などのC開発ツール

• Module::Install::XSUtil

2010年7月24日土曜日

Page 10: 5分で始める XS - tsukuba.xs#1

以下のコマンドでテンプレート作成$ h2xs -A -b 5.8.1 -n Foo::Bar$ cd Foo-Bar$ tree.|-- Bar.xs|-- Changes|-- MANIFEST|-- Makefile.PL|-- README|-- lib| `-- Foo| `-- Bar.pm|-- ppport.h`-- t `-- Foo-Bar.t

2010年7月24日土曜日

Page 11: 5分で始める XS - tsukuba.xs#1

とりあえずmake && make test

$ perl Makefile.PL$ make && make test

2010年7月24日土曜日

Page 12: 5分で始める XS - tsukuba.xs#1

うごいた!!

2010年7月24日土曜日

Page 13: 5分で始める XS - tsukuba.xs#1

もうちょっと便利にしてみる

2010年7月24日土曜日

Page 14: 5分で始める XS - tsukuba.xs#1

Makefile.PLuse inc::Module::Install;use Module::Install::XSUtil;

name 'Foo-Bar';all_from 'lib/Foo/Bar.pm';

use_ppport 3.19;cc_warnings;cc_src_paths qw(xs);

tests 't/*.t';test_requires 'Test::More';

auto_include;WriteAll;

2010年7月24日土曜日

Page 15: 5分で始める XS - tsukuba.xs#1

$ rm ppport.h$ mkdir xs$ mv Bar.xs xs$ tree.|-- Changes|-- MANIFEST|-- Makefile.PL|-- README|-- lib| `-- Foo| `-- Bar.pm|-- t| `-- Foo-Bar.t`-- xs `-- Bar.xs

2010年7月24日土曜日

Page 16: 5分で始める XS - tsukuba.xs#1

xs/Bar.xs#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"

#include "ppport.h"

MODULE = Foo::Bar PACKAGE = Foo::Bar

PROTOTYPES: DISABLE

intsum(int x, int y) CODE: RETVAL = x + y; OUTPUT: RETVAL

2010年7月24日土曜日

Page 17: 5分で始める XS - tsukuba.xs#1

実行してみる$ perl Makefile.PL$ make$ perl -Mblib -MFoo::Bar -le “print Foo::Bar::sum(1 ,2)”3

2010年7月24日土曜日

Page 18: 5分で始める XS - tsukuba.xs#1

うごいた!!

2010年7月24日土曜日

Page 19: 5分で始める XS - tsukuba.xs#1

複数のモジュールでXSを使いたい

2010年7月24日土曜日

Page 20: 5分で始める XS - tsukuba.xs#1

Foo::Bar / Foo::Baz で

xs を使う

2010年7月24日土曜日

Page 21: 5分で始める XS - tsukuba.xs#1

$ h2xs -A -b 5.8.1 -n Foo::XS

あとはさっきと一緒

2010年7月24日土曜日

Page 22: 5分で始める XS - tsukuba.xs#1

• xs/Bar.xs, xs/Baz.xs を同じように書く

• foo.h を書く

• xs/XS.xs を修正

• lib/Foo/Bar.pm, lib/Foo/Baz/pm を修正

2010年7月24日土曜日

Page 23: 5分で始める XS - tsukuba.xs#1

xs/Bar.xs#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"

#include "ppport.h"

MODULE = Foo::Bar PACKAGE = Foo::Bar

PROTOTYPES: DISABLE

intsum(int x, int y) CODE: RETVAL = x + y; OUTPUT: RETVAL

2010年7月24日土曜日

Page 24: 5分で始める XS - tsukuba.xs#1

xs/Baz.xs#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"

#include "ppport.h"

MODULE = Foo::Baz PACKAGE = Foo::Baz

PROTOTYPES: DISABLE

intdouble(int x) CODE: RETVAL = x * 2; OUTPUT: RETVAL

2010年7月24日土曜日

Page 25: 5分で始める XS - tsukuba.xs#1

foo.h

#define FOO_CALL_BOOT(name) STMT_START { \ EXTERN_C XS(CAT2(boot_, name)); \ PUSHMARK(SP); \ CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \ } STMT_END

2010年7月24日土曜日

Page 26: 5分で始める XS - tsukuba.xs#1

xs/XS.xs#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"

#include "ppport.h"#include "foo.h"

MODULE = Foo::XS

BOOT: FOO_CALL_BOOT(Foo__Bar); FOO_CALL_BOOT(Foo__Baz);

2010年7月24日土曜日

Page 27: 5分で始める XS - tsukuba.xs#1

Foo/Bar.pm Foo/Baz.pmpackage Foo::Bar; # or Bazuse strict;use warnings;use Foo::XS;

our $VERSION = ‘0.01’;

1;

2010年7月24日土曜日

Page 28: 5分で始める XS - tsukuba.xs#1

$ perl Makefile.pl$ make test$ perl -Mblib -MFoo::Bar -le “print Foo::Bar::sum(1,2)”3$ perl -Mblib -MFoo::Baz -le “print Foo::Baz::double(1)”2

2010年7月24日土曜日

Page 29: 5分で始める XS - tsukuba.xs#1

うごいた!!

2010年7月24日土曜日

Page 30: 5分で始める XS - tsukuba.xs#1

まとめ

• ちょっとしたCのラッパーだったら、XSの深い知識がなくてもなんとかなる

• SVとかtypemapとかは各自調べてください><

2010年7月24日土曜日