Skip to content

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

@Peach1

Description

@Peach1
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions