* Implementing global asJavaObject() function.

This commit is contained in:
hns 2007-03-28 14:44:55 +00:00
parent 05a978e901
commit b67a171983

View file

@ -74,7 +74,7 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
"getXmlDocument", "getHtmlDocument", "seal",
"getDBConnection", "getURL", "write", "writeln",
"serialize", "deserialize", "defineLibraryScope",
"wrapJavaMap", "unwrapJavaMap"
"wrapJavaMap", "unwrapJavaMap", "asJavaObject"
};
defineFunctionProperties(globalFuncs, GlobalObject.class, DONTENUM | READONLY | PERMANENT);
@ -426,6 +426,33 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
return new NativeJavaObject(core.global, obj, Map.class);
}
/**
* Convert an object into a wrapper that exposes the java
* methods of the object to JavaScript. This is useful for
* treating native numbers, strings, etc as their java
* counterpart such as java.lang.Double, java.lang.String etc.
* @param obj a java object that is wrapped in a special way
* Rhino
* @return the object wrapped as NativeJavaObject, exposing
* the public methods of the underlying class.
*/
public Object asJavaObject(Object obj) {
if (obj == null || obj instanceof NativeJavaObject
|| obj == Undefined.instance) {
return obj;
}
if (obj instanceof Wrapper) {
obj = ((Wrapper) obj).unwrap();
} else if (obj instanceof Scriptable) {
String className = ((Scriptable) obj).getClassName();
if ("Date".equals(className)) {
return new NativeJavaObject(this,
new Date((long) ScriptRuntime.toNumber(obj)), null);
}
}
return new NativeJavaObject(this, obj, null);
}
/**
*
*