# HG changeset patch # User Claus Gittinger # Date 1078839949 -3600 # Node ID 41b25e5f753230c9d84ae0107ff8a5116f765823 # Parent 7cc7ac5f29ca40d5e36760e8938d1bbbf8394cea sourceStream access refactored (preps for utf8 decoding) diff -r 7cc7ac5f29ca -r 41b25e5f7532 Method.st --- a/Method.st Tue Mar 09 13:13:58 2004 +0100 +++ b/Method.st Tue Mar 09 14:45:49 2004 +0100 @@ -446,26 +446,6 @@ "Created: 16.1.1997 / 01:25:52 / cg" ! -localSourceStream - "try to open a stream from a local source file, - searching in standard places." - - |fileName aStream| - - package notNil ifTrue:[ - fileName := Smalltalk getSourceFileName:(package copyReplaceAll:$: with:$/) , '/' , source. - fileName notNil ifTrue:[ - aStream := fileName asFilename readStreamOrNil. - aStream notNil ifTrue:[^ aStream]. - ]. - ]. - fileName := Smalltalk getSourceFileName:source. - fileName notNil ifTrue:[ - aStream := fileName asFilename readStreamOrNil. - ]. - ^ aStream -! - makeLocalStringSource "assure that the methods source code is stored locally as a string within the method (as opposed to an external string, which is accessed @@ -597,15 +577,6 @@ sourcePosition := nil ! -sourceChunkFromStream:aStream - Stream positionErrorSignal handle:[:ex | - ^ nil - ] do:[ - aStream position1Based:sourcePosition abs. - ]. - ^ aStream nextChunk. -! - sourceFilename "return the sourcefilename if source is extern; nil otherwise" @@ -638,163 +609,6 @@ ^ sourcePosition abs "Modified: 16.1.1997 / 01:28:25 / cg" -! - -sourceStream - "return an open sourceStream (needs positioning)" - - |aStream fileName who myClass mgr className classNameSymbol sep dir mod| - - " - if sourcePosition is nonNil, its the fileName and - abs(sourcePosition) is the offset. - Otherwise, source is the real source - " - source isNil ifTrue:[^ nil]. - sourcePosition isNil ifTrue:[^ source readStreamOrNil]. - - "/ keep the last source file open, because open/close - "/ operations maybe slow on NFS-mounted file systems. - "/ Since the reference to the file is weak, it will be closed - "/ automatically if the file is not referenced for a while. - "/ Neat trick. - - LastFileLock critical:[ - aStream := LastFileReference at:1. - LastFileReference at:1 put:0. - - (aStream isNil or:[aStream == 0]) ifTrue:[ - aStream := nil. - ] ifFalse:[ - LastSourceFileName = source ifFalse:[ - aStream close. - aStream := nil. - ] - ]. - LastSourceFileName := nil. - ]. - - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - - "/ a negative sourcePosition indicates - "/ that this is a local file - "/ (not to be requested via the sourceCodeManager) - "/ This kludge was added, to allow sourceCode to be - "/ saved to a local source file (i.e. 'st.src') - "/ and having a clue for which file is meant later. - - sourcePosition < 0 ifTrue:[ - aStream := source asFilename readStreamOrNil. - aStream isNil ifTrue:[ - "/ search in some standard places - fileName := Smalltalk getSourceFileName:source. - fileName notNil ifTrue:[ - aStream := fileName asFilename readStreamOrNil. - ]. - ]. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ]. - - "/ - "/ if there is no SourceManager, look in local standard places first - "/ - (mgr := Smalltalk at:#SourceCodeManager) isNil ifTrue:[ - aStream := self localSourceStream. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ]. - - "/ - "/ nope - ask my class for the source (this also invokes the SCMgr) - "/ - who := self who. - who notNil ifTrue:[ - myClass := who methodClass. - - (package notNil and:[package ~= myClass package]) ifTrue:[ - mgr notNil ifTrue:[ - "/ try to get the source using my package information ... - sep := package indexOfAny:'/\:'. - sep ~~ 0 ifTrue:[ - mod := package copyTo:sep - 1. - dir := package copyFrom:sep + 1. - aStream := mgr streamForClass:nil fileName:source revision:nil directory:dir module:mod cache:false. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ]. - ]. - ]. - - aStream := myClass sourceStreamFor:source. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ]. - - "/ - "/ nope - look in standard places - "/ (if there is a source-code manager - otherwise, we already did that) - "/ - mgr notNil ifTrue:[ - aStream := self localSourceStream. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ]. - - "/ - "/ final chance: try current directory - "/ - aStream isNil ifTrue:[ - aStream := source asFilename readStreamOrNil. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ]. - - (who isNil and:[source notNil]) ifTrue:[ - "/ - "/ mhmh - seems to be a method which used to be in some - "/ class, but has been overwritten by another or removed. - "/ (i.e. it has no containing class anyMore) - "/ try to guess the class from the sourceFileName. - "/ and retry. - "/ - className := Smalltalk classNameForFile:source. - (classNameSymbol := className asSymbolIfInterned) notNil ifTrue:[ - myClass := Smalltalk at:classNameSymbol ifAbsent:nil. - myClass notNil ifTrue:[ - aStream := myClass sourceStreamFor:source. - aStream notNil ifTrue:[ - LastSourceFileName := source. - LastFileReference at:1 put:aStream. - ^ aStream - ]. - ] - ] - ]. - - ^ nil ! ! !Method methodsFor:'accessing-visibility'! @@ -1895,6 +1709,193 @@ "Modified: 1.11.1996 / 16:27:04 / cg" ! ! +!Method methodsFor:'private'! + +localSourceStream + "try to open a stream from a local source file, + searching in standard places." + + |fileName aStream| + + package notNil ifTrue:[ + fileName := Smalltalk getSourceFileName:(package copyReplaceAll:$: with:$/) , '/' , source. + fileName notNil ifTrue:[ + aStream := fileName asFilename readStreamOrNil. + aStream notNil ifTrue:[^ aStream]. + ]. + ]. + fileName := Smalltalk getSourceFileName:source. + fileName notNil ifTrue:[ + aStream := fileName asFilename readStreamOrNil. + ]. + ^ aStream +! + +rawSourceStream + "return an open sourceStream (needs positioning)" + + |aStream fileName who myClass mgr className classNameSymbol sep dir mod| + + " + if sourcePosition is nonNil, its the fileName and + abs(sourcePosition) is the offset. + Otherwise, source is the real source + " + source isNil ifTrue:[^ nil]. + sourcePosition isNil ifTrue:[^ source readStream]. + + "/ keep the last source file open, because open/close + "/ operations maybe slow on NFS-mounted file systems. + "/ Since the reference to the file is weak, it will be closed + "/ automatically if the file is not referenced for a while. + "/ Neat trick. + + LastFileLock critical:[ + aStream := LastFileReference at:1. + LastFileReference at:1 put:0. + + (aStream isNil or:[aStream == 0]) ifTrue:[ + aStream := nil. + ] ifFalse:[ + LastSourceFileName = source ifFalse:[ + aStream close. + aStream := nil. + ] + ]. + LastSourceFileName := nil. + ]. + + aStream notNil ifTrue:[ + ^ aStream + ]. + + "/ a negative sourcePosition indicates + "/ that this is a local file + "/ (not to be requested via the sourceCodeManager) + "/ This kludge was added, to allow sourceCode to be + "/ saved to a local source file (i.e. 'st.src') + "/ and having a clue for which file is meant later. + + sourcePosition < 0 ifTrue:[ + aStream := source asFilename readStreamOrNil. + aStream isNil ifTrue:[ + "/ search in some standard places + fileName := Smalltalk getSourceFileName:source. + fileName notNil ifTrue:[ + aStream := fileName asFilename readStreamOrNil. + ]. + ]. + aStream notNil ifTrue:[ + ^ aStream + ]. + ]. + + "/ + "/ if there is no SourceManager, look in local standard places first + "/ + (mgr := Smalltalk at:#SourceCodeManager) isNil ifTrue:[ + aStream := self localSourceStream. + aStream notNil ifTrue:[ + ^ aStream + ]. + ]. + + "/ + "/ nope - ask my class for the source (this also invokes the SCMgr) + "/ + who := self who. + who notNil ifTrue:[ + myClass := who methodClass. + + (package notNil and:[package ~= myClass package]) ifTrue:[ + mgr notNil ifTrue:[ + "/ try to get the source using my package information ... + sep := package indexOfAny:'/\:'. + sep ~~ 0 ifTrue:[ + mod := package copyTo:sep - 1. + dir := package copyFrom:sep + 1. + aStream := mgr streamForClass:nil fileName:source revision:nil directory:dir module:mod cache:false. + aStream notNil ifTrue:[ + ^ aStream + ]. + ]. + ]. + ]. + + aStream := myClass sourceStreamFor:source. + aStream notNil ifTrue:[ + ^ aStream + ]. + ]. + + "/ + "/ nope - look in standard places + "/ (if there is a source-code manager - otherwise, we already did that) + "/ + mgr notNil ifTrue:[ + aStream := self localSourceStream. + aStream notNil ifTrue:[ + ^ aStream + ]. + ]. + + "/ + "/ final chance: try current directory + "/ + aStream isNil ifTrue:[ + aStream := source asFilename readStreamOrNil. + aStream notNil ifTrue:[ + ^ aStream + ]. + ]. + + (who isNil and:[source notNil]) ifTrue:[ + "/ + "/ mhmh - seems to be a method which used to be in some + "/ class, but has been overwritten by another or removed. + "/ (i.e. it has no containing class anyMore) + "/ try to guess the class from the sourceFileName. + "/ and retry. + "/ + className := Smalltalk classNameForFile:source. + (classNameSymbol := className asSymbolIfInterned) notNil ifTrue:[ + myClass := Smalltalk at:classNameSymbol ifAbsent:nil. + myClass notNil ifTrue:[ + aStream := myClass sourceStreamFor:source. + aStream notNil ifTrue:[ + ^ aStream + ]. + ] + ] + ]. + + ^ nil +! + +sourceChunkFromStream:aStream + Stream positionErrorSignal handle:[:ex | + ^ nil + ] do:[ + aStream position1Based:sourcePosition abs. + ]. + ^ aStream nextChunk. +! + +sourceStream + "return an open sourceStream (needs positioning)" + + |stream| + + stream := self rawSourceStream. + stream notNil ifTrue:[ + stream isExternalStream notNil ifTrue:[ + LastSourceFileName := source. + LastFileReference at:1 put:stream. + ] + ]. + ^ stream +! ! + !Method methodsFor:'private-compiler interface'! primitiveNumber @@ -2894,7 +2895,7 @@ !Method class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/Method.st,v 1.264 2004-03-08 19:05:11 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/Method.st,v 1.265 2004-03-09 13:45:49 cg Exp $' ! ! Method initialize!