Skip to content
This repository was archived by the owner on Nov 8, 2020. It is now read-only.
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Rhino is an implementation of JavaScript in Java.
## This repository is a fork of rhino for [fixRTM](https://github.com/anatawa12/fixRTM)

Here is the list of difference from the original Rhino:
- NativeString will search String's method.
- toString for function return real function source code.
- Packages.some.Class.class should return java.lang.Class Object.

## License

Expand Down
9 changes: 9 additions & 0 deletions src/org/mozilla/javascript/NativeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public final void initScriptFunction(Context cx, Scriptable scope)
ScriptRuntime.setFunctionProtoAndParent(this, scope);
}

public String decompileRaw() {
return null;
}

/**
* @param indent How much to indent the decompiled result
*
Expand All @@ -31,6 +35,11 @@ public final void initScriptFunction(Context cx, Scriptable scope)
@Override
final String decompile(int indent, int flags)
{
if (indent == 0 && flags == 0) {
String raw = decompileRaw();
if (raw != null)
return raw;
}
String encodedSource = getEncodedSource();
if (encodedSource == null) {
return super.decompile(indent, flags);
Expand Down
7 changes: 5 additions & 2 deletions src/org/mozilla/javascript/NativeJavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class NativeJavaClass extends NativeJavaObject implements Function
// Special property for getting the underlying Java class object.
static final String javaClassPropertyName = "__javaObject__";

static final String javaClassPropertyName2 = "class";

public NativeJavaClass() {
}

Expand All @@ -58,7 +60,8 @@ public String getClassName() {

@Override
public boolean has(String name, Scriptable start) {
return members.has(name, true) || javaClassPropertyName.equals(name);
return members.has(name, true) || javaClassPropertyName.equals(name)
|| javaClassPropertyName2.equals(name);
}

@Override
Expand All @@ -84,7 +87,7 @@ public Object get(String name, Scriptable start) {
Scriptable scope = ScriptableObject.getTopLevelScope(start);
WrapFactory wrapFactory = cx.getWrapFactory();

if (javaClassPropertyName.equals(name)) {
if (javaClassPropertyName.equals(name) || javaClassPropertyName2.equals(name)) {
return wrapFactory.wrap(cx, scope, javaObject,
ScriptRuntime.ClassClass);
}
Expand Down
63 changes: 63 additions & 0 deletions src/org/mozilla/javascript/NativeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

import java.text.Collator;
import java.text.Normalizer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.function.Function;

import org.mozilla.javascript.regexp.NativeRegExp;

Expand Down Expand Up @@ -126,6 +130,65 @@ protected void fillConstructorProperties(IdFunctionObject ctor)
super.fillConstructorProperties(ctor);
}

@Override
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result != NOT_FOUND) return result;
if (StringMethod_names.contains(name)) return NOT_FOUND;
// get ctx
Context ctx = Context.getCurrentContext();
if (ctx == null) return NOT_FOUND;
// get by object
result = ctx.getWrapFactory().wrapNewObject(ctx, ScriptableObject.getTopLevelScope(start), "")
.get(name, start);
if (result instanceof NativeJavaMethod) {
return new WrappedNativeFunction((NativeJavaMethod) result,
(obj) -> obj instanceof NativeString ? ((NativeString) obj).toCharSequence().toString() : null);
}
return result;
}

private static final Set<String> StringMethod_names = new HashSet<>(Arrays.asList(
"charAt", "charCodeAt", "codePointAt", "concat", "includes", "endsWith", "indexOf", "lastIndexOf",
"localeCompare", "match", "normalize", "padEnd", "padStart", "quote", "repeat", "replace", "search",
"slice", "split", "startsWith", "substr", "substring", "toLocaleLowerCase", "toLocaleUpperCase",
"toLowerCase", "toSource", "toUpperCase", "trim", "trimStart", "trimLeft", "trimEnd",
"trimRight", "anchor", "big", "blink", "bold", "fixed", "fontcolor", "fontsize", "italics",
"link", "small", "strike", "sub", "sup", "toString", "valueOf"
));

private static class WrappedNativeFunction extends NativeJavaMethod {
private final NativeJavaMethod javaMethod;
private final Function<Scriptable, Object> unwrap;

WrappedNativeFunction(NativeJavaMethod javaMethod, Function<Scriptable, Object> unwrap) {
super(javaMethod.methods, javaMethod.getFunctionName());
this.javaMethod = javaMethod;
this.unwrap = unwrap;
}

@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
if (thisObj == null)
return javaMethod.call(cx, scope, thisObj, args);

Object unwrapped = unwrap.apply(thisObj);
if (unwrapped instanceof Scriptable)
return javaMethod.call(cx, scope, (Scriptable) unwrapped, args);
if (unwrapped == null)
return javaMethod.call(cx, scope, thisObj, args);

thisObj = cx.getWrapFactory().wrapNewObject(cx, ScriptableObject.getTopLevelScope(scope), unwrapped);

return javaMethod.call(cx, scope, thisObj, args);
}

@Override
public Object getDefaultValue(Class<?> typeHint) {
return javaMethod.getDefaultValue(typeHint);
}
}

@Override
protected void initPrototypeId(int id)
{
Expand Down
4 changes: 4 additions & 0 deletions src/org/mozilla/javascript/optimizer/ClassCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public Object[] compileToClassFiles(String source,
}

Codegen codegen = new Codegen();

// to know real source code
codegen.sourceString = source;

codegen.setMainMethodClass(mainMethodClassName);
byte[] scriptClassBytes
= codegen.compileToClassFile(compilerEnv, scriptClassName,
Expand Down
34 changes: 33 additions & 1 deletion src/org/mozilla/javascript/optimizer/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,9 @@ private void generateFunctionInit(ClassFileWriter cfw,
cfw.stopMethod((short)3);
}

// to kwnow real source code
String sourceString;

private void generateNativeFunctionOverrides(ClassFileWriter cfw,
String encodedSource)
{
Expand All @@ -766,13 +769,20 @@ private void generateNativeFunctionOverrides(ClassFileWriter cfw,
final int Do_getParamOrVarName = 3;
final int Do_getEncodedSource = 4;
final int Do_getParamOrVarConst = 5;
final int SWITCH_COUNT = 6;
// add Do_decompileRaw
final int Do_decompileRaw = 6;
final int SWITCH_COUNT = 7;

for (int methodIndex = 0; methodIndex != SWITCH_COUNT; ++methodIndex) {
if (methodIndex == Do_getEncodedSource && encodedSource == null) {
continue;
}

// add condition for Do_decompileRaw
if (methodIndex == Do_decompileRaw && sourceString == null) {
continue;
}

// Generate:
// prologue;
// switch over function id to implement function-specific action
Expand Down Expand Up @@ -811,6 +821,15 @@ private void generateNativeFunctionOverrides(ClassFileWriter cfw,
ACC_PUBLIC);
cfw.addPush(encodedSource);
break;

// add for Do_decompileRaw
case Do_decompileRaw:
methodLocals = 1; // Only this
cfw.startMethod("decompileRaw", "()Ljava/lang/String;",
ACC_PUBLIC);
cfw.addPush(sourceString);
break;

default:
throw Kit.codeBug();
}
Expand Down Expand Up @@ -953,6 +972,19 @@ private void generateNativeFunctionOverrides(ClassFileWriter cfw,
cfw.add(ByteCode.ARETURN);
break;

// add for Do_decompileRaw
case Do_decompileRaw:
// Push number source start and end
// to prepare for encodedSource.substring(start, end)
cfw.addPush(n.getAbsolutePosition());
cfw.addPush(n.getAbsolutePosition() + n.getLength());
cfw.addInvoke(ByteCode.INVOKEVIRTUAL,
"java/lang/String",
"substring",
"(II)Ljava/lang/String;");
cfw.add(ByteCode.ARETURN);
break;

default:
throw Kit.codeBug();
}
Expand Down