1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 17:57: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 = { JanetAbstractType cfun_io_filetype = {
"core/file", "core/file",
0,
cfun_io_gc, cfun_io_gc,
NULL, NULL,
io_file_get, io_file_get,

View File

@ -298,7 +298,7 @@ static void marshal_one_abstract(MarshalState *st, Janet x, int flags) {
MARK_SEEN(); MARK_SEEN();
JanetMarshalContext context = {st, NULL, flags, NULL}; JanetMarshalContext context = {st, NULL, flags, NULL};
pushbyte(st, LB_ABSTRACT); pushbyte(st, LB_ABSTRACT);
pushint(st, at->id); marshal_one(st, janet_ckeywordv(at->name), flags + 1);
pushint(st, janet_abstract_size(abstract)); pushint(st, janet_abstract_size(abstract));
at->marshal(abstract, &context); at->marshal(abstract, &context);
} else { } 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) { static const uint8_t *unmarshal_one_abstract(UnmarshalState *st, const uint8_t *data, Janet *out, int flags) {
uint32_t id = readint(st, &data); Janet key;
const JanetAbstractType *at = janet_get_abstract_type(id); data = unmarshal_one(st, data, &key, flags + 1);
const JanetAbstractType *at = janet_get_abstract_type(key);
if (at == NULL) return NULL; if (at == NULL) return NULL;
if (at->unmarshal) { if (at->unmarshal) {
void *p = janet_abstract(at, readint(st, &data)); 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 = { static JanetAbstractType janet_parse_parsertype = {
"core/parser", "core/parser",
0,
parsergc, parsergc,
parsermark, parsermark,
parserget, parserget,

View File

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

View File

@ -138,7 +138,6 @@ static void ta_buffer_unmarshal(void *p, JanetMarshalContext *ctx) {
static const JanetAbstractType ta_buffer_type = { static const JanetAbstractType ta_buffer_type = {
"ta/buffer", "ta/buffer",
1000,
ta_buffer_gc, ta_buffer_gc,
NULL, NULL,
NULL, NULL,
@ -276,10 +275,9 @@ BUILD_TYPE(float64)
#undef DEFINE_VIEW_INITIALIZER #undef DEFINE_VIEW_INITIALIZER
#define DEFINE_VIEW_ABSTRACT_TYPE(type,tag) \ #define DEFINE_VIEW_ABSTRACT_TYPE(type) \
{ \ { \
"ta/"#type, \ "ta/"#type, \
tag, \
NULL, \ NULL, \
ta_mark, \ ta_mark, \
ta_get_##type, \ ta_get_##type, \
@ -289,16 +287,16 @@ BUILD_TYPE(float64)
} }
static const JanetAbstractType ta_array_types[] = { static const JanetAbstractType ta_array_types[] = {
DEFINE_VIEW_ABSTRACT_TYPE(uint8, 1001), DEFINE_VIEW_ABSTRACT_TYPE(uint8),
DEFINE_VIEW_ABSTRACT_TYPE(int8, 1002), DEFINE_VIEW_ABSTRACT_TYPE(int8),
DEFINE_VIEW_ABSTRACT_TYPE(uint16, 1003), DEFINE_VIEW_ABSTRACT_TYPE(uint16),
DEFINE_VIEW_ABSTRACT_TYPE(int16, 1004), DEFINE_VIEW_ABSTRACT_TYPE(int16),
DEFINE_VIEW_ABSTRACT_TYPE(uint32, 1005), DEFINE_VIEW_ABSTRACT_TYPE(uint32),
DEFINE_VIEW_ABSTRACT_TYPE(int32, 1006), DEFINE_VIEW_ABSTRACT_TYPE(int32),
DEFINE_VIEW_ABSTRACT_TYPE(uint64, 1007), DEFINE_VIEW_ABSTRACT_TYPE(uint64),
DEFINE_VIEW_ABSTRACT_TYPE(int64, 1008), DEFINE_VIEW_ABSTRACT_TYPE(int64),
DEFINE_VIEW_ABSTRACT_TYPE(float32, 1009), DEFINE_VIEW_ABSTRACT_TYPE(float32),
DEFINE_VIEW_ABSTRACT_TYPE(float64, 1010) DEFINE_VIEW_ABSTRACT_TYPE(float64)
}; };
#undef DEFINE_VIEW_ABSTRACT_TYPE #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 */ /* TODO for test it's not the good place for this function */
static Janet cfun_abstract_properties(int32_t argc, Janet *argv) { static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
const JanetAbstractType *at; const uint8_t *key = janet_getkeyword(argv, 0);
if (janet_checktype(argv[0], JANET_KEYWORD)) { const JanetAbstractType *at = janet_get_abstract_type(janet_wrap_keyword(key));
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);
}
if (at == NULL) { if (at == NULL) {
return janet_wrap_nil(); return janet_wrap_nil();
} }
JanetKV *props = janet_struct_begin(3); JanetKV *props = janet_struct_begin(2);
janet_struct_put(props, janet_ckeywordv("id"), janet_wrap_number(at->id));
janet_struct_put(props, janet_ckeywordv("name"), janet_ckeywordv(at->name)); 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))); janet_struct_put(props, janet_ckeywordv("marshal"), janet_wrap_boolean((at->marshal != NULL) && (at->unmarshal != NULL)));
return janet_wrap_struct(janet_struct_end(props)); 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 */ /* 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 { typedef struct {
const JanetAbstractType *at; const JanetAbstractType *at;
@ -296,29 +296,15 @@ typedef struct {
void janet_register_abstract_type(const JanetAbstractType *at) { void janet_register_abstract_type(const JanetAbstractType *at) {
JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *)janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap)); JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *)janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap));
abstract->at = at; abstract->at = at;
if (!(janet_checktype(janet_table_get(janet_vm_registry, janet_wrap_number(at->id)), JANET_NIL)) || if (!(janet_checktype(janet_table_get(janet_vm_registry, janet_ckeywordv(at->name)), 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 exists");
janet_panic("Register abstract type fail, a type with same name or id 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)); janet_table_put(janet_vm_registry, janet_ckeywordv(at->name), janet_wrap_abstract(abstract));
} }
const JanetAbstractType *janet_get_abstract_type(uint32_t id) { const JanetAbstractType *janet_get_abstract_type(Janet key) {
Janet twrap = janet_table_get(janet_vm_registry, janet_wrap_number(id)); Janet twrap = janet_table_get(janet_vm_registry, key);
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));
if (janet_checktype(twrap, JANET_NIL)) { if (janet_checktype(twrap, JANET_NIL)) {
return NULL; return NULL;
} }

View File

@ -814,7 +814,6 @@ typedef struct {
/* Defines an abstract type */ /* Defines an abstract type */
struct JanetAbstractType { struct JanetAbstractType {
const char *name; const char *name;
uint32_t id;
int (*gc)(void *data, size_t len); int (*gc)(void *data, size_t len);
int (*gcmark)(void *data, size_t len); int (*gcmark)(void *data, size_t len);
Janet(*get)(void *data, Janet key); 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 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(Janet key);
JANET_API const JanetAbstractType *janet_get_abstract_type_byname(const char *name);