GLScriptDWS.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. (*
  5. DelphiWebScript implementation for the GLScene scripting layer.
  6. *)
  7. unit GLScriptDWS;
  8. interface
  9. uses
  10. System.Classes,
  11. System.SysUtils,
  12. XCollection,
  13. GLScriptBase,
  14. dwsComp,
  15. dwsExprs,
  16. dwsSymbols,
  17. GLManager;
  18. type
  19. {This class only adds manager registration logic to the TDelphiWebScriptII
  20. class to enable the XCollection items (ie. TGLScriptDWS) retain it's
  21. assigned compiler from design to run -time. }
  22. TGLDelphiWebScript = class(TDelphiWebScript)
  23. public
  24. constructor Create(AOnwer : TComponent); override;
  25. destructor Destroy; override;
  26. end;
  27. // Implements DelphiWebScript scripting functionality through the abstracted GLScriptBase
  28. TGLScriptDWS = class(TGLScriptBase)
  29. private
  30. FDWSProgram : TProgram;
  31. FCompiler : TGLDelphiWebScript;
  32. FCompilerName : String;
  33. protected
  34. procedure SetCompiler(const Value : TGLDelphiWebScriptII);
  35. procedure ReadFromFiler(reader : TReader); override;
  36. procedure WriteToFiler(writer : TWriter); override;
  37. procedure Loaded; override;
  38. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  39. function GetState : TGLScriptState; override;
  40. public
  41. destructor Destroy; override;
  42. procedure Assign(Source: TPersistent); override;
  43. procedure Compile; override;
  44. procedure Start; override;
  45. procedure Stop; override;
  46. procedure Execute; override;
  47. procedure Invalidate; override;
  48. function Call(aName : String;
  49. aParams : array of Variant) : Variant; override;
  50. class function FriendlyName : String; override;
  51. class function FriendlyDescription : String; override;
  52. class function ItemCategory : String; override;
  53. property DWS2Program : TProgram read FDWS2Program;
  54. published
  55. property Compiler : TGLDelphiWebScriptII read FCompiler write SetCompiler;
  56. end;
  57. procedure Register;
  58. // --------------------------------------------------
  59. implementation
  60. // --------------------------------------------------
  61. // ---------------
  62. // --------------- Miscellaneous ---------------
  63. // ---------------
  64. procedure Register;
  65. begin
  66. RegisterClasses([TGLDelphiWebScript, TGLScriptDWS]);
  67. RegisterComponents('GLScene DWS', [TGLDelphiWebScript]);
  68. end;
  69. // ----------
  70. // ---------- TGLDelphiWebScript ----------
  71. // ----------
  72. constructor TGLDelphiWebScript.Create(AOnwer : TComponent);
  73. begin
  74. inherited;
  75. RegisterManager(Self);
  76. end;
  77. destructor TGLDelphiWebScript.Destroy;
  78. begin
  79. DeregisterManager(Self);
  80. inherited;
  81. end;
  82. // ---------------
  83. // --------------- TGLScriptDWS ---------------
  84. // ---------------
  85. destructor TGLScriptDWS.Destroy;
  86. begin
  87. Invalidate;
  88. inherited;
  89. end;
  90. procedure TGLScriptDWS.Assign(Source: TPersistent);
  91. begin
  92. inherited;
  93. if Source is TGLScriptDWS then begin
  94. Compiler:=TGLScriptDWS(Source).Compiler;
  95. end;
  96. end;
  97. procedure TGLScriptDWS.ReadFromFiler(reader : TReader);
  98. var
  99. archiveVersion : Integer;
  100. begin
  101. inherited;
  102. archiveVersion:=reader.ReadInteger;
  103. Assert(archiveVersion = 0);
  104. with reader do begin
  105. FCompilerName:=ReadString;
  106. end;
  107. end;
  108. procedure TGLScriptDWS.WriteToFiler(writer : TWriter);
  109. begin
  110. inherited;
  111. writer.WriteInteger(0); // archiveVersion
  112. with writer do begin
  113. if Assigned(FCompiler) then
  114. WriteString(FCompiler.GetNamePath)
  115. else
  116. WriteString('');
  117. end;
  118. end;
  119. procedure TGLScriptDWS.Loaded;
  120. var
  121. temp : TComponent;
  122. begin
  123. inherited;
  124. if FCompilerName<>'' then begin
  125. temp:=FindManager(TGLDelphiWebScript, FCompilerName);
  126. if Assigned(temp) then
  127. Compiler:=TGLDelphiWebScript(temp);
  128. FCompilerName:='';
  129. end;
  130. end;
  131. procedure TGLScriptDWS.Notification(AComponent: TComponent; Operation: TOperation);
  132. begin
  133. if (AComponent = Compiler) and (Operation = opRemove) then
  134. Compiler:=nil;
  135. end;
  136. class function TGLScriptDWS.FriendlyName : String;
  137. begin
  138. Result:='GLScriptDWS';
  139. end;
  140. class function TGLScriptDWS.FriendlyDescription : String;
  141. begin
  142. Result:='DelphiWebScript script';
  143. end;
  144. class function TGLScriptDWS.ItemCategory : String;
  145. begin
  146. Result:='';
  147. end;
  148. procedure TGLScriptDWS.Compile;
  149. begin
  150. Invalidate;
  151. if Assigned(Compiler) then
  152. FDWS2Program:=Compiler.Compile(Text.Text)
  153. else
  154. raise Exception.Create('No compiler assigned!');
  155. end;
  156. procedure TGLScriptDWS.Execute;
  157. begin
  158. if (State = ssUncompiled) then
  159. Compile
  160. else if (State = ssRunning) then
  161. Stop;
  162. if (State = ssCompiled) then
  163. FDWS2Program.Execute;
  164. end;
  165. procedure TGLScriptDWS.Invalidate;
  166. begin
  167. if (State <> ssUncompiled) or Assigned(FDWSProgram) then begin
  168. Stop;
  169. FreeAndNil(FDWSProgram);
  170. end;
  171. end;
  172. procedure TGLScriptDWS.Start;
  173. begin
  174. if (State = ssUncompiled) then
  175. Compile;
  176. if (State = ssCompiled) then
  177. FDWS2Program.BeginProgram(False);
  178. end;
  179. procedure TGLScriptDWS.Stop;
  180. begin
  181. if (State = ssRunning) then
  182. FDWS2Program.EndProgram;
  183. end;
  184. function TGLScriptDWS.Call(aName: String; aParams: array of Variant) : Variant;
  185. var
  186. Symbol : TSymbol;
  187. Output : IInfo;
  188. begin
  189. if (State <> ssRunning) then
  190. Start;
  191. if State = ssRunning then begin
  192. Symbol:=FDWSProgram.Table.FindSymbol(aName);
  193. if Assigned(Symbol) then begin
  194. if Symbol is TFuncSymbol then begin
  195. Output:=FDWSProgram.Info.Func[aName].Call(aParams);
  196. if Assigned(Output) then
  197. Result:=Output.Value;
  198. end else
  199. raise Exception.Create('Expected TFuncSymbol but found '+Symbol.ClassName+' for '+aName);
  200. end else
  201. raise Exception.Create('Symbol not found for '+aName);
  202. end;
  203. end;
  204. procedure TGLScriptDWS.SetCompiler(const Value : TGLDelphiWebScript);
  205. begin
  206. if Value<>FCompiler then begin
  207. FCompiler:=Value;
  208. Invalidate;
  209. end;
  210. end;
  211. function TGLScriptDWS.GetState : TGLScriptState;
  212. begin
  213. Result:=ssUncompiled;
  214. if Assigned(FDWSProgram) then begin
  215. case FDWSProgram.ProgramState of
  216. psReadyToRun : Result:=ssCompiled;
  217. psRunning : Result:=ssRunning;
  218. else
  219. if FDWSProgram.Msgs.HasErrors then begin
  220. if FDWSProgram.Msgs.HasCompilerErrors then
  221. Result:=ssCompileErrors
  222. else if FDWSProgram.Msgs.HasExecutionErrors then
  223. Result:=ssRunningErrors;
  224. Errors.Text:=FDWSProgram.Msgs.AsInfo;
  225. end;
  226. end;
  227. end;
  228. end;
  229. // --------------------------------------------------
  230. initialization
  231. // --------------------------------------------------
  232. RegisterXCollectionItemClass(TGLScriptDWS);
  233. // --------------------------------------------------
  234. finalization
  235. // --------------------------------------------------
  236. UnregisterXCollectionItemClass(TGLScriptDWS);
  237. end.