/interpreter/tags/at2dist110511/src/edu/vub/at/objects/mirrors/PrimitiveMethod.java

http://ambienttalk.googlecode.com/ · Java · 124 lines · 46 code · 15 blank · 63 comment · 0 complexity · 3df8c1cc417d5cd5ec25bd12ea651340 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * PrimitiveMethod.java created on 2-feb-2007 at 21:44:00
  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.mirrors;
  29. import edu.vub.at.eval.Evaluator;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XTypeMismatch;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.ATTable;
  35. import edu.vub.at.objects.grammar.ATBegin;
  36. import edu.vub.at.objects.grammar.ATSymbol;
  37. import edu.vub.at.objects.natives.NATMethod;
  38. import edu.vub.at.objects.natives.NATNil;
  39. import edu.vub.at.objects.natives.NATTable;
  40. import edu.vub.at.objects.natives.NATText;
  41. import edu.vub.at.objects.natives.grammar.NATAbstractGrammar;
  42. /**
  43. * A primitive method is the equivalent of a NativeClosure but for methods rather
  44. * than closures. The advantage of PrimtiveMethods is that their base_apply method
  45. * gives access to both arguments as well as to the runtime context that contains
  46. * the lexical environment in which they were defined.
  47. *
  48. * Example primitive methods are '==', '!=', 'new' and 'init' implemented by {@link NATNil}.
  49. *
  50. * Primitive methods should implement this method's base_apply method or invoke the
  51. * constructor taking a PrimitiveBody parameter and implement that class's meta_eval
  52. * method, if they want to make use of AT/2's parameter binding semantics.
  53. *
  54. * Primitive methods installed in native objects that can be extended should ensure
  55. * that they use the dynamic receiver stored in the application context (the AmbientTalk
  56. * 'self') rather than the Java 'this' variable to perform self-sends. The former will
  57. * properly invoke/select the overridden methods/fields of a child AT object, the latter
  58. * will simply refer to the native instance, disregarding any modifications by child objects.
  59. *
  60. * @author tvcutsem
  61. */
  62. public class PrimitiveMethod extends NATMethod {
  63. /**
  64. * Instances of this helper class represent primitive method bodies. To the
  65. * AT programmer, they look like empty methods (i.e. { nil }). The native Java
  66. * implementation is specified by overriding the meta_eval method.
  67. */
  68. public static abstract class PrimitiveBody extends NATAbstractGrammar implements ATBegin {
  69. /**
  70. * A primitive can override this method and has access to:
  71. * - ctx.lexicalScope = the call frame for this method invocation
  72. * - ctx.lexicalScope.lexicalParent = the object in which the method was found
  73. * - ctx.dynamicReceiver = the object on which the method was invoked
  74. */
  75. public abstract ATObject meta_eval(ATContext ctx) throws InterpreterException;
  76. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  77. return this;
  78. }
  79. public ATTable base_statements() { return NATTable.of(Evaluator.getNil()); }
  80. public NATText meta_print() throws InterpreterException {
  81. return NATText.atValue("<primitive body>");
  82. }
  83. public ATBegin asBegin() throws XTypeMismatch {
  84. return this;
  85. }
  86. }
  87. public PrimitiveMethod(ATSymbol name, ATTable formals, PrimitiveBody body, ATTable annotations) {
  88. super(name, formals, body, annotations);
  89. }
  90. public PrimitiveMethod(ATSymbol name, ATTable formals, PrimitiveBody body) {
  91. super(name, formals, body, NATTable.EMPTY);
  92. }
  93. /**
  94. * Constructor for the creation of primitive methods where the body is left empty.
  95. * The idea is that the creator of such primitive methods overrides the base_apply
  96. * method because the primitive method has no need for a call frame or parameter binding.
  97. */
  98. public PrimitiveMethod(ATSymbol name, ATTable formals) {
  99. super(name, formals, new PrimitiveBody() {
  100. private static final long serialVersionUID = -2177230227968386983L;
  101. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  102. return Evaluator.getNil();
  103. }
  104. }, NATTable.EMPTY);
  105. }
  106. public NATText meta_print() throws InterpreterException {
  107. return NATText.atValue("<primitive method:"+base_name()+">");
  108. }
  109. }