SlideShare a Scribd company logo
1 of 43
Download to read offline
PHP と
Ruby の
架け橋
             2013/01/13
      Tokyo Ruby Kaigi 10
                  do_aki
@do_aki

http://do-aki.net/
About 2 extensions

      php-extension ruby
Runnable ruby script in PHP (>=5.4)




           php_embed gem library
                       Runnable php script in Ruby(>= 1.9)
https://github.com/do-aki/php-ext-ruby
php-ext-ruby sample code
<?php
// php.ini : extension=ruby.so
ruby_eval(<<<'EOS'
     def hello
             “Ruby #{RUBY_VERSION} in PHP! "
     end
EOS
);

echo ruby_eval('hello()');   # Ruby 1.9.3 in PHP!
Web Application?
<?php
       sinatra on ext-ruby
// php.ini : extension=ruby.so
ruby_require(‘sinatra’);
ruby_eval(<<<EOS
     get ‘/’
             ‘Hello sinatra in PHP!’
     end
EOS
);
sinatra on ext-ruby
<?php                                  (not
                       work)
// php.ini : extension=ruby.so
ruby_require(‘sinatra’);
ruby_eval(<<<EOS
     get ‘/’
             ‘Hello sinatra in PHP!’
     end
EOS
);
ruby_require calls
            rb_require but…

•   rb_require is ‘require’ in Ruby script
•   require is not rb_require in Ruby 1.9
•   rb_require(“rubygems”) already called
•   rubygems override original require!

• also not work (another reason)
  rb_funcall(rb_mKernel, rb_intern(“require”)) ?
sinatra on ext-ruby again
<?php
ruby_eval(<<<EOS
     require 'sinatra/base‘
     class App < Sinatra::Base
           get '/' do
                  'Hello Sinatra in PHP!'
           end
     end
EOS
);
突然の死
(Segmentation fault)
次回作にご期待ください

• Runnable standard library
• ruby-mysql library runs too

• Web application works in PHP and Ruby if you
  make F/W which runnable on this extension!

                                     o...rz
