606 lines
22 KiB
C
606 lines
22 KiB
C
/**
|
|
* @PROJECT PH7 Engine for the AerScript Interpreter
|
|
* @COPYRIGHT See COPYING in the top level directory
|
|
* @FILE engine/classes.c
|
|
* @DESCRIPTION Native declarations for the built-in classes
|
|
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
|
|
*/
|
|
#include "ph7int.h"
|
|
|
|
/* Calls use the ordinary language dispatcher, including its exception result. */
|
|
static sxi32 BuiltinCall(ph7_context *pCtx, const char *zName, sxi32 nArg,
|
|
ph7_value **apArg, ph7_value *pResult) {
|
|
ph7_value *pCallable = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
if(pCallable == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_value_string(pCallable, zName, -1);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_context_call(pCtx, pCallable, nArg, apArg, pResult);
|
|
}
|
|
if(rc == PH7_EXCEPTION) {
|
|
ph7_result_value(pCtx, pResult);
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
static sxi32 BuiltinGet(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0,
|
|
(const char *)ph7_context_user_data(pCtx), pValue);
|
|
return rc == PH7_OK ? ph7_result_value(pCtx, pValue) : rc;
|
|
}
|
|
|
|
static sxi32 ExceptionConstruct(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
sxi32 rc, bValue;
|
|
SXUNUSED(nArg);
|
|
if(pValue == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_arg_value(pCtx, 0, pValue);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
bValue = ph7_value_to_bool(pValue);
|
|
rc = ph7_context_status(pCtx);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
if(bValue) {
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "message", apArg[0]);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "code", apArg[1]);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
/* These are the original library's getter values, not the caller's site. */
|
|
rc = ph7_value_string(pValue, "[MEMORY]", -1);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "file", pValue);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
rc = ph7_value_int(pValue, 1);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "line", pValue);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
rc = BuiltinCall(pCtx, "debug_backtrace", 0, 0, pValue);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "trace", pValue);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
rc = ph7_context_arg_value(pCtx, 2, pValue);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
bValue = ph7_value_to_bool(pValue);
|
|
rc = ph7_context_status(pCtx);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
return bValue ? ph7_context_attr_store(pCtx, pThis, 0, "previous", apArg[2]) : PH7_OK;
|
|
}
|
|
|
|
static sxi32 ExceptionString(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
static const char *const azField[] = { "file", "line", "code", "message" };
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
sxu32 n;
|
|
sxi32 rc, nLen;
|
|
const char *zValue;
|
|
char zNumber[32];
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
for(n = 0; n < SX_ARRAYSIZE(azField); n++) {
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, azField[n], pValue);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
if(n == 1 || n == 2) {
|
|
/* Known int fields: formatting must not hide a string allocation failure. */
|
|
nLen = (sxi32)SyBufferFormat(zNumber, sizeof(zNumber), "%qd", ph7_value_to_int64(pValue));
|
|
zValue = zNumber;
|
|
} else {
|
|
zValue = ph7_value_to_string(pValue, &nLen);
|
|
rc = ph7_context_status(pCtx);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
if(n) {
|
|
rc = ph7_result_string(pCtx, " ", 1);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
rc = ph7_result_string(pCtx, zValue, nLen);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
|
|
static sxi32 ExceptionTraceString(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
/* Intentionally retain the undefined-function behavior of the library. */
|
|
rc = BuiltinCall(pCtx, "debug_string_backtrace", 0, 0, pValue);
|
|
return rc == PH7_OK ? ph7_result_value(pCtx, pValue) : rc;
|
|
}
|
|
|
|
static sxi32 DirectoryStoreHandle(ph7_context *pCtx, ph7_value *pValue) {
|
|
sxi32 rc = ph7_context_attr_store(pCtx, ph7_context_this(pCtx), 0, "handle", pValue);
|
|
if(rc == SXERR_NOMATCH) {
|
|
/* The API reports a rejected store; the library assignment is fatal. */
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_ERR,
|
|
"Cannot assign a value of incompatible type to class attribute 'Directory->handle'");
|
|
return PH7_ABORT;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
static sxi32 DirectoryConstruct(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
sxi32 rc, bValue;
|
|
SXUNUSED(nArg);
|
|
if(pValue == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = BuiltinCall(pCtx, "opendir", 1, apArg, pValue);
|
|
if(rc == PH7_OK) {
|
|
rc = DirectoryStoreHandle(pCtx, pValue);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
bValue = ph7_value_to_bool(pValue);
|
|
rc = ph7_context_status(pCtx);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
return bValue ? ph7_context_attr_store(pCtx, pThis, 0, "path", apArg[0]) : PH7_OK;
|
|
}
|
|
|
|
static sxi32 DirectoryDestruct(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pHandle = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc, bValue;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0 || pHandle == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "handle", pHandle);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "handle", pValue);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
bValue = ph7_value_to_bool(pValue);
|
|
rc = ph7_context_status(pCtx);
|
|
if(rc != PH7_OK || !bValue) {
|
|
return rc;
|
|
}
|
|
return BuiltinCall(pCtx, "closedir", 1, &pHandle, pValue);
|
|
}
|
|
|
|
static sxi32 DirectoryRead(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pHandle = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pHandle == 0 || pResult == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "handle", pHandle);
|
|
if(rc == PH7_OK) {
|
|
rc = BuiltinCall(pCtx, "readdir", 1, &pHandle, pResult);
|
|
}
|
|
return rc == PH7_OK ? ph7_result_value(pCtx, pResult) : rc;
|
|
}
|
|
|
|
static sxi32 DirectoryRewind(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pHandle = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pHandle == 0 || pResult == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "handle", pHandle);
|
|
if(rc == PH7_OK) {
|
|
rc = BuiltinCall(pCtx, "rewinddir", 1, &pHandle, pResult);
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
static sxi32 DirectoryClose(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pHandle = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pHandle == 0 || pResult == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "handle", pHandle);
|
|
if(rc == PH7_OK) {
|
|
rc = BuiltinCall(pCtx, "closedir", 1, &pHandle, pResult);
|
|
}
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_value_int(pResult, 0);
|
|
}
|
|
/* Keep the resource assignment contract, including its existing fatal. */
|
|
return rc == PH7_OK ? DirectoryStoreHandle(pCtx, pResult) : rc;
|
|
}
|
|
|
|
static sxi32 StdConstruct(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
return ph7_context_attr_store(pCtx, ph7_context_this(pCtx), 0, "value", apArg[0]);
|
|
}
|
|
|
|
static sxi32 StdConvert(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
const ph7_type_def *pType = (const ph7_type_def *)ph7_context_user_data(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "value", pValue);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
/* Convert the attribute copy, not a new untyped scalar: its mixed flag is
|
|
* observable in the ordinary return-type check (including existing fatals). */
|
|
switch(pType->iType) {
|
|
case PH7_TYPE_INT:
|
|
ph7_value_to_int64(pValue);
|
|
break;
|
|
case PH7_TYPE_BOOL:
|
|
ph7_value_to_bool(pValue);
|
|
break;
|
|
case PH7_TYPE_FLOAT:
|
|
ph7_value_to_double(pValue);
|
|
break;
|
|
default:
|
|
ph7_value_to_string(pValue, 0);
|
|
break;
|
|
}
|
|
rc = ph7_context_status(pCtx);
|
|
return rc == PH7_OK ? ph7_result_value(pCtx, pValue) : rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* IOException: a core Exception subclass carrying an error code. */
|
|
/* ------------------------------------------------------------------ */
|
|
static const char *IoErrorDescription(int nErr) {
|
|
switch(nErr) {
|
|
case 0: return "No error";
|
|
case 1: return "Invalid argument";
|
|
case 2: return "I/O error";
|
|
default: return "Unknown error code";
|
|
}
|
|
}
|
|
/* Stamp file/line with the first script frame of the active call stack.
|
|
* Native frames ([NATIVE], line 0) are skipped so a catch(Exception $e)
|
|
* observes the script location that raised the error. */
|
|
static ph7_value *IoInstField(ph7_class_instance *pInst, const char *zName) {
|
|
SyString s;
|
|
if(pInst == 0) {
|
|
return 0;
|
|
}
|
|
SyStringInitFromBuf(&s, zName, (sxu32)SyStrlen(zName));
|
|
return PH7_ClassInstanceFetchAttr(pInst, &s);
|
|
}
|
|
static void IOExceptionStamp(ph7_vm *pVm, ph7_class_instance *pInst) {
|
|
ph7_value *pField;
|
|
SySet sTrace;
|
|
VmDebugTrace *pTrace;
|
|
if(pInst == 0 || VmExtractDebugTrace(pVm, &sTrace) != SXRET_OK) {
|
|
return;
|
|
}
|
|
while(SySetGetNextEntry(&sTrace, (void **)&pTrace) == SXRET_OK) {
|
|
if(pTrace->pFile == 0 || pTrace->nLine == 0) {
|
|
continue;
|
|
}
|
|
if(pTrace->pFile->nByte == sizeof("[NATIVE]") - 1 &&
|
|
SyMemcmp(pTrace->pFile->zString, "[NATIVE]", sizeof("[NATIVE]") - 1) == 0) {
|
|
continue;
|
|
}
|
|
pField = IoInstField(pInst, "file");
|
|
if(pField) {
|
|
PH7_MemObjRelease(pField);
|
|
PH7_MemObjInit(pVm, pField);
|
|
(void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte);
|
|
}
|
|
pField = IoInstField(pInst, "line");
|
|
if(pField) {
|
|
(void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine);
|
|
}
|
|
break;
|
|
}
|
|
SySetRelease(&sTrace);
|
|
}
|
|
static sxi32 IOExceptionConstruct(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_class_instance *pInst;
|
|
sxi32 rc;
|
|
if(pResult == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
/* Let the base Exception constructor own message/code/file/line/trace.
|
|
* parent:: is required: a plain dispatch would resolve back here. */
|
|
rc = ph7_context_parent(pCtx, "__construct", nArg > 2 ? 3 : 2, apArg, pResult);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
if(pThis && pThis->x.pOther) {
|
|
pInst = (ph7_class_instance *)pThis->x.pOther;
|
|
IOExceptionStamp(pCtx->pVm, pInst);
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static sxi32 IOExceptionDescription(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
|
|
int nErr = nArg > 0 ? ph7_value_to_int(apArg[0]) : -1;
|
|
return ph7_result_string(pCtx, IoErrorDescription(nErr), -1);
|
|
}
|
|
/* Core-owned IOException throw used by both the Console module and TextWriter
|
|
* so the same validation produces the same exception on every call path.
|
|
* The constructor stamps file/line; the pending transfer is left for the VM
|
|
* unwinder. Full cleanup on every failure path. */
|
|
PH7_PRIVATE sxi32 PH7_VmThrowIoException(ph7_context *pCtx, int nCode, const char *zMsg) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_class *pClass;
|
|
ph7_class_instance *pInst;
|
|
ph7_class_method *pCons;
|
|
ph7_value *pResult, *pObj, *apArg[2];
|
|
sxi32 rc;
|
|
if(pVm->iPendingTransfer) {
|
|
return PH7_EXCEPTION;
|
|
}
|
|
pClass = PH7_VmExtractClass(pVm, "IOException", sizeof("IOException") - 1, TRUE);
|
|
if(pClass == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
pInst = PH7_NewClassInstance(pVm, pClass);
|
|
if(pInst == 0) {
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
pResult = ph7_context_new_scalar(pCtx);
|
|
apArg[0] = ph7_context_new_scalar(pCtx);
|
|
apArg[1] = ph7_context_new_scalar(pCtx);
|
|
pObj = ph7_context_new_scalar(pCtx);
|
|
if(pResult == 0 || apArg[0] == 0 || apArg[1] == 0 || pObj == 0) {
|
|
PH7_ClassInstanceUnref(pInst);
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
ph7_value_string(apArg[0], zMsg, -1);
|
|
ph7_value_int(apArg[1], nCode);
|
|
pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
|
|
if(pCons) {
|
|
rc = PH7_VmCallClassMethod(pVm, pInst, pCons, pResult, 2, apArg);
|
|
if(pInst->iRef < 1) {
|
|
pInst->iRef = 1;
|
|
}
|
|
if(rc != PH7_OK) {
|
|
PH7_ClassInstanceUnref(pInst);
|
|
return rc;
|
|
}
|
|
}
|
|
if(pInst->iRef < 1) {
|
|
pInst->iRef = 1;
|
|
}
|
|
PH7_MemObjRelease(pObj);
|
|
pObj->x.pOther = pInst;
|
|
MemObjSetType(pObj, MEMOBJ_OBJ);
|
|
rc = ph7_result_value(pCtx, pObj);
|
|
return rc == PH7_OK ? PH7_EXCEPTION : rc;
|
|
}
|
|
|
|
PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
|
|
ph7_value *apValue[4] = { 0, 0, 0, 0 };
|
|
static const ph7_type_def aConversion[] = {
|
|
{ PH7_TYPE_INT, 0, 0 }, { PH7_TYPE_BOOL, 0, 0 },
|
|
{ PH7_TYPE_FLOAT, 0, 0 }, { PH7_TYPE_STRING, 0, 0 }
|
|
};
|
|
ph7_field_def aExceptionField[] = {
|
|
{ .zName = "message", .sType = { PH7_TYPE_STRING, 0, 0 }, .iFlags = PH7_MEMBER_PROTECTED },
|
|
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 }, .iFlags = PH7_MEMBER_PROTECTED },
|
|
{ .zName = "file", .sType = { PH7_TYPE_STRING, 0, 0 }, .iFlags = PH7_MEMBER_PROTECTED },
|
|
{ .zName = "line", .sType = { PH7_TYPE_INT, 0, 0 }, .iFlags = PH7_MEMBER_PROTECTED },
|
|
{ .zName = "trace", .sType = { PH7_TYPE_MIXED, 0, 1 }, .iFlags = PH7_MEMBER_PROTECTED },
|
|
{ .zName = "previous", .sType = { PH7_TYPE_OBJECT, 0, 0 }, .iFlags = PH7_MEMBER_PROTECTED }
|
|
};
|
|
ph7_param_def aExceptionParam[] = {
|
|
{ .zName = "message", .sType = { PH7_TYPE_STRING, 0, 0 } },
|
|
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } },
|
|
{ .zName = "previous", .sType = { PH7_TYPE_NAMED, "Exception", 0 } }
|
|
};
|
|
ph7_method_def aExceptionMethod[] = {
|
|
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aExceptionParam, .nParam = 3, .xFunc = ExceptionConstruct },
|
|
{ .zName = "getMessage", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = BuiltinGet, .pUserData = "message" },
|
|
{ .zName = "getCode", .sResult = { PH7_TYPE_INT, 0, 0 }, .xFunc = BuiltinGet, .pUserData = "code" },
|
|
{ .zName = "getFile", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = BuiltinGet, .pUserData = "file" },
|
|
{ .zName = "getLine", .sResult = { PH7_TYPE_INT, 0, 0 }, .xFunc = BuiltinGet, .pUserData = "line" },
|
|
{ .zName = "getTrace", .sResult = { PH7_TYPE_MIXED, 0, 0 }, .xFunc = BuiltinGet, .pUserData = "trace" },
|
|
{ .zName = "getTraceAsString", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = ExceptionTraceString },
|
|
{ .zName = "getPrevious", .sResult = { PH7_TYPE_OBJECT, 0, 0 }, .xFunc = BuiltinGet, .pUserData = "previous" },
|
|
{ .zName = "__toString", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = ExceptionString }
|
|
};
|
|
static const ph7_method_def aIteratorMethod[] = {
|
|
{ .zName = "current", .sResult = { PH7_TYPE_MIXED, 0, 0 } },
|
|
{ .zName = "key", .sResult = { PH7_TYPE_MIXED, 0, 0 } },
|
|
{ .zName = "next", .sResult = { PH7_TYPE_VOID, 0, 0 } },
|
|
{ .zName = "rewind", .sResult = { PH7_TYPE_VOID, 0, 0 } },
|
|
{ .zName = "valid", .sResult = { PH7_TYPE_BOOL, 0, 0 } }
|
|
};
|
|
static const ph7_method_def aAggregateMethod[] = {
|
|
{ .zName = "getIterator", .sResult = { PH7_TYPE_MIXED, 0, 0 } }
|
|
};
|
|
static const ph7_param_def aSerializedParam[] = {
|
|
{ .zName = "serialized", .sType = { PH7_TYPE_STRING, 0, 0 } }
|
|
};
|
|
static const ph7_method_def aSerializableMethod[] = {
|
|
{ .zName = "serialize", .sResult = { PH7_TYPE_STRING, 0, 0 } },
|
|
{ .zName = "unserialize", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aSerializedParam, .nParam = 1 }
|
|
};
|
|
static const ph7_field_def aDirectoryField[] = {
|
|
{ .zName = "handle", .sType = { PH7_TYPE_RESOURCE, 0, 0 } },
|
|
{ .zName = "path", .sType = { PH7_TYPE_STRING, 0, 0 } }
|
|
};
|
|
static const ph7_param_def aPathParam[] = {
|
|
{ .zName = "path", .sType = { PH7_TYPE_STRING, 0, 0 } }
|
|
};
|
|
static const ph7_method_def aDirectoryMethod[] = {
|
|
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aPathParam, .nParam = 1, .xFunc = DirectoryConstruct },
|
|
{ .zName = "__destruct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = DirectoryDestruct },
|
|
{ .zName = "read", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = DirectoryRead },
|
|
{ .zName = "rewind", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = DirectoryRewind },
|
|
{ .zName = "close", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = DirectoryClose }
|
|
};
|
|
static const ph7_field_def aStdField[] = {
|
|
{ .zName = "value", .sType = { PH7_TYPE_MIXED, 0, 0 } }
|
|
};
|
|
static const ph7_param_def aValueParam[] = {
|
|
{ .zName = "v", .sType = { PH7_TYPE_MIXED, 0, 0 } }
|
|
};
|
|
static const ph7_method_def aStdMethod[] = {
|
|
{ .zName = "__toInt", .sResult = { PH7_TYPE_INT, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[0] },
|
|
{ .zName = "__toBool", .sResult = { PH7_TYPE_BOOL, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[1] },
|
|
{ .zName = "__toFloat", .sResult = { PH7_TYPE_FLOAT, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[2] },
|
|
{ .zName = "__toString", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[3] },
|
|
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = StdConstruct, .iFlags = PH7_MEMBER_PRIVATE }
|
|
};
|
|
static ph7_constant_def aIoConst[] = {
|
|
{ .zName = "ERROR_NONE", .iFlags = PH7_MEMBER_PUBLIC },
|
|
{ .zName = "ERROR_ARGUMENT", .iFlags = PH7_MEMBER_PUBLIC },
|
|
{ .zName = "ERROR_IO", .iFlags = PH7_MEMBER_PUBLIC }
|
|
};
|
|
static ph7_param_def aIoParam[] = {
|
|
{ .zName = "message", .sType = { PH7_TYPE_STRING, 0, 0 } },
|
|
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } }
|
|
};
|
|
static const ph7_param_def aIoDescParam[] = {
|
|
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } }
|
|
};
|
|
static ph7_param_def aPrintLineParam[] = {
|
|
{ .zName = "value", .sType = { PH7_TYPE_MIXED, 0, 0 } }
|
|
};
|
|
static const ph7_method_def aIoMethod[] = {
|
|
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aIoParam, .nParam = 2, .xFunc = IOExceptionConstruct },
|
|
{ .zName = "GetDescription", .sResult = { PH7_TYPE_STRING, 0, 0 }, .aParam = aIoDescParam, .nParam = 1, .xFunc = IOExceptionDescription,
|
|
.iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
|
|
};
|
|
static const ph7_field_def aTextWriterField[] = {
|
|
{ .zName = "handle", .sType = { PH7_TYPE_RESOURCE, 0, 0 }, .iFlags = PH7_MEMBER_PRIVATE }
|
|
};
|
|
static const ph7_method_def aTextWriterMethod[] = {
|
|
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterConstruct, .iFlags = PH7_MEMBER_PRIVATE },
|
|
{ .zName = "__destruct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterDestruct },
|
|
{ .zName = "__clone", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterClone, .iFlags = PH7_MEMBER_PRIVATE },
|
|
{ .zName = "Print", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterPrint, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC },
|
|
{ .zName = "PrintLine", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aPrintLineParam, .nParam = 1, .xFunc = TextWriterPrintLine, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC },
|
|
{ .zName = "Flush", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterFlush }
|
|
};
|
|
static const char *const aIoBase[] = { "Exception" };
|
|
ph7_class_def aClass[] = {
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Exception", .aField = aExceptionField, .nField = SX_ARRAYSIZE(aExceptionField), .aMethod = aExceptionMethod, .nMethod = SX_ARRAYSIZE(aExceptionMethod) },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "IOException", .aConstant = aIoConst, .nConstant = SX_ARRAYSIZE(aIoConst), .aMethod = aIoMethod, .nMethod = SX_ARRAYSIZE(aIoMethod), .azBase = aIoBase, .nBase = 1 },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "TextWriter", .aField = aTextWriterField, .nField = SX_ARRAYSIZE(aTextWriterField), .aMethod = aTextWriterMethod, .nMethod = SX_ARRAYSIZE(aTextWriterMethod) },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Iterator", .aMethod = aIteratorMethod, .nMethod = SX_ARRAYSIZE(aIteratorMethod), .iFlags = PH7_DECL_INTERFACE },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "IteratorAggregate", .aMethod = aAggregateMethod, .nMethod = SX_ARRAYSIZE(aAggregateMethod), .iFlags = PH7_DECL_INTERFACE },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Serializable", .aMethod = aSerializableMethod, .nMethod = SX_ARRAYSIZE(aSerializableMethod), .iFlags = PH7_DECL_INTERFACE },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Directory", .aField = aDirectoryField, .nField = SX_ARRAYSIZE(aDirectoryField), .aMethod = aDirectoryMethod, .nMethod = SX_ARRAYSIZE(aDirectoryMethod) },
|
|
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "stdClass", .aField = aStdField, .nField = SX_ARRAYSIZE(aStdField), .aMethod = aStdMethod, .nMethod = SX_ARRAYSIZE(aStdMethod) }
|
|
};
|
|
ph7_value *apIoValue[SX_ARRAYSIZE(aIoConst)] = { 0 };
|
|
sxi32 rc = PH7_NOMEM;
|
|
sxu32 n;
|
|
for(n = 0; n < SX_ARRAYSIZE(apValue); n++) {
|
|
apValue[n] = ph7_new_scalar(pVm);
|
|
if(apValue[n] == 0) {
|
|
goto Cleanup;
|
|
}
|
|
}
|
|
for(n = 0; n < SX_ARRAYSIZE(apIoValue); n++) {
|
|
apIoValue[n] = ph7_new_scalar(pVm);
|
|
if(apIoValue[n] == 0) {
|
|
goto Cleanup;
|
|
}
|
|
ph7_value_int(apIoValue[n], (int)n);
|
|
aIoConst[n].pValue = apIoValue[n];
|
|
}
|
|
rc = ph7_value_string(apValue[0], "Unknown exception", -1);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_value_string(apValue[1], "", 0);
|
|
}
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_value_int(apValue[2], 0);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
goto Cleanup;
|
|
}
|
|
aExceptionField[0].pInitial = apValue[0];
|
|
aExceptionField[1].pInitial = apValue[2];
|
|
aExceptionParam[0].pDefault = apValue[1];
|
|
aExceptionParam[1].pDefault = apValue[2];
|
|
aExceptionParam[2].pDefault = apValue[3];
|
|
aIoParam[0].pDefault = apValue[1];
|
|
aIoParam[1].pDefault = apValue[2];
|
|
aPrintLineParam[0].pDefault = apValue[1];
|
|
for(n = 0; n < SX_ARRAYSIZE(aClass); n++) {
|
|
rc = ph7_create_class(pVm, &aClass[n]);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
}
|
|
Cleanup:
|
|
for(n = 0; n < SX_ARRAYSIZE(apIoValue); n++) {
|
|
if(apIoValue[n]) {
|
|
ph7_release_value(pVm, apIoValue[n]);
|
|
aIoConst[n].pValue = 0;
|
|
}
|
|
}
|
|
for(n = 0; n < SX_ARRAYSIZE(apValue); n++) {
|
|
if(apValue[n]) {
|
|
ph7_release_value(pVm, apValue[n]);
|
|
}
|
|
}
|
|
return rc;
|
|
}
|