Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/ScriptInterface.h
Show First 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | public: | ||||
*/ | */ | ||||
void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const; | void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const; | ||||
JSObject* CreateCustomObject(const std::string & typeName) const; | JSObject* CreateCustomObject(const std::string & typeName) const; | ||||
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs); | void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs); | ||||
/** | /** | ||||
* Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties. | * Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties. | ||||
* This is static so that callers like ToJSVal can use it with the JSContext directly instead of having to obtain the instance using GetScriptInterfaceAndCBData. | |||||
* Can throw an exception. | * Can throw an exception. | ||||
*/ | */ | ||||
template<typename... Args> | template<typename... Args> | ||||
bool CreateObject(JS::MutableHandleValue objectValue, Args const&... args) const | static bool CreateObject(JSContext* cx, JS::MutableHandleValue objectValue, Args const&... args) | ||||
{ | { | ||||
JSContext* cx = GetContext(); | |||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedObject obj(cx); | JS::RootedObject obj(cx); | ||||
if (!CreateObject_(&obj, args...)) | if (!CreateObject_(cx, &obj, args...)) | ||||
return false; | return false; | ||||
objectValue.setObject(*obj); | objectValue.setObject(*obj); | ||||
return true; | return true; | ||||
} | } | ||||
/** | /** | ||||
* Sets the given value to a new JS object or Null Value in case of out-of-memory. | * Sets the given value to a new JS object or Null Value in case of out-of-memory. | ||||
*/ | */ | ||||
void CreateArray(JS::MutableHandleValue objectValue, size_t length = 0) const; | static void CreateArray(JSContext* cx, JS::MutableHandleValue objectValue, size_t length = 0); | ||||
JS::Value GetGlobalObject() const; | JS::Value GetGlobalObject() const; | ||||
/** | /** | ||||
* Set the named property on the global object. | * Set the named property on the global object. | ||||
* Optionally makes it {ReadOnly, DontEnum}. We do not allow to make it DontDelete, so that it can be hotloaded | * 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. | * by deleting it and re-creating it, which is done by setting @p replace to true. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 234 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: | ||||
/** | /** | ||||
* Careful, the CreateObject_ helpers avoid creation of the JSAutoRequest! | * Careful, the CreateObject_ helpers avoid creation of the JSAutoRequest! | ||||
*/ | */ | ||||
bool CreateObject_(JS::MutableHandleObject obj) const; | static bool CreateObject_(JSContext* cx, JS::MutableHandleObject obj); | ||||
template<typename T, typename... Args> | template<typename T, typename... Args> | ||||
bool CreateObject_(JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args) const | static bool CreateObject_(JSContext* cx, JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args) | ||||
{ | { | ||||
// JSAutoRequest is the responsibility of the caller | // JSAutoRequest is the responsibility of the caller | ||||
JSContext* cx = GetContext(); | |||||
JS::RootedValue val(cx); | JS::RootedValue val(cx); | ||||
AssignOrToJSVal(cx, &val, propertyValue); | AssignOrToJSVal(cx, &val, propertyValue); | ||||
return CreateObject_(obj, args...) && JS_DefineProperty(cx, obj, propertyName, val, JSPROP_ENUMERATE); | return CreateObject_(cx, obj, args...) && JS_DefineProperty(cx, obj, propertyName, val, JSPROP_ENUMERATE); | ||||
} | } | ||||
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 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; | ||||
▲ Show 20 Lines • Show All 202 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator