PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 1731 lines | 451 code | 117 blank | 1163 comment | 18 complexity | d4dd4621ac4e2c35ee1f1798eba9b62e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  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.ATHandler;
  45. import edu.vub.at.objects.ATMethod;
  46. import edu.vub.at.objects.ATNil;
  47. import edu.vub.at.objects.ATNumber;
  48. import edu.vub.at.objects.ATNumeric;
  49. import edu.vub.at.objects.ATObject;
  50. import edu.vub.at.objects.ATTable;
  51. import edu.vub.at.objects.ATText;
  52. import edu.vub.at.objects.ATTypeTag;
  53. import edu.vub.at.objects.coercion.NativeTypeTags;
  54. import edu.vub.at.objects.grammar.ATAssignmentSymbol;
  55. import edu.vub.at.objects.grammar.ATSymbol;
  56. import edu.vub.at.objects.mirrors.NATMirage;
  57. import edu.vub.at.objects.mirrors.NATMirrorRoot;
  58. import edu.vub.at.objects.mirrors.NativeClosure;
  59. import edu.vub.at.parser.NATParser;
  60. import edu.vub.at.util.logging.Logging;
  61. import java.util.Iterator;
  62. import java.util.LinkedList;
  63. import java.util.List;
  64. import java.util.Set;
  65. /**
  66. * The singleton instance of this class represents the lexical root of an actor.
  67. * Since this lexical root is constant (it cannot be modified) and contains no mutable fields,
  68. * it is possible to share a singleton instance of this class among all actors.
  69. * <p>
  70. * The lexical root is an object containing globally visible AmbientTalk native methods.
  71. * Such methods include control structures such as <tt>if:then:else:</tt>
  72. * but also object creation methods like <tt>object:</tt> and reflective constructs
  73. * like <tt>reflect:</tt>.
  74. *
  75. * Furthermore, the lexical root is also the root of the lexical parent hierarchy for objects.
  76. * This means that this object's mirror is responsible for ending recursive meta-level methods
  77. * such as <tt>lookup</tt> and <tt>assignField</tt>.
  78. * <p>
  79. * Like any class whose instances represent native AmbientTalk objects, this class is a subclass
  80. * of {@link NativeATObject}. This means that this class can use the typical protocol of native objects
  81. * to implement base-level AmbientTalk methods as Java methods whose name is prefixed with
  82. * <tt>base_</tt>.
  83. * <p>
  84. * Note that OBJLexicalRoot is a <i>sentinel</i> class. The actual object bound to the
  85. * lexical root of an actor (accessible via the field <tt>root</tt> will be a normal
  86. * AmbientTalk object whose lexical parent is this object.
  87. * The real, empty, root object is local to each actor and is mutable. The definitions
  88. * from the <tt>init.at</tt> file are added to that object.
  89. *
  90. * @author smostinc
  91. * @author tvcutsem
  92. */
  93. public final class OBJLexicalRoot extends NATByCopy {
  94. /**
  95. * The singleton instance of the sentinel lexical root
  96. */
  97. static public final OBJLexicalRoot _INSTANCE_ = new OBJLexicalRoot();
  98. /**
  99. * Constructor made private for singleton design pattern
  100. */
  101. private OBJLexicalRoot() { }
  102. /**
  103. * The lexical root has a special lexical parent object which ends the recursion
  104. * along the lexical lookup chain. These methods cannot be implemented
  105. * directly in this class because this class still implements useful
  106. * <tt>base_</tt> Java methods which have to be invoked by means of the
  107. * implementations defined in {@link NativeATObject}.
  108. */
  109. private final NativeATObject lexicalSentinel_ = new NATByCopy() {
  110. // METHODS THAT END THE LEXICAL LOOKUP CHAIN
  111. public ATObject impl_callAccessor(ATSymbol selector, ATTable arguments) throws InterpreterException {
  112. throw new XUndefinedSlot("variable access", selector.toString());
  113. }
  114. public ATObject impl_callMutator(ATAssignmentSymbol selector, ATTable arguments) throws InterpreterException {
  115. throw new XUnassignableField(selector.toString());
  116. }
  117. public ATObject impl_callField(ATSymbol selector) throws InterpreterException {
  118. throw new XUndefinedSlot("variable access", selector.toString());
  119. }
  120. public ATClosure impl_lookupAccessor(final ATSymbol selector) throws InterpreterException {
  121. throw new XUndefinedSlot("accessor", selector.toString());
  122. }
  123. public ATClosure impl_lookupMutator(ATAssignmentSymbol selector) throws InterpreterException {
  124. throw new XUnassignableField(selector.toString());
  125. }
  126. public NATText meta_print() throws InterpreterException {
  127. return NATText.atValue("lexicalrootsentinel");
  128. }
  129. };
  130. public ATObject impl_lexicalParent() {
  131. return lexicalSentinel_;
  132. }
  133. /* -----------------------
  134. * -- Primitive Methods --
  135. * ----------------------- */
  136. /* ===============================================================================
  137. * NOTE: the code below has been replaced by dedicated syntax and AST elements.
  138. * However, the skeleton of this code may still prove useful in the future, if
  139. * we ever plan to implement all base_ native methods as true AmbientTalk methods
  140. * (i.e. as PrimitiveMethod instances).
  141. * ===============================================================================
  142. */
  143. /*
  144. private static final AGSymbol _IMPORT_NAME_ = AGSymbol.jAlloc("import:");
  145. private static final AGSymbol _IMPORT_ALIAS_NAME_ = AGSymbol.jAlloc("import:alias:");
  146. private static final AGSymbol _IMPORT_EXCLUDE_NAME_ = AGSymbol.jAlloc("import:exclude:");
  147. private static final AGSymbol _IMPORT_ALIAS_EXCLUDE_NAME_ = AGSymbol.jAlloc("import:alias:exclude:");
  148. private static final AGSymbol _SRC_PARAM_ = AGSymbol.jAlloc("sourceObject");
  149. private static final AGSymbol _ALIAS_PARAM_ = AGSymbol.jAlloc("aliases");
  150. private static final AGSymbol _EXCLUDE_PARAM_ = AGSymbol.jAlloc("exclude");
  151. */
  152. /*protected static final PrimitiveMethod _PRIM_IMPORT_ = new PrimitiveMethod(_IMPORT_NAME_, NATTable.atValue(new ATObject[] { _SRC_PARAM_ })) {
  153. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  154. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  155. return performImport(sourceObject, ctx, new Hashtable(), OBJLexicalRoot.getDefaultExcludedSlots());
  156. }
  157. };*/
  158. /**
  159. * def import: sourceObject alias: [ `oldname -> `newname , ... ]
  160. */
  161. /*protected static final PrimitiveMethod _PRIM_IMPORT_ALIAS_ = new PrimitiveMethod(_IMPORT_ALIAS_NAME_, NATTable.atValue(new ATObject[] { _SRC_PARAM_, _ALIAS_PARAM_ })) {
  162. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  163. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  164. ATObject aliases = arguments.base_at(NATNumber.atValue(2));
  165. return performImport(sourceObject, ctx, preprocessAliases(aliases.base_asTable()), OBJLexicalRoot.getDefaultExcludedSlots());
  166. }
  167. };*/
  168. /**
  169. * def import: sourceObject excludes: [ `name1, `name2, ... ]
  170. */
  171. /*protected static final PrimitiveMethod _PRIM_IMPORT_EXCLUDE_ = new PrimitiveMethod(_IMPORT_EXCLUDE_NAME_, NATTable.atValue(new ATObject[] { _SRC_PARAM_, _EXCLUDE_PARAM_ })) {
  172. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  173. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  174. ATObject exclusions = arguments.base_at(NATNumber.atValue(2));
  175. return performImport(sourceObject, ctx, new Hashtable(), preprocessExcludes(exclusions.base_asTable()));
  176. }
  177. };*/
  178. /**
  179. * def import: sourceObject alias: [ `oldname -> `newname, ... ] excludes: [ `name1, `name2, ... ]
  180. */
  181. /*protected static final PrimitiveMethod _PRIM_IMPORT_ALIAS_EXCLUDE_ = new PrimitiveMethod(_IMPORT_ALIAS_EXCLUDE_NAME_,
  182. NATTable.atValue(new ATObject[] { _SRC_PARAM_, _ALIAS_PARAM_, _EXCLUDE_PARAM_ })) {
  183. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  184. ATObject sourceObject = arguments.base_at(NATNumber.ONE);
  185. ATObject aliases = arguments.base_at(NATNumber.atValue(2));
  186. ATObject exclusions = arguments.base_at(NATNumber.atValue(3));
  187. return performImport(sourceObject, ctx, preprocessAliases(aliases.base_asTable()), preprocessExcludes(exclusions.base_asTable()));
  188. }
  189. };*/
  190. /**
  191. * Invoked whenever a new true AmbientTalk object is created that should
  192. * represent the root. This gives the lexical root a chance to install its
  193. * primitive methods.
  194. */
  195. /*public static void initializeRoot(NATObject root) {
  196. try {
  197. // add import: native
  198. root.meta_addMethod(_PRIM_IMPORT_);
  199. // add import:alias: native
  200. root.meta_addMethod(_PRIM_IMPORT_ALIAS_);
  201. // add import:exclude: native
  202. root.meta_addMethod(_PRIM_IMPORT_EXCLUDE_);
  203. // add import:alias:exclude: native
  204. root.meta_addMethod(_PRIM_IMPORT_ALIAS_EXCLUDE_);
  205. } catch (InterpreterException e) {
  206. Logging.Init_LOG.fatal("Failed to initialize the root!", e);
  207. }
  208. }*/
  209. /* ----------------------
  210. * -- Global variables --
  211. * ---------------------- */
  212. /**
  213. * <tt>nil</tt> evaluates to the nil object, which is
  214. * the empty, dynamic parent of all AmbientTalk objects.
  215. */
  216. public ATNil base_nil() {
  217. return Evaluator.getNil();
  218. }
  219. /**
  220. * <tt>true</tt> evaluates to the unique boolean true object.
  221. */
  222. public ATBoolean base_true() {
  223. return NATBoolean._TRUE_;
  224. }
  225. /**
  226. * <tt>false</tt> evaluates to the unique boolean false object.
  227. */
  228. public ATBoolean base_false() {
  229. return NATBoolean._FALSE_;
  230. }
  231. /**
  232. * <tt>/</tt> evaluates to the global namespace. It is
  233. * simply an alias for <tt>lobby</tt>.
  234. * @see #base_lobby()
  235. */
  236. public ATObject base__opdiv_() {
  237. return base_lobby();
  238. }
  239. /**
  240. * <tt>lobby</tt> evaluates to the global namespace object.
  241. * For each <tt>name=path</tt> entry on AmbientTalk's
  242. * <i>object path</i>, the lobby object contains a slot
  243. * <tt>name</tt> bound to a namespace object bound to
  244. * the directory referred to by <tt>path</tt>.
  245. * <p>
  246. * Accessing the lobby allows loading in AmbientTalk source code
  247. * from external files.
  248. */
  249. public ATObject base_lobby() {
  250. return Evaluator.getLobbyNamespace();
  251. }
  252. /**
  253. * <tt>root</tt> evaluates to the global lexical scope object.
  254. * This is the top-level object in which the definitions of
  255. * the file <tt>at/init/init.at</tt> are evaluated. All code
  256. * is assumed to be "nested" in the lexical root, so all definitions
  257. * of this object are lexically accessible.
  258. */
  259. public ATObject base_root() {
  260. return Evaluator.getGlobalLexicalScope();
  261. }
  262. /**
  263. * <tt>jlobby</tt> evaluates to the Java namespace root. It is a
  264. * special object which is part of the symbiosis infrastructure of
  265. * AmbientTalk. <tt>jlobby</tt> acts like an object that has field
  266. * names that correspond to Java package names. By selecting fields
  267. * from this object, an appropriate Java package can be created
  268. * from which a Java class can be accessed. Only the Java classes
  269. * accessible in the Java classpath are accessible.
  270. *
  271. * Example:
  272. * <code>jlobby.java.util.Vector</code> evaluates to a reference to
  273. * the Java <tt>Vector</tt> class.
  274. */
  275. public ATObject base_jlobby() {
  276. return Evaluator.getJLobbyRoot();
  277. }
  278. /**
  279. * <tt>network</tt> evaluates to the unique network control object.
  280. * It is a simple native object with two methods:
  281. * <ul>
  282. * <li><tt>network.online()</tt> makes the interpreter go online. This allows
  283. * publications of local actors to be discovered by remote objects and vice versa.
  284. * <li><tt>network.offline()</tt> makes the interpreter go offline. All
  285. * remote references to remote objects will become disconnected.
  286. * </ul>
  287. */
  288. public ATObject base_network() {
  289. return OBJNetwork._INSTANCE_;
  290. }
  291. /**
  292. * <tt>defaultMirror</tt> evaluates to the default mirror on objects. This
  293. * is the mirror encapsulating the standard AmbientTalk object semantics.
  294. * That is, it is a mirror with similar behaviour as the mirror created by
  295. * executing: <code>reflect: (object: { ... })</code>.
  296. *
  297. * The default mirror is an object with a read-only <tt>base</tt> field
  298. * that signifies the base-level object of this mirror. The main purpose
  299. * of this object is to serve as a prototype whose methods can be overridden
  300. * by custom mirrors. The syntax:
  301. * <pre>
  302. * mirror: { ... }
  303. * </pre>
  304. * is syntactic sugar for:
  305. * <pre>
  306. * extend: defaultMirror with: { ... }
  307. * </pre>
  308. *
  309. * Note that the default mirror is typed with the <tt>/.at.types.Mirror</tt> type.
  310. */
  311. public ATObject base_defaultMirror() {
  312. return Evaluator.getMirrorRoot();
  313. }
  314. /* ------------------------
  315. * -- Control Structures --
  316. * ------------------------ */
  317. /**
  318. * The <tt>if:then:</tt> control structure. Usage:
  319. * <pre>if: cond then: consequent</pre>
  320. *
  321. * pseudo-implementation:
  322. * <pre>cond.ifTrue: consequent</pre>
  323. *
  324. * Note that the consequent parameter should be a <i>closure</i>, i.e.
  325. * the caller is responsible for delaying the evaluation of the consequent!
  326. *
  327. * @param cond a boolean object
  328. * @param consequent a closure containing the code to execute if the boolean is true
  329. * @return if <tt>cond</tt> is true, the value of applying the consequent, <tt>nil</tt> otherwise
  330. */
  331. public ATObject base_if_then_(ATBoolean cond, ATClosure consequent) throws InterpreterException {
  332. return cond.base_ifTrue_(consequent);
  333. }
  334. /**
  335. * The <tt>if:then:else:</tt> control structure. Usage:
  336. * <pre>if: cond then: consequent else: alternative</pre>
  337. *
  338. * pseudo-implementation:
  339. * <pre>cond.ifTrue: consequent ifFalse: alternative</pre>
  340. *
  341. * Note that the consequent and alternative parameters should be <i>closures</i>, i.e.
  342. * the caller is responsible for delaying the evaluation of these arguments!
  343. *
  344. * @param cond a boolean object
  345. * @param consequent a closure containing the code to execute if the boolean is true
  346. * @param alternative a closure containing the code to execute if the boolean is false
  347. * @return the value of consequent if the boolean is true, the value of the alternative otherwise.
  348. */
  349. public ATObject base_if_then_else_(ATBoolean cond, ATClosure consequent, ATClosure alternative) throws InterpreterException {
  350. return cond.base_ifTrue_ifFalse_(consequent, alternative);
  351. }
  352. /**
  353. * The <tt>while:do:</tt> control structure. Usage:
  354. * <pre>while: condition do: body</pre>
  355. *
  356. * pseudo-implementation:
  357. * <pre>condition.whileTrue: body</pre>
  358. *
  359. * Note that <i>both</i> the condition and the body should be <i>closures</i>, because
  360. * they represent pieces of code that have to be executed repeatedly. Because of traditional
  361. * syntax, novice programmers are inclined to make the mistake of writing, e.g.:
  362. * <pre>while: (i < 10) do: { i := i + 1 }</pre>
  363. * Which is wrong because the first parameter should evaluate to a closure that itself
  364. * returns a boolean value, not to a boolean value directly.
  365. *
  366. * @param condition a closure expected to return a boolean object
  367. * @param body a closure containing the code to execute as long as the condition closure returns true
  368. * @return if conditions is true at least once, the last value of body, <tt>nil</tt> otherwise.
  369. */
  370. public ATObject base_while_do_(ATClosure condition, ATClosure body) throws InterpreterException {
  371. return condition.base_whileTrue_(body);
  372. }
  373. /**
  374. * The <tt>foreach:in:</tt> control structure. Usage:
  375. *
  376. * <pre>foreach: body in: table</pre>
  377. *
  378. * pseudo-implementation:
  379. * <pre>table.each: body</pre>
  380. *
  381. * Example: <code>[1,2,3].each: { |i| system.println(i) }</code>
  382. *
  383. * @param body a one-arity closure that is to be applied to each element of the table
  384. * @param tab a table to apply the body closure to
  385. * @return <tt>nil</tt>, by default
  386. */
  387. public ATObject base_foreach_in_(ATClosure body, ATTable tab) throws InterpreterException {
  388. return tab.base_each_(body);
  389. }
  390. /**
  391. * The <tt>do:if:</tt> control structure. Usage:
  392. * <pre>do: body if: condition</pre>
  393. *
  394. * pseudo-implementation:
  395. * <pre>condition.ifTrue: body</pre>
  396. *
  397. * In Ruby, this kind of control structure is called a <i>statement modifier</i>.
  398. *
  399. * @param body a zero-argument closure to execute if the condition is true
  400. * @param condition a boolean value
  401. * @return the result of invoking body if the condition is true or nil if the
  402. * condition is false
  403. */
  404. public ATObject base_do_if_(ATClosure body, ATBoolean condition) throws InterpreterException {
  405. return condition.base_ifTrue_(body);
  406. }
  407. /**
  408. * The <tt>do:unless:</tt> control structure. Usage:
  409. * <pre>do: body unless: condition</pre>
  410. *
  411. * pseudo-implementation:
  412. * <pre>condition.ifFalse: body</pre>
  413. *
  414. * In Ruby, this kind of control structure is called a <i>statement modifier</i>.
  415. * Example: <code>do: { file.close() } unless: (nil == file)</code>
  416. *
  417. * @param body a zero-argument closure to execute only if the condition is false
  418. * @param condition a boolean value
  419. * @return the result of invoking body if the condition is false, nil otherwise
  420. */
  421. public ATObject base_do_unless_(ATClosure body, ATBoolean condition) throws InterpreterException {
  422. return condition.base_ifFalse_(body);
  423. }
  424. /**
  425. * The <tt>let:</tt> construct. Usage:
  426. * <pre>let: { |var := value| body }</pre>
  427. *
  428. * pseudo-implementation:
  429. * <pre>closure()</pre>
  430. *
  431. * <tt>let:</tt> allows for the easy creation of temporary local variables.
  432. * This construct should be used in conjunction with a closure that declares optional
  433. * parameters. Because the closure will be invoked with zero arguments, all of the
  434. * parameters will be given their corresponding default initial value. The parameters
  435. * are defined local to the closure's body.
  436. *
  437. * AmbientTalk's <tt>let:</tt> behaves like Scheme's <tt>let*</tt> and <tt>letrec</tt>,
  438. * i.e. the following is legal:
  439. * <pre>let: {
  440. * |var1 := value1,
  441. * var2 := var1,
  442. * var3 := { ... var3() ... }|
  443. * ...
  444. *}</pre>
  445. *
  446. * @param body a closure which is supposed to declare some optional parameters
  447. * @return the result of invoking the body closure
  448. */
  449. public ATObject base_let_(ATClosure body) throws InterpreterException {
  450. return body.base_apply(NATTable.EMPTY);
  451. }
  452. /* ------------------------------------------
  453. * -- Actor Creation and accessing Methods --
  454. * ------------------------------------------ */
  455. /**
  456. * The <tt>actor: closure</tt> construct.
  457. *
  458. * The semantics of actor creation is as follows:
  459. * <ul>
  460. * <li> Mandatory parameters to the block of initialization code are treated as lexically visible
  461. * variables that have to remain available in the new actor behaviour. Hence, these variables
  462. * are evaluated to values immediately at creation-time and parameter-passed to the new actor.
  463. * <li> The closure containing the initialization code is unpacked, its lexical scope is disregarded
  464. * and the unwrapped method is serialized and sent to the new actor, which can use it to
  465. * initialize his behaviour object.
  466. * <li>The creating actor waits for the created actor to spawn a new behaviour and to return a far
  467. * reference to this behaviour. From that point on, the creating actor can run in parallel with
  468. * the created actor, which only then evaluates the initialization code to initialize its behaviour.
  469. * </ul>
  470. *
  471. * @param closure the closure whose parameters define lexical fields to be copied and whose
  472. * method specifies the code of the new actor's behaviour object
  473. * @return a far reference to the behaviour of the new actor
  474. */
  475. public ATObject base_actor_(ATClosure closure) throws InterpreterException {
  476. ATMethod method = closure.base_method();
  477. ATObject copiedBindings;
  478. // if no variables were specified to pass along to the new actor, calculate the
  479. // set of free variables for the actor
  480. if (method.base_parameters().base_isEmpty().asNativeBoolean().javaValue) {
  481. // introduce a private scope object that will hold copies
  482. // of the lexically free variables of the actor
  483. copiedBindings = new NATObject(OBJLexicalRoot._INSTANCE_, new ATTypeTag[] { NativeTypeTags._ISOLATE_ });
  484. // calculate the set of free variables of the initialization expression
  485. Set freeVars = method.base_bodyExpression().impl_freeVariables();
  486. // add all these free variables manually as fields to a custom
  487. Iterator it = freeVars.iterator();
  488. while (it.hasNext()) {
  489. ATSymbol freeVar = (ATSymbol) it.next();
  490. // extra check to weed out special variables like "super" and variables available in the lexical root
  491. if (! (Evaluator.getGlobalLexicalScope().meta_respondsTo(freeVar).asNativeBoolean().javaValue
  492. || OBJLexicalRoot._INSTANCE_.meta_respondsTo(freeVar).asNativeBoolean().javaValue)) {
  493. // lookup the variable in the lexical scope
  494. try {
  495. ATClosure accessor = closure.base_context().base_lexicalScope().impl_lookup(freeVar);
  496. // only add the variable if it refers to a field, rather than to a method
  497. if (accessor instanceof NativeClosure.Accessor) {
  498. copiedBindings.meta_defineField(freeVar, accessor.base_apply(NATTable.EMPTY));
  499. }
  500. } catch(XUndefinedSlot exc) {
  501. // silently ignore lexically free variables which cannot be found
  502. // the assumption is that these variables will be bound by means of
  503. // import statements
  504. Logging.Actor_LOG.warn("Undefined lexically free var while constructing actor: "+exc.getFieldName());
  505. }
  506. }
  507. }
  508. } else {
  509. copiedBindings = Evaluator.evalMandatoryPars(
  510. method.base_parameters(),
  511. closure.base_context());
  512. }
  513. Packet serializedBindings = new Packet("actor-bindings", copiedBindings);
  514. Packet serializedInitCode = new Packet("actor-initcode", method);
  515. return ELVirtualMachine.currentVM().createActor(serializedBindings, serializedInitCode);
  516. }
  517. /**
  518. * <tt>reflectOnActor</tt> evaluates to the mirror on the actor executing this code.
  519. * The actor mirror is an object whose behaviour is consulted for operations
  520. * such as creating and sending asynchronous messages or creating mirrors on
  521. * other objects. It can be replaced by a custom mirror by means of the actor
  522. * mirror's <tt>getExplicitActorMirror</tt> method.
  523. */
  524. public ATActorMirror base_reflectOnActor() throws InterpreterException {
  525. return ELActor.currentActor().getImplicitActorMirror().base_getExplicitActorMirror();
  526. }
  527. /**
  528. * The <tt>export: object as: topic</tt> construct. Pseudo-implementation:
  529. * <pre>actor.provide(topic, object)</pre>
  530. *
  531. * This construct enables the given object to become discoverable by objects
  532. * in other actors by means of the topic type.
  533. *
  534. * @param object the object to export to remote actors' objects
  535. * @param topic a type denoting the abstract 'publication topic' for this object's publication
  536. * @return a publication object whose <tt>cancel</tt> method can be used to cancel the publication.
  537. */
  538. public ATObject base_export_as_(ATObject object, ATTypeTag topic) throws InterpreterException {
  539. return ELActor.currentActor().getImplicitActorMirror().base_provide(topic, object);
  540. }
  541. /**
  542. * The <tt>when: topic discovered: handler</tt> construct. Pseudo-implementation:
  543. * <pre>actor.require(topic, handler, false)</pre>
  544. *
  545. * When an object is exported by <i>another</i> actor under topic, this construct triggers
  546. * the given code, passing a reference to the exported object as argument to the closure.
  547. *
  548. * Once the code block has run once, it will not be triggered again.
  549. *
  550. * @param topic the abstract 'subscription topic' used to find an exported object
  551. * @param handler a one-argument closure to apply to a discovered exported object
  552. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel the subscription,
  553. * such that the handler will no longer be invoked. Beware, however, that at the time the
  554. * subscription is cancelled, a request to apply the closure may already have been scheduled
  555. * for execution by the current actor. This request is not cancelled by invoking the <tt>cancel</tt> method.
  556. */
  557. public ATObject base_when_discovered_(ATTypeTag topic, ATClosure handler) throws InterpreterException {
  558. return ELActor.currentActor().getImplicitActorMirror().base_require(topic, handler, NATBoolean._FALSE_);
  559. }
  560. /**
  561. * The <tt>whenever: topic discovered: handler</tt> construct. Pseudo-implementation:
  562. * <pre>actor.require(topic, handler, true)</pre>
  563. *
  564. * When an object is exported by <i>another</i> actor under topic, this construct triggers
  565. * the given code, passing a reference to the exported object as argument to the closure.
  566. *
  567. * The code block can be fired multiple times upon discovering multiple exported objects.
  568. * To stop the block from triggering upon new publications, it must be explicitly cancelled
  569. *
  570. * @param topic the abstract 'subscription topic' used to find an exported object
  571. * @param handler a one-argument closure to apply to any discovered exported object
  572. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel the subscription,
  573. * such that the handler will no longer be invoked. Beware, however, that at the time the
  574. * subscription is cancelled, a request to apply the closure may already have been scheduled
  575. * for execution by the current actor. This request is not cancelled by invoking the <tt>cancel</tt> method.
  576. */
  577. public ATObject base_whenever_discovered_(ATTypeTag topic, ATClosure handler) throws InterpreterException {
  578. return ELActor.currentActor().getImplicitActorMirror().base_require(topic, handler, NATBoolean._TRUE_);
  579. }
  580. /**
  581. * The <tt>whenever: farReference disconnected: listener</tt> construct.
  582. * When the far reference is broken due to network disconnections, triggers the zero-arity listener
  583. * closure. It is possible to register listeners on local far references. These may trigger if the
  584. * local actor takes its object offline. In this case, these listeners will trigger as well.
  585. *
  586. * @param farReference a native far reference
  587. * @param listener a zero-arity closure to invoke if the far reference becomes disconnected
  588. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel future
  589. * notifications of the listener.
  590. */
  591. public ATObject base_whenever_disconnected_(ATFarReference farReference, ATClosure listener) throws InterpreterException {
  592. farReference.asNativeFarReference().addDisconnectionListener(listener);
  593. return new NATFarReference.NATDisconnectionSubscription(farReference.asNativeFarReference(), listener);
  594. }
  595. /**
  596. * The <tt>whenever: farReference reconnected: listener</tt> construct.
  597. * When the remote reference is reinstated after a network disconnection, trigger the zero-arity
  598. * listener. Although it is allowed to register these listeners on local far references,
  599. * these are normally not invoked because the only possibility for a local far ref to become
  600. * disconnected is because the object was taken offline, and this is a permanent disconnect.
  601. *
  602. * @param farReference a native far reference
  603. * @param listener a zero-arity closure to invoke if the far reference becomes reconnected
  604. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel future
  605. * notifications of the listener.
  606. */
  607. public ATObject base_whenever_reconnected_(ATFarReference farReference, ATClosure listener) throws InterpreterException {
  608. farReference.asNativeFarReference().addReconnectionListener(listener);
  609. return new NATFarReference.NATReconnectionSubscription(farReference.asNativeFarReference(), listener);
  610. }
  611. /**
  612. * The <tt>when: farReference takenOffline:</tt> construct.
  613. * When the (remote/local) far reference is broken because the object referenced was
  614. * taken offline, trigger the code.
  615. *
  616. * @param farReference a native far reference
  617. * @param listener a zero-arity closure to invoke if the referenced object has been taken offline.
  618. * @return a subscription object whose <tt>cancel</tt> method can be used to cancel future
  619. * notifications of the listener.
  620. */
  621. public ATObject base_when_takenOffline_(ATFarReference farReference, ATClosure listener) throws InterpreterException {
  622. farReference.asNativeFarReference().addTakenOfflineListener(listener);
  623. return new NATFarReference.NATExpiredSubscription(farReference.asNativeFarReference(), listener);
  624. }
  625. /**
  626. * The <tt>retract: farReference</tt> construct.
  627. * Retracts all currently unsent messages from the far reference's outbox.
  628. * This has the side effect that the returned messages will <b>not</b> be sent
  629. * automatically anymore, the programmer is responsible to explicitly resend
  630. * all messages that were retracted but still need to be sent.
  631. *
  632. * Note that the returned messages are copies of the original.
  633. * @param farReference the far reference of which to retract outgoing message sends
  634. * @return a table containing copies of all messages that were sent to this far reference, but
  635. * not yet transmitted by the far reference to its referent.
  636. */
  637. public ATTable base_retract_(ATFarReference farReference) throws InterpreterException {
  638. return farReference.meta_retractUnsentMessages();
  639. }
  640. /* -----------------------------
  641. * -- Object Creation Methods --
  642. * ----------------------------- */
  643. /**
  644. * The <tt>object:</tt> object creation primitive.
  645. * This construct creates a new AmbientTalk object where:
  646. * <ul>
  647. * <li>The object is initialized with the <i>code</i> of the argument closure.
  648. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  649. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  650. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  651. * <li>The object's <b>types</b> is <tt>[]</tt> (i.e. it has no types).
  652. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  653. * with a 'native' metaobject protocol).
  654. * </ul>
  655. *
  656. * Example: <code>object: { def x := 1; }</code>
  657. * <p>
  658. * Pseudo-implementation:
  659. * <pre>object: code childOf: nil extends: false taggedAs: [] mirroredBy: defaultMirror</pre>
  660. *
  661. * The closure used to initialize the object may contain formal parameters. The closure
  662. * will always be invoked with <i>its own mandatory formal parameters</i>. E.g., a closure
  663. * <code>{ |x| nil }</code> is invoked as <code>{ |x| nil }(x)</code>. The net effect of this
  664. * mechanic is that if <tt>x</tt> is a lexically visible variable at the object-creation
  665. * site, the value of the variable will be copied into a copy with the same name which
  666. * resides in the newly created object. This mechanic is primarily useful for copying surrounding
  667. * variables within the object, e.g. for isolates which lose access to their surrounding scope.
  668. * <p>
  669. * Also, if the closure has optional parameters, they will always be triggered.
  670. * The expressions to initialize the formal parameters are <i>evaluated</i>
  671. * in the context of the closure's lexical scope but are <i>added</i> to the newly created object.
  672. *
  673. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
  674. * @return a new AmbientTalk object with the properties defined above.
  675. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  676. */
  677. public ATObject base_object_(ATClosure code) throws InterpreterException {
  678. return base_object_childOf_extends_taggedAs_mirroredBy_(
  679. code,
  680. Evaluator.getNil(),
  681. NATBoolean._FALSE_ /* SHARES-A link */,
  682. NATTable.EMPTY,
  683. base_defaultMirror());
  684. }
  685. /**
  686. * The <tt>extend:with:</tt> object creation primitive.
  687. * This construct creates a new AmbientTalk object where:
  688. * <ul>
  689. * <li>The object is initialized with the <i>code</i> of the argument closure.
  690. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  691. * <li>The object's <b>dynamic parent</b> is the argument object.
  692. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  693. * <li>The object's <b>types</b> is <tt>[]</tt> (i.e. it has no types).
  694. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  695. * with a 'native' metaobject protocol).
  696. * </ul>
  697. *
  698. * Example: <code>extend: parent with: { def x := 1; }</code>
  699. * <p>
  700. * Pseudo-implementation:
  701. * <pre>object: code childOf: parent extends: true taggedAs: [] mirroredBy: defaultMirror</pre>
  702. *
  703. * @param parent the dynamic parent object of the newly created object.
  704. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
  705. * @return a new AmbientTalk object with the properties defined above.
  706. * @see #base_object_(ATClosure)
  707. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  708. */
  709. public ATObject base_extend_with_(ATObject parent, ATClosure code) throws InterpreterException {
  710. return base_object_childOf_extends_taggedAs_mirroredBy_(
  711. code,
  712. parent,
  713. NATBoolean._TRUE_ /* IS-A link */,
  714. NATTable.EMPTY,
  715. base_defaultMirror());
  716. }
  717. /**
  718. * The <tt>extend:with:taggedAs:</tt> object creation primitive.
  719. * This construct creates a new AmbientTalk object where:
  720. * <ul>
  721. * <li>The object is initialized with the <i>code</i> of the argument closure.
  722. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  723. * <li>The object's <b>dynamic parent</b> is the argument object.
  724. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  725. * <li>The object's <b>types</b> are initialized to the argument types table.
  726. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  727. * with a 'native' metaobject protocol).
  728. * </ul>
  729. *
  730. * Example: <code>extend: parent with: { def x := 1; } taggedAs: [foo,bar]</code>
  731. * <p>
  732. * Pseudo-implementation:
  733. * <pre>object: code childOf: parent extends: true taggedAs: types mirroredBy: defaultMirror</pre>
  734. *
  735. * @param parent the dynamic parent object of the newly created object.
  736. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  737. * @param types a table of types with which to type the newly created object.
  738. * @return a new AmbientTalk object with the properties defined above.
  739. * @see #base_object_(ATClosure)
  740. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  741. */
  742. public ATObject base_extend_with_taggedAs_(ATObject parent, ATClosure code, ATTable types) throws InterpreterException {
  743. return base_object_childOf_extends_taggedAs_mirroredBy_(
  744. code,
  745. parent,
  746. NATBoolean._TRUE_ /* IS-A link */,
  747. types,
  748. base_defaultMirror());
  749. }
  750. /**
  751. * The <tt>extend:with:mirroredBy:</tt> object creation primitive.
  752. * This construct creates a new AmbientTalk object where:
  753. * <ul>
  754. * <li>The object is initialized with the <i>code</i> of the argument closure.
  755. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  756. * <li>The object's <b>dynamic parent</b> is the argument object.
  757. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  758. * <li>The object's <b>types</b> are set to <tt>[]</tt> (i.e. the object has no types).
  759. * <li>The object's <b>mirror</b> is the given mirror. This means that this object is a <i>mirage</i>
  760. * whose metaobject protocol is entirely dictated by the given mirror.
  761. * </ul>
  762. *
  763. * Example: <code>extend: parent with: { def x := 1; } mirroredBy: (mirror: {...})</code>
  764. * <p>
  765. * Pseudo-implementation:
  766. * <pre>object: code childOf: parent extends: true taggedAs: [] mirroredBy: mirror</pre>
  767. *
  768. * @param parent the dynamic parent object of the newly created object.
  769. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  770. * @param mirror the mirror of the newly created mirage object.
  771. * @return a new AmbientTalk object with the properties defined above.
  772. * @see #base_object_(ATClosure)
  773. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  774. */
  775. public ATObject base_extend_with_mirroredBy_(ATObject parent, ATClosure code, ATObject mirror) throws InterpreterException {
  776. return base_object_childOf_extends_taggedAs_mirroredBy_(
  777. code,
  778. parent,
  779. NATBoolean._TRUE_ /* IS-A link */,
  780. NATTable.EMPTY,
  781. mirror);
  782. }
  783. /**
  784. * The <tt>extend:with:taggedAs:mirroredBy:</tt> object creation primitive.
  785. * This construct creates a new AmbientTalk object where:
  786. * <ul>
  787. * <li>The object is initialized with the <i>code</i> of the argument closure.
  788. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  789. * <li>The object's <b>dynamic parent</b> is the argument object.
  790. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  791. * <li>The object's <b>types</b> are initialized to the argument types table.
  792. * <li>The object's <b>mirror</b> is the given argument mirror. This means that the newly
  793. * created object is a <i>mirage</i> whose metaobject protocol is dictated by the given mirror.
  794. * </ul>
  795. *
  796. * Example: <code>extend: parent with: { def x := 1; } taggedAs: [foo,bar] mirroredBy: mirror</code>
  797. * <p>
  798. * Pseudo-implementation:
  799. * <pre>object: code childOf: parent extends: true taggedAs: types mirroredBy: mirror</pre>
  800. *
  801. * @param parent the dynamic parent object of the newly created object.
  802. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  803. * @param types a table of types with which to type the newly created object.
  804. * @param the mirror object of the newly created mirage object.
  805. * @return a new AmbientTalk object with the properties defined above.
  806. * @see #base_object_(ATClosure)
  807. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  808. */
  809. public ATObject base_extend_with_taggedAs_mirroredBy_(ATObject parent, ATClosure code, ATTable types, ATObject mirror) throws InterpreterException {
  810. return base_object_childOf_extends_taggedAs_mirroredBy_(
  811. code,
  812. parent,
  813. NATBoolean._TRUE_ /* IS-A link */,
  814. types,
  815. mirror);
  816. }
  817. /**
  818. * The <tt>share:with:</tt> object creation primitive.
  819. * This construct creates a new AmbientTalk object where:
  820. * <ul>
  821. * <li>The object is initialized with the <i>code</i> of the argument closure.
  822. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  823. * <li>The object's <b>dynamic parent</b> is the argument object.
  824. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  825. * <li>The object's <b>types</b> is <tt>[]</tt> (i.e. it has no types).
  826. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  827. * with a 'native' metaobject protocol).
  828. * </ul>
  829. *
  830. * Example: <code>share: parent with: { def x := 1; }</code>
  831. * <p>
  832. * Pseudo-implementation:
  833. * <pre>object: code childOf: parent extends: false taggedAs: [] mirroredBy: defaultMirror</pre>
  834. *
  835. * @param parent the dynamic parent object of the newly created object.
  836. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
  837. * @return a new AmbientTalk object with the properties defined above.
  838. * @see #base_object_(ATClosure)
  839. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  840. */
  841. public ATObject base_share_with_(ATObject parent, ATClosure code) throws InterpreterException {
  842. return base_object_childOf_extends_taggedAs_mirroredBy_(
  843. code,
  844. parent,
  845. NATBoolean._FALSE_ /* SHARES-A link */,
  846. NATTable.EMPTY,
  847. base_defaultMirror());
  848. }
  849. /**
  850. * The <tt>share:with:taggedAs:</tt> object creation primitive.
  851. * This construct creates a new AmbientTalk object where:
  852. * <ul>
  853. * <li>The object is initialized with the <i>code</i> of the argument closure.
  854. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  855. * <li>The object's <b>dynamic parent</b> is the argument object.
  856. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  857. * <li>The object's <b>types</b> are initialized to the argument types table.
  858. * <li>The object's <b>mirror</b> is the <tt>defaultMirror</tt> on objects (i.e. it is an object
  859. * with a 'native' metaobject protocol).
  860. * </ul>
  861. *
  862. * Example: <code>share: parent with: { def x := 1; } taggedAs: [foo,bar]</code>
  863. * <p>
  864. * Pseudo-implementation:
  865. * <pre>object: code childOf: parent extends: false taggedAs: types mirroredBy: defaultMirror</pre>
  866. *
  867. * @param parent the dynamic parent object of the newly created object.
  868. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  869. * @param types a table of types with which to type the newly created object.
  870. * @return a new AmbientTalk object with the properties defined above.
  871. * @see #base_object_(ATClosure)
  872. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  873. */
  874. public ATObject base_share_with_taggedAs_(ATObject parent, ATClosure code, ATTable types) throws InterpreterException {
  875. return base_object_childOf_extends_taggedAs_mirroredBy_(
  876. code,
  877. parent,
  878. NATBoolean._FALSE_ /* SHARES-A link */,
  879. types,
  880. base_defaultMirror());
  881. }
  882. /**
  883. * The <tt>share:with:mirroredBy:</tt> object creation primitive.
  884. * This construct creates a new AmbientTalk object where:
  885. * <ul>
  886. * <li>The object is initialized with the <i>code</i> of the argument closure.
  887. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  888. * <li>The object's <b>dynamic parent</b> is the argument object.
  889. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  890. * <li>The object's <b>types</b> are set to <tt>[]</tt> (i.e. the object has no types).
  891. * <li>The object's <b>mirror</b> is the given mirror. This means that this object is a <i>mirage</i>
  892. * whose metaobject protocol is entirely dictated by the given mirror.
  893. * </ul>
  894. *
  895. * Example: <code>share: parent with: { def x := 1; } mirroredBy: (mirror: {...})</code>
  896. * <p>
  897. * Pseudo-implementation:
  898. * <pre>object: code childOf: parent extends: false taggedAs: [] mirroredBy: mirror</pre>
  899. *
  900. * @param parent the dynamic parent object of the newly created object.
  901. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  902. * @param mirror the mirror of the newly created mirage object.
  903. * @return a new AmbientTalk object with the properties defined above.
  904. * @see #base_object_(ATClosure)
  905. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  906. */
  907. public ATObject base_share_with_mirroredBy_(ATObject parent, ATClosure code, ATObject mirror) throws InterpreterException {
  908. return base_object_childOf_extends_taggedAs_mirroredBy_(
  909. code,
  910. parent,
  911. NATBoolean._FALSE_ /* SHARES-A link */,
  912. NATTable.EMPTY,
  913. mirror);
  914. }
  915. /**
  916. * The <tt>share:with:taggedAs:mirroredBy:</tt> object creation primitive.
  917. * This construct creates a new AmbientTalk object where:
  918. * <ul>
  919. * <li>The object is initialized with the <i>code</i> of the argument closure.
  920. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  921. * <li>The object's <b>dynamic parent</b> is the argument object.
  922. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  923. * <li>The object's <b>types</b> are initialized to the argument types table.
  924. * <li>The object's <b>mirror</b> is the given argument mirror. This means that the newly
  925. * created object is a <i>mirage</i> whose metaobject protocol is dictated by the given mirror.
  926. * </ul>
  927. *
  928. * Example: <code>share: parent with: { def x := 1; } taggedAs: [foo,bar] mirroredBy: mirror</code>
  929. * <p>
  930. * Pseudo-implementation:
  931. * <pre>object: code childOf: parent extends: false taggedAs: types mirroredBy: mirror</pre>
  932. *
  933. * @param parent the dynamic parent object of the newly created object.
  934. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  935. * @param types a table of types with which to type the newly created object.
  936. * @param mirror the mirror object of the newly created mirage object.
  937. * @return a new AmbientTalk object with the properties defined above.
  938. * @see #base_object_(ATClosure)
  939. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  940. */
  941. public ATObject base_share_with_taggedAs_mirroredBy_(ATObject parent, ATClosure code, ATTable types, ATObject mirror) throws InterpreterException {
  942. return base_object_childOf_extends_taggedAs_mirroredBy_(
  943. code,
  944. parent,
  945. NATBoolean._FALSE_ /* SHARES-A link */,
  946. types,
  947. mirror);
  948. }
  949. /**
  950. * The <tt>object:taggedAs:</tt> object creation primitive.
  951. * This construct creates a new AmbientTalk object where:
  952. * <ul>
  953. * <li>The object is initialized with the <i>code</i> of the argument closure.
  954. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  955. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  956. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  957. * <li>The object's <b>types</b> are initialized to the argument types table.
  958. * <li>The object's <b>mirror</b> is <tt>defaultMirror</tt> (i.e. the object has the
  959. * default metaobject protocol).
  960. * </ul>
  961. *
  962. * Example: <code>object: { def x := 1; } taggedAs: [foo,bar]</code>
  963. * <p>
  964. * Pseudo-implementation:
  965. * <pre>object: code childOf: nil extends: false taggedAs: types mirroredBy: defaultMirror</pre>
  966. *
  967. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  968. * @param types a table of type tags with which to type the newly created object.
  969. * @return a new AmbientTalk object with the properties defined above.
  970. * @see #base_object_(ATClosure)
  971. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  972. */
  973. public ATObject base_object_taggedAs_(ATClosure code, ATTable types) throws InterpreterException {
  974. return base_object_childOf_extends_taggedAs_mirroredBy_(
  975. code,
  976. Evaluator.getNil(),
  977. NATBoolean._FALSE_ /* SHARES-A link */,
  978. types,
  979. base_defaultMirror());
  980. }
  981. /**
  982. * The <tt>isolate:</tt> object creation primitive.
  983. * This construct creates a new AmbientTalk object where:
  984. * <ul>
  985. * <li>The object is initialized with the <i>code</i> of the argument closure.
  986. * <li>The object's <b>lexical parent</b> is initialized to the lexical scope of the argument closure,
  987. * but because it is typed as an isolate, the parent link is replaced by a link to the lexical <tt>root</tt>.
  988. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  989. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  990. * <li>The object's <b>types</b> are initialized to <tt>[/.at.types.Isolate]</tt>, i.e.
  991. * the object is typed as an isolate.
  992. * <li>The object's <b>mirror</b> is <tt>defaultMirror</tt> (i.e. the object has the
  993. * default metaobject protocol).
  994. * </ul>
  995. *
  996. * Example: <code>isolate: { def x := 1; }</code>
  997. * <p>
  998. * Pseudo-implementation:
  999. * <pre>object: code childOf: nil extends: false taggedAs: [/.at.types.Isolate] mirroredBy: defaultMirror</pre>
  1000. *
  1001. * An isolate is an object without a proper lexical parent. It is as if the isolate is always
  1002. * defined at top-level. However, lexically visible variables can be retained by copying them into the isolate
  1003. * by means of formal parameters to the argument closure. Isolate objects are passed by-copy during
  1004. * inter-actor parameter and result passing.
  1005. *
  1006. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  1007. * @return a new AmbientTalk object with the properties defined above.
  1008. * @see #base_object_(ATClosure)
  1009. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  1010. */
  1011. public ATObject base_isolate_(ATClosure code) throws InterpreterException {
  1012. return base_object_taggedAs_(code, NATTable.of(NativeTypeTags._ISOLATE_));
  1013. }
  1014. /**
  1015. * The <tt>mirror:</tt> object creation primitive.
  1016. * This construct creates a new AmbientTalk object where:
  1017. * <ul>
  1018. * <li>The object is initialized with the <i>code</i> of the argument closure.
  1019. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  1020. * <li>The object's <b>dynamic parent</b> is <tt>defaultMirror</tt>.
  1021. * <li>The object's <b>parent type</b> is <b>IS-A</b> (i.e. it is an extension of its parent).
  1022. * <li>The object's <b>types</b> are initialized to <tt>[]</tt>.
  1023. * <li>The object's <b>mirror</b> is <tt>defaultMirror</tt> (i.e. the object has the
  1024. * default metaobject protocol).
  1025. * </ul>
  1026. *
  1027. * Example: <code>mirror: { def x := 1; }</code>
  1028. * <p>
  1029. * Pseudo-implementation:
  1030. * <pre>object: code childOf: defaultMirror extends: true taggedAs: [] mirroredBy: defaultMirror</pre>
  1031. *
  1032. * This construct is mere syntactic sugar for creating an extension of the default mirror root.
  1033. * It follows that AmbientTalk mirrors are plain AmbientTalk objects. They simply need to implement
  1034. * the entire metaobject protocol, and the easiest means to achieve this is by extending the default mirror.
  1035. * Also keep in mind that the mirror is an extension object. This is important because the default
  1036. * mirror has <i>state</i>, being the <tt>base</tt> field that points to the base-level object
  1037. * being mirrorred. Hence, always make sure that, if overriding <tt>init</tt>, the parent's
  1038. * <tt>init</tt> method is invoked with the appropriate <tt>base</tt> value.
  1039. *
  1040. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  1041. * @return a new AmbientTalk object with the properties defined above.
  1042. * @see #base_object_(ATClosure)
  1043. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  1044. */
  1045. public ATObject base_mirror_(ATClosure code) throws InterpreterException {
  1046. return base_object_childOf_extends_taggedAs_mirroredBy_(
  1047. code,
  1048. base_defaultMirror(),
  1049. NATBoolean._TRUE_ /* IS-A link */,
  1050. NATTable.EMPTY,
  1051. base_defaultMirror());
  1052. }
  1053. /**
  1054. * The <tt>object:mirroredBy:</tt> object creation primitive.
  1055. * This construct creates a new AmbientTalk object where:
  1056. * <ul>
  1057. * <li>The object is initialized with the <i>code</i> of the argument closure.
  1058. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  1059. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  1060. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  1061. * <li>The object's <b>types</b> are set to <tt>[]</tt> (i.e. the object has no types).
  1062. * <li>The object's <b>mirror</b> is the given mirror. This means that this object is a <i>mirage</i>
  1063. * whose metaobject protocol is entirely dictated by the given mirror.
  1064. * </ul>
  1065. *
  1066. * Example: <code>object: { def x := 1; } mirroredBy: (mirror: {...})</code>
  1067. * <p>
  1068. * Pseudo-implementation:
  1069. * <pre>object: code childOf: nil extends: false taggedAs: [] mirroredBy: mirror</pre>
  1070. *
  1071. * This primitive allows the construction of so-called <i>mirage</i> objects which are
  1072. * AmbientTalk objects whose metaobject protocol behaviour is dictated by a custom mirror
  1073. * object.
  1074. *
  1075. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  1076. * @param mirror the mirror prototype of the newly created mirage object, or a constructor closure.
  1077. * @return a new AmbientTalk object with the properties defined above.
  1078. * @see #base_object_(ATClosure)
  1079. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  1080. */
  1081. public ATObject base_object_mirroredBy_(ATClosure code, ATObject mirror) throws InterpreterException {
  1082. return base_object_childOf_extends_taggedAs_mirroredBy_(
  1083. code,
  1084. Evaluator.getNil(),
  1085. NATBoolean._FALSE_ /* SHARES-A link */,
  1086. NATTable.EMPTY,
  1087. mirror);
  1088. }
  1089. /**
  1090. * The <tt>object:taggedAs:mirroredBy:</tt> object creation primitive.
  1091. * This construct creates a new AmbientTalk object where:
  1092. * <ul>
  1093. * <li>The object is initialized with the <i>code</i> of the argument closure.
  1094. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  1095. * <li>The object's <b>dynamic parent</b> is <tt>nil</tt>.
  1096. * <li>The object's <b>parent type</b> is <b>SHARES-A</b> (i.e. it is not an extension of its parent).
  1097. * <li>The object's <b>types</b> are set to the argument types.
  1098. * <li>The object's <b>mirror</b> is a clone of given mirror, or the return value of a constructor closure. This means that this object is a <i>mirage</i>
  1099. * whose metaobject protocol is entirely dictated by the given mirror.
  1100. * </ul>
  1101. *
  1102. * Example: <code>object: { def x := 1; } taggedAs: [foo,bar] mirroredBy: (mirror: {...})</code>
  1103. * <p>
  1104. * Pseudo-implementation:
  1105. * <pre>object: code childOf: nil extends: false taggedAs: types mirroredBy: mirror</pre>
  1106. *
  1107. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  1108. * @param types a table of types with which to type the newly created object.
  1109. * @param mirror the mirror of the newly created mirage object.
  1110. * @return a new AmbientTalk object with the properties defined above.
  1111. * @see #base_object_(ATClosure)
  1112. * @see #base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure, ATObject, ATBoolean, ATTable, ATObject)
  1113. */
  1114. public ATObject base_object_taggedAs_mirroredBy_(ATClosure code, ATTable types, ATObject mirror) throws InterpreterException {
  1115. return base_object_childOf_extends_taggedAs_mirroredBy_(
  1116. code,
  1117. Evaluator.getNil(),
  1118. NATBoolean._FALSE_ /* SHARES-A link */,
  1119. types,
  1120. mirror);
  1121. }
  1122. /**
  1123. * The <tt>object:childOf:extends:taggedAs:mirroredBy:</tt> object creation primitive.
  1124. * This construct creates a new AmbientTalk object where:
  1125. * <ul>
  1126. * <li>The object is initialized with the <i>code</i> of the argument closure.
  1127. * <li>The object's <b>lexical parent</b> is the lexical scope of the argument closure.
  1128. * <li>The object's <b>dynamic parent</b> is the argument parent object.
  1129. * <li>The object's <b>parent type</b> is the argument parent type, true being <tt>IS-A</tt>, false being <tt>SHARES-A</tt>.
  1130. * <li>The object's <b>types</b> are set to the argument types table.
  1131. * <li>The object's <b>mirror</b> is a clone of the given mirror prototype,
  1132. * or the return value of the given constructor closure. This means that this object is a <i>mirage</i>
  1133. * whose metaobject protocol is entirely dictated by the given mirror, if the mirror is not <tt>defaultMirror</tt>.
  1134. * </ul>
  1135. *
  1136. * Example: <code>object: { def x := 1; } childOf: parent extends: true taggedAs: [foo,bar] mirroredBy: mirror</code>
  1137. * <p>
  1138. * Pseudo-implementation:
  1139. * <pre>let o = OBJECT(parent,code.lexicalParent,parentType,types);
  1140. * code.applyInScope(o, code.mandatoryPars);
  1141. * o
  1142. * </pre>
  1143. *
  1144. * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent.
  1145. * @param parent the dynamic parent object of the newly created object.
  1146. * @param parentType a boolean denoting whether or not the object is an extension of its dynamic parent.
  1147. * @param types a table of types with which the newly created object should be typed.
  1148. * @param mirror the mirror of the newly created object.
  1149. * @return a new AmbientTalk object with the properties defined above.
  1150. * @see #base_object_(ATClosure) for more information about the properties of the passed closure.
  1151. */
  1152. public ATObject base_object_childOf_extends_taggedAs_mirroredBy_(ATClosure code, ATObject parent, ATBoolean parentType, ATTable types, ATObject mirror) throws InterpreterException {
  1153. ATTypeTag[] typeArray = NATTypeTag.toTypeTagArray(types);
  1154. boolean parentRelation = parentType.asNativeBoolean().javaValue;
  1155. NATObject newObject;
  1156. // if the mirror is a 'default' mirror...
  1157. if (mirror instanceof NATMirrorRoot) {
  1158. // then create a native object
  1159. newObject = new NATObject(parent, // dynamic parent
  1160. code.base_context().base_lexicalScope(), // lexical parent
  1161. parentRelation, // IS-A or SHARES-A
  1162. typeArray); // initial types
  1163. } else {
  1164. // else create a mirage mirrored by the custom mirror
  1165. newObject = NATMirage.createMirage(code, parent, parentRelation, typeArray, mirror);
  1166. }
  1167. newObject.initializeWithCode(code);
  1168. return newObject;
  1169. }
  1170. /**
  1171. * The <tt>reflect:</tt> construct. This construct returns a mirror on an object.
  1172. *
  1173. * pseudo-implementation:
  1174. * <pre>actor.createMirror(reflectee)</pre>
  1175. *
  1176. * An actor can change its default mirror creation policy by installing a new
  1177. * actor protocol that overrides <tt>createMirror</tt>.
  1178. *
  1179. * @param reflectee the object to reflect upon
  1180. * @return a mirror reflecting on the given object
  1181. * @see ATActorMirror#base_createMirror(ATObject) for the details about mirror creation on objects.
  1182. */
  1183. public ATObject base_reflect_(ATObject reflectee) throws InterpreterException {
  1184. return base_reflectOnActor().base_createMirror(reflectee);
  1185. }
  1186. /**
  1187. * The <tt>clone:</tt> language construct. Returns a clone of an object.
  1188. *
  1189. * Care must be taken when cloning a mirror. If a mirror would simply be cloned
  1190. * using the regular cloning semantics, its base field would be <b>shared</b>
  1191. * between the clone and the original mirror (because of shallow copy semantics).
  1192. * However, the base object will still be tied to the original mirror, not the clone.
  1193. * Therefore, the clone: operator is implemented as follows:
  1194. *
  1195. * <pre>def clone: obj {
  1196. * if: (is: obj taggedAs: Mirror) then: {
  1197. * reflect: (clone: obj.base)
  1198. * } else: {
  1199. * (reflect: obj).clone()
  1200. * }
  1201. * }</pre>
  1202. *
  1203. * The default cloning semantics ensures that all fields of the object are shallow-copied.
  1204. * Because methods are immutable, a clone and its original object share their method dictionary,
  1205. * but whenever a change is made to the dictionary, the changer creates a local copy of the
  1206. * dictionary as to not modify any clones. Hence, each object is truly stand-alone and independent
  1207. * of its clone.
  1208. *
  1209. * @param original the object to copy
  1210. * @return a clone of the given object (default semantics results in a shallow copy)
  1211. */
  1212. public ATObject base_clone_(ATObject original) throws InterpreterException {
  1213. if (original.meta_isTaggedAs(NativeTypeTags._MIRROR_).asNativeBoolean().javaValue) {
  1214. return base_reflect_(base_clone_(original.impl_invokeAccessor(original, NATMirrorRoot._BASE_NAME_, NATTable.EMPTY)));
  1215. } else {
  1216. return original.meta_clone();
  1217. }
  1218. }
  1219. /**
  1220. * The <tt>takeOffline:</tt> construct.
  1221. * Removes an object from the export table of an actor. This ensures that the object
  1222. * is no longer remotely accessible. This method is the cornerstone of distributed
  1223. * garbage collection in AmbientTalk. When an object is taken offline, remote clients
  1224. * that would still access it perceive this as if the object had become permanently
  1225. * disconnected.
  1226. *
  1227. * @return <tt>nil</tt>.
  1228. */
  1229. public ATNil base_takeOffline_ (ATObject object) throws InterpreterException{
  1230. ELActor.currentActor().takeOffline(object);
  1231. return Evaluator.getNil();
  1232. }
  1233. /* -------------------
  1234. * -- Type Support -
  1235. * ------------------- */
  1236. /**
  1237. * The <tt>is: object taggedAs: type</tt> construct.
  1238. * @return true if the given object is typed with the given type, false otherwise
  1239. */
  1240. public ATBoolean base_is_taggedAs_(ATObject object, ATTypeTag type) throws InterpreterException {
  1241. return object.meta_isTaggedAs(type);
  1242. }
  1243. /**
  1244. * The <tt>tagsOf: object</tt> construct.
  1245. * @return a table of all of the <i>local</i> types of an object.
  1246. */
  1247. public ATTable base_tagsOf_(ATObject object) throws InterpreterException {
  1248. return object.meta_typeTags();
  1249. }
  1250. /* -------------------------------
  1251. * -- Exception Handling Support -
  1252. * ------------------------------- */
  1253. /**
  1254. * The <tt>try: { tryBlock } finally: { finallyBlock }</tt> construct.
  1255. *
  1256. * Applies the tryBlock closure (to <tt>[]</tt>).
  1257. * Whether the tryBlock raises an exception or not, the finallyBlock closure is guaranteed to be applied either
  1258. * after normal termination of the tryBlock or when an exception is propagated from the tryBlock.
  1259. */
  1260. public ATObject base_try_finally_(ATClosure tryBlock, ATClosure finallyBlock) throws InterpreterException {
  1261. try {
  1262. return tryBlock.base_apply(NATTable.EMPTY);
  1263. } finally {
  1264. finallyBlock.base_apply(NATTable.EMPTY);
  1265. }
  1266. }
  1267. /**
  1268. * The <tt>try: { tryBlock } usingHandlers: [ handler1, handler2, ... ] finally: { finallyBlock }</tt> construct.
  1269. *
  1270. * Applies the tryBlock closure (to <tt>[]</tt>) and handles exceptions using the given exception handlers.
  1271. * Whether the tryBlock raises an exception or not, the finallyBlock closure is guaranteed to be applied either
  1272. * after the termination of the tryBlock or the execution of a fitting handler either before the exception is
  1273. * propagated if no matching handler is provided. This construct is the most general means of doing exception
  1274. * handling in AmbientTalk.
  1275. *
  1276. * The handlers given in the handler table represent first-class handler objects,
  1277. * which should respond to the <tt>canHandle</tt> message.
  1278. * @see ATHandler for the interface to which a handler object has to adhere
  1279. */
  1280. public ATObject base_try_usingHandlers_finally_(ATClosure tryBlock, ATTable exceptionHandlers, ATClosure finallyBlock) throws InterpreterException {
  1281. try {
  1282. return tryBlock.base_apply(NATTable.EMPTY);
  1283. } catch(InterpreterException e) {
  1284. ATObject[] handlers = exceptionHandlers.asNativeTable().elements_;
  1285. // find the appropriate handler
  1286. for (int i = 0; i < handlers.length; i++) {
  1287. ATHandler handler = handlers[i].asHandler();
  1288. ATObject exc = e.getAmbientTalkRepresentation();
  1289. if (handler.base_canHandle(exc).asNativeBoolean().javaValue) {
  1290. return handler.base_handle(exc);
  1291. };
  1292. }
  1293. // no handler found, re-throw the exception
  1294. throw e;
  1295. } finally {
  1296. finallyBlock.base_apply(NATTable.EMPTY);
  1297. }
  1298. }
  1299. /**
  1300. * The <tt>try: { tryBlock } usingHandlers: [ handler1, handler2, ... ]</tt> construct.
  1301. *
  1302. * Ad hoc code for tryBlocks which have an empty finally block
  1303. * @see OBJLexicalRoot#base_try_usingHandlers_finally_(ATClosure, ATTable, ATClosure)
  1304. */
  1305. public ATObject base_try_usingHandlers_(ATClosure tryBlock, ATTable exceptionHandlers) throws InterpreterException {
  1306. // An ad hoc version is provided since not using a finally block is a lot cheaper
  1307. // when we know for sure we won't be needing one.
  1308. try {
  1309. return tryBlock.base_apply(NATTable.EMPTY);
  1310. } catch(InterpreterException e) {
  1311. ATObject[] handlers = exceptionHandlers.asNativeTable().elements_;
  1312. // find the appropriate handler
  1313. for (int i = 0; i < handlers.length; i++) {
  1314. ATHandler handler = handlers[i].asHandler();
  1315. ATObject exc = e.getAmbientTalkRepresentation();
  1316. if (handler.base_canHandle(exc).asNativeBoolean().javaValue) {
  1317. return handler.base_handle(exc);
  1318. };
  1319. }
  1320. // no handler found, re-throw the exception
  1321. throw e;
  1322. }
  1323. }
  1324. /**
  1325. * The <tt>try: { tryBlock} using: handler</tt> construct.
  1326. *
  1327. * Ad-hoc code for one exception handler.
  1328. *
  1329. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1330. */
  1331. public ATObject base_try_using_(ATClosure tryBlock, ATHandler handler) throws InterpreterException {
  1332. try {
  1333. return tryBlock.base_apply(NATTable.EMPTY);
  1334. } catch(InterpreterException e) {
  1335. ATObject exc = e.getAmbientTalkRepresentation();
  1336. if (handler.base_canHandle(exc).asNativeBoolean().javaValue) {
  1337. return handler.base_handle(exc);
  1338. } else {
  1339. throw e;
  1340. }
  1341. }
  1342. }
  1343. /**
  1344. * The <tt>try: { tryBlock} using: handler finally: { finallyBlock }</tt> construct.
  1345. *
  1346. * Ad-hoc code for one exception handler.
  1347. *
  1348. * @see #base_try_usingHandlers_finally_(ATClosure, ATTable, ATClosure)
  1349. */
  1350. public ATObject base_try_using_finally_(ATClosure tryBlock, ATHandler handler, ATClosure finallyBlock) throws InterpreterException {
  1351. try {
  1352. return tryBlock.base_apply(NATTable.EMPTY);
  1353. } catch(InterpreterException e) {
  1354. ATObject exc = e.getAmbientTalkRepresentation();
  1355. if (handler.base_canHandle(exc).asNativeBoolean().javaValue) {
  1356. return handler.base_handle(exc);
  1357. } else {
  1358. throw e;
  1359. }
  1360. } finally {
  1361. finallyBlock.base_apply(NATTable.EMPTY);
  1362. }
  1363. }
  1364. /**
  1365. * The <tt>try: { tryBlock} using: handler1 using: handler2</tt> construct.
  1366. *
  1367. * Ad-hoc code for two exception handlers.
  1368. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1369. */
  1370. public ATObject base_try_using_using_(ATClosure tryBlock, ATHandler hdl1, ATHandler hdl2) throws InterpreterException {
  1371. return base_try_usingHandlers_(tryBlock, NATTable.atValue(new ATObject[] { hdl1, hdl2 }));
  1372. }
  1373. /**
  1374. * The <tt>try: { tryBlock} using: handler1 using: handler2 finally: { finallyBlock }</tt> construct.
  1375. *
  1376. * Ad-hoc code for two exception handlers.
  1377. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1378. */
  1379. public ATObject base_try_using_using_finally_(ATClosure tryBlock, ATHandler hdl1, ATHandler hdl2, ATClosure finallyBlock) throws InterpreterException {
  1380. return base_try_usingHandlers_finally_(tryBlock, NATTable.atValue(new ATObject[] { hdl1, hdl2 }), finallyBlock);
  1381. }
  1382. /**
  1383. * The <tt>try: { tryBlock} using: hdl1 using: hdl2 using: hdl3</tt> construct.
  1384. *
  1385. * Ad-hoc code for three exception handlers
  1386. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1387. */
  1388. public ATObject base_try_using_using_using_(ATClosure tryBlock, ATHandler hdl1, ATHandler hdl2, ATHandler hdl3) throws InterpreterException {
  1389. return base_try_usingHandlers_(tryBlock, NATTable.atValue(new ATObject[] { hdl1, hdl2, hdl3 }));
  1390. }
  1391. /**
  1392. * The <tt>try: { tryBlock} using: hdl1 using: hdl2 using: hdl3 finally: { finallyBlock }</tt> construct.
  1393. *
  1394. * Ad-hoc code for three exception handlers.
  1395. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1396. */
  1397. public ATObject base_try_using_using_using_finally_(ATClosure tryBlock, ATHandler hdl1, ATHandler hdl2, ATHandler hdl3, ATClosure finallyBlock) throws InterpreterException {
  1398. return base_try_usingHandlers_finally_(tryBlock, NATTable.atValue(new ATObject[] { hdl1, hdl2, hdl3 }), finallyBlock);
  1399. }
  1400. /**
  1401. * The <tt>try: { tryBlock} catch: type using: { |e| replacementCode }</tt>
  1402. *
  1403. * 'Syntactic sugar' for one "in-line", native handler.
  1404. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1405. */
  1406. public ATObject base_try_catch_using_(ATClosure tryBlock, ATTypeTag filter, ATClosure replacementCode) throws InterpreterException {
  1407. return base_try_using_(tryBlock, new NATHandler(filter, replacementCode));
  1408. }
  1409. /**
  1410. * The <tt>try: { tryBlock} catch: type using: { |e| replacementCode } finally: { finallyBlock }</tt>
  1411. *
  1412. * 'Syntactic sugar' for one "in-line", native handler.
  1413. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1414. */
  1415. public ATObject base_try_catch_using_finally_(ATClosure tryBlock, ATTypeTag filter, ATClosure replacementCode, ATClosure finallyBlock) throws InterpreterException {
  1416. return base_try_using_finally_(tryBlock, new NATHandler(filter, replacementCode), finallyBlock);
  1417. }
  1418. /**
  1419. * The <tt>try:catch:using:catch:using:</tt> construct.
  1420. *
  1421. * <pre>try: {
  1422. * tryBlock
  1423. * } catch: type using: { |e|
  1424. * replacementCode
  1425. * } catch: type2 using: { |e|
  1426. * replacementCode2
  1427. * }</pre>
  1428. *
  1429. * 'Syntactic sugar' for two in-line handlers
  1430. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1431. */
  1432. public ATObject base_try_catch_using_catch_using_( ATClosure tryBlock,
  1433. ATTypeTag filter1, ATClosure hdl1,
  1434. ATTypeTag filter2, ATClosure hdl2) throws InterpreterException {
  1435. return base_try_using_using_(tryBlock, new NATHandler(filter1, hdl1), new NATHandler(filter2, hdl2));
  1436. }
  1437. /**
  1438. * The <tt>try:catch:using:catch:using:finally:</tt> construct.
  1439. *
  1440. * <pre>try: {
  1441. * tryBlock
  1442. * } catch: type using: { |e|
  1443. * replacementCode
  1444. * } catch: type2 using: { |e|
  1445. * replacementCode2
  1446. * } finally: {
  1447. * finalizationCode
  1448. * }</pre>
  1449. *
  1450. * 'Syntactic sugar' for two in-line handlers
  1451. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1452. */
  1453. public ATObject base_try_catch_using_catch_using_finally_( ATClosure tryBlock,
  1454. ATTypeTag filter1, ATClosure hdl1,
  1455. ATTypeTag filter2, ATClosure hdl2,
  1456. ATClosure finallyBlock) throws InterpreterException {
  1457. return base_try_using_using_finally_(tryBlock, new NATHandler(filter1, hdl1), new NATHandler(filter2, hdl2), finallyBlock);
  1458. }
  1459. /**
  1460. * The <tt>try:catch:using:catch:using:catch:using:</tt> construct.
  1461. *
  1462. * <pre>try: {
  1463. * tryBlock
  1464. * } catch: type using: { |e|
  1465. * replacementCode
  1466. * } catch: type2 using: { |e|
  1467. * replacementCode2
  1468. * } catch: type3 using: { |e|
  1469. * replacementCode3
  1470. * }</pre>
  1471. *
  1472. * 'Syntactic sugar' for three in-line handlers
  1473. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1474. */
  1475. public ATObject base_try_catch_using_catch_using_catch_using_(ATClosure tryBlock,
  1476. ATTypeTag filter1, ATClosure hdl1,
  1477. ATTypeTag filter2, ATClosure hdl2,
  1478. ATTypeTag filter3, ATClosure hdl3) throws InterpreterException {
  1479. return base_try_using_using_using_(tryBlock, new NATHandler(filter1, hdl1), new NATHandler(filter2, hdl2), new NATHandler(filter3, hdl3));
  1480. }
  1481. /**
  1482. * The <tt>try:catch:using:catch:using:catch:using:finally:</tt> construct.
  1483. *
  1484. * <pre>try: {
  1485. * tryBlock
  1486. * } catch: type using: { |e|
  1487. * replacementCode
  1488. * } catch: type2 using: { |e|
  1489. * replacementCode2
  1490. * } catch: type3 using: { |e|
  1491. * replacementCode3
  1492. * }</pre>
  1493. *
  1494. * 'Syntactic sugar' for three in-line handlers
  1495. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1496. */
  1497. public ATObject base_try_catch_using_catch_using_catch_using_finally_(ATClosure tryBlock,
  1498. ATTypeTag filter1, ATClosure hdl1,
  1499. ATTypeTag filter2, ATClosure hdl2,
  1500. ATTypeTag filter3, ATClosure hdl3,
  1501. ATClosure finallyBlock) throws InterpreterException {
  1502. return base_try_using_using_using_finally_(tryBlock, new NATHandler(filter1, hdl1), new NATHandler(filter2, hdl2), new NATHandler(filter3, hdl3), finallyBlock);
  1503. }
  1504. /**
  1505. * The <tt>handle: type with: { |e| replacementCode }</tt> construct.
  1506. *
  1507. * @return a first-class handler from a filter prototype and some handler code.
  1508. * @see ATHandler for the interface to which a handler object responds.
  1509. */
  1510. public ATObject base_handle_with_(ATTypeTag filter, ATClosure replacementCode) {
  1511. return new NATHandler(filter, replacementCode);
  1512. }
  1513. /**
  1514. * The <tt>raise: exception</tt> construct.
  1515. *
  1516. * Raises an exception which can be caught by dynamically installed try-catch-using blocks.
  1517. * @see #base_try_usingHandlers_(ATClosure, ATTable)
  1518. */
  1519. public ATNil base_raise_(ATObject anExceptionObject) throws InterpreterException {
  1520. throw Evaluator.asNativeException(anExceptionObject);
  1521. }
  1522. /* --------------------
  1523. * -- Unary Operators -
  1524. * -------------------- */
  1525. /**
  1526. * The unary <tt>!</tt> primitive.
  1527. * <pre>!b == b.not()</pre>
  1528. * @param b the boolean to negate.
  1529. * @return the negation of the boolean.
  1530. */
  1531. public ATBoolean base__opnot_(ATBoolean b) throws InterpreterException {
  1532. return b.base_not();
  1533. }
  1534. /**
  1535. * The unary <tt>-</tt> primitive.
  1536. * <pre>-NUM(n) == 0 - n</pre>
  1537. *
  1538. * @param n a number or a fraction to negate.
  1539. */
  1540. public ATNumeric base__opmns_(ATNumeric n) throws InterpreterException {
  1541. return NATNumber.ZERO.base__opmns_(n);
  1542. }
  1543. /**
  1544. * The unary <tt>+</tt> primitive.
  1545. * <pre>+NBR(n) == NBR(n)</pre>
  1546. */
  1547. public ATNumber base__oppls_(ATNumber n) throws InterpreterException {
  1548. return n;
  1549. }
  1550. /* -------------------
  1551. * -- Miscellaneous --
  1552. * ------------------- */
  1553. /**
  1554. * The <tt>read:</tt> metaprogramming construct. Parses the given text string into an
  1555. * abstract syntax tree.
  1556. *
  1557. * Example: <code>read: "x" => `x</code>
  1558. */
  1559. public ATAbstractGrammar base_read_(ATText source) throws InterpreterException {
  1560. return NATParser._INSTANCE_.base_parse(source);
  1561. }
  1562. /**
  1563. * The <tt>eval:in:</tt> metaprogramming construct.
  1564. * Evaluates the given AST in the context of the given object, returning its value.
  1565. *
  1566. * Example: <code>eval: `x in: object: { def x := 1 } => 1</code>
  1567. *
  1568. * This is a "dangerous" operation in the sense that lexical encapsulation of the given
  1569. * object can be violated.
  1570. */
  1571. public ATObject base_eval_in_(ATAbstractGrammar ast, ATObject obj) throws InterpreterException {
  1572. return ast.meta_eval(new NATContext(obj, obj));
  1573. }
  1574. /**
  1575. * The <tt>print:</tt> metaprogramming construct.
  1576. * This construct invokes the object mirror's <tt>print</tt> method.
  1577. *
  1578. * @return a text string being a human-readable representation of the given object.
  1579. */
  1580. public ATText base_print_(ATObject obj) throws InterpreterException {
  1581. return obj.meta_print();
  1582. }
  1583. /**
  1584. * Compare the receiver object to the <tt>root</tt> object.
  1585. * the reason for this custom implementation: during the execution
  1586. * of this method, 'this' should refer to the global lexical scope object (the root),
  1587. * not to the OBJLexicalRoot instance.
  1588. *
  1589. * When invoking one of these methods lexically, the receiver is always 'root'
  1590. * For example, <code>==(obj)</code> is equivalent to <code>root == obj</code> (or "root.==(obj)")
  1591. */
  1592. public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException {
  1593. return Evaluator.getGlobalLexicalScope().base__opeql__opeql_(comparand);
  1594. }
  1595. /**
  1596. * Instantiate the <tt>root</tt> object. I.e. <code>new()</code> is equivalent
  1597. * to <tt>root.new()</tt>.
  1598. */
  1599. /*public ATObject base_new(ATObject[] initargs) throws InterpreterException {
  1600. // root.new(@initargs)
  1601. ATObject root = Evaluator.getGlobalLexicalScope();
  1602. return root.meta_invoke(root,NATNil._NEW_NAME_,NATTable.atValue(initargs));
  1603. }*/
  1604. /**
  1605. * After deserialization, ensure that the lexical root remains unique.
  1606. */
  1607. public ATObject meta_resolve() throws InterpreterException {
  1608. return OBJLexicalRoot._INSTANCE_;
  1609. }
  1610. public NATText meta_print() throws InterpreterException {
  1611. return NATText.atValue("lexroot");
  1612. }
  1613. }