https://github.com/robloach/wren-bind
https://github.com/robloach/wren-bind
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/robloach/wren-bind
- Owner: RobLoach
- License: mit
- Created: 2021-05-20T06:37:11.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-21T22:33:54.000Z (about 5 years ago)
- Last Synced: 2025-06-02T01:41:36.774Z (about 1 year ago)
- Language: C
- Size: 12.7 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wren-bind
Provides a registry to easily bind multiple compiled Wren modules.
## Usage
1. This is a single-header library, so define `WREN_BIND_IMPLEMENTATION` in one of your `.c` source files when including *wren-bind.h*:
``` c
#define WREN_BIND_IMPLEMENTATION
#include "wren-bind.h"
```
2. Define your modules with the `WrenModule` struct:
``` c
void FooBar(WrenVM* vm) {
double a = wrenGetSlotDouble(vm, 1);
double b = wrenGetSlotDouble(vm, 2);
wrenSetSlotDouble(vm, 0, a + b);
}
WrenForeignMethodFn FooModuleMethod(WrenVM* vm, const char* module,
const char* className, bool isStatic, const char* signature) {
if (strcmp(module, "Foo") == 0) {
if (strcmp(className, "Foo") == 0) {
if (isStatic && strcmp(signature, "bar(_,_)") == 0) {
return FooBar;
}
}
}
return NULL;
}
WrenModule Foo() {
WrenModule module;
module.name = "Foo";
module.code = "\n\
class Foo {\
foreign static bar(a, b)\
}\
";
module.methodFunction = FooModuleMethod;
module.classFunction = NULL;
return module;
}
```
3. Register the module to the VM using the `wrenBind()` method:
``` c
WrenVM* vm = wrenNewVM(&config);
WrenModule foo = Foo();
wrenBind(vm, &foo);
```
4. Alternatively, you can bind the module by binding the parameters with `wrenBindModule()`:
``` c
WrenVM* vm = wrenNewVM(&config);
wrenBindModule(vm, "Foo", code, FooModuleMethod, NULL);
```
5. Once bound, you'll be able to use the Foo module from Wren:
``` js
import "Foo" for Foo
System.print(Foo.bar(5, 8))
```
## Notes
- Overrides `vm.bindForeignMethodFn` to manage multiple modules for foreign methods
- Overrides `vm.bindForeignClassFn` to manage multiple modules for classes
- Stores module data within `vm.config.userData`
## License
*wren-bind* is licensed under the [MIT License](LICENSE).