Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Created April 15, 2016 17:05
Show Gist options
  • Save H2CO3/848e479cfb433796ca0dd939aefbc96e to your computer and use it in GitHub Desktop.
Save H2CO3/848e479cfb433796ca0dd939aefbc96e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <spn/ctx.h>
typedef struct {
SpnContext *ctx;
SpnFunction *fn;
SpnValue arg;
} CallbackData;
static void SDL_somefunc(void (*callback)(void *), void *data)
{
callback(data);
}
static void c_callback(void *data)
{
CallbackData *cb_data = data;
spn_ctx_callfunc(cb_data->ctx, cb_data->fn, NULL, 1, &cb_data->arg);
}
static int spn_sdl_somefunc(SpnValue *ret, int argc, SpnValue argv[], void *ctx)
{
SpnFunction *fn = spn_objvalue(&argv[0]);
CallbackData cb_data = {
.ctx = ctx,
.fn = fn,
.arg = argv[1]
};
SDL_somefunc(c_callback, &cb_data);
return 0;
}
int main()
{
SpnContext ctx;
spn_ctx_init(&ctx);
SpnExtFunc fn = { "somefunc", spn_sdl_somefunc };
spn_ctx_addlib_cfuncs(&ctx, NULL, &fn, 1);
static void *userinfo = &userinfo;
SpnExtValue uinf = { "fake_userinfo", spn_makeweakuserinfo(userinfo) };
spn_ctx_addlib_values(&ctx, NULL, &uinf, 1);
spn_ctx_execstring(&ctx, "somefunc(fn (arg) { print(\"arg from within Sparkling: \", arg); }, fake_userinfo);", NULL);
printf("actual address of fake_userinfo from C: %p\n", userinfo);
spn_ctx_free(&ctx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment