/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/NATCallframe.java

http://ambienttalk.googlecode.com/ · Java · 356 lines · 195 code · 43 blank · 118 comment · 43 complexity · 31455611c32f7be5d6f102f82808ce76 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATCallframe.java created on Jul 28, 2006 at 11:26:17 AM
  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.XDuplicateSlot;
  31. import edu.vub.at.exceptions.XIllegalOperation;
  32. import edu.vub.at.exceptions.XSelectorNotFound;
  33. import edu.vub.at.exceptions.XUndefinedSlot;
  34. import edu.vub.at.objects.ATBoolean;
  35. import edu.vub.at.objects.ATField;
  36. import edu.vub.at.objects.ATMethod;
  37. import edu.vub.at.objects.ATNil;
  38. import edu.vub.at.objects.ATObject;
  39. import edu.vub.at.objects.ATTable;
  40. import edu.vub.at.objects.grammar.ATSymbol;
  41. import java.util.Iterator;
  42. import java.util.LinkedList;
  43. import java.util.Vector;
  44. /**
  45. * NATCallframe is a native implementation of a callframe. A callframe differs from
  46. * an ordinary object in the following regards:
  47. * - it has no dynamic parent
  48. * - it treats method definition as the addition of a closure to its variables.
  49. * - it cannot be extended nor cloned
  50. *
  51. * Callframes can be regarded as 'field-only' objects. Fields are implemented as follows:
  52. * - native fields are implemented efficiently using a 'map': the map datastructure maps
  53. * selectors to indices into a state vector, such that field names can be shared efficiently
  54. * across clones.
  55. * - custom fields are collected in a linked list. Their lookup and assignment is slower,
  56. * and when an object is cloned, the custom field objects are re-instantiated.
  57. * The new clone is passed as the sole argument to 'new'.
  58. *
  59. * @author tvcutsem
  60. * @author smostinc
  61. */
  62. public class NATCallframe extends NATByRef implements ATObject {
  63. protected FieldMap variableMap_;
  64. protected final Vector stateVector_;
  65. /**
  66. * The lexical parent 'scope' of this call frame/object.
  67. * A lexical scope should never travel along with an object when it is serialized,
  68. * hence it is declared transient. Serializable isolate objects will have to reset
  69. * this field upon deserialization.
  70. */
  71. protected transient ATObject lexicalParent_;
  72. protected LinkedList customFields_;
  73. /**
  74. * Default constructor: creates a new call frame with a given scope pointer.
  75. */
  76. public NATCallframe(ATObject lexicalParent) {
  77. variableMap_ = new FieldMap();
  78. stateVector_ = new Vector();
  79. lexicalParent_ = lexicalParent;
  80. customFields_ = null;
  81. }
  82. /**
  83. * Used internally for cloning a callframe/object.
  84. */
  85. protected NATCallframe(FieldMap varMap, Vector stateVector, ATObject lexicalParent, LinkedList customFields) {
  86. variableMap_ = varMap;
  87. stateVector_ = stateVector;
  88. lexicalParent_ = lexicalParent;
  89. customFields_ = customFields;
  90. }
  91. /* ------------------------------------------
  92. * -- Slot accessing and mutating protocol --
  93. * ------------------------------------------ */
  94. /**
  95. * A field can be added to either a call frame or an object.
  96. * In both cases, it is checked whether the field does not already exist.
  97. * If it does not, a new field is created and its value set to the given initial value.
  98. * @throws InterpreterException
  99. */
  100. public ATNil meta_defineField(ATSymbol name, ATObject value) throws InterpreterException {
  101. if (this.hasLocalField(name)) {
  102. // field already exists...
  103. throw new XDuplicateSlot(name);
  104. } else {
  105. boolean fieldAdded = variableMap_.put(name);
  106. if (!fieldAdded) {
  107. throw new RuntimeException("Assertion failed: field not added to map while not duplicate");
  108. }
  109. // field now defined, add its value to the state vector
  110. stateVector_.add(value);
  111. }
  112. return OBJNil._INSTANCE_;
  113. }
  114. /* ------------------------------------
  115. * -- Extension and cloning protocol --
  116. * ------------------------------------ */
  117. public ATObject meta_clone() throws InterpreterException {
  118. throw new XIllegalOperation("Cannot clone a call frame, clone its owning object instead.");
  119. }
  120. public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
  121. throw new XIllegalOperation("Cannot create a new instance of a call frame, new its owning object instead.");
  122. }
  123. /* ---------------------------------
  124. * -- Structural Access Protocol --
  125. * --------------------------------- */
  126. public ATNil meta_addField(ATField field) throws InterpreterException {
  127. // when adding a native field, revert to the more optimized implementation using the map
  128. if (field.isNativeField()) {
  129. return this.meta_defineField(field.base_name(), field.base_readField());
  130. }
  131. ATSymbol name = field.base_name();
  132. if (this.hasLocalField(name)) {
  133. // field already exists...
  134. throw new XDuplicateSlot(name);
  135. } else {
  136. // add a clone of the field initialized with its new host
  137. field = field.base_new(new ATObject[] { this }).asField();
  138. // add the field to the list of custom fields, which is created lazily
  139. if (customFields_ == null) {
  140. customFields_ = new LinkedList();
  141. }
  142. // append the custom field object
  143. customFields_.add(field);
  144. }
  145. return OBJNil._INSTANCE_;
  146. }
  147. public ATNil meta_addMethod(ATMethod method) throws InterpreterException {
  148. throw new XIllegalOperation("Cannot add method "+
  149. method.base_name().base_text().asNativeText().javaValue +
  150. " to a call frame. Add it as a closure field instead.");
  151. }
  152. public ATField meta_grabField(ATSymbol selector) throws InterpreterException {
  153. if (this.hasLocalNativeField(selector)) {
  154. return new NATField(selector, this);
  155. } else {
  156. ATField fld = this.getLocalCustomField(selector);
  157. if (fld != null) {
  158. return fld;
  159. } else {
  160. throw new XUndefinedSlot("field grabbed", selector.toString());
  161. }
  162. }
  163. }
  164. public ATMethod meta_grabMethod(ATSymbol selector) throws InterpreterException {
  165. throw new XSelectorNotFound(selector, this);
  166. }
  167. public ATTable meta_listFields() throws InterpreterException {
  168. ATObject[] nativeFields = new ATObject[stateVector_.size()];
  169. ATSymbol[] fieldNames = variableMap_.listFields();
  170. // native fields first
  171. for (int i = 0; i < fieldNames.length; i++) {
  172. nativeFields[i] = new NATField(fieldNames[i], this);
  173. }
  174. if (customFields_ == null) {
  175. // no custom fields
  176. return NATTable.atValue(nativeFields);
  177. } else {
  178. ATObject[] customFields = (ATObject[]) customFields_.toArray(new ATObject[customFields_.size()]);
  179. return NATTable.atValue(NATTable.collate(nativeFields, customFields));
  180. }
  181. }
  182. public ATTable meta_listMethods() throws InterpreterException {
  183. return NATTable.EMPTY;
  184. }
  185. public NATText meta_print() throws InterpreterException {
  186. return NATText.atValue("<callframe>");
  187. }
  188. /* ---------------------
  189. * -- Mirror Fields --
  190. * --------------------- */
  191. /**
  192. * Auxiliary method to dynamically select the 'super' field from this object.
  193. * Note that this method is part of the base-level interface to an object.
  194. *
  195. * Also note that this method performs the behaviour equivalent to evaluating
  196. * 'super' and not 'self.super', which could lead to infinite loops.
  197. */
  198. public ATObject base_super() throws InterpreterException {
  199. return this.impl_call(NATObject._SUPER_NAME_, NATTable.EMPTY);
  200. };
  201. public ATObject impl_lexicalParent() throws InterpreterException {
  202. return lexicalParent_;
  203. }
  204. /* --------------------------
  205. * -- Conversion Protocol --
  206. * -------------------------- */
  207. public boolean isCallFrame() {
  208. return true;
  209. }
  210. public ATBoolean meta_isCloneOf(ATObject original) throws InterpreterException {
  211. if( (original instanceof NATCallframe) &
  212. ! (original instanceof NATObject)) {
  213. FieldMap originalVariables = ((NATCallframe)original).variableMap_;
  214. return NATBoolean.atValue(
  215. variableMap_.isDerivedFrom(originalVariables));
  216. } else {
  217. return NATBoolean._FALSE_;
  218. }
  219. }
  220. public ATBoolean meta_isRelatedTo(ATObject object) throws InterpreterException {
  221. return super.meta_isRelatedTo(object);
  222. }
  223. /* -----------------------------
  224. * -- Object Passing protocol --
  225. * ----------------------------- */
  226. // protected methods, only to be used by NATCallframe and NATObject
  227. protected boolean hasLocalField(ATSymbol selector) throws InterpreterException {
  228. return hasLocalNativeField(selector) || hasLocalCustomField(selector);
  229. }
  230. protected boolean hasLocalNativeField(ATSymbol selector) {
  231. return variableMap_.get(selector) != -1;
  232. }
  233. protected boolean hasLocalCustomField(ATSymbol selector) throws InterpreterException {
  234. if (customFields_ == null) {
  235. return false;
  236. } else {
  237. Iterator it = customFields_.iterator();
  238. while (it.hasNext()) {
  239. ATField field = (ATField) it.next();
  240. if (field.base_name().equals(selector)) {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. }
  247. /**
  248. * Reads out the value of either a native or a custom field.
  249. * @throws XSelectorNotFound if no native or custom field with the given name exists locally.
  250. */
  251. protected ATObject getLocalField(ATSymbol selector) throws InterpreterException {
  252. int index = variableMap_.get(selector);
  253. if(index != -1) {
  254. return (ATObject) (stateVector_.get(index));
  255. } else {
  256. ATField fld = getLocalCustomField(selector);
  257. if (fld != null) {
  258. return fld.base_readField();
  259. } else {
  260. throw new XSelectorNotFound(selector, this);
  261. }
  262. }
  263. }
  264. /**
  265. * @return a custom field matching the given selector or null if such a field does not exist
  266. */
  267. protected ATField getLocalCustomField(ATSymbol selector) throws InterpreterException {
  268. if (customFields_ == null) {
  269. return null;
  270. } else {
  271. Iterator it = customFields_.iterator();
  272. while (it.hasNext()) {
  273. ATField field = (ATField) it.next();
  274. if (field.base_name().equals(selector)) {
  275. return field;
  276. }
  277. }
  278. return null;
  279. }
  280. }
  281. /**
  282. * Set a given field if it exists.
  283. */
  284. protected void setLocalField(ATSymbol selector, ATObject value) throws InterpreterException {
  285. int index = variableMap_.get(selector);
  286. if(index != -1) {
  287. // field exists, modify the state vector
  288. stateVector_.set(index, value);
  289. // ok
  290. } else {
  291. ATField fld = getLocalCustomField(selector);
  292. if (fld != null) {
  293. fld.base_writeField(value);
  294. // ok
  295. } else {
  296. // fail
  297. throw new XSelectorNotFound(selector, this);
  298. }
  299. }
  300. }
  301. /**
  302. * A call frame has no methods.
  303. */
  304. protected boolean hasLocalMethod(ATSymbol atSelector) throws InterpreterException {
  305. return false;
  306. }
  307. /**
  308. * A call frame has no methods.
  309. */
  310. protected ATMethod getLocalMethod(ATSymbol selector) throws InterpreterException {
  311. throw new XSelectorNotFound(selector, this);
  312. }
  313. }