Skip to content

Instantly share code, notes, and snippets.

@earl

earl/sample.c Secret

Created August 13, 2010 16:31
Show Gist options
  • Save earl/bc820cc3eb301c79c1ef to your computer and use it in GitHub Desktop.
Save earl/bc820cc3eb301c79c1ef to your computer and use it in GitHub Desktop.
/*
$ gcc -shared -fpack-struct=4 -Isrc/include/ -o sample.so sample.c
$ ./r3core -qs
Loading boot extensions...
>> import %./sample.so
>> add-mul 10 20 30
== 900
*/
#include "reb-c.h"
#include "reb-ext.h"
const char *init_block =
"REBOL [\n"
"Title: {Example extension}\n"
"Name: example\n"
"Type: extension\n"
"Exports: [add-mul]\n"
"]\n"
"add-mul: command [{Add and multiply integers.} a b c]\n"
;
RXIEXT const char *RX_Init(int opts, RXILIB *lib) {
RXI = lib;
if (lib->version == RXI_VERSION) return init_block;
return 0;
}
RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *data) {
RXA_INT64(frm, 1) =
(RXA_INT64(frm, 1) + RXA_INT64(frm, 2)) *
RXA_INT64(frm, 3);
return RXR_VALUE;
}
/*
$ gcc -shared -fpack-struct=4 -Isrc/include/ -o sample2.so sample2.c
$ ./r3core -qs
Loading boot extensions...
>> import %./sample2.so
>> sum-chars "testing"
== 766
*/
#include "reb-c.h"
#include "reb-ext.h"
const char *init_block =
"REBOL [\n"
"Title: {Another example extension}\n"
"Name: example2\n"
"Type: extension\n"
"Exports: [\n"
"sum-chars\n"
"]\n"
"]\n"
"sum-chars: command [{Checksum a string.} str [string!]]\n"
;
RXIEXT const char *RX_Init(int opts, RXILIB *lib) {
RXI = lib;
if (lib->version == RXI_VERSION) return init_block;
return 0;
}
RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *data) {
i32 idx, tail;
i64 sum = 0;
REBSER *ser;
ser = RXA_SERIES(frm, 1);
idx = RXA_INDEX(frm, 1);
tail = RXI_SERIES_INFO(ser, RXI_SER_TAIL);
for (; idx < tail; ++idx) {
sum += RXI_GET_CHAR(ser, idx);
}
RXA_INT64(frm, 1) = sum;
RXA_TYPE(frm, 1) = RXT_INTEGER;
return RXR_VALUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment