Skip to content

Commit

Permalink
feat: mvalue & serialization optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
vadzz-dev committed Mar 18, 2024
1 parent b951e89 commit f87467d
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 101 deletions.
8 changes: 6 additions & 2 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ void CNodeResourceImpl::Started(v8::Local<v8::Value> _exports)
{
if(!_exports->IsNullOrUndefined())
{
alt::MValueDict exports = std::dynamic_pointer_cast<alt::IMValueDict>(V8Helpers::V8ToMValue(_exports));
resource->SetExports(exports);
auto exports = V8Helpers::V8ToMValue(_exports);
if(exports->GetType() == alt::IMValue::Type::DICT)
resource->SetExports(std::static_pointer_cast<alt::IMValueDict>(exports));
else
Log::Warning << "Only object export is supported" << Log::Endl;

envStarted = true;
}
else
Expand Down
2 changes: 0 additions & 2 deletions server/src/bindings/ConnectionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ extern V8Class v8ConnectionInfo("ConnectionInfo",
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();

tpl->InstanceTemplate()->SetInternalFieldCount(1);

V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID);
V8Helpers::SetStaticAccessor(isolate, tpl, "all", AllGetter);

Expand Down
20 changes: 20 additions & 0 deletions shared/V8Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ class V8Class
std::unordered_map<v8::Isolate*, v8::Persistent<v8::FunctionTemplate, v8::CopyablePersistentTraits<v8::FunctionTemplate>>> tplMap;

public:
enum class InternalFields
{
OBJECT_CLASS,
BASE_OBJECT,
RESOURCE,

COUNT
};

enum class ObjectClass
{
NONE,
RESOURCE,
BASE_OBJECT,
VECTOR2,
VECTOR3,
RGBA,
QUATERNION,
};

static auto& All()
{
static std::unordered_map<std::string, V8Class*> _All;
Expand Down
7 changes: 4 additions & 3 deletions shared/V8Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class V8Entity
V8Entity(v8::Local<v8::Context> ctx, V8Class* __class, v8::Local<v8::Object> obj, alt::IBaseObject* _handle) : _class(__class), handle(_handle)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
obj->SetInternalField(0, v8::External::New(isolate, this));
V8Helpers::SetObjectClass(isolate, obj, V8Class::ObjectClass::BASE_OBJECT);
obj->SetInternalField(static_cast<int>(V8Class::InternalFields::BASE_OBJECT), v8::External::New(isolate, this));
jsVal.Reset(isolate, obj);
}

Expand All @@ -40,9 +41,9 @@ class V8Entity
if(!val->IsObject()) return nullptr;

v8::Local<v8::Object> obj = val.As<v8::Object>();
if(obj->InternalFieldCount() != 1) return nullptr;
if(obj->InternalFieldCount() <= static_cast<int>(V8Class::InternalFields::BASE_OBJECT)) return nullptr;

v8::Local<v8::Value> i = obj->GetInternalField(0);
v8::Local<v8::Value> i = obj->GetInternalField(static_cast<int>(V8Class::InternalFields::BASE_OBJECT));
if(!i->IsExternal()) return nullptr;

return static_cast<V8Entity*>(i.As<v8::External>()->Value());
Expand Down
16 changes: 16 additions & 0 deletions shared/V8Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,22 @@ std::string V8Helpers::GetJSValueTypeName(v8::Local<v8::Value> val)
return "unknown";
}

void V8Helpers::SetObjectClass(v8::Isolate* isolate, v8::Local<v8::Object> obj, V8Class::ObjectClass cls)
{
obj->SetInternalField(
static_cast<int>(V8Class::InternalFields::OBJECT_CLASS), v8::External::New(isolate, reinterpret_cast<void*>(cls))
);
}

V8Class::ObjectClass V8Helpers::GetObjectClass(v8::Local<v8::Object> obj)
{
if(obj->InternalFieldCount() <= static_cast<int>(V8Class::InternalFields::OBJECT_CLASS))
return V8Class::ObjectClass::NONE;

void* cls = obj->GetInternalField(static_cast<int>(V8Class::InternalFields::OBJECT_CLASS)).As<v8::External>()->Value();
return *reinterpret_cast<V8Class::ObjectClass*>(&cls);
}

