/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/NATField.java

http://ambienttalk.googlecode.com/ · Java · 184 lines · 120 code · 20 blank · 44 comment · 6 complexity · a12201386adc0ab79bd25b0a86cc46bf MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATField.java created on Jul 27, 2006 at 2:27:53 AM
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects.natives;
  29. import java.util.HashMap;
  30. import edu.vub.at.eval.Evaluator;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.exceptions.XIllegalOperation;
  33. import edu.vub.at.exceptions.XSelectorNotFound;
  34. import edu.vub.at.exceptions.XTypeMismatch;
  35. import edu.vub.at.objects.ATContext;
  36. import edu.vub.at.objects.ATField;
  37. import edu.vub.at.objects.ATMethod;
  38. import edu.vub.at.objects.ATObject;
  39. import edu.vub.at.objects.ATTable;
  40. import edu.vub.at.objects.coercion.NativeTypeTags;
  41. import edu.vub.at.objects.grammar.ATSymbol;
  42. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  43. import edu.vub.at.objects.natives.grammar.AGSymbol;
  44. import edu.vub.util.TempFieldGenerator;
  45. /**
  46. * NATField implements a causally connected field of an object. Rather than storing
  47. * these fields directly in every object, we choose to only make people pay when they
  48. * choose to reify them.
  49. *
  50. * @author smostinc, tvcutsem
  51. */
  52. public class NATField extends NATByRef implements ATField {
  53. private final ATSymbol name_;
  54. private final ATObject host_;
  55. /**
  56. * Constructs a new native field object linked to the field of a base-level
  57. * AmbientTalk object
  58. */
  59. public NATField(ATSymbol name, ATObject host) {
  60. name_ = name;
  61. host_ = host;
  62. }
  63. public ATSymbol base_name() {
  64. return name_;
  65. }
  66. public ATObject base_readField() throws InterpreterException {
  67. return host_.meta_invokeField(host_, name_);
  68. }
  69. public ATObject base_writeField(ATObject newValue) throws InterpreterException {
  70. return host_.impl_invoke(host_, name_.asAssignmentSymbol(), NATTable.of(newValue));
  71. }
  72. public NATText meta_print() throws InterpreterException {
  73. return NATText.atValue("<field:"+name_.meta_print().javaValue+">");
  74. }
  75. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  76. return this.impl_asCode(objectMap, false);
  77. }
  78. public NATText impl_asCode(TempFieldGenerator objectMap, boolean isIsolate) throws InterpreterException {
  79. if(objectMap.contains(this)) {
  80. return objectMap.getName(this);
  81. }
  82. String def = "";
  83. String nameAsCode = name_.meta_print().javaValue;
  84. if(nameAsCode != "super")
  85. def += "def ";
  86. //if(isIsolate) // why was this necessary?
  87. // def += "self.";
  88. return NATText.atValue(def + nameAsCode + " := " + base_readField().impl_asCode(objectMap).javaValue);
  89. }
  90. public boolean isNativeField() {
  91. return true;
  92. }
  93. public ATField asField() throws XTypeMismatch {
  94. return this;
  95. }
  96. /**
  97. * Fields can be re-initialized when installed in an object that is being cloned.
  98. * They expect the new owner of the field as the sole instance to their 'new' method
  99. */
  100. public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
  101. if (initargs.base_length() != NATNumber.ONE) {
  102. return super.meta_newInstance(initargs);
  103. } else {
  104. ATObject newhost = initargs.base_at(NATNumber.ONE);
  105. return new NATField(name_, (NATCallframe) newhost);
  106. }
  107. }
  108. public ATTable meta_typeTags() throws InterpreterException {
  109. return NATTable.of(NativeTypeTags._FIELD_);
  110. }
  111. public ATMethod base_accessor() throws InterpreterException {
  112. return accessorForField(this);
  113. }
  114. public ATMethod base_mutator() throws InterpreterException {
  115. return mutatorForField(this);
  116. }
  117. public static ATMethod accessorForField(final ATField f) throws InterpreterException {
  118. return new PrimitiveMethod(f.base_name(), NATTable.EMPTY, new PrimitiveMethod.PrimitiveBody() {
  119. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  120. return f.base_readField();
  121. }
  122. public NATText meta_print() throws InterpreterException {
  123. return NATText.atValue(f.base_readField().toString());
  124. }
  125. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  126. return f.base_readField().impl_asCode(objectMap);
  127. }
  128. }) {
  129. public NATText meta_print() throws InterpreterException {
  130. return NATText.atValue("<accessor method for:"+f.base_name()+">");
  131. }
  132. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  133. return NATText.atValue("def "
  134. + f.base_name().impl_asCode(objectMap).javaValue
  135. + "(){"+f.base_name().impl_asCode(objectMap).javaValue
  136. + "}");
  137. }
  138. };
  139. }
  140. public static ATMethod mutatorForField(final ATField f) throws InterpreterException {
  141. return new PrimitiveMethod(f.base_name().asAssignmentSymbol(), NATTable.of(AGSymbol.jAlloc("v")), new PrimitiveMethod.PrimitiveBody() {
  142. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  143. return f.base_writeField(ctx.base_lexicalScope().impl_callField(AGSymbol.jAlloc("v")));
  144. }
  145. public NATText meta_print() throws InterpreterException {
  146. return NATText.atValue(""+f.base_name()+" := v");
  147. }
  148. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  149. return NATText.atValue(""+f.base_name()+" := v");
  150. }
  151. }) {
  152. public NATText meta_print() throws InterpreterException {
  153. return NATText.atValue("<mutator method for:"+f.base_name()+">");
  154. }
  155. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  156. return NATText.atValue("def "
  157. + f.base_name().impl_asCode(objectMap).javaValue
  158. + ".:=(v){"
  159. + f.base_name().impl_asCode(objectMap).javaValue
  160. + ":=v}");
  161. }
  162. };
  163. }
  164. }