-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScript.cs
More file actions
executable file
·1140 lines (987 loc) · 39.6 KB
/
Script.cs
File metadata and controls
executable file
·1140 lines (987 loc) · 39.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WattleScript.Interpreter.CoreLib;
using WattleScript.Interpreter.Debugging;
using WattleScript.Interpreter.Diagnostics;
using WattleScript.Interpreter.Execution;
using WattleScript.Interpreter.Execution.VM;
using WattleScript.Interpreter.Interop;
using WattleScript.Interpreter.IO;
using WattleScript.Interpreter.Platforms;
using WattleScript.Interpreter.Tree;
using WattleScript.Interpreter.Tree.Expressions;
using WattleScript.Interpreter.Tree.Fast_Interface;
namespace WattleScript.Interpreter
{
/// <summary>
/// This class implements a WattleScript scripting session. Multiple Script objects can coexist in the same program but cannot share
/// data among themselves unless some mechanism is put in place.
/// </summary>
public class Script : IScriptPrivateResource
{
public enum ScriptParserMessageType
{
Error,
Warning,
Info
}
public class ScriptParserMessage
{
public string Msg { get; set; }
private Token Token { get; set; }
public ScriptParserMessageType Type { get; set; }
internal ScriptParserMessage(Token token)
{
Token = token;
Msg = $"unexpected symbol near '{token}'";
}
internal ScriptParserMessage(Token token, string msg)
{
Token = token;
Msg = msg;
}
}
/// <summary>
/// The version of the WattleScript engine
/// </summary>
public static readonly string VERSION;
/// <summary>
/// The version of the WattleScript engine as a double.
/// </summary>
public static readonly double VERSION_NUMBER;
/// <summary>
/// The Lua version being supported
/// </summary>
public const string LUA_VERSION = "5.2";
private Processor m_MainProcessor;
private List<SourceCode> m_Sources = new List<SourceCode>();
private Table m_GlobalTable;
private IDebugger m_Debugger;
private Table[] m_TypeMetatables = new Table[(int)LuaTypeExtensions.MaxMetaTypes];
private Table m_TablePrototype;
internal List<ScriptParserMessage> i_ParserMessages { get; set; } = new List<ScriptParserMessage>();
/// <summary>
/// Initializes the <see cref="Script"/> class.
/// </summary>
static Script()
{
GlobalOptions = new ScriptGlobalOptions();
DefaultOptions = new ScriptOptions()
{
DebugPrint = s => { GlobalOptions.Platform.DefaultPrint(s); },
DebugInput = s => GlobalOptions.Platform.DefaultInput(s),
CheckThreadAccess = true,
ScriptLoader = PlatformAutoDetector.GetDefaultScriptLoader(),
TailCallOptimizationThreshold = 65536
};
Version ver = typeof(Script).Assembly.GetName().Version;
VERSION = ver.ToString();
VERSION_NUMBER = ver.Major + ver.Minor / Math.Pow(10, Math.Floor(Math.Log10(ver.Minor) + 1));
}
/// <summary>
/// Initializes a new instance of the <see cref="Script"/> clas.s
/// </summary>
public Script()
: this(CoreModules.Preset_Default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Script"/> class.
/// </summary>
/// <param name="coreModules">The core modules to be pre-registered in the default global table.</param>
public Script(CoreModules coreModules)
{
Options = new ScriptOptions(DefaultOptions);
PerformanceStats = new PerformanceStatistics();
Registry = new Table(this);
m_MainProcessor = new Processor(this, m_GlobalTable);
m_GlobalTable = new Table(this).RegisterCoreModules(coreModules);
}
/// <summary>
/// Gets or sets the script loader which will be used as the value of the
/// ScriptLoader property for all newly created scripts.
/// </summary>
public static ScriptOptions DefaultOptions { get; private set; }
/// <summary>
/// Gets access to the script options.
/// </summary>
public ScriptOptions Options { get; private set; }
/// <summary>
/// Gets the global options, that is options which cannot be customized per-script.
/// </summary>
public static ScriptGlobalOptions GlobalOptions { get; private set; }
/// <summary>
/// Gets access to performance statistics.
/// </summary>
public PerformanceStatistics PerformanceStats { get; private set; }
/// <summary>
/// Gets the default global table for this script. Unless a different table is intentionally passed (or setfenv has been used)
/// execution uses this table.
/// </summary>
public Table Globals => m_GlobalTable;
/// <summary>
/// Loads a string containing a Lua/WattleScript function.
/// </summary>
/// <param name="code">The code.</param>
/// <param name="globalTable">The global table to bind to this chunk.</param>
/// <param name="funcFriendlyName">Name of the function used to report errors, etc.</param>
/// <returns>
/// A DynValue containing a function which will execute the loaded code.
/// </returns>
public DynValue LoadFunction(string code, Table globalTable = null, string funcFriendlyName = null)
{
this.CheckScriptOwnership(globalTable);
string chunkName = string.Format("libfunc_{0}", funcFriendlyName ?? m_Sources.Count.ToString());
SourceCode source = new SourceCode(chunkName, code, m_Sources.Count, this);
m_Sources.Add(source);
var func = Loader_Fast.LoadFunction(this, source, globalTable != null || m_GlobalTable != null);
SignalSourceCodeChange(source);
SignalByteCodeChange();
return MakeClosure(func, globalTable ?? m_GlobalTable);
}
/// <summary>
/// no-op
/// </summary>
private void SignalByteCodeChange()
{
if (m_Debugger != null)
{
//TODO: This is no-op for now
}
}
private void SignalSourceCodeChange(SourceCode source)
{
if (m_Debugger != null)
{
m_Debugger.SetSourceCode(source);
}
}
/// <summary>
/// Loads a string containing a Lua/WattleScript script.
/// </summary>
/// <param name="code">The code.</param>
/// <param name="globalTable">The global table to bind to this chunk.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing a function which will execute the loaded code.
/// </returns>
public DynValue LoadString(string code, Table globalTable = null, string codeFriendlyName = null)
{
this.CheckScriptOwnership(globalTable);
if (code.StartsWith(StringModule.BASE64_DUMP_HEADER))
{
code = code.Substring(StringModule.BASE64_DUMP_HEADER.Length);
byte[] data = Convert.FromBase64String(code);
using MemoryStream ms = new MemoryStream(data);
return LoadStream(ms, globalTable, codeFriendlyName);
}
string chunkName = string.Format("{0}", codeFriendlyName ?? "chunk_" + m_Sources.Count.ToString());
SourceCode source = new SourceCode(codeFriendlyName ?? chunkName, code, m_Sources.Count, this);
m_Sources.Add(source);
FunctionProto func = Loader_Fast.LoadChunk(this, source);
SignalSourceCodeChange(source);
SignalByteCodeChange();
return MakeClosure(func, globalTable ?? m_GlobalTable);
}
/// <summary>
/// Loads a Lua/WattleScript script from a System.IO.Stream. NOTE: This will *NOT* close the stream!
/// </summary>
/// <param name="stream">The stream containing code.</param>
/// <param name="globalTable">The global table to bind to this chunk.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc.</param>
/// <returns>
/// A DynValue containing a function which will execute the loaded code.
/// </returns>
public DynValue LoadStream(Stream stream, Table globalTable = null, string codeFriendlyName = null)
{
this.CheckScriptOwnership(globalTable);
if (!Processor.IsDumpStream(stream))
{
using StreamReader sr = new StreamReader(stream, Encoding.UTF8, true, 4096, true);
string scriptCode = sr.ReadToEnd();
return LoadString(scriptCode, globalTable, codeFriendlyName);
}
string chunkName = string.Format("{0}", codeFriendlyName ?? "dump_" + m_Sources.Count);
SourceCode source = new SourceCode(codeFriendlyName ?? chunkName,
string.Format("-- This script was decoded from a binary dump - dump_{0}", m_Sources.Count),
m_Sources.Count, this);
m_Sources.Add(source);
FunctionProto func = m_MainProcessor.Undump(stream, m_Sources.Count - 1);
SignalSourceCodeChange(source);
SignalByteCodeChange();
return MakeClosure(func, globalTable ?? m_GlobalTable);
}
/// <summary>
/// Dumps on the specified stream.
/// </summary>
/// <param name="function">The function.</param>
/// <param name="stream">The stream.</param>
/// <param name="writeSourceRefs">Write referenced line numbers</param>
/// <exception cref="System.ArgumentException">
/// function arg is not a function!
/// or
/// stream is readonly!
/// or
/// function arg has upvalues other than _ENV
/// </exception>
public void Dump(DynValue function, Stream stream, bool writeSourceRefs = true)
{
this.CheckScriptOwnership(function);
if (function.Type != DataType.Function)
throw new ArgumentException("function arg is not a function!");
if (!stream.CanWrite)
throw new ArgumentException("stream is readonly!");
Closure.UpvaluesType upvaluesType = function.Function.GetUpvaluesType();
if (upvaluesType == Closure.UpvaluesType.Closure)
throw new ArgumentException("function arg has upvalues other than _ENV");
m_MainProcessor.Dump(stream, function.Function.Function, writeSourceRefs);
}
/// <summary>
/// Dumps bytecode to byte[]
/// </summary>
/// <param name="function">The function.</param>
/// <param name="writeSourceRefs">Write referenced line numbers</param>
/// <exception cref="System.ArgumentException">
/// function arg is not a function!
/// or
/// stream is readonly!
/// or
/// function arg has upvalues other than _ENV
/// </exception>
public byte[] Dump(DynValue function, bool writeSourceRefs = true)
{
this.CheckScriptOwnership(function);
if (function.Type != DataType.Function)
throw new ArgumentException("function arg is not a function!");
Closure.UpvaluesType upvaluesType = function.Function.GetUpvaluesType();
if (upvaluesType == Closure.UpvaluesType.Closure)
throw new ArgumentException("function arg has upvalues other than _ENV");
using MemoryStream ms = new MemoryStream();
m_MainProcessor.Dump(ms, function.Function.Function, writeSourceRefs);
return ms.ToArray();
}
/// <summary>
/// Dumps the bytecode for a function to a human-readable string
/// </summary>
/// <param name="function"></param>
public string DumpString(DynValue function)
{
this.CheckScriptOwnership(function);
if (function.Type != DataType.Function)
throw new ArgumentException("function arg is not a function!");
return m_MainProcessor.DumpString(function.Function.Function);
}
/// <summary>
/// Loads a string containing a Lua/WattleScript script.
/// </summary>
/// <param name="filename">The code.</param>
/// <param name="globalContext">The global table to bind to this chunk.</param>
/// <param name="friendlyFilename">The filename to be used in error messages.</param>
/// <returns>
/// A DynValue containing a function which will execute the loaded code.
/// </returns>
public DynValue LoadFile(string filename, Table globalContext = null, string friendlyFilename = null)
{
this.CheckScriptOwnership(globalContext);
#pragma warning disable 618
filename = Options.ScriptLoader.ResolveFileName(filename, globalContext ?? m_GlobalTable);
#pragma warning restore 618
object code = Options.ScriptLoader.LoadFile(filename, globalContext ?? m_GlobalTable);
switch (code)
{
case string s:
return LoadString(s, globalContext, friendlyFilename ?? filename);
case byte[] bytes:
{
using MemoryStream ms = new MemoryStream(bytes);
return LoadStream(ms, globalContext, friendlyFilename ?? filename);
}
case Stream stream:
{
try
{
return LoadStream(stream, globalContext, friendlyFilename ?? filename);
}
finally
{
stream.Dispose();
}
}
case null:
throw new InvalidCastException("Unexpected null from IScriptLoader.LoadFile");
default:
throw new InvalidCastException(string.Format("Unsupported return type from IScriptLoader.LoadFile : {0}", code.GetType()));
}
}
/// <summary>
/// Loads and executes a string containing a Lua/WattleScript script.
/// </summary>
/// <param name="code">The code.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public DynValue DoString(string code, Table globalContext = null, string codeFriendlyName = null)
{
DynValue func = LoadString(code, globalContext, codeFriendlyName);
return Call(func);
}
/// <summary>
/// Loads and asynchronously executes a string containing a Lua/WattleScript script.
/// </summary>
/// <param name="code">The code.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public Task<DynValue> DoStringAsync(string code, Table globalContext = null, string codeFriendlyName = null)
{
DynValue func = LoadString(code, globalContext, codeFriendlyName);
return CallAsync(func);
}
/// <summary>
/// Loads and executes a stream containing a Lua/WattleScript script.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public DynValue DoStream(Stream stream, Table globalContext = null, string codeFriendlyName = null)
{
DynValue func = LoadStream(stream, globalContext, codeFriendlyName);
return Call(func);
}
/// <summary>
/// Loads and asynchronously executes a stream containing a Lua/WattleScript script.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public Task<DynValue> DoStreamAsync(Stream stream, Table globalContext = null, string codeFriendlyName = null)
{
DynValue func = LoadStream(stream, globalContext, codeFriendlyName);
return CallAsync(func);
}
/// <summary>
/// Loads and asynchronously executes a bytecode array containing a Lua/WattleScript script.
/// </summary>
/// <param name="bytecode">The bytecode.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public Task<DynValue> DoBytecodeAsync(byte[] bytecode, Table globalContext = null, string codeFriendlyName = null)
{
using MemoryStream ms = new MemoryStream(bytecode);
DynValue func = LoadStream(ms, globalContext, codeFriendlyName);
return CallAsync(func);
}
/// <summary>
/// Loads and executes a bytecode array containing a Lua/WattleScript script.
/// </summary>
/// <param name="bytecode">The bytecode.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public DynValue DoBytecode(byte[] bytecode, Table globalContext = null, string codeFriendlyName = null)
{
using MemoryStream ms = new MemoryStream(bytecode);
DynValue func = LoadStream(ms, globalContext, codeFriendlyName);
return Call(func);
}
/// <summary>
/// Loads and executes a file containing a Lua/WattleScript script.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
/// <returns>
/// A DynValue containing the result of the processing of the loaded chunk.
/// </returns>
public DynValue DoFile(string filename, Table globalContext = null, string codeFriendlyName = null)
{
DynValue func = LoadFile(filename, globalContext, codeFriendlyName);
return Call(func);
}
/// <summary>
/// Runs the specified file with all possible defaults for quick experimenting.
/// </summary>
/// <param name="filename">The filename.</param>
/// A DynValue containing the result of the processing of the executed script.
public static DynValue RunFile(string filename)
{
Script S = new Script();
return S.DoFile(filename);
}
/// <summary>
/// Runs the specified code with all possible defaults for quick experimenting.
/// </summary>
/// <param name="code">The Lua/WattleScript code.</param>
/// A DynValue containing the result of the processing of the executed script.
public static DynValue RunString(string code)
{
return new Script().DoString(code);
}
/// <summary>
/// Creates a closure from a bytecode address.
/// </summary>
/// <param name="proto">The function prototype.</param>
/// <param name="envTable">The env table to create a 0-upvalue</param>
/// <returns></returns>
private DynValue MakeClosure(FunctionProto proto, Table envTable = null)
{
this.CheckScriptOwnership(envTable);
Closure c;
if (envTable == null)
{
if ((proto.flags & FunctionFlags.IsChunk) == FunctionFlags.IsChunk)
{
c = new Closure(this, proto,
new SymbolRef[] { SymbolRef.Upvalue(WellKnownSymbols.ENV, 0) },
new Upvalue[1]);
}
else
{
c = new Closure(this, proto, Array.Empty<SymbolRef>(), Array.Empty<Upvalue>());
}
}
else
{
var syms = new SymbolRef[] {
new SymbolRef() { i_Env = null, i_Index= 0, i_Name = WellKnownSymbols.ENV, i_Type = SymbolRefType.DefaultEnv },
};
var vals = new Upvalue[] {
Upvalue.Create(DynValue.NewTable(envTable))
};
c = new Closure(this, proto, syms, vals);
}
return DynValue.NewClosure(c);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <returns>
/// The return value(s) of the function call.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue Call(DynValue function)
{
return Call(function, Array.Empty<DynValue>());
}
public Task<DynValue> CallAsync(DynValue function)
{
return CallAsync(function, Array.Empty<DynValue>());
}
/// <summary>
/// Calls the specified function, marking the first parameter as this/self.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns>
/// The return value(s) of the function call.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
/// <exception cref="System.ArgumentException">Thrown if args.Length is less than 1</exception>
public DynValue ThisCall(DynValue function, params DynValue[] args)
{
this.CheckScriptOwnership(function);
this.CheckScriptOwnership(args);
if (args == null || args.Length < 1)
throw new ArgumentException("args");
if (function.Type != DataType.Function && function.Type != DataType.ClrFunction)
{
DynValue metafunction = m_MainProcessor.GetMetamethod(function, "__call");
if (metafunction.IsNotNil())
{
DynValue[] metaargs = new DynValue[args.Length + 1];
metaargs[0] = function;
for (int i = 0; i < args.Length; i++)
metaargs[i + 1] = args[i];
function = metafunction;
args = metaargs;
}
else
{
throw new ArgumentException("function is not a function and has no __call metamethod.");
}
}
else if (function.Type == DataType.ClrFunction)
{
return function.Callback.ClrCallback(CreateDynamicExecutionContext(function.Callback), new CallbackArguments(args, DynValue.Nil, true));
}
return m_MainProcessor.ThisCall(function, args);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns>
/// The return value(s) of the function call.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> ThisCallAsync(DynValue function, params DynValue[] args)
{
this.CheckScriptOwnership(function);
this.CheckScriptOwnership(args);
if (args == null || args.Length < 1)
throw new ArgumentException("args");
if (function.Type != DataType.Function && function.Type != DataType.ClrFunction)
{
DynValue metafunction = m_MainProcessor.GetMetamethod(function, "__call");
if (metafunction.IsNotNil())
{
DynValue[] metaargs = new DynValue[args.Length + 1];
metaargs[0] = function;
for (int i = 0; i < args.Length; i++)
metaargs[i + 1] = args[i];
function = metafunction;
args = metaargs;
}
else
{
throw new ArgumentException("function is not a function and has no __call metamethod.");
}
}
else if (function.Type == DataType.ClrFunction)
{
return Task.FromResult(function.Callback.ClrCallback(
CreateDynamicExecutionContext(function.Callback), new CallbackArguments(args, DynValue.Nil, true)));
}
return m_MainProcessor.ThisCallAsync(function, args);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns>
/// The return value(s) of the function call.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue Call(DynValue function, params DynValue[] args)
{
this.CheckScriptOwnership(function);
this.CheckScriptOwnership(args);
if (function.Type != DataType.Function && function.Type != DataType.ClrFunction)
{
DynValue metafunction = m_MainProcessor.GetMetamethod(function, "__call");
if (metafunction.IsNotNil())
{
DynValue[] metaargs = new DynValue[args.Length + 1];
metaargs[0] = function;
for (int i = 0; i < args.Length; i++)
metaargs[i + 1] = args[i];
function = metafunction;
args = metaargs;
}
else
{
throw new ArgumentException("function is not a function and has no __call metamethod.");
}
}
else if (function.Type == DataType.ClrFunction)
{
return function.Callback.ClrCallback(CreateDynamicExecutionContext(function.Callback), new CallbackArguments(args, DynValue.Nil, false));
}
return m_MainProcessor.Call(function, args);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns>
/// The return value(s) of the function call.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> CallAsync(DynValue function, params DynValue[] args)
{
this.CheckScriptOwnership(function);
this.CheckScriptOwnership(args);
if (function.Type != DataType.Function && function.Type != DataType.ClrFunction)
{
DynValue metafunction = m_MainProcessor.GetMetamethod(function, "__call");
if (metafunction.IsNotNil())
{
DynValue[] metaargs = new DynValue[args.Length + 1];
metaargs[0] = function;
for (int i = 0; i < args.Length; i++)
metaargs[i + 1] = args[i];
function = metafunction;
args = metaargs;
}
else
{
throw new ArgumentException("function is not a function and has no __call metamethod.");
}
}
else if (function.Type == DataType.ClrFunction)
{
return Task.FromResult(function.Callback.ClrCallback(
CreateDynamicExecutionContext(function.Callback), new CallbackArguments(args, DynValue.Nil, false)));
}
return m_MainProcessor.CallAsync(function, args);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns>
/// The return value(s) of the function call.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue Call(DynValue function, params object[] args)
{
DynValue[] dargs = new DynValue[args.Length];
for (int i = 0; i < dargs.Length; i++)
dargs[i] = DynValue.FromObject(this, args[i]);
return Call(function, dargs);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue Call(object function)
{
return Call(DynValue.FromObject(this, function));
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue Call(object function, params object[] args)
{
return Call(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Calls the specified function.
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue Call(object function, params DynValue[] args)
{
return Call(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue ThisCall(object function, params DynValue[] args)
{
return ThisCall(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function. Converted to DynValue[] via DynValue.FromObject()</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue ThisCall(object function, params object[] args)
{
return ThisCall(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function. Converted to DynValue[] via DynValue.FromObject()</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public DynValue ThisCall(DynValue function, params object[] args)
{
DynValue[] dargs = new DynValue[args.Length];
for (int i = 0; i < dargs.Length; i++)
dargs[i] = DynValue.FromObject(this, args[i]);
return ThisCall(function, dargs);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> ThisCallAsync(object function, params DynValue[] args)
{
return ThisCallAsync(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function. Converted to DynValue[] via DynValue.FromObject()</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> ThisCallAsync(object function, params object[] args)
{
return ThisCallAsync(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function. Converted to DynValue[] via DynValue.FromObject()</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> ThisCallAsync(DynValue function, params object[] args)
{
DynValue[] dargs = new DynValue[args.Length];
for (int i = 0; i < dargs.Length; i++)
dargs[i] = DynValue.FromObject(this, args[i]);
return ThisCallAsync(function, dargs);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function. Converted to DynValue[] via DynValue.FromObject()</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> CallAsync(DynValue function, params object[] args)
{
DynValue[] dargs = new DynValue[args.Length];
for (int i = 0; i < dargs.Length; i++)
dargs[i] = DynValue.FromObject(this, args[i]);
return CallAsync(function, dargs);
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> CallAsync(object function)
{
return CallAsync(DynValue.FromObject(this, function));
}
/// <summary>
/// Calls the specified function, passing the first parameter as this
/// </summary>
/// <param name="function">The Lua/WattleScript function to be called </param>
/// <param name="args">The arguments to pass to the function. Converted to DynValue[] via DynValue.FromObject()</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
public Task<DynValue> CallAsync(object function, params object[] args)
{
return CallAsync(DynValue.FromObject(this, function), args);
}
/// <summary>
/// Creates a coroutine pointing at the specified function.
/// </summary>
/// <param name="function">The function.</param>
/// <returns>
/// The coroutine handle.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function or DataType.ClrFunction</exception>
public DynValue CreateCoroutine(DynValue function)
{
this.CheckScriptOwnership(function);
return function.Type switch
{
DataType.Function => m_MainProcessor.Coroutine_Create(function.Function),
DataType.ClrFunction => DynValue.NewCoroutine(new Coroutine(function.Callback)),
_ => throw new ArgumentException("function is not of DataType.Function or DataType.ClrFunction")
};
}
/// <summary>
/// Creates a coroutine pointing at the specified function.
/// </summary>
/// <param name="function">The function.</param>
/// <returns>
/// The coroutine handle.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function or DataType.ClrFunction</exception>
public DynValue CreateCoroutine(object function)
{
return CreateCoroutine(DynValue.FromObject(this, function));
}
/// <summary>
/// Gets or sets a value indicating whether the debugger is enabled.
/// Note that unless a debugger attached, this property returns a
/// value which might not reflect the real status of the debugger.
/// Use this property if you want to disable the debugger for some
/// executions.
/// </summary>
public bool DebuggerEnabled
{
get => m_MainProcessor.DebuggerEnabled;
set => m_MainProcessor.DebuggerEnabled = value;
}
/// <summary>
/// Attaches a debugger. This usually should be called by the debugger itself and not by user code.
/// </summary>
/// <param name="debugger">The debugger object.</param>
public void AttachDebugger(IDebugger debugger)
{
DebuggerEnabled = true;
m_Debugger = debugger;
m_MainProcessor.AttachDebugger(debugger);
foreach (SourceCode src in m_Sources)
SignalSourceCodeChange(src);
SignalByteCodeChange();
}
/// <summary>
/// Gets the source code.
/// </summary>
/// <param name="sourceCodeID">The source code identifier.</param>
/// <returns></returns>
public SourceCode GetSourceCode(int sourceCodeID)
{
return m_Sources[sourceCodeID];
}
/// <summary>
/// Gets the source code count.
/// </summary>
/// <value>
/// The source code count.
/// </value>
public int SourceCodeCount => m_Sources.Count;