惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

ccagml

使用valgrind观察luajit进程内存 - ccagml 从生产环境报错学习protobuf编码规则 - ccagml 跟着vscode插件学设计模式-工厂模式 - ccagml 拨开迷雾,探寻深夜游戏集群启动失败真相 - ccagml 从技能状态图标显示错误到给 LuaJIT 报告bug - ccagml 游戏系统MySQl执行超时问题排查 - ccagml lua中#号是怎么计算字符串长度的 - ccagml lua中#号是怎么计算长度的 - ccagml lua中tonumber做了什么 - ccagml
lua中pairs和ipairs都做了什么操作 - ccagml
2022-05-14 · via ccagml

1. 一些简单的例子

例子中可以看出 ipairs只输出了数组部分,nil截断之后也不会输出

a = {1, 2, nil, 4, b = 1}

for k, v in ipairs(a) do
    print("ipairs", k, v);
end
for k, v in pairs(a) do
    print("pairs", k, v);
end

// 输出
ipairs  1       1
ipairs  2       2
pairs   1       1
pairs   2       2
pairs   4       4
pairs   b       1


ipairs是怎么取值的

可以看出 每次取值是i++ 的取值法

static int ipairsaux(gafq_State *L)
{
    int i = gafqL_checkint(L, 2);
    gafqL_checktype(L, 1, GAFQ_TTABLE);
    i++; // 可以看出 每次取值是i++ 的取值法
    gafq_pushinteger(L, i);
    gafq_rawgeti(L, 1, i);
    return (gafq_isnil(L, -1)) ? 0 : 2;
}

pairs是怎么取值的

可以看出 pairs是通过 栈顶来取值的在gafqH_next中可以看出 会先去array部分 在取hash部分

static int gafqBase_next(gafq_State *L)
{
    gafqL_checktype(L, 1, GAFQ_TTABLE);
    gafq_settop(L, 2);
    if (gafq_next(L, 1))
        return 2;
    else
    {
        gafq_pushnil(L);
        return 1;
    }
}

GAFQ_API int gafq_next(gafq_State *L, int idx)
{
    StkId t;
    int more;
    gafq_lock(L);
    t = idx_to_Tvalue(L, idx);
    api_check(L, ttistable(t));
    more = gafqH_next(L, hvalue(t), L->top - 1);
    if (more)
    {
        api_incr_top(L);
    }
    else              
        L->top -= 1;
    gafq_unlock(L);
    return more;
}

int gafqH_next(gafq_State *L, Table *t, StkId key)
{
    int i = findindex(L, t, key);
    for (i++; i < t->sizearray; i++)
    {  
        if (!ttisnil(&t->array[i]))
        {
            setnvalue(key, cast_num(i + 1));
            setobj2s(L, key + 1, &t->array[i]);
            return 1;
        }
    }
    for (i -= t->sizearray; i < sizenode(t); i++)
    { 
        if (!ttisnil(gval(gnode(t, i))))
        { 
            setobj2s(L, key, key2tval(gnode(t, i)));
            setobj2s(L, key + 1, gval(gnode(t, i)));
            return 1;
        }
    }
    return 0;
}