├── .Rbuildignore ├── .github ├── .gitignore └── workflows │ └── R-CMD-check.yaml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── LICENSE.note ├── NAMESPACE ├── NEWS.md ├── R ├── lua.R ├── lua_func.R ├── lua_internal.R ├── lua_mode.R ├── lua_open.R ├── lua_parallel.R ├── lua_shell.R ├── luajr-package.R └── zzz.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── cleanup ├── cleanup.win ├── configure ├── configure.win ├── cran-comments.md ├── docs ├── 404.html ├── LICENSE-text.html ├── LICENSE.html ├── articles │ ├── index.html │ ├── luajr-module.html │ ├── luajr.html │ ├── luajr_files │ │ └── figure-html │ │ │ └── unnamed-chunk-14-1.png │ ├── objects.html │ └── packages.html ├── authors.html ├── deps │ ├── bootstrap-5.1.3 │ │ ├── bootstrap.bundle.min.js │ │ ├── bootstrap.bundle.min.js.map │ │ └── bootstrap.min.css │ ├── bootstrap-5.3.1 │ │ ├── bootstrap.bundle.min.js │ │ ├── bootstrap.bundle.min.js.map │ │ └── bootstrap.min.css │ ├── bootstrap-toc-1.0.1 │ │ └── bootstrap-toc.min.js │ ├── clipboard.js-2.0.11 │ │ └── clipboard.min.js │ ├── data-deps.txt │ ├── font-awesome-6.4.2 │ │ ├── css │ │ │ ├── all.css │ │ │ ├── all.min.css │ │ │ ├── v4-shims.css │ │ │ └── v4-shims.min.css │ │ └── webfonts │ │ │ ├── fa-brands-400.ttf │ │ │ ├── fa-brands-400.woff2 │ │ │ ├── fa-regular-400.ttf │ │ │ ├── fa-regular-400.woff2 │ │ │ ├── fa-solid-900.ttf │ │ │ ├── fa-solid-900.woff2 │ │ │ ├── fa-v4compatibility.ttf │ │ │ └── fa-v4compatibility.woff2 │ ├── font-awesome-6.5.2 │ │ ├── css │ │ │ ├── all.css │ │ │ ├── all.min.css │ │ │ ├── v4-shims.css │ │ │ └── v4-shims.min.css │ │ └── webfonts │ │ │ ├── fa-brands-400.ttf │ │ │ ├── fa-brands-400.woff2 │ │ │ ├── fa-regular-400.ttf │ │ │ ├── fa-regular-400.woff2 │ │ │ ├── fa-solid-900.ttf │ │ │ ├── fa-solid-900.woff2 │ │ │ ├── fa-v4compatibility.ttf │ │ │ └── fa-v4compatibility.woff2 │ ├── headroom-0.11.0 │ │ ├── headroom.min.js │ │ └── jQuery.headroom.min.js │ ├── jquery-3.6.0 │ │ ├── jquery-3.6.0.js │ │ ├── jquery-3.6.0.min.js │ │ └── jquery-3.6.0.min.map │ └── search-1.0.0 │ │ ├── autocomplete.jquery.min.js │ │ ├── fuse.min.js │ │ └── mark.min.js ├── index.html ├── katex-auto.js ├── lightswitch.js ├── link.svg ├── news │ └── index.html ├── pkgdown.js ├── pkgdown.yml ├── reference │ ├── Rplot001.png │ ├── figures │ │ └── README-pressure-1.png │ ├── index.html │ ├── lua.html │ ├── lua_func.html │ ├── lua_mode.html │ ├── lua_open.html │ ├── lua_parallel.html │ ├── lua_profile.html │ ├── lua_reset.html │ ├── lua_shell.html │ ├── luajr-package.html │ └── luajr.html ├── search.json └── sitemap.xml ├── inst ├── CITATION ├── include │ ├── luajr.h │ ├── luajr_const.h │ ├── luajr_funcdef.h │ └── luajr_funcs.h └── module │ ├── debugger.lua │ └── luajr.lua ├── local ├── benchmarks.R ├── build_vignettes.R ├── devnotes.txt ├── example.cpp ├── example_cpp.R ├── long_vector_test.R ├── luajrstdr.h ├── mymodule.lua ├── odetest.R ├── odetest.lua ├── old_debugger.lua ├── profile.R ├── scratch.R └── use_case.cpp ├── luajr.Rproj ├── man ├── figures │ └── README-pressure-1.png ├── lua.Rd ├── lua_func.Rd ├── lua_mode.Rd ├── lua_open.Rd ├── lua_parallel.Rd ├── lua_profile.Rd ├── lua_reset.Rd ├── lua_shell.Rd └── luajr-package.Rd ├── src ├── .gitignore ├── Makevars.in ├── lua_api.cpp ├── lua_internal.cpp ├── luajit │ ├── .gitattributes │ ├── .gitignore │ ├── COPYRIGHT │ ├── Makefile │ ├── README │ ├── dynasm │ │ ├── dasm_arm.h │ │ ├── dasm_arm.lua │ │ ├── dasm_arm64.h │ │ ├── dasm_arm64.lua │ │ ├── dasm_mips.h │ │ ├── dasm_mips.lua │ │ ├── dasm_mips64.lua │ │ ├── dasm_ppc.h │ │ ├── dasm_ppc.lua │ │ ├── dasm_proto.h │ │ ├── dasm_x64.lua │ │ ├── dasm_x86.h │ │ ├── dasm_x86.lua │ │ └── dynasm.lua │ ├── etc │ │ ├── luajit.1 │ │ └── luajit.pc │ └── src │ │ ├── .gitignore │ │ ├── Makefile │ │ ├── Makefile.dep │ │ ├── host │ │ ├── .gitignore │ │ ├── README │ │ ├── buildvm.c │ │ ├── buildvm.h │ │ ├── buildvm_asm.c │ │ ├── buildvm_fold.c │ │ ├── buildvm_lib.c │ │ ├── buildvm_libbc.h │ │ ├── buildvm_peobj.c │ │ ├── genlibbc.lua │ │ ├── genminilua.lua │ │ ├── genversion.lua │ │ └── minilua.c │ │ ├── jit │ │ ├── .gitignore │ │ ├── bc.lua │ │ ├── bcsave.lua │ │ ├── dis_arm.lua │ │ ├── dis_arm64.lua │ │ ├── dis_arm64be.lua │ │ ├── dis_mips.lua │ │ ├── dis_mips64.lua │ │ ├── dis_mips64el.lua │ │ ├── dis_mips64r6.lua │ │ ├── dis_mips64r6el.lua │ │ ├── dis_mipsel.lua │ │ ├── dis_ppc.lua │ │ ├── dis_x64.lua │ │ ├── dis_x86.lua │ │ ├── dump.lua │ │ ├── p.lua │ │ ├── v.lua │ │ └── zone.lua │ │ ├── lauxlib.h │ │ ├── lib_aux.c │ │ ├── lib_base.c │ │ ├── lib_bit.c │ │ ├── lib_buffer.c │ │ ├── lib_debug.c │ │ ├── lib_ffi.c │ │ ├── lib_init.c │ │ ├── lib_io.c │ │ ├── lib_jit.c │ │ ├── lib_math.c │ │ ├── lib_os.c │ │ ├── lib_package.c │ │ ├── lib_string.c │ │ ├── lib_table.c │ │ ├── lj_alloc.c │ │ ├── lj_alloc.h │ │ ├── lj_api.c │ │ ├── lj_arch.h │ │ ├── lj_asm.c │ │ ├── lj_asm.h │ │ ├── lj_asm_arm.h │ │ ├── lj_asm_arm64.h │ │ ├── lj_asm_mips.h │ │ ├── lj_asm_ppc.h │ │ ├── lj_asm_x86.h │ │ ├── lj_assert.c │ │ ├── lj_bc.c │ │ ├── lj_bc.h │ │ ├── lj_bcdump.h │ │ ├── lj_bcread.c │ │ ├── lj_bcwrite.c │ │ ├── lj_buf.c │ │ ├── lj_buf.h │ │ ├── lj_carith.c │ │ ├── lj_carith.h │ │ ├── lj_ccall.c │ │ ├── lj_ccall.h │ │ ├── lj_ccallback.c │ │ ├── lj_ccallback.h │ │ ├── lj_cconv.c │ │ ├── lj_cconv.h │ │ ├── lj_cdata.c │ │ ├── lj_cdata.h │ │ ├── lj_char.c │ │ ├── lj_char.h │ │ ├── lj_clib.c │ │ ├── lj_clib.h │ │ ├── lj_cparse.c │ │ ├── lj_cparse.h │ │ ├── lj_crecord.c │ │ ├── lj_crecord.h │ │ ├── lj_ctype.c │ │ ├── lj_ctype.h │ │ ├── lj_debug.c │ │ ├── lj_debug.h │ │ ├── lj_def.h │ │ ├── lj_dispatch.c │ │ ├── lj_dispatch.h │ │ ├── lj_emit_arm.h │ │ ├── lj_emit_arm64.h │ │ ├── lj_emit_mips.h │ │ ├── lj_emit_ppc.h │ │ ├── lj_emit_x86.h │ │ ├── lj_err.c │ │ ├── lj_err.h │ │ ├── lj_errmsg.h │ │ ├── lj_ff.h │ │ ├── lj_ffrecord.c │ │ ├── lj_ffrecord.h │ │ ├── lj_frame.h │ │ ├── lj_func.c │ │ ├── lj_func.h │ │ ├── lj_gc.c │ │ ├── lj_gc.h │ │ ├── lj_gdbjit.c │ │ ├── lj_gdbjit.h │ │ ├── lj_ir.c │ │ ├── lj_ir.h │ │ ├── lj_ircall.h │ │ ├── lj_iropt.h │ │ ├── lj_jit.h │ │ ├── lj_lex.c │ │ ├── lj_lex.h │ │ ├── lj_lib.c │ │ ├── lj_lib.h │ │ ├── lj_load.c │ │ ├── lj_mcode.c │ │ ├── lj_mcode.h │ │ ├── lj_meta.c │ │ ├── lj_meta.h │ │ ├── lj_obj.c │ │ ├── lj_obj.h │ │ ├── lj_opt_dce.c │ │ ├── lj_opt_fold.c │ │ ├── lj_opt_loop.c │ │ ├── lj_opt_mem.c │ │ ├── lj_opt_narrow.c │ │ ├── lj_opt_sink.c │ │ ├── lj_opt_split.c │ │ ├── lj_parse.c │ │ ├── lj_parse.h │ │ ├── lj_prng.c │ │ ├── lj_prng.h │ │ ├── lj_profile.c │ │ ├── lj_profile.h │ │ ├── lj_record.c │ │ ├── lj_record.h │ │ ├── lj_serialize.c │ │ ├── lj_serialize.h │ │ ├── lj_snap.c │ │ ├── lj_snap.h │ │ ├── lj_state.c │ │ ├── lj_state.h │ │ ├── lj_str.c │ │ ├── lj_str.h │ │ ├── lj_strfmt.c │ │ ├── lj_strfmt.h │ │ ├── lj_strfmt_num.c │ │ ├── lj_strscan.c │ │ ├── lj_strscan.h │ │ ├── lj_tab.c │ │ ├── lj_tab.h │ │ ├── lj_target.h │ │ ├── lj_target_arm.h │ │ ├── lj_target_arm64.h │ │ ├── lj_target_mips.h │ │ ├── lj_target_ppc.h │ │ ├── lj_target_x86.h │ │ ├── lj_trace.c │ │ ├── lj_trace.h │ │ ├── lj_traceerr.h │ │ ├── lj_udata.c │ │ ├── lj_udata.h │ │ ├── lj_vm.h │ │ ├── lj_vmevent.c │ │ ├── lj_vmevent.h │ │ ├── lj_vmmath.c │ │ ├── ljamalg.c │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── luaconf.h │ │ ├── luajit.c │ │ ├── luajit_rolling.h │ │ ├── luajrstdr.h │ │ ├── lualib.h │ │ ├── msvcbuild.bat │ │ ├── nxbuild.bat │ │ ├── ps4build.bat │ │ ├── ps5build.bat │ │ ├── psvitabuild.bat │ │ ├── vm_arm.dasc │ │ ├── vm_arm64.dasc │ │ ├── vm_mips.dasc │ │ ├── vm_mips64.dasc │ │ ├── vm_ppc.dasc │ │ ├── vm_x64.dasc │ │ ├── vm_x86.dasc │ │ ├── xb1build.bat │ │ └── xedkbuild.bat ├── luajit_build.h ├── luajrstdr.cpp ├── parallel.cpp ├── push_to.cpp ├── registry_entry.cpp ├── registry_entry.h ├── run_func.cpp ├── setup.cpp ├── shared.h ├── state.cpp └── tools.cpp ├── tests ├── testthat.R └── testthat │ ├── files │ └── root2.lua │ ├── test-attributes.R │ ├── test-basic_v.R │ ├── test-character_v.R │ ├── test-list.R │ ├── test-lua.R │ ├── test-lua_func.R │ ├── test-parallel.R │ ├── test-state.R │ └── test-types.R ├── tools └── luajit_relver.txt └── vignettes ├── .gitignore ├── luajr.Rmd ├── objects.Rmd └── packages.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.github$ 3 | ^\.Rproj\.user$ 4 | ^_pkgdown\.yml$ 5 | ^cran-comments\.md$ 6 | ^doc$ 7 | ^docs$ 8 | ^LICENSE\.md$ 9 | ^local$ 10 | ^Meta$ 11 | ^pkgdown$ 12 | ^README\.Rmd$ 13 | ^update_lj.sh$ 14 | ^update_dbg.sh$ 15 | ^CRAN-SUBMISSION$ 16 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | 34 | - uses: r-lib/actions/setup-pandoc@v2 35 | 36 | - uses: r-lib/actions/setup-r@v2 37 | with: 38 | r-version: ${{ matrix.config.r }} 39 | http-user-agent: ${{ matrix.config.http-user-agent }} 40 | use-public-rspm: true 41 | 42 | - uses: r-lib/actions/setup-r-dependencies@v2 43 | with: 44 | extra-packages: any::rcmdcheck 45 | needs: check 46 | 47 | - uses: r-lib/actions/check-r-package@v2 48 | with: 49 | upload-snapshots: true 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | inst/doc 6 | .Rdata 7 | .httr-oauth 8 | .DS_Store 9 | .quarto 10 | /doc/ 11 | /Meta/ 12 | 13 | # Generated files 14 | src/Makevars 15 | src/RcppExports.cpp 16 | R/RcppExports.R 17 | inst/include/luajr_conf.h 18 | inst/include/luajr_lua.h 19 | inst/include/luajr_lauxlib.h 20 | inst/include/luajr_lualib.h 21 | inst/include/luajr_luajit.h 22 | src/luajit/.relver 23 | CRAN-SUBMISSION 24 | 25 | update_lj.sh 26 | update_dbg.sh 27 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: luajr 2 | Type: Package 3 | Title: 'LuaJIT' Scripting 4 | Version: 0.1.9.9000 5 | Authors@R: c( 6 | person("Mike", "Pall", role = c("aut", "cph"), 7 | comment = "Author of the embedded LuaJIT compiler"), 8 | person("Lua.org, PUC-Rio", role = "cph", 9 | comment = "Copyright holders over portions of Lua source code included in LuaJIT"), 10 | person("Nicholas", "Davies", email = "nicholas.davies@lshtm.ac.uk", 11 | role = c("cre", "ctb", "cph"), 12 | comment = c("Author of the R package wrapper", ORCID = "0000-0002-1740-1412")), 13 | person("Scott Lembcke, Howling Moon Software", role = c("ctb", "cph"), 14 | comment = "Authors of the embedded debugger.lua debugger")) 15 | Description: An interface to 'LuaJIT' , a just-in-time 16 | compiler for the 'Lua' scripting language . Allows 17 | users to run 'Lua' code from 'R'. 18 | URL: https://github.com/nicholasdavies/luajr, https://nicholasdavies.github.io/luajr/ 19 | BugReports: https://github.com/nicholasdavies/luajr/issues 20 | License: MIT + file LICENSE 21 | Encoding: UTF-8 22 | SystemRequirements: 23 | GNU make 24 | Suggests: 25 | Rcpp, 26 | crayon, 27 | knitr, 28 | rmarkdown, 29 | testthat (>= 3.0.0) 30 | LazyData: true 31 | Roxygen: list(markdown = TRUE) 32 | RoxygenNote: 7.3.2 33 | VignetteBuilder: knitr 34 | Config/testthat/edition: 3 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2024 2 | COPYRIGHT HOLDER: Mike Pall, Lua.org, PUC-Rio, Nicholas Davies, Scott Lembcke, Howling Moon Software 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | *The following license statement applies to the luajr package as a whole, which 2 | is released under the MIT license. The luajr package includes the LuaJIT 3 | compiler by Mike Pall, which is released under the MIT license, and which 4 | itself includes components released under MIT or compatible licenses. The luajr 5 | package also embeds the debugger.lua Lua debugger by Scott Lembcke and Howling 6 | Moon Software, which is released under the MIT license. Copyright and license 7 | statements for LuaJIT and debugger.lua can be found in LICENSE.note.* 8 | 9 | # MIT License 10 | 11 | Copyright (c) 2024 Mike Pall, Lua.org, PUC-Rio, Nicholas Davies, Scott Lembcke, 12 | Howling Moon Software 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy 15 | of this software and associated documentation files (the "Software"), to deal 16 | in the Software without restriction, including without limitation the rights 17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | copies of the Software, and to permit persons to whom the Software is 19 | furnished to do so, subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in all 22 | copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 | SOFTWARE. 31 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(lua) 4 | export(lua_func) 5 | export(lua_mode) 6 | export(lua_open) 7 | export(lua_parallel) 8 | export(lua_profile) 9 | export(lua_reset) 10 | export(lua_shell) 11 | useDynLib(luajr, .registration = TRUE) 12 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # luajr (development version) 2 | 3 | - lua_func() can now accept an external pointer to a Lua function. This 4 | simplifies the process of adding Lua code to your R package, which is now 5 | explained in a new vignette. This addresses issue 6 | [#4](https://github.com/nicholasdavies/luajr/issues/4). Thanks to 7 | @al-obrien for asking about this! 8 | 9 | # luajr 0.1.9 10 | 11 | - Added debugging and profiling for Lua code. The debugger is Scott Lembcke's 12 | debugger.lua, and the profiler is LuaJIT's built-in sampling profiler. Also 13 | added the option of turning off JIT compilation. This is all accessed 14 | through a new function, lua_mode(). 15 | 16 | - Added further code to ensure that LuaJIT never calls exit() directly and 17 | never tries to read from or write to standard input or output streams, 18 | instead redirecting this to the R console. 19 | 20 | # luajr 0.1.8 21 | 22 | - The Lua "io" library is now capable of getting input from the R console 23 | (e.g. with io.read()) -- previously, trying this would cause R to hang 24 | (at least from RStudio). 25 | 26 | - The Lua os.exit() function now ends Lua execution without crashing RStudio. 27 | 28 | - lua_shell() now stores commands in the R console history. 29 | 30 | - The luajr build process now skips making libluajit.so and the luajit 31 | executable, as these are not needed for luajr. 32 | 33 | - Corrected an oversight in the documentation for lua_shell(); this fixes 34 | issue [#3](https://github.com/nicholasdavies/luajr/issues/3). Thanks to 35 | @SugarRayLua for bringing my attention to this! 36 | 37 | - Addressed some further issues turned up by CRAN checks. 38 | 39 | # luajr 0.1.7 40 | 41 | - Added support for passing the R type "raw" to Lua, as a string potentially 42 | with embedded nulls, and for returning strings with embedded nulls from 43 | Lua, which become "raw"s in R. 44 | 45 | - Replaced calls to Rf_allocVector3 with calls to Rf_allocVector, as the 46 | former is apparently not part of the API allowed in CRAN packages, as 47 | requested by CRAN. 48 | 49 | - @TimTaylor improved the R version of the "logistic map" example and 50 | benchmark in the main luajr vignette, so that now the R version is only 51 | 10x slower than the Lua version, not 2,500x slower. This fixes issue 52 | [#2](https://github.com/nicholasdavies/luajr/issues/2). Thanks Tim! 53 | 54 | - Removed a compiler flag, `-Wformat`, that was causing errors with some 55 | standard R environments, most notably the `rocker/r-base` Docker 56 | environment. This fixes issue 57 | [#1](https://github.com/nicholasdavies/luajr/issues/1). Thanks @jonocarroll 58 | for reporting and helping to fix luajr's first official bug! :-) 59 | 60 | # luajr 0.1.6 61 | 62 | - This version makes further changes to DESCRIPTION and LICENSE requested 63 | by CRAN. 64 | 65 | # luajr 0.1.5 66 | 67 | - This version updates the package DESCRIPTION and CITATION to better reflect 68 | Mike Pall's role as author of the embedded LuaJIT compiler. 69 | 70 | # luajr 0.1.4 71 | 72 | - Added luajr.NULL, to allow working with NULL in Lua. 73 | - Fixed some problems for CRAN. 74 | 75 | # luajr 0.1.3 76 | 77 | - Added a vignette describing the `luajr` Lua module. 78 | - Added `lua_parallel()` for basic multithreading. 79 | - Fixed compilation warnings about enums on some platforms. 80 | 81 | # luajr 0.1.2 82 | 83 | - luajr is now on [CRAN](https://CRAN.R-project.org/package=luajr)! 84 | -------------------------------------------------------------------------------- /R/lua.R: -------------------------------------------------------------------------------- 1 | #' Run Lua code 2 | #' 3 | #' Runs the specified Lua code. 4 | #' 5 | #' @param code Lua code block to run. 6 | #' @param filename If non-`NULL`, name of file to run. 7 | #' @param L [Lua state][lua_open] in which to run the code. `NULL` (default) 8 | #' uses the default Lua state for \pkg{luajr}. 9 | #' @return Lua value(s) returned by the code block converted to R object(s). 10 | #' Only a subset of all Lua types can be converted to R objects at present. 11 | #' If multiple values are returned, these are packaged in a `list`. 12 | #' @examples 13 | #' twelve <- lua("return 3*4") 14 | #' print(twelve) 15 | #' @export 16 | lua = function(code, filename = NULL, L = NULL) 17 | { 18 | if (is.null(filename)) { 19 | ret = .Call(`_luajr_run_code`, code, L) 20 | } else { 21 | ret = .Call(`_luajr_run_file`, filename, L) 22 | } 23 | 24 | if (is.null(ret)) invisible() else ret 25 | } 26 | -------------------------------------------------------------------------------- /R/lua_internal.R: -------------------------------------------------------------------------------- 1 | # Access to Lua API 2 | 3 | lua_gettop = function(L = NULL) .Call(`_luajr_lua_gettop`, L) 4 | -------------------------------------------------------------------------------- /R/lua_open.R: -------------------------------------------------------------------------------- 1 | #' Create a new Lua state 2 | #' 3 | #' Creates a new, empty Lua state and returns an external pointer wrapping that 4 | #' state. 5 | #' 6 | #' All Lua code is executed within a given Lua state. A Lua state is similar to 7 | #' the global environment in R, in that it is where all variables and functions 8 | #' are defined. \pkg{luajr} automatically maintains a "default" Lua state, so 9 | #' most users of \pkg{luajr} will not need to use [lua_open()]. 10 | #' 11 | #' However, if for whatever reason you want to maintain multiple different Lua 12 | #' states at a time, each with their own independent global variables and 13 | #' functions, [lua_open()] can be used to create a new Lua state which can then 14 | #' be passed to [lua()], [lua_func()] and [lua_shell()] via the `L` parameter. 15 | #' These functions will then operate within that Lua state instead of the 16 | #' default one. The default Lua state can be specified explicitly with 17 | #' `L = NULL`. 18 | #' 19 | #' Note that there is currently no way (provided by \pkg{luajr}) of saving a 20 | #' Lua state to disk so that the state can be restarted later. Also, there is 21 | #' no `lua_close` in \pkg{luajr} because Lua states are closed automatically 22 | #' when they are garbage collected in R. 23 | #' 24 | #' @return External pointer wrapping the newly created Lua state. 25 | #' @examples 26 | #' L1 <- lua_open() 27 | #' lua("a = 2") 28 | #' lua("a = 4", L = L1) 29 | #' lua("print(a)") # 2 30 | #' lua("print(a)", L = L1) # 4 31 | #' @export 32 | lua_open = function() { 33 | .Call(`_luajr_open`) 34 | } 35 | 36 | #' Reset the default Lua state 37 | #' 38 | #' Clears out all variables from the default Lua state, freeing up the 39 | #' associated memory. 40 | #' 41 | #' This resets the default [Lua state][lua_open] only. To reset a non-default 42 | #' Lua state `L` returned by [lua_open()], just do `L <- lua_open()` again. The 43 | #' memory previously used will be cleaned up at the next garbage collection. 44 | #' 45 | #' @return None. 46 | #' @examples 47 | #' lua("a = 2") 48 | #' lua_reset() 49 | #' lua("print(a)") # nil 50 | #' @export 51 | lua_reset = function() { 52 | invisible(.Call(`_luajr_reset`)) 53 | } 54 | -------------------------------------------------------------------------------- /R/lua_parallel.R: -------------------------------------------------------------------------------- 1 | #' Run Lua code in parallel 2 | #' 3 | #' Runs a Lua function multiple times, with function runs divided among 4 | #' multiple threads. 5 | #' 6 | #' This function is experimental. Its interface and behaviour are likely to 7 | #' change in subsequent versions of luajr. 8 | #' 9 | #' [lua_parallel()] works as follows. A number `threads` of new Lua states is 10 | #' created with the standard Lua libraries and the `luajr` module opened in 11 | #' each (i.e. as though the states were created using [lua_open()]). Then, a 12 | #' thread is launched for each state. Within each thread, the code in `pre` is 13 | #' run in the corresponding Lua state. Then, `func(i)` is called for each `i` 14 | #' in `1:n`, with the calls spread across the states. Finally, the Lua states 15 | #' are closed and the results are returned in a list. The list elements are 16 | #' returned in the correct order, i.e. the ordering of the returned list does 17 | #' not depend on the actual execution order of each call to `func`. 18 | #' 19 | #' Instead of an integer, `threads` can be a list of Lua states, e.g. `NULL` 20 | #' for the default Lua state or a state returned by [lua_open()]. This saves 21 | #' the time needed to open the new states, which takes a few milliseconds. 22 | #' 23 | #' @section Safety and performance: 24 | #' 25 | #' Note that `func` has to be thread-safe. All pure Lua code and built-in Lua 26 | #' library functions are thread-safe, except for certain functions in the 27 | #' built-in **os** and **io** libraries (search for "thread safe" in the 28 | #' [Lua 5.2 reference manual](https://www.lua.org/manual/5.2/manual.html)). 29 | #' 30 | #' Additionally, use of luajr reference types is **not** thread-safe because 31 | #' these use R to allocate and manage memory, and R is not thread-safe. This 32 | #' means that you cannot safely use `luajr.logical_r`, `luajr.integer_r`, 33 | #' `luajr.numeric_r`, `luajr.character_r`, or other reference types within 34 | #' `func`. `luajr.list` and `luajr.dataframe` are fine, provided the list 35 | #' entries / dataframe columns are value types. 36 | #' 37 | #' There is overhead associated with creating new Lua states and with gathering 38 | #' all the function results in an R list. It is advisable to check whether 39 | #' running your Lua code in parallel actually gives a substantial speed 40 | #' increase. 41 | #' 42 | #' @param func Lua expression evaluating to a function. 43 | #' @param n Number of function executions. 44 | #' @param threads Number of threads to create, or a list of existing Lua states 45 | #' (e.g. as created by [lua_open()]), all different, one for each thread. 46 | #' @param pre Lua code block to run once for each thread at creation. 47 | #' @return List of `n` values returned from the Lua function `func`. 48 | #' @examples 49 | #' lua_parallel("function(i) return i end", n = 4, threads = 2) 50 | #' @export 51 | lua_parallel = function(func, n, threads, pre = NA_character_) 52 | { 53 | if (is.double(threads)) threads = as.integer(threads); 54 | .Call(`_luajr_run_parallel`, func, as.integer(n), threads, pre) 55 | } 56 | -------------------------------------------------------------------------------- /R/lua_shell.R: -------------------------------------------------------------------------------- 1 | #' Run an interactive Lua shell 2 | #' 3 | #' When in interactive mode, provides a basic read-eval-print loop with LuaJIT. 4 | #' 5 | #' Enter an empty line to return to R. 6 | #' 7 | #' As a convenience, lines starting with an equals sign have the `"="` replaced 8 | #' with `"return "`, so that e.g. entering `=x` will show the value of `x` as 9 | #' returned to R. 10 | #' 11 | #' @inheritParams lua 12 | #' 13 | #' @return None. 14 | #' 15 | #' @export 16 | lua_shell = function(L = NULL) 17 | { 18 | # Short-circuit if not in interactive mode 19 | if (!interactive()) 20 | return (invisible()) 21 | 22 | # See if crayon package is available 23 | crayon_available = requireNamespace("crayon", quietly = TRUE); 24 | 25 | # Loop until blank line received 26 | line = "." 27 | prev_line = "" 28 | while (line != "") 29 | { 30 | # Read a line 31 | if (prev_line == "") { 32 | line = .Call(`_luajr_readline`, "lua > ") 33 | } else { 34 | # If we're building up multi-line input, show a special prompt 35 | line = .Call(`_luajr_readline`, " + ") 36 | if (line == "") { 37 | # Exit multi-line if nothing entered 38 | line = "." 39 | prev_line = "" 40 | next 41 | } else { 42 | # Otherwise build up input 43 | line = paste(prev_line, line) 44 | } 45 | } 46 | 47 | # If line starts with =, replace = with return 48 | if (substr(line, 1, 1) == "=") { 49 | xline = paste("return", substr(line, 2, nchar(line))) 50 | } else { 51 | xline = line 52 | } 53 | 54 | # Try to execute the line, capturing any error 55 | res = tryCatch(lua(xline, L = L), error = function(e) e) 56 | 57 | # If there is an error: 58 | if ("error" %in% class(res)) { 59 | if (grepl("''$", res$message)) { 60 | # Unexpected end of input: try to gather more input 61 | prev_line = line 62 | next 63 | } else if (res$message == "Quit debugger.") { 64 | # Quit debugger: continue 65 | next 66 | } else if (crayon_available) { 67 | # Other error: show error message in red if possible 68 | cat(crayon::red(res$message), "\n") 69 | } else { 70 | # Or just in normal colour if not possible 71 | cat(res$message, "\n") 72 | } 73 | 74 | # If not a syntax error: quit lua_shell() 75 | if (!grepl("^\\[.*\\]:", res$message)) { 76 | break 77 | } 78 | } else if (!is.null(res)) { 79 | # If line has returned a value, print it 80 | print(res) 81 | } 82 | 83 | prev_line = "" 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /R/luajr-package.R: -------------------------------------------------------------------------------- 1 | ## usethis namespace: start 2 | #' @useDynLib luajr, .registration = TRUE 3 | ## usethis namespace: end 4 | NULL 5 | 6 | # To instruct inline/Rcpp packages how to depend on luajr 7 | inlineCxxPlugin = function(...) 8 | { 9 | Rcpp::Rcpp.plugin.maker( 10 | include.after = "#include \n#include ", 11 | package = "luajr")(...) 12 | } 13 | 14 | #' luajr: LuaJIT Scripting 15 | #' 16 | #' 'luajr' provides an interface to [LuaJIT](https://luajit.org), a 17 | #' just-in-time compiler for the [Lua scripting language](https://www.lua.org). 18 | #' It allows users to run Lua code from R. 19 | #' 20 | #' @section The R API: 21 | #' * [lua()]: run Lua code 22 | #' * [lua_func()]: make a Lua function callable from R 23 | #' * [lua_shell()]: run an interactive Lua shell 24 | #' * [lua_open()]: create a new Lua state 25 | #' * [lua_reset()]: reset the default Lua state 26 | #' * [lua_parallel()]: run Lua code in parallel 27 | #' 28 | #' @section Further reading: 29 | #' For an introduction to 'luajr', see `vignette("luajr")` 30 | "_PACKAGE" 31 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onLoad = function(libname, pkgname) 2 | { 3 | # Provide path to package dylib for LuaJIT FFI 4 | .Call(`_luajr_locate_dylib`, getLoadedDLLs()[["luajr"]][["path"]]) 5 | # Provide path to luajr module 6 | .Call(`_luajr_locate_module`, system.file("module", "luajr.lua", package = "luajr")) 7 | # Provide path to debugger.lua 8 | .Call(`_luajr_locate_debugger`, system.file("module", "debugger.lua", package = "luajr")) 9 | invisible() 10 | } 11 | 12 | .onUnload = function(libname, pkgname) 13 | { 14 | # Close the shared Lua state 15 | lua_reset() 16 | invisible() 17 | } 18 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | ```{r, include = FALSE} 6 | knitr::opts_chunk$set( 7 | collapse = TRUE, 8 | comment = "#>", 9 | fig.path = "man/figures/README-", 10 | out.width = "100%" 11 | ) 12 | ``` 13 | 14 | # luajr: LuaJIT Scripting 15 | 16 | 17 | [![R-CMD-check](https://github.com/nicholasdavies/luajr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/nicholasdavies/luajr/actions/workflows/R-CMD-check.yaml) 18 | 19 | 20 | 21 | 22 | luajr provides an interface to [LuaJIT](https://luajit.org), a just-in-time compiler for the [Lua scripting language](https://www.lua.org). It allows users to run Lua code from R. 23 | 24 | One of the advantages of using luajr is that LuaJIT runs Lua code with very fast compilation times, fast execution times, and no need for an external toolchain, as the LuaJIT compiler is "built in" to the luajr package. This contrasts with e.g. [Rcpp](https://www.rcpp.org/), which results in compiled code with very fast execution times but slow compilation times and the need to invoke an external C++ compiler toolchain like `gcc` or `clang`. 25 | 26 | ## Installation 27 | 28 | You can install the released version of luajr from [CRAN](https://CRAN.R-project.org/package=luajr) with: 29 | 30 | ```{r, eval = FALSE} 31 | install.packages("luajr") 32 | ``` 33 | 34 | You can install the development version of luajr from [GitHub](https://github.com/nicholasdavies/luajr) with: 35 | 36 | ```{r, eval = FALSE} 37 | # install.packages("devtools") 38 | devtools::install_github("nicholasdavies/luajr") 39 | ``` 40 | 41 | ## Under development 42 | luajr is under **early development**. As such, the interface and behaviour of the package is subject to change. 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # luajr: LuaJIT Scripting 3 | 4 | 5 | 6 | [![R-CMD-check](https://github.com/nicholasdavies/luajr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/nicholasdavies/luajr/actions/workflows/R-CMD-check.yaml) 7 | 8 | 9 | 10 | 11 | luajr provides an interface to [LuaJIT](https://luajit.org), a 12 | just-in-time compiler for the [Lua scripting 13 | language](https://www.lua.org). It allows users to run Lua code from R. 14 | 15 | One of the advantages of using luajr is that LuaJIT runs Lua code with 16 | very fast compilation times, fast execution times, and no need for an 17 | external toolchain, as the LuaJIT compiler is “built in” to the luajr 18 | package. This contrasts with e.g. [Rcpp](https://www.rcpp.org/), which 19 | results in compiled code with very fast execution times but slow 20 | compilation times and the need to invoke an external C++ compiler 21 | toolchain like `gcc` or `clang`. 22 | 23 | ## Installation 24 | 25 | You can install the released version of luajr from 26 | [CRAN](https://CRAN.R-project.org/package=luajr) with: 27 | 28 | ``` r 29 | install.packages("luajr") 30 | ``` 31 | 32 | You can install the development version of luajr from 33 | [GitHub](https://github.com/nicholasdavies/luajr) with: 34 | 35 | ``` r 36 | # install.packages("devtools") 37 | devtools::install_github("nicholasdavies/luajr") 38 | ``` 39 | 40 | ## Under development 41 | 42 | luajr is under **early development**. As such, the interface and 43 | behaviour of the package is subject to change. 44 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: https://nicholasdavies.github.io/luajr/ 2 | template: 3 | bootstrap: 5 4 | reference: 5 | - title: Package overview 6 | contents: luajr 7 | - title: luajr basics 8 | contents: 9 | - lua 10 | - lua_func 11 | - lua_shell 12 | - title: Lua states 13 | contents: 14 | - lua_open 15 | - lua_reset 16 | - title: Parallel processing 17 | contents: 18 | - lua_parallel 19 | - title: Tools and options 20 | contents: 21 | - lua_mode 22 | - lua_profile 23 | authors: 24 | footer: 25 | roles: [cre] 26 | text: "Maintained by" 27 | sidebar: 28 | roles: [cre] 29 | before: > 30 | The luajr package bundles the [LuaJIT](https://luajit.org) compiler by Mike 31 | Pall (with portions by the authors of [Lua](https://www.lua.org)) and an R 32 | package wrapper by Nick Davies. It also bundles the debugger.lua Lua 33 | debugger by Scott Lembcke and Howling Moon Software. Therefore, in order of 34 | contribution, the authors of luajr are: 35 | after: > 36 | Please give credit to Mike Pall and the Lua authors, and to Scott Lembcke 37 | and Howling Moon Software for the debugger, but direct all your luajr 38 | questions to the package maintainer! 39 | -------------------------------------------------------------------------------- /cleanup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # Clean up Makevars 4 | rm -f src/Makevars 5 | 6 | # Clean up generated .relver 7 | rm -f src/luajit/.relver 8 | 9 | # Clean up luajit. Default value for $MAKE allows run from command line. 10 | if [ `uname` = Darwin ]; then 11 | (export MACOSX_DEPLOYMENT_TARGET=10.8; cd src/luajit && ${MAKE:-make} clean) 12 | else 13 | cd src/luajit && ${MAKE:-make} clean 14 | fi 15 | -------------------------------------------------------------------------------- /cleanup.win: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # Clean up Makevars.win 4 | rm -f src/Makevars.win 5 | 6 | # Clean up generated .relver 7 | rm -f src/luajit/.relver 8 | 9 | # Clean up luajit. Default value for $MAKE is for Github Actions R CMD check on Windows. 10 | cd src/luajit && ${MAKE:-make} clean 11 | -------------------------------------------------------------------------------- /configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ### Write to Makevars 4 | # Supply make options; retain user supplied PKG_ env variables. 5 | if [ `uname` = Darwin ]; then 6 | sed -e 's|@make@|MACOSX_DEPLOYMENT_TARGET=10.8 $(MAKE)|' \ 7 | -e "s|@PKG_CXXFLAGS@|$PKG_CXXFLAGS|" -e "s|@PKG_CPPFLAGS@|$PKG_CPPFLAGS|" \ 8 | -e "s|@PKG_LIBS@|$PKG_LIBS|" src/Makevars.in > src/Makevars 9 | else 10 | sed -e 's|@make@|$(MAKE)|' \ 11 | -e "s|@PKG_CXXFLAGS@|$PKG_CXXFLAGS|" -e "s|@PKG_CPPFLAGS@|$PKG_CPPFLAGS|" \ 12 | -e "s|@PKG_LIBS@|$PKG_LIBS|" src/Makevars.in > src/Makevars 13 | fi 14 | 15 | ### Write to .relver 16 | cp tools/luajit_relver.txt src/luajit/.relver 17 | 18 | exit 0 19 | -------------------------------------------------------------------------------- /configure.win: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ### Write to Makevars.win 4 | # Supply make options; retain user supplied PKG_ env variables. 5 | sed -e 's|@make@|$(MAKE)|' \ 6 | -e "s|@PKG_CXXFLAGS@|$PKG_CXXFLAGS|" -e "s|@PKG_CPPFLAGS@|$PKG_CPPFLAGS|" \ 7 | -e "s|@PKG_LIBS@|$PKG_LIBS|" src/Makevars.in > src/Makevars.win 8 | 9 | ### Write to .relver 10 | cp tools/luajit_relver.txt src/luajit/.relver 11 | 12 | exit 0 13 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | This submission addresses issues raised by CRAN checks on the compiled code. 2 | Previously, the bundled LuaJIT library had some calls to exit() and some calls 3 | to standard C I/O on stdin, stdout, or stderr. In this submission I have 4 | redirected all of these calls. Calls to exit() now redirect to Rf_error() and 5 | calls to standard I/O now redirect to R_ReadConsole() and R_WriteConsoleEx() 6 | as appropriate. 7 | 8 | ## R CMD check results 9 | 10 | 0 errors \| 0 warnings \| 1 note 11 | 12 | - NOTE: GNU make is a SystemRequirements. 13 | 14 | This NOTE is carried over from previous submissions of luajr to CRAN. 15 | 16 | Justification: This package wraps the LuaJIT compiler, which requires GNU 17 | make to build. The build system for LuaJIT is very complex, so it would be 18 | prohibitively difficult to get around this requirement. 19 | -------------------------------------------------------------------------------- /docs/LICENSE-text.html: -------------------------------------------------------------------------------- 1 | 2 | License • luajr 3 | Skip to contents 4 | 5 | 6 |
34 |
35 |
40 | 41 |
YEAR: 2024
42 | COPYRIGHT HOLDER: Mike Pall, Lua.org, PUC-Rio, Nicholas Davies, Scott Lembcke, Howling Moon Software
43 | 
44 | 45 |
46 | 47 | 48 |
51 | 52 | 55 | 56 |
57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /docs/articles/index.html: -------------------------------------------------------------------------------- 1 | 2 | Articles • luajr 3 | Skip to contents 4 | 5 | 6 |
34 |
35 |
39 | 40 |
41 |

