diff --git a/.gitignore b/.gitignore index 5d21df4..6c71b9c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ lua luac + +luacore.h diff --git a/Makefile b/Makefile index 72ca8ff..9006854 100644 --- a/Makefile +++ b/Makefile @@ -36,17 +36,17 @@ RM= rm -f # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= # Convenience platforms targets. -PLATS= guess aix bsd c89 freebsd generic ios linux linux-readline macosx mingw posix solaris +PLATS= guess aix bsd c89 freebsd generic ios linux macosx mingw posix solaris # What to install. TO_BIN= lua luac -TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp +TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp luacore.h llimits.h TO_LIB= liblua.a TO_MAN= lua.1 luac.1 # Lua version and release. -V= 5.4 -R= $V.7 +V= 5.5 +R= $V.0 # Targets start here. all: $(PLAT) diff --git a/README b/README deleted file mode 100644 index c394c69..0000000 --- a/README +++ /dev/null @@ -1,6 +0,0 @@ - -This is Lua 5.4.4, released on 13 Jan 2022. - -For installation instructions, license details, and -further information about Lua, see doc/readme.html. - diff --git a/README.md b/README.md new file mode 100644 index 0000000..14cfba2 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# PUC-Lua with exposed internals + +This is a **patched PUC-Lua exposing low-level implementation details**. +This version of Lua is used by some experimental projects including +[Pallene](https://www.github.com/pallene-lang/pallene) and +[LuaAOT](https://github.com/hugomg/lua-aot-5.4). + +Use these internal APIs at your own risk! +They are unstable and may change even after a bugfix patch (e.g. 5.4.2 -> 5.4.3). +Additionally, they are unsafe. +You can easily get a segfault or worse if you don't know what you are doing. + +# Compiling and installing + +Compile and install using the provided Makefile, +the same way you would for upstream PUC-Lua. +Detailed instructions can be found in the doc/readme.html. + +# Using + +There is a new header file called `luacore.h`, +which contains all the internals APIs from the Lua core (e.g `lgc.h`, `lstring.h`, etc). + +There is a new function `luaL_checkcoreversion` in `lauxlib.h`. +It is similar to `luaL_checkversion`, except that also compares the patch number (e.g. 5.4.2 vs 5.4.3). diff --git a/UPDATING.md b/UPDATING.md new file mode 100644 index 0000000..a8ed11f --- /dev/null +++ b/UPDATING.md @@ -0,0 +1,16 @@ +How to update to a new Lua release +================================== + +1. Switch to upstream branch +2. Replace doc/ src/ and Makefile with the new version +3. Commit and push + +4. Switch to main branch +5. Merge and fix conflicts +6. Commit and push + +7. git tag {5.x.x} +8. git push origin {5.x.x} + +You don't need to manually update luacore.h. +It is automatically generated by the src/Makefile. diff --git a/doc/OSIApproved.png b/doc/OSIApproved.png new file mode 100644 index 0000000..46f920e Binary files /dev/null and b/doc/OSIApproved.png differ diff --git a/doc/OSIApproved_100X125.png b/doc/OSIApproved_100X125.png deleted file mode 100644 index 795f7a0..0000000 Binary files a/doc/OSIApproved_100X125.png and /dev/null differ diff --git a/doc/contents.html b/doc/contents.html index e171048..b549321 100644 --- a/doc/contents.html +++ b/doc/contents.html @@ -1,7 +1,7 @@ -Lua 5.4 Reference Manual - contents +Lua 5.5 Reference Manual - contents @@ -10,8 +10,8 @@

-Lua -Lua 5.4 Reference Manual +Lua +Lua 5.5 Reference Manual

@@ -32,7 +32,7 @@

-Copyright © 2020–2024 Lua.org, PUC-Rio. +Copyright © 2020–2025 Lua.org, PUC-Rio. Freely available under the terms of the Lua license. @@ -44,7 +44,7 @@

