Last active
September 14, 2020 07:53
-
-
Save sogaiu/b96d62095d453a29033ebae6f07890c2 to your computer and use it in GitHub Desktop.
janet abstract and marshal / unmarshal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/janet-lang/janet/blob/master/src/include/janet.h | |
https://github.com/janet-lang/janet/blob/master/src/core/abstract.c | |
https://github.com/janet-lang/janet/blob/master/src/core/marsh.c | |
https://github.com/janet-lang/janet/blob/master/src/core/typedarray.c | |
https://github.com/janet-lang/janet/blob/master/src/core/peg.c | |
/* A context for marshaling and unmarshaling abstract types */ | |
typedef struct { | |
void *m_state; | |
void *u_state; | |
int flags; | |
const uint8_t *data; | |
const JanetAbstractType *at; | |
} JanetMarshalContext; | |
/* Defines an abstract type */ | |
struct JanetAbstractType { | |
const char *name; | |
int (*gc)(void *data, size_t len); | |
int (*gcmark)(void *data, size_t len); | |
int (*get)(void *data, Janet key, Janet *out); | |
void (*put)(void *data, Janet key, Janet value); | |
void (*marshal)(void *p, JanetMarshalContext *ctx); | |
void *(*unmarshal)(JanetMarshalContext *ctx); | |
void (*tostring)(void *p, JanetBuffer *buffer); | |
int (*compare)(void *lhs, void *rhs); | |
int32_t (*hash)(void *p, size_t len); | |
Janet(*next)(void *p, Janet key); | |
Janet(*call)(void *p, int32_t argc, Janet *argv); | |
}; | |
/* Some macros to let us add extra types to JanetAbstract types without | |
* needing to changing native modules that declare them as static const | |
* structures. If more fields are added, these macros are modified to include | |
* default values (usually NULL). This silences missing field warnings. */ | |
#define JANET_ATEND_NAME NULL,JANET_ATEND_GC | |
#define JANET_ATEND_GC NULL,JANET_ATEND_GCMARK | |
#define JANET_ATEND_GCMARK NULL,JANET_ATEND_GET | |
#define JANET_ATEND_GET NULL,JANET_ATEND_PUT | |
#define JANET_ATEND_PUT NULL,JANET_ATEND_MARSHAL | |
#define JANET_ATEND_MARSHAL NULL,JANET_ATEND_UNMARSHAL | |
#define JANET_ATEND_UNMARSHAL NULL,JANET_ATEND_TOSTRING | |
#define JANET_ATEND_TOSTRING NULL,JANET_ATEND_COMPARE | |
#define JANET_ATEND_COMPARE NULL,JANET_ATEND_HASH | |
#define JANET_ATEND_HASH NULL,JANET_ATEND_NEXT | |
#define JANET_ATEND_NEXT NULL,JANET_ATEND_CALL | |
#define JANET_ATEND_CALL | |
/* Abstract */ | |
#define janet_abstract_head(u) ((JanetAbstractHead *)((char *)u - offsetof(JanetAbstractHead, data))) | |
#define janet_abstract_from_head(gcobject) ((JanetAbstract)((char *)gcobject + offsetof(JanetAbstractHead, data))) | |
#define janet_abstract_type(u) (janet_abstract_head(u)->type) | |
#define janet_abstract_size(u) (janet_abstract_head(u)->size) | |
JANET_API void *janet_abstract_begin(const JanetAbstractType *type, size_t size); | |
JANET_API JanetAbstract janet_abstract_end(void *abstractTemplate); | |
JANET_API JanetAbstract janet_abstract(const JanetAbstractType *type, size_t size); /* begin and end in one call */ | |
/* Marshaling */ | |
#define JANET_MARSHAL_UNSAFE 0x20000 | |
JANET_API void janet_marshal( | |
JanetBuffer *buf, | |
Janet x, | |
JanetTable *rreg, | |
int flags); | |
JANET_API Janet janet_unmarshal( | |
const uint8_t *bytes, | |
size_t len, | |
int flags, | |
JanetTable *reg, | |
const uint8_t **next); | |
JANET_API JanetTable *janet_env_lookup(JanetTable *env); | |
JANET_API void janet_env_lookup_into(JanetTable *renv, JanetTable *env, const char *prefix, int recurse); | |
/* Marshal API */ | |
JANET_API void janet_marshal_size(JanetMarshalContext *ctx, size_t value); | |
JANET_API void janet_marshal_int(JanetMarshalContext *ctx, int32_t value); | |
JANET_API void janet_marshal_int64(JanetMarshalContext *ctx, int64_t value); | |
JANET_API void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value); | |
JANET_API void janet_marshal_bytes(JanetMarshalContext *ctx, const uint8_t *bytes, size_t len); | |
JANET_API void janet_marshal_janet(JanetMarshalContext *ctx, Janet x); | |
JANET_API void janet_marshal_abstract(JanetMarshalContext *ctx, JanetAbstract abstract); | |
JANET_API void janet_unmarshal_ensure(JanetMarshalContext *ctx, size_t size); | |
JANET_API size_t janet_unmarshal_size(JanetMarshalContext *ctx); | |
JANET_API int32_t janet_unmarshal_int(JanetMarshalContext *ctx); | |
JANET_API int64_t janet_unmarshal_int64(JanetMarshalContext *ctx); | |
JANET_API uint8_t janet_unmarshal_byte(JanetMarshalContext *ctx); | |
JANET_API void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, size_t len); | |
JANET_API Janet janet_unmarshal_janet(JanetMarshalContext *ctx); | |
JANET_API JanetAbstract janet_unmarshal_abstract(JanetMarshalContext *ctx, size_t size); | |
JANET_API void janet_register_abstract_type(const JanetAbstractType *at); | |
JANET_API const JanetAbstractType *janet_get_abstract_type(Janet key); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment