Skip to content

Instantly share code, notes, and snippets.

@masaori335
Created May 19, 2012 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaori335/2730090 to your computer and use it in GitHub Desktop.
Save masaori335/2730090 to your computer and use it in GitHub Desktop.
An Example of Gauche Extension Module
#include "example.h"
ScmClass* ExampleClass;
static void example_class_print (ScmObj obj, ScmPort* out, ScmWriteContext* ctx)
{
struct example* e = EXAMPLE_UNBOX(obj);
Scm_Printf(out, "#<example-class \"%p\">", e);
}
static void example_class_cleanup (ScmObj obj)
{
struct example* e = EXAMPLE_UNBOX(obj);
free(e);
}
/*
* Module initialization function.
*/
extern void Scm_Init_examplelib (ScmModule*);
void Scm_Init_example (void)
{
ScmModule *mod;
/* Register this DSO to Gauche */
SCM_INIT_EXTENSION(example);
/* Create the module if it doesn't exist yet. */
mod = SCM_MODULE(SCM_FIND_MODULE ("example", TRUE));
ExampleClass =
Scm_MakeForeignPointerClass(mod,
"<example-class>",
example_class_print,
example_class_cleanup,
SCM_FOREIGN_POINTER_KEEP_IDENTITY | SCM_FOREIGN_POINTER_MAP_NULL);
/* Register stub-generated procedures */
Scm_Init_examplelib (mod);
}
/* Prologue */
#ifndef GAUCHE_EXAMPLE_H
#define GAUCHE_EXAMPLE_H
#include <gauche.h>
#include <gauche/extend.h>
SCM_DECL_BEGIN
struct example {
int x;
};
extern ScmClass *ExampleClass;
#define EXAMPLE_P(obj) SCM_XTYPEP (obj, ExampleClass)
#define EXAMPLE_UNBOX(obj) SCM_FOREIGN_POINTER_REF (struct example*, obj)
#define EXAMPLE_BOX(obj) Scm_MakeForeignPointer (ExampleClass, obj)
/* Epilogue */
SCM_DECL_END
#endif /* GAUCHE_EXAMPLE_H */
(define-module example
(export <example-class>
))
(select-module example)
;; Loads extension
(dynamic-load "example")
"
#include \"example.h\"
"
(define-type <example-class> "struct example*" "example class" "EXAMPLE_P" "EXAMPLE_UNBOX" "EXAMPLE_BOX")
;; Local variables:
;; mode: scheme
;; end:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment