Refactor usecode task management and enhance teleportation logic
- Improved the queueUsecodeTask function to streamline process termination and context management. - Added robust error handling for event context creation and memory writing. - Enhanced the loadSceneAsync function to better handle avatar teleportation, including a new teleporter process. - Expanded entity dispatch tests to cover movement and teleporter processes, ensuring correct behavior under various conditions. - Introduced new tests for usecode runtime memory allocation and context creation, validating edge cases and error handling. - Updated flex archive tests to ensure proper loading and unloading of usecode slot chunk caches.
This commit is contained in:
parent
8c23bb02c9
commit
0771c86043
12 changed files with 1091 additions and 253 deletions
|
|
@ -1,14 +1,30 @@
|
|||
#include "remorse/usecode_vm.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
void writeWord(u8* bytes, std::size_t offset, u16 value)
|
||||
{
|
||||
bytes[offset] = static_cast<u8>(value);
|
||||
bytes[offset + 1] = static_cast<u8>(value >> 8);
|
||||
}
|
||||
|
||||
void writeFarPointer(u8* bytes, std::size_t offset, FarPtr16 pointer)
|
||||
{
|
||||
writeWord(bytes, offset, pointer.offset);
|
||||
writeWord(bytes, offset + sizeof(u16), pointer.segment);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<UsecodeInterpreterState> UsecodeInterpreterState::initialize(
|
||||
const std::vector<u8>& itemParameter, const std::vector<u8>& contextParameter,
|
||||
u16 modeFlags)
|
||||
u16 modeFlags, bool contextParameterPresent)
|
||||
{
|
||||
const bool saveStackPointer = !contextParameter.empty() && modeFlags != 0;
|
||||
const bool saveStackPointer = (!contextParameter.empty() || contextParameterPresent) && modeFlags != 0;
|
||||
const std::size_t savedPointerBytes = saveStackPointer ? sizeof(FarPtr16) : 0;
|
||||
if (itemParameter.size() > usecodeInterpreterStackLimit ||
|
||||
contextParameter.size() > usecodeInterpreterStackLimit - itemParameter.size() ||
|
||||
|
|
@ -22,10 +38,12 @@ std::optional<UsecodeInterpreterState> UsecodeInterpreterState::initialize(
|
|||
std::copy(bytes.begin(), bytes.end(), state.valueStack.begin() + state.stackTop);
|
||||
};
|
||||
pushBytes(itemParameter);
|
||||
const std::size_t stackTopBeforeContextParameter = state.stackTop;
|
||||
pushBytes(contextParameter);
|
||||
if (saveStackPointer) {
|
||||
state.stackTop -= savedPointerBytes;
|
||||
state.contextParameterOffset = static_cast<u16>(state.stackTop - sizeof(u16) * 2);
|
||||
state.contextParameterStackTop = static_cast<u16>(stackTopBeforeContextParameter);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
|
@ -484,6 +502,190 @@ bool writeUsecodeContextFactoryFields(u8* processBytes, std::size_t processSize,
|
|||
return true;
|
||||
}
|
||||
|
||||
std::optional<UsecodeContextInstance> createUsecodeContext(UsecodeRuntimeMemory& memory,
|
||||
const UsecodeContextCreateRequest& request)
|
||||
{
|
||||
const bool hasOwnerSource = request.ownerSource.offset != 0 || request.ownerSource.segment != 0;
|
||||
if (!memory.contains(request.codeSource, 1) || !memory.contains(request.ownerRows, 9) ||
|
||||
(hasOwnerSource && !memory.contains(request.ownerSource, 1))) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<u8> itemParameter;
|
||||
if (request.modeFlags != 0 && request.itemParameter) {
|
||||
if (request.itemParameter->size() != request.modeFlags) {
|
||||
return std::nullopt;
|
||||
}
|
||||
itemParameter = *request.itemParameter;
|
||||
}
|
||||
const std::vector<u8> contextParameter = request.contextParameter.value_or(std::vector<u8>{});
|
||||
auto interpreter = UsecodeInterpreterState::initialize(itemParameter, contextParameter,
|
||||
request.modeFlags, request.contextParameter.has_value());
|
||||
if (!interpreter || !interpreter->pushCodeAddress(request.slotIndex, request.valueAddOffset)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto processAddress = memory.allocate(usecodeContextProcessSize);
|
||||
if (!processAddress) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto callStackAddress = memory.allocate(usecodeInterpreterCallStackSize);
|
||||
if (!callStackAddress) {
|
||||
memory.release(*processAddress);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
UsecodeContextInstance context;
|
||||
context.processAddress = *processAddress;
|
||||
context.callStackAddress = *callStackAddress;
|
||||
writeFarPointer(context.processBytes.data(), usecodeContextInterpreterOffset, *callStackAddress);
|
||||
std::copy(interpreter->valueStack.begin() + interpreter->stackTop, interpreter->valueStack.end(),
|
||||
context.processBytes.begin() + usecodeContextInterpreterOffset + interpreter->stackTop);
|
||||
|
||||
const auto stackTopOffset = static_cast<u32>(processAddress->offset) +
|
||||
usecodeContextInterpreterOffset + static_cast<u32>(interpreter->stackTop);
|
||||
const auto stackBaseOffset = static_cast<u32>(processAddress->offset) + usecodeContextStackPointerOffset;
|
||||
if (stackTopOffset > 0xffff || stackBaseOffset > 0xffff) {
|
||||
memory.release(*callStackAddress);
|
||||
memory.release(*processAddress);
|
||||
return std::nullopt;
|
||||
}
|
||||
writeFarPointer(context.processBytes.data(), usecodeContextStackPointerOffset,
|
||||
FarPtr16{static_cast<u16>(stackTopOffset), processAddress->segment});
|
||||
writeFarPointer(context.processBytes.data(), usecodeContextStackBaseOffset,
|
||||
FarPtr16{static_cast<u16>(stackBaseOffset), processAddress->segment});
|
||||
writeWord(context.processBytes.data(), usecodeContextStackLimitOffset, interpreter->stackLimit);
|
||||
writeFarPointer(context.processBytes.data(), usecodeContextCodeSourceOffset, request.codeSource);
|
||||
writeFarPointer(context.processBytes.data(), usecodeContextSelfPointerOffset, *processAddress);
|
||||
writeFarPointer(context.processBytes.data(), usecodeContextOwnerRowsOffset, request.ownerRows);
|
||||
|
||||
const UsecodeContextFactoryFields fields{request.ownerSource, request.modeFlags,
|
||||
request.slotIndex, request.valueAddOffset, interpreter->contextParameterOffset.value_or(0)};
|
||||
if (!writeUsecodeContextFactoryFields(context.processBytes.data(), context.processBytes.size(), fields)) {
|
||||
memory.release(*callStackAddress);
|
||||
memory.release(*processAddress);
|
||||
return std::nullopt;
|
||||
}
|
||||
if (interpreter->contextParameterStackTop) {
|
||||
const std::size_t savedPointerOffset = usecodeContextInterpreterOffset + interpreter->stackTop;
|
||||
const u32 savedStackOffset = static_cast<u32>(processAddress->offset) +
|
||||
usecodeContextInterpreterOffset + *interpreter->contextParameterStackTop;
|
||||
if (savedPointerOffset > context.processBytes.size() - sizeof(FarPtr16) || savedStackOffset > 0xffff) {
|
||||
memory.release(*callStackAddress);
|
||||
memory.release(*processAddress);
|
||||
return std::nullopt;
|
||||
}
|
||||
writeFarPointer(context.processBytes.data(), savedPointerOffset,
|
||||
FarPtr16{static_cast<u16>(savedStackOffset), processAddress->segment});
|
||||
}
|
||||
|
||||
std::vector<u8> callStackBytes(usecodeInterpreterCallStackSize, 0);
|
||||
for (u16 index = 0; index < interpreter->callFrameCount; ++index) {
|
||||
const std::size_t frameOffset = static_cast<std::size_t>(index) * 2 * sizeof(u16);
|
||||
writeWord(callStackBytes.data(), frameOffset, interpreter->callFrames[index].offset);
|
||||
writeWord(callStackBytes.data(), frameOffset + sizeof(u16), interpreter->callFrames[index].segment);
|
||||
}
|
||||
writeWord(callStackBytes.data(), 0x80, interpreter->callFrameCount);
|
||||
|
||||
const std::vector<u8> processBytes(context.processBytes.begin(), context.processBytes.end());
|
||||
if (!memory.write(*processAddress, processBytes) || !memory.write(*callStackAddress, callStackBytes)) {
|
||||
memory.release(*callStackAddress);
|
||||
memory.release(*processAddress);
|
||||
return std::nullopt;
|
||||
}
|
||||
context.interpreter = std::move(*interpreter);
|
||||
return context;
|
||||
}
|
||||
|
||||
bool destroyUsecodeContext(UsecodeRuntimeMemory& memory, const UsecodeContextInstance& context) noexcept
|
||||
{
|
||||
const bool callStackReleased = memory.release(context.callStackAddress);
|
||||
const bool processReleased = memory.release(context.processAddress);
|
||||
return callStackReleased && processReleased;
|
||||
}
|
||||
|
||||
std::optional<UsecodeEventContext> createUsecodeEventContext(UsecodeRuntimeMemory& memory,
|
||||
const std::vector<u8>& ownerRows, u16 classId, u16 eventSlot, u16 sourceItem,
|
||||
const std::vector<u8>& code, u8 itemArgumentSize)
|
||||
{
|
||||
constexpr std::size_t ownerRowSize = 13;
|
||||
const std::size_t ownerRowOffset = static_cast<std::size_t>(classId) * ownerRowSize;
|
||||
if (code.size() <= usecodeEventBytecodeOffset ||
|
||||
(itemArgumentSize != 0 && itemArgumentSize != sizeof(u16)) ||
|
||||
ownerRowOffset > ownerRows.size() || ownerRows.size() - ownerRowOffset < ownerRowSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
UsecodeEventContext eventContext;
|
||||
const auto allocateRegion = [&memory, &eventContext](const std::vector<u8>& bytes)
|
||||
-> std::optional<FarPtr16> {
|
||||
auto region = memory.allocate(bytes.size());
|
||||
if (!region) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!memory.write(*region, bytes)) {
|
||||
memory.release(*region);
|
||||
return std::nullopt;
|
||||
}
|
||||
eventContext.ownedRegions.push_back(*region);
|
||||
return region;
|
||||
};
|
||||
|
||||
const auto codeRegion = allocateRegion(code);
|
||||
const std::vector<u8> ownerRow(ownerRows.begin() + ownerRowOffset,
|
||||
ownerRows.begin() + ownerRowOffset + ownerRowSize);
|
||||
const auto ownerRowRegion = codeRegion ? allocateRegion(ownerRow) : std::nullopt;
|
||||
const auto outputRegion = ownerRowRegion ? memory.allocate(sizeof(u16)) : std::nullopt;
|
||||
if (!codeRegion || !ownerRowRegion || !outputRegion) {
|
||||
if (outputRegion) {
|
||||
memory.release(*outputRegion);
|
||||
}
|
||||
for (const auto region : eventContext.ownedRegions) {
|
||||
memory.release(region);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
eventContext.processIdOutput = *outputRegion;
|
||||
eventContext.ownedRegions.push_back(*outputRegion);
|
||||
|
||||
std::vector<u8> contextParameter{
|
||||
static_cast<u8>(outputRegion->offset),
|
||||
static_cast<u8>(outputRegion->offset >> 8),
|
||||
static_cast<u8>(outputRegion->segment),
|
||||
static_cast<u8>(outputRegion->segment >> 8)
|
||||
};
|
||||
UsecodeContextCreateRequest request;
|
||||
request.codeSource = *codeRegion;
|
||||
request.ownerRows = FarPtr16{4, ownerRowRegion->segment};
|
||||
if (itemArgumentSize == sizeof(u16)) {
|
||||
request.itemParameter = std::vector<u8>{static_cast<u8>(sourceItem),
|
||||
static_cast<u8>(sourceItem >> 8)};
|
||||
}
|
||||
request.contextParameter = std::move(contextParameter);
|
||||
request.modeFlags = itemArgumentSize;
|
||||
request.slotIndex = classId;
|
||||
request.valueAddOffset = eventSlot;
|
||||
auto context = createUsecodeContext(memory, request);
|
||||
if (!context) {
|
||||
for (const auto region : eventContext.ownedRegions) {
|
||||
memory.release(region);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
eventContext.context = std::move(*context);
|
||||
return eventContext;
|
||||
}
|
||||
|
||||
bool destroyUsecodeEventContext(UsecodeRuntimeMemory& memory,
|
||||
const UsecodeEventContext& eventContext) noexcept
|
||||
{
|
||||
bool released = destroyUsecodeContext(memory, eventContext.context);
|
||||
for (const auto region : eventContext.ownedRegions) {
|
||||
released = memory.release(region) && released;
|
||||
}
|
||||
return released;
|
||||
}
|
||||
|
||||
UsecodeStepResult UsecodeProcessTask::step(const UsecodeOpcodeServices& services)
|
||||
{
|
||||
if (halted) {
|
||||
|
|
@ -502,6 +704,19 @@ bool UsecodeTaskRegistry::add(UsecodeProcessTask task)
|
|||
return tasks_.emplace(task.processId, std::move(task)).second;
|
||||
}
|
||||
|
||||
bool UsecodeTaskRegistry::addWithContext(UsecodeProcessTask task, UsecodeEventContext context)
|
||||
{
|
||||
const u16 processId = task.processId;
|
||||
if (processId < 3 || tasks_.find(processId) != tasks_.end() || contexts_.find(processId) != contexts_.end()) {
|
||||
destroyUsecodeEventContext(runtimeMemory_, context);
|
||||
return false;
|
||||
}
|
||||
task.interpreter = context.context.interpreter;
|
||||
tasks_.emplace(processId, std::move(task));
|
||||
contexts_.emplace(processId, std::move(context));
|
||||
return true;
|
||||
}
|
||||
|
||||
UsecodeProcessTask* UsecodeTaskRegistry::find(u16 processId)
|
||||
{
|
||||
const auto found = tasks_.find(processId);
|
||||
|
|
@ -514,9 +729,25 @@ const UsecodeProcessTask* UsecodeTaskRegistry::find(u16 processId) const
|
|||
return found == tasks_.end() ? nullptr : &found->second;
|
||||
}
|
||||
|
||||
const UsecodeEventContext* UsecodeTaskRegistry::context(u16 processId) const noexcept
|
||||
{
|
||||
const auto found = contexts_.find(processId);
|
||||
return found == contexts_.end() ? nullptr : &found->second;
|
||||
}
|
||||
|
||||
bool UsecodeTaskRegistry::remove(u16 processId)
|
||||
{
|
||||
return tasks_.erase(processId) != 0;
|
||||
const auto task = tasks_.find(processId);
|
||||
if (task == tasks_.end()) {
|
||||
return false;
|
||||
}
|
||||
const auto context = contexts_.find(processId);
|
||||
if (context != contexts_.end()) {
|
||||
destroyUsecodeEventContext(runtimeMemory_, context->second);
|
||||
contexts_.erase(context);
|
||||
}
|
||||
tasks_.erase(task);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
Loading…
Add table
Add a link
Reference in a new issue