-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.h
56 lines (52 loc) · 2.14 KB
/
helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once
#include <stdint.h>
#include "v8.h"
#include "compiler.h"
namespace Helpers
{
// Copies a uint32 value to the buffer, considering the endianness of the system
inline void CopyValueToBuffer(const uint8_t* buffer, size_t offset, uint32_t val)
{
bool isLittleEndian = true;
{
int n = 1;
isLittleEndian = *(char*)&n == 1;
}
uintptr_t dst = (uintptr_t)buffer + offset;
if(isLittleEndian) memcpy((void*)(dst), &val, sizeof(val));
else
{
// Code inspired by V8
uint8_t* src = reinterpret_cast<uint8_t*>(&val);
uint8_t* dstPtr = reinterpret_cast<uint8_t*>(dst);
for(size_t i = 0; i < sizeof(val); i++)
{
dstPtr[i] = src[sizeof(val) - i - 1];
}
}
}
// Copies 'SerializedCodeData::SourceHash' behaviour
inline uint32_t CreateV8SourceHash(uint32_t sourceSize)
{
// We always use modules, so this flag is always used
static constexpr uint32_t moduleFlagMask = (1 << 31);
return sourceSize | moduleFlagMask;
}
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