Here's a test case. As you can see, only Array and Date objects
correctly inherit the "isa" property from their prototypes, even
though it clearly *is* set on the prototype:
print("== Shell scope ==");
String.prototype.isa = "ok!";
Array.prototype.isa = "ok!";
Number.prototype.isa = "ok!";
Boolean.prototype.isa = "ok!";
Date.prototype.isa = "ok!";
var string = "string";
print("string=" + string + ", " + string.isa + " (" +
String.prototype.isa + ")");
var array = [1,2,3];
print("array=" + array + ", " + array.isa + " (" +
Array.prototype.isa + ")");
var number = 42;
print("number=" + number + ", " + number.isa + " (" +
Number.prototype.isa + ")");
var bool = true;
print("bool=" + bool + ", " + bool.isa + " (" +
Boolean.prototype.isa + ")");
var date = new Date();
print("date=" + date + ", " + date.isa + " (" + Date.prototype.isa
+ ")");
cx = Packages.org.mozilla.javascript.Context.enter();
scope = cx.initStandardObjects();
cx.evaluateString(scope, '\
print = function(obj)
{ java.lang.System.out.println(String(obj)); }; \
print("== Standard scope =="); \
String.prototype.isa = "ok!"; \
Array.prototype.isa = "ok!"; \
Number.prototype.isa = "ok!"; \
Boolean.prototype.isa = "ok!"; \
Date.prototype.isa = "ok!"; \
var string = "string"; \
print("string=" + string + ", " + string.isa + " (" +
String.prototype.isa + ")"); \
var array = [1,2,3]; \
print("array=" + array + ", " + array.isa + " (" +
Array.prototype.isa + ")"); \
var number = 42; \
print("number=" + number + ", " + number.isa + " (" +
Number.prototype.isa + ")"); \
var bool = true; \
print("bool=" + bool + ", " + bool.isa + " (" +
Boolean.prototype.isa + ")"); \
var date = new Date(); \
print("date=" + date + ", " + date.isa + " (" +
Date.prototype.isa + ")");',
"<cmd>", 1, null);
Any idea what's going on? Is this a manifestation of this bug:
https://bugzilla.mozilla.org/show_bug.cgi?id=374918
We use this technique in Objective-J (http://cappuccino.org) to "toll-
free bridge" the built in JS objects to their Objective-J
counterparts, so it would be great if it worked in Rhino (already does
work, only in the shell)
Thanks.
https://bugzilla.mozilla.org/show_bug.cgi?id=374918
In my test case (which was actually a very poor example since both
cases used the same values it appeared to work correctly), change the
first set of *.prototype.isa to equal something else like "bad!", and
you end up with this:
string=string, bad! (ok!)
array=1,2,3, ok! (ok!)
number=42, bad! (ok!)
bool=true, bad! (ok!)
date=Thu Oct 02 2008 03:18:35 GMT-0700 (PDT), ok! (ok!)
Coincidentally, Marc Guillemot just posted about this yesterday (which
I clearly missed...)
if I correctly understand you, it happens only in the Rhino shell, is
that right? I don't know the shell at all, but I can imagine that it is
an other manifestation of bug 374918 as the shell surely initializes a
default scope.
Cheers,
Marc.
--
Web: http://www.efficient-webtesting.com
Blog: http://mguillem.wordpress.com
I came up with an awful temporary workaround (specific to my
situation, not generic) that I think further confirms this is bug
#374918: whenever I call evaluateString I set the first scope's (Rhino
shell's scope) String.prototype.isa, Number.prototype.isa, and
Boolean.prototype.isa equal to the second scope's
(initStandardObjects's scope), then I restore them when it's done.
Then strings, numbers, and booleans have the correct "isa" property.