Difference between revisions of "Jump"

From Second Life Wiki
Jump to navigation Jump to search
m
m
Line 47: Line 47:


{{#vardefine:notes|
{{#vardefine:notes|
It is generally considered inadvisable to use jumps (commonly know as gotos).
It is generally considered inadvisable to use jumps (commonly know as gotos) where other flow control structures could be used.
* {{Wikipedia|Goto}}
* {{Wikipedia|Goto}}
* [http://xkcd.com/292/ XKCD:Goto]
* [http://xkcd.com/292/ XKCD:Goto]
==== Multiple Jumps Bug ====
The following function, <code>jumpy</code>, demonstrates the multiple-jump-sites-to-a-single-target bug. It appears this function should result in an infinite loop that will never end however because of this bug the second jump never happens and the function returns. LL has reported they will never fix this bug as it would be a breaking change.
<lsl>jumpy(){
    jump next;//first jump
    @next;
    jump next;//second jump - BUG never gets executed
    return;
}</lsl>
}}
}}


{{#vardefine:caveats|
{{#vardefine:caveats|
*If multiple jumps are declared for the same target label within scope, then only the first will function as expected, all others will silently fail.
*If multiple jump sites are declared for the same target label within scope, then only the first (top to bottom, left to right) will function as expected, all others will silently fail.
*Labels are scoped at the event and function level, meaning that it is not possible to declare duplicate labels within the same event or function, even if the labels are enclosed in different if-statements, loops etc. -- {{JIRA|SVC-6712}}
*Labels are scoped at the event and function level, meaning that it is not possible to declare duplicate labels within the same event or function, even if the labels are enclosed in different if-statements, loops etc. -- {{JIRA|SVC-6712}}
**Attempting this will result in an unhelpful CIL assembly related error. -- {{Jira|SCR-256}}
**Attempting this will result in an unhelpful CIL assembly related error. -- {{Jira|SCR-256}}

Revision as of 13:32, 20 January 2012

The correct title of this article is jump. The initial letter is shown capitalized due to technical restrictions.

jump target;

jump target;
• label target Name of a label inside the same function or event scope.

@target;

• label target A label that can be jumped to, if the jump is in the same scope or child scope. It isn't possible to jump between scopes (such as between functions, events or states).

Caveats

  • If multiple jump sites are declared for the same target label within scope, then only the first (top to bottom, left to right) will function as expected, all others will silently fail.
  • Labels are scoped at the event and function level, meaning that it is not possible to declare duplicate labels within the same event or function, even if the labels are enclosed in different if-statements, loops etc. -- SVC-6712
    • Attempting this will result in an unhelpful CIL assembly related error. -- SCR-256
  • If code exists after a return that is not encapsulated in a flow control structure, the compiler will return an error about the code being dead even if the code is accessible with a jump. -- SVC-1929

Examples

<lsl>integer a = 5; jump over; @in; a = 6; @over; llOwnerSay((string)a); if(a < 6)

   jump in;

//script will say 5 and then 6</lsl> <lsl>integer getLinkWithName(string name) {

   integer i = llGetLinkNumber() != 0;   // Start at zero (single prim) or 1 (two or more prims)
   integer x = llGetNumberOfPrims() + i; // [0, 1) or [1, llGetNumberOfPrims()]
   for (; i < x; ++i) {
       if (llGetLinkName(i) == name) 
           jump break; // Found it! Exit loop early with result
   }
   i = -1; // No prim with that name, return -1.
   @break;
   return i;

}</lsl>

Notes

It is generally considered inadvisable to use jumps (commonly know as gotos) where other flow control structures could be used.

Multiple Jumps Bug

The following function, jumpy, demonstrates the multiple-jump-sites-to-a-single-target bug. It appears this function should result in an infinite loop that will never end however because of this bug the second jump never happens and the function returns. LL has reported they will never fix this bug as it would be a breaking change. <lsl>jumpy(){

   jump next;//first jump
   @next;
   jump next;//second jump - BUG never gets executed
   return;

}</lsl>

See Also

Keywords

•  return
•  state