Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
jsj_JavaArray.c File Reference
#include <stdlib.h>
#include <string.h>
#include "jsj_private.h"

Go to the source code of this file.

Defines

#define JS7_ISDEC(c)   (((c) >= '0') && ((c) <= '9'))
#define JS7_UNDEC(c)   ((c) - '0')

Functions

static jsval try_convert_to_jsint (JSContext *cx, jsval idval)
static JSBool access_java_array_element (JSContext *cx, JNIEnv *jEnv, JSObject *obj, jsid id, jsval *vp, JSBool do_assignment)
 JavaArray_getPropertyById (JSContext *cx, JSObject *obj, jsval id, jsval *vp)
 JavaArray_setPropertyById (JSContext *cx, JSObject *obj, jsval id, jsval *vp)
 JavaArray_lookupProperty (JSContext *cx, JSObject *obj, jsid id, JSObject **objp, JSProperty **propp)
 JavaArray_defineProperty (JSContext *cx, JSObject *obj, jsid id, jsval value, JSPropertyOp getter, JSPropertyOp setter, uintN attrs, JSProperty **propp)
 JavaArray_getAttributes (JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp)
 JavaArray_setAttributes (JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp)
 JavaArray_deleteProperty (JSContext *cx, JSObject *obj, jsid id, jsval *vp)
 JavaArray_defaultValue (JSContext *cx, JSObject *obj, JSType type, jsval *vp)
 JavaArray_newEnumerate (JSContext *cx, JSObject *obj, JSIterateOp enum_op, jsval *statep, jsid *idp)
 JavaArray_checkAccess (JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode, jsval *vp, uintN *attrsp)
 JavaArray_getObjectOps (JSContext *cx, JSClass *clazz)
 JS_IMPORT_DATA (JSObjectOps)

Variables

JSObjectOps JavaArray_ops
JSClass JavaArray_class

Define Documentation

#define JS7_ISDEC (   c)    (((c) >= '0') && ((c) <= '9'))

Definition at line 53 of file jsj_JavaArray.c.

#define JS7_UNDEC (   c)    ((c) - '0')

Definition at line 54 of file jsj_JavaArray.c.


Function Documentation

static JSBool access_java_array_element ( JSContext cx,
JNIEnv jEnv,
JSObject obj,
jsid  id,
jsval vp,
JSBool  do_assignment 
) [static]

Definition at line 95 of file jsj_JavaArray.c.

{
    jsval idval;
    jarray java_array;
    JavaClassDescriptor *class_descriptor;
    JavaObjectWrapper *java_wrapper;
    jsize array_length, index;
    JavaSignature *array_component_signature;
    
    /* printf("In JavaArray_getProperty\n"); */
    
    java_wrapper = JS_GetPrivate(cx, obj);
    if (!java_wrapper) {
        const char *property_name;
        if (JS_IdToValue(cx, id, &idval) && JSVAL_IS_STRING(idval) &&
            (property_name = JS_GetStringBytes(JSVAL_TO_STRING(idval))) != NULL) {
            if (!strcmp(property_name, "constructor")) {
                if (vp)
                    *vp = JSVAL_VOID;
                return JS_TRUE;
            }
        }
        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                                JSJMSG_BAD_OP_JARRAY);
        return JS_FALSE;
    }
    class_descriptor = java_wrapper->class_descriptor;
    java_array = java_wrapper->java_obj;
    
    JS_ASSERT(class_descriptor->type == JAVA_SIGNATURE_ARRAY);

    JS_IdToValue(cx, id, &idval);

    if (!JSVAL_IS_INT(idval))
        idval = try_convert_to_jsint(cx, idval);

    if (!JSVAL_IS_INT(idval)) {
        /*
         * Usually, properties of JavaArray objects are indexed by integers, but
         * Java arrays also inherit all the methods of java.lang.Object, so a
         * string-valued property is also possible.
         */
        if (JSVAL_IS_STRING(idval)) {
            const char *member_name;
            
            member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval));
            
            if (do_assignment) {
                JSVersion version = JS_GetVersion(cx);

                if (!JSVERSION_IS_ECMA(version)) {
 
                    JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                        JSJMSG_CANT_WRITE_JARRAY, member_name);
                    return JS_FALSE;
                } else {
                    if (vp)
                        *vp = JSVAL_VOID;
                    return JS_TRUE;
                }
            } else {
                if (!strcmp(member_name, "length")) {
                    array_length = jsj_GetJavaArrayLength(cx, jEnv, java_array);
                    if (array_length < 0)
                        return JS_FALSE;
                    if (vp)
                        *vp = INT_TO_JSVAL(array_length);
                    return JS_TRUE;
                }
                
                /* Check to see if we're reflecting a Java array method */
                return JavaObject_getPropertyById(cx, obj, id, vp);
            }
        }

        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                            JSJMSG_BAD_INDEX_EXPR);
        return JS_FALSE;
    }
    
    index = JSVAL_TO_INT(idval);

