Avoid duplicated getMessage* code in ScriptRuntime and Context: now all the necessary code is moved to ScriptRuntime and the rest of code is updated to call ScriptRuntime

This commit is contained in:
igor%mir2.org 2004-09-25 18:11:44 +00:00
Родитель cf3965d900
Коммит 5ec731db71
4 изменённых файлов: 83 добавлений и 115 удалений

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

@ -54,8 +54,6 @@ import java.beans.*;
import java.io.*;
import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;
import java.text.MessageFormat;
import java.lang.reflect.*;
import org.mozilla.javascript.debug.*;
@ -811,7 +809,8 @@ public class Context
// with special build preprocessing but that would require some ant
// tweaking and then replacing token in resource files was simpler
if (implementationVersion == null) {
implementationVersion = getMessage0("implementation.version");
implementationVersion
= ScriptRuntime.getMessage0("implementation.version");
}
return implementationVersion;
}
@ -1039,21 +1038,21 @@ public class Context
static EvaluatorException reportRuntimeError0(String messageId)
{
String msg = getMessage0(messageId);
String msg = ScriptRuntime.getMessage0(messageId);
return reportRuntimeError(msg);
}
static EvaluatorException reportRuntimeError1(String messageId,
Object arg1)
{
String msg = getMessage1(messageId, arg1);
String msg = ScriptRuntime.getMessage1(messageId, arg1);
return reportRuntimeError(msg);
}
static EvaluatorException reportRuntimeError2(String messageId,
Object arg1, Object arg2)
{
String msg = getMessage2(messageId, arg1, arg2);
String msg = ScriptRuntime.getMessage2(messageId, arg1, arg2);
return reportRuntimeError(msg);
}
@ -1061,7 +1060,7 @@ public class Context
Object arg1, Object arg2,
Object arg3)
{
String msg = getMessage3(messageId, arg1, arg2, arg3);
String msg = ScriptRuntime.getMessage3(messageId, arg1, arg2, arg3);
return reportRuntimeError(msg);
}
@ -1069,7 +1068,8 @@ public class Context
Object arg1, Object arg2,
Object arg3, Object arg4)
{
String msg = getMessage4(messageId, arg1, arg2, arg3, arg4);
String msg
= ScriptRuntime.getMessage4(messageId, arg1, arg2, arg3, arg4);
return reportRuntimeError(msg);
}
@ -2244,37 +2244,6 @@ public class Context
/********** end of API **********/
static String getMessage0(String messageId)
{
return getMessage(messageId, null);
}
static String getMessage1(String messageId, Object arg1)
{
Object[] arguments = {arg1};
return getMessage(messageId, arguments);
}
static String getMessage2(String messageId, Object arg1, Object arg2)
{
Object[] arguments = {arg1, arg2};
return getMessage(messageId, arguments);
}
static String getMessage3(String messageId, Object arg1, Object arg2,
Object arg3)
{
Object[] arguments = {arg1, arg2, arg3};
return getMessage(messageId, arguments);
}
static String getMessage4(String messageId, Object arg1, Object arg2,
Object arg3, Object arg4)
{
Object[] arguments = {arg1, arg2, arg3, arg4};
return getMessage(messageId, arguments);
}
/**
* Internal method that reports an error for missing calls to
* enter().
@ -2289,39 +2258,6 @@ public class Context
return cx;
}
/* OPT there's a noticable delay for the first error! Maybe it'd
* make sense to use a ListResourceBundle instead of a properties
* file to avoid (synchronized) text parsing.
*/
static final String defaultResource =
"org.mozilla.javascript.resources.Messages";
static String getMessage(String messageId, Object[] arguments)
{
Context cx = getCurrentContext();
Locale locale = cx != null ? cx.getLocale() : Locale.getDefault();
// ResourceBundle does cacheing.
ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale);
String formatString;
try {
formatString = rb.getString(messageId);
} catch (java.util.MissingResourceException mre) {
throw new RuntimeException
("no message resource found for message property "+ messageId);
}
/*
* It's OK to format the string, even if 'arguments' is null;
* we need to format it anyway, to make double ''s collapse to
* single 's.
*/
// TODO: MessageFormat is not available on pJava
MessageFormat formatter = new MessageFormat(formatString);
return formatter.format(arguments);
}
private Object compileImpl(Scriptable scope,
Reader sourceReader, String sourceString,
String sourceName, int lineno,
@ -2493,12 +2429,10 @@ public class Context
private void newArrayHelper(Scriptable scope, Scriptable array)
{
scope = ScriptableObject.getTopLevelScope(scope);
array.setParentScope(scope);
Object ctor = ScriptRuntime.getTopLevelProp(scope, "Array");
if (ctor != null && ctor instanceof Scriptable) {
Scriptable s = (Scriptable) ctor;
array.setPrototype((Scriptable) s.get("prototype", s));
}
array.setPrototype(
ScriptableObject.getClassPrototype(scope, array.getClassName()));
}
final boolean isVersionECMA1()

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

@ -169,8 +169,7 @@ public class NativeJavaTopPackage
offset = index+1;
}
}
throw Context.reportRuntimeError(
Context.getMessage0("msg.not.java.obj"));
throw Context.reportRuntimeError0("msg.not.java.obj");
}
private static final Object FTAG = new Object();

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

