Opened 14 years ago

Closed 11 years ago

Last modified 11 years ago

#407 closed task (fixed)

Serialization of script class objects

Reported by: Philip Taylor Owned by: historic_bruno
Priority: Nice to Have Milestone: Alpha 14
Component: UI & Simulation Keywords:
Cc: Patch:

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

comment:1 by Philip Taylor, 14 years ago

Type: defecttask

comment:2 by (none), 14 years ago

Milestone: Unclassified

Milestone Unclassified deleted

comment:3 by Andrew, 14 years ago

Milestone: Backlog

comment:4 by historic_bruno, 11 years ago

Owner: changed from Philip Taylor to historic_bruno

comment:5 by historic_bruno, 11 years ago

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.

comment:6 by ben, 11 years ago

Resolution: fixed
Status: newclosed

In 13429:

Extends binary serializer to support some standard JS classes: Number, String, Boolean. Fixes #406.
Extends binary serializer to support typed arrays.
Extends binary serializer to support custom JS prototype objects in AIs, fixes #407.
Allows full serialization of AIs (not yet implemented). Refs #1089, #1886
Increases binary serializer script backref arena from 8 MB to 16 MB, refs #1842

comment:7 by leper, 11 years ago

Milestone: BacklogAlpha 14
Note: See TracTickets for help on using tickets.