/interpreter/tags/at2-build060407/src/edu/vub/at/objects/mirrors/NATMirage.java

http://ambienttalk.googlecode.com/ · Java · 524 lines · 347 code · 92 blank · 85 comment · 2 complexity · 974aca1b1cb9fd2e03a3c87f5c16f3d2 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATMirage.java created on Oct 2, 2006 at 10:08:12 PM
  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.mirrors;
  29. import edu.vub.at.actors.ATAsyncMessage;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XIllegalArgument;
  32. import edu.vub.at.exceptions.XTypeMismatch;
  33. import edu.vub.at.objects.ATBoolean;
  34. import edu.vub.at.objects.ATClosure;
  35. import edu.vub.at.objects.ATContext;
  36. import edu.vub.at.objects.ATField;
  37. import edu.vub.at.objects.ATMethod;
  38. import edu.vub.at.objects.ATNil;
  39. import edu.vub.at.objects.ATObject;
  40. import edu.vub.at.objects.ATStripe;
  41. import edu.vub.at.objects.ATTable;
  42. import edu.vub.at.objects.coercion.NativeStripes;
  43. import edu.vub.at.objects.grammar.ATSymbol;
  44. import edu.vub.at.objects.natives.FieldMap;
  45. import edu.vub.at.objects.natives.MethodDictionary;
  46. import edu.vub.at.objects.natives.NATNil;
  47. import edu.vub.at.objects.natives.NATObject;
  48. import edu.vub.at.objects.natives.NATTable;
  49. import edu.vub.at.objects.natives.NATText;
  50. import edu.vub.at.objects.natives.grammar.AGSymbol;
  51. import java.util.LinkedList;
  52. import java.util.Vector;
  53. /**
  54. * A NATMirage is an object that forwards all meta-operations invoked upon it (at
  55. * the java-level) to its designated mirror object. To cut off infinite meta-regress
  56. * it also has magic_ variants of them which delegate to the default implementation.
  57. *
  58. * Mirages can currently only be created for 'objects', not for 'isolates'.
  59. * Allowing isolates to be mirrored would require the introduction of 'isolate mirrors', since an isolate
  60. * can only be copied if its mirror can be copied.
  61. *
  62. * @author smostinc
  63. */
  64. public class NATMirage extends NATObject {
  65. // Whenever this field is set, the object should be tested for the _MIRROR_ native stripe
  66. private ATObject mirror_;
  67. public static NATMirage createMirage(ATClosure code, ATObject dynamicParent, boolean parentType, ATStripe[] stripes, ATObject mirror) throws InterpreterException {
  68. if (mirror.meta_isStripedWith(NativeStripes._MIRROR_).asNativeBoolean().javaValue) {
  69. // create a new, uninitialized mirage
  70. NATMirage newMirage = new NATMirage(dynamicParent, code.base_getContext().base_getLexicalScope(), parentType, stripes);
  71. // create a new instance of the mirror with the uninitialized mirage, this implicitly clones
  72. // the mirror and re-initializes it, setting the base field to this new mirage
  73. // def mirrorClone := mirror.new(<uninitialized mirage>)
  74. // the init method of the mirror root will normally
  75. ATObject mirrorClone = mirror.meta_invoke(mirror, NATObject._NEW_NAME_, NATTable.of(newMirage));
  76. // set the mirage's mirror to the cloned mirror
  77. newMirage.initializeWithMirror(mirrorClone);
  78. return newMirage;
  79. } else {
  80. throw new XIllegalArgument("Object used as a mirror without having the Mirror stripe: " + mirror);
  81. }
  82. }
  83. /**
  84. * Dedicated constructor for creating the initial empty mirage tied to the mirror root prototype.
  85. */
  86. protected NATMirage(OBJMirrorRoot mirror) {
  87. super();
  88. mirror_ = mirror;
  89. }
  90. public NATMirage(ATObject dynamicParent, ATObject lexParent, boolean parentType, ATStripe[] stripes) {
  91. super(dynamicParent, lexParent, parentType, stripes);
  92. mirror_ = NATNil._INSTANCE_; // set to nil while not initialized
  93. }
  94. /**
  95. * Private setter to be used in OBJMirrorRoot.init to break the chicken-and-egg cycle when
  96. * having to create both a mirror and its mirage simultaneously. The sequence is as follows:
  97. * 1) a new empty 'uninitialized' mirage is created, with mirror as nil
  98. * 2) a mirror is instantiated, leading to the invocation of its init method
  99. * 3) the initialization of a new OBJMirrorRoot assigns the uninitialized mirage to its 'base' field
  100. * 4) the mirror field of the uninitialized mirage is set to the newly created mirror, using this method.
  101. */
  102. private void initializeWithMirror(ATObject realMirror) {
  103. mirror_ = realMirror;
  104. }
  105. /**
  106. * Constructs a new ambienttalk mirage as a clone of an existing one. This results in a new
  107. * uninitialized mirage (i.e. a mirage whose mirror points to nil). The code that clones the
  108. * mirage must ensure that the mirror is correctly bound to a new instance of the cloned mirage's mirror.
  109. *
  110. *
  111. */
  112. protected NATMirage(FieldMap map,
  113. Vector state,
  114. LinkedList customFields,
  115. MethodDictionary methodDict,
  116. ATObject dynamicParent,
  117. ATObject lexicalParent,
  118. byte flags,
  119. ATStripe[] stripes) throws InterpreterException {
  120. super(map, state, customFields, methodDict, dynamicParent, lexicalParent, flags, stripes);
  121. mirror_ = NATNil._INSTANCE_;
  122. }
  123. public NATMirage asMirage() throws XTypeMismatch {
  124. return this;
  125. }
  126. // Called by the default NATObject Cloning algorithm
  127. protected NATObject createClone(FieldMap map,
  128. Vector state,
  129. LinkedList customFields,
  130. MethodDictionary methodDict,
  131. ATObject dynamicParent,
  132. ATObject lexicalParent,
  133. byte flags, ATStripe[] stripes) throws InterpreterException {
  134. NATMirage clonedMirage = new NATMirage(map,
  135. state,
  136. customFields,
  137. methodDict,
  138. dynamicParent,
  139. lexicalParent,
  140. flags,
  141. stripes);
  142. // clonedMirage.mirror := myMirror.new(clonedMirage)
  143. clonedMirage.mirror_ = mirror_.meta_invoke(mirror_, NATObject._NEW_NAME_, NATTable.of(clonedMirage));
  144. return clonedMirage;
  145. }
  146. /**
  147. * Access to the mirage's mirror, to enable a mirage to be 'upped' to a mirror value.
  148. */
  149. protected ATObject getMirror() {
  150. return mirror_;
  151. }
  152. // MAGIC METHODS
  153. // Cut-off for infinite meta-regress
  154. public ATNil magic_addMethod(ATMethod method) throws InterpreterException {
  155. return super.meta_addMethod(method);
  156. }
  157. public ATNil magic_assignField(ATObject receiver, ATSymbol selector, ATObject value) throws InterpreterException {
  158. return super.meta_assignField(receiver, selector, value);
  159. }
  160. public ATObject magic_clone() throws InterpreterException {
  161. return super.meta_clone();
  162. }
  163. /*
  164. if(cloneMirror.meta_isStripedWith(NativeStripes._MIRROR_).asNativeBoolean().javaValue) {
  165. NATMirage clone = (NATMirage)super.meta_clone();
  166. clone.mirror_ = cloneMirror;
  167. return clone;
  168. } else {
  169. throw new XIllegalArgument("Stratification violation : attempted to create a mirage from an object which is not striped to be a valid mirror.");
  170. }
  171. }*/
  172. public ATNil magic_defineField(ATSymbol name, ATObject value) throws InterpreterException {
  173. return super.meta_defineField(name, value);
  174. }
  175. public ATMethod magic_grabMethod(ATSymbol selector) throws InterpreterException {
  176. return super.meta_grabMethod(selector);
  177. }
  178. public ATObject magic_invoke(ATObject receiver, ATSymbol selector, ATTable arguments) throws InterpreterException {
  179. return super.meta_invoke(receiver, selector, arguments);
  180. }
  181. public ATTable magic_listMethods() throws InterpreterException {
  182. return super.meta_listMethods();
  183. }
  184. public ATObject magic_lookup(ATSymbol selector) throws InterpreterException {
  185. return super.meta_lookup(selector);
  186. }
  187. public ATObject magic_newInstance(ATTable initargs) throws InterpreterException {
  188. return super.meta_newInstance(initargs);
  189. }
  190. public NATText magic_print() throws InterpreterException {
  191. return super.meta_print();
  192. }
  193. public ATObject magic_receive(ATAsyncMessage message) throws InterpreterException {
  194. return super.meta_receive(message);
  195. }
  196. public ATBoolean magic_respondsTo(ATSymbol selector) throws InterpreterException {
  197. return super.meta_respondsTo(selector);
  198. }
  199. public ATObject magic_select(ATObject receiver, ATSymbol selector) throws InterpreterException {
  200. return super.meta_select(receiver, selector);
  201. }
  202. public ATNil magic_addField(ATField field) throws InterpreterException {
  203. return super.meta_addField(field);
  204. }
  205. public ATNil magic_assignVariable(ATSymbol name, ATObject value) throws InterpreterException {
  206. return super.meta_assignVariable(name, value);
  207. }
  208. public ATObject magic_doesNotUnderstand(ATSymbol selector) throws InterpreterException {
  209. return super.meta_doesNotUnderstand(selector);
  210. }
  211. public ATField magic_grabField(ATSymbol selector) throws InterpreterException {
  212. return super.meta_grabField(selector);
  213. }
  214. public ATTable magic_listFields() throws InterpreterException {
  215. return super.meta_listFields();
  216. }
  217. public ATObject magic_send(ATAsyncMessage message) throws InterpreterException {
  218. return super.meta_send(message);
  219. }
  220. public ATObject magic_eval(ATContext ctx) throws InterpreterException {
  221. return super.meta_eval(ctx);
  222. }
  223. public ATObject magic_quote(ATContext ctx) throws InterpreterException {
  224. return super.meta_quote(ctx);
  225. }
  226. public ATBoolean magic_isExtensionOfParent() throws InterpreterException {
  227. return super.meta_isExtensionOfParent();
  228. }
  229. public ATObject magic_getLexicalParent() throws InterpreterException {
  230. return super.meta_getLexicalParent();
  231. }
  232. public ATObject magic_pass() throws InterpreterException {
  233. return super.meta_pass();
  234. }
  235. public ATObject magic_resolve() throws InterpreterException {
  236. return super.meta_resolve();
  237. }
  238. public ATBoolean magic_isStripedWith(ATStripe stripe) throws InterpreterException {
  239. return super.meta_isStripedWith(stripe);
  240. }
  241. public ATTable magic_getStripes() throws InterpreterException {
  242. return super.meta_getStripes();
  243. }
  244. public ATBoolean magic_isCloneOf(ATObject original) throws InterpreterException {
  245. return super.meta_isCloneOf(original);
  246. }
  247. public ATBoolean magic_isRelatedTo(ATObject object) throws InterpreterException {
  248. return super.meta_isRelatedTo(object);
  249. }
  250. /* ========================================================================
  251. * Each meta_x method defined in ATObject is implemented in a mirage as a
  252. * forwarding method that asks its mirror to perform the operation on itself
  253. * instead.
  254. * ======================================================================== */
  255. public ATNil meta_addMethod(ATMethod method) throws InterpreterException {
  256. mirror_.meta_invoke(
  257. mirror_,
  258. AGSymbol.jAlloc("addMethod"),
  259. NATTable.atValue(new ATObject[] { method })
  260. );
  261. return NATNil._INSTANCE_;
  262. }
  263. public ATNil meta_assignField(ATObject receiver, ATSymbol selector, ATObject value) throws InterpreterException {
  264. mirror_.meta_invoke(
  265. mirror_,
  266. AGSymbol.jAlloc("assignField"),
  267. NATTable.atValue(new ATObject[] { receiver, selector, value })
  268. );
  269. return NATNil._INSTANCE_;
  270. }
  271. public ATObject meta_clone() throws InterpreterException {
  272. return mirror_.meta_invoke(
  273. mirror_,
  274. AGSymbol.jAlloc("clone"),
  275. NATTable.EMPTY);
  276. }
  277. public ATNil meta_defineField(ATSymbol name, ATObject value) throws InterpreterException {
  278. mirror_.meta_invoke(
  279. mirror_,
  280. AGSymbol.jAlloc("defineField"),
  281. NATTable.atValue(new ATObject[] { name, value }));
  282. return NATNil._INSTANCE_;
  283. }
  284. public ATMethod meta_grabMethod(ATSymbol selector) throws InterpreterException {
  285. return mirror_.meta_invoke(
  286. mirror_,
  287. AGSymbol.jAlloc("grabMethod"),
  288. NATTable.atValue(new ATObject[] { selector })
  289. ).asMethod();
  290. }
  291. public ATObject meta_invoke(ATObject receiver, ATSymbol selector, ATTable arguments) throws InterpreterException {
  292. return mirror_.meta_invoke(
  293. mirror_,
  294. AGSymbol.jAlloc("invoke"),
  295. NATTable.atValue(new ATObject[] { receiver, selector, arguments }));
  296. }
  297. public ATTable meta_listMethods() throws InterpreterException {
  298. return mirror_.meta_invoke(
  299. mirror_,
  300. AGSymbol.jAlloc("listMethods"),
  301. NATTable.EMPTY
  302. ).asTable();
  303. }
  304. public ATObject meta_lookup(ATSymbol selector) throws InterpreterException {
  305. return mirror_.meta_invoke(
  306. mirror_,
  307. AGSymbol.jAlloc("lookup"),
  308. NATTable.atValue(new ATObject[] { selector }));
  309. }
  310. public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
  311. return mirror_.meta_invoke(
  312. mirror_,
  313. AGSymbol.jAlloc("newInstance"),
  314. NATTable.atValue(new ATObject[] { initargs }));
  315. }
  316. public NATText meta_print() throws InterpreterException {
  317. return mirror_.meta_invoke(
  318. mirror_,
  319. AGSymbol.jAlloc("print"),
  320. NATTable.EMPTY).asNativeText();
  321. }
  322. public ATObject meta_receive(ATAsyncMessage message) throws InterpreterException {
  323. return mirror_.meta_invoke(
  324. mirror_,
  325. AGSymbol.jAlloc("receive"),
  326. NATTable.atValue(new ATObject[] { message }));
  327. }
  328. public ATBoolean meta_respondsTo(ATSymbol selector) throws InterpreterException {
  329. return mirror_.meta_invoke(
  330. mirror_,
  331. AGSymbol.jAlloc("respondsTo"),
  332. NATTable.atValue(new ATObject[] { selector })
  333. ).asBoolean();
  334. }
  335. public ATObject meta_select(ATObject receiver, ATSymbol selector) throws InterpreterException {
  336. return mirror_.meta_invoke(
  337. mirror_,
  338. AGSymbol.jAlloc("select"),
  339. NATTable.atValue(new ATObject[] { receiver, selector }));
  340. }
  341. public ATNil meta_addField(ATField field) throws InterpreterException {
  342. mirror_.meta_invoke(
  343. mirror_,
  344. AGSymbol.jAlloc("addField"),
  345. NATTable.atValue(new ATObject[] { field }));
  346. return NATNil._INSTANCE_;
  347. }
  348. public ATNil meta_assignVariable(ATSymbol name, ATObject value) throws InterpreterException {
  349. mirror_.meta_invoke(
  350. mirror_,
  351. AGSymbol.jAlloc("assignVariable"),
  352. NATTable.atValue(new ATObject[] { name, value }));
  353. return NATNil._INSTANCE_;
  354. }
  355. public ATObject meta_doesNotUnderstand(ATSymbol selector) throws InterpreterException {
  356. return mirror_.meta_invoke(
  357. mirror_,
  358. AGSymbol.jAlloc("doesNotUnderstand"),
  359. NATTable.atValue(new ATObject[] { selector }));
  360. }
  361. public ATField meta_grabField(ATSymbol selector) throws InterpreterException {
  362. return mirror_.meta_invoke(
  363. mirror_,
  364. AGSymbol.jAlloc("grabField"),
  365. NATTable.atValue(new ATObject[] { selector })).asField();
  366. }
  367. public ATTable meta_listFields() throws InterpreterException {
  368. return mirror_.meta_invoke(
  369. mirror_,
  370. AGSymbol.jAlloc("listFields"),
  371. NATTable.EMPTY).asTable();
  372. }
  373. public ATObject meta_send(ATAsyncMessage message) throws InterpreterException {
  374. mirror_.meta_invoke(
  375. mirror_,
  376. AGSymbol.jAlloc("send"),
  377. NATTable.atValue(new ATObject[] { message }));
  378. return NATNil._INSTANCE_;
  379. }
  380. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  381. return mirror_.meta_invoke(
  382. mirror_,
  383. AGSymbol.jAlloc("eval"),
  384. NATTable.atValue(new ATObject[] { ctx }));
  385. }
  386. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  387. return mirror_.meta_invoke(
  388. mirror_,
  389. AGSymbol.jAlloc("quote"),
  390. NATTable.atValue(new ATObject[] { ctx }));
  391. }
  392. public ATBoolean meta_isExtensionOfParent() throws InterpreterException {
  393. return mirror_.meta_select(
  394. mirror_,
  395. AGSymbol.jAlloc("isExtensionOfParent")).asBoolean();
  396. }
  397. public ATObject meta_getLexicalParent() throws InterpreterException {
  398. return mirror_.meta_select(
  399. mirror_,
  400. AGSymbol.jAlloc("lexicalParent"));
  401. }
  402. public ATObject meta_pass() throws InterpreterException {
  403. return mirror_.meta_invoke(
  404. mirror_, AGSymbol.jAlloc("pass"), NATTable.EMPTY);
  405. }
  406. public ATObject meta_resolve() throws InterpreterException {
  407. return mirror_.meta_invoke(
  408. mirror_, AGSymbol.jAlloc("resolve"), NATTable.EMPTY);
  409. }
  410. public ATBoolean meta_isStripedWith(ATStripe stripe) throws InterpreterException {
  411. return mirror_.meta_invoke(
  412. mirror_, AGSymbol.jAlloc("isStripedWith"), NATTable.of(stripe)).asBoolean();
  413. }
  414. public ATTable meta_getStripes() throws InterpreterException {
  415. return mirror_.meta_select(
  416. mirror_,
  417. AGSymbol.jAlloc("stripes")).asTable();
  418. }
  419. public ATBoolean meta_isCloneOf(ATObject original) throws InterpreterException {
  420. return mirror_.meta_invoke(
  421. mirror_,
  422. AGSymbol.jAlloc("isCloneOf"),
  423. NATTable.atValue(new ATObject[] { original })).asBoolean();
  424. }
  425. public ATBoolean meta_isRelatedTo(ATObject object) throws InterpreterException {
  426. return mirror_.meta_invoke(
  427. mirror_,
  428. AGSymbol.jAlloc("isRelatedTo"),
  429. NATTable.atValue(new ATObject[] { object })).asBoolean();
  430. }
  431. }