PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

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

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