A bunch of changes, some experimental.

Support for jsGet_ and jsSet_ prefixes to methods for explicit getter
and setter definition.
Addition of "importClass" and "importPackage" top-level functions.
The beginnings of a history object accessible from the shell.
This commit is contained in:
norris%netscape.com 1999-06-18 17:37:20 +00:00
Родитель 487d9ea921
Коммит aadce49c83
32 изменённых файлов: 666 добавлений и 365 удалений

Просмотреть файл

@ -89,7 +89,6 @@ function processFile(f) {
// main script: process each file in arguments list
print("here");
for (var i=0; i < arguments.length; i++) {
var filename = String(arguments[i]);
print("Checking " + filename + "...");

Просмотреть файл

@ -0,0 +1,109 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
package org.mozilla.javascript;
import java.util.Vector;
/**
* Class ImporterTopLevel
*
* This class defines a ScriptableObject that can be instantiated
* as a top-level ("global") object to provide functionality similar
* to Java's "import" statement.
* <p>
* This class can be used to create a top-level scope using the following code:
* <pre>
* Scriptable scope = cx.initStandardObjects(new ImporterTopLevel());
* </pre>
* Then JavaScript code will have access to the following methods:
* <ul>
* <li>importClass - will "import" a class by making its unqualified name
* available as a property of the top-level scope
* <li>importPackage - will "import" all the classes of the package by
* searching for unqualified names as classes qualified
* by the given package.
* </ul>
* The following code from the shell illustrates this use:
* <pre>
* js> importClass(java.io.File)
* js> f = new File('help.txt')
* help.txt
* js> importPackage(java.util)
* js> v = new Vector()
* []
*
* @author Norris Boyd
*/
public class ImporterTopLevel extends ScriptableObject {
public ImporterTopLevel() {
String[] names = { "importClass", "importPackage" };
try {
this.defineFunctionProperties(names, ImporterTopLevel.class,
ScriptableObject.DONTENUM);
} catch (PropertyException e) {
throw new Error(); // should never happen
}
}
public String getClassName() {
return "global";
}
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result == NOT_FOUND && importedPackages != null) {
for (int i=0; i < importedPackages.size(); i++) {
Object o = importedPackages.elementAt(i);
NativeJavaPackage p = (NativeJavaPackage) o;
Object v = p.get(name, start);
if (v != NOT_FOUND) {
if (result == NOT_FOUND)
result = v;
else
throw new EvaluatorException("Ambiguous import: " +
result + " and " + v);
}
}
}
return result;
}
public void importClass(Object cl) {
if (!(cl instanceof NativeJavaClass))
throw new EvaluatorException("not a class");// TODO: better msg
String s = ((NativeJavaClass) cl).getClassObject().getName();
String n = s.substring(s.lastIndexOf('.')+1);
if (this.has(n, this))
throw new EvaluatorException("property " + n +
" is already defined");
this.defineProperty(n, cl, DONTENUM);
}
public void importPackage(Object pkg) {
if (importedPackages == null)
importedPackages = new Vector();
if (!(pkg instanceof NativeJavaPackage))
throw new EvaluatorException("not a package");// TODO: better msg
importedPackages.addElement(pkg);
}
private Vector importedPackages;
}

Просмотреть файл

