Skip to content

Commit

Permalink
Merge pull request #50 from space928/main
Browse files Browse the repository at this point in the history
Fixes to re-enable JavaAdapter support
  • Loading branch information
LatvianModder authored Jan 21, 2025
2 parents 23d3f49 + 76642fc commit cf62b4b
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 54 deletions.
91 changes: 91 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,74 @@ public Scriptable newArray(Scriptable scope, Object[] elements) {
return result;
}

//--------------------------
// Primitive conversion methods
//--------------------------

/**
* Convert the value to a JavaScript boolean value.
* <p>
* See ECMA 9.2.
*
* @param value a JavaScript value
* @return the corresponding boolean value converted using
* the ECMA rules
*/
public boolean toBoolean(Object value) {
return ScriptRuntime.toBoolean(this, value);
}

/**
* Convert the value to a JavaScript Number value.
* <p>
* Returns a Java double for the JavaScript Number.
* <p>
* See ECMA 9.3.
*
* @param value a JavaScript value
* @return the corresponding double value converted using
* the ECMA rules
*/
public double toNumber(Object value) {
return ScriptRuntime.toNumber(this, value);
}

/**
* Convert the value to a JavaScript String value.
* <p>
* See ECMA 9.8.
* <p>
*
* @param value a JavaScript value
* @return the corresponding String value converted using
* the ECMA rules
*/
public String toString(Object value) {
return ScriptRuntime.toString(this, value);
}

/**
* Convert the value to an JavaScript object value.
* <p>
* Note that a scope must be provided to look up the constructors
* for Number, Boolean, and String.
* <p>
* See ECMA 9.9.
* <p>
* Additionally, arbitrary Java objects and classes will be
* wrapped in a Scriptable object with its Java fields and methods
* reflected as JavaScript properties of the object.
*
* @param value any Java object
* @param scope global scope containing constructors for Number,
* Boolean, and String
* @return new JavaScript object
*/
public Scriptable toObject(Object value, Scriptable scope) {
return ScriptRuntime.toObject(this, scope, value);
}


/**
* Get a value corresponding to a key.
* <p>
Expand Down Expand Up @@ -987,6 +1055,29 @@ public Object wrap(Scriptable scope, Object obj, TypeInfo target) {
}
}

// Wraps an object if needed
public Object wrapAny(Scriptable scope, Object obj) {
if (obj instanceof String ||
obj instanceof Boolean ||
obj instanceof Integer ||
obj instanceof Short ||
obj instanceof Long ||
obj instanceof Float ||
obj instanceof Double) {
return obj;
} else if (obj instanceof Character) {
return String.valueOf(((Character) obj).charValue());
}
Class<?> cls = obj.getClass();
TypeInfo ti = TypeInfo.NONE;
if (cls.isArray()) {
ti = TypeInfo.of(cls);
//return NativeJavaArray.wrap(scope, obj);
}

return wrap(scope, obj, ti);
}

public Object wrap(Scriptable scope, Object obj) {
return wrap(scope, obj, TypeInfo.NONE);
}
Expand Down
Loading

0 comments on commit cf62b4b

Please sign in to comment.