[SERVER-770] objects serialised to system.js collection lose prototype and all methods defined outside the constructor Created: 16/Mar/10  Updated: 16/Mar/10  Resolved: 16/Mar/10

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: 1.3.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: dan Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mac OS 10.6 64 bit. Seems to be JS-based, so probably all.


Participants:

 Description   

when an object is serialized to system.js and then later instantiated, it loses its prototype and all methods defined outside its constructor.

To see this, consider the following script

//currently this prints outputs and doesn't do any nice asserts
//this si for clarity of bug reporting

var s = db.system.js;

PrototypicalInheritanceObject = function ()

{ this.constructor_assigned_property = 'this constructor_assigned_property exists'; }

PrototypicalInheritanceObject.own_property = 'this own_property exists';

PrototypicalInheritanceObject.own_method = function ()

{ return "this own_method exists"; }

PrototypicalInheritanceObject.prototype.prototypical_property = 'this prototypical_property exists';

PrototypicalInheritanceObject.prototype.prototypical_method = function ()

{ return "this prototypical_method exists"; }

print("======================================");
print("Client side");
print("======================================");

//test that this behaves as normal in the shell interpreter
var p = new PrototypicalInheritanceObject();
print("constructor property - shell interpreter:");
print(p.constructor_assigned_property);
print("");
print("own property - shell interpreter:");
print(p.own_property);
print("");
print("own method - shell interpreter:");
print(tojson(p.own_method));
print("");
print("prototypical property - shell interpreter:");
print(p.prototypical_property);
print("");
print("prototypical method - shell interpreter:");
print(tojson(p.prototypical_method));

print("======================================");
print("server side");
print("======================================");
s.insert( { _id : "PrototypicalInheritanceObject", value : PrototypicalInheritanceObject});
print("constructor property - server interpreter:");
print(db.eval("var p = new PrototypicalInheritanceObject(); return p.constructor_assigned_property"));
print("");
print("own property - server interpreter:");
print(db.eval("var p = new PrototypicalInheritanceObject(); return p.own_property"));
print("");
print("own method - server interpreter:");
print(db.eval("var p = new PrototypicalInheritanceObject(); return tojson(p.own_method)"));
print("");
print("prototypical property - server interpreter:");
print(db.eval("var p = new PrototypicalInheritanceObject(); return p.prototypical_property"));
print("");
print("prototypical method - server interpreter:");
print(db.eval("var p = new PrototypicalInheritanceObject(); return tojson(p.prototypical_property)"));

The output for the client side and server side phases seem like they should be the same, but they are not.

======================================
Client side
======================================
constructor property - shell interpreter:
this constructor_assigned_property exists

own property - shell interpreter:
undefined

own method - shell interpreter:
undefined

prototypical property - shell interpreter:
this prototypical_property exists

prototypical method - shell interpreter:
function () { return "this prototypical_method exists"; }

======================================
server side
======================================
constructor property - server interpreter:
this constructor_assigned_property exists

own property - server interpreter:
null

own method - server interpreter:
undefined

prototypical property - server interpreter:
null

prototypical method - server interpreter:
undefined



 Comments   
Comment by Eliot Horowitz (Inactive) [ 16/Mar/10 ]

more on mongodb-user

Comment by dan [ 16/Mar/10 ]

Unsure if I've been missing the point here, so I opened up a discussion on mongo-users pertaining to this issue:
http://groups.google.com/group/mongodb-user/t/27aaf8261b5fff46

Generated at Thu Feb 08 02:55:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.