From ca7b131ae56a14318cadce4b68444fd9321affa4 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Sun, 13 Apr 2003 08:54:09 +0000 Subject: [PATCH] 1. In NativeJavaPackage.init define global properties only when NativeJavaPackage instance is fully initialized. 2. Memory optimization: instead of string array for predefined known packages, use single string which is split into substrings when necessary. --- .../mozilla/javascript/NativeJavaPackage.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java b/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java index e6b43c7a82c..e86c50cdd81 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java @@ -56,17 +56,19 @@ public class NativeJavaPackage extends ScriptableObject { // we know these are packages so we can skip the class check // note that this is ok even if the package isn't present. - static final String[] commonPackages = { - "java.lang", - "java.lang.reflect", - "java.io", - "java.math", - "java.util", - "java.util.zip", - "java.text", - "java.text.resources", - "java.applet", - }; + private static final String commonPackages = "" + +"java.lang;" + +"java.lang.reflect" + +"java.io;" + +"java.math;" + +"java.net;" + +"java.util;" + +"java.util.zip;" + +"java.text;" + +"java.text.resources;" + +"java.applet;" + +"javax.swing;" + ; public static class TopLevelPackage extends NativeJavaPackage implements Function @@ -121,21 +123,26 @@ public class NativeJavaPackage extends ScriptableObject { NativeJavaPackage javaAlias = (NativeJavaPackage)packages.get("java", packages); + for (int nameStart = 0; ;) { + int nameEnd = commonPackages.indexOf(';', nameStart); + if (nameEnd < 0) { break; } + String packageName = commonPackages.substring(nameStart, nameEnd); + packages.forcePackage(packageName); + nameStart = nameEnd + 1; + } + + Method[] m = FunctionObject.findMethods(NativeJavaPackage.class, + "jsFunction_getClass"); + FunctionObject f = new FunctionObject("getClass", m[0], scope); + // It's safe to downcast here since initStandardObjects takes // a ScriptableObject. ScriptableObject global = (ScriptableObject) scope; + global.defineProperty("getClass", f, ScriptableObject.DONTENUM); global.defineProperty("Packages", packages, ScriptableObject.DONTENUM); global.defineProperty("java", javaAlias, ScriptableObject.DONTENUM); - for (int i = 0; i < commonPackages.length; i++) - packages.forcePackage(commonPackages[i]); - - Method[] m = FunctionObject.findMethods(NativeJavaPackage.class, - "jsFunction_getClass"); - FunctionObject f = new FunctionObject("getClass", m[0], global); - global.defineProperty("getClass", f, ScriptableObject.DONTENUM); - // I think I'm supposed to return the prototype, but I don't have one. return packages; }