Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Properties set on primitives' prototypes not propagated to instances

0 views
Skip to first unread message

tlrobinson

unread,
Oct 2, 2008, 5:58:06 AM10/2/08
to
If I set a property on a primitive object's prototype, it doesn't seem
to be propagated to instanced of that primitive, but *not* in the
Rhino shell, only if I create a scope with initStandardObjects. Also,
built in objects that aren't primitives, but actual Objects (like
Array and Date) work correctly, it's only String, Number, and Boolean.

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.

tlrobinson

unread,
Oct 2, 2008, 6:28:34 AM10/2/08
to
On further inspection, it definitely looks like I've encountered bug
#374918:

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...)

http://groups.google.com/group/mozilla.dev.tech.js-engine.rhino/browse_thread/thread/ddc2994b60894387#

Marc Guillemot

unread,
Oct 2, 2008, 8:35:45 AM10/2/08
to
Hi,

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

tlrobinson

unread,
Oct 2, 2008, 5:28:23 PM10/2/08
to
I haven't tried it outside the shell (I try to stay in JS-land as much
as possible!) but I suspect that's right.

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.

0 new messages