Add "io" module with beginnings of a File class.

This commit is contained in:
Bob Nystrom
2015-09-16 07:34:49 -07:00
parent 251752fcfb
commit dc527c8d86
10 changed files with 143 additions and 2 deletions
+51
View File
@@ -0,0 +1,51 @@
#include <stdlib.h>
#include <string.h>
#include "uv.h"
#include "io.h"
#include "scheduler.h"
#include "vm.h"
#include "wren.h"
#include <stdio.h>
// Called by libuv when the stat call for size completes.
static void sizeCallback(uv_fs_t* request)
{
WrenValue* fiber = (WrenValue*)request->data;
if (request->result != 0)
{
schedulerResumeString(fiber, uv_strerror((int)request->result));
uv_fs_req_cleanup(request);
return;
}
double size = (double)request->statbuf.st_size;
uv_fs_req_cleanup(request);
schedulerResumeDouble(fiber, size);
}
static void startSize(WrenVM* vm)
{
const char* path = wrenGetArgumentString(vm, 1);
WrenValue* fiber = wrenGetArgumentValue(vm, 2);
// Store the fiber to resume when the request completes.
uv_fs_t* request = (uv_fs_t*)malloc(sizeof(uv_fs_t));
request->data = fiber;
uv_fs_stat(getLoop(), request, path, sizeCallback);
}
WrenForeignMethodFn ioBindForeign(
WrenVM* vm, const char* className, bool isStatic, const char* signature)
{
if (strcmp(className, "File") != 0) return NULL;
if (isStatic && strcmp(signature, "startSize_(_,_)") == 0) return startSize;
return NULL;
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef io_h
#define io_h
#include "wren.h"
WrenForeignMethodFn ioBindForeign(
WrenVM* vm, const char* className, bool isStatic, const char* signature);
#endif
+15
View File
@@ -0,0 +1,15 @@
import "scheduler" for Scheduler
class File {
static size(path) {
if (!(path is String)) Fiber.abort("Path must be a string.")
startSize_(path, Fiber.current)
var result = Scheduler.runNextScheduled_()
if (result is String) Fiber.abort(result)
return result
}
foreign static startSize_(path, fiber)
}
+17
View File
@@ -0,0 +1,17 @@
// Generated automatically from src/module/io.wren. Do not edit.
static const char* ioModuleSource =
"import \"scheduler\" for Scheduler\n"
"\n"
"class File {\n"
" static size(path) {\n"
" if (!(path is String)) Fiber.abort(\"Path must be a string.\")\n"
"\n"
" startSize_(path, Fiber.current)\n"
" var result = Scheduler.runNextScheduled_()\n"
" if (result is String) Fiber.abort(result)\n"
"\n"
" return result\n"
" }\n"
"\n"
" foreign static startSize_(path, fiber)\n"
"}\n";
+12
View File
@@ -35,6 +35,18 @@ void schedulerResume(WrenValue* fiber)
wrenReleaseValue(getVM(), fiber);
}
void schedulerResumeDouble(WrenValue* fiber, double value)
{
wrenCall(getVM(), resumeWithArg, NULL, "vd", fiber, value);
wrenReleaseValue(getVM(), fiber);
}
void schedulerResumeString(WrenValue* fiber, const char* text)
{
wrenCall(getVM(), resumeWithArg, NULL, "vs", fiber, text);
wrenReleaseValue(getVM(), fiber);
}
void schedulerReleaseMethods()
{
if (resume != NULL) wrenReleaseValue(getVM(), resume);
+2
View File
@@ -7,6 +7,8 @@ WrenForeignMethodFn schedulerBindForeign(
WrenVM* vm, const char* className, bool isStatic, const char* signature);
void schedulerResume(WrenValue* fiber);
void schedulerResumeDouble(WrenValue* fiber, double value);
void schedulerResumeString(WrenValue* fiber, const char* text);
void schedulerReleaseMethods();