#include "Python.h"
#include "structmember.h"


#define OFF(x) offsetof(PyXCodeObject, x)

static PyMemberDef xcode_memberlist[] = {
	{"co_xcode",	T_OBJECT,	OFF(co_xcode),		READONLY},
	{NULL}	/* Sentinel */
};

static PyXCodeObject *
PyXCode_New(void* pointer, int freevars)
{
	PyXCodeObject* xco;
	
	xco = PyObject_NEW(PyXCodeObject, &PyXCode_Type);
	xco->co_xcode = pointer;
	xco->co_freevars = freevars;
	return xco;
}

static PyObject *
xcode_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
	int pointer,freevars;

	if (!PyArg_ParseTuple(args, "ii", &pointer,&freevars))
        return NULL;

	return (PyObject*)PyXCode_New((void*)pointer,freevars);
}

static void
xcode_dealloc(PyXCodeObject *co)
{
	//Py_XDECREF(co->co_xcode);
	PyObject_DEL(co);
}

PyDoc_STRVAR(xcode_doc,
"xcode(co_xcode)\n\
\n\
Create a assembly based code object.  Not for the faint of heart.");

/* XXX code objects need to participate in GC? */

PyTypeObject PyXCode_Type = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,
	"code",
	sizeof(PyXCodeObject),
	0,
	(destructor)xcode_dealloc,  /* tp_dealloc */
	0,				/* tp_print */
	0, 				/* tp_getattr */
	0,				/* tp_setattr */
	0, /*(cmpfunc)code_compare, 		 tp_compare */
	0, /*(reprfunc)code_repr,		 tp_repr */
	0,				/* tp_as_number */
	0,				/* tp_as_sequence */
	0,				/* tp_as_mapping */
	0, /*(hashfunc)code_hash, 		tp_hash */
	0,				/* tp_call */
	0,				/* tp_str */
	PyObject_GenericGetAttr,	/* tp_getattro */
	0,				/* tp_setattro */
	0,				/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT,		/* tp_flags */
	xcode_doc,			/* tp_doc */
	0,				/* tp_traverse */
	0,				/* tp_clear */
	0,				/* tp_richcompare */
	0,				/* tp_weaklistoffset */
	0,				/* tp_iter */
	0,				/* tp_iternext */
	0,				/* tp_methods */
	xcode_memberlist,		/* tp_members */
	0,				/* tp_getset */
	0,				/* tp_base */
	0,				/* tp_dict */
	0,				/* tp_descr_get */
	0,				/* tp_descr_set */
	0,				/* tp_dictoffset */
	0,				/* tp_init */
	0,				/* tp_alloc */
	xcode_new,			/* tp_new */
};
