Skip to content
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
cmake_minimum_required(VERSION 3.16)

if (MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Zi /Od")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /DEBUG")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand Down Expand Up @@ -29,6 +36,7 @@ set(SOURCES
src/cxx/ACSVM/ACSVM/Thread.cpp
src/cxx/ACSVM/ACSVM/ThreadExec.cpp
src/cxx/ACSVM/ACSVM/Tracer.cpp
src/cxx/CrtDebug.cpp
)

add_library(HelionACS-obj OBJECT ${SOURCES})
Expand Down
19 changes: 15 additions & 4 deletions src/cs/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public unsafe ReadOnlySpan<char> GetStringSpan(uint argIndex, uint index)
}
}

public abstract class Executor
public abstract class Executor : IDisposable
{
const int FixedBits = 16;
const int FixedOne = 1 << FixedBits;
Expand All @@ -142,6 +142,7 @@ public abstract class Executor

unsafe readonly protected Interop.Executor* m_executor;
private readonly List<GCHandle> m_handles;
private bool m_disposed;

public unsafe Executor()
{
Expand All @@ -155,12 +156,22 @@ public unsafe Executor()
m_executor = Interop.Methods.MakeExecutor(callbacks, (void*)GCHandle.ToIntPtr(selfHandle));
}

~Executor()
public void Dispose()
{
PerformDispose();
GC.SuppressFinalize(this);
}

protected unsafe void PerformDispose()
{
if (m_disposed)
return;

Interop.Methods.FreeExecutor(m_executor);
foreach (var handle in m_handles)
{
handle.Free();
}

m_disposed = true;
}

protected GCHandle AddHandle(object obj) {
Expand Down
20 changes: 20 additions & 0 deletions src/cxx/CrtDebug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifdef _CRTDEBUGENABLE
#include <windows.h>
#include <crtdbg.h>
#endif
#include "CrtDebug.hpp"

static bool Init = false;

void CrtDebugInit()
{
#ifdef _CRTDEBUGENABLE
if (Init)
return;

Init = true;
int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flags |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flags);
#endif
}
3 changes: 3 additions & 0 deletions src/cxx/CrtDebug.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void CrtDebugInit();
38 changes: 26 additions & 12 deletions src/cxx/HelionACS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ACSVM/ACSVM/MemorySerial.hpp"
#include "ACSVM/ACSVM/Error.hpp"
#include "ACSVM/ACSVM/Stack.hpp"
#include "CrtDebug.hpp"
#include <cstddef>
#include <span>
#include <string_view>
Expand Down Expand Up @@ -88,6 +89,7 @@ class Env : public ACSVM::Environment {
void loadModule(ACSVM::Module *module) override {
auto data = this->callbacks.loadModuleCallback(this->executorContext, module->name.s->str, module->name.s->len);
module->readBytecode(data.data, data.length);
delete[] data.data;
}
ACSVM::Word callSpecImpl(ACSVM::Thread *thread, ACSVM::Word spec, const ACSVM::Word *argV, ACSVM::Word argC) override {
return this->callbacks.callSpecImplCallback(this->executorContext, thread, spec, argV, argC);
Expand Down Expand Up @@ -131,7 +133,8 @@ class Executor {
Executor(Callbacks callbacks, void* executorContext) : env(callbacks, executorContext) {}

void LoadHubMap(ACSVM::Word hubId, ACSVM::Word mapId, std::span<const char*> moduleNames) {
auto global = env.getGlobalScope(0); global->active = true;
auto global = env.getGlobalScope(0);
global->active = true;

if (
hubId != 0
Expand All @@ -145,12 +148,14 @@ class Executor {
this->env.currentHubScope = nullptr;
env.currentMapScope = nullptr;
}
this->env.currentHubScope = global->getHubScope(hubId); this->env.currentHubScope->active = true;
this->env.currentHubScope = global->getHubScope(hubId);
this->env.currentHubScope->active = true;
if (this->env.currentMapScope != nullptr && this->env.currentMapScope->id != mapId) {
this->env.currentMapScope->active = false;
this->env.currentMapScope = nullptr;
}
this->env.currentMapScope = this->env.currentHubScope->getMapScope(mapId); this->env.currentMapScope->active = true;
this->env.currentMapScope = this->env.currentHubScope->getMapScope(mapId);
this->env.currentMapScope->active = true;

if (!this->env.currentMapScope->hasModules()) {
auto modules = std::vector<ACSVM::Module *> {};
Expand All @@ -162,14 +167,14 @@ class Executor {
}
}

ACSVM::MapScope::ScriptStartInfo MakeInfo(CSThreadInfo info, ACSVM::Word* argV, std::size_t argC) {
ACSVM::MapScope::ScriptStartInfo MakeInfo(IndexThreadInfo* indexThreadInfo, ACSVM::Word* argV, std::size_t argC) {
auto actualInfo = ACSVM::MapScope::ScriptStartInfo {};
actualInfo.argV = argV;
actualInfo.argC = argC;
auto threadInfo = new IndexThreadInfo(info);
actualInfo.info = threadInfo;
actualInfo.info = indexThreadInfo;
return actualInfo;
}

ACSVM::ScriptName GetScriptName(ACSVM::Word scriptId) {
return ACSVM::ScriptName(scriptId);
}
Expand All @@ -183,26 +188,31 @@ class Executor {

ACSVM::Word ScriptStartType(ACSVM::Word type, ACSVM::Word* argV, std::size_t argC, CSThreadInfo info) {
if (this->env.currentMapScope == nullptr) { return 0; }
return this->env.currentMapScope->scriptStartType(type, this->MakeInfo(info, argV, argC));
IndexThreadInfo indexThreadInfo(info);
return this->env.currentMapScope->scriptStartType(type, this->MakeInfo(&indexThreadInfo, argV, argC));
}
ACSVM::Word ScriptStartTypeForced(ACSVM::Word type, ACSVM::Word* argV, std::size_t argC, CSThreadInfo info) {
if (this->env.currentMapScope == nullptr) { return 0; }
return this->env.currentMapScope->scriptStartTypeForced(type, this->MakeInfo(info, argV, argC));
IndexThreadInfo indexThreadInfo(info);
return this->env.currentMapScope->scriptStartTypeForced(type, this->MakeInfo(&indexThreadInfo, argV, argC));
}
template<typename T>
bool ScriptStart(T scriptId, ACSVM::Word hubId, ACSVM::Word mapId, ACSVM::Word* argV, std::size_t argC, CSThreadInfo info) {
if (this->env.currentMapScope == nullptr) { return false; }
return this->env.currentMapScope->scriptStart(this->GetScriptName(scriptId), this->GetScope(hubId, mapId), this->MakeInfo(info, argV, argC));
IndexThreadInfo indexThreadInfo(info);
return this->env.currentMapScope->scriptStart(this->GetScriptName(scriptId), this->GetScope(hubId, mapId), this->MakeInfo(&indexThreadInfo, argV, argC));
}
template<typename T>
bool ScriptStartForced(T scriptId, ACSVM::Word hubId, ACSVM::Word mapId, ACSVM::Word* argV, std::size_t argC, CSThreadInfo info) {
if (this->env.currentMapScope == nullptr) { return false; }
return this->env.currentMapScope->scriptStartForced(this->GetScriptName(scriptId), this->GetScope(hubId, mapId), this->MakeInfo(info, argV, argC));
IndexThreadInfo indexThreadInfo(info);
return this->env.currentMapScope->scriptStartForced(this->GetScriptName(scriptId), this->GetScope(hubId, mapId), this->MakeInfo(&indexThreadInfo, argV, argC));
}
template<typename T>
ACSVM::Word ScriptStartResult(T scriptId, ACSVM::Word* argV, std::size_t argC, CSThreadInfo info) {
if (this->env.currentMapScope == nullptr) { return 0; }
return this->env.currentMapScope->scriptStartResult(this->GetScriptName(scriptId), this->MakeInfo(info, argV, argC));
IndexThreadInfo indexThreadInfo(info);
return this->env.currentMapScope->scriptStartResult(this->GetScriptName(scriptId), this->MakeInfo(&indexThreadInfo, argV, argC));
}
template<typename T>
bool ScriptStop(T scriptId, ACSVM::Word hubId, ACSVM::Word mapId) {
Expand Down Expand Up @@ -309,15 +319,19 @@ class Executor {
};

ModuleData MakeModuleData(std::size_t length) {
ModuleData ret;
ModuleData ret{};
ret.data = (length != 0) ? (new ACSVM::Byte[length] { 0 }) : nullptr;
ret.length = length;
return ret;
}

Executor* MakeExecutor(Callbacks callbacks, void* executorContext) {
CrtDebugInit();
return new Executor(callbacks, executorContext);
}
void FreeExecutor(Executor* executor) {
delete executor;
}
void LoadHubMap(
Executor* executor,
ACSVM::Word hubId,
Expand Down
1 change: 1 addition & 0 deletions src/cxx/HelionACS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct Callbacks {
class Executor;

HELIONACS_API Executor* MakeExecutor(Callbacks callbacks, void* executorContext);
HELIONACS_API void FreeExecutor(Executor* executor);
struct CSThreadInfo {
int32_t activator;
int32_t line;
Expand Down
Loading