|
9 | 9 | #include "Util.h" |
10 | 10 | #include "V8GlobalHelpers.h" |
11 | 11 | #include "V8StringConstants.h" |
| 12 | +#include "NumericCasts.h" |
| 13 | +#include "NativeScriptException.h" |
12 | 14 |
|
13 | 15 | using namespace v8; |
14 | 16 | using namespace std; |
@@ -110,113 +112,120 @@ bool JsArgConverter::ConvertArg(const Local<Value>& arg, int index) |
110 | 112 | jlong javaLongValue; |
111 | 113 | auto jsObject = arg->ToObject(); |
112 | 114 |
|
113 | | - auto hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsLong()); |
114 | | - if (!hidden.IsEmpty()) |
115 | | - { |
116 | | - if (hidden->IsString()) |
117 | | - { |
118 | | - string strValue = ConvertToString(hidden->ToString()); |
119 | | - int64_t longArg = atoll(strValue.c_str()); |
120 | | - jlong value = (jlong) longArg; |
121 | | - success = ConvertFromCastFunctionObject(value, index); |
122 | | - } |
123 | | - else if (hidden->IsInt32()) |
124 | | - { |
125 | | - jlong value = (jlong) hidden->ToInt32()->IntegerValue(); |
126 | | - success = ConvertFromCastFunctionObject(value, index); |
127 | | - } |
128 | | - return success; |
129 | | - } |
130 | | - else if (ArgConverter::TryConvertToJavaLong(jsObject, javaLongValue)) |
131 | | - { |
132 | | - m_args[index].j = javaLongValue; |
133 | | - success = true; |
134 | | - return success; |
135 | | - } |
| 115 | + auto castType = NumericCasts::GetCastType(jsObject); |
136 | 116 |
|
137 | | - hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsByte()); |
138 | | - if (!hidden.IsEmpty()) |
139 | | - { |
140 | | - if (hidden->IsString()) |
141 | | - { |
142 | | - string strValue = ConvertToString(hidden->ToString()); |
143 | | - int byteArg = atoi(strValue.c_str()); |
144 | | - jbyte value = (jbyte) byteArg; |
145 | | - success = ConvertFromCastFunctionObject(value, index); |
146 | | - } |
147 | | - else if (hidden->IsInt32()) |
148 | | - { |
149 | | - jbyte value = (jbyte) hidden->ToInt32()->Int32Value(); |
150 | | - success = ConvertFromCastFunctionObject(value, index); |
151 | | - } |
152 | | - return success; |
153 | | - } |
| 117 | + Local<Value> castValue; |
| 118 | + JniLocalRef obj; |
154 | 119 |
|
155 | | - hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsShort()); |
156 | | - if (!hidden.IsEmpty()) |
| 120 | + switch (castType) |
157 | 121 | { |
158 | | - if (hidden->IsString()) |
159 | | - { |
160 | | - string strValue = ConvertToString(hidden->ToString()); |
161 | | - int shortArg = atoi(strValue.c_str()); |
162 | | - jshort value = (jshort) shortArg; |
163 | | - success = ConvertFromCastFunctionObject(value, index); |
164 | | - } |
165 | | - else if (hidden->IsInt32()) |
166 | | - { |
167 | | - jshort value = (jshort) hidden->ToInt32()->Int32Value(); |
168 | | - success = ConvertFromCastFunctionObject(value, index); |
169 | | - } |
170 | | - return success; |
171 | | - } |
| 122 | + case CastType::Char: |
| 123 | + castValue = NumericCasts::GetCastValue(jsObject); |
| 124 | + if (castValue->IsString()) |
| 125 | + { |
| 126 | + string value = ConvertToString(castValue->ToString()); |
| 127 | + m_args[index].c = (jchar) value[0]; |
| 128 | + success = true; |
| 129 | + } |
| 130 | + break; |
172 | 131 |
|
173 | | - hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsDouble()); |
174 | | - if (!hidden.IsEmpty()) |
175 | | - { |
176 | | - if (hidden->IsNumber()) |
177 | | - { |
178 | | - double doubleArg = hidden->ToNumber()->NumberValue(); |
179 | | - jdouble value = (jdouble) doubleArg; |
180 | | - success = ConvertFromCastFunctionObject(value, index); |
181 | | - } |
182 | | - return success; |
183 | | - } |
| 132 | + case CastType::Byte: |
| 133 | + castValue = NumericCasts::GetCastValue(jsObject); |
| 134 | + if (castValue->IsString()) |
| 135 | + { |
| 136 | + string strValue = ConvertToString(castValue->ToString()); |
| 137 | + int byteArg = atoi(strValue.c_str()); |
| 138 | + jbyte value = (jbyte) byteArg; |
| 139 | + success = ConvertFromCastFunctionObject(value, index); |
| 140 | + } |
| 141 | + else if (castValue->IsInt32()) |
| 142 | + { |
| 143 | + jbyte value = (jbyte) castValue->ToInt32()->Int32Value(); |
| 144 | + success = ConvertFromCastFunctionObject(value, index); |
| 145 | + } |
| 146 | + break; |
184 | 147 |
|
185 | | - hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsFloat()); |
186 | | - if (!hidden.IsEmpty()) |
187 | | - { |
188 | | - if (hidden->IsNumber()) |
189 | | - { |
190 | | - double floatArg = hidden->ToNumber()->NumberValue(); |
191 | | - jfloat value = (jfloat) floatArg; |
192 | | - success = ConvertFromCastFunctionObject(value, index); |
193 | | - } |
194 | | - return success; |
195 | | - } |
| 148 | + case CastType::Short: |
| 149 | + castValue = NumericCasts::GetCastValue(jsObject); |
| 150 | + if (castValue->IsString()) |
| 151 | + { |
| 152 | + string strValue = ConvertToString(castValue->ToString()); |
| 153 | + int shortArg = atoi(strValue.c_str()); |
| 154 | + jshort value = (jshort) shortArg; |
| 155 | + success = ConvertFromCastFunctionObject(value, index); |
| 156 | + } |
| 157 | + else if (castValue->IsInt32()) |
| 158 | + { |
| 159 | + jshort value = (jshort) castValue->ToInt32()->Int32Value(); |
| 160 | + success = ConvertFromCastFunctionObject(value, index); |
| 161 | + } |
| 162 | + break; |
196 | 163 |
|
197 | | - hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsChar()); |
198 | | - if (!hidden.IsEmpty()) |
199 | | - { |
200 | | - if (hidden->IsString()) |
201 | | - { |
202 | | - string value = ConvertToString(hidden->ToString()); |
203 | | - m_args[index].c = (jchar) value[0]; |
204 | | - success = true; |
205 | | - } |
206 | | - return success; |
207 | | - } |
| 164 | + case CastType::Long: |
| 165 | + castValue = NumericCasts::GetCastValue(jsObject); |
| 166 | + if (castValue->IsString()) |
| 167 | + { |
| 168 | + string strValue = ConvertToString(castValue->ToString()); |
| 169 | + int64_t longArg = atoll(strValue.c_str()); |
| 170 | + jlong value = (jlong) longArg; |
| 171 | + success = ConvertFromCastFunctionObject(value, index); |
| 172 | + } |
| 173 | + else if (castValue->IsInt32()) |
| 174 | + { |
| 175 | + jlong value = (jlong) castValue->ToInt32()->IntegerValue(); |
| 176 | + success = ConvertFromCastFunctionObject(value, index); |
| 177 | + } |
| 178 | + break; |
| 179 | + |
| 180 | + case CastType::Float: |
| 181 | + castValue = NumericCasts::GetCastValue(jsObject); |
| 182 | + if (castValue->IsNumber()) |
| 183 | + { |
| 184 | + double floatArg = castValue->ToNumber()->NumberValue(); |
| 185 | + jfloat value = (jfloat) floatArg; |
| 186 | + success = ConvertFromCastFunctionObject(value, index); |
| 187 | + } |
| 188 | + break; |
208 | 189 |
|
209 | | - auto obj = ObjectManager::GetJavaObjectByJsObjectStatic(jsObject); |
210 | | - success = !obj.IsNull(); |
| 190 | + case CastType::Double: |
| 191 | + castValue = NumericCasts::GetCastValue(jsObject); |
| 192 | + if (castValue->IsNumber()) |
| 193 | + { |
| 194 | + double doubleArg = castValue->ToNumber()->NumberValue(); |
| 195 | + jdouble value = (jdouble) doubleArg; |
| 196 | + success = ConvertFromCastFunctionObject(value, index); |
| 197 | + } |
| 198 | + break; |
211 | 199 |
|
212 | | - if (success) |
213 | | - { |
214 | | - SetConvertedObject(index, obj.Move(), obj.IsGlobal()); |
215 | | - } |
216 | | - else |
217 | | - { |
218 | | - sprintf(buff, "Cannot convert object to %s at index %d", typeSignature.c_str(), index); |
| 200 | + |
| 201 | + case CastType::None: |
| 202 | + obj = ObjectManager::GetJavaObjectByJsObjectStatic(jsObject); |
| 203 | + success = !obj.IsNull(); |
| 204 | + |
| 205 | + if (success) |
| 206 | + { |
| 207 | + SetConvertedObject(index, obj.Move(), obj.IsGlobal()); |
| 208 | + } |
| 209 | + else |
| 210 | + { |
| 211 | + sprintf(buff, "Cannot convert object to %s at index %d", typeSignature.c_str(), index); |
| 212 | + } |
| 213 | + break; |
| 214 | + |
| 215 | + default: |
| 216 | + throw NativeScriptException("Unsupported cast type"); |
219 | 217 | } |
| 218 | + |
| 219 | +// auto hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsLong()); |
| 220 | +// if (!hidden.IsEmpty()) |
| 221 | +// { |
| 222 | +// } |
| 223 | +// else if (ArgConverter::TryConvertToJavaLong(jsObject, javaLongValue)) |
| 224 | +// { |
| 225 | +// m_args[index].j = javaLongValue; |
| 226 | +// success = true; |
| 227 | +// return success; |
| 228 | +// } |
220 | 229 | } |
221 | 230 | else if (arg->IsUndefined() || arg->IsNull()) |
222 | 231 | { |
|
0 commit comments