DWSx.Script.pas 6.5 KB

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