@ -108,7 +108,7 @@ public class Parser
void addWarning(String messageId, String messageArg)
{
String message = Context.getMessage1(messageId, messageArg);
String message = ScriptRuntime.getMessage1(messageId, messageArg);
errorReporter.warning(message, sourceURI, ts.getLineno(),
ts.getLine(), ts.getOffset());
}
@ -116,7 +116,7 @@ public class Parser
void addError(String messageId)
{
++syntaxErrorCount;
String message = Context.getMessage0(messageId);
String message = ScriptRuntime.getMessage0(messageId);
errorReporter.error(message, sourceURI, ts.getLineno(),
ts.getLine(), ts.getOffset());
}
@ -349,14 +349,15 @@ public class Parser
nf.addChildToBack(pn, n);
}
} catch (StackOverflowError ex) {
String msg = Context.getMessage0("mag.too.deep.parser.recursion");
String msg = ScriptRuntime.getMessage0(
"mag.too.deep.parser.recursion");
throw Context.reportRuntimeError(msg, sourceURI,
ts.getLineno(), null, 0);
}
if (this.syntaxErrorCount != 0) {
String msg = String.valueOf(this.syntaxErrorCount);
msg = Context.getMessage1("msg.got.syntax.errors", msg);
msg = ScriptRuntime.getMessage1("msg.got.syntax.errors", msg);
throw errorReporter.runtimeError(msg, sourceURI, baseLineno,
null, 0);
}

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

@ -44,6 +44,9 @@
package org.mozilla.javascript;
import java.lang.reflect.*;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import org.mozilla.javascript.xml.XMLObject;
import org.mozilla.javascript.xml.XMLLib;
@ -2082,22 +2085,6 @@ public class ScriptRuntime {
return f;
}
private static Object call(Object fun, Context cx, Scriptable scope,
Object thisArg, Object[] args)
{
if (!(fun instanceof Function)) {
throw notFunctionError(fun);
}
Function function = (Function)fun;
Scriptable thisObj;
if (thisArg instanceof Scriptable || thisArg == null) {
thisObj = (Scriptable) thisArg;
} else {
thisObj = ScriptRuntime.toObject(cx, scope, thisArg);
}
return function.call(cx, scope, thisObj, args);
}
/**
* Perform function call in reference context. Should always
* return value that can be passed to
@ -2253,7 +2240,7 @@ public class ScriptRuntime {
if (cx.hasFeature(Context.FEATURE_STRICT_MODE)) {
throw Context.reportRuntimeError0("msg.eval.nonstring.strict");
}
String message = Context.getMessage0("msg.eval.nonstring");
String message = ScriptRuntime.getMessage0("msg.eval.nonstring");
Context.reportWarning(message);
return x;
}
@ -2283,7 +2270,7 @@ public class ScriptRuntime {
// infinite looping on while(true) { eval('foo bar') } -
// so we throw an EvaluatorException.
if (script == null) {
String message = Context.getMessage0("msg.syntax");
String message = ScriptRuntime.getMessage0("msg.syntax");
throw new EvaluatorException(message, filename, lineNumber,
null, 0);
}
@ -2869,7 +2856,7 @@ public class ScriptRuntime {
private static Scriptable locateDynamicScope(Context cx, Scriptable scope)
{
// Return cx.topCallScope is scope is present on its prototype chain
// Return cx.topCallScope if scope is present on its prototype chain
// and return scope otherwise.
// Should only be called when scope is top scope.
if (cx.topCallScope == scope) {
@ -3219,22 +3206,69 @@ public class ScriptRuntime {
}
}
public static String getMessage0(String messageId) {
return Context.getMessage0(messageId);
}
public static String getMessage1(String messageId, Object arg1) {
return Context.getMessage1(messageId, arg1);
}
public static String getMessage2
(String messageId, Object arg1, Object arg2)
public static String getMessage0(String messageId)
{
return Context.getMessage2(messageId, arg1, arg2);
return getMessage(messageId, null);
}
public static String getMessage(String messageId, Object[] arguments) {
return Context.getMessage(messageId, arguments);
public static String getMessage1(String messageId, Object arg1)
{
Object[] arguments = {arg1};
return getMessage(messageId, arguments);
}
public static String getMessage2(
String messageId, Object arg1, Object arg2)
{
Object[] arguments = {arg1, arg2};
return getMessage(messageId, arguments);
}
public static String getMessage3(
String messageId, Object arg1, Object arg2, Object arg3)
{
Object[] arguments = {arg1, arg2, arg3};
return getMessage(messageId, arguments);
}
public static String getMessage4(
String messageId, Object arg1, Object arg2, Object arg3, Object arg4)
{
Object[] arguments = {arg1, arg2, arg3, arg4};
return getMessage(messageId, arguments);
}
/* OPT there's a noticable delay for the first error! Maybe it'd
* make sense to use a ListResourceBundle instead of a properties
* file to avoid (synchronized) text parsing.
*/
public static String getMessage(String messageId, Object[] arguments)
{
final String defaultResource
= "org.mozilla.javascript.resources.Messages";
Context cx = Context.getCurrentContext();
Locale locale = cx != null ? cx.getLocale() : Locale.getDefault();
// ResourceBundle does cacheing.
ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale);
String formatString;
try {
formatString = rb.getString(messageId);
} catch (java.util.MissingResourceException mre) {
throw new RuntimeException
("no message resource found for message property "+ messageId);
}
/*
* It's OK to format the string, even if 'arguments' is null;
* we need to format it anyway, to make double ''s collapse to
* single 's.
*/
// TODO: MessageFormat is not available on pJava
MessageFormat formatter = new MessageFormat(formatString);
return formatter.format(arguments);
}
public static EcmaError constructError(String error, String message)