Contents

  • 2 – Basic Concepts
  • 3.4 – Expressions @@ -85,10 +85,8 @@

    Contents

  • 3.4.9 – Table Constructors
  • 3.4.10 – Function Calls
  • 3.4.11 – Function Definitions -
  • 3.4.12 – Lists of expressions, multiple results, and adjustment - +
  • 3.4.12 – Lists of Expressions, Multiple Results, and Adjustment -
  • 3.5 – Visibility Rules

  • 4 – The Application Program Interface @@ -97,7 +95,7 @@

    Contents

  • 4.2 – C Closures
  • 4.3 – Registry @@ -117,20 +115,21 @@

    Contents

  • 6 – The Standard Libraries

  • 7 – Lua Standalone @@ -151,7 +150,7 @@

    Index

    Lua functions

    -basic
    +basic
    _G
    _VERSION
    assert
    @@ -180,7 +179,7 @@

    Lua functions

    xpcall

    -coroutine
    +coroutine
    coroutine.close
    coroutine.create
    coroutine.isyieldable
    @@ -191,7 +190,7 @@

    Lua functions

    coroutine.yield

    -debug
    +debug
    debug.debug
    debug.gethook
    debug.getinfo
    @@ -210,7 +209,7 @@

    Lua functions

    debug.upvaluejoin

    -io
    +io
    io.close
    io.flush
    io.input
    @@ -238,7 +237,7 @@

    Lua functions

     

    -math
    +math
    math.abs
    math.acos
    math.asin
    @@ -249,7 +248,9 @@

     

    math.exp
    math.floor
    math.fmod
    +math.frexp
    math.huge
    +math.ldexp
    math.log
    math.max
    math.maxinteger
    @@ -268,7 +269,7 @@

     

    math.ult

    -os
    +os
    os.clock
    os.date
    os.difftime
    @@ -282,7 +283,7 @@

     

    os.tmpname

    -package
    +package
    package.config
    package.cpath
    package.loaded
    @@ -293,7 +294,7 @@

     

    package.searchpath

    -string
    +string
    string.byte
    string.char
    string.dump
    @@ -313,8 +314,9 @@

     

    string.upper

    -table
    +table
    table.concat
    +table.create
    table.insert
    table.move
    table.pack
    @@ -323,7 +325,7 @@

     

    table.unpack

    -utf8
    +utf8
    utf8.char
    utf8.charpattern
    utf8.codepoint
    @@ -366,11 +368,11 @@

    metamethods

    environment
    variables

    LUA_CPATH
    -LUA_CPATH_5_4
    +LUA_CPATH_5_5
    LUA_INIT
    -LUA_INIT_5_4
    +LUA_INIT_5_5
    LUA_PATH
    -LUA_PATH_5_4
    +LUA_PATH_5_5
    @@ -399,6 +401,7 @@

    C API

    lua_checkstack
    lua_close
    lua_closeslot
    +lua_closethread
    lua_compare
    lua_concat
    lua_copy
    @@ -444,6 +447,7 @@

    C API

    lua_newthread
    lua_newuserdatauv
    lua_next
    +lua_numbertocstring
    lua_numbertointeger
    lua_pcall
    lua_pcallk
    @@ -451,6 +455,7 @@

    C API

    lua_pushboolean
    lua_pushcclosure
    lua_pushcfunction
    +lua_pushexternalstring
    lua_pushfstring
    lua_pushglobaltable
    lua_pushinteger
    @@ -474,7 +479,6 @@

    C API

    lua_register
    lua_remove
    lua_replace
    -lua_resetthread
    lua_resume
    lua_rotate
    lua_setallocf
    @@ -529,6 +533,7 @@

    auxiliary library

    luaL_addsize
    luaL_addstring
    luaL_addvalue
    +luaL_alloc
    luaL_argcheck
    luaL_argerror
    luaL_argexpected
    @@ -563,11 +568,13 @@

    auxiliary library

    luaL_loadfile
    luaL_loadfilex
    luaL_loadstring
    +luaL_makeseed
    luaL_newlib
    luaL_newlibtable
    luaL_newmetatable
    luaL_newstate
    luaL_openlibs
    +luaL_openselectedlibs
    luaL_opt
    luaL_optinteger
    luaL_optlstring
    @@ -590,18 +597,8 @@

    auxiliary library

    luaL_unref
    luaL_where
    -

    standard library

    -luaopen_base
    -luaopen_coroutine
    -luaopen_debug
    -luaopen_io
    -luaopen_math
    -luaopen_os
    -luaopen_package
    -luaopen_string
    -luaopen_table
    -luaopen_utf8
    +LUAL_BUFFERSIZE

    constants

    @@ -610,6 +607,22 @@

    constants

    LUA_ERRMEM
    LUA_ERRRUN
    LUA_ERRSYNTAX
    +LUA_GCCOLLECT
    +LUA_GCCOUNT
    +LUA_GCCOUNTB
    +LUA_GCGEN
    +LUA_GCINC
    +LUA_GCISRUNNING
    +LUA_GCPARAM
    +LUA_GCPMAJORMINOR
    +LUA_GCPMINORMAJOR
    +LUA_GCPMINORMUL
    +LUA_GCPPAUSE
    +LUA_GCPSTEPMUL
    +LUA_GCPSTEPSIZE
    +LUA_GCRESTART
    +LUA_GCSTEP
    +LUA_GCSTOP
    LUA_HOOKCALL
    LUA_HOOKCOUNT
    LUA_HOOKLINE
    @@ -624,6 +637,7 @@

    constants

    LUA_MININTEGER
    LUA_MINSTACK
    LUA_MULTRET
    +LUA_N2SBUFFSZ
    LUA_NOREF
    LUA_OK
    LUA_OPADD
    @@ -660,7 +674,30 @@

    constants

    LUA_TUSERDATA
    LUA_USE_APICHECK
    LUA_YIELD
    -LUAL_BUFFERSIZE
    + +

    standard library

    +

    +LUA_COLIBK
    +LUA_DBLIBK
    +LUA_GLIBK
    +LUA_IOLIBK
    +LUA_LOADLIBK
    +LUA_MATHLIBK
    +LUA_OSLIBK
    +LUA_STRLIBK
    +LUA_TABLIBK
    +LUA_UTF8LIBK
    + +

    +LUA_COLIBNAME
    +LUA_DBLIBNAME
    +LUA_IOLIBNAME
    +LUA_LOADLIBNAME
    +LUA_MATHLIBNAME
    +LUA_OSLIBNAME
    +LUA_STRLIBNAME
    +LUA_TABLIBNAME
    +LUA_UTF8LIBNAME
    @@ -668,10 +705,10 @@

    constants

    diff --git a/doc/logo.gif b/doc/logo.png similarity index 100% rename from doc/logo.gif rename to doc/logo.png diff --git a/doc/lua.1 b/doc/lua.1 index 3c9e000..bffc583 100644 --- a/doc/lua.1 +++ b/doc/lua.1 @@ -1,5 +1,5 @@ -.\" $Id: lua.man,v 1.14 2024/05/08 18:48:27 lhf Exp $ -.TH LUA 1 "$Date: 2024/05/08 18:48:27 $" +.\" $Id: lua.man,v 1.14 2024/03/18 06:57:32 lhf Exp $ +.TH LUA 1 "$Date: 2024/03/18 06:57:32 $" .SH NAME lua \- Lua interpreter .SH SYNOPSIS @@ -61,7 +61,7 @@ a syntax error is found. Before handling command line options and scripts, .B lua checks the contents of the environment variables -.B LUA_INIT_5_4 +.B LUA_INIT_5_5 and .BR LUA_INIT , in that order. @@ -72,7 +72,7 @@ then is executed. Otherwise, the contents are assumed to be a Lua statement and is executed. When -.B LUA_INIT_5_4 +.B LUA_INIT_5_5 is defined, .B LUA_INIT is ignored. @@ -119,14 +119,14 @@ When defined, the version-specific variants take priority and the version-neutral variants are ignored. .TP -.B LUA_INIT, LUA_INIT_5_4 +.B LUA_INIT, LUA_INIT_5_5 Code to be executed before command line options and scripts. .TP -.B LUA_PATH, LUA_PATH_5_4 +.B LUA_PATH, LUA_PATH_5_5 Initial value of package.path, the path used by require to search for Lua loaders. .TP -.B LUA_CPATH, LUA_CPATH_5_4 +.B LUA_CPATH, LUA_CPATH_5_5 Initial value of package.cpath, the path used by require to search for C loaders. .SH EXIT STATUS diff --git a/doc/manual.html b/doc/manual.html index 574c743..26d4de2 100644 --- a/doc/manual.html +++ b/doc/manual.html @@ -1,7 +1,7 @@ -Lua 5.4 Reference Manual +Lua 5.5 Reference Manual @@ -10,8 +10,8 @@

    -Lua -Lua 5.4 Reference Manual +Lua +Lua 5.5 Reference Manual

    @@ -19,7 +19,7 @@

    -Copyright © 2020–2024 Lua.org, PUC-Rio. +Copyright © 2020–2025 Lua.org, PUC-Rio. Freely available under the terms of the Lua license. @@ -166,7 +166,7 @@

    2.1 – Values and Types

    Unless stated otherwise, any overflow when manipulating integer values wrap around, -according to the usual rules of two-complement arithmetic. +according to the usual rules of two's complement arithmetic. (In other words, the actual result is the unique representable integer that is equal modulo 2n to the mathematical result, @@ -190,7 +190,8 @@

    2.1 – Values and Types

    including embedded zeros ('\0'). Lua is also encoding-agnostic; it makes no assumptions about the contents of a string. -The length of any string in Lua must fit in a Lua integer. +The length of any string in Lua must fit in a Lua integer, +and the string plus a small header must fit in size_t.

    @@ -295,12 +296,105 @@

    2.1 – Values and Types

    -

    2.2 – Environments and the Global Environment

    +

    2.2 – Scopes, Variables, and Environments

    + +

    +A variable name refers to a global or a local variable according +to the declaration that is in context at that point of the code. +(For the purposes of this discussion, +a function's formal parameter is equivalent to a local variable.) + + +

    +All chunks start with an implicit declaration global *, +which declares all free names as global variables; +this preambular declaration becomes void inside the scope of any other +global declaration, +as the following example illustrates: + +

    +     X = 1       -- Ok, global by default
    +     do
    +       global Y  -- voids the implicit initial declaration
    +       Y = 1     -- Ok, Y declared as global
    +       X = 1     -- ERROR, X not declared
    +     end
    +     X = 2       -- Ok, global by default again
    +

    +So, outside any global declaration, +Lua works as global-by-default. +Inside any global declaration, +Lua works without a default: +All variables must be declared. + + +

    +Lua is a lexically scoped language. +The scope of a variable declaration begins at the first statement after +the declaration and lasts until the last non-void statement +of the innermost block that includes the declaration. +(Void statements are labels and empty statements.) + + +

    +A declaration shadows any declaration for the same name that +is in context at the point of the declaration. Inside this +shadow, any outer declaration for that name is void. +See the next example: + +

    +     global print, x
    +     x = 10                -- global variable
    +     do                    -- new block
    +       local x = x         -- new 'x', with value 10
    +       print(x)            --> 10
    +       x = x+1
    +       do                  -- another block
    +         local x = x+1     -- another 'x'
    +         print(x)          --> 12
    +       end
    +       print(x)            --> 11
    +     end
    +     print(x)              --> 10  (the global one)
    +
    + +

    +Notice that, in a declaration like local x = x, +the new x being declared is not in scope yet, +and so the x on the right-hand side refers to the outside variable. + + +

    +Because of the lexical scoping rules, +local variables can be freely accessed by functions +defined inside their scope. +A local variable used by an inner function is called an upvalue +(or external local variable, or simply external variable) +inside the inner function. + + +

    +Notice that each execution of a local statement +defines new local variables. +Consider the following example: + +

    +     a = {}
    +     local x = 20
    +     for i = 1, 10 do
    +       local y = 0
    +       a[i] = function () y = y + 1; return x + y end
    +     end
    +

    +The loop creates ten closures +(that is, ten instances of the anonymous function). +Each of these closures uses a different y variable, +while all of them share the same x. +

    As we will discuss further in §3.2 and §3.3.3, -any reference to a free name -(that is, a name not bound to any declaration) var +any reference to a global variable var is syntactically translated to _ENV.var. Moreover, every chunk is compiled in the scope of an external local variable named _ENV (see §3.3.2), @@ -310,12 +404,14 @@

    2.2 – Environments and the Global Environment

    Despite the existence of this external _ENV variable and the translation of free names, -_ENV is a completely regular name. +_ENV is a regular name. In particular, you can define new variables and parameters with that name. -Each reference to a free name uses the _ENV that is -visible at that point in the program, -following the usual visibility rules of Lua (see §3.5). +(However, you should not define _ENV as a global variable, +otherwise _ENV.var would translate to +_ENV._ENV.var and so on, in an infinite loop.) +Each reference to a global variable name uses the _ENV that is +visible at that point in the program.

    @@ -335,8 +431,8 @@

    2.2 – Environments and the Global Environment

    the default value for its _ENV variable is the global environment (see load). Therefore, by default, -free names in Lua code refer to entries in the global environment -and, therefore, they are also called global variables. +global variables in Lua code refer to entries in the global environment +and, therefore, they act as conventional global variables. Moreover, all standard libraries are loaded in the global environment and some functions there operate on that environment. You can use load (or loadfile) @@ -392,7 +488,9 @@

    2.3 – Error Handling

    is propagated with information about the error. Lua itself only generates errors whose error object is a string, but programs can generate errors with -any value as the error object. +any value as the error object, +except nil. +(Lua will change a nil as error object to a string message.) It is up to the Lua program or its host to handle such error objects. For historical reasons, an error object is often called an error message, @@ -467,7 +565,7 @@

    2.4 – Metatables and Metamethods

    You can replace the metatable of tables using the setmetatable function. You cannot change the metatable of other types from Lua code, -except by using the debug library (§6.10). +except by using the debug library (§6.11).

    @@ -477,7 +575,7 @@

    2.4 – Metatables and Metamethods

    that is, there is one single metatable for all numbers, one for all strings, etc. By default, a value has no metatable, -but the string library sets a metatable for the string type (see §6.4). +but the string library sets a metatable for the string type (see §6.5).

    @@ -490,7 +588,7 @@

    2.4 – Metatables and Metamethods