Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ZigでC拡張を作る 2024 Edition

osyoyu
September 29, 2024

ZigでC拡張を作る 2024 Edition

osyoyu

September 29, 2024
Tweet

More Decks by osyoyu

Other Decks in Programming

Transcript

  1. !

  2. $֦ுΛॻ͚͹͍͍ w 3VCZ͔Β$ͷίʔυΛݺͼग़ͤΔػೳ w ଎͍ίʔυΛݺͼग़͓ͤͯτΫ w ୅දྫͱͯ͠NZTRM͕͋Δ $Ͱॻ͔ΕͨMJCNZTRMDMJFOUΛݺΜͰ͍Δ  w

    ෆ౰ͳ΄Ͳ଎͘ͳΔ VALUE rb_nanika() { VALUE ary = rb_ary_new(); rb_ary_push(ary, INT2NUM(1)); return ary; } require_relative "./ext.so" p nanika #=> [1]
  3. $ͷ͕͍͍͜͜ #include <signal.h> static volatile sig_atomic_t flag = 0; int

    main(int argc, char *argv[], char *envp[]) { int heap = (int *)malloc(100 * sizeof(int)); int *ptr = heap[0]; *(ptr + 1) *= ptr * ptr; signal(SIGINT, signal_handler); for(;;) {}; } εʔύʔϋΧʔͬΆ͍
  4. $ͷ͕͜͜ྑ͘ͳ͍ εʔύʔϋΧʔ͗͢Δ #include <signal.h> static volatile sig_atomic_t flag = 0;

    int main(int argc, char *argv[], char *envp[]) { int heap = (int *)malloc(100 * sizeof(int)); int *ptr = heap[0]; *(ptr + 1) *= ptr * ptr; signal(SIGINT, signal_handler); for(;;) {}; }
  5. ͦ͜Ͱ;JH w ࠓճ͸$FYUFOTJPOΛ;JHͰॻ͘ͱͲ͏ͳΔ͔ʁͱ͍͏಺༰ VALUE rb_nanika() { VALUE ary = rb_ary_new();

    rb_ary_push(ary, INT2NUM(1)); return ary; } fn rb_nanika() ruby.VALUE { const ary = ruby.rb_ary_new(); ruby.rb_ary_push( ary, ruby.INT2NUM(1)
 ); return ary; }
  6. ;JH͍͍ͱ͜Ζ  ׂͱศརͳTUEMJC͕͋Δ ྫྷ੩ʹߟ͑Δͱࣙॻܕ͢Βͳ͍$͸ศརͰ͸ͳ͍ var dict = std.AutoHashMap( *const i8,

    i32
 ).init(...); defer dict.deinit(); try dict.put("ruby", 1); try dict.put("c", 2); try dict.put("zig", 3); w "SSBZ-JTU )BTI.BQ 4UBDL  ͳͲɺΑ͘࢖͏σʔλߏ଄͸ ἧ͍ͬͯΔ w ศརʂ w ͳΜͰ$ʹ͸ͳ͍Μͩ
  7. ;JH͍͍ͱ͜Ζ  $ؔ਺΋ϚΫϩ΋ݺ΂Δ ϚδͰʂʁ const ruby = @cImport({ @cInclude("ruby.h"); });

    fn fib(n: u32) ruby.VALUE { if (n<=1) { return n; } const res = fib(n-1) + fib(n-2); return ruby.INT2FIX(res) } w $''*ػೳ͕͋ͬͯ΋ɺ $ϚΫϩΛݺͼग़ͤΔݴޠ͸ ͦ͏ͳ͍ w 3VCZ$"1*Ͱ͸ϚΫϩ͕ͳ ͍ͱ݁ߏπϥ͍ͷͰɺॿ͔Δ w */5'*9 '*9*/5  35&45  w ΋ͪΖΜ$͔Β΋;JHΛݺ΂ Δ
  8. ;JH͍͍ͱ͜Ζ  ΅ͪ΅ͪTBGF ΅ͪ΅͙ͪΒ͍ var dict = std.AutoHashMap( *const i8,

    i32
 ).init(...); defer dict.deinit(); try dict.put("ruby", 1); try dict.put("c", 2); try dict.put("zig", 3); w OVMMΛ౿Έൈ͖ʹ͍͘Α͏ͳ ػೳ͕΅ͪ΅ͪ w 0QUJPOܕ w GSFF࿙ΕΛ๷͙ػೳ΋΅ͪ΅ͪ w EFGFS w ·͊΅ͪ΅ͪʜʜ w VTFBGUFSGSFFͱ͔͸๷͛ͳ ͍
  9. ;JH͍͍ͱ͜Ζ  $ίϯύΠϥʹͳΔ w طଘͷ$ HDD ࢿ࢈ʹ;JHίʔυΛॱ͍࣍ࠞͥͯ͘ͷ͕༰қ w 5ZQF4DSJQUͷBMMPX+TΈ͍ͨͳงғؾͰ$Λ࢖͑Δʢ఻ΘΔʁʣ w

    $3VCZࣗମ΋[JHDDͰίϯύΠϧ͢Δ͜ͱ͸ʜʜͰ͖Δ ~/ruby/build % CC="zig cc" ../configure % make % make install
  10. ͜Μͳײ͡ ΅ͪ΅ͪ଎͍ const std = @import("std"); const ruby = @cImport({

    @cInclude("ruby.h"); }); export fn Init_mygem() void { ruby.rb_define_method( ruby.rb_mKernel, "fib", @ptrCast(&rb_fib), 1); } fn rb_fib() ruby.VALUE { ... } w ࣗ͘͝વʹJODMVEFSVCZI ૬౰͕Ͱ͖͍ͯΔ w SVCZSC@EFpOF@NFUIPE Έͨ ͍ͳײ͡Ͱ؆୯ʹݺͼग़ͤΔ w !QUS$BTUͱ͔͕ඞཁʹͳͬͯ Δͷ͸͝Ѫᇷ
  11. $ʹίϯύΠϧ͢Δ ext.zig ext.c ext.so zig build-obj -ofmt=c gcc -fPIC -shared

    w (FNͷҰ෦ͱͯ͠;JHίʔυΛ ഑෍͢Δ৔߹ɺϢʔβʔ؀ڥʹ ;JHίϯύΠϥ͕ඞཁʹͳΔ w ʜʜͱ͍͏͜ͱʹ͸ͳΒͳͯ͘ɺ ;JHΛࣄલʹ$ʹม׵͓ͯ͘͜͠ ͱ͕Ͱ͖Δ w ͜ΕͳΒHDD͑͋͞Ε͹͍͍ w ΋ͪΖΜ௚઀TPΛ࡞Δ͜ͱ΋Ͱ͖ Δ
  12. VOTBGF\ͦͷ3VTU͸TBGFͰ͔͢ ^ w 3VCZͷ"1*Λݺͼग़ͨ͠ॠؒʹVOTBGFͰ͢Α w VOTBGF\SC@IBTI@OFX ^ w ͦ͏ʜʜ w

    Զͷॻ͘ίʔυ͕TBGF͡Όͳ͍Θ͚ͳ͍Μ͚ͩͲʜʜ w 4&(7͢Δ·Ͱ͸ಈ͍ͯΔ͚Ͳʜʜ
  13. VOTBGF\ͦͷ3VTU͸TBGFͰ͔͢ ^ w 3VCZͷ"1*Λݺͼग़ͨ͠ॠؒʹVOTBGFͰ͢Α w VOTBGF\SC@IBTI@OFX ^ w ͦ͏ʜʜ w

    Զͷॻ͘ίʔυ͕TBGF͡Όͳ͍Θ͚ͳ͍Μ͚ͩͲʜʜ w 4&(7͢Δ·Ͱ͸ಈ͍ͯΔ͚Ͳʜʜ ൱