=head1 NAME
SPVM::Document::Extension - C/C++ Binding using SPVM Extension(BETA before 1.0)
=head1 Tutorial
=head2 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 L<Extension::C>, but L<SPVM> implement this feature by default.
# SPVM/TestCase/Extension.spvm
package TestCase::Extension {
native sub sum : int ($num1 : int, $num2 : int);
}
// SPVM/TestCase/Extension.native/Extension.c
#include <spvm_api.h>
int32_t SPVM__TestCase__Extension__sum(SPVM_API* api, SPVM_API_VALUE* args) {
int32_t total = args[0].int_value + args[1].int_value;
return total;
}
At first, you specify C<native> descripter at SPVM subroutine.
package TestCase::Extension {
# native descripter
native sub sum : int ($num1 : int, $num2 : int);
}
Next, you create "SPVM/TestCase/Extension/Extension.c" file
SPVM/TestCase/Extension.spvm
SPVM/TestCase/Extension.native/Extension.c
Next, you write C language. You include C<spvm_api.h>.
#include <spvm_api.h>
C Function name must be replace C<:> with <_> and add C<SPVM__> to top of SPVM subroutine name. SPVM subroutine absolute name is
C<TestCase::Extension::sum>. C function name is C<SPVM__TestCase__Extension__sum>.
int32_t SPVM__TestCase__Extension__sum(SPVM_API* api, SPVM_API_VALUE* args) {
int32_t total = args[0].int_value + args[1].int_value;
return total;
}
First argument is C<api>. This is the pointer to C<SPVM_API>. Second argument is C<args>. This is array of C<SPVM_API_VALUE>.
SPVM_API_VALUE is union of C language. If you want to get int value, you do the followng.
args[0].int_value
You can get all type of value by the following member.
args[0].byte_value
args[0].short_value
args[0].int_value
args[0].long_value
args[0].float_value
args[0].double_value
args[0].object_value
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] SPVM_API_OBJECT*