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

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