зеркало из https://github.com/mozilla/pjs.git
Fix 60184, and add SwingApplication.js example, with change
to shell to prevent early exit.
This commit is contained in:
Родитель
1e72e2633a
Коммит
2e9a2bd01f
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* SwingApplication.js - a translation into JavaScript of
|
||||
* SwingApplication.java, a java.sun.com Swing example.
|
||||
*
|
||||
* @author Roger E Critchlow, Jr.
|
||||
*/
|
||||
|
||||
importPackage(Packages.javax.swing);
|
||||
importPackage(Packages.java.awt);
|
||||
importPackage(Packages.java.awt.event);
|
||||
|
||||
function createComponents() {
|
||||
var labelPrefix = "Number of button clicks: ";
|
||||
var numClicks = 0;
|
||||
var label = new JLabel(labelPrefix + numClicks);
|
||||
var button = new JButton("I'm a Swing button!");
|
||||
button.setMnemonic(KeyEvent.VK_I);
|
||||
button.addActionListener(new ActionListener({
|
||||
actionPerformed : function() {
|
||||
numClicks += 1;
|
||||
label.setText(labelPrefix + numClicks);
|
||||
}
|
||||
}));
|
||||
label.setLabelFor(button);
|
||||
|
||||
/*
|
||||
* An easy way to put space between a top-level container
|
||||
* and its contents is to put the contents in a JPanel
|
||||
* that has an "empty" border.
|
||||
*/
|
||||
var pane = new JPanel();
|
||||
pane.setBorder(BorderFactory.createEmptyBorder(
|
||||
30, //top
|
||||
30, //left
|
||||
10, //bottom
|
||||
30) //right
|
||||
);
|
||||
pane.setLayout(new GridLayout(0, 1));
|
||||
pane.add(button);
|
||||
pane.add(label);
|
||||
|
||||
return pane;
|
||||
}
|
||||
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
|
||||
} catch (e) { }
|
||||
|
||||
//Create the top-level container and add contents to it.
|
||||
var frame = new JFrame("SwingApplication");
|
||||
frame.getContentPane().add(createComponents(), BorderLayout.CENTER);
|
||||
|
||||
//Finish setting up the frame, and show it.
|
||||
frame.addWindowListener(new WindowAdapter({
|
||||
windowClosing : function() {
|
||||
java.lang.System.exit(0);
|
||||
}
|
||||
}) );
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
|
||||
|
||||
|
|
@ -20,6 +20,7 @@
|
|||
*
|
||||
* Contributor(s):
|
||||
* Norris Boyd
|
||||
* David C. Navas
|
||||
* Ted Neward
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
|
@ -456,9 +457,16 @@ public class FunctionObject extends NativeFunction {
|
|||
Object[] args)
|
||||
throws JavaScriptException
|
||||
{
|
||||
if (parmsLength < 0)
|
||||
return callVarargs(cx, thisObj, args, false);
|
||||
|
||||
if (parmsLength < 0) {
|
||||
// Ugly: allow variable-arg constructors that need access to the
|
||||
// scope to get it from the Context. Cleanest solution would be
|
||||
// to modify the varargs form, but that would require users with
|
||||
// the old form to change their code.
|
||||
cx.ctorScope = scope;
|
||||
Object result = callVarargs(cx, thisObj, args, false);
|
||||
cx.ctorScope = null;
|
||||
return result;
|
||||
}
|
||||
if (!isStatic) {
|
||||
// OPT: cache "clazz"?
|
||||
Class clazz = method != null ? method.getDeclaringClass()
|
||||
|
|
|
@ -1482,6 +1482,29 @@ public abstract class ScriptableObject implements Scriptable {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a method of an object.
|
||||
* <p>
|
||||
* @param obj the JavaScript object
|
||||
* @param methodName the name of the function property
|
||||
* @param args the arguments for the call
|
||||
* @exception JavaScriptException thrown if there were errors in the call
|
||||
*/
|
||||
public static Object callMethod(Scriptable obj, String methodName,
|
||||
Object[] args)
|
||||
throws JavaScriptException
|
||||
{
|
||||
Context cx = Context.enter();
|
||||
try {
|
||||
Object fun = getProperty(obj, methodName);
|
||||
if (fun == NOT_FOUND)
|
||||
fun = Undefined.instance;
|
||||
return ScriptRuntime.call(cx, fun, obj, args, getTopLevelScope(obj));
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
private static Scriptable getBase(Scriptable obj, String s) {
|
||||
Scriptable m = obj;
|
||||
while (m != null) {
|
||||
|
|
|
@ -66,6 +66,7 @@ public class Main {
|
|||
*/
|
||||
public static void main(String args[]) {
|
||||
int result = exec(args);
|
||||
if (result != 0)
|
||||
System.exit(result);
|
||||
}
|
||||
|
||||
|
@ -240,7 +241,21 @@ public class Main {
|
|||
Object result = evaluateReader(cx, global, reader,
|
||||
"<stdin>", startline);
|
||||
if (result != cx.getUndefinedValue()) {
|
||||
try {
|
||||
getErr().println(cx.toString(result));
|
||||
} catch (EcmaError ee) {
|
||||
String msg = ToolErrorReporter.getMessage(
|
||||
"msg.uncaughtJSException", ee.toString());
|
||||
exitCode = EXITCODE_RUNTIME_ERROR;
|
||||
if (ee.getSourceName() != null) {
|
||||
Context.reportError(msg, ee.getSourceName(),
|
||||
ee.getLineNumber(),
|
||||
ee.getLineSource(),
|
||||
ee.getColumnNumber());
|
||||
} else {
|
||||
Context.reportError(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
NativeArray h = global.history;
|
||||
h.put((int)h.jsGet_length(), h, source);
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
*
|
||||
* Contributor(s):
|
||||
* Norris Boyd
|
||||
* David C. Navas
|
||||
* Ted Neward
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
|
@ -456,9 +457,16 @@ public class FunctionObject extends NativeFunction {
|
|||
Object[] args)
|
||||
throws JavaScriptException
|
||||
{
|
||||
if (parmsLength < 0)
|
||||
return callVarargs(cx, thisObj, args, false);
|
||||
|
||||
if (parmsLength < 0) {
|
||||
// Ugly: allow variable-arg constructors that need access to the
|
||||
// scope to get it from the Context. Cleanest solution would be
|
||||
// to modify the varargs form, but that would require users with
|
||||
// the old form to change their code.
|
||||
cx.ctorScope = scope;
|
||||
Object result = callVarargs(cx, thisObj, args, false);
|
||||
cx.ctorScope = null;
|
||||
return result;
|
||||
}
|
||||
if (!isStatic) {
|
||||
// OPT: cache "clazz"?
|
||||
Class clazz = method != null ? method.getDeclaringClass()
|
||||
|
|
|
@ -1482,6 +1482,29 @@ public abstract class ScriptableObject implements Scriptable {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a method of an object.
|
||||
* <p>
|
||||
* @param obj the JavaScript object
|
||||
* @param methodName the name of the function property
|
||||
* @param args the arguments for the call
|
||||
* @exception JavaScriptException thrown if there were errors in the call
|
||||
*/
|
||||
public static Object callMethod(Scriptable obj, String methodName,
|
||||
Object[] args)
|
||||
throws JavaScriptException
|
||||
{
|
||||
Context cx = Context.enter();
|
||||
try {
|
||||
Object fun = getProperty(obj, methodName);
|
||||
if (fun == NOT_FOUND)
|
||||
fun = Undefined.instance;
|
||||
return ScriptRuntime.call(cx, fun, obj, args, getTopLevelScope(obj));
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
private static Scriptable getBase(Scriptable obj, String s) {
|
||||
Scriptable m = obj;
|
||||
while (m != null) {
|
||||
|
|
|
@ -66,6 +66,7 @@ public class Main {
|
|||
*/
|
||||
public static void main(String args[]) {
|
||||
int result = exec(args);
|
||||
if (result != 0)
|
||||
System.exit(result);
|
||||
}
|
||||
|
||||
|
@ -240,7 +241,21 @@ public class Main {
|
|||
Object result = evaluateReader(cx, global, reader,
|
||||
"<stdin>", startline);
|
||||
if (result != cx.getUndefinedValue()) {
|
||||
try {
|
||||
getErr().println(cx.toString(result));
|
||||
} catch (EcmaError ee) {
|
||||
String msg = ToolErrorReporter.getMessage(
|
||||
"msg.uncaughtJSException", ee.toString());
|
||||
exitCode = EXITCODE_RUNTIME_ERROR;
|
||||
if (ee.getSourceName() != null) {
|
||||
Context.reportError(msg, ee.getSourceName(),
|
||||
ee.getLineNumber(),
|
||||
ee.getLineSource(),
|
||||
ee.getColumnNumber());
|
||||
} else {
|
||||
Context.reportError(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
NativeArray h = global.history;
|
||||
h.put((int)h.jsGet_length(), h, source);
|
||||
|
|
Загрузка…
Ссылка в новой задаче