Easy C/C++ Binding using SPVM

I introduce Easy C/C++ Binding using SPVM . In Perl, C/C++ binding is very very hard work. If you use SPVM, the work become very easy.

If You don't yet know SPVM itself, See SPVM Document at first.

SPVM Module:

# lib/SPVM/MyMathNative.spvm
package MyMathNative {
  
  # Sub Declaration
  sub sum ($nums : int[]) : native int;
}

C Source File:

// lib/SPVM/MyMathNative.native/MyMathNative.c
#include 

int32_t SPVM__MyMathNative__sum(SPVM_API* api, SPVM_API_VALUE* args) {
  
  // First argument
  SPVM_API_OBJECT* sp_nums = args[0].object_value;
  
  // Array length
  int32_t length = api->get_array_length(api, sp_nums);
  
  // Elements pointer
  int32_t* nums = api->get_int_array_elements(api, sp_nums);
  
  // Culcurate total
  int32_t total = 0;
  {
    int32_t i;
    for (i = 0; i < length; i++) {
      total += nums[i];
    }
  }
  
  return total;
}

Use Extension Module from Perl:

use FindBin;
use lib "$FindBin::Bin/lib";

# Use SPVM module
use SPVM 'MyMathNative';

# New SPVM int array
my $sp_nums = SPVM::new_int_array([3, 6, 8, 9]);

# Call SPVM subroutine
my $total = SPVM::MyMathNative::sum($sp_nums);

print $total . "\n";

You maybe use XS or Inline::C or FFI to bind C/C++ library from now.

Today addition, You can select SPMV to bind C/C++.

If you learn more, see the following.

Easy C/C++ Binding using SPVM

SPVM is before 1.0 under development! I will change implementation and specification without warnings.


Leave a comment

About Yuki Kimoto

user-pic I'm Yuki Kimoto, Japanese Perl programmer.I create Perl modules and applications. GitPrep, SPVM, DBIx::Custom, Validator::Custom, Test::Mojo, Mojolicious::Plugin::AutoRoute, etc.