/* thread-safe */ static bool AddNativeMapping(IntPtr native, ScriptObject obj) { ScriptObjectToggleRef tref; if (native == IntPtr.Zero) { return(false); } lock (toggleRefs) { if (toggleRefs.ContainsKey(native)) { #if DEBUG throw new ExecutionEngineException("multiple mappings registered for the same NPObject peer"); #else Console.WriteLine("multiple mappings registered for the same NPObject peer 0x{0:x}, type = {1}", native, obj.GetType()); Console.WriteLine(Environment.StackTrace); return(false); #endif } tref = new ScriptObjectToggleRef(obj); toggleRefs[native] = tref; } tref.Initialize(); return(true); }
internal static ScriptObject LookupScriptObject(IntPtr obj_handle) { ScriptObject obj = null; ScriptObjectToggleRef tref = null; WeakReference wref = null; lock (toggleRefs) { toggleRefs.TryGetValue(obj_handle, out tref); } if (tref != null) { obj = tref.Target; } else { lock (cachedObjects) { cachedObjects.TryGetValue(obj_handle, out wref); if (wref != null) { if (wref.IsAlive) { obj = wref.Target as ScriptObject; } else { cachedObjects.Remove(obj_handle); } } } } return(obj); }
/* thread-safe */ static void FreeNativeMapping(ScriptObject obj) { ScriptObjectToggleRef tref = null; IntPtr native = obj.Handle; if (native == IntPtr.Zero) { return; } toggleRefs.TryGetValue(native, out tref); if (tref != null) { tref.Free(); toggleRefs.Remove(native); } GC.SuppressFinalize(obj); }