<?php
ruby_eval(<<<'EOC'
     require 'mysql'
     client = Mysql.connect('127.0.0.1', ‘usr', ‘pas’, 'test')
     client.query("SELECT id,name FROM
                        hoge").each do |id, name|
            p [id, name]
     end
EOC
);
php_embed (rubygems.org)




     https://rubygems.org/gems/php_embed
require ‘php_embed’
    php_embed sample code
PhpEmbed.eval <<EOS
      function hello($rv) {
            $pv = phpversion();
            return “PHP {$pv} in Ruby {$rv}!”;
      }
EOS
retval = PhpEmbed.call(:hello, RUBY_VERSION)
p retval # PHP 5.4.10 in Ruby 1.9.3!
PhpEmbed::Value Class
• This class holds a PHP variable
• Convert Ruby variables into PHP variables
  (new)
• Convert PHP variables into Ruby variables
  (to_X)

• PHPnized comparison rule
p_ary = PhpEmbed::Value.new([1, 2, 3])
  PhpEmbed::Value sample code
p p_ary                   # Array
p p_ary.to_a              # [1, 2, 3]
p p_ary.to_a[0].class # PhpEmbed::Value
---------------------------------------------------------
p_zero = PhpEmbed::Value.new(0)
p_false = PhpEmbed::Value.new(false)
p pzero == p_false # true
func = PhpEmbed::Value.evalsample
  PhpEmbed::Value            << EOS   code
     function ($ary) {
           return count($ary);
     }
EOS

p func.class           # PhpEmbed::Value
p func.callable?       # true
p func.call(1..100);   # 100
Web Application?
sorry……
PHP Architecture

                     PHP Script
                 Extensions
 Web
Server
  or
                         Zend
              SAPI
 OS                     Engine
PHP SAPIs

aolserver / apache / apache2filter /
apache2handler / apache_hooks /
caudium / cgi / cli / continuity / embed
/ fpm / isapi / litespeed / milter / nsapi
/ phttpd / pi3web / roxen / thttpd / tux
/ webjames
                under sapi directory in php-5.4.10 source code
PHP SAPIs

aolserver / apache / apache2filter /
        php_embed uses embed SAPI .
          But embed SAPI has not
apache2handler /web server
           interface to apache_hooks /

caudium / cgi / cli / continuity / embed
/ fpm / isapi / litespeed / milter / nsapi
/ phttpd / pi3web / roxen / thttpd / tux
/ webjames
                under sapi directory in php-5.4.10 source code
PHP Installer
• gem install php_embed
  – require “libphp5”
  – yum install php-embedded php-devel
  – Or compile php yourself


• gem install php_embed -- --compile-php
  – compile minimal php (in gem directory)
  – require gcc / tar / make
php-extension ruby
Runnable ruby script in PHP (>=5.4)




           php_embed gem library
                       Runnable php script in Ruby(>= 1.9)
Just for Fun
Difference between
   PHP and Ruby
   in extensions
Overview of PHP/Ruby extension

                    Extension
                   (C Function)
PHP                                          PHP
                                  retrun
        args
                                   value
Ruby                                         Ruby



 • Written by C language mostly
 • Method(Function) in Ruby/PHP calls C Function
Overview of PHP/Ruby extension

                          Extension
                         (C Function)
PHP                                                       PHP
         zval                             zval
        VALUE                            VALUE
Ruby                                                      Ruby



            zval                          VALUE
   internal representation of     internal representation of
        variable in PHP                variable in Ruby
zval           zval VSVALUE
                       VALUE
type info                  pointer (*RVALUE)
 + reference count            RBasic / RObject / RClass /
 + value                   RFloat / RString / RArray /
                           RRegexp / RHash … and others
   lval (integer)
   dval (floating point)   or value
   str (string)               – false / true / nil / symbol
   ht (hash table)            – same integer
                              – some float (>= 2.0?)
   obj (object)

               Simple                      Complex
Garbage Collection
zval                     VALUE

  reference counting        mark and sweep
  + circular reference       (conservative)
        collector



   difficulty handling       easy handling
zend_eval_string (PHP)
define:
  ZEND_API int zend_eval_string(
    char *str, zval *retval_ptr, char *string_name
    TSRMLS_DC);

example:
  char *code = “$value=1”, *name=“value”;
  zval ret;

  zend_eval_string(code, &ret, name TSRMLS_CC);
rb_eval_string (Ruby)
define:
  VALUE rb_eval_string(const char *str);

example:
  char *code = “value=1”;
  VALUE ret;

  ret = rb_eval_string(code);
Build extensions
PHP                 Ruby

 $ phpize            $ ruby extconf.rb
 $ configure         $ make
 $ make              $ make install
 $ make install

m4 macros           Ruby script
autoconf            mkmf library
Code Reading
GNU Global (htags)
 http://www.gnu.org/software/global/
cgdb
http://cgdb.github.com/
References
Ruby:
      RHG (Rubyソースコード完全解説)
      http://i.loveruby.net/ja/rhg/
PHP :
      php.net
      PHP Extension Writing (PHP Quebec 2009)
     http://talks.somabo.de/200903_montreal_php_extension_writing.pdf


                                              Notice: they are old a little
Commit log
Conclusion


 処理系/拡張
  いじるの
めっさ楽しい!
Thank you!



               2013/01/13 Tokyo Ruby Kaigi 10
                    A Bridge Between PHP and Ruby
                                           @do_aki
英語間違えてたらこっそり教えて><

More Related Content

What's hot

PHP traits, treat or threat?
PHP traits, treat or threat?PHP traits, treat or threat?
PHP traits, treat or threat?Nick Belhomme
 
What lies beneath the beautiful code?
What lies beneath the beautiful code?What lies beneath the beautiful code?
What lies beneath the beautiful code?Niranjan Sarade
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHPNick Belhomme
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMCharles Nutter
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)goccy
 
I Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java BytecodeI Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java BytecodeAlexander Shopov
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)Douglas Chen
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for DummiesElizabeth Smith
 
