static void fileReadBytesCallback(uv_fs_t* request) { if (handleRequestError(request)) return; FileRequestData* data = (FileRequestData*)request->data; uv_buf_t buffer = data->buffer; size_t count = request->result; // 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. schedulerResume(freeRequest(request), true); wrenSetSlotBytes(getVM(), 2, buffer.base, count); schedulerFinishResume(); // TODO: Likewise, freeing this after we resume is lame. free(buffer.base); }
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); }
static void setSlots(WrenVM* vm) { WrenHandle* handle = wrenGetSlotHandle(vm, 1); wrenSetSlotBool(vm, 1, true); wrenSetSlotBytes(vm, 2, "by\0te", 5); wrenSetSlotDouble(vm, 3, 1.5); wrenSetSlotString(vm, 4, "str"); // TODO: wrenSetSlotNull(). // Read the slots back to make sure they were set correctly. bool result = true; if (wrenGetSlotBool(vm, 1) != true) result = false; int length; const char* bytes = wrenGetSlotBytes(vm, 2, &length); if (length != 5) result = false; if (memcmp(bytes, "by\0te", length) != 0) result = false; if (wrenGetSlotDouble(vm, 3) != 1.5) result = false; if (strcmp(wrenGetSlotString(vm, 4), "str") != 0) result = false; if (result) { // Move the value into the return position. wrenSetSlotHandle(vm, 0, handle); } else { // If anything failed, return false. wrenSetSlotBool(vm, 0, false); } wrenReleaseHandle(vm, handle); }
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); }