Add a class to register stateful callbacks to JS

Use the class in autostart as an example.
This commit is contained in:
phosit
2025-02-17 20:01:04 +01:00
committed by phosit
parent 0afb5f3d06
commit 44605c1297
3 changed files with 157 additions and 33 deletions
@@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games.
/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -110,4 +110,24 @@ public:
TS_ASSERT_EQUALS(ret, 4);
}
}
void test_statefull()
{
ScriptInterface script{"Test", "Test", g_ScriptContext};
const ScriptRequest rq{script};
JS::RootedValue nativeScope{rq.cx, JS::ObjectValue(*rq.nativeScope)};
constexpr const char* name{"callback"};
{
bool called{false};
auto _ = ScriptFunction::Register(rq, name, [&](){
called = true;
});
TS_ASSERT(!called);
TS_ASSERT(ScriptFunction::CallVoid(rq, nativeScope, name));
TS_ASSERT(called);
}
TS_ASSERT(!ScriptFunction::CallVoid(rq, nativeScope, name));
}
};