⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsj_convert.c

📁 caffeine-monkey java实现的js模拟引擎
💻 C
📖 第 1 页 / 共 3 页
字号:
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Communicator client code, released * March 31, 1998. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** *//* This file is part of the Java-vendor-neutral implementation of LiveConnect * * Below is the code that converts between Java and JavaScript values of all * types. */#include <stdlib.h>#include <string.h>#ifdef OJI#include "prtypes.h"          /* Need platform-dependent definition of HAVE_LONG_LONG                                 for non-standard jri_md.h file */#endif#include "jsj_private.h"      /* LiveConnect internals *//* Floating-point double utilities, stolen from jsnum.h */#ifdef IS_LITTLE_ENDIAN#define JSDOUBLE_HI32(x)        (((uint32 *)&(x))[1])#define JSDOUBLE_LO32(x)        (((uint32 *)&(x))[0])#else#define JSDOUBLE_HI32(x)        (((uint32 *)&(x))[0])#define JSDOUBLE_LO32(x)        (((uint32 *)&(x))[1])#endif#define JSDOUBLE_HI32_SIGNBIT   0x80000000#define JSDOUBLE_HI32_EXPMASK   0x7ff00000#define JSDOUBLE_HI32_MANTMASK  0x000fffff#define JSDOUBLE_IS_NaN(x)                                                    \    ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK &&   \     (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK)))#define JSDOUBLE_IS_INFINITE(x)                                               \    ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK &&  \     !JSDOUBLE_LO32(x))static JSBoolconvert_js_obj_to_JSObject_wrapper(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj,                                   JavaSignature *signature,                                   int *cost, jobject *java_value){    if (!njJSObject) {        if (java_value)            JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL,                                                 JSJMSG_CANT_LOAD_JSOBJECT);        return JS_FALSE;    }    if (!(*jEnv)->IsAssignableFrom(jEnv, njJSObject, signature->java_class))        return JS_FALSE;    if (!java_value)        return JS_TRUE;    *java_value = jsj_WrapJSObject(cx, jEnv, js_obj);    return (*java_value != NULL);   }/* Copy an array from JS to Java;  Create a new Java array and populate its   elements, one by one, with the result of converting each JS array element   to the type of the array component. */static JSBoolconvert_js_array_to_java_array(JSContext *cx, JNIEnv *jEnv, JSObject *js_array,                               JavaSignature *signature,                               jobject *java_valuep){    jsuint i;    jsval js_val;    jsuint length;    jclass component_class;    jarray java_array;    JavaSignature *array_component_signature;    if (!JS_GetArrayLength(cx, js_array, &length))        return JS_FALSE;    /* Get the Java class of each element of the array */    array_component_signature = signature->array_component_signature;    component_class = array_component_signature->java_class;    /* Create a new empty Java array with the same length as the JS array */    java_array = (*jEnv)->CallStaticObjectMethod(jEnv, jlrArray, jlrArray_newInstance,                                                 component_class, length);    if (!java_array) {        jsj_ReportJavaError(cx, jEnv, "Error while constructing empty array of %s",                            jsj_GetJavaClassName(cx, jEnv, component_class));        return JS_FALSE;    }    /* Convert each element of the JS array to an element of the Java array.       If an error occurs, there is no need to worry about releasing the       individual elements of the Java array - they will eventually be GC'ed       by the JVM. */    for (i = 0; i < length; i++) {        if (!JS_LookupElement(cx, js_array, i, &js_val))            return JS_FALSE;        if (!jsj_SetJavaArrayElement(cx, jEnv, java_array, i, array_component_signature, js_val))            return JS_FALSE;    }    /* Return the result array */    *java_valuep = java_array;    return JS_TRUE;}jstringjsj_ConvertJSStringToJavaString(JSContext *cx, JNIEnv *jEnv, JSString *js_str){    jstring result;    result = (*jEnv)->NewString(jEnv, JS_GetStringChars(js_str),                                JS_GetStringLength(js_str));    if (!result) {        jsj_UnexpectedJavaError(cx, jEnv, "Couldn't construct instance "                                          "of java.lang.String");    }    return result;}/* * Convert a JS value to an instance of java.lang.Object or one of its subclasses,  * performing any necessary type coercion.  If non-trivial coercion is required, * the cost value is incremented.  If the java_value pass-by-reference argument * is non-NULL, the resulting Java value is stored there. * * Returns JS_TRUE if the conversion is possible, JS_FALSE otherwise */JSBooljsj_ConvertJSValueToJavaObject(JSContext *cx, JNIEnv *jEnv, jsval v, JavaSignature *signature,                               int *cost, jobject *java_value, JSBool *is_local_refp){    JSString *jsstr;    jclass target_java_class;        JS_ASSERT(IS_REFERENCE_TYPE(signature->type));    /* 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;    /* Get the Java type of the target value */    target_java_class = signature->java_class;        if (JSVAL_IS_OBJECT(v)) {        JSObject *js_obj = JSVAL_TO_OBJECT(v);                /* JS null is always assignable to a Java object */        if (!js_obj) {            if (java_value)                *java_value = NULL;            return JS_TRUE;        }                if (JS_InstanceOf(cx, js_obj, &JavaObject_class, 0) ||            JS_InstanceOf(cx, js_obj, &JavaArray_class, 0)) {                        /* The source value is a Java object wrapped inside a JavaScript               object.  Unwrap the JS object and return the original Java               object if it's class makes it assignment-compatible with the               target class using Java's assignability rules. */            JavaObjectWrapper *java_wrapper = JS_GetPrivate(cx, js_obj);            jobject java_obj = java_wrapper->java_obj;                        if ((*jEnv)->IsInstanceOf(jEnv, java_obj, target_java_class)) {                if (java_value)                    *java_value = java_obj;                return JS_TRUE;            }                        /* Fall through, to attempt conversion to a Java string */                    } else if (JS_InstanceOf(cx, js_obj, &JavaClass_class, 0)) {            /* We're dealing with the reflection of a Java class */            JavaClassDescriptor *java_class_descriptor = JS_GetPrivate(cx, js_obj);                        /* Check if target type is java.lang.Class class */            if ((*jEnv)->IsAssignableFrom(jEnv, jlClass, target_java_class)) {                if (java_value)                    *java_value = java_class_descriptor->java_class;                return JS_TRUE;            }                        /* Check if target type is netscape.javascript.JSObject wrapper class */            if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value)) {                if (java_value && *java_value)                    *is_local_refp = JS_TRUE;                return JS_TRUE;            }                        /* Fall through, to attempt conversion to a Java string */                    } else if (JS_InstanceOf(cx, js_obj, &JavaMember_class, 0)) {            if (!JS_ConvertValue(cx, v, JSTYPE_OBJECT, &v))                return JS_FALSE;            return jsj_ConvertJSValueToJavaObject(cx, jEnv, v, signature, cost,                                                  java_value, is_local_refp);        /* JS Arrays are converted, element by element, to Java arrays */        } else if (JS_IsArrayObject(cx, js_obj) && (signature->type == JAVA_SIGNATURE_ARRAY)) {            if (convert_js_array_to_java_array(cx, jEnv, js_obj, signature, java_value)) {                if (java_value && *java_value)                    *is_local_refp = JS_TRUE;                return JS_TRUE;            }            return JS_FALSE;        } else {            /* Otherwise, see if the target type is the  netscape.javascript.JSObject               wrapper class or one of its subclasses, in which case a               reference is passed to the original JS object by wrapping it               inside an instance of netscape.javascript.JSObject */            if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value))             {                if (java_value && *java_value)                    *is_local_refp = JS_TRUE;                return JS_TRUE;            }                        /* Fall through, to attempt conversion to a Java string */        }            } else if (JSVAL_IS_NUMBER(v)) {        /* JS numbers, integral or not, can be converted to instances of java.lang.Double */        if ((*jEnv)->IsAssignableFrom(jEnv, jlDouble, target_java_class)) {            if (java_value) {                jsdouble d;                if (!JS_ValueToNumber(cx, v, &d))                    goto conversion_error;                *java_value = (*jEnv)->NewObject(jEnv, jlDouble, jlDouble_Double, d);                if (*java_value) {                    *is_local_refp = JS_TRUE;                } else {                    jsj_UnexpectedJavaError(cx, jEnv,                        "Couldn't construct instance of java.lang.Double");                    return JS_FALSE;                }            }            return JS_TRUE;        }        /* Fall through, to attempt conversion to a java.lang.String ... */            } else if (JSVAL_IS_BOOLEAN(v)) {        /* JS boolean values can be converted to instances of java.lang.Boolean */        if ((*jEnv)->IsAssignableFrom(jEnv, jlBoolean, target_java_class)) {            if (java_value) {                JSBool b;                if (!JS_ValueToBoolean(cx, v, &b))                    goto conversion_error;                *java_value =                    (*jEnv)->NewObject(jEnv, jlBoolean, jlBoolean_Boolean, b);                if (*java_value) {                    *is_local_refp = JS_TRUE;                } else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -