mirror of
https://github.com/janet-lang/janet
synced 2024-11-05 08:16:16 +00:00
Make top level vars reuse ref cell when redefined at the top level.
This improves the repl experience while not messing with existing code very much, if at all.
This commit is contained in:
parent
07ec89276b
commit
5b5a7e5a24
@ -1614,9 +1614,6 @@ JanetTimestamp to_interval(const JanetTimestamp ts) {
|
|||||||
}
|
}
|
||||||
#define JANET_KQUEUE_INTERVAL(timestamp) (to_interval((timestamp - ts_now())))
|
#define JANET_KQUEUE_INTERVAL(timestamp) (to_interval((timestamp - ts_now())))
|
||||||
|
|
||||||
|
|
||||||
/* TODO: make this available be we using kqueue or epoll, instead of
|
|
||||||
* redefinining it for kqueue and epoll separately? */
|
|
||||||
static JanetTimestamp ts_now(void) {
|
static JanetTimestamp ts_now(void) {
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
janet_assert(-1 != clock_gettime(CLOCK_MONOTONIC, &now), "failed to get time");
|
janet_assert(-1 != clock_gettime(CLOCK_MONOTONIC, &now), "failed to get time");
|
||||||
|
@ -298,8 +298,20 @@ static int varleaf(
|
|||||||
/* Global var, generate var */
|
/* Global var, generate var */
|
||||||
JanetSlot refslot;
|
JanetSlot refslot;
|
||||||
JanetTable *entry = janet_table_clone(reftab);
|
JanetTable *entry = janet_table_clone(reftab);
|
||||||
JanetArray *ref = janet_array(1);
|
|
||||||
|
Janet redef_kw = janet_ckeywordv("redef");
|
||||||
|
int is_redef = janet_truthy(janet_table_get(c->env, redef_kw));
|
||||||
|
|
||||||
|
JanetArray *ref;
|
||||||
|
JanetBinding old_binding;
|
||||||
|
if (is_redef && (old_binding = janet_resolve_ext(c->env, sym),
|
||||||
|
old_binding.type == JANET_BINDING_VAR)) {
|
||||||
|
ref = janet_unwrap_array(old_binding.value);
|
||||||
|
} else {
|
||||||
|
ref = janet_array(1);
|
||||||
janet_array_push(ref, janet_wrap_nil());
|
janet_array_push(ref, janet_wrap_nil());
|
||||||
|
}
|
||||||
|
|
||||||
janet_table_put(entry, janet_ckeywordv("ref"), janet_wrap_array(ref));
|
janet_table_put(entry, janet_ckeywordv("ref"), janet_wrap_array(ref));
|
||||||
janet_table_put(entry, janet_ckeywordv("source-map"),
|
janet_table_put(entry, janet_ckeywordv("source-map"),
|
||||||
janet_wrap_tuple(janetc_make_sourcemap(c)));
|
janet_wrap_tuple(janetc_make_sourcemap(c)));
|
||||||
@ -332,16 +344,9 @@ static int defleaf(
|
|||||||
janet_table_put(entry, janet_ckeywordv("source-map"),
|
janet_table_put(entry, janet_ckeywordv("source-map"),
|
||||||
janet_wrap_tuple(janetc_make_sourcemap(c)));
|
janet_wrap_tuple(janetc_make_sourcemap(c)));
|
||||||
|
|
||||||
int is_redef = 0;
|
|
||||||
Janet redef_kw = janet_ckeywordv("redef");
|
Janet redef_kw = janet_ckeywordv("redef");
|
||||||
Janet meta_redef = janet_table_get(entry, redef_kw);
|
int is_redef = janet_truthy(janet_table_get(c->env, redef_kw));
|
||||||
if (janet_truthy(meta_redef)) {
|
if (is_redef) janet_table_put(entry, redef_kw, janet_wrap_true());
|
||||||
is_redef = 1;
|
|
||||||
} else if (janet_checktype(meta_redef, JANET_NIL) &&
|
|
||||||
janet_truthy(janet_table_get(c->env, redef_kw))) {
|
|
||||||
janet_table_put(entry, redef_kw, janet_wrap_true());
|
|
||||||
is_redef = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_redef) {
|
if (is_redef) {
|
||||||
JanetBinding binding = janet_resolve_ext(c->env, sym);
|
JanetBinding binding = janet_resolve_ext(c->env, sym);
|
||||||
|
@ -626,10 +626,11 @@ JanetBinding janet_resolve_ext(JanetTable *env, const uint8_t *sym) {
|
|||||||
binding.deprecation = JANET_BINDING_DEP_NORMAL;
|
binding.deprecation = JANET_BINDING_DEP_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int redef = janet_truthy(janet_table_get(entry_table, janet_ckeywordv("redef")));
|
|
||||||
int macro = janet_truthy(janet_table_get(entry_table, janet_ckeywordv("macro")));
|
int macro = janet_truthy(janet_table_get(entry_table, janet_ckeywordv("macro")));
|
||||||
Janet value = janet_table_get(entry_table, janet_ckeywordv("value"));
|
Janet value = janet_table_get(entry_table, janet_ckeywordv("value"));
|
||||||
Janet ref = janet_table_get(entry_table, janet_ckeywordv("ref"));
|
Janet ref = janet_table_get(entry_table, janet_ckeywordv("ref"));
|
||||||
|
int ref_is_valid = janet_checktype(ref, JANET_ARRAY);
|
||||||
|
int redef = ref_is_valid && janet_truthy(janet_table_get(entry_table, janet_ckeywordv("redef")));
|
||||||
|
|
||||||
if (macro) {
|
if (macro) {
|
||||||
binding.value = redef ? ref : value;
|
binding.value = redef ? ref : value;
|
||||||
@ -637,14 +638,14 @@ JanetBinding janet_resolve_ext(JanetTable *env, const uint8_t *sym) {
|
|||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!redef && janet_checktype(ref, JANET_ARRAY)) {
|
if (ref_is_valid) {
|
||||||
binding.value = ref;
|
binding.value = ref;
|
||||||
binding.type = JANET_BINDING_VAR;
|
binding.type = redef ? JANET_BINDING_DYNAMIC_DEF : JANET_BINDING_VAR;
|
||||||
return binding;
|
} else {
|
||||||
|
binding.value = value;
|
||||||
|
binding.type = JANET_BINDING_DEF;
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.value = redef ? ref : value;
|
|
||||||
binding.type = redef ? JANET_BINDING_DYNAMIC_DEF : JANET_BINDING_DEF;
|
|
||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,17 +302,7 @@
|
|||||||
(assert (= 1 (staticdef1-inc)) "before redefinition without :redef")
|
(assert (= 1 (staticdef1-inc)) "before redefinition without :redef")
|
||||||
(def staticdef1 1)
|
(def staticdef1 1)
|
||||||
(assert (= 1 (staticdef1-inc)) "after redefinition without :redef")
|
(assert (= 1 (staticdef1-inc)) "after redefinition without :redef")
|
||||||
(def dynamicdef1 :redef 0)
|
|
||||||
(defn dynamicdef1-inc [] (+ 1 dynamicdef1))
|
|
||||||
(assert (= 1 (dynamicdef1-inc)) "before redefinition with :redef")
|
|
||||||
(def dynamicdef1 :redef 1)
|
|
||||||
(assert (= 2 (dynamicdef1-inc)) "after redefinition with :redef")
|
|
||||||
(setdyn :redef true)
|
(setdyn :redef true)
|
||||||
(def staticdef2 {:redef false} 0)
|
|
||||||
(defn staticdef2-inc [] (+ 1 staticdef2))
|
|
||||||
(assert (= 1 (staticdef2-inc)) "before redefinition with :redef false")
|
|
||||||
(def staticdef2 {:redef false} 1)
|
|
||||||
(assert (= 1 (staticdef2-inc)) "after redefinition with :redef false")
|
|
||||||
(def dynamicdef2 0)
|
(def dynamicdef2 0)
|
||||||
(defn dynamicdef2-inc [] (+ 1 dynamicdef2))
|
(defn dynamicdef2-inc [] (+ 1 dynamicdef2))
|
||||||
(assert (= 1 (dynamicdef2-inc)) "before redefinition with dyn :redefs")
|
(assert (= 1 (dynamicdef2-inc)) "before redefinition with dyn :redefs")
|
||||||
|
Loading…
Reference in New Issue
Block a user