📄 jsj_convert.c
字号:
jsj_UnexpectedJavaError(cx, jEnv, "Couldn't construct instance " "of java.lang.Boolean"); return JS_FALSE; } } return JS_TRUE; } /* Fall through, to attempt conversion to a java.lang.String ... */ } /* If the source JS type is either a string or undefined, or if no conversion is possible from a number, boolean or JS object, see if the target type is java.lang.String */ if ((*jEnv)->IsAssignableFrom(jEnv, jlString, target_java_class)) { /* Convert to JS string, if necessary, and then to a Java Unicode string */ jsstr = JS_ValueToString(cx, v); if (jsstr) { if (java_value) { *java_value = jsj_ConvertJSStringToJavaString(cx, jEnv, jsstr); if (*java_value) { *is_local_refp = JS_TRUE; } else { return JS_FALSE; } } return JS_TRUE; } } conversion_error: return JS_FALSE;}/* Valid ranges for Java numeric types */#define jbyte_MAX_VALUE 127.0#define jbyte_MIN_VALUE -128.0#define jchar_MAX_VALUE 65535.0#define jchar_MIN_VALUE 0.0#define jshort_MAX_VALUE 32767.0#define jshort_MIN_VALUE -32768.0#define jint_MAX_VALUE 2147483647.0#define jint_MIN_VALUE -2147483648.0#define jlong_MAX_VALUE 9223372036854775807.0#define jlong_MIN_VALUE -9223372036854775808.0/* Utility macro for jsj_ConvertJSValueToJavaValue(), below */#define JSVAL_TO_INTEGRAL_JVALUE(type_name, member_name, member_type, jsval, java_value) \ if (!JSVAL_IS_NUMBER(v)) { \ if (!JS_ConvertValue(cx, v, JSTYPE_NUMBER, &v)) \ goto conversion_error; \ (*cost)++; \ } \ { \ member_type member_name; \ \ if (JSVAL_IS_INT(v)) { \ jsint ival = JSVAL_TO_INT(v); \ member_name = (member_type) ival; \ \ /* Check to see if the jsval's magnitude is too large to be \ representable in the target java type */ \ if (member_name != ival) \ goto numeric_conversion_error; \ } else { \ jdouble dval = *JSVAL_TO_DOUBLE(v); \ \ /* NaN becomes zero when converted to integral value */ \ if (JSDOUBLE_IS_NaN(dval)) \ goto numeric_conversion_error; \ \ /* Unrepresentably large numbers, including infinities, */ \ /* cause an error. */ \ else if ((dval >= member_type ## _MAX_VALUE + 1) || \ (dval <= member_type ## _MIN_VALUE - 1)) { \ goto numeric_conversion_error; \ } else \ member_name = (member_type) dval; \ \ /* Don't allow a non-integral number to be converted \ to an integral type */ \ /* Actually, we have to allow this for LC1 compatibility */ \ /* if ((jdouble)member_name != dval) \ (*cost)++; */ \ } \ if (java_value) \ java_value->member_name = member_name; \ }#ifdef XP_OS2/* OS2 utility macro for jsj_ConvertJSValueToJavaValue(), below *//* jlong is a structure, see jri_md.h, where the jlong_ macros are defined. */#define JSVAL_TO_JLONG_JVALUE(member_name, member_type, jsvalue, java_value) \ if (!JSVAL_IS_NUMBER(jsvalue)) { \ if (!JS_ConvertValue(cx, jsvalue, JSTYPE_NUMBER, &jsvalue)) \ goto conversion_error; \ (*cost)++; \ } \ { \ member_type member_name; \ \ if (JSVAL_IS_INT(jsvalue)) { \ jsint ival = JSVAL_TO_INT(jsvalue); \ jlong_I2L(member_name,ival); \ \ } else { \ jdouble dval = *JSVAL_TO_DOUBLE(jsvalue); \ \ /* NaN becomes zero when converted to integral value */ \ if (JSDOUBLE_IS_NaN(dval)) \ jlong_I2L(member_name,0); \ \ /* Unrepresentably large numbers, including infinities, */ \ /* cause an error. */ \ else if ((dval > member_type ## _MAX_VALUE) || \ (dval < member_type ## _MIN_VALUE)) { \ goto numeric_conversion_error; \ } else \ jlong_D2L(member_name,dval); \ \ /* Don't allow a non-integral number to be converted \ to an integral type */ \ /* Actually, we have to allow this for LC1 compatibility */ \ /*if (jlong_to_jdouble(member_name) != dval) \ (*cost)++;*/ \ } \ if (java_value) \ java_value->member_name = member_name; \ }static jdouble jlong_to_jdouble(jlong lvalue){ jdouble d; jlong_L2D(d,lvalue); return d;}#else#define jlong_to_jdouble(lvalue) ((jdouble) lvalue)#endif/* * Convert a JS value to a Java value of the given type signature. The cost * variable is incremented if coercion is required, e.g. the source value is * a string, but the target type is a boolean. * * Returns JS_FALSE if no conversion is possible, either because the jsval has * a type that is wholly incompatible with the Java value, or because a scalar * jsval can't be represented in a variable of the target type without loss of * precision, e.g. the source value is "4.2" but the destination type is byte. * If conversion is not possible and java_value is non-NULL, the JS error * reporter is called with an appropriate message. */ JSBooljsj_ConvertJSValueToJavaValue(JSContext *cx, JNIEnv *jEnv, jsval v_arg, JavaSignature *signature, int *cost, jvalue *java_value, JSBool *is_local_refp){ JavaSignatureChar type; jsval v; JSBool success = JS_FALSE; /* Initialize to default case, in which no new Java object is synthesized to perform the conversion and, therefore, no JNI local references are being held. */ *is_local_refp = JS_FALSE; type = signature->type; v = v_arg; switch (type) { case JAVA_SIGNATURE_BOOLEAN: if (!JSVAL_IS_BOOLEAN(v)) { if (!JS_ConvertValue(cx, v, JSTYPE_BOOLEAN, &v)) goto conversion_error; if (JSVAL_IS_VOID(v)) goto conversion_error; (*cost)++; } if (java_value) java_value->z = (jboolean)(JSVAL_TO_BOOLEAN(v) == JS_TRUE); break; case JAVA_SIGNATURE_SHORT: JSVAL_TO_INTEGRAL_JVALUE(short, s, jshort, v, java_value); break; case JAVA_SIGNATURE_BYTE: JSVAL_TO_INTEGRAL_JVALUE(byte, b, jbyte, v, java_value); break; case JAVA_SIGNATURE_CHAR: /* A one-character string can be converted into a character */ if (JSVAL_IS_STRING(v) && (JS_GetStringLength(JSVAL_TO_STRING(v)) == 1)) { v = INT_TO_JSVAL(*JS_GetStringChars(JSVAL_TO_STRING(v))); } JSVAL_TO_INTEGRAL_JVALUE(char, c, jchar, v, java_value); break; case JAVA_SIGNATURE_INT: JSVAL_TO_INTEGRAL_JVALUE(int, i, jint, v, java_value); break; case JAVA_SIGNATURE_LONG:#if (defined(XP_OS2) && !defined(HAVE_LONG_LONG)) JSVAL_TO_JLONG_JVALUE(j, jlong, v, java_value);#else JSVAL_TO_INTEGRAL_JVALUE(long, j, jlong, v, java_value);#endif break; case JAVA_SIGNATURE_FLOAT: if (!JSVAL_IS_NUMBER(v)) { if (!JS_ConvertValue(cx, v, JSTYPE_NUMBER, &v)) goto conversion_error; (*cost)++; } if (java_value) { if (JSVAL_IS_INT(v)) java_value->f = (jfloat) JSVAL_TO_INT(v); else java_value->f = (jfloat) *JSVAL_TO_DOUBLE(v); } break; case JAVA_SIGNATURE_DOUBLE: if (!JSVAL_IS_NUMBER(v)) { if (!JS_ConvertValue(cx, v, JSTYPE_NUMBER, &v)) goto conversion_error; (*cost)++; } if (java_value) { if (JSVAL_IS_INT(v)) java_value->d = (jdouble) JSVAL_TO_INT(v); else java_value->d = (jdouble) *JSVAL_TO_DOUBLE(v); } break; /* Non-primitive (reference) type */ default: JS_ASSERT(IS_REFERENCE_TYPE(type)); if (!jsj_ConvertJSValueToJavaObject(cx, jEnv, v, signature, cost, &java_value->l, is_local_refp)) goto conversion_error; break; case JAVA_SIGNATURE_UNKNOWN: case JAVA_SIGNATURE_VOID: JS_ASSERT(0); return JS_FALSE; } /* Success */ return JS_TRUE;numeric_conversion_error: success = JS_TRUE; /* Fall through ... */conversion_error: if (java_value) { const char *jsval_string; const char *class_name; JSString *jsstr; jsval_string = NULL; jsstr = JS_ValueToString(cx, v_arg); if (jsstr) jsval_string = JS_GetStringBytes(jsstr); if (!jsval_string) jsval_string = ""; class_name = jsj_ConvertJavaSignatureToHRString(cx, signature); JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, JSJMSG_CANT_CONVERT_JS, jsval_string, class_name); return JS_FALSE; } return success;}/* * A utility routine to create a JavaScript Unicode string from a * java.lang.String (Unicode) string. */JSString *jsj_ConvertJavaStringToJSString(JSContext *cx, JNIEnv *jEnv, jstring java_str){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -