/interpreter/tags/at_build150307/src/edu/vub/at/objects/natives/NATNamespace.java

http://ambienttalk.googlecode.com/ · Java · 209 lines · 101 code · 21 blank · 87 comment · 6 complexity · d6c80d4ca80b3125204d891f67762fce MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATNamespace.java created on 6-sep-2006 at 14:33:41
  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.eval.Evaluator;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XDuplicateSlot;
  32. import edu.vub.at.exceptions.XIOProblem;
  33. import edu.vub.at.exceptions.XTypeMismatch;
  34. import edu.vub.at.objects.ATAbstractGrammar;
  35. import edu.vub.at.objects.ATObject;
  36. import edu.vub.at.objects.ATStripe;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.mirrors.Reflection;
  39. import edu.vub.at.parser.NATParser;
  40. import java.io.File;
  41. import java.io.IOException;
  42. import java.util.LinkedList;
  43. import java.util.Vector;
  44. /**
  45. * Instances of the class NATNamespace represent namespace objects.
  46. *
  47. * Namespace objects act as regular AmbientTalk objects with the following differences and conventions:
  48. * - Behaviourally, a namespace object is mirrored by a mirror whose doesNotUnderstand
  49. * method reacts differently from the standard semantics of raising a 'selector not found' exception.
  50. * - Structurally, a namespace has the lexical root as its lexical parent and the dynamic root as its dynamic parent.
  51. * Furthermore, a namespace object encapsulates an absolute file system path and a relative 'path name'.
  52. * The name should correspond to a portion of the tail of the absolute path.
  53. * These variables are not visible to AmbientTalk code.
  54. *
  55. * When a slot is looked up in a namespace NS for a path P (via meta_select) and not found, the namespace object
  56. * queries the local file system to see whether the selector corresponds to a directory or file in the
  57. * directory P. Either the selector:
  58. * - corresponds to a directory, in which case the missing slot is bound to a new namespace object corresponding to the path P/selector
  59. * - corresponds to a file named selector.at, in which case:
  60. * 1) the slot is temporarily bound to nil
  61. * (this is to prevent loops when the code to be evaluated would refer to itself;
  62. * it also means there can be no circular dependencies, because referring to a slot still under construction yields nil)
  63. * 2) a new object FS (the 'file scope') is created.
  64. * This object acts as the local scope for the file and has access to its 'enclosing' namespace via the '~' slot.
  65. * Hence, it can refer to other files in the 'current directory' using ~.filename
  66. * 3) the code in the file is loaded and evaluated in the context (current=FS, self=FS, super=FS.parent=dynroot)
  67. * 4) the result of the evaluated code is bound to the missing slot.
  68. * The next time the slot is queried for in the namespace, the value is immediately returned. This prevents
  69. * files from being loaded twice.
  70. * - does not correspond to any file or directory, resulting in a selector not found exception as usual.
  71. *
  72. * @author tvcutsem
  73. * @author smostinc
  74. */
  75. public final class NATNamespace extends NATObject {
  76. private static final String _AT_EXT_ = ".at";
  77. private final File path_;
  78. private final String name_;
  79. /**
  80. * A namespace object encapsulates a given absolute path and represents the given relative path.
  81. *
  82. * @param name the name of this namespace (corresponding to a certain depth to the tail of the absolute path)
  83. * @param path an absolute path referring to a local file system directory.
  84. */
  85. public NATNamespace(String name, File path) {
  86. super();
  87. name_ = name;
  88. path_ = path;
  89. }
  90. /**
  91. * Private constructor used only for cloning
  92. */
  93. private NATNamespace(FieldMap map,
  94. Vector state,
  95. LinkedList customFields,
  96. MethodDictionary methodDict,
  97. ATObject dynamicParent,
  98. ATObject lexicalParent,
  99. byte flags,
  100. ATStripe[] stripes,
  101. File path,
  102. String name) throws InterpreterException {
  103. super(map, state, customFields, methodDict, dynamicParent, lexicalParent, flags, stripes);
  104. path_ = path;
  105. name_ = name;
  106. }
  107. /**
  108. * For a namespace object, doesNotUnderstand triggers the querying of the local file system
  109. * to load files corresponding to the missing selector.
  110. */
  111. public ATObject meta_doesNotUnderstand(ATSymbol selector) throws InterpreterException {
  112. // first, convert the AmbientTalk name to a Java selector. Java selectors are always valid filenames because
  113. // they do not contain special operator characters
  114. String javaSelector = Reflection.upSelector(selector);
  115. // first, try to see if the file exists and corresponds to a directory
  116. File dir = new File(path_, javaSelector);
  117. if (dir.exists() && dir.isDirectory()) {
  118. // create a new namespace object for this directory
  119. NATNamespace childNS = new NATNamespace(name_ + "/" + javaSelector, dir);
  120. // bind the new child namespace to the selector
  121. this.meta_defineField(selector, childNS);
  122. return childNS;
  123. } else {
  124. // try to see if a file with extension .at exists corresponding to the selector
  125. File src = new File(path_, javaSelector + _AT_EXT_);
  126. if (src.exists() && src.isFile()) {
  127. // bind the missing slot to nil to prevent calling this dNU recursively when evaluating the code in the file
  128. this.meta_defineField(selector, NATNil._INSTANCE_);
  129. // create a new file scope object for this file
  130. NATObject fileScope = createFileScopeFor(this);
  131. try {
  132. // load the code from the file
  133. String code = Evaluator.loadContentOfFile(src);
  134. // construct the proper evaluation context for the code
  135. NATContext ctx = new NATContext(fileScope, fileScope);
  136. // parse and evaluate the code in the proper context and bind its result to the missing slot
  137. ATAbstractGrammar source = NATParser.parse(src.getName(), code);
  138. ATObject result = source.meta_eval(ctx);
  139. this.meta_assignField(this, selector, result);
  140. return result;
  141. } catch (IOException e) {
  142. throw new XIOProblem(e);
  143. }
  144. } else { // neither a matching directory nor a matching file.at were found
  145. // perform the default dNU behaviour, which means raising a 'selector not found' exception
  146. return super.meta_doesNotUnderstand(selector);
  147. }
  148. }
  149. }
  150. public NATText meta_print() throws InterpreterException {
  151. return NATText.atValue("<ns:"+name_+">");
  152. }
  153. public static NATObject createFileScopeFor(NATNamespace ns) {
  154. NATObject fileScope = new NATObject();
  155. // a fileScope object is empty, save for a reference to its creating namespace
  156. try {
  157. fileScope.meta_defineField(Evaluator._CURNS_SYM_, ns);
  158. } catch (XDuplicateSlot e) {
  159. // impossible: the object is empty
  160. e.printStackTrace();
  161. } catch (XTypeMismatch e) {
  162. // impossible: the given selector is native
  163. e.printStackTrace();
  164. } catch (InterpreterException e) {
  165. // impossible : call cannot be intercepted : namespaces are not mirages
  166. e.printStackTrace();
  167. }
  168. return fileScope;
  169. }
  170. protected NATObject createClone(FieldMap map,
  171. Vector state,
  172. LinkedList customFields,
  173. MethodDictionary methodDict,
  174. ATObject dynamicParent,
  175. ATObject lexicalParent,
  176. byte flags, ATStripe[] stripes) throws InterpreterException {
  177. return new NATNamespace(map,
  178. state,
  179. customFields,
  180. methodDict,
  181. dynamicParent,
  182. lexicalParent,
  183. flags,
  184. stripes,
  185. path_,
  186. name_);
  187. }
  188. }