MCPcopy Create free account
hub / github.com/NativeScript/android / ConvertArg

Method ConvertArg

test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp:61–401  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59}
60
61bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value> &arg, int index) {
62 bool success = false;
63 stringstream s;
64
65 JEnv env;
66
67 Type returnType = JType::getClassType(m_return_type);
68 auto isolate = context->GetIsolate();
69
70 if (arg.IsEmpty()) {
71 s << "Cannot convert empty JavaScript object";
72 success = false;
73 } else if (arg->IsInt32() && (returnType == Type::Int || returnType == Type::Null)) {
74 jint value = arg->Int32Value(context).ToChecked();
75 auto javaObject = JType::NewInt(env, value);
76 SetConvertedObject(env, index, javaObject);
77
78 success = true;
79 } else if (arg->IsNumber() || arg->IsNumberObject()) {
80 double d = arg->NumberValue(context).ToChecked();
81 int64_t i = (int64_t) d;
82
83 //if returnType isNumber and this check is true, the number we'll try to convert is whole(integer)
84 bool isWholeNumber = d == i;
85
86 if (isWholeNumber) {
87 jobject obj;
88
89 //if returnType is long it will cast to long
90 //if there is no return type specified it will cast to int
91 //because default return type is null (ref type)
92 if ((INT_MIN <= i) && (i <= INT_MAX) &&
93 (returnType == Type::Int || returnType == Type::Null)) {
94 obj = JType::NewInt(env, (jint) d);
95 } else { /*isLong*/
96 obj = JType::NewLong(env, (jlong) d);
97 }
98
99 SetConvertedObject(env, index, obj);
100
101 success = true;
102 } else {
103 jobject obj;
104
105 //if returnType is double it will cast to double
106 //if there is no return type specified it will cast to float
107 //because default return type is null (ref type)
108 if ((FLT_MIN <= d) && (d <= FLT_MAX) &&
109 (returnType == Type::Float || returnType == Type::Null)) {
110 obj = JType::NewFloat(env, (jfloat) d);
111 } else {
112 /*isDouble*/
113 obj = JType::NewDouble(env, (jdouble) d);
114 }
115
116 SetConvertedObject(env, index, obj);
117
118 success = true;

Callers

nothing calls this directly

Calls 15

GetCastTypeFunction · 0.85
ToCheckedMethod · 0.80
BooleanValueMethod · 0.80
IsStringMethod · 0.80
ToLocalCheckedMethod · 0.80
GetObjectManagerMethod · 0.80
NewDirectByteBufferMethod · 0.80
GetObjectClassMethod · 0.80
GetMethodIDMethod · 0.80
GetStaticMethodIDMethod · 0.80

Tested by

no test coverage detected