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

http://ambienttalk.googlecode.com/ · Java · 261 lines · 127 code · 34 blank · 100 comment · 6 complexity · 5943e745c000247bd726db991faec287 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATNil.java created on 15 jul 2007 at 18:33:28
  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.XArityMismatch;
  33. import edu.vub.at.objects.ATBoolean;
  34. import edu.vub.at.objects.ATClosure;
  35. import edu.vub.at.objects.ATContext;
  36. import edu.vub.at.objects.ATNil;
  37. import edu.vub.at.objects.ATObject;
  38. import edu.vub.at.objects.ATTable;
  39. import edu.vub.at.objects.grammar.ATAssignmentSymbol;
  40. import edu.vub.at.objects.grammar.ATSymbol;
  41. import edu.vub.at.objects.mirrors.NATIntrospectiveMirror;
  42. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  43. import edu.vub.at.objects.natives.grammar.AGSplice;
  44. import edu.vub.at.objects.natives.grammar.AGSymbol;
  45. import edu.vub.util.TempFieldGenerator;
  46. /**
  47. * This class encapsulates the behaviour of the native
  48. * <tt>nil</tt> AmbientTalk object.
  49. *
  50. * def nil := object: {
  51. * super := dynamicSentinel;
  52. * def ==(other); // native implementation
  53. * def !=(other) { self.==(other).not }
  54. * def new(@args) { def c := (reflect: self).clone(); c.init(@args); c }
  55. * def init(@args) { }
  56. * }
  57. *
  58. * @author tvcutsem
  59. */
  60. public class NATNil extends NATObject implements ATNil {
  61. /**
  62. * Nil has a special parent object which ends the recursion
  63. * along the dynamic delegation chain. These methods cannot be implemented
  64. * directly in this class because this class still implements useful
  65. * <tt>base_</tt> Java methods which have to be invoked by means of the
  66. * implementations defined in {@link NativeATObject}.
  67. *
  68. * This object is shared by all actors, but it is constant so introduces
  69. * no concurrency issues.
  70. */
  71. private static final NativeATObject dynamicSentinel_ = new NATByCopy() {
  72. private static final long serialVersionUID = -1307795172754062220L;
  73. // METHODS THAT END THE DYNAMIC DELEGATION CHAIN
  74. public ATBoolean meta_respondsTo(ATSymbol selector) throws InterpreterException {
  75. // no more delegation
  76. return NATBoolean._FALSE_;
  77. }
  78. /**
  79. * When performing <tt>o.m()</tt> and <tt>m</tt> is not found, invoke
  80. * <tt>doesNotUnderStand</tt> and apply the resulting closure to the given arguments.
  81. */
  82. public ATObject impl_invokeAccessor(ATObject receiver, ATSymbol selector, ATTable arguments) throws InterpreterException {
  83. return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
  84. }
  85. public ATObject impl_invokeMutator(ATObject receiver, ATAssignmentSymbol selector, ATTable arguments) throws InterpreterException {
  86. return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
  87. }
  88. /**
  89. * When performing <tt>o.x</tt> and <tt>x</tt> is not found, invoke
  90. * <tt>doesNotUnderStand</tt> and apply the corresponding closure with zero arguments.
  91. */
  92. public ATObject meta_invokeField(ATObject receiver, ATSymbol selector) throws InterpreterException {
  93. return receiver.meta_doesNotUnderstand(selector).base_apply(NATTable.EMPTY);
  94. }
  95. public ATClosure impl_selectAccessor(ATObject receiver, final ATSymbol selector) throws InterpreterException {
  96. return receiver.meta_doesNotUnderstand(selector);
  97. }
  98. public ATClosure impl_selectMutator(ATObject receiver, final ATAssignmentSymbol selector) throws InterpreterException {
  99. return receiver.meta_doesNotUnderstand(selector);
  100. }
  101. public NATText meta_print() throws InterpreterException {
  102. return NATText.atValue("dynamicsentinel");
  103. }
  104. };
  105. // The names of nil's primitive methods
  106. public static final AGSymbol _EQL_NAME_ = AGSymbol.jAlloc("==");
  107. public static final AGSymbol _NEW_NAME_ = AGSymbol.jAlloc("new");
  108. public static final AGSymbol _INI_NAME_ = AGSymbol.jAlloc("init");
  109. public static final ATSymbol _NEQ_NAME_ = AGSymbol.jAlloc("!=");
  110. // The primitive methods themselves
  111. /** def ==(comparand) { comparand.impl_identityEquals(self) } */
  112. private static final PrimitiveMethod _PRIM_EQL_ = new PrimitiveMethod(
  113. _EQL_NAME_, NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("comparand")})) {
  114. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  115. if (!arguments.base_length().equals(NATNumber.ONE)) {
  116. throw new XArityMismatch("==", 1, arguments.base_length().asNativeNumber().javaValue);
  117. }
  118. ATObject comparand = arguments.base_at(NATNumber.ONE);
  119. // make other object perform the actual pointer equality
  120. // if comparand is a proxy, it can delegate this request to its principal
  121. return comparand.impl_identityEquals(ctx.base_receiver());
  122. }
  123. };
  124. /** def new(@initargs) { (reflect: self).newInstance(initargs) } */
  125. private static final PrimitiveMethod _PRIM_NEW_ = new PrimitiveMethod(
  126. _NEW_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
  127. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  128. return ctx.base_receiver().meta_newInstance(arguments);
  129. // return ctx.base_receiver().base_new(arguments.asNativeTable().elements_);
  130. }
  131. };
  132. /** def init(@initargs) { self } */
  133. private static final PrimitiveMethod _PRIM_INI_ = new PrimitiveMethod(
  134. _INI_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
  135. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  136. return ctx.base_receiver();
  137. }
  138. };
  139. /** def !=(other) { self.==(other).not } */
  140. private static final PrimitiveMethod _PRIM_NEQ_ = new PrimitiveMethod(
  141. _NEQ_NAME_, NATTable.of(AGSymbol.jAlloc("other"))) {
  142. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  143. int arity = arguments.base_length().asNativeNumber().javaValue;
  144. if (arity != 1) {
  145. throw new XArityMismatch("!=", 1, arity);
  146. }
  147. ATObject other = arguments.base_at(NATNumber.ONE);
  148. // return ctx.receiver == other
  149. return ctx.base_receiver().meta_invoke(
  150. ctx.base_receiver(), new NATMethodInvocation(_EQL_NAME_, NATTable.of(other), NATTable.EMPTY)).asBoolean().base_not();
  151. }
  152. };
  153. /**
  154. * Construct and initialize a new instance of <tt>nil</tt>. Only one instance
  155. * of <tt>nil</tt> should be created per actor.
  156. */
  157. public NATNil() {
  158. // super(dynamicParent, lexicalParent, parentType)
  159. // super := dynamicSentinel
  160. // the lexical root of nil is set to dynamicSentinel_ because,
  161. // if we would make it refer to Evaluator.getGlobalLexicalRoot(),
  162. // we get an infinite recursion: nil requires root, root requires nil, ...
  163. super(dynamicSentinel_, dynamicSentinel_, NATObject._SHARES_A_);
  164. // add ==, new, init and != to the method dictionary directly
  165. // cannot use meta_addMethod because this method returns nil,
  166. // thus constructing nil requires already having a constructed nil...
  167. // super.meta_addMethod(_PRIM_NEQ_);
  168. methodDictionary_.put(_EQL_NAME_, _PRIM_EQL_);
  169. methodDictionary_.put(_NEW_NAME_, _PRIM_NEW_);
  170. methodDictionary_.put(_INI_NAME_, _PRIM_INI_);
  171. methodDictionary_.put(_NEQ_NAME_, _PRIM_NEQ_);
  172. }
  173. /**
  174. * The identity operator. In AmbientTalk, equality of objects
  175. * is by default pointer-equality (i.e. objects are equal only
  176. * if they are identical).
  177. *
  178. * @return by default, true if the parameter object and this object are identical,
  179. * false otherwise.
  180. */
  181. public ATBoolean base__opeql__opeql_(ATObject other) throws InterpreterException {
  182. return this.meta_invoke(this, new NATMethodInvocation(_EQL_NAME_, NATTable.of(other), NATTable.EMPTY)).asBoolean();
  183. }
  184. public ATObject base_init(ATObject[] initargs) throws InterpreterException {
  185. return this.meta_invoke(this, new NATMethodInvocation(_INI_NAME_, NATTable.atValue(initargs), NATTable.EMPTY));
  186. }
  187. public ATObject base_new(ATObject[] initargs) throws InterpreterException {
  188. return this.meta_invoke(this, new NATMethodInvocation(_NEW_NAME_, NATTable.atValue(initargs), NATTable.EMPTY));
  189. }
  190. public ATBoolean base__opnot__opeql_(ATObject other) throws InterpreterException {
  191. // immediately hard-wired implementation, because we know 'self' is really
  192. // bound to the Java 'this'.
  193. // return this.meta_invoke(this, _NEQ_NAME_, NATTable.of(other));
  194. return this.base__opeql__opeql_(other).base_not();
  195. }
  196. public ATObject base_super() {
  197. return dynamicSentinel_;
  198. }
  199. public ATObject meta_pass() throws InterpreterException {
  200. return this;
  201. }
  202. /**
  203. * After deserialization, ensure that nil becomes rebound to the nil
  204. * object of the new actor.
  205. */
  206. public ATObject meta_resolve() throws InterpreterException {
  207. return Evaluator.getNil();
  208. }
  209. /** nil is a singleton */
  210. public ATObject meta_clone() throws InterpreterException {
  211. return this;
  212. }
  213. public NATText meta_print() throws InterpreterException {
  214. return NATText.atValue("nil");
  215. }
  216. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  217. if (objectMap.contains(this)) {
  218. return objectMap.getName(this);
  219. }
  220. NATText name = objectMap.put(this, NATText.atValue("nil"));
  221. if (objectMap.inQuote()) {
  222. return NATText.atValue("#" + name.javaValue);
  223. } else {
  224. return name;
  225. }
  226. }
  227. }