/** * @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 */ #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; } 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 } }; 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 = "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) } }; 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; } } 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]; 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(apValue); n++) { if(apValue[n]) { ph7_release_value(pVm, apValue[n]); } } return rc; }