Skip to content

Commit 596badd

Browse files
authored
[cppia] Fix haxe overridden scriptable methods (#1273)
* [cppia] Add flag for scriptable overridden methods This allows overridden methods to be marked so that they do not add an addtional slot to the vtable, which has previously caused bugs due to incorrect vtable indices. * [cppia] Infer overrides from matching method name For haxe versions that do not tag overridden methods in __scriptableFunctions tables, we can infer overrides at runtime by comparing method names. * [cppia] Swap scriptable func argument order This way is a bit more backwards compatible. An overridden method will always have a super version provided.
1 parent f8eb283 commit 596badd

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

include/hx/Scriptable.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ struct ScriptNamedFunction : public ScriptFunction
1919
{
2020
ScriptNamedFunction(const ScriptFunction &s) : ScriptFunction(s), name(0), isStatic(false), superExecute(0) { }
2121

22+
#if (HXCPP_API_LEVEL >= 500)
23+
ScriptNamedFunction(const char *inName=0,StackExecute inExe=0,const char *inSig=0, bool inIsStatic=false, StackExecute superExecute=0, bool inIsOverride=false)
24+
: ScriptFunction(inExe, inSig), name(inName), isStatic(inIsStatic), isOverride(inIsOverride), superExecute(superExecute) { }
25+
#else
2226
ScriptNamedFunction(const char *inName=0,StackExecute inExe=0,const char *inSig=0, bool inIsStatic=false, StackExecute superExecute=0)
2327
: ScriptFunction(inExe, inSig), name(inName), isStatic(inIsStatic), superExecute(superExecute) { }
28+
#endif
2429

2530
const char *name;
2631
bool isStatic;
32+
#if (HXCPP_API_LEVEL >= 500)
33+
bool isOverride;
34+
#endif
2735
StackExecute superExecute;
2836
};
2937

src/hx/cppia/Cppia.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,9 @@ struct CppiaVar
575575

576576

577577

578+
#if (HXCPP_API_LEVEL >= 500)
579+
# define NATIVE_CLASS_OVERRIDES_MARKED
580+
#endif
578581

579582
class HaxeNativeClass
580583
{
@@ -596,6 +599,10 @@ class HaxeNativeClass
596599
static HaxeNativeClass *findClass(const std::string &inName);
597600
static HaxeNativeClass *hxObject();
598601
static void link();
602+
#ifndef NATIVE_CLASS_OVERRIDES_MARKED
603+
private:
604+
void addVtableEntries( std::vector<std::string> &outVtable, hx::UnorderedSet<std::string> &outMethodsSet);
605+
#endif
599606
};
600607

601608
class HaxeNativeInterface

src/hx/cppia/HaxeNative.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,36 @@ HaxeNativeClass::HaxeNativeClass(const std::string &inName, int inDataOffset, Sc
3030
haxeSuper = 0;
3131
}
3232

33+
#ifdef NATIVE_CLASS_OVERRIDES_MARKED
3334
void HaxeNativeClass::addVtableEntries( std::vector<std::string> &outVtable)
3435
{
3536
if (haxeSuper)
3637
haxeSuper->addVtableEntries(outVtable);
3738

3839
if (functions)
3940
for(ScriptNamedFunction *func = functions; func->name; func++)
40-
if (!func->isStatic)
41+
if (!func->isStatic && !func->isOverride)
4142
outVtable.push_back( func->name );
4243
}
44+
#else
45+
void HaxeNativeClass::addVtableEntries(std::vector<std::string>& outVtable) {
46+
hx::UnorderedSet<std::string> methodsSet;
47+
addVtableEntries(outVtable, methodsSet);
48+
}
49+
50+
void HaxeNativeClass::addVtableEntries( std::vector<std::string> &outVtable, hx::UnorderedSet<std::string>& outMethodsSet)
51+
{
52+
if (haxeSuper)
53+
haxeSuper->addVtableEntries(outVtable, outMethodsSet);
54+
55+
if (functions)
56+
for (ScriptNamedFunction* func = functions; func->name; func++)
57+
if (!func->isStatic && outMethodsSet.find(func->name) == outMethodsSet.end()) {
58+
outVtable.push_back(func->name);
59+
outMethodsSet.emplace(func->name);
60+
}
61+
}
62+
#endif
4363

4464
void HaxeNativeClass::dump()
4565
{

0 commit comments

Comments
 (0)