From bcfa4c6163db7c2eea1aae9f781677c88c92887c Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Thu, 19 Nov 2026 16:15:05 +0100 Subject: [PATCH] Migrate built-in classes and interfaces to native C --- engine/api.c | 2 +- engine/classes.c | 423 +++++++++++++++++++++++++++++++++++++++++++ engine/compiler.c | 84 +++++++-- engine/lib/dataset.c | 10 +- engine/parser.c | 6 +- engine/vfs.c | 14 +- engine/vm.c | 206 ++++++--------------- include/ph7int.h | 3 + 8 files changed, 574 insertions(+), 174 deletions(-) create mode 100644 engine/classes.c diff --git a/engine/api.c b/engine/api.c index 3c87df3..267529d 100644 --- a/engine/api.c +++ b/engine/api.c @@ -1606,7 +1606,7 @@ int ph7_value_string(ph7_value *pVal, const char *zString, int nLen) { /* Compute length automatically */ nLen = (int)SyStrlen(zString); } - SyBlobAppend(&pVal->sBlob, (const void *)zString, (sxu32)nLen); + return SyBlobAppend(&pVal->sBlob, (const void *)zString, (sxu32)nLen); } return PH7_OK; } diff --git a/engine/classes.c b/engine/classes.c new file mode 100644 index 0000000..f07505c --- /dev/null +++ b/engine/classes.c @@ -0,0 +1,423 @@ +/** + * @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; +} diff --git a/engine/compiler.c b/engine/compiler.c index a5ab8fc..ff5fcac 100644 --- a/engine/compiler.c +++ b/engine/compiler.c @@ -107,6 +107,7 @@ static sxi32 PH7_GenStateEnterBlock( * a tiny chunk of memory, there is no much we can do here. */ PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory"); + return SXERR_ABORT; } /* Zero the structure */ SyZero(pBlock, sizeof(GenBlock)); @@ -284,6 +285,7 @@ static ph7_value *PH7_GenStateInstallNumLiteral(ph7_gen_state *pGen, sxu32 *pIdx pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx); if(pObj == 0) { PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory"); + return 0; } *pIdx = nIdx; return pObj; @@ -442,6 +444,7 @@ PH7_PRIVATE sxi32 PH7_CompileSimpleString(ph7_gen_state *pGen, sxi32 iCompileFla pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx); if(pObj == 0) { PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory"); + return SXERR_ABORT; } PH7_MemObjInitFromString(pGen->pVm, pObj, 0); /* Compile the node */ @@ -1980,6 +1983,10 @@ static void GenStateCloseScope(ph7_gen_state *pGen, sxu32 nFirst) { sxu32 n, nEnd = PH7_VmInstrLength(pGen->pVm); pScope = (VmScopeInfo *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(VmScopeInfo)); if(pScope == 0) { + if(pGen->pVm->bInit) { + PH7_GenCompileError(pGen, E_ERROR, 1, "PH7 engine is running out-of-memory"); + return; + } PH7_VmMemoryError(pGen->pVm); } pScope->pParent = 0; @@ -3371,6 +3378,7 @@ static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen SyStringInitFromBuf(&sArg.sClass, zDup, pName->nByte); } else { PH7_GenCompileError(pGen, E_ERROR, pIn->nLine, "PH7 engine is running out-of-memory"); + return SXERR_ABORT; } } pIn++; @@ -3399,6 +3407,7 @@ static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen zDup = SyMemBackendStrDup(&pGen->pVm->sAllocator, SyStringData(&pIn->sData), SyStringLength(&pIn->sData)); if(zDup == 0) { PH7_GenCompileError(&(*pGen), E_ERROR, pIn->nLine, "PH7 engine is running out-of-memory"); + return SXERR_ABORT; } SyStringInitFromBuf(&sArg.sName, zDup, SyStringLength(&pIn->sData)); pIn++; @@ -3489,7 +3498,10 @@ static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen SyBlobRelease(&sSig); } /* Save in the argument set */ - SySetPut(&pFunc->aArgs, (const void *)&sArg); + rc = SySetPut(&pFunc->aArgs, (const void *)&sArg); + if(rc != SXRET_OK && pGen->pVm->bInit) { + return SXERR_ABORT; + } } if(SyBlobLength(&sSig) > 0) { /* Save function signature */ @@ -3514,6 +3526,7 @@ static sxi32 PH7_GenStateCompileFuncBody( rc = PH7_GenStateEnterBlock(&(*pGen), GEN_BLOCK_PROTECTED | GEN_BLOCK_FUNC, PH7_VmInstrLength(pGen->pVm), pFunc, &pBlock); if(rc != SXRET_OK) { PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory"); + return SXERR_ABORT; } /* The formal arguments are declared for the whole body: hand their pinned * categories to the static comparison checker, owned by this body block. @@ -3541,7 +3554,11 @@ static sxi32 PH7_GenStateCompileFuncBody( pInstrContainer = PH7_VmGetByteCodeContainer(pGen->pVm); PH7_VmSetByteCodeContainer(pGen->pVm, &pFunc->aByteCode); /* Compile the body */ - PH7_CompileBlock(&(*pGen)); + rc = PH7_CompileBlock(&(*pGen)); + if(rc == SXERR_ABORT || pGen->pVm->iInitError) { + PH7_VmSetByteCodeContainer(pGen->pVm, pInstrContainer); + return SXERR_ABORT; + } /* Fix exception jumps now the destination is resolved */ PH7_GenStateFixJumps(pGen->pCurrent, PH7_OP_THROW, PH7_VmInstrLength(pGen->pVm)); /* Mark the implicit function epilogue, distinct from other DONE instructions. */ @@ -3885,6 +3902,7 @@ loop: pAttr = PH7_NewClassAttr(pGen->pVm, pClass, pName, nLine, iProtection, iFlags, nType); if(pAttr == 0) { PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 engine is running out-of-memory"); + return SXERR_ABORT; } if(pGen->pIn->nType & PH7_TK_EQUAL /*'='*/) { SySet *pInstrContainer; @@ -3895,6 +3913,10 @@ loop: /* Compile attribute value. */ rc = PH7_CompileExpr(&(*pGen), EXPR_FLAG_COMMA_STATEMENT, 0); + if(pGen->pVm->iInitError || rc == SXERR_ABORT) { + PH7_VmSetByteCodeContainer(pGen->pVm, pInstrContainer); + return SXERR_ABORT; + } if(rc == SXERR_EMPTY) { PH7_GenCompileError(pGen, E_ERROR, nLine, "Attribute '%z': Missing default value", pName); } @@ -3987,6 +4009,7 @@ static sxi32 PH7_GenStateCompileClassMethod( pMeth = PH7_NewClassMethod(pGen->pVm, pClass, pName, nLine, iProtection, iFlags); if(pMeth == 0) { PH7_GenCompileError(&(*pGen), E_ERROR, nLine, "PH7 is running out-of-memory"); + return SXERR_ABORT; } /* Jump the left parenthesis '(' */ pGen->pIn++; @@ -4078,13 +4101,16 @@ static sxi32 PH7_CompileClassInterface(ph7_gen_state *pGen) { pGen->pIn++; /* Obtain a raw class */ pClass = PH7_NewRawClass(pGen->pVm, pName); - if(pClass == 0) { + if(pClass == 0 || (pGen->pVm->bInit && + (!pClass->hMethod.apBucket || !pClass->hAttr.apBucket || !pClass->hDerived.apBucket))) { PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory"); + return SXERR_ABORT; } /* Obtain a raw class inheritance storage */ pClassInfo = PH7_NewClassInfo(pGen->pVm, pName); if(pClassInfo == 0) { PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory"); + return SXERR_ABORT; } /* Mark as an interface */ pClass->pInfo = pClassInfo; @@ -4288,13 +4314,16 @@ static sxi32 PH7_GenStateCompileClass(ph7_gen_state *pGen, sxi32 iFlags) { pGen->pIn++; /* Obtain a raw class */ pClass = PH7_NewRawClass(pGen->pVm, pName); - if(pClass == 0) { + if(pClass == 0 || (pGen->pVm->bInit && + (!pClass->hMethod.apBucket || !pClass->hAttr.apBucket || !pClass->hDerived.apBucket))) { PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory"); + return SXERR_ABORT; } /* Obtain a raw class inheritance storage */ pClassInfo = PH7_NewClassInfo(pGen->pVm, pName); if(pClassInfo == 0) { PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory"); + return SXERR_ABORT; } pClass->pInfo = pClassInfo; /* Assume a standalone class */ @@ -5598,7 +5627,9 @@ static sxi32 PH7_CompileExpr( nExpr = 0; pRoot = 0; SySetInit(&sExprNode, &pGen->pVm->sAllocator, sizeof(ph7_expr_node *)); - SySetAlloc(&sExprNode, 0x10); + if(SySetAlloc(&sExprNode, 0x10) != SXRET_OK && pGen->pVm->bInit) { + return SXERR_ABORT; + } rc = SXRET_OK; /* Delimit the expression */ pEnd = pGen->pIn; @@ -5673,6 +5704,9 @@ static sxi32 PH7_CompileExpr( pGen->pEnd = pEnd; /* Try to get an expression tree */ rc = PH7_ExprMakeTree(&(*pGen), &sExprNode, &pRoot); + if(rc != SXRET_OK && pGen->pVm->bInit) { + rc = SXERR_ABORT; + } if(rc == SXRET_OK && pRoot) { rc = SXRET_OK; if(xTreeValidator) { @@ -5859,8 +5893,9 @@ static sxi32 PH7_GenStateCompileChunk( /* Compile the statement */ rc = xCons(&(*pGen)); } - if(rc == SXERR_ABORT) { + if(rc == SXERR_ABORT || pGen->pVm->iInitError) { /* Request to abort compilation */ + rc = SXERR_ABORT; break; } } @@ -5902,8 +5937,9 @@ static sxi32 PH7_GenStateCompileGlobalScope( } /* Compile the statement */ rc = xCons(&(*pGen)); - if(rc == SXERR_ABORT) { + if(rc == SXERR_ABORT || pGen->pVm->iInitError) { /* Request to abort compilation */ + rc = SXERR_ABORT; break; } } else { @@ -5936,7 +5972,10 @@ static sxi32 PH7_CompileScript( /* Advance the stream cursor */ pGen->pRawIn++; /* Tokenize the Aer chunk first */ - PH7_TokenizeAerScript(SyStringData(&pScript->sData), SyStringLength(&pScript->sData), pScript->nLine, &(*pTokenSet)); + rc = PH7_TokenizeAerScript(SyStringData(&pScript->sData), SyStringLength(&pScript->sData), pScript->nLine, &(*pTokenSet)); + if(rc != SXRET_OK && pGen->pVm->bInit) { + return SXERR_ABORT; + } /* Point to the head and tail of the token stream. */ pGen->pIn = (SyToken *)SySetBasePtr(pTokenSet); pGen->pEnd = &pGen->pIn[SySetUsed(pTokenSet)]; @@ -5989,12 +6028,19 @@ PH7_PRIVATE sxi32 PH7_CompileAerScript( /* Initialize the tokens containers */ SySetInit(&aRawToken, &pVm->sAllocator, sizeof(SyToken)); SySetInit(&aAerToken, &pVm->sAllocator, sizeof(SyToken)); - SySetAlloc(&aAerToken, 0xc0); + rc = SySetAlloc(&aAerToken, 0xc0); + if(rc != SXRET_OK && pVm->bInit) { + return rc; + } SyToken sTmp; sTmp.nLine = 1; sTmp.pUserData = 0; SyStringDupPtr(&sTmp.sData, pScript); - SySetPut(&aRawToken, (const void *)&sTmp); + rc = SySetPut(&aRawToken, (const void *)&sTmp); + if(rc != SXRET_OK && pVm->bInit) { + SySetRelease(&aAerToken); + return rc; + } pCodeGen = &pVm->sCodeGen; /* Process high-level tokens */ pCodeGen->pRawIn = (SyToken *)SySetBasePtr(&aRawToken); @@ -6084,9 +6130,11 @@ PH7_PRIVATE sxi32 PH7_InitCodeGenerator( pGen->pErrData = pErrData; SySetInit(&pGen->aLabel, &pVm->sAllocator, sizeof(Label)); SySetInit(&pGen->aGoto, &pVm->sAllocator, sizeof(JumpFixup)); - SyHashInit(&pGen->hLiteral, &pVm->sAllocator, 0, 0); - SyHashInit(&pGen->hVar, &pVm->sAllocator, 0, 0); - SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0); + if(SyHashInit(&pGen->hLiteral, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pGen->hVar, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0) != SXRET_OK) { + return SXERR_MEM; + } /* Error log buffer */ SyBlobInit(&pGen->sErrBuf, &pVm->sAllocator); /* General purpose working buffer */ @@ -6124,7 +6172,9 @@ PH7_PRIVATE sxi32 PH7_ResetCodeGenerator( * maps carrying no scope facts; the include path releases them, and this * fresh table, at the end of the unit. */ SyHashRelease(&pGen->hDeclType); - SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0); + if(SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0) != SXRET_OK && pVm->bInit) { + return SXERR_MEM; + } /* Point to the global scope */ pBlock = pGen->pCurrent; while(pBlock->pParent != 0) { @@ -6152,6 +6202,12 @@ PH7_PRIVATE sxi32 PH7_GenCompileError(ph7_gen_state *pGen, sxi32 nErrType, sxu32 SyString **ppFile; va_list ap; sxi32 rc; + /* Only the private, metadata-only builtin bootstrap is recoverable. + * Ordinary compilation retains its existing fatal diagnostics/policy. */ + if(nErrType == E_ERROR && pGen->pVm->bInit) { + pGen->pVm->iInitError = SXERR_MEM; + return SXERR_ABORT; + } /* Reset the working buffer */ SyBlobReset(pWorker); /* Peek the processed file path if available */ diff --git a/engine/lib/dataset.c b/engine/lib/dataset.c index 115cb28..b8b4019 100644 --- a/engine/lib/dataset.c +++ b/engine/lib/dataset.c @@ -22,18 +22,16 @@ PH7_PRIVATE sxi32 SySetPut(SySet *pSet, const void *pItem) { unsigned char *zbase; if(pSet->nUsed >= pSet->nSize) { void *pNew; + sxu32 nSize = pSet->nSize > 0 ? pSet->nSize * 2 : 8; if(pSet->pAllocator == 0) { return SXERR_LOCKED; } - if(pSet->nSize <= 0) { - pSet->nSize = 4; - } - pNew = SyMemBackendRealloc(pSet->pAllocator, pSet->pBase, pSet->eSize * pSet->nSize * 2); + pNew = SyMemBackendRealloc(pSet->pAllocator, pSet->pBase, pSet->eSize * nSize); if(pNew == 0) { return SXERR_MEM; } pSet->pBase = pNew; - pSet->nSize <<= 1; + pSet->nSize = nSize; } zbase = (unsigned char *)pSet->pBase; SX_MACRO_FAST_MEMCPY(pItem, &zbase[pSet->nUsed * pSet->eSize], pSet->eSize); @@ -384,4 +382,4 @@ PH7_PRIVATE SyHashEntry *SyHashLastEntry(SyHash *pHash) { } /* Last inserted entry */ return (SyHashEntry *)pHash->pList; -} \ No newline at end of file +} diff --git a/engine/parser.c b/engine/parser.c index 078e78c..430d00d 100644 --- a/engine/parser.c +++ b/engine/parser.c @@ -1288,7 +1288,11 @@ PH7_PRIVATE sxi32 PH7_ExprMakeTree(ph7_gen_state *pGen, SySet *pExprNode, ph7_ex return rc; } /* Save the extracted node */ - SySetPut(pExprNode, (const void *)&pNode); + rc = SySetPut(pExprNode, (const void *)&pNode); + if(rc != SXRET_OK && pGen->pVm->bInit) { + SyMemBackendPoolFree(&pGen->pVm->sAllocator, pNode); + return SXERR_ABORT; + } } if(SySetUsed(pExprNode) < 1) { /* Empty expression [i.e: A semi-colon;] */ diff --git a/engine/vfs.c b/engine/vfs.c index c2b86bd..e55b818 100644 --- a/engine/vfs.c +++ b/engine/vfs.c @@ -3606,10 +3606,16 @@ static int PH7_builtin_closedir(ph7_context *pCtx, int nArg, ph7_value **apArg) ph7_result_bool(pCtx, 0); return PH7_OK; } - /* Perform the requested operation */ - pStream->xCloseDir(pDev->pHandle); - /* Release the private stucture */ - ReleaseIOPrivate(pCtx, pDev); + /* Resources are copied raw pointers. Keep a directory-only tombstone until + * VM backend release, so stale aliases cannot dereference freed storage or + * close a newly opened handle at the same address. Invalidate before the + * device callback, which may re-enter the VM. One tiny wrapper per successful + * open is retained; the OS handle and buffer are released immediately. */ + void *pHandle = pDev->pHandle; + pDev->iMagic = 0x2126; + pDev->pHandle = 0; + SyBlobRelease(&pDev->sBuffer); + pStream->xCloseDir(pHandle); PH7_MemObjRelease(apArg[0]); return PH7_OK; } diff --git a/engine/vm.c b/engine/vm.c index f2fdfad..0868243 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -540,8 +540,12 @@ static VmFrame *VmNewFrame( pFrame->pUserData = pUserData; pFrame->pThis = pThis; pFrame->pVm = pVm; - SyHashInit(&pFrame->hConst, &pVm->sAllocator, 0, 0); - SyHashInit(&pFrame->hVar, &pVm->sAllocator, 0, 0); + if(SyHashInit(&pFrame->hConst, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pFrame->hVar, &pVm->sAllocator, 0, 0) != SXRET_OK) { + SyHashRelease(&pFrame->hConst); + SyMemBackendPoolFree(&pVm->sAllocator, pFrame); + return 0; + } SySetInit(&pFrame->sArg, &pVm->sAllocator, sizeof(VmSlot)); SySetInit(&pFrame->sArgPin, &pVm->sAllocator, sizeof(VmSlot)); SySetInit(&pFrame->sLocal, &pVm->sAllocator, sizeof(VmSlot)); @@ -1715,8 +1719,7 @@ static sxi32 VmPrepareNativeClass(ph7_vm *pVm, const ph7_class_def *pDef) { pVm->apRefObj = apRef; pVm->nRefSize = 16; } - /* SySetPut's first failed growth leaves a nonzero capacity without - * storage. Preallocate the VM sets that must survive a failed transaction. */ + /* Preallocate the VM sets that must survive a failed transaction. */ if((pVm->aFreeObj.nSize == 0 && SySetAlloc(&pVm->aFreeObj, 8) != SXRET_OK) || (pVm->aTypeNames.nSize == 0 && SySetAlloc(&pVm->aTypeNames, 8) != SXRET_OK)) { return PH7_NOMEM; @@ -2341,130 +2344,6 @@ PH7_PRIVATE ph7_value *VmReserveMemObj(ph7_vm *pVm, sxu32 *pIndex) { } /* Forward declaration */ static sxi32 VmEvalChunk(ph7_vm *pVm, ph7_context *pCtx, SyString *pChunk, int iFlags, ph7_value *pResult, ph7_include *pInclude); -/* - * Built-in classes/interfaces and some functions that cannot be implemented - * directly as foreign functions. - */ -#define PH7_BUILTIN_LIB \ - "class Exception { "\ - "protected string $message = 'Unknown exception';"\ - "protected int $code = 0;"\ - "protected string $file;"\ - "protected int $line;"\ - "protected mixed[] $trace;"\ - "protected object $previous;"\ - "public void __construct(string $message = '', int $code = 0, Exception $previous = null) {"\ - " if($message) {"\ - " $this->message = $message;"\ - " }"\ - " $this->code = $code;"\ - " $this->file = __FILE__;"\ - " $this->line = __LINE__;"\ - " $this->trace = debug_backtrace();"\ - " if($previous) {"\ - " $this->previous = $previous;"\ - " }"\ - "}"\ - "public string getMessage() {"\ - " return $this->message;"\ - "}"\ - " public int getCode() {"\ - " return $this->code;"\ - "}"\ - "public string getFile() {"\ - " return $this->file;"\ - "}"\ - "public int getLine() {"\ - " return $this->line;"\ - "}"\ - "public mixed getTrace() {"\ - " return $this->trace;"\ - "}"\ - "public string getTraceAsString() {"\ - " return debug_string_backtrace();"\ - "}"\ - "public object getPrevious() {"\ - " return $this->previous;"\ - "}"\ - "public string __toString(){"\ - " return $this->file + ' ' + $this->line + ' ' + $this->code + ' ' + $this->message;"\ - "}"\ - "}"\ - "class ErrorException extends Exception { "\ - "protected int $severity;"\ - "public void __construct(string $message = '',"\ - "int $code = 0, int $severity = 1, string $filename = __FILE__ , int $lineno = __LINE__ , Exception $previous = null) {"\ - " if($message) {"\ - " $this->message = $message;"\ - " }"\ - " $this->severity = $severity;"\ - " $this->code = $code;"\ - " $this->file = $filename;"\ - " $this->line = $lineno;"\ - " $this->trace = debug_backtrace();"\ - " if($previous) {"\ - " $this->previous = $previous;"\ - " }"\ - "}"\ - "public int getSeverity(){"\ - " return $this->severity;"\ - "}"\ - "}"\ - "interface Iterator {"\ - "public mixed current();"\ - "public mixed key();"\ - "public void next();"\ - "public void rewind();"\ - "public bool valid();"\ - "}"\ - "interface IteratorAggregate {"\ - "public mixed getIterator();"\ - "}"\ - "interface Serializable {"\ - "public string serialize();"\ - "public void unserialize(string $serialized);"\ - "}"\ - "/* Directory related IO */"\ - "class Directory {"\ - "public resource $handle;"\ - "public string $path;"\ - "public void __construct(string $path)"\ - "{"\ - " $this->handle = opendir($path);"\ - " if($this->handle) {"\ - " $this->path = $path;"\ - " }"\ - "}"\ - "public void __destruct()"\ - "{"\ - " if($this->handle) {"\ - " closedir($this->handle);"\ - " }"\ - "}"\ - "public string read()"\ - "{"\ - " return readdir($this->handle);"\ - "}"\ - "public void rewind()"\ - "{"\ - " rewinddir($this->handle);"\ - "}"\ - "public void close()"\ - "{"\ - " closedir($this->handle);"\ - " $this->handle = 0;"\ - "}"\ - "}"\ - "class stdClass{"\ - " public mixed $value;"\ - " /* Magic methods */"\ - " public int __toInt(){ return (int)$this->value; }"\ - " public bool __toBool(){ return (bool)$this->value; }"\ - " public float __toFloat(){ return (float)$this->value; }"\ - " public string __toString(){ return (string)$this->value; }"\ - " void __construct(mixed $v){ $this->value = $v; }"\ - "}" - /* * Initialize a freshly allocated PH7 Virtual Machine so that we can * start compiling the target PHP program. @@ -2474,34 +2353,49 @@ PH7_PRIVATE sxi32 PH7_VmInit( ph7 *pEngine, /* Master engine */ sxbool bDebug /* Debugging */ ) { - SyString sBuiltin; ph7_value *pObj; sxi32 rc; /* Zero the structure */ SyZero(pVm, sizeof(ph7_vm)); /* Initialize VM fields */ pVm->pEngine = &(*pEngine); - SyMemBackendInitFromParent(&pVm->sAllocator, &pEngine->sAllocator); + rc = SyMemBackendInitFromParent(&pVm->sAllocator, &pEngine->sAllocator); + if(rc != SXRET_OK) { + return rc; + } + pVm->bInit = TRUE; /* Instructions containers */ SySetInit(&pVm->aInstrSet, &pVm->sAllocator, sizeof(VmInstr)); SySetInit(&pVm->aByteCode, &pVm->sAllocator, sizeof(VmInstr)); - SySetAlloc(&pVm->aByteCode, 0xFF); + rc = SySetAlloc(&pVm->aByteCode, 0xFF); + if(rc != SXRET_OK) { + goto Err; + } pVm->pByteContainer = &pVm->aByteCode; /* Object containers */ SySetInit(&pVm->aMemObj, &pVm->sAllocator, sizeof(ph7_value)); - SySetAlloc(&pVm->aMemObj, 0xFF); + rc = SySetAlloc(&pVm->aMemObj, 0xFF); + if(rc != SXRET_OK) { + goto Err; + } /* Virtual machine internal containers */ SyBlobInit(&pVm->sConsumer, &pVm->sAllocator); SyBlobInit(&pVm->sArgv, &pVm->sAllocator); SyBlobInit(&pVm->sArgvEntries, &pVm->sAllocator); SySetInit(&pVm->aLitObj, &pVm->sAllocator, sizeof(ph7_value)); - SySetAlloc(&pVm->aLitObj, 0xFF); - SyHashInit(&pVm->hHostFunction, &pVm->sAllocator, 0, 0); - SyHashInit(&pVm->hFunction, &pVm->sAllocator, 0, 0); - SyHashInit(&pVm->hClass, &pVm->sAllocator, SyStrHash, (int (*)(const void *, const void *, sxu32))((SyStrncmp))); - SyHashInit(&pVm->hConstant, &pVm->sAllocator, 0, 0); - SyHashInit(&pVm->hSuper, &pVm->sAllocator, 0, 0); - SyHashInit(&pVm->hDBAL, &pVm->sAllocator, 0, 0); + rc = SySetAlloc(&pVm->aLitObj, 0xFF); + if(rc != SXRET_OK) { + goto Err; + } + if(SyHashInit(&pVm->hHostFunction, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pVm->hFunction, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pVm->hClass, &pVm->sAllocator, SyStrHash, (int (*)(const void *, const void *, sxu32))((SyStrncmp))) != SXRET_OK || + SyHashInit(&pVm->hConstant, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pVm->hSuper, &pVm->sAllocator, 0, 0) != SXRET_OK || + SyHashInit(&pVm->hDBAL, &pVm->sAllocator, 0, 0) != SXRET_OK) { + rc = SXERR_MEM; + goto Err; + } SySetInit(&pVm->aFreeObj, &pVm->sAllocator, sizeof(VmSlot)); SySetInit(&pVm->aSelf, &pVm->sAllocator, sizeof(ph7_class *)); SySetInit(&pVm->aAutoLoad, &pVm->sAllocator, sizeof(VmAutoLoadCB)); @@ -2564,15 +2458,21 @@ PH7_PRIVATE sxi32 PH7_VmInit( } /* VM correctly initialized,set the magic number */ pVm->nMagic = PH7_VM_INIT; - SyStringInitFromBuf(&sBuiltin, PH7_BUILTIN_LIB, sizeof(PH7_BUILTIN_LIB) - 1); - /* Precompile the built-in library */ - VmEvalChunk(&(*pVm), 0, &sBuiltin, PH7_AERSCRIPT_CODE, 0, 0); + /* Metadata only: functions and IO devices are installed by MakeReady. */ + rc = PH7_VmInstallBuiltinClasses(pVm); + if(rc != SXRET_OK) { + goto Err; + } if(bDebug) { /* Enable debugging */ pVm->bDebug = TRUE; } /* Reset the code generator */ - PH7_ResetCodeGenerator(&(*pVm), pEngine->xConf.xErr, pEngine->xConf.pErrData); + rc = PH7_ResetCodeGenerator(&(*pVm), pEngine->xConf.xErr, pEngine->xConf.pErrData); + if(rc != SXRET_OK) { + goto Err; + } + pVm->bInit = FALSE; return SXRET_OK; Err: SyMemBackendRelease(&pVm->sAllocator); @@ -11426,21 +11326,28 @@ static sxi32 VmEvalChunk( /* Initialize bytecode container */ SySetInit(&aByteCode, &pVm->sAllocator, sizeof(VmInstr)); rc = SySetAlloc(&aByteCode, 0x20); - if(rc != SXRET_OK && bRequire) { + if(rc != SXRET_OK && (bRequire || pVm->bInit)) { return rc; } /* Log compile-time errors */ xErr = pVm->pEngine->xConf.xErr; pErrData = pVm->pEngine->xConf.pErrData; if(!bInclude) { - PH7_ResetCodeGenerator(pVm, xErr, pErrData); + rc = PH7_ResetCodeGenerator(pVm, xErr, pErrData); + if(rc != SXRET_OK && pVm->bInit) { + SySetRelease(&aByteCode); + return rc; + } } /* Swap bytecode container */ pByteCode = pVm->pByteContainer; pVm->pByteContainer = &aByteCode; /* Push memory as a processed file path */ if((iFlags & PH7_AERSCRIPT_FILE) == 0) { - PH7_VmPushFilePath(pVm, "[MEMORY]", -1, TRUE, 0); + rc = PH7_VmPushFilePath(pVm, "[MEMORY]", -1, TRUE, 0); + if(rc != SXRET_OK && pVm->bInit) { + goto Cleanup; + } } /* Compile the chunk */ if(bInclude) { @@ -11452,7 +11359,10 @@ static sxi32 VmEvalChunk( } else { rc = PH7_CompileAerScript(pVm, pChunk, iFlags); } - if(rc != SXRET_OK && (bRequire || bInclude)) { + if(pVm->iInitError) { + rc = pVm->iInitError; + } + if(rc != SXRET_OK && (bRequire || bInclude || pVm->bInit)) { goto Cleanup; } ph7_value sResult; /* Return value */ @@ -11515,7 +11425,7 @@ Cleanup: /* Cleanup the mess left behind */ pVm->pByteContainer = pByteCode; SySetRelease(&aByteCode); - return bRequire || bInclude || rc == PH7_EXCEPTION || rc == PH7_ABORT ? rc : SXRET_OK; + return pVm->bInit || bRequire || bInclude || rc == PH7_EXCEPTION || rc == PH7_ABORT ? rc : SXRET_OK; } /* * value eval(string $code) diff --git a/include/ph7int.h b/include/ph7int.h index 35796e6..b753085 100644 --- a/include/ph7int.h +++ b/include/ph7int.h @@ -1266,6 +1266,8 @@ struct ph7_vm { void *pStdout; /* STDOUT IO stream */ void *pStderr; /* STDERR IO stream */ sxbool bDebug; /* TRUE to enable debugging */ + sxbool bInit; /* Unpublished bootstrap: errors return to bulk cleanup */ + sxi32 iInitError; /* Sticky bootstrap compiler failure */ 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 */ @@ -1765,6 +1767,7 @@ 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 sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm); 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"