mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-21 23:10:05 +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 <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
// we don't actually hash the whole string :eyes:
|
// we don't actually hash the whole string :eyes:
|
||||||
uint32_t hashString(const char *str, size_t sz) {
|
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);
|
cosmoM_free(state, CObjObject, objTbl);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case COBJ_STREAM: {
|
||||||
|
CObjStream *objStrm = (CObjStream*)obj;
|
||||||
|
close(objStrm->fd);
|
||||||
|
cosmoM_free(state, CObjStream, objStrm);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case COBJ_TABLE: {
|
case COBJ_TABLE: {
|
||||||
CObjTable *tbl = (CObjTable*)obj;
|
CObjTable *tbl = (CObjTable*)obj;
|
||||||
cosmoT_clearTable(state, &tbl->tbl);
|
cosmoT_clearTable(state, &tbl->tbl);
|
||||||
@ -181,6 +188,13 @@ CObjObject *cosmoO_newObject(CState *state) {
|
|||||||
return obj;
|
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 *cosmoO_newTable(CState *state) {
|
||||||
CObjTable *obj = (CObjTable*)cosmoO_allocateBase(state, sizeof(CObjTable), COBJ_TABLE);
|
CObjTable *obj = (CObjTable*)cosmoO_allocateBase(state, sizeof(CObjTable), COBJ_TABLE);
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ typedef enum CObjType {
|
|||||||
COBJ_STRING,
|
COBJ_STRING,
|
||||||
COBJ_OBJECT,
|
COBJ_OBJECT,
|
||||||
COBJ_TABLE,
|
COBJ_TABLE,
|
||||||
|
COBJ_STREAM,
|
||||||
COBJ_FUNCTION,
|
COBJ_FUNCTION,
|
||||||
COBJ_CFUNCTION,
|
COBJ_CFUNCTION,
|
||||||
// internal use
|
// internal use
|
||||||
@ -44,6 +45,11 @@ struct CObjString {
|
|||||||
bool isIString;
|
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 {
|
struct CObjError {
|
||||||
CommonHeader; // "is a" CObj
|
CommonHeader; // "is a" CObj
|
||||||
CValue err; // error string
|
CValue err; // error string
|
||||||
@ -109,6 +115,7 @@ struct CObjUpval {
|
|||||||
|
|
||||||
#define IS_STRING(x) isObjType(x, COBJ_STRING)
|
#define IS_STRING(x) isObjType(x, COBJ_STRING)
|
||||||
#define IS_OBJECT(x) isObjType(x, COBJ_OBJECT)
|
#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_TABLE(x) isObjType(x, COBJ_TABLE)
|
||||||
#define IS_FUNCTION(x) isObjType(x, COBJ_FUNCTION)
|
#define IS_FUNCTION(x) isObjType(x, COBJ_FUNCTION)
|
||||||
#define IS_CFUNCTION(x) isObjType(x, COBJ_CFUNCTION)
|
#define IS_CFUNCTION(x) isObjType(x, COBJ_CFUNCTION)
|
||||||
@ -117,6 +124,7 @@ struct CObjUpval {
|
|||||||
|
|
||||||
#define cosmoV_readString(x) ((CObjString*)cosmoV_readRef(x))
|
#define cosmoV_readString(x) ((CObjString*)cosmoV_readRef(x))
|
||||||
#define cosmoV_readCString(x) (((CObjString*)cosmoV_readRef(x))->str)
|
#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_readObject(x) ((CObjObject*)cosmoV_readRef(x))
|
||||||
#define cosmoV_readTable(x) ((CObjTable*)cosmoV_readRef(x))
|
#define cosmoV_readTable(x) ((CObjTable*)cosmoV_readRef(x))
|
||||||
#define cosmoV_readFunction(x) ((CObjFunction*)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);
|
bool cosmoO_isDescendant(CObj *obj, CObjObject *proto);
|
||||||
|
|
||||||
CObjObject *cosmoO_newObject(CState *state);
|
CObjObject *cosmoO_newObject(CState *state);
|
||||||
|
CObjStream *cosmoO_newStream(CState *state, int fd);
|
||||||
CObjTable *cosmoO_newTable(CState *state);
|
CObjTable *cosmoO_newTable(CState *state);
|
||||||
CObjFunction *cosmoO_newFunction(CState *state);
|
CObjFunction *cosmoO_newFunction(CState *state);
|
||||||
CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func);
|
CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func);
|
||||||
|
@ -33,13 +33,14 @@ typedef uint32_t cosmo_Flag;
|
|||||||
|
|
||||||
// objs
|
// objs
|
||||||
typedef struct CObj CObj;
|
typedef struct CObj CObj;
|
||||||
|
typedef struct CObjObject CObjObject;
|
||||||
|
typedef struct CObjStream CObjStream;
|
||||||
typedef struct CObjString CObjString;
|
typedef struct CObjString CObjString;
|
||||||
typedef struct CObjUpval CObjUpval;
|
typedef struct CObjUpval CObjUpval;
|
||||||
typedef struct CObjFunction CObjFunction;
|
typedef struct CObjFunction CObjFunction;
|
||||||
typedef struct CObjCFunction CObjCFunction;
|
typedef struct CObjCFunction CObjCFunction;
|
||||||
typedef struct CObjMethod CObjMethod;
|
typedef struct CObjMethod CObjMethod;
|
||||||
typedef struct CObjError CObjError;
|
typedef struct CObjError CObjError;
|
||||||
typedef struct CObjObject CObjObject;
|
|
||||||
typedef struct CObjTable CObjTable;
|
typedef struct CObjTable CObjTable;
|
||||||
typedef struct CObjClosure CObjClosure;
|
typedef struct CObjClosure CObjClosure;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ typedef enum IStringEnum {
|
|||||||
ISTRING_ITER, // __iter
|
ISTRING_ITER, // __iter
|
||||||
ISTRING_NEXT, // __next
|
ISTRING_NEXT, // __next
|
||||||
ISTRING_RESERVED, // __reserved
|
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;
|
} IStringEnum;
|
||||||
|
|
||||||
typedef struct ArrayCObj {
|
typedef struct ArrayCObj {
|
||||||
|
Loading…
Reference in New Issue
Block a user