Browse Source

New class: JavascriptEngine!

tags/2021-05-28
jules 11 years ago
parent
commit
6c5c461d73
7 changed files with 1745 additions and 3 deletions
  1. +0
    -0
      modules/juce_core/javascript/juce_JSON.cpp
  2. +0
    -0
      modules/juce_core/javascript/juce_JSON.h
  3. +1648
    -0
      modules/juce_core/javascript/juce_Javascript.cpp
  4. +92
    -0
      modules/juce_core/javascript/juce_Javascript.h
  5. +2
    -1
      modules/juce_core/juce_core.cpp
  6. +2
    -1
      modules/juce_core/juce_core.h
  7. +1
    -1
      modules/juce_core/juce_module_info

modules/juce_core/json/juce_JSON.cpp → modules/juce_core/javascript/juce_JSON.cpp View File


modules/juce_core/json/juce_JSON.h → modules/juce_core/javascript/juce_JSON.h View File


+ 1648
- 0
modules/juce_core/javascript/juce_Javascript.cpp
File diff suppressed because it is too large
View File


+ 92
- 0
modules/juce_core/javascript/juce_Javascript.h View File

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

+ 2
- 1
modules/juce_core/juce_core.cpp View File

@@ -119,7 +119,8 @@ namespace juce
#include "files/juce_FileOutputStream.cpp"
#include "files/juce_FileSearchPath.cpp"
#include "files/juce_TemporaryFile.cpp"
#include "json/juce_JSON.cpp"
#include "javascript/juce_JSON.cpp"
#include "javascript/juce_Javascript.cpp"
#include "containers/juce_DynamicObject.cpp"
#include "logging/juce_FileLogger.cpp"
#include "logging/juce_Logger.cpp"


+ 2
- 1
modules/juce_core/juce_core.h View File

@@ -234,7 +234,8 @@ extern JUCE_API void JUCE_CALLTYPE logAssertion (const char* file, int line) noe
#include "files/juce_TemporaryFile.h"
#include "streams/juce_FileInputSource.h"
#include "logging/juce_FileLogger.h"
#include "json/juce_JSON.h"
#include "javascript/juce_JSON.h"
#include "javascript/juce_Javascript.h"
#include "maths/juce_BigInteger.h"
#include "maths/juce_Expression.h"
#include "maths/juce_Random.h"


+ 1
- 1
modules/juce_core/juce_module_info View File

@@ -25,7 +25,7 @@
"logging/*",
"system/*",
"xml/*",
"json/*",
"javascript/*",
"zip/*",
"unit_tests/*",
"misc/*",


Loading…
Cancel
Save