Skip to content

Commit cdba159

Browse files
committed
[MERGE #5140 @dilijev] Add ChakraEngine for GlobalObject lifetime management
Merge pull request #5140 from dilijev:ChakraEngine
2 parents 149b3f5 + f565a83 commit cdba159

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

lib/Common/ConfigFlagsList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,10 +1271,12 @@ FLAGNR(Phases, Memspect, "Enables memspect tracking to perform mem
12711271
FLAGNR(Number, PolymorphicInlineThreshold , "Maximum size in bytecodes of a polymorphic inline candidate", DEFAULT_CONFIG_PolymorphicInlineThreshold)
12721272
FLAGNR(Boolean, PrimeRecycler , "Prime the recycler first", DEFAULT_CONFIG_PrimeRecycler)
12731273
FLAGNR(Boolean, PrivateHeap , "Use HeapAlloc with a private heap", DEFAULT_CONFIG_PrivateHeap)
1274+
FLAGNR(Boolean, TraceEngineRefcount , "Output traces for ScriptEngine AddRef/Release to debug lifetime management", false)
12741275
#if defined(CHECK_MEMORY_LEAK) || defined(LEAK_REPORT)
12751276
FLAGNR(Boolean, LeakStackTrace , "Include stack trace on leaked pinned object and heap objects", false)
12761277
FLAGNR(Boolean, ForceMemoryLeak , "Fake leak some memory to test leak report and check memory leak", false)
12771278
#endif
1279+
FLAGNR(Boolean, DumpAfterFinalGC, "Collect a process dump after calling finalgc", false)
12781280
FLAGNR(Boolean, ForceOldDateAPI , "Force Chakra to use old dates API regardless of availability of a new one", DEFAULT_CONFIG_ForceOldDateAPI)
12791281

12801282
FLAGNR(Number, JitLoopBodyHotLoopThreshold , "Number of times loop has to be iterated in jitloopbody before it is determined as hot", DEFAULT_CONFIG_JitLoopBodyHotLoopThreshold)

lib/Common/Memory/HeapAllocator.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,6 @@ MemoryLeakCheck::~MemoryLeakCheck()
582582
LeakRecord * current = head;
583583
do
584584
{
585-
586585
if (enableOutput)
587586
{
588587
Output::PrintBuffer(current->dump, wcslen(current->dump));

lib/Parser/RegexPattern.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,42 @@ namespace UnifiedRegex
2424
program,
2525
isLiteral);
2626
}
27+
2728
void RegexPattern::Finalize(bool isShutdown)
2829
{
29-
if(isShutdown)
30+
if (isShutdown)
31+
{
3032
return;
33+
}
3134

3235
const auto scriptContext = GetScriptContext();
33-
if(!scriptContext)
36+
if (!scriptContext)
37+
{
3438
return;
39+
}
3540

3641
#if DBG
37-
// In JSRT, we might not have a chance to close at finalize time.
38-
if(!isLiteral && !scriptContext->IsClosed() && !scriptContext->GetThreadContext()->IsJSRT())
42+
// In JSRT or ChakraEngine, we might not have a chance to close at finalize time
43+
if (!isLiteral && !scriptContext->IsClosed() &&
44+
!scriptContext->GetThreadContext()->IsJSRT() &&
45+
!scriptContext->GetLibrary()->IsChakraEngine())
3946
{
4047
const auto source = GetSource();
41-
RegexPattern *p;
42-
Assert(
43-
!GetScriptContext()->GetDynamicRegexMap()->TryGetValue(
44-
RegexKey(source.GetBuffer(), source.GetLength(), GetFlags()),
45-
&p) || ( source.GetLength() == 0 ) ||
46-
p != this);
48+
RegexPattern *p = nullptr;
49+
50+
bool hasRegexPatternForSourceKey = GetScriptContext()->GetDynamicRegexMap()->TryGetValue(
51+
RegexKey(source.GetBuffer(), source.GetLength(), GetFlags()), &p);
52+
bool isSourceLengthZero = source.GetLength() == 0;
53+
bool isUniquePattern = p != this;
54+
55+
Assert(!hasRegexPatternForSourceKey || isSourceLengthZero || isUniquePattern);
4756
}
4857
#endif
4958

50-
if(isShallowClone)
59+
if (isShallowClone)
60+
{
5161
return;
62+
}
5263

5364
rep.unified.program->FreeBody(scriptContext->RegexAllocator());
5465
}

lib/Runtime/Base/ScriptContext.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ namespace Js
474474
if (javascriptLibrary != nullptr)
475475
{
476476
javascriptLibrary->scriptContext = nullptr;
477-
javascriptLibrary = nullptr;
477+
478478
if (closed)
479479
{
480480
// if we just closed, we haven't unpin the object yet.
@@ -484,11 +484,13 @@ namespace Js
484484
#if ENABLE_NATIVE_CODEGEN
485485
Assert(this->IsClosedNativeCodeGenerator());
486486
#endif
487-
if (!GetThreadContext()->IsJSRT())
487+
if (globalObject && !GetThreadContext()->IsJSRT() && !GetLibrary()->IsChakraEngine())
488488
{
489489
this->recycler->RootRelease(globalObject);
490490
}
491491
}
492+
493+
javascriptLibrary = nullptr;
492494
}
493495

494496
// Normally the JavascriptLibraryBase will unregister the scriptContext from the threadContext.
@@ -824,7 +826,9 @@ namespace Js
824826
bool ScriptContext::Close(bool inDestructor)
825827
{
826828
if (isScriptContextActuallyClosed)
829+
{
827830
return false;
831+
}
828832

829833
InternalClose();
830834

@@ -835,7 +839,7 @@ namespace Js
835839
#if ENABLE_NATIVE_CODEGEN
836840
Assert(this->IsClosedNativeCodeGenerator());
837841
#endif
838-
if (!GetThreadContext()->IsJSRT())
842+
if (!GetThreadContext()->IsJSRT() && !GetLibrary()->IsChakraEngine())
839843
{
840844
GetRecycler()->RootRelease(globalObject);
841845
}

lib/Runtime/Library/JavascriptLibraryBase.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// if the size changed here.
99
#pragma once
1010

11+
class ChakraEngine;
12+
1113
namespace Js
1214
{
1315
class EngineInterfaceObject;
@@ -19,7 +21,8 @@ namespace Js
1921

2022
public:
2123
JavascriptLibraryBase(GlobalObject* globalObject):
22-
globalObject(globalObject)
24+
globalObject(globalObject),
25+
chakraEngine(nullptr)
2326
{
2427
}
2528
Var GetPI() { return pi; }
@@ -147,6 +150,8 @@ namespace Js
147150
PropertyId GetPropertyIdSymbolIterator() { return PropertyIds::_symbolIterator; };
148151
PropertyId GetPropertyIdSymbolToStringTag() { return PropertyIds::_symbolToStringTag; };
149152

153+
bool IsChakraEngine() const { return chakraEngine != nullptr; }
154+
150155
protected:
151156
Field(GlobalObject*) globalObject;
152157
Field(RuntimeFunction*) mapConstructor;
@@ -314,6 +319,7 @@ namespace Js
314319

315320
public:
316321
Field(ScriptContext*) scriptContext;
322+
Field(ChakraEngine*) chakraEngine;
317323

318324
private:
319325
virtual void Dispose(bool isShutdown) override;

0 commit comments

Comments
 (0)