Skip to content

Commit 793d8df

Browse files
authored
Merge pull request #441 from samisalreadytaken/vscript-getset-fallback
Fallback if IScriptInstanceHelper::Get/Set are not implemented
2 parents 734130a + 69b68fa commit 793d8df

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

sp/src/vscript/vscript_squirrel.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,19 @@ SQInteger get_stub(HSQUIRRELVM vm)
16501650
}
16511651
else
16521652
{
1653-
sq_retval = sqstd_throwerrorf(vm, "the index '%.50s' does not exist", key);
1653+
// Fallback
1654+
// Extra stack variables don't need to be popped, they are cleaned up on exit
1655+
sq_pushroottable(vm);
1656+
sq_push(vm, -2);
1657+
1658+
if ( SQ_SUCCEEDED( sq_rawget(vm, -2) ) )
1659+
{
1660+
sq_retval = 1;
1661+
}
1662+
else
1663+
{
1664+
sq_retval = sqstd_throwerrorf(vm, "the index '%.50s' does not exist", key);
1665+
}
16541666
}
16551667

16561668
var.Free();
@@ -1684,11 +1696,24 @@ SQInteger set_stub(HSQUIRRELVM vm)
16841696
classInstanceData->desc->pHelper->Set(classInstanceData->instance, key, var)
16851697
))
16861698
{
1687-
sq_retval = sqstd_throwerrorf(vm, "the index '%.50s' does not exist", key);
1699+
// Fallback
1700+
sq_pushroottable(vm);
1701+
sq_push(vm, -3);
1702+
sq_push(vm, -3);
1703+
1704+
if ( SQ_SUCCEEDED( sq_rawset(vm, -3) ) )
1705+
{
1706+
// rawset doesn't return correctly, pop env to return val
1707+
sq_pop(vm, 1);
1708+
sq_retval = 1;
1709+
}
1710+
else
1711+
{
1712+
sq_retval = sqstd_throwerrorf(vm, "the index '%.50s' does not exist", key);
1713+
}
16881714
}
16891715

16901716
var.Free();
1691-
sq_pop(vm, 1);
16921717
return sq_retval;
16931718
}
16941719

@@ -2564,13 +2589,16 @@ bool SquirrelVM::RegisterClass(ScriptClassDesc_t* pClassDesc)
25642589
sq_newclosure(vm_, tostring_stub, 0);
25652590
sq_newslot(vm_, -3, SQFalse);
25662591

2567-
sq_pushstring(vm_, "_get", -1);
2568-
sq_newclosure(vm_, get_stub, 0);
2569-
sq_newslot(vm_, -3, SQFalse);
2592+
if ( pClassDesc->pHelper )
2593+
{
2594+
sq_pushstring(vm_, "_get", -1);
2595+
sq_newclosure(vm_, get_stub, 0);
2596+
sq_newslot(vm_, -3, SQFalse);
25702597

2571-
sq_pushstring(vm_, "_set", -1);
2572-
sq_newclosure(vm_, set_stub, 0);
2573-
sq_newslot(vm_, -3, SQFalse);
2598+
sq_pushstring(vm_, "_set", -1);
2599+
sq_newclosure(vm_, set_stub, 0);
2600+
sq_newslot(vm_, -3, SQFalse);
2601+
}
25742602

25752603
sq_pushstring(vm_, "IsValid", -1);
25762604
sq_newclosure(vm_, IsValid_stub, 0);

0 commit comments

Comments
 (0)