/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/OBJLexicalRoot.java

http://ambienttalk.googlecode.com/ · Java · 2542 lines · 1226 code · 123 blank · 1193 comment · 158 complexity · 9bfdd6fde26521118313086642bb7591 MD5 · raw file

Large files are truncated click here to view the full file

  1. /**
  2. * AmbientTalk/2 Project
  3. * OBJLexicalRoot.java created on 8-aug-2006 at 16:51:10
  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.actors.ATActorMirror;
  30. import edu.vub.at.actors.ATFarReference;
  31. import edu.vub.at.actors.natives.ELActor;
  32. import edu.vub.at.actors.natives.ELVirtualMachine;
  33. import edu.vub.at.actors.natives.NATFarReference;
  34. import edu.vub.at.actors.natives.Packet;
  35. import edu.vub.at.actors.net.OBJNetwork;
  36. import edu.vub.at.eval.Evaluator;
  37. import edu.vub.at.exceptions.InterpreterException;
  38. import edu.vub.at.exceptions.XIllegalOperation;
  39. import edu.vub.at.exceptions.XUnassignableField;
  40. import edu.vub.at.exceptions.XUndefinedSlot;
  41. import edu.vub.at.objects.ATAbstractGrammar;
  42. import edu.vub.at.objects.ATBoolean;
  43. import edu.vub.at.objects.ATClosure;
  44. import edu.vub.at.objects.ATContext;
  45. import edu.vub.at.objects.ATHandler;
  46. import edu.vub.at.objects.ATMethod;
  47. import edu.vub.at.objects.ATNil;
  48. import edu.vub.at.objects.ATNumber;
  49. import edu.vub.at.objects.ATNumeric;
  50. import edu.vub.at.objects.ATObject;
  51. import edu.vub.at.objects.ATTable;
  52. import edu.vub.at.objects.ATText;
  53. import edu.vub.at.objects.ATTypeTag;
  54. import edu.vub.at.objects.coercion.NativeTypeTags;
  55. import edu.vub.at.objects.grammar.ATAssignmentSymbol;
  56. import edu.vub.at.objects.grammar.ATSymbol;
  57. import edu.vub.at.objects.mirrors.DirectNativeMethod;
  58. import edu.vub.at.objects.mirrors.JavaInterfaceAdaptor;
  59. import edu.vub.at.objects.mirrors.NATMirage;
  60. import edu.vub.at.objects.mirrors.NATMirrorRoot;
  61. import edu.vub.at.objects.mirrors.NativeClosure;
  62. import edu.vub.at.parser.NATParser;
  63. import edu.vub.at.util.logging.Logging;
  64. import java.lang.reflect.Method;
  65. import java.util.HashMap;
  66. import java.util.Iterator;
  67. import java.util.LinkedList;
  68. import java.util.List;
  69. import java.util.Set;
  70. import java.util.Map.Entry;
  71. import java.util.concurrent.atomic.AtomicInteger;
  72. /**
  73. * The singleton instance of this class represents the lexical root of an actor.
  74. * Since this lexical root is constant (it cannot be modified) and contains no mutable fields,
  75. * it is possible to share a singleton instance of this class among all actors.
  76. * <p>
  77. * The lexical root is an object containing globally visible AmbientTalk native methods.
  78. * Such methods include control structures such as <tt>if:then:else:</tt>
  79. * but also object creation methods like <tt>object:</tt> and reflective constructs
  80. * like <tt>reflect:</tt>.
  81. *
  82. * Furthermore, the lexical root is also the root of the lexical parent hierarchy for objects.
  83. * This means that this object's mirror is responsible for ending recursive meta-level methods
  84. * such as <tt>lookup</tt> and <tt>assignField</tt>.
  85. * <p>
  86. * Like any class whose instances represent native AmbientTalk objects, this class is a subclass
  87. * of {@link NativeATObject}. This means that this class can use the typical protocol of native objects
  88. * to implement base-level AmbientTalk methods as Java methods whose name is prefixed with
  89. * <tt>base_</tt>.
  90. * <p>
  91. * Note that OBJLexicalRoot is a <i>sentinel</i> class. The actual object bound to the
  92. * lexical root of an actor (accessible via the field <tt>root</tt> will be a normal
  93. * AmbientTalk object whose lexical parent is this object.
  94. * The real, empty, root object is local to each actor and is mutable. The definitions
  95. * from the <tt>init.at</tt> file are added to that object.
  96. *
  97. * @author smostinc
  98. * @author tvcutsem
  99. */
  100. public final class OBJLexicalRoot extends NATByCopy {
  101. /**
  102. * The singleton instance of the sentinel lexical root
  103. */
  104. static public final OBJLexicalRoot _INSTANCE_ = new OBJLexicalRoot();
  105. /**
  106. * Constructor made private for singleton design pattern
  107. */
  108. private OBJLexicalRoot() { }
  109. /**
  110. * The lexical root has a special lexical parent object which ends the recursion
  111. * along the lexical lookup chain. These methods cannot be implemented
  112. * directly in this class because this class still implements useful
  113. * <tt>base_</tt> Java methods which have to be invoked by means of the
  114. * implementations defined in {@link NativeATObject}.
  115. */
  116. private final NativeATObject lexicalSentinel_ = new NATByCopy() {
  117. // METHODS THAT END THE LEXICAL LOOKUP CHAIN
  118. public ATObject impl_callAccessor(ATSymbol selector, ATTable arguments) throws InterpreterException {
  119. throw new XUndefinedSlot("variable access", selector.toString());
  120. }
  121. public ATObject impl_callMutator(ATAssignmentSymbol selector, ATTable arguments) throws InterpreterException {
  122. throw new XUnassignableField(selector.toString());
  123. }
  124. public ATObject impl_callField(ATSymbol selector) throws InterpreterException {
  125. throw new XUndefinedSlot("variable access", selector.toString());
  126. }
  127. public ATClosure impl_lookupAccessor(final ATSymbol selector) throws InterpreterException {
  128. throw new XUndefinedSlot("accessor", selector.toString());
  129. }
  130. public ATClosure impl_lookupMutator(ATAssignmentSymbol selector) throws InterpreterException {
  131. throw new XUnassignableField(selector.toString());
  132. }
  133. public NATText meta_print() throws InterpreterException {
  134. return NATText.atValue("lexicalrootsentinel");
  135. }
  136. };
  137. public ATObject impl_lexicalParent() {
  138. return lexicalSentinel_;
  139. }
  140. /* -----------------------
  141. * -- Primitive Methods --
  142. * ----------------------- */
  143. /* ===============================================================================
  144. * NOTE: the code below has been replaced by dedicated syntax and AST elements.
  145. * However, the skeleton of this code may still prove useful in the future, if
  146. * we ever plan to implement all base_ native methods as true AmbientTalk methods
  147. * (i.e. as PrimitiveMethod instances).
  148. * ===============================================================================
  149. */
  150. /*
  151. private static final AGSymbol _IMPORT_NAME_ = AGSymbol.jAlloc("import:");
  152. private static final AGSymbol _IMPORT_ALIAS_NAME_ = AGSymbol.jAlloc("import:alias:");
  153. private static final AGSymbol _IMPORT_EXCLUDE_NAME_ = AGSymbol.jAlloc("import:exclude:");
  154. private static final AGSymbol _IMPORT_ALIAS_EXCLUDE_NAME_ = AGSymbol.jAlloc("import:alias:exclude:");
  155. private static final AGSymbol _SRC_PARAM_ = AGSymbol.jAlloc("sourceObject");
  156. private static final AGSymbol _ALIAS_PARAM_ = AGSymbol.jAlloc("aliases");
  157. private static final AGSymbol _EXCLUDE_PARAM_ = AGSymbol.jAlloc("exclude");
  158. */
  159. /*protected static final PrimitiveMethod _PRIM_IMPORT_ = new PrimitiveMethod(_IMPORT_NAME_, NATTable.atValue(new ATObject[] { _SRC_PARAM_ })) {
  160. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  161. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  162. return performImport(sourceObject, ctx, new Hashtable(), OBJLexicalRoot.getDefaultExcludedSlots());
  163. }
  164. };*/
  165. /**
  166. * def import: sourceObject alias: [ `oldname -> `newname , ... ]
  167. */
  168. /*protected static final PrimitiveMethod _PRIM_IMPORT_ALIAS_ = new PrimitiveMethod(_IMPORT_ALIAS_NAME_, NATTable.atValue(new ATObject[] { _SRC_PARAM_, _ALIAS_PARAM_ })) {
  169. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  170. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  171. ATObject aliases = arguments.base_at(NATNumber.atValue(2));
  172. return performImport(sourceObject, ctx, preprocessAliases(aliases.base_asTable()), OBJLexicalRoot.getDefaultExcludedSlots());
  173. }
  174. };*/
  175. /**
  176. * def import: sourceObject excludes: [ `name1, `name2, ... ]
  177. */
  178. /*protected static final PrimitiveMethod _PRIM_IMPORT_EXCLUDE_ = new PrimitiveMethod(_IMPORT_EXCLUDE_NAME_, NATTable.atValue(new ATObject[] { _SRC_PARAM_, _EXCLUDE_PARAM_ })) {
  179. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  180. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  181. ATObject exclusions = arguments.base_at(NATNumber.atValue(2));
  182. return performImport(sourceObject, ctx, new Hashtable(), preprocessExcludes(exclusions.base_asTable()));
  183. }
  184. };*/
  185. /**
  186. * def import: sourceObject alias: [ `oldname -> `newname, ... ] excludes: [ `name1, `name2, ... ]
  187. */
  188. /*protected static final PrimitiveMethod _PRIM_IMPORT_ALIAS_EXCLUDE_ = new PrimitiveMethod(_IMPORT_ALIAS_EXCLUDE_NAME_,
  189. NATTable.atValue(new ATObject[] { _SRC_PARAM_, _ALIAS_PARAM_, _EXCLUDE_PARAM_ })) {
  190. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  191. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  192. ATObject aliases = arguments.base_at(NATNumber.atValue(2));
  193. ATObject exclusions = arguments.base_at(NATNumber.atValue(3));
  194. return performImport(sourceObject, ctx, preprocessAliases(aliases.base_asTable()), preprocessExcludes(exclusions.base_asTable()));
  195. }
  196. };*/
  197. /**
  198. * Invoked whenever a new true AmbientTalk object is created that should
  199. * represent the root. This gives the lexical root a chance to install its
  200. * primitive methods.
  201. */
  202. /*public static void initializeRoot(NATObject root) {
  203. try {
  204. // add import: native
  205. root.meta_addMethod(_PRIM_IMPORT_);
  206. // add import:alias: native
  207. root.meta_addMethod(_PRIM_IMPORT_ALIAS_);
  208. // add import:exclude: native
  209. root.meta_addMethod(_PRIM_IMPORT_EXCLUDE_);
  210. // add import:alias:exclude: native
  211. root.meta_addMethod(_PRIM_IMPORT_ALIAS_EXCLUDE_);
  212. } catch (InterpreterException e) {
  213. Logging.Init_LOG.fatal("Failed to initialize the root!", e);
  214. }
  215. }*/
  216. /* ----------------------
  217. * -- Global variables --
  218. * ---------------------- */
  219. /**
  220. * <tt>nil</tt> evaluates to the nil object, which is
  221. * the empty, dynamic parent of all AmbientTalk objects.
  222. */
  223. public ATNil base_nil() {
  224. return Evaluator.getNil();
  225. }
  226. /**
  227. * <tt>true</tt> evaluates to the unique boolean true object.
  228. */
  229. public ATBoolean base_true() {
  230. return NATBoolean._TRUE_;
  231. }
  232. /**
  233. * <tt>false</tt> evaluates to the unique boolean false object.
  234. */
  235. public ATBoolean base_false() {
  236. return NATBoolean._FALSE_;
  237. }
  238. /**
  239. * <tt>/</tt> evaluates to the global namespace. It is
  240. * simply an alias for <tt>lobby</tt>.
  241. * @see #base_lobby()
  242. */
  243. public ATObject base__opdiv_() {
  244. return base_lobby();
  245. }
  246. /**
  247. * <tt>lobby</tt> evaluates to the global namespace object.
  248. * For each <tt>name=path</tt> entry on AmbientTalk's
  249. * <i>object path</i>, the lobby object contains a slot
  250. * <tt>name</tt> bound to a namespace object bound to
  251. * the directory referred to by <tt>path</tt>.
  252. * <p>
  253. * Accessing the lobby allows loading in AmbientTalk source code
  254. * from external files.
  255. */
  256. public ATObject base_lobby() {
  257. return Evaluator.getLobbyNamespace();
  258. }
  259. /**
  260. * <tt>root</tt> evaluates to the global lexical scope object.
  261. * This is the top-level object in which the definitions of
  262. * the file <tt>at/init/init.at</tt> are evaluated. All code
  263. * is assumed to be "nested" in the lexical root, so all definitions
  264. * of this object are lexically accessible.
  265. */
  266. public ATObject base_root() {
  267. return Evaluator.getGlobalLexicalScope();
  268. }
  269. /**
  270. * <tt>jlobby</tt> evaluates to the Java namespace root. It is a
  271. * special object which is part of the symbiosis infrastructure of
  272. * AmbientTalk. <tt>jlobby</tt> acts like an object that has field
  273. * names that correspond to Java package names. By selecting fields
  274. * from this object, an appropriate Java package can be created
  275. * from which a Java class can be accessed. Only the Java classes
  276. * accessible in the Java classpath are accessible.
  277. *
  278. * Example:
  279. * <code>jlobby.java.util.Vector</code> evaluates to a reference to
  280. * the Java <tt>Vector</tt> class.
  281. */
  282. public ATObject base_jlobby() {
  283. return Evaluator.getJLobbyRoot();
  284. }
  285. /**
  286. * <tt>network</tt> evaluates to the unique network control object.
  287. * It is a simple native object with two methods:
  288. * <ul>
  289. * <li><tt>network.online()</tt> makes the interpreter go online. This allows
  290. * publications of local actors to be discovered by remote objects and vice versa.
  291. * <li><tt>network.offline()</tt> makes the interpreter go offline. All
  292. * remote references to remote objects will become disconnected.
  293. * </ul>
  294. */
  295. public ATObject base_network() {
  296. return OBJNetwork._INSTANCE_;
  297. }
  298. /**
  299. * <tt>defaultMirror</tt> evaluates to the default mirror on objects. This
  300. * is the mirror encapsulating the standard AmbientTalk object semantics.
  301. * That is, it is a mirror with similar behaviour as the mirror created by
  302. * executing: <code>reflect: (object: { ... })</code>.
  303. *
  304. * The default mirror is an object with a read-only <tt>base</tt> field
  305. * that signifies the base-level object of this mirror. The main purpose
  306. * of this object is to serve as a prototype whose methods can be overridden
  307. * by custom mirrors. The syntax:
  308. * <pre>
  309. * mirror: { ... }
  310. * </pre>
  311. * is syntactic sugar for:
  312. * <pre>
  313. * extend: defaultMirror with: { ... }
  314. * </pre>
  315. *
  316. * Note that the default mirror is typed with the <tt>/.at.types.Mirror</tt> type.
  317. */
  318. public ATObject base_defaultMirror() {
  319. return Evaluator.getMirrorRoot();
  320. }
  321. /* ------------------------
  322. * -- Control Structures --
  323. * ------------------------ */
  324. /**
  325. * The <tt>if:then:</tt> control structure. Usage:
  326. * <pre>if: cond then: consequent</pre>
  327. *
  328. * pseudo-implementation:
  329. * <pre>cond.ifTrue: consequent</pre>
  330. *
  331. * Note that the consequent parameter should be a <i>closure</i>, i.e.
  332. * the caller is responsible for delaying the evaluation of the consequent!
  333. *
  334. * @param cond a boolean object
  335. * @param consequent a closure containing the code to execute if the boolean is true
  336. * @return if <tt>cond</tt> is true, the value of applying the consequent, <tt>nil</tt> otherwise
  337. */
  338. public ATObject base_if_then_(ATBoolean cond, ATClosure consequent) throws InterpreterException {
  339. return cond.base_ifTrue_(consequent);
  340. }
  341. /**
  342. * The <tt>if:then:else:</tt> control structure. Usage:
  343. * <pre>if: cond then: consequent else: alternative</pre>
  344. *
  345. * pseudo-implementation:
  346. * <pre>cond.ifTrue: consequent ifFalse: alternative</pre>
  347. *
  348. * Note that the consequent and alternative parameters should be <i>closures</i>, i.e.
  349. * the caller is responsible for delaying the evaluation of these arguments!
  350. *
  351. * @param cond a boolean object
  352. * @param consequent a closure containing the code to execute if the boolean is true
  353. * @param alternative a closure containing the code to execute if the boolean is false
  354. * @return the value of consequent if the boolean is true, the value of the alternative otherwise.
  355. */
  356. public ATObject base_if_then_else_(ATBoolean cond, ATClosure consequent, ATClosure alternative) throws InterpreterException {
  357. return cond.base_ifTrue_ifFalse_(consequent, alternative);
  358. }
  359. /**
  360. * The <tt>while:do:</tt> control structure. Usage:
  361. * <pre>while: condition do: body</pre>
  362. *
  363. * pseudo-implementation:
  364. * <pre>condition.whileTrue: body</pre>
  365. *
  366. * Note that <i>both</i> the condition and the body should be <i>closures</i>, because
  367. * they represent pieces of code that have to be executed repeatedly. Because of traditional
  368. * syntax, novice programmers are inclined to make the mistake of writing, e.g.:
  369. * <pre>while: (i < 10) do: { i := i + 1 }</pre>
  370. * Which is wrong because the first parameter should evaluate to a closure that itself
  371. * returns a boolean value, not to a boolean value directly.
  372. *
  373. * @param condition a closure expected to return a boolean object
  374. * @param body a closure containing the code to execute as long as the condition closure returns true
  375. * @return if conditions is true at least once, the last value of body, <tt>nil</tt> otherwise.
  376. */
  377. public ATObject base_while_do_(ATClosure condition, ATClosure body) throws InterpreterException {
  378. return condition.base_whileTrue_(body);
  379. }
  380. /**
  381. * The <tt>foreach:in:</tt> control structure. Usage:
  382. *
  383. * <pre>foreach: body in: table</pre>
  384. *
  385. * pseudo-implementation:
  386. * <pre>table.each: body</pre>
  387. *
  388. * Example: <code>[1,2,3].each: { |i| system.println(i) }</code>
  389. *
  390. * @param body a one-arity closure that is to be applied to each element of the table
  391. * @param tab a table to apply the body closure to
  392. * @return <tt>nil</tt>, by default
  393. */
  394. public ATObject base_foreach_in_(ATClosure body, ATTable tab) throws InterpreterException {
  395. return tab.base_each_(body);
  396. }
  397. /**
  398. * The <tt>do:if:</tt> control structure. Usage:
  399. * <pre>do: body if: condition</pre>
  400. *
  401. * pseudo-implementation:
  402. * <pre>condition.ifTrue: body</pre>
  403. *
  404. * In Ruby, this kind of control structure is called a <i>statement modifier</i>.
  405. *
  406. * @param body a zero-argument closure to execute if the condition is true
  407. * @param condition a boolean value
  408. * @return the result of invoking body if the condition is true or nil if the
  409. * condition is false
  410. */
  411. public ATObject base_do_if_(ATClosure body, ATBoolean condition) throws InterpreterException {
  412. return condition.base_ifTrue_(body);
  413. }
  414. /**
  415. * The <tt>do:unless:</tt> control structure. Usage:
  416. * <pre>do: body unless: condition</pre>
  417. *
  418. * pseudo-implementation:
  419. * <pre>condition.ifFalse: body</pre>
  420. *
  421. * In Ruby, this kind of control structure is called a <i>statement modifier</i>.
  422. * Example: <code>do: { file.close() } unless: (nil == file)</code>
  423. *
  424. * @param body a zero-argument closure to execute only if the condition is false
  425. * @param condition a boolean value
  426. * @return the result of invoking body if the condition is false, nil otherwise
  427. */
  428. public ATObject base_do_unless_(ATClosure body, ATBoolean condition) throws InterpreterException {
  429. return condition.base_ifFalse_(body);
  430. }
  431. /**
  432. * The <tt>let:</tt> construct. Usage:
  433. * <pre>let: { |var := value| body }</pre>
  434. *
  435. * pseudo-implementation:
  436. * <pre>closure()</pre>
  437. *
  438. * <tt>let:</tt> allows for the easy creation of temporary local variables.
  439. * This construct should be used in conjunction with a closure that declares optional
  440. * parameters. Because the closure will be invoked with zero arguments, all of the
  441. * parameters will be given their corresponding default initial value. The parameters
  442. * are defined local to the closure's body.
  443. *
  444. * AmbientTalk's <tt>let:</tt> behaves like Scheme's <tt>let*</tt> and <tt>letrec</tt>,
  445. * i.e. the following is legal:
  446. * <pre>let: {
  447. * |var1 := value1,
  448. * var2 := var1,
  449. * var3 := { ... var3() ... }|
  450. * ...
  451. *}</pre>
  452. *
  453. * @param body a closure which is supposed to declare some optional parameters
  454. * @return the result of invoking the body closure
  455. */
  456. public ATObject base_let_(ATClosure body) throws InterpreterException {
  457. return body.base_apply(NATTable.EMPTY);
  458. }
  459. /* ------------------------------------------
  460. * -- Actor Creation and accessing Methods --
  461. * ------------------------------------------ */
  462. /**
  463. * The <tt>actor: closure</tt> construct.
  464. *
  465. * The semantics of actor creation is as follows:
  466. * <ul>
  467. * <li> Mandatory parameters to the block of initialization code are treated as lexically visible
  468. * variables that have to remain available in the new actor behaviour. Hence, these variables
  469. * are evaluated to values immediately at creation-time and parameter-passed to the new actor.
  470. * <li> The closure containing the initialization code is unpacked, its lexical scope is disregarded
  471. * and the unwrapped method is serialized and sent to the new actor, which can use it to
  472. * initialize his behaviour object.
  473. * <li>The creating actor waits for the created actor to spawn a new behaviour and to return a far
  474. * reference to this behaviour. From that point on, the creating actor can run in parallel with
  475. * the created actor, which only then evaluates the initialization code to initialize its behaviour.
  476. * </ul>
  477. *
  478. * @param closure the closure whose parameters define lexical fields to be copied and whose
  479. * method specifies the code of the new actor's behaviour object
  480. * @return a far reference to the behaviour of the new actor
  481. */
  482. public ATObject base_actor_(ATClosure closure) throws InterpreterException {
  483. ATMethod method = closure.base_method();
  484. ATObject copiedBindings;
  485. // if no variables were specified to pass along to the new actor, calculate the
  486. // set of free variables for the actor
  487. if (method.base_parameters().base_isEmpty().asNativeBoolean().javaValue) {
  488. // introduce a private scope object that will hold copies
  489. // of the lexically free variables of the actor
  490. copiedBindings = new NATObject(OBJLexicalRoot._INSTANCE_, new ATTypeTag[] { NativeTypeTags._ISOLATE_ });
  491. // calculate the set of free variables of the initialization expression
  492. Set freeVars = method.base_bodyExpression().impl_freeVariables();
  493. // add all these free variables manually as fields to a custom
  494. Iterator it = freeVars.iterator();
  495. while (it.hasNext()) {
  496. ATSymbol freeVar = (ATSymbol) it.next();
  497. // extra check to weed out special variables like "super" and variables available in the lexical root
  498. if (! (Evaluator.getGlobalLexicalScope().meta_respondsTo(freeVar).asNativeBoolean().javaValue
  499. || OBJLexicalRoot._INSTANCE_.meta_respondsTo(freeVar).asNativeBoolean().javaValue)) {
  500. // lookup the variable in the lexical scope
  501. try {
  502. ATClosure accessor = closure.base_context().base_lexicalScope().impl_lookup(freeVar);
  503. // only add the variable if it refers to a field, rather than to a method
  504. if (accessor instanceof NativeClosure.Accessor) {
  505. copiedBindings.meta_defineField(freeVar, accessor.base_apply(NATTable.EMPTY));
  506. }
  507. } catch(XUndefinedSlot exc) {
  508. // silently ignore lexically free variables which cannot be found
  509. // the assumption is that these variables will be bound by means of
  510. // import statements
  511. Logging.Actor_LOG.warn("Undefined lexically free var while constructing actor: "+exc.getFieldName());
  512. }
  513. }
  514. }
  515. } else {
  516. copiedBindings = Evaluator.evalMandatoryPars(
  517. method.base_parameters(),
  518. closure.base_context());
  519. }
  520. Packet serializedBindings = new Packet("actor-bindings", copiedBindings);
  521. Packet serializedInitCode = new Packet("actor-initcode", method);
  522. return ELVirtualMachine.currentVM().createActor(serializedBindings, serializedInitCode);
  523. }
  524. /**
  525. * <tt>reflectOnActor</tt> evaluates to the mirror on the actor executing this code.
  526. * The actor mirror is an object whose behaviour is consulted for operations
  527. * such as creating and sending asynchronous messages or creating mirrors on
  528. * other objects. It can be replaced by a custom mirror by means of the actor
  529. * mirror's <tt>getExplicitActorMirror</tt> method.
  530. */
  531. public ATActorMirror base_reflectOnActor() throws InterpreterException {
  532. return ELActor.currentActor().getImplicitActorMirror().base_getExplicitActorMirror();
  533. }
  534. /**
  535. * The <tt>export: object as: topic</tt> construct. Pseudo-implementation:
  536. * <pre>actor.provide(topic, object)</pre>
  537. *
  538. * This construct enables the given object to become discoverable by objects
  539. * in other actors by means of the topic type.
  540. *
  541. * @param object the object to export to remote actors' objects
  542. * @param topic a type denoting the abstract 'publication topic' for this object's publication
  543. * @return a publication object whose <tt>cancel</tt> method can be used to cancel the publication.
  544. */
  545. public ATObject base_export_as_(ATObject object, ATTypeTag topic) throws InterpreterException {
  546. return ELActor.currentActor().getImplicitActorMirror().base_provide(topic, object);
  547. }
  548. /**
  549. * The <tt>when: topic discovered: handler</tt> construct. Pseudo-implementation:
  550. * <pre>actor.require(topic, handler, false)</pre>
  551. *
  552. * When an object is exported by <i>another</i> actor under topic, this construct triggers
  553. * the given code, passing a reference to the exported object as argument to the closure.
  554. *
  555. * Once the code block has run once, it will not be triggered again.
  556. *
  557. * @param topic the abstract 'subscription topic' used to find an exported object
  558. * @param handler a one-argument closure to apply to a discovered exported object
  559. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel the subscription,
  560. * such that the handler will no longer be invoked. Beware, however, that at the time the
  561. * subscription is cancelled, a request to apply the closure may already have been scheduled
  562. * for execution by the current actor. This request is not cancelled by invoking the <tt>cancel</tt> method.
  563. */
  564. public ATObject base_when_discovered_(ATTypeTag topic, ATClosure handler) throws InterpreterException {
  565. return ELActor.currentActor().getImplicitActorMirror().base_require(topic, handler, NATBoolean._FALSE_);
  566. }
  567. /**
  568. * The <tt>whenever: topic discovered: handler</tt> construct. Pseudo-implementation:
  569. * <pre>actor.require(topic, handler, true)</pre>
  570. *
  571. * When an object is exported by <i>another</i> actor under topic, this construct triggers
  572. * the given code, passing a reference to the exported object as argument to the closure.
  573. *
  574. * The code block can be fired multiple times upon discovering multiple exported objects.
  575. * To stop the block from triggering upon new publications, it must be explicitly cancelled
  576. *
  577. * @param topic the abstract 'subscription topic' used to find an exported object
  578. * @param handler a one-argument closure to apply to any discovered exported object
  579. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel the subscription,
  580. * such that the handler will no longer be invoked. Beware, however, that at the time the
  581. * subscription is cancelled, a request to apply the closure may already have been scheduled
  582. * for execution by the current actor. This request is not cancelled by invoking the <tt>cancel</tt> method.
  583. */
  584. public ATObject base_whenever_discovered_(ATTypeTag topic, ATClosure handler) throws InterpreterException {
  585. return ELActor.currentActor().getImplicitActorMirror().base_require(topic, handler, NATBoolean._TRUE_);
  586. }
  587. /**
  588. * The <tt>whenever: farReference disconnected: listener</tt> construct.
  589. * When the far reference is broken due to network disconnections, triggers the zero-arity listener
  590. * closure. It is possible to register listeners on local far references. These may trigger if the
  591. * local actor takes its object offline. In this case, these listeners will trigger as well.
  592. *
  593. * @param farReference a native far reference
  594. * @param listener a zero-arity closure to invoke if the far reference becomes disconnected
  595. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel future
  596. * notifications of the listener.
  597. */
  598. public ATObject base_whenever_disconnected_(ATFarReference farReference, ATClosure listener) throws InterpreterException {
  599. farReference.asNativeFarReference().addDisconnectionListener(listener);
  600. return new NATFarReference.NATDisconnectionSubscription(farReference.asNativeFarReference(), listener);
  601. }
  602. /**
  603. * The <tt>whenever: farReference reconnected: listener</tt> construct.
  604. * When the remote reference is reinstated after a network disconnection, trigger the zero-arity
  605. * listener. Although it is allowed to register these listeners on local far references,
  606. * these are normally not invoked because the only possibility for a local far ref to become
  607. * disconnected is because the object was taken offline, and this is a permanent disconnect.
  608. *
  609. * @param farReference a native far reference
  610. * @param listener a zero-arity closure to invoke if the far reference becomes reconnected
  611. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel future
  612. * notifications of the listener.
  613. */
  614. public ATObject base_whenever_reconnected_(ATFarReference farReference, ATClosure listener) throws InterpreterException {
  615. farReference.asNativeFarReference().addReconnectionListener(listener);
  616. return new NATFarReference.NATReconnectionSubscription(farReference.asNativeFarReference(), listener);
  617. }
  618. /**
  619. * The <tt>when: farReference takenOffline:</tt> construct.
  620. * When the (remote/local) far reference is broken because the object referenced was
  621. * taken offline, trigger the code.
  622. *
  623. * @param farReference a native far reference
  624. * @param listener a zero-arity closure to invoke if the referenced object has been taken offline.
  625. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel future
  626. * notifications of the listener.
  627. */
  628. public ATObject base_when_takenOffline_(ATFarReference farReference, ATClosure listener) throws InterpreterException {
  629. farReference.asNativeFarReference().addTakenOfflineListener(listener);
  630. return new NATFarReference.NATExpiredSubscription(farReference.asNativeFarReference(), listener);
  631. }
  632. /**
  633. * The <tt>retract: farReference</tt> construct.
  634. * Retracts all currently unsent messages from the far reference's outbox.
  635. * This has the side effect that the returned messages will <b>not</b> be sent
  636. * automatically anymore, the programmer is responsible to explicitly resend
  637. * all messages that were retracted but still need to be sent.
  638. *
  639. * Note that the returned messages are copies of the original.
  640. * @param farReference the far reference of which to retract outgoing message sends
  641. * @return a table containing copies of all messages that were sent to this far reference, but
  642. * not yet transmitted by the far reference to its referent.
  643. */
  644. public ATTable base_retract_(ATFarReference farReference) throws InterpreterException {
  645. return farReference.meta_retractUnsentMessages();
  646. }
  647. /* -----------------------------
  648. * -- Object Creation Methods --
  649. * ----------------------------- */
  650. /**
  651. * The <tt>object:</tt> object creation primitive.
  652. * This construct creates a new AmbientTalk object where:
  653. * <ul>
  654. * <li>The object is initialized with the <i>code</i> of the argument closure.
  655. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  656. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  657. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  658. * <li>The object's <b>types</b> is <tt>[]</tt> (i.e. it has no types).
  659. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  660. * with a 'native' metaobject protocol).
  661. * </ul>
  662. *
  663. * Example: <code>object: { def x := 1; }</code>
  664. * <p>
  665. * Pseudo-implementation:
  666. * <pre>object: code childOf: nil extends: false taggedAs: [] mirroredBy: defaultMirror</pre>
  667. *
  668. * The closure used to initialize the object may contain formal parameters. The closure
  669. * will always be invoked with <i>its own mandatory formal parameters</i>. E.g., a closure
  670. * <code>{ |x| nil }</code> is invoked as <code>{ |x| nil }(x)</code>. The net effect of this
  671. * mechanic is that if <tt>x</tt> is a lexically visible variable at the object-creation
  672. * site, the value of the variable will be copied into a copy with the same name which
  673. * resides in the newly created object. This mechanic is primarily useful for copying surrounding
  674. * variables within the object, e.g. for isolates which lose access to their surrounding scope.
  675. * <p>
  676. * Also, if the closure has optional parameters, they will always be triggered.
  677. * The expressions to initialize the formal parameters are <i>evaluated</i>
  678. * in the context of the closure's lexical scope but are <i>added</i> to the newly created object.
  679. *
  680. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
  681. * @return a new AmbientTalk object with the properties defined above.
  682. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  683. */
  684. public ATObject base_object_(ATClosure code) throws InterpreterException {
  685. return base_object_childOf_extends_taggedAs_mirroredBy_(
  686. code,
  687. Evaluator.getNil(),
  688. NATBoolean._FALSE_ /* SHARES-A link */,
  689. NATTable.EMPTY,
  690. base_defaultMirror());
  691. }
  692. /**
  693. * The <tt>extend:with:</tt> object creation primitive.
  694. * This construct creates a new AmbientTalk object where:
  695. * <ul>
  696. * <li>The object is initialized with the <i>code</i> of the argument closure.
  697. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  698. * <li>The object's <b>dynamic parent</b> is the argument object.
  699. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  700. * <li>The object's <b>types</b> is <tt>[]</tt> (i.e. it has no types).
  701. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  702. * with a 'native' metaobject protocol).
  703. * </ul>
  704. *
  705. * Example: <code>extend: parent with: { def x := 1; }</code>
  706. * <p>
  707. * Pseudo-implementation:
  708. * <pre>object: code childOf: parent extends: true taggedAs: [] mirroredBy: defaultMirror</pre>
  709. *
  710. * @param parent the dynamic parent object of the newly created object.
  711. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
  712. * @return a new AmbientTalk object with the properties defined above.
  713. * @see #base_object_(ATClosure)
  714. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  715. */
  716. public ATObject base_extend_with_(ATObject parent, ATClosure code) throws InterpreterException {
  717. return base_object_childOf_extends_taggedAs_mirroredBy_(
  718. code,
  719. parent,
  720. NATBoolean._TRUE_ /* IS-A link */,
  721. NATTable.EMPTY,
  722. base_defaultMirror());
  723. }
  724. /**
  725. * The <tt>extend:with:taggedAs:</tt> object creation primitive.
  726. * This construct creates a new AmbientTalk object where:
  727. * <ul>
  728. * <li>The object is initialized with the <i>code</i> of the argument closure.
  729. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  730. * <li>The object's <b>dynamic parent</b> is the argument object.
  731. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  732. * <li>The object's <b>types</b> are initialized to the argument types table.
  733. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  734. * with a 'native' metaobject protocol).
  735. * </ul>
  736. *
  737. * Example: <code>extend: parent with: { def x := 1; } taggedAs: [foo,bar]</code>
  738. * <p>
  739. * Pseudo-implementation:
  740. * <pre>object: code childOf: parent extends: true taggedAs: types mirroredBy: defaultMirror</pre>
  741. *
  742. * @param parent the dynamic parent object of the newly created object.
  743. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  744. * @param types a table of types with which to type the newly created object.
  745. * @return a new AmbientTalk object with the properties defined above.
  746. * @see #base_object_(ATClosure)
  747. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  748. */
  749. public ATObject base_extend_with_taggedAs_(ATObject parent, ATClosure code, ATTable types) throws InterpreterException {
  750. return base_object_childOf_extends_taggedAs_mirroredBy_(
  751. code,
  752. parent,
  753. NATBoolean._TRUE_ /* IS-A link */,
  754. types,
  755. base_defaultMirror());
  756. }
  757. /**
  758. * The <tt>extend:with:mirroredBy:</tt> object creation primitive.
  759. * This construct creates a new AmbientTalk object where:
  760. * <ul>
  761. * <li>The object is initialized with the <i>code</i> of the argument closure.
  762. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  763. * <li>The object's <b>dynamic parent</b> is the argument object.
  764. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  765. * <li>The object's <b>types</b> are set to <tt>[]</tt> (i.e. the object has no types).
  766. * <li>The object's <b>mirror</b> is the given mirror. This means that this object is a <i>mirage</i>
  767. * whose metaobject protocol is entirely dictated by the given mirror.
  768. * </ul>
  769. *
  770. * Example: <code>extend: parent with: { def x := 1; } mirroredBy: (mirror: {...})</code>
  771. * <p>
  772. * Pseudo-implementation:
  773. * <pre>object: code childOf: parent extends: true taggedAs: [] mirroredBy: mirror</pre>
  774. *
  775. * @param parent the dynamic parent object of the newly created object.
  776. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  777. * @param mirror the mirror of the newly created mirage object.
  778. * @return a new AmbientTalk object with the properties defined above.
  779. * @see #base_object_(ATClosure)
  780. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  781. */
  782. public ATObject base_extend_with_mirroredBy_(ATObject parent, ATClosure code, ATObject mirror) throws InterpreterException {
  783. return base_object_childOf_extends_taggedAs_mirroredBy_(
  784. code,
  785. parent,
  786. NATBoolean._TRUE_ /* IS-A link */,
  787. NATTable.EMPTY,
  788. mirror);
  789. }
  790. /**
  791. * The <tt>extend:with:taggedAs:mirroredBy:</tt> object creation primitive.
  792. * This construct creates a new AmbientTalk object where:
  793. * <ul>
  794. * <li>The object is initialized with the <i>code</i> of the argument closure.
  795. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  796. * <li>The object's <b>dynamic parent</b> is the argument object.
  797. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  798. * <li>The object's <b>types</b> are initialized to the argument types table.
  799. * <li>The object's <b>mirror</b> is the given argument mirror. This means that the newly
  800. * created object is a <i>mirage</i> whose metaobject protocol is dictated by the given mirror.
  801. * </ul>
  802. *
  803. * Example: <code>extend: parent with: { def x := 1; } taggedAs: [foo,bar] mirroredBy: mirror</code>
  804. * <p>
  805. * Pseudo-implementation:
  806. * <pre>object: code childOf: parent extends: true taggedAs: types mirroredBy: mirror</pre>
  807. *
  808. * @param parent the dynamic parent object of the newly created object.
  809. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  810. * @param types a table of types with which to type the newly created object.
  811. * @param the mirror object of the newly created mirage object.
  812. * @return a new AmbientTalk object with the properties defined above.
  813. * @see #base_object_(ATClosure)
  814. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  815. */
  816. public ATObject base_extend_with_taggedAs_mirroredBy_(ATObject parent, ATClosure code, ATTable types, ATObject mirror) throws InterpreterException {
  817. return base_object_childOf_extends_taggedAs_mirroredBy_(
  818. code,
  819. parent,
  820. NATBoolean._TRUE_ /* IS-A link */,
  821. types,
  822. mirror);
  823. }
  824. /**
  825. * The <tt>share:with:</tt> object creation primitive.
  826. * This construct creates a new AmbientTalk object where:
  827. * <ul>
  828. * <li>The object is initialized with the <i>code</i> of the argument closure.
  829. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  830. * <li>The object's <b>dynamic parent</b> is the argument object.
  831. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  832. * <li>The object's <b>types</b> is <tt>[]</tt> (i.e. it has no types).
  833. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  834. * with a 'native' metaobject protocol).
  835. * </ul>
  836. *
  837. * Example: <code>share: parent with: { def x := 1; }</code>
  838. * <p>
  839. * Pseudo-implementation:
  840. * <pre>object: code childOf: parent extends: false taggedAs: [] mirroredBy: defaultMirror</pre>
  841. *
  842. * @param parent the dynamic parent object of the newly created object.
  843. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
  844. * @return a new AmbientTalk object with the properties defined above.
  845. * @see #base_object_(ATClosure)
  846. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  847. */
  848. public ATObject base_share_with_(ATObject parent, ATClosure code) throws InterpreterException {
  849. return base_object_childOf_extends_taggedAs_mirroredBy_(
  850. code,
  851. parent,
  852. NATBoolean._FALSE_ /* SHARES-A link */,
  853. NATTable.EMPTY,
  854. base_defaultMirror());
  855. }
  856. /**
  857. * The <tt>share:with:taggedAs:</tt> object creation primitive.
  858. * This construct creates a new AmbientTalk object where:
  859. * <ul>
  860. * <li>The object is initialized with the <i>code</i> of the argument closure.
  861. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  862. * <li>The object's <b>dynamic parent</b> is the argument object.
  863. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  864. * <li>The object's <b>types</b> are initialized to the argument types table.
  865. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  866. * with a 'native' metaobject protocol).
  867. * </ul>
  868. *
  869. * Example: <code>share: parent with: { def x := 1; } taggedAs: [foo,bar]</code>
  870. * <p>
  871. * Pseudo-implementation:
  872. * <pre>object: code childOf: parent extends: false taggedAs: types mirroredBy: defaultMirror</pre>
  873. *
  874. * @param parent the dynamic parent object of the newly created object.
  875. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  876. * @param types a table of types with which to type the newly created object.
  877. * @return a new AmbientTalk object with the properties defined above.
  878. * @see #base_object_(ATClosure)
  879. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  880. */
  881. public ATObject base_share_with_taggedAs_(ATObject parent, ATClosure code, ATTable types) throws InterpreterException {
  882. return base_object_childOf_extends_taggedAs_mirroredBy_(
  883. code,
  884. parent,
  885. NATBoolean._FALSE_ /* SHARES-A link */,
  886. types,
  887. base_defaultMirror());
  888. }
  889. /**
  890. * The <tt>share:with:mirroredBy:</tt> object creation primitive.
  891. * This construct creates a new AmbientTalk object where:
  892. * <ul>
  893. * <li>The object is initialized with the <i>code</i> of the argument closure.
  894. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  895. * <li>The object's <b>dynamic parent</b> is the argument object.
  896. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  897. * <li>The object's <b>types</b> are set to <tt>[]</tt> (i.e. the object has no types).
  898. * <li>The object's <b>mirror</b> is the given mirror. This means that this object is a <i>mirage</i>
  899. * whose metaobject protocol is entirely dictated by the given mirror.
  900. * </ul>
  901. *
  902. * Example: <code>share: parent with: { def x := 1; } mirroredBy: (mirror: {...})</code>
  903. * <p>
  904. * Pseudo-implementation:
  905. * <pre>object: code childOf: parent extends: false taggedAs: [] mirroredBy: mirror</pre>
  906. *
  907. * @param parent the dynamic parent object of the newly created object.
  908. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  909. * @param mirror the mirror of the newly created mirage object.
  910. * @return a new AmbientTalk object with the properties defined above.
  911. * @see #base_object_(ATClosure)
  912. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  913. */
  914. public ATObject base_share_with_mirroredBy_(ATObject parent, ATClosure code, ATObject mirror) throws InterpreterException {
  915. return base_object_childOf_extends_taggedAs_mirroredBy_(
  916. code,
  917. parent,
  918. NATBoolean._FALSE_ /* SHARES-A link */,
  919. NATTable.EMPTY,
  920. mirror);
  921. }
  922. /**
  923. * The <tt>share:with:taggedAs:mirroredBy:</tt> object creation primitive.
  924. * This construct creates a new AmbientTalk object where:
  925. * <ul>
  926. * <li>The object is initialized with the <i>code</i> of the argument closure.
  927. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  928. * <li>The object's <b>dynamic parent</b> is the argument object.
  929. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  930. * <li>The object's <b>types</b> are initialized to the argument types table.
  931. * <li>The object's <b>mirror</b> is the given argument mirror. This means that the newly
  932. * created object is a <i>mirage</i> whose metaobject protocol is dictated by the given mirror.
  933. * </ul>
  934. *
  935. * Example: <code>share: parent with: { def x := 1; } taggedAs: [foo,bar] mirroredBy: mirror</code>
  936. * <p>
  937. * Pseudo-implementation:
  938. * <pre>object: code childOf: parent extends: false taggedAs: types mirroredBy: mirror</pre>
  939. *
  940. * @param parent the dynamic parent object of the newly created object.
  941. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  942. * @param types a table of types with which to type the newly created object.
  943. * @param mirror the mirror object of the newly created mirage object.
  944. * @return a new AmbientTalk object with the properties defined above.
  945. * @see #base_object_(ATClosure)
  946. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  947. */
  948. public ATObject base_share_with_taggedAs_mirroredBy_(ATObject parent, ATClosure code, ATTable types, ATObject mirror) throws InterpreterException {
  949. return base_object_childOf_extends_taggedAs_mirroredBy_(
  950. code,
  951. parent,
  952. NATBoolean._FALSE_ /* SHARES-A link */,
  953. types,
  954. mirror);
  955. }
  956. /**
  957. * The <tt>object:taggedAs:</tt> object creation primitive.
  958. * This construct creates a new AmbientTalk object where:
  959. * <ul>
  960. * <li>The object is initialized with the <i>code</i> of the argument closure.
  961. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  962. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  963. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  964. * <li>The object's <b>types</b> are initialized to the argument types table.
  965. * <li>The object's <b>mirror</b> is <tt>defaultMirror</tt> (i.e. the object has the
  966. * default metaobject protocol).
  967. * </ul>
  968. *
  969. * Example: <code>object: { def x := 1; } taggedAs: [foo,bar]</code>
  970. * <p>
  971. * Pseudo-implementation:
  972. * <pre>object: code childOf: nil extends: false taggedAs: types mirroredBy: defaultMirror</pre>
  973. *
  974. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  975. * @param types a table of type tags with which to type the newly created object.
  976. * @return a new AmbientTalk object with the properties defined above.
  977. * @see #base_object_(ATClosure)
  978. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  979. */
  980. public ATObject base_object_taggedAs_(ATClosure code, ATTable types) throws InterpreterException {
  981. return base_object_childOf_extends_taggedAs_mirroredBy_(
  982. code,
  983. Evaluator.getNil(),
  984. NATBoolean._FALSE_ /* SHARES-A link */,
  985. types,
  986. base_defaultMirror());
  987. }
  988. /**
  989. * The <tt>isolate:</tt> object creation primitive.
  990. * This construct creates a new AmbientTalk object where:
  991. * <ul>
  992. * <li>The object is initialized with the <i>code</i> of the argument closure.
  993. * <li>The object's <b>lexical parent</b> is initialized to the lexical scope of the argument closure,
  994. * but because it is typed as an isolate, the parent link is replaced by a link to the lexical <tt>root</tt>.
  995. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  996. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  997. * <li>The object's <b>types</b> are initialized to <tt>[/.at.types.Isolate]</tt>, i.e.
  998. * the object is typed as an isolate.
  999. * <li>The object's <b>mirror</b> is <tt>defaultMirror</tt> (i.e. the object has the
  1000. * default metaobject protocol).
  1001. * </ul>
  1002. *
  1003. * Example: <code>isolate: { def x := 1; }</code>
  1004. * <p>
  1005. * Pseudo-implementation:
  1006. * <pre>object: code childOf: nil extends: false taggedAs: [/.at.types.Isolate] mirroredBy: defaultMirror</pre>
  1007. *
  1008. * An iso…