/
Instruction.pas
258 lines (215 loc) · 6.22 KB
/
Instruction.pas
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
unit Instruction;
interface
uses SlimDirective, SlimContext, Rtti, Classes, SlimMethod;
type TInstruction = class
strict private
_rttiContextInitialized : Boolean;
_rttiContext : TRttiContext;
_instructions : array of TInstruction;
protected
function RttiContext : TRttiContext;
public
Id : string;
constructor Create; overload;
constructor Create(id : string); overload;
function GetLength : Integer; virtual;
function GetItem(index : Integer) : TInstruction;
property Length : Integer read GetLength;
function Execute(context : TSlimContext) : TSlimDirective; virtual;
procedure Add(item : TInstruction);
end;
type TListInstruction = class(TInstruction)
public
function Execute(context : TSlimContext): TSlimDirective; override;
end;
type TMakeInstruction = class(TInstruction)
strict private
function FindType(context : TSlimContext) : TRttiInstanceType;
function GetQualifiedNamesToTry(context : TSlimContext) : TStrings;
function CreateInstance(rttiType : TRttiInstanceType) : TObject;
public
InstanceName : string;
ClassToMake : string;
constructor Create; overload;
constructor Create(id, classToMake, instanceName : string); overload;
function Execute(context : TSlimContext): TSlimDirective; override;
end;
type TImportInstruction = class(TInstruction)
public
Path : string;
constructor Create; overload;
constructor Create(id, path : string); overload;
function Execute(context : TSlimContext): TSlimDirective; override;
end;
type TCallInstruction = class(TInstruction)
strict private
_arguments : TStrings;
_currentContext : TSlimContext;
function MethodToCall : TRttiMethod;
function InstanceType : TRttiType;
function Instance : TObject;
function SlimMethod : TSlimMethod;
public
InstanceName : string;
FunctionName : string;
property Arguments : TStrings read _arguments;
constructor Create; overload;
constructor Create(id, instanceName, functionToCall : string); overload;
procedure AddArgument(argument : string);
function Execute(context : TSlimContext): TSlimDirective; override;
end;
implementation
uses Logger, SysUtils;
{ TInstruction }
procedure TInstruction.Add(item : TInstruction);
begin
SetLength(_instructions, GetLength() + 1);
_instructions[GetLength() - 1] := item;
end;
constructor TInstruction.Create;
begin
end;
constructor TInstruction.Create(id: string);
begin
Self.Id := id;
end;
function TInstruction.Execute(context : TSlimContext): TSlimDirective;
begin
// Log('Execute ' + Self.ClassName + ' ' + Id);
Result := TSlimDirective.Create;
Result.Add(Id);
end;
function TInstruction.GetItem(index: Integer): TInstruction;
begin
Result := _instructions[index];
end;
function TInstruction.GetLength: Integer;
begin
Result := System.Length(_instructions);
end;
function TInstruction.RttiContext: TRttiContext;
begin
if not _rttiContextInitialized then
_rttiContext := TRttiContext.Create;
Result := _rttiContext;
end;
{ TMakeInstruction }
constructor TMakeInstruction.Create;
begin
end;
constructor TMakeInstruction.Create(id, classToMake, instanceName: string);
begin
inherited Create(id);
Self.ClassToMake := classToMake;
Self.InstanceName := instanceName;
end;
function TMakeInstruction.Execute(context : TSlimContext): TSlimDirective;
var foundType : TRttiInstanceType;
begin
Result := inherited;
foundType := FindType(context);
if foundType = nil then
Result.Add('__EXCEPTION__:message:<<NO_CLASS ' + ClassToMake + '>>')
else
begin
Result.Add('OK');
context.RegisterInstance(InstanceName, CreateInstance(foundType));
end;
end;
function TMakeInstruction.CreateInstance(rttiType : TRttiInstanceType): TObject;
var instance : TValue;
begin
instance := rttiType.GetMethod('Create').Invoke(rttiType.MetaclassType, []);
Result := instance.AsObject;
end;
function TMakeInstruction.FindType(context : TSlimContext): TRttiInstanceType;
var qualifiedName : string;
begin
Result := nil;
for qualifiedName in GetQualifiedNamesToTry(context) do
begin
Result := RttiContext.FindType(qualifiedName) as TRttiInstanceType;
if Result <> nil then
break;
end;
end;
function TMakeInstruction.GetQualifiedNamesToTry(context: TSlimContext): TStrings;
var importPath : string;
begin
Result := TStringList.Create;
Result.Add(ClassToMake);
for importPath in context.ImportPaths do
begin
Result.Add(importPath + '.' + ClassToMake);
end;
end;
{ TImportInstruction }
constructor TImportInstruction.Create(id, path : string);
begin
inherited Create(id);
Self.Path := path;
end;
constructor TImportInstruction.Create;
begin
end;
function TImportInstruction.Execute(context : TSlimContext): TSlimDirective;
begin
Result := inherited;
Result.Add('OK');
context.AddImportPath(Path);
end;
{ TCallInstruction }
constructor TCallInstruction.Create;
begin
_arguments := TStringList.Create;
end;
constructor TCallInstruction.Create(id, instanceName, functionToCall: string);
begin
inherited Create(id);
_arguments := TStringList.Create;
Self.FunctionName := functionToCall;
Self.InstanceName := instanceName;
end;
procedure TCallInstruction.AddArgument(argument: string);
begin
Arguments.Add(argument);
end;
function TCallInstruction.Execute(context : TSlimContext): TSlimDirective;
begin
Result := inherited;
_currentContext := context;
Result.Add(SlimMethod.Call(Arguments));
end;
function TCallInstruction.SlimMethod: TSlimMethod;
begin
if MethodToCall = nil then
Result := TNullSlimMethod.Create
else
Result := TSlimMethod.Create;
Result.FunctionName := FunctionName;
Result.Instance := Instance;
end;
function TCallInstruction.MethodToCall: TRttiMethod;
begin
Result := InstanceType.GetMethod(FunctionName);
end;
function TCallInstruction.InstanceType: TRttiType;
begin
Result := RttiContext.GetType(Instance.ClassType);
end;
function TCallInstruction.Instance: TObject;
begin
Result := _currentContext.GetRegisteredInstance(InstanceName);
end;
{ TListInstruction }
function TListInstruction.Execute(context : TSlimContext): TSlimDirective;
var i : Integer;
begin
inherited;
Result := TSlimDirective.Create;
for i := 0 to GetLength - 1 do
begin
Result.Add(GetItem(i).Execute(context));
end;
end;
end.