void schedulerResume(WrenValue* fiber, bool hasArgument) { WrenVM* vm = getVM(); wrenEnsureSlots(vm, 2 + (hasArgument ? 1 : 0)); wrenSetSlotValue(vm, 0, schedulerClass); wrenSetSlotValue(vm, 1, fiber); wrenReleaseValue(vm, fiber); // If we don't need to wait for an argument to be stored on the stack, resume // it now. if (!hasArgument) resume(resume1); }
static void stdinReadCallback(uv_stream_t* stream, ssize_t numRead, const uv_buf_t* buffer) { WrenVM* vm = getVM(); if (stdinClass == NULL) { wrenEnsureSlots(vm, 1); wrenGetVariable(vm, "io", "Stdin", 0); stdinClass = wrenGetSlotValue(vm, 0); } if (stdinOnData == NULL) { stdinOnData = wrenMakeCallHandle(vm, "onData_(_)"); } // If stdin was closed, send null to let io.wren know. if (numRead == UV_EOF) { wrenEnsureSlots(vm, 2); wrenSetSlotValue(vm, 0, stdinClass); wrenSetSlotNull(vm, 1); wrenCall(vm, stdinOnData); shutdownStdin(); return; } // TODO: Handle other errors. // TODO: Having to copy the bytes here is a drag. It would be good if Wren's // embedding API supported a way to *give* it bytes that were previously // allocated using Wren's own allocator. wrenEnsureSlots(vm, 2); wrenSetSlotValue(vm, 0, stdinClass); wrenSetSlotBytes(vm, 1, buffer->base, numRead); wrenCall(vm, stdinOnData); // TODO: Likewise, freeing this after we resume is lame. free(buffer->base); }
void callRunTests(WrenVM* vm) { wrenEnsureSlots(vm, 1); wrenGetVariable(vm, "main", "Call", 0); WrenValue* callClass = wrenGetSlotValue(vm, 0); WrenValue* noParams = wrenMakeCallHandle(vm, "noParams"); WrenValue* zero = wrenMakeCallHandle(vm, "zero()"); WrenValue* one = wrenMakeCallHandle(vm, "one(_)"); WrenValue* two = wrenMakeCallHandle(vm, "two(_,_)"); // Different arity. wrenEnsureSlots(vm, 1); wrenSetSlotValue(vm, 0, callClass); wrenCall(vm, noParams); wrenEnsureSlots(vm, 1); wrenSetSlotValue(vm, 0, callClass); wrenCall(vm, zero); wrenEnsureSlots(vm, 2); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotDouble(vm, 1, 1.0); wrenCall(vm, one); wrenEnsureSlots(vm, 3); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotDouble(vm, 1, 1.0); wrenSetSlotDouble(vm, 2, 2.0); wrenCall(vm, two); // Returning a value. WrenValue* getValue = wrenMakeCallHandle(vm, "getValue()"); wrenEnsureSlots(vm, 1); wrenSetSlotValue(vm, 0, callClass); wrenCall(vm, getValue); WrenValue* value = wrenGetSlotValue(vm, 0); // Different argument types. wrenEnsureSlots(vm, 3); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotBool(vm, 1, true); wrenSetSlotBool(vm, 2, false); wrenCall(vm, two); wrenEnsureSlots(vm, 3); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotDouble(vm, 1, 1.2); wrenSetSlotDouble(vm, 2, 3.4); wrenCall(vm, two); wrenEnsureSlots(vm, 3); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotString(vm, 1, "string"); wrenSetSlotString(vm, 2, "another"); wrenCall(vm, two); wrenEnsureSlots(vm, 3); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotNull(vm, 1); wrenSetSlotValue(vm, 2, value); wrenCall(vm, two); // Truncate a string, or allow null bytes. wrenEnsureSlots(vm, 3); wrenSetSlotValue(vm, 0, callClass); wrenSetSlotBytes(vm, 1, "string", 3); wrenSetSlotBytes(vm, 2, "b\0y\0t\0e", 7); wrenCall(vm, two); // Call ignores with extra temporary slots on stack. wrenEnsureSlots(vm, 10); wrenSetSlotValue(vm, 0, callClass); for (int i = 1; i < 10; i++) { wrenSetSlotDouble(vm, i, i * 0.1); } wrenCall(vm, one); wrenReleaseValue(vm, callClass); wrenReleaseValue(vm, noParams); wrenReleaseValue(vm, zero); wrenReleaseValue(vm, one); wrenReleaseValue(vm, two); wrenReleaseValue(vm, getValue); wrenReleaseValue(vm, value); }