2021-03-01 21:52:24 +01:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2010-01-09 20:20:14 +01:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SCRIPTINTERFACE
|
|
|
|
#define INCLUDED_SCRIPTINTERFACE
|
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2016-01-23 16:17:56 +01:00
|
|
|
#include "maths/Fixed.h"
|
2014-01-04 11:14:53 +01:00
|
|
|
#include "ps/Errors.h"
|
2021-05-13 11:43:33 +02:00
|
|
|
#include "scriptinterface/ScriptConversions.h"
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
#include "scriptinterface/ScriptExceptions.h"
|
2021-05-03 18:07:26 +02:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
#include "scriptinterface/ScriptTypes.h"
|
2014-11-13 12:19:28 +01:00
|
|
|
|
2018-12-28 15:58:35 +01:00
|
|
|
#include <map>
|
|
|
|
|
2014-01-04 11:14:53 +01:00
|
|
|
ERROR_GROUP(Scripting);
|
|
|
|
ERROR_TYPE(Scripting, SetupFailed);
|
|
|
|
|
|
|
|
ERROR_SUBGROUP(Scripting, LoadFile);
|
|
|
|
ERROR_TYPE(Scripting_LoadFile, OpenFailed);
|
|
|
|
ERROR_TYPE(Scripting_LoadFile, EvalErrors);
|
|
|
|
|
|
|
|
ERROR_TYPE(Scripting, CallFunctionFailed);
|
|
|
|
ERROR_TYPE(Scripting, DefineConstantFailed);
|
|
|
|
ERROR_TYPE(Scripting, CreateObjectFailed);
|
|
|
|
ERROR_TYPE(Scripting, TypeDoesNotExist);
|
|
|
|
|
|
|
|
ERROR_SUBGROUP(Scripting, DefineType);
|
|
|
|
ERROR_TYPE(Scripting_DefineType, AlreadyExists);
|
|
|
|
ERROR_TYPE(Scripting_DefineType, CreationFailed);
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// Set the maximum number of function arguments that can be handled
|
|
|
|
// (This should be as small as possible (for compiler efficiency),
|
|
|
|
// but as large as necessary for all wrapped functions)
|
2013-08-03 21:20:20 +02:00
|
|
|
#define SCRIPT_INTERFACE_MAX_ARGS 8
|
2010-01-09 20:20:14 +01:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
class ScriptInterface;
|
2010-01-09 20:20:14 +01:00
|
|
|
struct ScriptInterface_impl;
|
2010-10-31 23:00:28 +01:00
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
class ScriptContext;
|
|
|
|
// Using a global object for the context is a workaround until Simulation, AI, etc,
|
|
|
|
// use their own threads and also their own contexts.
|
|
|
|
extern thread_local shared_ptr<ScriptContext> g_ScriptContext;
|
2014-01-04 11:14:53 +01:00
|
|
|
|
2020-12-09 15:39:14 +01:00
|
|
|
namespace boost { namespace random { class rand48; } }
|
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
/**
|
2020-11-30 10:03:20 +01:00
|
|
|
* Abstraction around a SpiderMonkey JS::Realm.
|
2010-10-31 23:00:28 +01:00
|
|
|
*
|
|
|
|
* Thread-safety:
|
|
|
|
* - May be used in non-main threads.
|
|
|
|
* - Each ScriptInterface must be created, used, and destroyed, all in a single thread
|
|
|
|
* (it must never be shared between threads).
|
|
|
|
*/
|
2010-01-09 20:20:14 +01:00
|
|
|
class ScriptInterface
|
|
|
|
{
|
2014-08-09 23:16:25 +02:00
|
|
|
NONCOPYABLE(ScriptInterface);
|
2016-11-23 12:18:37 +01:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
friend class ScriptRequest;
|
2010-01-09 20:20:14 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2021-03-02 21:01:14 +01:00
|
|
|
* @param nativeScopeName Name of global object that functions (via ScriptFunction::Register) will
|
2010-01-09 20:20:14 +01:00
|
|
|
* be placed into, as a scoping mechanism; typically "Engine"
|
2012-03-01 04:55:05 +01:00
|
|
|
* @param debugName Name of this interface for CScriptStats purposes.
|
2020-11-14 11:57:50 +01:00
|
|
|
* @param context ScriptContext to use when initializing this interface.
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
2020-11-14 11:57:50 +01:00
|
|
|
ScriptInterface(const char* nativeScopeName, const char* debugName, const shared_ptr<ScriptContext>& context);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
~ScriptInterface();
|
|
|
|
|
2020-11-13 17:44:15 +01:00
|
|
|
struct CmptPrivate
|
2014-01-04 11:14:53 +01:00
|
|
|
{
|
2020-11-14 09:46:32 +01:00
|
|
|
ScriptInterface* pScriptInterface; // the ScriptInterface object the compartment belongs to
|
2014-01-04 11:14:53 +01:00
|
|
|
void* pCBData; // meant to be used as the "this" object for callback functions
|
2020-11-13 17:44:15 +01:00
|
|
|
} m_CmptPrivate;
|
2014-01-04 11:14:53 +01:00
|
|
|
|
|
|
|
void SetCallbackData(void* pCBData);
|
2020-11-13 17:44:15 +01:00
|
|
|
static CmptPrivate* GetScriptInterfaceAndCBData(JSContext* cx);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2020-11-18 15:39:04 +01:00
|
|
|
/**
|
|
|
|
* GetGeneralJSContext returns the context without starting a GC request and without
|
|
|
|
* entering the ScriptInterface compartment. It should only be used in specific situations,
|
|
|
|
* for instance when initializing a persistent rooted.
|
|
|
|
* If you need the compartmented context of the ScriptInterface, you should create a
|
|
|
|
* ScriptInterface::Request and use the context from that.
|
|
|
|
*/
|
|
|
|
JSContext* GetGeneralJSContext() const;
|
2020-11-14 11:57:50 +01:00
|
|
|
shared_ptr<ScriptContext> GetContext() const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2012-07-03 04:16:45 +02:00
|
|
|
/**
|
2020-11-14 09:46:32 +01:00
|
|
|
* Load global scripts that most script interfaces need,
|
2012-07-03 04:16:45 +02:00
|
|
|
* located in the /globalscripts directory. VFS must be initialized.
|
|
|
|
*/
|
|
|
|
bool LoadGlobalScripts();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the default JS random number geenrator with a seeded, network-sync'd one.
|
|
|
|
*/
|
2020-12-09 15:39:14 +01:00
|
|
|
bool ReplaceNondeterministicRNG(boost::random::rand48& rng);
|
2010-05-22 02:38:33 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
2011-01-12 13:29:00 +01:00
|
|
|
* Call a constructor function, equivalent to JS "new ctor(arg)".
|
2014-08-02 18:30:15 +02:00
|
|
|
* @param ctor An object that can be used as constructor
|
|
|
|
* @param argv Constructor arguments
|
|
|
|
* @param out The new object; On error an error message gets logged and out is Null (out.isNull() == true).
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const;
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2015-12-22 15:08:32 +01:00
|
|
|
JSObject* CreateCustomObject(const std::string & typeName) const;
|
2014-01-04 11:14:53 +01:00
|
|
|
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
2010-01-24 18:24:35 +01:00
|
|
|
* Set the named property on the global object.
|
2019-07-19 23:58:58 +02:00
|
|
|
* Optionally makes it {ReadOnly, DontEnum}. We do not allow to make it DontDelete, so that it can be hotloaded
|
|
|
|
* by deleting it and re-creating it, which is done by setting @p replace to true.
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2019-01-13 17:37:41 +01:00
|
|
|
bool SetGlobal(const char* name, const T& value, bool replace = false, bool constant = true, bool enumerate = true);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2020-12-17 18:51:18 +01:00
|
|
|
/**
|
|
|
|
* Get an object from the global scope or any lexical scope.
|
|
|
|
* This can return globally accessible objects even if they are not properties
|
|
|
|
* of the global object (e.g. ES6 class definitions).
|
|
|
|
* @param name - Name of the property.
|
|
|
|
* @param out The object or null.
|
|
|
|
*/
|
|
|
|
static bool GetGlobalProperty(const ScriptRequest& rq, const std::string& name, JS::MutableHandleValue out);
|
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2015-02-14 02:49:34 +01:00
|
|
|
/**
|
|
|
|
* Convert an object to a UTF-8 encoded string, either with JSON
|
|
|
|
* (if pretty == true and there is no JSON error) or with toSource().
|
|
|
|
*
|
|
|
|
* We have to use a mutable handle because JS_Stringify requires that for unknown reasons.
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
std::string ToString(JS::MutableHandleValue obj, bool pretty = false) const;
|
2010-06-30 23:23:41 +02:00
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
/**
|
2014-11-13 02:26:22 +01:00
|
|
|
* Parse a UTF-8-encoded JSON string. Returns the unmodified value on error
|
|
|
|
* and prints an error message.
|
|
|
|
* @return true on success; false otherwise
|
2010-10-31 23:00:28 +01:00
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
bool ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const;
|
2010-10-31 23:00:28 +01:00
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
/**
|
2014-08-03 00:21:50 +02:00
|
|
|
* Read a JSON file. Returns the unmodified value on error and prints an error message.
|
2011-01-12 13:29:00 +01:00
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
void ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const;
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2010-08-04 23:15:41 +02:00
|
|
|
/**
|
|
|
|
* Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
std::string StringifyJSON(JS::MutableHandleValue obj, bool indent = true) const;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
|
|
|
* Load and execute the given script in a new function scope.
|
|
|
|
* @param filename Name for debugging purposes (not used to load the file)
|
|
|
|
* @param code JS code to execute
|
|
|
|
* @return true on successful compilation and execution; false otherwise
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
bool LoadScript(const VfsPath& filename, const std::string& code) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
/**
|
|
|
|
* Load and execute the given script in the global scope.
|
|
|
|
* @param filename Name for debugging purposes (not used to load the file)
|
|
|
|
* @param code JS code to execute
|
|
|
|
* @return true on successful compilation and execution; false otherwise
|
|
|
|
*/
|
2020-11-30 10:03:20 +01:00
|
|
|
bool LoadGlobalScript(const VfsPath& filename, const std::string& code) const;
|
2011-04-15 07:23:51 +02:00
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
/**
|
|
|
|
* Load and execute the given script in the global scope.
|
|
|
|
* @return true on successful compilation and execution; false otherwise
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
bool LoadGlobalScriptFile(const VfsPath& path) const;
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2020-11-30 10:03:20 +01:00
|
|
|
/**
|
|
|
|
* Evaluate some JS code in the global scope.
|
|
|
|
* @return true on successful compilation and execution; false otherwise
|
|
|
|
*/
|
|
|
|
bool Eval(const char* code) const;
|
|
|
|
bool Eval(const char* code, JS::MutableHandleValue out) const;
|
|
|
|
template<typename T> bool Eval(const char* code, T& out) const;
|
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
/**
|
|
|
|
* MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and
|
|
|
|
* returns the generated number.
|
2016-11-23 12:18:37 +01:00
|
|
|
* Math_random (with underscore, not this function) is a global function, but different random number generators can be
|
2014-03-28 21:26:32 +01:00
|
|
|
* stored per ScriptInterface. It calls MathRandom of the current ScriptInterface instance.
|
|
|
|
*/
|
|
|
|
bool MathRandom(double& nbr);
|
2010-11-15 16:03:40 +01:00
|
|
|
|
2019-08-13 16:11:43 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the private data field of a JSObject that is an instance of the given JSClass.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
static T* GetPrivate(const ScriptRequest& rq, JS::HandleObject thisobj, JSClass* jsClass)
|
2019-08-13 16:11:43 +02:00
|
|
|
{
|
2020-11-13 14:18:22 +01:00
|
|
|
T* value = static_cast<T*>(JS_GetInstancePrivate(rq.cx, thisobj, jsClass, nullptr));
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
|
|
|
|
if (value == nullptr)
|
|
|
|
ScriptException::Raise(rq, "Private data of the given object is null!");
|
|
|
|
|
2019-08-13 16:11:43 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the private data field of a JS Object that is an instance of the given JSClass.
|
|
|
|
* If an error occurs, GetPrivate will report it with the according stack.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
static T* GetPrivate(const ScriptRequest& rq, JS::CallArgs& callArgs, JSClass* jsClass)
|
2019-08-13 16:11:43 +02:00
|
|
|
{
|
|
|
|
if (!callArgs.thisv().isObject())
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptException::Raise(rq, "Cannot retrieve private JS class data because from a non-object value!");
|
2019-08-13 16:11:43 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedObject thisObj(rq.cx, &callArgs.thisv().toObject());
|
|
|
|
T* value = static_cast<T*>(JS_GetInstancePrivate(rq.cx, thisObj, jsClass, &callArgs));
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
|
|
|
|
if (value == nullptr)
|
|
|
|
ScriptException::Raise(rq, "Private data of the given object is null!");
|
|
|
|
|
2019-08-13 16:11:43 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-07-26 22:31:29 +02:00
|
|
|
private:
|
2019-01-13 17:37:41 +01:00
|
|
|
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
struct CustomType
|
2014-01-04 11:14:53 +01:00
|
|
|
{
|
2016-09-02 18:53:22 +02:00
|
|
|
JS::PersistentRootedObject m_Prototype;
|
2016-08-02 18:12:11 +02:00
|
|
|
JSClass* m_Class;
|
|
|
|
JSNative m_Constructor;
|
2014-01-04 11:14:53 +01:00
|
|
|
};
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
// Take care to keep this declaration before heap rooted members. Destructors of heap rooted
|
2020-11-12 10:34:40 +01:00
|
|
|
// members have to be called before the custom destructor of ScriptInterface_impl.
|
2015-05-25 03:23:27 +02:00
|
|
|
std::unique_ptr<ScriptInterface_impl> m;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2020-12-09 15:39:14 +01:00
|
|
|
boost::random::rand48* m_rng;
|
2014-01-04 11:14:53 +01:00
|
|
|
std::map<std::string, CustomType> m_CustomObjectTypes;
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2019-01-13 17:37:41 +01:00
|
|
|
bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace, bool constant, bool enumerate)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue val(rq.cx);
|
2021-05-13 11:43:33 +02:00
|
|
|
Script::ToJSVal(rq, &val, value);
|
2019-01-13 17:37:41 +01:00
|
|
|
return SetGlobal_(name, val, replace, constant, enumerate);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 10:03:20 +01:00
|
|
|
template<typename T>
|
|
|
|
bool ScriptInterface::Eval(const char* code, T& ret) const
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue rval(rq.cx);
|
2020-11-30 10:03:20 +01:00
|
|
|
if (!Eval(code, &rval))
|
2010-01-09 20:20:14 +01:00
|
|
|
return false;
|
2021-05-13 11:43:33 +02:00
|
|
|
return Script::FromJSVal(rq, rval, ret);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // INCLUDED_SCRIPTINTERFACE
|