Skip to content

Commit f0867ae

Browse files
committed
Traverse wren class hierarchy + A few small fixes
1 parent e6ad570 commit f0867ae

9 files changed

Lines changed: 68 additions & 19 deletions

File tree

OverEngine/premake5.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ project "OverEngine"
7777
"%{includeDir.fmt}",
7878
"%{includeDir.yaml_cpp}",
7979
"%{includeDir.wren}",
80+
"%{includeDir.wren_vm}",
8081
}
8182

8283
defines
8384
{
8485
"GLFW_INCLUDE_NONE",
86+
"YAML_CPP_STATIC_DEFINE",
8587
}
8688

8789
filter "system:windows"

OverEngine/src/OverEngine/Scripting/Wren.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ namespace OverEngine
4949

5050
// This wont register superclasses fields.
5151
wrenpp::VM::reportClassFn = [this](ClassInfo* classInfo) {
52-
this->m_FieldNames[classInfo->name->value] = Vector<String>();
53-
auto& fieldNames = this->m_FieldNames[classInfo->name->value];
52+
this->m_FieldNames[classInfo->name->value] = std::make_pair(false, Vector<String>());
53+
auto& fieldNames = this->m_FieldNames[classInfo->name->value].second;
5454

5555
SymbolTable* syms = &classInfo->fields;
5656
for (ObjString** current = syms->data; current < syms->data + syms->count; current++)
5757
fieldNames.push_back((*current)->value);
5858
};
5959

60+
m_FieldNames["Object"] = std::make_pair(true, Vector<String>());
61+
6062
wrenpp::VM::loadModuleFn = [](const char* mod) -> char* {
6163
#define WREN_MOD(m) if (strcmp(mod, #m) == 0) return const_cast<char*>(WrenSources::m##ModuleSource);
6264

@@ -111,7 +113,7 @@ namespace OverEngine
111113
wrenReleaseHandle(m_VM, m_OnCollisionEnterMethod);
112114
wrenReleaseHandle(m_VM, m_OnCollisionExitMethod);
113115

114-
for (auto [_, handle] : m_CallHandles)
116+
for (auto& [_, handle] : m_CallHandles)
115117
wrenReleaseHandle(m_VM, handle);
116118
}
117119

@@ -144,6 +146,26 @@ namespace OverEngine
144146
return ToString(&handle);
145147
}
146148

149+
const Vector<String>& Wren::GetFields(ObjClass* klass) const
150+
{
151+
auto& [hasSuperClassFields, fields] = m_FieldNames.at(klass->name->value);
152+
153+
if (!hasSuperClassFields)
154+
AddSuperClassFields(klass);
155+
156+
return fields;
157+
}
158+
159+
void Wren::AddSuperClassFields(ObjClass* klass) const
160+
{
161+
auto& [hasSuperClassFields, fields] = m_FieldNames[klass->name->value];
162+
163+
auto& superClassFields = GetFields(klass->superclass);
164+
fields.insert(fields.begin(), superClassFields.begin(), superClassFields.end());
165+
166+
hasSuperClassFields = true;
167+
}
168+
147169
WrenScriptClass::WrenScriptClass(const Ref<Wren>& vm, const char* moduleName, const char* className)
148170
: m_VM(vm)
149171
{
@@ -180,16 +202,16 @@ namespace OverEngine
180202
wrenReleaseHandle(m_VM->GetRaw(), m_InstanceHandle);
181203
}
182204

