Extend native class API to support full AerScript declarations
Build / AerScript (push) Successful in 18s

This commit is contained in:
2026-11-18 20:57:39 +01:00
parent 61006fe848
commit 58f604aea5
10 changed files with 1556 additions and 328 deletions
+57 -1
View File
@@ -546,19 +546,31 @@ typedef struct ph7_type_def {
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;
/* Evaluators use ph7_result_* and return PH7_OK, PH7_ABORT or PH7_EXCEPTION.
* They run at the same evaluation point as the corresponding source expression. */
typedef int (*ph7_initializer_func)(ph7_context *);
typedef struct ph7_field_def {
const char *zName;
ph7_type_def sType;
const ph7_value *pInitial;
unsigned int iFlags;
ph7_initializer_func xInitial;
void *pInitialData;
} ph7_field_def;
typedef struct ph7_constant_def {
const char *zName;
const ph7_value *pValue;
unsigned int iFlags;
ph7_initializer_func xInitial;
void *pInitialData;
} ph7_constant_def;
typedef struct ph7_param_def {
const char *zName; /* Exact lowercase this/parent are reserved, even for static methods. */
const char *zName; /* Explicit parameters shadow implicit this/parent bindings. */
ph7_type_def sType;
unsigned int iFlags;
const ph7_value *pDefault;
ph7_initializer_func xDefault;
void *pDefaultData;
} ph7_param_def;
typedef struct ph7_method_def {
const char *zName;
@@ -570,6 +582,8 @@ typedef struct ph7_method_def {
unsigned int iFlags;
} ph7_method_def;
typedef struct ph7_class_def {
unsigned int nSize; /* sizeof(ph7_class_def), checked before any later field */
unsigned int nVersion; /* PH7_CLASS_DEF_VERSION; covers all nested record layouts */
const char *zName;
const ph7_field_def *aField;
unsigned int nField;
@@ -577,13 +591,55 @@ typedef struct ph7_class_def {
unsigned int nConstant;
const ph7_method_def *aMethod;
unsigned int nMethod;
unsigned int iFlags;
const char *const *azBase;
unsigned int nBase;
const char *const *azInterface;
unsigned int nInterface;
/* Optional declaration order: indices into fields followed by constants.
* NULL/0 keeps the default: all fields, then all constants. Otherwise
* supply every index exactly once, in source declaration order. */
const unsigned int *aMemberOrder;
unsigned int nMemberOrder;
} ph7_class_def;
#define PH7_CLASS_DEF_VERSION 2
#define PH7_MEMBER_STATIC 0x01
#define PH7_MEMBER_VIRTUAL 0x04
#define PH7_MEMBER_FINAL 0x08
#define PH7_MEMBER_PUBLIC 0x10
#define PH7_MEMBER_PROTECTED 0x20
#define PH7_MEMBER_PRIVATE 0x40
#define PH7_PARAM_REF 0x01
#define PH7_DECL_FINAL 0x01
#define PH7_DECL_INTERFACE 0x02
#define PH7_DECL_VIRTUAL 0x04
/* 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);
/* Indexed operations reacquire moving VM storage on every call. argv contains
* by-value snapshots; use these operations for immediate reference semantics. */
PH7_APIEXPORT int ph7_context_arg_value(ph7_context *pCtx, unsigned int nArg, ph7_value *pOut);
PH7_APIEXPORT int ph7_context_arg_store(ph7_context *pCtx, unsigned int nArg, ph7_value *pValue);
/* A forwarding handle is borrowed until this callback returns. Calls reacquire
* its live indexed binding; ordinary value setters still mutate only its copy. */
PH7_APIEXPORT int ph7_context_arg_bind(ph7_context *pCtx, unsigned int nArg, ph7_value *pOut);
/* pObject selects instance storage; otherwise zClass selects static/constant
* storage. Access is checked in the callback's declaring language scope. */
PH7_APIEXPORT int ph7_context_attr_value(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pOut);
PH7_APIEXPORT int ph7_context_attr_store(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pValue);
PH7_APIEXPORT int ph7_context_call(ph7_context *pCtx, ph7_value *pCallable,
int nArg, ph7_value **apArg, ph7_value *pResult);
PH7_APIEXPORT int ph7_context_parent(ph7_context *pCtx, const char *zMethod,
int nArg, ph7_value **apArg, ph7_value *pResult);
PH7_APIEXPORT int ph7_context_var_value(ph7_context *pCtx, const char *zName, ph7_value *pOut);
PH7_APIEXPORT int ph7_context_var_store(ph7_context *pCtx, const char *zName, ph7_value *pValue);
/* Legacy scalar conversion APIs cannot return an exception status. Query this
* before further native side effects; returning it propagates the completion. */
PH7_APIEXPORT int ph7_context_status(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);
+34
View File
@@ -627,6 +627,7 @@ struct ph7_value {
#define MEMOBJ_VARIABLE 0 /* Memory value is variable */
#define MEMOBJ_PARENTOBJ 1 /* Memory value is 'parent' object */
#define MEMOBJ_THISOBJ 2 /* Memory value is 'this' object */
#define MEMOBJ_INDEX_HANDLE 4 /* Native context forwarding handle, not a value flag */
/* Allowed variable data types.
*/
#define MEMOBJ_BOOL 0x0001 /* Memory value is a boolean */
@@ -758,6 +759,7 @@ struct ph7_context {
sxi32 iFlags; /* Call flags */
ph7_value *pThis; /* Stable borrowed native method receiver */
sxi32 bResultSet; /* Distinguish explicit NULL from a bare return */
void *pNativeFrame; /* Borrowed argument frame for indexed context operations */
};
/*
* Each hashmap entry [i.e: array(4,5,6)] is recorded in an instance
@@ -951,6 +953,9 @@ struct ph7_vm_func_arg {
sxu32 nType; /* Type of this argument [i.e: array, int, string, float, object, etc.] */
SyString sClass; /* Class name if the argument expect a class instance [i.e: function foo(BaseClass $bar){} ] */
sxi32 iFlags; /* Configuration flags */
ph7_value *pNativeDefault; /* Owned snapshot, distinct from an absent default */
ph7_initializer_func xNativeDefault;
void *pNativeData;
};
/*
* Each static variable is parsed out and remembered in an instance
@@ -1071,6 +1076,10 @@ struct ph7_class {
SySet aMro; /* C3 linearization, including this class first */
sxi32 iMroState; /* 0: unbuilt, 1: building, 2: ready */
sxi32 bNative; /* Public native declaration */
sxi32 iNativeState; /* 0: pending, 1: initializing, 2: usable */
sxi32 bMounting; /* Reentrant static initializer guard */
sxi32 bMethodsMounted; /* Concrete declarations enter the function table once */
sxu32 nNativeWarnings; /* Accepted declaration diagnostics, delivered after commit */
};
/* Class configuration flags */
#define PH7_CLASS_FINAL 0x001 /* Class is final [cannot be extended] */
@@ -1094,6 +1103,8 @@ struct ph7_class_attr {
sxu32 nIdx; /* Attribute index */
sxu32 nLine; /* Line number on which this attribute was defined */
ph7_value *pNativeInitial; /* Owned native declaration snapshot, if present */
ph7_initializer_func xNativeInitial;
void *pNativeData;
};
/* Attribute configuration */
#define PH7_CLASS_ATTR_STATIC 0x001 /* Static attribute */
@@ -1256,6 +1267,13 @@ struct ph7_vm {
void *pStderr; /* STDERR IO stream */
sxbool bDebug; /* TRUE to enable debugging */
sxbool bErrReport; /* TRUE to report all runtime Error/Warning/Notice/Deprecated */
SySet *pClassJournal; /* Non-reentrant, callback-free metadata preparation */
ph7_class *pStagedClass; /* Unpublished declaration visible only to preparation */
sxi32 bClassDeferred; /* A forward hierarchy name is not installed yet */
sxi32 bNativeRegister; /* Callback-free declaration transaction is active */
sxu32 nNativeReserve; /* Conservative rollback free-slot capacity bound */
sxi32 iPendingTransfer; /* Conversion/readiness completion crossing scalar APIs */
ph7_value sPendingTransfer;
int nExceptDepth; /* Exception depth */
int closure_cnt; /* Loaded closures counter */
int json_rc; /* JSON return status [refer to json_encode()/json_decode()] */
@@ -1286,6 +1304,7 @@ struct VmFrame {
SyHash hConst; /* Constant hashtable for fast lookup */
SyHash hVar; /* Variable hashtable for fast lookup */
SySet sArg; /* Function arguments container */
SySet sArgPin; /* Indexed reference arguments retained through reentry */
SySet sRef; /* Local reference table (VmSlot instance) */
SySet sConstDecl; /* Constants already declared during this invocation */
sxi32 iFlags; /* Frame configuration flags (See below) */
@@ -1336,6 +1355,7 @@ struct VmRefObj {
SySet aArrEntries; /* Foreign hashmap entries [i.e: array(&$a) ] */
sxu32 nIdx; /* Referenced object index */
sxu32 nCapture; /* Closure owners: keep contents and index alive */
sxu32 nBorrow; /* Active reference argument handles retain the same index */
sxi32 iFlags; /* Configuration flags */
VmRefObj *pNextCollide, *pPrevCollide; /* Collision link */
VmRefObj *pNext, *pPrev; /* List of all referenced objects */
@@ -1745,6 +1765,20 @@ 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 const char *PH7_ClassMethodDeclarationError(ph7_class *pClass,
const SyString *pName, sxu32 nType, sxi32 iProtection, sxi32 iFlags);
#define PH7_INTERFACE_VISIBILITY_ERROR "interface: Access type must be public"
PH7_PRIVATE sxi32 PH7_ClassConstantProtection(ph7_class *pClass, sxi32 iProtection);
PH7_PRIVATE sxi32 PH7_VmContextAttr(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pValue, sxi32 bStore);
PH7_PRIVATE sxi32 PH7_ClassPrepareNative(ph7_vm *pVm, ph7_class *pClass);
PH7_PRIVATE sxi32 PH7_VmContextParent(ph7_context *pCtx, const char *zMethod,
int nArg, ph7_value **apArg, ph7_value *pResult);
PH7_PRIVATE void PH7_VmDeferTransfer(ph7_vm *pVm, sxi32 rc, ph7_value *pValue);
PH7_PRIVATE sxi32 PH7_VmTakeTransfer(ph7_vm *pVm, ph7_value *pResult);
PH7_PRIVATE sxi32 PH7_VmContextVar(ph7_context *pCtx, const char *zName,
ph7_value *pValue, sxi32 bStore);
PH7_PRIVATE sxi32 PH7_MemObjCopy(ph7_value *pSrc, ph7_value *pDest);
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);