public override void BindUnmanagedAndManagedInstances(object obj, JSContext ctxt) { var key = ctxt.Key; var tkp = new TypeAndKeyPair { type = rootType, key = key }; outer.keyToObjectCache.Add(tkp, obj); var top = new TypeAndObjectPair { type = rootType, obj = obj }; outer.objectToKeyCache.Add(top, key); // obj is always new outer.objectToDisconnect.Add(obj, () => Disconnect(tkp, top)); }
// XREF1093 public override object Import(StringParser sp) { sp.SkipWS(); if (sp.TryConsumeLit("null")) return null; else { var key = sp.ConsumeEscapedString(); sp.SkipWS(); var qnm = default(string); if (!sp.TryConsumeLit("null")) qnm = sp.ConsumeEscapedString(); if (sp.Failed) throw new InvalidOperationException("invalid response"); var obj = default(object); var tkp = new TypeAndKeyPair { type = rootType, key = key }; if (outer.keyToObjectCache.TryGetValue(tkp, out obj)) { if (!type.IsAssignableFrom(obj.GetType())) throw outer.MakeInvalidCastException(); } else { if (qnm == null) obj = outer.CreateDefaultInstance(type, key, 0); else { var rttype = QualifiedNameToType(qnm); if (!type.IsAssignableFrom(rttype)) throw outer.MakeInvalidCastException(); obj = outer.CreateDefaultInstance(rttype, key, 0); } outer.keyToObjectCache.Add(tkp, obj); var top = new TypeAndObjectPair { type = rootType, obj = obj }; outer.objectToKeyCache.Add(top, key); // obj is always new outer.objectToDisconnect.Add(obj, () => Disconnect(tkp, top)); } return obj; } }
public override object BindCreatedInstance(StringParser sp, object obj) { if (outer.objectToDisconnect.ContainsKey(obj)) throw new InvalidOperationException("object already associated with unmanaged instance"); sp.SkipWS(); var key = sp.ConsumeEscapedString(); if (sp.Failed) throw new InvalidOperationException("invalid response"); var tkp = new TypeAndKeyPair { type = rootType, key = key }; outer.keyToObjectCache.Add(tkp, obj); var top = new TypeAndObjectPair { type = rootType, obj = obj }; outer.objectToKeyCache.Add(top, key); outer.objectToDisconnect.Add(obj, () => Disconnect(tkp, top)); return obj; }
private void Disconnect(TypeAndKeyPair tkp, TypeAndObjectPair top) { outer.keyToObjectCache.Remove(tkp); outer.objectToKeyCache.Remove(top); outer.Eval ((sb, _) => { sb.Append(outer.database.RootExpression); sb.Append(".DisconnectKeyedObject("); sb.Append(outer.TypeIndex(null, type)); sb.Append(",\""); Lexemes.AppendStringToJavaScript(sb, tkp.key); sb.Append("\");"); }, null); }