All vignettes

42 |
43 | 44 |
Introduction to `luajr`
45 |
46 |
R objects in Lua code
47 |
48 |
R packages with Lua code
49 |
50 |
51 |
52 | 53 | 54 |
57 | 58 | 61 | 62 |
63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /docs/articles/luajr_files/figure-html/unnamed-chunk-14-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/articles/luajr_files/figure-html/unnamed-chunk-14-1.png -------------------------------------------------------------------------------- /docs/deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Bootstrap Table of Contents v1.0.1 (http://afeld.github.io/bootstrap-toc/) 3 | * Copyright 2015 Aidan Feldman 4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ 5 | !function(a){"use strict";window.Toc={helpers:{findOrFilter:function(e,t){var n=e.find(t);return e.filter(t).add(n).filter(":not([data-toc-skip])")},generateUniqueIdBase:function(e){return a(e).text().trim().replace(/\'/gi,"").replace(/[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v]/g,"-").replace(/-{2,}/g,"-").substring(0,64).replace(/^-+|-+$/gm,"").toLowerCase()||e.tagName.toLowerCase()},generateUniqueId:function(e){for(var t=this.generateUniqueIdBase(e),n=0;;n++){var r=t;if(0')},createChildNavList:function(e){var t=this.createNavList();return e.append(t),t},generateNavEl:function(e,t){var n=a('');n.attr("href","#"+e),n.text(t);var r=a("
  • ");return r.append(n),r},generateNavItem:function(e){var t=this.generateAnchor(e),n=a(e),r=n.data("toc-text")||n.text();return this.generateNavEl(t,r)},getTopLevel:function(e){for(var t=1;t<=6;t++){if(1 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-brands-400.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-brands-400.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-brands-400.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-brands-400.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-regular-400.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-regular-400.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-regular-400.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-regular-400.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-solid-900.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-solid-900.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-solid-900.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-solid-900.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-v4compatibility.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-v4compatibility.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.4.2/webfonts/fa-v4compatibility.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.4.2/webfonts/fa-v4compatibility.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-brands-400.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-brands-400.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-brands-400.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-brands-400.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-regular-400.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-regular-400.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-regular-400.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-regular-400.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-solid-900.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-solid-900.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-solid-900.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-solid-900.woff2 -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-v4compatibility.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-v4compatibility.ttf -------------------------------------------------------------------------------- /docs/deps/font-awesome-6.5.2/webfonts/fa-v4compatibility.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/deps/font-awesome-6.5.2/webfonts/fa-v4compatibility.woff2 -------------------------------------------------------------------------------- /docs/deps/headroom-0.11.0/jQuery.headroom.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * headroom.js v0.9.4 - Give your page some headroom. Hide your header until you need it 3 | * Copyright (c) 2017 Nick Williams - http://wicky.nillia.ms/headroom.js 4 | * License: MIT 5 | */ 6 | 7 | !function(a){a&&(a.fn.headroom=function(b){return this.each(function(){var c=a(this),d=c.data("headroom"),e="object"==typeof b&&b;e=a.extend(!0,{},Headroom.options,e),d||(d=new Headroom(this,e),d.init(),c.data("headroom",d)),"string"==typeof b&&(d[b](),"destroy"===b&&c.removeData("headroom"))})},a("[data-headroom]").each(function(){var b=a(this);b.headroom(b.data())}))}(window.Zepto||window.jQuery); -------------------------------------------------------------------------------- /docs/katex-auto.js: -------------------------------------------------------------------------------- 1 | // https://github.com/jgm/pandoc/blob/29fa97ab96b8e2d62d48326e1b949a71dc41f47a/src/Text/Pandoc/Writers/HTML.hs#L332-L345 2 | document.addEventListener("DOMContentLoaded", function () { 3 | var mathElements = document.getElementsByClassName("math"); 4 | var macros = []; 5 | for (var i = 0; i < mathElements.length; i++) { 6 | var texText = mathElements[i].firstChild; 7 | if (mathElements[i].tagName == "SPAN") { 8 | katex.render(texText.data, mathElements[i], { 9 | displayMode: mathElements[i].classList.contains("display"), 10 | throwOnError: false, 11 | macros: macros, 12 | fleqn: false 13 | }); 14 | }}}); 15 | -------------------------------------------------------------------------------- /docs/lightswitch.js: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * Color mode toggler for Bootstrap's docs (https://getbootstrap.com/) 4 | * Copyright 2011-2023 The Bootstrap Authors 5 | * Licensed under the Creative Commons Attribution 3.0 Unported License. 6 | * Updates for {pkgdown} by the {bslib} authors, also licensed under CC-BY-3.0. 7 | */ 8 | 9 | const getStoredTheme = () => localStorage.getItem('theme') 10 | const setStoredTheme = theme => localStorage.setItem('theme', theme) 11 | 12 | const getPreferredTheme = () => { 13 | const storedTheme = getStoredTheme() 14 | if (storedTheme) { 15 | return storedTheme 16 | } 17 | 18 | return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' 19 | } 20 | 21 | const setTheme = theme => { 22 | if (theme === 'auto') { 23 | document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')) 24 | } else { 25 | document.documentElement.setAttribute('data-bs-theme', theme) 26 | } 27 | } 28 | 29 | function bsSetupThemeToggle () { 30 | 'use strict' 31 | 32 | const showActiveTheme = (theme, focus = false) => { 33 | var activeLabel, activeIcon; 34 | 35 | document.querySelectorAll('[data-bs-theme-value]').forEach(element => { 36 | const buttonTheme = element.getAttribute('data-bs-theme-value') 37 | const isActive = buttonTheme == theme 38 | 39 | element.classList.toggle('active', isActive) 40 | element.setAttribute('aria-pressed', isActive) 41 | 42 | if (isActive) { 43 | activeLabel = element.textContent; 44 | activeIcon = element.querySelector('span').classList.value; 45 | } 46 | }) 47 | 48 | const themeSwitcher = document.querySelector('#dropdown-lightswitch') 49 | if (!themeSwitcher) { 50 | return 51 | } 52 | 53 | themeSwitcher.setAttribute('aria-label', activeLabel) 54 | themeSwitcher.querySelector('span').classList.value = activeIcon; 55 | 56 | if (focus) { 57 | themeSwitcher.focus() 58 | } 59 | } 60 | 61 | window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { 62 | const storedTheme = getStoredTheme() 63 | if (storedTheme !== 'light' && storedTheme !== 'dark') { 64 | setTheme(getPreferredTheme()) 65 | } 66 | }) 67 | 68 | window.addEventListener('DOMContentLoaded', () => { 69 | showActiveTheme(getPreferredTheme()) 70 | 71 | document 72 | .querySelectorAll('[data-bs-theme-value]') 73 | .forEach(toggle => { 74 | toggle.addEventListener('click', () => { 75 | const theme = toggle.getAttribute('data-bs-theme-value') 76 | setTheme(theme) 77 | setStoredTheme(theme) 78 | showActiveTheme(theme, true) 79 | }) 80 | }) 81 | }) 82 | } 83 | 84 | setTheme(getPreferredTheme()); 85 | bsSetupThemeToggle(); 86 | -------------------------------------------------------------------------------- /docs/link.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: '3.2' 2 | pkgdown: 2.1.2 3 | pkgdown_sha: ~ 4 | articles: 5 | luajr: luajr.html 6 | objects: objects.html 7 | packages: packages.html 8 | last_built: 2025-05-01T15:56Z 9 | urls: 10 | reference: https://nicholasdavies.github.io/luajr/reference 11 | article: https://nicholasdavies.github.io/luajr/articles 12 | -------------------------------------------------------------------------------- /docs/reference/Rplot001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/reference/Rplot001.png -------------------------------------------------------------------------------- /docs/reference/figures/README-pressure-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/docs/reference/figures/README-pressure-1.png -------------------------------------------------------------------------------- /docs/reference/luajr.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/sitemap.xml: -------------------------------------------------------------------------------- 1 | 2 | https://nicholasdavies.github.io/luajr/404.html 3 | https://nicholasdavies.github.io/luajr/LICENSE-text.html 4 | https://nicholasdavies.github.io/luajr/LICENSE.html 5 | https://nicholasdavies.github.io/luajr/articles/index.html 6 | https://nicholasdavies.github.io/luajr/articles/luajr-module.html 7 | https://nicholasdavies.github.io/luajr/articles/luajr.html 8 | https://nicholasdavies.github.io/luajr/articles/objects.html 9 | https://nicholasdavies.github.io/luajr/articles/packages.html 10 | https://nicholasdavies.github.io/luajr/authors.html 11 | https://nicholasdavies.github.io/luajr/index.html 12 | https://nicholasdavies.github.io/luajr/news/index.html 13 | https://nicholasdavies.github.io/luajr/reference/index.html 14 | https://nicholasdavies.github.io/luajr/reference/lua.html 15 | https://nicholasdavies.github.io/luajr/reference/lua_func.html 16 | https://nicholasdavies.github.io/luajr/reference/lua_mode.html 17 | https://nicholasdavies.github.io/luajr/reference/lua_open.html 18 | https://nicholasdavies.github.io/luajr/reference/lua_parallel.html 19 | https://nicholasdavies.github.io/luajr/reference/lua_profile.html 20 | https://nicholasdavies.github.io/luajr/reference/lua_reset.html 21 | https://nicholasdavies.github.io/luajr/reference/lua_shell.html 22 | https://nicholasdavies.github.io/luajr/reference/luajr-package.html 23 | 24 | 25 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | bibentry("Manual", 2 | title = "{luajr}: {LuaJIT} Scripting", 3 | author = "Nicholas Davies", 4 | year = 2025, 5 | note = "R package", 6 | url = "https://nicholasdavies.github.io/luajr/") 7 | 8 | bibentry("Misc", 9 | title = "{LuaJIT} -- a Just-In-Time Compiler for Lua.", 10 | author = person("Mike", "Pall"), 11 | year = 2025, 12 | url = "https://luajit.org/") 13 | -------------------------------------------------------------------------------- /inst/include/luajr.h: -------------------------------------------------------------------------------- 1 | #ifndef LUAJR_API_H 2 | #define LUAJR_API_H 3 | 4 | struct SEXPREC; 5 | typedef struct SEXPREC* SEXP; 6 | 7 | // Lua and LuaJIT APIs 8 | #include "luajr_lua.h" 9 | #include "luajr_lauxlib.h" 10 | #include "luajr_lualib.h" 11 | #include "luajr_luajit.h" 12 | #include "luajr_const.h" 13 | 14 | // luajr API functions 15 | // Declare luajr API functions in src/shared.h, inst/include/luajr.h, and inst/include/luajr_funcs.h. 16 | extern SEXP (*luajr_open)(); 17 | extern SEXP (*luajr_reset)(); 18 | extern lua_State* (*luajr_newstate)(); 19 | extern lua_State* (*luajr_getstate)(SEXP Lx); 20 | extern void (*luajr_pushsexp)(lua_State* L, SEXP x, char as); 21 | extern SEXP (*luajr_tosexp)(lua_State* L, int index); 22 | extern void (*luajr_pass)(lua_State* L, SEXP args, const char* acode); 23 | extern SEXP (*luajr_return)(lua_State* L, int nret); 24 | extern SEXP (*luajr_run_code)(SEXP code, SEXP Lx); 25 | extern SEXP (*luajr_run_file)(SEXP filename, SEXP Lx); 26 | extern SEXP (*luajr_func_create)(SEXP func, SEXP Lx); 27 | extern SEXP (*luajr_func_call)(SEXP fx, SEXP alist, SEXP acode, SEXP Lx); 28 | extern void (*luajr_pushfunc)(SEXP fx); 29 | extern SEXP (*luajr_run_parallel)(SEXP func, SEXP n, SEXP threads, SEXP pre); 30 | extern void (*luajr_loadstring)(lua_State* L, const char* str); 31 | extern void (*luajr_dostring)(lua_State* L, const char* str, int tooling); 32 | extern void (*luajr_loadfile)(lua_State* L, const char* filename); 33 | extern void (*luajr_dofile)(lua_State* L, const char* filename, int tooling); 34 | extern void (*luajr_loadbuffer)(lua_State *L, const char *buff, unsigned int sz, const char *name); 35 | extern int (*luajr_pcall)(lua_State* L, int nargs, int nresults, const char* what, int tooling); 36 | extern SEXP (*luajr_set_mode)(SEXP debug, SEXP profile, SEXP jit); 37 | extern SEXP (*luajr_get_mode)(); 38 | extern int (*luajr_debug_mode)(); 39 | extern int (*luajr_profile_mode)(); 40 | extern void (*luajr_profile_collect)(lua_State* L); 41 | extern SEXP (*luajr_profile_data)(SEXP flush); 42 | extern SEXP (*luajr_makepointer)(void* ptr, int tag_code, void (*finalize)(SEXP)); 43 | extern void* (*luajr_getpointer)(SEXP x, int tag_code); 44 | 45 | #endif // LUAJR_API_H 46 | -------------------------------------------------------------------------------- /inst/include/luajr_const.h: -------------------------------------------------------------------------------- 1 | #ifndef LUAJR_CONST_H 2 | #define LUAJR_CONST_H 3 | 4 | #define LUAJR_TOOLING_NONE 0 5 | #define LUAJR_TOOLING_ALL 1 6 | #define LUAJR_NO_ERROR_HANDLING 2 7 | #define LUAJR_NO_PROFILE_COLLECT 4 8 | 9 | #define LUAJR_DEBUG_MODE_OFF 0 10 | #define LUAJR_DEBUG_MODE_ERROR 1 11 | #define LUAJR_DEBUG_MODE_STEP 2 12 | 13 | #define LUAJR_PROFILE_MODE_OFF 0 14 | #define LUAJR_PROFILE_MODE_ON 1 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /inst/include/luajr_funcdef.h: -------------------------------------------------------------------------------- 1 | #define API_FUNCTION(return_type, func_name, ...) \ 2 | return_type (*func_name)(__VA_ARGS__) = reinterpret_cast(R_GetCCallable("luajr", #func_name)); 3 | #include "luajr_funcs.h" 4 | #undef API_FUNCTION 5 | -------------------------------------------------------------------------------- /local/build_vignettes.R: -------------------------------------------------------------------------------- 1 | tools::buildVignettes(dir = ".", tangle=TRUE) 2 | dir.create("inst/doc") 3 | file.copy(dir("vignettes", full.names=TRUE), "inst/doc", overwrite=TRUE) 4 | -------------------------------------------------------------------------------- /local/devnotes.txt: -------------------------------------------------------------------------------- 1 | Feature plan / to do 2 | -------------------- 3 | - debug and shell don't mix well 4 | - wall clock time too in profiler? 5 | - ReadConsole seems to have a hard limit in the number of characters that can 6 | be read (1022). The warning and truncation happens when a line is longer than 7 | 1022 characters, not when the bufsize is longer than 1024. This is being fixed 8 | in R 4.4.2. Once that has happened, I will need to make the bufsize larger to 9 | accommodate this. 10 | - Finish documenting profiler (e.g. profiler object in lua_profile) and 11 | add flame graph thing 12 | - Dump bytecode from debugger? 13 | - Add lua_load(code, file, L) to turn code/file into bytecode using LuaJIT, 14 | which I guess could use lua_dump... 15 | - Improve and document using Lua C API from an R package 16 | - Make sure all API functions can handle NAs, wrong types, etc. 17 | - For luajr.list: 18 | v("/list/order", ord) -- reorder elements (like data.table) 19 | - Make thread safe, perhaps by locking on any R operations from within Lua 20 | 21 | - Mode where name suffix determines type, e.g. xs xb x1, or x_s, x_b, x_1? Or 22 | a preprocessor with specified types? (meaning, can declare a lua func as 23 | function(x:integer, y:numeric_r) etc) 24 | - Once there is something to cite, usethis::use_citation() 25 | - Do typedefs for underlying R logical, integer, numeric types? 26 | - There could be a 'debug mode' which does bounds checking on luajr types? 27 | 28 | 29 | luajit as subfolder in src/luajit 30 | --------------------------------- 31 | 32 | I tried doing this with git subtree, explained here: 33 | 34 | https://blog.developer.atlassian.com/the-power-of-git-subtree/ 35 | 36 | but ran into issues. Instead I now pull luajit to its own folder, 37 | and copy everything except .git to luajr/src. 38 | 39 | 40 | Warnings when making luajit 41 | --------------------------- 42 | 43 | Having -march=native in CFLAGS causes a warning about an uninitialized fs.len 44 | to be emitted when compiling ljamalg.o. Having looked at the offending source, 45 | it's not a real issue. 46 | -------------------------------------------------------------------------------- /local/example.cpp: -------------------------------------------------------------------------------- 1 | // [[Rcpp::depends(luajr)]] 2 | #include 3 | #include 4 | #include 5 | 6 | // [[Rcpp::export]] 7 | void exampleS() 8 | { 9 | lua_State* L = luajr_getstate(R_NilValue); 10 | lua_pushinteger(L, 100); 11 | Rcpp::Rcout << "Goodbye, world with stack size " << lua_gettop(L) << "!\n"; 12 | lua_pop(L, 1); 13 | } 14 | -------------------------------------------------------------------------------- /local/example_cpp.R: -------------------------------------------------------------------------------- 1 | # NOTE. Cannot test this using testthat because neither example compiles under 2 | # the environment set by testthat, though they compile on their own. There are 3 | # issues with the preprocessor not finding luajr.h, and then issues with 4 | # the linker not linking to luajr either. Seems like it may be more trouble than 5 | # it's worth to integrate this into formal testthat testing. But it should be 6 | # part of package testing, ideally. 7 | 8 | # Test whether we can use the Lua API from C++, using cppFunction 9 | Rcpp::cppFunction('void exampleF() { 10 | lua_State* L = luajr_getstate(R_NilValue); 11 | lua_pushinteger(L, 100); 12 | Rcpp::Rcout << "Ciao, world with stack size " << lua_gettop(L) << "!\\n"; 13 | lua_pop(L, 1); 14 | }', depends = "luajr"); 15 | 16 | exampleF() # "Ciao, world with stack size 1!" 17 | 18 | # Test whether we can use the Lua API from C++, using sourceCpp 19 | Rcpp::sourceCpp("./local/example.cpp") 20 | exampleS() # "Goodbye, world with stack size 1!" 21 | -------------------------------------------------------------------------------- /local/long_vector_test.R: -------------------------------------------------------------------------------- 1 | # Do long vectors work? 2 | 3 | library(luajr) 4 | 5 | biggie = rep(0L, 2^31) 6 | func = lua_func("function(x) x[3] = 10 end", "r") 7 | func(biggie) 8 | -------------------------------------------------------------------------------- /local/mymodule.lua: -------------------------------------------------------------------------------- 1 | local mymodule = {} 2 | 3 | local fave_name = "Nick" 4 | 5 | function mymodule.greet(name) 6 | print("Hello, " .. name .. "!") 7 | if name == fave_name then 8 | print("Incidentally, that's a great name. Nice one.") 9 | end 10 | end 11 | 12 | return mymodule 13 | -------------------------------------------------------------------------------- /local/odetest.R: -------------------------------------------------------------------------------- 1 | library(luajr) 2 | library(ggplot2) 3 | library(data.table) 4 | library(Rcpp) 5 | 6 | lua(filename = "./local/odetest.lua") 7 | runner = lua_func("run") 8 | 9 | sol <- runner() 10 | 11 | rm(sol) 12 | 13 | bench::mark( 14 | sol <- runner() 15 | ) 16 | 17 | ggplot(sol) + 18 | geom_line(aes(t, S)) + 19 | geom_line(aes(t, I)) + 20 | geom_line(aes(t, R)) 21 | 22 | # deSolve R version 23 | library(deSolve) 24 | 25 | beta = 0.05 26 | gamma = 0.025 27 | 28 | gradient = function(t, x, pars) 29 | { 30 | dS = -beta * x[["I"]] * x[["S"]] 31 | dI = beta * x[["I"]] * x[["S"]] - gamma * x[["I"]] 32 | dR = gamma * x[["I"]] 33 | return (list(c(dS, dI, dR))) 34 | } 35 | 36 | yini = c(S = 0.999, I = 0.001, R = 0.000) 37 | times = seq(1, 1000, by = 1) 38 | 39 | bench::mark( 40 | out <- ode(yini, times, gradient, c(), method = "rk4") 41 | ) 42 | 43 | out = as.data.table(out) 44 | 45 | ggplot(out) + 46 | geom_line(aes(time, S)) + 47 | geom_line(aes(time, I)) + 48 | geom_line(aes(time, R)) 49 | 50 | 51 | # deSolve Rcpp version 52 | 53 | cppFunction( 54 | 'List gradient2(double t, Rcpp::NumericVector x, SEXP pars) 55 | { 56 | double dS = -0.05 * x[1] * x[0]; 57 | double dI = 0.05 * x[1] * x[0] - 0.025 * x[1]; 58 | double dR = 0.025 * x[1]; 59 | return List::create(NumericVector::create(dS, dI, dR)); 60 | }') 61 | 62 | yini = c(S = 0.999, I = 0.001, R = 0.000) 63 | times = seq(1, 1000, by = 1) 64 | 65 | bench::mark( 66 | out <- ode(yini, times, gradient2, c(), method = "rk4") 67 | ) 68 | 69 | out = as.data.table(out) 70 | 71 | ggplot(out) + 72 | geom_line(aes(time, S)) + 73 | geom_line(aes(time, I)) + 74 | geom_line(aes(time, R)) 75 | -------------------------------------------------------------------------------- /local/odetest.lua: -------------------------------------------------------------------------------- 1 | local beta = 0.05 2 | local gamma = 0.025 3 | 4 | function gradient(t, x, dxdt) 5 | dxdt.S = -beta * x.I * x.S 6 | dxdt.I = beta * x.I * x.S - gamma * x.I 7 | dxdt.R = gamma * x.I 8 | end 9 | 10 | function rk4_step(t, h, x, k1, k2, k3, k4, q) 11 | -- Copy current state to q 12 | q.S = x.S 13 | q.I = x.I 14 | q.R = x.R 15 | 16 | -- Calculate Runge-Kutta 4 gradients 17 | -- k1 18 | gradient(t, x, k1) 19 | 20 | -- k2 21 | t = t + h/2 22 | x.S = q.S + h/2 * k1.S 23 | x.I = q.I + h/2 * k1.I 24 | x.R = q.R + h/2 * k1.R 25 | gradient(t, x, k2) 26 | 27 | -- k3 28 | x.S = q.S + h/2 * k2.S 29 | x.I = q.I + h/2 * k2.I 30 | x.R = q.R + h/2 * k2.R 31 | gradient(t, x, k3) 32 | 33 | -- k4 34 | t = t + h/2 35 | x.S = q.S + h * k3.S 36 | x.I = q.I + h * k3.I 37 | x.R = q.R + h * k3.R 38 | gradient(t, x, k4) 39 | 40 | -- update x 41 | x.S = q.S + h/6 * (k1.S + 2 * k2.S + 2 * k3.S + k4.S); 42 | x.I = q.I + h/6 * (k1.I + 2 * k2.I + 2 * k3.I + k4.I); 43 | x.R = q.R + h/6 * (k1.R + 2 * k2.R + 2 * k3.R + k4.R); 44 | end 45 | 46 | function run() 47 | -- Set up initial state 48 | local x = { S = 0.999, I = 0.001, R = 0 } 49 | 50 | -- Set up temporary storage 51 | local k1 = { S = 0, I = 0, R = 0 } 52 | local k2 = { S = 0, I = 0, R = 0 } 53 | local k3 = { S = 0, I = 0, R = 0 } 54 | local k4 = { S = 0, I = 0, R = 0 } 55 | local q = { S = 0, I = 0, R = 0 } 56 | 57 | -- Solution storage 58 | local df = luajr.dataframe() 59 | df.t = luajr.numeric_r(1000, 0) 60 | df.S = luajr.numeric_r(1000, 0) 61 | df.I = luajr.numeric_r(1000, 0) 62 | df.R = luajr.numeric_r(1000, 0) 63 | 64 | for t = 1,1000 do 65 | df.t[t] = t 66 | df.S[t] = x.S 67 | df.I[t] = x.I 68 | df.R[t] = x.R 69 | 70 | rk4_step(t, 1.0, x, k1, k2, k3, k4, q) 71 | end 72 | 73 | return df 74 | end 75 | -------------------------------------------------------------------------------- /local/profile.R: -------------------------------------------------------------------------------- 1 | library(luajr) 2 | library(data.table) 3 | library(ggplot2) 4 | 5 | plot_profile = function(p) 6 | { 7 | pp = sort(unlist(p)) 8 | dp = data.table(name = names(pp), time = unname(pp)) 9 | dp = dp[order(time)] 10 | dp[, line := as.integer(stringr::str_extract(name, ":([0-9]+)\\|", group = 1))] 11 | dp[, func := stringr::str_extract(name, "\\|(.*)$", group = 1)] 12 | 13 | ggplot(dp[time > 2]) + 14 | geom_col(aes(y = time * 100 / sum(dp$time), x = func, fill = as.factor(line)), colour = "black") + 15 | geom_text(aes(y = time * 100 / sum(dp$time), x = func, label = line, group = line), 16 | position = position_stack(vjust = .5)) + 17 | theme(legend.position = "none") + 18 | labs(x = "function", y = "Time (%)") 19 | } 20 | 21 | 22 | lua_mode(profile = TRUE, 23 | lua("local s = 0 24 | for i = 1,10^8 do 25 | s = s + i 26 | s = math.sin(s) 27 | s = math.exp(s) 28 | end 29 | return s")) 30 | 31 | prof = lua_profile() 32 | prof 33 | 34 | plot_profile(prof) 35 | -------------------------------------------------------------------------------- /local/use_case.cpp: -------------------------------------------------------------------------------- 1 | // Example use case of luajr 2 | // This would be inside a package that also links to luajr: 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // [[Rcpp::plugins(cpp14)]] 9 | 10 | // [[Rcpp::export]] 11 | Rcpp::NumericVector solve_ode_model( 12 | std::vector init = { 1.0, 1.0 }, 13 | std::string dxdt = "function(x) return { -x[1], x[2] } end", 14 | double t0 = 0.0, double t1 = 10.0, double t_step = 0.1 15 | ) 16 | { 17 | // etc... 18 | } 19 | -------------------------------------------------------------------------------- /luajr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageCleanBeforeInstall: No 21 | PackageInstallArgs: --no-multiarch --with-keep.source 22 | -------------------------------------------------------------------------------- /man/figures/README-pressure-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicholasdavies/luajr/9cae7a99a4f2676d074bf069bf06530172f44547/man/figures/README-pressure-1.png -------------------------------------------------------------------------------- /man/lua.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lua.R 3 | \name{lua} 4 | \alias{lua} 5 | \title{Run Lua code} 6 | \usage{ 7 | lua(code, filename = NULL, L = NULL) 8 | } 9 | \arguments{ 10 | \item{code}{Lua code block to run.} 11 | 12 | \item{filename}{If non-\code{NULL}, name of file to run.} 13 | 14 | \item{L}{\link[=lua_open]{Lua state} in which to run the code. \code{NULL} (default) 15 | uses the default Lua state for \pkg{luajr}.} 16 | } 17 | \value{ 18 | Lua value(s) returned by the code block converted to R object(s). 19 | Only a subset of all Lua types can be converted to R objects at present. 20 | If multiple values are returned, these are packaged in a \code{list}. 21 | } 22 | \description{ 23 | Runs the specified Lua code. 24 | } 25 | \examples{ 26 | twelve <- lua("return 3*4") 27 | print(twelve) 28 | } 29 | -------------------------------------------------------------------------------- /man/lua_open.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lua_open.R 3 | \name{lua_open} 4 | \alias{lua_open} 5 | \title{Create a new Lua state} 6 | \usage{ 7 | lua_open() 8 | } 9 | \value{ 10 | External pointer wrapping the newly created Lua state. 11 | } 12 | \description{ 13 | Creates a new, empty Lua state and returns an external pointer wrapping that 14 | state. 15 | } 16 | \details{ 17 | All Lua code is executed within a given Lua state. A Lua state is similar to 18 | the global environment in R, in that it is where all variables and functions 19 | are defined. \pkg{luajr} automatically maintains a "default" Lua state, so 20 | most users of \pkg{luajr} will not need to use \code{\link[=lua_open]{lua_open()}}. 21 | 22 | However, if for whatever reason you want to maintain multiple different Lua 23 | states at a time, each with their own independent global variables and 24 | functions, \code{\link[=lua_open]{lua_open()}} can be used to create a new Lua state which can then 25 | be passed to \code{\link[=lua]{lua()}}, \code{\link[=lua_func]{lua_func()}} and \code{\link[=lua_shell]{lua_shell()}} via the \code{L} parameter. 26 | These functions will then operate within that Lua state instead of the 27 | default one. The default Lua state can be specified explicitly with 28 | \code{L = NULL}. 29 | 30 | Note that there is currently no way (provided by \pkg{luajr}) of saving a 31 | Lua state to disk so that the state can be restarted later. Also, there is 32 | no \code{lua_close} in \pkg{luajr} because Lua states are closed automatically 33 | when they are garbage collected in R. 34 | } 35 | \examples{ 36 | L1 <- lua_open() 37 | lua("a = 2") 38 | lua("a = 4", L = L1) 39 | lua("print(a)") # 2 40 | lua("print(a)", L = L1) # 4 41 | } 42 | -------------------------------------------------------------------------------- /man/lua_parallel.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lua_parallel.R 3 | \name{lua_parallel} 4 | \alias{lua_parallel} 5 | \title{Run Lua code in parallel} 6 | \usage{ 7 | lua_parallel(func, n, threads, pre = NA_character_) 8 | } 9 | \arguments{ 10 | \item{func}{Lua expression evaluating to a function.} 11 | 12 | \item{n}{Number of function executions.} 13 | 14 | \item{threads}{Number of threads to create, or a list of existing Lua states 15 | (e.g. as created by \code{\link[=lua_open]{lua_open()}}), all different, one for each thread.} 16 | 17 | \item{pre}{Lua code block to run once for each thread at creation.} 18 | } 19 | \value{ 20 | List of \code{n} values returned from the Lua function \code{func}. 21 | } 22 | \description{ 23 | Runs a Lua function multiple times, with function runs divided among 24 | multiple threads. 25 | } 26 | \details{ 27 | This function is experimental. Its interface and behaviour are likely to 28 | change in subsequent versions of luajr. 29 | 30 | \code{\link[=lua_parallel]{lua_parallel()}} works as follows. A number \code{threads} of new Lua states is 31 | created with the standard Lua libraries and the \code{luajr} module opened in 32 | each (i.e. as though the states were created using \code{\link[=lua_open]{lua_open()}}). Then, a 33 | thread is launched for each state. Within each thread, the code in \code{pre} is 34 | run in the corresponding Lua state. Then, \code{func(i)} is called for each \code{i} 35 | in \code{1:n}, with the calls spread across the states. Finally, the Lua states 36 | are closed and the results are returned in a list. The list elements are 37 | returned in the correct order, i.e. the ordering of the returned list does 38 | not depend on the actual execution order of each call to \code{func}. 39 | 40 | Instead of an integer, \code{threads} can be a list of Lua states, e.g. \code{NULL} 41 | for the default Lua state or a state returned by \code{\link[=lua_open]{lua_open()}}. This saves 42 | the time needed to open the new states, which takes a few milliseconds. 43 | } 44 | \section{Safety and performance}{ 45 | 46 | 47 | Note that \code{func} has to be thread-safe. All pure Lua code and built-in Lua 48 | library functions are thread-safe, except for certain functions in the 49 | built-in \strong{os} and \strong{io} libraries (search for "thread safe" in the 50 | \href{https://www.lua.org/manual/5.2/manual.html}{Lua 5.2 reference manual}). 51 | 52 | Additionally, use of luajr reference types is \strong{not} thread-safe because 53 | these use R to allocate and manage memory, and R is not thread-safe. This 54 | means that you cannot safely use \code{luajr.logical_r}, \code{luajr.integer_r}, 55 | \code{luajr.numeric_r}, \code{luajr.character_r}, or other reference types within 56 | \code{func}. \code{luajr.list} and \code{luajr.dataframe} are fine, provided the list 57 | entries / dataframe columns are value types. 58 | 59 | There is overhead associated with creating new Lua states and with gathering 60 | all the function results in an R list. It is advisable to check whether 61 | running your Lua code in parallel actually gives a substantial speed 62 | increase. 63 | } 64 | 65 | \examples{ 66 | lua_parallel("function(i) return i end", n = 4, threads = 2) 67 | } 68 | -------------------------------------------------------------------------------- /man/lua_profile.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lua_mode.R 3 | \name{lua_profile} 4 | \alias{lua_profile} 5 | \title{Get profiling data} 6 | \usage{ 7 | lua_profile(flush = TRUE) 8 | } 9 | \arguments{ 10 | \item{flush}{If \code{TRUE}, clears the internal profile data buffer (default); 11 | if \code{FALSE}, doesn't. (Set to \code{FALSE} if you want to 'peek' at the profiling 12 | data collected so far, but you want to collect more data to add to this 13 | later.)} 14 | } 15 | \value{ 16 | An object of class \code{"lua_profile"}. 17 | } 18 | \description{ 19 | After running Lua code with the profiler active (using \code{\link[=lua_mode]{lua_mode()}}), use 20 | this function to get the profiling data that has been collected. 21 | } 22 | \details{ 23 | This function is experimental. Its interface and behaviour may change in 24 | subsequent versions of luajr. 25 | } 26 | \examples{ 27 | \dontrun{ 28 | lua_mode(profile = TRUE) 29 | pointless_computation = lua_func( 30 | "function() 31 | local s = startval 32 | for i = 1,10^8 do 33 | s = math.sin(s) 34 | s = math.exp(s^2) 35 | s = s + 1 36 | end 37 | return s 38 | end") 39 | lua("startval = 100") 40 | pointless_computation() 41 | lua_mode(profile = FALSE) 42 | 43 | prof = lua_profile() 44 | } 45 | } 46 | \seealso{ 47 | \code{\link[=lua_mode]{lua_mode()}} for generating the profiling data. 48 | } 49 | -------------------------------------------------------------------------------- /man/lua_reset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lua_open.R 3 | \name{lua_reset} 4 | \alias{lua_reset} 5 | \title{Reset the default Lua state} 6 | \usage{ 7 | lua_reset() 8 | } 9 | \value{ 10 | None. 11 | } 12 | \description{ 13 | Clears out all variables from the default Lua state, freeing up the 14 | associated memory. 15 | } 16 | \details{ 17 | This resets the default \link[=lua_open]{Lua state} only. To reset a non-default 18 | Lua state \code{L} returned by \code{\link[=lua_open]{lua_open()}}, just do \code{L <- lua_open()} again. The 19 | memory previously used will be cleaned up at the next garbage collection. 20 | } 21 | \examples{ 22 | lua("a = 2") 23 | lua_reset() 24 | lua("print(a)") # nil 25 | } 26 | -------------------------------------------------------------------------------- /man/lua_shell.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lua_shell.R 3 | \name{lua_shell} 4 | \alias{lua_shell} 5 | \title{Run an interactive Lua shell} 6 | \usage{ 7 | lua_shell(L = NULL) 8 | } 9 | \arguments{ 10 | \item{L}{\link[=lua_open]{Lua state} in which to run the code. \code{NULL} (default) 11 | uses the default Lua state for \pkg{luajr}.} 12 | } 13 | \value{ 14 | None. 15 | } 16 | \description{ 17 | When in interactive mode, provides a basic read-eval-print loop with LuaJIT. 18 | } 19 | \details{ 20 | Enter an empty line to return to R. 21 | 22 | As a convenience, lines starting with an equals sign have the \code{"="} replaced 23 | with \code{"return "}, so that e.g. entering \verb{=x} will show the value of \code{x} as 24 | returned to R. 25 | } 26 | -------------------------------------------------------------------------------- /man/luajr-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/luajr-package.R 3 | \docType{package} 4 | \name{luajr-package} 5 | \alias{luajr} 6 | \alias{luajr-package} 7 | \title{luajr: LuaJIT Scripting} 8 | \description{ 9 | 'luajr' provides an interface to \href{https://luajit.org}{LuaJIT}, a 10 | just-in-time compiler for the \href{https://www.lua.org}{Lua scripting language}. 11 | It allows users to run Lua code from R. 12 | } 13 | \section{The R API}{ 14 | 15 | \itemize{ 16 | \item \code{\link[=lua]{lua()}}: run Lua code 17 | \item \code{\link[=lua_func]{lua_func()}}: make a Lua function callable from R 18 | \item \code{\link[=lua_shell]{lua_shell()}}: run an interactive Lua shell 19 | \item \code{\link[=lua_open]{lua_open()}}: create a new Lua state 20 | \item \code{\link[=lua_reset]{lua_reset()}}: reset the default Lua state 21 | \item \code{\link[=lua_parallel]{lua_parallel()}}: run Lua code in parallel 22 | } 23 | } 24 | 25 | \section{Further reading}{ 26 | 27 | For an introduction to 'luajr', see \code{vignette("luajr")} 28 | } 29 | 30 | \seealso{ 31 | Useful links: 32 | \itemize{ 33 | \item \url{https://github.com/nicholasdavies/luajr} 34 | \item \url{https://nicholasdavies.github.io/luajr/} 35 | \item Report bugs at \url{https://github.com/nicholasdavies/luajr/issues} 36 | } 37 | 38 | } 39 | \author{ 40 | \strong{Maintainer}: Nicholas Davies \email{nicholas.davies@lshtm.ac.uk} (\href{https://orcid.org/0000-0002-1740-1412}{ORCID}) (Author of the R package wrapper) [contributor, copyright holder] 41 | 42 | Authors: 43 | \itemize{ 44 | \item Mike Pall (Author of the embedded LuaJIT compiler) [copyright holder] 45 | } 46 | 47 | Other contributors: 48 | \itemize{ 49 | \item Lua.org, PUC-Rio (Copyright holders over portions of Lua source code included in LuaJIT) [copyright holder] 50 | \item Scott Lembcke, Howling Moon Software (Authors of the embedded debugger.lua debugger) [contributor, copyright holder] 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.dll 4 | *.a 5 | -------------------------------------------------------------------------------- /src/Makevars.in: -------------------------------------------------------------------------------- 1 | # See Section 1.2.1 "Using 'Makevars'" of Writing R Extensions 2 | # cran.r-project.org/doc/manuals/R-exts.pdf 3 | 4 | PKG_CPPFLAGS=@PKG_CPPFLAGS@ -Iluajit/src 5 | PKG_CXXFLAGS=@PKG_CXXFLAGS@ 6 | PKG_LIBS=@PKG_LIBS@ luajit/src/libluajit.a 7 | 8 | $(SHLIB): luajit/src/libluajit.a 9 | 10 | # Build LuaJIT 11 | # The amalg target amalgamates luajit source into one large C file, which allows 12 | # GCC to generate 'shorter and faster code' (see Installation docs for LuaJIT). 13 | # This also overrides CC, CFLAGS etc for compiling the library, as recommended 14 | # by 'Writing R Extensions'. We also define LUAJIT_ENABLE_LUA52COMPAT. We turn 15 | # off some warnings for LuaJIT only, which has been extensively tested and for 16 | # which there are known diagnostic false positives 17 | # (see https://github.com/LuaJIT/LuaJIT/pull/1144). 18 | luajit/src/libluajit.a: 19 | @(cd luajit && @make@ amalg BUILDMODE=static \ 20 | CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS) -DLUAJIT_ENABLE_LUA52COMPAT -O2 -fomit-frame-pointer -Wno-pedantic -fno-sanitize=all" \ 21 | LDFLAGS="$(LDFLAGS) -fno-sanitize=all" AR="$(AR)" RANLIB="$(RANLIB)") 22 | -------------------------------------------------------------------------------- /src/lua_internal.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | #include 3 | extern "C" { 4 | #include "lua.h" 5 | #include "lualib.h" 6 | #include "lauxlib.h" 7 | } 8 | #define R_NO_REMAP 9 | #include 10 | #include 11 | 12 | // Access to Lua C API 13 | 14 | extern "C" SEXP luajr_lua_gettop(SEXP Lx) 15 | { 16 | return Rf_ScalarInteger(lua_gettop(luajr_getstate(Lx))); 17 | } 18 | -------------------------------------------------------------------------------- /src/luajit/.gitattributes: -------------------------------------------------------------------------------- 1 | /.relver export-subst 2 | -------------------------------------------------------------------------------- /src/luajit/.gitignore: -------------------------------------------------------------------------------- 1 | *.[oa] 2 | *.so 3 | *.obj 4 | *.lib 5 | *.exp 6 | *.dll 7 | *.exe 8 | *.manifest 9 | *.dmp 10 | *.swp 11 | .tags 12 | -------------------------------------------------------------------------------- /src/luajit/COPYRIGHT: -------------------------------------------------------------------------------- 1 | =============================================================================== 2 | LuaJIT -- a Just-In-Time Compiler for Lua. https://luajit.org/ 3 | 4 | Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | [ MIT license: https://www.opensource.org/licenses/mit-license.php ] 25 | 26 | =============================================================================== 27 | [ LuaJIT includes code from Lua 5.1/5.2, which has this license statement: ] 28 | 29 | Copyright (C) 1994-2012 Lua.org, PUC-Rio. 30 | 31 | Permission is hereby granted, free of charge, to any person obtaining a copy 32 | of this software and associated documentation files (the "Software"), to deal 33 | in the Software without restriction, including without limitation the rights 34 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 35 | copies of the Software, and to permit persons to whom the Software is 36 | furnished to do so, subject to the following conditions: 37 | 38 | The above copyright notice and this permission notice shall be included in 39 | all copies or substantial portions of the Software. 40 | 41 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 42 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 43 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 44 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 45 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 46 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 47 | THE SOFTWARE. 48 | 49 | =============================================================================== 50 | [ LuaJIT includes code from dlmalloc, which has this license statement: ] 51 | 52 | This is a version (aka dlmalloc) of malloc/free/realloc written by 53 | Doug Lea and released to the public domain, as explained at 54 | https://creativecommons.org/licenses/publicdomain 55 | 56 | =============================================================================== 57 | -------------------------------------------------------------------------------- /src/luajit/README: -------------------------------------------------------------------------------- 1 | README for LuaJIT 2.1 2 | --------------------- 3 | 4 | LuaJIT is a Just-In-Time (JIT) compiler for the Lua programming language. 5 | 6 | Project Homepage: https://luajit.org/ 7 | 8 | LuaJIT is Copyright (C) 2005-2025 Mike Pall. 9 | LuaJIT is free software, released under the MIT license. 10 | See full Copyright Notice in the COPYRIGHT file or in luajit.h. 11 | 12 | Documentation for LuaJIT is available in HTML format. 13 | Please point your favorite browser to: 14 | 15 | doc/luajit.html 16 | 17 | -------------------------------------------------------------------------------- /src/luajit/dynasm/dasm_mips64.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------ 2 | -- DynASM MIPS64 module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- See dynasm.lua for full copyright notice. 6 | ------------------------------------------------------------------------------ 7 | -- This module just sets 64 bit mode for the combined MIPS/MIPS64 module. 8 | -- All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | mips64 = true -- Using a global is an ugly, but effective solution. 12 | return require("dasm_mips") 13 | -------------------------------------------------------------------------------- /src/luajit/dynasm/dasm_proto.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** DynASM encoding engine prototypes. 3 | ** Copyright (C) 2005-2025 Mike Pall. All rights reserved. 4 | ** Released under the MIT license. See dynasm.lua for full copyright notice. 5 | */ 6 | 7 | #ifndef _DASM_PROTO_H 8 | #define _DASM_PROTO_H 9 | 10 | #include 11 | #include 12 | 13 | #define DASM_IDENT "DynASM 1.5.0" 14 | #define DASM_VERSION 10500 /* 1.5.0 */ 15 | 16 | #ifndef Dst_DECL 17 | #define Dst_DECL dasm_State **Dst 18 | #endif 19 | 20 | #ifndef Dst_REF 21 | #define Dst_REF (*Dst) 22 | #endif 23 | 24 | #ifndef DASM_FDEF 25 | #define DASM_FDEF extern 26 | #endif 27 | 28 | #ifndef DASM_M_GROW 29 | #define DASM_M_GROW(ctx, t, p, sz, need) \ 30 | do { \ 31 | size_t _sz = (sz), _need = (need); \ 32 | if (_sz < _need) { \ 33 | if (_sz < 16) _sz = 16; \ 34 | while (_sz < _need) _sz += _sz; \ 35 | (p) = (t *)realloc((p), _sz); \ 36 | if ((p) == NULL) exit(1); \ 37 | (sz) = _sz; \ 38 | } \ 39 | } while(0) 40 | #endif 41 | 42 | #ifndef DASM_M_FREE 43 | #define DASM_M_FREE(ctx, p, sz) free(p) 44 | #endif 45 | 46 | /* Internal DynASM encoder state. */ 47 | typedef struct dasm_State dasm_State; 48 | 49 | 50 | /* Initialize and free DynASM state. */ 51 | DASM_FDEF void dasm_init(Dst_DECL, int maxsection); 52 | DASM_FDEF void dasm_free(Dst_DECL); 53 | 54 | /* Setup global array. Must be called before dasm_setup(). */ 55 | DASM_FDEF void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl); 56 | 57 | /* Grow PC label array. Can be called after dasm_setup(), too. */ 58 | DASM_FDEF void dasm_growpc(Dst_DECL, unsigned int maxpc); 59 | 60 | /* Setup encoder. */ 61 | DASM_FDEF void dasm_setup(Dst_DECL, const void *actionlist); 62 | 63 | /* Feed encoder with actions. Calls are generated by pre-processor. */ 64 | DASM_FDEF void dasm_put(Dst_DECL, int start, ...); 65 | 66 | /* Link sections and return the resulting size. */ 67 | DASM_FDEF int dasm_link(Dst_DECL, size_t *szp); 68 | 69 | /* Encode sections into buffer. */ 70 | DASM_FDEF int dasm_encode(Dst_DECL, void *buffer); 71 | 72 | /* Get PC label offset. */ 73 | DASM_FDEF int dasm_getpclabel(Dst_DECL, unsigned int pc); 74 | 75 | #ifdef DASM_CHECKS 76 | /* Optional sanity checker to call between isolated encoding steps. */ 77 | DASM_FDEF int dasm_checkstep(Dst_DECL, int secmatch); 78 | #else 79 | #define dasm_checkstep(a, b) 0 80 | #endif 81 | 82 | 83 | #endif /* _DASM_PROTO_H */ 84 | -------------------------------------------------------------------------------- /src/luajit/dynasm/dasm_x64.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------ 2 | -- DynASM x64 module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- See dynasm.lua for full copyright notice. 6 | ------------------------------------------------------------------------------ 7 | -- This module just sets 64 bit mode for the combined x86/x64 module. 8 | -- All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | x64 = true -- Using a global is an ugly, but effective solution. 12 | return require("dasm_x86") 13 | -------------------------------------------------------------------------------- /src/luajit/etc/luajit.1: -------------------------------------------------------------------------------- 1 | .TH luajit 1 "" "" "LuaJIT documentation" 2 | .SH NAME 3 | luajit \- Just-In-Time Compiler for the Lua Language 4 | \fB 5 | .SH SYNOPSIS 6 | .B luajit 7 | [\fIoptions\fR]... [\fIscript\fR [\fIargs\fR]...] 8 | .SH "WEB SITE" 9 | .IR https://luajit.org 10 | .SH DESCRIPTION 11 | .PP 12 | This is the command-line program to run Lua programs with \fBLuaJIT\fR. 13 | .PP 14 | \fBLuaJIT\fR is a just-in-time (JIT) compiler for the Lua language. 15 | The virtual machine (VM) is based on a fast interpreter combined with 16 | a trace compiler. It can significantly improve the performance of Lua programs. 17 | .PP 18 | \fBLuaJIT\fR is API\- and ABI-compatible with the VM of the standard 19 | Lua\ 5.1 interpreter. When embedding the VM into an application, 20 | the built library can be used as a drop-in replacement. 21 | .SH OPTIONS 22 | .TP 23 | .BI "\-e " chunk 24 | Run the given chunk of Lua code. 25 | .TP 26 | .BI "\-l " library 27 | Load the named library, just like \fBrequire("\fR\fIlibrary\fR\fB")\fR. 28 | .TP 29 | .BI "\-b " ... 30 | Save or list bytecode. Run without arguments to get help on options. 31 | .TP 32 | .BI "\-j " command 33 | Perform LuaJIT control command (optional space after \fB\-j\fR). 34 | .TP 35 | .BI "\-O" [opt] 36 | Control LuaJIT optimizations. 37 | .TP 38 | .B "\-i" 39 | Run in interactive mode. 40 | .TP 41 | .B "\-v" 42 | Show \fBLuaJIT\fR version. 43 | .TP 44 | .B "\-E" 45 | Ignore environment variables. 46 | .TP 47 | .B "\-\-" 48 | Stop processing options. 49 | .TP 50 | .B "\-" 51 | Read script from stdin instead. 52 | .PP 53 | After all options are processed, the given \fIscript\fR is run. 54 | The arguments are passed in the global \fIarg\fR table. 55 | .PP 56 | Interactive mode is only entered, if no \fIscript\fR and no \fB\-e\fR 57 | option is given. Interactive mode can be left with EOF (\fICtrl\-Z\fB). 58 | .SH EXAMPLES 59 | .TP 60 | luajit hello.lua world 61 | 62 | Prints "Hello world", assuming \fIhello.lua\fR contains: 63 | .br 64 | print("Hello", arg[1]) 65 | .TP 66 | luajit \-e "local x=0; for i=1,1e9 do x=x+i end; print(x)" 67 | 68 | Calculates the sum of the numbers from 1 to 1000000000. 69 | .br 70 | And finishes in a reasonable amount of time, too. 71 | .TP 72 | luajit \-jv \-e "for i=1,10 do for j=1,10 do for k=1,100 do end end end" 73 | 74 | Runs some nested loops and shows the resulting traces. 75 | .SH COPYRIGHT 76 | .PP 77 | \fBLuaJIT\fR is Copyright \(co 2005-2025 Mike Pall. 78 | .br 79 | \fBLuaJIT\fR is open source software, released under the MIT license. 80 | .SH SEE ALSO 81 | .PP 82 | More details in the provided HTML docs or at: 83 | .IR https://luajit.org 84 | .br 85 | More about the Lua language can be found at: 86 | .IR https://lua.org/docs.html 87 | .PP 88 | lua(1) 89 | -------------------------------------------------------------------------------- /src/luajit/etc/luajit.pc: -------------------------------------------------------------------------------- 1 | # Package information for LuaJIT to be used by pkg-config. 2 | majver=2 3 | minver=1 4 | relver=ROLLING 5 | version=${majver}.${minver}.${relver} 6 | abiver=5.1 7 | 8 | prefix=/usr/local 9 | multilib=lib 10 | exec_prefix=${prefix} 11 | libdir=${exec_prefix}/${multilib} 12 | libname=luajit-${abiver} 13 | includedir=${prefix}/include/luajit-${majver}.${minver} 14 | 15 | INSTALL_LMOD=${prefix}/share/lua/${abiver} 16 | INSTALL_CMOD=${prefix}/${multilib}/lua/${abiver} 17 | 18 | Name: LuaJIT 19 | Description: Just-in-time compiler for Lua 20 | URL: https://luajit.org 21 | Version: ${version} 22 | Requires: 23 | Libs: -L${libdir} -l${libname} 24 | Libs.private: -Wl,-E -lm -ldl 25 | Cflags: -I${includedir} 26 | -------------------------------------------------------------------------------- /src/luajit/src/.gitignore: -------------------------------------------------------------------------------- 1 | luajit 2 | luajit.h 3 | luajit_relver.txt 4 | lj_bcdef.h 5 | lj_ffdef.h 6 | lj_libdef.h 7 | lj_recdef.h 8 | lj_folddef.h 9 | lj_vm.[sS] 10 | -------------------------------------------------------------------------------- /src/luajit/src/host/.gitignore: -------------------------------------------------------------------------------- 1 | minilua 2 | buildvm 3 | buildvm_arch.h 4 | -------------------------------------------------------------------------------- /src/luajit/src/host/README: -------------------------------------------------------------------------------- 1 | The files in this directory are only used during the build process of LuaJIT. 2 | For cross-compilation, they must be executed on the host, not on the target. 3 | 4 | These files should NOT be installed! 5 | -------------------------------------------------------------------------------- /src/luajit/src/host/buildvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** LuaJIT VM builder. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _BUILDVM_H 7 | #define _BUILDVM_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "lj_def.h" 16 | #include "lj_arch.h" 17 | 18 | /* Hardcoded limits. Increase as needed. */ 19 | #define BUILD_MAX_RELOC 200 /* Max. number of relocations. */ 20 | #define BUILD_MAX_FOLD 4096 /* Max. number of fold rules. */ 21 | 22 | /* Prefix for scanned library definitions. */ 23 | #define LIBDEF_PREFIX "LJLIB_" 24 | 25 | /* Prefix for scanned fold definitions. */ 26 | #define FOLDDEF_PREFIX "LJFOLD" 27 | 28 | /* Prefixes for generated labels. */ 29 | #define LABEL_PREFIX "lj_" 30 | #define LABEL_PREFIX_BC LABEL_PREFIX "BC_" 31 | #define LABEL_PREFIX_FF LABEL_PREFIX "ff_" 32 | #define LABEL_PREFIX_CF LABEL_PREFIX "cf_" 33 | #define LABEL_PREFIX_FFH LABEL_PREFIX "ffh_" 34 | #define LABEL_PREFIX_LIBCF LABEL_PREFIX "lib_cf_" 35 | #define LABEL_PREFIX_LIBINIT LABEL_PREFIX "lib_init_" 36 | 37 | /* Forward declaration. */ 38 | struct dasm_State; 39 | 40 | /* Build modes. */ 41 | #define BUILDDEF(_) \ 42 | _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \ 43 | _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \ 44 | _(folddef) 45 | 46 | typedef enum { 47 | #define BUILDENUM(name) BUILD_##name, 48 | BUILDDEF(BUILDENUM) 49 | #undef BUILDENUM 50 | BUILD__MAX 51 | } BuildMode; 52 | 53 | /* Code relocation. */ 54 | typedef struct BuildReloc { 55 | int32_t ofs; 56 | int sym; 57 | int type; 58 | } BuildReloc; 59 | 60 | typedef struct BuildSym { 61 | const char *name; 62 | int32_t ofs; 63 | } BuildSym; 64 | 65 | /* Build context structure. */ 66 | typedef struct BuildCtx { 67 | /* DynASM state pointer. Should be first member. */ 68 | struct dasm_State *D; 69 | /* Parsed command line. */ 70 | BuildMode mode; 71 | FILE *fp; 72 | const char *outname; 73 | char **args; 74 | /* Code and symbols generated by DynASM. */ 75 | uint8_t *code; 76 | size_t codesz; 77 | int npc, nglob, nsym, nreloc, nrelocsym; 78 | void **glob; 79 | BuildSym *sym; 80 | const char **relocsym; 81 | int32_t *bc_ofs; 82 | const char *beginsym; 83 | /* Strings generated by DynASM. */ 84 | const char *const *globnames; 85 | const char *const *extnames; 86 | const char *dasm_ident; 87 | const char *dasm_arch; 88 | /* Relocations. */ 89 | BuildReloc reloc[BUILD_MAX_RELOC]; 90 | } BuildCtx; 91 | 92 | extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz); 93 | extern void emit_asm(BuildCtx *ctx); 94 | extern void emit_peobj(BuildCtx *ctx); 95 | extern void emit_lib(BuildCtx *ctx); 96 | extern void emit_fold(BuildCtx *ctx); 97 | 98 | extern const char *const bc_names[]; 99 | extern const char *const ir_names[]; 100 | extern const char *const irt_names[]; 101 | extern const char *const irfpm_names[]; 102 | extern const char *const irfield_names[]; 103 | extern const char *const ircall_names[]; 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/luajit/src/host/genversion.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- Lua script to embed the rolling release version in luajit.h. 3 | ---------------------------------------------------------------------------- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | 8 | local arg = {...} 9 | local FILE_ROLLING_H = arg[1] or "luajit_rolling.h" 10 | local FILE_RELVER_TXT = arg[2] or "luajit_relver.txt" 11 | local FILE_LUAJIT_H = arg[3] or "luajit.h" 12 | 13 | local function file_read(file) 14 | local fp = assert(io.open(file, "rb"), "run from the wrong directory") 15 | local data = assert(fp:read("*a")) 16 | fp:close() 17 | return data 18 | end 19 | 20 | local function file_write_mod(file, data) 21 | local fp = io.open(file, "rb") 22 | if fp then 23 | local odata = assert(fp:read("*a")) 24 | fp:close() 25 | if odata == data then return end 26 | end 27 | fp = assert(io.open(file, "wb")) 28 | assert(fp:write(data)) 29 | assert(fp:close()) 30 | end 31 | 32 | local text = file_read(FILE_ROLLING_H):gsub("#error.-\n", "") 33 | local relver = file_read(FILE_RELVER_TXT):match("(%d+)") 34 | 35 | if relver then 36 | text = text:gsub("ROLLING", relver) 37 | else 38 | io.stderr:write([[ 39 | **** WARNING Cannot determine rolling release version from git log. 40 | **** WARNING The 'git' command must be available during the build. 41 | ]]) 42 | file_write_mod(FILE_RELVER_TXT, "ROLLING\n") -- Fallback for install target. 43 | end 44 | 45 | file_write_mod(FILE_LUAJIT_H, text) 46 | -------------------------------------------------------------------------------- /src/luajit/src/jit/.gitignore: -------------------------------------------------------------------------------- 1 | vmdef.lua 2 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_arm64be.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT ARM64BE disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- ARM64 instructions are always little-endian. So just forward to the 8 | -- common ARM64 disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | return require((string.match(..., ".*%.") or "").."dis_arm64") 12 | 13 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_mips64.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT MIPS64 disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- This module just exports the big-endian functions from the 8 | -- MIPS disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") 12 | return { 13 | create = dis_mips.create, 14 | disass = dis_mips.disass, 15 | regname = dis_mips.regname 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_mips64el.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT MIPS64EL disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- This module just exports the little-endian functions from the 8 | -- MIPS disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") 12 | return { 13 | create = dis_mips.create_el, 14 | disass = dis_mips.disass_el, 15 | regname = dis_mips.regname 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_mips64r6.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT MIPS64R6 disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- This module just exports the r6 big-endian functions from the 8 | -- MIPS disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") 12 | return { 13 | create = dis_mips.create_r6, 14 | disass = dis_mips.disass_r6, 15 | regname = dis_mips.regname 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_mips64r6el.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT MIPS64R6EL disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- This module just exports the r6 little-endian functions from the 8 | -- MIPS disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") 12 | return { 13 | create = dis_mips.create_r6_el, 14 | disass = dis_mips.disass_r6_el, 15 | regname = dis_mips.regname 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_mipsel.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT MIPSEL disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- This module just exports the little-endian functions from the 8 | -- MIPS disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") 12 | return { 13 | create = dis_mips.create_el, 14 | disass = dis_mips.disass_el, 15 | regname = dis_mips.regname 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/luajit/src/jit/dis_x64.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT x64 disassembler wrapper module. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- This module just exports the 64 bit functions from the combined 8 | -- x86/x64 disassembler module. All the interesting stuff is there. 9 | ------------------------------------------------------------------------------ 10 | 11 | local dis_x86 = require((string.match(..., ".*%.") or "").."dis_x86") 12 | return { 13 | create = dis_x86.create64, 14 | disass = dis_x86.disass64, 15 | regname = dis_x86.regname64 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/luajit/src/jit/zone.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | -- LuaJIT profiler zones. 3 | -- 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | -- Released under the MIT license. See Copyright Notice in luajit.h 6 | ---------------------------------------------------------------------------- 7 | -- 8 | -- This module implements a simple hierarchical zone model. 9 | -- 10 | -- Example usage: 11 | -- 12 | -- local zone = require("jit.zone") 13 | -- zone("AI") 14 | -- ... 15 | -- zone("A*") 16 | -- ... 17 | -- print(zone:get()) --> "A*" 18 | -- ... 19 | -- zone() 20 | -- ... 21 | -- print(zone:get()) --> "AI" 22 | -- ... 23 | -- zone() 24 | -- 25 | ---------------------------------------------------------------------------- 26 | 27 | local remove = table.remove 28 | 29 | return setmetatable({ 30 | flush = function(t) 31 | for i=#t,1,-1 do t[i] = nil end 32 | end, 33 | get = function(t) 34 | return t[#t] 35 | end 36 | }, { 37 | __call = function(t, zone) 38 | if zone then 39 | t[#t+1] = zone 40 | else 41 | return (assert(remove(t), "empty zone stack")) 42 | end 43 | end 44 | }) 45 | 46 | -------------------------------------------------------------------------------- /src/luajit/src/lib_init.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Library initialization. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | ** 5 | ** Major parts taken verbatim from the Lua interpreter. 6 | ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h 7 | */ 8 | 9 | #define lib_init_c 10 | #define LUA_LIB 11 | 12 | #include "lua.h" 13 | #include "lauxlib.h" 14 | #include "lualib.h" 15 | 16 | #include "lj_arch.h" 17 | 18 | static const luaL_Reg lj_lib_load[] = { 19 | { "", luaopen_base }, 20 | { LUA_LOADLIBNAME, luaopen_package }, 21 | { LUA_TABLIBNAME, luaopen_table }, 22 | { LUA_IOLIBNAME, luaopen_io }, 23 | { LUA_OSLIBNAME, luaopen_os }, 24 | { LUA_STRLIBNAME, luaopen_string }, 25 | { LUA_MATHLIBNAME, luaopen_math }, 26 | { LUA_DBLIBNAME, luaopen_debug }, 27 | { LUA_BITLIBNAME, luaopen_bit }, 28 | { LUA_JITLIBNAME, luaopen_jit }, 29 | { NULL, NULL } 30 | }; 31 | 32 | static const luaL_Reg lj_lib_preload[] = { 33 | #if LJ_HASFFI 34 | { LUA_FFILIBNAME, luaopen_ffi }, 35 | #endif 36 | { NULL, NULL } 37 | }; 38 | 39 | LUALIB_API void luaL_openlibs(lua_State *L) 40 | { 41 | const luaL_Reg *lib; 42 | for (lib = lj_lib_load; lib->func; lib++) { 43 | lua_pushcfunction(L, lib->func); 44 | lua_pushstring(L, lib->name); 45 | lua_call(L, 1, 0); 46 | } 47 | luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 48 | sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1); 49 | for (lib = lj_lib_preload; lib->func; lib++) { 50 | lua_pushcfunction(L, lib->func); 51 | lua_setfield(L, -2, lib->name); 52 | } 53 | lua_pop(L, 1); 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/luajit/src/lj_alloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Bundled memory allocator. 3 | ** Donated to the public domain. 4 | */ 5 | 6 | #ifndef _LJ_ALLOC_H 7 | #define _LJ_ALLOC_H 8 | 9 | #include "lj_def.h" 10 | 11 | #ifndef LUAJIT_USE_SYSMALLOC 12 | LJ_FUNC void *lj_alloc_create(PRNGState *rs); 13 | LJ_FUNC void lj_alloc_setprng(void *msp, PRNGState *rs); 14 | LJ_FUNC void lj_alloc_destroy(void *msp); 15 | LJ_FUNC void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/luajit/src/lj_asm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** IR assembler (SSA IR -> machine code). 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_ASM_H 7 | #define _LJ_ASM_H 8 | 9 | #include "lj_jit.h" 10 | 11 | #if LJ_HASJIT 12 | LJ_FUNC void lj_asm_trace(jit_State *J, GCtrace *T); 13 | LJ_FUNC void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, 14 | MCode *target); 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /src/luajit/src/lj_assert.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Internal assertions. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define lj_assert_c 7 | #define LUA_CORE 8 | 9 | #if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) 10 | 11 | #include 12 | 13 | #include "lj_obj.h" 14 | 15 | void lj_assert_fail(global_State *g, const char *file, int line, 16 | const char *func, const char *fmt, ...) 17 | { 18 | va_list argp; 19 | va_start(argp, fmt); 20 | fprintf(stderr, "LuaJIT ASSERT %s:%d: %s: ", file, line, func); 21 | vfprintf(stderr, fmt, argp); 22 | fputc('\n', stderr); 23 | va_end(argp); 24 | UNUSED(g); /* May be NULL. TODO: optionally dump state. */ 25 | abort(); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/luajit/src/lj_bc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Bytecode instruction modes. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define lj_bc_c 7 | #define LUA_CORE 8 | 9 | #include "lj_obj.h" 10 | #include "lj_bc.h" 11 | 12 | /* Bytecode offsets and bytecode instruction modes. */ 13 | #include "lj_bcdef.h" 14 | 15 | -------------------------------------------------------------------------------- /src/luajit/src/lj_bcdump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Bytecode dump definitions. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_BCDUMP_H 7 | #define _LJ_BCDUMP_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_lex.h" 11 | 12 | /* -- Bytecode dump format ------------------------------------------------ */ 13 | 14 | /* 15 | ** dump = header proto+ 0U 16 | ** header = ESC 'L' 'J' versionB flagsU [namelenU nameB*] 17 | ** proto = lengthU pdata 18 | ** pdata = phead bcinsW* uvdataH* kgc* knum* [debugB*] 19 | ** phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU 20 | ** [debuglenU [firstlineU numlineU]] 21 | ** kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* } 22 | ** knum = intU0 | (loU1 hiU) 23 | ** ktab = narrayU nhashU karray* khash* 24 | ** karray = ktabk 25 | ** khash = ktabk ktabk 26 | ** ktabk = ktabtypeU { intU | (loU hiU) | strB* } 27 | ** 28 | ** B = 8 bit, H = 16 bit, W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1 29 | */ 30 | 31 | /* Bytecode dump header. */ 32 | #define BCDUMP_HEAD1 0x1b 33 | #define BCDUMP_HEAD2 0x4c 34 | #define BCDUMP_HEAD3 0x4a 35 | 36 | /* If you perform *any* kind of private modifications to the bytecode itself 37 | ** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher. 38 | */ 39 | #define BCDUMP_VERSION 2 40 | 41 | /* Compatibility flags. */ 42 | #define BCDUMP_F_BE 0x01 43 | #define BCDUMP_F_STRIP 0x02 44 | #define BCDUMP_F_FFI 0x04 45 | #define BCDUMP_F_FR2 0x08 46 | 47 | #define BCDUMP_F_KNOWN (BCDUMP_F_FR2*2-1) 48 | 49 | #define BCDUMP_F_DETERMINISTIC 0x80000000 50 | 51 | /* Type codes for the GC constants of a prototype. Plus length for strings. */ 52 | enum { 53 | BCDUMP_KGC_CHILD, BCDUMP_KGC_TAB, BCDUMP_KGC_I64, BCDUMP_KGC_U64, 54 | BCDUMP_KGC_COMPLEX, BCDUMP_KGC_STR 55 | }; 56 | 57 | /* Type codes for the keys/values of a constant table. */ 58 | enum { 59 | BCDUMP_KTAB_NIL, BCDUMP_KTAB_FALSE, BCDUMP_KTAB_TRUE, 60 | BCDUMP_KTAB_INT, BCDUMP_KTAB_NUM, BCDUMP_KTAB_STR 61 | }; 62 | 63 | /* -- Bytecode reader/writer ---------------------------------------------- */ 64 | 65 | LJ_FUNC int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, 66 | void *data, uint32_t flags); 67 | LJ_FUNC GCproto *lj_bcread_proto(LexState *ls); 68 | LJ_FUNC GCproto *lj_bcread(LexState *ls); 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /src/luajit/src/lj_carith.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** C data arithmetic. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CARITH_H 7 | #define _LJ_CARITH_H 8 | 9 | #include "lj_obj.h" 10 | 11 | #if LJ_HASFFI 12 | 13 | LJ_FUNC int lj_carith_op(lua_State *L, MMS mm); 14 | 15 | #if LJ_32 16 | LJ_FUNC uint64_t lj_carith_shl64(uint64_t x, int32_t sh); 17 | LJ_FUNC uint64_t lj_carith_shr64(uint64_t x, int32_t sh); 18 | LJ_FUNC uint64_t lj_carith_sar64(uint64_t x, int32_t sh); 19 | LJ_FUNC uint64_t lj_carith_rol64(uint64_t x, int32_t sh); 20 | LJ_FUNC uint64_t lj_carith_ror64(uint64_t x, int32_t sh); 21 | #endif 22 | LJ_FUNC uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op); 23 | LJ_FUNC uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id); 24 | 25 | #if LJ_32 && LJ_HASJIT 26 | LJ_FUNC int64_t lj_carith_mul64(int64_t x, int64_t k); 27 | #endif 28 | LJ_FUNC uint64_t lj_carith_divu64(uint64_t a, uint64_t b); 29 | LJ_FUNC int64_t lj_carith_divi64(int64_t a, int64_t b); 30 | LJ_FUNC uint64_t lj_carith_modu64(uint64_t a, uint64_t b); 31 | LJ_FUNC int64_t lj_carith_modi64(int64_t a, int64_t b); 32 | LJ_FUNC uint64_t lj_carith_powu64(uint64_t x, uint64_t k); 33 | LJ_FUNC int64_t lj_carith_powi64(int64_t x, int64_t k); 34 | 35 | #endif 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /src/luajit/src/lj_ccallback.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** FFI C callback handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CCALLBACK_H 7 | #define _LJ_CCALLBACK_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_ctype.h" 11 | 12 | #if LJ_HASFFI 13 | 14 | /* Really belongs to lj_vm.h. */ 15 | LJ_ASMF void lj_vm_ffi_callback(void); 16 | 17 | LJ_FUNC MSize lj_ccallback_ptr2slot(CTState *cts, void *p); 18 | LJ_FUNCA lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf); 19 | LJ_FUNCA void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o); 20 | LJ_FUNC void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn); 21 | LJ_FUNC void lj_ccallback_mcode_free(CTState *cts); 22 | 23 | #endif 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/luajit/src/lj_cconv.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** C type conversions. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CCONV_H 7 | #define _LJ_CCONV_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_ctype.h" 11 | 12 | #if LJ_HASFFI 13 | 14 | /* Compressed C type index. ORDER CCX. */ 15 | enum { 16 | CCX_B, /* Bool. */ 17 | CCX_I, /* Integer. */ 18 | CCX_F, /* Floating-point number. */ 19 | CCX_C, /* Complex. */ 20 | CCX_V, /* Vector. */ 21 | CCX_P, /* Pointer. */ 22 | CCX_A, /* Refarray. */ 23 | CCX_S /* Struct/union. */ 24 | }; 25 | 26 | /* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */ 27 | static LJ_AINLINE uint32_t cconv_idx(CTInfo info) 28 | { 29 | uint32_t idx = ((info >> 26) & 15u); /* Dispatch bits. */ 30 | lj_assertX(ctype_type(info) <= CT_MAYCONVERT, 31 | "cannot convert ctype %08x", info); 32 | #if LJ_64 33 | idx = ((uint32_t)(U64x(f436fff5,fff7f021) >> 4*idx) & 15u); 34 | #else 35 | idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u); 36 | #endif 37 | lj_assertX(idx < 8, "cannot convert ctype %08x", info); 38 | return idx; 39 | } 40 | 41 | #define cconv_idx2(dinfo, sinfo) \ 42 | ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo))) 43 | 44 | #define CCX(dst, src) ((CCX_##dst << 3) + CCX_##src) 45 | 46 | /* Conversion flags. */ 47 | #define CCF_CAST 0x00000001u 48 | #define CCF_FROMTV 0x00000002u 49 | #define CCF_SAME 0x00000004u 50 | #define CCF_IGNQUAL 0x00000008u 51 | 52 | #define CCF_ARG_SHIFT 8 53 | #define CCF_ARG(n) ((n) << CCF_ARG_SHIFT) 54 | #define CCF_GETARG(f) ((f) >> CCF_ARG_SHIFT) 55 | 56 | LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags); 57 | LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, 58 | uint8_t *dp, uint8_t *sp, CTInfo flags); 59 | LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, 60 | TValue *o, uint8_t *sp); 61 | LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp); 62 | LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d, 63 | uint8_t *dp, TValue *o, CTInfo flags); 64 | LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o); 65 | LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o); 66 | LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, 67 | uint8_t *dp, TValue *o, MSize len); 68 | 69 | #endif 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /src/luajit/src/lj_cdata.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** C data management. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CDATA_H 7 | #define _LJ_CDATA_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_gc.h" 11 | #include "lj_ctype.h" 12 | 13 | #if LJ_HASFFI 14 | 15 | /* Get C data pointer. */ 16 | static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz) 17 | { 18 | if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ 19 | return ((void *)(uintptr_t)*(uint32_t *)p); 20 | } else { 21 | lj_assertX(sz == CTSIZE_PTR, "bad pointer size %d", sz); 22 | return *(void **)p; 23 | } 24 | } 25 | 26 | /* Set C data pointer. */ 27 | static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v) 28 | { 29 | if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ 30 | *(uint32_t *)p = (uint32_t)(uintptr_t)v; 31 | } else { 32 | lj_assertX(sz == CTSIZE_PTR, "bad pointer size %d", sz); 33 | *(void **)p = (void *)v; 34 | } 35 | } 36 | 37 | /* Allocate fixed-size C data object. */ 38 | static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz) 39 | { 40 | GCcdata *cd; 41 | #ifdef LUA_USE_ASSERT 42 | CType *ct = ctype_raw(cts, id); 43 | lj_assertCTS((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz, 44 | "inconsistent size of fixed-size cdata alloc"); 45 | #endif 46 | cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz); 47 | cd->gct = ~LJ_TCDATA; 48 | cd->ctypeid = ctype_check(cts, id); 49 | return cd; 50 | } 51 | 52 | /* Variant which works without a valid CTState. */ 53 | static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz) 54 | { 55 | GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz); 56 | cd->gct = ~LJ_TCDATA; 57 | cd->ctypeid = id; 58 | return cd; 59 | } 60 | 61 | LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id); 62 | LJ_FUNC GCcdata *lj_cdata_newv(lua_State *L, CTypeID id, CTSize sz, 63 | CTSize align); 64 | LJ_FUNC GCcdata *lj_cdata_newx(CTState *cts, CTypeID id, CTSize sz, 65 | CTInfo info); 66 | 67 | LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd); 68 | LJ_FUNC void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj, 69 | uint32_t it); 70 | 71 | LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, 72 | uint8_t **pp, CTInfo *qual); 73 | LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp); 74 | LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, 75 | CTInfo qual); 76 | 77 | #endif 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/luajit/src/lj_char.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Character types. 3 | ** Donated to the public domain. 4 | ** 5 | ** This is intended to replace the problematic libc single-byte NLS functions. 6 | ** These just don't make sense anymore with UTF-8 locales becoming the norm 7 | ** on POSIX systems. It never worked too well on Windows systems since hardly 8 | ** anyone bothered to call setlocale(). 9 | ** 10 | ** This table is hardcoded for ASCII. Identifiers include the characters 11 | ** 128-255, too. This allows for the use of all non-ASCII chars as identifiers 12 | ** in the lexer. This is a broad definition, but works well in practice 13 | ** for both UTF-8 locales and most single-byte locales (such as ISO-8859-*). 14 | ** 15 | ** If you really need proper character types for UTF-8 strings, please use 16 | ** an add-on library such as slnunicode: http://luaforge.net/projects/sln/ 17 | */ 18 | 19 | #define lj_char_c 20 | #define LUA_CORE 21 | 22 | #include "lj_char.h" 23 | 24 | LJ_DATADEF const uint8_t lj_char_bits[257] = { 25 | 0, 26 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, 27 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28 | 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 29 | 152,152,152,152,152,152,152,152,152,152, 4, 4, 4, 4, 4, 4, 30 | 4,176,176,176,176,176,176,160,160,160,160,160,160,160,160,160, 31 | 160,160,160,160,160,160,160,160,160,160,160, 4, 4, 4, 4,132, 32 | 4,208,208,208,208,208,208,192,192,192,192,192,192,192,192,192, 33 | 192,192,192,192,192,192,192,192,192,192,192, 4, 4, 4, 4, 1, 34 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 35 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 36 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 37 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 38 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 39 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 40 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, 41 | 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /src/luajit/src/lj_char.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Character types. 3 | ** Donated to the public domain. 4 | */ 5 | 6 | #ifndef _LJ_CHAR_H 7 | #define _LJ_CHAR_H 8 | 9 | #include "lj_def.h" 10 | 11 | #define LJ_CHAR_CNTRL 0x01 12 | #define LJ_CHAR_SPACE 0x02 13 | #define LJ_CHAR_PUNCT 0x04 14 | #define LJ_CHAR_DIGIT 0x08 15 | #define LJ_CHAR_XDIGIT 0x10 16 | #define LJ_CHAR_UPPER 0x20 17 | #define LJ_CHAR_LOWER 0x40 18 | #define LJ_CHAR_IDENT 0x80 19 | #define LJ_CHAR_ALPHA (LJ_CHAR_LOWER|LJ_CHAR_UPPER) 20 | #define LJ_CHAR_ALNUM (LJ_CHAR_ALPHA|LJ_CHAR_DIGIT) 21 | #define LJ_CHAR_GRAPH (LJ_CHAR_ALNUM|LJ_CHAR_PUNCT) 22 | 23 | /* Only pass -1 or 0..255 to these macros. Never pass a signed char! */ 24 | #define lj_char_isa(c, t) ((lj_char_bits+1)[(c)] & t) 25 | #define lj_char_iscntrl(c) lj_char_isa((c), LJ_CHAR_CNTRL) 26 | #define lj_char_isspace(c) lj_char_isa((c), LJ_CHAR_SPACE) 27 | #define lj_char_ispunct(c) lj_char_isa((c), LJ_CHAR_PUNCT) 28 | #define lj_char_isdigit(c) lj_char_isa((c), LJ_CHAR_DIGIT) 29 | #define lj_char_isxdigit(c) lj_char_isa((c), LJ_CHAR_XDIGIT) 30 | #define lj_char_isupper(c) lj_char_isa((c), LJ_CHAR_UPPER) 31 | #define lj_char_islower(c) lj_char_isa((c), LJ_CHAR_LOWER) 32 | #define lj_char_isident(c) lj_char_isa((c), LJ_CHAR_IDENT) 33 | #define lj_char_isalpha(c) lj_char_isa((c), LJ_CHAR_ALPHA) 34 | #define lj_char_isalnum(c) lj_char_isa((c), LJ_CHAR_ALNUM) 35 | #define lj_char_isgraph(c) lj_char_isa((c), LJ_CHAR_GRAPH) 36 | 37 | #define lj_char_toupper(c) ((c) - (lj_char_islower(c) >> 1)) 38 | #define lj_char_tolower(c) ((c) + lj_char_isupper(c)) 39 | 40 | LJ_DATA const uint8_t lj_char_bits[257]; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/luajit/src/lj_clib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** FFI C library loader. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CLIB_H 7 | #define _LJ_CLIB_H 8 | 9 | #include "lj_obj.h" 10 | 11 | #if LJ_HASFFI 12 | 13 | /* Namespace for C library indexing. */ 14 | #define CLNS_INDEX ((1u<env. */ 20 | } CLibrary; 21 | 22 | LJ_FUNC TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name); 23 | LJ_FUNC void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global); 24 | LJ_FUNC void lj_clib_unload(CLibrary *cl); 25 | LJ_FUNC void lj_clib_default(lua_State *L, GCtab *mt); 26 | 27 | #endif 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/luajit/src/lj_cparse.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** C declaration parser. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CPARSE_H 7 | #define _LJ_CPARSE_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_ctype.h" 11 | 12 | #if LJ_HASFFI 13 | 14 | /* C parser limits. */ 15 | #define CPARSE_MAX_BUF 32768 /* Max. token buffer size. */ 16 | #define CPARSE_MAX_DECLSTACK 100 /* Max. declaration stack depth. */ 17 | #define CPARSE_MAX_DECLDEPTH 20 /* Max. recursive declaration depth. */ 18 | #define CPARSE_MAX_PACKSTACK 7 /* Max. pack pragma stack depth. */ 19 | 20 | /* Flags for C parser mode. */ 21 | #define CPARSE_MODE_MULTI 1 /* Process multiple declarations. */ 22 | #define CPARSE_MODE_ABSTRACT 2 /* Accept abstract declarators. */ 23 | #define CPARSE_MODE_DIRECT 4 /* Accept direct declarators. */ 24 | #define CPARSE_MODE_FIELD 8 /* Accept field width in bits, too. */ 25 | #define CPARSE_MODE_NOIMPLICIT 16 /* Reject implicit declarations. */ 26 | #define CPARSE_MODE_SKIP 32 /* Skip definitions, ignore errors. */ 27 | 28 | typedef int CPChar; /* C parser character. Unsigned ext. from char. */ 29 | typedef int CPToken; /* C parser token. */ 30 | 31 | /* C parser internal value representation. */ 32 | typedef struct CPValue { 33 | union { 34 | int32_t i32; /* Value for CTID_INT32. */ 35 | uint32_t u32; /* Value for CTID_UINT32. */ 36 | }; 37 | CTypeID id; /* C Type ID of the value. */ 38 | } CPValue; 39 | 40 | /* C parser state. */ 41 | typedef struct CPState { 42 | CPChar c; /* Current character. */ 43 | CPToken tok; /* Current token. */ 44 | CPValue val; /* Token value. */ 45 | GCstr *str; /* Interned string of identifier/keyword. */ 46 | CType *ct; /* C type table entry. */ 47 | const char *p; /* Current position in input buffer. */ 48 | SBuf sb; /* String buffer for tokens. */ 49 | lua_State *L; /* Lua state. */ 50 | CTState *cts; /* C type state. */ 51 | TValue *param; /* C type parameters. */ 52 | const char *srcname; /* Current source name. */ 53 | BCLine linenumber; /* Input line counter. */ 54 | int depth; /* Recursive declaration depth. */ 55 | uint32_t tmask; /* Type mask for next identifier. */ 56 | uint32_t mode; /* C parser mode. */ 57 | uint8_t packstack[CPARSE_MAX_PACKSTACK]; /* Stack for pack pragmas. */ 58 | uint8_t curpack; /* Current position in pack pragma stack. */ 59 | } CPState; 60 | 61 | LJ_FUNC int lj_cparse(CPState *cp); 62 | 63 | LJ_FUNC int lj_cparse_case(GCstr *str, const char *match); 64 | 65 | #endif 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/luajit/src/lj_crecord.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Trace recorder for C data operations. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_CRECORD_H 7 | #define _LJ_CRECORD_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_jit.h" 11 | #include "lj_ffrecord.h" 12 | 13 | #if LJ_HASJIT && LJ_HASFFI 14 | LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd); 15 | LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd); 16 | LJ_FUNC void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd); 17 | LJ_FUNC void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd); 18 | LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd); 19 | LJ_FUNC void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd); 20 | LJ_FUNC void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd); 21 | LJ_FUNC void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd); 22 | LJ_FUNC void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd); 23 | LJ_FUNC void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd); 24 | LJ_FUNC void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd); 25 | LJ_FUNC void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd); 26 | LJ_FUNC void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd); 27 | LJ_FUNC void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd); 28 | 29 | LJ_FUNC void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd); 30 | LJ_FUNC int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd); 31 | LJ_FUNC int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd); 32 | LJ_FUNC int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd); 33 | LJ_FUNC TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr); 34 | 35 | LJ_FUNC void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd); 36 | LJ_FUNC TRef lj_crecord_loadiu64(jit_State *J, TRef tr, cTValue *o); 37 | #if LJ_HASBUFFER 38 | LJ_FUNC TRef lj_crecord_topcvoid(jit_State *J, TRef tr, cTValue *o); 39 | LJ_FUNC TRef lj_crecord_topuint8(jit_State *J, TRef tr); 40 | #endif 41 | #endif 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/luajit/src/lj_debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Debugging and introspection. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_DEBUG_H 7 | #define _LJ_DEBUG_H 8 | 9 | #include "lj_obj.h" 10 | 11 | typedef struct lj_Debug { 12 | /* Common fields. Must be in the same order as in lua.h. */ 13 | int event; 14 | const char *name; 15 | const char *namewhat; 16 | const char *what; 17 | const char *source; 18 | int currentline; 19 | int nups; 20 | int linedefined; 21 | int lastlinedefined; 22 | char short_src[LUA_IDSIZE]; 23 | int i_ci; 24 | /* Extended fields. Only valid if lj_debug_getinfo() is called with ext = 1.*/ 25 | int nparams; 26 | int isvararg; 27 | } lj_Debug; 28 | 29 | LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size); 30 | LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc); 31 | LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx); 32 | LJ_FUNC const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp, 33 | GCobj **op); 34 | LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc, 35 | BCReg slot, const char **name); 36 | LJ_FUNC const char *lj_debug_funcname(lua_State *L, cTValue *frame, 37 | const char **name); 38 | LJ_FUNC void lj_debug_shortname(char *out, GCstr *str, BCLine line); 39 | LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg, 40 | cTValue *frame, cTValue *nextframe); 41 | LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc); 42 | LJ_FUNC int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, 43 | int ext); 44 | #if LJ_HASPROFILE 45 | LJ_FUNC void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt, 46 | int depth); 47 | #endif 48 | 49 | /* Fixed internal variable names. */ 50 | #define VARNAMEDEF(_) \ 51 | _(FOR_IDX, "(for index)") \ 52 | _(FOR_STOP, "(for limit)") \ 53 | _(FOR_STEP, "(for step)") \ 54 | _(FOR_GEN, "(for generator)") \ 55 | _(FOR_STATE, "(for state)") \ 56 | _(FOR_CTL, "(for control)") 57 | 58 | enum { 59 | VARNAME_END, 60 | #define VARNAMEENUM(name, str) VARNAME_##name, 61 | VARNAMEDEF(VARNAMEENUM) 62 | #undef VARNAMEENUM 63 | VARNAME__MAX 64 | }; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /src/luajit/src/lj_err.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Error handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_ERR_H 7 | #define _LJ_ERR_H 8 | 9 | #include 10 | 11 | #include "lj_obj.h" 12 | 13 | typedef enum { 14 | #define ERRDEF(name, msg) \ 15 | LJ_ERR_##name, LJ_ERR_##name##_ = LJ_ERR_##name + sizeof(msg)-1, 16 | #include "lj_errmsg.h" 17 | LJ_ERR__MAX 18 | } ErrMsg; 19 | 20 | LJ_DATA const char *lj_err_allmsg; 21 | #define err2msg(em) (lj_err_allmsg+(int)(em)) 22 | 23 | LJ_FUNC GCstr *lj_err_str(lua_State *L, ErrMsg em); 24 | LJ_FUNCA_NORET void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode); 25 | LJ_FUNC_NORET void lj_err_mem(lua_State *L); 26 | LJ_FUNC_NORET void LJ_FASTCALL lj_err_stkov(lua_State *L); 27 | LJ_FUNC_NORET void LJ_FASTCALL lj_err_run(lua_State *L); 28 | #if LJ_HASJIT 29 | LJ_FUNCA_NORET void LJ_FASTCALL lj_err_trace(lua_State *L, int errcode); 30 | #endif 31 | LJ_FUNC_NORET void lj_err_msg(lua_State *L, ErrMsg em); 32 | LJ_FUNC_NORET void lj_err_lex(lua_State *L, GCstr *src, const char *tok, 33 | BCLine line, ErrMsg em, va_list argp); 34 | LJ_FUNC_NORET void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm); 35 | LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2); 36 | LJ_FUNC_NORET void lj_err_optype_call(lua_State *L, TValue *o); 37 | LJ_FUNC_NORET void lj_err_callermsg(lua_State *L, const char *msg); 38 | LJ_FUNC_NORET void lj_err_callerv(lua_State *L, ErrMsg em, ...); 39 | LJ_FUNC_NORET void lj_err_caller(lua_State *L, ErrMsg em); 40 | LJ_FUNC_NORET void lj_err_arg(lua_State *L, int narg, ErrMsg em); 41 | LJ_FUNC_NORET void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...); 42 | LJ_FUNC_NORET void lj_err_argtype(lua_State *L, int narg, const char *xname); 43 | LJ_FUNC_NORET void lj_err_argt(lua_State *L, int narg, int tt); 44 | 45 | #if LJ_UNWIND_JIT && !LJ_ABI_WIN 46 | LJ_FUNC uint8_t *lj_err_register_mcode(void *base, size_t sz, uint8_t *info); 47 | LJ_FUNC void lj_err_deregister_mcode(void *base, size_t sz, uint8_t *info); 48 | #else 49 | #define lj_err_register_mcode(base, sz, info) (info) 50 | #define lj_err_deregister_mcode(base, sz, info) UNUSED(base) 51 | #endif 52 | 53 | #if LJ_UNWIND_EXT && !LJ_ABI_WIN && defined(LUA_USE_ASSERT) 54 | LJ_FUNC void lj_err_verify(void); 55 | #else 56 | #define lj_err_verify() ((void)0) 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/luajit/src/lj_ff.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Fast function IDs. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_FF_H 7 | #define _LJ_FF_H 8 | 9 | /* Fast function ID. */ 10 | typedef enum { 11 | FF_LUA_ = FF_LUA, /* Lua function (must be 0). */ 12 | FF_C_ = FF_C, /* Regular C function (must be 1). */ 13 | #define FFDEF(name) FF_##name, 14 | #include "lj_ffdef.h" 15 | FF__MAX 16 | } FastFunc; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/luajit/src/lj_ffrecord.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Fast function call recorder. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_FFRECORD_H 7 | #define _LJ_FFRECORD_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_jit.h" 11 | 12 | #if LJ_HASJIT 13 | /* Data used by handlers to record a fast function. */ 14 | typedef struct RecordFFData { 15 | TValue *argv; /* Runtime argument values. */ 16 | ptrdiff_t nres; /* Number of returned results (defaults to 1). */ 17 | uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */ 18 | } RecordFFData; 19 | 20 | LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv); 21 | LJ_FUNC void lj_ffrecord_func(jit_State *J); 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/luajit/src/lj_func.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Function handling (prototypes, functions and upvalues). 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_FUNC_H 7 | #define _LJ_FUNC_H 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Prototypes. */ 12 | LJ_FUNC void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt); 13 | 14 | /* Upvalues. */ 15 | LJ_FUNCA void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level); 16 | LJ_FUNC void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv); 17 | 18 | /* Functions (closures). */ 19 | LJ_FUNC GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env); 20 | LJ_FUNC GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env); 21 | LJ_FUNCA GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent); 22 | LJ_FUNC void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *c); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/luajit/src/lj_gdbjit.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Client for the GDB JIT API. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_GDBJIT_H 7 | #define _LJ_GDBJIT_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_jit.h" 11 | 12 | #if LJ_HASJIT && defined(LUAJIT_USE_GDBJIT) 13 | 14 | LJ_FUNC void lj_gdbjit_addtrace(jit_State *J, GCtrace *T); 15 | LJ_FUNC void lj_gdbjit_deltrace(jit_State *J, GCtrace *T); 16 | 17 | #else 18 | #define lj_gdbjit_addtrace(J, T) UNUSED(T) 19 | #define lj_gdbjit_deltrace(J, T) UNUSED(T) 20 | #endif 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/luajit/src/lj_lex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Lexical analyzer. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_LEX_H 7 | #define _LJ_LEX_H 8 | 9 | #include 10 | 11 | #include "lj_obj.h" 12 | #include "lj_err.h" 13 | 14 | /* Lua lexer tokens. */ 15 | #define TKDEF(_, __) \ 16 | _(and) _(break) _(do) _(else) _(elseif) _(end) _(false) \ 17 | _(for) _(function) _(goto) _(if) _(in) _(local) _(nil) _(not) _(or) \ 18 | _(repeat) _(return) _(then) _(true) _(until) _(while) \ 19 | __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) __(ne, ~=) \ 20 | __(label, ::) __(number, ) __(name, ) __(string, ) \ 21 | __(eof, ) 22 | 23 | enum { 24 | TK_OFS = 256, 25 | #define TKENUM1(name) TK_##name, 26 | #define TKENUM2(name, sym) TK_##name, 27 | TKDEF(TKENUM1, TKENUM2) 28 | #undef TKENUM1 29 | #undef TKENUM2 30 | TK_RESERVED = TK_while - TK_OFS 31 | }; 32 | 33 | typedef int LexChar; /* Lexical character. Unsigned ext. from char. */ 34 | typedef int LexToken; /* Lexical token. */ 35 | 36 | /* Combined bytecode ins/line. Only used during bytecode generation. */ 37 | typedef struct BCInsLine { 38 | BCIns ins; /* Bytecode instruction. */ 39 | BCLine line; /* Line number for this bytecode. */ 40 | } BCInsLine; 41 | 42 | /* Info for local variables. Only used during bytecode generation. */ 43 | typedef struct VarInfo { 44 | GCRef name; /* Local variable name or goto/label name. */ 45 | BCPos startpc; /* First point where the local variable is active. */ 46 | BCPos endpc; /* First point where the local variable is dead. */ 47 | uint8_t slot; /* Variable slot. */ 48 | uint8_t info; /* Variable/goto/label info. */ 49 | } VarInfo; 50 | 51 | /* Lua lexer state. */ 52 | typedef struct LexState { 53 | struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */ 54 | struct lua_State *L; /* Lua state. */ 55 | TValue tokval; /* Current token value. */ 56 | TValue lookaheadval; /* Lookahead token value. */ 57 | const char *p; /* Current position in input buffer. */ 58 | const char *pe; /* End of input buffer. */ 59 | LexChar c; /* Current character. */ 60 | LexToken tok; /* Current token. */ 61 | LexToken lookahead; /* Lookahead token. */ 62 | SBuf sb; /* String buffer for tokens. */ 63 | lua_Reader rfunc; /* Reader callback. */ 64 | void *rdata; /* Reader callback data. */ 65 | BCLine linenumber; /* Input line counter. */ 66 | BCLine lastline; /* Line of last token. */ 67 | GCstr *chunkname; /* Current chunk name (interned string). */ 68 | const char *chunkarg; /* Chunk name argument. */ 69 | const char *mode; /* Allow loading bytecode (b) and/or source text (t). */ 70 | VarInfo *vstack; /* Stack for names and extents of local variables. */ 71 | MSize sizevstack; /* Size of variable stack. */ 72 | MSize vtop; /* Top of variable stack. */ 73 | BCInsLine *bcstack; /* Stack for bytecode instructions/line numbers. */ 74 | MSize sizebcstack; /* Size of bytecode stack. */ 75 | uint32_t level; /* Syntactical nesting level. */ 76 | int endmark; /* Trust bytecode end marker, even if not at EOF. */ 77 | int fr2; /* Generate bytecode for LJ_FR2 mode. */ 78 | } LexState; 79 | 80 | LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls); 81 | LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls); 82 | LJ_FUNC void lj_lex_next(LexState *ls); 83 | LJ_FUNC LexToken lj_lex_lookahead(LexState *ls); 84 | LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken tok); 85 | LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...); 86 | LJ_FUNC void lj_lex_init(lua_State *L); 87 | 88 | #ifdef LUA_USE_ASSERT 89 | #define lj_assertLS(c, ...) (lj_assertG_(G(ls->L), (c), __VA_ARGS__)) 90 | #else 91 | #define lj_assertLS(c, ...) ((void)ls) 92 | #endif 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /src/luajit/src/lj_mcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Machine code management. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_MCODE_H 7 | #define _LJ_MCODE_H 8 | 9 | #include "lj_obj.h" 10 | 11 | #if LJ_HASJIT || LJ_HASFFI 12 | LJ_FUNC void lj_mcode_sync(void *start, void *end); 13 | #endif 14 | 15 | #if LJ_HASJIT 16 | 17 | #include "lj_jit.h" 18 | 19 | LJ_FUNC void lj_mcode_free(jit_State *J); 20 | LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim); 21 | LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m); 22 | LJ_FUNC void lj_mcode_abort(jit_State *J); 23 | LJ_FUNC MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish); 24 | LJ_FUNC_NORET void lj_mcode_limiterr(jit_State *J, size_t need); 25 | 26 | #define lj_mcode_commitbot(J, m) (J->mcbot = (m)) 27 | 28 | #endif 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/luajit/src/lj_meta.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Metamethod handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_META_H 7 | #define _LJ_META_H 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Metamethod handling */ 12 | LJ_FUNC void lj_meta_init(lua_State *L); 13 | LJ_FUNC cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name); 14 | LJ_FUNC cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm); 15 | #if LJ_HASFFI 16 | LJ_FUNC int lj_meta_tailcall(lua_State *L, cTValue *tv); 17 | #endif 18 | 19 | #define lj_meta_fastg(g, mt, mm) \ 20 | ((mt) == NULL ? NULL : ((mt)->nomm & (1u<<(mm))) ? NULL : \ 21 | lj_meta_cache(mt, mm, mmname_str(g, mm))) 22 | #define lj_meta_fast(L, mt, mm) lj_meta_fastg(G(L), mt, mm) 23 | 24 | /* C helpers for some instructions, called from assembler VM. */ 25 | LJ_FUNCA cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k); 26 | LJ_FUNCA TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k); 27 | LJ_FUNCA TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, 28 | cTValue *rc, BCReg op); 29 | LJ_FUNCA TValue *lj_meta_cat(lua_State *L, TValue *top, int left); 30 | LJ_FUNCA TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o); 31 | LJ_FUNCA TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne); 32 | LJ_FUNCA TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins); 33 | LJ_FUNCA TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op); 34 | LJ_FUNCA void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp); 35 | LJ_FUNCA void lj_meta_call(lua_State *L, TValue *func, TValue *top); 36 | LJ_FUNCA void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /src/luajit/src/lj_obj.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Miscellaneous object handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define lj_obj_c 7 | #define LUA_CORE 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Object type names. */ 12 | LJ_DATADEF const char *const lj_obj_typename[] = { /* ORDER LUA_T */ 13 | "no value", "nil", "boolean", "userdata", "number", "string", 14 | "table", "function", "userdata", "thread", "proto", "cdata" 15 | }; 16 | 17 | LJ_DATADEF const char *const lj_obj_itypename[] = { /* ORDER LJ_T */ 18 | "nil", "boolean", "boolean", "userdata", "string", "upval", "thread", 19 | "proto", "function", "trace", "cdata", "table", "userdata", "number" 20 | }; 21 | 22 | /* Compare two objects without calling metamethods. */ 23 | int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2) 24 | { 25 | if (itype(o1) == itype(o2)) { 26 | if (tvispri(o1)) 27 | return 1; 28 | if (!tvisnum(o1)) 29 | return gcrefeq(o1->gcr, o2->gcr); 30 | } else if (!tvisnumber(o1) || !tvisnumber(o2)) { 31 | return 0; 32 | } 33 | return numberVnum(o1) == numberVnum(o2); 34 | } 35 | 36 | /* Return pointer to object or its object data. */ 37 | const void * LJ_FASTCALL lj_obj_ptr(global_State *g, cTValue *o) 38 | { 39 | UNUSED(g); 40 | if (tvisudata(o)) 41 | return uddata(udataV(o)); 42 | else if (tvislightud(o)) 43 | return lightudV(g, o); 44 | else if (LJ_HASFFI && tviscdata(o)) 45 | return cdataptr(cdataV(o)); 46 | else if (tvisgcv(o)) 47 | return gcV(o); 48 | else 49 | return NULL; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/luajit/src/lj_opt_dce.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define lj_opt_dce_c 7 | #define LUA_CORE 8 | 9 | #include "lj_obj.h" 10 | 11 | #if LJ_HASJIT 12 | 13 | #include "lj_ir.h" 14 | #include "lj_jit.h" 15 | #include "lj_iropt.h" 16 | 17 | /* Some local macros to save typing. Undef'd at the end. */ 18 | #define IR(ref) (&J->cur.ir[(ref)]) 19 | 20 | /* Scan through all snapshots and mark all referenced instructions. */ 21 | static void dce_marksnap(jit_State *J) 22 | { 23 | SnapNo i, nsnap = J->cur.nsnap; 24 | for (i = 0; i < nsnap; i++) { 25 | SnapShot *snap = &J->cur.snap[i]; 26 | SnapEntry *map = &J->cur.snapmap[snap->mapofs]; 27 | MSize n, nent = snap->nent; 28 | for (n = 0; n < nent; n++) { 29 | IRRef ref = snap_ref(map[n]); 30 | if (ref >= REF_FIRST) 31 | irt_setmark(IR(ref)->t); 32 | } 33 | } 34 | } 35 | 36 | /* Backwards propagate marks. Replace unused instructions with NOPs. */ 37 | static void dce_propagate(jit_State *J) 38 | { 39 | IRRef1 *pchain[IR__MAX]; 40 | IRRef ins; 41 | uint32_t i; 42 | for (i = 0; i < IR__MAX; i++) pchain[i] = &J->chain[i]; 43 | for (ins = J->cur.nins-1; ins >= REF_FIRST; ins--) { 44 | IRIns *ir = IR(ins); 45 | if (irt_ismarked(ir->t)) { 46 | irt_clearmark(ir->t); 47 | } else if (!ir_sideeff(ir)) { 48 | *pchain[ir->o] = ir->prev; /* Reroute original instruction chain. */ 49 | lj_ir_nop(ir); 50 | continue; 51 | } 52 | pchain[ir->o] = &ir->prev; 53 | if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); 54 | if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); 55 | } 56 | } 57 | 58 | /* Dead Code Elimination. 59 | ** 60 | ** First backpropagate marks for all used instructions. Then replace 61 | ** the unused ones with a NOP. Note that compressing the IR to eliminate 62 | ** the NOPs does not pay off. 63 | */ 64 | void lj_opt_dce(jit_State *J) 65 | { 66 | if ((J->flags & JIT_F_OPT_DCE)) { 67 | dce_marksnap(J); 68 | dce_propagate(J); 69 | memset(J->bpropcache, 0, sizeof(J->bpropcache)); /* Invalidate cache. */ 70 | } 71 | } 72 | 73 | #undef IR 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/luajit/src/lj_parse.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Lua parser (source code -> bytecode). 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_PARSE_H 7 | #define _LJ_PARSE_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_lex.h" 11 | 12 | LJ_FUNC GCproto *lj_parse(LexState *ls); 13 | LJ_FUNC GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t l); 14 | #if LJ_HASFFI 15 | LJ_FUNC void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/luajit/src/lj_prng.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Pseudo-random number generation. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_PRNG_H 7 | #define _LJ_PRNG_H 8 | 9 | #include "lj_def.h" 10 | 11 | LJ_FUNC int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs); 12 | LJ_FUNC uint64_t LJ_FASTCALL lj_prng_u64(PRNGState *rs); 13 | LJ_FUNC uint64_t LJ_FASTCALL lj_prng_u64d(PRNGState *rs); 14 | 15 | /* This is just the precomputed result of lib_math.c:random_seed(rs, 0.0). */ 16 | static LJ_AINLINE void lj_prng_seed_fixed(PRNGState *rs) 17 | { 18 | rs->u[0] = U64x(a0d27757,0a345b8c); 19 | rs->u[1] = U64x(764a296c,5d4aa64f); 20 | rs->u[2] = U64x(51220704,070adeaa); 21 | rs->u[3] = U64x(2a2717b5,a7b7b927); 22 | } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/luajit/src/lj_profile.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Low-overhead profiling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_PROFILE_H 7 | #define _LJ_PROFILE_H 8 | 9 | #include "lj_obj.h" 10 | 11 | #if LJ_HASPROFILE 12 | 13 | LJ_FUNC void LJ_FASTCALL lj_profile_interpreter(lua_State *L); 14 | #if !LJ_PROFILE_SIGPROF 15 | LJ_FUNC void LJ_FASTCALL lj_profile_hook_enter(global_State *g); 16 | LJ_FUNC void LJ_FASTCALL lj_profile_hook_leave(global_State *g); 17 | #endif 18 | 19 | #endif 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /src/luajit/src/lj_record.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Trace recorder (bytecode -> SSA IR). 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_RECORD_H 7 | #define _LJ_RECORD_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_jit.h" 11 | 12 | #if LJ_HASJIT 13 | /* Context for recording an indexed load/store. */ 14 | typedef struct RecordIndex { 15 | TValue tabv; /* Runtime value of table (or indexed object). */ 16 | TValue keyv; /* Runtime value of key. */ 17 | TValue valv; /* Runtime value of stored value. */ 18 | TValue mobjv; /* Runtime value of metamethod object. */ 19 | GCtab *mtv; /* Runtime value of metatable object. */ 20 | cTValue *oldv; /* Runtime value of previously stored value. */ 21 | TRef tab; /* Table (or indexed object) reference. */ 22 | TRef key; /* Key reference. */ 23 | TRef val; /* Value reference for a store or 0 for a load. */ 24 | TRef mt; /* Metatable reference. */ 25 | TRef mobj; /* Metamethod object reference. */ 26 | int idxchain; /* Index indirections left or 0 for raw lookup. */ 27 | } RecordIndex; 28 | 29 | LJ_FUNC int lj_record_objcmp(jit_State *J, TRef a, TRef b, 30 | cTValue *av, cTValue *bv); 31 | LJ_FUNC void lj_record_stop(jit_State *J, TraceLink linktype, TraceNo lnk); 32 | LJ_FUNC TRef lj_record_constify(jit_State *J, cTValue *o); 33 | LJ_FUNC TRef lj_record_vload(jit_State *J, TRef ref, MSize idx, IRType t); 34 | 35 | LJ_FUNC void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs); 36 | LJ_FUNC void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs); 37 | LJ_FUNC void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults); 38 | 39 | LJ_FUNC int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm); 40 | LJ_FUNC TRef lj_record_idx(jit_State *J, RecordIndex *ix); 41 | LJ_FUNC int lj_record_next(jit_State *J, RecordIndex *ix); 42 | 43 | LJ_FUNC void lj_record_ins(jit_State *J); 44 | LJ_FUNC void lj_record_setup(jit_State *J); 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/luajit/src/lj_serialize.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Object de/serialization. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_SERIALIZE_H 7 | #define _LJ_SERIALIZE_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_buf.h" 11 | 12 | #if LJ_HASBUFFER 13 | 14 | #define LJ_SERIALIZE_DEPTH 100 /* Default depth. */ 15 | 16 | LJ_FUNC void LJ_FASTCALL lj_serialize_dict_prep_str(lua_State *L, GCtab *dict); 17 | LJ_FUNC void LJ_FASTCALL lj_serialize_dict_prep_mt(lua_State *L, GCtab *dict); 18 | LJ_FUNC SBufExt * LJ_FASTCALL lj_serialize_put(SBufExt *sbx, cTValue *o); 19 | LJ_FUNC char * LJ_FASTCALL lj_serialize_get(SBufExt *sbx, TValue *o); 20 | LJ_FUNC GCstr * LJ_FASTCALL lj_serialize_encode(lua_State *L, cTValue *o); 21 | LJ_FUNC void lj_serialize_decode(lua_State *L, TValue *o, GCstr *str); 22 | #if LJ_HASJIT 23 | LJ_FUNC MSize LJ_FASTCALL lj_serialize_peektype(SBufExt *sbx); 24 | #endif 25 | 26 | #endif 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/luajit/src/lj_snap.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Snapshot handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_SNAP_H 7 | #define _LJ_SNAP_H 8 | 9 | #include "lj_obj.h" 10 | #include "lj_jit.h" 11 | 12 | #if LJ_HASJIT 13 | LJ_FUNC void lj_snap_add(jit_State *J); 14 | LJ_FUNC void lj_snap_purge(jit_State *J); 15 | LJ_FUNC void lj_snap_shrink(jit_State *J); 16 | LJ_FUNC IRIns *lj_snap_regspmap(jit_State *J, GCtrace *T, SnapNo snapno, 17 | IRIns *ir); 18 | LJ_FUNC void lj_snap_replay(jit_State *J, GCtrace *T); 19 | LJ_FUNC const BCIns *lj_snap_restore(jit_State *J, void *exptr); 20 | LJ_FUNC void lj_snap_grow_buf_(jit_State *J, MSize need); 21 | LJ_FUNC void lj_snap_grow_map_(jit_State *J, MSize need); 22 | 23 | static LJ_AINLINE void lj_snap_grow_buf(jit_State *J, MSize need) 24 | { 25 | if (LJ_UNLIKELY(need > J->sizesnap)) lj_snap_grow_buf_(J, need); 26 | } 27 | 28 | static LJ_AINLINE void lj_snap_grow_map(jit_State *J, MSize need) 29 | { 30 | if (LJ_UNLIKELY(need > J->sizesnapmap)) lj_snap_grow_map_(J, need); 31 | } 32 | 33 | #endif 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/luajit/src/lj_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** State and stack handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_STATE_H 7 | #define _LJ_STATE_H 8 | 9 | #include "lj_obj.h" 10 | 11 | #define incr_top(L) \ 12 | (++L->top >= tvref(L->maxstack) && (lj_state_growstack1(L), 0)) 13 | 14 | #define savestack(L, p) ((char *)(p) - mref(L->stack, char)) 15 | #define restorestack(L, n) ((TValue *)(mref(L->stack, char) + (n))) 16 | 17 | LJ_FUNC void lj_state_relimitstack(lua_State *L); 18 | LJ_FUNC void lj_state_shrinkstack(lua_State *L, MSize used); 19 | LJ_FUNCA void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need); 20 | LJ_FUNC void LJ_FASTCALL lj_state_growstack1(lua_State *L); 21 | LJ_FUNC int LJ_FASTCALL lj_state_cpgrowstack(lua_State *L, MSize need); 22 | 23 | static LJ_AINLINE void lj_state_checkstack(lua_State *L, MSize need) 24 | { 25 | if ((mref(L->maxstack, char) - (char *)L->top) <= 26 | (ptrdiff_t)need*(ptrdiff_t)sizeof(TValue)) 27 | lj_state_growstack(L, need); 28 | } 29 | 30 | LJ_FUNC lua_State *lj_state_new(lua_State *L); 31 | LJ_FUNC void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L); 32 | #if LJ_64 && !LJ_GC64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC)) 33 | LJ_FUNC lua_State *lj_state_newstate(lua_Alloc f, void *ud); 34 | #endif 35 | 36 | #define LJ_ALLOCF_INTERNAL ((lua_Alloc)(void *)(uintptr_t)(1237<<4)) 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /src/luajit/src/lj_str.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** String handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_STR_H 7 | #define _LJ_STR_H 8 | 9 | #include 10 | 11 | #include "lj_obj.h" 12 | 13 | /* String helpers. */ 14 | LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b); 15 | LJ_FUNC const char *lj_str_find(const char *s, const char *f, 16 | MSize slen, MSize flen); 17 | LJ_FUNC int lj_str_haspattern(GCstr *s); 18 | 19 | /* String interning. */ 20 | LJ_FUNC void lj_str_resize(lua_State *L, MSize newmask); 21 | LJ_FUNCA GCstr *lj_str_new(lua_State *L, const char *str, size_t len); 22 | LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s); 23 | LJ_FUNC void LJ_FASTCALL lj_str_init(lua_State *L); 24 | #define lj_str_freetab(g) \ 25 | (lj_mem_freevec(g, g->str.tab, g->str.mask+1, GCRef)) 26 | 27 | #define lj_str_newz(L, s) (lj_str_new(L, s, strlen(s))) 28 | #define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) 29 | #define lj_str_size(len) (sizeof(GCstr) + (((len)+4) & ~(MSize)3)) 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/luajit/src/lj_strscan.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** String scanning. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_STRSCAN_H 7 | #define _LJ_STRSCAN_H 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Options for accepted/returned formats. */ 12 | #define STRSCAN_OPT_TOINT 0x01 /* Convert to int32_t, if possible. */ 13 | #define STRSCAN_OPT_TONUM 0x02 /* Always convert to double. */ 14 | #define STRSCAN_OPT_IMAG 0x04 15 | #define STRSCAN_OPT_LL 0x08 16 | #define STRSCAN_OPT_C 0x10 17 | 18 | /* Returned format. */ 19 | typedef enum { 20 | STRSCAN_ERROR, 21 | STRSCAN_NUM, STRSCAN_IMAG, 22 | STRSCAN_INT, STRSCAN_U32, STRSCAN_I64, STRSCAN_U64, 23 | } StrScanFmt; 24 | 25 | LJ_FUNC StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o, 26 | uint32_t opt); 27 | LJ_FUNC int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o); 28 | #if LJ_DUALNUM 29 | LJ_FUNC int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o); 30 | #else 31 | #define lj_strscan_number(s, o) lj_strscan_num((s), (o)) 32 | #endif 33 | 34 | /* Check for number or convert string to number/int in-place (!). */ 35 | static LJ_AINLINE int lj_strscan_numberobj(TValue *o) 36 | { 37 | return tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), o)); 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/luajit/src/lj_tab.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Table handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_TAB_H 7 | #define _LJ_TAB_H 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Hash constants. Tuned using a brute force search. */ 12 | #define HASH_BIAS (-0x04c11db7) 13 | #define HASH_ROT1 14 14 | #define HASH_ROT2 5 15 | #define HASH_ROT3 13 16 | 17 | /* Scramble the bits of numbers and pointers. */ 18 | static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi) 19 | { 20 | #if LJ_TARGET_X86ORX64 21 | /* Prefer variant that compiles well for a 2-operand CPU. */ 22 | lo ^= hi; hi = lj_rol(hi, HASH_ROT1); 23 | lo -= hi; hi = lj_rol(hi, HASH_ROT2); 24 | hi ^= lo; hi -= lj_rol(lo, HASH_ROT3); 25 | #else 26 | lo ^= hi; 27 | lo = lo - lj_rol(hi, HASH_ROT1); 28 | hi = lo ^ lj_rol(hi, HASH_ROT1 + HASH_ROT2); 29 | hi = hi - lj_rol(lo, HASH_ROT3); 30 | #endif 31 | return hi; 32 | } 33 | 34 | /* Hash values are masked with the table hash mask and used as an index. */ 35 | static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash) 36 | { 37 | Node *n = noderef(t->node); 38 | return &n[hash & t->hmask]; 39 | } 40 | 41 | /* String IDs are generated when a string is interned. */ 42 | #define hashstr(t, s) hashmask(t, (s)->sid) 43 | 44 | #define hashlohi(t, lo, hi) hashmask((t), hashrot((lo), (hi))) 45 | #define hashnum(t, o) hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1)) 46 | #if LJ_GC64 47 | #define hashgcref(t, r) \ 48 | hashlohi((t), (uint32_t)gcrefu(r), (uint32_t)(gcrefu(r) >> 32)) 49 | #else 50 | #define hashgcref(t, r) hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS) 51 | #endif 52 | 53 | #define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0) 54 | 55 | LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits); 56 | LJ_FUNC GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h); 57 | #if LJ_HASJIT 58 | LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize); 59 | #endif 60 | LJ_FUNCA GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt); 61 | LJ_FUNC void LJ_FASTCALL lj_tab_clear(GCtab *t); 62 | LJ_FUNC void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t); 63 | LJ_FUNC void lj_tab_resize(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits); 64 | LJ_FUNCA void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize); 65 | 66 | /* Caveat: all getters except lj_tab_get() can return NULL! */ 67 | 68 | LJ_FUNCA cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key); 69 | LJ_FUNC cTValue *lj_tab_getstr(GCtab *t, const GCstr *key); 70 | LJ_FUNCA cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key); 71 | 72 | /* Caveat: all setters require a write barrier for the stored value. */ 73 | 74 | LJ_FUNCA TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key); 75 | LJ_FUNCA TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key); 76 | LJ_FUNC TValue *lj_tab_setstr(lua_State *L, GCtab *t, const GCstr *key); 77 | LJ_FUNC TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key); 78 | 79 | #define inarray(t, key) ((MSize)(key) < (MSize)(t)->asize) 80 | #define arrayslot(t, i) (&tvref((t)->array)[(i)]) 81 | #define lj_tab_getint(t, key) \ 82 | (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_getinth((t), (key))) 83 | #define lj_tab_setint(L, t, key) \ 84 | (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_setinth(L, (t), (key))) 85 | 86 | LJ_FUNC uint32_t LJ_FASTCALL lj_tab_keyindex(GCtab *t, cTValue *key); 87 | LJ_FUNCA int lj_tab_next(GCtab *t, cTValue *key, TValue *o); 88 | LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t); 89 | #if LJ_HASJIT 90 | LJ_FUNC MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint); 91 | #endif 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /src/luajit/src/lj_trace.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Trace management. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_TRACE_H 7 | #define _LJ_TRACE_H 8 | 9 | #include "lj_obj.h" 10 | 11 | #if LJ_HASJIT 12 | #include "lj_jit.h" 13 | #include "lj_dispatch.h" 14 | 15 | /* Trace errors. */ 16 | typedef enum { 17 | #define TREDEF(name, msg) LJ_TRERR_##name, 18 | #include "lj_traceerr.h" 19 | LJ_TRERR__MAX 20 | } TraceError; 21 | 22 | LJ_FUNC_NORET void lj_trace_err(jit_State *J, TraceError e); 23 | LJ_FUNC_NORET void lj_trace_err_info(jit_State *J, TraceError e); 24 | 25 | /* Trace management. */ 26 | LJ_FUNC GCtrace * LJ_FASTCALL lj_trace_alloc(lua_State *L, GCtrace *T); 27 | LJ_FUNC void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T); 28 | LJ_FUNC void lj_trace_reenableproto(GCproto *pt); 29 | LJ_FUNC void lj_trace_flushproto(global_State *g, GCproto *pt); 30 | LJ_FUNC void lj_trace_flush(jit_State *J, TraceNo traceno); 31 | LJ_FUNC int lj_trace_flushall(lua_State *L); 32 | LJ_FUNC void lj_trace_initstate(global_State *g); 33 | LJ_FUNC void lj_trace_freestate(global_State *g); 34 | 35 | /* Event handling. */ 36 | LJ_FUNC void lj_trace_ins(jit_State *J, const BCIns *pc); 37 | LJ_FUNCA void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc); 38 | LJ_FUNCA void LJ_FASTCALL lj_trace_stitch(jit_State *J, const BCIns *pc); 39 | LJ_FUNCA int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr); 40 | #if LJ_UNWIND_EXT 41 | LJ_FUNC uintptr_t LJ_FASTCALL lj_trace_unwind(jit_State *J, uintptr_t addr, ExitNo *ep); 42 | #endif 43 | 44 | /* Signal asynchronous abort of trace or end of trace. */ 45 | #define lj_trace_abort(g) (G2J(g)->state &= ~LJ_TRACE_ACTIVE) 46 | #define lj_trace_end(J) (J->state = LJ_TRACE_END) 47 | 48 | #else 49 | 50 | #define lj_trace_flushall(L) (UNUSED(L), 0) 51 | #define lj_trace_initstate(g) UNUSED(g) 52 | #define lj_trace_freestate(g) UNUSED(g) 53 | #define lj_trace_abort(g) UNUSED(g) 54 | #define lj_trace_end(J) UNUSED(J) 55 | 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/luajit/src/lj_traceerr.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Trace compiler error messages. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | /* This file may be included multiple times with different TREDEF macros. */ 7 | 8 | /* Recording. */ 9 | TREDEF(RECERR, "error thrown or hook called during recording") 10 | TREDEF(TRACEUV, "trace too short") 11 | TREDEF(TRACEOV, "trace too long") 12 | TREDEF(STACKOV, "trace too deep") 13 | TREDEF(SNAPOV, "too many snapshots") 14 | TREDEF(BLACKL, "blacklisted") 15 | TREDEF(RETRY, "retry recording") 16 | TREDEF(NYIBC, "NYI: bytecode %s") 17 | 18 | /* Recording loop ops. */ 19 | TREDEF(LLEAVE, "leaving loop in root trace") 20 | TREDEF(LINNER, "inner loop in root trace") 21 | TREDEF(LUNROLL, "loop unroll limit reached") 22 | 23 | /* Recording calls/returns. */ 24 | TREDEF(BADTYPE, "bad argument type") 25 | TREDEF(CJITOFF, "JIT compilation disabled for function") 26 | TREDEF(CUNROLL, "call unroll limit reached") 27 | TREDEF(DOWNREC, "down-recursion, restarting") 28 | TREDEF(NYIFFU, "NYI: unsupported variant of FastFunc %s") 29 | TREDEF(NYIRETL, "NYI: return to lower frame") 30 | 31 | /* Recording indexed load/store. */ 32 | TREDEF(STORENN, "store with nil or NaN key") 33 | TREDEF(NOMM, "missing metamethod") 34 | TREDEF(IDXLOOP, "looping index lookup") 35 | TREDEF(NYITMIX, "NYI: mixed sparse/dense table") 36 | 37 | /* Recording C data operations. */ 38 | TREDEF(NOCACHE, "symbol not in cache") 39 | TREDEF(NYICONV, "NYI: unsupported C type conversion") 40 | TREDEF(NYICALL, "NYI: unsupported C function type") 41 | 42 | /* Optimizations. */ 43 | TREDEF(GFAIL, "guard would always fail") 44 | TREDEF(PHIOV, "too many PHIs") 45 | TREDEF(TYPEINS, "persistent type instability") 46 | 47 | /* Assembler. */ 48 | TREDEF(MCODEAL, "failed to allocate mcode memory") 49 | TREDEF(MCODEOV, "machine code too long") 50 | TREDEF(MCODELM, "hit mcode limit (retrying)") 51 | TREDEF(SPILLOV, "too many spill slots") 52 | TREDEF(BADRA, "inconsistent register allocation") 53 | TREDEF(NYIIR, "NYI: cannot assemble IR instruction %d") 54 | TREDEF(NYIPHI, "NYI: PHI shuffling too complex") 55 | TREDEF(NYICOAL, "NYI: register coalescing too complex") 56 | 57 | #undef TREDEF 58 | 59 | /* Detecting unused error messages: 60 | awk -F, '/^TREDEF/ { gsub(/TREDEF./, ""); printf "grep -q LJ_TRERR_%s *.[ch] || echo %s\n", $1, $1}' lj_traceerr.h | sh 61 | */ 62 | -------------------------------------------------------------------------------- /src/luajit/src/lj_udata.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Userdata handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define lj_udata_c 7 | #define LUA_CORE 8 | 9 | #include "lj_obj.h" 10 | #include "lj_gc.h" 11 | #include "lj_err.h" 12 | #include "lj_udata.h" 13 | 14 | GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env) 15 | { 16 | GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata); 17 | global_State *g = G(L); 18 | newwhite(g, ud); /* Not finalized. */ 19 | ud->gct = ~LJ_TUDATA; 20 | ud->udtype = UDTYPE_USERDATA; 21 | ud->len = sz; 22 | /* NOBARRIER: The GCudata is new (marked white). */ 23 | setgcrefnull(ud->metatable); 24 | setgcref(ud->env, obj2gco(env)); 25 | /* Chain to userdata list (after main thread). */ 26 | setgcrefr(ud->nextgc, mainthread(g)->nextgc); 27 | setgcref(mainthread(g)->nextgc, obj2gco(ud)); 28 | return ud; 29 | } 30 | 31 | void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud) 32 | { 33 | lj_mem_free(g, ud, sizeudata(ud)); 34 | } 35 | 36 | #if LJ_64 37 | void *lj_lightud_intern(lua_State *L, void *p) 38 | { 39 | global_State *g = G(L); 40 | uint64_t u = (uint64_t)p; 41 | uint32_t up = lightudup(u); 42 | uint32_t *segmap = mref(g->gc.lightudseg, uint32_t); 43 | MSize segnum = g->gc.lightudnum; 44 | if (segmap) { 45 | MSize seg; 46 | for (seg = 0; seg <= segnum; seg++) 47 | if (segmap[seg] == up) /* Fast path. */ 48 | return (void *)(((uint64_t)seg << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); 49 | segnum++; 50 | /* Leave last segment unused to avoid clash with ITERN key. */ 51 | if (segnum >= (1 << LJ_LIGHTUD_BITS_SEG)-1) lj_err_msg(L, LJ_ERR_BADLU); 52 | } 53 | if (!((segnum-1) & segnum) && segnum != 1) { 54 | lj_mem_reallocvec(L, segmap, segnum, segnum ? 2*segnum : 2u, uint32_t); 55 | setmref(g->gc.lightudseg, segmap); 56 | } 57 | g->gc.lightudnum = segnum; 58 | segmap[segnum] = up; 59 | return (void *)(((uint64_t)segnum << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); 60 | } 61 | #endif 62 | 63 | -------------------------------------------------------------------------------- /src/luajit/src/lj_udata.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Userdata handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_UDATA_H 7 | #define _LJ_UDATA_H 8 | 9 | #include "lj_obj.h" 10 | 11 | LJ_FUNC GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env); 12 | LJ_FUNC void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud); 13 | #if LJ_64 14 | LJ_FUNC void * LJ_FASTCALL lj_lightud_intern(lua_State *L, void *p); 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /src/luajit/src/lj_vm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Assembler VM interface definitions. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_VM_H 7 | #define _LJ_VM_H 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Entry points for ASM parts of VM. */ 12 | LJ_ASMF void lj_vm_call(lua_State *L, TValue *base, int nres1); 13 | LJ_ASMF int lj_vm_pcall(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); 14 | typedef TValue *(*lua_CPFunction)(lua_State *L, lua_CFunction func, void *ud); 15 | LJ_ASMF int lj_vm_cpcall(lua_State *L, lua_CFunction func, void *ud, 16 | lua_CPFunction cp); 17 | LJ_ASMF int lj_vm_resume(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); 18 | LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_c(void *cframe, int errcode); 19 | LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_ff(void *cframe); 20 | #if LJ_ABI_WIN && LJ_TARGET_X86 21 | LJ_ASMF_NORET void LJ_FASTCALL lj_vm_rtlunwind(void *cframe, void *excptrec, 22 | void *unwinder, int errcode); 23 | #endif 24 | LJ_ASMF void lj_vm_unwind_c_eh(void); 25 | LJ_ASMF void lj_vm_unwind_ff_eh(void); 26 | #if LJ_TARGET_X86ORX64 27 | LJ_ASMF void lj_vm_unwind_rethrow(void); 28 | #endif 29 | #if LJ_TARGET_MIPS 30 | LJ_ASMF void lj_vm_unwind_stub(void); 31 | #endif 32 | 33 | /* Miscellaneous functions. */ 34 | #if LJ_TARGET_X86ORX64 35 | LJ_ASMF int lj_vm_cpuid(uint32_t f, uint32_t res[4]); 36 | #endif 37 | #if LJ_TARGET_PPC 38 | void lj_vm_cachesync(void *start, void *end); 39 | #endif 40 | LJ_ASMF double lj_vm_foldarith(double x, double y, int op); 41 | #if LJ_HASJIT 42 | LJ_ASMF double lj_vm_foldfpm(double x, int op); 43 | #endif 44 | #if !LJ_ARCH_HASFPU 45 | /* Declared in lj_obj.h: LJ_ASMF int32_t lj_vm_tobit(double x); */ 46 | #endif 47 | 48 | /* Dispatch targets for recording and hooks. */ 49 | LJ_ASMF void lj_vm_record(void); 50 | LJ_ASMF void lj_vm_inshook(void); 51 | LJ_ASMF void lj_vm_rethook(void); 52 | LJ_ASMF void lj_vm_callhook(void); 53 | LJ_ASMF void lj_vm_profhook(void); 54 | LJ_ASMF void lj_vm_IITERN(void); 55 | 56 | /* Trace exit handling. */ 57 | LJ_ASMF char lj_vm_exit_handler[]; 58 | LJ_ASMF char lj_vm_exit_interp[]; 59 | 60 | /* Internal math helper functions. */ 61 | #if LJ_TARGET_PPC || LJ_TARGET_ARM64 || (LJ_TARGET_MIPS && LJ_ABI_SOFTFP) 62 | #define lj_vm_floor floor 63 | #define lj_vm_ceil ceil 64 | #else 65 | LJ_ASMF double lj_vm_floor(double); 66 | LJ_ASMF double lj_vm_ceil(double); 67 | #if LJ_TARGET_ARM 68 | LJ_ASMF double lj_vm_floor_sf(double); 69 | LJ_ASMF double lj_vm_ceil_sf(double); 70 | #endif 71 | #endif 72 | #ifdef LUAJIT_NO_LOG2 73 | LJ_ASMF double lj_vm_log2(double); 74 | #else 75 | #define lj_vm_log2 log2 76 | #endif 77 | #if !(defined(_LJ_DISPATCH_H) && LJ_TARGET_MIPS) 78 | LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t, int32_t); 79 | #endif 80 | 81 | #if LJ_HASJIT 82 | #if LJ_TARGET_X86ORX64 83 | LJ_ASMF void lj_vm_floor_sse(void); 84 | LJ_ASMF void lj_vm_ceil_sse(void); 85 | LJ_ASMF void lj_vm_trunc_sse(void); 86 | #endif 87 | #if LJ_TARGET_PPC || LJ_TARGET_ARM64 88 | #define lj_vm_trunc trunc 89 | #else 90 | LJ_ASMF double lj_vm_trunc(double); 91 | #if LJ_TARGET_ARM 92 | LJ_ASMF double lj_vm_trunc_sf(double); 93 | #endif 94 | #endif 95 | #if LJ_HASFFI 96 | LJ_ASMF int lj_vm_errno(void); 97 | #endif 98 | LJ_ASMF TValue *lj_vm_next(GCtab *t, uint32_t idx); 99 | #endif 100 | 101 | /* Continuations for metamethods. */ 102 | LJ_ASMF void lj_cont_cat(void); /* Continue with concatenation. */ 103 | LJ_ASMF void lj_cont_ra(void); /* Store result in RA from instruction. */ 104 | LJ_ASMF void lj_cont_nop(void); /* Do nothing, just continue execution. */ 105 | LJ_ASMF void lj_cont_condt(void); /* Branch if result is true. */ 106 | LJ_ASMF void lj_cont_condf(void); /* Branch if result is false. */ 107 | LJ_ASMF void lj_cont_hook(void); /* Continue from hook yield. */ 108 | LJ_ASMF void lj_cont_stitch(void); /* Trace stitching. */ 109 | 110 | /* Start of the ASM code. */ 111 | LJ_ASMF char lj_vm_asm_begin[]; 112 | 113 | /* Bytecode offsets are relative to lj_vm_asm_begin. */ 114 | #define makeasmfunc(ofs) lj_ptr_sign((ASMFunction)(lj_vm_asm_begin + (ofs)), 0) 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /src/luajit/src/lj_vmevent.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** VM event handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #include 7 | 8 | #define lj_vmevent_c 9 | #define LUA_CORE 10 | 11 | #include "lj_obj.h" 12 | #include "lj_str.h" 13 | #include "lj_tab.h" 14 | #include "lj_state.h" 15 | #include "lj_dispatch.h" 16 | #include "lj_vm.h" 17 | #include "lj_vmevent.h" 18 | 19 | ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev) 20 | { 21 | global_State *g = G(L); 22 | GCstr *s = lj_str_newlit(L, LJ_VMEVENTS_REGKEY); 23 | cTValue *tv = lj_tab_getstr(tabV(registry(L)), s); 24 | if (tvistab(tv)) { 25 | int hash = VMEVENT_HASH(ev); 26 | tv = lj_tab_getint(tabV(tv), hash); 27 | if (tv && tvisfunc(tv)) { 28 | lj_state_checkstack(L, LUA_MINSTACK); 29 | setfuncV(L, L->top++, funcV(tv)); 30 | if (LJ_FR2) setnilV(L->top++); 31 | return savestack(L, L->top); 32 | } 33 | } 34 | g->vmevmask &= ~VMEVENT_MASK(ev); /* No handler: cache this fact. */ 35 | return 0; 36 | } 37 | 38 | void lj_vmevent_call(lua_State *L, ptrdiff_t argbase) 39 | { 40 | global_State *g = G(L); 41 | uint8_t oldmask = g->vmevmask; 42 | uint8_t oldh = hook_save(g); 43 | int status; 44 | g->vmevmask = 0; /* Disable all events. */ 45 | hook_vmevent(g); 46 | status = lj_vm_pcall(L, restorestack(L, argbase), 0+1, 0); 47 | if (LJ_UNLIKELY(status)) { 48 | /* Really shouldn't use stderr here, but where else to complain? */ 49 | L->top--; 50 | fputs("VM handler failed: ", stderr); 51 | fputs(tvisstr(L->top) ? strVdata(L->top) : "?", stderr); 52 | fputc('\n', stderr); 53 | } 54 | hook_restore(g, oldh); 55 | if (g->vmevmask != VMEVENT_NOCACHE) 56 | g->vmevmask = oldmask; /* Restore event mask, but not if not modified. */ 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/luajit/src/lj_vmevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** VM event handling. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LJ_VMEVENT_H 7 | #define _LJ_VMEVENT_H 8 | 9 | #include "lj_obj.h" 10 | 11 | /* Registry key for VM event handler table. */ 12 | #define LJ_VMEVENTS_REGKEY "_VMEVENTS" 13 | #define LJ_VMEVENTS_HSIZE 4 14 | 15 | #define VMEVENT_MASK(ev) ((uint8_t)1 << ((int)(ev) & 7)) 16 | #define VMEVENT_HASH(ev) ((int)(ev) & ~7) 17 | #define VMEVENT_HASHIDX(h) ((int)(h) << 3) 18 | #define VMEVENT_NOCACHE 255 19 | 20 | #define VMEVENT_DEF(name, hash) \ 21 | LJ_VMEVENT_##name##_, \ 22 | LJ_VMEVENT_##name = ((LJ_VMEVENT_##name##_) & 7)|((hash) << 3) 23 | 24 | /* VM event IDs. */ 25 | typedef enum { 26 | VMEVENT_DEF(BC, 0x00003883), 27 | VMEVENT_DEF(TRACE, 0x12d91467), 28 | VMEVENT_DEF(RECORD, 0x1284bf4f), 29 | VMEVENT_DEF(TEXIT, 0x129df2b0), 30 | VMEVENT_DEF(ERRFIN, 0x12d93888), 31 | LJ_VMEVENT__MAX 32 | } VMEvent; 33 | 34 | #ifdef LUAJIT_DISABLE_VMEVENT 35 | #define lj_vmevent_send(L, ev, args) UNUSED(L) 36 | #define lj_vmevent_send_(L, ev, args, post) UNUSED(L) 37 | #else 38 | #define lj_vmevent_send(L, ev, args) \ 39 | if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ 40 | ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ 41 | if (argbase) { \ 42 | args \ 43 | lj_vmevent_call(L, argbase); \ 44 | } \ 45 | } 46 | #define lj_vmevent_send_(L, ev, args, post) \ 47 | if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ 48 | ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ 49 | if (argbase) { \ 50 | args \ 51 | lj_vmevent_call(L, argbase); \ 52 | post \ 53 | } \ 54 | } 55 | 56 | LJ_FUNC ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev); 57 | LJ_FUNC void lj_vmevent_call(lua_State *L, ptrdiff_t argbase); 58 | #endif 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/luajit/src/lj_vmmath.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** Math helper functions for assembler VM. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define lj_vmmath_c 7 | #define LUA_CORE 8 | 9 | #include 10 | #include 11 | 12 | #include "lj_obj.h" 13 | #include "lj_ir.h" 14 | #include "lj_vm.h" 15 | 16 | /* -- Wrapper functions --------------------------------------------------- */ 17 | 18 | #if LJ_TARGET_X86 && __ELF__ && __PIC__ 19 | /* Wrapper functions to deal with the ELF/x86 PIC disaster. */ 20 | LJ_FUNCA double lj_wrap_log(double x) { return log(x); } 21 | LJ_FUNCA double lj_wrap_log10(double x) { return log10(x); } 22 | LJ_FUNCA double lj_wrap_exp(double x) { return exp(x); } 23 | LJ_FUNCA double lj_wrap_sin(double x) { return sin(x); } 24 | LJ_FUNCA double lj_wrap_cos(double x) { return cos(x); } 25 | LJ_FUNCA double lj_wrap_tan(double x) { return tan(x); } 26 | LJ_FUNCA double lj_wrap_asin(double x) { return asin(x); } 27 | LJ_FUNCA double lj_wrap_acos(double x) { return acos(x); } 28 | LJ_FUNCA double lj_wrap_atan(double x) { return atan(x); } 29 | LJ_FUNCA double lj_wrap_sinh(double x) { return sinh(x); } 30 | LJ_FUNCA double lj_wrap_cosh(double x) { return cosh(x); } 31 | LJ_FUNCA double lj_wrap_tanh(double x) { return tanh(x); } 32 | LJ_FUNCA double lj_wrap_atan2(double x, double y) { return atan2(x, y); } 33 | LJ_FUNCA double lj_wrap_pow(double x, double y) { return pow(x, y); } 34 | LJ_FUNCA double lj_wrap_fmod(double x, double y) { return fmod(x, y); } 35 | #endif 36 | 37 | /* -- Helper functions ---------------------------------------------------- */ 38 | 39 | /* Required to prevent the C compiler from applying FMA optimizations. 40 | ** 41 | ** Yes, there's -ffp-contract and the FP_CONTRACT pragma ... in theory. 42 | ** But the current state of C compilers is a mess in this regard. 43 | ** Also, this function is not performance sensitive at all. 44 | */ 45 | LJ_NOINLINE static double lj_vm_floormul(double x, double y) 46 | { 47 | return lj_vm_floor(x / y) * y; 48 | } 49 | 50 | double lj_vm_foldarith(double x, double y, int op) 51 | { 52 | switch (op) { 53 | case IR_ADD - IR_ADD: return x+y; break; 54 | case IR_SUB - IR_ADD: return x-y; break; 55 | case IR_MUL - IR_ADD: return x*y; break; 56 | case IR_DIV - IR_ADD: return x/y; break; 57 | case IR_MOD - IR_ADD: return x-lj_vm_floormul(x, y); break; 58 | case IR_POW - IR_ADD: return pow(x, y); break; 59 | case IR_NEG - IR_ADD: return -x; break; 60 | case IR_ABS - IR_ADD: return fabs(x); break; 61 | #if LJ_HASJIT 62 | case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; 63 | case IR_MIN - IR_ADD: return x < y ? x : y; break; 64 | case IR_MAX - IR_ADD: return x > y ? x : y; break; 65 | #endif 66 | default: return x; 67 | } 68 | } 69 | 70 | /* -- Helper functions for generated machine code ------------------------- */ 71 | 72 | #if (LJ_HASJIT && !(LJ_TARGET_ARM || LJ_TARGET_ARM64 || LJ_TARGET_PPC)) || LJ_TARGET_MIPS 73 | int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b) 74 | { 75 | uint32_t y, ua, ub; 76 | /* This must be checked before using this function. */ 77 | lj_assertX(b != 0, "modulo with zero divisor"); 78 | ua = a < 0 ? ~(uint32_t)a+1u : (uint32_t)a; 79 | ub = b < 0 ? ~(uint32_t)b+1u : (uint32_t)b; 80 | y = ua % ub; 81 | if (y != 0 && (a^b) < 0) y = y - ub; 82 | if (((int32_t)y^b) < 0) y = ~y+1u; 83 | return (int32_t)y; 84 | } 85 | #endif 86 | 87 | #if LJ_HASJIT 88 | 89 | #ifdef LUAJIT_NO_LOG2 90 | double lj_vm_log2(double a) 91 | { 92 | return log(a) * 1.4426950408889634074; 93 | } 94 | #endif 95 | 96 | /* Computes fpm(x) for extended math functions. */ 97 | double lj_vm_foldfpm(double x, int fpm) 98 | { 99 | switch (fpm) { 100 | case IRFPM_FLOOR: return lj_vm_floor(x); 101 | case IRFPM_CEIL: return lj_vm_ceil(x); 102 | case IRFPM_TRUNC: return lj_vm_trunc(x); 103 | case IRFPM_SQRT: return sqrt(x); 104 | case IRFPM_LOG: return log(x); 105 | case IRFPM_LOG2: return lj_vm_log2(x); 106 | default: lj_assertX(0, "bad fpm %d", fpm); 107 | } 108 | return 0; 109 | } 110 | 111 | #if LJ_HASFFI 112 | int lj_vm_errno(void) 113 | { 114 | return errno; 115 | } 116 | #endif 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /src/luajit/src/ljamalg.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** LuaJIT core and libraries amalgamation. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #define ljamalg_c 7 | #define LUA_CORE 8 | 9 | /* To get the mremap prototype. Must be defined before any system includes. */ 10 | #if defined(__linux__) && !defined(_GNU_SOURCE) 11 | #define _GNU_SOURCE 12 | #endif 13 | 14 | #ifndef WINVER 15 | #define WINVER 0x0501 16 | #endif 17 | 18 | #include "lua.h" 19 | #include "lauxlib.h" 20 | 21 | #include "lj_assert.c" 22 | #include "lj_gc.c" 23 | #include "lj_err.c" 24 | #include "lj_char.c" 25 | #include "lj_bc.c" 26 | #include "lj_obj.c" 27 | #include "lj_buf.c" 28 | #include "lj_str.c" 29 | #include "lj_tab.c" 30 | #include "lj_func.c" 31 | #include "lj_udata.c" 32 | #include "lj_meta.c" 33 | #include "lj_debug.c" 34 | #include "lj_prng.c" 35 | #include "lj_state.c" 36 | #include "lj_dispatch.c" 37 | #include "lj_vmevent.c" 38 | #include "lj_vmmath.c" 39 | #include "lj_strscan.c" 40 | #include "lj_strfmt.c" 41 | #include "lj_strfmt_num.c" 42 | #include "lj_serialize.c" 43 | #include "lj_api.c" 44 | #include "lj_profile.c" 45 | #include "lj_lex.c" 46 | #include "lj_parse.c" 47 | #include "lj_bcread.c" 48 | #include "lj_bcwrite.c" 49 | #include "lj_load.c" 50 | #include "lj_ctype.c" 51 | #include "lj_cdata.c" 52 | #include "lj_cconv.c" 53 | #include "lj_ccall.c" 54 | #include "lj_ccallback.c" 55 | #include "lj_carith.c" 56 | #include "lj_clib.c" 57 | #include "lj_cparse.c" 58 | #include "lj_lib.c" 59 | #include "lj_ir.c" 60 | #include "lj_opt_mem.c" 61 | #include "lj_opt_fold.c" 62 | #include "lj_opt_narrow.c" 63 | #include "lj_opt_dce.c" 64 | #include "lj_opt_loop.c" 65 | #include "lj_opt_split.c" 66 | #include "lj_opt_sink.c" 67 | #include "lj_mcode.c" 68 | #include "lj_snap.c" 69 | #include "lj_record.c" 70 | #include "lj_crecord.c" 71 | #include "lj_ffrecord.c" 72 | #include "lj_asm.c" 73 | #include "lj_trace.c" 74 | #include "lj_gdbjit.c" 75 | #include "lj_alloc.c" 76 | 77 | #include "lib_aux.c" 78 | #include "lib_base.c" 79 | #include "lib_math.c" 80 | #include "lib_string.c" 81 | #include "lib_table.c" 82 | #include "lib_io.c" 83 | #include "lib_os.c" 84 | #include "lib_package.c" 85 | #include "lib_debug.c" 86 | #include "lib_bit.c" 87 | #include "lib_jit.c" 88 | #include "lib_ffi.c" 89 | #include "lib_buffer.c" 90 | #include "lib_init.c" 91 | 92 | -------------------------------------------------------------------------------- /src/luajit/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // C++ wrapper for LuaJIT header files. 2 | 3 | extern "C" { 4 | #include "lua.h" 5 | #include "lauxlib.h" 6 | #include "lualib.h" 7 | #include "luajit.h" 8 | } 9 | 10 | -------------------------------------------------------------------------------- /src/luajit/src/luajit_rolling.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** LuaJIT -- a Just-In-Time Compiler for Lua. https://luajit.org/ 3 | ** 4 | ** Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | ** 6 | ** Permission is hereby granted, free of charge, to any person obtaining 7 | ** a copy of this software and associated documentation files (the 8 | ** "Software"), to deal in the Software without restriction, including 9 | ** without limitation the rights to use, copy, modify, merge, publish, 10 | ** distribute, sublicense, and/or sell copies of the Software, and to 11 | ** permit persons to whom the Software is furnished to do so, subject to 12 | ** the following conditions: 13 | ** 14 | ** The above copyright notice and this permission notice shall be 15 | ** included in all copies or substantial portions of the Software. 16 | ** 17 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | ** 25 | ** [ MIT license: https://www.opensource.org/licenses/mit-license.php ] 26 | */ 27 | 28 | #ifndef _LUAJIT_H 29 | #define _LUAJIT_H 30 | 31 | #include "lua.h" 32 | 33 | #define LUAJIT_VERSION "LuaJIT 2.1.ROLLING" 34 | #define LUAJIT_VERSION_NUM 20199 /* Deprecated. */ 35 | #define LUAJIT_VERSION_SYM luaJIT_version_2_1_ROLLING 36 | #define LUAJIT_COPYRIGHT "Copyright (C) 2005-2025 Mike Pall" 37 | #define LUAJIT_URL "https://luajit.org/" 38 | 39 | /* Modes for luaJIT_setmode. */ 40 | #define LUAJIT_MODE_MASK 0x00ff 41 | 42 | enum { 43 | LUAJIT_MODE_ENGINE, /* Set mode for whole JIT engine. */ 44 | LUAJIT_MODE_DEBUG, /* Set debug mode (idx = level). */ 45 | 46 | LUAJIT_MODE_FUNC, /* Change mode for a function. */ 47 | LUAJIT_MODE_ALLFUNC, /* Recurse into subroutine protos. */ 48 | LUAJIT_MODE_ALLSUBFUNC, /* Change only the subroutines. */ 49 | 50 | LUAJIT_MODE_TRACE, /* Flush a compiled trace. */ 51 | 52 | LUAJIT_MODE_WRAPCFUNC = 0x10, /* Set wrapper mode for C function calls. */ 53 | 54 | LUAJIT_MODE_MAX 55 | }; 56 | 57 | /* Flags or'ed in to the mode. */ 58 | #define LUAJIT_MODE_OFF 0x0000 /* Turn feature off. */ 59 | #define LUAJIT_MODE_ON 0x0100 /* Turn feature on. */ 60 | #define LUAJIT_MODE_FLUSH 0x0200 /* Flush JIT-compiled code. */ 61 | 62 | /* LuaJIT public C API. */ 63 | 64 | /* Control the JIT engine. */ 65 | LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode); 66 | 67 | /* Low-overhead profiling API. */ 68 | typedef void (*luaJIT_profile_callback)(void *data, lua_State *L, 69 | int samples, int vmstate); 70 | LUA_API void luaJIT_profile_start(lua_State *L, const char *mode, 71 | luaJIT_profile_callback cb, void *data); 72 | LUA_API void luaJIT_profile_stop(lua_State *L); 73 | LUA_API const char *luaJIT_profile_dumpstack(lua_State *L, const char *fmt, 74 | int depth, size_t *len); 75 | 76 | /* Enforce (dynamic) linker error for version mismatches. Call from main. */ 77 | LUA_API void LUAJIT_VERSION_SYM(void); 78 | 79 | #error "DO NOT USE luajit_rolling.h -- only include build-generated luajit.h" 80 | #endif 81 | -------------------------------------------------------------------------------- /src/luajit/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Standard library header. 3 | ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h 4 | */ 5 | 6 | #ifndef _LUALIB_H 7 | #define _LUALIB_H 8 | 9 | #include "lua.h" 10 | 11 | #define LUA_FILEHANDLE "FILE*" 12 | 13 | #define LUA_COLIBNAME "coroutine" 14 | #define LUA_MATHLIBNAME "math" 15 | #define LUA_STRLIBNAME "string" 16 | #define LUA_TABLIBNAME "table" 17 | #define LUA_IOLIBNAME "io" 18 | #define LUA_OSLIBNAME "os" 19 | #define LUA_LOADLIBNAME "package" 20 | #define LUA_DBLIBNAME "debug" 21 | #define LUA_BITLIBNAME "bit" 22 | #define LUA_JITLIBNAME "jit" 23 | #define LUA_FFILIBNAME "ffi" 24 | 25 | LUALIB_API int luaopen_base(lua_State *L); 26 | LUALIB_API int luaopen_math(lua_State *L); 27 | LUALIB_API int luaopen_string(lua_State *L); 28 | LUALIB_API int luaopen_table(lua_State *L); 29 | LUALIB_API int luaopen_io(lua_State *L); 30 | LUALIB_API int luaopen_os(lua_State *L); 31 | LUALIB_API int luaopen_package(lua_State *L); 32 | LUALIB_API int luaopen_debug(lua_State *L); 33 | LUALIB_API int luaopen_bit(lua_State *L); 34 | LUALIB_API int luaopen_jit(lua_State *L); 35 | LUALIB_API int luaopen_ffi(lua_State *L); 36 | LUALIB_API int luaopen_string_buffer(lua_State *L); 37 | 38 | LUALIB_API void luaL_openlibs(lua_State *L); 39 | 40 | #ifndef lua_assert 41 | #define lua_assert(x) ((void)0) 42 | #endif 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/luajit/src/psvitabuild.bat: -------------------------------------------------------------------------------- 1 | @rem Script to build LuaJIT with the PS Vita SDK. 2 | @rem Donated to the public domain. 3 | @rem 4 | @rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler) 5 | @rem Then cd to this directory and run this script. 6 | 7 | @if not defined INCLUDE goto :FAIL 8 | @if not defined SCE_PSP2_SDK_DIR goto :FAIL 9 | 10 | @setlocal 11 | @rem ---- Host compiler ---- 12 | @set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE 13 | @set LJLINK=link /nologo 14 | @set LJMT=mt /nologo 15 | @set DASMDIR=..\dynasm 16 | @set DASM=%DASMDIR%\dynasm.lua 17 | @set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c 18 | 19 | %LJCOMPILE% host\minilua.c 20 | @if errorlevel 1 goto :BAD 21 | %LJLINK% /out:minilua.exe minilua.obj 22 | @if errorlevel 1 goto :BAD 23 | if exist minilua.exe.manifest^ 24 | %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe 25 | 26 | @rem Check for 32 bit host compiler. 27 | @minilua 28 | @if errorlevel 8 goto :FAIL 29 | 30 | @set DASMFLAGS=-D FPU -D HFABI 31 | minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_arm.dasc 32 | @if errorlevel 1 goto :BAD 33 | 34 | if exist ..\.git ( git show -s --format=%%ct >luajit_relver.txt ) else ( type ..\.relver >luajit_relver.txt ) 35 | minilua host\genversion.lua 36 | 37 | %LJCOMPILE% /I "." /I %DASMDIR% -DLUAJIT_TARGET=LUAJIT_ARCH_ARM -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLJ_TARGET_PSVITA=1 host\buildvm*.c 38 | @if errorlevel 1 goto :BAD 39 | %LJLINK% /out:buildvm.exe buildvm*.obj 40 | @if errorlevel 1 goto :BAD 41 | if exist buildvm.exe.manifest^ 42 | %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe 43 | 44 | buildvm -m elfasm -o lj_vm.s 45 | @if errorlevel 1 goto :BAD 46 | buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% 47 | @if errorlevel 1 goto :BAD 48 | buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% 49 | @if errorlevel 1 goto :BAD 50 | buildvm -m libdef -o lj_libdef.h %ALL_LIB% 51 | @if errorlevel 1 goto :BAD 52 | buildvm -m recdef -o lj_recdef.h %ALL_LIB% 53 | @if errorlevel 1 goto :BAD 54 | buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% 55 | @if errorlevel 1 goto :BAD 56 | buildvm -m folddef -o lj_folddef.h lj_opt_fold.c 57 | @if errorlevel 1 goto :BAD 58 | 59 | @rem ---- Cross compiler ---- 60 | @set LJCOMPILE="%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2snc" -c -w -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC 61 | @set LJLIB="%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2ld32" -r --output= 62 | @set INCLUDE="" 63 | 64 | "%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2as" -o lj_vm.o lj_vm.s 65 | 66 | @if "%1" neq "debug" goto :NODEBUG 67 | @shift 68 | @set LJCOMPILE=%LJCOMPILE% -g -O0 69 | @set TARGETLIB=libluajitD.a 70 | goto :BUILD 71 | :NODEBUG 72 | @set LJCOMPILE=%LJCOMPILE% -O2 73 | @set TARGETLIB=libluajit.a 74 | :BUILD 75 | del %TARGETLIB% 76 | 77 | %LJCOMPILE% ljamalg.c 78 | @if errorlevel 1 goto :BAD 79 | %LJLIB%%TARGETLIB% ljamalg.o lj_vm.o 80 | @if errorlevel 1 goto :BAD 81 | 82 | @del *.o *.obj *.manifest minilua.exe buildvm.exe 83 | @echo. 84 | @echo === Successfully built LuaJIT for PS Vita === 85 | 86 | @goto :END 87 | :BAD 88 | @echo. 89 | @echo ******************************************************* 90 | @echo *** Build FAILED -- Please check the error messages *** 91 | @echo ******************************************************* 92 | @goto :END 93 | :FAIL 94 | @echo To run this script you must open a "Visual Studio .NET Command Prompt" 95 | @echo (32 bit host compiler). The PS Vita SDK must be installed, too. 96 | :END 97 | -------------------------------------------------------------------------------- /src/luajit/src/xb1build.bat: -------------------------------------------------------------------------------- 1 | @rem Script to build LuaJIT with the Xbox One SDK. 2 | @rem Donated to the public domain. 3 | @rem 4 | @rem Open a "Visual Studio .NET Command Prompt" (64 bit host compiler) 5 | @rem Then cd to this directory and run this script. 6 | 7 | @if not defined INCLUDE goto :FAIL 8 | @if not defined DurangoXDK goto :FAIL 9 | 10 | @setlocal 11 | @echo ---- Host compiler ---- 12 | @set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE 13 | @set LJLINK=link /nologo 14 | @set LJMT=mt /nologo 15 | @set DASMDIR=..\dynasm 16 | @set DASM=%DASMDIR%\dynasm.lua 17 | @set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c 18 | 19 | %LJCOMPILE% host\minilua.c 20 | @if errorlevel 1 goto :BAD 21 | %LJLINK% /out:minilua.exe minilua.obj 22 | @if errorlevel 1 goto :BAD 23 | if exist minilua.exe.manifest^ 24 | %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe 25 | 26 | @rem Error out for 64 bit host compiler 27 | @minilua 28 | @if not errorlevel 8 goto :FAIL 29 | 30 | @set DASMFLAGS=-D WIN -D FFI -D P64 31 | minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x64.dasc 32 | @if errorlevel 1 goto :BAD 33 | 34 | if exist ..\.git ( git show -s --format=%%ct >luajit_relver.txt ) else ( type ..\.relver >luajit_relver.txt ) 35 | minilua host\genversion.lua 36 | 37 | %LJCOMPILE% /I "." /I %DASMDIR% /D_DURANGO host\buildvm*.c 38 | @if errorlevel 1 goto :BAD 39 | %LJLINK% /out:buildvm.exe buildvm*.obj 40 | @if errorlevel 1 goto :BAD 41 | if exist buildvm.exe.manifest^ 42 | %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe 43 | 44 | buildvm -m peobj -o lj_vm.obj 45 | @if errorlevel 1 goto :BAD 46 | buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% 47 | @if errorlevel 1 goto :BAD 48 | buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% 49 | @if errorlevel 1 goto :BAD 50 | buildvm -m libdef -o lj_libdef.h %ALL_LIB% 51 | @if errorlevel 1 goto :BAD 52 | buildvm -m recdef -o lj_recdef.h %ALL_LIB% 53 | @if errorlevel 1 goto :BAD 54 | buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% 55 | @if errorlevel 1 goto :BAD 56 | buildvm -m folddef -o lj_folddef.h lj_opt_fold.c 57 | @if errorlevel 1 goto :BAD 58 | 59 | @echo ---- Cross compiler ---- 60 | 61 | @set CWD=%cd% 62 | @call "%DurangoXDK%\xdk\DurangoVars.cmd" XDK 63 | @cd /D "%CWD%" 64 | @shift 65 | 66 | @set LJCOMPILE="cl" /nologo /c /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /D_LIB /D_UNICODE /D_DURANGO 67 | @set LJLIB="lib" /nologo 68 | 69 | @if "%1"=="debug" ( 70 | @shift 71 | @set LJCOMPILE=%LJCOMPILE% /Zi /MDd /Od 72 | @set LJLINK=%LJLINK% /debug 73 | ) else ( 74 | @set LJCOMPILE=%LJCOMPILE% /MD /O2 /DNDEBUG 75 | ) 76 | 77 | @if "%1"=="amalg" goto :AMALG 78 | %LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c 79 | @if errorlevel 1 goto :BAD 80 | %LJLIB% /OUT:luajit.lib lj_*.obj lib_*.obj 81 | @if errorlevel 1 goto :BAD 82 | @goto :NOAMALG 83 | :AMALG 84 | %LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c 85 | @if errorlevel 1 goto :BAD 86 | %LJLIB% /OUT:luajit.lib ljamalg.obj lj_vm.obj 87 | @if errorlevel 1 goto :BAD 88 | :NOAMALG 89 | 90 | @del *.obj *.manifest minilua.exe buildvm.exe 91 | @echo. 92 | @echo === Successfully built LuaJIT for Xbox One === 93 | 94 | @goto :END 95 | :BAD 96 | @echo. 97 | @echo ******************************************************* 98 | @echo *** Build FAILED -- Please check the error messages *** 99 | @echo ******************************************************* 100 | @goto :END 101 | :FAIL 102 | @echo To run this script you must open a "Visual Studio .NET Command Prompt" 103 | @echo (64 bit host compiler). The Xbox One SDK must be installed, too. 104 | :END 105 | -------------------------------------------------------------------------------- /src/luajit/src/xedkbuild.bat: -------------------------------------------------------------------------------- 1 | @rem Script to build LuaJIT with the Xbox 360 SDK. 2 | @rem Donated to the public domain. 3 | @rem 4 | @rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler) 5 | @rem Then cd to this directory and run this script. 6 | 7 | @if not defined INCLUDE goto :FAIL 8 | @if not defined XEDK goto :FAIL 9 | 10 | @setlocal 11 | @rem ---- Host compiler ---- 12 | @set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE 13 | @set LJLINK=link /nologo 14 | @set LJMT=mt /nologo 15 | @set DASMDIR=..\dynasm 16 | @set DASM=%DASMDIR%\dynasm.lua 17 | @set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c 18 | 19 | %LJCOMPILE% host\minilua.c 20 | @if errorlevel 1 goto :BAD 21 | %LJLINK% /out:minilua.exe minilua.obj 22 | @if errorlevel 1 goto :BAD 23 | if exist minilua.exe.manifest^ 24 | %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe 25 | 26 | @rem Error out for 64 bit host compiler 27 | @minilua 28 | @if errorlevel 8 goto :FAIL 29 | 30 | @set DASMFLAGS=-D GPR64 -D FRAME32 -D PPE -D SQRT -D DUALNUM 31 | minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_ppc.dasc 32 | @if errorlevel 1 goto :BAD 33 | 34 | if exist ..\.git ( git show -s --format=%%ct >luajit_relver.txt ) else ( type ..\.relver >luajit_relver.txt ) 35 | minilua host\genversion.lua 36 | 37 | %LJCOMPILE% /I "." /I %DASMDIR% /D_XBOX_VER=200 /DLUAJIT_TARGET=LUAJIT_ARCH_PPC host\buildvm*.c 38 | @if errorlevel 1 goto :BAD 39 | %LJLINK% /out:buildvm.exe buildvm*.obj 40 | @if errorlevel 1 goto :BAD 41 | if exist buildvm.exe.manifest^ 42 | %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe 43 | 44 | buildvm -m peobj -o lj_vm.obj 45 | @if errorlevel 1 goto :BAD 46 | buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% 47 | @if errorlevel 1 goto :BAD 48 | buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% 49 | @if errorlevel 1 goto :BAD 50 | buildvm -m libdef -o lj_libdef.h %ALL_LIB% 51 | @if errorlevel 1 goto :BAD 52 | buildvm -m recdef -o lj_recdef.h %ALL_LIB% 53 | @if errorlevel 1 goto :BAD 54 | buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% 55 | @if errorlevel 1 goto :BAD 56 | buildvm -m folddef -o lj_folddef.h lj_opt_fold.c 57 | @if errorlevel 1 goto :BAD 58 | 59 | @rem ---- Cross compiler ---- 60 | @set LJCOMPILE="%XEDK%\bin\win32\cl" /nologo /c /MT /O2 /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /DNDEBUG /D_XBOX /D_LIB /DLUAJIT_USE_SYSMALLOC 61 | @set LJLIB="%XEDK%\bin\win32\lib" /nologo 62 | @set "INCLUDE=%XEDK%\include\xbox" 63 | 64 | @if "%1" neq "debug" goto :NODEBUG 65 | @shift 66 | @set "LJCOMPILE=%LJCOMPILE% /Zi" 67 | :NODEBUG 68 | @if "%1"=="amalg" goto :AMALG 69 | %LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c 70 | @if errorlevel 1 goto :BAD 71 | %LJLIB% /OUT:luajit20.lib lj_*.obj lib_*.obj 72 | @if errorlevel 1 goto :BAD 73 | @goto :NOAMALG 74 | :AMALG 75 | %LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c 76 | @if errorlevel 1 goto :BAD 77 | %LJLIB% /OUT:luajit20.lib ljamalg.obj lj_vm.obj 78 | @if errorlevel 1 goto :BAD 79 | :NOAMALG 80 | 81 | @del *.obj *.manifest minilua.exe buildvm.exe 82 | @echo. 83 | @echo === Successfully built LuaJIT for Xbox 360 === 84 | 85 | @goto :END 86 | :BAD 87 | @echo. 88 | @echo ******************************************************* 89 | @echo *** Build FAILED -- Please check the error messages *** 90 | @echo ******************************************************* 91 | @goto :END 92 | :FAIL 93 | @echo To run this script you must open a "Visual Studio .NET Command Prompt" 94 | @echo (32 bit host compiler). The Xbox 360 SDK must be installed, too. 95 | :END 96 | -------------------------------------------------------------------------------- /src/luajit_build.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** LuaJIT -- a Just-In-Time Compiler for Lua. https://luajit.org/ 3 | ** 4 | ** Copyright (C) 2005-2025 Mike Pall. All rights reserved. 5 | ** 6 | ** Permission is hereby granted, free of charge, to any person obtaining 7 | ** a copy of this software and associated documentation files (the 8 | ** "Software"), to deal in the Software without restriction, including 9 | ** without limitation the rights to use, copy, modify, merge, publish, 10 | ** distribute, sublicense, and/or sell copies of the Software, and to 11 | ** permit persons to whom the Software is furnished to do so, subject to 12 | ** the following conditions: 13 | ** 14 | ** The above copyright notice and this permission notice shall be 15 | ** included in all copies or substantial portions of the Software. 16 | ** 17 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | ** 25 | ** [ MIT license: https://www.opensource.org/licenses/mit-license.php ] 26 | */ 27 | 28 | #ifndef _LUAJIT_H 29 | #define _LUAJIT_H 30 | 31 | #include "lua.h" 32 | 33 | #define LUAJIT_VERSION "LuaJIT 2.1.1744318430" 34 | #define LUAJIT_VERSION_NUM 20199 /* Deprecated. */ 35 | #define LUAJIT_VERSION_SYM luaJIT_version_2_1_1744318430 36 | #define LUAJIT_COPYRIGHT "Copyright (C) 2005-2025 Mike Pall" 37 | #define LUAJIT_URL "https://luajit.org/" 38 | 39 | /* Modes for luaJIT_setmode. */ 40 | #define LUAJIT_MODE_MASK 0x00ff 41 | 42 | enum { 43 | LUAJIT_MODE_ENGINE, /* Set mode for whole JIT engine. */ 44 | LUAJIT_MODE_DEBUG, /* Set debug mode (idx = level). */ 45 | 46 | LUAJIT_MODE_FUNC, /* Change mode for a function. */ 47 | LUAJIT_MODE_ALLFUNC, /* Recurse into subroutine protos. */ 48 | LUAJIT_MODE_ALLSUBFUNC, /* Change only the subroutines. */ 49 | 50 | LUAJIT_MODE_TRACE, /* Flush a compiled trace. */ 51 | 52 | LUAJIT_MODE_WRAPCFUNC = 0x10, /* Set wrapper mode for C function calls. */ 53 | 54 | LUAJIT_MODE_MAX 55 | }; 56 | 57 | /* Flags or'ed in to the mode. */ 58 | #define LUAJIT_MODE_OFF 0x0000 /* Turn feature off. */ 59 | #define LUAJIT_MODE_ON 0x0100 /* Turn feature on. */ 60 | #define LUAJIT_MODE_FLUSH 0x0200 /* Flush JIT-compiled code. */ 61 | 62 | /* LuaJIT public C API. */ 63 | 64 | /* Control the JIT engine. */ 65 | LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode); 66 | 67 | /* Low-overhead profiling API. */ 68 | typedef void (*luaJIT_profile_callback)(void *data, lua_State *L, 69 | int samples, int vmstate); 70 | LUA_API void luaJIT_profile_start(lua_State *L, const char *mode, 71 | luaJIT_profile_callback cb, void *data); 72 | LUA_API void luaJIT_profile_stop(lua_State *L); 73 | LUA_API const char *luaJIT_profile_dumpstack(lua_State *L, const char *fmt, 74 | int depth, size_t *len); 75 | 76 | /* Enforce (dynamic) linker error for version mismatches. Call from main. */ 77 | LUA_API void LUAJIT_VERSION_SYM(void); 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/registry_entry.cpp: -------------------------------------------------------------------------------- 1 | #include "registry_entry.h" 2 | extern "C" { 3 | #include "lua.h" 4 | } 5 | #define R_NO_REMAP 6 | #include 7 | #include 8 | 9 | // Disarm all RegistryEntries within Lua state L. 10 | void RegistryEntry::DisarmAll(lua_State* L) 11 | { 12 | // Get luajrx table from registry on stack 13 | lua_getfield(L, LUA_REGISTRYINDEX, "luajrx"); 14 | 15 | // Iterate through all entries, disarming each 16 | lua_pushnil(L); 17 | while (lua_next(L, -2) != 0) 18 | { 19 | RegistryEntry* re = reinterpret_cast(lua_touserdata(L, -2)); 20 | re->l = 0; 21 | lua_pop(L, 1); 22 | } 23 | } 24 | 25 | // Destroy a registry entry pointed to by an R external pointer when it is no 26 | // longer needed (i.e. at program exit or garbage collection of the R pointer). 27 | void RegistryEntry::Finalize(SEXP xptr) 28 | { 29 | delete reinterpret_cast(R_ExternalPtrAddr(xptr)); 30 | R_ClearExternalPtr(xptr); 31 | } 32 | 33 | // Create a registry entry, registering and popping the value at the top of the stack. 34 | RegistryEntry::RegistryEntry(lua_State* L) 35 | : l(L) 36 | { 37 | lua_getfield(l, LUA_REGISTRYINDEX, "luajrx"); // Get luajrx table from registry on stack 38 | lua_insert(l, -2); // Move luajrx table below value 39 | lua_pushlightuserdata(l, (void*)this); // Push registry key to stack 40 | lua_insert(l, -2); // Move registry key below value on stack 41 | lua_rawset(l, -3); // Set value in luajrx table; pops key & value 42 | lua_pop(l, 1); // Pop luajrx 43 | } 44 | 45 | // Delete the registry entry. 46 | RegistryEntry::~RegistryEntry() 47 | { 48 | if (l == 0) return; 49 | lua_getfield(l, LUA_REGISTRYINDEX, "luajrx"); // Get luajrx table from registry on stack 50 | lua_pushlightuserdata(l, (void*)this); // Push registry key to stack 51 | lua_pushnil(l); // Push nil to stack 52 | lua_rawset(l, -3); // Erase value in luajrx table; pops key & nil 53 | lua_pop(l, 1); // Pop luajrx 54 | } 55 | 56 | // Put the registered value at the top of the stack. 57 | void RegistryEntry::Get() 58 | { 59 | if (l == 0) { lua_pushnil(l); return; } 60 | lua_getfield(l, LUA_REGISTRYINDEX, "luajrx"); // Get luajrx table from registry on stack 61 | lua_pushlightuserdata(l, (void*)this); // Push registry key to stack 62 | lua_rawget(l, -2); // Get value on stack 63 | lua_remove(l, -2); // Remove luajrx table from stack 64 | } 65 | 66 | // Is the internal state equal to this one? 67 | bool RegistryEntry::CheckState(lua_State* L) 68 | { 69 | return (l != 0) && (l == L); 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/registry_entry.h: -------------------------------------------------------------------------------- 1 | #ifndef REGISTRY_ENTRY_H 2 | #define REGISTRY_ENTRY_H 3 | 4 | // Forward declaration 5 | struct lua_State; 6 | struct SEXPREC; 7 | typedef struct SEXPREC* SEXP; 8 | 9 | // RegistryEntry: an entry on the Lua registry, which cleans itself up upon 10 | // destruction. First, push the desired value onto the top of the Lua stack, 11 | // then create a new RegistryEntry. To get the entry onto the 12 | // stack, use re->Get(). Note that RegistryEntry objects must be created by new, 13 | // as the logic relies upon the object always having the same address. 14 | class RegistryEntry 15 | { 16 | public: 17 | // Disarm all RegistryEntries within Lua state L. 18 | static void DisarmAll(lua_State* L); 19 | 20 | // Destroy a registry entry pointed to by an R external pointer when it is no 21 | // longer needed (i.e. at program exit or garbage collection of the R pointer). 22 | static void Finalize(SEXP xptr); 23 | 24 | // Create a registry entry, registering and popping the value at the top of the stack. 25 | RegistryEntry(lua_State* L); 26 | 27 | // No copy constructor. 28 | RegistryEntry(const RegistryEntry&) = delete; 29 | 30 | // No assignment operator. 31 | RegistryEntry& operator=(const RegistryEntry&) = delete; 32 | 33 | // Delete the registry entry. 34 | ~RegistryEntry(); 35 | 36 | // Put the registered value at the top of the stack. 37 | void Get(); 38 | 39 | // Is the internal state equal to this one? 40 | bool CheckState(lua_State* L); 41 | 42 | private: 43 | lua_State* l; // lua_State in which registry is stored 44 | }; 45 | 46 | #endif // REGISTRY_ENTRY_H 47 | -------------------------------------------------------------------------------- /src/shared.h: -------------------------------------------------------------------------------- 1 | // Forward declarations 2 | struct lua_State; 3 | struct SEXPREC; 4 | typedef SEXPREC* SEXP; 5 | 6 | // The shared global Lua state 7 | extern lua_State* L0; 8 | 9 | // luajr Lua module API registry keys 10 | extern int luajr_construct_ref; 11 | extern int luajr_construct_vec; 12 | extern int luajr_construct_list; 13 | extern int luajr_construct_null; 14 | extern int luajr_return_info; 15 | extern int luajr_return_copy; 16 | 17 | // We declare all functions to have C linkage to avoid name mangling and allow 18 | // the use of the package functions from C code. This file (shared.h) is only 19 | // included when building the R package, i.e. from C++, so no #ifdef __cplusplus 20 | // wrapper is needed here. 21 | extern "C" { 22 | 23 | // Declare luajr API functions in src/shared.h, inst/include/luajr.h, and inst/include/luajr_funcs.h. 24 | 25 | // Lua state related functions (state.cpp) 26 | SEXP luajr_locate_dylib(SEXP path); // Not in public API 27 | SEXP luajr_locate_module(SEXP path); // Not in public API 28 | SEXP luajr_locate_debugger(SEXP path); // Not in public API 29 | SEXP luajr_open(); 30 | SEXP luajr_reset(); 31 | lua_State* luajr_newstate(); 32 | lua_State* luajr_getstate(SEXP Lx); 33 | 34 | // Move values between R and Lua (push_to.cpp) 35 | void luajr_pushsexp(lua_State* L, SEXP x, char as); 36 | SEXP luajr_tosexp(lua_State* L, int index); 37 | void luajr_pass(lua_State* L, SEXP args, const char* acode); 38 | SEXP luajr_return(lua_State* L, int nret); 39 | 40 | // Run Lua code and functions (run_func.cpp) 41 | SEXP luajr_run_code(SEXP code, SEXP Lx); 42 | SEXP luajr_run_file(SEXP filename, SEXP Lx); 43 | SEXP luajr_func_create(SEXP func, SEXP Lx); 44 | SEXP luajr_func_call(SEXP fx, SEXP alist, SEXP acode, SEXP Lx); 45 | void luajr_pushfunc(SEXP fx); 46 | 47 | // Run Lua code in parallel (parallel.cpp) 48 | SEXP luajr_run_parallel(SEXP func, SEXP n, SEXP threads, SEXP pre); 49 | 50 | // Load and call Lua code, and control tooling (tools.cpp) 51 | void luajr_loadstring(lua_State* L, const char* str); 52 | void luajr_dostring(lua_State* L, const char* str, int tooling); 53 | void luajr_loadfile(lua_State* L, const char* filename); 54 | void luajr_dofile(lua_State* L, const char* filename, int tooling); 55 | void luajr_loadbuffer(lua_State *L, const char *buff, unsigned int sz, const char *name); 56 | int luajr_pcall(lua_State* L, int nargs, int nresults, const char* what, int tooling); 57 | SEXP luajr_set_mode(SEXP debug, SEXP profile, SEXP jit); 58 | SEXP luajr_get_mode(); 59 | int luajr_debug_mode(); 60 | int luajr_profile_mode(); 61 | void luajr_profile_collect(lua_State* L); 62 | SEXP luajr_profile_data(SEXP flush); 63 | 64 | // Miscellaneous functions (setup.cpp) 65 | SEXP luajr_makepointer(void* ptr, int tag_code, void (*finalize)(SEXP)); 66 | void* luajr_getpointer(SEXP x, int tag_code); 67 | int luajr_handle_lua_error(lua_State* L, int err, const char* what, char* buf); // Not in public API 68 | SEXP luajr_readline(SEXP prompt); // Not in public API 69 | 70 | // Access to Lua C API (lua_internal.cpp) 71 | SEXP luajr_lua_gettop(SEXP Lx); // Not in public API 72 | 73 | } // end of extern "C" 74 | 75 | // Type codes, for use with the Lua FFI 76 | enum 77 | { 78 | LOGICAL_T = 0, INTEGER_T = 1, NUMERIC_T = 2, CHARACTER_T = 3, 79 | REFERENCE_T = 0, VECTOR_T = 4, LIST_T = 8, NULL_T = 16, 80 | }; 81 | 82 | // External pointer code tags, for use with luajr_makepointer and luajr_getpointer 83 | enum 84 | { 85 | // For luajr_func_create, luajr_pushfunc, and luajr_tosexp with functions 86 | LUAJR_REGFUNC_CODE = 0x7CA12E6F, 87 | 88 | // For luajr_open and luajr_getstate's use of external pointers 89 | LUAJR_STATE_CODE = 0x7CA57A7E 90 | }; 91 | 92 | 93 | #define CheckSEXP(x, type) if (TYPEOF(x) != type) { Rf_error("%s expects %s to be of type %s", __func__, #x, Rf_type2char(type)); } 94 | #define CheckSEXPLen(x, type, len) if (TYPEOF(x) != type || Rf_length(x) != len) { Rf_error("%s expects %s to be of length %d and type %s", __func__, #x, len, Rf_type2char(type)); } 95 | 96 | #include "../inst/include/luajr_const.h" 97 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | # This file is part of the standard setup for testthat. 2 | # It is recommended that you do not modify it. 3 | # 4 | # Where should you do additional test configuration? 5 | # Learn more about the roles of various files in: 6 | # * https://r-pkgs.org/testing-design.html#sec-tests-files-overview 7 | # * https://testthat.r-lib.org/articles/special-files.html 8 | 9 | library(testthat) 10 | library(luajr) 11 | 12 | test_check("luajr") 13 | -------------------------------------------------------------------------------- /tests/testthat/files/root2.lua: -------------------------------------------------------------------------------- 1 | return math.sqrt(2) 2 | -------------------------------------------------------------------------------- /tests/testthat/test-attributes.R: -------------------------------------------------------------------------------- 1 | test_that("attributes work", { 2 | x = TRUE; attr(x, 'a') = 1L; 3 | expect_identical(lua("x = luajr.logical_r(1, true); x('a', luajr.integer_r(1, 1)); return x"), x) 4 | 5 | x = 1L; attr(x, 'a') = 1; 6 | expect_identical(lua("x = luajr.integer_r(1, 1); x('a', luajr.numeric_r(1, 1)); return x"), x) 7 | 8 | x = 1; attr(x, 'a') = 'a'; 9 | expect_identical(lua("x = luajr.numeric_r(1, 1); x('a', luajr.character_r(1, 'a')); return x"), x) 10 | 11 | x = 'a'; attr(x, 'a') = TRUE; 12 | expect_identical(lua("x = luajr.character_r(1, 'a'); x('a', luajr.logical_r(1, true)); return x"), x) 13 | 14 | expect_identical(lua("return x('a')"), TRUE) 15 | 16 | lua_reset() 17 | }) 18 | -------------------------------------------------------------------------------- /tests/testthat/test-list.R: -------------------------------------------------------------------------------- 1 | test_that("list index and length works", { 2 | lua("x = luajr.list()") 3 | lua("x[1] = 1") 4 | lua("x[2] = 0") 5 | lua("x.three = 0") 6 | lua("x.four = 4") 7 | lua("x[2] = 2") 8 | lua("x[3] = 3") 9 | expect_identical(lua("return #x"), 4) 10 | lua("x.three = nil") 11 | expect_identical(lua("return #x"), 3) 12 | lua("x[1] = nil") 13 | expect_identical(lua("return #x"), 2) 14 | expect_identical(lua("return x"), list(2, four = 4)) 15 | 16 | expect_identical(lua("return x[1]"), 2) 17 | expect_identical(lua("return x[2]"), 4) 18 | expect_identical(lua("return x.four"), 4) 19 | expect_null(lua("return x[3]")) 20 | expect_null(lua("return x[4]")) 21 | 22 | lua_reset() 23 | }) 24 | 25 | test_that("list attributes work", { 26 | lua("x = luajr.list()") 27 | lua("x.a = 'eh'") 28 | lua("x.b = 'be'") 29 | 30 | lua("x('at1', luajr.logical_r({ true }))") 31 | lua("x('at2', luajr.integer_r({ 1 }))") 32 | lua("x('at3', luajr.numeric_r({ 0.5 }))") 33 | lua("x('at4', luajr.character_r(3, 'hi'))") 34 | lua("x('at5', luajr.list())") 35 | 36 | x = lua("return x") 37 | expect_mapequal(attributes(x), list(at1 = TRUE, at2 = 1, at3 = 0.5, 38 | at4 = rep("hi", 3), at5 = list(), names = c("a", "b"))) 39 | 40 | lua_reset() 41 | }) 42 | -------------------------------------------------------------------------------- /tests/testthat/test-lua.R: -------------------------------------------------------------------------------- 1 | test_that("lua() returns basic types", { 2 | # These are all testing luajr_tosexp() as well as lua() 3 | 4 | # Test return of scalar types nil, boolean, number, string 5 | expect_null(lua("return glarblefleegle")) # Undefined variable should equate to nil -> NULL 6 | expect_identical(lua("return 2 > 1"), TRUE) # true condition -> TRUE 7 | expect_equal(lua("return math.pi*2"), 2*pi) # also testing namespace lookup and math ops 8 | expect_identical(lua("return 'Hello ' .. 'World!'"), "Hello World!") 9 | 10 | # Test return of various tables 11 | # Empty table -> empty list 12 | expect_identical(lua("return {}"), list()) 13 | # Array-like table -> array-like list 14 | expect_identical(lua("return {1, 2, 3}"), list(1, 2, 3)) 15 | # Named table -> Named list, but keys can be in any order (hence mapequal) 16 | expect_mapequal(lua("return {a = 1, b = 2}"), list(a = 1, b = 2)) 17 | # Table with both array and record parts -> array parts seem to always be first 18 | expect_identical(unname(lua("return {1, a = 2, 3, b = 4, 5}")[1:3]), list(1, 3, 5)) 19 | 20 | # Test return of Lua types that we convert to a pointer (see luajr_tosexp) 21 | expect_type(lua("return print"), "externalptr") 22 | }) 23 | 24 | test_that("lua() can run a file", { 25 | # Just checking override "filename" option in lua() 26 | # root2.lua returns math.sqrt(2) 27 | expect_equal(lua(filename = test_path("files", "root2.lua")), 2^0.5) 28 | }) 29 | 30 | test_that("lua() produces errors", { 31 | # Identifier on its own is a parse error 32 | expect_error(lua("a"), "'=' expected near ''") 33 | }) 34 | -------------------------------------------------------------------------------- /tests/testthat/test-parallel.R: -------------------------------------------------------------------------------- 1 | test_that("parallel code works", { 2 | # This is just a basic test to ensure the features of lua_parallel() work 3 | # while its interface is still being developed. 4 | expect_identical( 5 | lua_parallel("test", n = 8, threads = 4, pre = "test = function(i) return i * 2 end"), 6 | as.list(c(2, 4, 6, 8, 10, 12, 14, 16)) 7 | ) 8 | 9 | thr = list(lua_open(), lua_open(), lua_open(), NULL) 10 | expect_identical( 11 | lua_parallel("test", n = 8, threads = thr, pre = "test = function(i) return i end"), 12 | as.list(c(1, 2, 3, 4, 5, 6, 7, 8)) 13 | ) 14 | }) 15 | -------------------------------------------------------------------------------- /tests/testthat/test-state.R: -------------------------------------------------------------------------------- 1 | test_that("states can be opened", { 2 | # Open a new state and check its type 3 | L2 = lua_open() 4 | expect_type(L2, "externalptr") 5 | 6 | # Verify variables can be set within a state. 7 | lua("a = 2", L = L2) 8 | expect_identical(lua("return a", L = L2), 2) 9 | }) 10 | 11 | test_that("states are independent", { 12 | # Set the same variable in two different states and verify independence 13 | L2 = lua_open() 14 | 15 | lua("a = 2") 16 | lua("a = 3", L = L2) 17 | 18 | expect_identical(lua("return a"), 2) 19 | expect_identical(lua("return a", L = L2), 3) 20 | }) 21 | 22 | test_that("state pointers can be shallow copied", { 23 | # Set animal = "dog" in new state L2 24 | L2 = lua_open() 25 | lua("animal = 'dog'", L = L2) 26 | 27 | # Shallow copy L2 to L3 28 | L3 = L2 29 | 30 | # Verify set in L3, even after attempt to erase L2 31 | rm(L2) 32 | gc() 33 | expect_identical(lua("return animal", L = L3), "dog") 34 | }) 35 | 36 | test_that("states can be reset", { 37 | # Reset the default state 38 | lua("animal = 'fish'") 39 | lua_reset() 40 | expect_null(lua("return animal")) 41 | 42 | # 'Reset' a created state 43 | L2 = lua_open() 44 | lua("animal = 'cat'", L = L2) 45 | L2 = lua_open() 46 | expect_null(lua("return animal")) 47 | }) 48 | -------------------------------------------------------------------------------- /tests/testthat/test-types.R: -------------------------------------------------------------------------------- 1 | test_that("returning R value types works", { 2 | expect_identical(lua("return luajr.logical(3, true)"), c(TRUE, TRUE, TRUE)) 3 | expect_identical(lua("return luajr.integer({1, 2, 3})"), c(1L, 2L, 3L)) 4 | expect_equal(lua("return luajr.numeric(1, math.pi)"), pi) 5 | expect_identical(lua("return luajr.character({ 'well', 'I', 'never', '!' })"), c("well", "I", "never", "!")) 6 | expect_identical(lua("local l = luajr.list(); l[1] = 'moo'; l.bar = 'bark'; l.baz = luajr.numeric({1,2,3}); return l"), 7 | list("moo", bar = "bark", baz = c(1,2,3))) 8 | }) 9 | 10 | test_that("returning R reference types works", { 11 | expect_identical(lua("return luajr.logical_r(1, false)"), FALSE) 12 | expect_identical(lua("return luajr.integer_r(2, 2)"), c(2L, 2L)) 13 | expect_identical(lua("return luajr.numeric_r(2, 0.5)"), c(0.5, 0.5)) 14 | expect_identical(lua("return luajr.character_r(3, 'hi')"), rep('hi', 3)) 15 | }) 16 | 17 | test_that("extra types work", { 18 | # luajr.dataframe 19 | lua("x = luajr.dataframe()") 20 | lua("x.l = luajr.logical({true, false})") 21 | lua("x.i = luajr.integer({1, 2})") 22 | lua("x.r = luajr.numeric({1.1, 2.2})") 23 | lua("x.c = luajr.character({'hi', 'lo'})") 24 | expect_identical(lua("return x"), data.frame(l = c(TRUE, FALSE), i = c(1L, 2L), r = c(1.1, 2.2), c = c("hi", "lo"))) 25 | 26 | # luajr.matrix_r 27 | lua("x = luajr.matrix_r(3, 3)") 28 | lua("x[1] = 1") 29 | lua("x[5] = 1") 30 | lua("x[9] = 1") 31 | expect_identical(lua("return x"), diag(3)) 32 | 33 | # luajr.datamatrix_r 34 | lua("x = luajr.datamatrix_r(3, 3, {'a', 'b', 'c'})") 35 | lua("x[1] = 1") 36 | lua("x[5] = 1") 37 | lua("x[9] = 1") 38 | eyes = diag(3) 39 | colnames(eyes) = letters[1:3] 40 | expect_identical(lua("return x"), eyes) 41 | 42 | lua_reset() 43 | }) 44 | -------------------------------------------------------------------------------- /tools/luajit_relver.txt: -------------------------------------------------------------------------------- 1 | 1744318430 2 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | --------------------------------------------------------------------------------