REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 10-Aug-2009 Edit History  

REBOL 3 Concepts: Plugins: Example Plugins

Contents

General format

The examples below use this standard plugin source code format:

#include "reb-c.h"
#include "reb-plugin.h"

const char *init_block =
    <REBOL code goes here>
;

RPIEXT const char *RPI_Init(int opts, RPILIB *lib) {
    RPI = lib;
    if (lib->version == RPI_VERSION) return init_block;
    return 0;
}

RPIEXT int RPI_Call(int cmd, RPIFRM *frm) {
    <C command code goes here>
}

Notes:

For example, if the REBOL code is:

REBOL [
    Title: "Add two integers"
    Type: plugin
    Export: [addi]
]
addi: command [i1 [integer!] i2 [integer!]

and the C code is:

RPIEXT int RPI_Call(int cmd, RPIFRM *frm) {
    RPA_INT64(frm,1) = RPA_INT64(frm, 1) + RPA_INT64(frm, 2);\
    return RPR_VALUE;
}

The plugin source code would be:

#include "reb-plugin.h"

const char *init_block =
"REBOL [\n"
    "Title: {Add two integers}\n"
    "Type: plugin\n"
    "Export: [addi]\n"
"]\n"
"addi: command [i1 [integer!] i2 [integer!]\n"
;

RPIEXT const char *RPI_Init(int opts, RPILIB *lib) {
    RPI = lib;
    if (lib->version == RPI_VERSION) return init_block;
    return 0;
}

RPIEXT int RPI_Call(int cmd, RPIFRM *frm) {
    RPA_INT64(frm,1) = RPA_INT64(frm, 1) + RPA_INT64(frm, 2);\
    return RPR_VALUE;
}

Helper script to build the init block

Once you go beyond a simple init block, it gets tedious to maintain it as C strings. To make it easier to build the init_block text, we provide the make-plugin-data.r script.

Using this script will convert the plugin .r source file into a C data statement (as UTF-8). It will also create the exports block and will generate an enum constant for each command.

#include "reb-c.h"
#include "reb-plugin.h"

#include "plugin-data.h"

RPIEXT const char *RPI_Init(int opts, RPILIB *lib) {
    RPI = lib;
    if (lib->version == RPI_VERSION) return init_block;
    return 0;
}

RPIEXT int RPI_Call(int cmd, RPIFRM *frm) {
switch (cmd) {
case CMD_MY_CMD1:
    ...
case CMD_MY_CMD2:
    ...

Returning different values

examples

Math functions

examples

String functions

examples

Block functions

examples


  TOC < Back Next > REBOL WIP Wiki Feedback Admin