Skip to content

Commit

Permalink
Refactored RhinoConfig a bit, so that it can be used better in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed Jan 17, 2025
1 parent abc880e commit ffff7d0
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.RhinoConfig;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.SecurityController;
import org.mozilla.javascript.commonjs.module.ModuleScope;
import org.mozilla.javascript.commonjs.module.Require;
import org.mozilla.javascript.config.RhinoConfig;
import org.mozilla.javascript.tools.SourceReader;
import org.mozilla.javascript.tools.ToolErrorReporter;

Expand Down Expand Up @@ -133,7 +133,7 @@ public void quit(Context cx, int exitCode) {
*/
public static void main(String args[]) {
try {
if (RhinoConfig.DEFAULT.useJavaPolicySecurity()) {
if (RhinoConfig.get("rhino.use_java_policy_security", false)) {
initJavaPolicySecuritySupport();
}
} catch (SecurityException ex) {
Expand Down
1 change: 1 addition & 0 deletions rhino/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
exports org.mozilla.javascript.serialize;
exports org.mozilla.javascript.typedarrays;
exports org.mozilla.javascript.xml;
exports org.mozilla.javascript.config;

requires java.compiler;
requires jdk.dynalink;
Expand Down
237 changes: 0 additions & 237 deletions rhino/src/main/java/org/mozilla/javascript/RhinoConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.mozilla.javascript.config.RhinoConfig;

/** The class of exceptions thrown by the JavaScript engine. */
public abstract class RhinoException extends RuntimeException {
Expand Down Expand Up @@ -374,7 +375,7 @@ public static StackStyle getStackStyle() {
private static final long serialVersionUID = 1883500631321581169L;

// Just for testing!
private static StackStyle stackStyle = RhinoConfig.DEFAULT.stackStyle();
private static StackStyle stackStyle = RhinoConfig.get("rhino.stack.style", StackStyle.RHINO);

private String sourceName;
private int lineNumber;
Expand Down
6 changes: 4 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.mozilla.javascript;

import org.mozilla.javascript.config.RhinoConfig;

/**
* This class implements the JavaScript scanner.
*
Expand All @@ -24,8 +26,8 @@ public static enum CommentType {
}

// debug flags
public static final boolean printTrees = RhinoConfig.DEFAULT.printTrees();
static final boolean printICode = RhinoConfig.DEFAULT.printICodes();
public static final boolean printTrees = RhinoConfig.get("rhino.printTrees", false);
static final boolean printICode = RhinoConfig.get("rhino.printICode", false);
static final boolean printNames = printTrees || printICode;

/** Token types. These values correspond to JSTokenType values in jsscan.c. */
Expand Down
60 changes: 60 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/config/RhinoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.mozilla.javascript.config;

import java.util.Locale;

/**
* With RhinoConfig, you can access the {@link RhinoProperties} in a typesafe way.
*
* @author Roland Praml, Foconis Analytics GmbH
*/
public class RhinoConfig {

/** Returns the property as string. */
private static String get(String property, String defaultValue) {
Object ret = RhinoProperties.get(property);
if (ret != null) {
return ret.toString();
}
return defaultValue;
}

/** Returns the property as string with null as default. */
public static String get(String property) {
return get(property, (String) null);
}

/** Returns the property as enum. Note: default value must be specified */
public static <T extends Enum<T>> T get(String property, T defaultValue) {
Object ret = RhinoProperties.get(property);
if (ret != null) {
Class<T> enumType = (Class<T>) defaultValue.getClass();
// We assume, that enums all are in UPPERCASES
return Enum.valueOf(enumType, ret.toString().toUpperCase(Locale.ROOT));
}
return defaultValue;
}

/** Returns the property as boolean. */
public static boolean get(String property, boolean defaultValue) {
Object ret = RhinoProperties.get(property);
if (ret instanceof Boolean) {
return (Boolean) ret;
} else {
return "1".equals(ret) || "true".equals(ret);
}
}

/** Returns the property as integer. */
public static int get(String property, int defaultValue) {
Object ret = RhinoProperties.get(property);
if (ret instanceof Number) {
return ((Number) ret).intValue();
} else {
try {
return Integer.decode(ret.toString());
} catch (NumberFormatException e) {
}
}
return defaultValue;
}
}
Loading

0 comments on commit ffff7d0

Please sign in to comment.