Skip to content

Commit

Permalink
Add missing try catch and fix error log format
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Feb 9, 2022
1 parent 45dfac0 commit bec69ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 11 additions & 5 deletions compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci
return false;
}

v8::TryCatch tryCatch(isolate);
v8::Local<v8::Context> 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<v8::Value>(), false, false, true, v8::Local<v8::PrimitiveArray>());
v8::ScriptCompiler::Source source(v8::String::NewFromUtf8(isolate, sourceCode.c_str()).ToLocalChecked(), origin);
v8::MaybeLocal<v8::Module> 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<v8::Module> 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;
}

Expand All @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions compiler/helpers.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <stdint.h>
#include "v8.h"
#include "compiler.h"

namespace Helpers
{
Expand All @@ -26,4 +28,22 @@ namespace Helpers
}
}
}
inline void CheckTryCatch(const std::string& fileName, BytecodeCompiler::ILogger* logger, v8::TryCatch& tryCatch, v8::Local<v8::Context> ctx)
{
if(tryCatch.HasCaught())
{
v8::Local<v8::Message> message = tryCatch.Message();
if(!message.IsEmpty())
{
v8::Isolate* isolate = ctx->GetIsolate();
v8::MaybeLocal<v8::String> maybeString = message->Get();
v8::MaybeLocal<v8::String> maybeSourceLine = message->GetSourceLine(ctx);
v8::Maybe<int> 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

0 comments on commit bec69ed

Please sign in to comment.