PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Ruby/Runtime/ConstantStorage.cs

#
C# | 70 lines | 45 code | 10 blank | 15 comment | 8 complexity | 6888733effba3a8b5f5d0e7913c6d8ee MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using Microsoft.Scripting.Utils;
  17. using IronRuby.Builtins;
  18. namespace IronRuby.Runtime {
  19. internal struct ConstantStorage {
  20. internal static ConstantStorage Removed = new ConstantStorage(false);
  21. public readonly object Value;
  22. public readonly WeakReference WeakValue;
  23. internal bool IsRemoved {
  24. get { return Value == Removed.Value; }
  25. }
  26. internal ConstantStorage(object value) {
  27. Value = value;
  28. if (value == null) {
  29. // We use null as a missing constant sentinel and the real null is wrapped in a singleton WeakRef:
  30. WeakValue = ConstantSiteCache.WeakNull;
  31. } else {
  32. switch (Type.GetTypeCode(value.GetType())) {
  33. case TypeCode.Object:
  34. RubyModule module = value as RubyModule;
  35. if (module != null) {
  36. WeakValue = module.WeakSelf;
  37. } else {
  38. WeakValue = new WeakReference(value);
  39. }
  40. break;
  41. case TypeCode.String:
  42. WeakValue = new WeakReference(value);
  43. break;
  44. default:
  45. WeakValue = null;
  46. break;
  47. }
  48. }
  49. }
  50. internal ConstantStorage(object value, WeakReference weakValue) {
  51. Assert.NotNull(value, weakValue);
  52. Value = value;
  53. WeakValue = weakValue;
  54. }
  55. private ConstantStorage(bool dummy) {
  56. Value = new object();
  57. WeakValue = null;
  58. }
  59. }
  60. }