Ticket #2475: MapSerialization_v1.0.diff
File MapSerialization_v1.0.diff, 5.4 KB (added by , 10 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Timer.js
7 7 { 8 8 this.id = 0; 9 9 this.time = 0; 10 this.timers = {};10 this.timers = new Map(); 11 11 this.turnLength = 0; 12 12 }; 13 13 … … 33 33 Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data) 34 34 { 35 35 var id = ++this.id; 36 this.timers [id] = [ent, iid, funcname, this.time + time, 0, data];36 this.timers.set(id, [ent, iid, funcname, this.time + time, 0, data]); 37 37 return id; 38 38 }; 39 39 … … 51 51 if (typeof repeattime != "number" || !(repeattime > 0)) 52 52 error("Invalid repeattime to SetInterval of "+funcname); 53 53 var id = ++this.id; 54 this.timers [id] = [ent, iid, funcname, this.time + time, repeattime, data];54 this.timers.set(id, [ent, iid, funcname, this.time + time, repeattime, data]); 55 55 return id; 56 56 }; 57 57 … … 60 60 */ 61 61 Timer.prototype.CancelTimer = function(id) 62 62 { 63 delete this.timers[id];63 this.timers.delete(id); 64 64 }; 65 65 66 66 … … 73 73 // Collect the timers that need to run 74 74 // (We do this in two stages to avoid deleting from the timer list while 75 75 // we're in the middle of iterating through it) 76 var run = [];77 for ( var id in this.timers)76 var run = new Set(); 77 for (let id of this.timers.keys()) 78 78 { 79 if (this.timers [id][3] <= this.time)80 run. push(id);79 if (this.timers.get(id)[3] <= this.time) 80 run.add(id); 81 81 } 82 for ( var i = 0; i < run.length; ++i)82 for (let id of run) 83 83 { 84 var id = run[i]; 85 86 var t = this.timers[id]; 84 var t = this.timers.get(id); 87 85 if (!t) 88 86 continue; // an earlier timer might have cancelled this one, so skip it 89 87 … … 91 89 if (!cmp) 92 90 { 93 91 // The entity was probably destroyed; clean up the timer 94 delete this.timers[id];92 this.timers.delete(id); 95 93 continue; 96 94 } 97 95 … … 110 108 t[3] += t[4]; 111 109 // Add it to the list to get re-executed if it's soon enough 112 110 if (t[3] <= this.time) 113 run. push(id);111 run.add(id); 114 112 } 115 113 else 116 114 { 117 115 // Non-repeating time - delete it 118 delete this.timers[id];116 this.timers.delete(id); 119 117 } 120 118 } 121 119 } -
source/simulation2/serialization/BinarySerializer.cpp
223 223 m_Serializer.Bool("value", b); 224 224 break; 225 225 } 226 else if (protokey == JSProto_Map) 227 { 228 // There's no C++ API (yet) to work with maps. This code relies on the internal 229 // structure of the Iterator object returned by Map.entries. This is not ideal 230 // because the structure could change in the future (and actually does change with v31) 231 u32 mapSize; 232 m_ScriptInterface.GetProperty(val, "size", mapSize); 233 234 m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_MAP); 235 m_Serializer.NumberU32_Unbounded("map size", mapSize); 236 237 JS::RootedValue keyValueIterator(cx); 238 m_ScriptInterface.CallFunction(val, "entries", &keyValueIterator); 239 for(u32 i=0; i<mapSize; i++) 240 { 241 JS::RootedValue keyValuePair(cx); 242 ENSURE(m_ScriptInterface.CallFunction(keyValueIterator, "next", &keyValuePair)); 243 244 JS::RootedObject keyValuePairObj(cx, &keyValuePair.toObject()); 245 246 JS::RootedValue key(cx); 247 JS::RootedValue value(cx); 248 ENSURE(JS_GetElement(cx, keyValuePairObj, 0, key.address())); 249 ENSURE(JS_GetElement(cx, keyValuePairObj, 1, value.address())); 250 251 HandleScriptVal(key); 252 HandleScriptVal(value); 253 } 254 255 break; 256 } 226 257 else 227 258 { 228 259 // Unrecognized class -
source/simulation2/serialization/SerializedScriptTypes.h
34 34 SCRIPT_TYPE_OBJECT_PROTOTYPE = 11, // user-defined prototype 35 35 SCRIPT_TYPE_OBJECT_NUMBER = 12, // standard Number class 36 36 SCRIPT_TYPE_OBJECT_STRING = 13, // standard String class 37 SCRIPT_TYPE_OBJECT_BOOLEAN = 14 // standard Boolean class 37 SCRIPT_TYPE_OBJECT_BOOLEAN = 14, // standard Boolean class 38 SCRIPT_TYPE_OBJECT_MAP = 15 // Map class 38 39 }; 39 40 40 41 // ArrayBufferView subclasses (to avoid relying directly on the JSAPI enums) -
source/simulation2/serialization/StdDeserializer.cpp
389 389 390 390 return JS::ObjectValue(*bufferObj); 391 391 } 392 case SCRIPT_TYPE_OBJECT_MAP: 393 { 394 u32 mapSize; 395 NumberU32_Unbounded("map size", mapSize); 396 JS::RootedValue mapVal(cx); 397 m_ScriptInterface.Eval("(new Map())", &mapVal); 398 399 for(u32 i=0; i<mapSize; i++) 400 { 401 JS::RootedValue key(cx, ReadScriptVal("map key", JS::NullPtr())); 402 JS::RootedValue value(cx, ReadScriptVal("map value", JS::NullPtr())); 403 m_ScriptInterface.CallFunctionVoid(mapVal, "set", key, value); 404 } 405 AddScriptBackref(&mapVal.toObject()); 406 return mapVal; 407 } 392 408 default: 393 409 throw PSERROR_Deserialize_OutOfBounds(); 394 410 }