/**
  Testing various cases which have historically caused
  problems for value lifetimes...
*/

false || scope {
    assert 0 === refcount (false || scope {
        //var a array[0, array[0,1, array[3]] ]
        //assert 3 === a.1.(1+1).0
        var a object {"1":array [3,2,1]}
        a.1
    })
}

false || scope {
    var count = 0;
    var f = proc {n} {
        assert n>0
        var i = 0
        while {i<n} {
            set i i+1
        }
    }
    [f 100] /* valgrind: ensure that mem costs do not grow with N
               when recycling is on. */
    [f 300]
    [f 1000]
    [f 1]
    [f 1]
}

//assert "hi, hi" ===
assert 1 === refcount var xx scope {
    const s "hi, hi!"
}
unset xx

assert 0 === refcount scope {
    "hi, hi, hi!"
}

assert 17 === false || scope {
    var obj = object {a:17}
    obj.a
}
assert 1 < refcount (scope {
    var obj = object {a:17}
    return obj.prototype
})

scope {
    var o object{}
    assert 3 === refcount (o.(o) = o)
    unset o
}

0 && scope {
    var f1 proc{} {throw "f1!"},
        f2 proc{} {[f1]},
        f3 proc{} {[f2]}
    var f4 proc {}{
        $proc {}{
            const ex = catch {[f3]}
            assert 'exception' === typename ex
            //assert 'array' === typename ex.callStack
            //$print ex
            throw ex
        }
    }

    //const '#FOO#' 7 // fixme: strings as keys here
    //assert 7 === get '#FOO#'
    [f4]
}

return {end of script #10}