v8::MaybeLocal<v8::Value> V8Helpers::CallFunctionWithTimeout(v8::Local<v8::Function> fn, v8::Local<v8::Context> ctx, std::vector<v8::Local<v8::Value>>& args, uint32_t timeout)
{
v8::Isolate* isolate = ctx->GetIsolate();
Expand Down
3 changes: 3 additions & 0 deletions shared/V8Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ namespace V8Helpers
std::string Stringify(v8::Local<v8::Context> ctx, v8::Local<v8::Value> val);
std::string GetJSValueTypeName(v8::Local<v8::Value> val);

void SetObjectClass(v8::Isolate* isolate, v8::Local<v8::Object> obj, V8Class::ObjectClass cls);
V8Class::ObjectClass GetObjectClass(v8::Local<v8::Object> obj);

inline std::string GetStackTrace(const std::string errorMsg)
{
auto isolate = v8::Isolate::GetCurrent();
Expand Down
75 changes: 38 additions & 37 deletions shared/V8ResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,40 +218,40 @@ v8::Local<v8::Value> V8ResourceImpl::CreateRGBA(alt::RGBA rgba)
return V8Helpers::New(isolate, GetContext(), rgbaClass.Get(isolate), args);
}

bool V8ResourceImpl::IsVector3(v8::Local<v8::Value> val)
{
bool result = false;
val->InstanceOf(GetContext(), vector3Class.Get(isolate)).To(&result);
return result;
}

bool V8ResourceImpl::IsVector2(v8::Local<v8::Value> val)
{
bool result = false;
val->InstanceOf(GetContext(), vector2Class.Get(isolate)).To(&result);
return result;
}

bool V8ResourceImpl::IsQuaternion(v8::Local<v8::Value> val)
{
bool result = false;
val->InstanceOf(GetContext(), quaternionClass.Get(isolate)).To(&result);
return result;
}

bool V8ResourceImpl::IsRGBA(v8::Local<v8::Value> val)
{
bool result = false;
val->InstanceOf(GetContext(), rgbaClass.Get(isolate)).To(&result);
return result;
}

bool V8ResourceImpl::IsBaseObject(v8::Local<v8::Value> val)
{
bool result = false;
val->InstanceOf(GetContext(), baseObjectClass.Get(isolate)).To(&result);
return result;
}
//bool V8ResourceImpl::IsVector3(v8::Local<v8::Value> val)
//{
// bool result = false;
// val->InstanceOf(GetContext(), vector3Class.Get(isolate)).To(&result);
// return result;
//}
//
//bool V8ResourceImpl::IsVector2(v8::Local<v8::Value> val)
//{
// bool result = false;
// val->InstanceOf(GetContext(), vector2Class.Get(isolate)).To(&result);
// return result;
//}
//
//bool V8ResourceImpl::IsQuaternion(v8::Local<v8::Value> val)
//{
// bool result = false;
// val->InstanceOf(GetContext(), quaternionClass.Get(isolate)).To(&result);
// return result;
//}
//
//bool V8ResourceImpl::IsRGBA(v8::Local<v8::Value> val)
//{
// bool result = false;
// val->InstanceOf(GetContext(), rgbaClass.Get(isolate)).To(&result);
// return result;
//}
//
//bool V8ResourceImpl::IsBaseObject(v8::Local<v8::Value> val)
//{
// bool result = false;
// val->InstanceOf(GetContext(), baseObjectClass.Get(isolate)).To(&result);
// return result;
//}

void V8ResourceImpl::OnCreateBaseObject(alt::IBaseObject* handle)
{
Expand Down Expand Up @@ -285,7 +285,7 @@ void V8ResourceImpl::OnRemoveBaseObject(alt::IBaseObject* handle)
}

entities.erase(handle);
ent->GetJSVal(isolate)->SetInternalField(0, v8::External::New(isolate, nullptr));
ent->GetJSVal(isolate)->SetInternalField(static_cast<int>(V8Class::InternalFields::BASE_OBJECT), v8::External::New(isolate, nullptr));
delete ent;
}

Expand Down Expand Up @@ -538,7 +538,8 @@ v8::Local<v8::Object> V8ResourceImpl::GetOrCreateResourceObject(alt::IResource*
if(resourceObjects.count(resource) != 0) return resourceObjects.at(resource).Get(isolate);
// Create instance
v8::Local<v8::Object> obj = v8Resource.CreateInstance(GetContext());
obj->SetInternalField(0, v8::External::New(isolate, resource));
V8Helpers::SetObjectClass(isolate, obj, V8Class::ObjectClass::RESOURCE);
obj->SetInternalField(static_cast<int>(V8Class::InternalFields::RESOURCE), v8::External::New(isolate, resource));
resourceObjects.insert({ resource, V8Helpers::CPersistent<v8::Object>(isolate, obj) });
return obj;
}
Expand All @@ -547,7 +548,7 @@ void V8ResourceImpl::DeleteResourceObject(alt::IResource* resource)
{
if(resourceObjects.count(resource) == 0) return;
v8::Local<v8::Object> obj = resourceObjects.at(resource).Get(isolate);
obj->SetInternalField(0, v8::External::New(isolate, nullptr));
obj->SetInternalField(static_cast<int>(V8Class::InternalFields::RESOURCE), v8::External::New(isolate, nullptr));
resourceObjects.erase(resource);
}

