| 59 | } |
| 60 | |
| 61 | bool 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; |
nothing calls this directly
no test coverage detected