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 326 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 92 Lines • ▼ Show 20 Lines | private: | ||||
// Take care to keep this declaration before heap rooted members. Destructors of heap rooted | // Take care to keep this declaration before heap rooted members. Destructors of heap rooted | ||||
// members have to be called before the runtime destructor. | // members have to be called before the runtime destructor. | ||||
std::unique_ptr<ScriptInterface_impl> m; | std::unique_ptr<ScriptInterface_impl> m; | ||||
boost::rand48* m_rng; | boost::rand48* m_rng; | ||||
std::map<std::string, CustomType> m_CustomObjectTypes; | std::map<std::string, CustomType> m_CustomObjectTypes; | ||||
// The nasty macro/template bits are split into a separate file so you don't have to look at them | // The nasty macro/template bits are split into a separate file so you don't have to look at them | ||||
protected: | |||||
#include "FunctionWrapper.h" | |||||
public: | public: | ||||
// flags must be a constant expression so until C++17 template-arguments it is. | |||||
template <typename funcPtr, funcPtr callable, u16 flags = JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT> | |||||
static JSFunctionSpec Wrap(const char* name) | |||||
{ | |||||
return JS_FN(name, (&scriptMethod<funcPtr, callable>), args_info<funcPtr>::nb_args, flags); | |||||
} | |||||
template <typename funcPtr, funcPtr callable> | |||||
void RegisterGlobalFunction(const char* name) const | |||||
{ | |||||
Register(name, &(scriptMethod<funcPtr, callable>), args_info<funcPtr>::nb_args); | |||||
} | |||||
#include "NativeWrapperDecls.h" | #include "NativeWrapperDecls.h" | ||||
// This declares: | // This declares: | ||||
// | // | ||||
// template <R, T0..., TR (*fptr) (void* cbdata, T0...)> | // template <R, T0..., TR (*fptr) (void* cbdata, T0...)> | ||||
// void RegisterFunction(const char* functionName) const; | // void RegisterFunction(const char* functionName) const; | ||||
// | // | ||||
// template <R, T0..., TR (*fptr) (void* cbdata, T0...)> | // template <R, T0..., TR (*fptr) (void* cbdata, T0...)> | ||||
// static JSNative call; | // static JSNative call; | ||||
▲ Show 20 Lines • Show All 153 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
year.