183-
#define IMPL_METHOD_CALL_NO_PARAM(methode) \
184-
WrenInterpretResult WrenScriptInstance::methode() const \
185-
{ \
186-
return Call(m_VM->Get##methode##Method()); \
205+
#define IMPL_METHOD_CALL_NO_PARAM(method) \
206+
WrenInterpretResult WrenScriptInstance::method() const \
207+
{ \
208+
return Call(m_VM->Get##method##Method()); \
187209
}
188210

189-
#define IMPL_METHOD_CALL_WITH_PARAM(methode, params, wrenParams) \
190-
WrenInterpretResult WrenScriptInstance::methode(params) const \
191-
{ \
192-
return Call(m_VM->Get##methode##Method(), wrenParams); \
211+
#define IMPL_METHOD_CALL_WITH_PARAM(method, params, wrenParams) \
212+
WrenInterpretResult WrenScriptInstance::method(params) const \
213+
{ \
214+
return Call(m_VM->Get##method##Method(), wrenParams); \
193215
}
194216

195217
IMPL_METHOD_CALL_NO_PARAM(OnCreate)
@@ -204,12 +226,11 @@ namespace OverEngine
204226
ObjClass* klass = wrenGetClass(m_VM->GetRaw(), m_InstanceHandle->value);
205227
ObjInstance* instance = AS_INSTANCE(m_InstanceHandle->value);
206228

207-
auto& fields = m_VM->GetFields(klass->name->value);
229+
auto& fields = m_VM->GetFields(klass);
208230

209231
for (int i = 0; i < klass->numFields; i++)
210232
{
211-
// FIXME: Climb the inheritance chain to access superclass fields.
212-
ImGui::TextUnformatted(i == 0 ? "_entity" : fields[i - 1].c_str());
233+
ImGui::TextUnformatted(fields[i].c_str());
213234
ImGui::SameLine();
214235
ImGui::TextUnformatted(m_VM->ToString(instance->fields[i]).c_str());
215236
}

OverEngine/src/OverEngine/Scripting/Wren.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace OverEngine
5050
return wrenCall(m_VM, method);
5151
}
5252

53+
// TODO: run code on custom module.
54+
inline wrenpp::Result ExecuteString(const char* string) { return m_VM.executeString(string); }
5355
inline wrenpp::Result LoadModule(const char* moduleName) { return m_VM.executeModule(moduleName); }
5456
wrenpp::ModuleContext beginModule(const String& name) { return m_VM.beginModule(name); }
5557

@@ -60,15 +62,17 @@ namespace OverEngine
6062

6163
inline WrenHandle* GetScheduler() { return m_Scheduler; }
6264

63-
inline const Vector<String>& GetFields(const char* className) const { return m_FieldNames.at(className); }
65+
const Vector<String>& GetFields(ObjClass* klass) const;
6466

6567
private:
6668
// Implemented in WrenBindings.cpp
6769
void InitializeBindings();
70+
71+
void AddSuperClassFields(ObjClass* klass) const;
6872

6973
private:
7074
wrenpp::VM m_VM;
71-
UnorderedMap<String, Vector<String>> m_FieldNames;
75+
mutable UnorderedMap<String, std::pair<bool, Vector<String>>> m_FieldNames;
7276

7377
WrenHandle* m_Scheduler;
7478

OverEngine/src/Wren/scheduler.wren

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Scheduler {
4747
if (schedule.resumeTime <= Time.time) {
4848
addFiber(__scheduled.removeAt(i).fiber)
4949
}
50-
i = i - 1
50+
i = i - 1
5151
}
5252
}
5353

OverEngine/vendor/wren

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Run `GenerateProjectFiles.bat` and open the generated solution file in Visual St
1414
### Linux
1515
Build with CMake:
1616
```sh
17-
cmake -S . -B build && cmake --build build
17+
git clone https://github.com/OverShifted/OverEngine
18+
cd OverEngine
19+
cmake -B build -DCMAKE_BUILD_TYPE=Debug && cmake --build build
1820
```
1921
## Credits
2022
* [spdlog](https://github.com/gabime/spdlog)
@@ -30,3 +32,4 @@ cmake -S . -B build && cmake --build build
3032
* [wrenpp](https://github.com/Nelarius/wrenpp)
3133

3234
Contributions are welcome.
35+

Sandbox/src/SandboxECS/SandboxECS.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <OverEngine/Scene/SceneSerializer.h>
44
#include <wren.h>
55

6+
#include <OverEngine/ImGui/ExtraImGui.h>
67
#include <imgui.h>
78

89
using namespace OverEngine;
@@ -218,6 +219,18 @@ void SandboxECS::OnImGuiRender()
218219
ImGui::Begin("CameraController script");
219220
script->Inspect();
220221
ImGui::End();
222+
223+
ImGui::Begin("Wren console");
224+
static String wrenInputValue = "";
225+
ImGui::PushItemWidth(-35);
226+
ImGui::InputText("##WrenInput", &wrenInputValue);
227+
ImGui::PopItemWidth();
228+
ImGui::SameLine();
229+
if (ImGui::Button(">"))
230+
{
231+
m_VM->ExecuteString(wrenInputValue.c_str());
232+
}
233+
ImGui::End();
221234
}
222235

223236
void SandboxECS::OnEvent(Event& event)

Sandbox/src/wren/scripts.wren

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Player is Script {
4242
}
4343

4444
class CameraController is Script {
45+
static instances { __instances }
4546
construct new(e) {
4647
super(e)
4748
_foo = 42
@@ -54,6 +55,9 @@ class CameraController is Script {
5455
"color": "red"
5556
})
5657

58+
if (__instances == null) { __instances = [] }
59+
__instances.add(this)
60+
5761
// It will cause infinite recursion
5862
// _baz.add(_baz)
5963
}

premake5.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ includeDir["json"] = "%{wks.location}/OverEngine/vendor/json/include"
4747
includeDir["fmt"] = "%{wks.location}/OverEngine/vendor/fmt/include"
4848
includeDir["yaml_cpp"] = "%{wks.location}/OverEngine/vendor/yaml-cpp/include"
4949
includeDir["wren"] = "%{wks.location}/OverEngine/vendor/wren/src/include"
50+
includeDir["wren_vm"] = "%{wks.location}/OverEngine/vendor/wren/src/vm"
5051

5152
linkLibs = {
5253
"GLFW",
@@ -70,6 +71,7 @@ clientIncludes = {
7071
"%{includeDir.fmt}",
7172
"%{includeDir.yaml_cpp}",
7273
"%{includeDir.wren}",
74+
"%{includeDir.wren_vm}",
7375
}
7476

7577
group "Dependencies"

0 commit comments

Comments
 (0)