aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: b46c58de0a13ecc93723f430cc0373ed8a1f88d8 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
[![Build Status](https://travis-ci.org/fengari-lua/fengari.svg?branch=master)](https://travis-ci.org/fengari-lua/fengari)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![#fengari on Freenode](https://img.shields.io/Freenode/%23fengari.png)](https://webchat.freenode.net/?channels=fengari)

<p align="center">
    <img src="https://github.com/fengari-lua/fengari/raw/master/logo.png" alt="Fengari" width="304" height="304">
</p>


# fengari

🐺 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser

## So far

- [x] Lexing/Parsing
- [x] Parse bytecode
- [x] Opcodes
- [x] Basic types representation:
- [x] Tag Methods
- [ ] Standard library
    - [x] Base lib
    - [x] Coroutine
    - [x] Debug
    - [x] Math
    - [x] String
    - [x] Table
    - [x] utf8
    - [x] os (~~`os.setlocale()`~~)
    - [x] Package
    - [ ] io
        - [x] `file:__tostring()`
        - [x] `file:flush()`
        - [x] `file:write()`
        - [x] `io.close()`
        - [x] `io.stderr`
        - [x] `io.stdin`
        - [x] `io.stdout`
        - [x] `io.type()`
        - [x] `io.write()`
        - [x] `io.flush()`
        - [ ] `io.input()`: partially implemented
        - [ ] `io.lines()`
        - [ ] `io.open()`
        - [ ] `io.output()`: partially implemented
        - [ ] `io.popen()`
        - [ ] `io.read()`
        - [ ] `io.tmpfile()`
        - [ ] `file:lines()`
        - [ ] `file:read()`
        - [ ] `file:setvbuf()`
        - [ ] `file:__gc()`
- [x] C API
- [x] Auxiliary library
- [ ] Run [Lua test suite](https://github.com/lua/tests)
    - [x] `bitwise.lua` (16/16)
    - [x] `calls.lua` (32/32)
    - [x] `closure.lua` (16/16)
    - [x] `constructs.lua` (`_soft`) (10/10)
    - [x] `errors.lua` (64/64)
    - [x] `events.lua` (26/26)
    - [x] `goto.lua` (18/18)
    - [x] `literals.lua` (30/30)
    - [x] `locals.lua` (10/10)
    - [x] `nextvar.lua` (44/44)
    - [x] `strings.lua` (34/34)
    - [x] `vararg.lua` (8/8)
    - [ ] `attrib.lua`
    - [ ] `coroutine.lua` (32/40)
    - [ ] `math.lua` (44/68)
    - [ ] `pm.lua` (27/38)
    - [ ] `sort.lua` (21/24)
    - [ ] `tpack.lua` (20/32)
    - [ ] `utf8.lua` (14/20)
    - [ ] `api.lua`
    - [ ] `big.lua`
    - [ ] `code.lua`
    - [ ] `db.lua`
    - [ ] `verybig.lua`
- [ ] DOM API binding: [https://github.com/fengari-lua/fengari-interop](https://github.com/fengari-lua/fengari-interop)


## Extensions

### `dv = lua_todataview(L, idx)`

Equivalent to `lua_tolstring` but returns a [`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) instead of a string.


### `str = lua_toljsstring(L, idx)`

Equivalent to `lua_tolstring` but returns the string as a JavaScript string (as if `to_jsstring()` was called on the result).


### `lua_tojsstring(L, idx)`

Alias for `lua_toljsstring`.


### `lua_pushjsfunction(L, func)`

Alias for `lua_pushcfunction`.


### `lua_pushjsclosure(L, func, n)`

Alias for `lua_pushcclosure`.


### `lua_atnativeerror(L, func)`

Sets a function to be called if a native JavaScript error is thrown across a lua pcall.
The function will be run as if it were a message handler (see https://www.lua.org/manual/5.3/manual.html#2.3).
The current message handler will be run after the native error handler returns.


### `b = lua_isproxy(p, L)`

Returns a boolean `b` indicating whether `p` is a proxy (See `lua_toproxy`).
If `L` is non-null, only returns `true` if `p` belongs to the same global state.


### `p = lua_toproxy(L, idx)`

Returns a JavaScript object `p` that holds a reference to the lua value at the stack index `idx`.
This object can be called with a lua_State to push the value onto that state's stack.

This example would be an inefficient way to write `lua_pushvalue(L, 1)`:

```js
var p = lua_toproxy(L, 1);
p(L);
````


## References

- [Source code for Lua 5.3](lua.org/source/5.3/)
- [Lua 5.2 Bytecode and Virtual Machine](http://files.catwell.info/misc/mirror/lua-5.2-bytecode-vm-dirk-laurie/lua52vm.html)
- [Lua 5.3 Bytecode Reference](http://the-ravi-programming-language.readthedocs.io/en/latest/lua_bytecode_reference.html)
- [A No-Frills Introduction to Lua 5.1 VM Instructions](http://luaforge.net/docman/83/98/ANoFrillsIntroToLua51VMInstructions.pdf)