From 7b5825668d1f1a5e45fa51bbae7e2f12bde3eee6 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 20 Mar 2021 01:44:03 -0500 Subject: [PATCH] Added boilerplate for CObjStream --- src/cobj.c | 14 ++++++++++++++ src/cobj.h | 9 +++++++++ src/cosmo.h | 3 ++- src/cstate.h | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/cobj.c b/src/cobj.c index 5dc445d..467cb19 100644 --- a/src/cobj.c +++ b/src/cobj.c @@ -7,6 +7,7 @@ #include #include +#include // we don't actually hash the whole string :eyes: uint32_t hashString(const char *str, size_t sz) { @@ -54,6 +55,12 @@ void cosmoO_free(CState *state, CObj *obj) { cosmoM_free(state, CObjObject, objTbl); break; } + case COBJ_STREAM: { + CObjStream *objStrm = (CObjStream*)obj; + close(objStrm->fd); + cosmoM_free(state, CObjStream, objStrm); + break; + } case COBJ_TABLE: { CObjTable *tbl = (CObjTable*)obj; cosmoT_clearTable(state, &tbl->tbl); @@ -181,6 +188,13 @@ CObjObject *cosmoO_newObject(CState *state) { return obj; } +CObjStream *cosmoO_newStream(CState *state, int fd) { + CObjStream *strm = (CObjStream*)cosmoO_allocateBase(state, sizeof(CObjStream), COBJ_STREAM); + strm->fd = fd; + + return strm; +} + CObjTable *cosmoO_newTable(CState *state) { CObjTable *obj = (CObjTable*)cosmoO_allocateBase(state, sizeof(CObjTable), COBJ_TABLE); diff --git a/src/cobj.h b/src/cobj.h index 32f7bf6..e2e4676 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -7,6 +7,7 @@ typedef enum CObjType { COBJ_STRING, COBJ_OBJECT, COBJ_TABLE, + COBJ_STREAM, COBJ_FUNCTION, COBJ_CFUNCTION, // internal use @@ -44,6 +45,11 @@ struct CObjString { bool isIString; }; +struct CObjStream { + CommonHeader; // "is a" CObj + int fd; // handle to file descriptor, on POSIX compliant OSes this can also be a socket :pog: +}; + struct CObjError { CommonHeader; // "is a" CObj CValue err; // error string @@ -109,6 +115,7 @@ struct CObjUpval { #define IS_STRING(x) isObjType(x, COBJ_STRING) #define IS_OBJECT(x) isObjType(x, COBJ_OBJECT) +#define IS_STREAM(x) isObjType(x, COBJ_STREAM) #define IS_TABLE(x) isObjType(x, COBJ_TABLE) #define IS_FUNCTION(x) isObjType(x, COBJ_FUNCTION) #define IS_CFUNCTION(x) isObjType(x, COBJ_CFUNCTION) @@ -117,6 +124,7 @@ struct CObjUpval { #define cosmoV_readString(x) ((CObjString*)cosmoV_readRef(x)) #define cosmoV_readCString(x) (((CObjString*)cosmoV_readRef(x))->str) +#define cosmoV_readFD(x) (((CObjStream*)cosmoV_readRef(x))->fd) #define cosmoV_readObject(x) ((CObjObject*)cosmoV_readRef(x)) #define cosmoV_readTable(x) ((CObjTable*)cosmoV_readRef(x)) #define cosmoV_readFunction(x) ((CObjFunction*)cosmoV_readRef(x)) @@ -142,6 +150,7 @@ bool cosmoO_equal(CState *state, CObj* obj1, CObj* obj2); bool cosmoO_isDescendant(CObj *obj, CObjObject *proto); CObjObject *cosmoO_newObject(CState *state); +CObjStream *cosmoO_newStream(CState *state, int fd); CObjTable *cosmoO_newTable(CState *state); CObjFunction *cosmoO_newFunction(CState *state); CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func); diff --git a/src/cosmo.h b/src/cosmo.h index 3e5f457..9a9917d 100644 --- a/src/cosmo.h +++ b/src/cosmo.h @@ -33,13 +33,14 @@ typedef uint32_t cosmo_Flag; // objs typedef struct CObj CObj; +typedef struct CObjObject CObjObject; +typedef struct CObjStream CObjStream; typedef struct CObjString CObjString; typedef struct CObjUpval CObjUpval; typedef struct CObjFunction CObjFunction; typedef struct CObjCFunction CObjCFunction; typedef struct CObjMethod CObjMethod; typedef struct CObjError CObjError; -typedef struct CObjObject CObjObject; typedef struct CObjTable CObjTable; typedef struct CObjClosure CObjClosure; diff --git a/src/cstate.h b/src/cstate.h index a2f71ac..31e60f8 100644 --- a/src/cstate.h +++ b/src/cstate.h @@ -25,7 +25,7 @@ typedef enum IStringEnum { ISTRING_ITER, // __iter ISTRING_NEXT, // __next ISTRING_RESERVED, // __reserved - ISTRING_MAX + ISTRING_MAX // if this becomes greater than 33, we are out of space in cosmo_Flag. you'll have to change that to uint64_t } IStringEnum; typedef struct ArrayCObj {