CSC/ECE 517 Fall 2010/ch3 3i IC

From Expertiza_Wiki
Jump to navigation Jump to search

Mixing Static and Dynamic Code


Introduction

Many middleware solutions have been developed to combine static and dynamic languages to take advantage of both types of code. Previous wiki chapters have discussed JRuby, a Java implementation of Ruby. These solutions allow programmers the productivity luxuries of newer dynamic languages (e.g. Ruby, Python, Perl) with the speed, low-level functionality, and pre-existing code base of older static languages (e.g. C, C++, Java).

Advantages of Dynamic Languages

Advantages of Static Languages

  • Faster execution speed
  • Lower memory footprint (especially C/C++)
  • Strong potential for larger existing codebase


Below, we will see even more examples of mixing several widely used dynamic languages with C, C++, and Java.

Mixing Dynamic Code and C/C++

Ruby

CplusRuby Gem

CplusRuby is a Ruby gem that allows programmers to call Ruby code within C++ and vice versa. Assuming the user has Ruby installed, the CplusRuby can be installed with the following command [2]:


gem install cplus2ruby


An example is given below. In this sample, C code is embedded in Ruby code. This uses the cplusruby gem, which is garnered toward C code, while cplus2ruby was designed for C++ code in mind. [2 ] gives an example using CplusRuby in C++ and [6 ] gives the corresponding example in C.


require 'cplusruby'

class NeuralEntity < CplusRuby
  property :id
end

class Neuron < NeuralEntity
  property :potential,        :float
  property :last_spike_time,  :float
  property :pre_synapses,     :value

  method_c :stimulate, %(float at, float weight), %{
  // this is C code
  selfc->potential += at*weight;
}

  def initialize
    self.pre_synapses = []
  end
end

# generate C file, compile it and load the .so
CplusRuby.evaluate("inspire.cc", "-O3", "-lstdc++")

if __FILE__ == $0
n = Neuron.new
n.id = "n1"
n.potential = 1.0
n.stimulate(1.0, 2.0)
p n.potential # => 3.0
end


In the example above, we declare a class as a C class, but in Ruby syntax. However, we define the method stimulate in C syntax. We then use CplusRuby to evaluate the embedded C code to generate a C code file. CplusRuby then compiles the generated C code, which then becomes available to the Ruby code declared at the bottom of the code sample. This method allows performance sensitive code to be compiled as C, which is much faster than Ruby. These C methods can also call other C methods with native C performance. [6 ] It's worth pointing out that the properties of the class Neuron and NeuralEntity (id, potential, last_spike_time, etc.) are actually members of a C-struct, not instance variables. Their identity as struct members also allows for faster access.

Python

Using Boost

Boost allows programmers to seamlessly expose C++ classes and function to Python and vice versa. No special tools are needed (however, a build tool will be referenced later). All that is required are the Boost libraries and a C++ compiler. Boost is designed to allow Python to use C++ code with no internal modification to the C++ code that will be wrapped.

Exposing Functions

Below is a “Hello, World!” example. We first define a simple C++ function that returns a "Hello, World" string.

string greet()
{ 
  return "Hello, World!";
}

We can then add the following wrapper code to our C++ code to expose it Boost. First, we include the Boost header. Then we give the C++ module that we want to use in Python the name of hello and pass it to the function BOOST_PYTHON_MODULE, which allows us to declare the module. Within the function, we use the namespace designated for Boost (boost::python). We also define the name of the function that Python will use to refer to the greet() C++ function. Naturally, it is the same as the name given in C++ ("greet").

#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello)
{
  using namespace boost::python;
  def("greet", greet);
}

Below is the python code that uses the C++ module. First, we must import the module, which we named hello above. We can then use the name of the module to call C++ functions.

import hello
print hello.greet()

Output:
>> Hello, World!

Exposing Classes

Below is a simple C++ class with one attribute and a getter and setter for that attribute.

class Person
{
  private string name;

  void setName(string name)
  {
    this->name = name;
  }

  string getName()
  {
    return name;
  }
};

Next is the Boost wrapper code. Like the sample above, we include the Boost header and use the Boost namespace. We then declare the module name by passing it to BOOST_PYTHON_MODULE. Notice the definition of the class and the two functions are defined as one line of code.

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(person_module)
{
  class_<Person>("Person")
  .def("getName", &Person::getName)
  .def("setName", &Person::setName);
}

The Python code below imports the C++ module, creates an instance of the C++ Person class, sets its name to "John" and then outputs the name.

import person_module
student = person_module.Person()
student.setName('John')
student.getName()

Output:
>> John

SWIG

Previously we've seen specific examples of middleware libraries that allow us to mix specific languages with C/C++. SWIG (Simplified Wrapper and Interface Generator) is a tool that allows programmers to mix C/C++ programs and libraries with several dynamic languages, including Tcl, Perl, Python, Ruby, PHP, and Lua. SWIG was first released in 1995 and is still maintained to this day, with its most recent release on October 4, 2010.

