Back to index

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

Go to the source code of this file.

Classes

struct  JavaPackage_Private

Defines

#define STRTOK_1ST(str, seps, res)   strtok (str, seps)
#define STRTOK_OTHER(seps, res)   strtok (NULL, seps)

Functions

static JSObjectdefine_JavaPackage (JSContext *cx, JSObject *parent_obj, const char *obj_name, const char *path, int flags, int access)
 JavaPackage_setProperty (JSContext *cx, JSObject *obj, jsval slot, jsval *vp)
 JavaPackage_resolve (JSContext *cx, JSObject *obj, jsval id)
 JavaPackage_convert (JSContext *cx, JSObject *obj, JSType type, jsval *vp)
 JavaPackage_finalize (JSContext *cx, JSObject *obj)
static JSBool pre_define_java_packages (JSContext *cx, JSObject *global_obj, JavaPackageDef *predefined_packages)
 JavaPackage_toString (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
JSBool jsj_init_JavaPackage (JSContext *cx, JSObject *global_obj, JavaPackageDef *additional_predefined_packages)

Variables

JSClass JavaPackage_class
static JSBool quiet_resolve_failure
JavaPackageDef standard_java_packages []
static JSFunctionSpec JavaPackage_methods []

Class Documentation

struct JavaPackage_Private

Definition at line 69 of file jsj_JavaPackage.c.

Collaboration diagram for JavaPackage_Private:
Class Members
int flags
const char * path

Define Documentation

#define STRTOK_1ST (   str,
  seps,
  res 
)    strtok (str, seps)

Definition at line 409 of file jsj_JavaPackage.c.

#define STRTOK_OTHER (   seps,
  res 
)    strtok (NULL, seps)

Definition at line 410 of file jsj_JavaPackage.c.


Function Documentation

static JSObject* define_JavaPackage ( JSContext cx,
JSObject parent_obj,
const char *  obj_name,
const char *  path,
int  flags,
int  access 
) [static]

Definition at line 75 of file jsj_JavaPackage.c.