#if 0
    array_length = jsj_GetJavaArrayLength(cx, jEnv, java_array);
    if (array_length < 0)
        return JS_FALSE;

    /* Just let Java throw an exception instead of checking array bounds here */
    if (index < 0 || index >= array_length) {
        char numBuf[12];
        sprintf(numBuf, "%d", index);
        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL,
                                            JSJMSG_BAD_JARRAY_INDEX, numBuf);
        return JS_FALSE;
    }
#endif

    array_component_signature = class_descriptor->array_component_signature;

    if (!vp)
        return JS_TRUE;

    if (do_assignment) {
        return jsj_SetJavaArrayElement(cx, jEnv, java_array, index,
                                       array_component_signature, *vp);
    } else {
        return jsj_GetJavaArrayElement(cx, jEnv, java_array, index,
                                       array_component_signature, vp);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

JavaArray_checkAccess ( JSContext cx,
JSObject obj,
jsid  id,
JSAccessMode  mode,
jsval vp,
uintN attrsp 
)

Definition at line 392 of file jsj_JavaArray.c.

{
    switch (mode) {
    case JSACC_WATCH:
        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                            JSJMSG_JARRAY_PROP_WATCH);
        return JS_FALSE;

    case JSACC_IMPORT:
        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                            JSJMSG_JARRAY_PROP_EXPORT);
        return JS_FALSE;

    default:
        return JS_TRUE;
    }
}

Here is the call graph for this function:

JavaArray_defaultValue ( JSContext cx,
JSObject obj,
JSType  type,
jsval vp 
)

Definition at line 322 of file jsj_JavaArray.c.

{
    /* printf("In JavaArray_defaultValue()\n"); */
    return JavaObject_convert(cx, obj, JSTYPE_STRING, vp);
}

Here is the call graph for this function:

JavaArray_defineProperty ( JSContext cx,
JSObject obj,
jsid  id,
jsval  value,
JSPropertyOp  getter,
JSPropertyOp  setter,
uintN  attrs,
JSProperty **  propp 
)

Definition at line 267 of file jsj_JavaArray.c.

{
    jsval *vp = &value;
    if (propp)
        return JS_FALSE;
    if (attrs & ~(JSPROP_PERMANENT|JSPROP_ENUMERATE))
        return JS_FALSE;

    return JavaArray_setPropertyById(cx, obj, id, vp);
}

Here is the call graph for this function:

JavaArray_deleteProperty ( JSContext cx,
JSObject obj,
jsid  id,
jsval vp 
)

Definition at line 304 of file jsj_JavaArray.c.

{
    JSVersion version = JS_GetVersion(cx);

    *vp = JSVAL_FALSE;
    
    if (!JSVERSION_IS_ECMA(version)) {
        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                            JSJMSG_JARRAY_PROP_DELETE);
        return JS_FALSE;
    } else {
        /* Attempts to delete permanent properties are silently ignored
           by ECMAScript. */
        return JS_TRUE;
    }
}

Here is the call graph for this function:

JavaArray_getAttributes ( JSContext cx,
JSObject obj,
jsid  id,
JSProperty prop,
uintN attrsp 
)

Definition at line 281 of file jsj_JavaArray.c.

{
    /* We don't maintain JS property attributes for Java class members */
    *attrsp = JSPROP_PERMANENT|JSPROP_ENUMERATE;
    return JS_FALSE;
}
JavaArray_getObjectOps ( JSContext cx,
JSClass clazz 
)

Definition at line 442 of file jsj_JavaArray.c.

{
    return &JavaArray_ops;
}
JavaArray_getPropertyById ( JSContext cx,
JSObject obj,
jsval  id,
jsval vp 
)

Definition at line 212 of file jsj_JavaArray.c.

{
    JNIEnv *jEnv;
    JSJavaThreadState *jsj_env;
    JSBool result;

    jsj_env = jsj_EnterJava(cx, &jEnv);
    if (!jEnv)
        return JS_FALSE;
    result = access_java_array_element(cx, jEnv, obj, id, vp, JS_FALSE);
    jsj_ExitJava(jsj_env);
    return result;
}

Here is the call graph for this function:

JavaArray_lookupProperty ( JSContext cx,
JSObject obj,
jsid  id,
JSObject **  objp,
JSProperty **  propp 
)

Definition at line 242 of file jsj_JavaArray.c.

