Code inclusion rework that include several changes (#58):
The build was successful. Details

* include() builtin function allows now to include a chunk of code, eg. a body of some loop
 * include() allows to load and execute file several times
 * require() still allows to include file just once and included file must have a valid OOP syntax
 * both functions will throw E_ERROR when impossible to include specified file
This commit is contained in:
Rafal Kupiec 2019-11-27 20:07:50 +01:00
parent 63fd76c9c8
commit cb71daec12
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
2 changed files with 11 additions and 15 deletions

View File

@ -8875,7 +8875,7 @@ static sxi32 VmEvalChunk(
ph7_vm *pVm, /* Underlying Virtual Machine */ ph7_vm *pVm, /* Underlying Virtual Machine */
ph7_context *pCtx, /* Call Context */ ph7_context *pCtx, /* Call Context */
SyString *pChunk, /* PHP chunk to evaluate */ SyString *pChunk, /* PHP chunk to evaluate */
int iFlags /* Compile flag */ int iFlags /* Code evaluation flag */
) { ) {
SySet *pByteCode, aByteCode; SySet *pByteCode, aByteCode;
ProcConsumer xErr = 0; ProcConsumer xErr = 0;
@ -8891,7 +8891,7 @@ static sxi32 VmEvalChunk(
pByteCode = pVm->pByteContainer; pByteCode = pVm->pByteContainer;
pVm->pByteContainer = &aByteCode; pVm->pByteContainer = &aByteCode;
/* Push memory as a processed file path */ /* Push memory as a processed file path */
if((iFlags & PH7_AERSCRIPT_CODE) == 0) { if((iFlags & PH7_AERSCRIPT_FILE) == 0) {
PH7_VmPushFilePath(pVm, "[MEMORY]", -1, TRUE, 0); PH7_VmPushFilePath(pVm, "[MEMORY]", -1, TRUE, 0);
} }
/* Compile the chunk */ /* Compile the chunk */
@ -9042,7 +9042,7 @@ PH7_PRIVATE sxi32 PH7_VmPushFilePath(ph7_vm *pVm, const char *zPath, int nLen, s
static sxi32 VmExecIncludedFile( static sxi32 VmExecIncludedFile(
ph7_context *pCtx, /* Call Context */ ph7_context *pCtx, /* Call Context */
SyString *pPath, /* Script path or URL*/ SyString *pPath, /* Script path or URL*/
int IncludeOnce /* TRUE if called from include_once() or require_once() */ int iFlags /* Code evaluation flag */
) { ) {
sxi32 rc; sxi32 rc;
const ph7_io_stream *pStream; const ph7_io_stream *pStream;
@ -9065,8 +9065,8 @@ static sxi32 VmExecIncludedFile(
return SXERR_IO; return SXERR_IO;
} }
rc = SXRET_OK; /* Stupid cc warning */ rc = SXRET_OK; /* Stupid cc warning */
if(IncludeOnce && !isNew) { if(iFlags & PH7_AERSCRIPT_CODE && !isNew) {
/* Already included */ /* Already included (required) */
rc = SXERR_EXISTS; rc = SXERR_EXISTS;
} else { } else {
/* Read the whole file contents */ /* Read the whole file contents */
@ -9076,7 +9076,7 @@ static sxi32 VmExecIncludedFile(
/* Compile and execute the script */ /* Compile and execute the script */
SyStringInitFromBuf(&sScript, SyBlobData(&sContents), SyBlobLength(&sContents)); SyStringInitFromBuf(&sScript, SyBlobData(&sContents), SyBlobLength(&sContents));
pVm->nMagic = PH7_VM_INCL; pVm->nMagic = PH7_VM_INCL;
VmEvalChunk(pCtx->pVm, &(*pCtx), &sScript, PH7_AERSCRIPT_CODE); VmEvalChunk(pCtx->pVm, &(*pCtx), &sScript, iFlags);
pVm->nMagic = PH7_VM_EXEC; pVm->nMagic = PH7_VM_EXEC;
} }
} }
@ -9257,15 +9257,10 @@ static int vm_builtin_include(ph7_context *pCtx, int nArg, ph7_value **apArg) {
return SXRET_OK; return SXRET_OK;
} }
/* Open,compile and execute the desired script */ /* Open,compile and execute the desired script */
rc = VmExecIncludedFile(&(*pCtx), &sFile, TRUE); rc = VmExecIncludedFile(&(*pCtx), &sFile, PH7_AERSCRIPT_CHNK | PH7_AERSCRIPT_FILE);
if(rc == SXERR_EXISTS) {
/* File already included, return TRUE */
ph7_result_bool(pCtx, 1);
return SXRET_OK;
}
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
/* Emit a warning and return false */ /* Emit a warning and return false */
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "IO error while importing: '%z'", &sFile); PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "IO error while including file: '%z'", &sFile);
ph7_result_bool(pCtx, 0); ph7_result_bool(pCtx, 0);
} }
return SXRET_OK; return SXRET_OK;
@ -9292,7 +9287,7 @@ static int vm_builtin_require(ph7_context *pCtx, int nArg, ph7_value **apArg) {
return SXRET_OK; return SXRET_OK;
} }
/* Open,compile and execute the desired script */ /* Open,compile and execute the desired script */
rc = VmExecIncludedFile(&(*pCtx), &sFile, TRUE); rc = VmExecIncludedFile(&(*pCtx), &sFile, PH7_AERSCRIPT_CODE | PH7_AERSCRIPT_FILE);
if(rc == SXERR_EXISTS) { if(rc == SXERR_EXISTS) {
/* File already included, return TRUE */ /* File already included, return TRUE */
ph7_result_bool(pCtx, 1); ph7_result_bool(pCtx, 1);
@ -9300,7 +9295,7 @@ static int vm_builtin_require(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
/* Fatal,abort VM execution immediately */ /* Fatal,abort VM execution immediately */
PH7_VmThrowError(pCtx->pVm, PH7_CTX_ERR, "Fatal IO error while importing: '%z'", &sFile); PH7_VmThrowError(pCtx->pVm, PH7_CTX_ERR, "IO error while including file: '%z'", &sFile);
} }
return SXRET_OK; return SXRET_OK;
} }

View File

@ -360,6 +360,7 @@ typedef sxi64 ph7_int64;
#define PH7_AERSCRIPT_CODE 0x01 /* AerScript Code */ #define PH7_AERSCRIPT_CODE 0x01 /* AerScript Code */
#define PH7_AERSCRIPT_CHNK 0x02 /* AerScript Chunk of Code */ #define PH7_AERSCRIPT_CHNK 0x02 /* AerScript Chunk of Code */
#define PH7_AERSCRIPT_EXPR 0x04 /* AerScript Expression */ #define PH7_AERSCRIPT_EXPR 0x04 /* AerScript Expression */
#define PH7_AERSCRIPT_FILE 0x08 /* AerScript File Inclusion */
/* /*
* Call Context Error Message Severity Level. * Call Context Error Message Severity Level.
* *