Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/src/main/java/PrimitiveWrapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.WrapFactory;
import org.mozilla.javascript.lc.java.WrapFactoryImpl;

/**
* An example WrapFactory that can be used to avoid wrapping of Java types that can be converted to
Expand All @@ -21,7 +21,7 @@
*
* <p>The PrimitiveWrapFactory is enabled on a Context by calling setWrapFactory on that context.
*/
public class PrimitiveWrapFactory extends WrapFactory {
public class PrimitiveWrapFactory extends WrapFactoryImpl {

@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {
Expand Down
52 changes: 41 additions & 11 deletions examples/src/main/java/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import java.io.InputStreamReader;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.WrappedException;

/**
Expand Down Expand Up @@ -53,9 +53,38 @@ public static void main(String args[]) {

// Define some global functions particular to the shell. Note
// that these functions are not part of ECMA.
String[] names = {"print", "quit", "version", "load", "help"};
shell.defineFunctionProperties(names, Shell.class, ScriptableObject.DONTENUM);

shell.defineProperty(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make a bulk registeration? Like so:

var properties = Map.<String, SomeFunction>of(
    "print", Shell::print,
    "quit", Shell::quit,
    "version", Shell::version,
    "load", Shell::load,
    "help", Shell::help
);
for (var entry : properties.entrySet()) {
    // define property, but how to determine the "length"?
}

It should be easier to read and maintain

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please see https://github.com/mozilla/rhino/pull/1993/files
If static import is used, it would match in one line.
but maybe it makes sense to replace
defineProperty(this, "defineClass", 1, Global::defineClass, DONTENUM, DONTENUM | READONLY)
by
defineFunctionProperty(this, "defineClass", 1, Global::defineClass)

shell,
"print",
0,
Shell::print,
ScriptableObject.DONTENUM,
ScriptableObject.DONTENUM | ScriptableObject.READONLY);

shell.defineProperty(
shell,
"version",
0,
Shell::version,
ScriptableObject.DONTENUM,
ScriptableObject.DONTENUM | ScriptableObject.READONLY);

shell.defineProperty(
shell,
"load",
0,
Shell::load,
ScriptableObject.PERMANENT | ScriptableObject.DONTENUM,
ScriptableObject.DONTENUM);

shell.defineProperty(
shell,
"help",
1,
Shell::help,
ScriptableObject.PERMANENT | ScriptableObject.DONTENUM,
ScriptableObject.DONTENUM);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing quit?

args = processOptions(cx, args);

// Set up "arguments" in the global scope to contain the command
Expand Down Expand Up @@ -117,7 +146,7 @@ private static void usage(String s) {
*
* <p>This method is defined as a JavaScript function.
*/
public void help() {
private static Object help(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
p("");
p("Command Description");
p("======= ===========");
Expand All @@ -134,6 +163,7 @@ public void help() {
p("quit() Quit the shell. ");
p("version([number]) Get or set the JavaScript version number.");
p("");
return Undefined.instance;
}

/**
Expand All @@ -146,9 +176,8 @@ public void help() {
* @param cx the current Context for this thread
* @param thisObj the JavaScript <code>this</code> object
* @param args the array of arguments
* @param funObj the function object of the invoked JavaScript function
*/
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
private static Object print(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
for (int i = 0; i < args.length; i++) {
if (i > 0) System.out.print(" ");

Expand All @@ -158,6 +187,7 @@ public static void print(Context cx, Scriptable thisObj, Object[] args, Function
System.out.print(s);
}
System.out.println();
return Undefined.instance;
}

/**
Expand All @@ -167,8 +197,9 @@ public static void print(Context cx, Scriptable thisObj, Object[] args, Function
*
* <p>This method is defined as a JavaScript function.
*/
public void quit() {
public Object quit(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
quitting = true;
return Undefined.instance;
}

/**
Expand All @@ -179,10 +210,9 @@ public void quit() {
* @param cx the current Context for this thread
* @param thisObj the JavaScript <code>this</code> object
* @param args the array of arguments
* @param funObj the function object of the invoked JavaScript function
* @return the version no as double
*/
public static double version(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
private static double version(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
double result = cx.getLanguageVersion();
if (args.length > 0) {
double d = Context.toNumber(args[0]);
Expand All @@ -199,13 +229,13 @@ public static double version(Context cx, Scriptable thisObj, Object[] args, Func
* @param cx the current Context for this thread
* @param thisObj the JavaScript <code>this</code> object
* @param args the array of arguments
* @param funObj the function object of the invoked JavaScript function
*/
public static void load(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
private static Object load(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Shell shell = (Shell) getTopLevelScope(thisObj);
for (int i = 0; i < args.length; i++) {
shell.processSource(cx, Context.toString(args[i]));
}
return Undefined.instance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.NativeCall;
import org.mozilla.javascript.NativeObject;
Expand All @@ -34,6 +33,7 @@
import org.mozilla.javascript.debug.DebuggableObject;
import org.mozilla.javascript.debug.DebuggableScript;
import org.mozilla.javascript.debug.Debugger;
import org.mozilla.javascript.lc.java.ImporterTopLevel;

/** Dim or Debugger Implementation for Rhino. */
public class Dim {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package org.mozilla.javascript.tools.shell;

import java.util.Map;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.LambdaConstructor;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
Expand All @@ -29,25 +32,32 @@ public class Environment extends ScriptableObject {
static final long serialVersionUID = -430727378460177065L;

private Environment thePrototypeInstance = null;

public static void defineClass(ScriptableObject scope) {
try {
ScriptableObject.defineClass(scope, Environment.class);
} catch (Exception e) {
throw new Error(e.getMessage());
private static final String CLASS_NAME = "Environment";

static Object init(Context cx, ScriptableObject scope, boolean sealed) {
LambdaConstructor ctor = new LambdaConstructor(scope, CLASS_NAME, 1, Environment::new);

var proto = new NativeArray(0);
ctor.setPrototypeScriptable(proto);
Environment environment = new Environment(cx, scope, new Object[0]);
scope.defineProperty("environment", environment, ScriptableObject.DONTENUM);
if (sealed) {
ctor.sealObject();
((ScriptableObject) ctor.getPrototypeProperty()).sealObject();
}
return ctor;
}

@Override
public String getClassName() {
return "Environment";
return CLASS_NAME;
}

public Environment() {
if (thePrototypeInstance == null) thePrototypeInstance = this;
}

public Environment(ScriptableObject scope) {
public Environment(Context cx, Scriptable scope, Object[] args) {
setParentScope(scope);
Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
if (ctor != null && ctor instanceof Scriptable) {
Expand Down
Loading