The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

SPVM::Document::NativeInterface - SPVM Native Interface(BETA before 1.0)

What is SPVM Native Interface

SPVM Native Interface is the API to Manipulate Array and Object, and call Subroutine in C Level.

To improve peformance, you can write programing logic by C language and Native Interface.

You can do the following by SPVM Native Interface.

  • Create Array and Object.

  • Get Array Length.

  • Get numeric Array elements. Get and set object Array element.

  • Get and set Field.

  • Get and set Exception

I write some Examples for you to understand Native Interface.

1. Add two int value:

  #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;
  }

You must include spvm_api.h to use SPVM Native Interface.

C Function name must be the name which Replace : of SPVM subroutine with _, and add SPVM to the top of it.

  [SPVM subroutine]            [C Function]                  
  SPVM::TestCase::Extension::sum  SPVM__TestCase__Extension__sum   

Data Definision

SPVM_API

SPVM Native Interface object. This object is always First Argument of native function .

  int32_t SPVM__TestCase__Extension__array_sum(SPVM_API* api, SPVM_API_VALUE* args) {
    
  }

You can call Native Interface from this object, and First Argument of Native Interface is always this object.

  int32_t SPVM__TestCase__Extension__array_sum(SPVM_API* api, SPVM_API_VALUE* args) {
    // Call SPVM Native Interface
    int32_t length = api->get_array_length(api, args[0].object_value);
  }

Functions

get_type_id

  int32_t get_type_id(SPVM_API api, const char *name);

Get type id.

LINKAGE:

PARAMETERS:

api: SPVM_API object.

name: type name, for example, Math, String, String[].

RETURNS:

Returns a type id, or 0 if type can't be found.

THROWS:

1. if no definition for a requested type can be found.

new_object

  SPVM_API_OBJECT* new_object(SPVM_API* api, int32_t type_id);

Constructs a new SPVM object. type_id must be obtained by "get_type_id".

type_id must refer to package type(not array type, not numeric type, not void type, not invalid type id).

If not package type id is specified, unexpected behavior occur.

LINKAGE:

PARAMETERS:

api: SPVM_API object.

type_id: type id.

RETURNS:

Returns a SPVM_API_OBJECT object.

Native Interface List(TODO)

  int32_t (*get_array_length)(SPVM_API*, SPVM_API_OBJECT*);
  int8_t* (*get_byte_array_elements)(SPVM_API*, SPVM_API_OBJECT*);
  int16_t* (*get_short_array_elements)(SPVM_API*, SPVM_API_OBJECT*);
  int32_t* (*get_int_array_elements)(SPVM_API*, SPVM_API_OBJECT*);
  int64_t* (*get_long_array_elements)(SPVM_API*, SPVM_API_OBJECT*);
  float* (*get_float_array_elements)(SPVM_API*, SPVM_API_OBJECT*);
  double* (*get_double_array_elements)(SPVM_API*, SPVM_API_OBJECT*);
  SPVM_API_OBJECT* (*get_object_array_element)(SPVM_API*, SPVM_API_OBJECT*, int32_t index);
  void (*set_object_array_element)(SPVM_API*, SPVM_API_OBJECT*, int32_t index, SPVM_API_OBJECT* value);
  int32_t (*get_field_id)(SPVM_API*, SPVM_API_OBJECT*, const char*);
  int8_t (*get_byte_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  int16_t (*get_short_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  int32_t (*get_int_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  int64_t (*get_long_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  float (*get_float_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  double (*get_double_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  SPVM_API_OBJECT* (*get_object_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t);
  void (*set_byte_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, int8_t);
  void (*set_short_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, int16_t);
  void (*set_int_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, int32_t);
  void (*set_long_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, int64_t);
  void (*set_float_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, float);
  void (*set_double_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, double);
  void (*set_object_field)(SPVM_API*, SPVM_API_OBJECT*, int32_t, SPVM_API_OBJECT*);
  int32_t (*get_sub_id)(SPVM_API*, const char*);
  void (*call_void_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  int8_t (*call_byte_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  int16_t (*call_short_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  int32_t (*call_int_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  int64_t (*call_long_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  float (*call_float_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  double (*call_double_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  SPVM_API_OBJECT* (*call_object_sub)(SPVM_API* api, int32_t sub_id, SPVM_API_VALUE* args);
  SPVM_API_OBJECT* (*new_byte_array)(SPVM_API*, int32_t);
  SPVM_API_OBJECT* (*new_short_array)(SPVM_API*, int32_t);
  SPVM_API_OBJECT* (*new_int_array)(SPVM_API*, int32_t);
  SPVM_API_OBJECT* (*new_long_array)(SPVM_API*, int32_t);
  SPVM_API_OBJECT* (*new_float_array)(SPVM_API*, int32_t);
  SPVM_API_OBJECT* (*new_double_array)(SPVM_API*, int32_t);
  SPVM_API_OBJECT* (*new_object_array)(SPVM_API*, int32_t, int32_t);
  SPVM_API_OBJECT* (*new_byte_array_string)(SPVM_API* api, const char* string);
  SPVM_API_OBJECT* (*get_exception)(SPVM_API* api);
  void (*set_exception)(SPVM_API* api, SPVM_API_OBJECT* exception);
  int32_t (*get_ref_count)(SPVM_API* api, SPVM_API_OBJECT* base_object);
  void (*inc_ref_count)(SPVM_API* api, SPVM_API_OBJECT* base_object);
  void (*dec_ref_count)(SPVM_API* api, SPVM_API_OBJECT* base_object);
  void (*inc_dec_ref_count)(SPVM_API* api, SPVM_API_OBJECT* base_object);
  int32_t (*get_objects_count)(SPVM_API* api);
  SPVM_API_RUNTIME* (*get_runtime)(SPVM_API* api);