{
    JSObject *package_obj;
    JavaPackage_Private *package;

    package_obj = JS_DefineObject(cx, parent_obj, obj_name, &JavaPackage_class, 0, JSPROP_PERMANENT | access);
    
    if (!package_obj)
        return NULL;
    
    /* Attach private, native data to the JS object */
    package = (JavaPackage_Private *)JS_malloc(cx, sizeof(JavaPackage_Private));
    JS_SetPrivate(cx, package_obj, (void *)package);
    if (path)
        package->path = JS_strdup(cx, path);
    else
        package->path = "";

    package->flags = flags;

    /* Check for OOM */
    if (!package->path) {
        JS_DeleteProperty(cx, parent_obj, obj_name);
        JS_free(cx, package);
        return NULL;
    }

    return package_obj;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 266 of file jsj_JavaPackage.c.

{
    JSString *str;
    char *name, *cp;

    JavaPackage_Private *package = JS_GetPrivate(cx, obj);
    if (!package) {
        fprintf(stderr, "JavaPackage_resolve: no private data!\n");
        return JS_FALSE;
    }

    switch (type) {

    /* Pretty-printing of JavaPackage */
    case JSTYPE_VOID:   /* Default value */
    case JSTYPE_NUMBER:
    case JSTYPE_STRING:
        /* Convert '/' to '.' so that it looks like Java language syntax. */
        if (!package->path)
            break;
        name = JS_smprintf("[JavaPackage %s]", package->path);
        if (!name) {
            JS_ReportOutOfMemory(cx);
            return JS_FALSE;
        }
        for (cp = name; *cp != '\0'; cp++)
            if (*cp == '/')
                *cp = '.';
        str = JS_NewString(cx, name, strlen(name));
        if (!str) {
            free(name);
            /* It's not necessary to call JS_ReportOutOfMemory(), as
               JS_NewString() will do so on failure. */
            return JS_FALSE;
        }

        *vp = STRING_TO_JSVAL(str);
        break;

    case JSTYPE_OBJECT:
        *vp = OBJECT_TO_JSVAL(obj);
        break;

    default:
        break;
    }
    return JS_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JavaPackage_finalize ( JSContext cx,
JSObject obj 
)

Definition at line 319 of file jsj_JavaPackage.c.

{
    JavaPackage_Private *package = JS_GetPrivate(cx, obj);
    if (!package)
        return;

    if (package->path)
        JS_free(cx, (char *)package->path);
    JS_free(cx, package);
}

Here is the call graph for this function:

JavaPackage_resolve ( JSContext cx,
JSObject obj,
jsval  id 
)

Definition at line 133 of file jsj_JavaPackage.c.

{
    JavaPackage_Private *package;
    JSBool ok = JS_TRUE;
    jclass jclazz;
    char *subPath, *newPath;
    const char *path;
    JNIEnv *jEnv;
    JSJavaThreadState *jsj_env;

    /* Painful hack for pre_define_java_packages() */
    if (quiet_resolve_failure)
        return JS_FALSE;
                
    package = (JavaPackage_Private *)JS_GetPrivate(cx, obj);
    if (!package)
        return JS_TRUE;

    if (!JSVAL_IS_STRING(id))
        return JS_TRUE;
    subPath = JS_GetStringBytes(JSVAL_TO_STRING(id));

    /*
     * There will be an attempt to invoke the toString() method when producing
     * the string representation of a JavaPackage.  When this occurs, avoid
     * creating a bogus toString package.  (This means that no one can ever
     * create a package with the simple name "toString", but we'll live with
     * that limitation.)
     */
    if (!strcmp(subPath, "toString"))
        return JS_FALSE;

    path = package->path;
    newPath = JS_smprintf("%s%s%s", path, (path[0] ? "/" : ""), subPath);
    if (!newPath) {
        JS_ReportOutOfMemory(cx);
        return JS_FALSE;
    }

    jsj_env = jsj_EnterJava(cx, &jEnv);
    if (!jEnv) {
        ok = JS_FALSE;
        goto out;
    }

    /*
        Unfortunately, Java provides no way to find out whether a particular
        name is a package or not.  The only way to tell is to try to load the
        name as a class file and, if that fails, assume it's a package.  This
        makes things work as expected for the most part, but it has three
        noticeable problems that keep coming up:

        - You can refer to a package like java.lang.i.buried.paul without
          generating a complaint.  Of course, you'll never be able to refer to
          any classes through it.

        - An annoying consequence of the above is that misspelling a class name
          results in a cryptic error about packages.

        - In a browser context, i.e. where applets are involved, figuring out
          whether something is a class may require looking for it over the net
          using the current classloader.  This means that the first time you
          refer to java.lang.System in a js context, there will be an attempt
          to search for [[DOCBASE]]/java.class on the server.
    
        A solution is to explicitly tell jsjava the names of all the (local)
        packages on the CLASSPATH.  (Not implemented yet.)

    */
    jclazz = (*jEnv)->FindClass(jEnv, newPath);
    if (jclazz) {
        JSObject *newClass;

        newClass = jsj_define_JavaClass(cx, jEnv, obj, subPath, jclazz);
        (*jEnv)->DeleteLocalRef(jEnv, jclazz);
        if (!newClass) {
            ok = JS_FALSE;
            goto out;
        }
    } else {

        /* We assume that any failed attempt to load a class is because it
           doesn't exist.  If we wanted to do a better job, we would check
           the exception type and make sure that it's NoClassDefFoundError */
        (*jEnv)->ExceptionClear(jEnv);

        /*
         * If there's no class of the given name, then we must be referring to
         * a package.  However, don't allow bogus sub-packages of pre-defined
         * system packages to be created.
         */
        if (JS_InstanceOf(cx, obj, &JavaPackage_class, NULL)) {
            JavaPackage_Private *current_package;

            current_package = JS_GetPrivate(cx, obj);
            if (current_package->flags & PKG_SYSTEM) {
                char *msg, *cp;
                msg = JS_strdup(cx, newPath);

                /* Check for OOM */
                if (msg) {
                    /* Convert package of form "java/lang" to "java.lang" */
                    for (cp = msg; *cp != '\0'; cp++)
                        if (*cp == '/')
                            *cp = '.';
                    JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                                JSJMSG_MISSING_PACKAGE, msg);
                    free((char*)msg);
                }
                               
                ok = JS_FALSE;
                goto out;
            }
        }

        if (!define_JavaPackage(cx, obj, subPath, newPath, 0, JSPROP_READONLY)) {
            ok = JS_FALSE;
            goto out;
        }
        
#ifdef DEBUG
        /* printf("JavaPackage \'%s\' created\n", newPath); */
#endif

    }
    
out:
    free(newPath);
    jsj_ExitJava(jsj_env);
    return ok;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JavaPackage_setProperty ( JSContext cx,
JSObject obj,
jsval  slot,
jsval vp 
)

Definition at line 114 of file jsj_JavaPackage.c.

{
    JavaPackage_Private *package = JS_GetPrivate(cx, obj);
    if (!package) {
        JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                                JSJMSG_BAD_ADD_TO_PACKAGE);
        return JS_FALSE;
    }
    JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                                JSJMSG_DONT_ADD_TO_PACKAGE);
    return JS_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JavaPackage_toString ( JSContext cx,
JSObject obj,
uintN  argc,
jsval argv,
jsval rval 
)

Definition at line 515 of file jsj_JavaPackage.c.

{
    if (!JS_InstanceOf(cx, obj, &JavaPackage_class, argv))
        return JS_FALSE;
    return JavaPackage_convert(cx, obj, JSTYPE_STRING, rval);
}

Here is the call graph for this function:

JSBool jsj_init_JavaPackage ( JSContext cx,
JSObject global_obj,
JavaPackageDef additional_predefined_packages 
)

Definition at line 533 of file jsj_JavaPackage.c.

                                                                     {

    /* Define JavaPackage class */
    if (!JS_InitClass(cx, global_obj, 0, &JavaPackage_class,
                      0, 0, 0, JavaPackage_methods, 0, 0))
        return JS_FALSE;

    /* Add top-level packages, e.g. : java, netscape, sun */
    if (!pre_define_java_packages(cx, global_obj, standard_java_packages))
        return JS_FALSE;
    if (!pre_define_java_packages(cx, global_obj, additional_predefined_packages))
        return JS_FALSE;
    
    return JS_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static JSBool pre_define_java_packages ( JSContext cx,
JSObject global_obj,
JavaPackageDef predefined_packages 
) [static]

Definition at line 420 of file jsj_JavaPackage.c.

{
    JSBool package_exists;
    JSObject *parent_obj;
    JavaPackageDef *package_def;
    char *simple_name, *cp, *package_name, *path;
    int flags;

    if (!predefined_packages)
        return JS_TRUE;

    /* Iterate over all pre-defined Java packages */
    for (package_def = predefined_packages; package_def->name; package_def++) {
#if HAVE_STRTOK_R
       char *nextstr;
#endif
        package_name = path = NULL;

        parent_obj = global_obj;
        package_name = strdup(package_def->name);
        if (!package_name)
            goto out_of_memory;

        /* Walk the chain of JavaPackage objects to get to the parent of the
           rightmost sub-package in the fully-qualified package name. */
        for (simple_name = STRTOK_1ST(package_name, ".", nextstr); simple_name /*1*/; simple_name = STRTOK_OTHER(".", nextstr)) {
            jsval v;

            if (!simple_name) {
                JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, 
                                        JSJMSG_DOUBLE_SHIPPING, package_name);
                goto error;
            }

            /* Check to see if the sub-package already exists */
            quiet_resolve_failure = JS_TRUE;
            package_exists = JS_LookupProperty(cx, parent_obj, simple_name, &v) && JSVAL_IS_OBJECT(v);
            quiet_resolve_failure = JS_FALSE;

            if (package_exists) {
                parent_obj = JSVAL_TO_OBJECT(v);
                continue;
            }

            /* New package objects should only be created at the terminal
               sub-package in a fully-qualified package-name */
            if (STRTOK_OTHER(".", nextstr)) {
                JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL,
                                JSJMSG_BAD_PACKAGE_PREDEF,
                               package_def->name);
                goto error;
            }
            
            if (package_def->path) {
                path = strdup(package_def->path);
                if (!path)
                    goto out_of_memory;
            } else {
                /*
                 * The default path is specified, so create it from the
                 * fully-qualified package name.
                 */
                path = strdup(package_def->name);
                if (!path)
                    goto out_of_memory;
                /* Transform package name, e.g. "java.lang" ==> "java/lang" */
                for (cp = path; *cp != '\0'; cp++) {
                    if (*cp == '.')
                         *cp = '/';
                }
            }
            flags = package_def->flags;
            parent_obj = define_JavaPackage(cx, parent_obj, simple_name, path, flags,
                                            package_def->access);
            if (!parent_obj)
                goto error;
 
            free(path);
            break;
        }
        free(package_name);
    }
    return JS_TRUE;

out_of_memory:
    JS_ReportOutOfMemory(cx);

error:
    JS_FREE_IF(cx, package_name);
    JS_FREE_IF(cx, path);
    return JS_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
 {
    {"toString",   JavaPackage_toString,        0,      0,      0},
    {0, 0, 0, 0, 0},
}

Definition at line 523 of file jsj_JavaPackage.c.

Definition at line 127 of file jsj_JavaPackage.c.

Definition at line 351 of file jsj_JavaPackage.c.