2020-01-12 21:04:22 +01:00
|
|
|
/* Copyright (C) 2020 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"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "ScriptTypes.h"
|
2014-01-04 11:14:53 +01:00
|
|
|
#include "ps/Errors.h"
|
2014-11-13 12:19:28 +01:00
|
|
|
|
2018-12-28 15:58:35 +01:00
|
|
|
#include <boost/random/linear_congruential.hpp>
|
|
|
|
#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, RegisterFunctionFailed);
|
|
|
|
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
|
|
|
|
2011-03-22 02:34:45 +01:00
|
|
|
// TODO: what's a good default?
|
|
|
|
#define DEFAULT_RUNTIME_SIZE 16 * 1024 * 1024
|
2014-09-22 22:13:04 +02:00
|
|
|
#define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 2 * 1024 *1024
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
struct ScriptInterface_impl;
|
2010-10-31 23:00:28 +01:00
|
|
|
|
2011-01-16 00:35:20 +01:00
|
|
|
class ScriptRuntime;
|
|
|
|
|
2014-01-04 11:14:53 +01:00
|
|
|
extern shared_ptr<ScriptRuntime> g_ScriptRuntime;
|
|
|
|
|
2013-03-07 14:49:49 +01:00
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
/**
|
|
|
|
* Abstraction around a SpiderMonkey JSContext.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
public:
|
|
|
|
|
2011-01-16 00:35:20 +01:00
|
|
|
/**
|
|
|
|
* Returns a runtime, which can used to initialise any number of
|
|
|
|
* ScriptInterfaces contexts. Values created in one context may be used
|
|
|
|
* in any other context from the same runtime (but not any other runtime).
|
|
|
|
* Each runtime should only ever be used on a single thread.
|
2011-03-22 02:34:45 +01:00
|
|
|
* @param runtimeSize Maximum size in bytes of the new runtime
|
2011-01-16 00:35:20 +01:00
|
|
|
*/
|
2016-11-23 12:18:37 +01:00
|
|
|
static shared_ptr<ScriptRuntime> CreateRuntime(shared_ptr<ScriptRuntime> parentRuntime = shared_ptr<ScriptRuntime>(), int runtimeSize = DEFAULT_RUNTIME_SIZE,
|
2014-09-22 22:13:04 +02:00
|
|
|
int heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER);
|
2011-01-16 00:35:20 +01:00
|
|
|
|
2014-01-23 12:32:08 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param nativeScopeName Name of global object that functions (via RegisterFunction) will
|
|
|
|
* 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.
|
|
|
|
* @param runtime ScriptRuntime to use when initializing this interface.
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
2011-01-16 00:35:20 +01:00
|
|
|
ScriptInterface(const char* nativeScopeName, const char* debugName, const shared_ptr<ScriptRuntime>& runtime);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
~ScriptInterface();
|
|
|
|
|
2014-01-04 11:14:53 +01:00
|
|
|
struct CxPrivate
|
|
|
|
{
|
|
|
|
ScriptInterface* pScriptInterface; // the ScriptInterface object the current context belongs to
|
|
|
|
void* pCBData; // meant to be used as the "this" object for callback functions
|
|
|
|
} m_CxPrivate;
|
|
|
|
|
|
|
|
void SetCallbackData(void* pCBData);
|
|
|
|
static CxPrivate* GetScriptInterfaceAndCBData(JSContext* cx);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-06 00:36:35 +02:00
|
|
|
JSContext* GetContext() const;
|
2014-01-04 11:14:53 +01:00
|
|
|
JSRuntime* GetJSRuntime() const;
|
|
|
|
shared_ptr<ScriptRuntime> GetRuntime() const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2012-07-03 04:16:45 +02:00
|
|
|
/**
|
|
|
|
* Load global scripts that most script contexts need,
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool ReplaceNondeterministicRNG(boost::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);
|
|
|
|
|
2019-07-22 21:35:14 +02:00
|
|
|
/**
|
|
|
|
* Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties.
|
2019-09-13 02:56:51 +02: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 21:35:14 +02:00
|
|
|
* Can throw an exception.
|
|
|
|
*/
|
2019-08-17 05:30:07 +02:00
|
|
|
template<typename... Args>
|
2019-09-13 02:56:51 +02:00
|
|
|
static bool CreateObject(JSContext* cx, JS::MutableHandleValue objectValue, Args const&... args)
|
2019-07-22 21:35:14 +02:00
|
|
|
{
|
2019-08-17 05:30:07 +02:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
JS::RootedObject obj(cx);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2019-09-13 02:56:51 +02:00
|
|
|
if (!CreateObject_(cx, &obj, args...))
|
2019-08-17 05:30:07 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
objectValue.setObject(*obj);
|
|
|
|
return true;
|
2019-07-22 21:35:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the given value to a new JS object or Null Value in case of out-of-memory.
|
|
|
|
*/
|
2019-09-13 02:56:51 +02:00
|
|
|
static void CreateArray(JSContext* cx, JS::MutableHandleValue objectValue, size_t length = 0);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2017-11-25 07:49:58 +01:00
|
|
|
JS::Value GetGlobalObject() const;
|
2010-11-17 00:00:52 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the named property on the given object.
|
|
|
|
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2017-08-24 02:32:42 +02:00
|
|
|
bool SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant = false, bool enumerate = true) const;
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2013-11-10 00:26:17 +01:00
|
|
|
/**
|
|
|
|
* Set the named property on the given object.
|
|
|
|
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2017-08-24 02:32:42 +02:00
|
|
|
bool SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant = false, bool enumerate = true) const;
|
2013-11-10 00:26:17 +01:00
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
/**
|
|
|
|
* Set the integer-named property on the given object.
|
|
|
|
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2017-08-24 02:32:42 +02:00
|
|
|
bool SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant = false, bool enumerate = true) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2011-06-28 01:27:25 +02:00
|
|
|
/**
|
|
|
|
* Get the named property on the given object.
|
|
|
|
*/
|
2010-01-09 20:20:14 +01:00
|
|
|
template<typename T>
|
2017-03-24 19:47:03 +01:00
|
|
|
bool GetProperty(JS::HandleValue obj, const char* name, T& out) const;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
/**
|
2014-07-26 22:31:29 +02:00
|
|
|
* Get the named property of the given object.
|
2014-03-28 21:26:32 +01:00
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
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 20:20:14 +01:00
|
|
|
|
2011-06-28 01:27:25 +02:00
|
|
|
/**
|
|
|
|
* Get the integer-named property on the given object.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2017-03-24 19:47:03 +01:00
|
|
|
bool GetPropertyInt(JS::HandleValue obj, int name, T& out) const;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2014-07-26 22:31:29 +02:00
|
|
|
/**
|
|
|
|
* Get the named property of the given object.
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) const;
|
2014-07-26 22:31:29 +02:00
|
|
|
|
2011-06-28 01:27:25 +02:00
|
|
|
/**
|
|
|
|
* Check the named property has been defined on the given object.
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
bool HasProperty(JS::HandleValue obj, const char* name) const;
|
2010-04-09 20:45:28 +02:00
|
|
|
|
2020-06-14 11:49:32 +02: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 20:20:14 +01:00
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2017-03-24 19:47:03 +01:00
|
|
|
bool FreezeObject(JS::HandleValue objVal, bool deep) const;
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2017-03-24 19:47:03 +01:00
|
|
|
bool Eval(const char* code) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2017-03-24 19:47:03 +01:00
|
|
|
template<typename CHAR> bool Eval(const CHAR* code, JS::MutableHandleValue out) const;
|
|
|
|
template<typename T, typename CHAR> bool Eval(const CHAR* code, T& out) const;
|
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
|
|
|
/**
|
|
|
|
* Report the given error message through the JS error reporting mechanism,
|
|
|
|
* and throw a JS exception. (Callers can check IsPendingException, and must
|
2015-01-24 15:46:52 +01:00
|
|
|
* return false in that case to propagate the exception.)
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
void ReportError(const char* msg) const;
|
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
|
|
|
|
*/
|
2017-03-24 19:47:03 +01:00
|
|
|
bool LoadGlobalScript(const VfsPath& filename, const std::wstring& 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
|
|
|
|
2010-05-06 00:36:35 +02:00
|
|
|
/**
|
|
|
|
* Construct a new value (usable in this ScriptInterface's context) by cloning
|
|
|
|
* a value from a different context.
|
|
|
|
* Complex values (functions, XML, etc) won't be cloned correctly, but basic
|
|
|
|
* types and cyclic references should be fine.
|
|
|
|
*/
|
2017-08-24 02:32:42 +02:00
|
|
|
JS::Value CloneValueFromOtherContext(const ScriptInterface& otherContext, JS::HandleValue val) const;
|
2010-05-06 00:36:35 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
2017-08-28 12:27:36 +02:00
|
|
|
* Convert a JS::Value to a C++ type. (This might trigger GC.)
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
2014-07-12 21:08:39 +02:00
|
|
|
template<typename T> static bool FromJSVal(JSContext* cx, const JS::HandleValue val, T& ret);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-28 12:27:36 +02:00
|
|
|
* Convert a C++ type to a JS::Value. (This might trigger GC. The return
|
2010-01-09 20:20:14 +01:00
|
|
|
* value must be rooted if you don't want it to be collected.)
|
2014-03-28 21:26:32 +01: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 20:20:14 +01:00
|
|
|
*/
|
2014-07-14 21:52:35 +02:00
|
|
|
template<typename T> static void ToJSVal(JSContext* cx, JS::MutableHandleValue ret, T const& val);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2017-09-13 00:52:15 +02:00
|
|
|
/**
|
|
|
|
* Convert a named property of an object to a C++ type.
|
|
|
|
*/
|
2020-07-12 11:25:03 +02:00
|
|
|
template<typename T> static bool FromJSProperty(JSContext* cx, const JS::HandleValue val, const char* name, T& ret, bool strict = false);
|
2017-09-13 00:52:15 +02:00
|
|
|
|
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
|
|
|
|
2011-01-16 00:35:20 +01:00
|
|
|
/**
|
2017-08-28 12:27:36 +02:00
|
|
|
* Structured clones are a way to serialize 'simple' JS::Values into a buffer
|
2011-01-16 00:35:20 +01:00
|
|
|
* that can safely be passed between contexts and runtimes and threads.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class StructuredClone
|
|
|
|
{
|
|
|
|
NONCOPYABLE(StructuredClone);
|
|
|
|
public:
|
|
|
|
StructuredClone();
|
|
|
|
~StructuredClone();
|
2014-03-28 21:26:32 +01:00
|
|
|
u64* m_Data;
|
2011-01-16 00:35:20 +01:00
|
|
|
size_t m_Size;
|
|
|
|
};
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
shared_ptr<StructuredClone> WriteStructuredClone(JS::HandleValue v) const;
|
|
|
|
void ReadStructuredClone(const shared_ptr<StructuredClone>& ptr, JS::MutableHandleValue ret) const;
|
2011-01-16 00:35:20 +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>
|
|
|
|
static T* GetPrivate(JSContext* cx, JS::HandleObject thisobj, JSClass* jsClass)
|
|
|
|
{
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
T* value = static_cast<T*>(JS_GetInstancePrivate(cx, thisobj, jsClass, nullptr));
|
|
|
|
if (value == nullptr && !JS_IsExceptionPending(cx))
|
|
|
|
JS_ReportError(cx, "Private data of the given object is null!");
|
|
|
|
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>
|
|
|
|
static T* GetPrivate(JSContext* cx, JS::CallArgs& callArgs, JSClass* jsClass)
|
|
|
|
{
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
if (!callArgs.thisv().isObject())
|
|
|
|
{
|
|
|
|
JS_ReportError(cx, "Cannot retrieve private JS class data because from a non-object value!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JS::RootedObject thisObj(cx, &callArgs.thisv().toObject());
|
|
|
|
T* value = static_cast<T*>(JS_GetInstancePrivate(cx, thisObj, jsClass, &callArgs));
|
|
|
|
if (value == nullptr && !JS_IsExceptionPending(cx))
|
|
|
|
JS_ReportError(cx, "Private data of the given object is null!");
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-07-20 21:45:18 +02: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>
|
2015-01-24 15:46:52 +01:00
|
|
|
static void AssignOrToJSVal(JSContext* cx, JS::MutableHandleValue handle, const T& a);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 12:18:37 +01:00
|
|
|
* used with JS::Value. If the specialization for JS::Value should be allowed, you can use this
|
2015-01-24 15:46:52 +01:00
|
|
|
* "unrooted" version of AssignOrToJSVal.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void AssignOrToJSValUnrooted(JSContext* cx, JS::MutableHandleValue handle, const T& a)
|
|
|
|
{
|
|
|
|
AssignOrToJSVal(cx, handle, a);
|
|
|
|
}
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2016-01-23 15:42:59 +01: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>
|
|
|
|
static T AssignOrFromJSVal(JSContext* cx, const JS::HandleValue& val, bool& ret);
|
2014-07-26 22:31:29 +02:00
|
|
|
|
|
|
|
private:
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2019-08-17 05:30:07 +02:00
|
|
|
/**
|
|
|
|
* Careful, the CreateObject_ helpers avoid creation of the JSAutoRequest!
|
|
|
|
*/
|
2019-09-13 02:56:51 +02:00
|
|
|
static bool CreateObject_(JSContext* cx, JS::MutableHandleObject obj);
|
2019-08-17 05:30:07 +02:00
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
2019-09-13 02:56:51 +02:00
|
|
|
static bool CreateObject_(JSContext* cx, JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args)
|
2019-08-17 05:30:07 +02:00
|
|
|
{
|
|
|
|
// JSAutoRequest is the responsibility of the caller
|
|
|
|
JS::RootedValue val(cx);
|
|
|
|
AssignOrToJSVal(cx, &val, propertyValue);
|
|
|
|
|
2019-09-13 02:56:51 +02:00
|
|
|
return CreateObject_(cx, obj, args...) && JS_DefineProperty(cx, obj, propertyName, val, JSPROP_ENUMERATE);
|
2019-08-17 05:30:07 +02:00
|
|
|
}
|
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const;
|
2017-03-24 19:47:03 +01:00
|
|
|
bool Eval_(const char* code, JS::MutableHandleValue ret) const;
|
|
|
|
bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const;
|
2019-01-13 17:37:41 +01:00
|
|
|
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate);
|
2019-07-19 23:58:58 +02: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;
|
2017-03-24 19:47:03 +01:00
|
|
|
bool GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const;
|
|
|
|
bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
static bool IsExceptionPending(JSContext* cx);
|
|
|
|
|
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
|
|
|
};
|
2017-08-24 02:32:42 +02:00
|
|
|
void Register(const char* name, JSNative fptr, size_t nargs) const;
|
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
|
|
|
|
// members have to be called before the runtime destructor.
|
2015-05-25 03:23:27 +02:00
|
|
|
std::unique_ptr<ScriptInterface_impl> m;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
boost::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
|
|
|
|
|
|
|
// The nasty macro/template bits are split into a separate file so you don't have to look at them
|
|
|
|
public:
|
|
|
|
#include "NativeWrapperDecls.h"
|
|
|
|
// This declares:
|
|
|
|
//
|
|
|
|
// template <R, T0..., TR (*fptr) (void* cbdata, T0...)>
|
2017-08-24 02:32:42 +02:00
|
|
|
// void RegisterFunction(const char* functionName) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
//
|
|
|
|
// template <R, T0..., TR (*fptr) (void* cbdata, T0...)>
|
2010-11-17 00:00:52 +01:00
|
|
|
// static JSNative call;
|
2010-01-09 20:20:14 +01:00
|
|
|
//
|
|
|
|
// template <R, T0..., JSClass*, TC, TR (TC:*fptr) (T0...)>
|
2010-11-17 00:00:52 +01:00
|
|
|
// static JSNative callMethod;
|
2010-01-09 20:20:14 +01:00
|
|
|
//
|
2017-01-20 03:25:19 +01:00
|
|
|
// template <R, T0..., JSClass*, TC, TR (TC:*fptr) const (T0...)>
|
|
|
|
// static JSNative callMethodConst;
|
|
|
|
//
|
2017-01-29 00:37:15 +01:00
|
|
|
// template <T0...>
|
2010-01-09 20:20:14 +01:00
|
|
|
// static size_t nargs();
|
2017-01-29 00:37:15 +01:00
|
|
|
//
|
|
|
|
// template <R, T0...>
|
|
|
|
// bool CallFunction(JS::HandleValue val, const char* name, R& ret, const T0&...) const;
|
|
|
|
//
|
|
|
|
// template <R, T0...>
|
|
|
|
// bool CallFunction(JS::HandleValue val, const char* name, JS::Rooted<R>* ret, const T0&...) const;
|
|
|
|
//
|
|
|
|
// template <R, T0...>
|
|
|
|
// bool CallFunction(JS::HandleValue val, const char* name, JS::MutableHandle<R> ret, const T0&...) const;
|
|
|
|
//
|
|
|
|
// template <T0...>
|
|
|
|
// bool CallFunctionVoid(JS::HandleValue val, const char* name, const T0&...) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Implement those declared functions
|
|
|
|
#include "NativeWrapperDefns.h"
|
|
|
|
|
2014-07-20 21:45:18 +02:00
|
|
|
template<typename T>
|
2015-01-24 15:46:52 +01:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal(JSContext* cx, JS::MutableHandleValue handle, const T& a)
|
|
|
|
{
|
|
|
|
ToJSVal(cx, handle, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::PersistentRootedValue>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::PersistentRootedValue& a)
|
2014-07-20 21:45:18 +02:00
|
|
|
{
|
2015-01-24 15:46:52 +01:00
|
|
|
handle.set(a);
|
2014-07-20 21:45:18 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 18:45:16 +02:00
|
|
|
template<>
|
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::Heap<JS::Value> >(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::Heap<JS::Value>& a)
|
|
|
|
{
|
|
|
|
handle.set(a);
|
|
|
|
}
|
|
|
|
|
2014-07-20 21:45:18 +02:00
|
|
|
template<>
|
2015-01-24 15:46:52 +01:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::RootedValue>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::RootedValue& a)
|
2014-07-20 21:45:18 +02:00
|
|
|
{
|
|
|
|
handle.set(a);
|
|
|
|
}
|
|
|
|
|
2014-07-26 22:31:29 +02:00
|
|
|
template <>
|
2015-01-24 15:46:52 +01:00
|
|
|
inline void ScriptInterface::AssignOrToJSVal<JS::HandleValue>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::HandleValue& a)
|
2014-07-26 22:31:29 +02:00
|
|
|
{
|
|
|
|
handle.set(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2015-01-24 15:46:52 +01:00
|
|
|
inline void ScriptInterface::AssignOrToJSValUnrooted<JS::Value>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::Value& a)
|
2014-07-26 22:31:29 +02:00
|
|
|
{
|
|
|
|
handle.set(a);
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:42:59 +01:00
|
|
|
template<typename T>
|
|
|
|
inline T ScriptInterface::AssignOrFromJSVal(JSContext* cx, const JS::HandleValue& val, bool& ret)
|
|
|
|
{
|
|
|
|
T retVal;
|
|
|
|
ret = FromJSVal(cx, val, retVal);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(JSContext* UNUSED(cx), const JS::HandleValue& val, bool& ret)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-07-14 21:52:35 +02:00
|
|
|
JSAutoRequest rq(GetContext());
|
|
|
|
JS::RootedValue val(GetContext());
|
2015-01-24 15:46:52 +01:00
|
|
|
AssignOrToJSVal(GetContext(), &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
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-07-19 23:58:58 +02:00
|
|
|
bool ScriptInterface::SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant, bool enumerate) const
|
2011-01-12 13:29:00 +01:00
|
|
|
{
|
2014-07-14 21:52:35 +02:00
|
|
|
JSAutoRequest rq(GetContext());
|
|
|
|
JS::RootedValue val(GetContext());
|
2015-01-24 15:46:52 +01:00
|
|
|
AssignOrToJSVal(GetContext(), &val, value);
|
2019-07-19 23:58:58 +02:00
|
|
|
return SetProperty_(obj, name, val, constant, enumerate);
|
2011-01-12 13:29:00 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 00:26:17 +01:00
|
|
|
template<typename T>
|
2019-07-19 23:58:58 +02:00
|
|
|
bool ScriptInterface::SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant, bool enumerate) const
|
2013-11-10 00:26:17 +01:00
|
|
|
{
|
2014-07-20 21:45:18 +02:00
|
|
|
JSAutoRequest rq(GetContext());
|
|
|
|
JS::RootedValue val(GetContext());
|
2015-01-24 15:46:52 +01:00
|
|
|
AssignOrToJSVal(GetContext(), &val, value);
|
2019-07-19 23:58:58 +02:00
|
|
|
return SetProperty_(obj, name, val, constant, enumerate);
|
2013-11-10 00:26:17 +01:00
|
|
|
}
|
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
template<typename T>
|
2019-07-19 23:58:58 +02:00
|
|
|
bool ScriptInterface::SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant, bool enumerate) const
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2014-07-14 21:52:35 +02:00
|
|
|
JSAutoRequest rq(GetContext());
|
|
|
|
JS::RootedValue val(GetContext());
|
2015-01-24 15:46:52 +01:00
|
|
|
AssignOrToJSVal(GetContext(), &val, value);
|
2019-07-19 23:58:58 +02:00
|
|
|
return SetPropertyInt_(obj, name, val, constant, enumerate);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-03-24 19:47:03 +01:00
|
|
|
bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, T& out) const
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
JSContext* cx = GetContext();
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
JS::RootedValue val(cx);
|
2017-03-24 19:47:03 +01:00
|
|
|
if (!GetProperty_(obj, name, &val))
|
2010-01-09 20:20:14 +01:00
|
|
|
return false;
|
2014-03-28 21:26:32 +01:00
|
|
|
return FromJSVal(cx, val, out);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-06-28 01:27:25 +02:00
|
|
|
template<typename T>
|
2017-03-24 19:47:03 +01:00
|
|
|
bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, T& out) const
|
2011-06-28 01:27:25 +02:00
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
JSAutoRequest rq(GetContext());
|
|
|
|
JS::RootedValue val(GetContext());
|
2017-03-24 19:47:03 +01:00
|
|
|
if (!GetPropertyInt_(obj, name, &val))
|
2011-06-28 01:27:25 +02:00
|
|
|
return false;
|
|
|
|
return FromJSVal(GetContext(), val, out);
|
|
|
|
}
|
|
|
|
|
2014-07-26 22:31:29 +02:00
|
|
|
template<typename CHAR>
|
2017-03-24 19:47:03 +01:00
|
|
|
bool ScriptInterface::Eval(const CHAR* code, JS::MutableHandleValue ret) const
|
2014-07-26 22:31:29 +02:00
|
|
|
{
|
2017-03-24 19:47:03 +01:00
|
|
|
if (!Eval_(code, ret))
|
2014-07-26 22:31:29 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-23 23:45:07 +01:00
|
|
|
template<typename T, typename CHAR>
|
2017-03-24 19:47:03 +01:00
|
|
|
bool ScriptInterface::Eval(const CHAR* code, T& ret) const
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2014-07-12 21:08:39 +02:00
|
|
|
JSAutoRequest rq(GetContext());
|
|
|
|
JS::RootedValue rval(GetContext());
|
2017-03-24 19:47:03 +01:00
|
|
|
if (!Eval_(code, &rval))
|
2010-01-09 20:20:14 +01:00
|
|
|
return false;
|
|
|
|
return FromJSVal(GetContext(), rval, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // INCLUDED_SCRIPTINTERFACE
|