Files
wren/core/fiber.html
Bob Nystrom 0a1c30118f Regenerate.
2015-03-13 07:58:04 -07:00

127 lines
7.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Fiber Class Wren</title>
<link rel="stylesheet" type="text/css" href="../style.css" />
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel='stylesheet' type='text/css'>
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
the viewport. -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
</head>
<body id="top" class="core">
<header>
<div class="page">
<div class="main-column">
<h1><a href="../">wren</a></h1>
<h2>a classy little scripting language</h2>
</div>
</div>
</header>
<div class="page">
<nav>
<ul>
<li><a href="./">Core Library</a></li>
</ul>
<section>
<h2>core classes</h2>
<ul>
<li><a href="bool.html">Bool</a></li>
<li><a href="class.html">Class</a></li>
<li><a href="fiber.html">Fiber</a></li>
<li><a href="fn.html">Fn</a></li>
<li><a href="list.html">List</a></li>
<li><a href="map.html">Map</a></li>
<li><a href="null.html">Null</a></li>
<li><a href="num.html">Num</a></li>
<li><a href="object.html">Object</a></li>
<li><a href="range.html">Range</a></li>
<li><a href="sequence.html">Sequence</a></li>
<li><a href="string.html">String</a></li>
</ul>
</section>
</nav>
<main>
<h1>Fiber Class</h1>
<p>A lightweight coroutine. <a href="../fibers.html">Here</a> is a gentle introduction.</p>
<h3>new <strong>Fiber</strong>(function) <a href="#new-fiber(function)" name="new-fiber(function)" class="header-anchor">#</a></h3>
<p>Creates a new fiber that executes <code>function</code> in a separate coroutine when the
fiber is run. Does not immediately start running the fiber.</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="n">fiber</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Fiber</span> <span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;I won&#39;t get printed&quot;</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
<h3>Fiber.<strong>current</strong> <a href="#fibercurrent" name="fibercurrent" class="header-anchor">#</a></h3>
<p>The currently executing fiber.</p>
<h3>Fiber.<strong>yield</strong>() <a href="#fiberyield()" name="fiberyield()" class="header-anchor">#</a></h3>
<p>Pauses the current fiber and transfers control to the parent fiber. "Parent"
here means the last fiber that was started using <code>call</code> and not <code>run</code>.</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="n">fiber</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Fiber</span> <span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;Before yield&quot;</span><span class="p">)</span>
<span class="n">Fiber</span><span class="p">.</span><span class="n">yield</span><span class="p">()</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;After yield&quot;</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">fiber</span><span class="p">.</span><span class="n">call</span><span class="p">()</span> <span class="c1">// &quot;Before yield&quot;</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;After call&quot;</span><span class="p">)</span> <span class="c1">// &quot;After call&quot;</span>
<span class="n">fiber</span><span class="p">.</span><span class="n">call</span><span class="p">()</span> <span class="c1">// &quot;After yield&quot;</span>
</pre></div>
<p>When resumed, the parent fiber's <code>call()</code> method returns <code>null</code>.</p>
<p>If a yielded fiber is resumed by calling <code>call()</code> or <code>run()</code> with an argument,
<code>yield()</code> returns that value.</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="n">fiber</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Fiber</span> <span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">Fiber</span><span class="p">.</span><span class="n">yield</span><span class="p">())</span> <span class="c1">// &quot;value&quot;</span>
<span class="p">}</span>
<span class="n">fiber</span><span class="p">.</span><span class="n">call</span><span class="p">()</span> <span class="c1">// Run until the first yield.</span>
<span class="n">fiber</span><span class="p">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;value&quot;</span><span class="p">)</span> <span class="c1">// Resume the fiber.</span>
</pre></div>
<p>If it was resumed by calling <code>call()</code> or <code>run()</code> with no argument, it returns
<code>null</code>.</p>
<p>If there is no parent fiber to return to, this exits the interpreter. This can
be useful to pause execution until the host application wants to resume it
later.</p>
<div class="codehilite"><pre><span class="n">Fiber</span><span class="p">.</span><span class="n">yield</span><span class="p">()</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;this does not get reached&quot;</span><span class="p">)</span>
</pre></div>
<h3>Fiber.<strong>yield</strong>(value) <a href="#fiberyield(value)" name="fiberyield(value)" class="header-anchor">#</a></h3>
<p>Similar to <code>Fiber.yield</code> but provides a value to return to the parent fiber's
<code>call</code>.</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="n">fiber</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Fiber</span> <span class="p">{</span>
<span class="n">Fiber</span><span class="p">.</span><span class="n">yield</span><span class="p">(</span><span class="s2">&quot;value&quot;</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">fiber</span><span class="p">.</span><span class="n">call</span><span class="p">())</span> <span class="c1">// &quot;value&quot;</span>
</pre></div>
<h3><strong>call</strong>() <a href="#call()" name="call()" class="header-anchor">#</a></h3>
<p><strong>TODO</strong></p>
<h3><strong>call</strong>(value) <a href="#call(value)" name="call(value)" class="header-anchor">#</a></h3>
<p><strong>TODO</strong></p>
<h3><strong>isDone</strong> <a href="#isdone" name="isdone" class="header-anchor">#</a></h3>
<p>Whether the fiber's main function has completed and the fiber can no longer be
run. This returns <code>false</code> if the fiber is currently running or has yielded.</p>
<h3><strong>run</strong>() <a href="#run()" name="run()" class="header-anchor">#</a></h3>
<p><strong>TODO</strong></p>
<h3><strong>run</strong>(value) <a href="#run(value)" name="run(value)" class="header-anchor">#</a></h3>
<p><strong>TODO</strong></p>
</main>
</div>
<footer>
<div class="page">
<div class="main-column">
<p>Wren lives <a href="https://github.com/munificent/wren">on GitHub</a> &mdash; Made with &#x2764; by <a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a>.</p>
<div class="main-column">
</div>
</footer>
</body>
</html>