Program termination: Difference between revisions

Added Wren
(Added Wren)
Line 1,578:
'...
End Sub</lang>
 
=={{header|Wren}}==
All Wren code runs within the context of a ''fiber''. Fibers are similar to lightweight co-operatively scheduled threads except that they are managed entirely by Wren's VM and don't use OS thread resources.
 
A main fiber is created automatically when a script begins and further fibers can then be created as required.
 
To terminate a script from within a conditional, two options are available: Fiber.suspend or Fiber.abort.
 
Fiber.suspend pauses the current fiber, stops the interpreter and returns control to the host application though the fiber can be resumed later. If called from a Wren CLI script, control simply returns to the operating system as there is no host application as such and therefore no possibility of resumption.
 
Fiber.abort generally means that a runtime error has occurred though you can still call it if you wish with either no error or a manufactured error. After aborting the current fiber, if the error is not caught, all invoking fibers up to the main one are also aborted and the VM is exited with an appropriate error message and stack trace. As far as I know, no special cleanup is provided by the VM itself.
 
The following example illustrates the use of Fiber.suspend in a Wren CLI script.
<lang ecmascript>import "io" for Stdin, Stdout
 
System.write("Do you want to terminate the program y/n ? ")
Stdout.flush()
var yn = Stdin.readLine()
if (yn == "y" || yn == "Y") {
System.print("OK, shutting down")
Fiber.suspend() // return to OS
}
System.print("OK, carrying on")</lang>
 
{{out}}
<pre>
$ wren_cli program_termination.wren
Do you want to terminate the program y/n ? y
OK, shutting down
$
</pre>
 
=={{header|XPL0}}==
9,477

edits