PHP 7 OPCache extension review
PHP 7 OPCache extension reviewPHP 7 OPCache extension review
PHP 7 OPCache extension reviewjulien pauli
 
Lifting The Veil - Reading Java Bytecode During Lunchtime
Lifting The Veil - Reading Java Bytecode During LunchtimeLifting The Veil - Reading Java Bytecode During Lunchtime
Lifting The Veil - Reading Java Bytecode During LunchtimeAlexander Shopov
 
Lifting The Veil - Reading Java Bytecode
Lifting The Veil - Reading Java BytecodeLifting The Veil - Reading Java Bytecode
Lifting The Veil - Reading Java BytecodeAlexander Shopov
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7julien pauli
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 

What's hot (20)

PHP traits, treat or threat?
PHP traits, treat or threat?PHP traits, treat or threat?
PHP traits, treat or threat?
 
What lies beneath the beautiful code?
What lies beneath the beautiful code?What lies beneath the beautiful code?
What lies beneath the beautiful code?
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
Modern PHP
Modern PHPModern PHP
Modern PHP
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVM
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
I Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java BytecodeI Know Kung Fu - Juggling Java Bytecode
I Know Kung Fu - Juggling Java Bytecode
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Shark
Shark Shark
Shark
 
PHP 7 OPCache extension review
PHP 7 OPCache extension reviewPHP 7 OPCache extension review
PHP 7 OPCache extension review
 
Lifting The Veil - Reading Java Bytecode During Lunchtime
Lifting The Veil - Reading Java Bytecode During LunchtimeLifting The Veil - Reading Java Bytecode During Lunchtime
Lifting The Veil - Reading Java Bytecode During Lunchtime
 
Lifting The Veil - Reading Java Bytecode
Lifting The Veil - Reading Java BytecodeLifting The Veil - Reading Java Bytecode
Lifting The Veil - Reading Java Bytecode
 
Puppet NBLUG 2008-09
Puppet NBLUG 2008-09Puppet NBLUG 2008-09
Puppet NBLUG 2008-09
 
Lecture8
Lecture8Lecture8
Lecture8
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 

Similar to A bridge between php and ruby

DevOps in PHP environment
DevOps in PHP environmentDevOps in PHP environment
DevOps in PHP environmentEvaldo Felipe
 
Php7 HHVM and co
Php7 HHVM and coPhp7 HHVM and co
Php7 HHVM and coweltling
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012rivierarb
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and coPierre Joye
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckrICh morrow
 
Opal chapter 4_a_new_hope
Opal chapter 4_a_new_hopeOpal chapter 4_a_new_hope
Opal chapter 4_a_new_hopeForrest Chang
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsMark Baker
 
Flying under the radar
Flying under the radarFlying under the radar
Flying under the radarMark Baker
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Muhamad Al Imran
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Racksickill
 

Similar to A bridge between php and ruby (20)

PHP
PHPPHP
PHP
 
DevOps in PHP environment
DevOps in PHP environmentDevOps in PHP environment
DevOps in PHP environment
 
Php7 HHVM and co
Php7 HHVM and coPhp7 HHVM and co
Php7 HHVM and co
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
 
How DSL works on Ruby
How DSL works on RubyHow DSL works on Ruby
How DSL works on Ruby
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
Opal chapter 4_a_new_hope
Opal chapter 4_a_new_hopeOpal chapter 4_a_new_hope
Opal chapter 4_a_new_hope
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
Flying under the radar
Flying under the radarFlying under the radar
Flying under the radar
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Prersentation
PrersentationPrersentation
Prersentation
 
Php resque
Php resquePhp resque
Php resque
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Laravel level 0 (introduction)
Laravel level 0 (introduction)Laravel level 0 (introduction)
Laravel level 0 (introduction)
 
Ruby
RubyRuby
Ruby
 

