-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathCoroutineTests.cs
More file actions
368 lines (287 loc) · 7.81 KB
/
Copy pathCoroutineTests.cs
File metadata and controls
368 lines (287 loc) · 7.81 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoonSharp.Interpreter.Interop;
using NUnit.Framework;
namespace MoonSharp.Interpreter.Tests.EndToEnd
{
[TestFixture]
class CoroutineTests
{
private class YieldingIndexTarget
{
}
private class YieldingIndexDescriptor : IUserDataDescriptor
{
public string Name
{
get { return "YieldingIndexTarget"; }
}
public Type Type
{
get { return typeof(YieldingIndexTarget); }
}
public DynValue Index(Script script, object obj, DynValue index, bool isDirectIndexing)
{
return DynValue.NewYieldReq(new[] { DynValue.NewString("yielded:" + index.String) });
}
public bool SetIndex(Script script, object obj, DynValue index, DynValue value, bool isDirectIndexing)
{
return false;
}
public string AsString(object obj)
{
return null;
}
public DynValue MetaIndex(Script script, object obj, string metaname)
{
return null;
}
public bool IsTypeCompatible(Type type, object obj)
{
return type.IsInstanceOfType(obj);
}
}
[Test]
public void Coroutine_Basic()
{
string script = @"
s = ''
function foo()
for i = 1, 4 do
s = s .. i;
coroutine.yield();
end
end
function bar()
for i = 5, 9 do
s = s .. i;
coroutine.yield();
end
end
cf = coroutine.create(foo);
cb = coroutine.create(bar);
for i = 1, 4 do
coroutine.resume(cf);
s = s .. '-';
coroutine.resume(cb);
s = s .. ';';
end
return s;
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.String, res.Type);
Assert.AreEqual("1-5;2-6;3-7;4-8;", res.String);
}
[Test]
public void Coroutine_Wrap()
{
string script = @"
s = ''
function foo()
for i = 1, 4 do
s = s .. i;
coroutine.yield();
end
end
function bar()
for i = 5, 9 do
s = s .. i;
coroutine.yield();
end
end
cf = coroutine.wrap(foo);
cb = coroutine.wrap(bar);
for i = 1, 4 do
cf();
s = s .. '-';
cb();
s = s .. ';';
end
return s;
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.String, res.Type);
Assert.AreEqual("1-5;2-6;3-7;4-8;", res.String);
}
[Test]
public void Coroutine_ClrBoundaryHandling()
{
string code = @"
function a()
callback(b)
end
function b()
coroutine.yield();
end
c = coroutine.create(a);
return coroutine.resume(c);
";
// Load the code and get the returned function
Script script = new Script();
script.Globals["callback"] = DynValue.NewCallback(
(ctx, args) => args[0].Function.Call()
);
DynValue ret = script.DoString(code);
Assert.AreEqual(DataType.Tuple, ret.Type);
Assert.AreEqual(2, ret.Tuple.Length);
Assert.AreEqual(DataType.Boolean, ret.Tuple[0].Type);
Assert.AreEqual(false, ret.Tuple[0].Boolean);
Assert.AreEqual(DataType.String, ret.Tuple[1].Type);
Assert.IsTrue(ret.Tuple[1].String.EndsWith("attempt to yield across a CLR-call boundary"));
}
[Test]
public void Coroutine_VariousErrorHandling()
{
string last = "";
string code = @"
function checkresume(step, ex, ey)
local x, y = coroutine.resume(c)
assert(x == ex, 'Step ' .. step .. ': ' .. tostring(ex) .. ' was expected, got ' .. tostring(x));
assert(y:endsWith(ey), 'Step ' .. step .. ': ' .. tostring(ey) .. ' was expected, got ' .. tostring(y));
end
t = { }
m = { __tostring = function() print('2'); coroutine.yield(); print('3'); end }
setmetatable(t, m);
function a()
checkresume(1, false, 'cannot resume non-suspended coroutine');
coroutine.yield('ok');
print(t);
coroutine.yield('ok');
end
c = coroutine.create(a);
checkresume(2, true, 'ok');
checkresume(3, false, 'attempt to yield across a CLR-call boundary');
checkresume(4, false, 'cannot resume dead coroutine');
checkresume(5, false, 'cannot resume dead coroutine');
checkresume(6, false, 'cannot resume dead coroutine');
";
// Load the code and get the returned function
Script script = new Script();
script.Options.DebugPrint = (s) => last = s;
script.DoString(code);
Assert.AreEqual(last, "2");
}
[Test]
public void Coroutine_Direct_Resume()
{
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 = "";
while (coroutine.Coroutine.State != CoroutineState.Dead)
{
DynValue x = coroutine.Coroutine.Resume();
ret = ret + x.ToString();
}
Assert.AreEqual("1234567", ret);
}
[Test]
public void Coroutine_UserDataIndexCanYield()
{
string code = @"
return function(target)
state = 'before'
local value = target.value
state = 'after'
return value
end
";
Script script = new Script();
DynValue function = script.DoString(code);
DynValue coroutine = script.CreateCoroutine(function);
DynValue target = UserData.Create(new YieldingIndexTarget(), new YieldingIndexDescriptor());
DynValue yielded = coroutine.Coroutine.Resume(target);
Assert.AreEqual(CoroutineState.Suspended, coroutine.Coroutine.State);
Assert.AreEqual(DataType.String, yielded.Type);
Assert.AreEqual("yielded:value", yielded.String);
Assert.AreEqual("before", script.Globals.Get("state").String);
DynValue returned = coroutine.Coroutine.Resume(DynValue.NewString("resumed"));
Assert.AreEqual(CoroutineState.Dead, coroutine.Coroutine.State);
Assert.AreEqual(DataType.String, returned.Type);
Assert.AreEqual("resumed", returned.String);
Assert.AreEqual("after", script.Globals.Get("state").String);
}
[Test]
public void Coroutine_Direct_AsEnumerable()
{
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();
}
Assert.AreEqual("1234567", ret);
}
[Test]
public void Coroutine_AutoYield()
{
string code = @"
function fib(n)
if (n == 0 or n == 1) then
return 1;
else
return fib(n - 1) + fib(n - 2);
end
end
";
// Load the code and get the returned function
Script script = new Script(CoreModules.None);
script.DoString(code);
// get the function
DynValue function = script.Globals.Get("fib");
// Create the coroutine in C#
DynValue coroutine = script.CreateCoroutine(function);
// Set the automatic yield counter every 10 instructions.
// 10 is a too small! Use a much bigger value in your code to avoid interrupting too often!
coroutine.Coroutine.AutoYieldCounter = 10;
int cycles = 0;
DynValue result = null;
// Cycle until we get that the coroutine has returned something useful and not an automatic yield..
for (result = coroutine.Coroutine.Resume(8);
result.Type == DataType.YieldRequest;
result = coroutine.Coroutine.Resume())
{
cycles += 1;
}
// Check the values of the operation
Assert.AreEqual(DataType.Number, result.Type);
Assert.AreEqual(34, result.Number);
// Check the autoyield actually triggered
Assert.IsTrue(cycles > 10);
}
}
}