Expand Down
4 changes: 2 additions & 2 deletions shared/V8ResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ class V8ResourceImpl : public alt::IResource::Impl
v8::Local<v8::Value> CreateQuaternion(alt::Quaternion quat);
v8::Local<v8::Value> CreateRGBA(alt::RGBA rgba);

bool IsVector3(v8::Local<v8::Value> val);
/*bool IsVector3(v8::Local<v8::Value> val);
bool IsVector2(v8::Local<v8::Value> val);
bool IsQuaternion(v8::Local<v8::Value> val);
bool IsRGBA(v8::Local<v8::Value> val);
bool IsBaseObject(v8::Local<v8::Value> val);
bool IsBaseObject(v8::Local<v8::Value> val);*/

void OnCreateBaseObject(alt::IBaseObject* handle) override;
void OnRemoveBaseObject(alt::IBaseObject* handle) override;
Expand Down
2 changes: 1 addition & 1 deletion shared/bindings/BaseObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ extern V8Class v8BaseObject("BaseObject",
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();

tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->InstanceTemplate()->SetInternalFieldCount(static_cast<int>(V8Class::InternalFields::COUNT));

V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetById);

Expand Down
8 changes: 6 additions & 2 deletions shared/bindings/Blip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ static void ConstructorPointBlip(const v8::FunctionCallbackInfo<v8::Value>& info

else if(info.Length() == 2)
{
if(resource->IsVector3(info[0]))
V8Class::ObjectClass cls = V8Class::ObjectClass::NONE;
if(info[0]->IsObject())
cls = V8Helpers::GetObjectClass(info[0].As<v8::Object>());

if(cls == V8Class::ObjectClass::VECTOR3) // TODO: we should allow IVector3
{
V8_ARG_TO_VECTOR3(1, pos);
V8_ARG_TO_BOOLEAN(2, global);
blip = ICore::Instance().CreateBlip(global, alt::IBlip::BlipType::DESTINATION, pos);
}
else if(resource->IsBaseObject(info[0]))
else if(cls == V8Class::ObjectClass::BASE_OBJECT)
{
V8_ARG_TO_BASE_OBJECT(1, ent, IEntity, "entity");
V8_ARG_TO_BOOLEAN(2, global);
Expand Down
5 changes: 2 additions & 3 deletions shared/bindings/Quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_CHECK_ARGS_LEN2(1, 4);

v8::Local<v8::Object> _this = info.This();
V8Helpers::SetObjectClass(isolate, _this, V8Class::ObjectClass::QUATERNION);

v8::Local<v8::Value> x, y, z, w;

Expand Down Expand Up @@ -84,7 +85,5 @@ extern V8Class v8Quaternion("Quaternion",
Constructor,
[](v8::Local<v8::FunctionTemplate> tpl)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();

tpl->InstanceTemplate()->SetInternalFieldCount(1); // !! Needs to be set so V8 knows its a custom class !!
tpl->InstanceTemplate()->SetInternalFieldCount(static_cast<int>(V8Class::InternalFields::COUNT));
});
5 changes: 2 additions & 3 deletions shared/bindings/RGBA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)

V8_CHECK_ARGS_LEN_MIN_MAX(1, 4);

V8Helpers::SetObjectClass(isolate, info.This(), V8Class::ObjectClass::QUATERNION);
int32_t r = 0, g = 0, b = 0, a = 255;

if(info.Length() == 1)
Expand Down Expand Up @@ -70,7 +71,5 @@ extern V8Class v8RGBA("RGBA",
&Constructor,
[](v8::Local<v8::FunctionTemplate> tpl)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();

tpl->InstanceTemplate()->SetInternalFieldCount(1); // !! Needs to be set so V8 knows its a custom class !!
tpl->InstanceTemplate()->SetInternalFieldCount(static_cast<int>(V8Class::InternalFields::COUNT));
});
Loading

0 comments on commit f87467d

Please sign in to comment.