1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Remove AT id use name as tag

This commit is contained in:
J.-F. Cap 2019-02-24 02:51:34 +01:00
parent af48912f11
commit 9c1c7fb384
7 changed files with 24 additions and 51 deletions

View File

@ -53,7 +53,6 @@ static Janet io_file_get(void *p, Janet);
JanetAbstractType cfun_io_filetype = {
"core/file",
0,
cfun_io_gc,
NULL,
io_file_get,

View File

@ -298,7 +298,7 @@ static void marshal_one_abstract(MarshalState *st, Janet x, int flags) {
MARK_SEEN();
JanetMarshalContext context = {st, NULL, flags, NULL};
pushbyte(st, LB_ABSTRACT);
pushint(st, at->id);
marshal_one(st, janet_ckeywordv(at->name), flags + 1);
pushint(st, janet_abstract_size(abstract));
at->marshal(abstract, &context);
} else {
@ -933,8 +933,9 @@ void janet_unmarshal_janet(JanetMarshalContext *ctx, Janet *out) {
}
static const uint8_t *unmarshal_one_abstract(UnmarshalState *st, const uint8_t *data, Janet *out, int flags) {
uint32_t id = readint(st, &data);
const JanetAbstractType *at = janet_get_abstract_type(id);
Janet key;
data = unmarshal_one(st, data, &key, flags + 1);
const JanetAbstractType *at = janet_get_abstract_type(key);
if (at == NULL) return NULL;
if (at->unmarshal) {
void *p = janet_abstract(at, readint(st, &data));

View File

@ -624,7 +624,6 @@ static Janet parserget(void *p, Janet key);
static JanetAbstractType janet_parse_parsertype = {
"core/parser",
0,
parsergc,
parsermark,
parserget,

View File

@ -981,7 +981,6 @@ static int peg_mark(void *p, size_t size) {
static JanetAbstractType peg_type = {
"core/peg",
0,
NULL,
peg_mark,
NULL,

View File

@ -138,7 +138,6 @@ static void ta_buffer_unmarshal(void *p, JanetMarshalContext *ctx) {
static const JanetAbstractType ta_buffer_type = {
"ta/buffer",
1000,
ta_buffer_gc,
NULL,
NULL,
@ -276,10 +275,9 @@ BUILD_TYPE(float64)
#undef DEFINE_VIEW_INITIALIZER
#define DEFINE_VIEW_ABSTRACT_TYPE(type,tag) \
#define DEFINE_VIEW_ABSTRACT_TYPE(type) \
{ \
"ta/"#type, \
tag, \
NULL, \
ta_mark, \
ta_get_##type, \
@ -289,16 +287,16 @@ BUILD_TYPE(float64)
}
static const JanetAbstractType ta_array_types[] = {
DEFINE_VIEW_ABSTRACT_TYPE(uint8, 1001),
DEFINE_VIEW_ABSTRACT_TYPE(int8, 1002),
DEFINE_VIEW_ABSTRACT_TYPE(uint16, 1003),
DEFINE_VIEW_ABSTRACT_TYPE(int16, 1004),
DEFINE_VIEW_ABSTRACT_TYPE(uint32, 1005),
DEFINE_VIEW_ABSTRACT_TYPE(int32, 1006),
DEFINE_VIEW_ABSTRACT_TYPE(uint64, 1007),
DEFINE_VIEW_ABSTRACT_TYPE(int64, 1008),
DEFINE_VIEW_ABSTRACT_TYPE(float32, 1009),
DEFINE_VIEW_ABSTRACT_TYPE(float64, 1010)
DEFINE_VIEW_ABSTRACT_TYPE(uint8),
DEFINE_VIEW_ABSTRACT_TYPE(int8),
DEFINE_VIEW_ABSTRACT_TYPE(uint16),
DEFINE_VIEW_ABSTRACT_TYPE(int16),
DEFINE_VIEW_ABSTRACT_TYPE(uint32),
DEFINE_VIEW_ABSTRACT_TYPE(int32),
DEFINE_VIEW_ABSTRACT_TYPE(uint64),
DEFINE_VIEW_ABSTRACT_TYPE(int64),
DEFINE_VIEW_ABSTRACT_TYPE(float32),
DEFINE_VIEW_ABSTRACT_TYPE(float64)
};
#undef DEFINE_VIEW_ABSTRACT_TYPE
@ -400,19 +398,12 @@ static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) {
/* TODO for test it's not the good place for this function */
static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
const JanetAbstractType *at;
if (janet_checktype(argv[0], JANET_KEYWORD)) {
const uint8_t *keyw = janet_unwrap_keyword(argv[0]);
at = janet_get_abstract_type_byname((const char *)keyw);
} else {
uint32_t id = (uint32_t)janet_getinteger(argv, 0);
at = janet_get_abstract_type(id);
}
const uint8_t *key = janet_getkeyword(argv, 0);
const JanetAbstractType *at = janet_get_abstract_type(janet_wrap_keyword(key));
if (at == NULL) {
return janet_wrap_nil();
}
JanetKV *props = janet_struct_begin(3);
janet_struct_put(props, janet_ckeywordv("id"), janet_wrap_number(at->id));
JanetKV *props = janet_struct_begin(2);
janet_struct_put(props, janet_ckeywordv("name"), janet_ckeywordv(at->name));
janet_struct_put(props, janet_ckeywordv("marshal"), janet_wrap_boolean((at->marshal != NULL) && (at->unmarshal != NULL)));
return janet_wrap_struct(janet_struct_end(props));

View File

@ -286,7 +286,7 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
/* Abstract type introspection */
static const JanetAbstractType type_wrap = {"core/type_info", 0, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType type_wrap = {"core/type_info", NULL, NULL, NULL, NULL, NULL, NULL};
typedef struct {
const JanetAbstractType *at;
@ -296,29 +296,15 @@ typedef struct {
void janet_register_abstract_type(const JanetAbstractType *at) {
JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *)janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap));
abstract->at = at;
if (!(janet_checktype(janet_table_get(janet_vm_registry, janet_wrap_number(at->id)), JANET_NIL)) ||
!(janet_checktype(janet_table_get(janet_vm_registry, janet_ckeywordv(at->name)), JANET_NIL))) {
janet_panic("Register abstract type fail, a type with same name or id exists");
if (!(janet_checktype(janet_table_get(janet_vm_registry, janet_ckeywordv(at->name)), JANET_NIL))) {
janet_panic("Register abstract type fail, a type with same name exists");
}
janet_table_put(janet_vm_registry, janet_wrap_number(at->id), janet_wrap_abstract(abstract));
janet_table_put(janet_vm_registry, janet_ckeywordv(at->name), janet_wrap_abstract(abstract));
}
const JanetAbstractType *janet_get_abstract_type(uint32_t id) {
Janet twrap = janet_table_get(janet_vm_registry, janet_wrap_number(id));
if (janet_checktype(twrap, JANET_NIL)) {
return NULL;
}
if (!janet_checktype(twrap, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(twrap)) != &type_wrap)) {
janet_panic("expected abstract type");
}
JanetAbstractTypeWrap *w = (JanetAbstractTypeWrap *)janet_unwrap_abstract(twrap);
return w->at;
}
const JanetAbstractType *janet_get_abstract_type_byname(const char *name) {
Janet twrap = janet_table_get(janet_vm_registry, janet_ckeywordv(name));
const JanetAbstractType *janet_get_abstract_type(Janet key) {
Janet twrap = janet_table_get(janet_vm_registry, key);
if (janet_checktype(twrap, JANET_NIL)) {
return NULL;
}

View File

@ -814,7 +814,6 @@ typedef struct {
/* Defines an abstract type */
struct JanetAbstractType {
const char *name;
uint32_t id;
int (*gc)(void *data, size_t len);
int (*gcmark)(void *data, size_t len);
Janet(*get)(void *data, Janet key);
@ -1271,8 +1270,7 @@ JANET_API void janet_unmarshal_janet(JanetMarshalContext *ctx, Janet *out);
JANET_API void janet_register_abstract_type(const JanetAbstractType *at);
JANET_API const JanetAbstractType *janet_get_abstract_type(uint32_t id);
JANET_API const JanetAbstractType *janet_get_abstract_type_byname(const char *name);
JANET_API const JanetAbstractType *janet_get_abstract_type(Janet key);