DWS.Script.pas 6.6 KB

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