Add API to call Wren method from C code.

This gives you a simple, efficient way to invoke a method on
some Wren object from C code, passing in arguments.

The basic API is in place and works, but there's still lots to do:

- Lots of error handling.
- Documentation.
- Tests!
This commit is contained in:
Bob Nystrom
2015-02-28 13:31:15 -08:00
parent 6b05610c6a
commit 876c2d9208
8 changed files with 245 additions and 16 deletions
+33
View File
@@ -6,6 +6,11 @@
typedef struct WrenVM WrenVM;
// A handle to a method, bound to a receiver.
//
// This is used to call a Wren method on some object from C code.
typedef struct WrenMethod WrenMethod;
// A generic allocation function that handles all explicit memory management
// used by Wren. It's used like so:
//
@@ -118,6 +123,34 @@ void wrenFreeVM(WrenVM* vm);
WrenInterpretResult wrenInterpret(WrenVM* vm, const char* sourcePath,
const char* source);
// Creates a handle that can be used to invoke a method with [signature] on the
// object in [module] currently stored in top-level [variable].
//
// This handle can be used repeatedly to directly invoke that method from C
// code using [wrenCall].
//
// When done with this handle, it must be released by calling
// [wrenReleaseMethod].
WrenMethod* wrenGetMethod(WrenVM* vm, const char* module, const char* variable,
const char* signature);
// Calls [method], passing in a series of arguments whose types must match the
// specifed [argTypes]. This is a string where each character identifies the
// type of a single argument, in orde. The allowed types are:
//
// - "b" - A C `int` converted to a Wren Bool.
// - "d" - A C `double` converted to a Wren Num.
// - "i" - A C `int` converted to a Wren Num.
// - "s" - A C null-terminated `const char*` converted to a Wren String. Wren
// will allocate its own string and copy the characters from this, so
// you don't have to worry about the lifetime of the string you pass to
// Wren.
void wrenCall(WrenVM* vm, WrenMethod* method, const char* argTypes, ...);
// Releases memory associated with [method]. After calling this, [method] can
// no longer be used.
void wrenReleaseMethod(WrenVM* vm, WrenMethod* method);
// TODO: Figure out how these interact with modules.
// Defines a foreign method implemented by the host application. Looks for a