Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ style '&' reference possible? #4

Open
Peach1 opened this issue Sep 13, 2024 · 1 comment
Open

C++ style '&' reference possible? #4

Peach1 opened this issue Sep 13, 2024 · 1 comment

Comments

@Peach1
Copy link

Peach1 commented Sep 13, 2024

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 5

void SQVM::FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex)

FindOuter(closure->_outervalues[i], &STK(_integer(v._src)));

_parent->MarkLocalAsOuter(pos);

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
@mingodad
Copy link
Owner

Everything is possible when one decides to take time and effort but maybe you'll be better served by something that already implements it like https://github.com/vovkos/jancy.
If you really need something easier is to pass the data as a table/array:

function mod(vx) { vx.x = 5; }  local vx = {x=123]; mod(vx); print(vx.x);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants