aboutsummaryrefslogtreecommitdiff
path: root/src/ltable.js
blob: 95ca5a59bdca9f8d750207f73a4d7015c317c656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*jshint esversion: 6 */
"use strict";

const assert  = require('assert');

const lobject = require('./lobject.js');
const nil     = require('./ldo.js').nil;
const Table   = lobject.Table;


/*
** Try to find a boundary in table 't'. A 'boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
*/
Table.prototype.luaH_getn = function() {
    let array = this.value.array;
    let hash = this.value.hash;

    let j = array.length;
    if (j > 0 && array[j - 1].ttisnil()) {
        /* there is a boundary in the array part: (binary) search for it */
        let i = 0;
        while (j - i > 1) {
            let m = (i+j)/2;
            if (array[m - 1].ttisnil()) j = m;
            else i = m;
        }
        return i;
    }
    /* else must find a boundary in hash part */
    else if (hash.size === 0)
        return j;
    else return hash.get(j);
};