Browse Source

Move VM initialization into separate step to enable errors reporting earlier.

pull/21/head
Rafal Kupiec 3 years ago
parent
commit
63b0f450d3
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
3 changed files with 67 additions and 50 deletions
  1. +40
    -28
      engine/api.c
  2. +0
    -4
      engine/vm.c
  3. +27
    -18
      sapi/cli/main.c

+ 40
- 28
engine/api.c View File

@ -604,6 +604,44 @@ int ph7_release(ph7 *pEngine) {
SyMemBackendPoolFree(&sMPGlobal.sAllocator, pEngine);
return rc;
}
int ph7_vm_init(
ph7 *pEngine, /* Running PH7 engine */
ph7_vm **ppOutVm /* OUT: A pointer to the virtual machine */
) {
ph7_vm *pVm;
int rc;
if(ppOutVm) {
*ppOutVm = 0;
}
/* Allocate a new virtual machine */
pVm = (ph7_vm *)SyMemBackendPoolAlloc(&pEngine->sAllocator, sizeof(ph7_vm));
if(pVm == 0) {
/* If the supplied memory subsystem is so sick that we are unable to allocate
* a tiny chunk of memory, there is no much we can do here. */
if(ppOutVm) {
*ppOutVm = 0;
}
return PH7_NOMEM;
}
/* Initialize the Virtual Machine */
rc = PH7_VmInit(pVm, &(*pEngine));
if(rc != PH7_OK) {
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
if(ppOutVm) {
*ppOutVm = 0;
}
return PH7_VM_ERR;
}
/* Reset the error message consumer */
SyBlobReset(&pEngine->xConf.sErrConsumer);
/* Set the default VM output consumer callback and it's
* private data. */
pVm->sVmConsumer.xConsumer = PH7_VmBlobConsumer;
pVm->sVmConsumer.pUserData = &pVm->sConsumer;
/* Point to the freshly created VM */
*ppOutVm = pVm;
return PH7_OK;
}
/*
* Compile a raw PHP script.
* To execute a PHP code, it must first be compiled into a byte-code program using this routine.
@ -621,33 +659,14 @@ static sxi32 ProcessScript(
sxi32 iFlags, /* Compile-time flags */
const char *zFilePath /* File path if script come from a file. NULL otherwise */
) {
ph7_vm *pVm;
ph7_vm *pVm = *ppVm;
int iFileDir, rc;
char *pFileDir, *fFilePath[PATH_MAX + 1];
char *pFilePath[PATH_MAX + 1];
/* Allocate a new virtual machine */
pVm = (ph7_vm *)SyMemBackendPoolAlloc(&pEngine->sAllocator, sizeof(ph7_vm));
if(pVm == 0) {
/* If the supplied memory subsystem is so sick that we are unable to allocate
* a tiny chunk of memory, there is no much we can do here. */
if(ppVm) {
*ppVm = 0;
}
return PH7_NOMEM;
}
if(iFlags < 0) {
/* Default compile-time flags */
iFlags = 0;
}
/* Initialize the Virtual Machine */
rc = PH7_VmInit(pVm, &(*pEngine));
if(rc != PH7_OK) {
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
if(ppVm) {
*ppVm = 0;
}
return PH7_VM_ERR;
}
/* Install local import path which is the current directory */
ph7_vm_config(pVm, PH7_VM_CONFIG_IMPORT_PATH, "./");
if(zFilePath && SyRealPath(zFilePath, fFilePath) == PH7_OK) {
@ -660,8 +679,6 @@ static sxi32 ProcessScript(
/* Push processed file path */
PH7_VmPushFilePath(pVm, pFilePath, -1, TRUE, 0);
}
/* Reset the error message consumer */
SyBlobReset(&pEngine->xConf.sErrConsumer);
/* Compile the script */
PH7_CompileScript(pVm, &(*pScript), iFlags);
if(pVm->sCodeGen.nErr > 0 || pVm == 0) {
@ -691,8 +708,6 @@ static sxi32 ProcessScript(
/* Script successfully compiled,link to the list of active virtual machines */
MACRO_LD_PUSH(pEngine->pVms, pVm);
pEngine->iVm++;
/* Point to the freshly created VM */
*ppVm = pVm;
/* Ready to execute PH7 bytecode */
return PH7_OK;
Release:
@ -772,9 +787,6 @@ int ph7_compile_v2(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm
int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int iFlags) {
const ph7_vfs *pVfs;
int rc;
if(ppOutVm) {
*ppOutVm = 0;
}
rc = PH7_OK; /* cc warning */
if(PH7_ENGINE_MISUSE(pEngine) || SX_EMPTY_STR(zFilePath)) {
return PH7_CORRUPT;
@ -859,7 +871,7 @@ int ph7_vm_config(ph7_vm *pVm, int iConfigOp, ...) {
return PH7_ABORT; /* Another thread have released this instance */
}
#endif
/* Confiugure the virtual machine */
/* Configure the virtual machine */
va_start(ap, iConfigOp);
rc = PH7_VmConfigure(&(*pVm), iConfigOp, ap);
va_end(ap);


+ 0
- 4
engine/vm.c View File

@ -1412,10 +1412,6 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
if(pVm->aOps == 0) {
return SXERR_MEM;
}
/* Set the default VM output consumer callback and it's
* private data. */
pVm->sVmConsumer.xConsumer = PH7_VmBlobConsumer;
pVm->sVmConsumer.pUserData = &pVm->sConsumer;
/* Allocate the reference table */
pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */
pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize);


+ 27
- 18
sapi/cli/main.c View File

@ -152,25 +152,17 @@ int main(int argc, char **argv) {
Output_Consumer, /* Error log consumer */
0 /* NULL: Callback Private data */
);
/* Now,it's time to compile our PHP file */
rc = ph7_compile_file(
pEngine, /* PH7 Engine */
argv[n], /* Path to the PHP file to compile */
&pVm, /* OUT: Compiled PHP program */
0 /* IN: Compile flags */
);
if(rc != PH7_OK) { /* Compile error */
if(rc == PH7_IO_ERR) {
Fatal("IO error while opening the target file");
/* Initialize the VM */
rc = ph7_vm_init(pEngine, &pVm);
if(rc != PH7_OK) {
if(rc == PH7_NOMEM) {
Fatal("Out of memory");
} else if(rc == PH7_VM_ERR) {
Fatal("VM initialization error");
} else {
/* Compile-time error, your output (STDOUT) should display the error messages */
Fatal("Compile error");
}
}
/*
* Now we have our script compiled,it's time to configure our VM.
* Now we have our VM initialized,it's time to configure our VM.
* We will install the VM output consumer callback defined above
* so that we can consume the VM output and redirect it to STDOUT.
*/
@ -186,16 +178,33 @@ int main(int argc, char **argv) {
if(rc != PH7_OK) {
Fatal("Error while configuring the VM error reporting");
}
if(err_report) {
/* Report script run-time errors */
ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT);
}
/* Now,it's time to compile our PHP file */
rc = ph7_compile_file(
pEngine, /* PH7 Engine */
argv[n], /* Path to the PHP file to compile */
&pVm, /* OUT: Compiled PHP program */
0 /* IN: Compile flags */
);
if(rc != PH7_OK) { /* Compile error */
if(rc == PH7_IO_ERR) {
Fatal("IO error while opening the target file");
} else if(rc == PH7_VM_ERR) {
Fatal("VM initialization error");
} else {
/* Compile-time error, your output (STDOUT) should display the error messages */
Fatal("Compile error");
}
}
/* Register script agruments so we can access them later using the $argv[]
* array from the compiled PHP program.
*/
for(n = n + 1; n < argc ; ++n) {
ph7_vm_config(pVm, PH7_VM_CONFIG_ARGV_ENTRY, argv[n]/* Argument value */);
}
if(err_report) {
/* Report script run-time errors */
ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT);
}
if(dump_vm) {
/* Dump PH7 byte-code instructions */
ph7_vm_dump_v2(pVm,


Loading…
Cancel
Save