-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathamScript.Environment.pas
More file actions
241 lines (199 loc) · 6.56 KB
/
amScript.Environment.pas
File metadata and controls
241 lines (199 loc) · 6.56 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
unit amScript.Environment;
(*
* 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
Generics.Collections,
dwsSymbols,
dwsUtils,
amScript.Module,
amScript.Host.API;
// -----------------------------------------------------------------------------
//
// TScriptEnvironment
//
// -----------------------------------------------------------------------------
type
TScriptEnvironment = class(TInterfacedSelfObject, IdwsEnvironment, IScriptEnvironment)
strict private
class var FEnvironments: TList<TScriptEnvironment>;
private
FWrappedObjects: TDictionary<TObject, TScriptObjectWrapperBase>;
FApplication: IScriptHostApplication;
FDocument: IScriptHostDocument;
FItem: TObject;
protected
procedure ClearLeakedWrappers;
// IScriptEnvironment
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);
protected
class constructor Create;
class destructor Destroy;
public
constructor Create(const AApplication: IScriptHostApplication; const ADocument: IScriptHostDocument; AItem: TObject);
destructor Destroy; override;
// Find wrapper across all environments
class function FindWrappers(AObject: TObject): TWrapperList;
class procedure UnregisterWrappers(AObject: TObject);
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
implementation
uses
SysUtils,
Classes,
{$ifdef DEBUG}
Dialogs,
{$endif DEBUG}
{$ifdef DEBUG}
amDialogs,
{$endif DEBUG}
amScript.API;
type
TScriptObjectWrapperBaseCracker = class(TScriptObjectWrapperBase);
// -----------------------------------------------------------------------------
//
// TScriptEnvironment
//
// -----------------------------------------------------------------------------
class constructor TScriptEnvironment.Create;
begin
FEnvironments := TList<TScriptEnvironment>.Create;
end;
class destructor TScriptEnvironment.Destroy;
begin
FreeAndNil(FEnvironments);
end;
// -----------------------------------------------------------------------------
constructor TScriptEnvironment.Create(const AApplication: IScriptHostApplication; const ADocument: IScriptHostDocument; AItem: TObject);
begin
inherited Create;
FApplication := AApplication;
FDocument := ADocument;
FItem := AItem;
FEnvironments.Add(Self);
end;
destructor TScriptEnvironment.Destroy;
begin
ClearLeakedWrappers;
FEnvironments.Remove(Self);
FreeAndNil(FWrappedObjects);
inherited;
end;
// -----------------------------------------------------------------------------
procedure TScriptEnvironment.ClearLeakedWrappers;
var
i: integer;
Wrappers: TArray<TScriptObjectWrapperBase>;
{$ifdef DEBUG}
Item: TPair<TObject, TScriptObjectWrapperBase>;
s: string;
{$endif DEBUG}
begin
if (FWrappedObjects = nil) then
exit;
{$ifdef DEBUG}
if (FWrappedObjects.Count > 0) then
begin
for Item in FWrappedObjects do
s := s + #13 + Item.Key.ClassName + ' wrapped by ' + Item.Value.ClassName;
MessageTaskDlgEx('Script', Format('Leaked wrappers: %d'+s, [FWrappedObjects.Count]), mtInformation, [mbOK]);
end;
{$endif DEBUG}
Wrappers := FWrappedObjects.Values.ToArray;
for i := Length(Wrappers)-1 downto 0 do
Wrappers[i].DetachScriptObject;
FWrappedObjects.Clear;
end;
function TScriptEnvironment.GetApplication: IScriptHostApplication;
begin
Result := FApplication;
end;
// -----------------------------------------------------------------------------
function TScriptEnvironment.GetItem: TObject;
begin
Result := FItem;
end;
// -----------------------------------------------------------------------------
function TScriptEnvironment.GetDocument: IScriptHostDocument;
begin
Result := FDocument;
end;
// -----------------------------------------------------------------------------
procedure TScriptEnvironment.RegisterWrapper(Wrapper: TScriptObjectWrapperBase);
begin
if (FWrappedObjects = nil) then
FWrappedObjects := TDictionary<TObject, TScriptObjectWrapperBase>.Create;
FWrappedObjects.AddOrSetValue(Wrapper.Item, Wrapper);
if (Wrapper.Item is TComponent) then
ScriptService.RegisterWrappedComponent(TComponent(Wrapper.Item));
end;
procedure TScriptEnvironment.UnregisterWrapper(Wrapper: TScriptObjectWrapperBase);
var
Item: TPair<TObject, TScriptObjectWrapperBase>;
begin
if (FWrappedObjects <> nil) then
begin
Item := FWrappedObjects.ExtractPair(Wrapper.Item);
if (Item.Value <> nil) then
TScriptObjectWrapperBaseCracker(Wrapper).Unregistered;
end;
end;
function TScriptEnvironment.FindWrapper(AObject: TObject): TScriptObjectWrapperBase;
begin
if (FWrappedObjects <> nil) then
begin
if (not FWrappedObjects.TryGetValue(AObject, Result)) then
Result := nil;
end else
Result := nil;
end;
// -----------------------------------------------------------------------------
class function TScriptEnvironment.FindWrappers(AObject: TObject): TWrapperList;
var
i: integer;
Wrapper: TScriptObjectWrapperBase;
begin
ASSERT(FEnvironments <> nil);
for i := 0 to FEnvironments.Count-1 do
begin
Wrapper := FEnvironments[i].FindWrapper(AObject);
if (Wrapper <> nil) then
begin
SetLength(Result, Length(Result)+1);
Result[Length(Result)-1] := Wrapper;
end;
end;
end;
class procedure TScriptEnvironment.UnregisterWrappers(AObject: TObject);
var
i: integer;
Wrapper: TScriptObjectWrapperBase;
begin
if (FEnvironments = nil) then
exit;
for i := FEnvironments.Count-1 downto 0 do
begin
Wrapper := FEnvironments[i].FindWrapper(AObject);
if (Wrapper <> nil) then
TScriptObjectWrapperBaseCracker(Wrapper).Unregister;
end;
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
end.