PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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