-
Notifications
You must be signed in to change notification settings - Fork 10
Description
function mod(&x) { x = 5; } local x = 123; mod(x); print(x);Result desired:
5
We know existing-Squirrel cannot do this, but is it possible to implement this in Squirrel sqvm.cpp and sqcompiler.cpp?
How to modify integers by reference, like C++ can do
void mod(int& x) { x = 5; } int x = 123; mod(x); printf("%i\n", x);5
In Squirrel, the approximate thing is _OP_SETOUTER _OP_GETOUTER. It is used by closure functions
local x = 123; function mod() { x = 5; } mod(); print(x) // x is now 5squilu/SquiLu/squirrel/sqvm.cpp
Line 1934 in ef96bae
| void SQVM::FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex) |
squilu/SquiLu/squirrel/sqvm.cpp
Line 687 in ef96bae
| FindOuter(closure->_outervalues[i], &STK(_integer(v._src))); |
squilu/SquiLu/squirrel/sqfuncstate.cpp
Line 390 in ef96bae
| _parent->MarkLocalAsOuter(pos); |
squilu/SquiLu/squirrel/sqvm.cpp
Line 1162 in ef96bae
| OPCODE_TARGET(SETOUTER) { |
See _OP_SETOUTER _OP_GETOUTER MarkLocalAsOuter() GetOuterVariable() and FindOuter()
function mod()
-----OUTERS
0 x
-----Instructions
[000] _OP_LOADINT 5
[001] _OP_SETOUTER 255 0[x] // could be extended to support true references
[002] _OP_LOADNULLS 1 1
[003] _OP_RETURN 255 0
local x = 123; function mod() { x = 5; } mod(); print(x)5
With some modification, could real references be added to Squirrel / Squilu ?:
function mod(&x) { x = 5; } local x = 123; mod(x); print(x);5