Closed Bug 8672 Opened 25 years ago Closed 25 years ago

messed up class/object hierarchy when using eval

Categories

(Core :: JavaScript Engine, defect, P3)

x86
Windows 95
defect

Tracking

()

RESOLVED FIXED

People

(Reporter: martin.honnen, Assigned: rogerl)

Details

Both c4.5 as well as mozilla m6 have an anomality regarding the class
hierarchy when using eval to define a prototype based object hierarchy.
c4.5 does not show the correct constructor and m6 with js1.4 support for
instanceof does not show the right relation. Hotjava with the rhino js
(I suppose) is doing it correctly. I use the following example

function God (name, desc) {
  this.name = name || '';
  this.desc = desc || '';
}
function NetGod (home, name, desc) {
  this.name = name || '';
  this.desc = desc || '';
  this.home = home;
}
NetGod.prototype = new God();
var kibo = new NetGod ('www.kibo.com', 'Kibo', 'He who greps');


plus the standard

function instanceOf(object, constructor) {
   while (object != null) {
      if (object == constructor.prototype)
         return true;
      object = object.__proto__;
   }
   return false;
}

Now when the above is included as SCRIPT together with the following
test output

alert('kibo.constructor: ' + kibo.constructor + '\n' + 'instanceOf
(kibo, NetGod): ' + instanceOf (kibo, NetGod) + '\n' + 'instanceOf
(kibo, God): ' + instanceOf (kibo, God) + '\n');
alert('kibo instanceof God: ' + (kibo instanceof God) + '\n' + 'kibo
instanceof NetGod: ' + (kibo instanceof NetGod) );

it shows
  kibo.constructor as God
and true for all instanceof/instanceOf checks.

But if the whole stuff is put in a js string and evaluated after body
onload the result is different:
  kibo.constructor as NetGod
and false for all instanceof/instanceOf checks.

Here is the complete example:

<HTML>
<HEAD>
<SCRIPT>
var os1 = "'kibo.constructor: ' + kibo.constructor + '\\n' + 'instanceOf (kibo,
NetGod): ' + instanceOf (kibo, NetGod) + '\\n' + 'instanceOf (kibo, God): ' +
instanceOf (kibo, God) + '\\n'";
var os2 = "'kibo instanceof God: ' + (kibo instanceof God) + '\\n' + 'kibo
instanceof NetGod: ' + (kibo instanceof NetGod) ";
var js = "function God (name, desc) {" + "\n\t"
       + "this.name = name || '';" + "\n\t"
       + "this.desc = desc || '';" + "\n\t"
       + "}" + "\n"
       + "function NetGod (home, name, desc) {" + "\n\t"
       + "this.name = name || '';" + "\n\t"
       + "this.desc = desc || '';" + "\n\t"
       + "this.home = home;" + "\n\t"
       + "}" + "\n"
       + "NetGod.prototype = new God();" + "\n"
       + "var kibo = new NetGod ('www.kibo.com', 'Kibo', 'He who greps');" +
"\n"
       + "alert(" + os1 + ");" + "\n"
       + (document.getElementById ? "alert(" + os2 + ");" : '');
</SCRIPT>
<SCRIPT>
function instanceOf(object, constructor) {
   while (object != null) {
      if (object == constructor.prototype)
         return true;
      object = object.__proto__;
   }
   return false;
}
</SCRIPT>
<SCRIPT>
eval(js);
function init () {
  document.gui.input.value = js;
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="eval(js); init();">
<FORM NAME="gui">
<INPUT TYPE="button" VALUE="evaluate"
       ONCLICK="this.form.output.value = eval(this.form.input.value);"
>
<BR>
<TEXTAREA NAME="input" ROWS="10" COLS="80" WRAP="off"></TEXTAREA>
<BR>
<TEXTAREA NAME="output" ROWS="10" COLS="80" WRAP="off"></TEXTAREA>
</FORM>
</BODY>
</HTML>


Note the difference between the first two alerts and the following ones. Both
Hotjava and IE5 show the result consistently like first given.
Assignee: mccabe → rogerl
Sorry to let this go stale.

Roger, could you take a look at this one?
Status: NEW → ASSIGNED
The problem is that the functions inside the onload mechanism are handled with
closure, and when the prototype property for the closure was set, it didn't
reach the constructor. I fixed this by acquiring the prototype for the
constructor call fro the constructing object rather than the function it gets
converted into. This is still a bug for Rhino - I need to ask Norris since I
think it's going to involve an API twiddle.
I checked in a fix for this (in Monkey) even though it's pending ECMA committee
discussion. The problem still exits for Rhino.
Status: ASSIGNED → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
Fixed in Rhino, too.
You need to log in before you can comment on or make changes to this bug.