Context.st
changeset 18437 c59fbf8e7b3a
parent 18412 dc22d0d82e28
child 18442 bd42fa983e3f
child 18977 0088eb87454d
equal deleted inserted replaced
18436:b729c0e1d40c 18437:c59fbf8e7b3a
   296 !
   296 !
   297 
   297 
   298 argAt:n
   298 argAt:n
   299     "return the n'th argument"
   299     "return the n'th argument"
   300 
   300 
   301     n > self numArgs ifTrue:[
   301     n > self argumentCount ifTrue:[
   302 	^ self error:'invalid arg access'
   302         ^ self subscriptBoundsError:n.
   303     ].
   303     ].
   304     ^ self at:n
   304     ^ self at:n
   305 
   305 
   306     "Modified: 12.10.1996 / 21:44:28 / cg"
   306     "Modified: 12.10.1996 / 21:44:28 / cg"
   307 !
   307 !
   308 
   308 
   309 argAt:n put:value
   309 argAt:n put:value
   310     "set the n'th argument - useful when the receiver should be restarted"
   310     "set the n'th argument - useful when the receiver should be restarted"
   311 
   311 
   312     n > self numArgs ifTrue:[
   312     n > self argumentCount ifTrue:[
   313 	^ self error:'invalid arg access'
   313         ^ self subscriptBoundsError:n.
   314     ].
   314     ].
   315     self at:n put:value.
   315     self at:n put:value.
   316     ^ value
   316     ^ value
   317 
   317 
   318     "Modified: 12.10.1996 / 21:44:32 / cg"
   318     "Modified: 12.10.1996 / 21:44:32 / cg"
   321 args
   321 args
   322     "return an array filled with the arguments of this context"
   322     "return an array filled with the arguments of this context"
   323 
   323 
   324     |n|
   324     |n|
   325 
   325 
   326     n := self numArgs.
   326     n := self argumentCount.
   327     n == 0 ifTrue:[
   327     n == 0 ifTrue:[
   328 	"/ little optimization here - avaoid creating empty containers
   328         "/ little optimization here - avaoid creating empty containers
   329 	^ #()
   329         ^ #()
   330     ].
   330     ].
   331     ^ (Array new:n) replaceFrom:1 to:n with:self.
   331     ^ (Array new:n) replaceFrom:1 to:n with:self.
   332 !
   332 !
   333 
   333 
   334 argsAndVars
   334 argsAndVars
   335     "return an array filled with the arguments and variables of this context"
   335     "return an array filled with the arguments and variables of this context"
   336 
   336 
   337     |n|
   337     |n|
   338 
   338 
   339     n := self numArgs + self numVars.
   339     n := self argumentCount + self numVars.
   340     n == 0 ifTrue:[
   340     n == 0 ifTrue:[
   341 	"/ little optimization here - avoid creating empty containers
   341         "/ little optimization here - avoid creating empty containers
   342 	^ #()
   342         ^ #()
   343     ].
   343     ].
   344     ^ (Array new:n) replaceFrom:1 to:n with:self.
   344     ^ (Array new:n) replaceFrom:1 to:n with:self.
   345 
   345 
   346     "Modified: 23.10.1996 / 16:19:41 / cg"
   346     "Modified: 23.10.1996 / 16:19:41 / cg"
   347 !
   347 !
   689 
   689 
   690 numTemps
   690 numTemps
   691     "return the number of temporary variables of the Block/Method.
   691     "return the number of temporary variables of the Block/Method.
   692      (for debugging only)"
   692      (for debugging only)"
   693 
   693 
   694     ^ self size - self numArgs - self numVars
   694     ^ self size - self argumentCount - self numVars
   695 
   695 
   696     "Created: 23.10.1996 / 16:19:10 / cg"
   696     "Created: 23.10.1996 / 16:19:10 / cg"
   697     "Modified: 23.10.1996 / 16:19:48 / cg"
   697     "Modified: 23.10.1996 / 16:19:48 / cg"
   698 !
   698 !
   699 
   699 
   874 temporaries
   874 temporaries
   875     "return an array filled with the temporaries of this context"
   875     "return an array filled with the temporaries of this context"
   876 
   876 
   877     |nonTemps mySize|
   877     |nonTemps mySize|
   878 
   878 
   879     nonTemps := self numArgs + self numVars.
   879     nonTemps := self argumentCount + self numVars.
   880     mySize := self numTemps.
   880     mySize := self numTemps.
   881     mySize == 0 ifTrue:[
   881     mySize == 0 ifTrue:[
   882 	"/ little optimization here - avaoid creating empty containers
   882         "/ little optimization here - avaoid creating empty containers
   883 	^ #()
   883         ^ #()
   884     ].
   884     ].
   885     ^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonTemps+1
   885     ^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonTemps+1
   886 
   886 
   887     "Modified: 23.10.1996 / 16:20:00 / cg"
   887     "Modified: 23.10.1996 / 16:20:00 / cg"
   888 !
   888 !
   889 
   889 
   890 varAt:n
   890 varAt:n
   891     "return the n'th local variable"
   891     "return the n'th local variable"
   892 
   892 
   893     ^ self at:(n + self numArgs)
   893     ^ self at:(n + self argumentCount)
   894 !
   894 !
   895 
   895 
   896 varAt:n put:value
   896 varAt:n put:value
   897     "set the n'th local variable - useful when the receiver should be restarted
   897     "set the n'th local variable - useful when the receiver should be restarted
   898      or continued"
   898      or continued"
   899 
   899 
   900     self at:(n + self numArgs) put:value
   900     self at:(n + self argumentCount) put:value
   901 !
   901 !
   902 
   902 
   903 vars
   903 vars
   904     "return an array filled with the local variables of this context"
   904     "return an array filled with the local variables of this context"
   905 
   905 
   906     |nonVars mySize|
   906     |nonVars mySize|
   907 
   907 
   908     nonVars := self numArgs.
   908     nonVars := self argumentCount.
   909     mySize := self numVars.
   909     mySize := self numVars.
   910     mySize == 0 ifTrue:[
   910     mySize == 0 ifTrue:[
   911 	"/ little optimization here - avaoid creating empty containers
   911         "/ little optimization here - avaoid creating empty containers
   912 	^ #()
   912         ^ #()
   913     ].
   913     ].
   914     ^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonVars+1
   914     ^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonVars+1
   915 
   915 
   916     "Modified: 23.10.1996 / 16:20:06 / cg"
   916     "Modified: 23.10.1996 / 16:20:06 / cg"
   917 ! !
   917 ! !
  1032 fullPrint
  1032 fullPrint
  1033     "print the receiver, selector and args of the context
  1033     "print the receiver, selector and args of the context
  1034      - used only for MiniDebuggers walkback print"
  1034      - used only for MiniDebuggers walkback print"
  1035 
  1035 
  1036     self receiverPrintString errorPrint. ' ' errorPrint. selector errorPrint.
  1036     self receiverPrintString errorPrint. ' ' errorPrint. selector errorPrint.
  1037     self numArgs ~~ 0 ifTrue: [
  1037     self argumentCount ~~ 0 ifTrue: [
  1038 	' ' errorPrint. self argsDisplayString errorPrint
  1038         ' ' errorPrint. self argsDisplayString errorPrint
  1039     ].
  1039     ].
  1040     ' [' errorPrint. self lineNumber errorPrint. ']' errorPrintCR
  1040     ' [' errorPrint. self lineNumber errorPrint. ']' errorPrintCR
  1041 
  1041 
  1042     "
  1042     "
  1043      thisContext fullPrint
  1043      thisContext fullPrint
  1730 
  1730 
  1731 displayArgsOn:aStream
  1731 displayArgsOn:aStream
  1732     | n "{ Class: SmallInteger }"
  1732     | n "{ Class: SmallInteger }"
  1733       s |
  1733       s |
  1734 
  1734 
  1735     n := self numArgs.
  1735     n := self argumentCount.
  1736     1 to:n do:[:index |
  1736     1 to:n do:[:index |
  1737 	Error handle:[:ex |
  1737         Error handle:[:ex |
  1738 	    s := '*Error in argString*'.
  1738             s := '*Error in argString*'.
  1739 	] do:[
  1739         ] do:[
  1740 	    s := self argStringFor:(self at:index).
  1740             s := self argStringFor:(self at:index).
  1741 	    s := s contractTo:100.
  1741             s := s contractTo:100.
  1742 	].
  1742         ].
  1743 
  1743 
  1744 	aStream nextPutAll:s asString string.
  1744         aStream nextPutAll:s asString string.
  1745 	index ~~ n ifTrue:[ aStream space ].
  1745         index ~~ n ifTrue:[ aStream space ].
  1746     ].
  1746     ].
  1747 
  1747 
  1748     "Modified: / 07-03-2012 / 13:09:17 / cg"
  1748     "Modified: / 07-03-2012 / 13:09:17 / cg"
  1749 !
  1749 !
  1750 
  1750 
  2531     "helper: given a context, return a collection of arg&var names"
  2531     "helper: given a context, return a collection of arg&var names"
  2532 
  2532 
  2533     |homeContext homeMethod block numArgs numVars m src
  2533     |homeContext homeMethod block numArgs numVars m src
  2534      sel isDoIt blocksLineNr extractFromBlock sender|
  2534      sel isDoIt blocksLineNr extractFromBlock sender|
  2535 
  2535 
  2536     numArgs := self numArgs.
  2536     numArgs := self argumentCount.
  2537     numVars := self numVars.
  2537     numVars := self numVars.
  2538     (numArgs == 0 and:[numVars == 0]) ifTrue:[^ #()].
  2538     (numArgs == 0 and:[numVars == 0]) ifTrue:[^ #()].
  2539 
  2539 
  2540     homeContext := self methodHome.
  2540     homeContext := self methodHome.
  2541     homeContext notNil ifTrue:[
  2541     homeContext notNil ifTrue:[
  2542 	sel := homeContext selector.
  2542         sel := homeContext selector.
  2543 	homeMethod := homeContext method.
  2543         homeMethod := homeContext method.
  2544     ].
  2544     ].
  2545 
  2545 
  2546     extractFromBlock :=
  2546     extractFromBlock :=
  2547 	[
  2547         [
  2548 	    |blockNode argNames varNames vars args blocksHome|
  2548             |blockNode argNames varNames vars args blocksHome|
  2549 
  2549 
  2550 	    blockNode := Compiler
  2550             blockNode := Compiler
  2551 			    blockAtLine:blocksLineNr
  2551                             blockAtLine:blocksLineNr
  2552 			    in:m
  2552                             in:m
  2553 			    orSource:src
  2553                             orSource:src
  2554 			    numArgs:numArgs
  2554                             numArgs:numArgs
  2555 			    numVars:numVars.
  2555                             numVars:numVars.
  2556 
  2556 
  2557 	    blockNode notNil ifTrue:[
  2557             blockNode notNil ifTrue:[
  2558 		"/ a kludge
  2558                 "/ a kludge
  2559 		blockNode lineNumber == blocksLineNr ifTrue:[
  2559                 blockNode lineNumber == blocksLineNr ifTrue:[
  2560 		    blocksHome := blockNode home.
  2560                     blocksHome := blockNode home.
  2561 		    (blocksHome notNil and:[blocksHome isBlock]) ifTrue:[
  2561                     (blocksHome notNil and:[blocksHome isBlock]) ifTrue:[
  2562 			(blocksHome numArgs == numArgs
  2562                         (blocksHome numArgs == numArgs
  2563 			and:[ blocksHome numVars == numVars ]) ifTrue:[
  2563                         and:[ blocksHome numVars == numVars ]) ifTrue:[
  2564 			    blockNode := blocksHome
  2564                             blockNode := blocksHome
  2565 			].
  2565                         ].
  2566 		    ].
  2566                     ].
  2567 		].
  2567                 ].
  2568 
  2568 
  2569 		argNames := #().
  2569                 argNames := #().
  2570 		varNames := #().
  2570                 varNames := #().
  2571 
  2571 
  2572 		numArgs > 0 ifTrue:[
  2572                 numArgs > 0 ifTrue:[
  2573 		    vars := blockNode arguments.
  2573                     vars := blockNode arguments.
  2574 		    vars notEmptyOrNil ifTrue:[
  2574                     vars notEmptyOrNil ifTrue:[
  2575 			argNames := vars collect:[:var | var name]
  2575                         argNames := vars collect:[:var | var name]
  2576 		    ]
  2576                     ]
  2577 		].
  2577                 ].
  2578 		numVars > 0 ifTrue:[
  2578                 numVars > 0 ifTrue:[
  2579 		    vars := blockNode variablesIncludingInlined: (homeMethod code notNil and:[homeMethod byteCode isNil]).
  2579                     vars := blockNode variablesIncludingInlined: (homeMethod code notNil and:[homeMethod byteCode isNil]).
  2580 		    vars notEmptyOrNil ifTrue:[
  2580                     vars notEmptyOrNil ifTrue:[
  2581 			varNames := vars collect:[:var | var name].
  2581                         varNames := vars collect:[:var | var name].
  2582 		    ]
  2582                     ]
  2583 		].
  2583                 ].
  2584 		^ argNames , varNames
  2584                 ^ argNames , varNames
  2585 	    ].
  2585             ].
  2586 	].
  2586         ].
  2587 
  2587 
  2588     "/ #doIt needs special handling below
  2588     "/ #doIt needs special handling below
  2589     isDoIt := (sel == #'doIt') or:[sel == #'doIt:'].
  2589     isDoIt := (sel == #'doIt') or:[sel == #'doIt:'].
  2590     self isBlockContext ifFalse:[
  2590     self isBlockContext ifFalse:[
  2591 	isDoIt ifTrue:[
  2591         isDoIt ifTrue:[
  2592 	    homeMethod notNil ifTrue:[
  2592             homeMethod notNil ifTrue:[
  2593 		"/ special for #doIt
  2593                 "/ special for #doIt
  2594 		m := nil.
  2594                 m := nil.
  2595 		src := ('[' , homeMethod source , '\]') withCRs.
  2595                 src := ('[' , homeMethod source , '\]') withCRs.
  2596 		"/ blocksLineNr := self lineNumber.
  2596                 "/ blocksLineNr := self lineNumber.
  2597 		blocksLineNr := (self home ? self) lineNumber.
  2597                 blocksLineNr := (self home ? self) lineNumber.
  2598 		extractFromBlock value.
  2598                 extractFromBlock value.
  2599 	    ]
  2599             ]
  2600 	].
  2600         ].
  2601 
  2601 
  2602 	homeMethod notNil ifTrue:[
  2602         homeMethod notNil ifTrue:[
  2603 	    ^ homeMethod methodArgAndVarNamesInContext: self.
  2603             ^ homeMethod methodArgAndVarNamesInContext: self.
  2604 	].
  2604         ].
  2605 	^ #()
  2605         ^ #()
  2606     ].
  2606     ].
  2607 
  2607 
  2608     homeMethod notNil ifTrue:[
  2608     homeMethod notNil ifTrue:[
  2609 	isDoIt ifTrue:[
  2609         isDoIt ifTrue:[
  2610 	    "/ special for #doIt
  2610             "/ special for #doIt
  2611 	    "/ my source is found in the method.
  2611             "/ my source is found in the method.
  2612 	    m := nil.
  2612             m := nil.
  2613 	    src := ('[' , homeMethod source , '\]') withCRs.
  2613             src := ('[' , homeMethod source , '\]') withCRs.
  2614 	] ifFalse:[
  2614         ] ifFalse:[
  2615 	    m := homeMethod.
  2615             m := homeMethod.
  2616 	    src := nil.
  2616             src := nil.
  2617 	].
  2617         ].
  2618 	blocksLineNr := self lineNumber.
  2618         blocksLineNr := self lineNumber.
  2619 	extractFromBlock value.
  2619         extractFromBlock value.
  2620 	blocksLineNr := self home lineNumber.
  2620         blocksLineNr := self home lineNumber.
  2621 	extractFromBlock value.
  2621         extractFromBlock value.
  2622     ].
  2622     ].
  2623 
  2623 
  2624     blocksLineNr isNil ifTrue:[
  2624     blocksLineNr isNil ifTrue:[
  2625 	self isBlockContext ifTrue:[
  2625         self isBlockContext ifTrue:[
  2626 	    sender := self sender.
  2626             sender := self sender.
  2627 	    (sender notNil
  2627             (sender notNil
  2628 	    and:[sender receiver isBlock
  2628             and:[sender receiver isBlock
  2629 	    and:[sender selector startsWith:'value']])
  2629             and:[sender selector startsWith:'value']])
  2630 	    ifTrue:[
  2630             ifTrue:[
  2631 		block := sender receiver.
  2631                 block := sender receiver.
  2632 		src := block source.
  2632                 src := block source.
  2633 		src isNil ifTrue:[
  2633                 src isNil ifTrue:[
  2634 		    self error:'no source'.
  2634                     self error:'no source'.
  2635 		].
  2635                 ].
  2636 		blocksLineNr := 1.
  2636                 blocksLineNr := 1.
  2637 		extractFromBlock value.
  2637                 extractFromBlock value.
  2638 	    ].
  2638             ].
  2639 	    sender := nil.
  2639             sender := nil.
  2640 	].
  2640         ].
  2641     ].
  2641     ].
  2642 
  2642 
  2643     ^ #()
  2643     ^ #()
  2644 
  2644 
  2645     "Modified: / 19-08-2013 / 12:13:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2645     "Modified: / 19-08-2013 / 12:13:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2845 
  2845 
  2846     count := 0.
  2846     count := 0.
  2847 
  2847 
  2848     c := self findNextContextWithSelector:selector or:nil or:nil.
  2848     c := self findNextContextWithSelector:selector or:nil or:nil.
  2849     [c notNil] whileTrue:[
  2849     [c notNil] whileTrue:[
  2850 	(c receiver == receiver) ifTrue:[
  2850         (c receiver == receiver) ifTrue:[
  2851 	    c method == self method ifTrue:[
  2851             c method == self method ifTrue:[
  2852 		sameArgs := true.
  2852                 sameArgs := true.
  2853 		1 to:self numArgs do:[:i |
  2853                 1 to:self argumentCount do:[:i |
  2854 		    (c argAt:1) ~~ (self argAt:i)ifTrue:[
  2854                     (c argAt:1) ~~ (self argAt:i)ifTrue:[
  2855 			sameArgs := false
  2855                         sameArgs := false
  2856 		    ]
  2856                     ]
  2857 		].
  2857                 ].
  2858 		sameArgs ifTrue:[^ true].
  2858                 sameArgs ifTrue:[^ true].
  2859 	    ]
  2859             ]
  2860 	].
  2860         ].
  2861 	c := c findNextContextWithSelector:selector or:nil or:nil.
  2861         c := c findNextContextWithSelector:selector or:nil or:nil.
  2862 
  2862 
  2863 	"
  2863         "
  2864 	 this special test was added to get out after a while
  2864          this special test was added to get out after a while
  2865 	 if the sender chain is corrupt - this gives us at least
  2865          if the sender chain is corrupt - this gives us at least
  2866 	 a chance to find those errors.
  2866          a chance to find those errors.
  2867 	"
  2867         "
  2868 	count := count + 1.
  2868         count := count + 1.
  2869 	count >= 100000 ifTrue:[
  2869         count >= 100000 ifTrue:[
  2870 	    'Context [warning]: bad context chain' errorPrintCR.
  2870             'Context [warning]: bad context chain' errorPrintCR.
  2871 	    ^ true
  2871             ^ true
  2872 	]
  2872         ]
  2873     ].
  2873     ].
  2874     ^ false
  2874     ^ false
  2875 !
  2875 !
  2876 
  2876 
  2877 isRecursive
  2877 isRecursive
  2920 ! !
  2920 ! !
  2921 
  2921 
  2922 !Context class methodsFor:'documentation'!
  2922 !Context class methodsFor:'documentation'!
  2923 
  2923 
  2924 version
  2924 version
  2925     ^ '$Header: /cvs/stx/stx/libbasic/Context.st,v 1.224 2015-05-27 19:09:55 cg Exp $'
  2925     ^ '$Header: /cvs/stx/stx/libbasic/Context.st,v 1.225 2015-06-05 16:09:48 stefan Exp $'
  2926 !
  2926 !
  2927 
  2927 
  2928 version_CVS
  2928 version_CVS
  2929     ^ '$Header: /cvs/stx/stx/libbasic/Context.st,v 1.224 2015-05-27 19:09:55 cg Exp $'
  2929     ^ '$Header: /cvs/stx/stx/libbasic/Context.st,v 1.225 2015-06-05 16:09:48 stefan Exp $'
  2930 !
  2930 !
  2931 
  2931 
  2932 version_HG
  2932 version_HG
  2933 
  2933 
  2934     ^ '$Changeset: <not expanded> $'
  2934     ^ '$Changeset: <not expanded> $'