mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 06:38:45 +01:00
- wrenGetArgumentCount() -> wrenGetSlotCount() - wrenGetArgument___() -> wrenGetSlot___() Also, the get functions assert that the value is the right type instead of checking at runtime. This puts the onus on the caller to be safe, but maximizes performance.
113 lines
2.6 KiB
C
113 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "foreign_class.h"
|
|
|
|
static int finalized = 0;
|
|
|
|
static void apiFinalized(WrenVM* vm)
|
|
{
|
|
wrenReturnDouble(vm, finalized);
|
|
}
|
|
|
|
static void counterAllocate(WrenVM* vm)
|
|
{
|
|
double* value = (double*)wrenAllocateForeign(vm, sizeof(double));
|
|
*value = 0;
|
|
}
|
|
|
|
static void counterIncrement(WrenVM* vm)
|
|
{
|
|
double* value = (double*)wrenGetSlotForeign(vm, 0);
|
|
double increment = wrenGetSlotDouble(vm, 1);
|
|
|
|
*value += increment;
|
|
}
|
|
|
|
static void counterValue(WrenVM* vm)
|
|
{
|
|
double value = *(double*)wrenGetSlotForeign(vm, 0);
|
|
wrenReturnDouble(vm, value);
|
|
}
|
|
|
|
static void pointAllocate(WrenVM* vm)
|
|
{
|
|
double* coordinates = (double*)wrenAllocateForeign(vm, sizeof(double[3]));
|
|
|
|
// This gets called by both constructors, so sniff the slot count to see
|
|
// which one was invoked.
|
|
if (wrenGetSlotCount(vm) == 1)
|
|
{
|
|
coordinates[0] = 0.0;
|
|
coordinates[1] = 0.0;
|
|
coordinates[2] = 0.0;
|
|
}
|
|
else
|
|
{
|
|
coordinates[0] = wrenGetSlotDouble(vm, 1);
|
|
coordinates[1] = wrenGetSlotDouble(vm, 2);
|
|
coordinates[2] = wrenGetSlotDouble(vm, 3);
|
|
}
|
|
}
|
|
|
|
static void pointTranslate(WrenVM* vm)
|
|
{
|
|
double* coordinates = (double*)wrenGetSlotForeign(vm, 0);
|
|
coordinates[0] += wrenGetSlotDouble(vm, 1);
|
|
coordinates[1] += wrenGetSlotDouble(vm, 2);
|
|
coordinates[2] += wrenGetSlotDouble(vm, 3);
|
|
}
|
|
|
|
static void pointToString(WrenVM* vm)
|
|
{
|
|
double* coordinates = (double*)wrenGetSlotForeign(vm, 0);
|
|
char result[100];
|
|
sprintf(result, "(%g, %g, %g)",
|
|
coordinates[0], coordinates[1], coordinates[2]);
|
|
wrenReturnString(vm, result, (int)strlen(result));
|
|
}
|
|
|
|
static void resourceAllocate(WrenVM* vm)
|
|
{
|
|
wrenAllocateForeign(vm, 0);
|
|
}
|
|
|
|
static void resourceFinalize(WrenVM* vm)
|
|
{
|
|
finalized++;
|
|
}
|
|
|
|
WrenForeignMethodFn foreignClassBindMethod(const char* signature)
|
|
{
|
|
if (strcmp(signature, "static ForeignClass.finalized") == 0) return apiFinalized;
|
|
if (strcmp(signature, "Counter.increment(_)") == 0) return counterIncrement;
|
|
if (strcmp(signature, "Counter.value") == 0) return counterValue;
|
|
if (strcmp(signature, "Point.translate(_,_,_)") == 0) return pointTranslate;
|
|
if (strcmp(signature, "Point.toString") == 0) return pointToString;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void foreignClassBindClass(
|
|
const char* className, WrenForeignClassMethods* methods)
|
|
{
|
|
if (strcmp(className, "Counter") == 0)
|
|
{
|
|
methods->allocate = counterAllocate;
|
|
return;
|
|
}
|
|
|
|
if (strcmp(className, "Point") == 0)
|
|
{
|
|
methods->allocate = pointAllocate;
|
|
return;
|
|
}
|
|
|
|
if (strcmp(className, "Resource") == 0)
|
|
{
|
|
methods->allocate = resourceAllocate;
|
|
methods->finalize = resourceFinalize;
|
|
return;
|
|
}
|
|
}
|