Skip to content

Commit

Permalink
extract fileio to a C module
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Dec 14, 2023
1 parent c4d8e8d commit 031ca32
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 63 deletions.
3 changes: 0 additions & 3 deletions src/kuroko/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ struct BaseClasses {
KrkClass * enumerateClass; /**< Yield pairs of indexes and values from an iterator. */
KrkClass * HelperClass; /**< Class implementation of 'help' object */
KrkClass * LicenseReaderClass; /**< Class implementation of 'license' object */
KrkClass * FileClass; /**< fileio.File */
KrkClass * BinaryFileClass; /**< fileio.BinaryFile */
KrkClass * DirectoryClass; /**< fileio.Directory */
KrkClass * CompilerStateClass; /**< Compiler global state */
KrkClass * CellClass; /**< Upvalue cell */
KrkClass * setClass; /**< Unordered hashset */
Expand Down
32 changes: 19 additions & 13 deletions src/fileio.c → src/modules/module_fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include <kuroko/memory.h>
#include <kuroko/util.h>


static KrkClass * fileio_File;
static KrkClass * fileio_BinaryFile;
static KrkClass * fileio_Directory;

/**
* @brief Object for a C `FILE*` stream.
* @extends KrkInstance
Expand All @@ -24,10 +29,10 @@ struct File {
int unowned;
};

#define IS_File(o) (krk_isInstanceOf(o, KRK_BASE_CLASS(File)))
#define IS_File(o) (krk_isInstanceOf(o, fileio_File))
#define AS_File(o) ((struct File*)AS_OBJECT(o))

#define IS_BinaryFile(o) (krk_isInstanceOf(o, KRK_BASE_CLASS(BinaryFile)))
#define IS_BinaryFile(o) (krk_isInstanceOf(o, fileio_BinaryFile))
#define AS_BinaryFile(o) ((struct File*)AS_OBJECT(o))

/**
Expand All @@ -39,7 +44,7 @@ struct Directory {
DIR * dirPtr;
};

#define IS_Directory(o) (krk_isInstanceOf(o, KRK_BASE_CLASS(Directory)))
#define IS_Directory(o) (krk_isInstanceOf(o, fileio_Directory))
#define AS_Directory(o) ((struct Directory*)AS_OBJECT(o))

#define CURRENT_CTYPE struct File *
Expand Down Expand Up @@ -77,7 +82,7 @@ KRK_Function(open) {
if (!file) return krk_runtimeError(vm.exceptions->ioError, "open: failed to open file; system returned: %s", strerror(errno));

/* Now let's build an object to hold it */
KrkInstance * fileObject = krk_newInstance(isBinary ? KRK_BASE_CLASS(BinaryFile) : KRK_BASE_CLASS(File));
KrkInstance * fileObject = krk_newInstance(isBinary ? fileio_BinaryFile : fileio_File);
krk_push(OBJECT_VAL(fileObject));

/* Let's put the filename in there somewhere... */
Expand Down Expand Up @@ -259,7 +264,7 @@ KRK_Method(File,__exit__) {
}

static void makeFileInstance(KrkInstance * module, const char name[], FILE * file, const char mode[]) {
KrkInstance * fileObject = krk_newInstance(KRK_BASE_CLASS(File));
KrkInstance * fileObject = krk_newInstance(fileio_File);
krk_push(OBJECT_VAL(fileObject));
KrkValue filename = OBJECT_VAL(krk_copyString(name,strlen(name)));
krk_push(filename);
Expand Down Expand Up @@ -433,7 +438,7 @@ KRK_Function(opendir) {
DIR * dir = opendir(path->chars);
if (!dir) return krk_runtimeError(vm.exceptions->ioError, "opendir: %s", strerror(errno));

struct Directory * dirObj = (void *)krk_newInstance(KRK_BASE_CLASS(Directory));
struct Directory * dirObj = (void *)krk_newInstance(fileio_Directory);
krk_push(OBJECT_VAL(dirObj));

krk_attachNamedValue(&dirObj->inst.fields, "path", OBJECT_VAL(path));
Expand Down Expand Up @@ -489,11 +494,10 @@ KRK_Method(Directory,__exit__) {
return FUNC_NAME(Directory,close)(1,argv,0);
}

void krk_module_init_fileio(void) {
KrkValue krk_module_onload_fileio(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_attachNamedObject(&vm.modules, "fileio", (KrkObj*)module);
krk_attachNamedObject(&module->fields, "__name__", (KrkObj*)S("fileio"));
krk_attachNamedValue(&module->fields, "__file__", NONE_VAL());
krk_push(OBJECT_VAL(module));

KRK_DOC(module,
"@brief Provides access to C <stdio> buffered file I/O functions.\n\n"
"The @c fileio module provides classes and functions for reading "
Expand All @@ -502,7 +506,7 @@ void krk_module_init_fileio(void) {
);

/* Define a class to represent files. (Should this be a helper method?) */
KrkClass * File = krk_makeClass(module, &KRK_BASE_CLASS(File), "File", KRK_BASE_CLASS(object));
KrkClass * File = krk_makeClass(module, &fileio_File, "File", KRK_BASE_CLASS(object));
KRK_DOC(File,"Interface to a buffered file stream.");
File->allocSize = sizeof(struct File);
File->_ongcsweep = _file_sweep;
Expand All @@ -527,7 +531,7 @@ void krk_module_init_fileio(void) {
krk_defineNative(&File->methods, "__repr__", FUNC_NAME(File,__str__));
krk_finalizeClass(File);

KrkClass * BinaryFile = krk_makeClass(module, &KRK_BASE_CLASS(BinaryFile), "BinaryFile", File);
KrkClass * BinaryFile = krk_makeClass(module, &fileio_BinaryFile, "BinaryFile", File);
KRK_DOC(BinaryFile,
"Equivalent to @ref File but using @ref bytes instead of string @ref str."
);
Expand All @@ -537,7 +541,7 @@ void krk_module_init_fileio(void) {
BIND_METHOD(BinaryFile,write);
krk_finalizeClass(BinaryFile);

KrkClass * Directory = krk_makeClass(module, &KRK_BASE_CLASS(Directory), "Directory", KRK_BASE_CLASS(object));
KrkClass * Directory = krk_makeClass(module, &fileio_Directory, "Directory", KRK_BASE_CLASS(object));
KRK_DOC(Directory,
"Represents an opened file system directory."
);
Expand Down Expand Up @@ -567,4 +571,6 @@ void krk_module_init_fileio(void) {
"@arguments path\n\n"
"Opens the directory at @p path and returns a @ref Directory object. If @p path could not be opened or is not "
"a directory, @ref IOError will be raised.");

return krk_pop();
}
1 change: 0 additions & 1 deletion src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,6 @@ void krk_initVM(int flags) {
if (!(vm.globalFlags & KRK_GLOBAL_NO_DEFAULT_MODULES)) {
#ifndef KRK_NO_SYSTEM_MODULES
krk_module_init_kuroko();
krk_module_init_fileio();
#endif
#ifndef KRK_DISABLE_THREADS
krk_module_init_threading();
Expand Down
46 changes: 0 additions & 46 deletions tools/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,51 +95,6 @@ KRK_Function(demofunc4) {
return NONE_VAL();
}

struct File {
KrkInstance inst;
FILE * filePtr;
};

KRK_Function(print) {
int argcount = 0;
const KrkValue * args = NULL;
const char * sep = " ";
size_t sep_len = 1;
const char * end = "\n";
size_t end_len = 1;
KrkValue file = NONE_VAL();
int flush = 0;

if (!krk_parseArgs(
"*s#s#V!p",
(const char *[]){"sep","end","file","flush"},
&argcount, &args,
&sep, &sep_len, &end, &end_len,
KRK_BASE_CLASS(File), &file,
&flush)) {
return NONE_VAL();
}

FILE * out = file == NONE_VAL() ? stdout : ((struct File*)AS_INSTANCE(file))->filePtr;
if (!out) return krk_runtimeError(vm.exceptions->ioError, "file is closed");

for (int i = 0; i < argcount; ++i) {
krk_printValue(out, args[i]);
if (unlikely(krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION)) return NONE_VAL();
if (i + 1 != argcount) {
fwrite(sep,1,sep_len,out);
}
}

fwrite(end,1,end_len,out);

if (flush) {
fflush(out);
}

return NONE_VAL();
}

KRK_Function(takeschars) {
int a = 0, b = 0, c = 0;

Expand Down Expand Up @@ -181,7 +136,6 @@ int main(int argc, char * argv[]) {
BIND_FUNC(krk_currentThread.module, demofunc2);
BIND_FUNC(krk_currentThread.module, demofunc3);
BIND_FUNC(krk_currentThread.module, demofunc4);
BIND_FUNC(krk_currentThread.module, print);
BIND_FUNC(krk_currentThread.module, takeschars);
BIND_FUNC(krk_currentThread.module, parseints);
BIND_FUNC(krk_currentThread.module, parsefloats);
Expand Down

0 comments on commit 031ca32

Please sign in to comment.