Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/ScriptInterface.h
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* 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 127 Lines • ▼ Show 20 Lines | public: | ||||
JS::Value GetGlobalObject() const; | JS::Value GetGlobalObject() const; | ||||
/** | /** | ||||
* Set the named property on the global object. | * Set the named property on the global object. | ||||
* If @p replace is true, an existing property will be overwritten; otherwise attempts | * If @p replace is true, an existing property will be overwritten; otherwise attempts | ||||
* to set an already-defined value will fail. | * to set an already-defined value will fail. | ||||
*/ | */ | ||||
template<typename T> | template<typename T> | ||||
bool SetGlobal(const char* name, const T& value, bool replace = false); | bool SetGlobal(const char* name, const T& value, bool replace = false, bool constant = true, bool enumerate = true); | ||||
/** | /** | ||||
* Set the named property on the given object. | * Set the named property on the given object. | ||||
* Optionally makes it {ReadOnly, DontDelete, DontEnum}. | * Optionally makes it {ReadOnly, DontDelete, DontEnum}. | ||||
*/ | */ | ||||
template<typename T> | template<typename T> | ||||
bool SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant = false, bool enumerate = true) const; | bool SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant = false, bool enumerate = true) const; | ||||
▲ Show 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | public: | ||||
template <typename T> | template <typename T> | ||||
static T AssignOrFromJSVal(JSContext* cx, const JS::HandleValue& val, bool& ret); | static T AssignOrFromJSVal(JSContext* cx, const JS::HandleValue& val, bool& ret); | ||||
private: | private: | ||||
bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const; | bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const; | ||||
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 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 readonly, bool enumerate) const; | bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool readonly, bool enumerate) const; | ||||
bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool readonly, bool enumerate) const; | bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool readonly, bool enumerate) const; | ||||
bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate) const; | bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, 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); | static bool IsExceptionPending(JSContext* cx); | ||||
static const JSClass* GetClass(JS::HandleObject obj); | static const JSClass* GetClass(JS::HandleObject obj); | ||||
static void* GetPrivate(JS::HandleObject obj); | static void* GetPrivate(JS::HandleObject obj); | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | |||||
template<> | template<> | ||||
inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(JSContext* UNUSED(cx), const JS::HandleValue& val, bool& ret) | inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(JSContext* UNUSED(cx), const JS::HandleValue& val, bool& ret) | ||||
{ | { | ||||
ret = true; | ret = true; | ||||
return val; | return val; | ||||
} | } | ||||
template<typename T> | template<typename T> | ||||
bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace) | bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace, bool constant, bool enumerate) | ||||
{ | { | ||||
JSAutoRequest rq(GetContext()); | JSAutoRequest rq(GetContext()); | ||||
JS::RootedValue val(GetContext()); | JS::RootedValue val(GetContext()); | ||||
AssignOrToJSVal(GetContext(), &val, value); | AssignOrToJSVal(GetContext(), &val, value); | ||||
return SetGlobal_(name, val, replace); | return SetGlobal_(name, val, replace, constant, enumerate); | ||||
} | } | ||||
template<typename T> | template<typename T> | ||||
bool ScriptInterface::SetProperty(JS::HandleValue obj, const char* name, const T& value, bool readonly, bool enumerate) const | bool ScriptInterface::SetProperty(JS::HandleValue obj, const char* name, const T& value, bool readonly, bool enumerate) const | ||||
{ | { | ||||
JSAutoRequest rq(GetContext()); | JSAutoRequest rq(GetContext()); | ||||
JS::RootedValue val(GetContext()); | JS::RootedValue val(GetContext()); | ||||
AssignOrToJSVal(GetContext(), &val, value); | AssignOrToJSVal(GetContext(), &val, value); | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator