[Paper Reading] The Implementation of Lua 5.0

Ierusalimschy, R., De Figueiredo, L. H., & Celes Filho, W. (2005). The Implementation of Lua 5.0. J. UCS, 11(7), 1159-1176.

Introduction

Main novelties:

  • Register-based virtual machine
  • Optimized tables
  • Closures
  • Coroutines

Values

Dynamically-typed language, basic types:

  • nil
  • boolean: true, false
  • number: double (float/long)
  • string: arrays of bytes
  • table: associative arrays
  • function
  • userdata: pointers to user memory blocks: heavy, subject to garbage collection; light, freed by user
  • thread: coroutines
typedef struct {
    int tag;  /* Identify the type */
    Value v;
} TObject;

typedef union {
    GCObject *gc; /* strings, functions, heavy userdata and threads */
    void *p;  /* light userdata */
    lua_Number n;  /* numbers */
    int b;  /* booleans */
} Value;

Tables

A Lua table

Hybrid: hash part and array part

Growth of array part:

  • at least half the slots between 1 and n are in use
  • at least on used slot between n/2+1 and n <script type="math/tex" id="MathJax-Element-50">n</script>

Hash part: uses a mix of chained scatter table with Brent’s variation

Functions and Closures

An upvalue before and after being "closed".

Implement closures: upvalue,
flat closures

Threads and Coroutines

create, resume, yield

interpreter cannot use its internal C stack to implement calls in the interpreted code

uses C stack to keep track of the stack of active coroutines at any given time

cactus structure

The Virtual Machine

The instructions in Lua's virtual machine

Bytecode for a Lua function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值