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.

159 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. class JavaScriptDemo : public Component,
  20. private CodeDocument::Listener,
  21. private Timer
  22. {
  23. public:
  24. JavaScriptDemo()
  25. {
  26. setOpaque (true);
  27. addAndMakeVisible (editor = new CodeEditorComponent (codeDocument, nullptr));
  28. editor->setFont (Font (Font::getDefaultMonospacedFontName(), 14.0f, Font::plain));
  29. editor->setTabSize (4, true);
  30. outputDisplay.setMultiLine (true);
  31. outputDisplay.setReadOnly (true);
  32. outputDisplay.setCaretVisible (false);
  33. outputDisplay.setFont (Font (Font::getDefaultMonospacedFontName(), 14.0f, Font::plain));
  34. addAndMakeVisible (outputDisplay);
  35. codeDocument.addListener (this);
  36. editor->loadContent (
  37. "/*\n"
  38. " Javascript! In this simple demo, the native\n"
  39. " code provides an object called \'Demo\' which\n"
  40. " has a method \'print\' that writes to the\n"
  41. " console below...\n"
  42. "*/\n"
  43. "\n"
  44. "Demo.print (\"Hello World in JUCE + Javascript!\");\n"
  45. "Demo.print (\"\");\n"
  46. "\n"
  47. "function factorial (n)\n"
  48. "{\n"
  49. " var total = 1;\n"
  50. " while (n > 0)\n"
  51. " total = total * n--;\n"
  52. " return total;\n"
  53. "}\n"
  54. "\n"
  55. "for (var i = 1; i < 10; ++i)\n"
  56. " Demo.print (\"Factorial of \" + i \n"
  57. " + \" = \" + factorial (i));\n");
  58. }
  59. void runScript()
  60. {
  61. outputDisplay.clear();
  62. JavascriptEngine engine;
  63. engine.maximumExecutionTime = RelativeTime::seconds (5);
  64. engine.registerNativeObject ("Demo", new DemoClass (*this));
  65. const double startTime = Time::getMillisecondCounterHiRes();
  66. Result result = engine.execute (codeDocument.getAllContent());
  67. const double elapsedMs = Time::getMillisecondCounterHiRes() - startTime;
  68. if (result.failed())
  69. outputDisplay.setText (result.getErrorMessage());
  70. else
  71. outputDisplay.insertTextAtCaret ("\n(Execution time: " + String (elapsedMs, 2) + " milliseconds)");
  72. }
  73. void consoleOutput (const String& message)
  74. {
  75. outputDisplay.moveCaretToEnd();
  76. outputDisplay.insertTextAtCaret (message + newLine);
  77. }
  78. //==============================================================================
  79. // This class is used by the script, and provides methods that the JS can call.
  80. struct DemoClass : public DynamicObject
  81. {
  82. DemoClass (JavaScriptDemo& demo) : owner (demo)
  83. {
  84. setMethod ("print", print);
  85. }
  86. static Identifier getClassName() { return "Demo"; }
  87. static var print (const var::NativeFunctionArgs& args)
  88. {
  89. if (args.numArguments > 0)
  90. if (DemoClass* thisObject = dynamic_cast<DemoClass*> (args.thisObject.getObject()))
  91. thisObject->owner.consoleOutput (args.arguments[0].toString());
  92. return var::undefined();
  93. }
  94. JavaScriptDemo& owner;
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoClass)
  96. };
  97. void paint (Graphics& g) override
  98. {
  99. fillStandardDemoBackground (g);
  100. }
  101. private:
  102. CodeDocument codeDocument;
  103. ScopedPointer<CodeEditorComponent> editor;
  104. TextEditor outputDisplay;
  105. void codeDocumentTextInserted (const String&, int) override { startTimer (300); }
  106. void codeDocumentTextDeleted (int, int) override { startTimer (300); }
  107. void timerCallback() override
  108. {
  109. stopTimer();
  110. runScript();
  111. }
  112. void resized() override
  113. {
  114. Rectangle<int> r (getLocalBounds().reduced (8));
  115. editor->setBounds (r.removeFromTop (proportionOfHeight (0.6f)));
  116. outputDisplay.setBounds (r.withTrimmedTop (8));
  117. }
  118. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavaScriptDemo)
  119. };
  120. // This static object will register this demo type in a global list of demos..
  121. static JuceDemoType<JavaScriptDemo> demo ("40 JavaScript");