// --- snip --- newObject = Type.createInstance(objectClass,[]); // Initializes the class // --- snip --- // Generate copying body var f_id = 0; for(f in Reflect.fields(newObject)) { if( f == '_manager' ) continue; var field = Reflect.field(newObject, f); if( field != null ) { code.prepareObject += ", f."+f; if( new String(field) == field || Reflect.hasField(field, '__a') || Reflect.isObject(field) && !Std.is(field, data.ObjectField) ) throw Error.ModelDefinition("The new() constructor of class "+Type.getClassName(objectClass)+" is too complicated and will impact performance severely."+ " Please only set numbers, booleans and objects implementing data.ObjectField"); if( Reflect.isObject(field) ) code.copyFields += f+" => $new(f["+(f_id++)+"]),\n\t\t\t"; else code.copyFields += f+" => f["+(f_id++)+"],\n\t\t\t"; // Delete field in newObject so it wont be copied unessaserily // Reflect.setField(newObject, f, null); } } // Strip first comma code.prepareObject = code.prepareObject.substr(2); // Generate get function body for(f in 0 ... objectFields.length) code.getFields += "if(s["+f+"] != null) o."+objectFields[f]+"._v = s["+f+"];\n\t\t"; // Generate set function body for(f in 0 ... objectFields.length) code.setFields += "if(o."+objectFields[f]+".changed) a["+f+"] = if( (s = o."+objectFields[f]+".serialize()) != null ) s.__s;\n\t\t"; // Generate code using template resource var nekoml = new haxe.Template(Std.resource("MemoryPersistenceMacro.nml")).execute(code); // trace(nekoml.toString()); // --- snip --- // --- generates: --- makeGetter = function(h, f, m, p) { f = $array(f.df_1, f.df_2, f.nrTest, f.upgradeWasHandled); return function(i) { var o = { df_1 => $new(f[0]), df_2 => $new(f[1]), nrTest => f[2], upgradeWasHandled => f[3], _manager => m, _oid => i } $objsetproto(o,p); var s = $hget(h,i,null); if( s == null ) return o; if(s[0] != null) o.df_1._v = s[0]; if(s[1] != null) o.df_2._v = s[1]; return o; } } makeSetter = function(h, hlen) { return function(o) { var a = $hget(h, o._oid, null); if( a == null) { a = $amake(hlen); $hset(h, o._oid, a, null); } var s; if(o.df_1.changed) a[0] = if( (s = o.df_1.serialize()) != null ) s.__s; if(o.df_2.changed) a[1] = if( (s = o.df_2.serialize()) != null ) s.__s; return a; } } $exports.makeGetter = makeGetter; $exports.makeSetter = makeSetter;