Skip to content

Reuse existing Persistent object when converting to Local #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Source/Noesis.Javascript/JavascriptExternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void GCCallback(const WeakCallbackInfo<JavascriptExternal>& data)
}

void
JavascriptExternal::Wrap(Isolate* isolate, Local<Object> object)
JavascriptExternal::InitializePersistent(Isolate* isolate, Local<Object> object)
{
object->SetInternalField(0, External::New(isolate, this));
mPersistent.Reset(isolate, object);
Expand All @@ -88,6 +88,26 @@ JavascriptExternal::Wrap(Isolate* isolate, Local<Object> object)

////////////////////////////////////////////////////////////////////////////////////////////////////

Local<Object>
JavascriptExternal::ToLocal(Isolate* isolate)
{
if (!mPersistent.IsEmpty())
return Local<Object>::New(isolate, mPersistent);

auto context = JavascriptContext::GetCurrent();

EscapableHandleScope scope(isolate);

Local<FunctionTemplate> templ = context->GetObjectWrapperConstructorTemplate(GetObject()->GetType());
Local<ObjectTemplate> instanceTemplate = templ->InstanceTemplate();
Local<Object> object = instanceTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
InitializePersistent(isolate, object);

return scope.Escape(object);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

System::Object^
JavascriptExternal::GetObject()
{
Expand Down Expand Up @@ -376,7 +396,7 @@ void JavascriptExternal::IteratorCallback(const v8::FunctionCallbackInfo<Value>&

auto context = JavascriptContext::GetCurrent();
auto enumeratorExternal = context->WrapObject(enumerator);
enumeratorExternal->Wrap(isolate, iteratorInstance);
enumeratorExternal->InitializePersistent(isolate, iteratorInstance);

iArgs.GetReturnValue().Set(iteratorInstance);
}
Expand Down
4 changes: 3 additions & 1 deletion Source/Noesis.Javascript/JavascriptExternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class JavascriptExternal

Local<Function> GetIterator();

void Wrap(Isolate* isolate, Local<Object> object);
Local<Object> ToLocal(Isolate* isolate);

////////////////////////////////////////////////////////////
// Data members
Expand All @@ -101,6 +101,8 @@ class JavascriptExternal

SetParameterOptions mOptions;

void InitializePersistent(Isolate* isolate, Local<Object> object);

static void IteratorCallback(const v8::FunctionCallbackInfo<Value>& iArgs);
static void IteratorNextCallback(const v8::FunctionCallbackInfo<Value>& iArgs);
};
Expand Down
8 changes: 1 addition & 7 deletions Source/Noesis.Javascript/JavascriptInterop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,7 @@ JavascriptInterop::WrapObject(System::Object^ iObject)
{
v8::Isolate *isolate = JavascriptContext::GetCurrentIsolate();
JavascriptExternal *external = context->WrapObject(iObject);

Local<FunctionTemplate> templ = context->GetObjectWrapperConstructorTemplate(iObject->GetType());
Local<ObjectTemplate> instanceTemplate = templ->InstanceTemplate();
Local<Object> object = instanceTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
external->Wrap(isolate, object);

return object;
return external->ToLocal(isolate);
}

throw gcnew System::Exception("No context currently active.");
Expand Down