Skip to content

Commit

Permalink
Added ToStringJS, removed a bunch of utilities, moved some stuff to C…
Browse files Browse the repository at this point in the history
…ontextFactory
  • Loading branch information
LatvianModder committed May 16, 2024
1 parent 810e679 commit 1d2a37d
Show file tree
Hide file tree
Showing 55 changed files with 599 additions and 1,708 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Rhino

A fork of https://github.com/mozilla/rhino modified for use in Minecraft mods
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod_id=rhino
archives_base_name=rhino
mod_name=Rhino
maven_group=dev.latvian.mods
mod_version=2005.2.3
mod_version=2005.2.4
mod_author=LatvianModder
curseforge_id=416294
modrinth_id=sk9knFPE
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/mods/rhino/ArrowFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ArrowFunction(Context cx, Scriptable scope, Callable targetFunction, Scri
ScriptRuntime.setFunctionProtoAndParent(cx, scope, this);

Function thrower = ScriptRuntime.typeErrorThrower(cx);
NativeObject throwing = new NativeObject(cx);
NativeObject throwing = new NativeObject(cx.factory);
throwing.put(cx, "get", throwing, thrower);
throwing.put(cx, "set", throwing, thrower);
throwing.put(cx, "enumerable", throwing, Boolean.FALSE);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/rhino/BaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
* {@link #call} unless they are already set.
*/
public Scriptable createObject(Context cx, Scriptable scope) {
Scriptable newInstance = new NativeObject(cx);
Scriptable newInstance = new NativeObject(cx.factory);
newInstance.setPrototype(getClassPrototype(cx));
newInstance.setParentScope(getParentScope());
return newInstance;
Expand Down Expand Up @@ -445,7 +445,7 @@ private synchronized Object setupDefaultPrototype(Context cx) {
if (prototypeProperty != null) {
return prototypeProperty;
}
NativeObject obj = new NativeObject(cx);
NativeObject obj = new NativeObject(cx.factory);
final int attr = DONTENUM;
obj.defineProperty(cx, "constructor", this, attr);
// put the prototype property into the object now, then in the
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/mods/rhino/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BoundFunction(Context cx, Scriptable scope, Callable targetFunction, Scri
ScriptRuntime.setFunctionProtoAndParent(cx, scope, this);

Function thrower = ScriptRuntime.typeErrorThrower(cx);
NativeObject throwing = new NativeObject(cx);
NativeObject throwing = new NativeObject(cx.factory);
throwing.put(cx, "get", throwing, thrower);
throwing.put(cx, "set", throwing, thrower);
throwing.put(cx, "enumerable", throwing, Boolean.FALSE);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/mods/rhino/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ private void visitExpression(Node node, int contextFlags) {
case Token.USE_STACK ->
// Indicates that stack was modified externally,
// like placed catch object
stackChange(1);
stackChange(1);
case Token.REF_CALL, Token.CALL, Token.NEW -> {
if (type == Token.NEW) {
visitExpression(child, 0);
Expand Down
143 changes: 5 additions & 138 deletions src/main/java/dev/latvian/mods/rhino/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@
import dev.latvian.mods.rhino.ast.ScriptNode;
import dev.latvian.mods.rhino.classfile.ClassFileWriter.ClassFileFormatException;
import dev.latvian.mods.rhino.regexp.RegExp;
import dev.latvian.mods.rhino.util.CustomJavaToJsWrapper;
import dev.latvian.mods.rhino.util.CustomJavaToJsWrapperProvider;
import dev.latvian.mods.rhino.util.CustomJavaToJsWrapperProviderHolder;
import dev.latvian.mods.rhino.util.DefaultRemapper;
import dev.latvian.mods.rhino.util.Remapper;
import dev.latvian.mods.rhino.util.wrap.TypeWrappers;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
* This class represents the runtime context of an executing script.
Expand All @@ -54,10 +44,6 @@

@SuppressWarnings("ThrowableNotThrown")
public class Context {
public static Context enter() {
return new Context();
}

/**
* Report a warning using the error reporter for the current thread.
*
Expand Down Expand Up @@ -234,7 +220,7 @@ public static Object jsToJava(Context cx, Object value, Class<?> desiredType) th
return value;
}

return NativeJavaObject.coerceTypeImpl(cx.hasTypeWrappers() ? cx.getTypeWrappers() : null, desiredType, value, cx);
return NativeJavaObject.coerceTypeImpl(cx.factory.hasTypeWrappers() ? cx.factory.getTypeWrappers() : null, desiredType, value, cx);
}

/**
Expand Down Expand Up @@ -302,6 +288,7 @@ public static String getSourcePositionFromStack(Context cx, int[] linep) {
return null;
}

public final ContextFactory factory;
public final Object lock = new Object();

// Generate an observer count on compiled code
Expand Down Expand Up @@ -334,16 +321,10 @@ public static String getSourcePositionFromStack(Context cx, int[] linep) {

// custom data

final List<CustomJavaToJsWrapperProviderHolder<?>> customScriptableWrappers = new ArrayList<>();
final Map<Class<?>, CustomJavaToJsWrapperProvider> customScriptableWrapperCache = new HashMap<>();
private final Map<String, Object> properties = new HashMap<>();
TypeWrappers typeWrappers;
Remapper remapper = DefaultRemapper.INSTANCE;
private transient Map<Class<?>, JavaMembers> classTable;
private transient Map<JavaAdapter.JavaAdapterSignature, Class<?>> classAdapterCache;
private transient Map<Class<?>, Object> interfaceAdapterCache;
private int generatedClassSerial;
private ClassShutter classShutter;
private WrapFactory wrapFactory;

/**
Expand All @@ -352,7 +333,8 @@ public static String getSourcePositionFromStack(Context cx, int[] linep) {
*
* @throws IllegalArgumentException if factory parameter is null.
*/
protected Context() {
public Context(ContextFactory factory) {
this.factory = factory;
maximumInterpreterStackDepth = Integer.MAX_VALUE;
}

Expand Down Expand Up @@ -678,7 +660,7 @@ final Function compileFunction(Scriptable scope, String source, Evaluator compil
* @return the new object
*/
public Scriptable newObject(Scriptable scope) {
NativeObject result = new NativeObject(this);
NativeObject result = new NativeObject(factory);
ScriptRuntime.setBuiltinProtoAndParent(this, scope, result, TopLevel.Builtins.Object);
return result;
}
Expand Down Expand Up @@ -907,11 +889,6 @@ public void setGenerateObserverCount(boolean generateObserverCount) {
* The method is useful to observe long running scripts and if necessary
* to terminate them.
* <p>
* The default implementation calls
* {@link ContextFactory#observeInstructionCount(Context cx,
* int instructionCount)}
* that allows to customize Context behavior without introducing
* Context subclasses.
*
* @param instructionCount amount of script instruction executed since
* last call to <code>observeInstructionCount</code>
Expand All @@ -922,15 +899,6 @@ protected void observeInstructionCount(int instructionCount) {

/********** end of API **********/

/**
* Create class loader for generated classes.
* The method calls {@link ContextFactory#createClassLoader(ClassLoader)}
* using the result of {@link #getFactory()}.
*/
public GeneratedClassLoader createClassLoader(ClassLoader parent) {
return new DefiningClassLoader(parent);
}

public final ClassLoader getApplicationClassLoader() {
if (applicationClassLoader == null) {
ClassLoader threadLoader = Thread.currentThread().getContextClassLoader();
Expand Down Expand Up @@ -1096,107 +1064,6 @@ synchronized void cacheInterfaceAdapter(Class<?> cl, Object iadapter) {
interfaceAdapterCache.put(cl, iadapter);
}

public TypeWrappers getTypeWrappers() {
if (typeWrappers == null) {
typeWrappers = new TypeWrappers();
}

return typeWrappers;
}

public boolean hasTypeWrappers() {
return typeWrappers != null;
}

public Remapper getRemapper() {
return remapper;
}

public void setRemapper(Remapper remapper) {
this.remapper = remapper;
}

@Nullable
@SuppressWarnings("unchecked")
public CustomJavaToJsWrapper wrapCustomJavaToJs(Object javaObject) {
if (customScriptableWrappers.isEmpty()) {
return null;
}

var provider = customScriptableWrapperCache.get(javaObject.getClass());

if (provider == null) {
for (CustomJavaToJsWrapperProviderHolder wrapper : customScriptableWrappers) {
provider = wrapper.create(javaObject);

if (provider != null) {
break;
}
}

if (provider == null) {
provider = CustomJavaToJsWrapperProvider.NONE;
}

customScriptableWrapperCache.put(javaObject.getClass(), provider);
}

return provider.create(javaObject);
}

public <T> void addCustomJavaToJsWrapper(Predicate<T> predicate, CustomJavaToJsWrapperProvider<T> provider) {
customScriptableWrappers.add(new CustomJavaToJsWrapperProviderHolder<>(predicate, provider));
}

public <T> void addCustomJavaToJsWrapper(Class<T> type, CustomJavaToJsWrapperProvider<T> provider) {
addCustomJavaToJsWrapper(new CustomJavaToJsWrapperProviderHolder.PredicateFromClass<>(type), provider);
}

public void setProperty(String key, @Nullable Object value) {
if (value == null) {
properties.remove(key);
} else {
properties.put(key, value);
}
}

@Nullable
public Object getProperty(String key) {
return properties.get(key);
}

@Nullable
@SuppressWarnings("unchecked")
public <T> T getProperty(String key, T def) {
return (T) properties.getOrDefault(key, def);
}

@Nullable
public final synchronized ClassShutter getClassShutter() {
return classShutter;
}

/**
* Set the LiveConnect access filter for this context.
* <p> {@link ClassShutter} may only be set if it is currently null.
* Otherwise a SecurityException is thrown.
*
* @param shutter a ClassShutter object
* @throws SecurityException if there is already a ClassShutter
* object for this Context
*/
public synchronized final void setClassShutter(ClassShutter shutter) {
if (shutter == null) {
throw new IllegalArgumentException();
}

if (classShutter != null) {
throw new SecurityException("Cannot overwrite existing " + "ClassShutter object");
}

classShutter = shutter;
}

/**
* Return the current WrapFactory, or null if none is defined.
*
Expand Down
Loading

0 comments on commit 1d2a37d

Please sign in to comment.