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.

123 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A simple javascript interpreter!
  22. It's not fully standards-compliant, and won't be as fast as the fancy JIT-compiled
  23. engines that you get in browsers, but this is an extremely compact, low-overhead javascript
  24. interpreter, which is integrated with the juce var and DynamicObject classes. If you need
  25. a few simple bits of scripting in your app, and want to be able to easily let the JS
  26. work with native objects defined as DynamicObject subclasses, then this might do the job.
  27. To use, simply create an instance of this class and call execute() to run your code.
  28. Variables that the script sets can be retrieved with evaluate(), and if you need to provide
  29. native objects for the script to use, you can add them with registerNativeObject().
  30. One caveat: Because the values and objects that the engine works with are DynamicObject
  31. and var objects, they use reference-counting rather than garbage-collection, so if your
  32. script creates complex connections between objects, you run the risk of creating cyclic
  33. dependencies and hence leaking.
  34. @tags{Core}
  35. */
  36. class JUCE_API JavascriptEngine final
  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. /** Attempts to parse and run a javascript expression, and returns the result.
  54. If there's a syntax error, or the expression can't be evaluated, the return value
  55. will be var::undefined(). The errorMessage parameter gives you a way to find out
  56. any parsing errors.
  57. You can specify a maximum time for which the program is allowed to run, and
  58. it'll return with an error message if this time is exceeded.
  59. */
  60. var evaluate (const String& javascriptCode,
  61. Result* errorMessage = nullptr);
  62. /** Calls a function in the root namespace, and returns the result.
  63. The function arguments are passed in the same format as used by native
  64. methods in the var class.
  65. */
  66. var callFunction (const Identifier& function,
  67. const var::NativeFunctionArgs& args,
  68. Result* errorMessage = nullptr);
  69. /** Calls a function object in the namespace of a dynamic object, and returns the result.
  70. The function arguments are passed in the same format as used by native
  71. methods in the var class.
  72. */
  73. var callFunctionObject (DynamicObject* objectScope,
  74. const var& functionObject,
  75. const var::NativeFunctionArgs& args,
  76. Result* errorMessage = nullptr);
  77. /** Adds a native object to the root namespace.
  78. The object passed-in is reference-counted, and will be retained by the
  79. engine until the engine is deleted. The name must be a simple JS identifier,
  80. without any dots.
  81. */
  82. void registerNativeObject (const Identifier& objectName, DynamicObject* object);
  83. /** This value indicates how long a call to one of the evaluate methods is permitted
  84. to run before timing-out and failing.
  85. The default value is a number of seconds, but you can change this to whatever value
  86. suits your application.
  87. */
  88. RelativeTime maximumExecutionTime;
  89. /** When called from another thread, causes the interpreter to time-out as soon as possible */
  90. void stop() noexcept;
  91. /** Provides access to the set of properties of the root namespace object. */
  92. const NamedValueSet& getRootObjectProperties() const noexcept;
  93. private:
  94. JUCE_PUBLIC_IN_DLL_BUILD (struct RootObject)
  95. const ReferenceCountedObjectPtr<RootObject> root;
  96. void prepareTimeout() const noexcept;
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavascriptEngine)
  98. };
  99. } // namespace juce