Ticket #407 (new task)

Opened 4 years ago

Last modified 8 weeks ago

Serialization of script class objects

Reported by: Philip Owned by: historic_bruno
Priority: Nice to Have Milestone: Backlog
Component: UI & Simulation Keywords:
Cc:

Description

It should be possible for scripts to create and use classes (function Thing(x) { this.x = x; }; Thing.prototype.foo = ...; y = new Thing(1); etc), and have them correctly (de)serialized.

This is probably infeasible in the general case, but it would be useful to support the simple case: classes are defined as top-level functions (not defined or changed at run-time inside other functions), no prototype chains, etc, so the code can simply serialize the object like a plain Object plus a class name that gets looked up in the appropriate script's scope when deserializing.

Change History

comment:1 Changed 4 years ago by Philip

  • Type changed from defect to task

comment:2 Changed 3 years ago by anonymous

  • Milestone Unclassified deleted

Milestone Unclassified deleted

comment:3 Changed 3 years ago by wacko

  • Milestone set to Backlog

comment:4 Changed 8 weeks ago by historic_bruno

  • Owner changed from Philip to historic_bruno

comment:5 Changed 8 weeks ago by historic_bruno

Current idea is to have the script that defines the constructor/prototype call a new function Engine.RegisterSerializablePrototype("Thing", Thing.prototype) which stores the name string and JSObject* associated with the prototype in a std::map or something. It is required that both the name and prototype be unique and only registered once (AIs have several script contexts, and each one might have an identically named but slightly different prototype definition).

The serializer uses the prototypes map to find if an arbitrary object's prototype matches a registered one, if so it gets marked as a class type object, the prototype name is stored, then its properties are enumerated like any other object. On deserialization, the reverse happens: it looks up the name and finds the prototype JSObject* which is passed to JS_NewObject, then its properties are restored - though not preserving the original prototype chain.

Note: See TracTickets for help on using tickets.