More from do_aki

Tritonn から Elasticsearch への移行話
Tritonn から Elasticsearch への移行話Tritonn から Elasticsearch への移行話
Tritonn から Elasticsearch への移行話do_aki
 
php-src の歩き方
php-src の歩き方php-src の歩き方
php-src の歩き方do_aki
 
PHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 とPHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 とdo_aki
 
PHPとシグナル、その裏側
PHPとシグナル、その裏側PHPとシグナル、その裏側
PHPとシグナル、その裏側do_aki
 
再考:列挙型
再考:列挙型再考:列挙型
再考:列挙型do_aki
 
signal の話 或いは Zend Signals とは何か
signal の話 或いは Zend Signals とは何かsignal の話 或いは Zend Signals とは何か
signal の話 或いは Zend Signals とは何かdo_aki
 
PHP AST 徹底解説(補遺)
PHP AST 徹底解説(補遺)PHP AST 徹底解説(補遺)
PHP AST 徹底解説(補遺)do_aki
 
PHP AST 徹底解説
PHP AST 徹底解説PHP AST 徹底解説
PHP AST 徹底解説do_aki
 
Writing php extensions in golang
Writing php extensions in golangWriting php extensions in golang
Writing php extensions in golangdo_aki
 
php7's ast
php7's astphp7's ast
php7's astdo_aki
 
N対1 レプリケーション + Optimizer Hint
N対1 レプリケーション + Optimizer HintN対1 レプリケーション + Optimizer Hint
N対1 レプリケーション + Optimizer Hintdo_aki
 
20150212 プレゼンテーションzen
20150212 プレゼンテーションzen20150212 プレゼンテーションzen
20150212 プレゼンテーションzendo_aki
 
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」do_aki
 
20141017 introduce razor
20141017 introduce razor20141017 introduce razor
20141017 introduce razordo_aki
 
20141011 mastering mysqlnd
20141011 mastering mysqlnd20141011 mastering mysqlnd
20141011 mastering mysqlnddo_aki
 
php in ruby
php in rubyphp in ruby
php in rubydo_aki
 
PHP から Groonga を使うにはこんなコードになるよ!
PHP から Groonga を使うにはこんなコードになるよ!PHP から Groonga を使うにはこんなコードになるよ!
PHP から Groonga を使うにはこんなコードになるよ!do_aki
 
N:1 Replication meets MHA
N:1 Replication meets MHAN:1 Replication meets MHA
N:1 Replication meets MHAdo_aki
 
Php radomize
Php radomizePhp radomize
Php radomizedo_aki
 
php and sapi and zendengine2 and...
php and sapi and zendengine2 and...php and sapi and zendengine2 and...
php and sapi and zendengine2 and...do_aki
 

More from do_aki (20)

Tritonn から Elasticsearch への移行話
Tritonn から Elasticsearch への移行話Tritonn から Elasticsearch への移行話
Tritonn から Elasticsearch への移行話
 
php-src の歩き方
php-src の歩き方php-src の歩き方
php-src の歩き方
 
PHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 とPHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 と
 
PHPとシグナル、その裏側
PHPとシグナル、その裏側PHPとシグナル、その裏側
PHPとシグナル、その裏側
 
再考:列挙型
再考:列挙型再考:列挙型
再考:列挙型
 
signal の話 或いは Zend Signals とは何か
signal の話 或いは Zend Signals とは何かsignal の話 或いは Zend Signals とは何か
signal の話 或いは Zend Signals とは何か
 
PHP AST 徹底解説(補遺)
PHP AST 徹底解説(補遺)PHP AST 徹底解説(補遺)
PHP AST 徹底解説(補遺)
 
PHP AST 徹底解説
PHP AST 徹底解説PHP AST 徹底解説
PHP AST 徹底解説
 
Writing php extensions in golang
Writing php extensions in golangWriting php extensions in golang
Writing php extensions in golang
 
php7's ast
php7's astphp7's ast
php7's ast
 
