Inheritance: ScriptableObject
Exemplo n.º 1
0
		public virtual Scriptable Construct(Context cx, Scriptable scope, object[] args)
		{
			ClassLoader loader = null;
			if (args.Length != 0)
			{
				object arg = args[0];
				if (arg is Wrapper)
				{
					arg = ((Wrapper)arg).Unwrap();
				}
				if (arg is ClassLoader)
				{
					loader = (ClassLoader)arg;
				}
			}
			if (loader == null)
			{
				Context.ReportRuntimeError0("msg.not.classloader");
				return null;
			}
			NativeJavaPackage pkg = new NativeJavaPackage(true, string.Empty, loader);
			ScriptRuntime.SetObjectProtoAndParent(pkg, scope);
			return pkg;
		}
Exemplo n.º 2
0
		public static void Init(Context cx, Scriptable scope, bool @sealed)
		{
			ClassLoader loader = cx.GetApplicationClassLoader();
			Rhino.NativeJavaTopPackage top = new Rhino.NativeJavaTopPackage(loader);
			top.SetPrototype(GetObjectPrototype(scope));
			top.SetParentScope(scope);
			for (int i = 0; i != commonPackages.Length; i++)
			{
				NativeJavaPackage parent = top;
				for (int j = 0; j != commonPackages[i].Length; j++)
				{
					parent = parent.ForcePackage(commonPackages[i][j], scope);
				}
			}
			// getClass implementation
			IdFunctionObject getClass = new IdFunctionObject(top, FTAG, Id_getClass, "getClass", 1, scope);
			// We want to get a real alias, and not a distinct JavaPackage
			// with the same packageName, so that we share classes and top
			// that are underneath.
			string[] topNames = ScriptRuntime.GetTopPackageNames();
			NativeJavaPackage[] topPackages = new NativeJavaPackage[topNames.Length];
			for (int i_1 = 0; i_1 < topNames.Length; i_1++)
			{
				topPackages[i_1] = (NativeJavaPackage)top.Get(topNames[i_1], top);
			}
			// It's safe to downcast here since initStandardObjects takes
			// a ScriptableObject.
			ScriptableObject global = (ScriptableObject)scope;
			if (@sealed)
			{
				getClass.SealObject();
			}
			getClass.ExportAsScopeProperty();
			global.DefineProperty("Packages", top, ScriptableObject.DONTENUM);
			for (int i_2 = 0; i_2 < topNames.Length; i_2++)
			{
				global.DefineProperty(topNames[i_2], topPackages[i_2], ScriptableObject.DONTENUM);
			}
		}
Exemplo n.º 3
0
		// set up a name which is known to be a package so we don't
		// need to look for a class by that name
		internal virtual Rhino.NativeJavaPackage ForcePackage(string name, Scriptable scope)
		{
			object cached = base.Get(name, this);
			if (cached != null && cached is Rhino.NativeJavaPackage)
			{
				return (Rhino.NativeJavaPackage)cached;
			}
			else
			{
				string newPackage = packageName.Length == 0 ? name : packageName + "." + name;
				Rhino.NativeJavaPackage pkg = new Rhino.NativeJavaPackage(true, newPackage, classLoader);
				ScriptRuntime.SetObjectProtoAndParent(pkg, scope);
				base.Put(name, this, pkg);
				return pkg;
			}
		}
Exemplo n.º 4
0
		private void ImportPackage(NativeJavaPackage pkg)
		{
			if (pkg == null)
			{
				return;
			}
			lock (importedPackages)
			{
				for (int j = 0; j != importedPackages.Size(); j++)
				{
					if (pkg.Equals(importedPackages.Get(j)))
					{
						return;
					}
				}
				importedPackages.Add(pkg);
			}
		}
Exemplo n.º 5
0
		internal virtual object GetPkgProperty(string name, Scriptable start, bool createPkg)
		{
			lock (this)
			{
				object cached = base.Get(name, start);
				if (cached != ScriptableConstants.NOT_FOUND)
				{
					return cached;
				}
				if (negativeCache != null && negativeCache.Contains(name))
				{
					// Performance optimization: see bug 421071
					return null;
				}
				string className = (packageName.Length == 0) ? name : packageName + '.' + name;
				Context cx = Context.GetContext();
				ClassShutter shutter = cx.GetClassShutter();
				Scriptable newValue = null;
				if (shutter == null || shutter.VisibleToScripts(className))
				{
					Type cl = null;
					if (classLoader != null)
					{
						cl = Kit.ClassOrNull(classLoader, className);
					}
					else
					{
						cl = Kit.ClassOrNull(className);
					}
					if (cl != null)
					{
						WrapFactory wrapFactory = cx.GetWrapFactory();
						newValue = wrapFactory.WrapJavaClass(cx, GetTopLevelScope(this), cl);
						newValue.SetPrototype(GetPrototype());
					}
				}
				if (newValue == null)
				{
					if (createPkg)
					{
						Rhino.NativeJavaPackage pkg;
						pkg = new Rhino.NativeJavaPackage(true, className, classLoader);
						ScriptRuntime.SetObjectProtoAndParent(pkg, GetParentScope());
						newValue = pkg;
					}
					else
					{
						// add to negative cache
						if (negativeCache == null)
						{
							negativeCache = new HashSet<string>();
						}
						negativeCache.Add(name);
					}
				}
				if (newValue != null)
				{
					// Make it available for fast lookup and sharing of
					// lazily-reflected constructors and static members.
					base.Put(name, start, newValue);
				}
				return newValue;
			}
		}