| 51 | } |
| 52 | |
| 53 | void ArrayHelper::CreateJavaArray(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 54 | auto isolate = info.GetIsolate(); |
| 55 | auto context = isolate->GetCurrentContext(); |
| 56 | |
| 57 | if (info.Length() != 2) { |
| 58 | Throw(isolate, "Expect two parameters."); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | auto type = info[0]; |
| 63 | auto length = info[1]; |
| 64 | |
| 65 | JniLocalRef array; |
| 66 | |
| 67 | auto runtime = Runtime::GetRuntime(isolate); |
| 68 | auto objectManager = runtime->GetObjectManager(); |
| 69 | |
| 70 | if (type->IsString()) { |
| 71 | if (!length->IsInt32()) { |
| 72 | Throw(isolate, "Expect integer value as a second argument."); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | jint len = length->Int32Value(context).FromJust(); |
| 77 | if (len < 0) { |
| 78 | Throw(isolate, "Expect non-negative integer value as a second argument."); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | auto typeName = ArgConverter::ConvertToString(type.As<String>()); |
| 83 | array = JniLocalRef(CreateArrayByClassName(typeName, len)); |
| 84 | } else if (type->IsFunction()) { |
| 85 | if (!length->IsInt32()) { |
| 86 | Throw(isolate, "Expect integer value as a second argument."); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | jint len = length->Int32Value(context).FromJust(); |
| 91 | if (len < 0) { |
| 92 | Throw(isolate, "Expect non-negative integer value as a second argument."); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | auto func = type.As<Function>(); |
| 97 | |
| 98 | Local<Value> classVal; |
| 99 | func->Get(context, ArgConverter::ConvertToV8String(isolate, "class")).ToLocal(&classVal); |
| 100 | if (classVal.IsEmpty()) { |
| 101 | Throw(isolate, "Expect known class as a second argument."); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | auto clazz = classVal.As<Object>(); |
| 106 | auto c = objectManager->GetJavaObjectByJsObject(clazz.As<Object>()); |
| 107 | |
| 108 | JEnv env; |
| 109 | array = env.NewObjectArray(len, static_cast<jclass>(c), nullptr); |
| 110 | } else { |
nothing calls this directly
no test coverage detected