Skip to content

Commit

Permalink
Fix debug assert for v8::External::New calls
Browse files Browse the repository at this point in the history
- V8 doesn't allow to call v8::External::New with value == nullptr
  • Loading branch information
kin4stat committed Sep 11, 2024
1 parent 977b1e9 commit 8806037
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client/src/bindings/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static void Destroy(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_CHECK(worker, "Worker is invalid");

worker->Destroy();
info.This()->SetInternalField(0, v8::External::New(isolate, nullptr));
info.This()->SetInternalField(0, v8::Local<v8::Value>{});
static_cast<CV8ResourceImpl*>(resource)->RemoveWorker(worker);
}

Expand Down
4 changes: 2 additions & 2 deletions shared/V8ResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void V8ResourceImpl::OnRemoveBaseObject(alt::IBaseObject* handle)
}

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

Expand Down Expand Up @@ -555,7 +555,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(static_cast<int>(V8Class::InternalFields::RESOURCE), v8::External::New(isolate, nullptr));
obj->SetInternalField(static_cast<int>(V8Class::InternalFields::RESOURCE), v8::Local<v8::Value>{});
resourceObjects.erase(resource);
}

Expand Down
15 changes: 13 additions & 2 deletions shared/helpers/Bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ void V8Helpers::RegisterFunc(v8::Local<v8::Object> exports, const std::string& _

v8::Local<v8::String> name = V8Helpers::JSValue(_name);

v8::Local<v8::Function> fn = v8::Function::New(ctx, cb, v8::External::New(isolate, data)).ToLocalChecked();
v8::Local<v8::Value> v8_data;
if (data)
{
v8_data = v8::External::New(isolate, data);
}
v8::Local<v8::Function> fn = v8::Function::New(ctx, cb, v8_data).ToLocalChecked();
fn->SetName(name);

exports->Set(ctx, name, fn);
Expand Down Expand Up @@ -82,7 +87,13 @@ void V8Helpers::SetFunction(v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8
{
v8::Local<v8::String> _name = v8::String::NewFromUtf8(isolate, name, v8::NewStringType::kInternalized).ToLocalChecked();

v8::Local<v8::Function> fn = v8::Function::New(ctx, cb, v8::External::New(isolate, userData)).ToLocalChecked();
v8::Local<v8::Value> v8_data;
if (userData)
{
v8_data = v8::External::New(isolate, userData);
}

v8::Local<v8::Function> fn = v8::Function::New(ctx, cb, v8_data).ToLocalChecked();
fn->SetName(_name);

target->Set(ctx, _name, fn);
Expand Down

0 comments on commit 8806037

Please sign in to comment.