Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/ScriptRuntime.cpp
/* Copyright (C) 2016 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc)) | ||||
{ | { | ||||
outstring[i] = (wchar_t)str[i]; | outstring[i] = (wchar_t)str[i]; | ||||
} | } | ||||
printf("---------------------------------------\n: %ls \n---------------------------------------\n", outstring); | printf("---------------------------------------\n: %ls \n---------------------------------------\n", outstring); | ||||
#endif | #endif | ||||
} | } | ||||
void ScriptRuntime::GCCallback(JSRuntime* UNUSED(rt), JSGCStatus status, void *data) | |||||
{ | |||||
if (status == JSGC_END) | |||||
reinterpret_cast<ScriptRuntime*>(data)->GCCallbackMember(); | |||||
} | |||||
void ScriptRuntime::GCCallbackMember() | |||||
{ | |||||
m_FinalizationListObjectIdCache.clear(); | |||||
} | |||||
void ScriptRuntime::AddDeferredFinalizationObject(const std::shared_ptr<void>& obj) | |||||
{ | |||||
m_FinalizationListObjectIdCache.push_back(obj); | |||||
} | |||||
ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger): | ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger): | ||||
m_LastGCBytes(0), | m_LastGCBytes(0), | ||||
m_LastGCCheck(0.0f), | m_LastGCCheck(0.0f), | ||||
m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger), | m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger), | ||||
m_RuntimeSize(runtimeSize) | m_RuntimeSize(runtimeSize) | ||||
{ | { | ||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptRuntimes!"); | ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptRuntimes!"); | ||||
JSRuntime* parentJSRuntime = parentRuntime ? parentRuntime->m_rt : nullptr; | JSRuntime* parentJSRuntime = parentRuntime ? parentRuntime->m_rt : nullptr; | ||||
m_rt = JS_NewRuntime(runtimeSize, JS::DefaultNurseryBytes, parentJSRuntime); | m_rt = JS_NewRuntime(runtimeSize, JS::DefaultNurseryBytes, parentJSRuntime); | ||||
ENSURE(m_rt); // TODO: error handling | ENSURE(m_rt); // TODO: error handling | ||||
JS::SetGCSliceCallback(m_rt, GCSliceCallbackHook); | JS::SetGCSliceCallback(m_rt, GCSliceCallbackHook); | ||||
JS_SetGCCallback(m_rt, ScriptRuntime::GCCallback, this); | |||||
JS_SetGCParameter(m_rt, JSGC_MAX_MALLOC_BYTES, m_RuntimeSize); | JS_SetGCParameter(m_rt, JSGC_MAX_MALLOC_BYTES, m_RuntimeSize); | ||||
JS_SetGCParameter(m_rt, JSGC_MAX_BYTES, m_RuntimeSize); | JS_SetGCParameter(m_rt, JSGC_MAX_BYTES, m_RuntimeSize); | ||||
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL); | JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL); | ||||
// The whole heap-growth mechanism seems to work only for non-incremental GCs. | // The whole heap-growth mechanism seems to work only for non-incremental GCs. | ||||
// We disable it to make it more clear if full GCs happen triggered by this JSAPI internal mechanism. | // We disable it to make it more clear if full GCs happen triggered by this JSAPI internal mechanism. | ||||
JS_SetGCParameter(m_rt, JSGC_DYNAMIC_HEAP_GROWTH, false); | JS_SetGCParameter(m_rt, JSGC_DYNAMIC_HEAP_GROWTH, false); | ||||
ScriptEngine::GetSingleton().RegisterRuntime(m_rt); | ScriptEngine::GetSingleton().RegisterRuntime(m_rt); | ||||
} | } | ||||
ScriptRuntime::~ScriptRuntime() | ScriptRuntime::~ScriptRuntime() | ||||
{ | { | ||||
JS_SetGCCallback(m_rt, nullptr, nullptr); | |||||
JS_DestroyRuntime(m_rt); | JS_DestroyRuntime(m_rt); | ||||
ENSURE(m_FinalizationListObjectIdCache.empty() && "Leak: Removing callback while some objects still aren't finalized!"); | |||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptRuntime!"); | ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptRuntime!"); | ||||
ScriptEngine::GetSingleton().UnRegisterRuntime(m_rt); | ScriptEngine::GetSingleton().UnRegisterRuntime(m_rt); | ||||
} | } | ||||
void ScriptRuntime::RegisterContext(JSContext* cx) | void ScriptRuntime::RegisterContext(JSContext* cx) | ||||
{ | { | ||||
m_Contexts.push_back(cx); | m_Contexts.push_back(cx); | ||||
▲ Show 20 Lines • Show All 123 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator