Skip to content

Commit dee2abf

Browse files
committed
Add builder functions for union types' AS3 representations
Also update and add a bit more asdoc
1 parent efb8a29 commit dee2abf

33 files changed

+571
-15
lines changed

AS3/src/com/cff/anebe/BytecodeEditor.as

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.cff.anebe
22
{
3+
import com.cff.anebe.ir.ASClass;
4+
import com.cff.anebe.ir.ASMultiname;
5+
6+
import flash.events.Event;
37
import flash.events.EventDispatcher;
48
import flash.events.StatusEvent;
59
import flash.external.ExtensionContext;
610
import flash.utils.ByteArray;
711
import flash.utils.CompressionAlgorithm;
8-
import com.cff.anebe.ir.ASClass;
9-
import com.cff.anebe.ir.ASMultiname;
10-
import flash.events.Event;
1112

1213
/**
1314
* The main interface of this library. Allows editing bytecode of passed in SWF files.

AS3/src/com/cff/anebe/ir/ASClass.as

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ package com.cff.anebe.ir
1010
{
1111
private var context:ExtensionContext;
1212

13+
/** Sealed flag: indicates that the class cannot have properties added to it dynamically */
1314
public static const FLAG_SEALED:String = "SEALED";
15+
16+
/** Final flag: indicates that the class cannot be inherited from */
1417
public static const FLAG_FINAL:String = "FINAL";
18+
19+
/** Interface flag: indicates that the class is an interface */
1520
public static const FLAG_INTERFACE:String = "INTERFACE";
21+
22+
/** Protected namespace flag: indicates that the class uses and has a protected namespace. */
1623
public static const FLAG_PROTECTEDNS:String = "PROTECTEDNS";
1724

1825
/**
@@ -386,7 +393,7 @@ package com.cff.anebe.ir
386393
{
387394
if (v == null)
388395
{
389-
v = new ASNamespace();
396+
throw new Error("Cannot set protected namespace to null");
390397
}
391398
var ret:Object = context.call("SetProtectedNamespace", v);
392399

AS3/src/com/cff/anebe/ir/ASError.as

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ package com.cff.anebe.ir
1212
/** The message that the assembler gave for this error */
1313
public var message:String;
1414

15+
/**
16+
* Builds an error. Likely should only be used internally.
17+
* @param loc The instruction where the error occurred
18+
* @param message Description of the error
19+
*/
1520
public function ASError(loc:ASInstruction, message:String = "")
1621
{
1722
this.loc = loc;

AS3/src/com/cff/anebe/ir/ASException.as

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package com.cff.anebe.ir
2222
public var exceptionName:ASMultiname;
2323

2424
/**
25-
* Builds an ASException. Null labels are interpreted as instruction 0, offset 0
25+
* Builds an ASException.
2626
* @param from The instruction where the exception catch range starts
2727
* @param to The instruction where the exception catch range stops
2828
* @param target The target instruction for the catch block

AS3/src/com/cff/anebe/ir/ASInstruction.as

+29
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,35 @@ package com.cff.anebe.ir
226226
this.args = args;
227227
}
228228

229+
/**
230+
* If this is a local get or set instruction, returns the index of the local. Otherwise throws an error.
231+
* @return Index of the local this instruction manipulates.
232+
*/
233+
public function localIndex():uint
234+
{
235+
switch (opcode)
236+
{
237+
case OP_getlocal:
238+
case OP_setlocal:
239+
return args[0] as uint;
240+
case OP_getlocal0:
241+
case OP_setlocal0:
242+
return 0;
243+
case OP_getlocal1:
244+
case OP_setlocal1:
245+
return 1;
246+
case OP_getlocal2:
247+
case OP_setlocal2:
248+
return 2;
249+
case OP_getlocal3:
250+
case OP_setlocal3:
251+
return 3;
252+
253+
default:
254+
throw new Error("Cannot get the local index of an instruction that does not get or set a local");
255+
}
256+
}
257+
229258
/**
230259
* If v is 0-3, the corresponding result of GetLocal0-3. Otherwise the result of GetLocal(v).
231260
*/

AS3/src/com/cff/anebe/ir/ASMultiname.as

+40-5
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,66 @@ package com.cff.anebe.ir
66
*/
77
public class ASMultiname
88
{
9+
/** Type of the multiname. Types are listed as TYPE_* constants in this class */
910
public var type:String;
10-
// Used for all types except RTQNameL, RTQNameLA, and TypeName
11+
12+
/** For types QName, Multiname, RTQName, and their A equivalents, the name associated with the multiname. Null for others. */
1113
public var name:String;
12-
// Used for QName and QNameA
14+
15+
/** For types QName and QNameA, the namespace associated with the multiname. Null for others. */
1316
public var ns:ASNamespace;
14-
// Used for Multiname and MultinameL
17+
18+
/** For types Multiname, MultinameL, and their A equivalents, the set of namespaces associated with the multiname. Null for others. */
1519
public var nsSet:Vector.<ASNamespace>;
16-
// Used for typename
20+
21+
/** For type TypeName, the name of the type being parameterized. Null for others. */
1722
public var typename:ASMultiname;
18-
// Also used for typename
23+
24+
/** For type TypeName, the parameters to the type being parameterized. Null for others. */
1925
public var params:Vector.<ASMultiname>;
2026

27+
/** Type string for QNames, which have a namespace and a name. */
2128
public static const TYPE_QNAME:String = "QName";
29+
30+
/** Type string for Multinames, which have a name and a set of namespaces. */
2231
public static const TYPE_MULTINAME:String = "Multiname";
32+
33+
/** Type string for run-time QNames, which have a name but no namespace. */
2334
public static const TYPE_RTQNAME:String = "RTQName";
35+
36+
/** Type string for run-time late QNames, which have neither name nor namespace. */
2437
public static const TYPE_RTQNAMEL:String = "RTQNameL";
38+
39+
/** Type string for late Multinames, which have a set of namespaces but no name. */
2540
public static const TYPE_MULTINAMEL:String = "MultinameL";
41+
42+
/** Type string for TypeNames, which have a type and parameters to that type. */
2643
public static const TYPE_TYPENAME:String = "TypeName";
2744

45+
/** Type string for A-type QNames, which have a namespace and a name. Usage of this is unknown. */
2846
public static const TYPE_QNAMEA:String = "QNameA";
47+
48+
/** Type string for A-type Multinames, which have a name and a set of namespaces. Usage of this is unknown. */
2949
public static const TYPE_MULTINAMEA:String = "MultinameA";
50+
51+
/** Type string for A-type run-time QNames, which have a name but no namespace. Usage of this is unknown. */
3052
public static const TYPE_RTQNAMEA:String = "RTQNameA";
53+
54+
/** Type string for A-type run-time late QNames, which have neither name nor namespace. Usage of this is unknown. */
3155
public static const TYPE_RTQNAMELA:String = "RTQNameLA";
56+
57+
/** Type string for A-type late Multinames, which have a set of namespaces but no name. Usage of this is unknown. */
3258
public static const TYPE_MULTINAMELA:String = "MultinameLA";
3359

60+
/**
61+
* Builds an ASMultiname from scratch. Should probably not be used; see instead helper functions in the package com.cff.anebe.ir.multinames
62+
* @param type Type of the ASMultiname
63+
* @param name Name associated with the multiname, if the type has one
64+
* @param ns Namespace associated with the multiname, if the type has one
65+
* @param nsSet Namespace set associated with the multiname, if the type has one
66+
* @param typename Type name being parameterized (for TypeNames)
67+
* @param params Type parameters (for TypeNames)
68+
*/
3469
public function ASMultiname(type:String = null, name:String = null, ns:ASNamespace = null, nsSet:Vector.<ASNamespace> = null, typename:ASMultiname = null, params:Vector.<ASMultiname> = null)
3570
{
3671
this.type = type;

AS3/src/com/cff/anebe/ir/ASNamespace.as

+24-1
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,43 @@ package com.cff.anebe.ir
66
*/
77
public class ASNamespace
88
{
9+
/** Type of the namespace. Types are listed as TYPE_* in this class. */
910
public var type:String;
11+
12+
/** Namespace name. */
1013
public var name:String;
1114

15+
/** If there are multiple namespaces with the same name, disambiguates them. This should only very rarely be non-zero. */
1216
public var id:int = 0;
1317

18+
/** Type string for private namespaces */
1419
public static const TYPE_PRIVATE:String = "PrivateNamespace";
20+
21+
/** Type string for "normal" namespaces */
1522
public static const TYPE_NORMAL:String = "Namespace";
23+
24+
/** Type string for package namespaces */
1625
public static const TYPE_PACKAGE:String = "PackageNamespace";
26+
27+
/** Type string for package internal namespaces (things defined as "internal" in AS3 code) */
1728
public static const TYPE_PACKAGEINTERNAL:String = "PackageInternalNs";
29+
30+
/** Type string for protected namespaces */
1831
public static const TYPE_PROTECTED:String = "ProtectedNamespace";
32+
33+
/** Type string for explicit namespaces */
1934
public static const TYPE_EXPLICIT:String = "ExplicitNamespace";
35+
36+
/** Type string for static protected namespaces */
2037
public static const TYPE_STATICPROTECTED:String = "StaticProtectedNs";
2138

22-
public function ASNamespace(type:String = TYPE_NORMAL, name:String = "", id:int = 0)
39+
/**
40+
* Builds an ASNamespace from scratch. Should probably not be used; see instead helper functions in the package com.cff.anebe.ir.namespaces
41+
* @param type Namespace type
42+
* @param name Namespace name
43+
* @param id Disambiguation ID
44+
*/
45+
public function ASNamespace(type:String, name:String, id:int = 0)
2346
{
2447
this.type = type;
2548
this.name = name;

AS3/src/com/cff/anebe/ir/ASTrait.as

+43-4
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,75 @@ package com.cff.anebe.ir
66
*/
77
public class ASTrait
88
{
9+
/** Kind of the trait. Trait kinds are listed under KIND_* in this class. */
910
public var kind:String;
11+
12+
/** Attributes this trait has. Trait attributes are listed under ATTR_* in this class */
1013
public var attributes:Vector.<String>;
14+
15+
/** Slot/dispId of this trait. 0 lets the runtime assign a slot. */
1116
public var slotId:uint;
17+
18+
/** Metadata attached to this trait. */
1219
public var metadata:Vector.<ASMetadata>;
20+
21+
/** Name of this trait. Must always be a QName. */
1322
public var name:ASMultiname;
1423

15-
// If this is a slot or a const, this will be the type of the stored variable. Otherwise null.
24+
/** For slot or const kinds, type of the stored variable. Null for others. */
1625
public var typename:ASMultiname;
1726

18-
/** If this trait's kind is slot or const, the value of the stored variable, or null if there is none. Otherwise null. */
27+
/** For slot or const kinds, the value the trait gets initialized to. May be null if there is no value. Null for other kinds. */
1928
public var value:ASValue;
2029

21-
/** If this trait's kind is getter, setter, method, or function, the associated function object. Otherwise null. */
30+
/** For getter, setter, method, or function kinds, the ASMethod that represents the function. Null for others. */
2231
public var funcOrMethod:ASMethod;
2332

24-
/** If this trait's kind is class, the associated class object. Otherwise null. */
33+
/** For class kind, the ASClass that represents the stored class. Null for others. */
2534
public var clazz:ASClass;
2635

36+
/** Kind string for slots */
2737
public static const KIND_SLOT:String = "slot";
38+
39+
/** Kind string for methods */
2840
public static const KIND_METHOD:String = "method";
41+
42+
/** Kind string for getters */
2943
public static const KIND_GETTER:String = "getter";
44+
45+
/** Kind string for setters */
3046
public static const KIND_SETTER:String = "setter";
47+
48+
/** Kind string for classes */
3149
public static const KIND_CLASS:String = "class";
50+
51+
/** Kind string for functions */
3252
public static const KIND_FUNCTION:String = "function";
53+
54+
/** Kind string for consts */
3355
public static const KIND_CONST:String = "const";
3456

57+
/** Final attribute: indicates this trait cannot be overridden. */
3558
public static const ATTR_FINAL:String = "FINAL";
59+
60+
/** Override attribute: indicates this trait overrides a base class's. */
3661
public static const ATTR_OVERRIDE:String = "OVERRIDE";
62+
63+
/** Metadata attribute: indicates that this trait has metadata. */
3764
public static const ATTR_METADATA:String = "METADATA";
3865

66+
/**
67+
* Builds an ASTrait from scratch. Probably shouldn't be used; see instead helper functions in the package com.cff.anebe.ir.traits
68+
* @param kind Trait kind
69+
* @param attributes Trait attributes
70+
* @param slotId Trait slot ID or disp ID
71+
* @param name Trait name
72+
* @param typename Trait type name, if present
73+
* @param metadata Trait metadata, if present
74+
* @param value Trait initial value, if present
75+
* @param funcOrMethod Function stored in trait, if present
76+
* @param clazz Class stored in trait, if present
77+
*/
3978
public function ASTrait(kind:String = null, attributes:Vector.<String> = null, slotId:uint = 0, name:ASMultiname = null, typename:ASMultiname = null, metadata:Vector.<ASMetadata> = null, value:ASValue = null, funcOrMethod:ASMethod = null, clazz:ASClass = null)
4079
{
4180
this.kind = kind;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cff.anebe.ir.multinames
2+
{
3+
import com.cff.anebe.ir.ASMultiname;
4+
import com.cff.anebe.ir.ASNamespace;
5+
6+
/**
7+
* Builds an ASMultiname that represents a QName
8+
* @param ns Namespace of the QName
9+
* @param name Name of the QName
10+
* @return Built ASMultiname
11+
*/
12+
public function ASQName(ns:ASNamespace, name:String):ASMultiname
13+
{
14+
return new ASMultiname(ASMultiname.TYPE_QNAME, name, ns);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cff.anebe.ir.multinames
2+
{
3+
import com.cff.anebe.ir.ASMultiname;
4+
import com.cff.anebe.ir.ASNamespace;
5+
6+
/**
7+
* Builds an ASMultiname that represents a QNameA
8+
* @param ns Namespace of the QNameA
9+
* @param name Name of the QNameA
10+
* @return Built ASMultiname
11+
*/
12+
public function ASQNameA(ns:ASNamespace, name:String):ASMultiname
13+
{
14+
return new ASMultiname(ASMultiname.TYPE_QNAMEA, name, ns);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cff.anebe.ir.multinames
2+
{
3+
import com.cff.anebe.ir.ASMultiname;
4+
import com.cff.anebe.ir.ASNamespace;
5+
6+
/**
7+
* Builds an ASMultiname that represents a Multiname
8+
* @param nsSet Set of namespaces associated with the Multiname
9+
* @param name Name of the Multiname
10+
* @return Built ASMultiname
11+
*/
12+
public function Multiname(name:String, nsSet:Vector.<ASNamespace>):ASMultiname
13+
{
14+
return new ASMultiname(ASMultiname.TYPE_MULTINAME, name, null, nsSet);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cff.anebe.ir.multinames
2+
{
3+
import com.cff.anebe.ir.ASMultiname;
4+
import com.cff.anebe.ir.ASNamespace;
5+
6+
/**
7+
* Builds an ASMultiname that represents a MultinameA
8+
* @param nsSet Set of namespaces associated with the MultinameA
9+
* @param name Name of the MultinameA
10+
* @return Built ASMultiname
11+
*/
12+
public function MultinameA(name:String, nsSet:Vector.<ASNamespace>):ASMultiname
13+
{
14+
return new ASMultiname(ASMultiname.TYPE_MULTINAMEA, name, null, nsSet);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cff.anebe.ir.multinames
2+
{
3+
import com.cff.anebe.ir.ASMultiname;
4+
import com.cff.anebe.ir.ASNamespace;
5+
6+
/**
7+
* Builds an ASMultiname that represents a MultinameL
8+
* @param nsSet Set of namespaces associated with the MultinameL
9+
* @return Built ASMultiname
10+
*/
11+
public function MultinameL(nsSet:Vector.<ASNamespace>):ASMultiname
12+
{
13+
return new ASMultiname(ASMultiname.TYPE_MULTINAMEL, null, null, nsSet);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cff.anebe.ir.multinames
2+
{
3+
import com.cff.anebe.ir.ASMultiname;
4+
import com.cff.anebe.ir.ASNamespace;
5+
6+
/**
7+
* Builds an ASMultiname that represents a MultinameLA
8+
* @param nsSet Set of namespaces associated with the MultinameLA
9+
* @return Built ASMultiname
10+
*/
11+
public function MultinameLA(nsSet:Vector.<ASNamespace>):ASMultiname
12+
{
13+
return new ASMultiname(ASMultiname.TYPE_MULTINAMELA, null, null, nsSet);
14+
}
15+
}

0 commit comments

Comments
 (0)