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