SWIG maintains two purposes for embedding dynamic language code in C/C++

  1. Faster customization using the scripting language
  2. Easier to write test scripts, including unit tests

SWIG allows programmers to replace the main() function of a C program with a scripting interpreter from which they can control the application. This ability adds flexibility and makes the program more easily modifiable. The scripting interface allows programmers to easily modify program behavior without having to modify low-level C/C++ code.

Example

Below, we declare two simple math functions, add and subtract.

/* File : math.c */

int a = 5;
int b = 9;

int add(int x, int y)
{
  return x + y;
}

int subtract(int x, int y)
{
  return x - y;
}

Next we create an interface file, which SWIG will use to register the C functions.

/* math.i */

%module math
%{
  extern int a;
  extern int b;
  extern int add(int x, int y);
  extern int subtract(int x, int y);
%}

extern int a;
extern int b;
extern int add(int x, int y);
extern int subtract(int x, int y);

Alternatively, we can include a header file that contains our function definitions. Assuming a math.h file that declares are variables and functions, the code can be wrapped likewise:

/* math.i */

%module math
%{
  /* Include header file in wrapper code */
  #include "math.h"
%}
/* Parse header file to generate wrappers */
%include "math.h"

The following shows the procedure for compiling the C code to use with Python. First, the interface file, math.i, created above is passed to SWIG while specifying the wrapper language as Python. Next, the C code is compiled while including the Python libraries. Finally, the object files creating by the compilation are linked to create our math_module shared object. (For more information on shared objects, see [9].

(Note: The compilation procedure below is specific to UNIX systems.)

$ swig -python math.i
$ gcc -c math.c math_wrap.c -I/usr/include/python
$ ld -shared math.o math_wrap.o -o math_module.so

We can now use the C module in Python as follows:

>>> import math_module
>>> math_module.add(5,8)
13
>>> math_module.subtract(15,5)
10

For more examples using other languages besides Python, see the SWIG tutorial. [8]

Mixing Dynamic Code and Java

Perl

Using Inline::Java

The Inline::Java module allows you to put Java code directly inline with a Perl script. After the Java code has been compiled, Perl queries the Java classes to see what public methods have been defined, since only public methods can be exported to Perl. These classes and methods are available to the Perl program as if they had been written in Perl. [5]

The code below shows how to declare and use a Java class in Perl. The below example shows a simple Person class. It has one instance variable and two methods, a getter and setter for that instance variable.

public class Person
{
  String name;

  public getName()
  {
    return name;
  }

  public setName(String newName)
  {
    this.name = newName;
  }
}

The code below shows how to wrap the Java code in Perl. The actual implementation of the Person class is omitted since it is shown above.

#!/usr/bin/perl
use strict; use warnings;

use Inline Java => <<'EOJ';
public class Person
{
  // The class body is shown in the Java Code above
}
EOJ

The Perl code below shows the instantiation of the Person class. The code also initializes the instance variable name to "John Smith". Finally, the name of the Person object is printed.

my $student = Person->new("John Smith");
print $student->getName(), "\n";

(This section on Java and Perl is mostly referenced from [4].)

In the Perl example above, we omit the details of the Java class, since they are detailed above. The declaration 'EOJ' ("end of Java", in this case) at the beginning of the Java class declaration in the Perl code is a qualifier to dictate where to end the Java code. In this particular example, we include the Java code directly in our Perl file. However, you can also include Java code via a file reference.

Conclusion

Several programs and libraries exist to allow programmers to mix static and dynamic code. In the case of mixing modern languages with C and C++, programmers can harness the speed which comes with calling C functions and using C++ classes. Regarding Java, programmers can use pre-existing code written in Java, but interface with it with dynamic languages that allow for faster and easier development. In both cases of mixing dynamic code with C++ and Java, programmers are allowed to work with dynamic languages with less restrictions on typing while using code that is already written, saving time by not having to unnecessarily re-write code.

References

[1] http://en.wikipedia.org/wiki/SWIG

[2] Michael Neumann. Cplus2Ruby - Gluing C++ and Ruby together in an OO manner. http://rubydoc.info/gems/cplus2ruby/1.2.0/frames

[3] Dave Abrahams. Boost.Python. http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html

[4] Phil Crow. Bringing Java into Perl. http://www.perl.com/pub/2003/11/07/java.html. 2003.

[5] Patrick LeBoutillier. Inline::Java http://search.cpan.org/~patl/Inline-Java-0.52/Java.pod

[6] Michael Neumann. CplusRuby - Gluing C and Ruby. http://www.ntecs.de/blog/articles/2007/09/21/cplusruby-gluing-c-and-ruby/. 2007.

[7] What is SWIG? http://www.swig.org/exec.html

[8] SWIG Tutorial. http://swig.org/tutorial.html

[9] http://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries