Add C API for registering native AerScript classes
Build / AerScript (push) Successful in 24s

This commit is contained in:
2026-11-14 23:26:28 +01:00
parent d98f6a8e91
commit 61006fe848
9 changed files with 896 additions and 87 deletions
+54
View File
@@ -530,6 +530,60 @@ PH7_APIEXPORT int ph7_vm_reset(ph7_vm *pVm);
PH7_APIEXPORT int ph7_vm_release(ph7_vm *pVm);
PH7_APIEXPORT int ph7_vm_dump(ph7_vm *pVm, int (*xConsumer)(const void *, unsigned int, void *), void *pUserData);
/* In-process Extending Interfaces */
/*
* Public native class declarations. Names are NUL-terminated identifiers.
* A NULL initializer pointer means no initializer; a value containing NULL is
* an explicit initializer. Descriptors and values are copied by registration.
* Callback code and user data must remain valid until the VM is released.
*/
enum ph7_type {
PH7_TYPE_BOOL = 1, PH7_TYPE_CALLBACK, PH7_TYPE_CHAR, PH7_TYPE_FLOAT,
PH7_TYPE_INT, PH7_TYPE_MIXED, PH7_TYPE_OBJECT, PH7_TYPE_RESOURCE,
PH7_TYPE_STRING, PH7_TYPE_VOID, PH7_TYPE_NAMED
};
typedef struct ph7_type_def {
int iType;
const char *zClass; /* Required only for PH7_TYPE_NAMED; self is allowed. */
int bArray; /* Zero or one: the language's T[] annotation. */
} ph7_type_def;
typedef struct ph7_field_def {
const char *zName;
ph7_type_def sType;
const ph7_value *pInitial;
unsigned int iFlags;
} ph7_field_def;
typedef struct ph7_constant_def {
const char *zName;
const ph7_value *pValue;
} ph7_constant_def;
typedef struct ph7_param_def {
const char *zName; /* Exact lowercase this/parent are reserved, even for static methods. */
ph7_type_def sType;
} ph7_param_def;
typedef struct ph7_method_def {
const char *zName;
ph7_type_def sResult;
const ph7_param_def *aParam;
unsigned int nParam;
int (*xFunc)(ph7_context *, int, ph7_value **);
void *pUserData;
unsigned int iFlags;
} ph7_method_def;
typedef struct ph7_class_def {
const char *zName;
const ph7_field_def *aField;
unsigned int nField;
const ph7_constant_def *aConstant;
unsigned int nConstant;
const ph7_method_def *aMethod;
unsigned int nMethod;
} ph7_class_def;
#define PH7_MEMBER_STATIC 0x01
/* Return an exception object using ph7_result_value(), then this status. */
#define PH7_EXCEPTION (-255)
PH7_APIEXPORT int ph7_create_class(ph7_vm *pVm, const ph7_class_def *pDef);
/* Borrowed receiver, valid only during this callback; NULL for static calls. */
PH7_APIEXPORT ph7_value *ph7_context_this(ph7_context *pCtx);
PH7_APIEXPORT int ph7_create_function(ph7_vm *pVm, const char *zName, int (*xFunc)(ph7_context *, int, ph7_value **), void *pUserData);
PH7_APIEXPORT int ph7_delete_function(ph7_vm *pVm, const char *zName);
PH7_APIEXPORT int ph7_create_constant(ph7_vm *pVm, const char *zName, void (*xExpand)(ph7_value *, void *), void *pUserData);
+12 -1
View File
@@ -756,6 +756,8 @@ struct ph7_context {
*/
ph7_vm *pVm; /* Virtual machine that own this context */
sxi32 iFlags; /* Call flags */
ph7_value *pThis; /* Stable borrowed native method receiver */
sxi32 bResultSet; /* Distinguish explicit NULL from a bare return */
};
/*
* Each hashmap entry [i.e: array(4,5,6)] is recorded in an instance
@@ -1014,6 +1016,7 @@ struct ph7_vm_func {
*/
void *pUserData; /* Upper layer private data associated with this instance */
ph7_vm_func *pNextName; /* Next VM function with the same name as this one */
ph7_user_func sNative; /* Native method body and overload-specific data */
};
/* Forward reference */
typedef struct ph7_builtin_constant ph7_builtin_constant;
@@ -1067,6 +1070,7 @@ struct ph7_class {
SySet aBase; /* Direct class bases in extends order */
SySet aMro; /* C3 linearization, including this class first */
sxi32 iMroState; /* 0: unbuilt, 1: building, 2: ready */
sxi32 bNative; /* Public native declaration */
};
/* Class configuration flags */
#define PH7_CLASS_FINAL 0x001 /* Class is final [cannot be extended] */
@@ -1089,6 +1093,7 @@ struct ph7_class_attr {
sxu32 nType; /* Class attribute data type */
sxu32 nIdx; /* Attribute index */
sxu32 nLine; /* Line number on which this attribute was defined */
ph7_value *pNativeInitial; /* Owned native declaration snapshot, if present */
};
/* Attribute configuration */
#define PH7_CLASS_ATTR_STATIC 0x001 /* Static attribute */
@@ -1383,7 +1388,6 @@ struct VmShutdownCB {
int nArg; /* Total number of given arguments */
};
/* Uncaught exception code value */
#define PH7_EXCEPTION -255
/*
* Allowed value for ph7_vm.nMagic
@@ -1734,6 +1738,13 @@ PH7_PRIVATE sxi32 PH7_VmInstallForeignFunction(ph7_vm *pVm, const SyString *pNam
PH7_PRIVATE sxi32 PH7_VmInstallClass(ph7_vm *pVm, ph7_class *pClass);
PH7_PRIVATE sxi32 PH7_VmBlobConsumer(const void *pSrc, unsigned int nLen, void *pUserData);
PH7_PRIVATE ph7_value *PH7_ReserveMemObj(ph7_vm *pVm);
PH7_PRIVATE sxi32 PH7_VmInternType(ph7_vm *pVm, const SyString *pName, sxu32 *pType);
PH7_PRIVATE sxi32 PH7_MemObjSnapshot(ph7_value *pSrc, ph7_value *pDest);
PH7_PRIVATE sxi32 PH7_MemObjDeclareType(ph7_value *pValue, sxu32 nType, sxi32 bResolve);
PH7_PRIVATE sxi32 PH7_IsIdentifier(const char *zName, sxi32 bKeyword);
PH7_PRIVATE sxi32 PH7_IsMethodName(const char *zName);
PH7_PRIVATE sxi32 PH7_IsReservedConstant(const SyString *pName);
PH7_PRIVATE sxi32 PH7_VmCreateNativeClass(ph7_vm *pVm, const ph7_class_def *pDef);
PH7_PRIVATE ph7_value *PH7_ReserveConstObj(ph7_vm *pVm, sxu32 *pIndex);
PH7_PRIVATE sxi32 PH7_VmOutputConsume(ph7_vm *pVm, SyString *pString);
PH7_PRIVATE sxi32 PH7_VmOutputConsumeAp(ph7_vm *pVm, const char *zFormat, va_list ap);