Sirikata
liboh/plugins/js/JSSerializer.hpp
Go to the documentation of this file.
00001 #ifndef __SIRIKATA_JS_SERIALIZE_HPP__
00002 #define __SIRIKATA_JS_SERIALIZE_HPP__
00003 
00004 #include <sirikata/oh/Platform.hpp>
00005 
00006 #include <string>
00007 #include "JS_JSMessage.pbj.hpp"
00008 #include "JSObjectScript.hpp"
00009 #include "EmersonScript.hpp"
00010 #include <vector>
00011 #include <map>
00012 
00013 #include <v8.h>
00014 
00015 namespace Sirikata {
00016 namespace JS {
00017 
00018 const static int32 JSSERIALIZER_TOKEN= 30130;
00019 const static char* JSSERIALIZER_TOKEN_FIELD_NAME = "__JSSERIALIZER_TOKEN_FIELD_NAME&*^%$__";
00020 const static char* JSSERIALIZER_ROOT_OBJ_TOKEN   = "__JSDESERIALIZER_ROOT_OBJECT_NAME__";
00021 
00022 
00023 //Mult identify prototype of objects uniquely.  Cannot use "prototype" because
00024 //functions have prototype objects.  Must use a word that cannot be used to name
00025 //any other field.  Going with "this".
00026 const static char* JSSERIALIZER_PROTOTYPE_NAME= "this";
00027 
00028 const static char* FUNCTION_CONSTRUCTOR_TEXT = "function Function() { [native code] }";
00029 
00030 typedef std::vector<v8::Handle<v8::Object > > ObjectVec;
00031 typedef ObjectVec::iterator ObjectVecIter;
00032 
00033 struct LoopedObjPointer
00034 {
00035     LoopedObjPointer()
00036     {}
00037     LoopedObjPointer(const v8::Handle<v8::Object>& p, const String& n, int32 l)
00038      : parent(p),
00039        name(n),
00040        label(l)
00041     {}
00042 
00043     v8::Handle<v8::Object> parent;
00044     String name;
00045     int32 label;
00046 };
00047 
00048 typedef std::map<int32, v8::Handle<v8::Object> > ObjectMap;
00049 typedef ObjectMap::iterator ObjectMapIter;
00050 
00051 typedef std::vector<LoopedObjPointer> LoopedObjPointerList;
00052 typedef std::map<int32, LoopedObjPointerList> FixupMap;
00053 typedef FixupMap::iterator FixupMapIter;
00054 
00055 void debug_printSerialized(Sirikata::JS::Protocol::JSMessage jm, String prepend);
00056 void debug_printSerializedFieldVal(Sirikata::JS::Protocol::JSFieldValue jsfieldval, String prepend,String name);
00057 
00058 class JSSerializer
00059 {
00060     static void pointOtherObject(int32 int32ToPointTo,Sirikata::JS::Protocol::IJSFieldValue& jsf_value);
00061 
00062     static void annotateObject(ObjectVec& objVec, v8::Handle<v8::Object> v8Obj,int32 toStampWith);
00063 
00064     static void unmarkSerialized(ObjectVec& toUnmark);
00065     static void unmarkDeserialized(ObjectMap& objMap);
00066 
00067     static void setPrototype(v8::Handle<v8::Object> toSetProtoOf, v8::Handle<v8::Object> toSetTo);
00068     
00069     static void serializeVisible(v8::Local<v8::Object> v8Obj, Sirikata::JS::Protocol::IJSMessage&,int32& toStampWith,ObjectVec& allObjs);
00070     static void fillVisible(Sirikata::JS::Protocol::IJSMessage&, const SpaceObjectReference& listenTo);// Reused by serializePresence
00071     static void serializePresence(v8::Local<v8::Object> v8Obj, Sirikata::JS::Protocol::IJSMessage&,int32& toStampWith,ObjectVec& allObjs);
00072     static void serializeSystem(v8::Local<v8::Object> v8Obj, Sirikata::JS::Protocol::IJSMessage&,int32& toStampWith,ObjectVec& allObjs);
00073     static void serializeObjectInternal(v8::Local<v8::Value> v8Val, Sirikata::JS::Protocol::IJSMessage&,int32& toStampWith,ObjectVec& allObjs );
00074 
00075     static void serializeInternalFields(v8::Local<v8::Object> v8Obj, Sirikata::JS::Protocol::JSMessage&,ObjectVec& allObjs);
00076     static void serializeAddressable(v8::Local<v8::Object> v8Obj, Sirikata::JS::Protocol::JSMessage&,ObjectVec& allObjs);
00077 
00078 
00079     static void shallowCopyFields(v8::Handle<v8::Object> dst, v8::Handle<v8::Object> src);
00080     static bool deserializePerformFixups(ObjectMap& labeledObjs, FixupMap& toFixUp);
00081 
00082     static void serializeFieldValueInternal(Sirikata::JS::Protocol::IJSFieldValue& jsf_value,v8::Handle<v8::Value> prop_val,int32 & toStampWith,ObjectVec& objVec);
00083 
00084     
00085     static bool deserializeObjectInternal( EmersonScript* jsObjScript, Sirikata::JS::Protocol::JSMessage jsmessage,v8::Handle<v8::Object>& deserializeTo, ObjectMap& labeledObjs,FixupMap& toFixUp);
00086 
00087     static v8::Handle<v8::Value> deserializeFieldValue(EmersonScript* emerScript,
00088         Sirikata::JS::Protocol::JSFieldValue jsvalue, ObjectMap& labeledObjs,FixupMap& toFixUp,
00089         int32& toLoopTo);
00090 
00091 
00092 public:
00093     
00094     //deprecated
00095     static std::string serializeObject(v8::Local<v8::Value> v8Val,int32 toStamp = 0);
00096     static std::string serializeMessage(v8::Local<v8::Value> v8Val, int32 toStamp=0);
00097 
00098     //both of these must be called from within a v8 context
00099     static v8::Handle<v8::Value> deserializeMessage( EmersonScript* emerScript, Sirikata::JS::Protocol::JSFieldValue jsfieldval,bool& deserializeSuccessful);
00100     //both of these must be called from within a v8 context
00101     static v8::Handle<v8::Object> deserializeObject( EmersonScript* emerScript, Sirikata::JS::Protocol::JSMessage jsmessage,bool& deserializeSuccessful);
00102 };
00103 
00104 }}//end namespaces
00105 
00106 #endif