Skip to content

Commit

Permalink
Garbage changes to revert
Browse files Browse the repository at this point in the history
  • Loading branch information
caitp committed May 29, 2023
1 parent dd3513d commit f1adc76
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions NativeScript/runtime/ArgConverter.mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <typeinfo>
#include <Foundation/Foundation.h>
#include <sstream>
#include "ArgConverter.h"
Expand Down
4 changes: 3 additions & 1 deletion NativeScript/runtime/ArrayAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ - (instancetype)initWithJSObject:(Local<Object>)jsObject isolate:(Isolate*)isola
self->cache_ = Caches::Get(isolate);
self->object_ = std::make_shared<Persistent<Value>>(isolate, jsObject);
self->cache_->Instances.emplace(self, self->object_);
tns::SetValue(isolate, jsObject, MakeGarbageCollected<ObjCDataWrapper>(isolate, self));
auto p = MakeGarbageCollected<ObjCDataWrapper>(isolate, self);
tns::SetValue(isolate, jsObject, p);
return self;
}

return self;
Expand Down
3 changes: 3 additions & 0 deletions NativeScript/runtime/Caches.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "Common.h"
#include "Metadata.h"

#include <DataWrapper.h>

namespace tns {

struct StructInfo;
Expand Down Expand Up @@ -46,6 +48,7 @@ class Caches {
void* userData_;
};


Caches(v8::Isolate* isolate, const int& isolateId_ = -1);
~Caches();

Expand Down
2 changes: 2 additions & 0 deletions NativeScript/runtime/ClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
auto cache = Caches::Get(isolate);
Local<v8::Function> baseCtorFunc = cache->CtorFuncs.find(item->meta_->name())->second->Get(isolate);

// Previously extended class constructors stored their CacheItem in a single InternalField, and the CacheItem
// was retained by the Caches collection for the Isolate. This has changed
CacheItem* cacheItem = new CacheItem(nullptr, extendedClass);
Caches::Get(isolate)->registerCacheBoundObject(cacheItem);
Local<External> ext = External::New(isolate, cacheItem);
Expand Down
3 changes: 2 additions & 1 deletion NativeScript/runtime/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define Common_h

#include <type_traits>
#include <typeinfo>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
Expand Down Expand Up @@ -29,7 +30,7 @@ class ExternalData {
virtual ~ExternalData() = default;
bool IsExternalData() const { return header == kExternalDataHeader; }
bool IsClassBuilderData() const { assert(IsExternalData()); return type_ == ExternalDataType::ClassBuilder; }

private:
uint32_t header;
ExternalDataType type_;
Expand Down
18 changes: 15 additions & 3 deletions NativeScript/runtime/DataWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace tns {

static constexpr int NSDataWrapperTypeIndex = 0;
static constexpr int NSDataWrapperObjectIndex = 1;

class PrimitiveDataWrapper;

enum class WrapperType {
Expand Down Expand Up @@ -166,7 +169,6 @@ class BaseDataWrapper : public cppgc::GarbageCollected<BaseDataWrapper> {
BaseDataWrapper()
: gcProtected_(false) {
}

virtual ~BaseDataWrapper() = default;

const virtual WrapperType Type() {
Expand Down Expand Up @@ -411,15 +413,20 @@ class StructWrapper: public StructTypeWrapper {
}

bool HasParent() const {
//return this->parent_ != nullptr;
return !parent_.IsEmpty();
}
//std::shared_ptr<v8::Persistent<v8::Value>> Parent() {
//return this->parent_;
//}


void IncrementChildren() {
this->childCount_++;
//this->childCount_++;
}

void DecrementChildren() {
this->childCount_--;
//this->childCount_--;
}

int ChildCount() {
Expand All @@ -434,6 +441,7 @@ class StructWrapper: public StructTypeWrapper {
private:
void* data_;
int childCount_;
//std::shared_ptr<v8::Persistent<v8::Value>> parent_;
v8::TracedReference<v8::Value> parent_;
};

Expand Down Expand Up @@ -605,6 +613,10 @@ class BlockWrapper: public BaseDataWrapper {
typeEncoding_(typeEncoding) {
}

~BlockWrapper() {
return;
}

const WrapperType Type() {
return WrapperType::Block;
}
Expand Down
4 changes: 3 additions & 1 deletion NativeScript/runtime/DictionaryAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ - (instancetype)initWithJSObject:(Local<Object>)jsObject isolate:(Isolate*)isola
self->cache_ = Caches::Get(isolate);
self->object_ = std::make_shared<Persistent<Value>>(isolate, jsObject);
self->cache_->Instances.emplace(self, self->object_);
tns::SetValue(isolate, jsObject, MakeGarbageCollected<ObjCDataWrapper>(isolate, self));
auto p = MakeGarbageCollected<ObjCDataWrapper>(isolate, self);
tns::SetValue(isolate, jsObject, p);
return self;
}

return self;
Expand Down
3 changes: 1 addition & 2 deletions NativeScript/runtime/Helpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@
}

void tns::SetValue(Isolate* isolate, const Local<Object>& obj, BaseDataWrapper* value) {
if (obj.IsEmpty() || obj->IsNullOrUndefined()) {
if (obj.IsEmpty() || obj->IsNullOrUndefined())
return;
}

if (!AttachGarbageCollectedWrapper(obj, value))
tns::SetPrivateValue(obj, tns::ToV8String(isolate, "metadata"), CreateWrapperFor(isolate, value));
Expand Down
2 changes: 1 addition & 1 deletion NativeScript/runtime/InteropTypes.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
RegisterInteropType(context, types, "protocol", MakeGarbageCollected<PrimitiveDataWrapper>(isolate, sizeof(void*), BinaryTypeEncodingType::ProtocolEncoding));
RegisterInteropType(context, types, "class", MakeGarbageCollected<PrimitiveDataWrapper>(isolate, sizeof(void*), BinaryTypeEncodingType::ClassEncoding));
RegisterInteropType(context, types, "selector", MakeGarbageCollected<PrimitiveDataWrapper>(isolate, sizeof(void*), BinaryTypeEncodingType::SelectorEncoding));

bool success = interop->Set(context, tns::ToV8String(isolate, "types"), types).FromMaybe(false);
tns::Assert(success, isolate);

Expand Down
2 changes: 1 addition & 1 deletion NativeScript/runtime/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Local<v8::Function> Pointer::GetPointerCtorFunc(Local<Context> context) {
}

Local<FunctionTemplate> ctorFuncTemplate = FunctionTemplate::New(isolate, PointerConstructorCallback);

ctorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(kMinGarbageCollectedEmbedderFields);
ctorFuncTemplate->SetClassName(tns::ToV8String(isolate, "Pointer"));

Expand Down
5 changes: 5 additions & 0 deletions NativeScript/runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace tns {

static constexpr int kEmbedderNativeScript = 0x6E53;

class BaseDataWrapper;
class WorkerHolder;
class WorkerHolderData;
class Runtime {
public:
Runtime();
Expand Down
23 changes: 12 additions & 11 deletions NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>
#include <chrono>
#include <memory>
#include "Runtime.h"
#include "Caches.h"
#include "Console.h"
Expand Down Expand Up @@ -59,32 +60,32 @@

// TODO: fix race condition on workers where a queue can leak (maybe calling Terminate before Initialize?)
Caches::Workers->ForEach([currentIsolate](int& key, std::shared_ptr<Caches::WorkerState>& value) {
auto childWorkerWrapper = static_cast<WorkerWrapper*>(value->UserData());
if (childWorkerWrapper->GetMainIsolate() == currentIsolate) {
childWorkerWrapper->Terminate();
}
return false;
});
auto childWorkerWrapper = static_cast<WorkerWrapper*>(value->UserData());
if (childWorkerWrapper->GetMainIsolate() == currentIsolate) {
childWorkerWrapper->Terminate();
}
return false;
});

{
v8::Locker lock(isolate_);
DisposerPHV phv(isolate_);
// isolate_->VisitHandlesWithClassIds( &phv );

if (IsRuntimeWorker()) {
auto currentWorker = static_cast<WorkerWrapper*>(Caches::Workers->Get(this->workerId_)->UserData());
//auto currentWorker = static_cast<WorkerWrapper*>(Caches::Workers->Get(this->workerId_)->UserData());
Caches::Workers->Remove(this->workerId_);
// if the parent isolate is dead then deleting the wrapper is our responsibility
if (currentWorker->IsWeak()) {
delete currentWorker;
}
//if (currentWorker->IsWeak()) {
//delete currentWorker;
//}
}
Caches::Remove(this->isolate_);

this->isolate_->SetData(Constants::RUNTIME_SLOT, nullptr);
}

this->isolate_->Dispose();
currentIsolate->Dispose();

currentRuntime_ = nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion NativeScript/runtime/Worker.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
std::string workerPath = ToString(isolate, info[0]);
// TODO: Validate worker path and call worker.onerror if the script does not exist

WorkerWrapper* worker = MakeGarbageCollected<WorkerWrapper>(isolate, isolate, Worker::OnMessageCallback);
WorkerWrapper* worker = MakeGarbageCollected<WorkerWrapper>(isolate, Worker::OnMessageCallback);
tns::SetValue(isolate, thiz, worker);
std::shared_ptr<Persistent<Value>> poWorker = ObjectManager::Register(context, thiz);

Expand Down

0 comments on commit f1adc76

Please sign in to comment.