Skip to content

Function Reference Parameters (Extending the 'outer' system) #293

@Peach1

Description

@Peach1

So Squirrel language already has 'outers' which act like references:

local x = 0; function mod() { x = 5; } mod(); print(x) // x is now 5

Squirrel "Outers" are almost references, but Squirrel's outers are limited to local variables in the _parent function.

So, with some modification of Squirrel's outer system, how can we implement this feature:

function mod(&x) { x = 5; }  local x = 0; mod(x); print(x);

For Syntax: Parsing & in CreateFunction would need to call something like MarkLocalAsOuter() for each & reference parameter

Then for runtime

Possibly take this code fromSQVM::CLOSURE_OP()...

    if((nouters = func->_noutervalues)) {
        for(SQInteger i = 0; i<nouters; i++) {
            SQOuterVar &v = func->_outervalues[i];
            switch(v._type){
            case otLOCAL: // outer local stack target, might be possible to use for general references
                FindOuter(closure->_outervalues[i], &STK(_integer(v._src)));
                break;
            case otOUTER:
                closure->_outervalues[i] = _closure(ci->_closure)->_outervalues[_integer(v._src)];
                break;
            }
        }
    }

And setup "references" in _OP_PREPCALL so the reference stack is setup before the function gets called,

Any idea for how to extend the outer system and implement references?
It would be really cool for Squirrel.

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