13
13
import com .facebook .proguard .annotations .DoNotStrip ;
14
14
15
15
import java .util .ArrayList ;
16
+ import java .util .Arrays ;
17
+ import com .facebook .infer .annotation .Assertions ;
18
+ import javax .annotation .Nullable ;
16
19
17
20
/**
18
21
* Implementation of a NativeArray that allows read-only access to its members. This will generally
@@ -28,24 +31,142 @@ protected ReadableNativeArray(HybridData hybridData) {
28
31
super (hybridData );
29
32
}
30
33
34
+ //WriteOnce but not in the constructor fields
35
+ private @ Nullable Object [] mLocalArray ;
36
+ private @ Nullable ReadableType [] mLocalTypeArray ;
37
+
38
+ private static int jniPassCounter = 0 ;
39
+ private static boolean mUseNativeAccessor = false ;
40
+ public static void setUseNativeAccessor (boolean useNativeAccessor ) {
41
+ mUseNativeAccessor = useNativeAccessor ;
42
+ }
43
+ public static int getJNIPassCounter () {
44
+ return jniPassCounter ;
45
+ }
46
+
47
+ private Object [] getLocalArray () {
48
+ // Fast, non blocking check for the common case
49
+ if (mLocalArray != null ) {
50
+ return mLocalArray ;
51
+ }
52
+ synchronized (this ) {
53
+ // Make sure no concurrent call already updated
54
+ if (mLocalArray == null ) {
55
+ jniPassCounter ++;
56
+ mLocalArray = Assertions .assertNotNull (importArray ());
57
+ }
58
+ }
59
+ return mLocalArray ;
60
+ }
61
+ private native Object [] importArray ();
62
+
63
+ private ReadableType [] getLocalTypeArray () {
64
+ // Fast, non-blocking check for the common case
65
+ if (mLocalTypeArray != null ) {
66
+ return mLocalTypeArray ;
67
+ }
68
+ synchronized (this ) {
69
+ // Make sure no concurrent call already updated
70
+ if (mLocalTypeArray == null ) {
71
+ jniPassCounter ++;
72
+ Object [] tempArray = Assertions .assertNotNull (importTypeArray ());
73
+ mLocalTypeArray = Arrays .copyOf (tempArray , tempArray .length , ReadableType [].class );
74
+ }
75
+ }
76
+ return mLocalTypeArray ;
77
+ }
78
+ private native Object [] importTypeArray ();
79
+
31
80
@ Override
32
- public native int size ();
81
+ public int size () {
82
+ if (mUseNativeAccessor ) {
83
+ jniPassCounter ++;
84
+ return sizeNative ();
85
+ }
86
+ return getLocalArray ().length ;
87
+ }
88
+ private native int sizeNative ();
89
+
33
90
@ Override
34
- public native boolean isNull (int index );
91
+ public boolean isNull (int index ) {
92
+ if (mUseNativeAccessor ) {
93
+ jniPassCounter ++;
94
+ return isNullNative (index );
95
+ }
96
+ return getLocalArray ()[index ] == null ;
97
+ }
98
+ private native boolean isNullNative (int index );
99
+
35
100
@ Override
36
- public native boolean getBoolean (int index );
101
+ public boolean getBoolean (int index ) {
102
+ if (mUseNativeAccessor ) {
103
+ jniPassCounter ++;
104
+ return getBooleanNative (index );
105
+ }
106
+ return ((Boolean ) getLocalArray ()[index ]).booleanValue ();
107
+ }
108
+ private native boolean getBooleanNative (int index );
109
+
37
110
@ Override
38
- public native double getDouble (int index );
111
+ public double getDouble (int index ) {
112
+ if (mUseNativeAccessor ) {
113
+ jniPassCounter ++;
114
+ return getDoubleNative (index );
115
+ }
116
+ return ((Double ) getLocalArray ()[index ]).doubleValue ();
117
+ }
118
+ private native double getDoubleNative (int index );
119
+
39
120
@ Override
40
- public native int getInt (int index );
121
+ public int getInt (int index ) {
122
+ if (mUseNativeAccessor ) {
123
+ jniPassCounter ++;
124
+ return getIntNative (index );
125
+ }
126
+ return ((Double ) getLocalArray ()[index ]).intValue ();
127
+ }
128
+ private native int getIntNative (int index );
129
+
41
130
@ Override
42
- public native String getString (int index );
131
+ public String getString (int index ) {
132
+ if (mUseNativeAccessor ) {
133
+ jniPassCounter ++;
134
+ return getStringNative (index );
135
+ }
136
+ return (String ) getLocalArray ()[index ];
137
+ }
138
+ private native String getStringNative (int index );
139
+
43
140
@ Override
44
- public native ReadableNativeArray getArray (int index );
141
+ public ReadableNativeArray getArray (int index ) {
142
+ if (mUseNativeAccessor ) {
143
+ jniPassCounter ++;
144
+ return getArrayNative (index );
145
+ }
146
+ return (ReadableNativeArray ) getLocalArray ()[index ];
147
+ }
148
+ private native ReadableNativeArray getArrayNative (int index );
149
+
45
150
@ Override
46
- public native ReadableNativeMap getMap (int index );
151
+ public ReadableNativeMap getMap (int index ) {
152
+ if (mUseNativeAccessor ) {
153
+ jniPassCounter ++;
154
+ return getMapNative (index );
155
+ }
156
+ return (ReadableNativeMap ) getLocalArray ()[index ];
157
+ }
158
+ private native ReadableNativeMap getMapNative (int index );
159
+
47
160
@ Override
48
- public native ReadableType getType (int index );
161
+ public ReadableType getType (int index ) {
162
+ if (mUseNativeAccessor ) {
163
+ jniPassCounter ++;
164
+ return getTypeNative (index );
165
+ }
166
+ return getLocalTypeArray ()[index ];
167
+ }
168
+
169
+ private native ReadableType getTypeNative (int index );
49
170
50
171
@ Override
51
172
public Dynamic getDynamic (int index ) {
0 commit comments