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.

119 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. #pragma once
  18. //==============================================================================
  19. /**
  20. A simple javascript interpreter!
  21. It's not fully standards-compliant, and won't be as fast as the fancy JIT-compiled
  22. engines that you get in browsers, but this is an extremely compact, low-overhead javascript
  23. interpreter, which is integrated with the juce var and DynamicObject classes. If you need
  24. a few simple bits of scripting in your app, and want to be able to easily let the JS
  25. work with native objects defined as DynamicObject subclasses, then this might do the job.
  26. To use, simply create an instance of this class and call execute() to run your code.
  27. Variables that the script sets can be retrieved with evaluate(), and if you need to provide
  28. native objects for the script to use, you can add them with registerNativeObject().
  29. One caveat: Because the values and objects that the engine works with are DynamicObject
  30. and var objects, they use reference-counting rather than garbage-collection, so if your
  31. script creates complex connections between objects, you run the risk of creating cyclic
  32. dependencies and hence leaking.
  33. */
  34. class JUCE_API JavascriptEngine
  35. {
  36. public:
  37. /** Creates an instance of the engine.
  38. This creates a root namespace and defines some basic Object, String, Array
  39. and Math library methods.
  40. */
  41. JavascriptEngine();
  42. /** Destructor. */
  43. ~JavascriptEngine();
  44. /** Attempts to parse and run a block of javascript code.
  45. If there's a parse or execution error, the error description is returned in
  46. the result.
  47. You can specify a maximum time for which the program is allowed to run, and
  48. it'll return with an error message if this time is exceeded.
  49. */
  50. Result execute (const String& javascriptCode);
  51. /** Attempts to parse and run a javascript expression, and returns the result.
  52. If there's a syntax error, or the expression can't be evaluated, the return value
  53. will be var::undefined(). The errorMessage parameter gives you a way to find out
  54. any parsing errors.
  55. You can specify a maximum time for which the program is allowed to run, and
  56. it'll return with an error message if this time is exceeded.
  57. */
  58. var evaluate (const String& javascriptCode,
  59. Result* errorMessage = nullptr);
  60. /** Calls a function in the root namespace, and returns the result.
  61. The function arguments are passed in the same format as used by native
  62. methods in the var class.
  63. */
  64. var callFunction (const Identifier& function,
  65. const var::NativeFunctionArgs& args,
  66. Result* errorMessage = nullptr);
  67. /** Calls a function object in the namespace of a dynamic object, and returns the result.
  68. The function arguments are passed in the same format as used by native
  69. methods in the var class.
  70. */
  71. var callFunctionObject (DynamicObject* objectScope,
  72. const var& functionObject,
  73. const var::NativeFunctionArgs& args,
  74. Result* errorMessage = nullptr);
  75. /** Adds a native object to the root namespace.
  76. The object passed-in is reference-counted, and will be retained by the
  77. engine until the engine is deleted. The name must be a simple JS identifier,
  78. without any dots.
  79. */
  80. void registerNativeObject (const Identifier& objectName, DynamicObject* object);
  81. /** This value indicates how long a call to one of the evaluate methods is permitted
  82. to run before timing-out and failing.
  83. The default value is a number of seconds, but you can change this to whatever value
  84. suits your application.
  85. */
  86. RelativeTime maximumExecutionTime;
  87. /** When called from another thread, causes the interpreter to time-out as soon as possible */
  88. void stop() noexcept;
  89. /** Provides access to the set of properties of the root namespace object. */
  90. const NamedValueSet& getRootObjectProperties() const noexcept;
  91. private:
  92. JUCE_PUBLIC_IN_DLL_BUILD (struct RootObject)
  93. const ReferenceCountedObjectPtr<RootObject> root;
  94. void prepareTimeout() const noexcept;
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavascriptEngine)
  96. };