N対1 レプリケーション + Optimizer Hint
N対1 レプリケーション + Optimizer HintN対1 レプリケーション + Optimizer Hint
N対1 レプリケーション + Optimizer Hint
 
20150212 プレゼンテーションzen
20150212 プレゼンテーションzen20150212 プレゼンテーションzen
20150212 プレゼンテーションzen
 
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
 
20141017 introduce razor
20141017 introduce razor20141017 introduce razor
20141017 introduce razor
 
20141011 mastering mysqlnd
20141011 mastering mysqlnd20141011 mastering mysqlnd
20141011 mastering mysqlnd
 
php in ruby
php in rubyphp in ruby
php in ruby
 
PHP から Groonga を使うにはこんなコードになるよ!
PHP から Groonga を使うにはこんなコードになるよ!PHP から Groonga を使うにはこんなコードになるよ!
PHP から Groonga を使うにはこんなコードになるよ!
 
N:1 Replication meets MHA
N:1 Replication meets MHAN:1 Replication meets MHA
N:1 Replication meets MHA
 
Php radomize
Php radomizePhp radomize
Php radomize
 
php and sapi and zendengine2 and...
php and sapi and zendengine2 and...php and sapi and zendengine2 and...
php and sapi and zendengine2 and...
 

Recently uploaded

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Recently uploaded (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

A bridge between php and ruby

  • 1. PHP と Ruby の 架け橋 2013/01/13 Tokyo Ruby Kaigi 10 do_aki
  • 3. About 2 extensions php-extension ruby Runnable ruby script in PHP (>=5.4) php_embed gem library Runnable php script in Ruby(>= 1.9)
  • 5.
  • 6. php-ext-ruby sample code <?php // php.ini : extension=ruby.so ruby_eval(<<<'EOS' def hello “Ruby #{RUBY_VERSION} in PHP! " end EOS ); echo ruby_eval('hello()'); # Ruby 1.9.3 in PHP!
  • 8. <?php sinatra on ext-ruby // php.ini : extension=ruby.so ruby_require(‘sinatra’); ruby_eval(<<<EOS get ‘/’ ‘Hello sinatra in PHP!’ end EOS );
  • 9. sinatra on ext-ruby <?php (not work) // php.ini : extension=ruby.so ruby_require(‘sinatra’); ruby_eval(<<<EOS get ‘/’ ‘Hello sinatra in PHP!’ end EOS );
  • 10. ruby_require calls rb_require but… • rb_require is ‘require’ in Ruby script • require is not rb_require in Ruby 1.9 • rb_require(“rubygems”) already called • rubygems override original require! • also not work (another reason) rb_funcall(rb_mKernel, rb_intern(“require”)) ?
  • 11. sinatra on ext-ruby again <?php ruby_eval(<<<EOS require 'sinatra/base‘ class App < Sinatra::Base get '/' do 'Hello Sinatra in PHP!' end end EOS );
  • 13. 次回作にご期待ください • Runnable standard library • ruby-mysql library runs too • Web application works in PHP and Ruby if you make F/W which runnable on this extension! o...rz
  • 14. <?php ruby_eval(<<<'EOC' require 'mysql' client = Mysql.connect('127.0.0.1', ‘usr', ‘pas’, 'test') client.query("SELECT id,name FROM hoge").each do |id, name| p [id, name] end EOC );
  • 15. php_embed (rubygems.org) https://rubygems.org/gems/php_embed
  • 16. require ‘php_embed’ php_embed sample code PhpEmbed.eval <<EOS function hello($rv) { $pv = phpversion(); return “PHP {$pv} in Ruby {$rv}!”; } EOS retval = PhpEmbed.call(:hello, RUBY_VERSION) p retval # PHP 5.4.10 in Ruby 1.9.3!
  • 17. PhpEmbed::Value Class • This class holds a PHP variable • Convert Ruby variables into PHP variables (new) • Convert PHP variables into Ruby variables (to_X) • PHPnized comparison rule
  • 18. p_ary = PhpEmbed::Value.new([1, 2, 3]) PhpEmbed::Value sample code p p_ary # Array p p_ary.to_a # [1, 2, 3] p p_ary.to_a[0].class # PhpEmbed::Value --------------------------------------------------------- p_zero = PhpEmbed::Value.new(0) p_false = PhpEmbed::Value.new(false) p pzero == p_false # true
  • 19. func = PhpEmbed::Value.evalsample PhpEmbed::Value << EOS code function ($ary) { return count($ary); } EOS p func.class # PhpEmbed::Value p func.callable? # true p func.call(1..100); # 100
  • 22. PHP Architecture PHP Script Extensions Web Server or Zend SAPI OS Engine
  • 23. PHP SAPIs aolserver / apache / apache2filter / apache2handler / apache_hooks / caudium / cgi / cli / continuity / embed / fpm / isapi / litespeed / milter / nsapi / phttpd / pi3web / roxen / thttpd / tux / webjames under sapi directory in php-5.4.10 source code
  • 24. PHP SAPIs aolserver / apache / apache2filter / php_embed uses embed SAPI . But embed SAPI has not apache2handler /web server interface to apache_hooks / caudium / cgi / cli / continuity / embed / fpm / isapi / litespeed / milter / nsapi / phttpd / pi3web / roxen / thttpd / tux / webjames under sapi directory in php-5.4.10 source code
  • 25. PHP Installer • gem install php_embed – require “libphp5” – yum install php-embedded php-devel – Or compile php yourself • gem install php_embed -- --compile-php – compile minimal php (in gem directory) – require gcc / tar / make
  • 26.
  • 27. php-extension ruby Runnable ruby script in PHP (>=5.4) php_embed gem library Runnable php script in Ruby(>= 1.9)
  • 29. Difference between PHP and Ruby in extensions
  • 30. Overview of PHP/Ruby extension Extension (C Function) PHP PHP retrun args value Ruby Ruby • Written by C language mostly • Method(Function) in Ruby/PHP calls C Function
  • 31. Overview of PHP/Ruby extension Extension (C Function) PHP PHP zval zval VALUE VALUE Ruby Ruby zval VALUE internal representation of internal representation of variable in PHP variable in Ruby
  • 32. zval zval VSVALUE VALUE type info pointer (*RVALUE) + reference count RBasic / RObject / RClass / + value RFloat / RString / RArray / RRegexp / RHash … and others lval (integer) dval (floating point) or value str (string) – false / true / nil / symbol ht (hash table) – same integer – some float (>= 2.0?) obj (object) Simple Complex
  • 33. Garbage Collection zval VALUE reference counting mark and sweep + circular reference (conservative) collector difficulty handling easy handling
  • 34. zend_eval_string (PHP) define: ZEND_API int zend_eval_string( char *str, zval *retval_ptr, char *string_name TSRMLS_DC); example: char *code = “$value=1”, *name=“value”; zval ret; zend_eval_string(code, &ret, name TSRMLS_CC);
  • 35. rb_eval_string (Ruby) define: VALUE rb_eval_string(const char *str); example: char *code = “value=1”; VALUE ret; ret = rb_eval_string(code);
  • 36. Build extensions PHP Ruby $ phpize $ ruby extconf.rb $ configure $ make $ make $ make install $ make install m4 macros Ruby script autoconf mkmf library
  • 38. GNU Global (htags) http://www.gnu.org/software/global/
  • 40. References Ruby: RHG (Rubyソースコード完全解説) http://i.loveruby.net/ja/rhg/ PHP : php.net PHP Extension Writing (PHP Quebec 2009) http://talks.somabo.de/200903_montreal_php_extension_writing.pdf Notice: they are old a little
  • 42. Conclusion 処理系/拡張 いじるの めっさ楽しい!
  • 43. Thank you! 2013/01/13 Tokyo Ruby Kaigi 10 A Bridge Between PHP and Ruby @do_aki 英語間違えてたらこっそり教えて><