mirror of
https://github.com/janet-lang/janet
synced 2025-01-02 20:00:26 +00:00
Allow table/to-struct to take a prototype.
Use this prototype struct in freeze.
This commit is contained in:
parent
682f0f584f
commit
1b49934e4f
@ -2230,8 +2230,16 @@
|
|||||||
(tuple/slice (map freeze x))
|
(tuple/slice (map freeze x))
|
||||||
|
|
||||||
(or (= tx :table) (= tx :struct))
|
(or (= tx :table) (= tx :struct))
|
||||||
(let [sorted-kvs (array/join @[] ;(sort (map freeze (pairs x))))]
|
(let [temp-tab @{}]
|
||||||
(struct/with-proto (freeze (getproto x)) ;sorted-kvs))
|
# Handle multiple unique keys that freeze. Result should
|
||||||
|
# be independent of iteration order.
|
||||||
|
(eachp [k v] x
|
||||||
|
(def kk (freeze k))
|
||||||
|
(def vv (freeze v))
|
||||||
|
(def old (get temp-tab kk))
|
||||||
|
(def new (if (= nil old) vv (max vv old)))
|
||||||
|
(put temp-tab kk new))
|
||||||
|
(table/to-struct temp-tab (freeze (getproto x))))
|
||||||
|
|
||||||
(= tx :buffer)
|
(= tx :buffer)
|
||||||
(string x)
|
(string x)
|
||||||
|
@ -372,12 +372,14 @@ JANET_CORE_FN(cfun_table_setproto,
|
|||||||
}
|
}
|
||||||
|
|
||||||
JANET_CORE_FN(cfun_table_tostruct,
|
JANET_CORE_FN(cfun_table_tostruct,
|
||||||
"(table/to-struct tab)",
|
"(table/to-struct tab &opt proto)",
|
||||||
"Convert a table to a struct. Returns a new struct. This function "
|
"Convert a table to a struct. Returns a new struct.") {
|
||||||
"does not take into account prototype tables.") {
|
janet_arity(argc, 1, 2);
|
||||||
janet_fixarity(argc, 1);
|
|
||||||
JanetTable *t = janet_gettable(argv, 0);
|
JanetTable *t = janet_gettable(argv, 0);
|
||||||
return janet_wrap_struct(janet_table_to_struct(t));
|
JanetStruct proto = janet_optstruct(argv, argc, 1, NULL);
|
||||||
|
JanetStruct st = janet_table_to_struct(t);
|
||||||
|
janet_struct_proto(st) = proto;
|
||||||
|
return janet_wrap_struct(st);
|
||||||
}
|
}
|
||||||
|
|
||||||
JANET_CORE_FN(cfun_table_rawget,
|
JANET_CORE_FN(cfun_table_rawget,
|
||||||
|
Loading…
Reference in New Issue
Block a user