1
0
forked from Mirror/wren
Files
wren/test/api/call_calls_foreign.c
Bob Nystrom a5147aa2d9 Add a limited form of re-entrant calls.
This doesn't let you arbitrarily call back into the VM from within
foreign methods. I'm still not sure if that's even a good idea since
God knows what that would mean if you switch fibers while doing that.

But this does allow the very important use case of being able to call
a foreign method from within a call to wrenCall(). In other words,
foreign methods need to always be leaf calls on the call stack, but the
root of that stack can now come from runInterpreter() or wrenCall().

Fix #510.
2019-02-08 17:09:39 -08:00

45 lines
1.1 KiB
C

#include <stdio.h>
#include <string.h>
#include "wren.h"
static void api(WrenVM *vm) {
// Grow the slot array. This should trigger the stack to be moved.
wrenEnsureSlots(vm, 10);
wrenSetSlotNewList(vm, 0);
for (int i = 1; i < 10; i++)
{
wrenSetSlotDouble(vm, i, i);
wrenInsertInList(vm, 0, -1, i);
}
}
WrenForeignMethodFn callCallsForeignBindMethod(const char* signature)
{
if (strcmp(signature, "static CallCallsForeign.api()") == 0) return api;
return NULL;
}
void callCallsForeignRunTests(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "./test/api/call_calls_foreign", "CallCallsForeign", 0);
WrenHandle* apiClass = wrenGetSlotHandle(vm, 0);
WrenHandle *call = wrenMakeCallHandle(vm, "call(_)");
wrenEnsureSlots(vm, 2);
wrenSetSlotHandle(vm, 0, apiClass);
wrenSetSlotString(vm, 1, "parameter");
printf("slots before %d\n", wrenGetSlotCount(vm));
wrenCall(vm, call);
// We should have a single slot count for the return.
printf("slots after %d\n", wrenGetSlotCount(vm));
wrenReleaseHandle(vm, call);
wrenReleaseHandle(vm, apiClass);
}