-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChapter6.cs
More file actions
478 lines (372 loc) · 11.1 KB
/
Chapter6.cs
File metadata and controls
478 lines (372 loc) · 11.1 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WattleScript.Interpreter;
using WattleScript.Interpreter.Interop;
namespace Tutorials.Chapters
{
[Tutorial]
static class Chapter06
{
#region UserData classes
[WattleScriptUserData]
class MyClass
{
public event EventHandler SomethingHappened;
public void RaiseTheEvent()
{
if (SomethingHappened != null)
SomethingHappened(this, EventArgs.Empty);
}
public double calcHypotenuse(double a, double b)
{
return Math.Sqrt(a * a + b * b);
}
public string ManipulateString(string input, ref string tobeconcat, out string lowercase)
{
tobeconcat = input + tobeconcat;
lowercase = input.ToLower();
return input.ToUpper();
}
}
[WattleScriptUserData]
class MyClassStatic
{
public static double calcHypotenuse(double a, double b)
{
return Math.Sqrt(a * a + b * b);
}
}
class IndexerTestClass
{
Dictionary<int, int> mymap = new Dictionary<int, int>();
public int this[int idx]
{
get { return mymap[idx]; }
set { mymap[idx] = value; }
}
public int this[int idx1, int idx2, int idx3]
{
get { int idx = (idx1 + idx2) * idx3; return mymap[idx]; }
set { int idx = (idx1 + idx2) * idx3; mymap[idx] = value; }
}
}
class ArithmOperatorsTestClass : IComparable, System.Collections.IEnumerable
{
public int Value { get; set; }
public ArithmOperatorsTestClass()
{
}
public ArithmOperatorsTestClass(int value)
{
Value = value;
}
public int Length { get { return 117; } }
[WattleScriptUserDataMetamethod("__concat")]
public static int Concat(ArithmOperatorsTestClass o, int v)
{
return o.Value + v;
}
[WattleScriptUserDataMetamethod("__concat")]
public static int Concat(int v, ArithmOperatorsTestClass o)
{
return o.Value + v;
}
[WattleScriptUserDataMetamethod("__concat")]
public static int Concat(ArithmOperatorsTestClass o1, ArithmOperatorsTestClass o2)
{
return o1.Value + o2.Value;
}
public static int operator +(ArithmOperatorsTestClass o, int v)
{
return o.Value + v;
}
public static int operator +(int v, ArithmOperatorsTestClass o)
{
return o.Value + v;
}
public static int operator +(ArithmOperatorsTestClass o1, ArithmOperatorsTestClass o2)
{
return o1.Value + o2.Value;
}
public override bool Equals(object obj)
{
if (obj is double)
return ((double)obj) == Value;
ArithmOperatorsTestClass other = obj as ArithmOperatorsTestClass;
if (other == null) return false;
return Value == other.Value;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public int CompareTo(object obj)
{
if (obj is double)
return Value.CompareTo((int)(double)obj);
ArithmOperatorsTestClass other = obj as ArithmOperatorsTestClass;
if (other == null) return 1;
return Value.CompareTo(other.Value);
}
public System.Collections.IEnumerator GetEnumerator()
{
return (new List<int>() { 1, 2, 3 }).GetEnumerator();
}
[WattleScriptUserDataMetamethod("__call")]
public int DefaultMethod()
{
return -Value;
}
[WattleScriptUserDataMetamethod("__pairs")]
[WattleScriptUserDataMetamethod("__ipairs")]
public System.Collections.IEnumerator Pairs()
{
return (new List<DynValue>() {
DynValue.NewTuple(DynValue.NewString("a"), DynValue.NewString("A")),
DynValue.NewTuple(DynValue.NewString("b"), DynValue.NewString("B")),
DynValue.NewTuple(DynValue.NewString("c"), DynValue.NewString("C")) }).GetEnumerator();
}
}
#endregion
[Tutorial]
public static double CallMyClass1()
{
string scriptCode = @"
return obj.calcHypotenuse(3, 4);
";
// Automatically register all WattleScriptUserData types
UserData.RegisterAssembly();
Script script = new Script();
// Pass an instance of MyClass to the script in a global
script.Globals["obj"] = new MyClass();
DynValue res = script.DoString(scriptCode);
return res.Number;
}
[Tutorial]
static double CallMyClass2()
{
string scriptCode = @"
return obj.calcHypotenuse(3, 4);
";
// Register just MyClass, explicitely.
UserData.RegisterType<MyClass>();
Script script = new Script();
// create a userdata, again, explicitely.
DynValue obj = UserData.Create(new MyClass());
script.Globals.Set("obj", obj);
DynValue res = script.DoString(scriptCode);
return res.Number;
}
[Tutorial]
static double MyClassStaticThroughInstance()
{
string scriptCode = @"
return obj.calcHypotenuse(3, 4);
";
// Automatically register all WattleScriptUserData types
UserData.RegisterAssembly();
Script script = new Script();
script.Globals["obj"] = new MyClassStatic();
DynValue res = script.DoString(scriptCode);
return res.Number;
}
[Tutorial]
static double MyClassStaticThroughPlaceholder()
{
string scriptCode = @"
return obj.calcHypotenuse(3, 4);
";
// Automatically register all WattleScriptUserData types
UserData.RegisterAssembly();
Script script = new Script();
script.Globals["obj"] = typeof(MyClassStatic);
DynValue res = script.DoString(scriptCode);
return res.Number;
}
[Tutorial]
static string ByRefParams()
{
string scriptCode = @"
x, y, z = myobj:manipulateString('CiAo', 'hello');
return x, y, z
";
// Automatically register all WattleScriptUserData types
UserData.RegisterAssembly();
Script script = new Script();
script.Globals["myobj"] = new MyClass();
DynValue res = script.DoString(scriptCode);
return string.Join(", ", res.Tuple.Select(v => v.ToPrintString()));
}
[Tutorial]
static void IndexerTests()
{
string scriptCode = @"
-- sets the value of an indexer
o[5] = 19;
print(o[5]);
-- use the value of an indexer
x = 5 + o[5];
print(x);
-- sets the value of an indexer using multiple indices (not standard Lua!)
o[1,2,3] = 19;
print(o[1,2,3]);
-- use the value of an indexer using multiple indices (not standard Lua!)
x = 5 + o[1,2,3];
print(x);
";
UserData.RegisterType<IndexerTestClass>();
Script script = new Script();
script.Globals["o"] = new IndexerTestClass();
script.DoString(scriptCode);
}
[Tutorial]
static void OperatorsAndMetaMethods_WithAttributes()
{
string scriptCode = @"
print( o .. 1 );
print( 1 .. o );
print( o .. o );
";
UserData.RegisterType<ArithmOperatorsTestClass>();
Script script = new Script();
script.Globals["o"] = new ArithmOperatorsTestClass(5);
script.DoString(scriptCode);
}
[Tutorial]
static void OperatorsAndMetaMethods_WithOperatorsOverloads()
{
string scriptCode = @"
print( o + 1 );
print( 1 + o );
print( o + o );
";
UserData.RegisterType<ArithmOperatorsTestClass>();
Script script = new Script();
script.Globals["o"] = new ArithmOperatorsTestClass(5);
script.DoString(scriptCode);
}
[Tutorial]
static void OperatorsAndMetaMethods_Comparisons()
{
string scriptCode = @"
print( 'o == 1 ?', o == 1 );
print( '1 == o ?', 1 == o );
print( 'o == 5 ?', o == 5 );
print( 'o != 1 ?', o != 1 );
print( 'o < 1 ?', o < 1 );
print( 'o <= 1 ?', o <= 1 );
print( 'o < 6 ?', o < 6 );
print( 'o <= 6 ?', o <= 6 );
print( 'o > 1 ?', o > 1 );
print( 'o >= 1 ?', o >= 1 );
print( 'o > 6 ?', o > 6 );
print( 'o >= 6 ?', o >= 6 );
";
UserData.RegisterType<ArithmOperatorsTestClass>();
Script script = new Script();
script.Globals["o"] = new ArithmOperatorsTestClass(5);
script.DoString(scriptCode);
}
[Tutorial]
static void OperatorsAndMetaMethods_Length()
{
string scriptCode = @"
print( '#o + o ?', #o + o);
";
UserData.RegisterType<ArithmOperatorsTestClass>();
Script script = new Script();
script.Globals["o"] = new ArithmOperatorsTestClass(5);
script.DoString(scriptCode);
}
[Tutorial]
static void OperatorsAndMetaMethods_ForEach()
{
string scriptCode = @"
local sum = 0
for i in o do
sum = sum + i
end
print (sum);
";
UserData.RegisterType<ArithmOperatorsTestClass>();
Script script = new Script();
script.Globals["o"] = new ArithmOperatorsTestClass(5);
script.DoString(scriptCode);
}
[Tutorial]
static void Events()
{
string scriptCode = @"
function handler(o, a)
print('handled!', o, a);
end
myobj.somethingHappened.add(handler);
myobj.raiseTheEvent();
myobj.somethingHappened.remove(handler);
myobj.raiseTheEvent();
";
UserData.RegisterType<EventArgs>();
UserData.RegisterType<MyClass>();
Script script = new Script();
script.Globals["myobj"] = new MyClass();
script.DoString(scriptCode);
}
#pragma warning disable 67
#pragma warning disable 414
public class SampleClass
{
// Not visible - it's private
private void Method1() { }
// Visible - it's public
public void Method2() { }
// Visible - it's private but forced visible by attribute
[WattleScriptVisible(true)]
private void Method3() { }
// Not visible - it's public but forced hidden by attribute
[WattleScriptVisible(false)]
public void Method4() { }
// Not visible - it's private
private int Field1 = 0;
// Visible - it's public
public int Field2 = 0;
// Visible - it's private but forced visible by attribute
[WattleScriptVisible(true)]
private int Field3 = 0;
// Not visible - it's public but forced hidden by attribute
[WattleScriptVisible(false)]
public int Field4 = 0;
// Not visible at all - it's private
private int Property1 { get; set; }
// Read/write - it's public
public int Property2 { get; set; }
// Readonly - it's public, but the setter is private
public int Property3 { get; private set; }
// Write only! - the WattleScriptVisible makes the getter hidden and the setter visible!
public int Property4 { [WattleScriptVisible(false)] get; [WattleScriptVisible(true)] private set; }
// Write only! - the WattleScriptVisible makes the whole property hidden but another attribute resets the setter as visible!
[WattleScriptVisible(false)]
public int Property5 { get; [WattleScriptVisible(true)] private set; }
// Not visible at all - the WattleScriptVisible hides everything
[WattleScriptVisible(false)]
public int Property6 { get; set; }
// Not visible - it's private
private event EventHandler Event1;
// Visible - it's public
public event EventHandler Event2;
// Visible - it's private but forced visible by attribute
[WattleScriptVisible(true)]
private event EventHandler Event3;
// Not visible - it's public but forced hidden by attribute
[WattleScriptVisible(false)]
public event EventHandler Event4;
// Not visible - visibility modifiers over add and remove are not currently supported!
[WattleScriptVisible(false)]
public event EventHandler Event5 { [WattleScriptVisible(true)] add { } [WattleScriptVisible(true)] remove { } }
}
#pragma warning restore 414
#pragma warning restore 67
}
}