From bec69ed6a371dd4d84a8367a11b6ff0410cb6a58 Mon Sep 17 00:00:00 2001 From: LeonMrBonnie Date: Wed, 9 Feb 2022 14:05:30 +0100 Subject: [PATCH] Add missing try catch and fix error log format --- compiler/compiler.cpp | 16 +++++++++++----- compiler/helpers.h | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/compiler/compiler.cpp b/compiler/compiler.cpp index e5e6b81..38e10ff 100644 --- a/compiler/compiler.cpp +++ b/compiler/compiler.cpp @@ -22,23 +22,29 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci return false; } + v8::TryCatch tryCatch(isolate); + v8::Local ctx = v8::Context::New(isolate); + v8::Context::Scope ctxScope(ctx); + // Compile the file to a JavaScript module v8::ScriptOrigin origin( isolate, v8::String::NewFromUtf8(isolate, fileName.c_str()).ToLocalChecked(), 0, 0, false, -1, v8::Local(), false, false, true, v8::Local()); v8::ScriptCompiler::Source source(v8::String::NewFromUtf8(isolate, sourceCode.c_str()).ToLocalChecked(), origin); v8::MaybeLocal maybeModule = v8::ScriptCompiler::CompileModule(isolate, &source); - if(maybeModule.IsEmpty()) + if(maybeModule.IsEmpty() || tryCatch.HasCaught()) { - logger->LogError("Failed to compile module: " + logger->GetHighlightColor() + fileName); + logger->LogError("Failed to compile module: " + fileName); + Helpers::CheckTryCatch(fileName, logger, tryCatch, ctx); return false; } // Retrieve the bytecode from the module v8::Local module = maybeModule.ToLocalChecked(); v8::ScriptCompiler::CachedData* cache = v8::ScriptCompiler::CreateCodeCache(module->GetUnboundModuleScript()); - if(cache == nullptr) + if(cache == nullptr || tryCatch.HasCaught()) { - logger->LogError("Failed to create bytecode: " + logger->GetHighlightColor() + fileName); + logger->LogError("Failed to create bytecode: " + fileName); + Helpers::CheckTryCatch(fileName, logger, tryCatch, ctx); return false; } @@ -47,7 +53,7 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci bool writeResult = package->WriteFile(fileName, (void*)bytecodeResult.data(), bytecodeResult.size()); if(!writeResult) { - logger->LogError("Failed to write to file: " + logger->GetHighlightColor() + fileName); + logger->LogError("Failed to write to file: " + fileName); return false; } diff --git a/compiler/helpers.h b/compiler/helpers.h index 1e34ce3..6e46eb9 100644 --- a/compiler/helpers.h +++ b/compiler/helpers.h @@ -1,6 +1,8 @@ #pragma once #include +#include "v8.h" +#include "compiler.h" namespace Helpers { @@ -26,4 +28,22 @@ namespace Helpers } } } + inline void CheckTryCatch(const std::string& fileName, BytecodeCompiler::ILogger* logger, v8::TryCatch& tryCatch, v8::Local ctx) + { + if(tryCatch.HasCaught()) + { + v8::Local message = tryCatch.Message(); + if(!message.IsEmpty()) + { + v8::Isolate* isolate = ctx->GetIsolate(); + v8::MaybeLocal maybeString = message->Get(); + v8::MaybeLocal maybeSourceLine = message->GetSourceLine(ctx); + v8::Maybe maybeLine = message->GetLineNumber(ctx); + + if(!maybeLine.IsNothing()) logger->LogError("Exception at " + fileName + ":" + std::to_string(maybeLine.ToChecked())); + if(!maybeString.IsEmpty()) logger->LogError(*v8::String::Utf8Value(isolate, maybeString.ToLocalChecked())); + if(!maybeSourceLine.IsEmpty()) logger->LogError(*v8::String::Utf8Value(isolate, maybeSourceLine.ToLocalChecked())); + } + } + } } // namespace Helpers