Skip to content

Commit 9f34ffd

Browse files
committed
fix c++ warnings
1 parent 3c35905 commit 9f34ffd

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

.vscode/c_cpp_properties.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"/usr/local/include/node",
8+
"/usr/lib/jvm/java-17-openjdk-amd64/include/",
9+
"/usr/lib/jvm/java-17-openjdk-amd64/include/linux"
10+
],
11+
"defines": [],
12+
"compilerPath": "/usr/bin/gcc",
13+
"cStandard": "c17",
14+
"cppStandard": "gnu++17",
15+
"intelliSenseMode": "linux-gcc-x64"
16+
}
17+
],
18+
"version": 4
19+
}

src/utils.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
jobject v8ToJava_javaObject(JNIEnv* env, v8::Local<v8::Object> obj);
1212
jobject v8ToJava_javaLong(JNIEnv* env, v8::Local<v8::Object> obj);
1313

14+
bool hasSetFailed(Nan::Maybe<bool> v) {
15+
if (v.IsNothing()) {
16+
return false;
17+
}
18+
return v.ToChecked() == false;
19+
}
20+
1421
void javaReflectionGetMethods(JNIEnv *env, jclass clazz, std::list<jobject>* methods, bool includeStatic) {
1522
jclass clazzclazz = env->FindClass("java/lang/Class");
1623
jmethodID clazz_getMethods = env->GetMethodID(clazzclazz, "getMethods", "()[Ljava/lang/reflect/Method;");
@@ -452,7 +459,9 @@ v8::Local<v8::Value> javaExceptionToV8(Java* java, JNIEnv* env, jthrowable ex, c
452459
msg << "\n" << javaExceptionToString(env, ex);
453460

454461
v8::Local<v8::Value> v8ex = v8::Exception::Error(Nan::New<v8::String>(msg.str().c_str()).ToLocalChecked());
455-
((v8::Object*)*v8ex)->Set(Nan::GetCurrentContext(), Nan::New<v8::String>("cause").ToLocalChecked(), javaToV8(java, env, ex));
462+
if (hasSetFailed(((v8::Object*)*v8ex)->Set(Nan::GetCurrentContext(), Nan::New<v8::String>("cause").ToLocalChecked(), javaToV8(java, env, ex)))) {
463+
return v8::Exception::Error(Nan::New<v8::String>("could not set cause").ToLocalChecked());
464+
}
456465
return v8ex;
457466
}
458467

@@ -576,7 +585,9 @@ v8::Local<v8::Value> javaArrayToV8(Java* java, JNIEnv* env, jobjectArray objArra
576585
{
577586
jboolean* elems = env->GetBooleanArrayElements((jbooleanArray)objArray, 0);
578587
for(jsize i=0; i<arraySize; i++) {
579-
result->Set(Nan::GetCurrentContext(), i, Nan::New<v8::Boolean>(elems[i]));
588+
if(hasSetFailed(result->Set(Nan::GetCurrentContext(), i, Nan::New<v8::Boolean>(elems[i])))) {
589+
return v8::Exception::Error(Nan::New<v8::String>("set array element failed").ToLocalChecked());
590+
}
580591
}
581592
env->ReleaseBooleanArrayElements((jbooleanArray)objArray, elems, 0);
582593
}
@@ -638,7 +649,9 @@ v8::Local<v8::Value> javaArrayToV8(Java* java, JNIEnv* env, jobjectArray objArra
638649
jlong* elems = env->GetLongArrayElements((jlongArray)objArray, 0);
639650
for(jsize i=0; i<arraySize; i++) {
640651
jobject obj = longToJavaLongObj(env, elems[i]);
641-
result->Set(Nan::GetCurrentContext(), i, JavaObject::New(java, obj));
652+
if(hasSetFailed(result->Set(Nan::GetCurrentContext(), i, JavaObject::New(java, obj)))) {
653+
return v8::Exception::Error(Nan::New<v8::String>("could not set array element").ToLocalChecked());
654+
}
642655
}
643656
env->ReleaseLongArrayElements((jlongArray)objArray, elems, 0);
644657
}
@@ -648,7 +661,9 @@ v8::Local<v8::Value> javaArrayToV8(Java* java, JNIEnv* env, jobjectArray objArra
648661
for(jsize i=0; i<arraySize; i++) {
649662
jobject obj = env->GetObjectArrayElement(objArray, i);
650663
v8::Local<v8::Value> item = javaToV8(java, env, obj);
651-
result->Set(Nan::GetCurrentContext(), i, item);
664+
if(hasSetFailed(result->Set(Nan::GetCurrentContext(), i, item))) {
665+
return v8::Exception::Error(Nan::New<v8::String>("could not set array element").ToLocalChecked());
666+
}
652667
}
653668
break;
654669
}
@@ -711,7 +726,9 @@ v8::Local<v8::Value> javaToV8(Java* java, JNIEnv* env, jobject obj, DynamicProxy
711726
std::string strValue = javaObjectToString(env, obj);
712727
v8::Local<v8::Value> v8Result = Nan::New<v8::NumberObject>((double)result);
713728
v8::NumberObject* v8ResultNumberObject = v8::NumberObject::Cast(*v8Result);
714-
v8ResultNumberObject->Set(Nan::GetCurrentContext(), Nan::New<v8::String>("longValue").ToLocalChecked(), Nan::New<v8::String>(strValue.c_str()).ToLocalChecked());
729+
if(hasSetFailed(v8ResultNumberObject->Set(Nan::GetCurrentContext(), Nan::New<v8::String>("longValue").ToLocalChecked(), Nan::New<v8::String>(strValue.c_str()).ToLocalChecked()))) {
730+
return v8::Exception::Error(Nan::New<v8::String>("could not set longValue").ToLocalChecked());
731+
}
715732
SetHiddenValue(v8ResultNumberObject, Nan::New<v8::String>(V8_HIDDEN_MARKER_JAVA_LONG).ToLocalChecked(), Nan::New<v8::Boolean>(true));
716733
return v8Result;
717734
}

0 commit comments

Comments
 (0)