Skip to content

Instantly share code, notes, and snippets.

@frankhale
Last active June 30, 2020 03:13
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 frankhale/6732dfcacd8115a5039fdf4b1b666d1f to your computer and use it in GitHub Desktop.
Save frankhale/6732dfcacd8115a5039fdf4b1b666d1f to your computer and use it in GitHub Desktop.
#include "wren.h"
#include <stdio.h>
#include <string.h>
void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
void fooBar(WrenVM* vm) {
WrenType type = wrenGetSlotType(vm, 1);
if(type == WREN_TYPE_STRING) {
const char* text = wrenGetSlotString(vm, 1);
printf("fooBar: %s", text);
} else {
printf("bar requires a string\n");
}
}
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "my_module") == 0)
{
if (strcmp(className, "Foo") == 0)
{
if (isStatic && strcmp(signature, "bar(_)") == 0)
{
return fooBar;
}
}
}
}
void errorFn(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* message) {
printf("error type: %d\n", type);
printf("module: %s\n", module);
printf("line: %d\n", line);
printf("message: %s\n", message);
}
int main() {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.bindForeignMethodFn = &bindForeignMethod;
config.errorFn = &errorFn;
WrenVM* vm = wrenNewVM(&config);
WrenInterpretResult result = wrenInterpret(
vm,
"my_module",
"class Foo { foreign static bar(str) }\r\nFoo.bar(\"Hello, Wren World!\n\")");
printf("script result = %d\n", result);
wrenFreeVM(vm);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment