|
| 1 | +package com.cff.anebe.ir |
| 2 | +{ |
| 3 | + import flash.external.ExtensionContext; |
| 4 | + |
| 5 | + /** |
| 6 | + * A representation of an AS3 script, editable at a high level. |
| 7 | + * @author Chris |
| 8 | + */ |
| 9 | + public class ASScript |
| 10 | + { |
| 11 | + private var context:ExtensionContext; |
| 12 | + |
| 13 | + /** |
| 14 | + * This function should not be called by any user of this library. It will be automatically called by BytecodeEditor.GetScript. |
| 15 | + */ |
| 16 | + public function ASScript() |
| 17 | + { |
| 18 | + this.context = ExtensionContext.createExtensionContext("com.cff.anebe.ANEBytecodeEditor", "Script"); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Gets an instance trait present in the script |
| 23 | + * @param name Name of the trait to retrieve |
| 24 | + * @param favorSetter If this name refers to both a getter and a setter, true will retrieve the getter, while false will retrieve the setter. If it is neither or only one exists, has no effect. |
| 25 | + * @return The trait retrieved, or null if none were found |
| 26 | + */ |
| 27 | + public function getTrait(name:ASMultiname, favorSetter:Boolean = false):ASTrait |
| 28 | + { |
| 29 | + if (name == null || name.type != ASMultiname.TYPE_QNAME) |
| 30 | + { |
| 31 | + throw new ArgumentError("Trait name must be a QName"); |
| 32 | + } |
| 33 | + |
| 34 | + var ret:Object = context.call("GetTrait", name, favorSetter); |
| 35 | + |
| 36 | + if (ret == null) |
| 37 | + { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + if (ret is String) |
| 42 | + { |
| 43 | + throw new Error(ret); |
| 44 | + } |
| 45 | + |
| 46 | + return ret as ASTrait; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Sets a trait into the script. |
| 51 | + * If a trait does not exist by the name of the new trait, the new trait will be added. |
| 52 | + * If a getter exists by the name of the new trait and the new trait is a setter, the new trait will be added. |
| 53 | + * If a setter exists by the name of the new trait and the new trait is a getter, the new trait will be added. |
| 54 | + * If both a getter and setter exist by the name of the new trait and the new trait is neither a getter nor setter, the new trait will replace both the getter and setter. |
| 55 | + * If a trait of the same kind already exists by the name of the new trait, the new trait will replace that trait. |
| 56 | + * If a trait exists by the name of the new trait and the kinds do not match any of the above rules, the new trait will replace the original. |
| 57 | + * @param value The trait to set or add to the script |
| 58 | + */ |
| 59 | + public function setTrait(value:ASTrait):void |
| 60 | + { |
| 61 | + if (value == null) |
| 62 | + { |
| 63 | + throw new ArgumentError("Cannot set a null trait"); |
| 64 | + } |
| 65 | + if (value.name == null || value.name.type != ASMultiname.TYPE_QNAME) |
| 66 | + { |
| 67 | + throw new ArgumentError("Trait name must be a QName"); |
| 68 | + } |
| 69 | + |
| 70 | + var ret:Object = context.call("SetTrait", value); |
| 71 | + |
| 72 | + if (ret is String) |
| 73 | + { |
| 74 | + throw new Error(ret); |
| 75 | + } |
| 76 | + if (!(ret is Boolean) || !ret) |
| 77 | + { |
| 78 | + throw new Error("An unspecified error occurred"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Deletes a trait present in the script. |
| 84 | + * @param name Name of the trait to retrieve |
| 85 | + * @param favorSetter If this name refers to both a getter and a setter, true will delete the getter, while false will delete the setter. If it is neither or only one exists, has no effect. |
| 86 | + */ |
| 87 | + public function deleteTrait(name:ASMultiname, favorSetter:Boolean = false):void |
| 88 | + { |
| 89 | + if (name == null || name.type != ASMultiname.TYPE_QNAME) |
| 90 | + { |
| 91 | + throw new ArgumentError("Trait name must be a QName"); |
| 92 | + } |
| 93 | + |
| 94 | + var ret:Object = context.call("DeleteTrait", name, favorSetter); |
| 95 | + |
| 96 | + if (ret is String) |
| 97 | + { |
| 98 | + throw new Error(ret); |
| 99 | + } |
| 100 | + if (!(ret is Boolean) || !ret) |
| 101 | + { |
| 102 | + throw new Error("An unspecified error occurred"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Gets the initializer for a script. |
| 108 | + * Will be run either on program start (for the main script) or when "an entity exported from that script is first referenced by the program during property lookup". |
| 109 | + * @return The ASMethod that represents the script initializer |
| 110 | + */ |
| 111 | + public function getScriptInitializer():ASMethod |
| 112 | + { |
| 113 | + var ret:Object = context.call("GetInitializer"); |
| 114 | + |
| 115 | + if (ret is String) |
| 116 | + { |
| 117 | + throw new Error(ret); |
| 118 | + } |
| 119 | + if (!(ret is ASMethod)) |
| 120 | + { |
| 121 | + throw new Error("An unspecified error occurred"); |
| 122 | + } |
| 123 | + |
| 124 | + return ret as ASMethod; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the initializer for a script. |
| 129 | + * Will be run either on program start (for the main script) or when "an entity exported from that script is first referenced by the program during property lookup". |
| 130 | + * @param v The ASMethod that represents the new script initializer |
| 131 | + */ |
| 132 | + public function setScriptInitializer(v:ASMethod):void |
| 133 | + { |
| 134 | + if (v == null) |
| 135 | + { |
| 136 | + throw new ArgumentError("Static constructor cannot be set to null"); |
| 137 | + } |
| 138 | + var ret:Object = context.call("SetInitializer", v); |
| 139 | + |
| 140 | + if (ret is String) |
| 141 | + { |
| 142 | + throw new Error(ret); |
| 143 | + } |
| 144 | + if (!(ret is Boolean) || !ret) |
| 145 | + { |
| 146 | + throw new Error("An unspecified error occurred"); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments