/interpreter/tags/at2-build190607/src/edu/vub/at/objects/natives/NATClosureMethod.java

http://ambienttalk.googlecode.com/ · Java · 126 lines · 46 code · 16 blank · 64 comment · 0 complexity · 4676b45549ae874ed9f6526ed4c217d3 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATClosureMethod.java created on 16-nov-2006 at 9:12:46
  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.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XTypeMismatch;
  31. import edu.vub.at.objects.ATContext;
  32. import edu.vub.at.objects.ATMethod;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.ATTable;
  35. import edu.vub.at.objects.coercion.NativeTypeTags;
  36. import edu.vub.at.objects.grammar.ATBegin;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. /**
  39. * A 'closure method' is literally a function that sits in between of a full closure and a method.
  40. * It is a method that captures only its lexical scope of definition, but not the value of 'self'
  41. * and 'super' which are active at that time. When invoked, the method is evaluated in its lexical
  42. * scope, but with the values of 'self' and 'super' equal to those given to it at method invocation time.
  43. *
  44. * Closure methods are used to implement 'externally added' methods to objects. In such cases,
  45. * the external methods can only access their own surrounding lexical scope (and not that of the actual
  46. * object to which they are added), but their values for 'self' and 'super' will act as if the method
  47. * was actually defined within the object itself.
  48. *
  49. * See the description of the ATDefExternalMethod interface for more information.
  50. *
  51. * @author tvcutsem
  52. */
  53. public final class NATClosureMethod extends NATByRef implements ATMethod {
  54. private final ATObject lexicalScope_;
  55. private final ATMethod method_;
  56. public NATClosureMethod(ATObject scope, ATMethod method) throws InterpreterException {
  57. lexicalScope_ = scope;
  58. method_ = method;
  59. }
  60. /**
  61. * A closure method application acts exactly like a regular direct method application, except that
  62. * the given lexical scope is disregarded and replaced by the lexical scope encapsulated by the
  63. * closure method. The bindings for 'self' and 'super' remain intact.
  64. *
  65. * Closure methods are a result of external method definitions.
  66. *
  67. * @param arguments the evaluated actual arguments
  68. * @param ctx the context whose 'self' binding will be used during the execution of the method.
  69. * The lexical scope, however, will be replaced by the closure method's own lexical scope. A call frame will
  70. * be inserted into this lexical scope before executing the method body.
  71. * To ensure that 'super' points to the parent of the actual 'self' and not the parent of the object
  72. * performing the external method definition, a 'super' field is added to the call frame shadowing
  73. * the defining object's 'super' field.
  74. * @return the value of evaluating the function body
  75. */
  76. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  77. // should be: method_.base_apply(arguments, ctx.base_withLexicalEnvironment(lexicalScope_),
  78. // but this version is more efficient, as we immediately create the right context, rather than
  79. // creating a temporary context which will in turn be modified by method_.base_apply
  80. // hostObject = the object to which the external method was added (actually the object in which
  81. // the external method was now found)
  82. ATObject hostObject = ctx.base_getLexicalScope();
  83. ATObject hostParent = hostObject.base_getSuper();
  84. NATCallframe externalFrame = new NATCallframe(lexicalScope_);
  85. // super = the parent of the object to which this method was added
  86. externalFrame.meta_defineField(NATObject._SUPER_NAME_, hostParent);
  87. return method_.base_applyInScope(arguments, ctx.base_withLexicalEnvironment(externalFrame));
  88. }
  89. public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
  90. return method_.base_applyInScope(arguments, ctx);
  91. }
  92. public ATBegin base_getBodyExpression() throws InterpreterException {
  93. return method_.base_getBodyExpression();
  94. }
  95. public ATSymbol base_getName() throws InterpreterException {
  96. return method_.base_getName();
  97. }
  98. public ATTable base_getParameters() throws InterpreterException {
  99. return method_.base_getParameters();
  100. }
  101. public NATText meta_print() throws InterpreterException {
  102. return NATText.atValue("<closure:"+method_.base_getName()+">");
  103. }
  104. public ATMethod asMethod() throws XTypeMismatch {
  105. return this;
  106. }
  107. public ATTable meta_getTypeTags() throws InterpreterException {
  108. return NATTable.of(NativeTypeTags._METHOD_);
  109. }
  110. }