/interpreter/tags/at2-build060407/src/edu/vub/at/objects/mirrors/PrimitiveMethod.java

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