The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

93 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. /**
  22. A simple javascript interpreter!
  23. It's not fully standards-compliant, and won't be as fast as the fancy JIT-compiled
  24. engines that you get in browsers, but this is an extremely compact, low-overhead javascript
  25. interpreter, which is integrated with the juce var and DynamicObject classes. If you need
  26. a few simple bits of scripting in your app, and want to be able to easily let the JS
  27. work with native objects defined as DynamicObject subclasses, then this might do the job.
  28. To use, simply create an instance of this class and call execute() to run your code.
  29. Variables that the script sets can be retrieved with evaluate(), and if you need to provide
  30. native objects for the script to use, you can add them with registerNativeObject().
  31. One caveat: Because the values and objects that the engine works with are DynamicObject
  32. and var objects, they use reference-counting rather than garbage-collection, so if your
  33. script creates complex connections between objects, you run the risk of creating cyclic
  34. dependencies and hence leaking.
  35. */
  36. class JavascriptEngine
  37. {
  38. public:
  39. /** Creates an instance of the engine.
  40. This creates a root namespace and defines some basic Object, String, Array
  41. and Math library methods.
  42. */
  43. JavascriptEngine();
  44. /** Destructor. */
  45. ~JavascriptEngine();
  46. /** Attempts to parse and run a block of javascript code.
  47. If there's a parse or execution error, the error description is returned in
  48. the result.
  49. You can specify a maximum time for which the program is allowed to run, and
  50. it'll return with an error message if this time is exceeded.
  51. */
  52. Result execute (const String& javascriptCode,
  53. RelativeTime maximumRunTime = RelativeTime::seconds (10));
  54. /** Attempts to parse and run a javascript expression, and returns the result.
  55. If there's a syntax error, or the expression can't be evaluated, the return value
  56. will be var::undefined(). The errorMessage parameter gives you a way to find out
  57. any parsing errors.
  58. You can specify a maximum time for which the program is allowed to run, and
  59. it'll return with an error message if this time is exceeded.
  60. */
  61. var evaluate (const String& javascriptCode,
  62. Result* errorMessage = nullptr,
  63. RelativeTime maximumRunTime = RelativeTime::seconds (10));
  64. /** Adds a native object to the root namespace.
  65. The object passed-in is reference-counted, and will be retained by the
  66. engine until the engine is deleted. The name must be a simple JS identifier,
  67. without any dots.
  68. */
  69. void registerNativeObject (Identifier objectName, DynamicObject* object);
  70. private:
  71. struct RootObject;
  72. ReferenceCountedObjectPtr<RootObject> root;
  73. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavascriptEngine)
  74. };