On 15 sep, 23:23, "Folke Behrens" <[EMAIL PROTECTED]> wrote: > > With all the conversions to JSOs/overlay types going on I was > wondering if it makes sense to provide implementions of the core > JavaScript objects as well. I've already submitted RegExp (issue > 1727)? (Granted, Math and Date are pretty similar to their Java > siblings but having them around too only hurts compile-time and makes > the JSO collection complete.) > > GWT's core module has several lightweight JSOs for arrays, why not > take it further and provide classes for most of JavaScript's global > objects, and instead of using big JSNI methods in java.* classes we > use the JSOs and rewrite the methods completely in Java. There's a lot > of API compatibility code written inside JSNI methods that should > better be written in real Java. Also, private helper methods like > java.lang.Math.round0() could be moved to JSMath.round(). > > Well, I think this extra layer will lead to smaller and faster > applications. People coming from other JavaScript frameworks will feel > more comfortable if they can slice() and splice() their arrays right > away. > > The attached ZIP contains JSRegExp and JSArray<E>. Is this incubator > material? (They're far from finished.)
AFAIK, your methods taking Java arrays or varargs (equivalent to arrays) won't work in hosted mode, as a Java array is not a JsArray. See code attached to Issue 2793: http://code.google.com/p/google-web-toolkit/issues/detail?id=2793 (side note: create(T... elements) could be written: « return Array.apply(...) ») Also, I'm uncomfortable with the monkey patching of Array to add forEach and reduce methods if they're missing (and even if those methods aren't ever called); couldn't they're be a global (static) function, either assigned from Array.prototype.forEach (resp. Array.prototype.reduce) or the "workaround" function? (wouldn't the compiler consider such a function "dead code" if it were never called?) E.g. private final static JavaScriptObject forEachFn; static { forEachFn = Compat.loadArrayForEach(); // same for reduce } with Compat.loadArrayForEach returning Array.prototype.forEach instead of assining it: if (Array.prototype.forEach) { return Array.prototype.forEach; } else { return function(fun) { ... }; } AFAIK, many other "extensions to ECMA-262" aren't supported in Internet Explorer (even IE7), so I wouldn't include them (hence issue #2793 only dealing with ECMA-262 methods), or emulate them all as the forEach and reduce methods. Side notes: - GALGWT is using newInstance() instead of create(); I've migrated GWT-in-the-AIR to follow the same naming, so GWT-Incubator should use it too. - shouldn't your implementations of forEach, map, reduce, filter, etc. pass an anonymous function instead of the execute() function? (isn't there a risk of loosing the 'this' of the Function object by passing a method that way?): return this.forEach(function(elt,i,arr) { [EMAIL PROTECTED]::execute(...) (elt, i, arr); }); (still I did the very same thing as you in my sort() implementation attached to issue 2793) - in our project at work where we're using JsArray* classes, I wrote a JsArrayUtils with a "forIteration" method copying the array to a Java array in hosted mode and just casting the JsArray to a Java array (in JSNI) in web mode; allowing for "for (MyClass obj : JsArrayUtils.forIteration(myJsArrayOfMyClass)) { ... }" constructs; I think it'd be a useful addition (and the "forIteration" naming should hopefully make it clear what's its intended usage). Code already posted to this group (search for reinterpretCast or forIteration) - JSDate would benefit from "bridge" methods to build a JSDate from a Java Date (or cast, if possible; eventually accessing the private JavaScriptObject field from the emulated Date class) and vice versa. - I would rename the JsArray.Function interface to Action and the JsArray.TestFunction to Predicate (that's how they're named in .NET, FWIW: you execute an Action forEach element in the array, and you evaluate –rather than execute– a Predicate to filter the array or test if every or some items match). JsArray.MapFunction could be named Mapper and JsArray.ReduceFunction Reducer too? - an JSMath.abs(int) might be useful (doesn't generate type-casting code when casting the result back to an int), same for float, short and byte; and same for JSMath.max and JSMath.min (byte, short, int, float, double) - JSMath is actually only useful for its constants and its min and max overloads taking more than 2 arguments, as apart from that it's fully equivalent to (even a subset of) the java.lang.Math class. --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
