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

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