r9792 - in helma-ng/trunk: modules modules/core modules/helma src/org/helma/javascript
[email protected] Sat, 16 May 2009 00:18:50 +0200 (CEST)
| Newsgroups | gmane.comp.java.helma.cvs |
|---|---|
| Message-ID | <20090515221850.3E5D53D0D6@mia> |
Author: hannes
Date: 2009-05-16 00:18:50 +0200 (Sat, 16 May 2009)
New Revision: 9792
Modified:
helma-ng/trunk/modules/binary.js
helma-ng/trunk/modules/core/string.js
helma-ng/trunk/modules/helma/system.js
helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java
Log:
Add asJavaString() method to RhinoEngine and helma/system which returns a wrapper for a java string. This is easier to use for our needs (calling java.lang.String.toBytes(), mostly). Leave asJavaObject() there for other uses.
Details at http://dev.helma.org/trac/helma/changeset/9792
Modified: helma-ng/trunk/modules/binary.js
===================================================================
--- helma-ng/trunk/modules/binary.js 2009-05-15 22:18:47 UTC (rev 9791)
+++ helma-ng/trunk/modules/binary.js 2009-05-15 22:18:50 UTC (rev 9792)
@@ -1,5 +1,5 @@
-var asJavaObject = require('helma/system').asJavaObject;
+var asJavaString = require('helma/system').asJavaString;
var Binary = exports.Binary = function(bytes) {
this.bytes = bytes;
@@ -18,8 +18,8 @@
String.prototype.toBinary = function(encoding) {
var bytes = encoding ?
- asJavaObject(this.toString()).getBytes(encoding) :
- asJavaObject(this.toString()).getBytes();
+ asJavaString(this).getBytes(encoding) :
+ asJavaString(this).getBytes();
return new Binary(bytes);
};
Modified: helma-ng/trunk/modules/core/string.js
===================================================================
--- helma-ng/trunk/modules/core/string.js 2009-05-15 22:18:47 UTC (rev 9791)
+++ helma-ng/trunk/modules/core/string.js 2009-05-15 22:18:50 UTC (rev 9792)
@@ -14,6 +14,8 @@
* $Date: 2007-12-13 13:21:48 +0100 (Don, 13 Dez 2007) $
*/
+var asJavaString = require('helma/system').asJavaString;
+
__shared__ = true;
Object.defineProperty(String, 'ANUMPATTERN', { value: /[^a-zA-Z0-9]/ });
@@ -394,7 +396,7 @@
*/
Object.defineProperty(String.prototype, "md5", {
value: function() {
- var str = new java.lang.String(this);
+ var str = asJavaString(this);
var md = java.security.MessageDigest.getInstance('MD5');
var b = md.digest(str.getBytes())
var buf = new java.lang.StringBuffer(b.length * 2);
@@ -640,7 +642,7 @@
*/
Object.defineProperty(String.prototype, "enbase64", {
value: function() {
- var bytes = new java.lang.String(this) . getBytes();
+ var bytes = asJavaString(this).getBytes();
return new Packages.sun.misc.BASE64Encoder().encode(bytes);
}
});
Modified: helma-ng/trunk/modules/helma/system.js
===================================================================
--- helma-ng/trunk/modules/helma/system.js 2009-05-15 22:18:47 UTC (rev 9791)
+++ helma-ng/trunk/modules/helma/system.js 2009-05-15 22:18:50 UTC (rev 9792)
@@ -9,6 +9,7 @@
'properties',
'addHostObject',
'addRepository',
+ 'asJavaString',
'asJavaObject',
'createSandbox',
'evaluate',
@@ -64,9 +65,6 @@
/**
* 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
*/
@@ -75,6 +73,17 @@
}
/**
+ * Get a wrapper for a string that exposes the java.lang.String methods to JavaScript
+ * This is useful for accessing strings as java.lang.String without the cost of
+ * creating a new instance.
+ * @param object an object
+ * @return the object converted to a string and wrapped as native java object
+ */
+function asJavaString(object) {
+ return getRhinoEngine().asJavaString(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-15 22:18:47 UTC (rev 9791)
+++ helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java 2009-05-15 22:18:50 UTC (rev 9792)
@@ -662,10 +662,22 @@
}
/**
+ * Get a wrapper for a string that exposes the java.lang.String methods to JavaScript
+ * This is useful for accessing strings as java.lang.String without the cost of
+ * creating a new instance.
+ * @param object an object
+ * @return the object converted to a string and wrapped as native java object
+ */
+ public Object asJavaString(Object object) {
+ if (!(object instanceof String)) {
+ object = object.toString();
+ }
+ Context cx = Context.getCurrentContext();
+ return wrapFactory.wrapAsJavaObject(cx, topLevelScope, object, null);
+ }
+
+ /**
* 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
*/
@@ -677,10 +689,19 @@
return wrapFactory.wrapAsJavaObject(cx, topLevelScope, object, null);
}
+ /**
+ * Get the engine's WrapFactory.
+ * @return
+ */
public WrapFactory getWrapFactory() {
return wrapFactory;
}
+ /**
+ * Get a class wrapper that can be extended by adding properties to its prototype property.
+ * @param type a Java class
+ * @return an extensible wrapper for the class
+ */
public ExtendedJavaClass getExtendedClass(Class type) {
ExtendedJavaClass wrapper = javaWrappers.get(type.getName());
if (wrapper == null || wrapper == ExtendedJavaClass.NONE) {