forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAQtorial.html
More file actions
1266 lines (1015 loc) · 61 KB
/
FAQtorial.html
File metadata and controls
1266 lines (1015 loc) · 61 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
<html>
<head>
<link rel="shortcut icon" type="image/png" href="../favicon.png">
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<meta name=Generator content="Microsoft Word 15 (filtered)">
<title>ClearScript FAQtorial</title>
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Cambria;
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:0in;
margin-left:0in;
margin-bottom:.0001pt;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
h1
{mso-style-link:"Heading 1 Char";
margin-top:10.0pt;
margin-right:0in;
margin-bottom:0in;
margin-left:0in;
margin-bottom:.0001pt;
line-height:115%;
page-break-after:avoid;
font-size:14.0pt;
font-family:"Cambria",serif;
color:#365F91;}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{mso-style-link:"Header Char";
margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{mso-style-link:"Footer Char";
margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoTitle, li.MsoTitle, div.MsoTitle
{mso-style-link:"Title Char";
margin-top:6.0pt;
margin-right:0in;
margin-bottom:15.0pt;
margin-left:0in;
border:none;
padding:0in;
font-size:26.0pt;
font-family:"Cambria",serif;
color:#17365D;
letter-spacing:.25pt;}
p.MsoTitleCxSpFirst, li.MsoTitleCxSpFirst, div.MsoTitleCxSpFirst
{mso-style-link:"Title Char";
margin-top:6.0pt;
margin-right:0in;
margin-bottom:0in;
margin-left:0in;
margin-bottom:.0001pt;
border:none;
padding:0in;
font-size:26.0pt;
font-family:"Cambria",serif;
color:#17365D;
letter-spacing:.25pt;}
p.MsoTitleCxSpMiddle, li.MsoTitleCxSpMiddle, div.MsoTitleCxSpMiddle
{mso-style-link:"Title Char";
margin:0in;
margin-bottom:.0001pt;
border:none;
padding:0in;
font-size:26.0pt;
font-family:"Cambria",serif;
color:#17365D;
letter-spacing:.25pt;}
p.MsoTitleCxSpLast, li.MsoTitleCxSpLast, div.MsoTitleCxSpLast
{mso-style-link:"Title Char";
margin-top:0in;
margin-right:0in;
margin-bottom:15.0pt;
margin-left:0in;
border:none;
padding:0in;
font-size:26.0pt;
font-family:"Cambria",serif;
color:#17365D;
letter-spacing:.25pt;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle
{margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast
{margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.TitleChar
{mso-style-name:"Title Char";
mso-style-link:Title;
font-family:"Cambria",serif;
color:#17365D;
letter-spacing:.25pt;}
span.Heading1Char
{mso-style-name:"Heading 1 Char";
mso-style-link:"Heading 1";
font-family:"Cambria",serif;
color:#365F91;
font-weight:bold;}
span.HeaderChar
{mso-style-name:"Header Char";
mso-style-link:Header;}
span.FooterChar
{mso-style-name:"Footer Char";
mso-style-link:Footer;}
.MsoChpDefault
{font-family:"Calibri",sans-serif;}
.MsoPapDefault
{margin-bottom:10.0pt;
line-height:115%;}
/* Page Definitions */
@page WordSection1
{size:8.5in 11.0in;
margin:.5in .5in .5in .5in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple style="background-color: gray">
<div class=WordSection1 style="width: 9in; margin: auto; zoom: 1.2; background-color: white; padding: .25in">
<div style='border:none;border-bottom:solid #4F81BD 1.0pt;padding:0in 0in 4.0pt 0in'>
<p class=MsoTitle style='margin-top:0in'>ClearScript FAQtorial</p>
</div>
<h1 style='margin-left:.25in;text-indent:-.25in'>1. What’s a FAQtorial?</h1>
<p class=MsoNormal style='margin-left:.25in'>It’s a tutorial in the form of questions
and answers. It may look like a FAQ list, but it’s really a guide.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>2. OK, so what’s ClearScript?</h1>
<p class=MsoNormal style='margin-left:.25in'><a name="_Hlk482337160">ClearScript
is a library that allows you to add scripting to your .NET applications. It
supports </a><a
href="https://msdn.microsoft.com/en-us/library/hbxc2t98(v=vs.84).aspx">JScript</a>
and <a href="https://msdn.microsoft.com/en-us/library/t0aew7h6(v=vs.84).aspx">VBScript</a>
out of the box and in theory can work with other <a
href="http://msdn.microsoft.com/en-us/library/bb871518.aspx">Windows Script</a>
engines.</p>
<p class=MsoNormal style='margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b>
<a name="_Hlk482337197">ClearScript 5 supports the </a><a name="_Hlk482337218"></a><a
href="https://developers.google.com/v8/">V8</a> high-performance open-source JavaScript
engine. Unlike Windows Script engine instances, V8 instances have no thread
affinity and are suitable for server-side asynchronous scripting.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>3. Hasn’t that been done
before?</h1>
<p class=MsoNormal style='margin-left:.25in'>Perhaps, but sometimes it’s fun to
start something from scratch and see how far you can take it.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>4. Why would I want to add
scripting to my application anyway?</h1>
<p class=MsoNormal style='margin-left:.25in'>There are many possible reasons. Sophisticated
productivity applications often support script-based customization and
extension. The ability to automate an application may be desirable for testing purposes
or for running complex jobs. Another goal might be to process active content
that includes embedded scripts, such as web pages.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>5. Doesn’t .NET have its own
scripting technologies?</h1>
<p class=MsoNormal style='margin-left:.25in'>Yes, but they’re designed for a
different purpose. They typically introduce numerous language extensions in
order to “retarget” standard script languages for full-blown .NET development.
That is <i>not</i> ClearScript’s goal. ClearScript focuses on adding
standards-based scripting to <i>existing</i> .NET applications with minimal
effort.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>6. OK, but if it’s been done
before, what’s so special about ClearScript?</h1>
<p class=MsoNormal style='margin-left:.25in'>ClearScript is all about
simplicity. It’s one line of code to instantiate a script engine, another to
expose a host resource for scripting, and one more to execute a script. You
don’t worry about implementing hosting interfaces, creating scriptable proxies,
decorating class members, etc.</p>
<p class=MsoNormal style='margin-left:.25in'>Because it aims to make existing applications
scriptable without modification or special coding, ClearScript works hard to give
script code the ability to access a long list of .NET features – constructors,
methods, properties, fields, events, indexers, extension methods, nested types,
output parameters, statics, generics, enums, delegates, etc. It does this
without introducing special syntax or requiring script language extensions of
any kind.</p>
<p class=MsoNormal style='margin-left:.25in'>ClearScript also provides
streamlined host-to-script support, allowing the host to invoke script
functions and access script objects directly. In addition, it allows script-enabled
applications to be debugged in script mode.</p>
<p class=MsoNormal style='margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b>
ClearScript 5 supports simultaneous debugging of script, managed, and native code
for applications that use its new V8-based JavaScript engine.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>7. Fine, so how do I
instantiate a script engine with ClearScript?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>You simply “new up” a class instance:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> <b>Microsoft.ClearScript.V8</b>;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:blue'>var</span><span style='font-size:10.0pt;font-family:Consolas'>
engine = <span style='color:blue'>new</span> <b><span style='color:#2B91AF'>V8ScriptEngine</span></b>();</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>At this point the script engine contains
only its built-in objects and functions and has no access to the host
application.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>8. That’s not very useful; how
do I expose my host API for scripting?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>You make one method call to expose a host object or type:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.<b>AddHostObject</b>(<span style='color:#A31515'>"uri"</span>,
<span style='color:blue'>new</span> <span style='color:#2B91AF'>Uri</span>(<span
style='color:#A31515'>"http://www.example.com"</span>));</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.<b>AddHostType</b>(<span
style='color:#A31515'>"Console"</span>, <span style='color:blue'>typeof</span>(<span
style='color:#2B91AF'>Console</span>));</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>The script engine now contains two
root-level objects – a <i>host object</i> and a <i>host type</i> – that provide
access to the corresponding host resources.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>9. What can script code do
with a host object?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>It can access the object’s public members directly:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var uriQuery = <b>uri.Query</b>;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var uriString = <b>uri.ToString</b>();</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>10. What can script code do
with a host type?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>It can access the type’s static members and nested types
directly:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><b><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>Console.WriteLine</span></b><span
style='font-size:10.0pt;font-family:Consolas;color:#A31515'>('Downloading from
{0}...', uri);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><b><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>Console.Title</span></b><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'> = 'Please wait...';</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Host types can also be used to
invoke constructors and specify generic type arguments. There’ll be more on
that later.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>11. Once my host API is
exposed, how do I actually run a script?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Use one of these methods:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.<b>Execute</b>(<span style='color:#A31515'>"var
uriQuery = uri.Query;");</span></span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:blue'>var</span><span style='font-size:10.0pt;font-family:Consolas'>
result = engine.<b>Evaluate</b>(<span style='color:#A31515'>"Math.sqrt(Math.PI)"</span>);</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>12. Why have two different
methods for running scripts?</h1>
<p class=MsoNormal style='margin-left:.25in'>Some script languages have ambiguous
syntactic elements whose meaning depends on whether they’re part of a statement
or an expression. For example, in VBScript a single equals sign is used for
both assignment (statement) and equality testing (expression). To resolve this
ambiguity the host must establish the syntactic context for script execution. <b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Execute</span></b>
interprets script code in a statement context, while <b><span style='font-size:
10.0pt;line-height:115%;font-family:Consolas'>Evaluate</span></b> treats code
as an expression.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>13. You’ve covered host
methods and properties. What about fields?</h1>
<p class=MsoNormal style='margin-left:.25in'>To script code, the public fields
of a host object or type look exactly like properties.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>14. How do I call generic
methods?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>ClearScript supports C#-like type inference, so you can call
most generic methods just as you’d call them from C#. However, some generic
methods require explicit type arguments. To call such a method from script
code, place the required number of host types at the beginning of the argument
list. For example, suppose the host exposes the following types:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System.Linq;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.AddHostType(<span style='color:#A31515'>"Int32"</span>,
<span style='color:blue'>typeof</span>(<span style='color:blue'>int</span>));</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostType(<span
style='color:#A31515'>"Enumerable"</span>, <span style='color:blue'>typeof</span>(<span
style='color:#2B91AF'>Enumerable</span>));</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Here’s how to call the <a
href="http://msdn.microsoft.com/en-us/library/bb341042.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Enumerable.Empty</span></b></a>
generic method from script code:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var empty = Enumerable.<b>Empty(Int32)</b>;</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>15. What about extension
methods?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Extension methods are available if the type that implements
them has been exposed. Continuing the previous example, here’s script code that
calls the <a href="http://msdn.microsoft.com/en-us/library/bb298736.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Enumerable.ToArray</span></b></a>
extension method:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var numbers = Enumerable.Range(0, 10).<b>ToArray()</b>;</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Note that <a
href="http://msdn.microsoft.com/en-us/library/bb298736.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Enumerable.ToArray</span></b></a>
is a generic extension method whose type argument is inferred from its “this”
argument. As you can see, ClearScript allows you to call it using the same
convenient syntax as C#.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>16. Can I handle an event
using a script function?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. Here’s how to connect a host event source to a script
handler function:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var <b>connection</b> =
Console.CancelKeyPress.<b>connect</b>(function (sender, args) {</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'> Console.WriteLine('{0} pressed!',
args.SpecialKey);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>});</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>To break the connection, do this:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>connection.<b>disconnect</b>();</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>17. How do I create an instance
of a host type from script code?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Script code cannot instantiate a host type directly.
However, ClearScript provides a set of utility functions that includes this
capability:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> Microsoft.ClearScript;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.AddHostObject(<span style='color:#A31515'>"host"</span>,
<span style='color:blue'>new</span> <b><span style='color:#2B91AF'>HostFunctions</span></b>());</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostType(<span
style='color:#A31515'>"Uri"</span>, <span style='color:blue'>typeof</span>(<span
style='color:#2B91AF'>Uri</span>));</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Now you can create <a
href="http://msdn.microsoft.com/en-us/library/system.uri.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Uri</span></b></a>
class instances from script code:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var newUri = <b>host.newObj</b>(<b>Uri</b>, 'http://www.example.com');</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Note that <a
href="https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_HostFunctions_newObj__1.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>newObj</span></b></a>
is a generic method that requires an explicit type argument. Its first argument
(the type argument) is the host type to be instantiated. The rest of the
arguments are passed to the constructor that matches the argument signature.</p>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b> ClearScript
5 supports simplified instantiation syntax for JavaScript:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var newUri = <b>new Uri</b>('http://www.example.com');</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>18. What about an array?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>There’s a utility function for creating host arrays, too:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var uriArray = host.<b>newArr</b>(Uri, 10);</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>19. Can I access host array
elements by index?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>That depends on your script language. However, if a host
object implements <a
href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Collections.IList</span></b></a>,
ClearScript exposes read/write properties with numeric names that match the
object’s valid indices:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var firstUri = <b>uriArray[</b>0<b>]</b>;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><b><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>uriArray[</span></b><span style='font-size:10.0pt;font-family:
Consolas;color:#A31515'>1<b>]</b> = firstUri;</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>20. Can I call methods with
output parameters from script code?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. Suppose that a dictionary is exposed for scripting:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System.Collections.Generic;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostObject(<span
style='color:#A31515'>"dict"</span>, <span style='color:blue'>new</span>
<span style='color:#2B91AF'>Dictionary</span><<span style='color:blue'>string</span>,
<span style='color:#2B91AF'>Uri</span>>());</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>You can use a <i>host variable</i> to invoke the <a
href="http://msdn.microsoft.com/en-us/library/bb347013.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Dictionary.TryGetValue</span></b></a>
method from script code:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var <b>uriVar</b> = host.<b>newVar</b>(Uri);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>if (dict.TryGetValue('key', <b>uriVar.out</b>))
{</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'> Console.WriteLine('Found Uri value:
{0}.', <b>uriVar</b>);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>}</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Host variables support two special
properties in addition to <b><span style='font-size:10.0pt;line-height:115%;
font-family:Consolas'>out</span></b>. The first is <b><span style='font-size:
10.0pt;line-height:115%;font-family:Consolas'>ref</span></b>, which is similar
to <b><span style='font-size:10.0pt;line-height:115%;font-family:Consolas'>out</span></b>
but compatible with <a
href="http://msdn.microsoft.com/en-us/library/14akc2c7.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>ref</span></b></a>
parameters. The second is <b><span style='font-size:10.0pt;line-height:115%;
font-family:Consolas'>value</span></b>, which provides read/write access to the
value stored within the variable. If you’re passing a variable into an input
parameter, invoking <b><span style='font-size:10.0pt;line-height:115%;
font-family:Consolas'>value</span></b> is unnecessary.</p>
<h1 style='margin-left:.25in;text-indent:-.25in'>21. Can I route a callback to a
script function?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes, by creating a delegate that invokes your script
function. Consider this code:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System.Threading;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.AddHostType(<span style='color:#A31515'>"Timer"</span>,
<span style='color:blue'>typeof</span>(<span style='color:#2B91AF'>Timer</span>));</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostType(<span
style='color:#A31515'>"TimerCallback"</span>, <span style='color:
blue'>typeof</span>(<span style='color:#2B91AF'>TimerCallback</span>));</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>You can now create timers and handle their callbacks
from script code:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var <b>callback</b> = host.<b>del</b>(TimerCallback,
function (state) {</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'> Console.WriteLine('Timer fired: {0}.',
state);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>});</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var timer = host.newObj(Timer, <b>callback</b>, 'foo', 5000,
5000);</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>In addition to <a
href="https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_HostFunctions_del__1.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>del</span></b></a>,
ClearScript provides <a
href="https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_HostFunctions_proc.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>proc</span></b></a>
and <a
href="https://microsoft.github.io/ClearScript/Reference/html/Overload_Microsoft_ClearScript_HostFunctions_func.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>func</span></b></a>;
these functions create specialized delegates compatible with <b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Action</span></b>
and <b><span style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Func</span></b>
respectively.</p>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b>
ClearScript 5 supports simplified delegate creation syntax for JavaScript:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var callback = <b>new TimerCallback</b>(function
(state) {</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'> Console.WriteLine('Timer fired: {0}.',
state);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>});</span></p>
<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>22. Can I expose many host
types in one step?</h1>
<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. You can use a <i>host type collection</i> to expose all
the types defined in one or more assemblies:</p>
<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>
<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> Microsoft.ClearScript;</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>var</span><span style='font-size:10.0pt;
font-family:Consolas'> typeCollection = <span style='color:blue'>new</span> <b><span
style='color:#2B91AF'>HostTypeCollection</span></b>(<span style='color:#A31515'>"mscorlib",
"System", "System.Core"</span>);</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostObject(<span
style='color:#A31515'>"clr"</span>, typeCollection);</span></p>
<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Now all the types defined in the standard <b>mscorlib</b>,