You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
9.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. */
  20. #ifndef __JUCETICE_SCRIPTABLEENGINE_HEADER__
  21. #define __JUCETICE_SCRIPTABLEENGINE_HEADER__
  22. #if JUCE_SUPPORT_SCRIPTING
  23. //==============================================================================
  24. /** Manages a script to access functionality of juce
  25. @see angelscript
  26. */
  27. class ScriptableEngine
  28. {
  29. public:
  30. //==============================================================================
  31. enum EngineStatus
  32. {
  33. Invalid = 0,
  34. Ready = 1,
  35. Compiled = 2,
  36. Running = 3,
  37. Paused = 4
  38. };
  39. //==============================================================================
  40. /** Creates a script engine. */
  41. ScriptableEngine();
  42. /** Destructor. */
  43. virtual ~ScriptableEngine();
  44. //==============================================================================
  45. /** Executes a current script
  46. */
  47. virtual void executeScript (const String& script);
  48. /**
  49. */
  50. virtual bool compileScript (const String& scriptCode);
  51. /**
  52. */
  53. virtual void executeFunction (const String& functionName);
  54. /** Prints an error reported by the engine
  55. When the engine encounter an error compiling or at runtime, it will trigger
  56. this function. Obviously you can override this in order to allow your
  57. specific engine to explicitly ignore errors, or to redirect error reporting
  58. to a GUI component, a message box or whatever your application needs.
  59. */
  60. virtual void printErrorMessage (const String& type,
  61. const String& message,
  62. const String& section,
  63. const int lineNumber);
  64. //==============================================================================
  65. /** Return the current engine */
  66. void* getCurrentEngine () const { return engine; }
  67. protected:
  68. //==============================================================================
  69. /** Initialise the engine
  70. Must be called before trying to do anything to the engine itself, and if
  71. this returns false, then it will be impossible to use the engine itself.
  72. */
  73. virtual void initialise ();
  74. /** Shutdown the engine
  75. Should be called automatically when the object goes out of scope, but can be
  76. called if you need to manually free any engine and scripts allocated.
  77. */
  78. virtual void shutdown ();
  79. //==============================================================================
  80. void* engine;
  81. EngineStatus status;
  82. };
  83. #if 0
  84. #include "../juce_core/text/juce_XmlDocument.h"
  85. #include "../juce_core/threads/juce_Thread.h"
  86. #include "../juce_core/io/files/juce_DirectoryIterator.h"
  87. #include "../juce_appframework/events/juce_ChangeBroadcaster.h"
  88. #include "../juce_appframework/audio/midi/juce_MidiKeyboardState.h"
  89. #include "../juce_appframework/audio/processors/juce_AudioProcessor.h"
  90. #include "containers/jucetice_LockFreeFifo.h"
  91. class ScriptableEngine;
  92. class asIScriptEngine;
  93. class asIScriptContext;
  94. //==============================================================================
  95. /** Manages a script to access functionality of juce
  96. @see angelscript
  97. */
  98. class ScriptableEngine
  99. {
  100. public:
  101. //==============================================================================
  102. /** Creates a script engine. */
  103. ScriptableEngine();
  104. /** Destructor. */
  105. virtual ~ScriptableEngine();
  106. //==============================================================================
  107. void setDefaultContextStackSize (const int initialSize, const int maxSize);
  108. //==============================================================================
  109. /** Registers a global property to be used in the script
  110. This function is actually used in the main application to publish app managed
  111. objects and variables to the script sections.
  112. @param declaration this is the declaration of the variable,
  113. usually something like "Object obj"
  114. @param object this is a void pointer to the object the
  115. application is publishing
  116. */
  117. void registerGlobalProperty (const String& declaration, void* object);
  118. //==============================================================================
  119. /** Add a script section to the engine
  120. You can add any number of script sections you need, but u have to
  121. name them with different identifier. This also compile
  122. the script that u added
  123. @param strScript this is the script code as string
  124. @param strSection this is the name of the section you are compiling
  125. since more session are allowed, you have to make different
  126. names for different sections
  127. */
  128. bool compileScript (const String& strScript,
  129. const String& strSection = "main"
  130. // ,const String& strModule = 0,
  131. );
  132. //==============================================================================
  133. /** This check for a function defined in the script
  134. and execute it in the context we have created
  135. Actually this support only one context at time
  136. @param strFunction this is the declaration of the function you
  137. would like to execute
  138. */
  139. bool executeFunction (const String& strFunction
  140. // ,const String& strModule = 0,
  141. );
  142. /** This execute a string of code
  143. @param strScript this is the script you want to execute
  144. */
  145. bool executeString (const String& strScript);
  146. //==============================================================================
  147. /** This abort the current running context
  148. Actually this is only useful in debug mode
  149. */
  150. void abortExecution ();
  151. /** This let continue execution if in debug mode
  152. Actually this is only useful in debug mode
  153. */
  154. void continueExecution ();
  155. //==============================================================================
  156. /** This lets you add directory to the resolve path when looking for
  157. files to include or a plugin library to load
  158. */
  159. void addResolveFilePath (const File& directoryToLookInto);
  160. /** This lets you remove a previously added directory to the resolve path
  161. */
  162. void removeResolveFilePath (const File& directoryToLookInto);
  163. /** This lets you try to locate a file in the resolve paths, useful for
  164. trying to locate add directory to the resolve path when looking for
  165. files to include or a plugin library to load
  166. */
  167. File findResolveFilePaths (const String& fileName);
  168. //==============================================================================
  169. /** Passing a function identifier it returns the declaration
  170. */
  171. String resolveFunctionDeclaration (const int functionID);
  172. //==============================================================================
  173. void setEngineModel (ScriptableEngineModel* modelToNotify) { engineModel = modelToNotify; }
  174. //==============================================================================
  175. /**@internal*/
  176. asIScriptEngine* getEngine() const { return engine; }
  177. /**@internal*/
  178. void resetEngine ();
  179. /**@internal*/
  180. asIScriptContext* createContext();
  181. /**@internal*/
  182. bool executeContext (asIScriptContext* context);
  183. /**@internal*/
  184. String serializeVariable (asIScriptContext* ctx, const int i);
  185. protected:
  186. //==============================================================================
  187. /** Use this to do some jobs before script sections compilation */
  188. virtual void preCompileCallback () {}
  189. /** Used in debugging mode to do some jobs every line of the script */
  190. virtual void debugLineCallback (asIScriptContext* context) {}
  191. //==============================================================================
  192. /** Use this to set your specific report when the compiler
  193. gets messed up. Here you could write to a Log file or
  194. use the AlertWindow to report errors to user.
  195. */
  196. virtual void reportErrors (const String& errorType,
  197. const String& errorString,
  198. const String& fileName,
  199. const int lineNumber) {}
  200. //==============================================================================
  201. /** Use this to output lines from plugins in a IDE output section.
  202. This is useful when u want the user defined plugins to be able to
  203. interact with the ide directly, outputting strings in the text
  204. editor debugger.
  205. */
  206. virtual void reportOutput (const String& message, const int appendMode) {}
  207. StringArray resolvePaths;
  208. int timeOut;
  209. bool debugMode;
  210. bool warningsOn;
  211. bool preprocessFailed;
  212. bool compileFailed;
  213. bool runningScript;
  214. bool abortScript;
  215. bool continueSilently; // @TODO - change this to step to next breakpoint
  216. asIScriptEngine *engine;
  217. void* outStream;
  218. };
  219. #endif
  220. #endif // JUCE_SUPPORT_SCRIPTING
  221. #endif // __JUCETICE_SCRIPTABLEENGINE_HEADER__