Files
Toy/source/toy_function.c
Kayne Ruse 88100b128a Implemented native C functions called from Toy scripts
There's only example functions for now, but I'll add type-specific
functions later.
2026-04-16 21:14:42 +10:00

30 lines
941 B
C

#include "toy_function.h"
Toy_Function* Toy_createFunctionFromBytecode(Toy_Bucket** bucketHandle, unsigned char* bytecode, Toy_Scope* parentScope) {
Toy_Function* fn = (Toy_Function*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Function));
fn->type = TOY_FUNCTION_CUSTOM;
fn->bytecode.code = bytecode;
fn->bytecode.parentScope = parentScope;
Toy_private_incrementScopeRefCount(fn->bytecode.parentScope);
return fn;
}
Toy_Function* Toy_createFunctionFromCallback(Toy_Bucket** bucketHandle, Toy_nativeCallback callback) {
Toy_Function* fn = (Toy_Function*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Function));
fn->type = TOY_FUNCTION_NATIVE;
fn->native.callback = callback;
return fn;
}
TOY_API void Toy_freeFunction(Toy_Function* fn) {
if (fn->type == TOY_FUNCTION_CUSTOM) {
Toy_private_decrementScopeRefCount(fn->bytecode.parentScope);
}
else if (fn->type == TOY_FUNCTION_NATIVE) {
fn->native.callback = NULL;
}
}