Improved SOURCE function
[1/10] from: larry::ecotope::com at: 5-May-2001 15:28
Hi all,
I got tired of the SOURCE function not working for items inside objects, so
I wrote an improved version SRC which will do so. When the argument is not a
path, it simply calls SOURCE. I stole the hard part of the code from Doc
Kimbel's most excellent extended help function which is also an object
browser. Doc may be interested in adding a refinement (like help/s arg)
which would cause it to display the source for items in objects. Meanwhile
you can use my function. If you like it, just invoke the script below in
your user.r script.
Here are some examples:
>> src rebol/view/vid/track
track: func [blk][if verbose [print blk]]
>> src rebol/view/vid/verbose
verbose: false
>> src ctx-viewtop/dbug
dbug: func [msg [block! any-string!] /local dbg][
if not dbg: user-prefs/debug [exit]
msg: head insert remold msg "desktop "
either file? dbg [write/append dbg msg] [print msg]
]
>> src cosine
cosine: native [
"Returns the trigonometric cosine in degrees."
value [number!]
/radians "Value is specified in radians."
]
>>
Enjoy
-Larry
---------------------------code below---------------------
REBOL [
Title: "Extended Source"
Author: "Larry Palmiter"
File: %src.r
Date: 5-May-2001
Version: 0.1.0
Purpose: {Extends the SOURCE function to work
when the argument is a path into an object
}
Comments: {I stole the hard part of the code
from Doc Kimbel's wonderful extended help.r
http://rebol.dhs.org/help.r
Thanks Nenad !
}
]
src: func ['word /local value in-obj][
if not equal? type? :word path! [
source :word exit
]
value: to-lit-path :word
in-obj: repend copy [get in] [
copy/part value (length? value) - 1
word: to-lit-word last value
]
if error? try [value: do in-obj][
print "Error: this is not a valid path !"
exit
]
prin join word ": "
either any [native? :value op? :value action? :value][
print ["native" mold third :value]
][
print mold :value
]
]
[2/10] from: blazs:orac:au at: 6-May-2001 14:16
Hello Larry
On 06-May-01, Larry Palmiter wrote:
> Hi all,
> I got tired of the SOURCE function not working for items inside
<<quoted lines omitted: 24>>
> ]
>>>
I grabbed the src.r script, put it in my rebol directory, and ran
src.r, with the following result...
>> system/version
== 1.1.0.1.1
>> src system
<no further output>
I presume 'src should put out an error message of some sort or does it
actually try to display what source it can find within 'system?
> Enjoy
> -Larry
>
Regards
Blaz
[3/10] from: carl:cybercraft at: 6-May-2001 17:59
On 06-May-01, Blaz Segavac wrote:
> Hello Larry
> On 06-May-01, Larry Palmiter wrote:
<<quoted lines omitted: 38>>
> I presume 'src should put out an error message of some sort or does
> it actually try to display what source it can find within 'system?
I wouldn't blame Larry's code for the "no further output". As he says
above, if you don't give it a path it defaults to 'source, and
source system
will probably give you the same result. I eventually
just get a not-enough-memory error. (Though that's one REBOL error I
agree with in my computer's case, but it's probably not the case with
yours.)
--
Carl Read
[carl--cybercraft--co--nz]
[4/10] from: arolls:bigpond:au at: 6-May-2001 16:04
Good idea Larry,
I saw somewhere how to refer to inbuilt word
if you are redefining a word.
If you can refer this way to source, then
you can truly patch the source word.
Something like (pseudo-code):
source: func [word][
either it's-a-path? word [
; all your patching code
][
; use the inbuilt 'source function
system/.../source word
]
]
I just can't remember how to refer to original word in systeme.
I am having a look around... anyone?
Anton.
[5/10] from: larry:ecotope at: 5-May-2001 23:32
Hi Blaz
> I grabbed the src.r script, put it in my rebol directory, and ran
> src.r, with the following result...
>
> >> system/version
> == 1.1.0.1.1
> >> src system
> <no further output>
Sorry about that. In this case, SRC is just calling the the built-in SOURCE
function and it is that function which appears to lock up. In other words,
we get the same result as:
source system ; just don't do this
The apparent lockup (eventually you get something, if you don't run out of
memory) is because the MOLD function has some difficulties with recursive
references in the system object. However, these will work quickly:
src system/view
src/system/view/vid
but the output is too long for anything but a file capture. And the
recursive references are output as [...] .
The best way to explore the system object is to use Doc Kimbel's help.r to
browse the system object and sub-objects. When you see a function in the
object that is interesting, use SRC to see the coide, because help does not
show the function source code. Either can be used to see other types of
values in objects.
There is a slightly updated version on my rebsite:
http://www.nwlink.com/~ecotope1/reb/src.r
Regards
-Larry
[6/10] from: larry:ecotope at: 5-May-2001 23:59
Hi Anton
> Good idea Larry,
:-)
> I saw somewhere how to refer to inbuilt word
> if you are redefining a word.
<<quoted lines omitted: 9>>
> ]
> ]
Well, that is exactly what SRC does. The only difference is that I kept it
with a separate name. I think it confusing to patch over with the same name,
because it leads to people producing output which differs although
apparently using the same function.
SOURCE is just a small REBOL mezzanine function, it is not defined in any
special context. Try
source source
So I could have, and you may, if you like, just rename SRC to SOURCE which
will redefine the global word. No special considerations of object contexts
involved. But everytime you post an example using it, you will have to
explain that it is a special SOURCE function.
There is a slightly updated version on my rebsite:
http://www.nwlink.com/~ecotope1/reb/src.r
> I just can't remember how to refer to original word in systeme.
> I am having a look around... anyone?
Not sure what you mean there. Maybe something like this:
obj: context [print: func [x][system/words/print ["***" x]]
Because you are redefining the word print in the context of the object, all
references to the word print in the object refer to the new definition.
So if you just do this
o: context [print: func [x][print ["***" x]]
the interpreter goes into a stack recursion loop and you get a Stack
overflow error. This is avoided by using system/words/print to refer to the
global version.
Regards
-Larry
[7/10] from: larry:ecotope at: 6-May-2001 0:29
Hi Blaz, Carl, Anton
Just a footnote on
source system
It works instantly in Core. In View, probably everyone gets an out of memory
error. I just watched it and memory use grows to about 14 MB when the memory
error occurs. I have 128 MB of memory so it must be some kind of internal
memory error.
Odd, because I believe we used to be able to get the output in View/beta 4.
It is not just the view object code because
src rebol/view
works fine.
It was noted in some recent posts that the same lock-up out-of-memory
problem occurs with:
source ctx-viewtop
even though it is a fairly small piece of code (about a thousand lines).
The underlying problem is with the MOLD function. Newcomers should be aware
that this has been a problematic aspect of REBOL for a long time, although
seemingly fixed in Core.
-Larry
[8/10] from: arolls:bigpond:au at: 6-May-2001 19:14
Larry, you wrote:
> Well, that is exactly what SRC does. The only difference is that I kept it
> with a separate name. I think it confusing to patch over with the
> same name,
> because it leads to people producing output which differs although
> apparently using the same function.
I disagree a little, here.
You are extending source's abilities, making it
backwards compatible. (Unless people are writing
code that relies on source to fail.)
Before, no one would try to use a path with source.
Now they can.
The only place people can be confused is in explanatory
emails which they might not be able to reproduce in their
console.
For me, the clarity gained by using the word 'source is
stronger than the effort for the user in remembering
another word, 'src.
DocKimbel is confidant enough about his patch to call it
'help. I think you should be too.
Perhaps you can make 'src a code contribution to Rebol Tech?
Then it may be incorporated into a future rebol.
> SOURCE is just a small REBOL mezzanine function, it is not defined in any
> special context. Try
<<quoted lines omitted: 10>>
> Not sure what you mean there. Maybe something like this:
> obj: context [print: func [x][system/words/print ["***" x]]
That's it, yes.
> Because you are redefining the word print in the context of the
> object, all
<<quoted lines omitted: 5>>
> refer to the
> global version.
Interesting.
I see a possible problem with patching over the 'source
function as I suggested.
If the code to redefine 'source was executed twice,
however it may happen, then the reference to the original
definition would be overwritten, and the function
would then be calling itself, resulting in a loop,
wouldn't it?
If there is some way for a function to know
whether it has been defined before, then we
could catch the redefinition.
We could use a "static variable"...
That needs an object wrapped around the
function and a flag word to remember if
the function was defined before.
(I feel sure this was discussed before, but I
wasn't up to the level at that stage.)
> Regards
> -Larry
>
> > Anton.
Regards,
Anton.
[9/10] from: gjones05:mail:orion at: 6-May-2001 5:35
Hi, Larry,
About the time that I received your initial email yesterday in this
thread, I was running a modified version of what you had suggested on
May 3rd for ctx-viewtop. I like to run searches on the whole source
that is available and wanted a single text file. I ran this on a fresh
REBOL/View boot to console mode:
echo %viewtop.txt
print mold system
echo none
It prints a 1.5 MB file. Thanks for the idea.
For your on-the-fly looks, DocKimbel's and your extensions are and will
be a great help, but, ahhhh, to be able to see it all is also nice
sometimes.
--Scott Jones
[10/10] from: larry:ecotope at: 6-May-2001 12:28
Hi Scott
Yes, I can get the 1.5 MB output file also. On Win98 with 128 MB of ram, the
situation appears to be as follows.
If you boot to the console so that ctx-viewtop is still a block, that is
block? ctx-viewtop
returns true, then you can get a complete listing with echo using
print mold system
or
source system
But after ctx-viewtop is loaded to an object, I get an apparent lock-up
terminating with an out-of-memory error. The interesting thing is that the
error also occurs when trying to mold just the ctx-viewtop object, which is
not that large:
>> type? ctx-viewtop
== object!
>> print mold ctx-viewtop
** Script Error: Not enough memory
** Near: print mold ctx-viewtop
>>
When the memory error occurs, there is plenty of ram left as REBOL is only
using about 14 MB, so it must be some internal memory error.
Don't know if this happens with other platforms. Anyone?
Seems possible that there is just some bit of code in the ctx-viewtop object
which is causing the problem with source system.
-Larry
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted