NAME

SPVM::Document::NativeAPI - SPVM Native API

What is Native API

Native API is C level functions to manipulate data of SPVM in NativeAPI.

If you need more performance or bind C/C++ functions, you can use NativeAPI and Native API.

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 NativeAPI::C, but SPVM implement this feature by default.

# TestCase/NativeAPI.spvm
package TestCase::NativeAPI {
  native sub sum : int ($num1 : int, $num2 : int);
}

// TestCase/NativeAPI.native/NativeAPI.c
#include <spvm_native.h>

int32_t SPNATIVE__TestCase__NativeAPI__sum(SPVM_ENV* env, SPVM_VALUE* stack) {
  
  int32_t total = stack[0].ival + stack[1].ival;
  
  // Return value is set to stack[0]
  stack[0].ival = total;
  
  // If function success, return SPVM_SUCCESS
  return SPVM_SUCCESS;
}

At first, you specify native descripter at SPVM subroutine.

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

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

TestCase/NativeAPI.spvm
TestCase/NativeAPI.native/NativeAPI.c

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

#include <spvm_native.h>

C Function name start with SPNATIVE__. C Function name must be replace : with <_>. If SPVM subroutine belong to TestCase::NativeAPI package and subroutine name is Csum>, Declaration is the following.

int32_t SPNATIVE__TestCase__NativeAPI__sum(SPVM_ENV* env, SPVM_VALUE* stack) {

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

First argument is env. This is the pointer to SPVM_ENV. Second argument is stack. stack constains arguments.

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

stack[0].ival

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

stack[0].bval
stack[0].sval
stack[0].ival
stack[0].lval
stack[0].fval
stack[0].dval
stack[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*

NAME

SPVM::Guides::LanguageSpecification - The SPVM 1.0 Language Specification

DESCRIPTION

The SPVM 1.0 Language Specification is described in this document. SPVM is in beta testing for the 1.0 release. Language specifications are subject to change without warning.

SPVM Language is designed to be used from Perl. Perl can call SPVM subroutines.

SPVM Language provides fast array operation and fast numeric calculation.

SPVM subroutines is executed by the SPVM Runtime. its can be converted to shared libraries or DLL.

SPVM Language can be converted to machine codes, and you can execute them as a single executable.

Executables contain not only Unix/Linux/macOS/ios executable, but also Windows Native Executable,

C99 compliant

C99-compliant compiler and runtime

The source code for the SPVM compiler and SPVM runtime is written in C and conforms to C99.

Note that this does not mean that the user cannot call C versions other than C99 (C11, GNU extensions, etc.) or code written in C ++ from SPVM. This means that the SPVM compiler and runtime must be C99 compliant.

Correspondence of type with C99

The SPVM type exactly matches the following C99 types:

SPVM type C99 type Description
byte int8_t SPVM byte type matches C99 int8_t type.
short int16_t SPVM short type matches the C99 int16_t type.
int int32_t SPVM int type matches the C99 int32_t type.
long int64_t SPVM long type matches C99 int64_t type.
float float SPVM float type matches the C99 float type.
double double SPVM double type matches C99 double.
Object type void* SPVM object type matches the C99 void* type.
Multiple numeric type Numeric array type that matches tye type and the length of the fields of SPVM multiple numeric type

For example, if the multiple numeric type is

package Point_2i: mulnum_t {has x: int; has y: int;}

It matches the type declared in C99

int32_t var[2];

Parsing

comment

POD

package

module

Permission

Package variables

field

subroutine

Enumeration

BEGIN block

Lexical variable

block

scope

Array

value

An array of values

reference

warning

Exception handling

Callback

Week reference

formula

literal

Fat Comma

String

Undefined value

operator

Operator precedence

Sentence

Type

Type inference

Type conversion

Thread safe

Automatically loaded modules

Restriction