Skip to content

Instantly share code, notes, and snippets.

@hiiamboris
Last active November 30, 2023 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiiamboris/2eabe452bf9c90b41831b3566ab42074 to your computer and use it in GitHub Desktop.
Save hiiamboris/2eabe452bf9c90b41831b3566ab42074 to your computer and use it in GitHub Desktop.
An experiment in ad-hoc protection
Red [title: "an experiment with adhoc protection"]
;; problem is that once it's assigned to a deep-reactor, protection is lost
;; and we can only protect series, not maps or objects like this (limitation of ownership system)
;; REQUIRES https://codeberg.org/hiiamboris/red-common
#include %common/tree-hopping.red
guard: object [
own: func [s [series!]] [modify s 'owned reduce [self s]]
on-deep-change*: func [o w t a n i p] [
do make error! rejoin ["Attempt to modify protected value "mold/flat/part w 40]
]
]
block-walker: make-series-walker [any-block! any-object! map!]
protect: function [data [series!]] [
guard/own data
foreach-node data block-walker [
if series? :node/:key [guard/own :node/:key]
:node/:key
]
]
protect s: [1 2 3 [4] #(5 [6])]
try [append s 7]
try [append s/4 7]
try [append s/5/5 7]
?? s
append s 4
@hiiamboris
Copy link
Author

s: [1 2 3 [4] #(
    5 [6]
)]
*** User Error: Attempt to modify protected value [1 2 3 [4] #(5 [6])]
*** Where: do
*** Near : do make error! rejoin ["Attempt to modify" ]
*** Stack:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment