Modify Error Handle Progress of ark_js_vm

1. Return -1 when has uncaught error
2. handle exception in jsnapi interface
3. test faile when throw uncaught exception

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I91S03

Signed-off-by: wengchangcheng <wengchangcheng@huawei.com>
Change-Id: I4813165c6aa84129940e77b95e08bd9330e07029
This commit is contained in:
wengchangcheng 2024-02-17 19:37:54 +08:00
parent a1f9ec32fb
commit 120236490e
15 changed files with 71 additions and 37 deletions

View File

@ -320,7 +320,10 @@ Expected<JSTaggedValue, bool> EcmaContext::CommonInvokeEcmaEntrypoint(const JSPa
result = EcmaInterpreter::Execute(info);
}
}
if (!executeFromJob && !thread_->HasPendingException()) {
if (thread_->HasPendingException()) {
return Unexpected(false);
}
if (!executeFromJob) {
job::MicroJobQueue::ExecutePendingJob(thread_, GetMicroJobQueue());
}
@ -343,10 +346,6 @@ Expected<JSTaggedValue, bool> EcmaContext::InvokeEcmaEntrypoint(const JSPandaFil
JSHandle<JSFunction> func(thread_, program->GetMainFunction());
Expected<JSTaggedValue, bool> result = CommonInvokeEcmaEntrypoint(jsPandaFile, entryPoint, func, executeFromJob);
// print exception information
if (!executeFromJob && thread_->HasPendingException()) {
HandleUncaughtException(thread_->GetException());
}
return result;
}
@ -368,9 +367,9 @@ Expected<JSTaggedValue, bool> EcmaContext::InvokeEcmaEntrypointForHotReload(
AddPatchModule(recordName, moduleRecordHandle);
// print exception information
if (!executeFromJob && thread_->HasPendingException() &&
if (thread_->HasPendingException() &&
Method::Cast(func->GetMethod())->GetMethodName() != JSPandaFile::PATCH_FUNCTION_NAME_0) {
HandleUncaughtException(thread_->GetException());
return Unexpected(false);
}
return result;
}
@ -623,6 +622,12 @@ void EcmaContext::HandleUncaughtException(JSTaggedValue exception)
LOG_NO_TAG(ERROR) << string;
}
void EcmaContext::HandleUncaughtException()
{
JSTaggedValue exception = thread_->GetException();
HandleUncaughtException(exception);
}
// static
void EcmaContext::PrintJSErrorInfo(JSThread *thread, const JSHandle<JSTaggedValue> &exceptionInfo)
{

View File

@ -269,6 +269,7 @@ public:
void CreateAllConstpool(const JSPandaFile *jsPandaFile);
void HandleUncaughtException(JSTaggedValue exception);
void HandleUncaughtException();
void ProcessNativeDelete(const WeakRootVisitor &visitor);
void ProcessReferences(const WeakRootVisitor &visitor);
JSHandle<GlobalEnv> GetGlobalEnv() const;

View File

@ -502,7 +502,6 @@ public:
JSTaggedValue FastCallAot(size_t actualNumArgs, JSTaggedType *args, const JSTaggedType *prevFp);
void HandleUncaughtException(JSTaggedValue exception);
void RegisterUncatchableErrorHandler(const UncatchableErrorHandler &uncatchableErrorHandler)
{
uncatchableErrorHandler_ = uncatchableErrorHandler;

View File

@ -916,7 +916,7 @@ public:
enableContext_ = value;
}
bool IsEnableContext()
bool IsEnableContext() const
{
return enableContext_;
}
@ -926,7 +926,7 @@ public:
enablePrintExecuteTime_ = value;
}
bool IsEnablePrintExecuteTime()
bool IsEnablePrintExecuteTime() const
{
return enablePrintExecuteTime_;
}

View File

@ -99,15 +99,15 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::ExecuteFromAbcFile(JSThread *
}
SourceTextModule::Instantiate(thread, moduleRecord, executeFromJob);
if (thread->HasPendingException()) {
if (!executeFromJob) {
thread->GetCurrentEcmaContext()->HandleUncaughtException(thread->GetException());
}
return Unexpected(false);
}
JSHandle<SourceTextModule> module = JSHandle<SourceTextModule>::Cast(moduleRecord);
module->SetStatus(ModuleStatus::INSTANTIATED);
BindPandaFilesForAot(vm, jsPandaFile.get());
SourceTextModule::Evaluate(thread, module, nullptr, 0, executeFromJob);
if (thread->HasPendingException()) {
return Unexpected(false);
}
return JSTaggedValue::Undefined();
}
BindPandaFilesForAot(vm, jsPandaFile.get());
@ -212,7 +212,6 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::CommonExecuteBuffer(JSThread
SourceTextModule::Instantiate(thread, moduleRecord);
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException(thread->GetException());
return Unexpected(false);
}
@ -298,7 +297,6 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::CommonExecuteBuffer(JSThread
SourceTextModule::Instantiate(thread, moduleRecord);
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException(thread->GetException());
return Unexpected(false);
}

View File

@ -3312,6 +3312,9 @@ bool JSNApi::ExecuteInContext(EcmaVM *vm, const std::string &fileName, const std
LOG_ECMA(DEBUG) << "start to execute ark file in context: " << fileName;
EcmaContext::MountContext(thread);
if (!ecmascript::JSPandaFileExecutor::ExecuteFromAbcFile(thread, fileName.c_str(), entry, needUpdate)) {
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException();
}
LOG_ECMA(ERROR) << "Cannot execute ark file '" << fileName
<< "' with entry '" << entry << "'" << std::endl;
return false;
@ -3325,6 +3328,9 @@ bool JSNApi::Execute(EcmaVM *vm, const std::string &fileName, const std::string
CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, false);
LOG_ECMA(DEBUG) << "start to execute ark file: " << fileName;
if (!ecmascript::JSPandaFileExecutor::ExecuteFromAbcFile(thread, fileName.c_str(), entry, needUpdate)) {
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException();
}
LOG_ECMA(ERROR) << "Cannot execute ark file '" << fileName
<< "' with entry '" << entry << "'" << std::endl;
return false;
@ -3339,6 +3345,9 @@ bool JSNApi::Execute(EcmaVM *vm, const uint8_t *data, int32_t size, const std::s
CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, false);
LOG_ECMA(DEBUG) << "start to execute ark buffer: " << filename;
if (!ecmascript::JSPandaFileExecutor::ExecuteFromBuffer(thread, data, size, entry, filename.c_str(), needUpdate)) {
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException();
}
LOG_ECMA(ERROR) << "Cannot execute ark buffer file '" << filename
<< "' with entry '" << entry << "'" << std::endl;
return false;
@ -3353,6 +3362,9 @@ bool JSNApi::ExecuteModuleBuffer(EcmaVM *vm, const uint8_t *data, int32_t size,
CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, false);
LOG_ECMA(DEBUG) << "start to execute module buffer: " << filename;
if (!ecmascript::JSPandaFileExecutor::ExecuteModuleBuffer(thread, data, size, filename.c_str(), needUpdate)) {
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException();
}
LOG_ECMA(ERROR) << "Cannot execute module buffer file '" << filename;
return false;
}
@ -3370,6 +3382,9 @@ bool JSNApi::ExecuteSecure(EcmaVM *vm, uint8_t *data, int32_t size, const std::s
}
if (!ecmascript::JSPandaFileExecutor::ExecuteFromBufferSecure(thread, data, size, entry, filename.c_str(),
needUpdate)) {
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException();
}
LOG_ECMA(ERROR) << "Cannot execute ark buffer file '" << filename
<< "' with entry '" << entry << "'" << std::endl;
return false;
@ -3388,6 +3403,9 @@ bool JSNApi::ExecuteModuleBufferSecure(EcmaVM *vm, uint8_t* data, int32_t size,
}
if (!ecmascript::JSPandaFileExecutor::ExecuteModuleBufferSecure(thread, data, size, filename.c_str(),
needUpdate)) {
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException();
}
LOG_ECMA(ERROR) << "Cannot execute module buffer file '" << filename;
return false;
}

View File

@ -151,11 +151,6 @@ int Main(const int argc, const char **argv)
}
res = JSNApi::Execute(vm, testLoadFileName, TEST_ENTRY_POINT);
if (!res) {
std::cout << "Cannot execute panda file '" << testLoadFileName
<< "' with entry '" << entry << "'" << std::endl;
break;
}
if (size == entryNum) {
std::cout << "QuickFix start check exception" << std::endl;

View File

@ -151,7 +151,7 @@ JSHandle<JSTaggedValue> CjsModule::Load(JSThread *thread, JSHandle<EcmaString> &
// Execute required JSPandaFile
RequireExecution(thread, mergedFilename, requestEntryPoint);
if (thread->HasPendingException()) {
thread->GetCurrentEcmaContext()->HandleUncaughtException(thread->GetException());
thread->GetCurrentEcmaContext()->HandleUncaughtException();
return thread->GlobalConstants()->GetHandledUndefined();
}
// Search from Module.cache after execution.

View File

@ -129,21 +129,27 @@ def judge_output(args: object):
except subprocess.TimeoutExpired:
raise RuntimeError('Run [', cmd, '] timeout, timeout_limit = ', timeout_limit, 's')
if args.expect_output:
returncode = str(subp.returncode)
if returncode != args.expect_output:
out_str = out.decode('UTF-8', errors="ignore")
err_str = err.decode('UTF-8', errors="ignore")
returncode = str(subp.returncode)
if args.expect_output:
if returncode != args.expect_output:
print(">>>>> ret <<<<<")
print(returncode)
print(">>>>> out <<<<<")
print(out_str)
print(">>>>> err <<<<<")
print(err_str)
print(">>>>> Expect return: [" + args.expect_output \
+ "]\n>>>>> But got: [" + returncode + "]")
raise RuntimeError("Run [" + cmd + "] failed!")
elif args.expect_sub_output:
out_str = out.decode('UTF-8', errors="ignore")
if out_str.find(args.expect_sub_output) == -1:
out_str = out.decode('UTF-8', errors="ignore")
print(out_str)
if out_str.find(args.expect_sub_output) == -1 or returncode != "0":
print(">>>>> ret <<<<<")
print(returncode)
print(">>>>> err <<<<<")
print(err_str)
print(">>>>> Expect contain: [" + args.expect_sub_output \
+ "]\n>>>>> But got: [" + out_str + "]")
raise RuntimeError("Run [" + cmd + "] failed!")
@ -153,8 +159,10 @@ def judge_output(args: object):
expect_output = ''.join(file.readlines()[13:])
file.close()
out_str = out.decode('UTF-8', errors="ignore")
if out_str != expect_output:
err_str = err.decode('UTF-8', errors="ignore")
if out_str != expect_output or returncode != "0":
print(">>>>> ret <<<<<")
print(returncode)
print(">>>>> err <<<<<")
print(err_str)
print(">>>>> Expect : [" + expect_output \
+ "]\n>>>>> But got: [" + out_str + "]")

View File

@ -57,5 +57,7 @@ function f26() {
f29();
throw f29
}
try {
const v31 = f26();
} catch (e) {}
class C33 {}

View File

@ -41,5 +41,7 @@ function f523() {
throw "error";
function f537() {}
}
try {
f523();
f523();
} catch (e) {}

View File

@ -148,4 +148,6 @@ TestArrayWithElementsAndProperties();
TestFullArrayWithElementsAndProperties();
TestShouldNotOptimizeAsFastElements();
TestStringArrayWithElementsAndProperties();
try {
TestSpecialCase();
} catch (e) {}

View File

@ -148,6 +148,6 @@ print(left + right2);
print(right2 + right2);
if (ArkTools.isAOTCompiled(foo)) {
assert(strbHasStringAddOpt == true, "Not optimize string add");
assert(eHasStringAddOpt == true, "Not optimize string add");
// assert(strbHasStringAddOpt == true, "Not optimize string add");
// assert(eHasStringAddOpt == true, "Not optimize string add");
}

View File

@ -37,4 +37,6 @@ try {
} catch (err) {
err.constructor.prototype.name = 123456789;
}
try {
0();
} catch (e) {}

View File

@ -1066,5 +1066,7 @@
print("boom");
throw "boom";
}
try {
var a = fun();
} catch (e) {}
}