PageRenderTime 71ms CodeModel.GetById 24ms RepoModel.GetById 3ms app.codeStats 0ms

/src/sys/js/fan/Field.js

https://bitbucket.org/bedlaczech/fan-1.0
JavaScript | 136 lines | 85 code | 18 blank | 33 comment | 27 complexity | 77d6946bb958d49a24885f72b198bacd MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2009, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 24 Mar 09 Andy Frank Creation
  7. // 20 May 09 Andy Frank Refactor to new OO model
  8. //
  9. /**
  10. * Field.
  11. */
  12. fan.sys.Field = fan.sys.Obj.$extend(fan.sys.Slot);
  13. //////////////////////////////////////////////////////////////////////////
  14. // Factories
  15. //////////////////////////////////////////////////////////////////////////
  16. fan.sys.Field.makeSetFunc = function(map)
  17. {
  18. return fan.sys.Func.make(
  19. fan.sys.List.make(fan.sys.Param.$type),
  20. fan.sys.Void.$type,
  21. function(obj)
  22. {
  23. var keys = map.keys();
  24. for (var i=0; i<keys.size(); i++)
  25. {
  26. var field = keys.get(i);
  27. var val = map.get(field);
  28. field.set(obj, val, false); //, obj != inCtor);
  29. }
  30. });
  31. }
  32. //////////////////////////////////////////////////////////////////////////
  33. // Constructor
  34. //////////////////////////////////////////////////////////////////////////
  35. fan.sys.Field.prototype.$ctor = function(parent, name, flags, type, facets)
  36. {
  37. this.m_parent = parent;
  38. this.m_name = name;
  39. this.m_qname = parent.qname() + "." + name;
  40. this.m_flags = flags;
  41. this.m_type = type;
  42. this.m_$name = this.$$name(name);
  43. this.m_$qname = this.m_parent.m_$qname + '.m_' + this.m_$name;
  44. this.m_getter = null;
  45. this.m_setter = null;
  46. this.m_facets = new fan.sys.Facets(facets);
  47. }
  48. //////////////////////////////////////////////////////////////////////////
  49. // Obj
  50. //////////////////////////////////////////////////////////////////////////
  51. fan.sys.Field.prototype.trap = function(name, args)
  52. {
  53. // private undocumented access
  54. if (name == "getter") return this.m_getter;
  55. if (name == "setter") return this.m_setter;
  56. return fan.sys.Obj.prototype.trap.call(this, name, args);
  57. }
  58. //////////////////////////////////////////////////////////////////////////
  59. // Methods
  60. //////////////////////////////////////////////////////////////////////////
  61. fan.sys.Field.prototype.type = function() { return this.m_type; }
  62. fan.sys.Field.prototype.get = function(instance)
  63. {
  64. if (this.isStatic())
  65. {
  66. return eval(this.m_$qname);
  67. }
  68. else
  69. {
  70. var target = instance;
  71. if ((this.m_flags & fan.sys.FConst.Native) != 0)
  72. target = instance.peer;
  73. var getter = target[this.m_$name];
  74. if (getter != null)
  75. return getter.call(target);
  76. else
  77. return target["m_"+this.m_$name]
  78. }
  79. }
  80. fan.sys.Field.prototype.set = function(instance, value, checkConst)
  81. {
  82. if (checkConst === undefined) checkConst = true;
  83. // check const
  84. if ((this.m_flags & fan.sys.FConst.Const) != 0)
  85. {
  86. if (checkConst)
  87. throw fan.sys.ReadonlyErr.make("Cannot set const field " + this.m_qname);
  88. else if (value != null && !fan.sys.ObjUtil.isImmutable(value))
  89. throw fan.sys.ReadonlyErr.make("Cannot set const field " + this.m_qname + " with mutable value");
  90. }
  91. // check static
  92. if ((this.m_flags & fan.sys.FConst.Static) != 0) // && !parent.isJava())
  93. throw fan.sys.ReadonlyErr.make("Cannot set static field " + this.m_qname);
  94. // check type
  95. if (value != null && !fan.sys.ObjUtil.$typeof(value).is(this.m_type.toNonNullable()))
  96. throw fan.sys.ArgErr.make("Wrong type for field " + this.m_qname + ": " + this.m_type + " != " + fan.sys.ObjUtil.$typeof(value));
  97. // TODO
  98. //if (setter != null)
  99. //{
  100. // setter.invoke(instance, new Object[] { value });
  101. // return;
  102. //}
  103. if ((this.m_flags & fan.sys.FConst.Native) != 0)
  104. {
  105. var peer = instance.peer;
  106. var setter = peer[this.m_$name + "$"];
  107. setter.call(peer, instance, value);
  108. }
  109. else
  110. {
  111. var setter = instance[this.m_$name + "$"];
  112. if (setter != null)
  113. setter.call(instance, value);
  114. else
  115. instance["m_"+this.m_$name] = value;
  116. }
  117. }
  118. fan.sys.Field.prototype.$typeof = function() { return fan.sys.Field.$type; }