feat: add user_data API test and reorder WrenErrorFn vm parameter to first position
Add a new test suite for user data get/set operations on WrenVM, including a Wren script that validates default NULL userData, retrieval after VM creation, and mutation via wrenSetUserData. Also reorder the vm parameter in WrenErrorFn callback signature and all its call sites to match the convention used by other callback types, updating the typedef, the reportError implementation, and all invocations in the compiler and debug modules. Include the new source and header files in the Xcode project build configuration.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include "reset_stack_after_call_abort.h"
|
||||
#include "reset_stack_after_foreign_construct.h"
|
||||
#include "slots.h"
|
||||
#include "user_data.h"
|
||||
|
||||
// The name of the currently executing API test.
|
||||
const char* testName;
|
||||
@@ -59,6 +60,9 @@ static WrenForeignMethodFn bindForeignMethod(
|
||||
|
||||
method = slotsBindMethod(fullName);
|
||||
if (method != NULL) return method;
|
||||
|
||||
method = userDataBindMethod(fullName);
|
||||
if (method != NULL) return method;
|
||||
|
||||
fprintf(stderr,
|
||||
"Unknown foreign method '%s' for test '%s'\n", fullName, testName);
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "user_data.h"
|
||||
|
||||
static const char* data = "my user data";
|
||||
static const char* otherData = "other user data";
|
||||
|
||||
static void test(WrenVM* vm)
|
||||
{
|
||||
WrenConfiguration configuration;
|
||||
wrenInitConfiguration(&configuration);
|
||||
|
||||
// Should default to NULL.
|
||||
if (configuration.userData != NULL)
|
||||
{
|
||||
wrenSetSlotBool(vm, 0, false);
|
||||
return;
|
||||
}
|
||||
|
||||
configuration.userData = (void*)data;
|
||||
|
||||
WrenVM* otherVM = wrenNewVM(&configuration);
|
||||
|
||||
// Should be able to get it.
|
||||
if (wrenGetUserData(otherVM) != data)
|
||||
{
|
||||
wrenSetSlotBool(vm, 0, false);
|
||||
wrenFreeVM(otherVM);
|
||||
return;
|
||||
}
|
||||
|
||||
// Should be able to set it.
|
||||
wrenSetUserData(otherVM, (void*)otherData);
|
||||
|
||||
if (wrenGetUserData(otherVM) != otherData)
|
||||
{
|
||||
wrenSetSlotBool(vm, 0, false);
|
||||
wrenFreeVM(otherVM);
|
||||
return;
|
||||
}
|
||||
|
||||
wrenSetSlotBool(vm, 0, true);
|
||||
wrenFreeVM(otherVM);
|
||||
}
|
||||
|
||||
WrenForeignMethodFn userDataBindMethod(const char* signature)
|
||||
{
|
||||
if (strcmp(signature, "static UserData.test") == 0) return test;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "wren.h"
|
||||
|
||||
WrenForeignMethodFn userDataBindMethod(const char* signature);
|
||||
@@ -0,0 +1,5 @@
|
||||
class UserData {
|
||||
foreign static test
|
||||
}
|
||||
|
||||
System.print(UserData.test) // expect: true
|
||||
Reference in New Issue
Block a user