-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChapter12.cs
More file actions
81 lines (70 loc) · 1.6 KB
/
Chapter12.cs
File metadata and controls
81 lines (70 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WattleScript.Interpreter;
using WattleScript.Interpreter.Loaders;
using WattleScript.Interpreter.Platforms;
using WattleScript.RemoteDebugger;
namespace Tutorials.Chapters
{
[Tutorial]
static class Chapter12
{
[Tutorial]
static void CoroutinesFromCSharp()
{
string code = @"
return function()
local x = 0
while true do
x = x + 1
coroutine.yield(x)
end
end
";
// Load the code and get the returned function
Script script = new Script();
DynValue function = script.DoString(code);
// Create the coroutine in C#
DynValue coroutine = script.CreateCoroutine(function);
// Resume the coroutine forever and ever..
while (true)
{
DynValue x = coroutine.Coroutine.Resume();
Console.WriteLine("{0}", x);
}
}
[Tutorial]
static void CoroutinesAsCSharpIterator()
{
string code = @"
return function()
local x = 0
while true do
x = x + 1
coroutine.yield(x)
if (x > 5) then
return 7
end
end
end
";
// Load the code and get the returned function
Script script = new Script();
DynValue function = script.DoString(code);
// Create the coroutine in C#
DynValue coroutine = script.CreateCoroutine(function);
// Loop the coroutine
string ret = "";
foreach (DynValue x in coroutine.Coroutine.AsTypedEnumerable())
{
ret = ret + x.ToString();
}
Console.WriteLine(ret);
}
}
}