Adds support for passing JS::HandleValue to SetProperty and CallFunctionVoid without using ugly casing.

Also includes one little "demo-usecase", but additional code will be
changed to use that in future commits.

Refs #2415
Refs #2462

This was SVN commit r15542.
This commit is contained in:
Yves
2014-07-20 19:45:18 +00:00
parent 31f9ca9ebf
commit a9048fa43d
2 changed files with 40 additions and 11 deletions
+36 -9
View File
@@ -367,6 +367,18 @@ public:
jsval ReadStructuredClone(const shared_ptr<StructuredClone>& ptr);
private:
/**
* 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>
void AssignOrToJSVal(JS::MutableHandleValue handle, const T& a);
bool CallFunction_(JS::HandleValue val, const char* name, uint argc, jsval* argv, JS::MutableHandleValue ret);
bool Eval_(const char* code, JS::MutableHandleValue ret);
bool Eval_(const wchar_t* code, JS::MutableHandleValue ret);
@@ -414,6 +426,18 @@ public:
// Implement those declared functions
#include "NativeWrapperDefns.h"
template<typename T>
inline void ScriptInterface::AssignOrToJSVal(JS::MutableHandleValue handle, const T& a)
{
ToJSVal(GetContext(), handle, a);
}
template<>
inline void ScriptInterface::AssignOrToJSVal<JS::RootedValue>(JS::MutableHandleValue handle, const JS::RootedValue& a)
{
handle.set(a);
}
template<typename T0>
bool ScriptInterface::CallFunctionVoid(jsval val, const char* name, const T0& a0)
{
@@ -423,7 +447,7 @@ bool ScriptInterface::CallFunctionVoid(jsval val, const char* name, const T0& a0
JS::RootedValue val1(cx, val);
JS::AutoValueVector argv(cx);
argv.resize(1);
ToJSVal(cx, argv.handleAt(0), a0);
AssignOrToJSVal(argv.handleAt(0), a0);
return CallFunction_(val1, name, 1, argv.begin(), &jsRet);
}
@@ -436,8 +460,8 @@ bool ScriptInterface::CallFunctionVoid(jsval val, const char* name, const T0& a0
JS::RootedValue val1(cx, val);
JS::AutoValueVector argv(cx);
argv.resize(2);
ToJSVal(cx, argv.handleAt(0), a0);
ToJSVal(cx, argv.handleAt(1), a1);
AssignOrToJSVal(argv.handleAt(0), a0);
AssignOrToJSVal(argv.handleAt(1), a1);
return CallFunction_(val1, name, 2, argv.begin(), &jsRet);
}
@@ -450,9 +474,9 @@ bool ScriptInterface::CallFunctionVoid(jsval val, const char* name, const T0& a0
JS::RootedValue val1(cx, val);
JS::AutoValueVector argv(cx);
argv.resize(3);
ToJSVal(cx, argv.handleAt(0), a0);
ToJSVal(cx, argv.handleAt(1), a1);
ToJSVal(cx, argv.handleAt(2), a2);
AssignOrToJSVal(argv.handleAt(0), a0);
AssignOrToJSVal(argv.handleAt(1), a1);
AssignOrToJSVal(argv.handleAt(2), a2);
return CallFunction_(val1, name, 3, argv.begin(), &jsRet);
}
@@ -470,14 +494,17 @@ bool ScriptInterface::SetProperty(jsval obj, const char* name, const T& value, b
{
JSAutoRequest rq(GetContext());
JS::RootedValue val(GetContext());
ToJSVal(GetContext(), &val, value);
AssignOrToJSVal(&val, value);
return SetProperty_(obj, name, val, readonly, enumerate);
}
template<typename T>
bool ScriptInterface::SetProperty(jsval obj, const wchar_t* name, const T& value, bool readonly, bool enumerate)
{
return SetProperty_(obj, name, ToJSVal(GetContext(), value), readonly, enumerate);
JSAutoRequest rq(GetContext());
JS::RootedValue val(GetContext());
AssignOrToJSVal(&val, value);
return SetProperty_(obj, name, val, readonly, enumerate);
}
template<typename T>
@@ -485,7 +512,7 @@ bool ScriptInterface::SetPropertyInt(jsval obj, int name, const T& value, bool r
{
JSAutoRequest rq(GetContext());
JS::RootedValue val(GetContext());
ToJSVal(GetContext(), &val, value);
AssignOrToJSVal(&val, value);
return SetPropertyInt_(obj, name, val, readonly, enumerate);
}