mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-05 16:20:06 +00:00
Added boilerplate for CObjStream
This commit is contained in:
parent
d13cc398c8
commit
7b5825668d
14
src/cobj.c
14
src/cobj.c
@ -7,6 +7,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// 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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user