r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Graham
3-Jan-2006
[3067]
Ashley uses decimals.
JaimeVargas
4-Jan-2006
[3068x5]
Rebol doesn't stop to amaze me. Here is some pretty neat magic to 
make instances of classes with protected variables.
CounterClass: context [
	c: 0
	bump: does [c: c + 1]
	read: does [c]
	bump-by: func [inc][c: c + inc]
]

make-instance: func [
	class
	/local class-vars instance-data class-methods v
][
	class-vars: copy [*-private-*]
	class-methods: copy []
	instance-data: copy []
	foreach w next first class [
		either function! = type? v: get in class :w [
			append class-methods compose/deep [
				(to set-word! :w) func [(first :v)] [
					bind second get in class (to lit-word! :w) '*-private-*
					do reduce [get in class (to lit-word! :w) (first :v)]
				]
			]
		][	
			append class-vars :w
			append instance-data reduce [to set-word! :w :v]
		]
	]
	use class-vars compose/deep  [
		(instance-data)
		context [(class-methods)]
	]
]

ctr1: make-instance CounterClass
crt2: make-instance CounterClass

ctr1/bump ctr1/bump ctr1/read
ctr2/bump ctr2/read
Both ctr1 and ctr2 have private state that can only be access through 
the defined interface.
Only issue with this is that it can not handle refinements or local 
func vars yet. local func vars should be easy. Refinements is a challenge.
BTW. Both ctr1 and ctr2 share the funcs defined in the CounterClass. 
With standard objects you don't need all this trickery but you receive 
a copy of the funcs.
Anton
4-Jan-2006
[3073]
Henrik, better to use "weights" (decimals) rather than percentages, 
for the user of your style, anyway.
Henrik
5-Jan-2006
[3074x2]
anton, thanks, that's a good idea.
>> to-word "a b"
== a b

>> to-set-word "a b"
== a b:

Why is that legal?
Ladislav
5-Jan-2006
[3076x3]
I think, that the correct answer is: "why not?"
another example: type? to word! "a:"
or to word! "1"
Geomol
6-Jan-2006
[3079]
Jaime, that's a pretty neat trick with the classes. Actually it's 
an extension of the language with some feature, that is not initially 
possible. I'll use some time this week-end to make a deeper inspection 
of the code. Are you the author?
Pekr
6-Jan-2006
[3080x3]
struggling with lowering security in my script - how to lower it 
in my script?? I don't want to answer the question if rebol should 
lower it ...
a bug?

to-rebol-file to-local-file %/C/Rebol/
== %/C/Rebol


Why it removes trailing slash? Then if you submit it to load it fails 
....
regarding security - can I somehow, for my client, generate .exe, 
which will have directly lowered security? We simply want to automate 
packing/upacking archives, to allow user to choose source and destination 
dir .... surely we don't want to answer security dialog each time 
...
Volker
6-Jan-2006
[3083x2]
rebol -s
switches security off.
encap should not have it on, or?
Pekr
6-Jan-2006
[3085]
not in rebol ...
JaimeVargas
6-Jan-2006
[3086]
Geomol. Yes. I am the author.
Pekr
6-Jan-2006
[3087x2]
I mean - someone has incorrectly installed rebol and runs scripts 
by pressing enter in Total commander :-)
so I thought I can disable it directly in the script, to overcome 
requester :-)
Rebolek
6-Jan-2006
[3089]
what's wrong with running scripts from TC?
Volker
6-Jan-2006
[3090x2]
you can do 
  secure none
That asks on start and then all requesters etc are free.
Kru: no -s -option.
Rebolek
6-Jan-2006
[3092]
ok
Pekr
6-Jan-2006
[3093]
I don't want to answer any question :-)
Volker
6-Jan-2006
[3094x2]
but making a shortcut or menu-entry instead, is that to difficult?
Then encap?
Pekr
6-Jan-2006
[3096]
I will simply accept the rule that I should not develop outside my 
sandbox, or it gets denerving :-)
Volker
6-Jan-2006
[3097x4]
Or the cruel trick: put script in c:\ . then everything is in a subfolder. 
except of the 25 other letters.
I personally like the requesters. Its so easy to accidentally click. 
Then i can say "No dont delete this!"
(click and launch one of these half-baked test-script i mean)
For that total commander: is a bat to terrible?
Pekr
6-Jan-2006
[3101]
ah, bat could be a solution, yes, thanks ...
MichaelB
6-Jan-2006
[3102]
Jaime: I checked your code above: first I thought it's not possible, 
then I thought wow, but I got one thing left that doesn't work:

