2021-03-01 12:52:24 -08:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2010-01-09 11:20:14 -08: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 03:19:28 -08:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2016-01-23 07:17:56 -08:00
|
|
|
#include "maths/Fixed.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "ps/Errors.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 10:29:17 -08:00
|
|
|
#include "scriptinterface/ScriptExceptions.h"
|
|
|
|
|
#include "scriptinterface/ScriptTypes.h"
|
2014-11-13 03:19:28 -08:00
|
|
|
|
2018-12-28 06:58:35 -08:00
|
|
|
#include <map>
|
|
|
|
|
|
2014-01-04 02:14:53 -08: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 11:20:14 -08: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 12:20:20 -07:00
|
|
|
#define SCRIPT_INTERFACE_MAX_ARGS 8
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2020-11-30 01:03:20 -08:00
|
|
|
class JSStructuredCloneData;
|
|
|
|
|
|
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 10:29:17 -08:00
|
|
|
class ScriptInterface;
|
2010-01-09 11:20:14 -08:00
|
|
|
struct ScriptInterface_impl;
|
2010-10-31 15:00:28 -07:00
|
|
|
|
2020-11-14 02:57:50 -08: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 02:14:53 -08:00
|
|
|
|
2020-12-09 06:39:14 -08:00
|
|
|
namespace boost { namespace random { class rand48; } }
|
|
|
|
|
|
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 10:29:17 -08:00
|
|
|
/**
|
|
|
|
|
* RAII structure which encapsulates an access to the context and compartment of a ScriptInterface.
|
|
|
|
|
* This struct provides:
|
|
|
|
|
* - a pointer to the context, while acting like JSAutoRequest
|
2020-11-30 01:03:20 -08:00
|
|
|
* - a pointer to the global object of the compartment, while acting like JSAutoRealm
|
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 10:29:17 -08:00
|
|
|
*
|
|
|
|
|
* This way, getting and using those pointers is safe with respect to the GC
|
|
|
|
|
* and to the separation of compartments.
|
|
|
|
|
*/
|
|
|
|
|
class ScriptRequest
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ScriptRequest() = delete;
|
|
|
|
|
ScriptRequest(const ScriptRequest& rq) = delete;
|
|
|
|
|
ScriptRequest& operator=(const ScriptRequest& rq) = delete;
|
|
|
|
|
ScriptRequest(const ScriptInterface& scriptInterface);
|
|
|
|
|
ScriptRequest(const ScriptInterface* scriptInterface) : ScriptRequest(*scriptInterface) {}
|
|
|
|
|
ScriptRequest(shared_ptr<ScriptInterface> scriptInterface) : ScriptRequest(*scriptInterface) {}
|
|
|
|
|
~ScriptRequest();
|
|
|
|
|
|
|
|
|
|
JS::Value globalValue() const;
|
|
|
|
|
JSContext* cx;
|
|
|
|
|
JSObject* glob;
|
2021-03-01 12:52:24 -08:00
|
|
|
JS::HandleObject nativeScope;
|
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 10:29:17 -08:00
|
|
|
private:
|
2020-11-30 01:03:20 -08:00
|
|
|
JS::Realm* m_formerRealm;
|
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 10:29:17 -08:00
|
|
|
};
|
2013-03-07 05:49:49 -08:00
|
|
|
|
2010-10-31 15:00:28 -07:00
|
|
|
/**
|
2020-11-30 01:03:20 -08:00
|
|
|
* Abstraction around a SpiderMonkey JS::Realm.
|
2010-10-31 15:00:28 -07: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 11:20:14 -08:00
|
|
|
class ScriptInterface
|
|
|
|
|
{
|
2014-08-09 14:16:25 -07:00
|
|
|
NONCOPYABLE(ScriptInterface);
|
2016-11-23 03:18:37 -08: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 10:29:17 -08:00
|
|
|
friend class ScriptRequest;
|
2010-01-09 11:20:14 -08:00
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2021-03-02 12:01:14 -08:00
|
|
|
* @param nativeScopeName Name of global object that functions (via ScriptFunction::Register) will
|
2010-01-09 11:20:14 -08:00
|
|
|
* be placed into, as a scoping mechanism; typically "Engine"
|
2012-02-29 19:55:05 -08:00
|
|
|
* @param debugName Name of this interface for CScriptStats purposes.
|
2020-11-14 02:57:50 -08:00
|
|
|
* @param context ScriptContext to use when initializing this interface.
|
2010-01-09 11:20:14 -08:00
|
|
|
*/
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface(const char* nativeScopeName, const char* debugName, const shared_ptr<ScriptContext>& context);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
~ScriptInterface();
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
struct CmptPrivate
|
2014-01-04 02:14:53 -08:00
|
|
|
{
|
2020-11-14 00:46:32 -08:00
|
|
|
ScriptInterface* pScriptInterface; // the ScriptInterface object the compartment belongs to
|
2014-01-04 02:14:53 -08:00
|
|
|
void* pCBData; // meant to be used as the "this" object for callback functions
|
2020-11-13 08:44:15 -08:00
|
|
|
} m_CmptPrivate;
|
2014-01-04 02:14:53 -08:00
|
|
|
|
|
|
|
|
void SetCallbackData(void* pCBData);
|
2020-11-13 08:44:15 -08:00
|
|
|
static CmptPrivate* GetScriptInterfaceAndCBData(JSContext* cx);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2020-11-18 06:39:04 -08: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 02:57:50 -08:00
|
|
|
shared_ptr<ScriptContext> GetContext() const;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2012-07-02 19:16:45 -07:00
|
|
|
/**
|
2020-11-14 00:46:32 -08:00
|
|
|
* Load global scripts that most script interfaces need,
|
2012-07-02 19:16:45 -07: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 06:39:14 -08:00
|
|
|
bool ReplaceNondeterministicRNG(boost::random::rand48& rng);
|
2010-05-21 17:38:33 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
/**
|
2011-01-12 04:29:00 -08:00
|
|
|
* Call a constructor function, equivalent to JS "new ctor(arg)".
|
2014-08-02 09:30:15 -07: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 11:20:14 -08:00
|
|
|
*/
|
2017-03-24 11:47:03 -07:00
|
|
|
void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const;
|
2011-01-12 04:29:00 -08:00
|
|
|
|
2015-12-22 06:08:32 -08:00
|
|
|
JSObject* CreateCustomObject(const std::string & typeName) const;
|
2014-01-04 02:14:53 -08:00
|
|
|
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
|
|
|
|
|
|
2019-07-22 12:35:14 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties.
|
2019-09-12 17:56:51 -07:00
|
|
|
* This is static so that callers like ToJSVal can use it with the JSContext directly instead of having to obtain the instance using GetScriptInterfaceAndCBData.
|
2019-07-22 12:35:14 -07:00
|
|
|
* Can throw an exception.
|
|
|
|
|
*/
|
2019-08-16 20:30:07 -07:00
|
|
|
template<typename... Args>
|
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 10:29:17 -08:00
|
|
|
static bool CreateObject(const ScriptRequest& rq, JS::MutableHandleValue objectValue, Args const&... args)
|
2019-07-22 12:35:14 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedObject obj(rq.cx);
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
if (!CreateObject_(rq, &obj, args...))
|
2019-08-16 20:30:07 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
objectValue.setObject(*obj);
|
|
|
|
|
return true;
|
2019-07-22 12:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the given value to a new JS object or Null Value in case of out-of-memory.
|
|
|
|
|
*/
|
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 10:29:17 -08:00
|
|
|
static void CreateArray(const ScriptRequest& rq, JS::MutableHandleValue objectValue, size_t length = 0);
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
/**
|
2010-01-24 09:24:35 -08:00
|
|
|
* Set the named property on the global object.
|
2019-07-19 14:58:58 -07: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 11:20:14 -08:00
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2019-01-13 08:37:41 -08:00
|
|
|
bool SetGlobal(const char* name, const T& value, bool replace = false, bool constant = true, bool enumerate = true);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the named property on the given object.
|
|
|
|
|
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2017-08-23 17:32:42 -07:00
|
|
|
bool SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant = false, bool enumerate = true) const;
|
2011-01-12 04:29:00 -08:00
|
|
|
|
2013-11-09 15:26:17 -08:00
|
|
|
/**
|
|
|
|
|
* Set the named property on the given object.
|
|
|
|
|
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2017-08-23 17:32:42 -07:00
|
|
|
bool SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant = false, bool enumerate = true) const;
|
2013-11-09 15:26:17 -08:00
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
/**
|
|
|
|
|
* Set the integer-named property on the given object.
|
|
|
|
|
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2017-08-23 17:32:42 -07:00
|
|
|
bool SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant = false, bool enumerate = true) const;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2011-06-27 16:27:25 -07:00
|
|
|
/**
|
|
|
|
|
* Get the named property on the given object.
|
|
|
|
|
*/
|
2010-01-09 11:20:14 -08:00
|
|
|
template<typename T>
|
2017-03-24 11:47:03 -07:00
|
|
|
bool GetProperty(JS::HandleValue obj, const char* name, T& out) const;
|
|
|
|
|
bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const;
|
|
|
|
|
bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out) const;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2020-12-27 09:18:13 -08:00
|
|
|
template<typename T>
|
|
|
|
|
static bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, T& out);
|
|
|
|
|
static bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, JS::MutableHandleValue out);
|
|
|
|
|
static bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, JS::MutableHandleObject out);
|
|
|
|
|
|
2011-06-27 16:27:25 -07:00
|
|
|
/**
|
|
|
|
|
* Get the integer-named property on the given object.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2017-03-24 11:47:03 -07:00
|
|
|
bool GetPropertyInt(JS::HandleValue obj, int name, T& out) const;
|
|
|
|
|
bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) const;
|
2020-12-27 09:18:13 -08:00
|
|
|
bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleObject out) const;
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
static bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, T& out);
|
|
|
|
|
static bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, JS::MutableHandleValue out);
|
|
|
|
|
static bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, JS::MutableHandleObject out);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
2011-06-27 16:27:25 -07:00
|
|
|
/**
|
|
|
|
|
* Check the named property has been defined on the given object.
|
|
|
|
|
*/
|
2017-03-24 11:47:03 -07:00
|
|
|
bool HasProperty(JS::HandleValue obj, const char* name) const;
|
2010-04-09 11:45:28 -07:00
|
|
|
|
2020-12-17 09:51:18 -08: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);
|
|
|
|
|
|
2020-06-14 02:49:32 -07:00
|
|
|
/**
|
|
|
|
|
* Returns all properties of the object, both own properties and inherited.
|
|
|
|
|
* This is essentially equivalent to calling Object.getOwnPropertyNames()
|
|
|
|
|
* and recursing up the prototype chain.
|
|
|
|
|
* NB: this does not return properties with symbol or numeric keys, as that would
|
|
|
|
|
* require a variant in the vector, and it's not useful for now.
|
|
|
|
|
* @param enumerableOnly - only return enumerable properties.
|
|
|
|
|
*/
|
|
|
|
|
bool EnumeratePropertyNames(JS::HandleValue objVal, bool enumerableOnly, std::vector<std::string>& out) const;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2017-03-24 11:47:03 -07:00
|
|
|
bool FreezeObject(JS::HandleValue objVal, bool deep) const;
|
2011-01-12 04:29:00 -08:00
|
|
|
|
2015-02-13 17:49:34 -08: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 11:47:03 -07:00
|
|
|
std::string ToString(JS::MutableHandleValue obj, bool pretty = false) const;
|
2010-06-30 14:23:41 -07:00
|
|
|
|
2010-10-31 15:00:28 -07:00
|
|
|
/**
|
2014-11-12 17:26:22 -08: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 15:00:28 -07:00
|
|
|
*/
|
2017-03-24 11:47:03 -07:00
|
|
|
bool ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const;
|
2010-10-31 15:00:28 -07:00
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
/**
|
2014-08-02 15:21:50 -07:00
|
|
|
* Read a JSON file. Returns the unmodified value on error and prints an error message.
|
2011-01-12 04:29:00 -08:00
|
|
|
*/
|
2017-03-24 11:47:03 -07:00
|
|
|
void ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const;
|
2011-01-12 04:29:00 -08:00
|
|
|
|
2010-08-04 14:15:41 -07:00
|
|
|
/**
|
|
|
|
|
* Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
|
|
|
|
|
*/
|
2017-03-24 11:47:03 -07:00
|
|
|
std::string StringifyJSON(JS::MutableHandleValue obj, bool indent = true) const;
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2010-01-09 11:20:14 -08: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 11:47:03 -07:00
|
|
|
bool LoadScript(const VfsPath& filename, const std::string& code) const;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2011-04-14 22:23:51 -07: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 01:03:20 -08:00
|
|
|
bool LoadGlobalScript(const VfsPath& filename, const std::string& code) const;
|
2011-04-14 22:23:51 -07:00
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
/**
|
|
|
|
|
* Load and execute the given script in the global scope.
|
|
|
|
|
* @return true on successful compilation and execution; false otherwise
|
|
|
|
|
*/
|
2017-03-24 11:47:03 -07:00
|
|
|
bool LoadGlobalScriptFile(const VfsPath& path) const;
|
2011-01-12 04:29:00 -08:00
|
|
|
|
2020-11-30 01:03:20 -08: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;
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
/**
|
2017-08-28 03:27:36 -07:00
|
|
|
* Convert a JS::Value to a C++ type. (This might trigger GC.)
|
2010-01-09 11:20:14 -08: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 10:29:17 -08:00
|
|
|
template<typename T> static bool FromJSVal(const ScriptRequest& rq, const JS::HandleValue val, T& ret);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
/**
|
2017-08-28 03:27:36 -07:00
|
|
|
* Convert a C++ type to a JS::Value. (This might trigger GC. The return
|
2010-01-09 11:20:14 -08:00
|
|
|
* value must be rooted if you don't want it to be collected.)
|
2014-03-28 13:26:32 -07:00
|
|
|
* NOTE: We are passing the JS::Value by reference instead of returning it by value.
|
|
|
|
|
* The reason is a memory corruption problem that appears to be caused by a bug in Visual Studio.
|
|
|
|
|
* Details here: http://www.wildfiregames.com/forum/index.php?showtopic=17289&p=285921
|
2010-01-09 11:20:14 -08: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 10:29:17 -08:00
|
|
|
template<typename T> static void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, T const& val);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2017-09-12 15:52:15 -07:00
|
|
|
/**
|
|
|
|
|
* Convert a named property of an object to a C++ type.
|
|
|
|
|
*/
|
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 10:29:17 -08:00
|
|
|
template<typename T> static bool FromJSProperty(const ScriptRequest& rq, const JS::HandleValue val, const char* name, T& ret, bool strict = false);
|
2017-09-12 15:52:15 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
/**
|
|
|
|
|
* MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and
|
|
|
|
|
* returns the generated number.
|
2016-11-23 03:18:37 -08:00
|
|
|
* Math_random (with underscore, not this function) is a global function, but different random number generators can be
|
2014-03-28 13:26:32 -07:00
|
|
|
* stored per ScriptInterface. It calls MathRandom of the current ScriptInterface instance.
|
|
|
|
|
*/
|
|
|
|
|
bool MathRandom(double& nbr);
|
2010-11-15 07:03:40 -08:00
|
|
|
|
2011-01-15 15:35:20 -08:00
|
|
|
/**
|
2017-08-28 03:27:36 -07:00
|
|
|
* Structured clones are a way to serialize 'simple' JS::Values into a buffer
|
2020-11-14 00:46:32 -08:00
|
|
|
* that can safely be passed between compartments and between threads.
|
2011-01-15 15:35:20 -08:00
|
|
|
* A StructuredClone can be stored and read multiple times if desired.
|
|
|
|
|
* We wrap them in shared_ptr so memory management is automatic and
|
|
|
|
|
* thread-safe.
|
|
|
|
|
*/
|
2020-11-18 06:39:04 -08:00
|
|
|
using StructuredClone = shared_ptr<JSStructuredCloneData>;
|
|
|
|
|
|
2020-12-06 06:03:02 -08:00
|
|
|
StructuredClone WriteStructuredClone(JS::HandleValue v) const;
|
2020-11-18 06:39:04 -08:00
|
|
|
void ReadStructuredClone(const StructuredClone& ptr, JS::MutableHandleValue ret) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a new value (usable in this ScriptInterface's compartment) by cloning
|
|
|
|
|
* a value from a different compartment.
|
|
|
|
|
* Complex values (functions, XML, etc) won't be cloned correctly, but basic
|
|
|
|
|
* types and cyclic references should be fine.
|
|
|
|
|
*/
|
2020-12-06 06:03:02 -08:00
|
|
|
JS::Value CloneValueFromOtherCompartment(const ScriptInterface& otherCompartment, JS::HandleValue val) const;
|
2011-01-15 15:35:20 -08:00
|
|
|
|
2019-08-13 07:11:43 -07: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 10:29:17 -08:00
|
|
|
static T* GetPrivate(const ScriptRequest& rq, JS::HandleObject thisobj, JSClass* jsClass)
|
2019-08-13 07:11:43 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08: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 10:29:17 -08:00
|
|
|
|
|
|
|
|
if (value == nullptr)
|
|
|
|
|
ScriptException::Raise(rq, "Private data of the given object is null!");
|
|
|
|
|
|
2019-08-13 07:11:43 -07: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 10:29:17 -08:00
|
|
|
static T* GetPrivate(const ScriptRequest& rq, JS::CallArgs& callArgs, JSClass* jsClass)
|
2019-08-13 07:11:43 -07: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 10:29:17 -08:00
|
|
|
ScriptException::Raise(rq, "Cannot retrieve private JS class data because from a non-object value!");
|
2019-08-13 07:11:43 -07: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 10:29:17 -08:00
|
|
|
|
2020-11-13 05:18:22 -08: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 10:29:17 -08:00
|
|
|
|
|
|
|
|
if (value == nullptr)
|
|
|
|
|
ScriptException::Raise(rq, "Private data of the given object is null!");
|
|
|
|
|
|
2019-08-13 07:11:43 -07:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 12:45:18 -07:00
|
|
|
/**
|
|
|
|
|
* Converts |a| if needed and assigns it to |handle|.
|
|
|
|
|
* This is meant for use in other templates where we want to use the same code for JS::RootedValue&/JS::HandleValue and
|
|
|
|
|
* other types. Note that functions are meant to take JS::HandleValue instead of JS::RootedValue&, but this implicit
|
|
|
|
|
* conversion does not work for templates (exact type matches required for type deduction).
|
|
|
|
|
* A similar functionality could also be implemented as a ToJSVal specialization. The current approach was preferred
|
|
|
|
|
* because "conversions" from JS::HandleValue to JS::MutableHandleValue are unusual and should not happen "by accident".
|
|
|
|
|
*/
|
|
|
|
|
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 10:29:17 -08:00
|
|
|
static void AssignOrToJSVal(const ScriptRequest& rq, JS::MutableHandleValue handle, const T& a);
|
2015-01-24 06:46:52 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The same as AssignOrToJSVal, but also allows JS::Value for T.
|
|
|
|
|
* In most cases it's not safe to use the plain (unrooted) JS::Value type, but this can happen quite
|
|
|
|
|
* easily with template functions. The idea is that the linker prints an error if AssignOrToJSVal is
|
2016-11-23 03:18:37 -08:00
|
|
|
* used with JS::Value. If the specialization for JS::Value should be allowed, you can use this
|
2015-01-24 06:46:52 -08:00
|
|
|
* "unrooted" version of AssignOrToJSVal.
|
|
|
|
|
*/
|
|
|
|
|
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 10:29:17 -08:00
|
|
|
static void AssignOrToJSValUnrooted(const ScriptRequest& rq, JS::MutableHandleValue handle, const T& a)
|
2015-01-24 06:46:52 -08:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
AssignOrToJSVal(rq, handle, a);
|
2015-01-24 06:46:52 -08:00
|
|
|
}
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2016-01-23 06:42:59 -08:00
|
|
|
/**
|
|
|
|
|
* Converts |val| to T if needed or just returns it if it's a handle.
|
|
|
|
|
* This is meant for use in other templates where we want to use the same code for JS::HandleValue and
|
|
|
|
|
* other types.
|
|
|
|
|
*/
|
|
|
|
|
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 10:29:17 -08:00
|
|
|
static T AssignOrFromJSVal(const ScriptRequest& rq, const JS::HandleValue& val, bool& ret);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
|
|
|
|
private:
|
2016-11-23 03:18:37 -08: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 10:29:17 -08:00
|
|
|
static bool CreateObject_(const ScriptRequest& rq, JS::MutableHandleObject obj);
|
2019-08-16 20:30:07 -07:00
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
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 10:29:17 -08:00
|
|
|
static bool CreateObject_(const ScriptRequest& rq, JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args)
|
2019-08-16 20:30:07 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
AssignOrToJSVal(rq, &val, propertyValue);
|
2019-08-16 20:30:07 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
return CreateObject_(rq, obj, args...) && JS_DefineProperty(rq.cx, obj, propertyName, val, JSPROP_ENUMERATE);
|
2019-08-16 20:30:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-13 08:37:41 -08:00
|
|
|
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate);
|
2019-07-19 14:58:58 -07:00
|
|
|
bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool constant, bool enumerate) const;
|
|
|
|
|
bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool constant, bool enumerate) const;
|
|
|
|
|
bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool constant, bool enumerate) const;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
struct CustomType
|
2014-01-04 02:14:53 -08:00
|
|
|
{
|
2016-09-02 09:53:22 -07:00
|
|
|
JS::PersistentRootedObject m_Prototype;
|
2016-08-02 09:12:11 -07:00
|
|
|
JSClass* m_Class;
|
|
|
|
|
JSNative m_Constructor;
|
2014-01-04 02:14:53 -08:00
|
|
|
};
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
// Take care to keep this declaration before heap rooted members. Destructors of heap rooted
|
2020-11-12 01:34:40 -08:00
|
|
|
// members have to be called before the custom destructor of ScriptInterface_impl.
|
2015-05-24 18:23:27 -07:00
|
|
|
std::unique_ptr<ScriptInterface_impl> m;
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-12-09 06:39:14 -08:00
|
|
|
boost::random::rand48* m_rng;
|
2014-01-04 02:14:53 -08:00
|
|
|
std::map<std::string, CustomType> m_CustomObjectTypes;
|
2010-01-09 11:20:14 -08:00
|
|
|
};
|
|
|
|
|
|
2014-07-20 12:45:18 -07:00
|
|
|
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 10:29:17 -08:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal(const ScriptRequest& rq, JS::MutableHandleValue handle, const T& a)
|
2015-01-24 06:46:52 -08:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
ToJSVal(rq, handle, a);
|
2015-01-24 06:46:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
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 10:29:17 -08:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::PersistentRootedValue>(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::PersistentRootedValue& a)
|
2014-07-20 12:45:18 -07:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
handle.set(a);
|
2014-07-20 12:45:18 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 09:45:16 -07:00
|
|
|
template<>
|
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 10:29:17 -08:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::Heap<JS::Value> >(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::Heap<JS::Value>& a)
|
2019-09-05 09:45:16 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 12:45:18 -07:00
|
|
|
template<>
|
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 10:29:17 -08:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::RootedValue>(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::RootedValue& a)
|
2014-07-20 12:45:18 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 13:31:29 -07:00
|
|
|
template <>
|
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 10:29:17 -08:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::HandleValue>(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::HandleValue& a)
|
2014-07-26 13:31:29 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
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 10:29:17 -08:00
|
|
|
inline void ScriptInterface::AssignOrToJSValUnrooted<JS::Value>(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::Value& a)
|
2014-07-26 13:31:29 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 06:42:59 -08:00
|
|
|
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 10:29:17 -08:00
|
|
|
inline T ScriptInterface::AssignOrFromJSVal(const ScriptRequest& rq, const JS::HandleValue& val, bool& ret)
|
2016-01-23 06:42:59 -08:00
|
|
|
{
|
|
|
|
|
T retVal;
|
2020-11-13 05:18:22 -08:00
|
|
|
ret = FromJSVal(rq, val, retVal);
|
2016-01-23 06:42:59 -08:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
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 10:29:17 -08:00
|
|
|
inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(const ScriptRequest& UNUSED(rq), const JS::HandleValue& val, bool& ret)
|
2016-01-23 06:42:59 -08:00
|
|
|
{
|
|
|
|
|
ret = true;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
template<typename T>
|
2019-01-13 08:37:41 -08:00
|
|
|
bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace, bool constant, bool enumerate)
|
2010-01-09 11:20:14 -08: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
AssignOrToJSVal(rq, &val, value);
|
2019-01-13 08:37:41 -08:00
|
|
|
return SetGlobal_(name, val, replace, constant, enumerate);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2019-07-19 14:58:58 -07:00
|
|
|
bool ScriptInterface::SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant, bool enumerate) const
|
2011-01-12 04:29:00 -08: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
AssignOrToJSVal(rq, &val, value);
|
2019-07-19 14:58:58 -07:00
|
|
|
return SetProperty_(obj, name, val, constant, enumerate);
|
2011-01-12 04:29:00 -08:00
|
|
|
}
|
|
|
|
|
|
2013-11-09 15:26:17 -08:00
|
|
|
template<typename T>
|
2019-07-19 14:58:58 -07:00
|
|
|
bool ScriptInterface::SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant, bool enumerate) const
|
2013-11-09 15:26:17 -08: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
AssignOrToJSVal(rq, &val, value);
|
2019-07-19 14:58:58 -07:00
|
|
|
return SetProperty_(obj, name, val, constant, enumerate);
|
2013-11-09 15:26:17 -08:00
|
|
|
}
|
|
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
template<typename T>
|
2019-07-19 14:58:58 -07:00
|
|
|
bool ScriptInterface::SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant, bool enumerate) const
|
2010-01-09 11:20:14 -08: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
AssignOrToJSVal(rq, &val, value);
|
2019-07-19 14:58:58 -07:00
|
|
|
return SetPropertyInt_(obj, name, val, constant, enumerate);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2017-03-24 11:47:03 -07:00
|
|
|
bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, T& out) const
|
2010-01-09 11:20:14 -08: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-12-27 09:18:13 -08:00
|
|
|
return GetProperty(rq, obj, name, out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool ScriptInterface::GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, T& out)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
2020-12-27 09:18:13 -08:00
|
|
|
if (!GetProperty(rq, obj, name, &val))
|
2010-01-09 11:20:14 -08:00
|
|
|
return false;
|
2020-11-13 05:18:22 -08:00
|
|
|
return FromJSVal(rq, val, out);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2011-06-27 16:27:25 -07:00
|
|
|
template<typename T>
|
2017-03-24 11:47:03 -07:00
|
|
|
bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, T& out) const
|
2011-06-27 16:27:25 -07: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-12-27 09:18:13 -08:00
|
|
|
return GetPropertyInt(rq, obj, name, out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool ScriptInterface::GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, T& out)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
2020-12-27 09:18:13 -08:00
|
|
|
if (!GetPropertyInt(rq, obj, name, &val))
|
2011-06-27 16:27:25 -07:00
|
|
|
return false;
|
2020-11-13 05:18:22 -08:00
|
|
|
return FromJSVal(rq, val, out);
|
2011-06-27 16:27:25 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-26 13:31:29 -07:00
|
|
|
|
2020-11-30 01:03:20 -08:00
|
|
|
template<typename T>
|
|
|
|
|
bool ScriptInterface::Eval(const char* code, T& ret) const
|
2010-01-09 11:20:14 -08: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(this);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue rval(rq.cx);
|
2020-11-30 01:03:20 -08:00
|
|
|
if (!Eval(code, &rval))
|
2010-01-09 11:20:14 -08:00
|
|
|
return false;
|
2020-11-13 05:18:22 -08:00
|
|
|
return FromJSVal(rq, rval, ret);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // INCLUDED_SCRIPTINTERFACE
|