migrating to the latest JUCE version
This commit is contained in:
1386
deps/juce/modules/juce_core/javascript/juce_JSON.cpp
vendored
1386
deps/juce/modules/juce_core/javascript/juce_JSON.cpp
vendored
File diff suppressed because it is too large
Load Diff
272
deps/juce/modules/juce_core/javascript/juce_JSON.h
vendored
272
deps/juce/modules/juce_core/javascript/juce_JSON.h
vendored
@ -1,136 +1,136 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. 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.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Contains static methods for converting JSON-formatted text to and from var objects.
|
||||
|
||||
The var class is structurally compatible with JSON-formatted data, so these
|
||||
functions allow you to parse JSON into a var object, and to convert a var
|
||||
object to JSON-formatted text.
|
||||
|
||||
@see var
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API JSON
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Parses a string of JSON-formatted text, and returns a result code containing
|
||||
any parse errors.
|
||||
|
||||
This will return the parsed structure in the parsedResult parameter, and will
|
||||
return a Result object to indicate whether parsing was successful, and if not,
|
||||
it will contain an error message.
|
||||
|
||||
If you're not interested in the error message, you can use one of the other
|
||||
shortcut parse methods, which simply return a var() if the parsing fails.
|
||||
|
||||
Note that this will only parse valid JSON, which means that the item given must
|
||||
be either an object or an array definition. If you want to also be able to parse
|
||||
any kind of primitive JSON object, use the fromString() method.
|
||||
*/
|
||||
static Result parse (const String& text, var& parsedResult);
|
||||
|
||||
/** Attempts to parse some JSON-formatted text, and returns the result as a var object.
|
||||
|
||||
If the parsing fails, this simply returns var() - if you need to find out more
|
||||
detail about the parse error, use the alternative parse() method which returns a Result.
|
||||
|
||||
Note that this will only parse valid JSON, which means that the item given must
|
||||
be either an object or an array definition. If you want to also be able to parse
|
||||
any kind of primitive JSON object, use the fromString() method.
|
||||
*/
|
||||
static var parse (const String& text);
|
||||
|
||||
/** Attempts to parse some JSON-formatted text from a file, and returns the result
|
||||
as a var object.
|
||||
|
||||
Note that this is just a short-cut for reading the entire file into a string and
|
||||
parsing the result.
|
||||
|
||||
If the parsing fails, this simply returns var() - if you need to find out more
|
||||
detail about the parse error, use the alternative parse() method which returns a Result.
|
||||
*/
|
||||
static var parse (const File& file);
|
||||
|
||||
/** Attempts to parse some JSON-formatted text from a stream, and returns the result
|
||||
as a var object.
|
||||
|
||||
Note that this is just a short-cut for reading the entire stream into a string and
|
||||
parsing the result.
|
||||
|
||||
If the parsing fails, this simply returns var() - if you need to find out more
|
||||
detail about the parse error, use the alternative parse() method which returns a Result.
|
||||
*/
|
||||
static var parse (InputStream& input);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns a string which contains a JSON-formatted representation of the var object.
|
||||
If allOnOneLine is true, the result will be compacted into a single line of text
|
||||
with no carriage-returns. If false, it will be laid-out in a more human-readable format.
|
||||
The maximumDecimalPlaces parameter determines the precision of floating point numbers
|
||||
in scientific notation.
|
||||
@see writeToStream
|
||||
*/
|
||||
static String toString (const var& objectToFormat,
|
||||
bool allOnOneLine = false,
|
||||
int maximumDecimalPlaces = 15);
|
||||
|
||||
/** Parses a string that was created with the toString() method.
|
||||
This is slightly different to the parse() methods because they will reject primitive
|
||||
values and only accept array or object definitions, whereas this method will handle
|
||||
either.
|
||||
*/
|
||||
static var fromString (StringRef);
|
||||
|
||||
/** Writes a JSON-formatted representation of the var object to the given stream.
|
||||
If allOnOneLine is true, the result will be compacted into a single line of text
|
||||
with no carriage-returns. If false, it will be laid-out in a more human-readable format.
|
||||
The maximumDecimalPlaces parameter determines the precision of floating point numbers
|
||||
in scientific notation.
|
||||
@see toString
|
||||
*/
|
||||
static void writeToStream (OutputStream& output,
|
||||
const var& objectToFormat,
|
||||
bool allOnOneLine = false,
|
||||
int maximumDecimalPlaces = 15);
|
||||
|
||||
/** Returns a version of a string with any extended characters escaped. */
|
||||
static String escapeString (StringRef);
|
||||
|
||||
/** Parses a quoted string-literal in JSON format, returning the un-escaped result in the
|
||||
result parameter, and an error message in case the content was illegal.
|
||||
This advances the text parameter, leaving it positioned after the closing quote.
|
||||
*/
|
||||
static Result parseQuotedString (String::CharPointerType& text, var& result);
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
JSON() = delete; // This class can't be instantiated - just use its static methods.
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. 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.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Contains static methods for converting JSON-formatted text to and from var objects.
|
||||
|
||||
The var class is structurally compatible with JSON-formatted data, so these
|
||||
functions allow you to parse JSON into a var object, and to convert a var
|
||||
object to JSON-formatted text.
|
||||
|
||||
@see var
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API JSON
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Parses a string of JSON-formatted text, and returns a result code containing
|
||||
any parse errors.
|
||||
|
||||
This will return the parsed structure in the parsedResult parameter, and will
|
||||
return a Result object to indicate whether parsing was successful, and if not,
|
||||
it will contain an error message.
|
||||
|
||||
If you're not interested in the error message, you can use one of the other
|
||||
shortcut parse methods, which simply return a var() if the parsing fails.
|
||||
|
||||
Note that this will only parse valid JSON, which means that the item given must
|
||||
be either an object or an array definition. If you want to also be able to parse
|
||||
any kind of primitive JSON object, use the fromString() method.
|
||||
*/
|
||||
static Result parse (const String& text, var& parsedResult);
|
||||
|
||||
/** Attempts to parse some JSON-formatted text, and returns the result as a var object.
|
||||
|
||||
If the parsing fails, this simply returns var() - if you need to find out more
|
||||
detail about the parse error, use the alternative parse() method which returns a Result.
|
||||
|
||||
Note that this will only parse valid JSON, which means that the item given must
|
||||
be either an object or an array definition. If you want to also be able to parse
|
||||
any kind of primitive JSON object, use the fromString() method.
|
||||
*/
|
||||
static var parse (const String& text);
|
||||
|
||||
/** Attempts to parse some JSON-formatted text from a file, and returns the result
|
||||
as a var object.
|
||||
|
||||
Note that this is just a short-cut for reading the entire file into a string and
|
||||
parsing the result.
|
||||
|
||||
If the parsing fails, this simply returns var() - if you need to find out more
|
||||
detail about the parse error, use the alternative parse() method which returns a Result.
|
||||
*/
|
||||
static var parse (const File& file);
|
||||
|
||||
/** Attempts to parse some JSON-formatted text from a stream, and returns the result
|
||||
as a var object.
|
||||
|
||||
Note that this is just a short-cut for reading the entire stream into a string and
|
||||
parsing the result.
|
||||
|
||||
If the parsing fails, this simply returns var() - if you need to find out more
|
||||
detail about the parse error, use the alternative parse() method which returns a Result.
|
||||
*/
|
||||
static var parse (InputStream& input);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns a string which contains a JSON-formatted representation of the var object.
|
||||
If allOnOneLine is true, the result will be compacted into a single line of text
|
||||
with no carriage-returns. If false, it will be laid-out in a more human-readable format.
|
||||
The maximumDecimalPlaces parameter determines the precision of floating point numbers
|
||||
in scientific notation.
|
||||
@see writeToStream
|
||||
*/
|
||||
static String toString (const var& objectToFormat,
|
||||
bool allOnOneLine = false,
|
||||
int maximumDecimalPlaces = 15);
|
||||
|
||||
/** Parses a string that was created with the toString() method.
|
||||
This is slightly different to the parse() methods because they will reject primitive
|
||||
values and only accept array or object definitions, whereas this method will handle
|
||||
either.
|
||||
*/
|
||||
static var fromString (StringRef);
|
||||
|
||||
/** Writes a JSON-formatted representation of the var object to the given stream.
|
||||
If allOnOneLine is true, the result will be compacted into a single line of text
|
||||
with no carriage-returns. If false, it will be laid-out in a more human-readable format.
|
||||
The maximumDecimalPlaces parameter determines the precision of floating point numbers
|
||||
in scientific notation.
|
||||
@see toString
|
||||
*/
|
||||
static void writeToStream (OutputStream& output,
|
||||
const var& objectToFormat,
|
||||
bool allOnOneLine = false,
|
||||
int maximumDecimalPlaces = 15);
|
||||
|
||||
/** Returns a version of a string with any extended characters escaped. */
|
||||
static String escapeString (StringRef);
|
||||
|
||||
/** Parses a quoted string-literal in JSON format, returning the un-escaped result in the
|
||||
result parameter, and an error message in case the content was illegal.
|
||||
This advances the text parameter, leaving it positioned after the closing quote.
|
||||
*/
|
||||
static Result parseQuotedString (String::CharPointerType& text, var& result);
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
JSON() = delete; // This class can't be instantiated - just use its static methods.
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,122 +1,122 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. 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.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
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.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API JavascriptEngine final
|
||||
{
|
||||
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);
|
||||
|
||||
/** 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);
|
||||
|
||||
/** Calls a function in the root namespace, and returns the result.
|
||||
The function arguments are passed in the same format as used by native
|
||||
methods in the var class.
|
||||
*/
|
||||
var callFunction (const Identifier& function,
|
||||
const var::NativeFunctionArgs& args,
|
||||
Result* errorMessage = nullptr);
|
||||
|
||||
/** Calls a function object in the namespace of a dynamic object, and returns the result.
|
||||
The function arguments are passed in the same format as used by native
|
||||
methods in the var class.
|
||||
*/
|
||||
var callFunctionObject (DynamicObject* objectScope,
|
||||
const var& functionObject,
|
||||
const var::NativeFunctionArgs& args,
|
||||
Result* errorMessage = nullptr);
|
||||
|
||||
/** 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 (const Identifier& objectName, DynamicObject* object);
|
||||
|
||||
/** This value indicates how long a call to one of the evaluate methods is permitted
|
||||
to run before timing-out and failing.
|
||||
The default value is a number of seconds, but you can change this to whatever value
|
||||
suits your application.
|
||||
*/
|
||||
RelativeTime maximumExecutionTime;
|
||||
|
||||
/** When called from another thread, causes the interpreter to time-out as soon as possible */
|
||||
void stop() noexcept;
|
||||
|
||||
/** Provides access to the set of properties of the root namespace object. */
|
||||
const NamedValueSet& getRootObjectProperties() const noexcept;
|
||||
|
||||
private:
|
||||
JUCE_PUBLIC_IN_DLL_BUILD (struct RootObject)
|
||||
const ReferenceCountedObjectPtr<RootObject> root;
|
||||
void prepareTimeout() const noexcept;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavascriptEngine)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. 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.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
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.
|
||||
|
||||
@tags{Core}
|
||||
*/
|
||||
class JUCE_API JavascriptEngine final
|
||||
{
|
||||
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);
|
||||
|
||||
/** 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);
|
||||
|
||||
/** Calls a function in the root namespace, and returns the result.
|
||||
The function arguments are passed in the same format as used by native
|
||||
methods in the var class.
|
||||
*/
|
||||
var callFunction (const Identifier& function,
|
||||
const var::NativeFunctionArgs& args,
|
||||
Result* errorMessage = nullptr);
|
||||
|
||||
/** Calls a function object in the namespace of a dynamic object, and returns the result.
|
||||
The function arguments are passed in the same format as used by native
|
||||
methods in the var class.
|
||||
*/
|
||||
var callFunctionObject (DynamicObject* objectScope,
|
||||
const var& functionObject,
|
||||
const var::NativeFunctionArgs& args,
|
||||
Result* errorMessage = nullptr);
|
||||
|
||||
/** 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 (const Identifier& objectName, DynamicObject* object);
|
||||
|
||||
/** This value indicates how long a call to one of the evaluate methods is permitted
|
||||
to run before timing-out and failing.
|
||||
The default value is a number of seconds, but you can change this to whatever value
|
||||
suits your application.
|
||||
*/
|
||||
RelativeTime maximumExecutionTime;
|
||||
|
||||
/** When called from another thread, causes the interpreter to time-out as soon as possible */
|
||||
void stop() noexcept;
|
||||
|
||||
/** Provides access to the set of properties of the root namespace object. */
|
||||
const NamedValueSet& getRootObjectProperties() const noexcept;
|
||||
|
||||
private:
|
||||
JUCE_PUBLIC_IN_DLL_BUILD (struct RootObject)
|
||||
const ReferenceCountedObjectPtr<RootObject> root;
|
||||
void prepareTimeout() const noexcept;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JavascriptEngine)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
Reference in New Issue
Block a user