You're using the 'class word to bind the code of the functions of 
an object later to the right object - this doesn't work, because 
'class is always bound to the function context and thus has the last 
object referenced - in your example no problem, because the code 
is the same - but with different code doesn't work anymore - maybe 
with one of the closures it would work - because 'class gets always 
bound to a new context (but I'm not sure yet whether I understand 
it right)

CounterClass2: context [
	d: 0
	bump2: does [d: d + 1]
	read2: does [d]
	bump-by2: func [inc][d: d + inc]
]

ctr1: make-instance CounterClass
ctr2: make-instance CounterClass2

ctr1/bump ctr1/bump ctr1/read
ctr2/bump2 ctr2/read2


fails, because at ctr1/bump, class is bound to object CounterClass2 
which has only bump2


so if this gets sorted out - it seams to be really difficult to access 
the hidden contexts (or impossible, because after invoking the function 
the contexts are gone)
JaimeVargas
6-Jan-2006
[3103x5]
Humm. This is strange. Let me check it here.
Solved. See below.
make-instance: func [
	class [object!]
	/local class-vars instance-data class-methods v
][
	class-vars: copy [*-private-*]
	class-methods: copy []
	instance-data: copy []
	foreach w next first class [
		either function! = type? v: get in class :w [
			append class-methods compose/deep [
				(to set-word! :w) func [(first :v)] [
					bind second get in (:class) (to lit-word! :w) '*-private-*
					do reduce [get in (:class) (to lit-word! :w) (first :v)]
				]
			]
		][	
			append class-vars :w
			append instance-data reduce [to set-word! :w :v]
		]
	]
	use class-vars compose/deep  [
		(instance-data)
		context [(class-methods)]
	]
]
The beaty of this is that you are able to change a class method, 
changing the behaviour of all instances at the same time.
While the private state vars are kept private, and current.
MichaelB
6-Jan-2006
[3108x2]
yes - that's good now. I just have to try to access the object in 
malicous ways - if it's not possible then this is the first time 
I see (doesn't have to mean anything of course) completely hidden 
data of an object.
So we could make some rules how to make data completely invisible:

a) all words to be used later have to be used indirect via words 
in the function (like the traversing of the objects words via [foreach 
w next first 'object ... ]

b) if that's not possible the words used in the function (if they 
expose any context) have to be cleaned by a use which doesn't return 
the context


b) is actually the really smart thing to me - the 'use and the returning 
of the new context in 'use - so one can't catch the 'use context 
and get the words with the usual means
JaimeVargas
6-Jan-2006
[3110]
Howver there is a way to access the private ctx. It is leave as an 
exercise to the reader.
Volker
6-Jan-2006
[3111x2]
If you have access to a function-body you can get the values of all 
words. Still it is a lot obfuscated.
Oops. Saw your last posting was to late, sorry.
JaimeVargas
6-Jan-2006
[3113]
I actually there is a way to improve this which will make it 100% 
secure, but it will lose a different property.
MichaelB
6-Jan-2006
[3114]
Ok, me as the reader is searching then, at least it's not too obvious 
or I'm too blind today. :-)
JaimeVargas
6-Jan-2006
[3115]
;-)
MichaelB
6-Jan-2006
[3116]
ctr1/read
== 2
>> ctr1/bump
== 3
bl: [c]
== [c]
>> f: get in ctr1 'read
>> o: third tenth second :f
>> bind bl first second get in o 'read
== [c]
>> set first bl 12
== 12
>> ctr1/read
== 12

:-)


But how you want to prevent this ? I mean what property you talked 
about would get lost ?