11#include " pcheader.h"
22#include " Wren.h"
33
4+ #include < imgui.h>
5+
6+ extern " C" {
7+ #include < wren_vm.h>
8+ }
9+
410namespace WrenSources
511{
612 #include " Wren/components.wren.inc"
@@ -31,6 +37,7 @@ namespace OverEngine
3137 Wren::Wren ()
3238 : m_VM()
3339 {
40+ // FIXME: Only one VM instance can exist, because there callbacks are static variables.
3441 wrenpp::VM ::writeFn = [](const char * message) { if (strcmp (message, " \n " )) OE_INFO (" [wren] {}" , message); };
3542 wrenpp::VM ::errorFn = [](WrenErrorType type, const char * moduleName, int line, const char * message) -> void {
3643 const char * typeStr = ErrorTypeToString (type);
@@ -40,6 +47,15 @@ namespace OverEngine
4047 OE_ERROR (" {}> {}" , typeStr, message);
4148 };
4249
50+ // This wont register superclasses fields.
51+ wrenpp::VM ::reportClassFn = [this ](ClassInfo* classInfo) {
52+ this ->m_FieldNames [classInfo->name ->value ] = Vector<String>();
53+ auto & fieldNames = this ->m_FieldNames [classInfo->name ->value ];
54+
55+ SymbolTable* syms = &classInfo->fields ;
56+ for (ObjString** current = syms->data ; current < syms->data + syms->count ; current++)
57+ fieldNames.push_back ((*current)->value );
58+ };
4359
4460 wrenpp::VM ::loadModuleFn = [](const char * mod) -> char * {
4561 #define WREN_MOD (m ) if (strcmp(mod, #m) == 0 ) return const_cast <char *>(WrenSources::m##ModuleSource);
@@ -73,6 +89,8 @@ namespace OverEngine
7389 };
7490
7591 InitializeBindings ();
92+
93+ m_Scheduler = GetVariable (" scheduler" , " Scheduler" );
7694
7795 m_OnCreateMethod = wrenMakeCallHandle (m_VM, " onCreate()" );
7896 m_OnDestroyMethod = wrenMakeCallHandle (m_VM, " onDestroy()" );
@@ -92,17 +110,45 @@ namespace OverEngine
92110
93111 wrenReleaseHandle (m_VM, m_OnCollisionEnterMethod);
94112 wrenReleaseHandle (m_VM, m_OnCollisionExitMethod);
113+
114+ for (auto [_, handle] : m_CallHandles)
115+ wrenReleaseHandle (m_VM, handle);
95116 }
96117
97- WrenScriptClass::WrenScriptClass (const Ref<Wren>& vm, const char * moduleName, const char * className)
98- : m_VM(vm)
118+ WrenHandle* Wren::CallHandle (const char * signature) const
99119 {
100- wrenEnsureSlots (m_VM-> GetRaw (), 1 );
101- wrenGetVariable (m_VM-> GetRaw (), moduleName, className, 0 ) ;
120+ if ( auto handle = m_CallHandles. find (signature); handle != m_CallHandles. end ())
121+ return handle-> second ;
102122
103- OE_CORE_ASSERT (wrenGetSlotType (m_VM->GetRaw (), 0 ) != WREN_TYPE_NULL , " Wren class is null! Maybe it failed to compile." );
123+ WrenHandle* callHandle = wrenMakeCallHandle (m_VM, signature);
124+ m_CallHandles[signature] = callHandle;
125+ return callHandle;
126+ }
127+
128+ WrenHandle* Wren::GetVariable (const char * moduleName, const char * variableName)
129+ {
130+ wrenEnsureSlots (m_VM, 1 );
131+ wrenGetVariable (m_VM, moduleName, variableName, 0 );
132+ return wrenGetSlotType (m_VM, 0 ) == WREN_TYPE_NULL ? nullptr : wrenGetSlotHandle (m_VM, 0 );
133+ }
134+
135+ String Wren::ToString (WrenHandle* handle) const
136+ {
137+ Call (handle, CallHandle (" toString" ));
138+ return String (wrenGetSlotString (m_VM, 0 ));
139+ }
140+
141+ String Wren::ToString (Value value) const
142+ {
143+ WrenHandle handle { value, nullptr , nullptr };
144+ return ToString (&handle);
145+ }
104146
105- m_ClassHandle = wrenGetSlotHandle (m_VM->GetRaw (), 0 );
147+ WrenScriptClass::WrenScriptClass (const Ref<Wren>& vm, const char * moduleName, const char * className)
148+ : m_VM(vm)
149+ {
150+ m_ClassHandle = m_VM->GetVariable (moduleName, className);
151+ OE_CORE_ASSERT (m_ClassHandle, " Wren class is null! Maybe it failed to compile." );
106152 m_ConstructorHandle = wrenMakeCallHandle (m_VM->GetRaw (), " new(_)" );
107153 }
108154
@@ -114,7 +160,7 @@ namespace OverEngine
114160
115161 Ref<WrenScriptInstance> WrenScriptClass::Construct (const Entity& entity) const
116162 {
117- if (m_VM->CallMethod (m_ClassHandle, m_ConstructorHandle, entity) != WREN_RESULT_SUCCESS )
163+ if (m_VM->Call (m_ClassHandle, m_ConstructorHandle, entity) != WREN_RESULT_SUCCESS )
118164 {
119165 OE_CORE_ASSERT (false , " Script instantiation failure!" );
120166 return nullptr ;
@@ -134,34 +180,38 @@ namespace OverEngine
134180 wrenReleaseHandle (m_VM->GetRaw (), m_InstanceHandle);
135181 }
136182
137- // TODO: Return result
138- void WrenScriptInstance::OnCreate () const
139- {
140- m_VM-> CallMethod (m_InstanceHandle, m_VM->GetOnCreateMethod ());
141- }
183+ # define IMPL_METHOD_CALL_NO_PARAM ( methode ) \
184+ WrenInterpretResult WrenScriptInstance::methode () const \
185+ { \
186+ return Call ( m_VM->Get ##methode## Method ()); \
187+ }
142188
143- void WrenScriptInstance::OnDestroy () const
144- {
145- m_VM->CallMethod (m_InstanceHandle, m_VM->GetOnDestroyMethod ());
146- }
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); \
193+ }
147194
148- void WrenScriptInstance::OnUpdate (float delta) const
149- {
150- m_VM->CallMethod (m_InstanceHandle, m_VM->GetOnUpdateMethod (), delta);
151- }
195+ IMPL_METHOD_CALL_NO_PARAM (OnCreate)
196+ IMPL_METHOD_CALL_NO_PARAM (OnDestroy)
197+ IMPL_METHOD_CALL_WITH_PARAM (OnUpdate, float delta, delta)
198+ IMPL_METHOD_CALL_WITH_PARAM (OnLateUpdate, float delta, delta)
199+ IMPL_METHOD_CALL_NO_PARAM (OnCollisionEnter)
200+ IMPL_METHOD_CALL_NO_PARAM (OnCollisionExit)
152201
153- void WrenScriptInstance::OnLateUpdate ( float delta ) const
202+ void WrenScriptInstance::Inspect ( ) const
154203 {
155- m_VM-> CallMethod (m_InstanceHandle, m_VM->GetOnLateUpdateMethod (), delta );
156- }
204+ ObjClass* klass = wrenGetClass ( m_VM->GetRaw (), m_InstanceHandle-> value );
205+ ObjInstance* instance = AS_INSTANCE (m_InstanceHandle-> value );
157206
158- void WrenScriptInstance::OnCollisionEnter () const
159- {
160- m_VM->CallMethod (m_InstanceHandle, m_VM->GetOnCollisionEnterMethod ());
161- }
207+ auto & fields = m_VM->GetFields (klass->name ->value );
162208
163- void WrenScriptInstance::OnCollisionExit () const
164- {
165- m_VM->CallMethod (m_InstanceHandle, m_VM->GetOnCollisionExitMethod ());
209+ for (int i = 0 ; i < klass->numFields ; i++)
210+ {
211+ // FIXME: Climb the inheritance chain to access superclass fields.
212+ ImGui::TextUnformatted (i == 0 ? " _entity" : fields[i - 1 ].c_str ());
213+ ImGui::SameLine ();
214+ ImGui::TextUnformatted (m_VM->ToString (instance->fields [i]).c_str ());
215+ }
166216 }
167217}
0 commit comments