@ -178,8 +178,8 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.1,2
*/
public static Object js_Array(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
throws JavaScriptException
{
if (!inNewExpr) {
@ -205,11 +205,11 @@ public class NativeArray extends ScriptableObject {
private static final int lengthAttr = ScriptableObject.DONTENUM |
ScriptableObject.PERMANENT;
public long js_getLength() {
public long jsGet_length() {
return length;
}
public void js_setLength(Object val) {
public void jsSet_length(Object val) {
/* XXX do we satisfy this?
* 15.4.5.1 [[Put]](P, V):
* 1. Call the [[CanPut]] method of A with name P.
@ -258,9 +258,9 @@ public class NativeArray extends ScriptableObject {
static double getLengthProperty(Scriptable obj) {
// These will both give numeric lengths within Uint32 range.
if (obj instanceof NativeString)
return (double)((NativeString)obj).js_getLength();
return (double)((NativeString)obj).jsGet_length();
if (obj instanceof NativeArray)
return (double)((NativeArray)obj).js_getLength();
return (double)((NativeArray)obj).jsGet_length();
return ScriptRuntime.toUint32(ScriptRuntime
.getProp(obj, "length", obj));
}
@ -309,7 +309,7 @@ public class NativeArray extends ScriptableObject {
}
}
public static String js_toString(Context cx, Scriptable thisObj,
public static String jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
return toStringHelper(cx, thisObj,
@ -393,7 +393,7 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.4.3
*/
public static String js_join(Context cx, Scriptable thisObj,
public static String jsFunction_join(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
StringBuffer result = new StringBuffer();
@ -421,7 +421,7 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.4.4
*/
public static Scriptable js_reverse(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_reverse(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
long len = (long)getLengthProperty(thisObj);
@ -440,7 +440,7 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.4.5
*/
public static Scriptable js_sort(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_sort(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
@ -605,7 +605,7 @@ public class NativeArray extends ScriptableObject {
* Non-ECMA methods.
*/
public static Object js_push(Context cx, Scriptable thisObj,
public static Object jsFunction_push(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
double length = getLengthProperty(thisObj);
@ -630,8 +630,8 @@ public class NativeArray extends ScriptableObject {
return new Long((long)length);
}
public static Object js_pop(Context cx, Scriptable thisObj, Object[] args,
Function funObj)
public static Object jsFunction_pop(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Object result;
double length = getLengthProperty(thisObj);
@ -653,8 +653,8 @@ public class NativeArray extends ScriptableObject {
return result;
}
public static Object js_shift(Context cx, Scriptable thisObj, Object[] args,
Function funOjb)
public static Object jsFunction_shift(Context cx, Scriptable thisObj,
Object[] args, Function funOjb)
{
Object result;
double length = getLengthProperty(thisObj);
@ -684,7 +684,7 @@ public class NativeArray extends ScriptableObject {
return result;
}
public static Object js_unshift(Context cx, Scriptable thisObj,
public static Object jsFunction_unshift(Context cx, Scriptable thisObj,
Object[] args, Function funOjb)
{
Object result;
@ -713,7 +713,7 @@ public class NativeArray extends ScriptableObject {
return new Long((long)length);
}
public static Object js_splice(Context cx, Scriptable thisObj,
public static Object jsFunction_splice(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
/* create an empty Array to return. */
@ -820,7 +820,7 @@ public class NativeArray extends ScriptableObject {
/*
* Python-esque sequence operations.
*/
public static Scriptable js_concat(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_concat(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
/* Concat tries to keep the definition of an array as general
@ -873,7 +873,7 @@ public class NativeArray extends ScriptableObject {
return result;
}
public static Scriptable js_slice(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_slice(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Scriptable scope = getTopLevelScope(funObj);

Просмотреть файл

@ -47,8 +47,8 @@ public class NativeBoolean extends ScriptableObject {
return super.getDefaultValue(typeHint);
}
public static Object Boolean(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
boolean b = args.length >= 1
? ScriptRuntime.toBoolean(args[0])
@ -62,11 +62,11 @@ public class NativeBoolean extends ScriptableObject {
return b ? Boolean.TRUE : Boolean.FALSE;
}
public String toString() {
public String jsFunction_toString() {
return booleanValue ? "true" : "false";
}
public boolean valueOf() {
public boolean jsFunction_valueOf() {
return booleanValue;
}

Просмотреть файл

@ -80,8 +80,8 @@ public final class NativeCall extends ScriptableObject {
return "Call";
}
public static Object js_Call(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
if (!inNewExpr) {
Object[] errArgs = { "Call" };

Просмотреть файл

@ -63,7 +63,7 @@ public class NativeClosure extends ScriptableObject implements Function {
return proto.construct(cx, getParentScope(), args);
}
public static Object js_Closure(Context cx, Object[] args,
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
Object[] msgArgs = { "Closure" };

Просмотреть файл

@ -779,8 +779,8 @@ public class NativeDate extends ScriptableObject {
}
/* the javascript constructor */
public static Object js_Date(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
// if called as a function, just return a string
// representing the current time.
@ -860,7 +860,7 @@ public class NativeDate extends ScriptableObject {
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
public String js_toString() {
public String jsFunction_toString() {
/* all for want of printf. All of this garbage seems necessary
* because Java carefully avoids providing non-localized
* string formatting. We need to avoid localization to ensure
@ -872,7 +872,7 @@ public class NativeDate extends ScriptableObject {
return date_format(this.date);
}
public String js_toLocaleString() {
public String jsFunction_toLocaleString() {
if (this.date != this.date)
return js_NaN_date_str;
@ -883,7 +883,7 @@ public class NativeDate extends ScriptableObject {
return localeDateFormatter.format(tempdate);
}
public String js_toUTCString() {
public String jsFunction_toUTCString() {
if (this.date != this.date)
return js_NaN_date_str;
@ -932,7 +932,7 @@ public class NativeDate extends ScriptableObject {
return result.toString();
}
public double js_valueOf() {
public double jsFunction_valueOf() {
return this.date;
}

Просмотреть файл

@ -183,7 +183,7 @@ public class NativeFunction extends ScriptableObject implements Function {
public String decompile(int indent, boolean toplevel, boolean justbody) {
if (source == null)
return "function " + js_getName() +
return "function " + jsGet_name() +
"() {\n\t[native code]\n}\n";
// Spew tokens in source, for debugging.
@ -820,7 +820,7 @@ public class NativeFunction extends ScriptableObject implements Function {
return result.toString();
}
public static Object js_toString(Context cx, Scriptable thisObj,
public static Object jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Object val = thisObj.getDefaultValue(ScriptRuntime.FunctionClass);
@ -837,7 +837,7 @@ public class NativeFunction extends ScriptableObject implements Function {
return ((NativeFunction) val).decompile(indent, true, false);
}
public static Scriptable js_Function(Context cx, Object[] args,
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
int arglen = args.length;
@ -892,12 +892,12 @@ public class NativeFunction extends ScriptableObject implements Function {
*
* A proposed ECMA extension for round 2.
*/
public static Object js_apply(Context cx, Scriptable thisObj,
public static Object jsFunction_apply(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
if (args.length != 2)
return js_call(cx, thisObj, args, funObj);
return jsFunction_call(cx, thisObj, args, funObj);
Object val = thisObj.getDefaultValue(ScriptRuntime.FunctionClass);
Scriptable newThis = args[0] == null
? null
@ -911,7 +911,7 @@ public class NativeFunction extends ScriptableObject implements Function {
*
* A proposed ECMA extension for round 2.
*/
public static Object js_call(Context cx, Scriptable thisObj,
public static Object jsFunction_call(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
@ -931,7 +931,7 @@ public class NativeFunction extends ScriptableObject implements Function {
}
}
public int js_getLength() {
public int jsGet_length() {
Context cx = Context.getContext();
if (cx.getLanguageVersion() != Context.VERSION_1_2)
return argCount;
@ -941,11 +941,11 @@ public class NativeFunction extends ScriptableObject implements Function {
return activation.getOriginalArguments().length;
}
public int js_getArity() {
public int jsGet_arity() {
return argCount;
}
public String js_getName() {
public String jsGet_name() {
if (names != null && names[0].length() > 0)
return names[0];

Просмотреть файл

@ -47,9 +47,6 @@ public class NativeJavaPackage extends ScriptableObject {
"java.text",
"java.text.resources",
"java.applet",
"netscape.security",
"netscape.plugin",
"netscape.application",
};
public static Scriptable init(Scriptable scope)

Просмотреть файл

@ -59,8 +59,8 @@ public class NativeNumber extends ScriptableObject {
return "Number";
}
public static Object Number(Context cx, Object[] args, Function funObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function funObj, boolean inNewExpr)
{
double d = args.length >= 1
? ScriptRuntime.toNumber(args[0])
@ -73,15 +73,18 @@ public class NativeNumber extends ScriptableObject {
return new Double(d);
}
public String toString(Object base) {
if (base == Undefined.instance)
return ScriptRuntime.numberToString(doubleValue, 10);
else
return ScriptRuntime.numberToString(doubleValue,
ScriptRuntime.toInt32(base));
public String toString() {
return jsFunction_toString(Undefined.instance);
}
public double valueOf() {
public String jsFunction_toString(Object base) {
int i = base == Undefined.instance
? 10
: ScriptRuntime.toInt32(base);
return ScriptRuntime.numberToString(doubleValue, i);
}
public double jsFunction_valueOf() {
return doubleValue;
}

Просмотреть файл

@ -38,8 +38,8 @@ public class NativeObject extends ScriptableObject {
return "Object";
}
public static Object js_Object(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
throws JavaScriptException
{
if (!inNewExpr) {
@ -57,12 +57,12 @@ public class NativeObject extends ScriptableObject {
public String toString() {
Context cx = Context.getContext();
if (cx != null)
return js_toString(cx, this, null, null);
return jsFunction_toString(cx, this, null, null);
else
return "[object " + getClassName() + "]";
}
public static String js_toString(Context cx, Scriptable thisObj,
public static String jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (cx.getLanguageVersion() != cx.VERSION_1_2)
@ -118,7 +118,7 @@ public class NativeObject extends ScriptableObject {
}
}
public static Object js_valueOf(Context cx, Scriptable thisObj,
public static Object jsFunction_valueOf(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
return thisObj;

Просмотреть файл

@ -63,7 +63,7 @@ public class NativeScript extends NativeFunction implements Script {
* The Java method defining the JavaScript Script constructor.
*
*/
public static Object js_Script(Context cx, Object[] args,
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
String source = args.length == 0
@ -95,18 +95,18 @@ public class NativeScript extends NativeFunction implements Script {
}
}
public Scriptable js_compile(String source) {
public Scriptable jsFunction_compile(String source) {
script = compile(null, source);
return this;
}
public Object js_exec() throws JavaScriptException {
public Object jsFunction_exec() throws JavaScriptException {
Object[] msgArgs = { "exec" };
throw Context.reportRuntimeError(
Context.getMessage("msg.cant.call.indirect", msgArgs));
}
public static Object js_toString(Context cx, Scriptable thisObj,
public static Object jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Script thisScript = ((NativeScript) thisObj).script;
@ -119,7 +119,7 @@ public class NativeScript extends NativeFunction implements Script {
/*
* Override method in NativeFunction to avoid ever returning "anonymous"
*/
public String js_getName() {
public String jsGet_name() {
return "";
}

Просмотреть файл

@ -97,8 +97,8 @@ public class NativeString extends ScriptableObject implements Wrapper {
return s.toString();
}
public static Object js_String(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
String s = args.length >= 1
? ScriptRuntime.toString(args[0])
@ -116,11 +116,11 @@ public class NativeString extends ScriptableObject implements Wrapper {
}
/* ECMA 15.5.4.2: 'the toString function is not generic.' */
public String js_toString() {
public String jsFunction_toString() {
return string;
}
public String js_valueOf() {
public String jsFunction_valueOf() {
return string;
}
@ -143,7 +143,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
*
* See ECMA 15.5.4.[4,5]
*/
public static String js_charAt(Context cx, Scriptable thisObj,
public static String jsFunction_charAt(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 1)
@ -160,7 +160,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
return target.substring((int)pos, (int)pos + 1);
}
public static double js_charCodeAt(Context cx, Scriptable thisObj,
public static double jsFunction_charCodeAt(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 1)
@ -181,7 +181,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
* See ECMA 15.5.4.6. Uses Java String.indexOf()
* OPT to add - BMH searching from jsstr.c.
*/
public static int js_indexOf(Context cx, Scriptable thisObj,
public static int jsFunction_indexOf(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 2)
@ -205,7 +205,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
* See ECMA 15.5.4.7
*
*/
public static int js_lastIndexOf(Context cx, Scriptable thisObj,
public static int jsFunction_lastIndexOf(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 2)
@ -350,7 +350,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
* a limit argument and accepts a regular expression as the split
* argument.
*/
public static Object js_split(Context cx, Scriptable thisObj,
public static Object jsFunction_split(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
@ -446,7 +446,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
*
* See ECMA 15.5.4.[9,10]
*/
public static String js_substring(Context cx, Scriptable thisObj,
public static String jsFunction_substring(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 1)
@ -490,28 +490,28 @@ public class NativeString extends ScriptableObject implements Wrapper {
*
* See ECMA 15.5.4.[11,12]
*/
public static String js_toLowerCase(Context cx, Scriptable thisObj,
public static String jsFunction_toLowerCase(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
return target.toLowerCase();
}
public static String js_toUpperCase(Context cx, Scriptable thisObj,
public static String jsFunction_toUpperCase(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
return target.toUpperCase();
}
public double js_getLength() {
public double jsGet_length() {
return (double) string.length();
}
/**
* Non-ECMA methods.
*/
public static String js_substr(Context cx, Scriptable thisObj,
public static String jsFunction_substr(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
@ -548,7 +548,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
/**
* Python-esque sequence operations.
*/
public static String js_concat(Context cx, Scriptable thisObj,
public static String jsFunction_concat(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
StringBuffer result = new StringBuffer();
@ -560,7 +560,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
return result.toString();
}
public static String js_slice(Context cx, Scriptable thisObj,
public static String jsFunction_slice(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
@ -615,55 +615,55 @@ public class NativeString extends ScriptableObject implements Wrapper {
return result.toString();
}
public String js_bold() {
public String jsFunction_bold() {
return tagify("B", null, null);
}
public String js_italics() {
public String jsFunction_italics() {
return tagify("I", null, null);
}
public String js_fixed() {
public String jsFunction_fixed() {
return tagify("TT", null, null);
}
public String js_strike() {
public String jsFunction_strike() {
return tagify("STRIKE", null, null);
}
public String js_small() {
public String jsFunction_small() {
return tagify("SMALL", null, null);
}
public String js_big() {
public String jsFunction_big() {
return tagify("BIG", null, null);
}
public String js_blink() {
public String jsFunction_blink() {
return tagify("BLINK", null, null);
}
public String js_sup() {
public String jsFunction_sup() {
return tagify("SUP", null, null);
}
public String js_sub() {
public String jsFunction_sub() {
return tagify("SUB", null, null);
}
public String js_fontsize(String value) {
public String jsFunction_fontsize(String value) {
return tagify("FONT SIZE", "FONT", value);
}
public String js_fontcolor(String value) {
public String jsFunction_fontcolor(String value) {
return tagify("FONT COLOR", "FONT", value);
}
public String js_link(String value) {
public String jsFunction_link(String value) {
return tagify("A HREF", "A", value);
}
public String js_anchor(String value) {
public String jsFunction_anchor(String value) {
return tagify("A NAME", "A", value);
}
@ -675,21 +675,21 @@ public class NativeString extends ScriptableObject implements Wrapper {
return string;
}
public static Object js_match(Context cx, Scriptable thisObj,
public static Object jsFunction_match(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
return checkReProxy(cx).match(cx, thisObj, args, funObj);
}
public static Object js_search(Context cx, Scriptable thisObj,
public static Object jsFunction_search(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
return checkReProxy(cx).search(cx, thisObj, args, funObj);
}
public static Object js_replace(Context cx, Scriptable thisObj,
public static Object jsFunction_replace(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{

Просмотреть файл

@ -31,7 +31,8 @@ public class NativeWith implements Scriptable {
public static void init(Scriptable scope) {
NativeWith w = new NativeWith();
w.setPrototype(ScriptableObject.getObjectPrototype(scope));
Method[] m = FunctionObject.findMethods(NativeWith.class, "With");
Method[] m = FunctionObject.findMethods(NativeWith.class,
"jsConstructor");
FunctionObject f = new FunctionObject("With", m[0], scope);
f.addAsConstructor(scope, w);
}
@ -120,8 +121,8 @@ public class NativeWith implements Scriptable {
return prototype.hasInstance(value);
}
public static Object With(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
Object[] msgArgs = { "With" };
throw Context.reportRuntimeError(

Просмотреть файл

@ -119,7 +119,6 @@ public abstract class ScriptableObject implements Scriptable {
* @param start the object in which the lookup began
* @return the value of the property (may be null), or NOT_FOUND
*/
public Object get(String name, Scriptable start) {
int hashCode;
if (name == lastName) {
@ -593,50 +592,43 @@ public abstract class ScriptableObject implements Scriptable {
* methods are used to initialize a class in the following manner.<p>
*
* First, the zero-parameter constructor of the class is called to
* create the prototypical instance. If no such constructor exists,
* create the prototype. If no such constructor exists,
* a ClassDefinitionException is thrown. <p>
*
* Next, all methods are scanned. If any method
* begins with a special prefix, then only methods with
* special prefixes are considered for defining functions
* and properties. These special prefixes are
* Next, all methods are scanned for special prefixes that indicate that they
* have special meaning for defining JavaScript objects.
* These special prefixes are
* <ul>
* <li><code>js_</code> for a JavaScript property or function, as
* determined by the name of the method
* <li><code>jsFunction_</code> for a JavaScript function
* <li><code>jsStaticFunction_</code> for a JavaScript function that
* is a property of the constructor
* <li><code>jsProperty_</code> for a JavaScript property
* <li><code>jsGet_</code> for a getter of a JavaScript property
* <li><code>jsSet_</code> for a setter of a JavaScript property
* <li><code>jsConstructor</code> for a JavaScript function that
* is the constructor
* </ul><p>
*
* If no methods begin with any of these prefixes, all methods
* are added as JavaScript functions or properties except
* those with names matching the names of methods in
* <code>org.mozilla.javascript.Function</code>
* or any of its supertypes. This means that
* <code>call</code>, which is a method in Function, and
* <code>get</code>, which is a method in Scriptable
* (which Function extends), are both excluded from
* defining JavaScript methods.<p>
* If the method's name begins with "jsFunction_", a JavaScript function
* is created with a name formed from the rest of the Java method name
* following "jsFunction_". So a Java method named "jsFunction_foo" will
* define a JavaScript method "foo". Calling this JavaScript function
* will cause the Java method to be called. The parameters of the method
* must be of number and types as defined by the FunctionObject class.
* The JavaScript function is then added as a property
* of the prototype. <p>
*
* If after removing any prefixes, a method's name matches the name of
* the class (determined by calling <code>getClassName()</code>
* on the prototypical instance), it is considered to define the
* JavaScript constructor.<p>
* If the method's name begins with "jsStaticFunction_", it is handled
* similarly except that the resulting JavaScript function is added as a
* property of the constructor object. The Java method must be static.
*
* If the method's name begins with "get" or "set" after
* any prefix other than "jsFunction_" is removed, the method is
* If the method's name begins with "jsGet_" or "jsSet_", the method is
* considered to define a property. Accesses to the defined property
* will result in calls to these getter and setter methods. If no
* setter is defined, the property is defined as READONLY.<p>
*
* Otherwise, a JavaScript function is created that will call the
* given method. This function is then added as a property
* of the prototypical instance. So if a JavaScript
* function beginning with "get" or "set" is desired, it must be
* prefixed with "jsFunction_" to distinquish it from a method
* defining a property.<p>
*
* If the method's name is "jsConstructor", the method is
* considered to define the body of the constructor. Only one
* method of this name may be defined.
* If no method is found that can serve as constructor, a Java
* constructor will be selected to serve as the JavaScript
* constructor in the following manner. If the class has only one
@ -644,7 +636,7 @@ public abstract class ScriptableObject implements Scriptable {
* the JavaScript constructor. If the the class has two constructors,
* one must be the zero-argument constructor (otherwise an
* ClassDefinitionException would have already been thrown
* when the prototypical instance was to be created). In this case
* when the prototype was to be created). In this case
* the Java constructor with one or more parameters will be used
* to define the JavaScript constructor. If the class has three
* or more constructors, an ClassDefinitionException
@ -669,8 +661,7 @@ public abstract class ScriptableObject implements Scriptable {
* @exception InvocationTargetException if an exception is thrown
* during execution of methods of the named class
* @exception ClassDefinitionException if an appropriate
* constructor cannot be found to create the prototypical
* instance
* constructor cannot be found to create the prototype
* @exception PropertyException if getter and setter
* methods do not conform to the requirements of the
* defineProperty method
@ -709,8 +700,7 @@ public abstract class ScriptableObject implements Scriptable {
* @exception InvocationTargetException if an exception is thrown
* during execution of methods of the named class
* @exception ClassDefinitionException if an appropriate
* constructor cannot be found to create the prototypical
* instance
* constructor cannot be found to create the prototype
* @exception PropertyException if getter and setter
* methods do not conform to the requirements of the
* defineProperty method
@ -763,13 +753,29 @@ public abstract class ScriptableObject implements Scriptable {
// Find out whether there are any methods that begin with
// "js". If so, then only methods that begin with special
// prefixes will be defined as JavaScript entities.
// The prefixes "js_" and "jsProperty_" are deprecated.
final String genericPrefix = "js_";
final String functionPrefix = "jsFunction_";
final String staticFunctionPrefix = "jsStaticFunction_";
final String propertyPrefix = "jsProperty_";
final String getterPrefix = "jsGet_";
final String setterPrefix = "jsSet_";
boolean hasPrefix = false;
Method[] ctorMeths = FunctionObject.findMethods(clazz,
"jsConstructor");
Member ctorMember = null;
if (ctorMeths != null) {
if (ctorMeths.length > 1) {
Object[] args = { ctorMeths[0], ctorMeths[1] };
throw new ClassDefinitionException(
Context.getMessage("msg.multiple.ctors", args));
}
ctorMember = ctorMeths[0];
}
// Deprecated: look for functions with the same name as the class
// and consider them constructors.
boolean hasPrefix = false;
for (int i=0; i < methods.length; i++) {
String name = methods[i].getName();
String prefix = null;
@ -781,8 +787,6 @@ public abstract class ScriptableObject implements Scriptable {
prefix = functionPrefix;
else if (name.startsWith(staticFunctionPrefix))
prefix = staticFunctionPrefix;
else if (name.startsWith(propertyPrefix))
prefix = propertyPrefix;
if (prefix != null) {
hasPrefix = true;
name = name.substring(prefix.length());
@ -816,9 +820,11 @@ public abstract class ScriptableObject implements Scriptable {
FunctionObject ctor = new FunctionObject(className, ctorMember, scope);
ctor.addAsConstructor(scope, proto);
if (!hasPrefix && exclusionList == null)
exclusionList = getExclusionList();
Method finishInit = null;
for (int i=0; i < methods.length; i++) {
if (methods[i].getDeclaringClass() != clazz)
if (!hasPrefix && methods[i].getDeclaringClass() != clazz)
continue;
String name = methods[i].getName();
if (name.equals("finishInit")) {
@ -836,13 +842,10 @@ public abstract class ScriptableObject implements Scriptable {
String prefix = null;
if (hasPrefix) {
if (name.startsWith(genericPrefix)) {
name = name.substring(genericPrefix.length());
prefix = genericPrefix;
} else if (name.startsWith(functionPrefix)) {
name = name.substring(functionPrefix.length());
prefix = functionPrefix;
} else if (name.startsWith(staticFunctionPrefix)) {
name = name.substring(staticFunctionPrefix.length());
prefix = staticFunctionPrefix;
if (!Modifier.isStatic(methods[i].getModifiers())) {
throw new ClassDefinitionException(
@ -851,16 +854,47 @@ public abstract class ScriptableObject implements Scriptable {
} else if (name.startsWith(propertyPrefix)) {
name = name.substring(propertyPrefix.length());
prefix = propertyPrefix;
} else if (name.startsWith(getterPrefix)) {
prefix = getterPrefix;
} else if (name.startsWith(setterPrefix)) {
prefix = setterPrefix;
} else {
continue;
}
name = name.substring(prefix.length());
} else if (exclusionList.get(name) != null)
continue;
if (methods[i] == ctorMember) {
continue;
}
if ((name.startsWith("get") || name.startsWith("set"))
&& name.length() > 3 &&
if (prefix != null && prefix.equals(setterPrefix))
continue; // deal with set when we see get
if (prefix != null && prefix.equals(getterPrefix)) {
if (!(proto instanceof ScriptableObject)) {
Object[] args = { proto.getClass().toString(), name };
throw new PropertyException(
Context.getMessage("msg.extend.scriptable", args));
}
Method[] setter = FunctionObject.findMethods(
clazz,
setterPrefix + name);
if (setter != null && setter.length != 1) {
Object[] errArgs = { name, clazz.getName() };
throw new PropertyException(
Context.getMessage("msg.no.overload", errArgs));
}
int attr = ScriptableObject.PERMANENT |
ScriptableObject.DONTENUM |
(setter != null ? 0
: ScriptableObject.READONLY);
Method m = setter == null ? null : setter[0];
((ScriptableObject) proto).defineProperty(name, null,
methods[i], m,
attr);
continue;
}
if ((name.startsWith("get") || name.startsWith("set")) &&
name.length() > 3 &&
!(hasPrefix && (prefix.equals(functionPrefix) ||
prefix.equals(staticFunctionPrefix))))
{

Просмотреть файл

@ -203,7 +203,7 @@ public class NativeRegExp extends ScriptableObject implements Function {
boolean test, Function funObj)
{
if (!(thisObj instanceof NativeRegExp)) {
Object[] errArgs = { ((NativeFunction) funObj).js_getName() };
Object[] errArgs = { ((NativeFunction) funObj).jsGet_name() };
throw Context.reportRuntimeError(
ScriptRuntime.getMessage("msg.incompat.call", errArgs));
}

Просмотреть файл

@ -43,7 +43,7 @@ import org.mozilla.javascript.debug.SourceTextManagerImpl;
*/
public class Main extends ScriptableObject {
public class Main extends ImporterTopLevel {
/**
* Main entry point.
@ -108,6 +108,10 @@ public class Main extends ScriptableObject {
global.defineProperty("environment", environment,
ScriptableObject.DONTENUM);
global.history = (NativeArray) cx.newArray(global, 0);
global.defineProperty("history", global.history,
ScriptableObject.DONTENUM);
/*
TODO: enable debugger
if (global.debug) {
@ -472,6 +476,8 @@ public class Main extends ScriptableObject {
if (result != cx.getUndefinedValue()) {
System.err.println(cx.toString(result));
}
NativeArray h = global.history;
h.put((int)h.jsGet_length(), h, source);
}
catch (WrappedException we) {
// Some form of exception was caught by JavaScript and
@ -579,6 +585,7 @@ public class Main extends ScriptableObject {
boolean debug = false;
boolean processStdin = true;
boolean showDebuggerUI = false;
NativeArray history;
}

Просмотреть файл

@ -0,0 +1,109 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
package org.mozilla.javascript;
import java.util.Vector;
/**
* Class ImporterTopLevel
*
* This class defines a ScriptableObject that can be instantiated
* as a top-level ("global") object to provide functionality similar
* to Java's "import" statement.
* <p>
* This class can be used to create a top-level scope using the following code:
* <pre>
* Scriptable scope = cx.initStandardObjects(new ImporterTopLevel());
* </pre>
* Then JavaScript code will have access to the following methods:
* <ul>
* <li>importClass - will "import" a class by making its unqualified name
* available as a property of the top-level scope
* <li>importPackage - will "import" all the classes of the package by
* searching for unqualified names as classes qualified
* by the given package.
* </ul>
* The following code from the shell illustrates this use:
* <pre>
* js> importClass(java.io.File)
* js> f = new File('help.txt')
* help.txt
* js> importPackage(java.util)
* js> v = new Vector()
* []
*
* @author Norris Boyd
*/
public class ImporterTopLevel extends ScriptableObject {
public ImporterTopLevel() {
String[] names = { "importClass", "importPackage" };
try {
this.defineFunctionProperties(names, ImporterTopLevel.class,
ScriptableObject.DONTENUM);
} catch (PropertyException e) {
throw new Error(); // should never happen
}
}
public String getClassName() {
return "global";
}
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result == NOT_FOUND && importedPackages != null) {
for (int i=0; i < importedPackages.size(); i++) {
Object o = importedPackages.elementAt(i);
NativeJavaPackage p = (NativeJavaPackage) o;
Object v = p.get(name, start);
if (v != NOT_FOUND) {
if (result == NOT_FOUND)
result = v;
else
throw new EvaluatorException("Ambiguous import: " +
result + " and " + v);
}
}
}
return result;
}
public void importClass(Object cl) {
if (!(cl instanceof NativeJavaClass))
throw new EvaluatorException("not a class");// TODO: better msg
String s = ((NativeJavaClass) cl).getClassObject().getName();
String n = s.substring(s.lastIndexOf('.')+1);
if (this.has(n, this))
throw new EvaluatorException("property " + n +
" is already defined");
this.defineProperty(n, cl, DONTENUM);
}
public void importPackage(Object pkg) {
if (importedPackages == null)
importedPackages = new Vector();
if (!(pkg instanceof NativeJavaPackage))
throw new EvaluatorException("not a package");// TODO: better msg
importedPackages.addElement(pkg);
}
private Vector importedPackages;
}

Просмотреть файл

@ -178,8 +178,8 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.1,2
*/
public static Object js_Array(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
throws JavaScriptException
{
if (!inNewExpr) {
@ -205,11 +205,11 @@ public class NativeArray extends ScriptableObject {
private static final int lengthAttr = ScriptableObject.DONTENUM |
ScriptableObject.PERMANENT;
public long js_getLength() {
public long jsGet_length() {
return length;
}
public void js_setLength(Object val) {
public void jsSet_length(Object val) {
/* XXX do we satisfy this?
* 15.4.5.1 [[Put]](P, V):
* 1. Call the [[CanPut]] method of A with name P.
@ -258,9 +258,9 @@ public class NativeArray extends ScriptableObject {
static double getLengthProperty(Scriptable obj) {
// These will both give numeric lengths within Uint32 range.
if (obj instanceof NativeString)
return (double)((NativeString)obj).js_getLength();
return (double)((NativeString)obj).jsGet_length();
if (obj instanceof NativeArray)
return (double)((NativeArray)obj).js_getLength();
return (double)((NativeArray)obj).jsGet_length();
return ScriptRuntime.toUint32(ScriptRuntime
.getProp(obj, "length", obj));
}
@ -309,7 +309,7 @@ public class NativeArray extends ScriptableObject {
}
}
public static String js_toString(Context cx, Scriptable thisObj,
public static String jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
return toStringHelper(cx, thisObj,
@ -393,7 +393,7 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.4.3
*/
public static String js_join(Context cx, Scriptable thisObj,
public static String jsFunction_join(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
StringBuffer result = new StringBuffer();
@ -421,7 +421,7 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.4.4
*/
public static Scriptable js_reverse(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_reverse(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
long len = (long)getLengthProperty(thisObj);
@ -440,7 +440,7 @@ public class NativeArray extends ScriptableObject {
/**
* See ECMA 15.4.4.5
*/
public static Scriptable js_sort(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_sort(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
@ -605,7 +605,7 @@ public class NativeArray extends ScriptableObject {
* Non-ECMA methods.
*/
public static Object js_push(Context cx, Scriptable thisObj,
public static Object jsFunction_push(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
double length = getLengthProperty(thisObj);
@ -630,8 +630,8 @@ public class NativeArray extends ScriptableObject {
return new Long((long)length);
}
public static Object js_pop(Context cx, Scriptable thisObj, Object[] args,
Function funObj)
public static Object jsFunction_pop(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Object result;
double length = getLengthProperty(thisObj);
@ -653,8 +653,8 @@ public class NativeArray extends ScriptableObject {
return result;
}
public static Object js_shift(Context cx, Scriptable thisObj, Object[] args,
Function funOjb)
public static Object jsFunction_shift(Context cx, Scriptable thisObj,
Object[] args, Function funOjb)
{
Object result;
double length = getLengthProperty(thisObj);
@ -684,7 +684,7 @@ public class NativeArray extends ScriptableObject {
return result;
}
public static Object js_unshift(Context cx, Scriptable thisObj,
public static Object jsFunction_unshift(Context cx, Scriptable thisObj,
Object[] args, Function funOjb)
{
Object result;
@ -713,7 +713,7 @@ public class NativeArray extends ScriptableObject {
return new Long((long)length);
}
public static Object js_splice(Context cx, Scriptable thisObj,
public static Object jsFunction_splice(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
/* create an empty Array to return. */
@ -820,7 +820,7 @@ public class NativeArray extends ScriptableObject {
/*
* Python-esque sequence operations.
*/
public static Scriptable js_concat(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_concat(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
/* Concat tries to keep the definition of an array as general
@ -873,7 +873,7 @@ public class NativeArray extends ScriptableObject {
return result;
}
public static Scriptable js_slice(Context cx, Scriptable thisObj,
public static Scriptable jsFunction_slice(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Scriptable scope = getTopLevelScope(funObj);

Просмотреть файл

@ -47,8 +47,8 @@ public class NativeBoolean extends ScriptableObject {
return super.getDefaultValue(typeHint);
}
public static Object Boolean(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
boolean b = args.length >= 1
? ScriptRuntime.toBoolean(args[0])
@ -62,11 +62,11 @@ public class NativeBoolean extends ScriptableObject {
return b ? Boolean.TRUE : Boolean.FALSE;
}
public String toString() {
public String jsFunction_toString() {
return booleanValue ? "true" : "false";
}
public boolean valueOf() {
public boolean jsFunction_valueOf() {
return booleanValue;
}

Просмотреть файл

@ -80,8 +80,8 @@ public final class NativeCall extends ScriptableObject {
return "Call";
}
public static Object js_Call(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
if (!inNewExpr) {
Object[] errArgs = { "Call" };

Просмотреть файл

@ -779,8 +779,8 @@ public class NativeDate extends ScriptableObject {
}
/* the javascript constructor */
public static Object js_Date(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
// if called as a function, just return a string
// representing the current time.
@ -860,7 +860,7 @@ public class NativeDate extends ScriptableObject {
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
public String js_toString() {
public String jsFunction_toString() {
/* all for want of printf. All of this garbage seems necessary
* because Java carefully avoids providing non-localized
* string formatting. We need to avoid localization to ensure
@ -872,7 +872,7 @@ public class NativeDate extends ScriptableObject {
return date_format(this.date);
}
public String js_toLocaleString() {
public String jsFunction_toLocaleString() {
if (this.date != this.date)
return js_NaN_date_str;
@ -883,7 +883,7 @@ public class NativeDate extends ScriptableObject {
return localeDateFormatter.format(tempdate);
}
public String js_toUTCString() {
public String jsFunction_toUTCString() {
if (this.date != this.date)
return js_NaN_date_str;
@ -932,7 +932,7 @@ public class NativeDate extends ScriptableObject {
return result.toString();
}
public double js_valueOf() {
public double jsFunction_valueOf() {
return this.date;
}

Просмотреть файл

@ -183,7 +183,7 @@ public class NativeFunction extends ScriptableObject implements Function {
public String decompile(int indent, boolean toplevel, boolean justbody) {
if (source == null)
return "function " + js_getName() +
return "function " + jsGet_name() +
"() {\n\t[native code]\n}\n";
// Spew tokens in source, for debugging.
@ -820,7 +820,7 @@ public class NativeFunction extends ScriptableObject implements Function {
return result.toString();
}
public static Object js_toString(Context cx, Scriptable thisObj,
public static Object jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Object val = thisObj.getDefaultValue(ScriptRuntime.FunctionClass);
@ -837,7 +837,7 @@ public class NativeFunction extends ScriptableObject implements Function {
return ((NativeFunction) val).decompile(indent, true, false);
}
public static Scriptable js_Function(Context cx, Object[] args,
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
int arglen = args.length;
@ -892,12 +892,12 @@ public class NativeFunction extends ScriptableObject implements Function {
*
* A proposed ECMA extension for round 2.
*/
public static Object js_apply(Context cx, Scriptable thisObj,
public static Object jsFunction_apply(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
if (args.length != 2)
return js_call(cx, thisObj, args, funObj);
return jsFunction_call(cx, thisObj, args, funObj);
Object val = thisObj.getDefaultValue(ScriptRuntime.FunctionClass);
Scriptable newThis = args[0] == null
? null
@ -911,7 +911,7 @@ public class NativeFunction extends ScriptableObject implements Function {
*
* A proposed ECMA extension for round 2.
*/
public static Object js_call(Context cx, Scriptable thisObj,
public static Object jsFunction_call(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
@ -931,7 +931,7 @@ public class NativeFunction extends ScriptableObject implements Function {
}
}
public int js_getLength() {
public int jsGet_length() {
Context cx = Context.getContext();
if (cx.getLanguageVersion() != Context.VERSION_1_2)
return argCount;
@ -941,11 +941,11 @@ public class NativeFunction extends ScriptableObject implements Function {
return activation.getOriginalArguments().length;
}
public int js_getArity() {
public int jsGet_arity() {
return argCount;
}
public String js_getName() {
public String jsGet_name() {
if (names != null && names[0].length() > 0)
return names[0];

Просмотреть файл

@ -47,9 +47,6 @@ public class NativeJavaPackage extends ScriptableObject {
"java.text",
"java.text.resources",
"java.applet",
"netscape.security",
"netscape.plugin",
"netscape.application",
};
public static Scriptable init(Scriptable scope)

Просмотреть файл

@ -59,8 +59,8 @@ public class NativeNumber extends ScriptableObject {
return "Number";
}
public static Object Number(Context cx, Object[] args, Function funObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function funObj, boolean inNewExpr)
{
double d = args.length >= 1
? ScriptRuntime.toNumber(args[0])
@ -73,15 +73,18 @@ public class NativeNumber extends ScriptableObject {
return new Double(d);
}
public String toString(Object base) {
if (base == Undefined.instance)
return ScriptRuntime.numberToString(doubleValue, 10);
else
return ScriptRuntime.numberToString(doubleValue,
ScriptRuntime.toInt32(base));
public String toString() {
return jsFunction_toString(Undefined.instance);
}
public double valueOf() {
public String jsFunction_toString(Object base) {
int i = base == Undefined.instance
? 10
: ScriptRuntime.toInt32(base);
return ScriptRuntime.numberToString(doubleValue, i);
}
public double jsFunction_valueOf() {
return doubleValue;
}

Просмотреть файл

@ -38,8 +38,8 @@ public class NativeObject extends ScriptableObject {
return "Object";
}
public static Object js_Object(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
throws JavaScriptException
{
if (!inNewExpr) {
@ -57,12 +57,12 @@ public class NativeObject extends ScriptableObject {
public String toString() {
Context cx = Context.getContext();
if (cx != null)
return js_toString(cx, this, null, null);
return jsFunction_toString(cx, this, null, null);
else
return "[object " + getClassName() + "]";
}
public static String js_toString(Context cx, Scriptable thisObj,
public static String jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (cx.getLanguageVersion() != cx.VERSION_1_2)
@ -118,7 +118,7 @@ public class NativeObject extends ScriptableObject {
}
}
public static Object js_valueOf(Context cx, Scriptable thisObj,
public static Object jsFunction_valueOf(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
return thisObj;

Просмотреть файл

@ -63,7 +63,7 @@ public class NativeScript extends NativeFunction implements Script {
* The Java method defining the JavaScript Script constructor.
*
*/
public static Object js_Script(Context cx, Object[] args,
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
String source = args.length == 0
@ -95,18 +95,18 @@ public class NativeScript extends NativeFunction implements Script {
}
}
public Scriptable js_compile(String source) {
public Scriptable jsFunction_compile(String source) {
script = compile(null, source);
return this;
}
public Object js_exec() throws JavaScriptException {
public Object jsFunction_exec() throws JavaScriptException {
Object[] msgArgs = { "exec" };
throw Context.reportRuntimeError(
Context.getMessage("msg.cant.call.indirect", msgArgs));
}
public static Object js_toString(Context cx, Scriptable thisObj,
public static Object jsFunction_toString(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
Script thisScript = ((NativeScript) thisObj).script;
@ -119,7 +119,7 @@ public class NativeScript extends NativeFunction implements Script {
/*
* Override method in NativeFunction to avoid ever returning "anonymous"
*/
public String js_getName() {
public String jsGet_name() {
return "";
}

Просмотреть файл

@ -97,8 +97,8 @@ public class NativeString extends ScriptableObject implements Wrapper {
return s.toString();
}
public static Object js_String(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
String s = args.length >= 1
? ScriptRuntime.toString(args[0])
@ -116,11 +116,11 @@ public class NativeString extends ScriptableObject implements Wrapper {
}
/* ECMA 15.5.4.2: 'the toString function is not generic.' */
public String js_toString() {
public String jsFunction_toString() {
return string;
}
public String js_valueOf() {
public String jsFunction_valueOf() {
return string;
}
@ -143,7 +143,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
*
* See ECMA 15.5.4.[4,5]
*/
public static String js_charAt(Context cx, Scriptable thisObj,
public static String jsFunction_charAt(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 1)
@ -160,7 +160,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
return target.substring((int)pos, (int)pos + 1);
}
public static double js_charCodeAt(Context cx, Scriptable thisObj,
public static double jsFunction_charCodeAt(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 1)
@ -181,7 +181,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
* See ECMA 15.5.4.6. Uses Java String.indexOf()
* OPT to add - BMH searching from jsstr.c.
*/
public static int js_indexOf(Context cx, Scriptable thisObj,
public static int jsFunction_indexOf(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 2)
@ -205,7 +205,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
* See ECMA 15.5.4.7
*
*/
public static int js_lastIndexOf(Context cx, Scriptable thisObj,
public static int jsFunction_lastIndexOf(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 2)
@ -350,7 +350,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
* a limit argument and accepts a regular expression as the split
* argument.
*/
public static Object js_split(Context cx, Scriptable thisObj,
public static Object jsFunction_split(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
@ -446,7 +446,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
*
* See ECMA 15.5.4.[9,10]
*/
public static String js_substring(Context cx, Scriptable thisObj,
public static String jsFunction_substring(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
if (args.length < 1)
@ -490,28 +490,28 @@ public class NativeString extends ScriptableObject implements Wrapper {
*
* See ECMA 15.5.4.[11,12]
*/
public static String js_toLowerCase(Context cx, Scriptable thisObj,
public static String jsFunction_toLowerCase(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
return target.toLowerCase();
}
public static String js_toUpperCase(Context cx, Scriptable thisObj,
public static String jsFunction_toUpperCase(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
return target.toUpperCase();
}
public double js_getLength() {
public double jsGet_length() {
return (double) string.length();
}
/**
* Non-ECMA methods.
*/
public static String js_substr(Context cx, Scriptable thisObj,
public static String jsFunction_substr(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
@ -548,7 +548,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
/**
* Python-esque sequence operations.
*/
public static String js_concat(Context cx, Scriptable thisObj,
public static String jsFunction_concat(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
StringBuffer result = new StringBuffer();
@ -560,7 +560,7 @@ public class NativeString extends ScriptableObject implements Wrapper {
return result.toString();
}
public static String js_slice(Context cx, Scriptable thisObj,
public static String jsFunction_slice(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
String target = ScriptRuntime.toString(thisObj);
@ -615,55 +615,55 @@ public class NativeString extends ScriptableObject implements Wrapper {
return result.toString();
}
public String js_bold() {
public String jsFunction_bold() {
return tagify("B", null, null);
}
public String js_italics() {
public String jsFunction_italics() {
return tagify("I", null, null);
}
public String js_fixed() {
public String jsFunction_fixed() {
return tagify("TT", null, null);
}
public String js_strike() {
public String jsFunction_strike() {
return tagify("STRIKE", null, null);
}
public String js_small() {
public String jsFunction_small() {
return tagify("SMALL", null, null);
}
public String js_big() {
public String jsFunction_big() {
return tagify("BIG", null, null);
}
public String js_blink() {
public String jsFunction_blink() {
return tagify("BLINK", null, null);
}
public String js_sup() {
public String jsFunction_sup() {
return tagify("SUP", null, null);
}
public String js_sub() {
public String jsFunction_sub() {
return tagify("SUB", null, null);
}
public String js_fontsize(String value) {
public String jsFunction_fontsize(String value) {
return tagify("FONT SIZE", "FONT", value);
}
public String js_fontcolor(String value) {
public String jsFunction_fontcolor(String value) {
return tagify("FONT COLOR", "FONT", value);
}
public String js_link(String value) {
public String jsFunction_link(String value) {
return tagify("A HREF", "A", value);
}
public String js_anchor(String value) {
public String jsFunction_anchor(String value) {
return tagify("A NAME", "A", value);
}
@ -675,21 +675,21 @@ public class NativeString extends ScriptableObject implements Wrapper {
return string;
}
public static Object js_match(Context cx, Scriptable thisObj,
public static Object jsFunction_match(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
return checkReProxy(cx).match(cx, thisObj, args, funObj);
}
public static Object js_search(Context cx, Scriptable thisObj,
public static Object jsFunction_search(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{
return checkReProxy(cx).search(cx, thisObj, args, funObj);
}
public static Object js_replace(Context cx, Scriptable thisObj,
public static Object jsFunction_replace(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws JavaScriptException
{

Просмотреть файл

@ -31,7 +31,8 @@ public class NativeWith implements Scriptable {
public static void init(Scriptable scope) {
NativeWith w = new NativeWith();
w.setPrototype(ScriptableObject.getObjectPrototype(scope));
Method[] m = FunctionObject.findMethods(NativeWith.class, "With");
Method[] m = FunctionObject.findMethods(NativeWith.class,
"jsConstructor");
FunctionObject f = new FunctionObject("With", m[0], scope);
f.addAsConstructor(scope, w);
}
@ -120,8 +121,8 @@ public class NativeWith implements Scriptable {
return prototype.hasInstance(value);
}
public static Object With(Context cx, Object[] args, Function ctorObj,
boolean inNewExpr)
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
Object[] msgArgs = { "With" };
throw Context.reportRuntimeError(

Просмотреть файл

@ -119,7 +119,6 @@ public abstract class ScriptableObject implements Scriptable {
* @param start the object in which the lookup began
* @return the value of the property (may be null), or NOT_FOUND
*/
public Object get(String name, Scriptable start) {
int hashCode;
if (name == lastName) {
@ -593,50 +592,43 @@ public abstract class ScriptableObject implements Scriptable {
* methods are used to initialize a class in the following manner.<p>
*
* First, the zero-parameter constructor of the class is called to
* create the prototypical instance. If no such constructor exists,
* create the prototype. If no such constructor exists,
* a ClassDefinitionException is thrown. <p>
*
* Next, all methods are scanned. If any method
* begins with a special prefix, then only methods with
* special prefixes are considered for defining functions
* and properties. These special prefixes are
* Next, all methods are scanned for special prefixes that indicate that they
* have special meaning for defining JavaScript objects.
* These special prefixes are
* <ul>
* <li><code>js_</code> for a JavaScript property or function, as
* determined by the name of the method
* <li><code>jsFunction_</code> for a JavaScript function
* <li><code>jsStaticFunction_</code> for a JavaScript function that
* is a property of the constructor
* <li><code>jsProperty_</code> for a JavaScript property
* <li><code>jsGet_</code> for a getter of a JavaScript property
* <li><code>jsSet_</code> for a setter of a JavaScript property
* <li><code>jsConstructor</code> for a JavaScript function that
* is the constructor
* </ul><p>
*
* If no methods begin with any of these prefixes, all methods
* are added as JavaScript functions or properties except
* those with names matching the names of methods in
* <code>org.mozilla.javascript.Function</code>
* or any of its supertypes. This means that
* <code>call</code>, which is a method in Function, and
* <code>get</code>, which is a method in Scriptable
* (which Function extends), are both excluded from
* defining JavaScript methods.<p>
* If the method's name begins with "jsFunction_", a JavaScript function
* is created with a name formed from the rest of the Java method name
* following "jsFunction_". So a Java method named "jsFunction_foo" will
* define a JavaScript method "foo". Calling this JavaScript function
* will cause the Java method to be called. The parameters of the method
* must be of number and types as defined by the FunctionObject class.
* The JavaScript function is then added as a property
* of the prototype. <p>
*
* If after removing any prefixes, a method's name matches the name of
* the class (determined by calling <code>getClassName()</code>
* on the prototypical instance), it is considered to define the
* JavaScript constructor.<p>
* If the method's name begins with "jsStaticFunction_", it is handled
* similarly except that the resulting JavaScript function is added as a
* property of the constructor object. The Java method must be static.
*
* If the method's name begins with "get" or "set" after
* any prefix other than "jsFunction_" is removed, the method is
* If the method's name begins with "jsGet_" or "jsSet_", the method is
* considered to define a property. Accesses to the defined property
* will result in calls to these getter and setter methods. If no
* setter is defined, the property is defined as READONLY.<p>
*
* Otherwise, a JavaScript function is created that will call the
* given method. This function is then added as a property
* of the prototypical instance. So if a JavaScript
* function beginning with "get" or "set" is desired, it must be
* prefixed with "jsFunction_" to distinquish it from a method
* defining a property.<p>
*
* If the method's name is "jsConstructor", the method is
* considered to define the body of the constructor. Only one
* method of this name may be defined.
* If no method is found that can serve as constructor, a Java
* constructor will be selected to serve as the JavaScript
* constructor in the following manner. If the class has only one
@ -644,7 +636,7 @@ public abstract class ScriptableObject implements Scriptable {
* the JavaScript constructor. If the the class has two constructors,
* one must be the zero-argument constructor (otherwise an
* ClassDefinitionException would have already been thrown
* when the prototypical instance was to be created). In this case
* when the prototype was to be created). In this case
* the Java constructor with one or more parameters will be used
* to define the JavaScript constructor. If the class has three
* or more constructors, an ClassDefinitionException
@ -669,8 +661,7 @@ public abstract class ScriptableObject implements Scriptable {
* @exception InvocationTargetException if an exception is thrown
* during execution of methods of the named class
* @exception ClassDefinitionException if an appropriate
* constructor cannot be found to create the prototypical
* instance
* constructor cannot be found to create the prototype
* @exception PropertyException if getter and setter
* methods do not conform to the requirements of the
* defineProperty method
@ -709,8 +700,7 @@ public abstract class ScriptableObject implements Scriptable {
* @exception InvocationTargetException if an exception is thrown
* during execution of methods of the named class
* @exception ClassDefinitionException if an appropriate
* constructor cannot be found to create the prototypical
* instance
* constructor cannot be found to create the prototype
* @exception PropertyException if getter and setter
* methods do not conform to the requirements of the
* defineProperty method
@ -763,13 +753,29 @@ public abstract class ScriptableObject implements Scriptable {
// Find out whether there are any methods that begin with
// "js". If so, then only methods that begin with special
// prefixes will be defined as JavaScript entities.
// The prefixes "js_" and "jsProperty_" are deprecated.
final String genericPrefix = "js_";
final String functionPrefix = "jsFunction_";
final String staticFunctionPrefix = "jsStaticFunction_";
final String propertyPrefix = "jsProperty_";
final String getterPrefix = "jsGet_";
final String setterPrefix = "jsSet_";
boolean hasPrefix = false;
Method[] ctorMeths = FunctionObject.findMethods(clazz,
"jsConstructor");
Member ctorMember = null;
if (ctorMeths != null) {
if (ctorMeths.length > 1) {
Object[] args = { ctorMeths[0], ctorMeths[1] };
throw new ClassDefinitionException(
Context.getMessage("msg.multiple.ctors", args));
}
ctorMember = ctorMeths[0];
}
// Deprecated: look for functions with the same name as the class
// and consider them constructors.
boolean hasPrefix = false;
for (int i=0; i < methods.length; i++) {
String name = methods[i].getName();
String prefix = null;
@ -781,8 +787,6 @@ public abstract class ScriptableObject implements Scriptable {
prefix = functionPrefix;
else if (name.startsWith(staticFunctionPrefix))
prefix = staticFunctionPrefix;
else if (name.startsWith(propertyPrefix))
prefix = propertyPrefix;
if (prefix != null) {
hasPrefix = true;
name = name.substring(prefix.length());
@ -816,9 +820,11 @@ public abstract class ScriptableObject implements Scriptable {
FunctionObject ctor = new FunctionObject(className, ctorMember, scope);
ctor.addAsConstructor(scope, proto);
if (!hasPrefix && exclusionList == null)
exclusionList = getExclusionList();
Method finishInit = null;
for (int i=0; i < methods.length; i++) {
if (methods[i].getDeclaringClass() != clazz)
if (!hasPrefix && methods[i].getDeclaringClass() != clazz)
continue;
String name = methods[i].getName();
if (name.equals("finishInit")) {
@ -836,13 +842,10 @@ public abstract class ScriptableObject implements Scriptable {
String prefix = null;
if (hasPrefix) {
if (name.startsWith(genericPrefix)) {
name = name.substring(genericPrefix.length());
prefix = genericPrefix;
} else if (name.startsWith(functionPrefix)) {
name = name.substring(functionPrefix.length());
prefix = functionPrefix;
} else if (name.startsWith(staticFunctionPrefix)) {
name = name.substring(staticFunctionPrefix.length());
prefix = staticFunctionPrefix;
if (!Modifier.isStatic(methods[i].getModifiers())) {
throw new ClassDefinitionException(
@ -851,16 +854,47 @@ public abstract class ScriptableObject implements Scriptable {
} else if (name.startsWith(propertyPrefix)) {
name = name.substring(propertyPrefix.length());
prefix = propertyPrefix;
} else if (name.startsWith(getterPrefix)) {
prefix = getterPrefix;
} else if (name.startsWith(setterPrefix)) {
prefix = setterPrefix;
} else {
continue;
}
name = name.substring(prefix.length());
} else if (exclusionList.get(name) != null)
continue;
if (methods[i] == ctorMember) {
continue;
}
if ((name.startsWith("get") || name.startsWith("set"))
&& name.length() > 3 &&
if (prefix != null && prefix.equals(setterPrefix))
continue; // deal with set when we see get
if (prefix != null && prefix.equals(getterPrefix)) {
if (!(proto instanceof ScriptableObject)) {
Object[] args = { proto.getClass().toString(), name };
throw new PropertyException(
Context.getMessage("msg.extend.scriptable", args));
}
Method[] setter = FunctionObject.findMethods(
clazz,
setterPrefix + name);
if (setter != null && setter.length != 1) {
Object[] errArgs = { name, clazz.getName() };
throw new PropertyException(
Context.getMessage("msg.no.overload", errArgs));
}
int attr = ScriptableObject.PERMANENT |
ScriptableObject.DONTENUM |
(setter != null ? 0
: ScriptableObject.READONLY);
Method m = setter == null ? null : setter[0];
((ScriptableObject) proto).defineProperty(name, null,
methods[i], m,
attr);
continue;
}
if ((name.startsWith("get") || name.startsWith("set")) &&
name.length() > 3 &&
!(hasPrefix && (prefix.equals(functionPrefix) ||
prefix.equals(staticFunctionPrefix))))
{

Просмотреть файл

@ -203,7 +203,7 @@ public class NativeRegExp extends ScriptableObject implements Function {
boolean test, Function funObj)
{
if (!(thisObj instanceof NativeRegExp)) {
Object[] errArgs = { ((NativeFunction) funObj).js_getName() };
Object[] errArgs = { ((NativeFunction) funObj).jsGet_name() };
throw Context.reportRuntimeError(
ScriptRuntime.getMessage("msg.incompat.call", errArgs));
}

Просмотреть файл

@ -43,7 +43,7 @@ import org.mozilla.javascript.debug.SourceTextManagerImpl;
*/
public class Main extends ScriptableObject {
public class Main extends ImporterTopLevel {
/**
* Main entry point.
@ -108,6 +108,10 @@ public class Main extends ScriptableObject {
global.defineProperty("environment", environment,
ScriptableObject.DONTENUM);
global.history = (NativeArray) cx.newArray(global, 0);
global.defineProperty("history", global.history,
ScriptableObject.DONTENUM);
/*
TODO: enable debugger
if (global.debug) {
@ -472,6 +476,8 @@ public class Main extends ScriptableObject {
if (result != cx.getUndefinedValue()) {
System.err.println(cx.toString(result));
}
NativeArray h = global.history;
h.put((int)h.jsGet_length(), h, source);
}
catch (WrappedException we) {
// Some form of exception was caught by JavaScript and
@ -579,6 +585,7 @@ public class Main extends ScriptableObject {
boolean debug = false;
boolean processStdin = true;
boolean showDebuggerUI = false;
NativeArray history;
}