EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Just starting out? Need help? Post your questions and find answers here.
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Post by gurj »

EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Code: Select all

Gosub uu
End
uu:
jk=MessageRequester("","---aaaa---",#PB_MessageRequester_Warning|#PB_MessageRequester_YesNoCancel )
Select jk
 Case #PB_MessageRequester_No
  Goto uu
 Case #PB_MessageRequester_Cancel
  Goto re3
 Case #PB_MessageRequester_Yes
  Goto uu
EndSelect
re3:
Return
but this ok,when Not use Select:

Code: Select all

Gosub uu
End
uu:
jk=MessageRequester("","---aaaa---",#PB_MessageRequester_Warning|#PB_MessageRequester_YesNoCancel )
If jk=#PB_MessageRequester_No
  Goto uu
 ElseIf jk=#PB_MessageRequester_Cancel
  Goto re3
 ElseIf jk=#PB_MessageRequester_Yes
  Goto uu
EndIf
re3:
Return

; IDE Options = PureBasic 5.62 (Windows - x86)
; CursorPosition = 1
; Folding = -
; EnableUnicode
; EnableXP
Last edited by gurj on Wed Oct 31, 2018 1:39 pm, edited 1 time in total.
my pb for chinese:
http://ataorj.ys168.com
User avatar
idle
Always Here
Always Here
Posts: 5096
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Select has bug? see jk=#PB_MessageRequester_Cancel

Post by idle »

It's been an issue for quite some time it would seem, you can use this though if you really want to
viewtopic.php?f=12&t=55242

Code: Select all

Macro Gosub(label,depth=0)  ;depth is needed to jmp and return out and in of nested selects 
   CompilerIf depth
      CompilerIf SizeOf(integer) = 8  
         !label#_a=depth                               ;set a fasm preprocesor variable for the nest depth 
         !repeat label#_a                                 
           !pop rax                                           ;assmebless pop rax  
           !end repeat
       CompilerElse 
          !label#_a=depth 
          !repeat label#_a
           !pop eax
         !end repeat 
     CompilerEndIf   
  CompilerElse   
      !label#_a=0 
  CompilerEndIf
   !jmp label                                                   
   !if ~ defined label#__return | defined @f       ;defines the return label if it's not defined  
      !label#__return:                                            ;assembles return label          
      !@@:                                                             ;assembles anonymous label 
   !end if    
 EndMacro 

Macro Return(label)  
   
   !if label#_a > 0  
   CompilerIf SizeOf(integer) = 8  
         !repeat label#_a
           !push rax
         !end repeat   
     CompilerElse 
       !repeat label#_a
            !push eax
         !end repeat   
     CompilerEndIf  
 !end if 
 !jmp label#__return 
EndMacro    

Macro Goto(label,depth=0)  ;depth is needed to jmp and return out and in of nested selects 
   CompilerIf depth 
      CompilerIf SizeOf(integer) = 8 
         !repeat depth 
            !pop rax
         !end repeat    
      CompilerElse 
         !repeat depth 
            !pop eax 
         !end repeat    
    CompilerEndIf    
 CompilerEndIf 
   !jmp label 
EndMacro    

Macro label(name)
   !name#:
EndMacro 

;example 
  
Procedure b()
  Protected  v.i=5 ,x=6
      
  Repeat              
    Select x 
      Case 6      
        Select v
          Case 5   
            Gosub(l1,2)   ;we are nested in 2 selects and exiting 2 levels to l1 
            Debug "after gosub Select l1 "+Str(v)
          Case 6        
            Gosub(l2,2) ;
            Debug "after gosub select l2 "+Str(v)
          Default 
            Goto(l3,2) ;we are nested in 2 selects and exiting 2 levels to l3 
        EndSelect
    EndSelect 
  ForEver  
    
  label(l3)    
   ProcedureReturn v
      
  !l1:             ;we jumped here from within the select statement using Gosub(l1,2) the stack pointer is ballanced    
    v+1            ;without the stack adjustment the protected variables wouldn't be accessible after the jump  
    Return(l1);l,2)  ;return back to the select statement which is nested 2 levels using Return(l1,2) and restore the stack pointer   
      
  !l2:          
    v+1 
    Return(l2);  ;comment out  
    Debug "goto l3"  
    Goto(l3)        ;if you commented out Return(l2,2) you will jump to l3 from here no need to adjust depth
    ;you got here having exited the Select at Case 6 of Select v 
 EndProcedure
 
 
Procedure c()
            
  w.s=Chr(65)
  For i=Asc("B") To Asc("Z")
    Gosub(cc)                 
    Gosub(cc1)
    w+Chr(i)
  Next i
  Debug w
  ProcedureReturn
  
  !cc:
  w.s+"-"
  Gosub(ee)
  Return(cc)
  
  !cc1: 
  w.s+"-"
  Return(cc1)
  
  !ee:
  w.s+"."
  Return(ee)
          
 EndProcedure
 
 
 b()
 c()
 Debug "done"  
 
 Gosub(uu)
 Debug "bye" 
End
!uu:
jk=MessageRequester("","---aaaa---",#PB_MessageRequester_Warning|#PB_MessageRequester_YesNoCancel )
Select jk
  Case #PB_MessageRequester_No
    Debug "no"
  Goto(uu,1)
Case #PB_MessageRequester_Cancel
  Debug "cancel" 
  Goto(re3,1)
 Case #PB_MessageRequester_Yes
   Debug "yes" 
   Goto(uu,1)
EndSelect
!re3:
Return(uu)
 
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: Select has bug? see jk=#PB_MessageRequester_Cancel

Post by gurj »

thanks you,idle !
my pb for chinese:
http://ataorj.ys168.com
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Select has bug? see jk=#PB_MessageRequester_Cancel

Post by Little John »

gurj wrote:Select has bug?
No. The issue with "Goto" that you encounter is documented behaviour. Just read the help for "Goto".
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: Select has bug? see jk=#PB_MessageRequester_Cancel

Post by gurj »

thanks you,Little John!
yes,you is right.
in goto help,mention select...
my pb for chinese:
http://ataorj.ys168.com
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Post by gurj »

sorry,should delete this post.
(Select EndSelect) no wrong,should this:

Code: Select all

Gosub uu
End
uu:
jk=MessageRequester("","---aaaa---",#PB_MessageRequester_Warning|#PB_MessageRequester_YesNoCancel )
Select jk
 Case #PB_MessageRequester_No
  Goto uu
 Case #PB_MessageRequester_Cancel
  Goto re3
 Case #PB_MessageRequester_Yes
  Goto uu
EndSelect
;TODO add:{
End
;}
re3:
Return
Last edited by gurj on Sun Apr 21, 2019 8:56 pm, edited 1 time in total.
my pb for chinese:
http://ataorj.ys168.com
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Post by Josh »

A little workaround, without guarantee:

Code: Select all

Gosub uu
End
uu:
jk=MessageRequester("","---aaaa---",#PB_MessageRequester_Warning|#PB_MessageRequester_YesNoCancel )
Select jk
 Case #PB_MessageRequester_Yes
  FakeReturn
  Goto uu
 Case #PB_MessageRequester_No
  FakeReturn
  Goto uu
 Case #PB_MessageRequester_Cancel
  FakeReturn
  Goto re3
EndSelect
;TODO add:{
End
;}
re3:
Return
sorry for my bad english
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Post by gurj »

thanks Josh!
sorry,
I should delete my previous post.
my pb for chinese:
http://ataorj.ys168.com
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Post by Demivec »

I know you didn't ask and Josh's solution addresses technical things well but I just thought I would throw out another variation in duplicating the same program flow of your test snippet:

Code: Select all

Gosub uu
End
uu:
Repeat
  status = #False
  jk=MessageRequester("","---aaaa---",#PB_MessageRequester_Warning|#PB_MessageRequester_YesNoCancel )
  Select jk
    Case #PB_MessageRequester_Yes
      Continue
    Case #PB_MessageRequester_No
      Continue
    Case #PB_MessageRequester_Cancel
      status = #True
      Break
  EndSelect
ForEver
If status: Goto re3: EndIf 

;TODO add:{
End
;}
re3:
Return
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: EndSelect has bug? see jk=#PB_MessageRequester_Cancel

Post by gurj »

thanks Demivec !
your code fit this:
in pb's doc:
Note: To exit a loop safely, you always must use Break instead of Goto, and never use it inside a Select/EndSelect block (Unless you have the ability to manage the stack yourself, correctly.)
my pb for chinese:
http://ataorj.ys168.com
Post Reply