-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathHostFunctions.cs
More file actions
1381 lines (1293 loc) · 67.3 KB
/
HostFunctions.cs
File metadata and controls
1381 lines (1293 loc) · 67.3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
/// <summary>
/// Provides optional script-callable utility functions.
/// </summary>
/// <remarks>
/// Use <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c> to expose a
/// <c>HostFunctions</c> instance to script code. Each instance can only be exposed in one
/// script engine.
/// </remarks>
public class HostFunctions : IScriptableObject
{
private ScriptEngine engine;
// ReSharper disable EmptyConstructor
/// <summary>
/// Initializes a new <c><see cref="HostFunctions"/></c> instance.
/// </summary>
public HostFunctions()
{
// the help file builder (SHFB) insists on an empty constructor here
}
// ReSharper restore EmptyConstructor
#region script-callable interface
// ReSharper disable InconsistentNaming
// ReSharper disable GrammarMistakeInComment
/// <summary>
/// Creates an empty host object.
/// </summary>
/// <returns>A new empty host object.</returns>
/// <remarks>
/// This function is provided for script languages that do not support external
/// instantiation. It creates an object that supports dynamic property addition and
/// removal. The host can manipulate it via the <c><see cref="IPropertyBag"/></c> interface.
/// </remarks>
/// <example>
/// The following code creates an empty host object and adds several properties to it.
/// It assumes that an instance of <c><see cref="HostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// var item = host.newObj();
/// item.label = "Widget";
/// item.weight = 123.45;
/// </code>
/// </example>
public PropertyBag newObj()
{
return new PropertyBag();
}
// ReSharper restore GrammarMistakeInComment
/// <summary>
/// Creates a host object of the specified type. This version is invoked if the specified
/// type can be used as a type argument.
/// </summary>
/// <typeparam name="T">The type of object to create.</typeparam>
/// <param name="args">Optional constructor arguments.</param>
/// <returns>A new host object of the specified type.</returns>
/// <remarks>
/// <para>
/// This function is provided for script languages that do not support external
/// instantiation. It is overloaded with <c><see cref="newObj(object, object[])"/></c> and
/// selected at runtime if <typeparamref name="T"/> can be used as a type argument.
/// </para>
/// <para>
/// For information about the mapping between host members and script-callable properties
/// and methods, see
/// <c><see cref="ScriptEngine.AddHostObject(string, HostItemFlags, object)">AddHostObject</see></c>.
/// </para>
/// </remarks>
/// <example>
/// The following code imports the <c><see cref="System.Random"/></c> class, creates an
/// instance using the
/// <c><see href="https://docs.microsoft.com/en-us/dotnet/api/system.random.-ctor#System_Random__ctor_System_Int32_">Random(Int32)</see></c>
/// constructor, and calls the <c><see cref="System.Random.NextDouble"/></c> method.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// var RandomT = host.type("System.Random");
/// var random = host.newObj(RandomT, 100);
/// var value = random.NextDouble();
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public T newObj<T>(params object[] args)
{
return (T)typeof(T).CreateInstance(args);
}
/// <summary>
/// Creates a host object of the specified type. This version is invoked if the specified
/// type cannot be used as a type argument.
/// </summary>
/// <param name="type">The type of object to create.</param>
/// <param name="args">Optional constructor arguments.</param>
/// <returns>A new host object of the specified type.</returns>
/// <remarks>
/// <para>
/// This function is provided for script languages that do not support external
/// instantiation. It is overloaded with <c><see cref="newObj{T}"/></c> and selected at runtime if
/// <paramref name="type"/> cannot be used as a type argument. Note that this applies
/// to some host types that support instantiation, such as certain COM/ActiveX types.
/// </para>
/// <para>
/// For information about the mapping between host members and script-callable properties
/// and methods, see
/// <c><see cref="ScriptEngine.AddHostObject(string, HostItemFlags, object)">AddHostObject</see></c>.
/// </para>
/// </remarks>
public object newObj(object type, params object[] args)
{
return GetUniqueHostType(type, "type").CreateInstance(args);
}
/// <summary>
/// Performs dynamic instantiation.
/// </summary>
/// <param name="target">The dynamic host object that provides the instantiation operation to perform.</param>
/// <param name="args">Optional instantiation arguments.</param>
/// <returns>The result of the operation, which is usually a new dynamic host object.</returns>
/// <remarks>
/// This function is provided for script languages that do not support external
/// instantiation.
/// </remarks>
public object newObj(IDynamicMetaObjectProvider target, params object[] args)
{
MiscHelpers.VerifyNonNullArgument(target, nameof(target));
if (target.GetMetaObject(Expression.Constant(target)).TryCreateInstance(args, out var result))
{
return result;
}
throw new InvalidOperationException("Invalid dynamic instantiation");
}
/// <summary>
/// Creates a host array with the specified element type.
/// </summary>
/// <typeparam name="T">The element type of the array to create.</typeparam>
/// <param name="lengths">One or more integers representing the array dimension lengths.</param>
/// <returns>A new host array with the specified element type.</returns>
/// <remarks>
/// For information about the mapping between host members and script-callable properties
/// and methods, see
/// <c><see cref="ScriptEngine.AddHostObject(string, HostItemFlags, object)">AddHostObject</see></c>.
/// </remarks>
/// <example>
/// The following code creates a 5x3 host array of strings.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// var StringT = host.type("System.String");
/// var array = host.newArr(StringT, 5, 3);
/// </code>
/// </example>
/// <c><seealso cref="HostFunctions.newArr(int[])"/></c>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object newArr<T>(params int[] lengths)
{
return Array.CreateInstance(typeof(T), lengths);
}
/// <summary>
/// Creates a host array with <c><see cref="System.Object"/></c> as the element type.
/// </summary>
/// <param name="lengths">One or more integers representing the array dimension lengths.</param>
/// <returns>A new host array with <c><see cref="System.Object"/></c> as the element type.</returns>
/// <remarks>
/// For information about the mapping between host members and script-callable properties
/// and methods, see
/// <c><see cref="ScriptEngine.AddHostObject(string, HostItemFlags, object)">AddHostObject</see></c>.
/// </remarks>
/// <c><seealso cref="HostFunctions.newArr{T}(int[])"/></c>
public object newArr(params int[] lengths)
{
return newArr<object>(lengths);
}
// ReSharper disable GrammarMistakeInComment
/// <summary>
/// Creates a host variable of the specified type.
/// </summary>
/// <typeparam name="T">The type of variable to create.</typeparam>
/// <param name="initValue">An optional initial value for the variable.</param>
/// <returns>A new host variable of the specified type.</returns>
/// <remarks>
/// <para>
/// A host variable is a strongly typed object that holds a value of the specified type.
/// Host variables are useful for passing method arguments by reference. In addition to
/// being generally interchangeable with their stored values, host variables support the
/// following properties:
/// </para>
/// <para>
/// <list type="table">
/// <listheader>
/// <term>Property</term>
/// <term>Access</term>
/// <description>Description</description>
/// </listheader>
/// <item>
/// <term><c>value</c></term>
/// <term>read-write</term>
/// <description>The current value of the host variable.</description>
/// </item>
/// <item>
/// <term><c>out</c></term>
/// <term>read-only</term>
/// <description>A reference to the host variable that can be passed as an <c><see href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier">out</see></c> argument.</description>
/// </item>
/// <item>
/// <term><c>ref</c></term>
/// <term>read-only</term>
/// <description>A reference to the host variable that can be passed as a <c><see href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref">ref</see></c> argument.</description>
/// </item>
/// </list>
/// </para>
/// </remarks>
/// <example>
/// The following code demonstrates using a host variable to invoke a method with an
/// <c>out</c> parameter.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import a dictionary type
/// var StringT = host.type("System.String");
/// var StringDictT = host.type("System.Collections.Generic.Dictionary", StringT, StringT);
/// // create and populate a dictionary
/// var dict = host.newObj(StringDictT);
/// dict.Add("foo", "bar");
/// dict.Add("baz", "qux");
/// // look up a dictionary entry
/// var result = host.newVar(StringT);
/// var found = dict.TryGetValue("baz", result.out);
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object newVar<T>(T initValue = default)
{
return new HostVariable<T>(initValue);
}
// ReSharper restore GrammarMistakeInComment
/// <summary>
/// Creates a delegate that invokes a script function.
/// </summary>
/// <typeparam name="T">The type of delegate to create.</typeparam>
/// <param name="scriptFunc">The script function for which to create a delegate.</param>
/// <returns>A new delegate that invokes the specified script function.</returns>
/// <remarks>
/// If the delegate signature includes parameters passed by reference, the corresponding
/// arguments to the script function will be <see cref="newVar{T}">host variables</see>.
/// The script function can set the value of an output argument by assigning the
/// corresponding host variable's <c>value</c> property.
/// </remarks>
/// <example>
/// The following code demonstrates delegating a callback to a script function.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // create and populate an array of integers
/// var EnumerableT = host.type("System.Linq.Enumerable", "System.Core");
/// var array = EnumerableT.Range(1, 5).ToArray();
/// // import the callback type required to call Array.ForEach
/// var Int32T = host.type("System.Int32");
/// var CallbackT = host.type("System.Action", Int32T);
/// // use Array.ForEach to calculate a sum
/// var sum = 0;
/// var ArrayT = host.type("System.Array");
/// ArrayT.ForEach(array, host.del(CallbackT, function (value) { sum += value; }));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, string, object[])"/></c>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public T del<T>(object scriptFunc)
{
return DelegateFactory.CreateDelegate<T>(GetEngine(), scriptFunc);
}
/// <summary>
/// Creates a delegate that invokes a script function and returns no value.
/// </summary>
/// <param name="argCount">The number of arguments to pass to the script function.</param>
/// <param name="scriptFunc">The script function for which to create a delegate.</param>
/// <returns>A new delegate that invokes the specified script function and returns no value.</returns>
/// <remarks>
/// This function creates a delegate that accepts <paramref name="argCount"/> arguments and
/// returns no value. The type of all parameters is <c><see cref="System.Object"/></c>. Such a
/// delegate is often useful in strongly typed contexts because of
/// <see href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/">contravariance</see>.
/// </remarks>
/// <example>
/// The following code demonstrates delegating a callback to a script function.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // create and populate an array of strings
/// var StringT = host.type("System.String");
/// var array = host.newArr(StringT, 3);
/// array.SetValue("first", 0);
/// array.SetValue("second", 1);
/// array.SetValue("third", 2);
/// // use Array.ForEach to generate console output
/// var ArrayT = host.type("System.Array");
/// var ConsoleT = host.type("System.Console");
/// ArrayT.ForEach(array, host.proc(1, function (value) { ConsoleT.WriteLine(value); }));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
/// <c><seealso cref="newArr{T}"/></c>
public object proc(int argCount, object scriptFunc)
{
return DelegateFactory.CreateProc(GetEngine(), scriptFunc, argCount);
}
/// <summary>
/// Creates a delegate that invokes a script function and returns a value of the specified type.
/// </summary>
/// <typeparam name="T">The return value type.</typeparam>
/// <param name="argCount">The number of arguments to pass to the script function.</param>
/// <param name="scriptFunc">The script function for which to create a delegate.</param>
/// <returns>A new delegate that invokes the specified script function and returns a value of the specified type.</returns>
/// <remarks>
/// This function creates a delegate that accepts <paramref name="argCount"/> arguments and
/// returns a value of the specified type. The type of all parameters is
/// <c><see cref="System.Object"/></c>. Such a delegate is often useful in strongly typed contexts
/// because of
/// <see href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/">contravariance</see>.
/// </remarks>
/// <example>
/// The following code demonstrates delegating a callback to a script function.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // create and populate an array of strings
/// var StringT = host.type("System.String");
/// var array = host.newArr(StringT, 3);
/// array.SetValue("first", 0);
/// array.SetValue("second", 1);
/// array.SetValue("third", 2);
/// // import LINQ extensions
/// var EnumerableT = host.type("System.Linq.Enumerable", "System.Core");
/// // use LINQ to create an array of modified strings
/// var selector = host.func(StringT, 1, function (value) { return value.toUpperCase(); });
/// array = array.Select(selector).ToArray();
/// </code>
/// </example>
/// <c><seealso cref="HostFunctions.func(int, object)"/></c>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
/// <c><seealso cref="ExtendedHostFunctions.type(string, string, object[])"/></c>
public object func<T>(int argCount, object scriptFunc)
{
return DelegateFactory.CreateFunc<T>(GetEngine(), scriptFunc, argCount);
}
/// <summary>
/// Creates a delegate that invokes a script function and returns its result value.
/// </summary>
/// <param name="argCount">The number of arguments to pass to the script function.</param>
/// <param name="scriptFunc">The script function for which to create a delegate.</param>
/// <returns>A new delegate that invokes the specified script function and returns its result value.</returns>
/// <remarks>
/// <para>
/// This function creates a delegate that accepts <paramref name="argCount"/> arguments and
/// returns the result of invoking <paramref name="scriptFunc"/>. The type of all
/// parameters and the return value is <c><see cref="System.Object"/></c>. Such a delegate is
/// often useful in strongly typed contexts because of
/// <see href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/">contravariance</see>.
/// </para>
/// <para>
/// For information about the types of result values that script code can return, see
/// <c><see cref="ScriptEngine.Evaluate(string, bool, string)"/></c>.
/// </para>
/// </remarks>
/// <c><seealso cref="HostFunctions.func{T}(int, object)"/></c>
public object func(int argCount, object scriptFunc)
{
return func<object>(argCount, scriptFunc);
}
/// <summary>
/// Gets the <c><see cref="System.Type"/></c> for the specified host type. This version is invoked
/// if the specified object can be used as a type argument.
/// </summary>
/// <typeparam name="T">The host type for which to get the <c><see cref="System.Type"/></c>.</typeparam>
/// <returns>The <c><see cref="System.Type"/></c> for the specified host type.</returns>
/// <remarks>
/// <para>
/// This function is similar to C#'s
/// <c><see href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/typeof">typeof</see></c>
/// operator. It is overloaded with <c><see cref="typeOf(object)"/></c> and selected at runtime if
/// <typeparamref name="T"/> can be used as a type argument.
/// </para>
/// <para>
/// This function throws an exception if the script engine's
/// <c><see cref="ScriptEngine.AllowReflection"/></c> property is set to <c>false</c>.
/// </para>
/// </remarks>
/// <example>
/// The following code retrieves the assembly-qualified name of a host type.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// var StringT = host.type("System.String");
/// var name = host.typeOf(StringT).AssemblyQualifiedName;
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public Type typeOf<T>()
{
GetEngine().CheckReflection();
return typeof(T);
}
/// <summary>
/// Gets the <c><see cref="System.Type"/></c> for the specified host type. This version is invoked
/// if the specified object cannot be used as a type argument.
/// </summary>
/// <param name="value">The host type for which to get the <c><see cref="System.Type"/></c>.</param>
/// <returns>The <c><see cref="System.Type"/></c> for the specified host type.</returns>
/// <remarks>
/// <para>
/// This function is similar to C#'s
/// <c><see href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/typeof">typeof</see></c>
/// operator. It is overloaded with <c><see cref="typeOf{T}"/></c> and selected at runtime if
/// <paramref name="value"/> cannot be used as a type argument. Note that this applies to
/// some host types; examples are static types and overloaded generic type groups.
/// </para>
/// <para>
/// This function throws an exception if the script engine's
/// <c><see cref="ScriptEngine.AllowReflection"/></c> property is set to <c>false</c>.
/// </para>
/// </remarks>
/// <example>
/// The following code retrieves the assembly-qualified name of a host type.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// var ConsoleT = host.type("System.Console");
/// var name = host.typeOf(ConsoleT).AssemblyQualifiedName;
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public Type typeOf(object value)
{
GetEngine().CheckReflection();
return GetUniqueHostType(value, "value");
}
/// <summary>
/// Determines whether an object is compatible with the specified host type.
/// </summary>
/// <typeparam name="T">The host type with which to test <paramref name="value"/> for compatibility.</typeparam>
/// <param name="value">The object to test for compatibility with the specified host type.</param>
/// <returns><c>True</c> if <paramref name="value"/> is compatible with the specified type, <c>false</c> otherwise.</returns>
/// <remarks>
/// This function is similar to C#'s
/// <c><see href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is">is</see></c>
/// operator.
/// </remarks>
/// <example>
/// The following code defines a function that determines whether an object implements
/// <c><see cref="System.IComparable"/></c>.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// function isComparable(value)
/// {
/// var IComparableT = host.type("System.IComparable");
/// return host.isType(IComparableT, value);
/// }
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public bool isType<T>(object value)
{
return value is T;
}
/// <summary>
/// Casts an object to the specified host type, returning <c>null</c> if the cast fails.
/// </summary>
/// <typeparam name="T">The host type to which to cast <paramref name="value"/>.</typeparam>
/// <param name="value">The object to cast to the specified host type.</param>
/// <returns>The result of the cast if successful, <c>null</c> otherwise.</returns>
/// <remarks>
/// This function is similar to C#'s
/// <c><see href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as">as</see></c>
/// operator.
/// </remarks>
/// <example>
/// The following code defines a function that disposes an object if it implements
/// <c><see cref="System.IDisposable"/></c>.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// function dispose(value)
/// {
/// var IDisposableT = host.type("System.IDisposable");
/// var disposable = host.asType(IDisposableT, value);
/// if (disposable) {
/// disposable.Dispose();
/// }
/// }
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object asType<T>(object value) where T : class
{
return HostItem.Wrap(GetEngine(), value as T, typeof(T));
}
/// <summary>
/// Casts an object to the specified host type.
/// </summary>
/// <typeparam name="T">The host type to which to cast <paramref name="value"/>.</typeparam>
/// <param name="value">The object to cast to the specified host type.</param>
/// <returns>The result of the cast.</returns>
/// <remarks>
/// If the cast fails, this function throws an exception.
/// </remarks>
/// <example>
/// The following code casts a floating-point value to a 32-bit integer.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// var Int32T = host.type("System.Int32");
/// var intValue = host.cast(Int32T, 12.5);
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object cast<T>(object value)
{
return HostItem.Wrap(GetEngine(), value.DynamicCast<T>(), typeof(T));
}
/// <summary>
/// Determines whether an object is a host type. This version is invoked if the specified
/// object cannot be used as a type argument.
/// </summary>
/// <param name="value">The object to test.</param>
/// <returns><c>True</c> if <paramref name="value"/> is a host type, <c>false</c> otherwise.</returns>
/// <remarks>
/// This function is overloaded with <c><see cref="isTypeObj{T}"/></c> and selected at runtime if
/// <paramref name="value"/> cannot be used as a type argument. Note that this applies to
/// some host types; examples are static types and overloaded generic type groups.
/// </remarks>
public bool isTypeObj(object value)
{
return value is HostType;
}
// ReSharper disable UnusedTypeParameter
/// <summary>
/// Determines whether an object is a host type. This version is invoked if the specified
/// object can be used as a type argument.
/// </summary>
/// <typeparam name="T">The host type (ignored).</typeparam>
/// <returns><c>True</c>.</returns>
/// <remarks>
/// This function is overloaded with <c><see cref="isTypeObj(object)"/></c> and selected at
/// runtime if <typeparamref name="T"/> can be used as a type argument. Because type
/// arguments are always host types, this method ignores its type argument and always
/// returns <c>true</c>.
/// </remarks>
public bool isTypeObj<T>()
{
return true;
}
// ReSharper restore UnusedTypeParameter
/// <summary>
/// Determines whether the specified value is <c>null</c>.
/// </summary>
/// <param name="value">The value to test.</param>
/// <returns><c>True</c> if <paramref name="value"/> is <c>null</c>, <c>false</c> otherwise.</returns>
/// <remarks>
/// Use this function to test field, property, and method return values when <c>null</c>
/// result wrapping is in effect (see
/// <c><see cref="ScriptMemberFlags.WrapNullResult"/></c> and
/// <c><see cref="ScriptEngine.EnableNullResultWrapping"/></c>).
/// </remarks>
/// <c><seealso cref="ScriptMemberFlags.WrapNullResult"/></c>
/// <c><seealso cref="ScriptEngine.EnableNullResultWrapping"/></c>
public bool isNull(object value)
{
return value is null;
}
/// <summary>
/// Creates a strongly typed flag set.
/// </summary>
/// <typeparam name="T">The type of flag set to create.</typeparam>
/// <param name="args">The flags to include in the flag set.</param>
/// <returns>A strongly typed flag set containing the specified flags.</returns>
/// <remarks>
/// This function throws an exception if <typeparamref name="T"/> is not a flag set type.
/// </remarks>
/// <example>
/// The following code demonstrates using a strongly typed flag set.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import URI types
/// var UriT = host.type("System.Uri", "System");
/// var UriFormatT = host.type("System.UriFormat", "System");
/// var UriComponentsT = host.type("System.UriComponents", "System");
/// // create a URI
/// var uri = host.newObj(UriT, "http://www.example.com:8080/path/to/file/sample.htm?x=1&y=2");
/// // extract URI components
/// var components = host.flags(UriComponentsT.Scheme, UriComponentsT.Host, UriComponentsT.Path);
/// var result = uri.GetComponents(components, UriFormatT.Unescaped);
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, string, object[])"/></c>
public T flags<T>(params T[] args)
{
var type = typeof(T);
if (!type.IsFlagsEnum(ScriptEngine.Current))
{
throw new InvalidOperationException(MiscHelpers.FormatInvariant("{0} is not a flag set type", type.GetFullFriendlyName()));
}
try
{
return (T)Enum.ToObject(typeof(T), args.Aggregate(0UL, (flags, arg) => flags | Convert.ToUInt64(arg, CultureInfo.InvariantCulture)));
}
catch (OverflowException)
{
return (T)Enum.ToObject(typeof(T), args.Aggregate(0L, (flags, arg) => flags | Convert.ToInt64(arg, CultureInfo.InvariantCulture)));
}
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.SByte"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.SByte"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.SByte"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.SByte"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.SByte"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.SByte");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toSByte(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toSByte(IConvertible value)
{
return HostObject.Wrap(Convert.ToSByte(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Byte"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.Byte"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.Byte"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.Byte"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.Byte"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.Byte");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toByte(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toByte(IConvertible value)
{
return HostObject.Wrap(Convert.ToByte(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Int16"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.Int16"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.Int16"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.Int16"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.Int16"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.Int16");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toInt16(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toInt16(IConvertible value)
{
return HostObject.Wrap(Convert.ToInt16(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.UInt16"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.UInt16"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.UInt16"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.UInt16"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.UInt16"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.UInt16");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toUInt16(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toUInt16(IConvertible value)
{
return HostObject.Wrap(Convert.ToUInt16(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Char"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.Char"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.Char"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.Char"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.Char"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.Char");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toChar(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toChar(IConvertible value)
{
return HostObject.Wrap(Convert.ToChar(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Int32"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.Int32"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.Int32"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.Int32"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.Int32"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.Int32");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toInt32(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toInt32(IConvertible value)
{
return HostObject.Wrap(Convert.ToInt32(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.UInt32"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.UInt32"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.UInt32"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.UInt32"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.UInt32"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.UInt32");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toUInt32(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toUInt32(IConvertible value)
{
return HostObject.Wrap(Convert.ToUInt32(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Int64"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.Int64"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.Int64"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.Int64"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.Int64"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.Int64");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toInt64(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toInt64(IConvertible value)
{
return HostObject.Wrap(Convert.ToInt64(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.UInt64"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.UInt64"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.UInt64"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.UInt64"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.UInt64"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.UInt64");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toUInt64(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toUInt64(IConvertible value)
{
return HostObject.Wrap(Convert.ToUInt64(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Single"/></c> instance.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An object that can be passed to a parameter of type <c><see cref="System.Single"/></c>.</returns>
/// <remarks>
/// This function converts <paramref name="value"/> to <c><see cref="System.Single"/></c> and
/// packages the result to retain its numeric type across the host-script boundary. It may
/// be useful for passing arguments to <c><see cref="System.Single"/></c> parameters if the script
/// engine does not support that type natively.
/// </remarks>
/// <example>
/// The following code adds an element of type <c><see cref="System.Single"/></c> to a strongly
/// typed list.
/// It assumes that an instance of <c><see cref="ExtendedHostFunctions"/></c> is exposed under
/// the name "host"
/// (see <c><see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see></c>).
/// <code lang="JavaScript">
/// // import types
/// var ElementT = host.type("System.Single");
/// var ListT = host.type("System.Collections.Generic.List", ElementT);
/// // create a list
/// var list = host.newObj(ListT);
/// // add a list element
/// list.Add(host.toSingle(42));
/// </code>
/// </example>
/// <c><seealso cref="ExtendedHostFunctions.type(string, object[])"/></c>
public object toSingle(IConvertible value)
{
return HostObject.Wrap(Convert.ToSingle(value));
}
/// <summary>
/// Converts the specified value to a strongly typed <c><see cref="System.Double"/></c> instance.