2014-04-01 23:16:52 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "NativeJSContainer.h"
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#include <jni.h>
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#include "Bundle.h"
|
|
|
|
#include "GeneratedJNINatives.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsJSUtils.h"
|
2014-04-01 23:16:53 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#include <mozilla/Vector.h>
|
|
|
|
#include <mozilla/jni/Accessors.h>
|
|
|
|
#include <mozilla/jni/Refs.h>
|
|
|
|
#include <mozilla/jni/Utils.h>
|
2014-04-01 23:16:53 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
/**
|
|
|
|
* NativeJSContainer.cpp implements the native methods in both
|
|
|
|
* NativeJSContainer and NativeJSObject, using JSAPI to retrieve values from a
|
|
|
|
* JSObject and using JNI to return those values to Java code.
|
|
|
|
*/
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
namespace {
|
2014-04-01 23:16:52 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool CheckThread()
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
jni::ThrowException("java/lang/IllegalThreadStateException",
|
|
|
|
"Not on Gecko thread");
|
|
|
|
return false;
|
2014-04-01 23:16:52 +04:00
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 23:16:53 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool
|
|
|
|
CheckJSCall(bool result)
|
|
|
|
{
|
|
|
|
if (!result) {
|
|
|
|
jni::ThrowException("java/lang/UnsupportedOperationException",
|
|
|
|
"JSAPI call failed");
|
2014-04-01 23:16:53 +04:00
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return result;
|
|
|
|
}
|
2014-04-01 23:16:53 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class C> bool
|
|
|
|
CheckJNIArgument(const jni::Ref<C>& arg)
|
|
|
|
{
|
|
|
|
if (!arg) {
|
|
|
|
jni::ThrowException("java/lang/IllegalArgumentException",
|
|
|
|
"Null argument");
|
2014-04-01 23:16:53 +04:00
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return !!arg;
|
|
|
|
}
|
2014-04-01 23:16:52 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
nsresult
|
|
|
|
CheckSDKCall(nsresult rv)
|
2014-04-01 23:16:52 +04:00
|
|
|
{
|
2015-08-05 00:47:28 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
jni::ThrowException("java/lang/UnsupportedOperationException",
|
|
|
|
"SDK JNI call failed");
|
|
|
|
}
|
|
|
|
return rv;
|
2014-04-01 23:16:52 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Convert a JNI string to a char16_t string that JSAPI expects.
|
|
|
|
class JSJNIString final
|
2014-04-01 23:16:54 +04:00
|
|
|
{
|
2015-08-05 00:47:28 +03:00
|
|
|
JNIEnv* const mEnv;
|
|
|
|
jni::String::Param mJNIString;
|
|
|
|
const char16_t* const mJSString;
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
public:
|
2015-08-05 00:47:28 +03:00
|
|
|
JSJNIString(JNIEnv* env, jni::String::Param str)
|
2014-04-01 23:16:54 +04:00
|
|
|
: mEnv(env)
|
|
|
|
, mJNIString(str)
|
2015-08-05 00:47:28 +03:00
|
|
|
, mJSString(!str ? nullptr : reinterpret_cast<const char16_t*>(
|
|
|
|
mEnv->GetStringChars(str.Get(), nullptr)))
|
|
|
|
{}
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
~JSJNIString() {
|
|
|
|
if (mJNIString) {
|
2015-08-05 00:47:28 +03:00
|
|
|
mEnv->ReleaseStringChars(mJNIString.Get(),
|
2014-04-01 23:16:54 +04:00
|
|
|
reinterpret_cast<const jchar*>(mJSString));
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
operator const char16_t*() const {
|
2014-04-01 23:16:54 +04:00
|
|
|
return mJSString;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
size_t Length() const {
|
2015-08-05 00:47:28 +03:00
|
|
|
return static_cast<size_t>(mEnv->GetStringLength(mJNIString.Get()));
|
2014-04-01 23:16:54 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
} // namepsace
|
2014-04-01 23:16:54 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
class NativeJSContainerImpl final
|
2015-08-05 00:47:28 +03:00
|
|
|
: public NativeJSObject::Natives<NativeJSContainerImpl>
|
2015-08-05 00:47:28 +03:00
|
|
|
, public NativeJSContainer::Natives<NativeJSContainerImpl>
|
2014-05-12 20:50:47 +04:00
|
|
|
{
|
2015-08-05 00:47:28 +03:00
|
|
|
typedef NativeJSContainerImpl Self;
|
|
|
|
typedef NativeJSContainer::Natives<NativeJSContainerImpl> ContainerBase;
|
|
|
|
typedef NativeJSObject::Natives<NativeJSContainerImpl> ObjectBase;
|
2014-04-01 23:16:54 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
typedef JS::PersistentRooted<JSObject*> PersistentObject;
|
|
|
|
|
|
|
|
JNIEnv* const mEnv;
|
|
|
|
// Context that the object is valid in
|
|
|
|
JSContext* const mJSContext;
|
|
|
|
// Root JS object
|
|
|
|
PersistentObject mJSObject;
|
|
|
|
// Children objects
|
|
|
|
Vector<NativeJSObject::GlobalRef, 0> mChildren;
|
|
|
|
|
|
|
|
bool CheckObject() const
|
|
|
|
{
|
|
|
|
if (!mJSObject) {
|
|
|
|
jni::ThrowException("java/lang/NullPointerException",
|
|
|
|
"Null JSObject");
|
|
|
|
}
|
|
|
|
return !!mJSObject;
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Check that a JS Value contains a particular property type as indicaed by
|
|
|
|
// the property's InValue method (e.g. StringProperty::InValue).
|
|
|
|
bool CheckProperty(bool (Self::*InValue)(JS::HandleValue) const,
|
|
|
|
JS::HandleValue val) const
|
|
|
|
{
|
|
|
|
if (!(this->*InValue)(val)) {
|
|
|
|
// XXX this can happen when converting a double array inside a
|
|
|
|
// Bundle, because double arrays can be misidentified as an int
|
|
|
|
// array. The workaround is to add a dummy first element to the
|
|
|
|
// array that is a floating point value, i.e. [0.5, ...].
|
|
|
|
jni::ThrowException(
|
|
|
|
"org/mozilla/gecko/util/NativeJSObject$InvalidPropertyException",
|
|
|
|
"Property type mismatch");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 23:16:54 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Primitive properties
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<bool (JS::Value::*IsType)() const> bool
|
|
|
|
PrimitiveInValue(JS::HandleValue val) const
|
|
|
|
{
|
|
|
|
return (static_cast<const JS::Value&>(val).*IsType)();
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<typename U, U (JS::Value::*ToType)() const> U
|
|
|
|
PrimitiveFromValue(JS::HandleValue val) const
|
|
|
|
{
|
|
|
|
return (static_cast<const JS::Value&>(val).*ToType)();
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class Prop> typename Prop::NativeArray
|
|
|
|
PrimitiveNewArray(JS::HandleObject array, size_t length) const
|
|
|
|
{
|
|
|
|
typedef typename Prop::JNIType JNIType;
|
|
|
|
|
|
|
|
// Fill up a temporary buffer for our array, then use
|
|
|
|
// JNIEnv::Set*ArrayRegion to fill out array in one go.
|
|
|
|
|
|
|
|
UniquePtr<JNIType[]> buffer = MakeUnique<JNIType[]>(length);
|
2014-05-17 02:25:29 +04:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2015-08-05 00:47:28 +03:00
|
|
|
JS::RootedValue elem(mJSContext);
|
|
|
|
if (!CheckJSCall(JS_GetElement(mJSContext, array, i, &elem)) ||
|
|
|
|
!CheckProperty(Prop::InValue, elem)) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
buffer[i] = JNIType((this->*Prop::FromValue)(elem));
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
auto jarray = Prop::NativeArray::Adopt(
|
|
|
|
mEnv, (mEnv->*Prop::NewJNIArray)(length));
|
2014-05-17 02:25:29 +04:00
|
|
|
if (!jarray) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
(mEnv->*Prop::SetJNIArrayRegion)(
|
|
|
|
jarray.Get(), 0, length, buffer.get());
|
|
|
|
if (mEnv->ExceptionCheck()) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return jarray;
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<typename U, typename UA, typename V, typename VA,
|
|
|
|
bool (JS::Value::*IsType)() const,
|
|
|
|
U (JS::Value::*ToType)() const,
|
|
|
|
VA (JNIEnv::*NewArray_)(jsize),
|
|
|
|
void (JNIEnv::*SetArrayRegion_)(VA, jsize, jsize, const V*)>
|
|
|
|
struct PrimitiveProperty
|
|
|
|
{
|
|
|
|
// C++ type for a primitive property (e.g. bool)
|
|
|
|
typedef U NativeType;
|
|
|
|
// C++ type for the fallback value used in opt* methods
|
|
|
|
typedef U NativeFallback;
|
|
|
|
// Type for an array of the primitive type (e.g. BooleanArray::LocalRef)
|
|
|
|
typedef typename UA::LocalRef NativeArray;
|
|
|
|
// Type for the fallback value used in opt*Array methods
|
|
|
|
typedef const typename UA::Ref ArrayFallback;
|
|
|
|
// JNI type (e.g. jboolean)
|
|
|
|
typedef V JNIType;
|
|
|
|
|
|
|
|
// JNIEnv function to create a new JNI array of the primiive type
|
|
|
|
typedef decltype(NewArray_) NewJNIArray_t;
|
|
|
|
static constexpr NewJNIArray_t NewJNIArray = NewArray_;
|
|
|
|
|
|
|
|
// JNIEnv function to fill a JNI array of the primiive type
|
|
|
|
typedef decltype(SetArrayRegion_) SetJNIArrayRegion_t;
|
|
|
|
static constexpr SetJNIArrayRegion_t SetJNIArrayRegion = SetArrayRegion_;
|
|
|
|
|
|
|
|
// Function to determine if a JS Value contains the primitive type
|
|
|
|
typedef decltype(&Self::PrimitiveInValue<IsType>) InValue_t;
|
|
|
|
static constexpr InValue_t InValue = &Self::PrimitiveInValue<IsType>;
|
|
|
|
|
|
|
|
// Function to convert a JS Value to the primitive type
|
|
|
|
typedef decltype(&Self::PrimitiveFromValue<U, ToType>) FromValue_t;
|
|
|
|
static constexpr FromValue_t FromValue
|
|
|
|
= &Self::PrimitiveFromValue<U, ToType>;
|
|
|
|
|
|
|
|
// Function to convert a JS array to a JNI array
|
|
|
|
typedef decltype(&Self::PrimitiveNewArray<PrimitiveProperty>) NewArray_t;
|
|
|
|
static constexpr NewArray_t NewArray
|
|
|
|
= &Self::PrimitiveNewArray<PrimitiveProperty>;
|
|
|
|
};
|
|
|
|
|
|
|
|
// String properties
|
|
|
|
|
|
|
|
bool StringInValue(JS::HandleValue val) const
|
|
|
|
{
|
|
|
|
return val.isString();
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::String::LocalRef
|
|
|
|
StringFromValue(const JS::HandleString str) const
|
|
|
|
{
|
|
|
|
nsAutoJSString autoStr;
|
|
|
|
if (!CheckJSCall(autoStr.init(mJSContext, str))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Param<String> can automatically convert a nsString to jstring.
|
|
|
|
return jni::Param<jni::String>(autoStr, mEnv);
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::String::LocalRef
|
|
|
|
StringFromValue(JS::HandleValue val)
|
|
|
|
{
|
|
|
|
const JS::RootedString str(mJSContext, val.toString());
|
|
|
|
return StringFromValue(str);
|
2014-05-12 20:50:47 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Bundle properties
|
|
|
|
|
|
|
|
sdk::Bundle::LocalRef
|
|
|
|
BundleFromValue(const JS::HandleObject obj)
|
|
|
|
{
|
2015-08-05 21:02:00 +03:00
|
|
|
JS::Rooted<JS::IdVector> ids(mJSContext, JS::IdVector(mJSContext));
|
|
|
|
if (!CheckJSCall(JS_Enumerate(mJSContext, obj, &ids))) {
|
2014-04-01 23:16:55 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
const size_t length = ids.length();
|
|
|
|
sdk::Bundle::LocalRef newBundle(mEnv);
|
|
|
|
NS_ENSURE_SUCCESS(CheckSDKCall(
|
|
|
|
sdk::Bundle::New(length, &newBundle)), nullptr);
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Iterate through each property of the JS object. For each property,
|
|
|
|
// determine its type from a list of supported types, and convert that
|
|
|
|
// proeprty to the supported type.
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
for (size_t i = 0; i < ids.length(); i++) {
|
|
|
|
const JS::RootedId id(mJSContext, ids[i]);
|
|
|
|
JS::RootedValue idVal(mJSContext);
|
|
|
|
if (!CheckJSCall(JS_IdToValue(mJSContext, id, &idVal))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JS::RootedString idStr(mJSContext,
|
|
|
|
JS::ToString(mJSContext, idVal));
|
|
|
|
if (!CheckJSCall(!!idStr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
jni::String::LocalRef name = StringFromValue(idStr);
|
|
|
|
JS::RootedValue val(mJSContext);
|
|
|
|
if (!name ||
|
|
|
|
!CheckJSCall(JS_GetPropertyById(mJSContext, obj, id, &val))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PUT_IN_BUNDLE_IF_TYPE_IS(TYPE) \
|
|
|
|
if ((this->*TYPE##Property::InValue)(val)) { \
|
|
|
|
auto jval = (this->*TYPE##Property::FromValue)(val); \
|
|
|
|
if (mEnv->ExceptionCheck()) { \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
|
|
|
NS_ENSURE_SUCCESS(CheckSDKCall( \
|
|
|
|
newBundle->Put##TYPE(name, jval)), nullptr); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
((void) 0) // Accommodate trailing semicolon.
|
|
|
|
|
|
|
|
// Scalar values are faster to check, so check them first.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Boolean);
|
|
|
|
// Int can be casted to double, so check int first.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Int);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Double);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(String);
|
|
|
|
// There's no "putObject", so don't check ObjectProperty
|
|
|
|
|
|
|
|
// Check for array types if scalar checks all failed.
|
|
|
|
// XXX empty arrays are treated as boolean arrays. Workaround is
|
|
|
|
// to always have a dummy element to create a non-empty array.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(BooleanArray);
|
|
|
|
// XXX because we only check the first element of an array,
|
|
|
|
// a double array can potentially be seen as an int array.
|
|
|
|
// When that happens, the Bundle conversion will fail.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(IntArray);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(DoubleArray);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(StringArray);
|
|
|
|
// There's no "putObjectArray", so don't check ObjectArrayProperty
|
|
|
|
// There's no "putBundleArray", so don't check BundleArrayProperty
|
|
|
|
|
|
|
|
// Use Bundle as the default catch-all for objects
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Bundle);
|
|
|
|
|
|
|
|
#undef PUT_IN_BUNDLE_IF_TYPE_IS
|
|
|
|
|
|
|
|
// We tried all supported types; just bail.
|
|
|
|
jni::ThrowException("java/lang/UnsupportedOperationException",
|
|
|
|
"Unsupported property type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return jni::Object::LocalRef::Adopt(newBundle.Env(),
|
|
|
|
newBundle.Forget());
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
sdk::Bundle::LocalRef
|
|
|
|
BundleFromValue(JS::HandleValue val)
|
|
|
|
{
|
2014-04-01 23:16:55 +04:00
|
|
|
if (val.isNull()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
JS::RootedObject object(mJSContext, &val.toObject());
|
|
|
|
return BundleFromValue(object);
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Object properties
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool ObjectInValue(JS::HandleValue val) const
|
|
|
|
{
|
|
|
|
return val.isObjectOrNull();
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSObject::LocalRef
|
|
|
|
ObjectFromValue(JS::HandleValue val)
|
|
|
|
{
|
|
|
|
if (val.isNull()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JS::RootedObject object(mJSContext, &val.toObject());
|
|
|
|
return CreateChild(object);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class Prop> typename Prop::NativeArray
|
|
|
|
ObjectNewArray(JS::HandleObject array, size_t length)
|
|
|
|
{
|
|
|
|
auto jarray = Prop::NativeArray::Adopt(mEnv, mEnv->NewObjectArray(
|
|
|
|
length,
|
|
|
|
jni::Accessor::EnsureClassRef<typename Prop::ClassType>(mEnv),
|
|
|
|
nullptr));
|
2014-05-17 02:25:29 +04:00
|
|
|
if (!jarray) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
|
|
|
|
// For object arrays, we have to set each element separately.
|
2014-05-17 02:25:29 +04:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2015-08-05 00:47:28 +03:00
|
|
|
JS::RootedValue elem(mJSContext);
|
|
|
|
if (!CheckJSCall(JS_GetElement(mJSContext, array, i, &elem)) ||
|
|
|
|
!CheckProperty(Prop::InValue, elem)) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
mEnv->SetObjectArrayElement(
|
|
|
|
jarray.Get(), i, (this->*Prop::FromValue)(elem).Get());
|
|
|
|
if (mEnv->ExceptionCheck()) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return jarray;
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class Class,
|
|
|
|
bool (Self::*InValue_)(JS::HandleValue) const,
|
|
|
|
typename Class::LocalRef (Self::*FromValue_)(JS::HandleValue)>
|
|
|
|
struct BaseObjectProperty
|
|
|
|
{
|
|
|
|
// JNI class for the object type (e.g. jni::String)
|
|
|
|
typedef Class ClassType;
|
|
|
|
|
|
|
|
// See comments in PrimitiveProperty.
|
|
|
|
typedef typename ClassType::LocalRef NativeType;
|
|
|
|
typedef const typename ClassType::Ref NativeFallback;
|
|
|
|
typedef typename jni::ObjectArray::LocalRef NativeArray;
|
|
|
|
typedef const jni::ObjectArray::Ref ArrayFallback;
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
typedef decltype(InValue_) InValue_t;
|
|
|
|
static constexpr InValue_t InValue = InValue_;
|
|
|
|
|
|
|
|
typedef decltype(FromValue_) FromValue_t;
|
|
|
|
static constexpr FromValue_t FromValue = FromValue_;
|
|
|
|
|
|
|
|
typedef decltype(&Self::ObjectNewArray<BaseObjectProperty>) NewArray_t;
|
|
|
|
static constexpr NewArray_t NewArray
|
|
|
|
= &Self::ObjectNewArray<BaseObjectProperty>;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Array properties
|
|
|
|
|
|
|
|
template<class Prop> bool
|
|
|
|
ArrayInValue(JS::HandleValue val) const
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
if (!val.isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
JS::RootedObject obj(mJSContext, &val.toObject());
|
2015-08-29 07:55:40 +03:00
|
|
|
bool isArray;
|
2014-05-17 02:25:29 +04:00
|
|
|
uint32_t length = 0;
|
2015-08-29 07:55:40 +03:00
|
|
|
if (!JS_IsArrayObject(mJSContext, obj, &isArray) ||
|
|
|
|
!isArray ||
|
2015-08-05 00:47:28 +03:00
|
|
|
!JS_GetArrayLength(mJSContext, obj, &length)) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!length) {
|
|
|
|
// Empty arrays are always okay.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// We only check to see the first element is the target type. If the
|
|
|
|
// array has mixed types, we'll throw an error during actual conversion.
|
2015-08-05 00:47:28 +03:00
|
|
|
JS::RootedValue element(mJSContext);
|
|
|
|
return JS_GetElement(mJSContext, obj, 0, &element) &&
|
|
|
|
(this->*Prop::InValue)(element);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class Prop> typename Prop::NativeArray
|
|
|
|
ArrayFromValue(JS::HandleValue val)
|
|
|
|
{
|
|
|
|
JS::RootedObject obj(mJSContext, &val.toObject());
|
2014-05-17 02:25:29 +04:00
|
|
|
uint32_t length = 0;
|
2015-08-05 00:47:28 +03:00
|
|
|
if (!CheckJSCall(JS_GetArrayLength(mJSContext, obj, &length))) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return (this->*Prop::NewArray)(obj, length);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class Prop>
|
|
|
|
struct ArrayProperty
|
|
|
|
{
|
|
|
|
// See comments in PrimitiveProperty.
|
|
|
|
typedef typename Prop::NativeArray NativeType;
|
|
|
|
typedef typename Prop::ArrayFallback NativeFallback;
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
typedef decltype(&Self::ArrayInValue<Prop>) InValue_t;
|
|
|
|
static constexpr InValue_t InValue
|
|
|
|
= &Self::ArrayInValue<Prop>;
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
typedef decltype(&Self::ArrayFromValue<Prop>) FromValue_t;
|
|
|
|
static constexpr FromValue_t FromValue
|
|
|
|
= &Self::ArrayFromValue<Prop>;
|
|
|
|
};
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// "Has" property is a special property type that is used to implement
|
|
|
|
// NativeJSObject.has, by returning true from InValue and FromValue for
|
|
|
|
// every existing property, and having false as the fallback value for
|
|
|
|
// when a property doesn't exist.
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool HasValue(JS::HandleValue val) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
struct HasProperty
|
|
|
|
{
|
|
|
|
// See comments in PrimitiveProperty.
|
|
|
|
typedef bool NativeType;
|
|
|
|
typedef bool NativeFallback;
|
|
|
|
|
|
|
|
typedef decltype(&Self::HasValue) HasValue_t;
|
|
|
|
static constexpr HasValue_t InValue = &Self::HasValue;
|
|
|
|
static constexpr HasValue_t FromValue = &Self::HasValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Statically cast from bool to jboolean (unsigned char); it works
|
|
|
|
// since false and JNI_FALSE have the same value (0), and true and
|
|
|
|
// JNI_TRUE have the same value (1).
|
|
|
|
typedef PrimitiveProperty<
|
|
|
|
bool, jni::BooleanArray, jboolean, jbooleanArray,
|
|
|
|
&JS::Value::isBoolean, &JS::Value::toBoolean,
|
|
|
|
&JNIEnv::NewBooleanArray, &JNIEnv::SetBooleanArrayRegion>
|
|
|
|
BooleanProperty;
|
|
|
|
|
|
|
|
typedef PrimitiveProperty<
|
|
|
|
double, jni::DoubleArray, jdouble, jdoubleArray,
|
|
|
|
&JS::Value::isNumber, &JS::Value::toNumber,
|
|
|
|
&JNIEnv::NewDoubleArray, &JNIEnv::SetDoubleArrayRegion>
|
|
|
|
DoubleProperty;
|
|
|
|
|
|
|
|
typedef PrimitiveProperty<
|
|
|
|
int32_t, jni::IntArray, jint, jintArray,
|
|
|
|
&JS::Value::isInt32, &JS::Value::toInt32,
|
|
|
|
&JNIEnv::NewIntArray, &JNIEnv::SetIntArrayRegion>
|
|
|
|
IntProperty;
|
|
|
|
|
|
|
|
typedef BaseObjectProperty<
|
|
|
|
jni::String, &Self::StringInValue, &Self::StringFromValue>
|
|
|
|
StringProperty;
|
|
|
|
|
|
|
|
typedef BaseObjectProperty<
|
|
|
|
sdk::Bundle, &Self::ObjectInValue, &Self::BundleFromValue>
|
|
|
|
BundleProperty;
|
|
|
|
|
|
|
|
typedef BaseObjectProperty<
|
|
|
|
NativeJSObject, &Self::ObjectInValue, &Self::ObjectFromValue>
|
|
|
|
ObjectProperty;
|
|
|
|
|
|
|
|
typedef ArrayProperty<BooleanProperty> BooleanArrayProperty;
|
|
|
|
typedef ArrayProperty<DoubleProperty> DoubleArrayProperty;
|
|
|
|
typedef ArrayProperty<IntProperty> IntArrayProperty;
|
|
|
|
typedef ArrayProperty<StringProperty> StringArrayProperty;
|
|
|
|
typedef ArrayProperty<BundleProperty> BundleArrayProperty;
|
|
|
|
typedef ArrayProperty<ObjectProperty> ObjectArrayProperty;
|
|
|
|
|
|
|
|
template<class Prop>
|
|
|
|
typename Prop::NativeType
|
|
|
|
GetProperty(jni::String::Param name,
|
|
|
|
typename Prop::NativeFallback* fallback = nullptr)
|
|
|
|
{
|
|
|
|
if (!CheckThread() || !CheckObject()) {
|
|
|
|
return typename Prop::NativeType();
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
const JSJNIString nameStr(mEnv, name);
|
|
|
|
JS::RootedValue val(mJSContext);
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
if (!CheckJNIArgument(name) ||
|
|
|
|
!CheckJSCall(JS_GetUCProperty(
|
|
|
|
mJSContext, mJSObject, nameStr, nameStr.Length(), &val))) {
|
|
|
|
return typename Prop::NativeType();
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Strictly, null is different from undefined in JS. However, in
|
|
|
|
// practice, null is often used to indicate a property doesn't exist in
|
|
|
|
// the same manner as undefined. Therefore, we treat null in the same
|
|
|
|
// way as undefined when checking property existence (bug 1014965).
|
|
|
|
if (val.isUndefined() || val.isNull()) {
|
|
|
|
if (fallback) {
|
|
|
|
return mozilla::Move(*fallback);
|
|
|
|
}
|
|
|
|
jni::ThrowException(
|
2014-08-06 22:38:29 +04:00
|
|
|
"org/mozilla/gecko/util/NativeJSObject$InvalidPropertyException",
|
2014-04-01 23:16:56 +04:00
|
|
|
"Property does not exist");
|
2015-08-05 00:47:28 +03:00
|
|
|
return typename Prop::NativeType();
|
2014-04-01 23:16:56 +04:00
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
|
|
|
|
if (!CheckProperty(Prop::InValue, val)) {
|
|
|
|
return typename Prop::NativeType();
|
|
|
|
}
|
|
|
|
return (this->*Prop::FromValue)(val);
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSObject::LocalRef CreateChild(JS::HandleObject object)
|
|
|
|
{
|
|
|
|
auto instance = NativeJSObject::New();
|
|
|
|
mozilla::UniquePtr<NativeJSContainerImpl> impl(
|
|
|
|
new NativeJSContainerImpl(instance.Env(), mJSContext, object));
|
|
|
|
|
|
|
|
ObjectBase::AttachNative(instance, mozilla::Move(impl));
|
2016-01-15 00:12:13 +03:00
|
|
|
if (!mChildren.append(NativeJSObject::GlobalRef(instance))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSContainerImpl(JNIEnv* env, JSContext* cx, JS::HandleObject object)
|
|
|
|
: mEnv(env)
|
|
|
|
, mJSContext(cx)
|
|
|
|
, mJSObject(cx, object)
|
|
|
|
{}
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
public:
|
2015-08-05 00:47:28 +03:00
|
|
|
~NativeJSContainerImpl()
|
|
|
|
{
|
|
|
|
// Dispose of all children on destruction. The children will in turn
|
|
|
|
// dispose any of their children (i.e. our grandchildren) and so on.
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSObject::LocalRef child(mEnv);
|
2015-08-05 00:47:28 +03:00
|
|
|
for (size_t i = 0; i < mChildren.length(); i++) {
|
2015-08-05 00:47:28 +03:00
|
|
|
child = mChildren[i];
|
|
|
|
ObjectBase::GetNative(child)->ObjectBase::DisposeNative(child);
|
2015-08-05 00:47:28 +03:00
|
|
|
}
|
2014-05-12 20:50:47 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
static NativeJSContainer::LocalRef
|
|
|
|
CreateInstance(JSContext* cx, JS::HandleObject object)
|
|
|
|
{
|
|
|
|
auto instance = NativeJSContainer::New();
|
2015-08-05 00:47:28 +03:00
|
|
|
mozilla::UniquePtr<NativeJSContainerImpl> impl(
|
|
|
|
new NativeJSContainerImpl(instance.Env(), cx, object));
|
|
|
|
|
|
|
|
ContainerBase::AttachNative(instance, mozilla::Move(impl));
|
2015-08-05 00:47:28 +03:00
|
|
|
return instance;
|
|
|
|
}
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// NativeJSContainer methods
|
|
|
|
|
|
|
|
void DisposeNative(const NativeJSContainer::LocalRef& instance)
|
|
|
|
{
|
|
|
|
if (!CheckThread()) {
|
|
|
|
return;
|
2014-05-12 20:50:47 +04:00
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
ContainerBase::DisposeNative(instance);
|
|
|
|
}
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSContainer::LocalRef Clone()
|
|
|
|
{
|
|
|
|
if (!CheckThread()) {
|
2014-05-12 20:50:47 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 00:47:28 +03:00
|
|
|
return CreateInstance(mJSContext, mJSObject);
|
|
|
|
}
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// NativeJSObject methods
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool GetBoolean(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<BooleanProperty>(name);
|
|
|
|
}
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool OptBoolean(jni::String::Param name, bool fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<BooleanProperty>(name, &fallback);
|
2014-05-12 20:50:47 +04:00
|
|
|
}
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::BooleanArray::LocalRef
|
|
|
|
GetBooleanArray(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<BooleanArrayProperty>(name);
|
|
|
|
}
|
2014-04-01 23:16:54 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::BooleanArray::LocalRef
|
|
|
|
OptBooleanArray(jni::String::Param name, jni::BooleanArray::Param fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<BooleanArrayProperty>(name, &fallback);
|
|
|
|
}
|
2014-04-01 23:16:52 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::Object::LocalRef
|
|
|
|
GetBundle(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<BundleProperty>(name);
|
|
|
|
}
|
2014-04-01 23:16:52 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::Object::LocalRef
|
|
|
|
OptBundle(jni::String::Param name, jni::Object::Param fallback)
|
|
|
|
{
|
|
|
|
// Because the GetProperty expects a sdk::Bundle::Param,
|
|
|
|
// we have to do conversions here from jni::Object::Param.
|
|
|
|
const auto& fb = sdk::Bundle::Ref::From(fallback.Get());
|
|
|
|
return GetProperty<BundleProperty>(name, &fb);
|
|
|
|
}
|
2014-04-01 23:16:53 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::ObjectArray::LocalRef
|
|
|
|
GetBundleArray(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<BundleArrayProperty>(name);
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::ObjectArray::LocalRef
|
|
|
|
OptBundleArray(jni::String::Param name, jni::ObjectArray::Param fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<BundleArrayProperty>(name, &fallback);
|
|
|
|
}
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
double GetDouble(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<DoubleProperty>(name);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
double OptDouble(jni::String::Param name, double fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<DoubleProperty>(name, &fallback);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::DoubleArray::LocalRef
|
|
|
|
GetDoubleArray(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<DoubleArrayProperty>(name);
|
|
|
|
}
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::DoubleArray::LocalRef
|
|
|
|
OptDoubleArray(jni::String::Param name, jni::DoubleArray::Param fallback)
|
|
|
|
{
|
|
|
|
jni::DoubleArray::LocalRef fb(fallback);
|
|
|
|
return GetProperty<DoubleArrayProperty>(name, &fb);
|
|
|
|
}
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
int GetInt(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<IntProperty>(name);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
int OptInt(jni::String::Param name, int fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<IntProperty>(name, &fallback);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::IntArray::LocalRef
|
|
|
|
GetIntArray(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<IntArrayProperty>(name);
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::IntArray::LocalRef
|
|
|
|
OptIntArray(jni::String::Param name, jni::IntArray::Param fallback)
|
|
|
|
{
|
|
|
|
jni::IntArray::LocalRef fb(fallback);
|
|
|
|
return GetProperty<IntArrayProperty>(name, &fb);
|
|
|
|
}
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSObject::LocalRef
|
|
|
|
GetObject(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<ObjectProperty>(name);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
NativeJSObject::LocalRef
|
|
|
|
OptObject(jni::String::Param name, NativeJSObject::Param fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<ObjectProperty>(name, &fallback);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::ObjectArray::LocalRef
|
|
|
|
GetObjectArray(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<ObjectArrayProperty>(name);
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::ObjectArray::LocalRef
|
|
|
|
OptObjectArray(jni::String::Param name, jni::ObjectArray::Param fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<ObjectArrayProperty>(name, &fallback);
|
|
|
|
}
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::String::LocalRef
|
|
|
|
GetString(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<StringProperty>(name);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::String::LocalRef
|
|
|
|
OptString(jni::String::Param name, jni::String::Param fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<StringProperty>(name, &fallback);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::ObjectArray::LocalRef
|
|
|
|
GetStringArray(jni::String::Param name)
|
|
|
|
{
|
|
|
|
return GetProperty<StringArrayProperty>(name);
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::ObjectArray::LocalRef
|
|
|
|
OptStringArray(jni::String::Param name, jni::ObjectArray::Param fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<StringArrayProperty>(name, &fallback);
|
|
|
|
}
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
bool Has(jni::String::Param name)
|
|
|
|
{
|
|
|
|
bool no = false;
|
|
|
|
// Fallback to false indicating no such property.
|
|
|
|
return GetProperty<HasProperty>(name, &no);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
jni::Object::LocalRef ToBundle()
|
|
|
|
{
|
|
|
|
if (!CheckThread() || !CheckObject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return BundleFromValue(mJSObject);
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
private:
|
|
|
|
static bool AppendJSON(const char16_t* buf, uint32_t len, void* data)
|
|
|
|
{
|
|
|
|
static_cast<nsAutoString*>(data)->Append(buf, len);
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
public:
|
|
|
|
jni::String::LocalRef ToString()
|
|
|
|
{
|
|
|
|
if (!CheckThread() || !CheckObject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
JS::RootedValue value(mJSContext, JS::ObjectValue(*mJSObject));
|
|
|
|
nsAutoString json;
|
|
|
|
if (!CheckJSCall(JS_Stringify(mJSContext, &value, nullptr,
|
|
|
|
JS::NullHandleValue, AppendJSON, &json))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return jni::Param<jni::String>(json, mEnv);
|
|
|
|
}
|
|
|
|
};
|
2014-05-17 02:25:29 +04:00
|
|
|
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
// Define the "static constexpr" members of our property types (e.g.
|
|
|
|
// PrimitiveProperty<>::InValue). This is tricky because there are a lot of
|
|
|
|
// template parameters, so we use macros to make it simpler.
|
2014-04-01 23:16:55 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#define DEFINE_PRIMITIVE_PROPERTY_MEMBER(Name) \
|
|
|
|
template<typename U, typename UA, typename V, typename VA, \
|
|
|
|
bool (JS::Value::*I)() const, \
|
|
|
|
U (JS::Value::*T)() const, \
|
|
|
|
VA (JNIEnv::*N)(jsize), \
|
|
|
|
void (JNIEnv::*S)(VA, jsize, jsize, const V*)> \
|
|
|
|
constexpr typename NativeJSContainerImpl \
|
|
|
|
::PrimitiveProperty<U, UA, V, VA, I, T, N, S>::Name##_t \
|
|
|
|
NativeJSContainerImpl::PrimitiveProperty<U, UA, V, VA, I, T, N, S>::Name
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
DEFINE_PRIMITIVE_PROPERTY_MEMBER(NewJNIArray);
|
|
|
|
DEFINE_PRIMITIVE_PROPERTY_MEMBER(SetJNIArrayRegion);
|
|
|
|
DEFINE_PRIMITIVE_PROPERTY_MEMBER(InValue);
|
|
|
|
DEFINE_PRIMITIVE_PROPERTY_MEMBER(FromValue);
|
|
|
|
DEFINE_PRIMITIVE_PROPERTY_MEMBER(NewArray);
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#undef DEFINE_PRIMITIVE_PROPERTY_MEMBER
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#define DEFINE_OBJECT_PROPERTY_MEMBER(Name) \
|
|
|
|
template<class C, \
|
|
|
|
bool (NativeJSContainerImpl::*I)(JS::HandleValue) const, \
|
|
|
|
typename C::LocalRef (NativeJSContainerImpl::*F)(JS::HandleValue)> \
|
|
|
|
constexpr typename NativeJSContainerImpl \
|
|
|
|
::BaseObjectProperty<C, I, F>::Name##_t \
|
|
|
|
NativeJSContainerImpl::BaseObjectProperty<C, I, F>::Name
|
2014-04-01 23:16:54 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
DEFINE_OBJECT_PROPERTY_MEMBER(InValue);
|
|
|
|
DEFINE_OBJECT_PROPERTY_MEMBER(FromValue);
|
|
|
|
DEFINE_OBJECT_PROPERTY_MEMBER(NewArray);
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
#undef DEFINE_OBJECT_PROPERTY_MEMBER
|
2014-04-01 23:16:54 +04:00
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
template<class P> constexpr typename NativeJSContainerImpl::ArrayProperty<P>
|
|
|
|
::InValue_t NativeJSContainerImpl::ArrayProperty<P>::InValue;
|
|
|
|
template<class P> constexpr typename NativeJSContainerImpl::ArrayProperty<P>
|
|
|
|
::FromValue_t NativeJSContainerImpl::ArrayProperty<P>::FromValue;
|
|
|
|
|
|
|
|
constexpr NativeJSContainerImpl::HasProperty::HasValue_t
|
|
|
|
NativeJSContainerImpl::HasProperty::InValue;
|
|
|
|
constexpr NativeJSContainerImpl::HasProperty::HasValue_t
|
|
|
|
NativeJSContainerImpl::HasProperty::FromValue;
|
|
|
|
|
|
|
|
|
|
|
|
NativeJSContainer::LocalRef
|
|
|
|
CreateNativeJSContainer(JSContext* cx, JS::HandleObject object)
|
|
|
|
{
|
|
|
|
return NativeJSContainerImpl::CreateInstance(cx, object);
|
2014-04-01 23:16:54 +04:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:47:28 +03:00
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|
|
|
|
|