From 1c141aa2980d1cabf85e399bd46633b3520f9ca9 Mon Sep 17 00:00:00 2001 From: egregius313 Date: Thu, 15 Jun 2017 10:30:39 -0400 Subject: [PATCH 1/7] Added compare-and-set! function Added the compare-and-set! function for atoms. --- pixie/stdlib.pxi | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pixie/stdlib.pxi b/pixie/stdlib.pxi index 6c908a82..6070f410 100644 --- a/pixie/stdlib.pxi +++ b/pixie/stdlib.pxi @@ -3110,3 +3110,15 @@ ex: (vary-meta x assoc :foo 42)" :added "0.1"} [f x] (->Iterate f x)) + +(defn compare-and-set! + {:doc "Atomically sets the value of the atom to newval iff the current value of atom is oldval. + Returns true if reset happens, else false." + :signatures [[atom oldval newval]] + :added "0.2"} + [atom oldval newval] + (if (= @atom oldval) + (do + (reset! atom newval) + true) + false) From 4fb90f615576fede18de668b3b96eea9a29d5d14 Mon Sep 17 00:00:00 2001 From: egregius313 Date: Thu, 15 Jun 2017 21:20:28 -0400 Subject: [PATCH 2/7] Updates to the Atom class Added the IWatch#-with-watch and the IValidate#-with-validator methods. --- pixie/vm/atom.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/pixie/vm/atom.py b/pixie/vm/atom.py index 28254673..65a64379 100644 --- a/pixie/vm/atom.py +++ b/pixie/vm/atom.py @@ -13,18 +13,41 @@ def with_meta(self, meta): def meta(self): return self._meta - def __init__(self, boxed_value, meta=nil): + def with_validator(self, validator): + return Atom(self._boxed_value, + self._meta, + self._watch_key, + self._watch_fn, + validator=validator) + + def with_watch(self, watch_key, watch_fn): + return Atom(self._boxed_value, + self._meta, + watch_key, + watch_fn, + self._validator) + + def __init__(self, boxed_value, + meta=nil, + watch_key=nil, watch_fn=nil, + validator=nil): self._boxed_value = boxed_value self._meta = meta + self._watch_key = watch_key + self._watch_fn = watch_fn + self._validator = validator @extend(proto._reset_BANG_, Atom) def _reset(self, v): assert isinstance(self, Atom) + if self._validator is not nil: + affirm(self._validator.invoke([v]), u"Invalid State Exception: Invalid reference state.") + if self._watch_fn is not nil: + self._watch_fn.invoke([self._watch_key, self, self._boxed_value, v]) self._boxed_value = v return v - @extend(proto._deref, Atom) def _deref(self): assert isinstance(self, Atom) @@ -40,6 +63,16 @@ def _with_meta(self, meta): assert isinstance(self, Atom) return self.with_meta(meta) +@extend(proto._with_watch, Atom) +def _with_watch(self, watch_key, watch_fn): + assert isinstance(self, Atom) + return self.with_watch(watch_key, watch_fn) + +@extend(proto._with_validator, Atom) +def _with_validator(self, validate_fn): + assert isinstance(self, Atom) + return self.with_validator(validate_fn) + @as_var("atom") def atom(val=nil): return Atom(val) From 0abd533e8fb6f2b612b846cd16a83b7727eeea6c Mon Sep 17 00:00:00 2001 From: egregius313 Date: Thu, 15 Jun 2017 21:28:07 -0400 Subject: [PATCH 3/7] Added IWatch and IValidate protocols --- pixie/vm/stdlib.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pixie/vm/stdlib.py b/pixie/vm/stdlib.py index d8611d72..b20cc20d 100644 --- a/pixie/vm/stdlib.py +++ b/pixie/vm/stdlib.py @@ -63,6 +63,9 @@ defprotocol("pixie.stdlib", "IMessageObject", ["-call-method", "-get-attr"]) +defprotocol("pixie.stdlib", "IWatch", ["-with-watch"]) +defprotocol("pixie.stdlib", "IValidate", ["-with-validator"]) + def maybe_mark_finalizer(self, tp): if self is _finalize_BANG_: print "MARKING ", tp From 29de1fccc3fd02821d8d8c643f5918182ecb3095 Mon Sep 17 00:00:00 2001 From: egregius313 Date: Thu, 15 Jun 2017 21:30:30 -0400 Subject: [PATCH 4/7] Added import affirm --- pixie/vm/atom.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pixie/vm/atom.py b/pixie/vm/atom.py index 65a64379..a9cb3df9 100644 --- a/pixie/vm/atom.py +++ b/pixie/vm/atom.py @@ -1,4 +1,5 @@ import pixie.vm.object as object +from pixie.vm.object import affirm from pixie.vm.code import extend, as_var from pixie.vm.primitives import nil import pixie.vm.stdlib as proto From dac3b4b9ccec4339bc6e10dda10c6910ec94014e Mon Sep 17 00:00:00 2001 From: egregius313 Date: Tue, 20 Jun 2017 10:48:30 -0400 Subject: [PATCH 5/7] Removed validator code Validator code was not commit-ready --- pixie/vm/atom.py | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/pixie/vm/atom.py b/pixie/vm/atom.py index a9cb3df9..c2d14a80 100644 --- a/pixie/vm/atom.py +++ b/pixie/vm/atom.py @@ -1,5 +1,4 @@ import pixie.vm.object as object -from pixie.vm.object import affirm from pixie.vm.code import extend, as_var from pixie.vm.primitives import nil import pixie.vm.stdlib as proto @@ -14,36 +13,23 @@ def with_meta(self, meta): def meta(self): return self._meta - def with_validator(self, validator): - return Atom(self._boxed_value, - self._meta, - self._watch_key, - self._watch_fn, - validator=validator) - def with_watch(self, watch_key, watch_fn): return Atom(self._boxed_value, self._meta, watch_key, - watch_fn, - self._validator) + watch_fn) def __init__(self, boxed_value, meta=nil, - watch_key=nil, watch_fn=nil, - validator=nil): + watch_key=nil, watch_fn=nil) self._boxed_value = boxed_value self._meta = meta self._watch_key = watch_key self._watch_fn = watch_fn - self._validator = validator - @extend(proto._reset_BANG_, Atom) def _reset(self, v): assert isinstance(self, Atom) - if self._validator is not nil: - affirm(self._validator.invoke([v]), u"Invalid State Exception: Invalid reference state.") if self._watch_fn is not nil: self._watch_fn.invoke([self._watch_key, self, self._boxed_value, v]) self._boxed_value = v @@ -69,11 +55,6 @@ def _with_watch(self, watch_key, watch_fn): assert isinstance(self, Atom) return self.with_watch(watch_key, watch_fn) -@extend(proto._with_validator, Atom) -def _with_validator(self, validate_fn): - assert isinstance(self, Atom) - return self.with_validator(validate_fn) - @as_var("atom") def atom(val=nil): return Atom(val) From f718376b683c869b471586d6e7e99fbfb827aaf2 Mon Sep 17 00:00:00 2001 From: egregius313 Date: Tue, 20 Jun 2017 11:04:22 -0400 Subject: [PATCH 6/7] Added with-watch fn to the stdlib. --- pixie/vm/stdlib.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pixie/vm/stdlib.py b/pixie/vm/stdlib.py index b20cc20d..ed8a49d6 100644 --- a/pixie/vm/stdlib.py +++ b/pixie/vm/stdlib.py @@ -281,6 +281,10 @@ def __meta(a): def __with_meta(a, b): return rt._with_meta(a, b) +@as_var("with-watch") +def __with_watch(a, b, c): + return rt._with_watch(a, b, c) + @returns(bool) @as_var("has-meta?") def __has_meta(a): From 85c9e49d26dcf798db44571e460e8ab12b6cd683 Mon Sep 17 00:00:00 2001 From: egregius313 Date: Tue, 20 Jun 2017 15:16:15 -0400 Subject: [PATCH 7/7] Added missing colon Forgot to add a colon of all things :( --- pixie/vm/atom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pixie/vm/atom.py b/pixie/vm/atom.py index c2d14a80..e21f2dfe 100644 --- a/pixie/vm/atom.py +++ b/pixie/vm/atom.py @@ -21,7 +21,7 @@ def with_watch(self, watch_key, watch_fn): def __init__(self, boxed_value, meta=nil, - watch_key=nil, watch_fn=nil) + watch_key=nil, watch_fn=nil): self._boxed_value = boxed_value self._meta = meta self._watch_key = watch_key