github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/jvm/gojvm_c.c (about) 1 #include "gojvm_c.h" 2 3 bool hasPrefix(const char *str, const char *sub) { 4 return strncmp(str, sub, strlen(sub)) == 0; 5 } 6 7 char* getSubType(char* type) { 8 size_t len = strlen(type); 9 char* s = (char*)malloc(len); 10 int inType = 0; 11 int iidx = 0; 12 for (int i = 0; i < len; i++) { 13 if (type[i] == '<') { 14 inType = 1; 15 continue; 16 } 17 if (type[i] == '>') break; 18 if (inType) s[iidx++] = type[i]; 19 } 20 return s; 21 } 22 23 char* getRealSig(char* sig) { 24 size_t len = strlen(sig); 25 char* s = (char*) malloc(len); 26 int inType = 0; 27 int iidx = 0; 28 for (int i = 0; i < len; i++) { 29 if (sig[i] == '<') { 30 inType = 1; 31 continue; 32 } 33 if (sig[i] == '>') { 34 inType = 0; 35 continue; 36 } 37 if (!inType) { 38 s[iidx++] = sig[i]; 39 } 40 } 41 return s; 42 } 43 44 jvalue* makeParams(JNIEnv* env, int len, char** types, void** args) { 45 jvalue *v = malloc(sizeof(jvalue) * len); 46 for (int i = 0; i < len; i++) { 47 if (strcmp(types[i], "Ljava/lang/String;") == 0) { 48 v[i].l = (*env)->NewStringUTF(env, (char*)args[i]); 49 } else if (strcmp(types[i], "I") == 0) { 50 v[i].i = *((int*)args[i]); 51 } else if (strcmp(types[i], "J") == 0) { 52 v[i].j = *((long*)args[i]); 53 } else if (strcmp(types[i], "F") == 0) { 54 v[i].f = *((float*)args[i]); 55 } else if (strcmp(types[i], "D") == 0) { 56 v[i].d = *((double*)args[i]); 57 } else if (strcmp(types[i], "B") == 0) { 58 v[i].b = *((unsigned char*)args[i]); 59 } else if (strcmp(types[i], "S") == 0) { 60 v[i].s = *((short*)args[i]); 61 } else if (strcmp(types[i], "Z") == 0) { 62 int bi = *((int*)args[i]); 63 v[i].z = bi == 0 ? JNI_FALSE : JNI_TRUE; 64 } 65 } 66 return v; 67 } 68 69 void freeParams(JNIEnv* env, int len, char** types, jvalue* v) { 70 for (int i = 0; i < len; i++) { 71 if (strcmp(types[i], "Ljava/lang/String;") == 0) { 72 (*env)->DeleteLocalRef(env, v[i].l); 73 } 74 } 75 free(v); 76 } 77 78 _GO_EXPORT JavaVM* createJvm(char* classPath, char* xms, char* xmx, char* xmn, char* xss) { 79 JavaVM* jvm; 80 JNIEnv* env; 81 JavaVMInitArgs vm_args; 82 JavaVMOption options[5]; 83 84 options[0].optionString = (char*)malloc(strlen("-Djava.class.path=") + strlen(classPath) + 1); 85 sprintf(options[0].optionString, "-Djava.class.path=%s", classPath); 86 options[1].optionString = (char*)malloc(strlen("-Xms") + strlen(xms) + 1); 87 sprintf(options[1].optionString, "-Xms%s", xms); 88 options[2].optionString = (char*)malloc(strlen("-Xmx") + strlen(xmx) + 1); 89 sprintf(options[2].optionString, "-Xmx%s", xmx); 90 options[3].optionString = (char*)malloc(strlen("-Xmn") + strlen(xmn) + 1); 91 sprintf(options[3].optionString, "-Xmn%s", xmn); 92 options[4].optionString = (char*)malloc(strlen("-Xss") + strlen(xss) + 1); 93 sprintf(options[4].optionString, "-Xss%s", xss); 94 95 vm_args.version = JNI_VERSION_1_8; 96 vm_args.nOptions = 5; 97 vm_args.options = options; 98 vm_args.ignoreUnrecognized = JNI_FALSE; 99 100 jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); 101 if (res < 0) { 102 printf("create jvm failed\n"); 103 return NULL; 104 } 105 (*jvm)->DetachCurrentThread(jvm); 106 return jvm; 107 } 108 109 _GO_EXPORT int destroyJvm(JavaVM* jvm) { 110 jint res = (*jvm)->DestroyJavaVM(jvm); 111 if (res < 0) { 112 printf("destroy jvm failed\n"); 113 return 1; 114 } 115 return 0; 116 } 117 118 119 _GO_EXPORT JNIEnv* attachJvm(JavaVM* jvm) { 120 JNIEnv* env; 121 jint res = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL); 122 if (res < 0) { 123 printf("attach jvm failed\n"); 124 return NULL; 125 } 126 return env; 127 } 128 129 _GO_EXPORT void detachJvm(JavaVM* jvm) { 130 (*jvm)->DetachCurrentThread(jvm); 131 } 132 133 _GO_EXPORT jclass findClass(JNIEnv* env, char* className) { 134 jclass cls = (*env)->FindClass(env, className); 135 if (cls == NULL) { 136 printf("find class failed\n"); 137 return NULL; 138 } 139 return cls; 140 } 141 142 _GO_EXPORT jclass getObjectClass(JNIEnv* env, jobject obj) { 143 return (*env)->GetObjectClass(env, obj); 144 } 145 146 _GO_EXPORT jobject newJavaObject(JNIEnv* env, jclass clazz) { 147 jmethodID m = (*env)->GetMethodID(env, clazz, "<init>", "()V"); 148 jobject jret = (*env)->NewObject(env, clazz, m); 149 return jret; 150 } 151 152 _GO_EXPORT void freeJavaObject(JNIEnv* env, jobject obj) { 153 (*env)->DeleteLocalRef(env, obj); 154 } 155 156 _GO_EXPORT void freeJavaClassRef(JNIEnv* env, jclass clz) { 157 (*env)->DeleteLocalRef(env, clz); 158 } 159 160 _GO_EXPORT WRAP_STATIC_VOID_METHOD(Void) 161 _GO_EXPORT WRAP_STATIC_METHOD(Object, jobject, jobject) 162 _GO_EXPORT WRAP_STATIC_STRING_METHOD(String) 163 _GO_EXPORT WRAP_STATIC_METHOD(Int, int, jint) 164 _GO_EXPORT WRAP_STATIC_METHOD(Long, long, jlong) 165 _GO_EXPORT WRAP_STATIC_METHOD(Short, short, jshort) 166 _GO_EXPORT WRAP_STATIC_METHOD(Byte, unsigned char, jbyte) 167 _GO_EXPORT WRAP_STATIC_METHOD(Float, float, jfloat) 168 _GO_EXPORT WRAP_STATIC_METHOD(Double, double, jdouble) 169 _GO_EXPORT WRAP_STATIC_METHOD(Boolean, int, jboolean) 170 171 _GO_EXPORT WRAP_STATIC_FIELD_GET(Object, jobject, jobject) 172 _GO_EXPORT WRAP_STATIC_FIELD_SET(Object, jobject) 173 _GO_EXPORT WRAP_STATIC_FIELD_GET_STRING(String) 174 _GO_EXPORT WRAP_STATIC_FIELD_SET_STRING(String) 175 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Int, int, jint, "I") 176 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Int, int, jint, "I") 177 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Long, long, jlong, "J") 178 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Long, long, jlong, "J") 179 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Short, short, jshort , "S") 180 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Short, short, jshort, "S") 181 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Byte, unsigned char, jbyte , "B") 182 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Byte, unsigned char, jbyte, "B") 183 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Float, float, jfloat, "F") 184 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Float, float, jfloat, "F") 185 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Double, double, jdouble, "D") 186 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Double, double, jdouble, "D") 187 _GO_EXPORT WRAP_STATIC_FIELD_GET_SIG(Boolean, int, jboolean , "Z") 188 _GO_EXPORT WRAP_STATIC_FIELD_SET_SIG(Boolean, int, jboolean, "Z") 189 190 _GO_EXPORT WRAP_VOID_METHOD(Void) 191 _GO_EXPORT WRAP_METHOD(Object, jobject, jobject) 192 _GO_EXPORT WRAP_STRING_METHOD(String) 193 _GO_EXPORT WRAP_METHOD(Int, int, jint) 194 _GO_EXPORT WRAP_METHOD(Long, long, jlong) 195 _GO_EXPORT WRAP_METHOD(Short, short, jshort) 196 _GO_EXPORT WRAP_METHOD(Byte, unsigned char, jbyte) 197 _GO_EXPORT WRAP_METHOD(Float, float, jfloat) 198 _GO_EXPORT WRAP_METHOD(Double, double, jdouble) 199 _GO_EXPORT WRAP_METHOD(Boolean, int, jboolean) 200 201 _GO_EXPORT WRAP_FIELD_GET(Object, jobject, jobject) 202 _GO_EXPORT WRAP_FIELD_SET(Object, jobject) 203 _GO_EXPORT WRAP_FIELD_GET_STRING(String) 204 _GO_EXPORT WRAP_FIELD_SET_STRING(String) 205 _GO_EXPORT WRAP_FIELD_GET_SIG(Int, int, jint, "I") 206 _GO_EXPORT WRAP_FIELD_SET_SIG(Int, int, jint, "I") 207 _GO_EXPORT WRAP_FIELD_GET_SIG(Long, long, jlong, "J") 208 _GO_EXPORT WRAP_FIELD_SET_SIG(Long, long, jlong, "J") 209 _GO_EXPORT WRAP_FIELD_GET_SIG(Short, short, jshort , "S") 210 _GO_EXPORT WRAP_FIELD_SET_SIG(Short, short, jshort, "S") 211 _GO_EXPORT WRAP_FIELD_GET_SIG(Byte, unsigned char, jbyte , "B") 212 _GO_EXPORT WRAP_FIELD_SET_SIG(Byte, unsigned char, jbyte, "B") 213 _GO_EXPORT WRAP_FIELD_GET_SIG(Float, float, jfloat, "F") 214 _GO_EXPORT WRAP_FIELD_SET_SIG(Float, float, jfloat, "F") 215 _GO_EXPORT WRAP_FIELD_GET_SIG(Double, double, jdouble, "D") 216 _GO_EXPORT WRAP_FIELD_SET_SIG(Double, double, jdouble, "D") 217 _GO_EXPORT WRAP_FIELD_GET_SIG(Boolean, int, jboolean , "Z") 218 _GO_EXPORT WRAP_FIELD_SET_SIG(Boolean, int, jboolean, "Z")