r9698 - in helma-ng/trunk: modules/helma src/org/helma/javascript
[email protected] Mon, 4 May 2009 13:52:17 +0200 (CEST)
| Newsgroups | gmane.comp.java.helma.cvs |
|---|---|
| Message-ID | <20090504115217.C31B83D0D6@mia> |
Author: hannes
Date: 2009-05-04 13:52:17 +0200 (Mon, 04 May 2009)
New Revision: 9698
Modified:
helma-ng/trunk/modules/helma/system.js
helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java
Log:
Add asJavaObject() to RhinoEngine and helma/system to access strings as java.lang.String without creating a new instance.
Details at http://dev.helma.org/trac/helma/changeset/9698
Modified: helma-ng/trunk/modules/helma/system.js
===================================================================
--- helma-ng/trunk/modules/helma/system.js 2009-05-04 09:07:23 UTC (rev 9697)
+++ helma-ng/trunk/modules/helma/system.js 2009-05-04 11:52:17 UTC (rev 9698)
@@ -6,6 +6,7 @@
export('addHostObject',
'addRepository',
+ 'asJavaObject',
'createSandbox',
'evaluate',
'extendJavaClass',
@@ -59,6 +60,18 @@
}
/**
+ * Get a wrapper for an object that exposes it as Java object to JavaScript.
+ * This is useful for accessing strings and other primitives as their
+ * java.lang.* counterparts from JavaScript using the existing instance rather
+ * than allocating a new object via new java.lang.Foo() constructor.
+ * @param object an object
+ * @return the object wrapped as native java object
+ */
+function asJavaObject(object) {
+ return getRhinoEngine().asJavaObject(object);
+}
+
+/**
* Get the Rhino optimization level for the current thread and context.
* The optimization level is an integer between -1 (interpreter mode)
* and 9 (compiled mode, all optimizations enabled). The default level
Modified: helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java
===================================================================
--- helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java 2009-05-04 09:07:23 UTC (rev 9697)
+++ helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java 2009-05-04 11:52:17 UTC (rev 9698)
@@ -656,6 +656,21 @@
return loader;
}
+ /**
+ * Get a wrapper for an object that exposes it as Java object to JavaScript.
+ * This is useful for accessing strings and other primitives as their
+ * java.lang.* counterparts from JavaScript using the existing instance rather
+ * than allocating a new object via new java.lang.Foo() constructor.
+ * @param object an object
+ * @return the object wrapped as native java object
+ */
+ public Object asJavaObject(Object object) {
+ if (object instanceof Wrapper) {
+ return object;
+ }
+ return wrapFactory.wrapAsJavaObject(Context.getCurrentContext(), topLevelScope, object, null);
+ }
+
public WrapFactory getWrapFactory() {
return wrapFactory;
}