++ed by:
SKOLYCHEV FAYLAND BEROV ILUX MHOWARD

5 PAUSE users
4 non-PAUSE users.

Yuki Kimoto
and 1 contributors

NAME

SPVM::Document::Extension - C/C++ Binding using SPVM Extension(BETA before 1.0)

Tutorial

Runtime Compile Native Function

If you use the feature Runtime Compile Native Function, you can write program using C language in runtime. This is similar with Extension::C, but SPVM implement this feature by default.

  # TestCase/Extension.spvm
  package TestCase::Extension {
    native sub sum : int ($num1 : int, $num2 : int);
  }
  
  // TestCase/Extension.native/Extension.c
  #include <spvm_native.h>
  
  int32_t SPVM_NATIVE_SUB(TestCase__Extension__sum) (SPVM_ENV* env, SPVM_VALUE* args) {
    
    int32_t total = args[0].ival + args[1].ival;
    
    // Return value is set to args[0]
    args[0].ival = total;
    
    // If function success, return 0
    return 0;
  }

At first, you specify native descripter at SPVM subroutine.

  package TestCase::Extension {
    # native descripter
    native sub sum : int ($num1 : int, $num2 : int);
  }

Next, you create "TestCase/Extension/Extension.c" file

  TestCase/Extension.spvm
  TestCase/Extension.native/Extension.c

Next, you write C language. You include spvm_native.h.

  #include <spvm_native.h>

C Function name is created by SPVM_NATIVE_SUB macro. C Function name must be replace : with <_>. If SPVM subroutine belong to TestCase::Extension package and subroutine name is Csum>, Declaration is the following.

  int32_t SPVM_NATIVE_SUB(TestCase__Extension__sum) (SPVM_ENV* env, SPVM_VALUE* args) {

    int32_t total = args[0].ival + args[1].ival;
    
    // Return value is set to args[0]
    args[0].ival = total;
    
    // If function success, return 0
    return 0;
  }

First argument is env. This is the pointer to SPVM_ENV. Second argument is args. This is array of SPVM_VALUE.

SPVM_VALUE is union of C language. If you want to get int value, you do the followng.

  args[0].ival

You can get all type of value by the following member.

  args[0].bval
  args[0].sval
  args[0].ival
  args[0].lval
  args[0].fval
  args[0].dval
  args[0].oval

Type of return value is corresponding the folloing type.

  [SPVM]              [C]
  byte                int8_t
  short               int16_t
  int                 int32_t
  long                int64_t
  float               float
  double              double
  [Object value]      void*