[REBOL] context of a function Re:(2)
From: brian:hawley:bigfoot at: 18-Aug-2000 23:20
>During the REBOL 2 development process REBOL was changed to
>specifically prohibit that kind of access. This was done to
>keep the interpreter from crashing. It's a bad idea to do
>this kind of thing, as it leads to completely unstable code.
> >> f: func [a] [a]
> >> aa: first second :f
> >> clear second :f
> >> append second :f 'print
> >> append second :f aa
>== [print a]
> >> source f
>f: func [a][print a]
> >> f 3
Well Galt, that shows me :)
Looking back, I must have remembered the change to the behavior
of first and second as applied to object! values. Before, you
could get at the actual word and value blocks of the objects by
using those functions, and changing those blocks crashed REBOL.
Those functions were changed to return copies of the blocks so
the crashes wouldn't happen.
For some reason, I assumed that first, second and third were
changed similarly for function! values, for the same reasons.
After testing, it appears that only the first function returns
a copy of the argument block, keeping you from being able to
modify the number of arguments the function actually takes.
This leads me to believe it likely that object! and function!
contexts are implemented with the same data structure inside
REBOL, and that the first function as applied to functions is
passed on to the internal object context.
What does interest me is that the second and third functions
applied to function values return the _actual_ code and spec
blocks of the function!
This allows you to all sorts of evil tricks, such as self-
modifying code, functions that take a different number of
parameters than the help says they do, etc. Evil stuff. :)
On the more practical side, you could use the parse function
of the code and spec blocks to patch function code and help
bugs in place. Otherwise you would have to worry about loose
references to the old version of the function, possibly making
the system unstable.
Being able to modify the original code block of a function
without reassigning the function word is a security hole
though, as I'll explain in my next message.