<commit_before>"use strict";

const assert  = require('assert');

const defs    = require('./defs.js');
const lobject = require('./lobject.js');
const CT      = defs.constant_types;

class Proto {

    constructor(L) {
        this.id = L.l_G.id_counter++;
        this.k = [];              // constants used by the function
        this.p = [];              // functions defined inside the function
        this.code = [];           // opcodes
        this.cache = null;        // last-created closure with this prototype
        this.lineinfo = [];       // map from opcodes to source lines (debug information)
        this.upvalues = [];       // upvalue information
        this.numparams = 0;       // number of fixed parameters
        this.is_vararg = false;
        this.maxstacksize = 0;    // number of registers needed by this function
        this.locvars = [];        // information about local variables (debug information)
        this.linedefined = 0;     // debug information
        this.lastlinedefined = 0; // debug information
        this.source = null;       // used for debug information
    }

}

class UpVal {

    constructor(L) {
        this.id = L.l_G.id_counter++;
        this.v = void 0; /* if open: reference to TValue on stack. if closed: TValue */
        this.vOff = void 0; /* if open: index on stack. if closed: undefined */
        this.refcount = 0;
        this.open_next = null; /* linked list (when open) */
    }

    isopen() {
        return this.vOff !== void 0;
    }

}

const luaF_newLclosure = function(L, n) {
    let c = new lobject.LClosure(L, n);
    return c;
};


const luaF_findupval = function(L, level) {
    let prevp;
    let p = L.openupval;
    while (p !== null && p.vOff >= level) {
        assert(p.isopen());
        if (p.vOff === level) /* found a corresponding upvalue? */
            return p; /* return it */
        prevp = p;
        p = p.open_next;
    }
    /* not found: create a new upvalue */
    let uv = new UpVal(L);
    /* link it to list of open upvalues */
    uv.open_next = p;
    if (prevp)
        prevp.open_next = uv;
    else
        L.openupval = uv;
    uv.v = L.stack[level]; /* current value lives in the stack */
    uv.vOff = level;
    return uv;
};

const luaF_close = function(L, level) {
    while (L.openupval !== null && L.openupval.vOff >= level) {
        let uv = L.openupval;
        assert(uv.isopen());
        L.openupval = uv.open_next; /* remove from 'open' list */
        if (uv.refcount === 0) { /* no references? */
            /* free upvalue */
            uv.v = void 0;
            uv.open_next = null;
        } else {
            let from = uv.v;
            uv.v = new lobject.TValue(from.type, from.value);
        }
        uv.vOff = void 0;
    }
};

/*<commit_msg>src/lfunc.js: Use lua_assert instead of assert<commit_after>"use strict";

const defs    = require('./defs.js');
const { lua_assert } = require('./llimits.js');
const lobject = require('./lobject.js');
const CT      = defs.constant_types;

class Proto {

    constructor(L) {
        this.id = L.l_G.id_counter++;
        this.k = [];              // constants used by the function
        this.p = [];              // functions defined inside the function
        this.code = [];           // opcodes
        this.cache = null;        // last-created closure with this prototype
        this.lineinfo = [];       // map from opcodes to source lines (debug information)
        this.upvalues = [];       // upvalue information
        this.numparams = 0;       // number of fixed parameters
        this.is_vararg = false;
        this.maxstacksize = 0;    // number of registers needed by this function
        this.locvars = [];        // information about local variables (debug information)
        this.linedefined = 0;     // debug information
        this.lastlinedefined = 0; // debug information
        this.source = null;       // used for debug information
    }

}

class UpVal {

    constructor(L) {
        this.id = L.l_G.id_counter++;
        this.v = void 0; /* if open: reference to TValue on stack. if closed: TValue */
        this.vOff = void 0; /* if open: index on stack. if closed: undefined */
        this.refcount = 0;
        this.open_next = null; /* linked list (when open) */
    }

    isopen() {
        return this.vOff !== void 0;
    }

}

const luaF_newLclosure = function(L, n) {
    let c = new lobject.LClosure(L, n);
    return c;
};


const luaF_findupval = function(L, level) {
    let prevp;
    let p = L.openupval;
    while (p !== null && p.vOff >= level) {
        lua_assert(p.isopen());
        if (p.vOff === level) /* found a corresponding upvalue? */
            return p; /* return it */
        prevp = p;
        p = p.open_next;
    }
    /* not found: create a new upvalue */
    let uv = new UpVal(L);
    /* link it to list of open upvalues */
    uv.open_next = p;
    if (prevp)
        prevp.open_next = uv;
    else
        L.openupval = uv;
    uv.v = L.stack[level]; /* current value lives in the stack */
    uv.vOff = level;
    return uv;
};

const luaF_close = function(L, level) {
    while (L.openupval !== null && L.openupval.vOff >= level) {
        let uv = L.openupval;
        lua_assert(uv.isopen());
        L.openupval = uv.open_next; /* remove from 'open' list */
        if (uv.refcount === 0) { /* no references? */
            /* free upvalue */
            uv.v = void 0;
            uv.open_next = null;
        } else {
            let from = uv.v;
            uv.v = new lobject.TValue(from.type, from.value);
        }
        uv.vOff = void 0;
    }
};

/*
{'fraction_non_alphanumeric': 0.13042707779549884, 'fraction_numerical': 0.003898635477582846, 'mean_word_length': 1.778926637124569, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2428124', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1738, 'n_words': 511}