feat: add foreign class support with allocation, construct opcodes, and CLI callback wiring
Implement the initial infrastructure for foreign classes in the Wren VM, including new opcodes (FOREIGN_CONSTRUCT, FOREIGN_CLASS), a WrenBindForeignClassFn callback type in the public API, and updated CLI vm.c to store and forward foreign binding callbacks via setForeignCallbacks(). The compiler now tracks whether a class is foreign via a new isForeign flag in ClassCompiler, emits CODE_FOREIGN_CONSTRUCT instead of CODE_CONSTRUCT for foreign class constructors, and errors on field definitions inside foreign classes. The debug dump and opcode header gain entries for the new opcodes, and wrenBindSuperclass handles the -1 numFields sentinel for foreign classes to prevent field inheritance from superclasses.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "foreign_class.h"
|
||||
|
||||
static void counterAllocate(WrenVM* vm)
|
||||
{
|
||||
double* value = (double*)wrenAllocateForeign(vm, sizeof(double));
|
||||
*value = 0;
|
||||
}
|
||||
|
||||
static void counterIncrement(WrenVM* vm)
|
||||
{
|
||||
double* value = (double*)wrenGetArgumentForeign(vm, 0);
|
||||
double increment = wrenGetArgumentDouble(vm, 1);
|
||||
|
||||
*value += increment;
|
||||
}
|
||||
|
||||
static void counterValue(WrenVM* vm)
|
||||
{
|
||||
double value = *(double*)wrenGetArgumentForeign(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 argument count to see
|
||||
// which one was invoked.
|
||||
if (wrenGetArgumentCount(vm) == 1)
|
||||
{
|
||||
coordinates[0] = 0.0;
|
||||
coordinates[1] = 0.0;
|
||||
coordinates[2] = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
coordinates[0] = wrenGetArgumentDouble(vm, 1);
|
||||
coordinates[1] = wrenGetArgumentDouble(vm, 2);
|
||||
coordinates[2] = wrenGetArgumentDouble(vm, 3);
|
||||
}
|
||||
}
|
||||
|
||||
static void pointTranslate(WrenVM* vm)
|
||||
{
|
||||
double* coordinates = (double*)wrenGetArgumentForeign(vm, 0);
|
||||
coordinates[0] += wrenGetArgumentDouble(vm, 1);
|
||||
coordinates[1] += wrenGetArgumentDouble(vm, 2);
|
||||
coordinates[2] += wrenGetArgumentDouble(vm, 3);
|
||||
}
|
||||
|
||||
static void pointToString(WrenVM* vm)
|
||||
{
|
||||
double* coordinates = (double*)wrenGetArgumentForeign(vm, 0);
|
||||
char result[100];
|
||||
sprintf(result, "(%g, %g, %g)",
|
||||
coordinates[0], coordinates[1], coordinates[2]);
|
||||
wrenReturnString(vm, result, (int)strlen(result));
|
||||
}
|
||||
|
||||
WrenForeignMethodFn foreignClassBindMethod(const char* signature)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user