{
    JNIEnv *jEnv;
    JSErrorReporter old_reporter;
    JSJavaThreadState *jsj_env;

    jsj_env = jsj_EnterJava(cx, &jEnv);
    if (!jEnv)
        return JS_FALSE;

    old_reporter = JS_SetErrorReporter(cx, NULL);
    if (access_java_array_element(cx, jEnv, obj, id, NULL, JS_FALSE)) {
        *objp = obj;
        *propp = (JSProperty*)1;
    } else {
        *objp = NULL;
        *propp = NULL;
    }
    JS_SetErrorReporter(cx, old_reporter);
    jsj_ExitJava(jsj_env);
    return JS_TRUE;
}

Here is the call graph for this function:

JavaArray_newEnumerate ( JSContext cx,
JSObject obj,
JSIterateOp  enum_op,
jsval statep,
jsid idp 
)

Definition at line 329 of file jsj_JavaArray.c.

{
    JavaObjectWrapper *java_wrapper;
    JSJavaThreadState *jsj_env;
    JNIEnv *jEnv;
    jsize array_length, index;
    JSBool ok = JS_TRUE;

    java_wrapper = JS_GetPrivate(cx, obj);
    /* Check for prototype object */
    if (!java_wrapper) {
        *statep = JSVAL_NULL;
        if (idp)
            *idp = INT_TO_JSVAL(0);
        return JS_TRUE;
    }
        
    /* Get the Java per-thread environment pointer for this JSContext */
    jsj_env = jsj_EnterJava(cx, &jEnv);
    if (!jEnv)
        return JS_FALSE;

    array_length = jsj_GetJavaArrayLength(cx, jEnv, java_wrapper->java_obj);
    if (array_length < 0) {
        jsj_ExitJava(jsj_env);
        return JS_FALSE;
    }

    switch(enum_op) {
    case JSENUMERATE_INIT:
        *statep = INT_TO_JSVAL(0);

        if (idp)
            *idp = INT_TO_JSVAL(array_length);
        break;
        
    case JSENUMERATE_NEXT:
        index = JSVAL_TO_INT(*statep);
        if (index < array_length) {
            JS_ValueToId(cx, INT_TO_JSVAL(index), idp);
            index++;
            *statep = INT_TO_JSVAL(index);
            break;
        }

        /* Fall through ... */

    case JSENUMERATE_DESTROY:
        *statep = JSVAL_NULL;
        break;

    default:
        JS_ASSERT(0);
        ok = JS_FALSE;
        break;
    }

    jsj_ExitJava(jsj_env);
    return ok;
}

Here is the call graph for this function:

JavaArray_setAttributes ( JSContext cx,
JSObject obj,
jsid  id,
JSProperty prop,
uintN attrsp 
)

Definition at line 290 of file jsj_JavaArray.c.

{
    /* We don't maintain JS property attributes for Java class members */
    if (*attrsp != (JSPROP_PERMANENT|JSPROP_ENUMERATE)) {
        JS_ASSERT(0);
        return JS_FALSE;
    }

    /* Silently ignore all setAttribute attempts */
    return JS_TRUE;
}
JavaArray_setPropertyById ( JSContext cx,
JSObject obj,
jsval  id,
jsval vp 
)

Definition at line 227 of file jsj_JavaArray.c.

{
    JNIEnv *jEnv;
    JSJavaThreadState *jsj_env;
    JSBool result;
    
    jsj_env = jsj_EnterJava(cx, &jEnv);
    if (!jEnv)
        return JS_FALSE;
    result = access_java_array_element(cx, jEnv, obj, id, vp, JS_TRUE);
    jsj_ExitJava(jsj_env);
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 463 of file jsj_JavaArray.c.

{
    if (!JS_InitClass(cx, global_obj, 
        0, &JavaArray_class, 0, 0,
        0, 0, 0, 0))
        return JS_FALSE;
    
    return JS_TRUE;
}
static jsval try_convert_to_jsint ( JSContext cx,
jsval  idval 
) [static]

Definition at line 62 of file jsj_JavaArray.c.

{
    const jschar *cp;
    JSString *jsstr;
    
    jsstr = JS_ValueToString(cx, idval);
    if (!jsstr)
        return idval;

    cp = JS_GetStringChars(jsstr);
    if (JS7_ISDEC(*cp)) {
        jsuint index = JS7_UNDEC(*cp++);
        jsuint oldIndex = 0;
        jsuint c = 0;
        if (index != 0) {
            while (JS7_ISDEC(*cp)) {
                oldIndex = index;
                c = JS7_UNDEC(*cp);
                index = 10*index + c;
                cp++;
            }
        }
        if (*cp == 0 &&
            (oldIndex < (JSVAL_INT_MAX / 10) ||
            (oldIndex == (JSVAL_INT_MAX / 10) && c < (JSVAL_INT_MAX % 10)))) {
            return INT_TO_JSVAL(index);
        }
    }
    return idval;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:

Definition at line 447 of file jsj_JavaArray.c.