Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/ScriptInterface.h
/* Copyright (C) 2020 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
Stan: year. | |||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
#define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 2 * 1024 *1024 | #define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 2 * 1024 *1024 | ||||
struct ScriptInterface_impl; | struct ScriptInterface_impl; | ||||
class ScriptRuntime; | class ScriptRuntime; | ||||
extern shared_ptr<ScriptRuntime> g_ScriptRuntime; | extern shared_ptr<ScriptRuntime> g_ScriptRuntime; | ||||
// Early-declare to friend in ScriptInterface. | |||||
class ScriptWrapper; | |||||
/** | /** | ||||
* Abstraction around a SpiderMonkey JSContext. | * Abstraction around a SpiderMonkey JSContext. | ||||
* | * | ||||
* Thread-safety: | * Thread-safety: | ||||
* - May be used in non-main threads. | * - May be used in non-main threads. | ||||
* - Each ScriptInterface must be created, used, and destroyed, all in a single thread | * - Each ScriptInterface must be created, used, and destroyed, all in a single thread | ||||
* (it must never be shared between threads). | * (it must never be shared between threads). | ||||
*/ | */ | ||||
class ScriptInterface | class ScriptInterface | ||||
{ | { | ||||
NONCOPYABLE(ScriptInterface); | NONCOPYABLE(ScriptInterface); | ||||
friend class ScriptWrapper; | |||||
public: | public: | ||||
/** | /** | ||||
* Returns a runtime, which can used to initialise any number of | * Returns a runtime, which can used to initialise any number of | ||||
* ScriptInterfaces contexts. Values created in one context may be used | * ScriptInterfaces contexts. Values created in one context may be used | ||||
* in any other context from the same runtime (but not any other runtime). | * in any other context from the same runtime (but not any other runtime). | ||||
* Each runtime should only ever be used on a single thread. | * Each runtime should only ever be used on a single thread. | ||||
* @param runtimeSize Maximum size in bytes of the new runtime | * @param runtimeSize Maximum size in bytes of the new runtime | ||||
▲ Show 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | public: | ||||
/** | /** | ||||
* Report the given error message through the JS error reporting mechanism, | * Report the given error message through the JS error reporting mechanism, | ||||
* and throw a JS exception. (Callers can check IsPendingException, and must | * and throw a JS exception. (Callers can check IsPendingException, and must | ||||
* return false in that case to propagate the exception.) | * return false in that case to propagate the exception.) | ||||
*/ | */ | ||||
void ReportError(const char* msg) const; | void ReportError(const char* msg) const; | ||||
/** | /** | ||||
* Return true if a JS exception is pending. | |||||
*/ | |||||
static bool IsExceptionPending(JSContext* cx); | |||||
/** | |||||
* Load and execute the given script in a new function scope. | * Load and execute the given script in a new function scope. | ||||
* @param filename Name for debugging purposes (not used to load the file) | * @param filename Name for debugging purposes (not used to load the file) | ||||
* @param code JS code to execute | * @param code JS code to execute | ||||
* @return true on successful compilation and execution; false otherwise | * @return true on successful compilation and execution; false otherwise | ||||
*/ | */ | ||||
bool LoadScript(const VfsPath& filename, const std::string& code) const; | bool LoadScript(const VfsPath& filename, const std::string& code) const; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | public: | ||||
u64* m_Data; | u64* m_Data; | ||||
size_t m_Size; | size_t m_Size; | ||||
}; | }; | ||||
shared_ptr<StructuredClone> WriteStructuredClone(JS::HandleValue v) const; | shared_ptr<StructuredClone> WriteStructuredClone(JS::HandleValue v) const; | ||||
void ReadStructuredClone(const shared_ptr<StructuredClone>& ptr, JS::MutableHandleValue ret) const; | void ReadStructuredClone(const shared_ptr<StructuredClone>& ptr, JS::MutableHandleValue ret) const; | ||||
/** | /** | ||||
* Retrieve the private data field of a JSObject. | |||||
* NB: if you know the JSClass, use one of the below instead. | |||||
*/ | |||||
template <typename T> | |||||
static T* getPrivate(JSContext* cx, JS::CallArgs& callArgs) | |||||
{ | |||||
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_GetPrivate(thisObj)); | |||||
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 JSObject that is an instance of the given JSClass. | * Retrieve the private data field of a JSObject that is an instance of the given JSClass. | ||||
*/ | */ | ||||
template <typename T> | template <typename T> | ||||
static T* GetPrivate(JSContext* cx, JS::HandleObject thisobj, JSClass* jsClass) | static T* GetPrivate(JSContext* cx, JS::HandleObject thisobj, JSClass* jsClass) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
T* value = static_cast<T*>(JS_GetInstancePrivate(cx, thisobj, jsClass, nullptr)); | T* value = static_cast<T*>(JS_GetInstancePrivate(cx, thisobj, jsClass, nullptr)); | ||||
if (value == nullptr && !JS_IsExceptionPending(cx)) | if (value == nullptr && !JS_IsExceptionPending(cx)) | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | private: | ||||
bool Eval_(const char* code, JS::MutableHandleValue ret) const; | bool Eval_(const char* code, JS::MutableHandleValue ret) const; | ||||
bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const; | bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const; | ||||
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate); | bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate); | ||||
bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool constant, bool enumerate) const; | 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 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; | bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool constant, bool enumerate) const; | ||||
bool GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const; | bool GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const; | ||||
bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const; | bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const; | ||||
static bool IsExceptionPending(JSContext* cx); | |||||
struct CustomType | struct CustomType | ||||
{ | { | ||||
JS::PersistentRootedObject m_Prototype; | JS::PersistentRootedObject m_Prototype; | ||||
JSClass* m_Class; | JSClass* m_Class; | ||||
JSNative m_Constructor; | JSNative m_Constructor; | ||||
}; | }; | ||||
void Register(const char* name, JSNative fptr, size_t nargs) const; | void Register(const char* name, JSNative fptr, size_t nargs) const; | ||||
▲ Show 20 Lines • Show All 171 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
year.