Skip to content

Commit 480b476

Browse files
committedJul 10, 2024·
Update lape
1 parent 7525983 commit 480b476

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed
 

‎Source/script/simba.script.pas

+23-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ interface
1919
PSimbaScript = ^TSimbaScript;
2020
TSimbaScript = class(TObject)
2121
protected
22-
FState: TInitBool;
2322
FUserTerminated: Boolean;
2423
FTargetWindow: TWindowHandle;
2524
FHints: Boolean;
@@ -28,6 +27,7 @@ TSimbaScript = class(TObject)
2827
FScriptFileName: String;
2928

3029
FCompiler: TSimbaScript_Compiler;
30+
FCodeRunner: TLapeCodeRunner;
3131
FCompileTime: Double;
3232
FRunningTime: Double;
3333

@@ -155,12 +155,14 @@ function TSimbaScript.DoCompilerHandleDirective(Sender: TLapeCompiler; Directive
155155

156156
function TSimbaScript.GetState: ESimbaScriptState;
157157
begin
158-
case FState of
159-
bTrue: Result := ESimbaScriptState.STATE_RUNNING;
160-
bFalse: Result := ESimbaScriptState.STATE_STOP;
161-
else
162-
Result := ESimbaScriptState.STATE_PAUSED;
163-
end;
158+
if (FCodeRunner = nil) then
159+
Result := ESimbaScriptState.STATE_NONE
160+
else if FCodeRunner.isRunning then
161+
Result := ESimbaScriptState.STATE_RUNNING
162+
else if FCodeRunner.isStopped then
163+
Result := ESimbaScriptState.STATE_STOP
164+
else if FCodeRunner.isPaused then
165+
Result := ESimbaScriptState.STATE_PAUSED;
164166
end;
165167

166168
procedure TSimbaScript.SetTargetWindow(Value: String);
@@ -213,15 +215,16 @@ function TSimbaScript.Run: Boolean;
213215

214216
FRunningTime := HighResolutionTime();
215217
try
216-
RunCode(FCompiler.Emitter, FState);
218+
FCodeRunner := TLapeCodeRunner.Create(FCompiler.Emitter);
219+
FCodeRunner.Run();
217220
finally
218221
FRunningTime := HighResolutionTime() - FRunningTime;
219222

220223
FPlugins.CallOnStop();
221224

222225
if FUserTerminated then
223-
FCompiler.CallProc('_CallOnUserTerminate', True);
224-
FCompiler.CallProc('_CallOnTerminate', True);
226+
FCompiler.CallProc('_CallOnUserTerminate');
227+
FCompiler.CallProc('_CallOnTerminate');
225228
end;
226229

227230
Result := True;
@@ -231,8 +234,6 @@ constructor TSimbaScript.Create(Communication: TSimbaScriptCommunication);
231234
begin
232235
inherited Create();
233236

234-
FState := bTrue;
235-
236237
FSimbaCommunication := Communication;
237238
FScript := FSimbaCommunication.GetScript(FScriptFileName);
238239
end;
@@ -241,8 +242,6 @@ constructor TSimbaScript.Create(FileName: String; Communication: TSimbaScriptCom
241242
begin
242243
inherited Create();
243244

244-
FState := bTrue;
245-
246245
FSimbaCommunication := Communication;
247246

248247
FScriptFileName := FileName;
@@ -268,33 +267,36 @@ destructor TSimbaScript.Destroy;
268267
end;
269268
if (FSimbaCommunication <> nil) then
270269
FreeAndNil(FSimbaCommunication);
270+
if (FCodeRunner <> nil) then
271+
FreeAndNil(FCodeRunner);
271272

272273
inherited Destroy();
273274
end;
274275

275276
procedure TSimbaScript.SetState(Value: ESimbaScriptState);
276277
begin
278+
if (FCodeRunner = nil) then
279+
Exit;
280+
277281
case Value of
278282
ESimbaScriptState.STATE_RUNNING:
279283
begin
280284
FPlugins.CallOnResume();
281-
FCompiler.CallProc('_CallOnResume', True);
282-
283-
FState := bTrue;
285+
FCompiler.CallProc('_CallOnResume');
286+
FCodeRunner.Resume();
284287
end;
285288

286289
ESimbaScriptState.STATE_PAUSED:
287290
begin
288-
FState := bUnknown;
289-
291+
FCodeRunner.Pause();
290292
FPlugins.CallOnPause();
291-
FCompiler.CallProc('_CallOnPause', True);
293+
FCompiler.CallProc('_CallOnPause');
292294
end;
293295

294296
ESimbaScriptState.STATE_STOP:
295297
begin
296298
FUserTerminated := True;
297-
FState := bFalse;
299+
FCodeRunner.Stop();
298300
end;
299301
end;
300302

‎Source/script/simba.script_compiler.pas

+16-20
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TSimbaScript_Compiler = class(TLapeCompiler)
4949
procedure Import; virtual;
5050
function Compile: Boolean; override;
5151

52-
procedure CallProc(ProcName: String; UseFFI: Boolean);
52+
procedure CallProc(ProcName: String);
5353
property ImportingSection: String read GetImportingSection write FImportingSection;
5454
end;
5555

@@ -159,7 +159,7 @@ function TSimbaScript_Compiler.Compile: Boolean;
159159
Result := inherited Compile();
160160
end;
161161

162-
procedure TSimbaScript_Compiler.CallProc(ProcName: String; UseFFI: Boolean);
162+
procedure TSimbaScript_Compiler.CallProc(ProcName: String);
163163
var
164164
Method: TLapeGlobalVar;
165165
begin
@@ -168,16 +168,12 @@ procedure TSimbaScript_Compiler.CallProc(ProcName: String; UseFFI: Boolean);
168168
(TLapeType_Method(Method.VarType).Res <> nil) or (TLapeType_Method(Method.VarType).Params.Count <> 0) then
169169
SimbaException('CallProc: Invalid procedure "%s"', [ProcName]);
170170

171-
if UseFFI then
172-
begin
173-
with LapeExportWrapper(Method) do
174-
try
175-
TProcedure(Func)();
176-
finally
177-
Free();
178-
end;
179-
end else
180-
RunCode(FEmitter, [], PCodePos(Method.Ptr)^);
171+
with TLapeCodeRunner.Create(Emitter) do
172+
try
173+
Run([], PCodePos(Method.Ptr)^);
174+
finally
175+
Free();
176+
end;
181177
end;
182178

183179
function TSimbaScript_Compiler.GetImportingSection: String;
@@ -294,23 +290,23 @@ procedure TSimbaScript_Compiler.InitBaseString;
294290

295291
addGlobalFunc('function CompareStr(s1, s2: string): Int32;', @_LapeCompareStr);
296292
addGlobalFunc('function CompareText(s1, s2: string): Int32;', @_LapeCompareText);
297-
addGlobalFunc('function SameText(s1, s2: string): EvalBool;', @_LapeSameText);
293+
addGlobalFunc('function SameText(s1, s2: string): Boolean;', @_LapeSameText);
298294

299295
// Uses current user locale
300296
addGlobalFunc('function AnsiUpperCase(s: string): string;', @_LapeAnsiUpperCase);
301297
addGlobalFunc('function AnsiLowerCase(s: string): string;', @_LapeAnsiLowerCase);
302298
addGlobalFunc('function AnsiCompareStr(s1, s2: string): Int32;', @_LapeAnsiCompareStr);
303299
addGlobalFunc('function AnsiCompareText(s1, s2: string): Int32;', @_LapeAnsiCompareText);
304-
addGlobalFunc('function AnsiSameText(s1,s2: string): EvalBool;', @_LapeAnsiSameText);
305-
addGlobalFunc('function AnsiSameStr(s1,s2: string): EvalBool;', @_LapeAnsiSameStr);
300+
addGlobalFunc('function AnsiSameText(s1,s2: string): Boolean;', @_LapeAnsiSameText);
301+
addGlobalFunc('function AnsiSameStr(s1,s2: string): Boolean;', @_LapeAnsiSameStr);
306302

307303
// Uses current user locale
308304
addGlobalFunc('function WideUpperCase(s: WideString): WideString;', @_LapeWideUpperCase);
309305
addGlobalFunc('function WideLowerCase(s: WideString): WideString;', @_LapeWideLowerCase);
310306
addGlobalFunc('function WideCompareStr(s1, s2: WideString): Int32;', @_LapeWideCompareStr);
311307
addGlobalFunc('function WideCompareText(s1, s2: WideString): Int32;', @_LapeWideCompareText);
312-
addGlobalFunc('function WideSameText(s1,s2: WideString): EvalBool;', @_LapeWideSameText);
313-
addGlobalFunc('function WideSameStr(s1,s2: WideString): EvalBool;', @_LapeWideSameStr);
308+
addGlobalFunc('function WideSameText(s1,s2: WideString): Boolean;', @_LapeWideSameText);
309+
addGlobalFunc('function WideSameStr(s1,s2: WideString): Boolean;', @_LapeWideSameStr);
314310
addGlobalFunc('function WideFormat(Fmt: WideString; Args: array of Variant): WideString;', @_LapeWideFormat);
315311

316312
addGlobalFunc('function Pos(Substr, Source: AnsiString): SizeInt; overload;', @_LapePosA);
@@ -342,10 +338,10 @@ procedure TSimbaScript_Compiler.InitBaseString;
342338
addGlobalFunc('function StrToFloat(s: string; Def: Double): Double; overload;', @_LapeStrToFloatDef);
343339
addGlobalFunc('function StrToCurr(s: string): Currency; overload;', @_LapeStrToCurr);
344340
addGlobalFunc('function StrToCurr(s: string; Def: Currency): Currency; overload;', @_LapeStrToCurrDef);
345-
addGlobalFunc('function StrToBool(s: string): EvalBool; overload;', @_LapeStrToBool);
346-
addGlobalFunc('function StrToBool(s: string; Default: EvalBool): EvalBool; overload;', @_LapeStrToBoolDef);
341+
addGlobalFunc('function StrToBool(s: string): Boolean; overload;', @_LapeStrToBool);
342+
addGlobalFunc('function StrToBool(s: string; Default: Boolean): Boolean; overload;', @_LapeStrToBoolDef);
347343

348-
addGlobalFunc('function BoolToStr(B: EvalBool; TrueS: string = ''True''; FalseS: string = ''False''): string;', @_LapeBoolToStr);
344+
addGlobalFunc('function BoolToStr(B: Boolean; TrueS: string = ''True''; FalseS: string = ''False''): string;', @_LapeBoolToStr);
349345
addGlobalFunc('function FloatToStr(f: Double): string;', @_LapeToString_Double);
350346
addGlobalFunc('function CurrToStr(Value: Currency): string;', @_LapeToString_Currency);
351347

0 commit comments

Comments
 (0)
Please sign in to comment.