-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathamScript.Module.pas
More file actions
1283 lines (1074 loc) · 39.6 KB
/
amScript.Module.pas
File metadata and controls
1283 lines (1074 loc) · 39.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit amScript.Module;
(*
* Copyright © 2012 Anders Melander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
interface
uses
SysUtils, Classes,
Generics.Collections,
Controls,
Windows,
Graphics,
TypInfo,
dwsComp,
dwsExprs,
dwsCompiler,
dwsSymbols,
dwsFunctions,
dwsFileSystem,
dwsUtils,
dwsComConnector,
dwsJSONConnector,
dwsErrors,
dwsDataContext,
{$ifndef OLD_DWSCRIPT}
dwsUnitSymbols,
dwsInfo,
{$endif OLD_DWSCRIPT}
amScript.API,
amScript.Host.API,
amScript.Provider.API,
amScript.Package.API;
// -----------------------------------------------------------------------------
//
// IScriptObjectInfo
//
// -----------------------------------------------------------------------------
type
IScriptObjectInfo = interface
function Info: IInfo;
end;
// -----------------------------------------------------------------------------
//
// TScriptObjectWrapper
//
// -----------------------------------------------------------------------------
type
IScriptEnvironment = interface;
TScriptObjectWrapperBase = class
private
FItem: TObject;
FScriptObj: IScriptObj;
FExecution: IdwsProgramExecution;
FEnvironment: IScriptEnvironment;
FOwnsItem: boolean;
FDestroying: boolean;
protected
function GetExecution: IdwsProgramExecution;
// Destroy wrapped object
procedure DoFreeWrappedObject; virtual;
procedure FreeWrappedObject; virtual;
/// <summary>Unregister calls Environment.UnregisterWrapper to remove the wrapper from the wrapper list.</summary>
procedure Unregister;
/// <summary>Unregistered is called by Environment.UnregisterWrapper.</summary>
procedure Unregistered;
public
constructor Create(const AScriptObj: IScriptObj; AItem: TObject; AOwnsItem: boolean = False); overload; virtual;
constructor Create(AProgramInfo: TProgramInfo; TypeSym: TSymbol; AItem: TObject; AOwnsItem: boolean = False); overload;
constructor Create(AProgramInfo: TProgramInfo; AItem: TObject; AOwnsItem: boolean = False); overload;
destructor Destroy; override;
// Disassociate wrapped object with wrapper without destroying object
procedure DetachWrappedObject; virtual;
// Clear reference to script objects
procedure DetachScriptObject; virtual;
property Item: TObject read FItem;
property ScriptObj: IScriptObj read FScriptObj;
property OwnsItem: boolean read FOwnsItem write FOwnsItem;
property Destroying: boolean read FDestroying;
property Execution: IdwsProgramExecution read GetExecution write FExecution;
function AcquireInfo: IScriptObjectInfo;
function CheckScriptObj: boolean;
end;
TWrapperList = array of TScriptObjectWrapperBase;
TScriptObjectWrapper<T: Class> = class(TScriptObjectWrapperBase)
private
protected
function GetItem: T;
public
property Item: T read GetItem;
end;
// -----------------------------------------------------------------------------
//
// TScriptComponentWrapper
//
// -----------------------------------------------------------------------------
TScriptComponentWrapperBase<T: TComponent> = class(TScriptObjectWrapper<T>)
private
protected
procedure DoFreeWrappedObject; override;
public
constructor Create(const AScriptObj: IScriptObj; AItem: TObject; AOwnsItem: boolean = False); overload; override;
constructor CreateOwned(AProgramInfo: TProgramInfo; const AScriptObj: IScriptObj; AComponentClass: TComponentClass);
end;
TScriptComponentWrapper = class(TScriptComponentWrapperBase<TComponent>);
// -----------------------------------------------------------------------------
//
// TScriptControlWrapper
//
// -----------------------------------------------------------------------------
TScriptControlWrapper = class(TScriptComponentWrapperBase<TControl>)
private
protected
function GetWinControl: TWinControl;
public
constructor Create(const AScriptObj: IScriptObj; AItem: TObject; AOwnsItem: boolean = False); override;
procedure DetachWrappedObject; override;
property WinControl: TWinControl read GetWinControl;
procedure OnClickHandler(Sender: TObject);
procedure OnDblClickHandler(Sender: TObject);
end;
TScriptControlWrapperClass = class of TScriptControlWrapper;
TScriptControlWrapperBase<T: TControl> = class(TScriptControlWrapper)
private
protected
function GetItem: T;
public
property Item: T read GetItem;
end;
// -----------------------------------------------------------------------------
//
// TScriptObjectInfo
//
// -----------------------------------------------------------------------------
TScriptObjectInfo = class(TInterfacedObject, IScriptObjectInfo)
private
FExecution: IdwsProgramExecution;
FProgramInfo: TProgramInfo;
FData: TData;
FInfo: IInfo;
protected
// IScriptObjectInfo
function Info: IInfo;
public
constructor Create(const AExecution: IdwsProgramExecution; const AScriptObject: IScriptObj); overload;
constructor Create(const AScriptObject: IScriptObj); overload;
destructor Destroy; override;
class function IsValidScriptObject(const AScriptObject: IScriptObj): boolean;
end;
// -----------------------------------------------------------------------------
//
// IScriptEnvironment
//
// -----------------------------------------------------------------------------
IScriptEnvironment = interface(IdwsEnvironment)
['{BC74C115-64E8-4F95-8022-9EE9B59BF8E0}']
function GetApplication: IScriptHostApplication;
property Application: IScriptHostApplication read GetApplication;
function GetDocument: IScriptHostDocument;
property Document: IScriptHostDocument read GetDocument;
function GetItem: TObject;
property Item: TObject read GetItem;
procedure RegisterWrapper(Wrapper: TScriptObjectWrapperBase);
function FindWrapper(AObject: TObject): TScriptObjectWrapperBase;
procedure UnregisterWrapper(Wrapper: TScriptObjectWrapperBase);
end;
// -----------------------------------------------------------------------------
//
// TCustomScriptModule
//
// -----------------------------------------------------------------------------
// Base class for script modules.
// Uses reference counted life time management.
// -----------------------------------------------------------------------------
type
TCustomScriptModule = class abstract(TDataModule, IUnknown)
private
FRefCount: integer;
protected
// IUnknown
function QueryInterface(const IID: TGUID; out Obj): HResult; override; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
constructor Create(AOwner: TComponent); override;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
end;
// -----------------------------------------------------------------------------
//
// TScriptModule
//
// -----------------------------------------------------------------------------
// Base class for script RTL library modules.
// Former TCustomDataModuleScriptModule.
// -----------------------------------------------------------------------------
type
TScriptModule = class abstract(TCustomScriptModule, IScriptModule)
private
protected
type
TPopulateEnumPredicate = reference to function(Value: integer): boolean;
TPopulateEnumFilter = reference to function(const Value: string): string;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function CheckScriptObj(const ScriptObj: IScriptObj): boolean;
function Environment(const ScriptObj: IScriptObj): IScriptEnvironment; overload;
function Environment(Info: TProgramInfo): IScriptEnvironment; overload;
function Environment(Wrapper: TScriptObjectWrapperBase): IScriptEnvironment; overload;
// Easy access to global TScriptEnvironment wrapper list function from modules
class function ScriptFindWrappers(AObject: TObject): TWrapperList;
class procedure ScriptUnregisterWrappers(AObject: TObject);
// IScriptModule
procedure Initialize(DelphiWebScript: TDelphiWebScript); virtual;
procedure Finalize; virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
class procedure RegisterModule;
class function CreateScriptObject(Info: TProgramInfo; const ClassName: string; ExtObject: TObject; CreateNilObject: boolean = False; MapClass: boolean = True): IScriptObj; overload;
class function CreateScriptObject(const Execution: IdwsProgramExecution; const ClassName: string; ExtObject: TObject; CreateNilObject: boolean = False): IScriptObj; overload;
class function CreateScriptObject(Info: TProgramInfo; const ClassName: string; ExtObject: pointer; CreateNilObject: boolean = False): IScriptObj; overload;
class function CreateResultScriptObject(Info: TProgramInfo; ExtObject: TObject; CreateNilObject: boolean = False): IScriptObj; overload;
class function CreateResultScriptObject(Info: TProgramInfo): IScriptObj; overload;
class function CreateResultScriptObject(Info: TProgramInfo; const Unk: IUnknown; CreateNilObject: boolean = False): IScriptObj; overload;
class procedure PopulateScriptEnum(dwsUnit: TdwsUnit; const ScriptName: string; Info: PTypeInfo; Clear: boolean = True; Predicate: TPopulateEnumPredicate = nil; Filter: TPopulateEnumFilter = nil);
end;
// -----------------------------------------------------------------------------
//
// Error handling
//
// -----------------------------------------------------------------------------
function ScriptHandleException(const Execution: IdwsProgramExecution): boolean;
function ShowScriptException(const AMsg, AInfo: string; const Execution: IdwsProgramExecution = nil; AllowAbort: boolean = True): boolean; overload;
function ShowScriptException(E: Exception; const Execution: IdwsProgramExecution = nil; AllowAbort: boolean = True): boolean; overload;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
type
// TControlCracker must be declared in interface or we get a compiler error:
// [DCC Error] ...: E2506 Method of parameterized type declared in interface section must not use local symbol 'TControlCracker'
TControlCracker = class(TControl);
implementation
uses
Variants,
Dialogs,
dwsXPlatform,
{$ifndef OLD_DWSCRIPT}
dwsScriptSource,
dwsInfoClasses,
dwsCompilerContext,
{$else OLD_DWSCRIPT}
dwsUnitSymbols,
dwsInfo,
{$endif OLD_DWSCRIPT}
IOUtils,
amDialogs,
amScript.Service,
amScript.Environment;
// -----------------------------------------------------------------------------
//
// ScriptHandleException
//
// -----------------------------------------------------------------------------
type
TRaiseExceptExpr = class(TProgramExpr)
private
FException: pointer;
public
constructor Create;
destructor Destroy; override;
procedure EvalAsVariant(exec : TdwsExecution; var result : Variant); override;
procedure EvalNoResult(exec: TdwsExecution); override;
function ScriptPos: TScriptPos; override;
end;
constructor TRaiseExceptExpr.Create;
begin
FException := AcquireExceptionObject;
end;
destructor TRaiseExceptExpr.Destroy;
begin
if (FException <> nil) then
ReleaseExceptionObject;
inherited;
end;
procedure TRaiseExceptExpr.EvalAsVariant(exec: TdwsExecution; var result: Variant);
begin
VarClear(result);
EvalNoResult(exec);
end;
procedure TRaiseExceptExpr.EvalNoResult(exec: TdwsExecution);
var
p: pointer;
begin
p := FException;
FException := nil;
raise TObject(p);
end;
function TRaiseExceptExpr.ScriptPos: TScriptPos;
begin
Result := cNullPos;
end;
type
TProgramExecutionCracker = class(TdwsProgramExecution);
// -----------------------------------------------------------------------------
function ScriptHandleException(const Execution: IdwsProgramExecution): boolean;
var
Expr: TProgramExpr;
// ExceptObject: pointer;
begin
Result := False;
// ExceptObject := AcquireExceptionObject;
if (ExceptObject = nil) then
exit;
try
if (TObject(ExceptObject) is Exception) then
begin
if (ShowScriptException(Exception(ExceptObject), Execution)) then
exit(True);
end;
if (Execution <> nil) then
begin
Expr := TRaiseExceptExpr.Create;
try
TProgramExecutionCracker(TdwsProgramExecution(Execution)).RunProgramExpr(Expr);
finally
Expr.Free;
end;
end;
finally
// ReleaseExceptionObject;
end;
end;
// -----------------------------------------------------------------------------
function ShowScriptException(E: Exception; const Execution: IdwsProgramExecution; AllowAbort: boolean): boolean;
var
Msg: string;
Info: string;
begin
Msg := E.Message;
Info := E.ClassName;
if (E is EScriptError) then
begin
if (EScriptError(E).ScriptPos.Defined) then
try
Info := Info + #13 + EScriptError(E).ScriptPos.AsInfo;
except
Info := Info + #13 + '- Failed to extract ScriptPos';
end;
if (Length(EScriptError(E).ScriptCallStack) > 0) then
try
Info := Info + #13#13 + TExprBase.CallStackToString(EScriptError(E).ScriptCallStack);
except
// Above has been known to bomb
Info := Info + #13 + '- Failed to extract script call stack';
end;
Result := ShowScriptException(Msg, Info, nil, AllowAbort);
end else
Result := ShowScriptException(Msg, Info, Execution, AllowAbort);
end;
function ShowScriptException(const AMsg, AInfo: string; const Execution: IdwsProgramExecution; AllowAbort: boolean): boolean; overload;
var
Res: integer;
Info: string;
Buttons: TMsgDlgButtons;
begin
Info := AInfo;
if (Execution <> nil) then
begin
if (Execution.GetLastScriptErrorExpr <> nil) and (Execution.GetLastScriptErrorExpr.ScriptPos.Defined) then
try
Info := Info + #13 + Execution.GetLastScriptErrorExpr.ScriptPos.AsInfo;
except
Info := Info + #13 + '- Failed to extract ScriptPos';
end;
if (Length(Execution.GetCallStack) > 0) then
try
Info := Info + #13#13 + TExprBase.CallStackToString(Execution.GetCallStack);
except
// Darn!
Info := Info + #13 + '- Failed to extract script call stack';
end;
end;
Buttons := [mbOK];
if (AllowAbort) then
Include(Buttons, mbAbort);
Res := MessageTaskDlgEx('Script run time error', AMsg + '|' + Info, mtWarning, Buttons, mbOK);
Result := (Res = mrOK);
end;
// -----------------------------------------------------------------------------
//
// TScriptObjectInfo
//
// -----------------------------------------------------------------------------
constructor TScriptObjectInfo.Create(const AExecution: IdwsProgramExecution; const AScriptObject: IScriptObj);
begin
inherited Create;
FExecution := AExecution;
FProgramInfo := TdwsProgramExecution(FExecution).AcquireProgramInfo(nil);
SetLength(FData, 1);
FData[0] := AScriptObject;
CreateInfoOnSymbol(FInfo, FProgramInfo, AScriptObject.ClassSym, FData, 0);
// CreateInfoOnSymbol(FInfo, TdwsProgramExecution(FExecution).ProgramInfo, AScriptObject.ClassSym, FData, 0);
end;
constructor TScriptObjectInfo.Create(const AScriptObject: IScriptObj);
var
Execution: IdwsProgramExecution;
begin
inherited Create;
FExecution := nil;
Execution := TScriptObjInstance(AScriptObject).ExecutionContext;
FProgramInfo := Execution.Info;
SetLength(FData, 1);
FData[0] := AScriptObject;
CreateInfoOnSymbol(FInfo, FProgramInfo, AScriptObject.ClassSym, FData, 0);
end;
destructor TScriptObjectInfo.Destroy;
begin
FInfo := nil;
if (FExecution <> nil) then
TdwsProgramExecution(FExecution).ReleaseProgramInfo(FProgramInfo);
inherited;
end;
function TScriptObjectInfo.Info: IInfo;
begin
Result := FInfo;
end;
class function TScriptObjectInfo.IsValidScriptObject(const AScriptObject: IScriptObj): boolean;
var
Execution: IdwsProgramExecution;
begin
if (AScriptObject = nil) or (AScriptObject.Destroyed) then
Exit(False);
Execution := TScriptObjInstance(AScriptObject).ExecutionContext;
Result := (Execution <> nil);
end;
// -----------------------------------------------------------------------------
//
// TScriptObjectWrapper
//
// -----------------------------------------------------------------------------
function TScriptObjectWrapperBase.CheckScriptObj: boolean;
begin
ASSERT(FScriptObj <> nil);
Result := (not FScriptObj.Destroyed) and (FScriptObj.ClassSym <> nil);
end;
procedure TScriptObjectWrapperBase.DetachScriptObject;
begin
Unregister;
FScriptObj := nil;
end;
constructor TScriptObjectWrapperBase.Create(const AScriptObj: IScriptObj; AItem: TObject; AOwnsItem: boolean);
begin
inherited Create;
FScriptObj := AScriptObj;
FItem := AItem;
FOwnsItem := AOwnsItem;
FEnvironment := IScriptEnvironment(Execution.Environment);
FEnvironment.RegisterWrapper(Self);
end;
constructor TScriptObjectWrapperBase.Create(AProgramInfo: TProgramInfo; TypeSym: TSymbol; AItem: TObject; AOwnsItem: boolean);
var
ScriptObj: IScriptObj;
begin
ScriptObj := TScriptModule.CreateScriptObject(AProgramInfo, TypeSym.Name, Self, False, False);
Create(ScriptObj, AItem, AOwnsItem);
end;
constructor TScriptObjectWrapperBase.Create(AProgramInfo: TProgramInfo; AItem: TObject; AOwnsItem: boolean);
begin
Create(AProgramInfo, AProgramInfo.ResultVars.TypeSym, AItem, AOwnsItem);
end;
destructor TScriptObjectWrapperBase.Destroy;
begin
// Avoid recursion through Unregister->Environment.UnregisterWrapper->Unregistered->DetachScriptObject->TdwsUnit.OnCleanUp->Destroy
FDestroying := True;
Unregister;
FreeWrappedObject;
DetachWrappedObject;
DetachScriptObject;
inherited;
end;
procedure TScriptObjectWrapperBase.DetachWrappedObject;
begin
Unregister;
FItem := nil;
end;
procedure TScriptObjectWrapperBase.FreeWrappedObject;
begin
DoFreeWrappedObject;
end;
procedure TScriptObjectWrapperBase.DoFreeWrappedObject;
begin
if (FOwnsItem) then
FreeAndNil(FItem)
else
FItem := nil;
end;
function TScriptObjectWrapperBase.GetExecution: IdwsProgramExecution;
begin
Result := FExecution;
if (Result = nil) and (FScriptObj <> nil) then
Result := TScriptObjInstance(FScriptObj).ExecutionContext as IdwsProgramExecution;
end;
procedure TScriptObjectWrapperBase.Unregister;
var
Environment: IScriptEnvironment;
begin
if (FEnvironment <> nil) then
begin
Environment := FEnvironment;
FEnvironment := nil;
Environment.UnregisterWrapper(Self);
end;
end;
procedure TScriptObjectWrapperBase.Unregistered;
begin
if (FDestroying) then
exit;
FEnvironment := nil;
DetachWrappedObject;
DetachScriptObject;
end;
function TScriptObjectWrapperBase.AcquireInfo: IScriptObjectInfo;
begin
Result := TScriptObjectInfo.Create(FScriptObj);
end;
// -----------------------------------------------------------------------------
function TScriptObjectWrapper<T>.GetItem: T;
begin
Result := T(inherited Item);
end;
// -----------------------------------------------------------------------------
//
// TScriptComponentWrapper
//
// -----------------------------------------------------------------------------
constructor TScriptComponentWrapperBase<T>.Create(const AScriptObj: IScriptObj; AItem: TObject; AOwnsItem: boolean);
begin
ASSERT(AItem is TComponent);
inherited Create(AScriptObj, AItem, AOwnsItem);
end;
constructor TScriptComponentWrapperBase<T>.CreateOwned(AProgramInfo: TProgramInfo; const AScriptObj: IScriptObj; AComponentClass: TComponentClass);
var
OwnerWrapper: TScriptObjectWrapperBase;
Owner: TComponent;
Component: TComponent;
begin
OwnerWrapper := AProgramInfo.Params[0].ExternalObject as TScriptObjectWrapperBase;
if (OwnerWrapper <> nil) and (OwnerWrapper.Item is TComponent) then
Owner := TComponent(OwnerWrapper.Item)
else
Owner := nil;
Component := AComponentClass.Create(Owner);
Create(AScriptObj, Component, True); // Script has created item: Wrapper owns item by default
end;
procedure TScriptComponentWrapperBase<T>.DoFreeWrappedObject;
begin
if (Item <> nil) and (not (csDestroying in Item.ComponentState)) then
inherited DoFreeWrappedObject;
end;
// -----------------------------------------------------------------------------
//
// TScriptControlWrapper
//
// -----------------------------------------------------------------------------
constructor TScriptControlWrapper.Create(const AScriptObj: IScriptObj; AItem: TObject; AOwnsItem: boolean);
begin
ASSERT(AItem is TControl);
inherited Create(AScriptObj, AItem, AOwnsItem);
TControl(AItem).Margins.SetBounds(0, 0, 0, 0);
end;
// -----------------------------------------------------------------------------
procedure TScriptControlWrapper.DetachWrappedObject;
begin
if (Item <> nil) then
begin
TControlCracker(Item).OnClick := nil;
TControlCracker(Item).OnDblClick := nil;
end;
inherited;
end;
// -----------------------------------------------------------------------------
function TScriptControlWrapper.GetWinControl: TWinControl;
begin
if (Item <> nil) and (not(Item is TWinControl)) then
raise Exception.CreateFmt('Invalid cast to TWinControl from %s', [Item.ClassName]);
Result := TWinControl(Item);
end;
// -----------------------------------------------------------------------------
procedure TScriptControlWrapper.OnClickHandler(Sender: TObject);
var
ScriptObjectInfo: IScriptObjectInfo;
Delegate: IInfo;
begin
if (not CheckScriptObj) then
begin
TControlCracker(Item).OnClick := nil;
raise EScript.Create('Script Object has been destroyed');
end;
ScriptObjectInfo := AcquireInfo;
Delegate := ScriptObjectInfo.Info.Member['FOnClick'];
if (Delegate <> nil) and (not Delegate.ValueIsEmpty) then
begin
// One way:
Delegate.Parameter['Sender'].Value := FScriptObj;
try
Delegate.Call;
except
TControlCracker(Item).OnClick := nil;
ScriptHandleException(Execution);
end;
// Another way:
// Delegate.Call([FScriptObj]);
end;
end;
procedure TScriptControlWrapper.OnDblClickHandler(Sender: TObject);
var
ScriptObjectInfo: IScriptObjectInfo;
Delegate: IInfo;
begin
if (not CheckScriptObj) then
begin
TControlCracker(Item).OnDblClick := nil;
raise EScript.Create('Script Object has been destroyed');
end;
ScriptObjectInfo := AcquireInfo;
Delegate := ScriptObjectInfo.Info.Member['FOnDblClick'];
if (Delegate <> nil) and (not Delegate.ValueIsEmpty) then
begin
try
Delegate.Call([FScriptObj]);
except
TControlCracker(Item).OnDblClick := nil;
ScriptHandleException(Execution);
end;
end;
end;
// -----------------------------------------------------------------------------
//
// TScriptControlWrapperBase<T>
//
// -----------------------------------------------------------------------------
function TScriptControlWrapperBase<T>.GetItem: T;
begin
Result := T(inherited Item);
end;
// -----------------------------------------------------------------------------
//
// TCustomScriptModule
//
// -----------------------------------------------------------------------------
procedure TCustomScriptModule.AfterConstruction;
begin
inherited;
// Release the constructor's implicit refcount
InterlockedDecrement(FRefCount);
end;
procedure TCustomScriptModule.BeforeDestruction;
begin
inherited;
ASSERT(FRefCount = 0);
end;
constructor TCustomScriptModule.Create(AOwner: TComponent);
procedure DeprecateUnimplemented(dwsFunction: TdwsFunction); overload;
begin
if (Assigned(dwsFunction.OnEval)) or (Assigned(dwsFunction.OnFastEval)) or (dwsFunction.Deprecated <> '') then
exit;
dwsFunction.Deprecated := 'Not implemented';
OutputDebugString(dwsFunction.GetNamePath+' not implemented');
end;
procedure DeprecateUnimplemented(dwsClass: TdwsClass; dwsMethod: TdwsMethod); overload;
begin
if (Assigned(dwsMethod.OnEval)) or (dwsMethod.Deprecated <> '') then
exit;
if (maAbstract in dwsMethod.Attributes) then
begin
if (dwsClass.IsSealed) then
begin
dwsMethod.Deprecated := 'Not implemented';
OutputDebugString(dwsMethod.GetNamePath+' abstract, not implemented and class is sealed');
exit;
end;
if (maVirtual in dwsMethod.Attributes) then
exit;
dwsMethod.Deprecated := 'Not implemented';
OutputDebugString(dwsMethod.GetNamePath+' abstract, not implemented and not marked virtual');
exit;
end;
dwsMethod.Deprecated := 'Not implemented';
if (dwsClass.IsSealed) then
OutputDebugString(dwsMethod.GetNamePath+' not implemented (and class is sealed)')
else
if (dwsClass.IsAbstract) then
OutputDebugString(dwsMethod.GetNamePath+' not implemented (but class is abstract)')
else
OutputDebugString(dwsMethod.GetNamePath+' not implemented');
end;
procedure DeprecateUnimplemented(dwsProperty: TdwsProperty; dwsMethod: TdwsMethod; dwsField: TdwsField; Reader: boolean); overload;
var
sType: string;
Offset: integer;
IndexOffset: integer;
i: integer;
begin
if (Reader) then
begin
sType := 'reader';
Offset := 0;
end else
begin
sType := 'writer';
Offset := 1;
end;
if (VarIsEmpty(dwsProperty.IndexValue)) then
IndexOffset := 0
else
IndexOffset := 1;
if (dwsMethod = nil) and (dwsField = nil) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' field or method not found');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not implemented';
end else
if (dwsField <> nil) then
begin
if (dwsProperty.Parameters.Count > 0) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' is a field but property has parameters');
dwsProperty.Deprecated := 'Not correctly implemented';
exit;
end;
if (IndexOffset > 0) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' is a field but property has index');
dwsProperty.Deprecated := 'Not correctly implemented';
exit;
end;
if (not AnsiSameText(dwsProperty.DataType, dwsField.DataType)) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' field has incorrect type');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
exit;
end;
end else
if (dwsMethod <> nil) then
begin
if not ((maAbstract in dwsMethod.Attributes) or (Assigned(dwsMethod.OnEval)) or (dwsMethod.Deprecated <> '')) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method not implemented');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not implemented';
// continue
end;
if (dwsProperty.Parameters.Count <> dwsMethod.Parameters.Count-Offset-IndexOffset) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect parameter count');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
exit;
end;
if (IndexOffset > 0) and (not AnsiSameText(dwsProperty.IndexType, TdwsParameter(dwsMethod.Parameters.Items[0]).DataType)) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect index parameter type');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
// continue
end;
if (Reader) then
begin
if (not (dwsMethod.Kind in [mkFunction, mkClassFunction])) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect Kind');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
// continue
end;
if (not AnsiSameText(dwsProperty.DataType, dwsMethod.ResultType)) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect result type');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
// continue
end;
end else
begin
if (not (dwsMethod.Kind in [mkProcedure, mkMethod, mkClassProcedure, mkClassMethod])) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect Kind');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
// continue
end;
if (not AnsiSameText(dwsProperty.DataType, TdwsParameter(dwsMethod.Parameters.Items[dwsMethod.Parameters.Count-1]).DataType)) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect parameter type');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
// continue
end;
end;
for i := 0 to dwsProperty.Parameters.Count-1 do
if (not AnsiSameText(TdwsParameter(dwsProperty.Parameters.Items[i]).DataType, TdwsParameter(dwsMethod.Parameters.Items[i+IndexOffset]).DataType)) then
begin
OutputDebugString(dwsProperty.GetNamePath+': '+sType+' method has incorrect parameter type');
if (dwsProperty.Deprecated = '') then
dwsProperty.Deprecated := 'Not correctly implemented';
break;
end;
end;
end;
procedure DeprecateUnimplemented(dwsUnit: TdwsUnit; dwsClass: TdwsClass; dwsProperty: TdwsProperty); overload;
var
dwsMethod: TdwsMethod;
dwsField: TdwsField;
dwsBaseClass: TdwsClass;
begin
if (dwsProperty.ReadAccess = '') and (dwsProperty.WriteAccess = '') then
begin
if (dwsClass.Ancestor = '') then
begin
OutputDebugString(dwsProperty.GetNamePath+' can not propagate without base class');
dwsProperty.Deprecated := 'Not correctly implemented';
end else
if (dwsUnit.Classes.Symbols[dwsClass.Ancestor] <> nil) then
begin
dwsBaseClass := TdwsClass(dwsUnit.Classes.Symbols[dwsClass.Ancestor]);
if (dwsBaseClass.Properties.Symbols[dwsProperty.Name] = nil) then
;//OutputDebugString(dwsProperty.GetNamePath+' was delegated but is not found in base class');
end;
exit;
end;
if (dwsProperty.ReadAccess = '') then
OutputDebugString(dwsProperty.GetNamePath+' has no reader specified')
else
begin
dwsField := TdwsField(dwsClass.Fields.Symbols[dwsProperty.ReadAccess]);
dwsMethod := TdwsMethod(dwsClass.Methods.Symbols[dwsProperty.ReadAccess]);
DeprecateUnimplemented(dwsProperty, dwsMethod, dwsField, True);
end;
if (dwsProperty.WriteAccess <> '') then
begin
dwsField := TdwsField(dwsClass.Fields.Symbols[dwsProperty.WriteAccess]);
if (dwsField <> nil) and (dwsProperty.Parameters.Count > 0) then
begin