Help with working on my own Alchemy project

So I've a collection of C++ classes which I now have converting fine with Alchemy to a swc file and can call the exposed functions fine from my AS3 code.

What I'd really like to do is recreate stuff like the WCK's b2Vec.as class,

Code:
public class b2Vec2 extends b2Base {

    public function b2Vec2(p:int) {
        _ptr = p;
    }

    public function get v2():V2 {
        return new V2(x, y);
    }

    public function set v2(v:V2):void {
        x = v.x;
        y = v.y;
    }

    public function get x():Number { return mem._mrf(_ptr + 0); }
    public function set x(v:Number):void { mem._mwf(_ptr + 0, v); }
    public function get y():Number { return mem._mrf(_ptr + 4); }
    public function set y(v:Number):void { mem._mwf(_ptr + 4, v); }

}


This is a simple example, but shows what I'd like to do. On the C side of things, b2Vec2 is a struct,

Code:
/// A 2D column vector.
struct b2Vec2
{
    /// Default constructor does nothing (for performance).
    b2Vec2() {}

    ....

    float32 x, y;
};


So, for this struct, it's easy to calculate that the first variable of a b2Vec2 object is a float, which will be the value in x and can be read via Alchemy's MemUser classes _mrf (read a fload from a point in memory) with _mrf(pointerAddress) and you can read in the second float with _mrf(pointerAddress + 4).

My question is, if you're not a C++ expert (me), is there any way to get the definition of a class, as in the addresses of all the variables within and what they are? So, for the b2Vec2 one, I'd imaging it'd be something like, float32 x 0 float34 y 4 ...

The reason I'm asking is because one of the classes in particular has loads of variables and to try and get each and every one's information so I can access it directly from the AS3 code would be lots of work and I'm going to assume I'll introduce plenty of human error to it.
Yes, finding the memory offsets & writing the getter/setters is automated. Take a look at the "probe" folder. This is a special flash project that includes a separate alchemy compiled SWC that includes the Box2D library. It uses the C++ "offsetof" macro to trace out all the getters/setters to the output panel when run. If you look at "Box2D.c" in the "probe" folder, the first part is a bunch of "#defines" for each AS3 variable type (int, boolean, etc.). Then the appropriate #define is used on the variables that need to be exposed for each class. If you look at the file you'll probably start to understand. When you run the flash project in debug mode everything is output and you just copy / past into your class.

If you have a C++ pointer to another class, and you want that class also represented in AS3, that gets a bit trickier. I can explain if you want though.
Huge thanks for the reply! I've just started to look at the probe/Box2D.c file and that absolutely helps massively! I'm gonna have to put a bit of elbow grease into figuring out how to set one of those up for myself, but that will definitely make the whole process so much easier.

There are a few classes with pointers to other classes, yes, so if you could go into that again it would be *massively* helpful, thank you.
Ok, take a look at this #define:

Code:
#define C(class, member, rclass) \
   o = (int)offsetof(class, member); \
   sprintf(s, "public var %s:%s; // %s = new %s(_ptr + %d);", #member, #rclass, #member, #rclass, o); TS(s);


Used here:

Code:
C(b2AABB, lowerBound, b2Vec2);


Outputs this:

Code:
public var lowerBound:b2Vec2; // lowerBound = new b2Vec2(_ptr + 0);


That part that is commented out? That is copy pasted into the constructor. So when you create a new b2AABB in AS3, a new b2AABB is created in C++ along with the C++ b2Vec2. Then on the AS3 side a new b2Vec2 is created pointing to the right place in memory. So you've got an AS3 class structure very closely resembling the C++ structure.

Here's another tricky #define:

Code:
#define R(class, member, rclass) \
   o = (int)offsetof(class, member); \
   sprintf(s, "public var _%s:%s;", #member, #rclass); TS(s); \
   sprintf(s, "public function get %s():%s { return _%s; }", #member, #rclass, #member); TS(s); \
   sprintf(s, "public function set %s(v:%s):void { mem._mw32(_ptr + %d, v._ptr); _%s = v; }", #member, #rclass, o, #member); TS(s);


Used here:

Code:
R(b2FixtureDef, shape, b2Shape);


Outputs this:

Code:
public var _shape:b2Shape;
public function get shape():b2Shape { return _shape; }
public function set shape(v:b2Shape):void { mem._mw32(_ptr + 4, v._ptr); _shape = v; }


In this case the b2Fixture and the b2Shape are created separately on the AS3 side (the C++ versions are also created). Then later the b2shape is added to the b2Fixture via the setter for the shape property. The setter then makes sure the pointer on the C++ side is pointing to the C++ b2Shape by writing an address in memory to the address in memory of the pointer.

Ok now here's something completely wacky on the AS3 side. This is the constructor for b2Contact:

Code:
      public function b2Contact(p:int, fA:b2Fixture = null, fB:b2Fixture = null) {
         _ptr = p;
         m_manifold = new b2Manifold(_ptr + 64);
         
         /// Address of b2Fixture + userData offset -> deref to AS3 = AS3 b2Fixture.
         m_fixtureA = fA ? fA : deref(mem._mr32(mem._mr32(_ptr + 48) + 44)) as b2Fixture;
         m_fixtureB = fB ? fB : deref(mem._mr32(mem._mr32(_ptr + 52) + 44)) as b2Fixture;
      }


Contact events are sent to the AS3 side via contact event callbacks in the C++ code. So the C++ code sends the address of the b2Contact back to AS3 and an AS2 b2Contact is created. Here's where it gets tricky. The fixtures (shapes) involved in the contact have already been created in AS3. So from just the pointer to the b2Contact we have to find the original AS3 b2Fixture. Each b2Fixture in C++ keeps a reference to it's AS3 object via the userData property. So we have to find the address of that userData property then call "deref" on it to get at the AS3 object. Here's the deref function:

Code:
      /**
       * dereference a C++ pointer or AS3_Val that is pointing to an AS3 object.
       */
      public static function deref(adr:int):* {
         return vt.get(adr);
      }


That vt.get(adr) bit is undocumented. I figured it out by looking at the AS3 code generated by alchemy.

Hope that helps!

Original thread: https://box2d.org/forum/viewtopic.php?t=7694