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.

166 lines
5.4KB

  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. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceDemoHeader.h"
  20. //==============================================================================
  21. class JavaScriptDemo : public Component,
  22. private CodeDocument::Listener,
  23. private Timer
  24. {
  25. public:
  26. JavaScriptDemo()
  27. {
  28. setOpaque (true);
  29. addAndMakeVisible (editor = new CodeEditorComponent (codeDocument, nullptr));
  30. editor->setFont (Font (Font::getDefaultMonospacedFontName(), 14.0f, Font::plain));
  31. editor->setTabSize (4, true);
  32. outputDisplay.setMultiLine (true);
  33. outputDisplay.setReadOnly (true);
  34. outputDisplay.setCaretVisible (false);
  35. outputDisplay.setFont (Font (Font::getDefaultMonospacedFontName(), 14.0f, Font::plain));
  36. addAndMakeVisible (outputDisplay);
  37. codeDocument.addListener (this);
  38. editor->loadContent (
  39. "/*\n"
  40. " Javascript! In this simple demo, the native\n"
  41. " code provides an object called \'Demo\' which\n"
  42. " has a method \'print\' that writes to the\n"
  43. " console below...\n"
  44. "*/\n"
  45. "\n"
  46. "Demo.print (\"Hello World in JUCE + Javascript!\");\n"
  47. "Demo.print (\"\");\n"
  48. "\n"
  49. "function factorial (n)\n"
  50. "{\n"
  51. " var total = 1;\n"
  52. " while (n > 0)\n"
  53. " total = total * n--;\n"
  54. " return total;\n"
  55. "}\n"
  56. "\n"
  57. "for (var i = 1; i < 10; ++i)\n"
  58. " Demo.print (\"Factorial of \" + i \n"
  59. " + \" = \" + factorial (i));\n");
  60. }
  61. void runScript()
  62. {
  63. outputDisplay.clear();
  64. JavascriptEngine engine;
  65. engine.maximumExecutionTime = RelativeTime::seconds (5);
  66. engine.registerNativeObject ("Demo", new DemoClass (*this));
  67. const double startTime = Time::getMillisecondCounterHiRes();
  68. Result result = engine.execute (codeDocument.getAllContent());
  69. const double elapsedMs = Time::getMillisecondCounterHiRes() - startTime;
  70. if (result.failed())
  71. outputDisplay.setText (result.getErrorMessage());
  72. else
  73. outputDisplay.insertTextAtCaret ("\n(Execution time: " + String (elapsedMs, 2) + " milliseconds)");
  74. }
  75. void consoleOutput (const String& message)
  76. {
  77. outputDisplay.moveCaretToEnd();
  78. outputDisplay.insertTextAtCaret (message + newLine);
  79. }
  80. //==============================================================================
  81. // This class is used by the script, and provides methods that the JS can call.
  82. struct DemoClass : public DynamicObject
  83. {
  84. DemoClass (JavaScriptDemo& demo) : owner (demo)
  85. {
  86. setMethod ("print", print);
  87. }
  88. static Identifier getClassName() { return "Demo"; }
  89. static var print (const var::NativeFunctionArgs& args)
  90. {
  91. if (args.numArguments > 0)
  92. if (DemoClass* thisObject = dynamic_cast<DemoClass*> (args.thisObject.getObject()))
  93. thisObject->owner.consoleOutput (args.arguments[0].toString());
  94. return var::undefined();
  95. }
  96. JavaScriptDemo& owner;
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoClass)
  98. };
  99. void paint (Graphics& g) override
  100. {
  101. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  102. }
  103. private:
  104. CodeDocument codeDocument;
  105. ScopedPointer<CodeEditorComponent> editor;
  106. TextEditor outputDisplay;
  107. void codeDocumentTextInserted (const String&, int) override { startTimer (300); }
  108. void codeDocumentTextDeleted (int, int) override { startTimer (300); }
  109. void timerCallback() override
  110. {
  111. stopTimer();
  112. runScript();
  113. }
  114. void resized() override
  115. {
  116. Rectangle<int> r (getLocalBounds().reduced (8));
  117. editor->setBounds (r.removeFromTop (proportionOfHeight (0.6f)));
  118. outputDisplay.setBounds (r.withTrimmedTop (8));
  119. }
  120. void lookAndFeelChanged() override
  121. {
  122. outputDisplay.applyFontToAllText (outputDisplay.getFont());
  123. }
  124. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavaScriptDemo)
  125. };
  126. // This static object will register this demo type in a global list of demos..
  127. static JuceDemoType<JavaScriptDemo> demo ("40 JavaScript");