Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ScriptContext Class Reference

Abstraction around a SpiderMonkey JSContext. More...

#include <ScriptContext.h>

Public Member Functions

 ScriptContext (int contextSize, int heapGrowthBytesGCTrigger)
 
 ~ScriptContext ()
 
void MaybeIncrementalGC (double delay)
 MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough memory to be worth the amount of time it would take. More...
 
void ShrinkingGC ()
 
void RegisterRealm (JS::Realm *realm)
 This is used to keep track of realms which should be prepared for a GC. More...
 
void UnRegisterRealm (JS::Realm *realm)
 
void RunJobs ()
 Runs the promise continuation. More...
 
JSContext * GetGeneralJSContext () const
 GetGeneralJSContext returns the context without starting a GC request and without entering any compartment. More...
 

Static Public Member Functions

static std::shared_ptr< ScriptContextCreateContext (int contextSize=DEFAULT_CONTEXT_SIZE, int heapGrowthBytesGCTrigger=DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER)
 Returns a context, in which any number of ScriptInterfaces compartments can live. More...
 

Private Member Functions

void PrepareZonesForIncrementalGC () const
 

Private Attributes

JSContext * m_cx
 
const std::unique_ptr< Script::JobQueuem_JobQueue
 
std::list< JS::Realm * > m_Realms
 
int m_ContextSize
 
int m_HeapGrowthBytesGCTrigger
 
int m_LastGCBytes {0}
 
double m_LastGCCheck {0.0}
 

Detailed Description

Abstraction around a SpiderMonkey JSContext.

A single ScriptContext, with the associated context, should only be used on a single thread.

(One means to share data between threads and contexts is to create a Script::StructuredClone.)

Constructor & Destructor Documentation

◆ ScriptContext()

ScriptContext::ScriptContext ( int  contextSize,
int  heapGrowthBytesGCTrigger 
)

◆ ~ScriptContext()

ScriptContext::~ScriptContext ( )

Member Function Documentation

◆ CreateContext()

std::shared_ptr< ScriptContext > ScriptContext::CreateContext ( int  contextSize = DEFAULT_CONTEXT_SIZE,
int  heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 
)
static

Returns a context, in which any number of ScriptInterfaces compartments can live.

Each context should only ever be used on a single thread.

Parameters
parentContextParent context from the parent thread, with which we share some thread-safe data
contextSizeMaximum size in bytes of the new context
heapGrowthBytesGCTriggerSize in bytes of cumulated allocations after which a GC will be triggered

◆ GetGeneralJSContext()

JSContext * ScriptContext::GetGeneralJSContext ( ) const
inline

GetGeneralJSContext returns the context without starting a GC request and without entering any compartment.

It should only be used in specific situations, such as creating a new compartment, or when initializing a persistent rooted. If you need the compartmented context of a ScriptInterface, you should create a ScriptRequest and use the context from that.

◆ MaybeIncrementalGC()

void ScriptContext::MaybeIncrementalGC ( double  delay)

MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough memory to be worth the amount of time it would take.

It does this with our own logic and NOT some predefined JSAPI logic because such functionality currently isn't available out of the box. It does incremental GC which means it will collect one slice each time it's called until the garbage collection is done. This can and should be called quite regularly. The delay parameter allows you to specify a minimum time since the last GC in seconds (the delay should be a fraction of a second in most cases though). It will only start a new incremental GC or another GC slice if this time is exceeded. The user of this function is responsible for ensuring that GC can run with a small enough delay to get done with the work.

◆ PrepareZonesForIncrementalGC()

void ScriptContext::PrepareZonesForIncrementalGC ( ) const
private

◆ RegisterRealm()

void ScriptContext::RegisterRealm ( JS::Realm *  realm)

This is used to keep track of realms which should be prepared for a GC.

◆ RunJobs()

void ScriptContext::RunJobs ( )

Runs the promise continuation.

On contexts where promises can be used this function has to be called. This function has to be called frequently.

◆ ShrinkingGC()

void ScriptContext::ShrinkingGC ( )

◆ UnRegisterRealm()

void ScriptContext::UnRegisterRealm ( JS::Realm *  realm)

Member Data Documentation

◆ m_ContextSize

int ScriptContext::m_ContextSize
private

◆ m_cx

JSContext* ScriptContext::m_cx
private

◆ m_HeapGrowthBytesGCTrigger

int ScriptContext::m_HeapGrowthBytesGCTrigger
private

◆ m_JobQueue

const std::unique_ptr<Script::JobQueue> ScriptContext::m_JobQueue
private

◆ m_LastGCBytes

int ScriptContext::m_LastGCBytes {0}
private

◆ m_LastGCCheck

double ScriptContext::m_LastGCCheck {0.0}
private

◆ m_Realms

std::list<JS::Realm*> ScriptContext::m_Realms
private

The documentation for this class was generated from the following files: