This commit is contained in:
+58
-148
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user