WrenValue -> WrenHandle.
This commit is contained in:
+28
-28
@@ -28,20 +28,20 @@
|
||||
|
||||
typedef struct sFileRequestData
|
||||
{
|
||||
WrenValue* fiber;
|
||||
WrenHandle* fiber;
|
||||
uv_buf_t buffer;
|
||||
} FileRequestData;
|
||||
|
||||
static const int stdinDescriptor = 0;
|
||||
|
||||
// Handle to the Stat class object.
|
||||
static WrenValue* statClass = NULL;
|
||||
static WrenHandle* statClass = NULL;
|
||||
|
||||
// Handle to the Stdin class object.
|
||||
static WrenValue* stdinClass = NULL;
|
||||
static WrenHandle* stdinClass = NULL;
|
||||
|
||||
// Handle to an onData_() method call. Called when libuv provides data on stdin.
|
||||
static WrenValue* stdinOnData = NULL;
|
||||
static WrenHandle* stdinOnData = NULL;
|
||||
|
||||
// The stream used to read from stdin. Initialized on the first read.
|
||||
static uv_stream_t* stdinStream = NULL;
|
||||
@@ -61,13 +61,13 @@ static void shutdownStdin()
|
||||
|
||||
if (stdinClass != NULL)
|
||||
{
|
||||
wrenReleaseValue(getVM(), stdinClass);
|
||||
wrenReleaseHandle(getVM(), stdinClass);
|
||||
stdinClass = NULL;
|
||||
}
|
||||
|
||||
if (stdinOnData != NULL)
|
||||
{
|
||||
wrenReleaseValue(getVM(), stdinOnData);
|
||||
wrenReleaseHandle(getVM(), stdinOnData);
|
||||
stdinOnData = NULL;
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ void ioShutdown()
|
||||
|
||||
if (statClass != NULL)
|
||||
{
|
||||
wrenReleaseValue(getVM(), statClass);
|
||||
wrenReleaseHandle(getVM(), statClass);
|
||||
statClass = NULL;
|
||||
}
|
||||
}
|
||||
@@ -94,7 +94,7 @@ static bool handleRequestError(uv_fs_t* request)
|
||||
if (request->result >= 0) return false;
|
||||
|
||||
FileRequestData* data = (FileRequestData*)request->data;
|
||||
WrenValue* fiber = (WrenValue*)data->fiber;
|
||||
WrenHandle* fiber = (WrenHandle*)data->fiber;
|
||||
|
||||
schedulerResumeError(fiber, uv_strerror((int)request->result));
|
||||
|
||||
@@ -105,7 +105,7 @@ static bool handleRequestError(uv_fs_t* request)
|
||||
}
|
||||
|
||||
// Allocates a new request that resumes [fiber] when it completes.
|
||||
uv_fs_t* createRequest(WrenValue* fiber)
|
||||
uv_fs_t* createRequest(WrenHandle* fiber)
|
||||
{
|
||||
uv_fs_t* request = (uv_fs_t*)malloc(sizeof(uv_fs_t));
|
||||
|
||||
@@ -119,10 +119,10 @@ uv_fs_t* createRequest(WrenValue* fiber)
|
||||
// Releases resources used by [request].
|
||||
//
|
||||
// Returns the fiber that should be resumed after [request] completes.
|
||||
WrenValue* freeRequest(uv_fs_t* request)
|
||||
WrenHandle* freeRequest(uv_fs_t* request)
|
||||
{
|
||||
FileRequestData* data = (FileRequestData*)request->data;
|
||||
WrenValue* fiber = data->fiber;
|
||||
WrenHandle* fiber = data->fiber;
|
||||
|
||||
free(data);
|
||||
uv_fs_req_cleanup(request);
|
||||
@@ -154,7 +154,7 @@ static void directoryListCallback(uv_fs_t* request)
|
||||
void directoryList(WrenVM* vm)
|
||||
{
|
||||
const char* path = wrenGetSlotString(vm, 1);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 2));
|
||||
|
||||
// TODO: Check return.
|
||||
uv_fs_scandir(getLoop(), request, path, 0, directoryListCallback);
|
||||
@@ -189,7 +189,7 @@ static void fileDeleteCallback(uv_fs_t* request)
|
||||
void fileDelete(WrenVM* vm)
|
||||
{
|
||||
const char* path = wrenGetSlotString(vm, 1);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 2));
|
||||
|
||||
// TODO: Check return.
|
||||
uv_fs_unlink(getLoop(), request, path, fileDeleteCallback);
|
||||
@@ -227,7 +227,7 @@ void fileOpen(WrenVM* vm)
|
||||
{
|
||||
const char* path = wrenGetSlotString(vm, 1);
|
||||
int flags = (int)wrenGetSlotDouble(vm, 2);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 3));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 3));
|
||||
|
||||
// TODO: Allow controlling access.
|
||||
uv_fs_open(getLoop(), request, path, mapFileFlags(flags), S_IRUSR | S_IWUSR,
|
||||
@@ -248,7 +248,7 @@ static void fileSizeCallback(uv_fs_t* request)
|
||||
void fileSizePath(WrenVM* vm)
|
||||
{
|
||||
const char* path = wrenGetSlotString(vm, 1);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 2));
|
||||
uv_fs_stat(getLoop(), request, path, fileSizeCallback);
|
||||
}
|
||||
|
||||
@@ -274,7 +274,7 @@ void fileClose(WrenVM* vm)
|
||||
// Mark it closed immediately.
|
||||
*foreign = -1;
|
||||
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 1));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 1));
|
||||
uv_fs_close(getLoop(), request, fd, fileCloseCallback);
|
||||
wrenSetSlotBool(vm, 0, false);
|
||||
}
|
||||
@@ -307,7 +307,7 @@ static void fileReadBytesCallback(uv_fs_t* request)
|
||||
|
||||
void fileReadBytes(WrenVM* vm)
|
||||
{
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 3));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 3));
|
||||
|
||||
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
||||
// TODO: Assert fd != -1.
|
||||
@@ -336,7 +336,7 @@ static void realPathCallback(uv_fs_t* request)
|
||||
void fileRealPath(WrenVM* vm)
|
||||
{
|
||||
const char* path = wrenGetSlotString(vm, 1);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 2));
|
||||
uv_fs_realpath(getLoop(), request, path, realPathCallback);
|
||||
}
|
||||
|
||||
@@ -353,11 +353,11 @@ static void statCallback(uv_fs_t* request)
|
||||
if (statClass == NULL)
|
||||
{
|
||||
wrenGetVariable(vm, "io", "Stat", 0);
|
||||
statClass = wrenGetSlotValue(vm, 0);
|
||||
statClass = wrenGetSlotHandle(vm, 0);
|
||||
}
|
||||
|
||||
// Create a foreign Stat object to store the stat struct.
|
||||
wrenSetSlotValue(vm, 2, statClass);
|
||||
wrenSetSlotHandle(vm, 2, statClass);
|
||||
wrenSetSlotNewForeign(vm, 2, 2, sizeof(uv_stat_t));
|
||||
|
||||
// Copy the stat data.
|
||||
@@ -371,14 +371,14 @@ static void statCallback(uv_fs_t* request)
|
||||
void fileStat(WrenVM* vm)
|
||||
{
|
||||
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 1));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 1));
|
||||
uv_fs_fstat(getLoop(), request, fd, statCallback);
|
||||
}
|
||||
|
||||
void fileSize(WrenVM* vm)
|
||||
{
|
||||
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 1));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 1));
|
||||
uv_fs_fstat(getLoop(), request, fd, fileSizeCallback);
|
||||
}
|
||||
|
||||
@@ -398,12 +398,12 @@ void fileWriteBytes(WrenVM* vm)
|
||||
int length;
|
||||
const char* bytes = wrenGetSlotBytes(vm, 1, &length);
|
||||
size_t offset = (size_t)wrenGetSlotDouble(vm, 2);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 3));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 3));
|
||||
|
||||
FileRequestData* data = (FileRequestData*)request->data;
|
||||
|
||||
data->buffer.len = length;
|
||||
// TODO: Instead of copying, just create a WrenValue for the byte string and
|
||||
// TODO: Instead of copying, just create a WrenHandle for the byte string and
|
||||
// hold on to it in the request until the write is done.
|
||||
// TODO: Handle allocation failure.
|
||||
data->buffer.base = (char*)malloc(length);
|
||||
@@ -416,7 +416,7 @@ void fileWriteBytes(WrenVM* vm)
|
||||
void statPath(WrenVM* vm)
|
||||
{
|
||||
const char* path = wrenGetSlotString(vm, 1);
|
||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
||||
uv_fs_t* request = createRequest(wrenGetSlotHandle(vm, 2));
|
||||
uv_fs_stat(getLoop(), request, path, statCallback);
|
||||
}
|
||||
|
||||
@@ -556,7 +556,7 @@ static void stdinReadCallback(uv_stream_t* stream, ssize_t numRead,
|
||||
{
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenGetVariable(vm, "io", "Stdin", 0);
|
||||
stdinClass = wrenGetSlotValue(vm, 0);
|
||||
stdinClass = wrenGetSlotHandle(vm, 0);
|
||||
}
|
||||
|
||||
if (stdinOnData == NULL)
|
||||
@@ -568,7 +568,7 @@ static void stdinReadCallback(uv_stream_t* stream, ssize_t numRead,
|
||||
if (numRead == UV_EOF)
|
||||
{
|
||||
wrenEnsureSlots(vm, 2);
|
||||
wrenSetSlotValue(vm, 0, stdinClass);
|
||||
wrenSetSlotHandle(vm, 0, stdinClass);
|
||||
wrenSetSlotNull(vm, 1);
|
||||
wrenCall(vm, stdinOnData);
|
||||
|
||||
@@ -582,7 +582,7 @@ static void stdinReadCallback(uv_stream_t* stream, ssize_t numRead,
|
||||
// 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);
|
||||
wrenSetSlotHandle(vm, 0, stdinClass);
|
||||
wrenSetSlotBytes(vm, 1, buffer->base, numRead);
|
||||
wrenCall(vm, stdinOnData);
|
||||
|
||||
|
||||
+15
-15
@@ -8,16 +8,16 @@
|
||||
#include "vm.h"
|
||||
|
||||
// A handle to the "Scheduler" class object. Used to call static methods on it.
|
||||
static WrenValue* schedulerClass;
|
||||
static WrenHandle* schedulerClass;
|
||||
|
||||
// This method resumes a fiber that is suspended waiting on an asynchronous
|
||||
// operation. The first resumes it with zero arguments, and the second passes
|
||||
// one.
|
||||
static WrenValue* resume1;
|
||||
static WrenValue* resume2;
|
||||
static WrenValue* resumeError;
|
||||
static WrenHandle* resume1;
|
||||
static WrenHandle* resume2;
|
||||
static WrenHandle* resumeError;
|
||||
|
||||
static void resume(WrenValue* method)
|
||||
static void resume(WrenHandle* method)
|
||||
{
|
||||
WrenInterpretResult result = wrenCall(getVM(), method);
|
||||
|
||||
@@ -34,20 +34,20 @@ void schedulerCaptureMethods(WrenVM* vm)
|
||||
{
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenGetVariable(vm, "scheduler", "Scheduler", 0);
|
||||
schedulerClass = wrenGetSlotValue(vm, 0);
|
||||
schedulerClass = wrenGetSlotHandle(vm, 0);
|
||||
|
||||
resume1 = wrenMakeCallHandle(vm, "resume_(_)");
|
||||
resume2 = wrenMakeCallHandle(vm, "resume_(_,_)");
|
||||
resumeError = wrenMakeCallHandle(vm, "resumeError_(_,_)");
|
||||
}
|
||||
|
||||
void schedulerResume(WrenValue* fiber, bool hasArgument)
|
||||
void schedulerResume(WrenHandle* fiber, bool hasArgument)
|
||||
{
|
||||
WrenVM* vm = getVM();
|
||||
wrenEnsureSlots(vm, 2 + (hasArgument ? 1 : 0));
|
||||
wrenSetSlotValue(vm, 0, schedulerClass);
|
||||
wrenSetSlotValue(vm, 1, fiber);
|
||||
wrenReleaseValue(vm, fiber);
|
||||
wrenSetSlotHandle(vm, 0, schedulerClass);
|
||||
wrenSetSlotHandle(vm, 1, fiber);
|
||||
wrenReleaseHandle(vm, fiber);
|
||||
|
||||
// If we don't need to wait for an argument to be stored on the stack, resume
|
||||
// it now.
|
||||
@@ -59,7 +59,7 @@ void schedulerFinishResume()
|
||||
resume(resume2);
|
||||
}
|
||||
|
||||
void schedulerResumeError(WrenValue* fiber, const char* error)
|
||||
void schedulerResumeError(WrenHandle* fiber, const char* error)
|
||||
{
|
||||
schedulerResume(fiber, true);
|
||||
wrenSetSlotString(getVM(), 2, error);
|
||||
@@ -72,8 +72,8 @@ void schedulerShutdown()
|
||||
if (schedulerClass == NULL) return;
|
||||
|
||||
WrenVM* vm = getVM();
|
||||
wrenReleaseValue(vm, schedulerClass);
|
||||
wrenReleaseValue(vm, resume1);
|
||||
wrenReleaseValue(vm, resume2);
|
||||
wrenReleaseValue(vm, resumeError);
|
||||
wrenReleaseHandle(vm, schedulerClass);
|
||||
wrenReleaseHandle(vm, resume1);
|
||||
wrenReleaseHandle(vm, resume2);
|
||||
wrenReleaseHandle(vm, resumeError);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
// on the stack and then call [schedulerFinishResume] to complete the call.
|
||||
//
|
||||
// Otherwise, the call resumes immediately. Releases [fiber] when called.
|
||||
void schedulerResume(WrenValue* fiber, bool hasArgument);
|
||||
void schedulerResume(WrenHandle* fiber, bool hasArgument);
|
||||
|
||||
void schedulerFinishResume();
|
||||
void schedulerResumeError(WrenValue* fiber, const char* error);
|
||||
void schedulerResumeError(WrenHandle* fiber, const char* error);
|
||||
|
||||
void schedulerShutdown();
|
||||
|
||||
|
||||
+2
-2
@@ -16,7 +16,7 @@ static void timerCloseCallback(uv_handle_t* handle)
|
||||
// Called by libuv when the timer has completed.
|
||||
static void timerCallback(uv_timer_t* handle)
|
||||
{
|
||||
WrenValue* fiber = (WrenValue*)handle->data;
|
||||
WrenHandle* fiber = (WrenHandle*)handle->data;
|
||||
|
||||
// Tell libuv that we don't need the timer anymore.
|
||||
uv_close((uv_handle_t*)handle, timerCloseCallback);
|
||||
@@ -28,7 +28,7 @@ static void timerCallback(uv_timer_t* handle)
|
||||
void timerStartTimer(WrenVM* vm)
|
||||
{
|
||||
int milliseconds = (int)wrenGetSlotDouble(vm, 1);
|
||||
WrenValue* fiber = wrenGetSlotValue(vm, 2);
|
||||
WrenHandle* fiber = wrenGetSlotHandle(vm, 2);
|
||||
|
||||
// Store the fiber to resume when the timer completes.
|
||||
uv_timer_t* handle = (uv_timer_t*)malloc(sizeof(uv_timer_t));
|
||||
|
||||
Reference in New Issue
Block a user