Add stat instance method to File.
This commit is contained in:
parent
3101d9c499
commit
b331915259
@ -18,6 +18,7 @@ extern void fileClose(WrenVM* vm);
|
|||||||
extern void fileDescriptor(WrenVM* vm);
|
extern void fileDescriptor(WrenVM* vm);
|
||||||
extern void fileReadBytes(WrenVM* vm);
|
extern void fileReadBytes(WrenVM* vm);
|
||||||
extern void fileSize(WrenVM* vm);
|
extern void fileSize(WrenVM* vm);
|
||||||
|
extern void fileStat(WrenVM* vm);
|
||||||
extern void fileWriteBytes(WrenVM* vm);
|
extern void fileWriteBytes(WrenVM* vm);
|
||||||
extern void processAllArguments(WrenVM* vm);
|
extern void processAllArguments(WrenVM* vm);
|
||||||
extern void statPath(WrenVM* vm);
|
extern void statPath(WrenVM* vm);
|
||||||
@ -34,7 +35,7 @@ extern void timerStartTimer(WrenVM* vm);
|
|||||||
// If you add a new method to the longest class below, make sure to bump this.
|
// If you add a new method to the longest class below, make sure to bump this.
|
||||||
// Note that it also includes an extra slot for the sentinel value indicating
|
// Note that it also includes an extra slot for the sentinel value indicating
|
||||||
// the end of the list.
|
// the end of the list.
|
||||||
#define MAX_METHODS_PER_CLASS 11
|
#define MAX_METHODS_PER_CLASS 12
|
||||||
|
|
||||||
// The maximum number of foreign classes a single built-in module defines.
|
// The maximum number of foreign classes a single built-in module defines.
|
||||||
//
|
//
|
||||||
@ -107,6 +108,7 @@ static ModuleRegistry modules[] =
|
|||||||
METHOD("descriptor", fileDescriptor)
|
METHOD("descriptor", fileDescriptor)
|
||||||
METHOD("readBytes_(_,_,_)", fileReadBytes)
|
METHOD("readBytes_(_,_,_)", fileReadBytes)
|
||||||
METHOD("size_(_)", fileSize)
|
METHOD("size_(_)", fileSize)
|
||||||
|
METHOD("stat_(_)", fileStat)
|
||||||
METHOD("writeBytes_(_,_,_)", fileWriteBytes)
|
METHOD("writeBytes_(_,_,_)", fileWriteBytes)
|
||||||
END_CLASS
|
END_CLASS
|
||||||
CLASS(Stat)
|
CLASS(Stat)
|
||||||
|
|||||||
@ -293,47 +293,8 @@ void fileReadBytes(WrenVM* vm)
|
|||||||
fileReadBytesCallback);
|
fileReadBytesCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fileSize(WrenVM* vm)
|
|
||||||
{
|
|
||||||
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
|
||||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 1));
|
|
||||||
|
|
||||||
uv_fs_fstat(getLoop(), request, fd, fileSizeCallback);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void fileWriteBytesCallback(uv_fs_t* request)
|
|
||||||
{
|
|
||||||
if (handleRequestError(request)) return;
|
|
||||||
|
|
||||||
FileRequestData* data = (FileRequestData*)request->data;
|
|
||||||
free(data->buffer.base);
|
|
||||||
|
|
||||||
schedulerResume(freeRequest(request), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fileWriteBytes(WrenVM* vm)
|
|
||||||
{
|
|
||||||
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
|
||||||
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));
|
|
||||||
|
|
||||||
FileRequestData* data = (FileRequestData*)request->data;
|
|
||||||
|
|
||||||
data->buffer.len = length;
|
|
||||||
// TODO: Instead of copying, just create a WrenValue 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);
|
|
||||||
memcpy(data->buffer.base, bytes, length);
|
|
||||||
|
|
||||||
uv_fs_write(getLoop(), request, fd, &data->buffer, 1, offset,
|
|
||||||
fileWriteBytesCallback);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Called by libuv when the stat call completes.
|
// Called by libuv when the stat call completes.
|
||||||
static void statPathCallback(uv_fs_t* request)
|
static void statCallback(uv_fs_t* request)
|
||||||
{
|
{
|
||||||
if (handleRequestError(request)) return;
|
if (handleRequestError(request)) return;
|
||||||
|
|
||||||
@ -381,11 +342,56 @@ static void statPathCallback(uv_fs_t* request)
|
|||||||
schedulerFinishResume();
|
schedulerFinishResume();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void fileStat(WrenVM* vm)
|
||||||
|
{
|
||||||
|
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
||||||
|
uv_fs_t* request = createRequest(wrenGetSlotValue(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_fstat(getLoop(), request, fd, fileSizeCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fileWriteBytesCallback(uv_fs_t* request)
|
||||||
|
{
|
||||||
|
if (handleRequestError(request)) return;
|
||||||
|
|
||||||
|
FileRequestData* data = (FileRequestData*)request->data;
|
||||||
|
free(data->buffer.base);
|
||||||
|
|
||||||
|
schedulerResume(freeRequest(request), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fileWriteBytes(WrenVM* vm)
|
||||||
|
{
|
||||||
|
int fd = *(int*)wrenGetSlotForeign(vm, 0);
|
||||||
|
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));
|
||||||
|
|
||||||
|
FileRequestData* data = (FileRequestData*)request->data;
|
||||||
|
|
||||||
|
data->buffer.len = length;
|
||||||
|
// TODO: Instead of copying, just create a WrenValue 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);
|
||||||
|
memcpy(data->buffer.base, bytes, length);
|
||||||
|
|
||||||
|
uv_fs_write(getLoop(), request, fd, &data->buffer, 1, offset,
|
||||||
|
fileWriteBytesCallback);
|
||||||
|
}
|
||||||
|
|
||||||
void statPath(WrenVM* vm)
|
void statPath(WrenVM* vm)
|
||||||
{
|
{
|
||||||
const char* path = wrenGetSlotString(vm, 1);
|
const char* path = wrenGetSlotString(vm, 1);
|
||||||
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));
|
||||||
uv_fs_stat(getLoop(), request, path, statPathCallback);
|
uv_fs_stat(getLoop(), request, path, statCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void allocCallback(uv_handle_t* handle, size_t suggestedSize,
|
static void allocCallback(uv_handle_t* handle, size_t suggestedSize,
|
||||||
|
|||||||
@ -90,6 +90,12 @@ foreign class File {
|
|||||||
return Scheduler.runNextScheduled_()
|
return Scheduler.runNextScheduled_()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stat {
|
||||||
|
ensureOpen_()
|
||||||
|
stat_(Fiber.current)
|
||||||
|
return Stat.new_(Scheduler.runNextScheduled_())
|
||||||
|
}
|
||||||
|
|
||||||
readBytes(count) { readBytes(count, 0) }
|
readBytes(count) { readBytes(count, 0) }
|
||||||
|
|
||||||
readBytes(count, offset) {
|
readBytes(count, offset) {
|
||||||
@ -133,6 +139,7 @@ foreign class File {
|
|||||||
foreign close_(fiber)
|
foreign close_(fiber)
|
||||||
foreign readBytes_(count, offset, fiber)
|
foreign readBytes_(count, offset, fiber)
|
||||||
foreign size_(fiber)
|
foreign size_(fiber)
|
||||||
|
foreign stat_(fiber)
|
||||||
foreign writeBytes_(bytes, offset, fiber)
|
foreign writeBytes_(bytes, offset, fiber)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -92,6 +92,12 @@ static const char* ioModuleSource =
|
|||||||
" return Scheduler.runNextScheduled_()\n"
|
" return Scheduler.runNextScheduled_()\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
" stat {\n"
|
||||||
|
" ensureOpen_()\n"
|
||||||
|
" stat_(Fiber.current)\n"
|
||||||
|
" return Stat.new_(Scheduler.runNextScheduled_())\n"
|
||||||
|
" }\n"
|
||||||
|
"\n"
|
||||||
" readBytes(count) { readBytes(count, 0) }\n"
|
" readBytes(count) { readBytes(count, 0) }\n"
|
||||||
"\n"
|
"\n"
|
||||||
" readBytes(count, offset) {\n"
|
" readBytes(count, offset) {\n"
|
||||||
@ -135,6 +141,7 @@ static const char* ioModuleSource =
|
|||||||
" foreign close_(fiber)\n"
|
" foreign close_(fiber)\n"
|
||||||
" foreign readBytes_(count, offset, fiber)\n"
|
" foreign readBytes_(count, offset, fiber)\n"
|
||||||
" foreign size_(fiber)\n"
|
" foreign size_(fiber)\n"
|
||||||
|
" foreign stat_(fiber)\n"
|
||||||
" foreign writeBytes_(bytes, offset, fiber)\n"
|
" foreign writeBytes_(bytes, offset, fiber)\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import "io" for File
|
import "io" for File
|
||||||
import "scheduler" for Scheduler
|
|
||||||
|
|
||||||
var file = File.open("test/io/file/file.txt")
|
var file = File.open("test/io/file/file.txt")
|
||||||
System.print(file.size) // expect: 19
|
System.print(file.size) // expect: 19
|
||||||
|
|||||||
18
test/io/file/stat.wren
Normal file
18
test/io/file/stat.wren
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import "io" for File, Stat
|
||||||
|
|
||||||
|
var file = File.open("test/io/file/file.txt")
|
||||||
|
var stat = file.stat
|
||||||
|
|
||||||
|
System.print(stat is Stat) // expect: true
|
||||||
|
System.print(stat.device is Num) // expect: true
|
||||||
|
System.print(stat.inode is Num) // expect: true
|
||||||
|
System.print(stat.mode is Num) // expect: true
|
||||||
|
System.print(stat.linkCount) // expect: 1
|
||||||
|
System.print(stat.user is Num) // expect: true
|
||||||
|
System.print(stat.group is Num) // expect: true
|
||||||
|
System.print(stat.specialDevice) // expect: 0
|
||||||
|
System.print(stat.size) // expect: 19
|
||||||
|
System.print(stat.blockSize is Num) // expect: true
|
||||||
|
System.print(stat.blockCount is Num) // expect: true
|
||||||
|
|
||||||
|
file.close()
|
||||||
6
test/io/file/stat_after_close.wren
Normal file
6
test/io/file/stat_after_close.wren
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import "io" for File
|
||||||
|
|
||||||
|
var file = File.open("test/io/file/file.txt")
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
file.stat // expect runtime error: File is not open.
|
||||||
Loading…
Reference in New Issue
Block a user