Compare commits

..

71 Commits

Author SHA1 Message Date
ZeroSkill1
cf1cb1cfc5 fix a few cases of improper varg usage in luaL_error 2025-04-26 14:38:59 +02:00
ihaveahax
9ae4351bce
Lua documentation fixes (#910)
* lua-doc: Fix fs.find_all documentation

It returns an array, not a string. Also document return information.

* lua-doc: Remove TODO for fs.verify_with_sha_file

No reason to add fs.read_file errors here as that function gets
pcall-ed. If it fails, nil gets returned.

* lua-doc: Consistently refer to tables/arrays as tables

That's what they're called within Lua all the time.

* lua-doc: fix incorrect references to util module
2025-04-26 14:38:25 +02:00
ihaveahax
99f1abd7a4 lua io: fix incorrect mode detection
string.find has pattern matching by default, so it was incorrectly
reading "r+" when the mode was supposed to be "r". So this disables the
pattern matching and does a plain substring search.
2025-04-26 14:37:46 +02:00
ihaveahax
15eb3b1ebe
GM9Megascript: remove "Un-install Hax" (#916)
This option is confusing because despite it saying "will completely
remove CFW", it is not sufficient to actually fully remove custom
firmware. It doesn't, and can't, detect everything like region changes,
modified system titles, and other things that could brick a stock
console.

There have been numerous cases of bricked consoles because someone saw
this, thinking it would do everything needed to restore a console back
to an unmodified state, and getting a bricked console that needs a
flashcart purchase to fix.

(There is also a issue where, even if everything else was in order, it
can still brick a console due to a bad SD card. For some reason it
copies the FIRM to the SD card, where it can potentially get corrupted
if a counterfeit card was used.)

A proper uninstall guide is required, and we have one here:
https://3ds.hacks.guide/uninstall-cfw
2025-04-26 14:36:36 +02:00
ihaveahax
4424c37a89
Fix fs.list_dir not closing directory (#902)
I forgot fvx_closedir oops!
2025-03-24 17:11:46 +01:00
TophattedWasTaken
bc84780036
Lua examples: Small error corrections (#898)
* HelloScript.lua: Correct typos

* Lua docs: fget corresponds to fs.read_file
2025-03-24 17:08:53 +01:00
d0k3
712df196f1 Updated 9th anniversary splash 2025-03-22 14:06:35 +01:00
d0k3
a20c955e2a Updated readme credits 2025-03-22 14:06:27 +01:00
Pk11
7b20581fce
Update translations with Lua strings (#893) 2025-03-21 13:00:41 +01:00
ihaveahax
ba0272ff5d
Lua support (#878)
* Test implementation of lua

* Trust that lua knows what its doing with this

Silence warnings

* actually update top screen when Thingy is called, disable unnecessary ShowPrompt calls

* readme

* change init for a simple test, print error on top screen too

* Readme

* change init for a simple test, print error on top screen too

* enable more lua libs, edit init.lua with string examples

* one more readme edit before bed

* Readme

* change init for a simple test, print error on top screen too

* enable more lua libs, edit init.lua with string examples

* make lua a proper file type, add test UI library with two functions, remove luacmd command

* remove old attempts at editing lauxlib and liolib

* README

* README

* FS lib, new UI stuff

* consistency with "type* ptr" maybe

* add custom package searcher, reset package.path

* new functions for UI including basic print output buffer, add "Lua scripts..." option to home/power menu

* build vram0.tar including subdirs of data

* move default path to GM9LUA_DEFAULT_PATH

* FS_FileGetData

* testfgd, add GM9VERSION global, update README, fix indentation

* FS_FileGetData will return a nil instead if it fails

* it's actually luaL_pushfail

* os

* use luaL_tolstring instead of lua_tostring

* fix test/remove debugging showprompt

* os.clock float attempt

* fix print for real

* fix swapped offset and size for FileGetData

* finish OS stuff

* fix os.clock

* shorten table in/out

* remove .vscode dir

* enum test

* support building without lua

* NO_LUA hides menu options (except when you directly select a lua file)

* update UI lib to better match the ideas on #1

* dockermake

* add DrawPNG function

* whoops its ShowPNG

* minor fixes, add DrawPNG

* fix AskPrompt, add all showprompts mentioned in #1

* add newly added functions to readme

* try to keep separate code and data

* update lua to 5.4.7

* remove test libraries now that i want to attempt to implement in a real api

* add nix flake for building

* add various lua functions, some of them taken from the old attempt but with new names

* add dev shell to flake.nix

* remove test lua scripts in root

* add test lua scripts in data/luascripts

* add a whole bunch more lua functions and stuff

* add more test lua scripts

* add more functions, add preload script, add test io compatibility module

* add more functions and test scripts

* more functions and stuff

* more functions and stuff

* more functions and stuff, plus a wip ctrcheck reimpl

* yet more functions and stuff

* even more functions and stuff

* command comparison table.ods

* update command comparison table.ods

* update command comparison table.ods

* Add files via upload

* update command comparison table.ods

* update ui.show_text to use DrawStringCenter, update ctrcheck rewrite

* Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs.

This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact.

* use the makefile definition

* add title module, move around some functions, update command comparison table

* add readme for lua

* remove liolib.c and loslib.c

* more functions and things, use CheckWritePermissionsLuaError in place of more manual checks, update command comparison table

* add missing constant

* set CURRDIR to nil instead of "(null)" if not found

* remove gm9enum (unused since the restart)

* add ui.check_key

* split fs module to lua overlay and _fs internal module, and add a check for fs.write_file in lua

* add fs.ask_select_file and fs.ask_select_dir

* add fs.key_dump, replace overwrite_all and append_all with overwrite and append

* add fs.cart_dump and sys.emu_base

* add ctrtool, update flake.lock

* add io append mode

* make sure io.open with write mode starts with an empty file, add os.remove and os.rename aliases

* properly implement os.remove compatibility

* add fs.verify_with_sha_file, fix PathIsDirectory by using stat instead of opendir

* add util.running_as_module (untested)

* move scripts over to https://github.com/ihaveamac/GM9-lua-script-experiments

* remove ods and dockermake.sh

* remove data/scripts

* add lua autorun (untested)

* fix syntax error

* add sys.check_embedded_backup

* remove accidental symlink

* add sys.check_raw_rtc

* fix ui.show_file_text_viewer not freeing memory or reporting an error if OOM happens

* add todo notes for ui

* work-in-progress lua doc

* formatting fix

* up heading level for all sections

* Revert "up heading level for all sections"

This reverts commit 6ef14b619536b4253e341ba40b4dea728358979d.

* separators

* fix name and error for fs.move

* do explicit permission checks in fs.move

* fix error string for fs.copy

* fix function name for fs.dir_info

* fix error string for fs.find and fs.find_not

* fix function name for fs.img_umount

* partial fs doc

* finished fs doc, string fixes for fs module

* document fs.cart_dump encrypted opt, remove stat from fs.verify_with_sha_file

* title doc

* sys doc, error string updates

* util doc

* add json.lua to lua-doc

* add fs.find_all

* add 3dstool to flake

* make fs.find_all recursive actually recursive, document fs.find_all

* add "for" to comparison table

* change fs.find to return nil if no path was found, instead of raising an error

* change ui.echo to automatically word wrap (untested)

* Revert "change ui.echo to automatically word wrap (untested)"

This reverts commit 2524e7707708e9818162c31f9f004b6301a3061b.

* switch devkitNix to upstream

* flake.lock: Update

Flake lock file updates:

• Updated input 'devkitNix':
    'github:ihaveamac/devkitNix/883d173b94e3da8dc4cc0860cdda8c36b738817c' (2024-12-05)
  → 'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
• Updated input 'devkitNix/nixpkgs':
    'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
  → 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'hax-nur':
    'github:ihaveamac/nur-packages/c570b3830f7dd4d655afb109300529c896cd8855' (2024-12-05)
  → 'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
• Updated input 'hax-nur/nixpkgs':
    'github:NixOS/nixpkgs/2c15aa59df0017ca140d9ba302412298ab4bf22a' (2024-12-02)
  → 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
  → 'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)

* flake.lock: Update

Flake lock file updates:

• Updated input 'devkitNix':
    'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
  → 'github:bandithedoge/devkitNix/a344b0200a044f2d2ff99685f13ff7c53106428e' (2025-02-06)
• Updated input 'devkitNix/nixpkgs':
    'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
  → 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)
• Updated input 'hax-nur':
    'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
  → 'github:ihaveamac/nur-packages/2ce890cab4e948109ad1ad82ba18e69240a0d352' (2025-02-06)
• Updated input 'hax-nur/nixpkgs':
    'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
  → 'github:NixOS/nixpkgs/8532db2a88ba56de9188af72134d93e39fd825f3' (2025-02-02)
• Added input 'hax-nur/treefmt-nix':
    'github:numtide/treefmt-nix/bebf27d00f7d10ba75332a0541ac43676985dea3' (2025-01-28)
• Added input 'hax-nur/treefmt-nix/nixpkgs':
    follows 'hax-nur/nixpkgs'
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)
  → 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)

* make devkitNix and hax-nur inputs follow nixpkgs

* Also dump section headers on .dis file

* prepare for upstream merge

* Restore original README.
* Remove flake, was only used for my own testing

* fix accidental removal of LIBS

* copy lua-doc.md into release archive

* README: update to mention Lua in place of GM9Script, add credits

* lua-doc: fix typo

* add sample HelloScript

* lua-doc: remove wip notice, since all gm9script features are replicated

* remove accidental inclusion of language.inl

* Fix mixture of tabs and spaces

* remove accidental nix leftover

* re-add @ for add2tar command

---------

Co-authored-by: luigoalma <luigoalma@hotmail.com>
Co-authored-by: Gruetzig <florianavilov@gmail.com>
Co-authored-by: Florian <88926852+Gruetzig@users.noreply.github.com>
Co-authored-by: Wolfvak <soherrera1@hotmail.com>
2025-03-21 08:25:04 +01:00
Wolfvak
50270a820c
Split up the ARM9 code and data sections (#883)
* Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs.

This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact.

* use the makefile definition

* Also dump section headers on .dis file
2025-03-15 14:25:23 +01:00
Pk11
61c79e3e3f Add Fusion font for Chinese 2025-03-13 13:37:54 +01:00
Pk11
ab222de6b1 Update translations
- New! Chinese (Simplified) 100%
- German 100%
- French 94%
- Dutch 46%
- Spanish 41%
- Italian 27%
- Ryukyuan 18%
- Indonesian still 100%, but changed
2025-03-13 13:37:54 +01:00
Wolfvak
143fcf0d6b Display hours in ETA if the remaining seconds is >= 3600 2025-03-12 17:12:02 +01:00
Wolfvak
157848c770 Add .vscode to the gitignore 2025-03-09 17:06:18 -03:00
Pk11
8cde50e091
Automate translation files (#880)
* Automate language.inl file

* Move version number to JSON file

Better than being a magic number somewhere in the code, source code gets it from source.json

* Automate creation of .trf translation files
2024-11-30 08:24:15 -03:00
profi200
105f4ae5f7
Increased CTRCARD clock from 4.18 to 13.4 MHz on ROM reads. (#873) 2024-11-26 19:32:51 -03:00
ZeroSkill1
9e7df4c52d 'update CI to use upload-artifacts v4' 2024-11-14 12:49:09 +01:00
ZeroSkill1
208f12bde7 Fix multi RedNAND 2x not being set up properly 2024-11-14 12:49:09 +01:00
Wolfvak
7b6b478582
Use VA start and end linker symbols instead of length (#865) 2024-06-19 16:19:45 -03:00
Fra
eee63dd155
Fix fcram boot bug (#864)
Fixed old bug that appeared regarding the fcram boot failing.
2024-06-15 15:37:11 -03:00
Ricca665
dab90a9162 Update Makefile
Fixes python3 not found when running make on windows
2024-04-04 15:25:50 +02:00
Luís Marques
26990ca23a
Key 0x2C is different per platform (#842)
Change KeyY to generate same KeyNormal on dev
2024-03-17 11:27:27 -03:00
Pk11
338a2aa98a
Fix crashing in script runner mode (#840) 2024-02-20 15:13:57 -03:00
Luís Marques
399740b50e
Undefined behavior fix on ticket.c (#838)
Stack garbage is always a luck of the draw
This was causing issues on non-LTO build.
LTO was just lucky.
2024-01-25 11:28:03 +01:00
ihaveahax
ad8b5e0a8c
arm9/arm11 Makefile: Fix building with spaces in path (#828)
This quotes $(CURDIR) which fixes an issue with building GM9 in a path
with spaces, which Windows users are likely to have.
2023-08-25 21:58:22 -03:00
Wolfvak
14b390a943
Move the start of AHBWRAM to be immediately after the VECTORS (#827) 2023-08-23 10:51:35 -03:00
Wolfvak
031762a1fe
Embed the VRAM tar data in the ARM9 executable (#824)
* Embed the VRAM tar data in the ARM9 executable

* Fix Makefile dependency order

* Use address difference instead of absolute word for the VRAM drive limit
2023-07-24 10:17:37 -03:00
Danny Tsai
8b362c977a
Correct installable address whitelist (#816)
* Correct installable address whitelist

blacklist is stored at 0xb088~0xb0bf, which is ulong[14] in
{start(inclusive), end(exclusive)} pair.
one thing to note is that boot9 use inclusive comparing with
blacklist start for both section load address and
section load address + section size (comparing logic is
at 0xa42e~0xa449), so if the firm fits perfectly at the end
of the space right before the blacklisted range,
it'll also be rejected.

* shrink vram drive size to avoid bleeding into blacklisted range
2023-07-21 21:39:08 -03:00
Nemris
11b05d7a3d Fix leftover typo 2023-07-20 12:58:36 +02:00
Nemris
3008bfed61 Reduce noise and drop dataclasses 2023-07-20 12:58:36 +02:00
Nemris
f7a9b3eec8 Refactor to improve modularity
This commit adds documentation and type annotations, and allows the
script to be imported as a module.
2023-07-20 12:58:36 +02:00
Pk11
e1fa23a031 Update translations
- Spanish: 31%
- Japanese (kana only): New, 27%
- Ryukyuan (Uchinaaguchi): New, 18%
2023-07-10 16:35:42 +02:00
Pk11
8006fbd5db Update translations
- Improvements to Indonesian
2023-07-10 16:35:42 +02:00
Pk11
b4f04d3620 Update translations
- Indonesian: New and DONE!
- Italian: New, 11%
- Spanish, French, Dutch: Fairly minor improvements
2023-07-10 16:35:42 +02:00
Pk11
eb37a21354 Remove untranslated trailing strings 2023-07-10 16:35:42 +02:00
Pk11
a68d7d0cb7 Update translations
Added the date/time and number formats
I just did these myself since I wanted to test them more thoroughly, based on JavaScript's Intl
2023-07-10 16:35:42 +02:00
Pk11
87f9b82cd3 Update translations
- Added 3 new strings, only Japanese translated so far
2023-07-10 16:35:42 +02:00
Pk11
a293c008d5 Update translations
- Japanese: Improved
- German: 66%
- French: 53%
- Dutch: 27%
2023-07-10 16:35:42 +02:00
Pk11
780016933c Update translations
Japanese 100%!!
2023-07-10 16:35:42 +02:00
Pk11
041ff61d41 Update translations
- Japanese: 71%
- French: 39%
- 10 strings edited
2023-07-10 16:35:42 +02:00
Pk11
1fd15657d8 Update default font, add Japanese font 2023-07-10 16:35:42 +02:00
Pk11
0971b3d9fa Add translations
Spanish, French, Japanese, Dutch, Polish, and Russian
2023-07-10 16:35:42 +02:00
Danny Tsai
b7c97af144
Update README.md to match current VRAM drive info (#815) 2023-06-02 18:13:09 -03:00
Wolfvak
723529e2d8
Fix compilation warnings on dkA r60 (#808)
Mostly just switched out the bound strn* functions for their unbound variants when the string is a known constant
2023-04-25 10:13:28 -03:00
Wolfvak
b5fca3bc7e
Use the correct MCU LED period for calculations (#807)
Fixes #772
2023-04-25 10:13:07 -03:00
Pk11
620e5061c5 Add font/language info to README, Pk11 to credits 2023-04-15 12:54:02 +02:00
Pk11
5aaac66eef Use sizeof in snprintf where possible, ensure UTF_BUFFER_BYTESIZE
This commit looks a lot bigger than it really is, I noticed a couple spots where with these issues so I ran a regex to find all possible occurrences and switched all that could be, after manually ensuring it was actually correct

Using sizeof on the buffer (as long as the buffer is a char *array*, not a pointer!!) greatly reduces the chance of something having the wrong size because of a later change to the buffer, notably a couple snprintfs were missed in the UTF_BUFFER_BYTESIZE change
2023-04-15 12:54:02 +02:00
Pk11
439e06334b Add language selection on first load & from HOME 2023-04-15 12:54:02 +02:00
Pk11
8303440c19 Add loading translations from TRF
Reduce pointer magic use

Both translations and fonts
2023-04-15 12:54:02 +02:00
Pk11
93ee590cad Make strings translatable 2023-04-15 12:54:02 +02:00
Pk11
cae3d272d3
Only reserve space in trimmed NDS files for RSA key if it exists (#804) 2023-04-09 15:38:35 -03:00
ZeroSkill1
64414e12ab fix buffer overflow
This would cause exceptions when encrypting/decrypting CIA files to
`0:/gm9/out`.
2023-03-23 16:57:47 +01:00
Pk11
9514755989 Update Cyrillic homoglyphs
Whoops, forgot to update a couple of the Cyrillic letters where I changed their Latin homoglyphs
2023-01-31 13:21:52 +01:00
Pk11
ccd21984b2 Remake ASCII part of font
Actually a little more than ASCII, everything that was previously from the Linux kernel font
Linux is GPL2, GM9 is GPL3, thus this is sorta kinda maybe in violation of that
It's complicated since typefaces cannot be copyrighted in many countries, so arguably it's fine
2023-01-31 13:21:52 +01:00
d0k3
a23ba0e14b Fix a typo 2022-11-28 09:12:56 +01:00
d0k3
3710ed975b Enable compatibility with mGBA RTS savegames 2022-11-28 09:11:57 +01:00
Pk11
9416ec5ac0 Make select prompt scroll if too many options
Also a little cleanup to the file browse one for consistency
2022-10-24 14:35:04 +02:00
Myriachan
c9d792cb27 Add Brazil to SysInfo serial number recognition. 2022-09-27 16:59:57 +02:00
d0k3
096e6c3cb7 Add padding byte value to cart info file
Fixes #780
2022-07-22 21:43:23 +02:00
Gabriel Marcano
b11194397a
use sizeof with snprintf when target is an array (#778)
- GCC warned of a case when the size specified in a snprintf call was
   larger than the size of a target buffer. To fix, when possible use
   sizeof() to match buffer size.
2022-06-23 17:37:19 -03:00
aspargas2
658c9b491c use --use-blx linker argument
makes firm about 500 bytes smaller for free
2022-04-16 18:40:47 -04:00
d0k3
f611b31c0c Sixth Anniversary splash 2022-03-22 19:42:06 +01:00
d0k3
c13bba4cfe Fixed a boatload of compiler warnings 2022-03-22 19:40:47 +01:00
d0k3
d95a606ec2 Fix bad trimming of certain NDS dumps
Fixes #763
2022-03-08 18:47:32 +01:00
Pk11
586d30fafa Improve DSiWare save generation 2022-02-21 23:57:02 -06:00
aspargas2
682b570ef7 scripting: don't initialize dynamic env vars until they're needed
this should fix the long delay on loading any script
2022-01-12 11:33:04 -05:00
aspargas2
8d1557191f fix conflicting move/copy flags 2022-01-08 19:07:53 -05:00
aspargas2
cb11db6f1b fix crashes when showing certain error messages 2021-12-02 18:33:53 -05:00
aspargas2
1554aac4e1 fix hexeditor instructions formatting 2021-12-02 14:22:13 -05:00
aspargas2
fdbca10773 add format attribute to printf-like functions 2021-12-01 17:07:46 -05:00
186 changed files with 48119 additions and 1512 deletions

View File

@ -35,7 +35,7 @@ jobs:
rm $ZIPNAME rm $ZIPNAME
echo "OUTNAME=${ZIPNAME%.zip}" >> $GITHUB_ENV echo "OUTNAME=${ZIPNAME%.zip}" >> $GITHUB_ENV
- uses: actions/upload-artifact@v2 - uses: actions/upload-artifact@v4
with: with:
name: ${{ env.OUTNAME }} name: ${{ env.OUTNAME }}
path: release/* path: release/*

7
.gitignore vendored
View File

@ -5,6 +5,7 @@
*.obj *.obj
*.elf *.elf
*.map *.map
*.dis
# Precompiled Headers # Precompiled Headers
*.gch *.gch
@ -35,10 +36,14 @@
# OS leftovers # OS leftovers
desktop.ini desktop.ini
.DS_Store
# Sublime files # Sublime files
*.sublime-* *.sublime-*
# Visual Studio Code files
.vscode
# Build directories # Build directories
/build /build
/output /output
@ -49,3 +54,5 @@ desktop.ini
# User additions # User additions
/zzz_backup /zzz_backup
/arm9/source/language.inl
*.trf

View File

@ -16,18 +16,28 @@ export RELDIR := release
export COMMON_DIR := ../common export COMMON_DIR := ../common
# Definitions for initial RAM disk # Definitions for initial RAM disk
VRAM_OUT := $(OUTDIR)/vram0.tar VRAM_TAR := $(OUTDIR)/vram0.tar
VRAM_DATA := data VRAM_DATA := data/*
VRAM_FLAGS := --make-new --path-limit 99 --size-limit 262144 VRAM_FLAGS := --make-new --path-limit 99
ifeq ($(NTRBOOT),1) ifeq ($(NTRBOOT),1)
VRAM_SCRIPTS := resources/gm9/scripts VRAM_SCRIPTS := resources/gm9/scripts/*
endif endif
# Definitions for translation files
JSON_FOLDER := resources/languages
TRF_FOLDER := resources/gm9/languages
SOURCE_JSON := $(JSON_FOLDER)/source.json
LANGUAGE_INL := arm9/source/language.inl
JSON_FILES := $(filter-out $(SOURCE_JSON),$(wildcard $(JSON_FOLDER)/*.json))
TRF_FILES := $(subst $(JSON_FOLDER),$(TRF_FOLDER),$(JSON_FILES:.json=.trf))
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
ifeq ($(TERM),cygwin) ifeq ($(TERM),cygwin)
PY3 := py -3 # Windows / CMD/PowerShell PY3 := py -3 # Windows / CMD/PowerShell
else else
PY3 := python3 # Windows / MSYS2 PY3 := py # Windows / MSYS2
endif endif
else else
PY3 := python3 # Unix-like PY3 := python3 # Unix-like
@ -40,18 +50,18 @@ export ASFLAGS := -g -x assembler-with-cpp $(INCLUDE)
export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \ export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \
-g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \ -g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \
-fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \ -fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \
-Wno-unused-function -Wno-format-truncation $(INCLUDE) -ffunction-sections -fdata-sections -Wno-unused-function -Wno-format-truncation -Wno-format-nonliteral $(INCLUDE) -ffunction-sections -fdata-sections
export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096 export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096
ELF := arm9/arm9.elf arm11/arm11.elf ELF := arm9/arm9_code.elf arm9/arm9_data.elf arm11/arm11.elf
.PHONY: all firm vram0 elf release clean .PHONY: all firm $(VRAM_TAR) elf release clean
all: firm all: firm
clean: clean:
@set -e; for elf in $(ELF); do \ @set -e; for elf in $(ELF); do \
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \ $(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
done done
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_OUT) @rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR) $(LANGUAGE_INL) $(TRF_FILES)
unmarked_readme: .FORCE unmarked_readme: .FORCE
@$(PY3) utils/unmark.py -f README.md data/README_internal.md @$(PY3) utils/unmark.py -f README.md data/README_internal.md
@ -75,29 +85,44 @@ release: clean unmarked_readme
@cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/ @cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/
@cp $(ELF) $(RELDIR)/elf @cp $(ELF) $(RELDIR)/elf
@cp $(CURDIR)/README.md $(RELDIR) @cp $(CURDIR)/README.md $(RELDIR)
@cp $(CURDIR)/resources/lua-doc.md $(RELDIR)/lua-doc.md
@cp -R $(CURDIR)/resources/gm9 $(RELDIR)/gm9 @cp -R $(CURDIR)/resources/gm9 $(RELDIR)/gm9
@cp -R $(CURDIR)/resources/sample $(RELDIR)/sample @cp -R $(CURDIR)/resources/sample $(RELDIR)/sample
@-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/* @-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/*
vram0: $(VRAM_TAR): $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA) $(VRAM_SCRIPTS)
@mkdir -p "$(OUTDIR)" @mkdir -p "$(@D)"
@echo "Creating $(VRAM_OUT)" @echo "Creating $@"
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_OUT) $(shell ls -d $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA)/* $(VRAM_SCRIPTS)) @$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_TAR) $(shell ls -d -1 $^)
$(LANGUAGE_INL): $(SOURCE_JSON)
@echo "Creating $@"
@$(PY3) utils/transcp.py $< $@
$(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json
@$(PY3) utils/transriff.py $< $@
%.elf: .FORCE %.elf: .FORCE
@echo "Building $@" @echo "Building $@"
@$(MAKE) --no-print-directory -C $(@D) @$(MAKE) --no-print-directory -C $(@D) $(@F)
firm: $(ELF) vram0 # Indicate a few explicit dependencies:
@test `wc -c <$(VRAM_OUT)` -le 262144 # The ARM9 data section depends on the VRAM drive
arm9/arm9_data.elf: $(VRAM_TAR) $(LANGUAGE_INL)
# And the code section depends on the data section being built already
arm9/arm9_code.elf: arm9/arm9_data.elf
firm: $(ELF) $(TRF_FILES)
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)") @mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
@echo "[FLAVOR] $(FLAVOR)" @echo "[FLAVOR] $(FLAVOR)"
@echo "[VERSION] $(VERSION)" @echo "[VERSION] $(VERSION)"
@echo "[BUILD] $(DBUILTL)" @echo "[BUILD] $(DBUILTL)"
@echo "[FIRM] $(FIRM)" @echo "[FIRM] $(FIRM)"
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -A 0x80C0000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy @$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
@echo "[FIRM] $(FIRMD)" @echo "[FIRM] $(FIRMD)"
@$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -A 0x80C0000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy @$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
vram0: $(VRAM_TAR) .FORCE # legacy target name
.FORCE: .FORCE:

View File

@ -1,4 +1,6 @@
LIBS ?=
OBJECTS := $(patsubst $(SOURCE)/%.s, $(BUILD)/%.o, \ OBJECTS := $(patsubst $(SOURCE)/%.s, $(BUILD)/%.o, \
$(patsubst $(SOURCE)/%.c, $(BUILD)/%.o, \ $(patsubst $(SOURCE)/%.c, $(BUILD)/%.o, \
$(call rwildcard, $(SOURCE), *.s *.c))) $(call rwildcard, $(SOURCE), *.s *.c)))
@ -11,11 +13,12 @@ all: $(TARGET).elf
.PHONY: clean .PHONY: clean
clean: clean:
@rm -rf $(BUILD) $(TARGET).elf $(TARGET).map @rm -rf $(BUILD) $(TARGET).elf $(TARGET).dis $(TARGET).map
$(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON) $(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON)
@mkdir -p "$(@D)" @mkdir -p "$(@D)"
@$(CC) $(LDFLAGS) $^ -o $@ @$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)
@$(OBJDUMP) -S -h $@ > $@.dis
$(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c $(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c
@mkdir -p "$(@D)" @mkdir -p "$(@D)"

View File

@ -1,3 +1,4 @@
export OBJDUMP := arm-none-eabi-objdump
dirname = $(shell dirname $(1)) dirname = $(shell dirname $(1))
@ -67,6 +68,10 @@ ifdef SD_TIMEOUT
CFLAGS += -DSD_TIMEOUT=$(SD_TIMEOUT) CFLAGS += -DSD_TIMEOUT=$(SD_TIMEOUT)
endif endif
ifeq ($(NO_LUA),1)
CFLAGS += -DNO_LUA
endif
ifdef N_PANES ifdef N_PANES
CFLAGS += -DN_PANES=$(N_PANES) CFLAGS += -DN_PANES=$(N_PANES)
endif endif

View File

@ -41,7 +41,7 @@ Build `GodMode9.firm` via `make firm`. This requires [firmtool](https://github.c
You may run `make release` to get a nice, release-ready package of all required files. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make FLAVOR=SafeMode9`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font by replacing `font_default.frf` inside the `data` directory. You may also hardcode the brightness via `make FIXED_BRIGHTNESS=x`, whereas `x` is a value between 0...15. You may run `make release` to get a nice, release-ready package of all required files. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make FLAVOR=SafeMode9`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font by replacing `font_default.frf` inside the `data` directory. You may also hardcode the brightness via `make FIXED_BRIGHTNESS=x`, whereas `x` is a value between 0...15.
Further customization is possible by hardcoding `aeskeydb.bin` (just put the file into the `data` folder when compiling). All files put into the `data` folder will turn up in the `V:` drive, but keep in mind there's a hard 3MB limit for all files inside, including overhead. A standalone script runner is compiled by providing `autorun.gm9` (again, in the `data` folder) and building with `make SCRIPT_RUNNER=1`. There's more possibility for customization, read the Makefiles to learn more. Further customization is possible by hardcoding `aeskeydb.bin` (just put the file into the `data` folder when compiling). All files put into the `data` folder will turn up in the `V:` drive, but keep in mind there's a hard 223.5KiB limit for all files inside, including overhead. A standalone script runner is compiled by providing `autorun.lua` or `autorun.gm9` (again, in the `data` folder) and building with `make SCRIPT_RUNNER=1`. There's more possibility for customization, read the Makefiles to learn more.
To build a .firm signed with SPI boot keys (for ntrboot and the like), run `make NTRBOOT=1`. You may need to rename the output files if the ntrboot installer you use uses hardcoded filenames. Some features such as boot9 / boot11 access are not currently available from the ntrboot environment. To build a .firm signed with SPI boot keys (for ntrboot and the like), run `make NTRBOOT=1`. You may need to rename the output files if the ntrboot installer you use uses hardcoded filenames. Some features such as boot9 / boot11 access are not currently available from the ntrboot environment.
@ -65,6 +65,10 @@ For certain functionality, GodMode9 may need 'support files'. Support files shou
* __`seeddb.bin`__: This file is optional and required to decrypt and mount seed-encrypted NCCHs and CIAs (if the seed in question is not installed to your NAND). Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt. * __`seeddb.bin`__: This file is optional and required to decrypt and mount seed-encrypted NCCHs and CIAs (if the seed in question is not installed to your NAND). Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt.
* __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to decrypt and install contents downloaded from CDN (for DSi and 3DS content). * __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to decrypt and install contents downloaded from CDN (for DSi and 3DS content).
### Fonts and translations
GodMode9 also supports custom fonts and translations as support files. These both use custom formats, fonts use FRF (Font RIFF) files which can be created using the `fontriff.py` Python script in the 'utils' folder. Translations use TRF (Translation RIFF) files from the `transriff.py` script. Examples of the inputs to these scripts can be found in the 'fonts' and 'languages' folders of the 'resources' folder respectively.
TRF files can be placed in `0:/gm9/languages` to show in the language menu accessible from the HOME menu and shown on first load. Official translations are provided from the community via the [GodMode9 Crowdin](https://crowdin.com/project/GodMode9). Languages can use a special font by having an FRF with the same name, for example `en.trf` and `en.frf`.
## Drives in GodMode9 ## Drives in GodMode9
GodMode9 provides access to system data via drives, a listing of what each drive contains and additional info follows below. Some of these drives are removable (such as drive `7:`), some will only turn up if they are available (drive `8:` and everything associated with EmuNAND, f.e.). Information on the 3DS console file system is also found on [3Dbrew.org](https://3dbrew.org/wiki/Flash_Filesystem). GodMode9 provides access to system data via drives, a listing of what each drive contains and additional info follows below. Some of these drives are removable (such as drive `7:`), some will only turn up if they are available (drive `8:` and everything associated with EmuNAND, f.e.). Information on the 3DS console file system is also found on [3Dbrew.org](https://3dbrew.org/wiki/Flash_Filesystem).
@ -88,7 +92,7 @@ GodMode9 provides access to system data via drives, a listing of what each drive
* __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only. * __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only.
* __`T: TICKET.DB IMAGE / BDRI IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This drive also provides access to other BDRI images, such as the Title database (`title.db`). * __`T: TICKET.DB IMAGE / BDRI IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This drive also provides access to other BDRI images, such as the Title database (`title.db`).
* __`M: MEMORY VIRTUAL`__: This provides access to various memory regions. This is protected by a special write permission, and caution is advised when doing modifications inside this drive. This drive also gives access to `boot9.bin`, `boot11.bin` (boot9strap only) and `otp.mem` (sighaxed systems only). * __`M: MEMORY VIRTUAL`__: This provides access to various memory regions. This is protected by a special write permission, and caution is advised when doing modifications inside this drive. This drive also gives access to `boot9.bin`, `boot11.bin` (boot9strap only) and `otp.mem` (sighaxed systems only).
* __`V: VRAM VIRTUAL`__: This drive resides in the first VRAM bank and contains files essential to GodMode9. The font (in FRF format), the splash logo (in PNG format) and the readme file are found there, as well as any file that is provided inside the `data` folder at build time. This is read-only. * __`V: VRAM VIRTUAL`__: This drive resides in part of ARM9 internal memory and contains files essential to GodMode9. The font (in FRF format), the splash logo (in PNG format) and the readme file are found there, as well as any file that is provided inside the `data` folder at build time. This is read-only.
* __`Y: TITLE MANAGER`__: The title manager is accessed via the HOME menu and provides easy access to all installed titles. * __`Y: TITLE MANAGER`__: The title manager is accessed via the HOME menu and provides easy access to all installed titles.
* __`Z: LAST SEARCH`__: After a search operation, search results are found inside this drive. The drive can be accessed at a later point to return to the former search results. * __`Z: LAST SEARCH`__: After a search operation, search results are found inside this drive. The drive can be accessed at a later point to return to the former search results.
@ -118,8 +122,9 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
* __Inject a file to another file__: Put exactly one file (the file to be injected from) into the clipboard (via the Y button). Press A on the file to be injected to. There will be an option to inject the first file into it. * __Inject a file to another file__: Put exactly one file (the file to be injected from) into the clipboard (via the Y button). Press A on the file to be injected to. There will be an option to inject the first file into it.
### Scripting functionality ### Scripting functionality
* __Run .gm9 scripts from anywhere on your SD card__: You can run scripts in .gm9 format via the A button menu. .gm9 scripts use a shell-like language and can be edited in any text editor. For an overview of usable commands have a look into the sample scripts included in the release archive. *Don't run scripts from untrusted sources.* * __Run .lua scripts from anywhere on your SD card__: You can run Lua scripts via the A button menu. For an overview of usable commands have a look into the documentation and sample scripts included in the release archive. *Don't run scripts from untrusted sources.*
* __Run .gm9 scripts via a neat menu__: Press the HOME button, select `More...` -> `Scripts...`. Any script you put into `0:/gm9/scripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either. * __Run Lua scripts via a neat menu__: Press the HOME button, select `More...` -> `Lua scripts...`. Any script you put into `0:/gm9/luascripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either.
* __Run legacy .gm9 scripts__: The old format of .gm9 scripts is still available, but is deprecated and will see no further development.
### SD card handling ### SD card handling
* __Format your SD card / setup an EmuNAND__: Press the HOME button, select `More...` -> `SD format menu`. This also allows to setup a RedNAND (single/multi) or GW type EmuNAND on your SD card. You will get a warning prompt and an unlock sequence before any operation starts. * __Format your SD card / setup an EmuNAND__: Press the HOME button, select `More...` -> `SD format menu`. This also allows to setup a RedNAND (single/multi) or GW type EmuNAND on your SD card. You will get a warning prompt and an unlock sequence before any operation starts.
@ -179,9 +184,13 @@ This tool would not have been possible without the help of numerous people. Than
* **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions * **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions
* **SciresM** for helping me figure out RomFS and for boot9strap * **SciresM** for helping me figure out RomFS and for boot9strap
* **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom * **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom
* **ihaveamac** for first developing the simple CIA generation method and for being of great help in porting it * **ihaveamac** for implementing Lua support, and first developing the simple CIA generation method and for being of great help in porting it
* **DarkRTA** for linker support during the implementation of Lua
* **luigoalma** for fixing Lua to compile without issues
* **Gruetzig** for re-implementing the Lua os module
* **wwylele** and **aspargas2** for documenting and implementing the DISA, DIFF, and BDRI formats * **wwylele** and **aspargas2** for documenting and implementing the DISA, DIFF, and BDRI formats
* **dratini0** for savefile management, based on [TWLSaveTool](https://github.com/TuxSH/TWLSaveTool/) * **dratini0** for savefile management, based on [TWLSaveTool](https://github.com/TuxSH/TWLSaveTool/)
* **Pk11** for unicode support and her ongoing work on GodMode9 translations and translation support
* **b1l1s** for helping me figure out A9LH compatibility * **b1l1s** for helping me figure out A9LH compatibility
* **Gelex** and **AuroraWright** for helping me figure out various things * **Gelex** and **AuroraWright** for helping me figure out various things
* **stuckpixel** for the new 6x10 font and help on various things * **stuckpixel** for the new 6x10 font and help on various things
@ -198,6 +207,7 @@ This tool would not have been possible without the help of numerous people. Than
* **Lilith Valentine** for testing and helpful advice * **Lilith Valentine** for testing and helpful advice
* **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator) * **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator)
* **Amazingmax fonts** for the Amazdoom font * **Amazingmax fonts** for the Amazdoom font
* **TakWolf** for [fusion-pixel-font](https://github.com/TakWolf/fusion-pixel-font) used for Chinese and Korean
* The fine folks on **the official GodMode9 IRC channel and Discord server** * The fine folks on **the official GodMode9 IRC channel and Discord server**
* The fine folks on **freenode #Cakey** * The fine folks on **freenode #Cakey**
* All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors** * All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors**

View File

@ -1,6 +1,6 @@
PROCESSOR := ARM11 PROCESSOR := ARM11
TARGET := $(shell basename $(CURDIR)) TARGET := $(shell basename "$(CURDIR)")
SOURCE := source SOURCE := source
BUILD := build BUILD := build
@ -11,7 +11,7 @@ INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
ASFLAGS += $(SUBARCH) $(INCLUDE) ASFLAGS += $(SUBARCH) $(INCLUDE)
CFLAGS += $(SUBARCH) $(INCLUDE) -flto CFLAGS += $(SUBARCH) $(INCLUDE) -flto
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
include ../Makefile.common include ../Makefile.common
include ../Makefile.build include ../Makefile.build

View File

@ -16,7 +16,7 @@ SECTIONS
__text_va = ABSOLUTE(.); __text_va = ABSOLUTE(.);
*(.text*) *(.text*)
. = ALIGN(4K); . = ALIGN(4K);
__text_len = . - __text_va; __text_va_end = .;
} >AXIWRAM } >AXIWRAM
.data : ALIGN(4K) .data : ALIGN(4K)
@ -25,7 +25,7 @@ SECTIONS
__data_va = ABSOLUTE(.); __data_va = ABSOLUTE(.);
*(.data*) *(.data*)
. = ALIGN(4K); . = ALIGN(4K);
__data_len = . - __data_va; __data_va_end = .;
} >AXIWRAM } >AXIWRAM
.rodata : ALIGN(4K) .rodata : ALIGN(4K)
@ -34,7 +34,7 @@ SECTIONS
__rodata_va = ABSOLUTE(.); __rodata_va = ABSOLUTE(.);
*(.rodata*) *(.rodata*)
. = ALIGN(4K); . = ALIGN(4K);
__rodata_len = . - __rodata_va; __rodata_va_end = .;
} >AXIWRAM } >AXIWRAM
.shared (NOLOAD) : ALIGN(4K) .shared (NOLOAD) : ALIGN(4K)
@ -43,7 +43,7 @@ SECTIONS
__shared_va = ABSOLUTE(.); __shared_va = ABSOLUTE(.);
*(.shared*) *(.shared*)
. = ALIGN(4K); . = ALIGN(4K);
__shared_len = . - __shared_va; __shared_va_end = .;
} >AXIWRAM } >AXIWRAM
.bss (NOLOAD) : ALIGN(4K) .bss (NOLOAD) : ALIGN(4K)
@ -52,6 +52,6 @@ SECTIONS
__bss_va = ABSOLUTE(.); __bss_va = ABSOLUTE(.);
*(.bss*) *(.bss*)
. = ALIGN(4K); . = ALIGN(4K);
__bss_len = . - __bss_va; __bss_va_end = .;
} >AXIWRAM } >AXIWRAM
} }

View File

@ -76,10 +76,12 @@ __boot:
b 1b b 1b
corezero_start: corezero_start:
@ assume __bss_len is 128 byte aligned @ assumes the .bss section size is 128 byte aligned (or zero)
ldr r0, =__bss_pa ldr r0, =__bss_pa
ldr r1, =__bss_len ldr r1, =__bss_va_end @ calculate the length of .bss using the VA start and end
add r1, r0, r1 ldr r2, =__bss_va
sub r1, r1, r2
add r1, r0, r1 @ fixup to be PA start and end
mov r2, #0 mov r2, #0
mov r3, #0 mov r3, #0
mov r4, #0 mov r4, #0

View File

@ -22,6 +22,8 @@
#include <stdatomic.h> #include <stdatomic.h>
#include "common.h"
#include "arm/timer.h" #include "arm/timer.h"
#include "hw/gpio.h" #include "hw/gpio.h"
@ -116,16 +118,13 @@ u32 mcuGetSpecialHID(void)
void mcuSetStatusLED(u32 period_ms, u32 color) void mcuSetStatusLED(u32 period_ms, u32 color)
{ {
u32 r, g, b; u32 r, g, b, delay;
mcuStatusLED ledState; mcuStatusLED ledState;
// handle proper non-zero periods delay = clamp((period_ms * 0x200) / 1000, 1, 0xFF);
// so small the hardware can't handle it
if (period_ms != 0 && period_ms < 63)
period_ms = 63;
ledState.delay = (period_ms * 0x10) / 1000; ledState.delay = delay;
ledState.smoothing = 0x40; ledState.smoothing = delay;
ledState.loop_delay = 0x10; ledState.loop_delay = 0x10;
ledState.unk = 0; ledState.unk = 0;

View File

@ -20,7 +20,7 @@
#include <types.h> #include <types.h>
#define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_len; #define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_va_end;
DEF_SECT_(text) DEF_SECT_(text)
DEF_SECT_(data) DEF_SECT_(data)
DEF_SECT_(rodata) DEF_SECT_(rodata)
@ -30,6 +30,6 @@ DEF_SECT_(shared)
#define SECTION_VA(n) ((u32)&__##n##_va) #define SECTION_VA(n) ((u32)&__##n##_va)
#define SECTION_PA(n) ((u32)&__##n##_pa) #define SECTION_PA(n) ((u32)&__##n##_pa)
#define SECTION_LEN(n) ((u32)&__##n##_len) #define SECTION_LEN(n) (((u32)(&__##n##_va_end) - (u32)(&__##n##_va)))
#define SECTION_TRI(n) SECTION_VA(n), SECTION_PA(n), SECTION_LEN(n) #define SECTION_TRI(n) SECTION_VA(n), SECTION_PA(n), SECTION_LEN(n)

View File

@ -1,17 +1,24 @@
PROCESSOR := ARM9 PROCESSOR := ARM9
TARGET := $(shell basename $(CURDIR)) TARGET := $(shell basename "$(CURDIR)")
SOURCE := source SOURCE := source
BUILD := build BUILD := build
SUBARCH := -D$(PROCESSOR) -march=armv5te -mtune=arm946e-s -mthumb -mfloat-abi=soft SUBARCH := -D$(PROCESSOR) -march=armv5te -mtune=arm946e-s -mthumb -mfloat-abi=soft
INCDIRS := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/lodepng source/qrcodegen source/system source/utils INCDIRS := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/lodepng source/lua source/qrcodegen source/system source/utils
INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)") INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
ASFLAGS += $(SUBARCH) $(INCLUDE) ASFLAGS += $(SUBARCH) $(INCLUDE)
CFLAGS += $(SUBARCH) $(INCLUDE) -fno-builtin-memcpy -flto CFLAGS += $(SUBARCH) $(INCLUDE) -fno-builtin-memcpy -flto
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
LIBS += -lm
include ../Makefile.common include ../Makefile.common
include ../Makefile.build include ../Makefile.build
arm9_data.elf: arm9.elf
$(OBJCOPY) -O elf32-littlearm -j .rodata* -j .data* -j .bss* $< $@
arm9_code.elf: arm9.elf
$(OBJCOPY) -O elf32-littlearm -j .text* -j .vectors* $< $@

View File

@ -4,8 +4,10 @@ ENTRY(_start)
MEMORY MEMORY
{ {
AHBWRAM (RWX) : ORIGIN = 0x08006000, LENGTH = 512K
VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64 VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64
CODEMEM (RX) : ORIGIN = 0x08000040, LENGTH = 512K - 64
BOOTROM (R) : ORIGIN = 0x08080000, LENGTH = 128K /* BootROM mirrors, don't touch! */
DATAMEM (RW) : ORIGIN = 0x080A0000, LENGTH = 384K
} }
SECTIONS SECTIONS
@ -16,7 +18,7 @@ SECTIONS
KEEP(*(.vectors)); KEEP(*(.vectors));
. = ALIGN(4); . = ALIGN(4);
__vectors_len = ABSOLUTE(.) - __vectors_vma; __vectors_len = ABSOLUTE(.) - __vectors_vma;
} >VECTORS AT>AHBWRAM } >VECTORS AT>CODEMEM
.text : ALIGN(4) { .text : ALIGN(4) {
__text_s = ABSOLUTE(.); __text_s = ABSOLUTE(.);
@ -24,24 +26,28 @@ SECTIONS
*(.text*); *(.text*);
. = ALIGN(4); . = ALIGN(4);
__text_e = ABSOLUTE(.); __text_e = ABSOLUTE(.);
} >AHBWRAM } >CODEMEM
.rodata : ALIGN(4) { .rodata : ALIGN(4) {
*(.rodata*); *(.rodata*);
. = ALIGN(4); . = ALIGN(4);
} >AHBWRAM __exidx_start = .;
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
__exidx_end = .;
. = ALIGN(4);
} >DATAMEM
.data : ALIGN(4) { .data : ALIGN(4) {
*(.data*); *(.data*);
. = ALIGN(4); . = ALIGN(4);
} >AHBWRAM } >DATAMEM
.bss : ALIGN(4) { .bss (NOLOAD) : ALIGN(4) {
__bss_start = .; __bss_start = .;
*(.bss*); *(.bss*);
. = ALIGN(4); . = ALIGN(4);
__bss_end = .; __bss_end = .;
} >AHBWRAM } >DATAMEM
__end__ = ABSOLUTE(.); __end__ = ABSOLUTE(.);
} }

View File

@ -27,7 +27,7 @@ void CreateScreenshot(void) {
fvx_rmkdir(OUTPUT_PATH); fvx_rmkdir(OUTPUT_PATH);
get_dstime(&dstime); get_dstime(&dstime);
snprintf(filename, 64, OUTPUT_PATH "/snap_%02X%02X%02X%02X%02X%02X.png", snprintf(filename, sizeof(filename), OUTPUT_PATH "/snap_%02X%02X%02X%02X%02X%02X.png",
dstime.bcd_Y, dstime.bcd_M, dstime.bcd_D, dstime.bcd_Y, dstime.bcd_M, dstime.bcd_D,
dstime.bcd_h, dstime.bcd_m, dstime.bcd_s); dstime.bcd_h, dstime.bcd_m, dstime.bcd_s);
filename[63] = '\0'; filename[63] = '\0';

View File

@ -1,5 +1,6 @@
#include <stdarg.h> #include <stdarg.h>
#include "language.h"
#include "swkbd.h" #include "swkbd.h"
#include "timer.h" #include "timer.h"
#include "hid.h" #include "hid.h"
@ -80,7 +81,7 @@ static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase
if (key->id == KEY_TXTBOX) return; if (key->id == KEY_TXTBOX) return;
char keystr[16]; char keystr[16];
if (key->id >= 0x80) snprintf(keystr, 16, "%s", keystrs[key->id - 0x80]); if (key->id >= 0x80) snprintf(keystr, sizeof(keystr), "%s", keystrs[key->id - 0x80]);
else { else {
keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id; keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id;
keystr[1] = 0; keystr[1] = 0;
@ -146,8 +147,8 @@ static void DrawTextBox(const TouchBox* txtbox, const char* inputstr, const u32
// draw input string // draw input string
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c", DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c",
(*scroll) ? '<' : '|', (*scroll) ? '<' : '|',
input_shown_size, (int) input_shown_size,
input_shown_size, (int) input_shown_size,
(*scroll > inputstr_size) ? "" : inputstr + *scroll, (*scroll > inputstr_size) ? "" : inputstr + *scroll,
(inputstr_size - (s32) *scroll > input_shown_size) ? '>' : '|' (inputstr_size - (s32) *scroll > input_shown_size) ? '>' : '|'
); );
@ -159,11 +160,11 @@ static void DrawTextBox(const TouchBox* txtbox, const char* inputstr, const u32
} }
DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s", DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s",
1 + cpos, (int) (1 + cpos),
1 + cpos, (int) (1 + cpos),
"", "",
input_shown_length - cpos, (int) (input_shown_length - cpos),
input_shown_length - cpos, (int) (input_shown_length - cpos),
"" ""
); );
} }
@ -253,9 +254,9 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
TouchBox* textbox = swkbd_alphabet; // always use this textbox TouchBox* textbox = swkbd_alphabet; // always use this textbox
static bool show_instr = true; static bool show_instr = true;
static const char* instr = "Keyboard Controls:\n \n←/→ - Move cursor\nR - Caps / Capslock\nX - Delete char\nY - Insert char\nA - Submit\nB - Cancel\n \nSELECT switches to\nclassic prompt"; const char* instr = STR_KEYBOARD_CONTROLS_DETAILS;
if (show_instr) { if (show_instr) {
ShowPrompt(false, instr); ShowPrompt(false, "%s", instr);
show_instr = false; show_instr = false;
} }
@ -268,7 +269,7 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
char str[512]; // arbitrary limit, should be more than enough char str[512]; // arbitrary limit, should be more than enough
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, 512, format, va); vsnprintf(str, sizeof(str), format, va);
va_end(va); va_end(va);
u32 str_width = GetDrawStringWidth(str); u32 str_width = GetDrawStringWidth(str);
if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT; if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT;
@ -332,7 +333,7 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
swkbd = swkbd_numpad; swkbd = swkbd_numpad;
} else if (key == KEY_SWITCH) { } else if (key == KEY_SWITCH) {
ClearScreen(BOT_SCREEN, COLOR_STD_BG); ClearScreen(BOT_SCREEN, COLOR_STD_BG);
return ShowStringPrompt(inputstr, max_size, str); return ShowStringPrompt(inputstr, max_size, "%s", str);
} else if (key == KEY_UNICODE) { } else if (key == KEY_UNICODE) {
if (cursor > 3 && cursor <= inputstr_size) { if (cursor > 3 && cursor <= inputstr_size) {
u16 codepoint = 0; u16 codepoint = 0;

View File

@ -86,4 +86,4 @@ enum {
#define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt) #define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt)
bool ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...); bool PRINTF_ARGS(3) ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...);

View File

@ -2,6 +2,7 @@
#include "ui.h" #include "ui.h"
#include "hid.h" #include "hid.h"
#include "crc16.h" #include "crc16.h"
#include "language.h"
#include "spiflash.h" #include "spiflash.h"
#include "support.h" #include "support.h"
@ -46,8 +47,8 @@ bool ShowTouchCalibrationDialog(void)
// clear screen, draw instructions // clear screen, draw instructions
ClearScreen(BOT_SCREEN, COLOR_STD_BG); ClearScreen(BOT_SCREEN, COLOR_STD_BG);
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s",
"Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!"); STR_TOUCH_CROSSHAIRS_TO_CALIBRATE_TOUCHSCREEN_USE_STYLUS);
// set calibration defaults // set calibration defaults
SetCalibrationDefaults(); SetCalibrationDefaults();

View File

@ -15,12 +15,24 @@
#include "power.h" #include "power.h"
#include "hid.h" #include "hid.h"
#include "fixp.h" #include "fixp.h"
#include "language.h"
#define STRBUF_SIZE 512 // maximum size of the string buffer #define STRBUF_SIZE 512 // maximum size of the string buffer
#define FONT_MAX_WIDTH 8 #define FONT_MAX_WIDTH 8
#define FONT_MAX_HEIGHT 10 #define FONT_MAX_HEIGHT 10
#define PROGRESS_REFRESH_RATE 30 // the progress bar is only allowed to draw to screen every X milliseconds #define PROGRESS_REFRESH_RATE 30 // the progress bar is only allowed to draw to screen every X milliseconds
typedef struct {
char chunk_id[4]; // NOT null terminated
u32 size;
} RiffChunkHeader;
typedef struct {
u8 width;
u8 height;
u16 count;
} FontMeta;
static u32 font_width = 0; static u32 font_width = 0;
static u32 font_height = 0; static u32 font_height = 0;
static u32 font_count = 0; static u32 font_count = 0;
@ -129,8 +141,8 @@ u32 GetCharacter(const char** str)
return c; return c;
} }
u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) { const u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
char* hdr = (char*) pbm; const char* hdr = (const char*) pbm;
u32 hdr_max_size = min(512, pbm_size); u32 hdr_max_size = min(512, pbm_size);
u32 pbm_w = 0; u32 pbm_w = 0;
u32 pbm_h = 0; u32 pbm_h = 0;
@ -191,48 +203,51 @@ u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
return (u8*) pbm + p; return (u8*) pbm + p;
} }
u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count) { const u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count) {
u8 *ptr = (u8*) riff; const void* ptr = riff;
u8 riff_w = 0; const RiffChunkHeader* riff_header;
u8 riff_h = 0; const RiffChunkHeader* chunk_header;
u16 riff_count = 0;
// check header magic, then skip over // check header magic and load size
if (memcmp(ptr, "RIFF", 4) != 0) return NULL; if (!ptr) return NULL;
riff_header = ptr;
if (memcmp(riff_header->chunk_id, "RIFF", 4) != 0) return NULL;
// ensure enough space is allocated // ensure enough space is allocated
u32 data_size; if (riff_header->size > riff_size) return NULL;
memcpy(&data_size, ptr + 4, sizeof(u32));
if (data_size > riff_size) return NULL;
ptr += 8; ptr += sizeof(RiffChunkHeader);
while ((u32)(ptr - riff) < riff_header->size + sizeof(RiffChunkHeader)) {
chunk_header = ptr;
// check for and load META section // check for and load META section
if (memcmp(ptr, "META", 4) == 0) { if (memcmp(chunk_header->chunk_id, "META", 4) == 0) {
riff_w = ptr[8];
riff_h = ptr[9];
memcpy(&riff_count, ptr + 10, sizeof(u16));
u32 section_size; if (chunk_header->size != 4) return NULL;
memcpy(&section_size, ptr + 4, sizeof(u32));
ptr += 8 + section_size;
if (riff_w > FONT_MAX_WIDTH || riff_h > FONT_MAX_HEIGHT) return NULL; const FontMeta *meta = ptr + sizeof(RiffChunkHeader);
} else return NULL; if (meta->width > FONT_MAX_WIDTH || meta->height > FONT_MAX_HEIGHT) return NULL;
// all good // all good
if (w) *w = riff_w; if (w) *w = meta->width;
if (h) *h = riff_h; if (h) *h = meta->height;
if (count) *count = riff_count; if (count) *count = meta->count;
return ptr; return ptr;
} }
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
}
return NULL;
}
// sets the font from a given RIFF or PBM // sets the font from a given RIFF or PBM
// if no font is given, the font is fetched from the default VRAM0 location // if no font is given, the font is fetched from the default VRAM0 location
bool SetFont(const void* font, u32 font_size) { bool SetFont(const void* font, u32 font_size) {
u32 w, h; u32 w, h;
u16 count; u16 count;
u8* ptr = NULL; const u8* ptr = NULL;
if (!font) { if (!font) {
u64 font_size64 = 0; u64 font_size64 = 0;
@ -248,33 +263,31 @@ bool SetFont(const void* font, u32 font_size) {
font_height = h; font_height = h;
font_count = count; font_count = count;
// character data const RiffChunkHeader* riff_header;
if (memcmp(ptr, "CDAT", 4) == 0) { const RiffChunkHeader* chunk_header;
u32 section_size;
memcpy(&section_size, ptr + 4, sizeof(u32));
// load total size
riff_header = font;
while (((u32)ptr - (u32)font) < riff_header->size + sizeof(RiffChunkHeader)) {
chunk_header = (const void *)ptr;
if (memcmp(chunk_header->chunk_id, "CDAT", 4) == 0) { // character data
if (font_bin) free(font_bin); if (font_bin) free(font_bin);
font_bin = malloc(font_height * font_count); font_bin = malloc(font_height * font_count);
if (!font_bin) return NULL; if (!font_bin) return NULL;
memcpy(font_bin, ptr + 8, font_height * font_count); memcpy(font_bin, ptr + sizeof(RiffChunkHeader), font_height * font_count);
} else if (memcmp(chunk_header->chunk_id, "CMAP", 4) == 0) { // character map
ptr += 8 + section_size;
} else return NULL;
// character map
if (memcmp(ptr, "CMAP", 4) == 0) {
u32 section_size;
memcpy(&section_size, ptr + 4, sizeof(u32));
if (font_map) free(font_map); if (font_map) free(font_map);
font_map = malloc(sizeof(u16) * font_count); font_map = malloc(sizeof(u16) * font_count);
if (!font_map) return NULL; if (!font_map) return NULL;
memcpy(font_map, ptr + 8, sizeof(u16) * font_count); memcpy(font_map, ptr + sizeof(RiffChunkHeader), sizeof(u16) * font_count);
}
ptr += 8 + section_size; ptr += sizeof(RiffChunkHeader) + chunk_header->size;
} else return NULL; }
} else if ((ptr = GetFontFromPbm(font, font_size, &w, &h))) { } else if ((ptr = GetFontFromPbm(font, font_size, &w, &h))) {
font_count = 0x100; font_count = 0x100;
@ -576,18 +589,13 @@ void ResizeString(char* dest, const char* orig, int nlength, int tpos, bool alig
if (nlength < olength) { if (nlength < olength) {
TruncateString(dest, orig, nlength, tpos); TruncateString(dest, orig, nlength, tpos);
} else if (!align_right) {
int nsize = 0;
for (int i = 0; i < nlength || (orig[nsize] & 0xC0) == 0x80; nsize++) {
if ((orig[nsize] & 0xC0) != 0x80) i++;
}
snprintf(dest, UTF_BUFFER_BYTESIZE(nlength), "%-*.*s", nsize, nsize, orig);
} else { } else {
int nsize = 0; int nsize = 0;
for (int i = 0; i < nlength || (orig[nsize] & 0xC0) == 0x80; nsize++) { int osize = strnlen(orig, 256);
if ((orig[nsize] & 0xC0) != 0x80) i++; for (int i = 0; i < nlength || (nsize <= osize && (orig[nsize] & 0xC0) == 0x80); nsize++) {
if (nsize > osize || (orig[nsize] & 0xC0) != 0x80) i++;
} }
snprintf(dest, UTF_BUFFER_BYTESIZE(nlength), "%*.*s", nsize, nsize, orig); snprintf(dest, UTF_BUFFER_BYTESIZE(nlength), align_right ? "%*.*s" : "%-*.*s", nsize, nsize, orig);
} }
} }
@ -625,20 +633,20 @@ void FormatNumber(char* str, u64 number) { // str should be 32 byte in size
for (; number / (mag1000 * 1000) > 0; mag1000 *= 1000); for (; number / (mag1000 * 1000) > 0; mag1000 *= 1000);
for (; mag1000 > 0; mag1000 /= 1000) { for (; mag1000 > 0; mag1000 /= 1000) {
u32 pos = strnlen(str, 31); u32 pos = strnlen(str, 31);
snprintf(str + pos, 31 - pos, "%0*llu%c", (pos) ? 3 : 1, (number / mag1000) % 1000, (mag1000 > 1) ? ',' : '\0'); snprintf(str + pos, 31 - pos, "%0*llu%s", (pos) ? 3 : 1, (number / mag1000) % 1000, (mag1000 > 1) ? STR_THOUSAND_SEPARATOR : "");
} }
} }
void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just to be safe void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just to be safe
const char* units[] = {" Byte", " kB", " MB", " GB"}; const char* units[] = {STR_BYTE, STR_KB, STR_MB, STR_GB};
if (bytes == (u64) -1) snprintf(str, 32, "INVALID"); if (bytes == (u64) -1) snprintf(str, 32, "%s", STR_INVALID);
else if (bytes < 1024) snprintf(str, 32, "%llu%s", bytes, units[0]); else if (bytes < 1024) snprintf(str, 32, "%llu%s", bytes, units[0]);
else { else {
u32 scale = 1; u32 scale = 1;
u64 bytes100 = (bytes * 100) >> 10; u64 bytes100 = (bytes * 100) >> 10;
for(; (bytes100 >= 1024*100) && (scale < 3); scale++, bytes100 >>= 10); for(; (bytes100 >= 1024*100) && (scale < 3); scale++, bytes100 >>= 10);
snprintf(str, 32, "%llu.%llu%s", bytes100 / 100, (bytes100 % 100) / 10, units[scale]); snprintf(str, 32, "%llu%s%llu%s", bytes100 / 100, STR_DECIMAL_SEPARATOR, (bytes100 % 100) / 10, units[scale]);
} }
} }
@ -705,7 +713,7 @@ void ShowIconString(u16* icon, int w, int h, const char *format, ...)
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
ShowIconStringF(MAIN_SCREEN, icon, w, h, str); ShowIconStringF(MAIN_SCREEN, icon, w, h, "%s", str);
} }
bool ShowPrompt(bool ask, const char *format, ...) bool ShowPrompt(bool ask, const char *format, ...)
@ -720,7 +728,7 @@ bool ShowPrompt(bool ask, const char *format, ...)
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s\n \n%s", str, DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s\n \n%s", str,
(ask) ? "(<A> yes, <B> no)" : "(<A> to continue)"); (ask) ? STR_A_YES_B_NO : STR_A_TO_CONTINUE);
while (true) { while (true) {
u32 pad_state = InputWait(0); u32 pad_state = InputWait(0);
@ -775,7 +783,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
ClearScreenF(true, false, color_bg); ClearScreenF(true, false, color_bg);
DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str); DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str);
#ifndef TIMER_UNLOCK #ifndef TIMER_UNLOCK
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, enter this:"); DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "%s", STR_TO_PROCEED_ENTER_THIS);
// generate sequence // generate sequence
const char *dpad_symbols[] = { "", "", "", "" }; // R L U D const char *dpad_symbols[] = { "", "", "", "" }; // R L U D
@ -812,7 +820,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
lvl = 0; lvl = 0;
} }
#else #else
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, hold <X>:"); DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, STR_TO_PROCEED_HOLD_X);
while (!CheckButton(BUTTON_B)) { while (!CheckButton(BUTTON_B)) {
for (u32 n = 0; n < seqlen; n++) { for (u32 n = 0; n < seqlen; n++) {
@ -839,10 +847,11 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
} }
#endif #endif
u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) { u32 ShowSelectPrompt(int n, const char** options, const char *format, ...) {
u32 str_width, str_height; u32 str_width, str_height;
u32 x, y, yopt; u32 x, y, yopt;
u32 sel = 0; int sel = 0, scroll = 0;
int n_show = min(n, 10);
char str[STRBUF_SIZE]; char str[STRBUF_SIZE];
va_list va; va_list va;
@ -854,30 +863,64 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
// else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0; // else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0;
str_width = GetDrawStringWidth(str); str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str) + (n * (line_height + 2)) + (3 * line_height); str_height = GetDrawStringHeight(str) + (n_show * (line_height + 2)) + (3 * line_height);
if (str_width < 24 * font_width) str_width = 24 * font_width; if (str_width < 24 * font_width) str_width = 24 * font_width;
for (u32 i = 0; i < n; i++) if (str_width < GetDrawStringWidth(options[i])) for (int i = 0; i < n; i++) if (str_width < GetDrawStringWidth(options[i]) + (3 * font_width))
str_width = GetDrawStringWidth(options[i]); str_width = GetDrawStringWidth(options[i]) + (3 * font_width);
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2; x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2; y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
yopt = y + GetDrawStringHeight(str) + 8; yopt = y + GetDrawStringHeight(str) + 8;
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
DrawStringF(MAIN_SCREEN, x, yopt + (n*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)"); DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "%s", STR_A_SELECT_B_CANCEL);
while (true) { while (true) {
for (u32 i = 0; i < n; i++) { for (int i = scroll; i < scroll+n_show; i++) {
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*i), (sel == i) ? COLOR_STD_FONT : COLOR_LIGHTGREY, COLOR_STD_BG, "%2.2s %s", DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)), (sel == i) ? COLOR_STD_FONT : COLOR_LIGHTGREY, COLOR_STD_BG, "%2.2s %s",
(sel == i) ? "->" : "", options[i]); (sel == i) ? "->" : "", options[i]);
} }
// show [n more]
if (n - n_show - scroll > 0) {
char more_str[UTF_BUFFER_BYTESIZE(str_width / font_width)], temp_str[UTF_BUFFER_BYTESIZE(64)];
snprintf(temp_str, sizeof(temp_str), STR_N_MORE, (n - (n_show-1) - scroll));
ResizeString(more_str, temp_str, str_width / font_width, 8, false);
DrawString(MAIN_SCREEN, more_str, x, yopt + (line_height+2)*(n_show-1), COLOR_LIGHTGREY, COLOR_STD_BG);
}
// show scroll bar
u32 bar_x = x + str_width + 2;
const u32 flist_height = (n_show * (line_height + 2));
const u32 bar_width = 2;
if (n > n_show) { // draw position bar at the right
const u32 bar_height_min = 32;
u32 bar_height = (n_show * flist_height) / n;
if (bar_height < bar_height_min) bar_height = bar_height_min;
const u32 bar_y = ((u64) scroll * (flist_height - bar_height)) / (n - n_show) + yopt;
DrawRectangle(MAIN_SCREEN, bar_x, bar_y, bar_width, bar_height, COLOR_SIDE_BAR);
}
u32 pad_state = InputWait(0); u32 pad_state = InputWait(0);
if (pad_state & BUTTON_DOWN) sel = (sel+1) % n; if (pad_state & BUTTON_DOWN) sel = (sel+1) % n;
else if (pad_state & BUTTON_UP) sel = (sel+n-1) % n; else if (pad_state & BUTTON_UP) sel = (sel+n-1) % n;
else if (pad_state & BUTTON_RIGHT) sel += n_show;
else if (pad_state & BUTTON_LEFT) sel -= n_show;
else if (pad_state & BUTTON_A) break; else if (pad_state & BUTTON_A) break;
else if (pad_state & BUTTON_B) { else if (pad_state & BUTTON_B) {
sel = n; sel = n;
break; break;
} }
if (sel < 0) sel = 0;
else if (sel >= n) sel = n-1;
int prev_scroll = scroll;
if (sel < scroll) scroll = sel;
else if (sel == n-1 && sel >= (scroll + n_show - 1)) scroll = sel - n_show + 1;
else if (sel >= (scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1;
if (scroll != prev_scroll) {
DrawRectangle(MAIN_SCREEN, x + font_width * 3, yopt, str_width + 4, (n_show * (line_height + 2)), COLOR_STD_BG);
}
} }
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
@ -885,12 +928,12 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
return (sel >= n) ? 0 : sel + 1; return (sel >= n) ? 0 : sel + 1;
} }
u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const char *format, ...) { u32 ShowFileScrollPrompt(int n, const DirEntry** options, bool hide_ext, const char *format, ...) {
u32 str_height, fname_len; u32 str_height, fname_len;
u32 x, y, yopt; u32 x, y, yopt;
const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40; const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40;
int sel = 0, scroll = 0; int sel = 0, scroll = 0;
u32 n_show = min(n, 10); int n_show = min(n, 10);
char str[STRBUF_SIZE]; char str[STRBUF_SIZE];
va_list va; va_list va;
@ -909,9 +952,9 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)"); DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "%s", STR_A_SELECT_B_CANCEL);
while (true) { while (true) {
for (u32 i = scroll; i < scroll+n_show; i++) { for (int i = scroll; i < scroll+n_show; i++) {
char bytestr[16]; char bytestr[16];
FormatBytes(bytestr, options[i]->size); FormatBytes(bytestr, options[i]->size);
@ -925,18 +968,19 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
ResizeString(content_str, temp_str, fname_len, 8, false); ResizeString(content_str, temp_str, fname_len, 8, false);
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)), DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)),
(sel == (int)i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s", (sel == i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s",
(sel == (int)i) ? "->" : "", content_str); (sel == i) ? "->" : "", content_str);
DrawStringF(MAIN_SCREEN, x + item_width - font_width * 11, yopt + ((line_height+2)*(i-scroll)), DrawStringF(MAIN_SCREEN, x + item_width - font_width * 11, yopt + ((line_height+2)*(i-scroll)),
(sel == (int)i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%10.10s", (sel == i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%10.10s",
(options[i]->type == T_DIR) ? "(dir)" : (options[i]->type == T_DOTDOT) ? "(..)" : bytestr); (options[i]->type == T_DIR) ? STR_DIR : (options[i]->type == T_DOTDOT) ? "(..)" : bytestr);
} }
// show [n more] // show [n more]
if (n - n_show - scroll) { if (n - n_show - scroll > 0) {
char more_str[64 + 1]; char more_str[UTF_BUFFER_BYTESIZE(item_width / font_width)], temp_str[UTF_BUFFER_BYTESIZE(64)];
snprintf(more_str, 64, " [%d more]", (int)(n - (n_show-1) - scroll)); snprintf(temp_str, sizeof(temp_str), STR_N_MORE, (n - (n_show-1) - scroll));
DrawStringF(MAIN_SCREEN, x, yopt + (line_height+2)*(n_show-1), COLOR_LIGHTGREY, COLOR_STD_BG, "%-*s", item_width / font_width, more_str); ResizeString(more_str, temp_str, item_width / font_width, 8, false);
DrawString(MAIN_SCREEN, more_str, x, yopt + (line_height+2)*(n_show-1), COLOR_LIGHTGREY, COLOR_STD_BG);
} }
// show scroll bar // show scroll bar
u32 bar_x = x + item_width + 2; u32 bar_x = x + item_width + 2;
@ -954,8 +998,8 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
} else DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, flist_height, COLOR_STD_BG); } else DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, flist_height, COLOR_STD_BG);
u32 pad_state = InputWait(0); u32 pad_state = InputWait(0);
if (pad_state & BUTTON_DOWN) sel++; if (pad_state & BUTTON_DOWN) sel = (sel+1) % n;
else if (pad_state & BUTTON_UP) sel--; else if (pad_state & BUTTON_UP) sel = (sel+n-1) % n;
else if (pad_state & BUTTON_RIGHT) sel += n_show; else if (pad_state & BUTTON_RIGHT) sel += n_show;
else if (pad_state & BUTTON_LEFT) sel -= n_show; else if (pad_state & BUTTON_LEFT) sel -= n_show;
else if (pad_state & BUTTON_A) break; else if (pad_state & BUTTON_A) break;
@ -964,15 +1008,15 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
break; break;
} }
if (sel < 0) sel = 0; if (sel < 0) sel = 0;
else if (sel >= (int)n) sel = n-1; else if (sel >= n) sel = n-1;
if (sel < scroll) scroll = sel; if (sel < scroll) scroll = sel;
else if (sel == (int) n-1 && sel >= (int)(scroll + n_show - 1)) scroll = sel - n_show + 1; else if (sel == n-1 && sel >= (scroll + n_show - 1)) scroll = sel - n_show + 1;
else if (sel >= (int)(scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1; else if (sel >= (scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1;
} }
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
return (sel >= (int)n) ? 0 : sel + 1; return (sel >= n) ? 0 : sel + 1;
} }
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...) { u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...) {
@ -989,7 +1033,7 @@ u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *f
ButtonToString(keys[i], buttonstr); ButtonToString(keys[i], buttonstr);
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n<%s> %s", buttonstr, options[i]); ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n<%s> %s", buttonstr, options[i]);
} }
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n \n<%s> %s", "B", "cancel"); ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n \n<%s> %s", "B", STR_CANCEL);
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
@ -1038,7 +1082,7 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
DrawStringF(MAIN_SCREEN, x + 8, y + str_height - 40, COLOR_STD_FONT, COLOR_STD_BG, DrawStringF(MAIN_SCREEN, x + 8, y + str_height - 40, COLOR_STD_FONT, COLOR_STD_BG,
"R - (↑↓) fast scroll\nL - clear data%s", resize ? "\nX - remove char\nY - insert char" : ""); "%s\n%s", STR_R_FAST_SCROLL_L_CLEAR_DATA, resize ? STR_X_REMOVE_CHAR_Y_INSERT_CHAR : "");
// wait for all keys released // wait for all keys released
while (HID_ReadState() & BUTTON_ANY); while (HID_ReadState() & BUTTON_ANY);
@ -1076,15 +1120,15 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
DrawStringF(MAIN_SCREEN, x, y + str_height - 76, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c\n%-*.*s^%-*.*s", DrawStringF(MAIN_SCREEN, x, y + str_height - 76, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c\n%-*.*s^%-*.*s",
(scroll) ? '<' : '|', (scroll) ? '<' : '|',
input_shown_size, (int) input_shown_size,
input_shown_size, (int) input_shown_size,
(scroll > inputstr_size) ? "" : inputstr + scroll, (scroll > inputstr_size) ? "" : inputstr + scroll,
(inputstr_size - (s32) scroll > input_shown_size) ? '>' : '|', (inputstr_size - (s32) scroll > input_shown_size) ? '>' : '|',
1 + cpos, (int) 1 + cpos,
1 + cpos, (int) 1 + cpos,
"", "",
input_shown_length - cpos, (int) input_shown_length - cpos,
input_shown_length - cpos, (int) input_shown_length - cpos,
"" ""
); );
@ -1221,7 +1265,7 @@ u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...) {
va_list va; va_list va;
if (n_digits > 16) n_digits = 16; if (n_digits > 16) n_digits = 16;
snprintf(inputstr, 16 + 1, "%0*llX", (int) n_digits, start_val); snprintf(inputstr, sizeof(inputstr), "%0*llX", (int) n_digits, start_val);
va_start(va, format); va_start(va, format);
if (ShowInputPrompt(inputstr, n_digits + 1, 0, alphabet, format, va)) { if (ShowInputPrompt(inputstr, n_digits + 1, 0, alphabet, format, va)) {
@ -1238,7 +1282,7 @@ u64 ShowNumberPrompt(u64 start_val, const char *format, ...) {
u64 ret = 0; u64 ret = 0;
va_list va; va_list va;
snprintf(inputstr, 20 + 1, "%llu", start_val); snprintf(inputstr, sizeof(inputstr), "%llu", start_val);
va_start(va, format); va_start(va, format);
if (ShowInputPrompt(inputstr, 20 + 1, 1, alphabet, format, va)) { if (ShowInputPrompt(inputstr, 20 + 1, 1, alphabet, format, va)) {
@ -1358,7 +1402,7 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
const u32 text_pos_y = bar_pos_y + bar_height + 2; const u32 text_pos_y = bar_pos_y + bar_height + 2;
u32 prog_width = ((total > 0) && (current <= total)) ? (current * (bar_width-4)) / total : 0; u32 prog_width = ((total > 0) && (current <= total)) ? (current * (bar_width-4)) / total : 0;
u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0; u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0;
char tempstr[64]; char tempstr[UTF_BUFFER_BYTESIZE(64)];
char progstr[UTF_BUFFER_BYTESIZE(64)]; char progstr[UTF_BUFFER_BYTESIZE(64)];
static u64 last_msec_elapsed = 0; static u64 last_msec_elapsed = 0;
@ -1383,16 +1427,20 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
DrawRectangle(MAIN_SCREEN, bar_pos_x + 2 + prog_width, bar_pos_y + 2, (bar_width-4) - prog_width, bar_height - 4, COLOR_STD_BG); DrawRectangle(MAIN_SCREEN, bar_pos_x + 2 + prog_width, bar_pos_y + 2, (bar_width-4) - prog_width, bar_height - 4, COLOR_STD_BG);
TruncateString(progstr, opstr, min(63, (bar_width / FONT_WIDTH_EXT) - 7), 8); TruncateString(progstr, opstr, min(63, (bar_width / FONT_WIDTH_EXT) - 7), 8);
snprintf(tempstr, 64, "%s (%lu%%)", progstr, prog_percent); snprintf(tempstr, sizeof(tempstr), "%s (%lu%%)", progstr, prog_percent);
ResizeString(progstr, tempstr, bar_width / FONT_WIDTH_EXT, 8, false); ResizeString(progstr, tempstr, bar_width / FONT_WIDTH_EXT, 8, false);
DrawString(MAIN_SCREEN, progstr, bar_pos_x, text_pos_y, COLOR_STD_FONT, COLOR_STD_BG); DrawString(MAIN_SCREEN, progstr, bar_pos_x, text_pos_y, COLOR_STD_FONT, COLOR_STD_BG);
if (sec_elapsed >= 1) { if (sec_elapsed >= 1) {
snprintf(tempstr, 16, "ETA %02llum%02llus", sec_remain / 60, sec_remain % 60); if (sec_remain >= 3600) {
snprintf(tempstr, sizeof(tempstr), STR_ETA_N_HOUR_N_MIN_N_SEC, (sec_remain / 3600), (sec_remain / 60) % 60, sec_remain % 60);
} else {
snprintf(tempstr, sizeof(tempstr), STR_ETA_N_MIN_N_SEC, sec_remain / 60, sec_remain % 60);
}
ResizeString(progstr, tempstr, 16, 8, true); ResizeString(progstr, tempstr, 16, 8, true);
DrawString(MAIN_SCREEN, progstr, bar_pos_x + bar_width - 1 - (FONT_WIDTH_EXT * 16), DrawString(MAIN_SCREEN, progstr, bar_pos_x + bar_width - 1 - (FONT_WIDTH_EXT * 16),
bar_pos_y - line_height - 1, COLOR_STD_FONT, COLOR_STD_BG); bar_pos_y - line_height - 1, COLOR_STD_FONT, COLOR_STD_BG);
} }
DrawString(MAIN_SCREEN, "(hold B to cancel)", bar_pos_x + 2, text_pos_y + 14, COLOR_STD_FONT, COLOR_STD_BG); DrawString(MAIN_SCREEN, STR_HOLD_B_TO_CANCEL, bar_pos_x + 2, text_pos_y + 14, COLOR_STD_FONT, COLOR_STD_BG);
last_prog_width = prog_width; last_prog_width = prog_width;
@ -1405,13 +1453,7 @@ int ShowBrightnessConfig(int set_brightness)
u32 btn_input, bar_count; u32 btn_input, bar_count;
int bar_x_pos, bar_y_pos, bar_width, bar_height; int bar_x_pos, bar_y_pos, bar_width, bar_height;
const char *brightness_str = const char *brightness_str = STR_BRIGHTNESS_CONTROLS;
"[←] Decrease brightness\n"
"[→] Increase brightness\n"
" \n"
"[X] Use volume slider control\n"
"[A] Set current brightness\n"
"[B] Cancel";
static const u16 brightness_slider_colmasks[] = { static const u16 brightness_slider_colmasks[] = {
COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE
}; };
@ -1432,7 +1474,7 @@ int ShowBrightnessConfig(int set_brightness)
// draw initial UI stuff // draw initial UI stuff
DrawStringF(MAIN_SCREEN, DrawStringF(MAIN_SCREEN,
(SCREEN_WIDTH_MAIN - GetDrawStringWidth(brightness_str)) / 2, (SCREEN_WIDTH_MAIN - GetDrawStringWidth(brightness_str)) / 2,
(SCREEN_HEIGHT / 4) * 2, COLOR_STD_FONT, COLOR_STD_BG, brightness_str); (SCREEN_HEIGHT / 4) * 2, COLOR_STD_FONT, COLOR_STD_BG, "%s", brightness_str);
// draw all color gradient bars // draw all color gradient bars
for (int x = 0; x < bar_width; x++) { for (int x = 0; x < bar_width; x++) {

View File

@ -24,6 +24,8 @@
#define UTF_MAX_BYTES_PER_RUNE 4 #define UTF_MAX_BYTES_PER_RUNE 4
#define UTF_BUFFER_BYTESIZE(rune_count) (((rune_count) + 1) * UTF_MAX_BYTES_PER_RUNE) #define UTF_BUFFER_BYTESIZE(rune_count) (((rune_count) + 1) * UTF_MAX_BYTES_PER_RUNE)
#define PRINTF_ARGS(n) __attribute__ ((format (printf, (n), (n) + 1)))
#define TOP_SCREEN ((u16*)VRAM_TOP_LA) #define TOP_SCREEN ((u16*)VRAM_TOP_LA)
#define BOT_SCREEN ((u16*)VRAM_BOT_A) #define BOT_SCREEN ((u16*)VRAM_BOT_A)
@ -43,13 +45,13 @@
#ifndef AUTO_UNLOCK #ifndef AUTO_UNLOCK
bool ShowUnlockSequence(u32 seqlvl, const char *format, ...); bool PRINTF_ARGS(2) ShowUnlockSequence(u32 seqlvl, const char *format, ...);
#else #else
#define ShowUnlockSequence ShowPrompt #define ShowUnlockSequence ShowPrompt
#endif #endif
u8* GetFontFromPbm(const void* pbm, const u32 riff_size, u32* w, u32* h); const u8* GetFontFromPbm(const void* pbm, const u32 riff_size, u32* w, u32* h);
u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count); const u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count);
bool SetFont(const void* font, const u32 font_size); bool SetFont(const void* font, const u32 font_size);
u16 GetColor(const u16 *screen, int x, int y); u16 GetColor(const u16 *screen, int x, int y);
@ -63,8 +65,8 @@ void DrawQrCode(u16 *screen, const u8* qrcode);
void DrawCharacter(u16 *screen, u32 character, int x, int y, u32 color, u32 bgcolor); void DrawCharacter(u16 *screen, u32 character, int x, int y, u32 color, u32 bgcolor);
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor); void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor);
void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...); void PRINTF_ARGS(6) DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...);
void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...); void PRINTF_ARGS(4) DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...);
u32 GetCharSize(const char* str); u32 GetCharSize(const char* str);
u32 GetPrevCharSize(const char* str); u32 GetPrevCharSize(const char* str);
@ -81,19 +83,19 @@ void TruncateString(char* dest, const char* orig, int nlength, int tpos);
void FormatNumber(char* str, u64 number); void FormatNumber(char* str, u64 number);
void FormatBytes(char* str, u64 bytes); void FormatBytes(char* str, u64 bytes);
void ShowString(const char *format, ...); void PRINTF_ARGS(1) ShowString(const char *format, ...);
void ShowStringF(u16* screen, const char *format, ...); void PRINTF_ARGS(2) ShowStringF(u16* screen, const char *format, ...);
void ShowIconString(u16* icon, int w, int h, const char *format, ...); void PRINTF_ARGS(4) ShowIconString(u16* icon, int w, int h, const char *format, ...);
void ShowIconStringF(u16* screen, u16* icon, int w, int h, const char *format, ...); void PRINTF_ARGS(5) ShowIconStringF(u16* screen, u16* icon, int w, int h, const char *format, ...);
bool ShowPrompt(bool ask, const char *format, ...); bool PRINTF_ARGS(2) ShowPrompt(bool ask, const char *format, ...);
u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...); u32 PRINTF_ARGS(3) ShowSelectPrompt(int n, const char** options, const char *format, ...);
u32 ShowFileScrollPrompt(u32 n, const DirEntry** entries, bool hide_ext, const char *format, ...); u32 PRINTF_ARGS(4) ShowFileScrollPrompt(int n, const DirEntry** entries, bool hide_ext, const char *format, ...);
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...); u32 PRINTF_ARGS(4) ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...);
bool ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...); bool PRINTF_ARGS(3) ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...);
u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...); u64 PRINTF_ARGS(3) ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...);
u64 ShowNumberPrompt(u64 start_val, const char *format, ...); u64 PRINTF_ARGS(2) ShowNumberPrompt(u64 start_val, const char *format, ...);
bool ShowDataPrompt(u8* data, u32* size, const char *format, ...); bool PRINTF_ARGS(3) ShowDataPrompt(u8* data, u32* size, const char *format, ...);
bool ShowRtcSetterPrompt(void* time, const char *format, ...); bool PRINTF_ARGS(2) ShowRtcSetterPrompt(void* time, const char *format, ...);
bool ShowProgress(u64 current, u64 total, const char* opstr); bool ShowProgress(u64 current, u64 total, const char* opstr);
int ShowBrightnessConfig(int set_brightness); int ShowBrightnessConfig(int set_brightness);

View File

@ -40,10 +40,13 @@ u32 GetUnitKeysType(void)
} }
void CryptAesKeyInfo(AesKeyInfo* info) { void CryptAesKeyInfo(AesKeyInfo* info) {
static u8 zeroes[16] = { 0 }; static const u8 zeros[16] = { 0 };
static const u8 keyY_dev[16] = {
0xA2, 0x32, 0x4A, 0x7E, 0x7C, 0xE6, 0x1A, 0x9A, 0x45, 0x4A, 0x52, 0x19, 0xB3, 0x5B, 0xE9, 0x3B };
const u8* aeskeyY = GetUnitKeysType() == KEYS_DEVKIT ? &keyY_dev[0] : &zeros[0];
u8 ctr[16] = { 0 }; u8 ctr[16] = { 0 };
memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes
setup_aeskeyY(0x2C, zeroes); setup_aeskeyY(0x2C, aeskeyY);
use_aeskey(0x2C); use_aeskey(0x2C);
set_ctr(ctr); set_ctr(ctr);
aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE); aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE);
@ -152,7 +155,7 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
// load legacy slot0x??Key?.bin file instead // load legacy slot0x??Key?.bin file instead
if (!found && (type != 'I')) { if (!found && (type != 'I')) {
char fname[64]; char fname[64];
snprintf(fname, 64, "slot0x%02lXKey%s%s.bin", keyslot, snprintf(fname, sizeof(fname), "slot0x%02lXKey%s%s.bin", keyslot,
(type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : ""); (type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : "");
found = (LoadSupportFile(fname, key, 16) == 16); found = (LoadSupportFile(fname, key, 16) == 16);
} }

View File

@ -8,6 +8,7 @@
#include "keydb.h" #include "keydb.h"
#include "ctrtransfer.h" #include "ctrtransfer.h"
#include "scripting.h" #include "scripting.h"
#include "gm9lua.h"
#include "png.h" #include "png.h"
#include "ui.h" // only for font file detection #include "ui.h" // only for font file detection
@ -129,6 +130,8 @@ u64 IdentifyFileType(const char* path) {
return FONT_PBM; return FONT_PBM;
} else if (GetFontFromRiff(data, fsize, NULL, NULL, NULL)) { } else if (GetFontFromRiff(data, fsize, NULL, NULL, NULL)) {
return FONT_RIFF; return FONT_RIFF;
} else if (GetLanguage(data, fsize, NULL, NULL, NULL)) {
return TRANSLATION;
} else if ((fsize > sizeof(AgbHeader)) && } else if ((fsize > sizeof(AgbHeader)) &&
(ValidateAgbHeader((AgbHeader*) data) == 0)) { (ValidateAgbHeader((AgbHeader*) data) == 0)) {
return GAME_GBA; return GAME_GBA;
@ -161,8 +164,11 @@ u64 IdentifyFileType(const char* path) {
return BIN_CIFNSH; return BIN_CIFNSH;
} else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) { } else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) {
u64 type = 0; u64 type = 0;
if ((fsize < SCRIPT_MAX_SIZE) && (strncasecmp(ext, SCRIPT_EXT, strnlen(SCRIPT_EXT, 16) + 1) == 0)) if ((fsize < SCRIPT_MAX_SIZE) && (strcasecmp(ext, SCRIPT_EXT) == 0))
type |= TXT_SCRIPT; // should be a script (which is also generic text) type |= TXT_SCRIPT; // should be a script (which is also generic text)
// this should check if it's compiled lua bytecode (done with luac), which is NOT text
else if ((fsize < LUASCRIPT_MAX_SIZE) && (strcasecmp(ext, LUASCRIPT_EXT) == 0))
type |= TXT_LUA;
if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC; if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC;
return type; return type;
} else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) && } else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) &&

View File

@ -39,6 +39,8 @@
#define FONT_RIFF (1ULL<<34) #define FONT_RIFF (1ULL<<34)
#define NOIMG_NAND (1ULL<<35) #define NOIMG_NAND (1ULL<<35)
#define HDR_NAND (1ULL<<36) #define HDR_NAND (1ULL<<36)
#define TRANSLATION (1ULL<<37)
#define TXT_LUA (1ULL<<38)
#define TYPE_BASE 0xFFFFFFFFFFULL // 40 bit reserved for base types #define TYPE_BASE 0xFFFFFFFFFFULL // 40 bit reserved for base types
// #define FLAG_FIRM (1ULL<<58) // <--- for CXIs containing FIRMs // #define FLAG_FIRM (1ULL<<58) // <--- for CXIs containing FIRMs
@ -76,9 +78,11 @@
#define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB)) #define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB))
#define FTYPE_KEYINSTALL(tp) (tp&(BIN_KEYDB)) #define FTYPE_KEYINSTALL(tp) (tp&(BIN_KEYDB))
#define FTYPE_SCRIPT(tp) (tp&(TXT_SCRIPT)) #define FTYPE_SCRIPT(tp) (tp&(TXT_SCRIPT))
#define FTYPE_LUA(tp) (tp&(TXT_LUA))
#define FTYPE_FONT(tp) (tp&(FONT_PBM|FONT_RIFF)) #define FTYPE_FONT(tp) (tp&(FONT_PBM|FONT_RIFF))
#define FTYPE_TRANSLATION(tp) (tp&(TRANSLATION))
#define FTYPE_GFX(tp) (tp&(GFX_PNG)) #define FTYPE_GFX(tp) (tp&(GFX_PNG))
#define FTYPE_SETABLE(tp) (tp&(FONT_PBM|FONT_RIFF)) #define FTYPE_SETABLE(tp) (tp&(FONT_PBM|FONT_RIFF|TRANSLATION))
#define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM)) #define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM))
#define FTYPE_INSTALLABLE(tp) (tp&(SYS_FIRM)) #define FTYPE_INSTALLABLE(tp) (tp&(SYS_FIRM))
#define FTYPE_AGBSAVE(tp) (tp&(SYS_AGBSAVE)) #define FTYPE_AGBSAVE(tp) (tp&(SYS_AGBSAVE))

View File

@ -1,6 +1,7 @@
#include "fsdrive.h" #include "fsdrive.h"
#include "fsgame.h" #include "fsgame.h"
#include "fsinit.h" #include "fsinit.h"
#include "language.h"
#include "virtual.h" #include "virtual.h"
#include "vcart.h" #include "vcart.h"
#include "sddata.h" #include "sddata.h"
@ -84,13 +85,13 @@ bool GetFATVolumeLabel(const char* drv, char* label) {
} }
bool GetRootDirContentsWorker(DirStruct* contents) { bool GetRootDirContentsWorker(DirStruct* contents) {
static const char* drvname[] = { FS_DRVNAME }; const char* drvname[] = { FS_DRVNAME };
static const char* drvnum[] = { FS_DRVNUM }; static const char* drvnum[] = { FS_DRVNUM };
u32 n_entries = 0; u32 n_entries = 0;
char sdlabel[DRV_LABEL_LEN]; char sdlabel[DRV_LABEL_LEN];
if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel)) if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel))
strcpy(sdlabel, "NOLABEL"); strcpy(sdlabel, STR_LAB_NOLABEL);
char carttype[16]; char carttype[16];
GetVCartTypeString(carttype); GetVCartTypeString(carttype);
@ -101,15 +102,15 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
if (!DriveType(drvnum[i])) continue; // drive not available if (!DriveType(drvnum[i])) continue; // drive not available
entry->p_name = 4; entry->p_name = 4;
entry->name = entry->path + entry->p_name; entry->name = entry->path + entry->p_name;
memset(entry->path, 0x00, 64); memset(entry->path, 0x00, 256);
snprintf(entry->path, 4, "%s", drvnum[i]); snprintf(entry->path, 4, "%s", drvnum[i]);
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
snprintf(entry->name, 32, "[%s] %s", drvnum[i], snprintf(entry->name, 252, "[%s] %s", drvnum[i],
(*(drvnum[i]) == '7') ? "FAT IMAGE" : (*(drvnum[i]) == '7') ? STR_LAB_FAT_IMAGE :
(*(drvnum[i]) == '8') ? "BONUS DRIVE" : (*(drvnum[i]) == '8') ? STR_LAB_BONUS_DRIVE :
(*(drvnum[i]) == '9') ? "RAMDRIVE" : "UNK"); (*(drvnum[i]) == '9') ? STR_LAB_RAMDRIVE : "UNK");
else if (*(drvnum[i]) == 'G') // Game drive special handling else if (*(drvnum[i]) == 'G') // Game drive special handling
snprintf(entry->name, 32, "[%s] %s %s", drvnum[i], snprintf(entry->name, 252, "[%s] %s %s", drvnum[i],
(GetMountState() & GAME_CIA ) ? "CIA" : (GetMountState() & GAME_CIA ) ? "CIA" :
(GetMountState() & GAME_NCSD ) ? "NCSD" : (GetMountState() & GAME_NCSD ) ? "NCSD" :
(GetMountState() & GAME_NCCH ) ? "NCCH" : (GetMountState() & GAME_NCCH ) ? "NCCH" :
@ -119,10 +120,10 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
(GetMountState() & SYS_FIRM ) ? "FIRM" : (GetMountState() & SYS_FIRM ) ? "FIRM" :
(GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]); (GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]);
else if (*(drvnum[i]) == 'C') // Game cart handling else if (*(drvnum[i]) == 'C') // Game cart handling
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], carttype); snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], carttype);
else if (*(drvnum[i]) == '0') // SD card handling else if (*(drvnum[i]) == '0') // SD card handling
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel); snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
else snprintf(entry->name, 32, "[%s] %s", drvnum[i], drvname[i]); else snprintf(entry->name, 252, "[%s] %s", drvnum[i], drvname[i]);
entry->size = GetTotalSpace(entry->path); entry->size = GetTotalSpace(entry->path);
entry->type = T_ROOT; entry->type = T_ROOT;
entry->marked = 0; entry->marked = 0;
@ -211,7 +212,7 @@ void SearchDirContents(DirStruct* contents, const char* path, const char* patter
void GetDirContents(DirStruct* contents, const char* path) { void GetDirContents(DirStruct* contents, const char* path) {
if (*search_path && (DriveType(path) & DRV_SEARCH)) { if (*search_path && (DriveType(path) & DRV_SEARCH)) {
ShowString("Searching, please wait..."); ShowString("%s", STR_SEARCHING_PLEASE_WAIT);
SearchDirContents(contents, search_path, search_pattern, true); SearchDirContents(contents, search_path, search_pattern, true);
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
} else if (title_manager_mode && (DriveType(path) & DRV_TITLEMAN)) { } else if (title_manager_mode && (DriveType(path) & DRV_TITLEMAN)) {
@ -231,7 +232,7 @@ uint64_t GetFreeSpace(const char* path)
FATFS* fsobj = GetMountedFSObject(path); FATFS* fsobj = GetMountedFSObject(path);
if ((pdrv < 0) || !fsobj) return 0; if ((pdrv < 0) || !fsobj) return 0;
snprintf(fsname, 3, "%i:", pdrv); snprintf(fsname, sizeof(fsname), "%i:", pdrv);
if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK) if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK)
return 0; return 0;

View File

@ -32,16 +32,16 @@
#define DRV_LABEL_LEN (36) #define DRV_LABEL_LEN (36)
#define FS_DRVNAME \ #define FS_DRVNAME \
"SDCARD", \ STR_LAB_SDCARD, \
"SYSNAND CTRNAND", "SYSNAND TWLN", "SYSNAND TWLP", "SYSNAND SD", "SYSNAND VIRTUAL", \ STR_LAB_SYSNAND_CTRNAND, STR_LAB_SYSNAND_TWLN, STR_LAB_SYSNAND_TWLP, STR_LAB_SYSNAND_SD, STR_LAB_SYSNAND_VIRTUAL, \
"EMUNAND CTRNAND", "EMUNAND TWLN", "EMUNAND TWLP", "EMUNAND SD", "EMUNAND VIRTUAL", \ STR_LAB_EMUNAND_CTRNAND, STR_LAB_EMUNAND_TWLN, STR_LAB_EMUNAND_TWLP, STR_LAB_EMUNAND_SD, STR_LAB_EMUNAND_VIRTUAL, \
"IMGNAND CTRNAND", "IMGNAND TWLN", "IMGNAND TWLP", "IMGNAND VIRTUAL", \ STR_LAB_IMGNAND_CTRNAND, STR_LAB_IMGNAND_TWLN, STR_LAB_IMGNAND_TWLP, STR_LAB_IMGNAND_VIRTUAL, \
"GAMECART", \ STR_LAB_GAMECART, \
"GAME IMAGE", "AESKEYDB IMAGE", "BDRI IMAGE", "DISA/DIFF IMAGE", \ STR_LAB_GAME_IMAGE, STR_LAB_AESKEYDB_IMAGE, STR_LAB_BDRI_IMAGE, STR_LAB_DISA_DIFF_IMAGE, \
"MEMORY VIRTUAL", \ STR_LAB_MEMORY_VIRTUAL, \
"VRAM VIRTUAL", \ STR_LAB_VRAM_VIRTUAL, \
"TITLE MANAGER", \ STR_LAB_TITLE_MANAGER, \
"LAST SEARCH" \ STR_LAB_LAST_SEARCH
#define FS_DRVNUM \ #define FS_DRVNUM \
"0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", \ "0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", \

View File

@ -1,6 +1,7 @@
#include "fsgame.h" #include "fsgame.h"
#include "fsperm.h" #include "fsperm.h"
#include "gameutil.h" #include "gameutil.h"
#include "language.h"
#include "tie.h" #include "tie.h"
#include "ui.h" #include "ui.h"
#include "vff.h" #include "vff.h"
@ -39,7 +40,7 @@ bool GoodRenamer(DirEntry* entry, bool ask) {
TruncateString(oldname_tr, entry->name, 32, 8); TruncateString(oldname_tr, entry->name, 32, 8);
strncpy(newname_ww, goodname, 256); strncpy(newname_ww, goodname, 256);
WordWrapString(newname_ww, 32); WordWrapString(newname_ww, 32);
if (!ShowPrompt(true, "%s\nRename to good name?\n \n%s", oldname_tr, newname_ww)) if (!ShowPrompt(true, "%s\n%s\n \n%s", oldname_tr, STR_RENAME_TO_GOOD_NAME, newname_ww))
return true; // call it a success because user choice return true; // call it a success because user choice
} }

View File

@ -21,7 +21,7 @@ bool InitExtFS() {
for (u32 i = 1; i < NORM_FS; i++) { for (u32 i = 1; i < NORM_FS; i++) {
char fsname[8]; char fsname[8];
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
if (fs_mounted[i]) continue; if (fs_mounted[i]) continue;
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK); fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) { if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) {
@ -44,7 +44,7 @@ bool InitImgFS(const char* path) {
u32 drv_i = NORM_FS - IMGN_FS; u32 drv_i = NORM_FS - IMGN_FS;
char fsname[8]; char fsname[8];
for (; drv_i < NORM_FS; drv_i++) { for (; drv_i < NORM_FS; drv_i++) {
snprintf(fsname, 7, "%lu:", drv_i); snprintf(fsname, sizeof(fsname), "%lu:", drv_i);
if (!(DriveType(fsname)&DRV_IMAGE)) break; if (!(DriveType(fsname)&DRV_IMAGE)) break;
} }
// deinit virtual filesystem // deinit virtual filesystem
@ -58,7 +58,7 @@ bool InitImgFS(const char* path) {
else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1; else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1;
// reinit image filesystem // reinit image filesystem
for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) { for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) {
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK); fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
} }
return GetMountState(); return GetMountState();
@ -71,7 +71,7 @@ void DeinitExtFS() {
for (u32 i = NORM_FS - 1; i > 0; i--) { for (u32 i = NORM_FS - 1; i > 0; i--) {
if (fs_mounted[i]) { if (fs_mounted[i]) {
char fsname[8]; char fsname[8];
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
f_mount(NULL, fsname, 1); f_mount(NULL, fsname, 1);
fs_mounted[i] = false; fs_mounted[i] = false;
} }
@ -91,7 +91,7 @@ void DismountDriveType(u32 type) { // careful with this - no safety checks
} }
for (u32 i = 0; i < NORM_FS; i++) { for (u32 i = 0; i < NORM_FS; i++) {
char fsname[8]; char fsname[8];
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
if (!fs_mounted[i] || !(type & DriveType(fsname))) if (!fs_mounted[i] || !(type & DriveType(fsname)))
continue; continue;
f_mount(NULL, fsname, 1); f_mount(NULL, fsname, 1);

View File

@ -4,6 +4,7 @@
#include "image.h" #include "image.h"
#include "unittype.h" #include "unittype.h"
#include "essentials.h" #include "essentials.h"
#include "language.h"
#include "ui.h" #include "ui.h"
#include "sdmmc.h" #include "sdmmc.h"
@ -20,7 +21,7 @@
static u32 write_permissions = PERM_BASE; static u32 write_permissions = PERM_BASE;
bool CheckWritePermissions(const char* path) { bool CheckWritePermissions(const char* path) {
char area_name[16]; char area_name[UTF_BUFFER_BYTESIZE(16)];
int drvtype = DriveType(path); int drvtype = DriveType(path);
u32 perm; u32 perm;
@ -40,7 +41,7 @@ bool CheckWritePermissions(const char* path) {
// SD card write protection check // SD card write protection check
if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) { if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) {
ShowPrompt(false, "SD card is write protected!\nCan't continue."); ShowPrompt(false, "%s", STR_SD_WRITE_PROTECTED_CANT_CONTINUE);
return false; return false;
} }
@ -63,7 +64,7 @@ bool CheckWritePermissions(const char* path) {
if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3; if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3;
} }
perm = perms[lvl]; perm = perms[lvl];
snprintf(area_name, 16, "SysNAND (lvl%lu)", lvl); snprintf(area_name, sizeof(area_name), STR_SYSNAND_LVL_N, lvl);
} else if (drvtype & DRV_EMUNAND) { } else if (drvtype & DRV_EMUNAND) {
static const u32 perms[] = { PERM_EMU_LVL0, PERM_EMU_LVL1 }; static const u32 perms[] = { PERM_EMU_LVL0, PERM_EMU_LVL1 };
u32 lvl = (drvtype & (DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0; u32 lvl = (drvtype & (DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0;
@ -73,34 +74,34 @@ bool CheckWritePermissions(const char* path) {
if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1; if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1;
} }
perm = perms[lvl]; perm = perms[lvl];
snprintf(area_name, 16, "EmuNAND (lvl%lu)", lvl); snprintf(area_name, sizeof(area_name), STR_EMUNAND_LVL_N, lvl);
} else if (drvtype & DRV_GAME) { } else if (drvtype & DRV_GAME) {
perm = PERM_GAME; perm = PERM_GAME;
snprintf(area_name, 16, "game images"); snprintf(area_name, sizeof(area_name), "%s", STR_GAME_IMAGES);
} else if (drvtype & DRV_CART) { } else if (drvtype & DRV_CART) {
perm = PERM_CART; perm = PERM_CART;
snprintf(area_name, 16, "gamecart saves"); snprintf(area_name, sizeof(area_name), "%s", STR_GAMECART_SAVES);
} else if (drvtype & DRV_VRAM) { } else if (drvtype & DRV_VRAM) {
perm = PERM_VRAM; perm = PERM_VRAM;
snprintf(area_name, 16, "vram0"); snprintf(area_name, sizeof(area_name), "vram0");
} else if (drvtype & DRV_XORPAD) { } else if (drvtype & DRV_XORPAD) {
perm = PERM_XORPAD; perm = PERM_XORPAD;
snprintf(area_name, 16, "XORpads"); snprintf(area_name, sizeof(area_name), "XORpads");
} else if (drvtype & DRV_IMAGE) { } else if (drvtype & DRV_IMAGE) {
perm = PERM_IMAGE; perm = PERM_IMAGE;
snprintf(area_name, 16, "images"); snprintf(area_name, sizeof(area_name), "%s", STR_IMAGES);
} else if (drvtype & DRV_MEMORY) { } else if (drvtype & DRV_MEMORY) {
perm = PERM_MEMORY; perm = PERM_MEMORY;
snprintf(area_name, 16, "memory areas"); snprintf(area_name, sizeof(area_name), "%s", STR_MEMORY_AREAS);
} else if (strncasecmp(path_f, "0:/Nintendo 3DS", 15) == 0) { // this check could be better } else if (strncasecmp(path_f, "0:/Nintendo 3DS", 15) == 0) { // this check could be better
perm = PERM_SDDATA; perm = PERM_SDDATA;
snprintf(area_name, 16, "SD system data"); snprintf(area_name, sizeof(area_name), "%s", STR_SD_SYSTEM_DATA);
} else if (drvtype & DRV_SDCARD) { } else if (drvtype & DRV_SDCARD) {
perm = PERM_SDCARD; perm = PERM_SDCARD;
snprintf(area_name, 16, "SD card"); snprintf(area_name, sizeof(area_name), "%s", STR_SD_CARD);
} else if (drvtype & DRV_RAMDRIVE) { } else if (drvtype & DRV_RAMDRIVE) {
perm = PERM_RAMDRIVE; perm = PERM_RAMDRIVE;
snprintf(area_name, 16, "RAM drive"); snprintf(area_name, sizeof(area_name), "%s", STR_RAM_DRIVE);
} else { } else {
return false; return false;
} }
@ -112,14 +113,14 @@ bool CheckWritePermissions(const char* path) {
// offer unlock if possible // offer unlock if possible
if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) { if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) {
// ask the user // ask the user
if (!ShowPrompt(true, "Writing to %s is locked!\nUnlock it now?", area_name)) if (!ShowPrompt(true, STR_WRITING_TO_DRIVE_IS_LOCKED_UNLOCK_NOW, area_name))
return false; return false;
return SetWritePermissions(perm, true); return SetWritePermissions(perm, true);
} }
// unlock not possible // unlock not possible
ShowPrompt(false, "Unlock write permission for\n%s is not allowed.", area_name); ShowPrompt(false, STR_UNLOCK_WRITE_FOR_DRIVE_NOT_ALLOWED, area_name);
return false; return false;
} }
@ -144,65 +145,65 @@ bool SetWritePermissions(u32 perm, bool add_perm) {
switch (perm) { switch (perm) {
case PERM_BASE: case PERM_BASE:
if (!ShowUnlockSequence(1, "You want to enable base\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_BASE_WRITE))
return false; return false;
break; break;
case PERM_SDCARD: case PERM_SDCARD:
if (!ShowUnlockSequence(1, "You want to enable SD card\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SD_WRITE))
return false; return false;
break; break;
case PERM_IMAGE: case PERM_IMAGE:
if (!ShowUnlockSequence(1, "You want to enable image\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_IMAGE_WRITE))
return false; return false;
break; break;
case PERM_RAMDRIVE: case PERM_RAMDRIVE:
if (!ShowUnlockSequence(1, "You want to enable RAM drive\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_RAM_DRIVE_WRITE))
return false; return false;
break; break;
case PERM_EMU_LVL0: case PERM_EMU_LVL0:
if (!ShowUnlockSequence(1, "You want to enable EmuNAND\nlvl0 writing permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_EMUNAND_0_WRITE))
return false; return false;
break; break;
case PERM_SYS_LVL0: case PERM_SYS_LVL0:
if (!ShowUnlockSequence(1, "You want to enable SysNAND\nlvl0 writing permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SYSNAND_0_WRITE))
return false; return false;
break; break;
case PERM_EMU_LVL1: case PERM_EMU_LVL1:
if (!ShowUnlockSequence(2, "You want to enable EmuNAND\nlvl1 writing permissions.\n \nThis enables you to modify\nrecoverable system data,\nuser data & savegames.")) if (!ShowUnlockSequence(2, "%s", STR_ENABLE_EMUNAND_1_WRITE))
return false; return false;
break; break;
case PERM_SYS_LVL1: case PERM_SYS_LVL1:
if (!ShowUnlockSequence(2, "You want to enable SysNAND\nlvl1 writing permissions.\n \nThis enables you to modify\nsystem data, installations,\nuser data & savegames.")) if (!ShowUnlockSequence(2, "%s", STR_ENABLE_SYSNAND_1_WRITE))
return false; return false;
break; break;
case PERM_CART: case PERM_CART:
if (!ShowUnlockSequence(2, "You want to enable gamecart\nsave writing permissions.")) if (!ShowUnlockSequence(2, "%s", STR_ENABLE_GAMECART_SAVE_WRITE))
return false; return false;
break; break;
#ifndef SAFEMODE #ifndef SAFEMODE
case PERM_SYS_LVL2: case PERM_SYS_LVL2:
if (!ShowUnlockSequence(3, "!Better be careful!\n \nYou want to enable SysNAND\nlvl2 writing permissions.\n \nThis enables you to modify\nirrecoverable system data!")) if (!ShowUnlockSequence(3, "%s", STR_ENABLE_SYSNAND_2_WRITE))
return false; return false;
break; break;
case PERM_MEMORY: case PERM_MEMORY:
if (!ShowUnlockSequence(4, "!Better be careful!\n \nYou want to enable memory\nwriting permissions.\n \nWriting to certain areas may\nlead to unexpected results.")) if (!ShowUnlockSequence(4, "%s", STR_ENABLE_MEMORY_WRITE))
return false; return false;
break; break;
case PERM_SDDATA: case PERM_SDDATA:
if (!ShowUnlockSequence(5, "!THIS IS NOT RECOMMENDED!\n \nYou want to enable SD data\nwriting permissions.\n \nEverything here is encrypted.\nIt is recommended to use the\nA:/B: drives for modification\nof installations, user data &\nsavegames instead.")) if (!ShowUnlockSequence(5, "%s", STR_ENABLE_SD_DATA_WRITE))
return false; return false;
break; break;
case PERM_SYS_LVL3: case PERM_SYS_LVL3:
if (!ShowUnlockSequence(6, "!THIS IS YOUR ONLY WARNING!\n \nYou want to enable SysNAND\nlvl3 writing permissions.\n \nThis enables you to OVERWRITE\nyour bootloader installation,\nessential system files and/or\nBRICK your console!")) if (!ShowUnlockSequence(6, "%s", STR_ENABLE_SYSNAND_3_WRITE))
return false; return false;
break; break;
default: default:
ShowPrompt(false, "Unlock write permission is not allowed."); ShowPrompt(false, "%s", STR_UNLOCK_WRITE_NOT_ALLOWED);
return false; return false;
break; break;
#else #else
default: default:
ShowPrompt(false, "Can't unlock write permission.\nTry GodMode9 instead!"); ShowPrompt(false, "%s", STR_CANT_UNLOCK_WRITE_TRY_GODMODE9);
return false; return false;
break; break;
#endif #endif

View File

@ -11,9 +11,10 @@
#include "ff.h" #include "ff.h"
#include "ui.h" #include "ui.h"
#include "swkbd.h" #include "swkbd.h"
#include "language.h"
#define SKIP_CUR (1UL<<10) #define SKIP_CUR (1UL<<11)
#define OVERWRITE_CUR (1UL<<11) #define OVERWRITE_CUR (1UL<<12)
#define _MAX_FS_OPT 8 // max file selector options #define _MAX_FS_OPT 8 // max file selector options
@ -46,13 +47,13 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
// FAT size check // FAT size check
if (fat_size < 0x80000) { // minimum free space: 256MB if (fat_size < 0x80000) { // minimum free space: 256MB
ShowPrompt(false, "Error: SD card is too small"); ShowPrompt(false, "%s", STR_ERROR_SD_TOO_SMALL);
return false; return false;
} }
// Write protection check // Write protection check
if (SD_WRITE_PROTECTED) { if (SD_WRITE_PROTECTED) {
ShowPrompt(false, "SD card is write protected!\nCan't continue."); ShowPrompt(false, "%s", STR_SD_WRITE_PROTECTED_CANT_CONTINUE);
return false; return false;
} }
@ -67,15 +68,15 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
// one last warning.... // one last warning....
// 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough // 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough
if (!ShowUnlockSequence(5, "!WARNING!\n \nProceeding will format this SD.\nThis will irreversibly delete\nALL data on it.")) if (!ShowUnlockSequence(5, "%s", STR_WARNING_PROCEEDING_WILL_FORMAT_SD_DELETE_ALL_DATA))
return false; return false;
ShowString("Formatting SD, please wait..."); ShowString("%s", STR_FORMATTING_SD_PLEASE_WAIT);
// write the MBR to disk // write the MBR to disk
// !this assumes a fully deinitialized file system! // !this assumes a fully deinitialized file system!
if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) || if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) ||
(emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) { (emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) {
ShowPrompt(false, "Error: SD card i/o failure"); ShowPrompt(false, "%s", STR_ERROR_SD_CARD_IO_FAILURE);
return false; return false;
} }
@ -104,9 +105,9 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
} }
bool SetupBonusDrive(void) { bool SetupBonusDrive(void) {
if (!ShowUnlockSequence(3, "Format the bonus drive?\nThis will irreversibly delete\nALL data on it.")) if (!ShowUnlockSequence(3, "%s", STR_FORMAT_BONUS_DRIVE_DELETE_ALL_DATA))
return false; return false;
ShowString("Formatting drive, please wait..."); ShowString("%s", STR_FORMATTING_DRIVE_PLEASE_WAIT);
if (GetMountState() & IMG_NAND) InitImgFS(NULL); if (GetMountState() & IMG_NAND) InitImgFS(NULL);
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
@ -130,7 +131,7 @@ bool FileUnlock(const char* path) {
char pathstr[UTF_BUFFER_BYTESIZE(32)]; char pathstr[UTF_BUFFER_BYTESIZE(32)];
TruncateString(pathstr, path, 32, 8); TruncateString(pathstr, path, 32, 8);
if (GetMountState() && (res == FR_LOCKED) && if (GetMountState() && (res == FR_LOCKED) &&
(ShowPrompt(true, "%s\nFile is currently mounted.\nUnmount to unlock?", pathstr))) { (ShowPrompt(true, "%s\n%s", pathstr, STR_FILE_IS_MOUNTED_UNMOUNT_TO_UNLOCK))) {
InitImgFS(NULL); InitImgFS(NULL);
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false; return false;
@ -250,7 +251,7 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
if (!CheckWritePermissions(dest)) return false; if (!CheckWritePermissions(dest)) return false;
if (strncasecmp(dest, orig, 256) == 0) { if (strncasecmp(dest, orig, 256) == 0) {
ShowPrompt(false, "Error: Can't inject file into itself"); ShowPrompt(false, "%s", STR_ERROR_CANT_INJECT_FILE_INTO_ITSELF);
return false; return false;
} }
@ -269,12 +270,12 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
// check file limits // check file limits
if (!allow_expand && (off_dest + size > fvx_size(&dfile))) { if (!allow_expand && (off_dest + size > fvx_size(&dfile))) {
ShowPrompt(false, "Operation would write beyond end of file"); ShowPrompt(false, "%s", STR_OPERATION_WOULD_WRITE_BEYOND_EOF);
fvx_close(&dfile); fvx_close(&dfile);
fvx_close(&ofile); fvx_close(&ofile);
return false; return false;
} else if (off_orig + size > fvx_size(&ofile)) { } else if (off_orig + size > fvx_size(&ofile)) {
ShowPrompt(false, "Not enough data in file"); ShowPrompt(false, "%s", STR_NOT_ENOUGH_DATA_IN_FILE);
fvx_close(&dfile); fvx_close(&dfile);
fvx_close(&ofile); fvx_close(&ofile);
return false; return false;
@ -295,8 +296,8 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
ret = false; ret = false;
if (ret && !ShowProgress(pos + bytes_read, size, orig)) { if (ret && !ShowProgress(pos + bytes_read, size, orig)) {
if (flags && (*flags & NO_CANCEL)) { if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is not allowed here"); ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "B button detected. Cancel?"); } else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
ShowProgress(pos + bytes_read, size, orig); ShowProgress(pos + bytes_read, size, orig);
} }
@ -325,7 +326,7 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
// check file limits // check file limits
if (!allow_expand && (offset + size > fvx_size(&dfile))) { if (!allow_expand && (offset + size > fvx_size(&dfile))) {
ShowPrompt(false, "Operation would write beyond end of file"); ShowPrompt(false, "%s", STR_OPERATION_WOULD_WRITE_BEYOND_EOF);
fvx_close(&dfile); fvx_close(&dfile);
return false; return false;
} }
@ -345,8 +346,8 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
ret = false; ret = false;
if (ret && !ShowProgress(pos + bytes_written, size, dest)) { if (ret && !ShowProgress(pos + bytes_written, size, dest)) {
if (flags && (*flags & NO_CANCEL)) { if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is not allowed here"); ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "B button detected. Cancel?"); } else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, dest); ShowProgress(0, 0, dest);
ShowProgress(pos + bytes_written, size, dest); ShowProgress(pos + bytes_written, size, dest);
} }
@ -362,8 +363,8 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
bool FileCreateDummy(const char* cpath, const char* filename, u64 size) { bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
char npath[256]; // 256 is the maximum length of a full path char npath[256]; // 256 is the maximum length of a full path
if (!CheckWritePermissions(cpath)) return false; if (!CheckWritePermissions(cpath)) return false;
if (filename) snprintf(npath, 255, "%s/%s", cpath, filename); if (filename) snprintf(npath, sizeof(npath), "%s/%s", cpath, filename);
else snprintf(npath, 255, "%s", cpath); else snprintf(npath, sizeof(npath), "%s", cpath);
// create dummy file (fail if already existing) // create dummy file (fail if already existing)
// then, expand the file size via cluster preallocation // then, expand the file size via cluster preallocation
@ -380,7 +381,7 @@ bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
bool DirCreate(const char* cpath, const char* dirname) { bool DirCreate(const char* cpath, const char* dirname) {
char npath[256]; // 256 is the maximum length of a full path char npath[256]; // 256 is the maximum length of a full path
if (!CheckWritePermissions(cpath)) return false; if (!CheckWritePermissions(cpath)) return false;
snprintf(npath, 255, "%s/%s", cpath, dirname); snprintf(npath, sizeof(npath), "%s/%s", cpath, dirname);
if (fa_mkdir(npath) != FR_OK) return false; if (fa_mkdir(npath) != FR_OK) return false;
return (fa_stat(npath, NULL) == FR_OK); return (fa_stat(npath, NULL) == FR_OK);
} }
@ -466,7 +467,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
// the copy process takes place here // the copy process takes place here
if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) { if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) {
if (ShowPrompt(true, "%s\nB button detected. Cancel?", deststr)) return false; if (ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL)) return false;
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
} }
if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing
@ -476,14 +477,14 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
char* fname = orig + strnlen(orig, 256); char* fname = orig + strnlen(orig, 256);
if (append) { if (append) {
if (!silent) ShowPrompt(false, "%s\nError: Cannot append a folder", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_CANNOT_APPEND_FOLDER);
return false; return false;
} }
// create the destination folder if it does not already exist // create the destination folder if it does not already exist
if (fvx_opendir(&pdir, dest) != FR_OK) { if (fvx_opendir(&pdir, dest) != FR_OK) {
if (fvx_mkdir(dest) != FR_OK) { if (fvx_mkdir(dest) != FR_OK) {
if (!silent) ShowPrompt(false, "%s\nError: Overwriting file with dir", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_OVERWRITING_FILE_WITH_DIR);
return false; return false;
} }
} else fvx_closedir(&pdir); } else fvx_closedir(&pdir);
@ -516,7 +517,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
} else if (move) { // moving if destination exists } else if (move) { // moving if destination exists
if (fvx_stat(dest, &fno) != FR_OK) return false; if (fvx_stat(dest, &fno) != FR_OK) return false;
if (fno.fattrib & AM_DIR) { if (fno.fattrib & AM_DIR) {
if (!silent) ShowPrompt(false, "%s\nError: Overwriting dir with file", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_OVERWRITING_DIR_WITH_FILE);
return false; return false;
} }
if (fvx_unlink(dest) != FR_OK) return false; if (fvx_unlink(dest) != FR_OK) return false;
@ -535,7 +536,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
if ((!append || (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)) && if ((!append || (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)) &&
(fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)) { (fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)) {
if (!silent) ShowPrompt(false, "%s\nError: Cannot open destination file", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_CANNOT_OPEN_DESTINATION_FILE);
fvx_close(&ofile); fvx_close(&ofile);
return false; return false;
} }
@ -544,7 +545,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
osize = fvx_size(&ofile); osize = fvx_size(&ofile);
dsize = append ? fvx_size(&dfile) : 0; // always 0 if not appending to file dsize = append ? fvx_size(&dfile) : 0; // always 0 if not appending to file
if ((fvx_lseek(&dfile, (osize + dsize)) != FR_OK) || (fvx_sync(&dfile) != FR_OK) || (fvx_tell(&dfile) != (osize + dsize))) { // check space via cluster preallocation if ((fvx_lseek(&dfile, (osize + dsize)) != FR_OK) || (fvx_sync(&dfile) != FR_OK) || (fvx_tell(&dfile) != (osize + dsize))) { // check space via cluster preallocation
if (!silent) ShowPrompt(false, "%s\nError: Not enough space available", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_NOT_ENOUGH_SPACE_AVAILABLE);
ret = false; ret = false;
} }
@ -566,8 +567,8 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
u64 total = osize; u64 total = osize;
if (ret && !ShowProgress(current, total, orig)) { if (ret && !ShowProgress(current, total, orig)) {
if (flags && (*flags & NO_CANCEL)) { if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "%s\nCancel is not allowed here", deststr); ShowPrompt(false, "%s\n%s", deststr, STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "%s\nB button detected. Cancel?", deststr); } else ret = !ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
ShowProgress(current, total, orig); ShowProgress(current, total, orig);
} }
@ -614,14 +615,14 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// moving only for regular FAT drives (= not alias drives) // moving only for regular FAT drives (= not alias drives)
if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) { if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) {
ShowPrompt(false, "Error: Only FAT files can be moved"); ShowPrompt(false, "%s", STR_ERROR_ONLY_FAT_FILES_CAN_BE_MOVED);
return false; return false;
} }
// is destination part of origin? // is destination part of origin?
u32 olen = strnlen(lorig, 255); u32 olen = strnlen(lorig, 255);
if ((strncasecmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) { if ((strncasecmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) {
ShowPrompt(false, "%s\nError: Destination is part of origin", deststr); ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_DESTINATION_IS_PART_OF_ORIGIN);
return false; return false;
} }
@ -633,7 +634,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// check & fix destination == origin // check & fix destination == origin
while (strncasecmp(ldest, lorig, 255) == 0) { while (strncasecmp(ldest, lorig, 255) == 0) {
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s\nDestination equals origin\nChoose another name?", deststr)) if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s\n%s", deststr, STR_ERROR_DESTINATION_EQUALS_ORIGIN_CHOOSE_ANOTHER_NAME))
return false; return false;
} }
@ -644,12 +645,11 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
return true; return true;
} }
const char* optionstr[5] = const char* optionstr[5] =
{"Choose new name", "Overwrite file(s)", "Skip file(s)", "Overwrite all", "Skip all"}; {STR_CHOOSE_NEW_NAME, STR_OVERWRITE_FILES, STR_SKIP_FILES, STR_OVERWRITE_ALL, STR_SKIP_ALL};
u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr, u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr, STR_DESTINATION_ALREADY_EXISTS, deststr);
"Destination already exists:\n%s", deststr);
if (user_select == 1) { if (user_select == 1) {
do { do {
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "Choose new destination name")) if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s", STR_CHOOSE_NEW_DESTINATION_NAME))
return false; return false;
} while (fa_stat(ldest, NULL) == FR_OK); } while (fa_stat(ldest, NULL) == FR_OK);
} else if (user_select == 2) { } else if (user_select == 2) {
@ -673,7 +673,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// setup buffer // setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) { if (!buffer) {
ShowPrompt(false, "Out of memory."); ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
return false; return false;
} }
@ -699,20 +699,20 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// prevent illegal operations // prevent illegal operations
if (force_unmount && (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE))) { if (force_unmount && (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE))) {
ShowPrompt(false, "Copy operation is not allowed"); ShowPrompt(false, "%s", STR_COPY_OPERATION_IS_NOT_ALLOWED);
return false; return false;
} }
// check destination == origin // check destination == origin
if (strncasecmp(ldest, lorig, 255) == 0) { if (strncasecmp(ldest, lorig, 255) == 0) {
ShowPrompt(false, "%s\nDestination equals origin", deststr); ShowPrompt(false, "%s\n%s", deststr, STR_DESTINATION_EQUALS_ORIGIN);
return false; return false;
} }
// setup buffer // setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) { if (!buffer) {
ShowPrompt(false, "Out of memory."); ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
return false; return false;
} }
@ -731,7 +731,7 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
char dest[256]; // maximum path name length in FAT char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/'); char* oname = strrchr(orig, '/');
if (oname == NULL) return false; // not a proper origin path if (oname == NULL) return false; // not a proper origin path
snprintf(dest, 255, "%s/%s", destdir, (++oname)); snprintf(dest, sizeof(dest), "%s/%s", destdir, (++oname));
// virtual destination special handling // virtual destination special handling
if (GetVirtualSource(destdir) & ~VRT_BDRI) { if (GetVirtualSource(destdir) & ~VRT_BDRI) {
@ -745,9 +745,9 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
if (!ReadVirtualDir(&dvfile, &vdir)) return false; if (!ReadVirtualDir(&dvfile, &vdir)) return false;
if (dvfile.size == osize) break; // file found if (dvfile.size == osize) break; // file found
} }
if (!ShowPrompt(true, "Entry not found: %s\nInject into %s instead?", dest, dvfile.name)) if (!ShowPrompt(true, STR_ENTRY_NOT_FOUND_PATH_INJECT_INTO_PATH_INSTEAD, dest, dvfile.name))
return false; return false;
snprintf(dest, 255, "%s/%s", destdir, dvfile.name); snprintf(dest, sizeof(dest), "%s/%s", destdir, dvfile.name);
} else if (osize < dvfile.size) { // if origin is smaller than destination... } else if (osize < dvfile.size) { // if origin is smaller than destination...
char deststr[UTF_BUFFER_BYTESIZE(36)]; char deststr[UTF_BUFFER_BYTESIZE(36)];
char origstr[UTF_BUFFER_BYTESIZE(36)]; char origstr[UTF_BUFFER_BYTESIZE(36)];
@ -758,7 +758,7 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
FormatBytes(osizestr, osize); FormatBytes(osizestr, osize);
FormatBytes(dsizestr, dvfile.size); FormatBytes(dsizestr, dvfile.size);
if (dvfile.size > osize) { if (dvfile.size > osize) {
if (!ShowPrompt(true, "File smaller than available space:\n%s (%s)\n%s (%s)\nContinue?", origstr, osizestr, deststr, dsizestr)) if (!ShowPrompt(true, STR_FILE_SMALLER_THAN_SPACE_SIZES_CONTINUE, origstr, osizestr, deststr, dsizestr))
return false; return false;
} }
} }
@ -772,7 +772,7 @@ bool PathMove(const char* destdir, const char* orig, u32* flags) {
char dest[256]; // maximum path name length in FAT char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/'); char* oname = strrchr(orig, '/');
if (oname == NULL) return false; // not a proper origin path if (oname == NULL) return false; // not a proper origin path
snprintf(dest, 255, "%s/%s", destdir, (++oname)); snprintf(dest, sizeof(dest), "%s/%s", destdir, (++oname));
return PathMoveCopy(dest, orig, flags, true); return PathMoveCopy(dest, orig, flags, true);
} }
@ -832,13 +832,13 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
(entry->type == T_DOTDOT) || (strncmp(entry->name, "._", 2) == 0)) (entry->type == T_DOTDOT) || (strncmp(entry->name, "._", 2) == 0))
continue; continue;
if (!new_style && n_opt == _MAX_FS_OPT) { if (!new_style && n_opt == _MAX_FS_OPT) {
snprintf(opt_names[n_opt++], 32, "[more...]"); snprintf(opt_names[n_opt++], 32, "%s", STR_BRACKET_MORE);
break; break;
} }
if (!new_style) { if (!new_style) {
char temp_str[256]; char temp_str[256];
snprintf(temp_str, 256, "%s", entry->name); snprintf(temp_str, sizeof(temp_str), "%s", entry->name);
if (hide_ext && (entry->type == T_FILE)) { if (hide_ext && (entry->type == T_FILE)) {
char* dot = strrchr(temp_str, '.'); char* dot = strrchr(temp_str, '.');
if (dot) *dot = '\0'; if (dot) *dot = '\0';
@ -849,7 +849,7 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
n_found++; n_found++;
} }
if ((pos >= contents->n_entries) && (n_opt < n_found) && !new_style) if ((pos >= contents->n_entries) && (n_opt < n_found) && !new_style)
snprintf(opt_names[n_opt++], 32, "[more...]"); snprintf(opt_names[n_opt++], 32, "%s", STR_BRACKET_MORE);
if (!n_opt) break; if (!n_opt) break;
const char* optionstr[_MAX_FS_OPT+1] = { NULL }; const char* optionstr[_MAX_FS_OPT+1] = { NULL };
@ -874,7 +874,7 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
if (!n_found) { // not a single matching entry found if (!n_found) { // not a single matching entry found
char pathstr[UTF_BUFFER_BYTESIZE(32)]; char pathstr[UTF_BUFFER_BYTESIZE(32)];
TruncateString(pathstr, path_local, 32, 8); TruncateString(pathstr, path_local, 32, 8);
ShowPrompt(false, "%s\nNo usable entries found.", pathstr); ShowPrompt(false, "%s\n%s", pathstr, STR_NO_USABLE_ENTRIES_FOUND);
return false; return false;
} }
} }

View File

@ -276,7 +276,7 @@ bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable
// build the alias path (id0) // build the alias path (id0)
u32 sha256sum[8]; u32 sha256sum[8];
sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE); sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE);
snprintf(alias, 127, "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX", snprintf(alias, sizeof(alias), "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX",
sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]); sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
// find the alias path (id1) // find the alias path (id1)

View File

@ -17,7 +17,7 @@ bool CheckSupportFile(const char* fname)
const char* base_paths[] = { SUPPORT_FILE_PATHS }; const char* base_paths[] = { SUPPORT_FILE_PATHS };
for (u32 i = 0; i < countof(base_paths); i++) { for (u32 i = 0; i < countof(base_paths); i++) {
char path[256]; char path[256];
snprintf(path, 256, "%s/%s", base_paths[i], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
if (fvx_stat(path, NULL) == FR_OK) if (fvx_stat(path, NULL) == FR_OK)
return true; return true;
} }
@ -40,7 +40,7 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len)
for (u32 i = 0; i < countof(base_paths); i++) { for (u32 i = 0; i < countof(base_paths); i++) {
UINT len32; UINT len32;
char path[256]; char path[256];
snprintf(path, 256, "%s/%s", base_paths[i], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK) if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK)
return len32; return len32;
} }
@ -68,7 +68,7 @@ bool SaveSupportFile(const char* fname, void* buffer, size_t len)
// write support file // write support file
if (idx >= 0) { if (idx >= 0) {
char path[256]; char path[256];
snprintf(path, 256, "%s/%s", base_paths[idx], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[idx], fname);
fvx_unlink(path); fvx_unlink(path);
if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK) if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK)
return true; return true;

View File

@ -3,7 +3,9 @@
#include "common.h" #include "common.h"
// scripts / payloads dir names // scripts / payloads dir names
#define LANGUAGES_DIR "languages"
#define SCRIPTS_DIR "scripts" #define SCRIPTS_DIR "scripts"
#define LUASCRIPTS_DIR "luascripts"
#define PAYLOADS_DIR "payloads" #define PAYLOADS_DIR "payloads"
bool CheckSupportFile(const char* fname); bool CheckSupportFile(const char* fname);
@ -11,5 +13,6 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len);
bool SaveSupportFile(const char* fname, void* buffer, size_t len); bool SaveSupportFile(const char* fname, void* buffer, size_t len);
bool SetAsSupportFile(const char* fname, const char* source); bool SetAsSupportFile(const char* fname, const char* source);
bool GetSupportDir(char* path, const char* dname);
bool CheckSupportDir(const char* fpath); bool CheckSupportDir(const char* fpath);
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern); bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);

View File

@ -8,6 +8,7 @@
#define fvx_tell(fp) ((fp)->fptr) #define fvx_tell(fp) ((fp)->fptr)
#define fvx_size(fp) ((fp)->obj.objsize) #define fvx_size(fp) ((fp)->obj.objsize)
#define fvx_eof(fp) (fvx_tell(fp) == fvx_size(fp))
#define FN_ANY 0x00 #define FN_ANY 0x00
#define FN_HIGHEST 0x01 #define FN_HIGHEST 0x01

View File

@ -18,6 +18,7 @@
#include <libgen.h> #include <libgen.h>
#include "language.h"
#include "common.h" #include "common.h"
#include "timer.h" #include "timer.h"
#include "crc32.h" #include "crc32.h"
@ -143,18 +144,18 @@ static bool BEAT_UpdateProgress(const BEAT_Context *ctx)
static const char *BEAT_ErrString(int error) static const char *BEAT_ErrString(int error)
{ // Get an error description string { // Get an error description string
switch(error) { switch(error) {
case BEAT_OK: return "No error"; case BEAT_OK: return STR_BEAT_NO_ERROR;
case BEAT_EOAL: return "End of action list"; case BEAT_EOAL: return STR_BEAT_END_OF_ACTION_LIST;
case BEAT_ABORTED: return "Aborted by user"; case BEAT_ABORTED: return STR_BEAT_ABORTED_BY_USER;
case BEAT_IO_ERROR: return "Failed to read/write file"; case BEAT_IO_ERROR: return STR_BEAT_FAILED_TO_READ_WRITE_FILE;
case BEAT_OVERFLOW: return "Attempted to write beyond end of file"; case BEAT_OVERFLOW: return STR_BEAT_ATTEMPTED_TO_WRITE_BEYOND_EOF;
case BEAT_BADPATCH: return "Invalid patch file"; case BEAT_BADPATCH: return STR_BEAT_INVALID_PATCH_FILE;
case BEAT_BADINPUT: return "Invalid input file"; case BEAT_BADINPUT: return STR_BEAT_INVALID_INPUT_FILE;
case BEAT_BADOUTPUT: return "Output file checksum mismatch"; case BEAT_BADOUTPUT: return STR_BEAT_OUTPUT_FILE_CHECKSUM_MISMATCH;
case BEAT_BADCHKSUM: return "File checksum failed"; case BEAT_BADCHKSUM: return STR_BEAT_FILE_CHECKSUM_FAILED;
case BEAT_PATCH_EXPECT: return "Expected more patch data"; case BEAT_PATCH_EXPECT: return STR_BEAT_EXPECTED_MORE_PATCH_DATA;
case BEAT_OUT_OF_MEMORY: return "Out of memory"; case BEAT_OUT_OF_MEMORY: return STR_BEAT_OUT_OF_MEMORY;
default: return "Unknown error"; default: return STR_BEAT_UNKNOWN_ERROR;
} }
} }
@ -221,7 +222,7 @@ static s32 BEAT_DecodeSigned(u32 val) // Extract the signed number
static int BEAT_RunActions(BEAT_Context *ctx, const BEAT_Action *acts) static int BEAT_RunActions(BEAT_Context *ctx, const BEAT_Action *acts)
{ // Parses an action list and runs commands specified in `acts` { // Parses an action list and runs commands specified in `acts`
u32 vli, len; u32 vli, len;
int cmd, res; int cmd, res = BEAT_OK;
while((res == BEAT_OK) && while((res == BEAT_OK) &&
(ctx->foff[BEAT_PF] < (BEAT_RANGE(ctx, BEAT_PF) - ctx->eoal_offset))) { (ctx->foff[BEAT_PF] < (BEAT_RANGE(ctx, BEAT_PF) - ctx->eoal_offset))) {
@ -660,19 +661,18 @@ static int BEAT_Run(const char *p, const char *s, const char *d, bool bpm)
progress_timer = timer_start(); progress_timer = timer_start();
res = (bpm ? BPM_InitCTX : BPS_InitCTX)(&ctx, p, s, d); res = (bpm ? BPM_InitCTX : BPS_InitCTX)(&ctx, p, s, d);
if (res != BEAT_OK) { if (res != BEAT_OK) {
ShowPrompt(false, "Failed to initialize %s file:\n%s", ShowPrompt(false, bpm ? STR_FAILED_TO_INITIALIZE_BPM_FILE : STR_FAILED_TO_INITIALIZE_BPS_FILE, BEAT_ErrString(res));
bpm ? "BPM" : "BPS", BEAT_ErrString(res));
} else { } else {
res = (bpm ? BPM_RunActions : BPS_RunActions)(&ctx); res = (bpm ? BPM_RunActions : BPS_RunActions)(&ctx);
switch(res) { switch(res) {
case BEAT_OK: case BEAT_OK:
ShowPrompt(false, "Patch successfully applied"); ShowPrompt(false, "%s", STR_PATCH_SUCCESSFULLY_APPLIED);
break; break;
case BEAT_ABORTED: case BEAT_ABORTED:
ShowPrompt(false, "Patching aborted by user"); ShowPrompt(false, "%s", STR_PATCHING_ABORTED_BY_USER);
break; break;
default: default:
ShowPrompt(false, "Failed to run patch:\n%s", BEAT_ErrString(res)); ShowPrompt(false, STR_FAILED_TO_RUN_PATCH, BEAT_ErrString(res));
break; break;
} }
} }

View File

@ -1,4 +1,5 @@
#include "codelzss.h" #include "codelzss.h"
#include "language.h"
#include "ui.h" #include "ui.h"
#define CODE_COMP_SIZE(f) ((f)->off_size_comp & 0xFFFFFF) #define CODE_COMP_SIZE(f) ((f)->off_size_comp & 0xFFFFFF)
@ -45,10 +46,10 @@ u32 DecompressCodeLzss(u8* code, u32* code_size, u32 max_size) {
// main decompression loop // main decompression loop
while ((ptr_in > comp_start) && (ptr_out > comp_start)) { while ((ptr_in > comp_start) && (ptr_out > comp_start)) {
if (!ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code...")) { if (!ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE)) {
if (ShowPrompt(true, "Decompressing .code...\nB button detected. Cancel?")) return 1; if (ShowPrompt(true, "%s", STR_DECOMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) return 1;
ShowProgress(0, data_end - data_start, "Decompressing .code..."); ShowProgress(0, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code..."); ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
} }
// sanity check // sanity check
@ -242,13 +243,13 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
u8* pDest = a_pCompressed + a_uUncompressedSize; u8* pDest = a_pCompressed + a_uUncompressedSize;
while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) { while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) {
if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code...")) { if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE)) {
if (ShowPrompt(true, "Compressing .code...\nB button detected. Cancel?")) { if (ShowPrompt(true, "%s", STR_COMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) {
bResult = false; bResult = false;
break; break;
} }
ShowProgress(0, a_uUncompressedSize, "Compressing .code..."); ShowProgress(0, a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code..."); ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
} }
u8* pFlag = --pDest; u8* pFlag = --pDest;

View File

@ -12,18 +12,19 @@
// valid addresses for FIRM section loading // valid addresses for FIRM section loading
// pairs of start / end address, provided by Wolfvak // pairs of start / end address, provided by Wolfvak
#define FIRM_VALID_ADDRESS \ #define FIRM_VALID_ADDRESS \
0x08000040, 0x08100000, \
0x18000000, 0x18600000, \ 0x18000000, 0x18600000, \
0x1FF00000, 0x1FFFFC00 0x1FF00000, 0x1FFFFC00
// valid addresses (installable) for FIRM section loading // valid addresses (installable) for FIRM section loading
#define FIRM_VALID_ADDRESS_INSTALL \ #define FIRM_VALID_ADDRESS_INSTALL \
FIRM_VALID_ADDRESS, \ FIRM_VALID_ADDRESS, \
0x08000040, 0x080F7FFF, \
0x10000000, 0x10200000 0x10000000, 0x10200000
// valid addresses (bootable) for FIRM section loading // valid addresses (bootable) for FIRM section loading
#define FIRM_VALID_ADDRESS_BOOT \ #define FIRM_VALID_ADDRESS_BOOT \
FIRM_VALID_ADDRESS, \ FIRM_VALID_ADDRESS, \
0x08000040, 0x08100000, \
0x20000000, 0x27FFFA00 0x20000000, 0x27FFFA00
static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT }; static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT };

View File

@ -87,3 +87,17 @@ u32 ValidateAgbHeader(AgbHeader* agb) {
return 0; return 0;
} */ } */
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
const char* AgbDestStr(const char* code) {
switch(code[3]) {
case 'J': return STR_REGION_JAPAN;
case 'E': return STR_REGION_AMERICAS;
case 'P': return STR_REGION_EUROPE;
case 'D': return STR_REGION_GERMANY;
case 'F': return STR_REGION_FRANCE;
case 'I': return STR_REGION_ITALY;
case 'S': return STR_REGION_SPAIN;
default: return STR_REGION_UNKNOWN;
}
}

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "language.h"
#define GBAVC_MAGIC '.', 'C', 'A', 'A' #define GBAVC_MAGIC '.', 'C', 'A', 'A'
#define AGBSAVE_MAGIC '.', 'S', 'A', 'V' #define AGBSAVE_MAGIC '.', 'S', 'A', 'V'
@ -28,16 +29,6 @@
((size) == GBASAVE_FLASH_64K) || \ ((size) == GBASAVE_FLASH_64K) || \
((size) == GBASAVE_FLASH_128K)) ((size) == GBASAVE_FLASH_128K))
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
#define AGB_DESTSTR(code) \
(((code)[3] == 'J') ? "Japan" : \
((code)[3] == 'E') ? "USA/English" : \
((code)[3] == 'P') ? "Europe/Elsewhere" : \
((code)[3] == 'D') ? "German" : \
((code)[3] == 'F') ? "French" : \
((code)[3] == 'I') ? "Italian" : \
((code)[3] == 'S') ? "Spanish" : "Unknown")
// see: http://3dbrew.org/wiki/3DS_Virtual_Console#Footer // see: http://3dbrew.org/wiki/3DS_Virtual_Console#Footer
// still a lot of unknowns in here, also redundant stuff left out // still a lot of unknowns in here, also redundant stuff left out
@ -89,5 +80,8 @@ typedef struct {
} __attribute__((packed, aligned(16))) AgbHeader; } __attribute__((packed, aligned(16))) AgbHeader;
u32 ValidateAgbSaveHeader(AgbSaveHeader* header); u32 ValidateAgbSaveHeader(AgbSaveHeader* header);
u32 ValidateAgbHeader(AgbHeader* agb); u32 ValidateAgbHeader(AgbHeader* agb);
const char* AgbDestStr(const char* code);

View File

@ -5,6 +5,7 @@
#include "ips.h" #include "ips.h"
#include "common.h" #include "common.h"
#include "fsperm.h" #include "fsperm.h"
#include "language.h"
#include "ui.h" #include "ui.h"
#include "vff.h" #include "vff.h"
@ -30,21 +31,21 @@ char errName[256];
int displayError(int errcode) { int displayError(int errcode) {
switch(errcode) { switch(errcode) {
case IPS_NOTTHIS: case IPS_NOTTHIS:
ShowPrompt(false, "%s\nThe patch is most likely not intended for this file.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCH_MOST_LIKELY_NOT_FOR_THIS_FILE); break;
case IPS_THISOUT: case IPS_THISOUT:
ShowPrompt(false, "%s\nYou most likely applied the patch on the output file.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_YOU_MOST_LIKELY_APPLIED_PATCH_ON_OUTPUT); break;
case IPS_SCRAMBLED: case IPS_SCRAMBLED:
ShowPrompt(false, "%s\nThe patch is technically valid,\nbut seems scrambled or malformed.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCH_TECHNICALLY_VALID_BUT_SEEMS_SCRAMBLED); break;
case IPS_INVALID: case IPS_INVALID:
ShowPrompt(false, "%s\nThe patch is invalid.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCH_IS_INVALID); break;
case IPS_16MB: case IPS_16MB:
ShowPrompt(false, "%s\nOne or both files is bigger than 16MB.\nThe IPS format doesn't support that.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_FILES_BIGGER_THAN_16MB_IPS_DOESNT_SUPPORT_THAT); break;
case IPS_INVALID_FILE_PATH: case IPS_INVALID_FILE_PATH:
ShowPrompt(false, "%s\nThe requested file path was invalid.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_REQUESTED_FILE_PATH_WAS_INVALID); break;
case IPS_CANCELED: case IPS_CANCELED:
ShowPrompt(false, "%s\nPatching canceled.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCHING_CANCELED); break;
case IPS_MEMORY: case IPS_MEMORY:
ShowPrompt(false, "%s\nNot enough memory.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_NOT_ENOUGH_MEMORY); break;
} }
fvx_close(&patchFile); fvx_close(&patchFile);
fvx_close(&inFile); fvx_close(&inFile);
@ -112,7 +113,7 @@ UINT read24() {
int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) { int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) {
int error = IPS_INVALID; int error = IPS_INVALID;
UINT outlen_min, outlen_max, outlen_min_mem; UINT outlen_min, outlen_max, outlen_min_mem;
snprintf(errName, 256, "%s", patchName); snprintf(errName, sizeof(errName), "%s", patchName);
if (fvx_open(&patchFile, patchName, FA_READ) != FR_OK) return displayError(IPS_INVALID_FILE_PATH); if (fvx_open(&patchFile, patchName, FA_READ) != FR_OK) return displayError(IPS_INVALID_FILE_PATH);
patchSize = fvx_size(&patchFile); patchSize = fvx_size(&patchFile);
@ -140,7 +141,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
while (offset != 0x454F46) // 454F46=EOF while (offset != 0x454F46) // 454F46=EOF
{ {
if (!ShowProgress(patchOffset, patchSize, patchName)) { if (!ShowProgress(patchOffset, patchSize, patchName)) {
if (ShowPrompt(true, "%s\nB button detected. Cancel?", patchName)) return displayError(IPS_CANCELED); if (ShowPrompt(true, "%s\n%s", patchName, STR_B_DETECTED_CANCEL)) return displayError(IPS_CANCELED);
ShowProgress(0, patchSize, patchName); ShowProgress(0, patchSize, patchName);
ShowProgress(patchOffset, patchSize, patchName); ShowProgress(patchOffset, patchSize, patchName);
} }
@ -211,7 +212,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
while (offset != 0x454F46) while (offset != 0x454F46)
{ {
if (!ShowProgress(offset, outSize, outName)) { if (!ShowProgress(offset, outSize, outName)) {
if (ShowPrompt(true, "%s\nB button detected. Cancel?", outName)) return displayError(IPS_CANCELED); if (ShowPrompt(true, "%s\n%s", outName, STR_B_DETECTED_CANCEL)) return displayError(IPS_CANCELED);
ShowProgress(0, outSize, outName); ShowProgress(0, outSize, outName);
ShowProgress(offset, outSize, outName); ShowProgress(offset, outSize, outName);
} }

View File

@ -55,20 +55,24 @@ u32 BuildTwlSaveHeader(void* sav, u32 size) {
u16 n_sct = 1; u16 n_sct = 1;
u16 sct_track = 1; u16 sct_track = 1;
u16 sct_heads = 1; u16 sct_heads = 1;
while (true) { u16 n_sct_next = 0;
if (sct_heads < sct_track) { while (n_sct_next <= n_sct_max) {
u16 n_sct_next = sct_track * (sct_heads+1) * (sct_heads+1); n_sct_next = sct_track * (sct_heads + 1) * (sct_heads + 1);
if (n_sct_next < n_sct_max) { if (n_sct_next <= n_sct_max) {
sct_heads++; sct_heads++;
n_sct = n_sct_next; n_sct = n_sct_next;
} else break;
} else { sct_track++;
u16 n_sct_next = (sct_track+1) * sct_heads * sct_heads; n_sct_next = sct_track * sct_heads * sct_heads;
if (n_sct_next < n_sct_max) { if (n_sct_next <= n_sct_max) {
n_sct = n_sct_next;
}
}
}
n_sct_next = (sct_track + 1) * sct_heads * sct_heads;
if (n_sct_next <= n_sct_max) {
sct_track++; sct_track++;
n_sct = n_sct_next; n_sct = n_sct_next;
} else break;
}
} }
// sectors per cluster (should be identical to Nintendo) // sectors per cluster (should be identical to Nintendo)

View File

@ -1,4 +1,5 @@
#include "common.h" #include "common.h"
#include "language.h"
#include "region.h" #include "region.h"
// Names of system regions, short form. // Names of system regions, short form.
@ -12,13 +13,16 @@ const char* const g_regionNamesShort[SMDH_NUM_REGIONS] = {
"TWN", "TWN",
}; };
// Names of system regions, long form. // Names of system regions, long form and translatable.
const char* const g_regionNamesLong[SMDH_NUM_REGIONS] = { const char* regionNameLong(int region) {
"Japan", switch(region) {
"Americas", case REGION_JPN: return STR_REGION_JAPAN;
"Europe", case REGION_USA: return STR_REGION_AMERICAS;
"Australia", case REGION_EUR: return STR_REGION_EUROPE;
"China", case REGION_AUS: return STR_REGION_AUSTRALIA;
"Korea", case REGION_CHN: return STR_REGION_CHINA;
"Taiwan", case REGION_KOR: return STR_REGION_KOREA;
case REGION_TWN: return STR_REGION_TAIWAN;
default: return STR_REGION_UNKNOWN;
}
}; };

View File

@ -27,5 +27,5 @@
// Names of system regions, short form. // Names of system regions, short form.
extern const char* const g_regionNamesShort[SMDH_NUM_REGIONS]; extern const char* const g_regionNamesShort[SMDH_NUM_REGIONS];
// Names of system regions, long form. // Names of system regions, long form and translatable.
extern const char* const g_regionNamesLong[SMDH_NUM_REGIONS]; const char* regionNameLong(int region);

View File

@ -120,7 +120,7 @@ u32 BuildVariableFakeTicket(Ticket** ticket, u32* ticket_size, const u8* title_i
} }
u32 BuildFakeTicket(Ticket* ticket, const u8* title_id) { u32 BuildFakeTicket(Ticket* ticket, const u8* title_id) {
Ticket* tik; Ticket* tik = NULL;
u32 ticket_size = sizeof(TicketCommon); u32 ticket_size = sizeof(TicketCommon);
u32 res = BuildVariableFakeTicket(&tik, &ticket_size, title_id, TICKET_MAX_CONTENTS); u32 res = BuildVariableFakeTicket(&tik, &ticket_size, title_id, TICKET_MAX_CONTENTS);
if (res != 0) return res; if (res != 0) return res;

View File

@ -27,7 +27,7 @@ void CTR_CmdReadData(u32 sector, u32 length, u32 blocks, void* buffer)
(u32)((sector << 9) & 0xFFFFFFFF), (u32)((sector << 9) & 0xFFFFFFFF),
0x00000000, 0x00000000 0x00000000, 0x00000000
}; };
CTR_SendCommand(read_cmd, length, blocks, 0x704822C, buffer); CTR_SendCommand(read_cmd, length, blocks, 0x104822C, buffer); // Clock divider 5 (13.4 MHz). Same as Process9.
} }
void CTR_CmdReadHeader(void* buffer) void CTR_CmdReadHeader(void* buffer)

View File

@ -64,6 +64,11 @@ u32 GetCartName(char* name, CartData* cdata) {
u32 GetCartInfoString(char* info, size_t info_size, CartData* cdata) { u32 GetCartInfoString(char* info, size_t info_size, CartData* cdata) {
size_t info_index = 0; size_t info_index = 0;
u8 padding;
// read the last byte of the cart storage, but ignore the result
ReadCartBytes(&padding, cdata->cart_size - 1, 1, cdata, false);
if (cdata->cart_type & CART_CTR) { if (cdata->cart_type & CART_CTR) {
CartDataCtr* cdata_i = (CartDataCtr*)cdata; CartDataCtr* cdata_i = (CartDataCtr*)cdata;
NcsdHeader* ncsd = &(cdata_i->ncsd); NcsdHeader* ncsd = &(cdata_i->ncsd);
@ -107,8 +112,10 @@ u32 GetCartInfoString(char* info, size_t info_size, CartData* cdata) {
"Save chip ID : <none>\n"); "Save chip ID : <none>\n");
info_index += snprintf(info + info_index, info_size - info_index, info_index += snprintf(info + info_index, info_size - info_index,
"Padding Byte : %02X\n"
"Timestamp : 20%02X-%02X-%02X %02X:%02X:%02X\n" "Timestamp : 20%02X-%02X-%02X %02X:%02X:%02X\n"
"GM9 Version : %s\n", "GM9 Version : %s\n",
padding,
init_time.bcd_Y, init_time.bcd_M, init_time.bcd_D, init_time.bcd_Y, init_time.bcd_M, init_time.bcd_D,
init_time.bcd_h, init_time.bcd_m, init_time.bcd_s, init_time.bcd_h, init_time.bcd_m, init_time.bcd_s,
VERSION); VERSION);
@ -247,7 +254,7 @@ u32 InitCartRead(CartData* cdata) {
if (nds_header->device_capacity >= 15) return 1; // too big, not valid if (nds_header->device_capacity >= 15) return 1; // too big, not valid
if (cdata->cart_size == 0) if (cdata->cart_size == 0)
cdata->cart_size = (128 * 1024) << nds_header->device_capacity; cdata->cart_size = (128 * 1024) << nds_header->device_capacity;
cdata->data_size = nds_header->ntr_rom_size + 0x88; cdata->data_size = nds_header->ntr_rom_size;
cdata->arm9i_rom_offset = 0; cdata->arm9i_rom_offset = 0;
// TWL header // TWL header
@ -266,6 +273,15 @@ u32 InitCartRead(CartData* cdata) {
NTR_CmdReadHeader(cdata->storage); NTR_CmdReadHeader(cdata->storage);
if (!NTR_Secure_Init(cdata->storage, NULL, Cart_GetID(), 1)) return 1; if (!NTR_Secure_Init(cdata->storage, NULL, Cart_GetID(), 1)) return 1;
} }
} else {
// Check if immediately after the reported cart size
// is the magic number string 'ac' (auth code).
// If found, add 0x88 bytes for the download play RSA key.
u16 rsaMagic;
ReadCartBytes(&rsaMagic, cdata->data_size, 2, cdata, false);
if(rsaMagic == 0x6361) {
cdata->data_size += 0x88;
}
} }
// store encrypted secure area // store encrypted secure area

File diff suppressed because it is too large Load Diff

188
arm9/source/language.c Normal file
View File

@ -0,0 +1,188 @@
#include "language.h"
#include "fsdrive.h"
#include "fsutil.h"
#include "support.h"
#include "ui.h"
#define STRING(what, def) const char* STR_##what = NULL;
#include "language.inl"
#undef STRING
static const char** translation_ptrs[] = {
#define STRING(what, def) &STR_##what,
#include "language.inl"
#undef STRING
};
static const char* translation_fallbacks[] = {
#define STRING(what, def) def,
#include "language.inl"
#undef STRING
};
static char* translation_data = NULL;
typedef struct {
char name[32];
char path[256];
} Language;
typedef struct {
char chunk_id[4]; // NOT null terminated
u32 size;
} RiffChunkHeader;
typedef struct {
u32 version;
u32 count;
char languageName[32];
} LanguageMeta;
STATIC_ASSERT(sizeof(LanguageMeta) == 40);
bool SetLanguage(const void* translation, u32 translation_size) {
u32 str_count;
const void* ptr = translation;
const RiffChunkHeader* riff_header;
const RiffChunkHeader* chunk_header;
// Free old translation data
if (translation_data) {
free(translation_data);
translation_data = NULL;
}
if ((ptr = GetLanguage(translation, translation_size, NULL, &str_count, NULL))) {
// load total size
riff_header = translation;
while ((u32)(ptr - translation) < riff_header->size + sizeof(RiffChunkHeader)) {
chunk_header = ptr;
if (memcmp(chunk_header->chunk_id, "SDAT", 4) == 0) { // string data
if (chunk_header->size > 0) {
translation_data = malloc(chunk_header->size);
if (!translation_data) goto fallback;
memcpy(translation_data, ptr + sizeof(RiffChunkHeader), chunk_header->size);
}
} else if (memcmp(chunk_header->chunk_id, "SMAP", 4) == 0) { // string map
// string data must come before the map
if (!translation_data && str_count > 0) goto fallback;
u16* string_map = (u16*)(ptr + sizeof(RiffChunkHeader));
// Load all the strings
for (u32 i = 0; i < countof(translation_ptrs); i++) {
if (i < str_count) {
*translation_ptrs[i] = (translation_data + string_map[i]);
} else {
*translation_ptrs[i] = translation_fallbacks[i];
}
}
}
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
}
return true;
}
fallback:
if (translation_data) {
free(translation_data);
translation_data = NULL;
}
for (u32 i = 0; i < countof(translation_ptrs); i++) {
*translation_ptrs[i] = translation_fallbacks[i];
}
return false;
}
const void* GetLanguage(const void* riff, const u32 riff_size, u32* version, u32* count, char* language_name) {
const void* ptr = riff;
const RiffChunkHeader* riff_header;
const RiffChunkHeader* chunk_header;
// check header magic and load size
if (!ptr) return NULL;
riff_header = ptr;
if (memcmp(riff_header->chunk_id, "RIFF", 4) != 0) return NULL;
// ensure enough space is allocated
if (riff_header->size > riff_size) return NULL;
ptr += sizeof(RiffChunkHeader);
while ((u32)(ptr - riff) < riff_header->size + sizeof(RiffChunkHeader)) {
chunk_header = ptr;
// check for and load META section
if (memcmp(chunk_header->chunk_id, "META", 4) == 0) {
if (chunk_header->size != sizeof(LanguageMeta)) return NULL;
const LanguageMeta *meta = ptr + sizeof(RiffChunkHeader);
if (meta->version != TRANSLATION_VER || meta->count > countof(translation_ptrs)) return NULL;
// all good
if (version) *version = meta->version;
if (count) *count = meta->count;
if (language_name) strcpy(language_name, meta->languageName);
return ptr;
}
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
}
return NULL;
}
int compLanguage(const void* e1, const void* e2) {
const Language* entry2 = (const Language*) e2;
const Language* entry1 = (const Language*) e1;
return strncasecmp(entry1->name, entry2->name, 32);
}
bool LanguageMenu(char* result, const char* title) {
DirStruct* langDir = (DirStruct*)malloc(sizeof(DirStruct));
if (!langDir) return false;
char path[256];
if (!GetSupportDir(path, LANGUAGES_DIR)) return false;
GetDirContents(langDir, path);
char* header = (char*)malloc(0x2C0);
Language* langs = (Language*)malloc(langDir->n_entries * sizeof(Language));
int langCount = 0;
// Find all valid files and get their language names
for (u32 i = 0; i < langDir->n_entries; i++) {
if (langDir->entry[i].type == T_FILE) {
size_t fsize = FileGetSize(langDir->entry[i].path);
FileGetData(langDir->entry[i].path, header, 0x2C0, 0);
if (GetLanguage(header, fsize, NULL, NULL, langs[langCount].name)) {
memcpy(langs[langCount].path, langDir->entry[i].path, 256);
langCount++;
}
}
}
free(langDir);
free(header);
qsort(langs, langCount, sizeof(Language), compLanguage);
// Make an array of just the names for the select promt
const char* langNames[langCount];
for (int i = 0; i < langCount; i++) {
langNames[i] = langs[i].name;
}
u32 selected = ShowSelectPrompt(langCount, langNames, "%s", title);
if (selected > 0 && result) {
memcpy(result, langs[selected - 1].path, 256);
}
return selected > 0;
}

12
arm9/source/language.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "common.h"
#define STRING(what, def) extern const char* STR_##what;
#include "language.inl"
#undef STRING
bool SetLanguage(const void* translation, u32 translation_size);
const void* GetLanguage(const void* riff, u32 riff_size, u32* version, u32* count, char* language_name);
bool LanguageMenu(char* result, const char* title);

14
arm9/source/lua/README.md Normal file
View File

@ -0,0 +1,14 @@
This is Lua 5.4.7 with a few modifications:
* Patches made to silence warnings: https://github.com/ihaveamac/GodMode9/commit/9905b939b26aae3422c906c7858d8852764fa279
* lua.c, luac.c, lua.hpp removed (not useful in GodMode9)
* liolib.c, loslib.c removed (replaced with custom implementations)
## License of Lua 5.4.7
Copyright © 19942024 Lua.org, PUC-Rio.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,800 @@
#ifndef NO_LUA
#include "gm9internalfs.h"
#include "fs.h"
#include "ui.h"
#include "utils.h"
#include "sha.h"
#include "nand.h"
#include "language.h"
#include "hid.h"
#include "game.h"
#include "gamecart.h"
#define _MAX_FOR_DEPTH 16
static u8 no_data_hash_256[32] = { SHA256_EMPTY_HASH };
static u8 no_data_hash_1[32] = { SHA1_EMPTY_HASH };
static bool PathIsDirectory(const char* path) {
FRESULT res;
FILINFO fno;
res = fvx_stat(path, &fno);
if (res != FR_OK) return false;
return fno.fattrib & AM_DIR;
}
static void CreateStatTable(lua_State* L, FILINFO* fno) {
lua_createtable(L, 0, 4); // create nested table
lua_pushstring(L, fno->fname);
lua_setfield(L, -2, "name");
lua_pushstring(L, (fno->fattrib & AM_DIR) ? "dir" : "file");
lua_setfield(L, -2, "type");
lua_pushinteger(L, fno->fsize);
lua_setfield(L, -2, "size");
lua_pushboolean(L, fno->fattrib & AM_RDO);
lua_setfield(L, -2, "read_only");
// ... and leave this table on the stack for the caller to deal with
}
static int internalfs_move(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.move");
const char* path_src = luaL_checkstring(L, 1);
const char* path_dst = luaL_checkstring(L, 2);
FILINFO fno;
CheckWritePermissionsLuaError(L, path_src);
CheckWritePermissionsLuaError(L, path_dst);
u32 flags = BUILD_PATH;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, NO_CANCEL | SILENT | OVERWRITE_ALL | SKIP_ALL);
}
if (!(flags & OVERWRITE_ALL) && (fvx_stat(path_dst, &fno) == FR_OK)) {
return luaL_error(L, "destination already exists on %s -> %s and {overwrite=true} was not used", path_src, path_dst);
}
if (!(PathMoveCopy(path_dst, path_src, &flags, true))) {
return luaL_error(L, "PathMoveCopy failed on %s -> %s", path_src, path_dst);
}
return 0;
}
static int internalfs_remove(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.remove");
const char* path = luaL_checkstring(L, 1);
CheckWritePermissionsLuaError(L, path);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 2, flags, RECURSIVE);
}
if (!(flags & RECURSIVE)) {
if (PathIsDirectory(path)) {
return luaL_error(L, "requested directory remove without {recursive=true} on %s", path);
}
}
if (!(PathDelete(path))) {
return luaL_error(L, "PathDelete failed on %s", path);
}
return 0;
}
static int internalfs_copy(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.copy");
const char* path_src = luaL_checkstring(L, 1);
const char* path_dst = luaL_checkstring(L, 2);
FILINFO fno;
CheckWritePermissionsLuaError(L, path_dst);
u32 flags = BUILD_PATH;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, CALC_SHA | USE_SHA1 | NO_CANCEL | SILENT | OVERWRITE_ALL | SKIP_ALL | APPEND_ALL | RECURSIVE);
}
if (!(flags & RECURSIVE)) {
if (PathIsDirectory(path_src)) {
return luaL_error(L, "requested directory copy without {recursive=true} on %s -> %s", path_src, path_dst);
}
}
if (!(flags & OVERWRITE_ALL) && (fvx_stat(path_dst, &fno) == FR_OK)) {
return luaL_error(L, "destination already exists on %s -> %s and {overwrite=true} was not used", path_src, path_dst);
}
if (!(PathMoveCopy(path_dst, path_src, &flags, false))) {
return luaL_error(L, "PathMoveCopy failed on %s -> %s", path_src, path_dst);
}
return 0;
}
static int internalfs_mkdir(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.mkdir");
const char* path = luaL_checkstring(L, 1);
CheckWritePermissionsLuaError(L, path);
FRESULT res = fvx_rmkdir(path);
if (res != FR_OK) {
return luaL_error(L, "could not mkdir %s (%d)", path, res);
}
return 0;
}
static int internalfs_list_dir(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.list_dir");
const char* path = luaL_checkstring(L, 1);
lua_newtable(L);
DIR dir;
FILINFO fno;
FRESULT res = fvx_opendir(&dir, path);
if (res != FR_OK) {
lua_pop(L, 1); // remove final table from stack
return luaL_error(L, "could not opendir %s (%d)", path, res);
}
for (int i = 1; true; i++) {
res = fvx_readdir(&dir, &fno);
if (res != FR_OK) {
fvx_closedir(&dir);
lua_pop(L, 1); // remove final table from stack
return luaL_error(L, "could not readdir %s (%d)", path, res);
}
if (fno.fname[0] == 0) break;
CreateStatTable(L, &fno);
lua_seti(L, -2, i); // add nested table to final table
}
fvx_closedir(&dir);
return 1;
}
static int internalfs_stat(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.stat");
const char* path = luaL_checkstring(L, 1);
FILINFO fno;
FRESULT res = fvx_stat(path, &fno);
if (res != FR_OK) {
return luaL_error(L, "could not stat %s (%d)", path, res);
}
CreateStatTable(L, &fno);
return 1;
}
// TODO: make this manually check for permissions recursively
static int internalfs_fix_cmacs(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.fix_cmacs");
const char* path = luaL_checkstring(L, 1);
ShowString("%s", STR_FIXING_CMACS_PLEASE_WAIT);
if (RecursiveFixFileCmac(path) != 0) {
return luaL_error(L, "fixcmac failed");
}
return 0;
}
static int internalfs_stat_fs(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.stat_fs");
const char* path = luaL_checkstring(L, 1);
u64 freespace = GetFreeSpace(path);
u64 totalspace = GetTotalSpace(path);
u64 usedspace = totalspace - freespace;
lua_createtable(L, 0, 3);
lua_pushinteger(L, freespace);
lua_setfield(L, -2, "free");
lua_pushinteger(L, totalspace);
lua_setfield(L, -2, "total");
lua_pushinteger(L, usedspace);
lua_setfield(L, -2, "used");
return 1;
}
static int internalfs_dir_info(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.dir_info");
const char* path = luaL_checkstring(L, 1);
u64 tsize = 0;
u32 tdirs = 0;
u32 tfiles = 0;
if (!DirInfo(path, &tsize, &tdirs, &tfiles)) {
return luaL_error(L, "error when running DirInfo");
}
lua_createtable(L, 0, 3);
lua_pushinteger(L, tsize);
lua_setfield(L, -2, "size");
lua_pushinteger(L, tdirs);
lua_setfield(L, -2, "dirs");
lua_pushinteger(L, tfiles);
lua_setfield(L, -2, "files");
return 1;
}
static int FileDirSelector(lua_State* L, const char* path_orig, const char* prompt, bool is_dir, bool include_dirs, bool explorer) {
bool ret;
char path[_VAR_CNT_LEN] = { 0 };
char choice[_VAR_CNT_LEN] = { 0 };
strncpy(path, path_orig, _VAR_CNT_LEN);
if (strncmp(path, "Z:", 2) == 0) {
return luaL_error(L, "forbidden drive");
} else if (!is_dir) {
u32 flags_ext = include_dirs ? 0 : NO_DIRS;
char *npattern = strrchr(path, '/');
if (!npattern) {
return luaL_error(L, "invalid path");
}
*(npattern++) = '\0';
ret = FileSelector(choice, prompt, path, npattern, flags_ext, explorer);
} else {
ret = FileSelector(choice, prompt, path, NULL, NO_FILES | SELECT_DIRS, explorer);
}
if (ret) {
lua_pushstring(L, choice);
} else {
lua_pushnil(L);
}
return 1;
}
static int internalfs_ask_select_file(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.ask_select_file");
const char* prompt = luaL_checkstring(L, 1);
const char* path = luaL_checkstring(L, 2);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, INCLUDE_DIRS | EXPLORER);
};
return FileDirSelector(L, path, prompt, false, (flags & INCLUDE_DIRS), (flags & EXPLORER));
}
static int internalfs_ask_select_dir(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.ask_select_dir");
const char* prompt = luaL_checkstring(L, 1);
const char* path = luaL_checkstring(L, 2);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, EXPLORER);
};
return FileDirSelector(L, path, prompt, true, true, (flags & EXPLORER));
}
static int internalfs_find(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.find");
const char* pattern = luaL_checkstring(L, 1);
char path[_VAR_CNT_LEN] = { 0 };
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 2, flags, FIND_FIRST);
}
u8 mode = (flags & FIND_FIRST) ? FN_LOWEST : FN_HIGHEST;
FRESULT res = fvx_findpath(path, pattern, mode);
if (res == FR_NO_PATH) {
lua_pushnil(L);
} else if (res != FR_OK) {
return luaL_error(L, "failed to find %s (%d)", pattern, res);
} else {
lua_pushstring(L, path);
}
return 1;
}
// this should probably be rewritten
static int internalfs_find_all(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.find_all");
const char* dir = luaL_checkstring(L, 1);
const char* pattern = luaL_checkstring(L, 2);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, RECURSIVE);
}
char forpath[_VAR_CNT_LEN] = { 0 };
// without re-implementing for_handler, i need to give it a "*" pattern
// and then manually compare each filename to see if it matches
// so that a recursive search actually works
bool forstatus = for_handler(NULL, dir, "*", flags & RECURSIVE);
if (!forstatus) {
return luaL_error(L, "could not open directory");
}
lua_newtable(L);
int i = 1;
char* slash;
while (true) {
forstatus = for_handler(forpath, NULL, NULL, false);
if (!forstatus) {
forpath[0] = '\0';
}
if (!forpath[0]) {
// finish for_handler
for_handler(NULL, NULL, NULL, false);
break;
} else {
slash = strrchr(forpath, '/');
if (!slash) bkpt; // this should never, ever happen
if (fvx_match_name(slash+1, pattern) == FR_OK) {
lua_pushstring(L, forpath);
lua_seti(L, -2, i++);
}
}
}
return 1;
}
static int internalfs_find_not(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.find_not");
const char* pattern = luaL_checkstring(L, 1);
char path[_VAR_CNT_LEN] = { 0 };
FRESULT res = fvx_findnopath(path, pattern);
if (res != FR_OK) {
return luaL_error(L, "failed to find %s (%d)", pattern, res);
}
lua_pushstring(L, path);
return 1;
}
static int internalfs_exists(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.exists");
const char* path = luaL_checkstring(L, 1);
FILINFO fno;
lua_pushboolean(L, (fvx_stat(path, &fno) == FR_OK));
return 1;
}
static int internalfs_is_dir(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.is_dir");
const char* path = luaL_checkstring(L, 1);
lua_pushboolean(L, PathIsDirectory(path));
return 1;
}
static int internalfs_is_file(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.is_file");
const char* path = luaL_checkstring(L, 1);
FILINFO fno;
FRESULT res = fvx_stat(path, &fno);
if (res != FR_OK) {
lua_pushboolean(L, false);
} else {
lua_pushboolean(L, !(fno.fattrib & AM_DIR));
}
return 1;
}
static int internalfs_read_file(lua_State* L) {
CheckLuaArgCount(L, 3, "_fs.read_file");
const char* path = luaL_checkstring(L, 1);
lua_Integer offset = luaL_checkinteger(L, 2);
lua_Integer size = luaL_checkinteger(L, 3);
char *buf = malloc(size);
if (!buf) {
return luaL_error(L, "could not allocate memory to read file");
}
UINT bytes_read = 0;
FRESULT res = fvx_qread(path, buf, offset, size, &bytes_read);
if (res != FR_OK) {
free(buf);
return luaL_error(L, "could not read %s (%d)", path, res);
}
lua_pushlstring(L, buf, bytes_read);
free(buf);
return 1;
}
static int internalfs_write_file(lua_State* L) {
CheckLuaArgCount(L, 3, "_fs.write_file");
const char* path = luaL_checkstring(L, 1);
lua_Integer offset = luaL_checkinteger(L, 2);
size_t data_length = 0;
const char* data = luaL_checklstring(L, 3, &data_length);
CheckWritePermissionsLuaError(L, path);
UINT bytes_written = 0;
FRESULT res = fvx_qwrite(path, data, offset, data_length, &bytes_written);
if (res != FR_OK) {
return luaL_error(L, "error writing %s (%d)", path, res);
}
lua_pushinteger(L, bytes_written);
return 1;
}
static int internalfs_fill_file(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 4, "_fs.fill_file");
const char* path = luaL_checkstring(L, 1);
lua_Integer offset = luaL_checkinteger(L, 2);
lua_Integer size = luaL_checkinteger(L, 3);
lua_Integer byte = luaL_checkinteger(L, 4);
u8 real_byte = byte & 0xFF;
u32 flags = ALLOW_EXPAND;
if (extra) {
flags = GetFlagsFromTable(L, 4, flags, NO_CANCEL);
};
if ((byte < 0) || (byte > 0xFF)) {
return luaL_error(L, "byte is not between 0x00 and 0xFF (got: %I)", byte);
}
CheckWritePermissionsLuaError(L, path);
if (!(FileSetByte(path, offset, size, real_byte, &flags))) {
return luaL_error(L, "FileSetByte failed on %s", path);
}
return 0;
}
static int internalfs_make_dummy_file(lua_State* L) {
CheckLuaArgCount(L, 2, "_fs.make_dummy_file");
const char* path = luaL_checkstring(L, 1);
lua_Integer size = luaL_checkinteger(L, 2);
CheckWritePermissionsLuaError(L, path);
if (!(FileCreateDummy(path, NULL, size))) {
return luaL_error(L, "FileCreateDummy failed on %s", path);
}
return 0;
}
static int internalfs_truncate(lua_State* L) {
CheckLuaArgCount(L, 2, "_fs.truncate");
const char* path = luaL_checkstring(L, 1);
lua_Integer size = luaL_checkinteger(L, 2);
FIL fp;
FRESULT res;
CheckWritePermissionsLuaError(L, path);
res = f_open(&fp, path, FA_READ | FA_WRITE);
if (res != FR_OK) {
return luaL_error(L, "failed to open %s (note: this only works on FAT filesystems, not virtual)", path);
}
res = f_lseek(&fp, size);
if (res != FR_OK) {
f_close(&fp);
return luaL_error(L, "failed to seek on %s", path);
}
res = f_truncate(&fp);
if (res != FR_OK) {
f_close(&fp);
return luaL_error(L, "failed to truncate %s", path);
}
f_close(&fp);
return 0;
}
static int internalfs_img_mount(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.img_mount");
const char* path = luaL_checkstring(L, 1);
bool res = InitImgFS(path);
if (!res) {
return luaL_error(L, "failed to mount %s", path);
}
return 0;
}
static int internalfs_img_umount(lua_State* L) {
CheckLuaArgCount(L, 0, "_fs.img_umount");
InitImgFS(NULL);
return 0;
}
static int internalfs_get_img_mount(lua_State* L) {
CheckLuaArgCount(L, 0, "_fs.get_img_mount");
char path[256] = { 0 };
strncpy(path, GetMountPath(), 256);
if (path[0] == 0) {
// since lua treats "" as true, return a nil to make if/else easier
lua_pushnil(L);
} else {
lua_pushstring(L, path);
}
return 1;
}
// TODO: what if someone does offset != 0 but size = 0 (end of file)?
static int internalfs_hash_file(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 3, "_fs.hash_file");
const char* path = luaL_checkstring(L, 1);
lua_Integer offset = luaL_checkinteger(L, 2);
lua_Integer size = luaL_checkinteger(L, 3);
FRESULT res;
FILINFO fno;
if (size == 0) {
res = fvx_stat(path, &fno);
if (res != FR_OK) {
return luaL_error(L, "failed to stat %s", path);
}
size = fno.fsize;
}
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 4, flags, USE_SHA1);
}
const u8 hashlen = (flags & USE_SHA1) ? 20 : 32;
u8 hash_fil[0x20];
if (size == 0) {
// shortcut by just returning the hash of empty data
memcpy(hash_fil, (flags & USE_SHA1) ? no_data_hash_1 : no_data_hash_256, hashlen);
} else if (!(FileGetSha(path, hash_fil, offset, size, (flags & USE_SHA1)))) {
return luaL_error(L, "FileGetSha failed on %s", path);
}
lua_pushlstring(L, (char*)hash_fil, hashlen);
return 1;
}
static int internalfs_hash_data(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.hash_data");
size_t data_length = 0;
const char* data = luaL_checklstring(L, 1, &data_length);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 2, flags, USE_SHA1);
}
const u8 hashlen = (flags & USE_SHA1) ? 20 : 32;
u8 hash_fil[0x20];
if (data_length == 0) {
// shortcut by just returning the hash of empty data
memcpy(hash_fil, (flags & USE_SHA1) ? no_data_hash_1 : no_data_hash_256, hashlen);
} else {
sha_quick(hash_fil, data, data_length, (flags & USE_SHA1) ? SHA1_MODE : SHA256_MODE);
}
lua_pushlstring(L, (char*)hash_fil, hashlen);
return 1;
}
static int internalfs_allow(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.allow");
const char* path = luaL_checkstring(L, 1);
u32 flags = 0;
bool allowed;
if (extra) {
flags = GetFlagsFromTable(L, 2, 0, ASK_ALL);
}
if (flags & ASK_ALL) {
allowed = CheckDirWritePermissions(path);
} else {
allowed = CheckWritePermissions(path);
}
lua_pushboolean(L, allowed);
return 1;
};
static int internalfs_verify(lua_State* L) {
CheckLuaArgCount(L, 1, "_fs.verify");
const char* path = luaL_checkstring(L, 1);
bool res;
u64 filetype = IdentifyFileType(path);
if (filetype & IMG_NAND) res = (ValidateNandDump(path) == 0);
else res = (VerifyGameFile(path) == 0);
lua_pushboolean(L, res);
return 1;
}
static int internalfs_sd_is_mounted(lua_State* L) {
CheckLuaArgCount(L, 0, "_fs.sd_is_mounted");
lua_pushboolean(L, CheckSDMountState());
return 1;
}
static int internalfs_sd_switch(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 0, "_fs.sd_switch");
const char* message;
if (extra) {
message = luaL_checkstring(L, 1);
} else {
message = "Please switch the SD card now.";
}
bool ret;
DeinitExtFS();
if (!(ret = CheckSDMountState())) {
return luaL_error(L, "%s", STR_SCRIPTERR_SD_NOT_MOUNTED);
}
u32 pad_state;
DeinitSDCardFS();
ShowString("%s\n \n%s", message, STR_EJECT_SD_CARD);
while (!((pad_state = InputWait(0)) & (BUTTON_B|SD_EJECT)));
if (pad_state & SD_EJECT) {
ShowString("%s\n \n%s", message, STR_INSERT_SD_CARD);
while (!((pad_state = InputWait(0)) & (BUTTON_B|SD_INSERT)));
}
if (pad_state & BUTTON_B) {
return luaL_error(L, "user canceled");
}
InitSDCardFS();
AutoEmuNandBase(true);
InitExtFS();
return 0;
}
static int internalfs_key_dump(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.key_dump");
const char* opts[] = {SEEDINFO_NAME, TIKDB_NAME_ENC, TIKDB_NAME_DEC, NULL};
int opt = luaL_checkoption(L, 1, NULL, opts);
bool ret = false;
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 2, 0, OVERWRITE_ALL);
}
if (opt == 1 || opt == 2) {
bool tik_dec = opt == 2;
if (flags & OVERWRITE_ALL) fvx_unlink(tik_dec ? OUTPUT_PATH "/" TIKDB_NAME_DEC : OUTPUT_PATH "/" TIKDB_NAME_ENC);
if (BuildTitleKeyInfo(NULL, tik_dec, false) == 0) {
ShowString(STR_BUILDING_TO_OUT_ARG, OUTPUT_PATH, opts[opt]);
if (((BuildTitleKeyInfo("1:/dbs/ticket.db", tik_dec, false) == 0) ||
(BuildTitleKeyInfo("4:/dbs/ticket.db", tik_dec, false) == 0)) &&
(BuildTitleKeyInfo(NULL, tik_dec, true) == 0))
ret = true;
}
} else if (opt == 0) {
if (flags & OVERWRITE_ALL) fvx_unlink(OUTPUT_PATH "/" SEEDINFO_NAME);
if (BuildSeedInfo(NULL, false) == 0) {
ShowString(STR_BUILDING_TO_OUT_ARG, OUTPUT_PATH, opts[opt]);
if (((BuildSeedInfo("1:", false) == 0) ||
(BuildSeedInfo("4:", false) == 0)) &&
(BuildSeedInfo(NULL, true) == 0))
ret = true;
}
}
if (!ret) {
return luaL_error(L, "building %s failed", opts[opt]);
}
return 0;
}
static int internalfs_cart_dump(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.cart_dump");
const char* path = luaL_checkstring(L, 1);
u64 fsize = (u64)luaL_checkinteger(L, 2);
bool ret = false;
const char* errstr = "";
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 3, flags, ENCRYPTED);
}
CartData* cdata = (CartData*) malloc(sizeof(CartData));
u8* buf = (u8*) malloc(STD_BUFFER_SIZE);
ret = false;
if (!cdata || !buf) {
errstr = "out of memory";
} else if (InitCartRead(cdata) != 0){
errstr = "cart init fail";
} else {
SetSecureAreaEncryption(flags & ENCRYPTED);
fvx_unlink(path);
ret = true;
errstr = "cart dump failed or canceled";
for (u64 p = 0; p < fsize; p += STD_BUFFER_SIZE) {
u64 len = min((fsize - p), STD_BUFFER_SIZE);
ShowProgress(p, fsize, path);
if (!ShowProgress(p, fsize, path) ||
(ReadCartBytes(buf, p, len, cdata, false) != 0) ||
(fvx_qwrite(path, buf, p, len, NULL) != FR_OK)) {
ret = false;
break;
}
}
}
free(buf);
free(cdata);
if (!ret) {
return luaL_error(L, "%s", errstr);
}
return 0;
}
static const luaL_Reg internalfs_lib[] = {
{"move", internalfs_move},
{"remove", internalfs_remove},
{"copy", internalfs_copy},
{"mkdir", internalfs_mkdir},
{"list_dir", internalfs_list_dir},
{"stat", internalfs_stat},
{"stat_fs", internalfs_stat_fs},
{"dir_info", internalfs_dir_info},
{"ask_select_file", internalfs_ask_select_file},
{"ask_select_dir", internalfs_ask_select_dir},
{"find", internalfs_find},
{"find_all", internalfs_find_all},
{"find_not", internalfs_find_not},
{"exists", internalfs_exists},
{"is_dir", internalfs_is_dir},
{"is_file", internalfs_is_file},
{"read_file", internalfs_read_file},
{"write_file", internalfs_write_file},
{"fill_file", internalfs_fill_file},
{"make_dummy_file", internalfs_make_dummy_file},
{"truncate", internalfs_truncate},
{"img_mount", internalfs_img_mount},
{"img_umount", internalfs_img_umount},
{"get_img_mount", internalfs_get_img_mount},
{"hash_file", internalfs_hash_file},
{"hash_data", internalfs_hash_data},
{"verify", internalfs_verify},
{"allow", internalfs_allow},
{"sd_is_mounted", internalfs_sd_is_mounted},
{"sd_switch", internalfs_sd_switch},
{"fix_cmacs", internalfs_fix_cmacs},
{"key_dump", internalfs_key_dump},
{"cart_dump", internalfs_cart_dump},
{NULL, NULL}
};
int gm9lua_open_internalfs(lua_State* L) {
luaL_newlib(L, internalfs_lib);
return 1;
}
#endif

View File

@ -0,0 +1,23 @@
#pragma once
#include "gm9lua.h"
#define GM9LUA_INTERNALFSLIBNAME "_fs"
#define SHA256_EMPTY_HASH \
0xE3, 0xB0, 0xC4, 0x42, \
0x98, 0xFC, 0x1C, 0x14, \
0x9A, 0xFB, 0xF4, 0xC8, \
0x99, 0x6F, 0xB9, 0x24, \
0x27, 0xAE, 0x41, 0xE4, \
0x64, 0x9B, 0x93, 0x4C, \
0xA4, 0x95, 0x99, 0x1B, \
0x78, 0x52, 0xB8, 0x55
#define SHA1_EMPTY_HASH \
0xDA, 0x39, 0xA3, 0xEE, \
0x5E, 0x6B, 0x4B, 0x0D, \
0x32, 0x55, 0xBF, 0xEF, \
0x95, 0x60, 0x18, 0x90, \
0xAF, 0xD8, 0x07, 0x09
int gm9lua_open_internalfs(lua_State* L);

View File

@ -0,0 +1,181 @@
#ifndef NO_LUA
#include "gm9internalsys.h"
#include "bootfirm.h"
#include "fs.h"
#include "pxi.h"
#include "game.h"
#include "power.h"
#include "sha.h"
#include "nand.h"
#include "utils.h"
#include "ui.h"
#include "rtc.h"
#include "godmode.h"
#define UNUSED(x) ((void)(x))
static int internalsys_boot(lua_State* L) {
CheckLuaArgCount(L, 1, "_sys.boot");
const char* path = lua_tostring(L, 1);
u8* firm = (u8*) malloc(FIRM_MAX_SIZE);
if (!firm) {
return luaL_error(L, "out of memory");
}
size_t firm_size = FileGetData(path, firm, FIRM_MAX_SIZE, 0);
if (!(firm_size && IsBootableFirm(firm, firm_size))) {
return luaL_error(L, "not a bootable firm");
}
char fixpath[256] = { 0 };
if ((*path == '0') || (*path == '1'))
snprintf(fixpath, sizeof(fixpath), "%s%s", (*path == '0') ? "sdmc" : "nand", path + 1);
else strncpy(fixpath, path, 256);
fixpath[255] = '\0';
DeinitExtFS();
DeinitSDCardFS();
PXI_DoCMD(PXICMD_LEGACY_BOOT, NULL, 0);
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
BootFirm((FirmHeader*)(void*)firm, fixpath);
while(1);
}
static int internalsys_reboot(lua_State* L) {
CheckLuaArgCount(L, 0, "_sys.reboot");
DeinitExtFS();
DeinitSDCardFS();
Reboot();
return 0;
}
static int internalsys_power_off(lua_State* L) {
CheckLuaArgCount(L, 0, "_sys.power_off");
DeinitExtFS();
DeinitSDCardFS();
PowerOff();
return 0;
}
static int internalsys_get_id0(lua_State* L) {
CheckLuaArgCount(L, 1, "_sys.get_id0");
const char* path = lua_tostring(L, 1);
char env_id0[32+1];
u8 sd_keyy[0x10] __attribute__((aligned(4)));
if (FileGetData(path, sd_keyy, 0x10, 0x110) == 0x10) {
u32 sha256sum[8];
sha_quick(sha256sum, sd_keyy, 0x10, SHA256_MODE);
snprintf(env_id0, sizeof(env_id0), "%08lx%08lx%08lx%08lx",
sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
lua_pushstring(L, env_id0);
} else {
lua_pushnil(L);
}
return 1;
}
static int internalsys_next_emu(lua_State* L) {
CheckLuaArgCount(L, 0, "_sys.next_emu");
DismountDriveType(DRV_EMUNAND);
AutoEmuNandBase(false);
InitExtFS();
return 0;
}
static int internalsys_get_emu_base(lua_State* L) {
CheckLuaArgCount(L, 0, "_sys.get_emu_base");
lua_pushinteger(L, GetEmuNandBase());
return 1;
}
static int internalsys_check_embedded_backup(lua_State* L) {
CheckLuaArgCount(L, 0, "_sys.check_embedded_backup");
if (PathExist("S:/essential.exefs")) {
lua_pushboolean(L, true);
return 1;
}
bool ncsd_check = CheckGenuineNandNcsd();
if (!ncsd_check) {
lua_pushnil(L);
return 1;
}
bool ret = false;
if (ncsd_check && ShowPrompt(true, "%s", STR_ESSENTIAL_BACKUP_NOT_FOUND_CREATE_NOW)) {
if (EmbedEssentialBackup("S:/nand.bin") == 0) {
u32 flags = BUILD_PATH | SKIP_ALL;
PathCopy(OUTPUT_PATH, "S:/essential.exefs", &flags);
ShowPrompt(false, STR_BACKUP_EMBEDDED_WRITTEN_TO_OUT, OUTPUT_PATH);
ret = true;
} else {
ret = false;
}
} else {
ret = false;
}
lua_pushboolean(L, ret);
return 1;
}
static int internalsys_check_raw_rtc(lua_State* L) {
CheckLuaArgCount(L, 0, "_sys.check_raw_rtc");
bool result = false;
DsTime dstime;
get_dstime(&dstime);
if (DSTIMEGET(&dstime, bcd_Y) >= 18) {
result = true;
} else if (ShowPrompt(true, "%s", STR_RTC_DATE_TIME_SEEMS_TO_BE_WRONG_SET_NOW) &&
ShowRtcSetterPrompt(&dstime, "%s", STR_TITLE_SET_RTC_DATE_TIME)) {
//char timestr[UTF_BUFFER_BYTESIZE(32)];
set_dstime(&dstime);
// this is only in godmode.h
//GetTimeString(timestr, true, true);
// ShowPrompt(false, STR_NEW_RTC_DATE_TIME_IS_TIME, timestr);
result = true;
}
lua_pushboolean(L, result);
return 1;
}
static int internalsys_global_bkpt(lua_State* L) {
UNUSED(L);
bkpt;
while(1);
}
static const luaL_Reg internalsys_lib[] = {
{"boot", internalsys_boot},
{"reboot", internalsys_reboot},
{"power_off", internalsys_power_off},
{"get_id0", internalsys_get_id0},
{"next_emu", internalsys_next_emu},
{"get_emu_base", internalsys_get_emu_base},
{"check_embedded_backup", internalsys_check_embedded_backup},
{"check_raw_rtc", internalsys_check_raw_rtc},
{NULL, NULL}
};
static const luaL_Reg internalsys_global_lib[] = {
{"bkpt", internalsys_global_bkpt},
{NULL, NULL}
};
int gm9lua_open_internalsys(lua_State* L) {
luaL_newlib(L, internalsys_lib);
lua_pushglobaltable(L); // push global table to stack
luaL_setfuncs(L, internalsys_global_lib, 0); // set global funcs
lua_pop(L, 1); // pop global table from stack
return 1;
}
#endif

View File

@ -0,0 +1,6 @@
#pragma once
#include "gm9lua.h"
#define GM9LUA_INTERNALSYSLIBNAME "_sys"
int gm9lua_open_internalsys(lua_State* L);

128
arm9/source/lua/gm9loader.c Normal file
View File

@ -0,0 +1,128 @@
#ifndef NO_LUA
#include "gm9loader.h"
#include "gm9lua.h"
#include "vff.h"
#include "ui.h"
// a lot of this code is based on stuff in loadlib.c but adapted for GM9
// similar to readable
static int Readable(const char* filename) {
FIL f;
FRESULT res = fvx_open(&f, filename, FA_READ | FA_OPEN_EXISTING);
if (res != FR_OK) return 0;
fvx_close(&f);
return 1;
}
// similar to getnextfilename
static const char* GetNextFileName(char** path, char* end) {
char *sep;
char *name = *path;
if (name == end)
return NULL; /* no more names */
else if (*name == '\0') { /* from previous iteration? */
*name = *LUA_PATH_SEP; /* restore separator */
name++; /* skip it */
}
sep = strchr(name, *LUA_PATH_SEP); /* find next separator */
if (sep == NULL) /* separator not found? */
sep = end; /* name goes until the end */
*sep = '\0'; /* finish file name */
*path = sep; /* will start next search from here */
return name;
}
// similar to pusherrornotfound
static void PushErrorNotFound(lua_State* L, const char* path) {
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addstring(&b, "no file '");
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
luaL_addstring(&b, "'");
luaL_pushresult(&b);
}
// similar to searchpath
static const char* SearchPath(lua_State* L, const char* name, const char* path, const char* sep) {
luaL_Buffer buff;
char* pathname;
char* endpathname;
const char* filename;
if (*sep != '\0' && strchr(name, *sep) != NULL)
name = luaL_gsub(L, name, sep, "/");
luaL_buffinit(L, &buff);
// add path to the buffer, replacing marks ('?') with the file name
luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
luaL_addchar(&buff, '\0');
pathname = luaL_buffaddr(&buff);
endpathname = pathname + luaL_bufflen(&buff) + 1;
while ((filename = GetNextFileName(&pathname, endpathname)) != NULL) {
if (Readable(filename))
return lua_pushstring(L, filename);
}
luaL_pushresult(&buff);
PushErrorNotFound(L, lua_tostring(L, -1));
return NULL;
}
// similar to findfile
static const char* FindLuaFile(lua_State* L, const char* name, const char* pname) {
const char* path;
lua_getfield(L, lua_upvalueindex(1), pname); // gets 'package' table
path = lua_tostring(L, -1);
if (path == NULL) luaL_error(L, "'package.%s' must be a string", pname);
return SearchPath(L, name, path, ".");
}
// similar to checkload
static int CheckLoad(lua_State* L, int stat, const char* filename) {
if (stat) {
lua_pushstring(L, filename);
return 2; // return open function and filename
} else {
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1));
}
}
// similar to searcher_Lua
static int PackageSearcher(lua_State* L) {
const char *filename;
const char *name = luaL_checkstring(L, 1);
filename = FindLuaFile(L, name, "path");
if (filename == NULL) return 1; // module not found in this path
return CheckLoad(L, (LoadLuaFile(L, filename) == LUA_OK), filename);
}
void ResetPackageSearchersAndPath(lua_State* L) {
// get package module
lua_getglobal(L, "package");
// the default package.path only makes sense on a full OS
// maybe this should include the lua script's current directory somehow...
lua_pushliteral(L, GM9LUA_DEFAULT_PATH);
lua_setfield(L, -2, "path");
// package.cpath is for loading binary modules, useless on GM9
lua_pushliteral(L, "");
lua_setfield(L, -2, "cpath");
// the default package searchers only make sense on a full OS
// so here we replace the lua loader with a custom one, and remove the C/Croot loaders
// leaving the initial one (preload)
lua_getfield(L, -1, "searchers");
lua_pushvalue(L, -2); // copy 'package' to the top of the stack, to set 'package' as upvalue for all searchers
lua_pushcclosure(L, PackageSearcher, 1); // push PackageSearcher with one upvalue being the "packages" module/table
lua_rawseti(L, -2, 2); // replace default lua loader
lua_pushnil(L);
lua_rawseti(L, -2, 3); // remove C loader
lua_pushnil(L);
lua_rawseti(L, -2, 4); // remove C root loader
lua_pop(L, 1); // remove "searchers"
lua_pop(L, 1); // remove "packages"
}
#endif

View File

@ -0,0 +1,15 @@
#include "gm9lua.h"
/*
* 0:/gm9/luapackages/?.lua;
* 0:/gm9/luapackages/?/init.lua;
* V:/luapackages/?.lua;
* V:/luapackages/?/init.lua
*/
#define GM9LUA_DEFAULT_PATH \
"0:/gm9/luapackages/"LUA_PATH_MARK".lua" LUA_PATH_SEP \
"0:/gm9/luapackages/"LUA_PATH_MARK"/init.lua" LUA_PATH_SEP \
"V:/luapackages/"LUA_PATH_MARK".lua" LUA_PATH_SEP \
"V:/luapackages/"LUA_PATH_MARK"/init.lua"
void ResetPackageSearchersAndPath(lua_State* L);

216
arm9/source/lua/gm9lua.c Normal file
View File

@ -0,0 +1,216 @@
#include "gm9lua.h"
#include "ui.h"
#include "language.h"
#ifndef NO_LUA
#include "fs.h"
#include "ff.h"
#include "vff.h"
#include "fsutil.h"
#include "unittype.h"
#include "nand.h"
#include "gm9loader.h"
#include "gm9os.h"
#include "gm9ui.h"
#include "gm9title.h"
#include "gm9internalfs.h"
#include "gm9internalsys.h"
#define DEBUGSP(x) ShowPrompt(false, (x))
typedef struct GM9LuaLoadF {
int n; // pre-read characters
FIL f;
FRESULT res;
char buff[BUFSIZ];
} GM9LuaLoadF;
// similar to "getF" in lauxlib.c
static const char* GetF(lua_State* L, void* ud, size_t* size) {
GM9LuaLoadF* lf = (GM9LuaLoadF*)ud;
UINT br = 0;
(void)L; // unused
if (lf->n > 0) { // check for pre-read characters
*size = lf->n; // return those
lf->n = 0;
} else {
if (fvx_eof(&lf->f)) return NULL;
lf->res = fvx_read(&lf->f, lf->buff, BUFSIZ, &br);
*size = (size_t)br;
if (lf->res != FR_OK) return NULL;
}
return lf->buff;
}
// similar to "errfile" in lauxlib.c
static int ErrFile(lua_State* L, const char* what, int fnameindex, FRESULT res) {
const char* filename = lua_tostring(L, fnameindex) + 1;
lua_pushfstring(L, "cannot %s %s:\nfatfs error %d", what, filename, res);
lua_remove(L, fnameindex);
return LUA_ERRFILE;
}
int LoadLuaFile(lua_State* L, const char* filename) {
GM9LuaLoadF lf;
lf.n = 0;
int status;
int fnameindex = lua_gettop(L) + 1; // index of filename on the stack
lua_pushfstring(L, "@%s", filename);
lf.res = fvx_open(&lf.f, filename, FA_READ | FA_OPEN_EXISTING);
if (lf.res != FR_OK) return ErrFile(L, "open", fnameindex, lf.res);
status = lua_load(L, GetF, &lf, lua_tostring(L, -1), NULL);
fvx_close(&lf.f);
if (lf.res != FR_OK) {
lua_settop(L, fnameindex);
return ErrFile(L, "read", fnameindex, lf.res);
}
lua_remove(L, fnameindex);
return status;
}
u32 GetFlagsFromTable(lua_State* L, int pos, u32 flags_ext_starter, u32 allowed_flags) {
char types[FLAGS_COUNT][14] = { FLAGS_STR };
int types_int[FLAGS_COUNT] = { FLAGS_CONSTS };
u32 flags_ext = flags_ext_starter;
for (int i = 0; i < FLAGS_COUNT; i++) {
if (!(allowed_flags & types_int[i])) continue;
lua_getfield(L, pos, types[i]);
if (lua_toboolean(L, -1)) flags_ext |= types_int[i];
lua_pop(L, 1);
}
return flags_ext;
}
void CheckWritePermissionsLuaError(lua_State* L, const char* path) {
if (!CheckWritePermissions(path)) {
luaL_error(L, "writing not allowed: %s", path);
}
}
static const luaL_Reg gm9lualibs[] = {
// built-ins
{LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
{LUA_DBLIBNAME, luaopen_debug},
// gm9 custom
{GM9LUA_OSLIBNAME, gm9lua_open_os},
{GM9LUA_UILIBNAME, gm9lua_open_ui},
{GM9LUA_TITLELIBNAME, gm9lua_open_title},
// gm9 custom internals (usually wrapped by a pure lua module)
{GM9LUA_INTERNALFSLIBNAME, gm9lua_open_internalfs},
{GM9LUA_INTERNALSYSLIBNAME, gm9lua_open_internalsys},
{NULL, NULL}
};
static void loadlibs(lua_State* L) {
const luaL_Reg* lib;
for (lib = gm9lualibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); // remove lib from stack
}
}
static bool RunFile(lua_State* L, const char* file) {
int result = LoadLuaFile(L, file);
if (result != LUA_OK) {
char errstr[BUFSIZ] = {0};
strlcpy(errstr, lua_tostring(L, -1), BUFSIZ);
WordWrapString(errstr, 0);
ShowPrompt(false, "Error during loading:\n%s", errstr);
return false;
}
if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) {
char errstr[BUFSIZ] = {0};
strlcpy(errstr, lua_tostring(L, -1), BUFSIZ);
WordWrapString(errstr, 0);
ShowPrompt(false, "Error during execution:\n%s", errstr);
return false;
}
return true;
}
// this is also taken from scripting.c
static inline bool isntrboot(void) {
// taken over from Luma 3DS:
// https://github.com/AuroraWright/Luma3DS/blob/bb5518b0f68d89bcd8efaf326355a770d5e57856/source/main.c#L58-L62
const vu8 *bootMediaStatus = (const vu8 *) 0x1FFFE00C;
const vu32 *bootPartitionsStatus = (const vu32 *) 0x1FFFE010;
// shell closed, no error booting NTRCARD, NAND partitions not even considered
return (bootMediaStatus[3] == 2) && !bootMediaStatus[1] && !bootPartitionsStatus[0] && !bootPartitionsStatus[1];
}
bool ExecuteLuaScript(const char* path_script) {
lua_State* L = luaL_newstate();
loadlibs(L);
ResetPackageSearchersAndPath(L);
ClearOutputBuffer();
// current path
char curr_dir[_VAR_CNT_LEN];
if (path_script) {
strncpy(curr_dir, path_script, _VAR_CNT_LEN);
curr_dir[_VAR_CNT_LEN-1] = '\0';
char* slash = strrchr(curr_dir, '/');
if (slash) *slash = '\0';
lua_pushstring(L, curr_dir);
} else {
lua_pushnil(L);
}
lua_setglobal(L, "CURRDIR");
lua_pushliteral(L, VERSION);
lua_setglobal(L, "GM9VER");
lua_pushstring(L, path_script);
lua_setglobal(L, "SCRIPT");
lua_pushliteral(L, OUTPUT_PATH);
lua_setglobal(L, "GM9OUT");
lua_pushstring(L, IS_UNLOCKED ? (isntrboot() ? "ntrboot" : "sighax") : "");
lua_setglobal(L, "HAX");
lua_pushinteger(L, GetNandSizeSectors(NAND_SYSNAND) * 0x200);
lua_setglobal(L, "NANDSIZE");
lua_pushboolean(L, IS_DEVKIT);
lua_setglobal(L, "IS_DEVKIT");
lua_pushstring(L, IS_O3DS ? "O3DS" : "N3DS");
lua_setglobal(L, "CONSOLE_TYPE");
bool result = RunFile(L, "V:/preload.lua");
if (!result) {
ShowPrompt(false, "A fatal error happened in GodMode9's preload script.\n \nThis is not an error with your code, but with\nGodMode9. Please report it on GitHub.");
lua_close(L);
return false;
}
RunFile(L, path_script);
lua_close(L);
return true;
}
#else
// No-Lua version
bool ExecuteLuaScript(const char* path_script) {
(void)path_script; // unused
ShowPrompt(false, "%s", STR_LUA_NOT_INCLUDED);
return false;
}
#endif

47
arm9/source/lua/gm9lua.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include "common.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "scripting.h"
// this should probably go in filesys/fsutil.h
#define RECURSIVE (1UL<<11)
#define TO_EMUNAND (1UL<<12)
#define LEGIT (1UL<<13)
#define FIND_FIRST (1UL<<14)
#define INCLUDE_DIRS (1UL<<15)
#define EXPLORER (1UL<<16)
#define ENCRYPTED (1UL<<17)
#define FLAGS_STR "no_cancel", "silent", "calc_sha", "sha1", "skip", "overwrite", "append", "all", "recursive", "to_emunand", "legit", "first", "include_dirs", "explorer", "encrypted"
#define FLAGS_CONSTS NO_CANCEL, SILENT, CALC_SHA, USE_SHA1, SKIP_ALL, OVERWRITE_ALL, APPEND_ALL, ASK_ALL, RECURSIVE, TO_EMUNAND, LEGIT, FIND_FIRST, INCLUDE_DIRS, EXPLORER, ENCRYPTED
#define FLAGS_COUNT 15
#define LUASCRIPT_EXT "lua"
#define LUASCRIPT_MAX_SIZE STD_BUFFER_SIZE
// taken from arm9/source/utils/scripting.c
#define _VAR_CNT_LEN 256
#ifndef NO_LUA
static inline void CheckLuaArgCount(lua_State* L, int argcount, const char* cmd) {
int args = lua_gettop(L);
if (args != argcount) {
luaL_error(L, "bad number of arguments passed to '%s' (expected %d, got %d)", cmd, argcount, args);
}
}
// this is used in cases where a function accepts a flags table or something else
static inline bool CheckLuaArgCountPlusExtra(lua_State* L, int argcount, const char* cmd) {
int args = lua_gettop(L);
if (args != argcount && args != argcount + 1) {
luaL_error(L, "bad number of arguments passed to '%s' (expected %d or %d, got %d)", cmd, argcount, argcount + 1, args);
}
return args == argcount + 1;
}
int LoadLuaFile(lua_State* L, const char* filename);
u32 GetFlagsFromTable(lua_State* L, int pos, u32 flags_ext_starter, u32 allowed_flags);
void CheckWritePermissionsLuaError(lua_State* L, const char* path);
#endif
bool ExecuteLuaScript(const char* path_script);

686
arm9/source/lua/gm9os.c Normal file
View File

@ -0,0 +1,686 @@
#ifndef NO_LUA
#include "gm9os.h"
#include "timer.h"
#include "rtc.h"
#include "ui.h"
u64 osclock;
static inline bool isLeapYear(u32 year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
size_t getWeekday(bool abbreviated, char* out, u8 weekday) {
if (abbreviated) {
switch(weekday) {
case 1:
strcat(out, "Mon");
break;
case 2:
strcat(out, "Tue");
break;
case 3:
strcat(out, "Wed");
break;
case 4:
strcat(out, "Thu");
break;
case 5:
strcpy(out, "Fri");
break;
case 6:
strcat(out, "Sat");
break;
case 0:
case 7:
strcat(out, "Sun");
break;
default:
strcat(out, "");
return 0;
}
return 3;
}
switch(weekday) {
case 1:
strcat(out, "Monday");
return 6;
case 2:
strcat(out, "Tuesday");
return 7;
case 3:
strcat(out, "Wednesday");
return 9;
case 4:
strcat(out, "Thursday");
return 8;
case 5:
strcpy(out, "Friday");
return 6;
case 6:
strcat(out, "Saturday");
return 8;
case 7:
case 0:
strcat(out, "Sunday");
return 6;
default:
strcat(out, "");
return 0;
}
return 0;
}
size_t getMonthName(bool abbreviated, char* out, u8 month) {
if (abbreviated) {
switch(month) {
case 1:
strcat(out, "Jan");
break;
case 2:
strcat(out, "Feb");
break;
case 3:
strcat(out, "Mar");
break;
case 4:
strcat(out, "Apr");
break;
case 5:
strcat(out, "May");
break;
case 6:
strcat(out, "Jun");
break;
case 7:
strcat(out, "Jul");
break;
case 8:
strcat(out, "Aug");
break;
case 9:
strcat(out, "Sep");
break;
case 10:
strcat(out, "Oct");
break;
case 11:
strcat(out, "Nov");
break;
case 12:
strcat(out, "Dec");
break;
default:
strcat(out, "");
return 0;
}
return 3;
}
switch(month) {
case 1:
strcat(out, "January");
return 7;
case 2:
strcat(out, "February");
return 8;
case 3:
strcat(out, "March");
return 5;
case 4:
strcat(out, "April");
return 5;
case 5:
strcat(out, "May");
return 3;
case 6:
strcat(out, "Juny");
return 4;
case 7:
strcat(out, "July");
return 4;
case 8:
strcat(out, "August");
return 6;
case 9:
strcat(out, "September");
return 9;
case 10:
strcat(out, "October");
return 7;
case 11:
strcat(out, "November");
return 8;
case 12:
strcat(out, "December");
return 8;
default:
strcat(out, "");
return 0;
}
return 0;
}
u16 getDaysMonths(u32 months, u8 years) {
u8 daysInMonth[12] = {31, isLeapYear(2000 + years) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //is this ?: bad practice?
u16 ret;
for (u32 month = 0; month < months - 1; month++) {
ret += daysInMonth[month];
}
return ret;
}
u64 calcUnixTime(u8 years, u8 months, u8 days, u8 hours, u8 minutes, u8 seconds) {
u8 daysInMonth[12] = {31, isLeapYear(2000 + years) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //is this ?: bad practice?
u32 curdays;
u64 ret = 0;
ret += seconds;
ret += minutes * 60;
ret += hours * 60 * 60;
ret += (days - 1) * 24 * 60 * 60;
for (u16 year = 0; year < years + 30; year++) { //+30 because unix time starting in 1970 but rtc starts in 2000
if (isLeapYear(2000 + year)) {
curdays = 366;
} else {
curdays = 365;
}
ret += curdays * 24 * 60 * 60;
}
for (u16 month = 0; month < months - 1; month++) {
ret += daysInMonth[month] * 24 * 60 * 60;
}
return ret;
}
u64 getUnixTimeFromRtc(DsTime *dstime) {
u8
seconds = DSTIMEGET(dstime, bcd_s),
minutes = DSTIMEGET(dstime, bcd_m),
hours = DSTIMEGET(dstime, bcd_h),
days = DSTIMEGET(dstime, bcd_D),
months = DSTIMEGET(dstime, bcd_M),
years = DSTIMEGET(dstime, bcd_Y);
return calcUnixTime(years, months, days, hours, minutes, seconds);
}
u64 timer_usec( u64 start_time ) {
return timer_ticks( start_time ) / (TICKS_PER_SEC/1000000);
}
void weekdayfix(DsTime *dstime) {
int days = getUnixTimeFromRtc(dstime) / 86400; //days since thursday 1 1 1970
u8 weekday = (days + 5) % 7;
dstime->weekday = NUM2BCD(weekday);
}
void unixtodstime(u64 unixtime, DsTime *dstime) {
u32 seconds, minutes, hours, days, year, month;
seconds = unixtime;
minutes = seconds / 60;
seconds %= 60;
hours = minutes / 60;
minutes %= 60;
days = hours / 24;
hours %= 24;
year = 1970;
while(true)
{
bool leapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
u16 daysInYear = leapYear ? 366 : 365;
if(days >= daysInYear)
{
days -= daysInYear;
++year;
}
else
{
static const u8 daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(month = 0; month < 12; ++month)
{
u8 dim = daysInMonth[month];
if (month == 1 && leapYear)
++dim;
if (days >= dim)
days -= dim;
else
break;
}
break;
}
}
days++;
month++;
u8 bcd_year = year-2000;
dstime->bcd_Y = NUM2BCD(bcd_year);
dstime->bcd_M = NUM2BCD(month);
dstime->bcd_D = NUM2BCD(days);
dstime->bcd_h = NUM2BCD(hours);
dstime->bcd_m = NUM2BCD(minutes);
dstime->bcd_s = NUM2BCD(seconds);
dstime->weekday = 0;
}
bool my_strftime(char* _out, size_t _maxsize, const char* str, DsTime *dstime) { //my refers to github.com/Gruetzig
weekdayfix(dstime);
size_t strl = strlen(str);
size_t outpos = 0;
char out[_maxsize+10];
memset(out, 0, _maxsize+10);
u8 minute, hour, day, month, year, weekday, second, weeknumber;
u16 currentday, nextsunday, fyear;
char numbuf[3], numbuf2[5], numbuf3[9], numnum1[3], numnum2[3], numnum3[3];
if (!is_valid_dstime(dstime)) {
return false;
}
for (size_t i = 0;i<strl;i++) {
if (str[i] == '%') {
i++;
switch(str[i]) {
case 'a':
outpos += getWeekday(true, out, DSTIMEGET(dstime, weekday));
break;
case 'A':
outpos += getWeekday(false, out, DSTIMEGET(dstime, weekday));
break;
case 'b':
outpos += getMonthName(true, out, DSTIMEGET(dstime, bcd_M));
break;
case 'B':
outpos += getMonthName(false, out, DSTIMEGET(dstime, bcd_M));
break;
case 'c':
char buf[100];
my_strftime(buf, _maxsize-outpos-10, "%a %b %d %X %Y", dstime);
strcat(out, buf);
outpos += strlen(buf);
break;
case 'd':
day = DSTIMEGET(dstime, bcd_D);
if (day < 10) {
sprintf(numbuf, "0%d", day);
} else {
sprintf(numbuf, "%d", day);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'H':
hour = DSTIMEGET(dstime, bcd_h);
if (day < 10) {
sprintf(numbuf, "0%d", hour);
} else {
sprintf(numbuf, "%d", hour);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'I':
hour = DSTIMEGET(dstime, bcd_h);
if (hour > 12) {
hour = hour - 12;
}
if (!hour) {
hour = 12;
}
if (hour < 10) {
sprintf(numbuf, "0%d", hour);
} else {
sprintf(numbuf, "%d", hour);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'j':
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
if (currentday < 10) {
sprintf(numbuf, "00%d", currentday);
} else if (currentday < 100) {
sprintf(numbuf, "0%d", currentday);
} else {
sprintf(numbuf, "%d", currentday);
}
strcat(out, numbuf);
outpos += 3;
break;
case 'm':
month = DSTIMEGET(dstime, bcd_M);
if (month < 10) {
sprintf(numbuf, "0%d", month);
} else {
sprintf(numbuf, "%d", month);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'M':
minute = DSTIMEGET(dstime, bcd_m);
if (minute < 10) {
sprintf(numbuf, "0%d", minute);
} else {
sprintf(numbuf, "%d", minute);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'p':
hour = DSTIMEGET(dstime, bcd_h);
if (hour >= 12) {
strcat(out, "PM");
} else {
strcat(out, "AM");
}
outpos += 2;
break;
case 'S':
second = DSTIMEGET(dstime, bcd_m);
if (second < 10) {
sprintf(numbuf, "0%d", second);
} else {
sprintf(numbuf, "%d", second);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'U':
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
weekday = DSTIMEGET(dstime, weekday);
nextsunday = ((7-weekday)+currentday);
weeknumber = (nextsunday/7)+1;
if (weeknumber < 10) {
sprintf(numbuf, "0%d", weeknumber);
} else {
sprintf(numbuf, "%d", weeknumber);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'w':
weekday = DSTIMEGET(dstime, weekday);
if (weekday == 7) {
weekday = 0;
}
sprintf(out, "%d", weekday);
strcat(out, numbuf);
outpos++;
break;
case 'W':
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
weekday = DSTIMEGET(dstime, weekday);
nextsunday = ((8-weekday)+currentday);
weeknumber = (nextsunday/7);
if (weeknumber < 10) {
sprintf(numbuf, "0%d", weeknumber);
} else {
sprintf(numbuf, "%d", weeknumber);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'x':
month = DSTIMEGET(dstime, bcd_M);
day = DSTIMEGET(dstime, bcd_D);
year = DSTIMEGET(dstime, bcd_Y);
if (month < 10) {
sprintf(numnum1, "0%d", month);
} else {
sprintf(numnum1, "%d", month);
}
if (day < 10) {
sprintf(numnum2, "0%d", day);
} else {
sprintf(numnum2, "%d", day);
}
if (year < 10) {
sprintf(numnum3, "0%d", year);
} else {
sprintf(numnum3, "%d", year);
}
sprintf(numbuf3, "%s/%s/%s", numnum1, numnum2, numnum3);
strcat(out, numbuf3);
outpos += 8;
break;
case 'X':
hour = DSTIMEGET(dstime, bcd_h);
minute = DSTIMEGET(dstime, bcd_m);
second = DSTIMEGET(dstime, bcd_s);
if (hour < 10) {
sprintf(numnum1, "0%d", hour);
} else {
sprintf(numnum1, "%d", hour);
}
if (minute < 10) {
sprintf(numnum2, "0%d", minute);
} else {
sprintf(numnum2, "%d", minute);
}
if (second < 10) {
sprintf(numnum3, "0%d", second);
} else {
sprintf(numnum3, "%d", second);
}
sprintf(numbuf3, "%s:%s:%s", numnum1, numnum2, numnum3);
strcat(out, numbuf3);
outpos += 8;
break;
case 'y':
year = DSTIMEGET(dstime, bcd_Y);
if (year < 10) {
sprintf(numbuf, "0%d", year);
} else {
sprintf(numbuf, "%d", year);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'Y':
fyear = DSTIMEGET(dstime, bcd_Y);
fyear += 2000;
sprintf(numbuf2, "%d", fyear);
strcat(out, numbuf2);
outpos += 4;
break;
case '%':
strcat(out, "%");
outpos++;
break;
default:
break; //not implemented
}
} else {
out[outpos] = str[i];
outpos++;
}
if (outpos > _maxsize) {
break;
}
}
strncpy(_out, out, _maxsize);
return true;
}
static int os_time(lua_State *L) {
int args = lua_gettop(L);
u64 unixtime;
switch(args) {
case 0:
DsTime dstime;
get_dstime(&dstime);
unixtime = getUnixTimeFromRtc(&dstime);
lua_pushinteger(L, unixtime);
return 1;
case 1:
lua_geti(L, 1, 1);
int year = lua_tointeger(L, -1);
lua_pop(L, 1);
if (year >= 2000) year -= 2000;
lua_geti(L, 1, 2);
int month = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 3);
int day = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 4);
int hour = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 5);
int minute = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 6);
int second = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pushinteger(L, calcUnixTime(year, month, day, hour, minute, second));
return 1;
default:
return luaL_error(L, "not a valid amount of arguments");
}
}
static int os_date(lua_State *L) {
DsTime dstime;
get_dstime(&dstime);
char retbuf[100];
memset(retbuf, 0, 100);
int args = lua_gettop(L);
switch(args) {
case 0:
my_strftime(retbuf, 100, "%c", &dstime);
lua_pushstring(L, retbuf);
return 1;
case 1:
const char* str = lua_tostring(L, 1);
if ((strcmp(str, "*t") == 0 || strcmp(str, "!*t") == 0)) {
weekdayfix(&dstime);
//return table with date values
lua_newtable(L);
//year
lua_pushinteger(L, 2000+DSTIMEGET(&dstime, bcd_Y));
lua_setfield(L, -2, "year");
//month
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_M));
lua_setfield(L, -2, "month");
//wday (weekday)
lua_pushinteger(L, DSTIMEGET(&dstime, weekday));
lua_setfield(L, -2, "wday");
//yday (yearday)
lua_pushinteger(L, getDaysMonths(DSTIMEGET(&dstime, bcd_M), DSTIMEGET(&dstime, bcd_Y))+DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "yday");
//day
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "day");
//hour
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_h));
lua_setfield(L, -2, "hour");
//minute
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_m));
lua_setfield(L, -2, "min");
//second
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_s));
lua_setfield(L, -2, "sec");
return 1;
} else {
my_strftime(retbuf, 100, lua_tostring(L, 1), &dstime);
lua_pushstring(L, retbuf);
return 1;
}
case 2:
if (lua_tointeger(L, 2) < 946684800) { //unix timestamp is 01.01.2000 00:00:00, so everything before is previous century and not supported
return luaL_error(L, "unix timestamp from before 2000 is not supported");
}
const char* str2 = lua_tostring(L, 1);
if ((strcmp(str2, "*t") == 0 || strcmp(str2, "!*t") == 0)) {
unixtodstime( lua_tointeger(L, 2) , &dstime);
weekdayfix(&dstime);
//return table with date values
lua_newtable(L);
//year
lua_pushinteger(L, 2000+DSTIMEGET(&dstime, bcd_Y));
lua_setfield(L, -2, "year");
//month
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_M));
lua_setfield(L, -2, "month");
//wday (weekday)
lua_pushinteger(L, DSTIMEGET(&dstime, weekday));
lua_setfield(L, -2, "wday");
//yday (yearday)
lua_pushinteger(L, getDaysMonths(DSTIMEGET(&dstime, bcd_M), DSTIMEGET(&dstime, bcd_Y))+DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "yday");
//day
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "day");
//hour
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_h));
lua_setfield(L, -2, "hour");
//minute
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_m));
lua_setfield(L, -2, "min");
//second
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_s));
lua_setfield(L, -2, "sec");
return 1;
} else {
unixtodstime( lua_tointeger(L, 2) , &dstime);
my_strftime(retbuf, 100, str2, &dstime);
lua_pushstring(L, retbuf);
return 1;
}
default:
return luaL_error(L, "not a valid amount of arguments");
}
}
static int os_clock(lua_State *L) {
lua_pushnumber(L, timer_usec(osclock)/10000000.0);
return 1;
}
static int os_difftime(lua_State *L) {
u64 t2 = lua_tointeger(L, 1);
u64 t1 = lua_tointeger(L, 2);
lua_pushinteger(L, t2-t1);
return 1;
}
// os.remove and os.rename are done in data/luapackages/fs.lua
static const luaL_Reg os[] = {
{"clock", os_clock},
{"time", os_time},
{"date", os_date},
{"difftime", os_difftime},
{NULL, NULL}
};
int gm9lua_open_os(lua_State* L) {
luaL_newlib(L, os);
osclock = timer_start();
return 1;
}
#endif

6
arm9/source/lua/gm9os.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include "gm9lua.h"
#define GM9LUA_OSLIBNAME "os"
int gm9lua_open_os(lua_State* L);

183
arm9/source/lua/gm9title.c Normal file
View File

@ -0,0 +1,183 @@
#ifndef NO_LUA
#include "gm9lua.h"
#include "utils.h"
#include "fs.h"
#include "language.h"
#include "ui.h"
#include "game.h"
#include "ips.h"
#include "bps.h"
static int title_decrypt(lua_State* L) {
CheckLuaArgCount(L, 1, "title.decrypt");
const char* path = luaL_checkstring(L, 1);
const char* whichfailed = "";
u64 filetype = IdentifyFileType(path);
bool ret;
if (filetype & BIN_KEYDB) {
ret = (CryptAesKeyDb(path, true, false) == 0);
whichfailed = "CryptAesKeyDb";
} else {
ret = (CryptGameFile(path, true, false) == 0);
whichfailed = "CryptGameFile";
}
if (!ret) {
return luaL_error(L, "%s failed on %s", whichfailed, path);
}
return 0;
}
static int title_encrypt(lua_State* L) {
CheckLuaArgCount(L, 1, "title.encrypt");
const char* path = luaL_checkstring(L, 1);
const char* whichfailed = "";
u64 filetype = IdentifyFileType(path);
bool ret;
if (filetype & BIN_KEYDB) {
ret = (CryptAesKeyDb(path, true, true) == 0);
whichfailed = "CryptAesKeyDb";
} else {
ret = (CryptGameFile(path, true, true) == 0);
whichfailed = "CryptGameFile";
}
if (!ret) {
return luaL_error(L, "%s failed on %s", whichfailed, path);
}
return 0;
}
static int title_install(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "title.install");
const char* path = luaL_checkstring(L, 1);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 2, flags, TO_EMUNAND);
};
bool ret = (InstallGameFile(path, (flags & TO_EMUNAND)) == 0);
if (!ret) {
return luaL_error(L, "InstallGameFile failed on %s", path);
}
return 0;
}
static int title_build_cia(lua_State* L) {
bool extra = CheckLuaArgCountPlusExtra(L, 1, "title.build_cia");
const char* path = luaL_checkstring(L, 1);
u32 flags = 0;
if (extra) {
flags = GetFlagsFromTable(L, 2, flags, LEGIT);
};
bool ret = (BuildCiaFromGameFile(path, (flags & LEGIT)) == 0);
if (!ret) {
return luaL_error(L, "BuildCiaFromGameFile failed on %s", path);
}
return 0;
}
static int title_extract_code(lua_State* L) {
CheckLuaArgCount(L, 2, "title.extract_code");
const char* path_src = luaL_checkstring(L, 1);
const char* path_dst = luaL_checkstring(L, 2);
u64 filetype = IdentifyFileType(path_src);
if (!FTYPE_HASCODE(filetype)) {
return luaL_error(L, "%s does not have code", path_src);
} else {
CheckWritePermissionsLuaError(L, path_dst);
ShowString("%s", STR_EXTRACTING_DOT_CODE);
bool ret = (ExtractCodeFromCxiFile(path_src, path_dst, NULL) == 0);
if (!ret) {
return luaL_error(L, "failed to extract code from %s", path_src);
}
}
return 0;
}
static int title_compress_code(lua_State* L) {
CheckLuaArgCount(L, 2, "title.compress_code");
const char* path_src = luaL_checkstring(L, 1);
const char* path_dst = luaL_checkstring(L, 2);
CheckWritePermissionsLuaError(L, path_dst);
ShowString("%s", STR_COMPRESSING_DOT_CODE);
bool ret = (CompressCode(path_src, path_dst) == 0);
if (!ret) {
return luaL_error(L, "failed to compress code from %s", path_src);
}
return 0;
}
static int title_apply_ips(lua_State* L) {
CheckLuaArgCount(L, 3, "title.apply_ips");
const char* path_patch = luaL_checkstring(L, 1);
const char* path_src = luaL_checkstring(L, 2);
const char* path_target = luaL_checkstring(L, 3);
bool ret = (ApplyIPSPatch(path_patch, path_src, path_target) == 0);
if (!ret) {
return luaL_error(L, "ApplyIPSPatch failed");
}
return 0;
}
static int title_apply_bps(lua_State* L) {
CheckLuaArgCount(L, 3, "title.apply_bps");
const char* path_patch = luaL_checkstring(L, 1);
const char* path_src = luaL_checkstring(L, 2);
const char* path_target = luaL_checkstring(L, 3);
bool ret = (ApplyBPSPatch(path_patch, path_src, path_target) == 0);
if (!ret) {
return luaL_error(L, "ApplyBPSPatch failed");
}
return 0;
}
static int title_apply_bpm(lua_State* L) {
CheckLuaArgCount(L, 3, "title.apply_bpm");
const char* path_patch = luaL_checkstring(L, 1);
const char* path_src = luaL_checkstring(L, 2);
const char* path_target = luaL_checkstring(L, 3);
bool ret = (ApplyBPMPatch(path_patch, path_src, path_target) == 0);
if (!ret) {
return luaL_error(L, "ApplyBPMPatch failed");
}
return 0;
}
static const luaL_Reg title_lib[] = {
{"decrypt", title_decrypt},
{"encrypt", title_encrypt},
{"install", title_install},
{"build_cia", title_build_cia},
{"extract_code", title_extract_code},
{"compress_code", title_compress_code},
{"apply_ips", title_apply_ips},
{"apply_bps", title_apply_bps},
{"apply_bpm", title_apply_bpm},
{NULL, NULL}
};
int gm9lua_open_title(lua_State* L) {
luaL_newlib(L, title_lib);
return 1;
}
#endif

View File

@ -0,0 +1,6 @@
#pragma once
#include "gm9lua.h"
#define GM9LUA_TITLELIBNAME "title"
int gm9lua_open_title(lua_State* L);

360
arm9/source/lua/gm9ui.c Normal file
View File

@ -0,0 +1,360 @@
#ifndef NO_LUA
#include "gm9ui.h"
#include "ui.h"
#include "fs.h"
#include "png.h"
#include "swkbd.h"
#include "qrcodegen.h"
#include "utils.h"
#include "hid.h"
#define MAXOPTIONS 256
#define MAXOPTIONS_STR "256"
#define OUTPUTMAXLINES 24
#define OUTPUTMAXCHARSPERLINE 80 // make sure this includes space for '\0'
// this output buffer stuff is especially a test, it needs to take into account newlines and fonts that are not 8x10
char output_buffer[OUTPUTMAXLINES][OUTPUTMAXCHARSPERLINE]; // hold 24 lines
void ShiftOutputBufferUp(void) {
for (int i = 0; i < OUTPUTMAXLINES - 1; i++) {
memcpy(output_buffer[i], output_buffer[i + 1], OUTPUTMAXCHARSPERLINE);
}
}
void ClearOutputBuffer(void) {
memset(output_buffer, 0, sizeof(output_buffer));
}
void WriteToOutputBuffer(char* text) {
strlcpy(output_buffer[OUTPUTMAXLINES - 1], text, OUTPUTMAXCHARSPERLINE);
}
void RenderOutputBuffer(void) {
ClearScreenF(false, true, COLOR_STD_BG);
for (int i = 0; i < OUTPUTMAXLINES; i++) {
DrawString(ALT_SCREEN, output_buffer[i], 0, i * 10, COLOR_STD_FONT, COLOR_TRANSPARENT);
}
}
static int ui_echo(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.echo");
const char* text = lua_tostring(L, 1);
ShowPrompt(false, "%s", text);
return 0;
}
static int ui_ask(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.ask");
const char* text = lua_tostring(L, 1);
bool ret = ShowPrompt(true, "%s", text);
lua_pushboolean(L, ret);
return 1;
}
static int ui_ask_hex(lua_State* L) {
CheckLuaArgCount(L, 3, "ui.ask_hex");
const char* text = lua_tostring(L, 1);
u64 initial_hex = lua_tonumber(L, 2);
u32 n_digits = lua_tonumber(L, 3);
u64 ret = ShowHexPrompt(initial_hex, n_digits, "%s", text);
if (ret == (u64) -1) {
lua_pushnil(L);
} else {
lua_pushnumber(L, ret);
}
return 1;
}
static int ui_ask_number(lua_State* L) {
CheckLuaArgCount(L, 2, "ui.ask_number");
const char* text = lua_tostring(L, 1);
u64 initial_num = lua_tonumber(L, 2);
u64 ret = ShowNumberPrompt(initial_num, "%s", text);
if (ret == (u64) -1) {
lua_pushnil(L);
} else {
lua_pushnumber(L, ret);
}
return 1;
}
static int ui_ask_text(lua_State* L) {
CheckLuaArgCount(L, 3, "ui.ask_text");
const char* prompt = lua_tostring(L, 1);
const char* _initial_text = lua_tostring(L, 2);
u32 initial_text_size = strlen(_initial_text)+1;
char initial_text[initial_text_size];
snprintf(initial_text, initial_text_size, "%s", _initial_text);
u32 max_size = lua_tonumber(L, 3);
bool result = ShowKeyboardOrPrompt(initial_text, max_size, "%s", prompt);
if (result)
lua_pushstring(L, initial_text);
else
lua_pushnil(L);
return 1;
}
static int ui_clear(lua_State* L) {
CheckLuaArgCount(L, 0, "ui.clear");
ClearScreen(ALT_SCREEN, COLOR_STD_BG);
return 0;
}
static int ui_show_png(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.show_png");
const char* path = luaL_checkstring(L, 1);
u16 *screen = ALT_SCREEN;
u16 *bitmap = NULL;
u8* png = (u8*) malloc(SCREEN_SIZE(screen));
u32 bitmap_width, bitmap_height;
if (png) {
u32 png_size = FileGetData(path, png, SCREEN_SIZE(screen), 0);
if (!png_size) {
free(png);
return luaL_error(L, "Could not read %s", path);
}
if (png_size && png_size < SCREEN_SIZE(screen)) {
bitmap = PNG_Decompress(png, png_size, &bitmap_width, &bitmap_height);
if (!bitmap) {
free(png);
return luaL_error(L, "Invalid PNG file");
}
}
free(png);
if (!bitmap) {
return luaL_error(L, "PNG too large");
} else if ((SCREEN_WIDTH(screen) < bitmap_width) || (SCREEN_HEIGHT < bitmap_height)) {
free(bitmap);
return luaL_error(L, "PNG too large");
} else {
ClearScreen(ALT_SCREEN, COLOR_STD_BG);
DrawBitmap(
screen, // screen
-1, // x coordinate from argument
-1, // y coordinate from argument
bitmap_width, // width
bitmap_height, // height
bitmap // bitmap
);
free(bitmap);
}
}
return 0;
}
static int ui_show_text(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.show_text");
const char* text = lua_tostring(L, 1);
ClearScreen(ALT_SCREEN, COLOR_STD_BG);
DrawStringCenter(ALT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", text);
return 0;
}
static int ui_show_game_info(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.show_game_info");
const char* path = luaL_checkstring(L, 1);
bool ret = (ShowGameFileIcon(path, ALT_SCREEN) == 0);
if (!ret) {
return luaL_error(L, "ShowGameFileIcon failed on %s", path);
}
return 0;
}
static int ui_show_qr(lua_State* L) {
CheckLuaArgCount(L, 2, "ui.show_qr");
size_t data_len;
const char* text = luaL_checkstring(L, 1);
const char* data = luaL_checklstring(L, 2, &data_len);
const u32 screen_size = SCREEN_SIZE(ALT_SCREEN);
u8* screen_copy = (u8*) malloc(screen_size);
u8 qrcode[qrcodegen_BUFFER_LEN_MAX];
u8 temp[qrcodegen_BUFFER_LEN_MAX];
bool ret = screen_copy && qrcodegen_encodeText(data, temp, qrcode, qrcodegen_Ecc_LOW,
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
if (ret) {
memcpy(screen_copy, ALT_SCREEN, screen_size);
DrawQrCode(ALT_SCREEN, qrcode);
ShowPrompt(false, "%s", text);
memcpy(ALT_SCREEN, screen_copy, screen_size);
} else {
return luaL_error(L, "could not allocate memory");
}
free(screen_copy);
return 0;
}
static int ui_show_text_viewer(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.show_text_viewer");
size_t len = 0;
const char* text = luaL_tolstring(L, 1, &len);
// validate text ourselves so we can return a better error
// MemTextViewer calls ShowPrompt if it's bad, and i don't want that
if (!(ValidateText(text, len))) {
return luaL_error(L, "text validation failed");
}
if (!(MemTextViewer(text, len, 1, false))) {
return luaL_error(L, "failed to run MemTextViewer");
}
return 0;
}
static int ui_show_file_text_viewer(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.show_file_text_viewer");
const char* path = luaL_checkstring(L, 1);
// validate text ourselves so we can return a better error
// MemTextViewer calls ShowPrompt if it's bad, and i don't want that
// and FileTextViewer calls the above function
char* text = malloc(STD_BUFFER_SIZE);
if (!text) {
return luaL_error(L, "could not allocate memory");
};
// TODO: replace this with something that can detect file read errors and actual 0-length files
size_t flen = FileGetData(path, text, STD_BUFFER_SIZE - 1, 0);
text[flen] = '\0';
u32 len = (ptrdiff_t)memchr(text, '\0', flen + 1) - (ptrdiff_t)text;
if (!(ValidateText(text, len))) {
free(text);
return luaL_error(L, "text validation failed");
}
if (!(MemTextViewer(text, len, 1, false))) {
free(text);
return luaL_error(L, "failed to run MemTextViewer");
}
free(text);
return 0;
}
static int ui_ask_selection(lua_State* L) {
CheckLuaArgCount(L, 2, "ui.ask_selection");
const char* text = lua_tostring(L, 1);
char* options[MAXOPTIONS];
const char* tmpstr;
size_t len;
int i;
luaL_argcheck(L, lua_istable(L, 2), 2, "table expected");
lua_Integer opttablesize = luaL_len(L, 2);
luaL_argcheck(L, opttablesize <= MAXOPTIONS, 2, "more than " MAXOPTIONS_STR " options given");
for (i = 0; i < opttablesize; i++) {
lua_geti(L, 2, i + 1);
tmpstr = lua_tolstring(L, -1, &len);
options[i] = malloc(len + 1);
strlcpy(options[i], tmpstr, len + 1);
lua_pop(L, 1);
}
int result = ShowSelectPrompt(opttablesize, (const char**)options, "%s", text);
for (i = 0; i < opttablesize; i++) free(options[i]);
// lua only treats "false" and "nil" as false values
// so to make this easier, return nil and not 0 if no choice was made
// https://www.lua.org/manual/5.4/manual.html#3.3.4
if (result)
lua_pushinteger(L, result);
else
lua_pushnil(L);
return 1;
}
static int ui_format_bytes(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.format_bytes");
lua_Integer size = luaL_checkinteger(L, 1);
char bytesstr[32] = { 0 };
FormatBytes(bytesstr, (u64)size);
lua_pushstring(L, bytesstr);
return 1;
}
static int ui_global_print(lua_State* L) {
//const char* text = lua_tostring(L, 1);
char buf[OUTPUTMAXCHARSPERLINE] = {0};
int argcount = lua_gettop(L);
for (int i = 0; i < lua_gettop(L); i++) {
const char* str = luaL_tolstring(L, i+1, NULL);
if (str) {
strlcat(buf, str, OUTPUTMAXCHARSPERLINE);
lua_pop(L, 1);
} else {
// idk
strlcat(buf, "(unknown)", OUTPUTMAXCHARSPERLINE);
}
if (i < argcount) strlcat(buf, " ", OUTPUTMAXCHARSPERLINE);
}
ShiftOutputBufferUp();
WriteToOutputBuffer((char*)buf);
RenderOutputBuffer();
return 0;
}
// TODO: use luaL_checkoption which will auto-raise an error
// use BUTTON_STRINGS from common/hid_map.h
static int ui_check_key(lua_State* L) {
CheckLuaArgCount(L, 1, "ui.check_key");
const char* key = luaL_checkstring(L, 1);
lua_pushboolean(L, CheckButton(StringToButton((char*)key)));
return 1;
}
static const luaL_Reg ui_lib[] = {
{"echo", ui_echo},
{"ask", ui_ask},
{"ask_hex", ui_ask_hex},
{"ask_number", ui_ask_number},
{"ask_text", ui_ask_text},
{"ask_selection", ui_ask_selection},
{"clear", ui_clear},
{"show_png", ui_show_png},
{"show_text", ui_show_text},
{"show_game_info", ui_show_game_info},
{"show_qr", ui_show_qr},
{"show_text_viewer", ui_show_text_viewer},
{"show_file_text_viewer", ui_show_file_text_viewer},
{"format_bytes", ui_format_bytes},
{"check_key", ui_check_key},
{NULL, NULL}
};
static const luaL_Reg ui_global_lib[] = {
{"print", ui_global_print},
{NULL, NULL}
};
int gm9lua_open_ui(lua_State* L) {
luaL_newlib(L, ui_lib);
lua_pushglobaltable(L); // push global table to stack
luaL_setfuncs(L, ui_global_lib, 0); // set global funcs
lua_pop(L, 1); // pop global table from stack
return 1;
}
#endif

10
arm9/source/lua/gm9ui.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include "gm9lua.h"
#define GM9LUA_UILIBNAME "ui"
void ShiftOutputBufferUp(void);
void ClearOutputBuffer(void);
void RenderOutputBuffer(void);
void WriteToOutputBuffer(char* text);
int gm9lua_open_ui(lua_State* L);

1463
arm9/source/lua/lapi.c Normal file

File diff suppressed because it is too large Load Diff

52
arm9/source/lua/lapi.h Normal file
View File

@ -0,0 +1,52 @@
/*
** $Id: lapi.h $
** Auxiliary functions from Lua API
** See Copyright Notice in lua.h
*/
#ifndef lapi_h
#define lapi_h
#include "llimits.h"
#include "lstate.h"
/* Increments 'L->top.p', checking for stack overflows */
#define api_incr_top(L) {L->top.p++; \
api_check(L, L->top.p <= L->ci->top.p, \
"stack overflow");}
/*
** If a call returns too many multiple returns, the callee may not have
** stack space to accommodate all results. In this case, this macro
** increases its stack space ('L->ci->top.p').
*/
#define adjustresults(L,nres) \
{ if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
L->ci->top.p = L->top.p; }
/* Ensure the stack has at least 'n' elements */
#define api_checknelems(L,n) \
api_check(L, (n) < (L->top.p - L->ci->func.p), \
"not enough elements in the stack")
/*
** To reduce the overhead of returning from C functions, the presence of
** to-be-closed variables in these functions is coded in the CallInfo's
** field 'nresults', in a way that functions with no to-be-closed variables
** with zero, one, or "all" wanted results have no overhead. Functions
** with other number of wanted results, as well as functions with
** variables to be closed, have an extra check.
*/
#define hastocloseCfunc(n) ((n) < LUA_MULTRET)
/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
#define codeNresults(n) (-(n) - 3)
#define decodeNresults(n) (-(n) - 3)
#endif

1126
arm9/source/lua/lauxlib.c Normal file

File diff suppressed because it is too large Load Diff

301
arm9/source/lua/lauxlib.h Normal file
View File

@ -0,0 +1,301 @@
/*
** $Id: lauxlib.h $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
#ifndef lauxlib_h
#define lauxlib_h
#include <stddef.h>
#include <stdio.h>
#include "luaconf.h"
#include "lua.h"
/* global table */
#define LUA_GNAME "_G"
typedef struct luaL_Buffer luaL_Buffer;
/* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1)
/* key, in the registry, for table of loaded modules */
#define LUA_LOADED_TABLE "_LOADED"
/* key, in the registry, for table of preloaded loaders */
#define LUA_PRELOAD_TABLE "_PRELOAD"
typedef struct luaL_Reg {
const char *name;
lua_CFunction func;
} luaL_Reg;
#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
#define luaL_checkversion(L) \
luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname);
LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
size_t *l);
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
const char *def, size_t *l);
LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg);
LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def);
LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg);
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg,
lua_Integer def);
LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t);
LUALIB_API void (luaL_checkany) (lua_State *L, int arg);
LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname);
LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
LUALIB_API void (luaL_where) (lua_State *L, int lvl);
LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
const char *const lst[]);
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
/* predefined references */
#define LUA_NOREF (-2)
#define LUA_REFNIL (-1)
LUALIB_API int (luaL_ref) (lua_State *L, int t);
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
const char *mode);
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
const char *name, const char *mode);
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
LUALIB_API lua_State *(luaL_newstate) (void);
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
const char *p, const char *r);
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
const char *p, const char *r);
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
const char *msg, int level);
LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
lua_CFunction openf, int glb);
/*
** ===============================================================
** some useful macros
** ===============================================================
*/
#define luaL_newlibtable(L,l) \
lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
#define luaL_newlib(L,l) \
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
#define luaL_argcheck(L, cond,arg,extramsg) \
((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
#define luaL_argexpected(L,cond,arg,tname) \
((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname))))
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
#define luaL_dofile(L, fn) \
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
#define luaL_dostring(L, s) \
(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
/*
** Perform arithmetic operations on lua_Integer values with wrap-around
** semantics, as the Lua core does.
*/
#define luaL_intop(op,v1,v2) \
((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2)))
/* push the value used to represent failure/error */
#define luaL_pushfail(L) lua_pushnil(L)
/*
** Internal assertions for in-house debugging
*/
#if !defined(lua_assert)
#if defined LUAI_ASSERT
#include <assert.h>
#define lua_assert(c) assert(c)
#else
#define lua_assert(c) ((void)0)
#endif
#endif
/*
** {======================================================
** Generic Buffer manipulation
** =======================================================
*/
struct luaL_Buffer {
char *b; /* buffer address */
size_t size; /* buffer size */
size_t n; /* number of characters in buffer */
lua_State *L;
union {
LUAI_MAXALIGN; /* ensure maximum alignment for buffer */
char b[LUAL_BUFFERSIZE]; /* initial buffer */
} init;
};
#define luaL_bufflen(bf) ((bf)->n)
#define luaL_buffaddr(bf) ((bf)->b)
#define luaL_addchar(B,c) \
((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \
((B)->b[(B)->n++] = (c)))
#define luaL_addsize(B,s) ((B)->n += (s))
#define luaL_buffsub(B,s) ((B)->n -= (s))
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz);
LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
/* }====================================================== */
/*
** {======================================================
** File handles for IO library
** =======================================================
*/
/*
** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
** initial structure 'luaL_Stream' (it may contain other fields
** after that initial structure).
*/
#define LUA_FILEHANDLE "FILE*"
typedef struct luaL_Stream {
FILE *f; /* stream (NULL for incompletely created streams) */
lua_CFunction closef; /* to close stream (NULL for closed streams) */
} luaL_Stream;
/* }====================================================== */
/*
** {==================================================================
** "Abstraction Layer" for basic report of messages and errors
** ===================================================================
*/
/* print a string */
#if !defined(lua_writestring)
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
#endif
/* print a newline and flush the output */
#if !defined(lua_writeline)
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
#endif
/* print an error message */
#if !defined(lua_writestringerror)
#define lua_writestringerror(s,p) \
(fprintf(stderr, (s), (p)), fflush(stderr))
#endif
/* }================================================================== */
/*
** {============================================================
** Compatibility with deprecated conversions
** =============================================================
*/
#if defined(LUA_COMPAT_APIINTCASTS)
#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
#define luaL_optunsigned(L,a,d) \
((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
#endif
/* }============================================================ */
#endif

549
arm9/source/lua/lbaselib.c Normal file
View File

@ -0,0 +1,549 @@
/*
** $Id: lbaselib.c $
** Basic library
** See Copyright Notice in lua.h
*/
#define lbaselib_c
#define LUA_LIB
#include "lprefix.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int luaB_print (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
for (i = 1; i <= n; i++) { /* for each argument */
size_t l;
const char *s = luaL_tolstring(L, i, &l); /* convert it to string */
if (i > 1) /* not the first element? */
lua_writestring("\t", 1); /* add a tab before it */
lua_writestring(s, l); /* print it */
lua_pop(L, 1); /* pop result */
}
lua_writeline();
return 0;
}
/*
** Creates a warning with all given arguments.
** Check first for errors; otherwise an error may interrupt
** the composition of a warning, leaving it unfinished.
*/
static int luaB_warn (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
luaL_checkstring(L, 1); /* at least one argument */
for (i = 2; i <= n; i++)
luaL_checkstring(L, i); /* make sure all arguments are strings */
for (i = 1; i < n; i++) /* compose warning */
lua_warning(L, lua_tostring(L, i), 1);
lua_warning(L, lua_tostring(L, n), 0); /* close warning */
return 0;
}
#define SPACECHARS " \f\n\r\t\v"
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
lua_Unsigned n = 0;
int neg = 0;
s += strspn(s, SPACECHARS); /* skip initial spaces */
if (*s == '-') { s++; neg = 1; } /* handle sign */
else if (*s == '+') s++;
if (!isalnum((unsigned char)*s)) /* no digit? */
return NULL;
do {
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
: (toupper((unsigned char)*s) - 'A') + 10;
if (digit >= base) return NULL; /* invalid numeral */
n = n * base + digit;
s++;
} while (isalnum((unsigned char)*s));
s += strspn(s, SPACECHARS); /* skip trailing spaces */
*pn = (lua_Integer)((neg) ? (0u - n) : n);
return s;
}
static int luaB_tonumber (lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion? */
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
lua_settop(L, 1); /* yes; return it */
return 1;
}
else {
size_t l;
const char *s = lua_tolstring(L, 1, &l);
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
return 1; /* successful conversion to number */
/* else not a number */
luaL_checkany(L, 1); /* (but there must be some parameter) */
}
}
else {
size_t l;
const char *s;
lua_Integer n = 0; /* to avoid warnings */
lua_Integer base = luaL_checkinteger(L, 2);
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
s = lua_tolstring(L, 1, &l);
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
if (b_str2int(s, (int)base, &n) == s + l) {
lua_pushinteger(L, n);
return 1;
} /* else not a number */
} /* else not a number */
luaL_pushfail(L); /* not a number */
return 1;
}
static int luaB_error (lua_State *L) {
int level = (int)luaL_optinteger(L, 2, 1);
lua_settop(L, 1);
if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
luaL_where(L, level); /* add extra information */
lua_pushvalue(L, 1);
lua_concat(L, 2);
}
return lua_error(L);
}
static int luaB_getmetatable (lua_State *L) {
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L);
return 1; /* no metatable */
}
luaL_getmetafield(L, 1, "__metatable");
return 1; /* returns either __metatable field (if present) or metatable */
}
static int luaB_setmetatable (lua_State *L) {
int t = lua_type(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL))
return luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2);
lua_setmetatable(L, 1);
return 1;
}
static int luaB_rawequal (lua_State *L) {
luaL_checkany(L, 1);
luaL_checkany(L, 2);
lua_pushboolean(L, lua_rawequal(L, 1, 2));
return 1;
}
static int luaB_rawlen (lua_State *L) {
int t = lua_type(L, 1);
luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
"table or string");
lua_pushinteger(L, lua_rawlen(L, 1));
return 1;
}
static int luaB_rawget (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_settop(L, 2);
lua_rawget(L, 1);
return 1;
}
static int luaB_rawset (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_settop(L, 3);
lua_rawset(L, 1);
return 1;
}
static int pushmode (lua_State *L, int oldmode) {
if (oldmode == -1)
luaL_pushfail(L); /* invalid call to 'lua_gc' */
else
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
: "generational");
return 1;
}
/*
** check whether call to 'lua_gc' was valid (not inside a finalizer)
*/
#define checkvalres(res) { if (res == -1) break; }
static int luaB_collectgarbage (lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul",
"isrunning", "generational", "incremental", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
switch (o) {
case LUA_GCCOUNT: {
int k = lua_gc(L, o);
int b = lua_gc(L, LUA_GCCOUNTB);
checkvalres(k);
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
return 1;
}
case LUA_GCSTEP: {
int step = (int)luaL_optinteger(L, 2, 0);
int res = lua_gc(L, o, step);
checkvalres(res);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCSETPAUSE:
case LUA_GCSETSTEPMUL: {
int p = (int)luaL_optinteger(L, 2, 0);
int previous = lua_gc(L, o, p);
checkvalres(previous);
lua_pushinteger(L, previous);
return 1;
}
case LUA_GCISRUNNING: {
int res = lua_gc(L, o);
checkvalres(res);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCGEN: {
int minormul = (int)luaL_optinteger(L, 2, 0);
int majormul = (int)luaL_optinteger(L, 3, 0);
return pushmode(L, lua_gc(L, o, minormul, majormul));
}
case LUA_GCINC: {
int pause = (int)luaL_optinteger(L, 2, 0);
int stepmul = (int)luaL_optinteger(L, 3, 0);
int stepsize = (int)luaL_optinteger(L, 4, 0);
return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
}
default: {
int res = lua_gc(L, o);
checkvalres(res);
lua_pushinteger(L, res);
return 1;
}
}
luaL_pushfail(L); /* invalid call (inside a finalizer) */
return 1;
}
static int luaB_type (lua_State *L) {
int t = lua_type(L, 1);
luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
lua_pushstring(L, lua_typename(L, t));
return 1;
}
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
else {
lua_pushnil(L);
return 1;
}
}
static int pairscont (lua_State *L, int status, lua_KContext k) {
(void)L; (void)status; (void)k; /* unused */
return 3;
}
static int luaB_pairs (lua_State *L) {
luaL_checkany(L, 1);
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
lua_pushcfunction(L, luaB_next); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
}
else {
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
}
return 3;
}
/*
** Traversal function for 'ipairs'
*/
static int ipairsaux (lua_State *L) {
lua_Integer i = luaL_checkinteger(L, 2);
i = luaL_intop(+, i, 1);
lua_pushinteger(L, i);
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
}
/*
** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
** (The given "table" may not be a table.)
*/
static int luaB_ipairs (lua_State *L) {
luaL_checkany(L, 1);
lua_pushcfunction(L, ipairsaux); /* iteration function */
lua_pushvalue(L, 1); /* state */
lua_pushinteger(L, 0); /* initial value */
return 3;
}
static int load_aux (lua_State *L, int status, int envidx) {
if (l_likely(status == LUA_OK)) {
if (envidx != 0) { /* 'env' parameter? */
lua_pushvalue(L, envidx); /* environment for loaded function */
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
lua_pop(L, 1); /* remove 'env' if not used by previous call */
}
return 1;
}
else { /* error (message is on top of the stack) */
luaL_pushfail(L);
lua_insert(L, -2); /* put before error message */
return 2; /* return fail plus error message */
}
}
static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = luaL_optstring(L, 2, NULL);
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
int status = luaL_loadfilex(L, fname, mode);
return load_aux(L, status, env);
}
/*
** {======================================================
** Generic Read function
** =======================================================
*/
/*
** reserved slot, above all arguments, to hold a copy of the returned
** string to avoid it being collected while parsed. 'load' has four
** optional arguments (chunk, source name, mode, and environment).
*/
#define RESERVEDSLOT 5
/*
** Reader for generic 'load' function: 'lua_load' uses the
** stack for internal stuff, so the reader cannot change the
** stack top. Instead, it keeps its resulting string in a
** reserved slot inside the stack.
*/
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
(void)(ud); /* not used */
luaL_checkstack(L, 2, "too many nested functions");
lua_pushvalue(L, 1); /* get function */
lua_call(L, 0, 1); /* call it */
if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* pop result */
*size = 0;
return NULL;
}
else if (l_unlikely(!lua_isstring(L, -1)))
luaL_error(L, "reader function must return a string");
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
return lua_tolstring(L, RESERVEDSLOT, size);
}
static int luaB_load (lua_State *L) {
int status;
size_t l;
const char *s = lua_tolstring(L, 1, &l);
const char *mode = luaL_optstring(L, 3, "bt");
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s);
status = luaL_loadbufferx(L, s, l, chunkname, mode);
}
else { /* loading from a reader function */
const char *chunkname = luaL_optstring(L, 2, "=(load)");
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
status = lua_load(L, generic_reader, NULL, chunkname, mode);
}
return load_aux(L, status, env);
}
/* }====================================================== */
static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
(void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
return lua_gettop(L) - 1;
}
static int luaB_dofile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1);
if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
return lua_error(L);
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
return dofilecont(L, 0, 0);
}
static int luaB_assert (lua_State *L) {
if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
return lua_gettop(L); /* return all arguments */
else { /* error */
luaL_checkany(L, 1); /* there must be a condition */
lua_remove(L, 1); /* remove it */
lua_pushliteral(L, "assertion failed!"); /* default message */
lua_settop(L, 1); /* leave only message (default if no other one) */
return luaB_error(L); /* call 'error' */
}
}
static int luaB_select (lua_State *L) {
int n = lua_gettop(L);
if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
lua_pushinteger(L, n-1);
return 1;
}
else {
lua_Integer i = luaL_checkinteger(L, 1);
if (i < 0) i = n + i;
else if (i > n) i = n;
luaL_argcheck(L, 1 <= i, 1, "index out of range");
return n - (int)i;
}
}
/*
** Continuation function for 'pcall' and 'xpcall'. Both functions
** already pushed a 'true' before doing the call, so in case of success
** 'finishpcall' only has to return everything in the stack minus
** 'extra' values (where 'extra' is exactly the number of items to be
** ignored).
*/
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */
lua_pushboolean(L, 0); /* first result (false) */
lua_pushvalue(L, -2); /* error message */
return 2; /* return false, msg */
}
else
return lua_gettop(L) - (int)extra; /* return all results */
}
static int luaB_pcall (lua_State *L) {
int status;
luaL_checkany(L, 1);
lua_pushboolean(L, 1); /* first result if no errors */
lua_insert(L, 1); /* put it in place */
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
return finishpcall(L, status, 0);
}
/*
** Do a protected call with error handling. After 'lua_rotate', the
** stack will have <f, err, true, f, [args...]>; so, the function passes
** 2 to 'finishpcall' to skip the 2 first values when returning results.
*/
static int luaB_xpcall (lua_State *L) {
int status;
int n = lua_gettop(L);
luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
lua_pushboolean(L, 1); /* first result */
lua_pushvalue(L, 1); /* function */
lua_rotate(L, 3, 2); /* move them below function's arguments */
status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
return finishpcall(L, status, 2);
}
static int luaB_tostring (lua_State *L) {
luaL_checkany(L, 1);
luaL_tolstring(L, 1, NULL);
return 1;
}
static const luaL_Reg base_funcs[] = {
{"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage},
{"dofile", luaB_dofile},
{"error", luaB_error},
{"getmetatable", luaB_getmetatable},
{"ipairs", luaB_ipairs},
{"loadfile", luaB_loadfile},
{"load", luaB_load},
{"next", luaB_next},
{"pairs", luaB_pairs},
{"pcall", luaB_pcall},
{"print", luaB_print},
{"warn", luaB_warn},
{"rawequal", luaB_rawequal},
{"rawlen", luaB_rawlen},
{"rawget", luaB_rawget},
{"rawset", luaB_rawset},
{"select", luaB_select},
{"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
{"xpcall", luaB_xpcall},
/* placeholders */
{LUA_GNAME, NULL},
{"_VERSION", NULL},
{NULL, NULL}
};
LUAMOD_API int luaopen_base (lua_State *L) {
/* open lib into global table */
lua_pushglobaltable(L);
luaL_setfuncs(L, base_funcs, 0);
/* set global _G */
lua_pushvalue(L, -1);
lua_setfield(L, -2, LUA_GNAME);
/* set global _VERSION */
lua_pushliteral(L, LUA_VERSION);
lua_setfield(L, -2, "_VERSION");
return 1;
}

1874
arm9/source/lua/lcode.c Normal file

File diff suppressed because it is too large Load Diff

101
arm9/source/lua/lcode.h Normal file
View File

@ -0,0 +1,101 @@
/*
** $Id: lcode.h $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
#ifndef lcode_h
#define lcode_h
#include "llex.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
/*
** Marks the end of a patch list. It is an invalid value both as an absolute
** address, and as a list link (would link an element to itself).
*/
#define NO_JUMP (-1)
/*
** grep "ORDER OPR" if you change these enums (ORDER OP)
*/
typedef enum BinOpr {
/* arithmetic operators */
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
OPR_DIV, OPR_IDIV,
/* bitwise operators */
OPR_BAND, OPR_BOR, OPR_BXOR,
OPR_SHL, OPR_SHR,
/* string operator */
OPR_CONCAT,
/* comparison operators */
OPR_EQ, OPR_LT, OPR_LE,
OPR_NE, OPR_GT, OPR_GE,
/* logical operators */
OPR_AND, OPR_OR,
OPR_NOBINOPR
} BinOpr;
/* true if operation is foldable (that is, it is arithmetic or bitwise) */
#define foldbinop(op) ((op) <= OPR_SHR)
#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
/* get (pointer to) instruction of given 'expdesc' */
#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
int B, int C, int k);
LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_jump (FuncState *fs);
LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
LUAI_FUNC int luaK_getlabel (FuncState *fs);
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
expdesc *v2, int line);
LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc,
int ra, int asize, int hsize);
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
LUAI_FUNC void luaK_finish (FuncState *fs);
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg);
#endif

210
arm9/source/lua/lcorolib.c Normal file
View File

@ -0,0 +1,210 @@
/*
** $Id: lcorolib.c $
** Coroutine Library
** See Copyright Notice in lua.h
*/
#define lcorolib_c
#define LUA_LIB
#include "lprefix.h"
#include <stdlib.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static lua_State *getco (lua_State *L) {
lua_State *co = lua_tothread(L, 1);
luaL_argexpected(L, co, 1, "thread");
return co;
}
/*
** Resumes a coroutine. Returns the number of results for non-error
** cases or -1 for errors.
*/
static int auxresume (lua_State *L, lua_State *co, int narg) {
int status, nres;
if (l_unlikely(!lua_checkstack(co, narg))) {
lua_pushliteral(L, "too many arguments to resume");
return -1; /* error flag */
}
lua_xmove(L, co, narg);
status = lua_resume(co, L, narg, &nres);
if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
if (l_unlikely(!lua_checkstack(L, nres + 1))) {
lua_pop(co, nres); /* remove results anyway */
lua_pushliteral(L, "too many results to resume");
return -1; /* error flag */
}
lua_xmove(co, L, nres); /* move yielded values */
return nres;
}
else {
lua_xmove(co, L, 1); /* move error message */
return -1; /* error flag */
}
}
static int luaB_coresume (lua_State *L) {
lua_State *co = getco(L);
int r;
r = auxresume(L, co, lua_gettop(L) - 1);
if (l_unlikely(r < 0)) {
lua_pushboolean(L, 0);
lua_insert(L, -2);
return 2; /* return false + error message */
}
else {
lua_pushboolean(L, 1);
lua_insert(L, -(r + 1));
return r + 1; /* return true + 'resume' returns */
}
}
static int luaB_auxwrap (lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L));
if (l_unlikely(r < 0)) { /* error? */
int stat = lua_status(co);
if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
stat = lua_closethread(co, L); /* close its tbc variables */
lua_assert(stat != LUA_OK);
lua_xmove(co, L, 1); /* move error message to the caller */
}
if (stat != LUA_ERRMEM && /* not a memory error and ... */
lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
luaL_where(L, 1); /* add extra info, if available */
lua_insert(L, -2);
lua_concat(L, 2);
}
return lua_error(L); /* propagate error */
}
return r;
}
static int luaB_cocreate (lua_State *L) {
lua_State *NL;
luaL_checktype(L, 1, LUA_TFUNCTION);
NL = lua_newthread(L);
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
return 1;
}
static int luaB_cowrap (lua_State *L) {
luaB_cocreate(L);
lua_pushcclosure(L, luaB_auxwrap, 1);
return 1;
}
static int luaB_yield (lua_State *L) {
return lua_yield(L, lua_gettop(L));
}
#define COS_RUN 0
#define COS_DEAD 1
#define COS_YIELD 2
#define COS_NORM 3
static const char *const statname[] =
{"running", "dead", "suspended", "normal"};
static int auxstatus (lua_State *L, lua_State *co) {
if (L == co) return COS_RUN;
else {
switch (lua_status(co)) {
case LUA_YIELD:
return COS_YIELD;
case LUA_OK: {
lua_Debug ar;
if (lua_getstack(co, 0, &ar)) /* does it have frames? */
return COS_NORM; /* it is running */
else if (lua_gettop(co) == 0)
return COS_DEAD;
else
return COS_YIELD; /* initial state */
}
default: /* some error occurred */
return COS_DEAD;
}
}
}
static int luaB_costatus (lua_State *L) {
lua_State *co = getco(L);
lua_pushstring(L, statname[auxstatus(L, co)]);
return 1;
}
static int luaB_yieldable (lua_State *L) {
lua_State *co = lua_isnone(L, 1) ? L : getco(L);
lua_pushboolean(L, lua_isyieldable(co));
return 1;
}
static int luaB_corunning (lua_State *L) {
int ismain = lua_pushthread(L);
lua_pushboolean(L, ismain);
return 2;
}
static int luaB_close (lua_State *L) {
lua_State *co = getco(L);
int status = auxstatus(L, co);
switch (status) {
case COS_DEAD: case COS_YIELD: {
status = lua_closethread(co, L);
if (status == LUA_OK) {
lua_pushboolean(L, 1);
return 1;
}
else {
lua_pushboolean(L, 0);
lua_xmove(co, L, 1); /* move error message */
return 2;
}
}
default: /* normal or running coroutine */
return luaL_error(L, "cannot close a %s coroutine", statname[status]);
}
}
static const luaL_Reg co_funcs[] = {
{"create", luaB_cocreate},
{"resume", luaB_coresume},
{"running", luaB_corunning},
{"status", luaB_costatus},
{"wrap", luaB_cowrap},
{"yield", luaB_yield},
{"isyieldable", luaB_yieldable},
{"close", luaB_close},
{NULL, NULL}
};
LUAMOD_API int luaopen_coroutine (lua_State *L) {
luaL_newlib(L, co_funcs);
return 1;
}

64
arm9/source/lua/lctype.c Normal file
View File

@ -0,0 +1,64 @@
/*
** $Id: lctype.c $
** 'ctype' functions for Lua
** See Copyright Notice in lua.h
*/
#define lctype_c
#define LUA_CORE
#include "lprefix.h"
#include "lctype.h"
#if !LUA_USE_CTYPE /* { */
#include <limits.h>
#if defined (LUA_UCID) /* accept UniCode IDentifiers? */
/* consider all non-ascii codepoints to be alphabetic */
#define NONA 0x01
#else
#define NONA 0x00 /* default */
#endif
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
0x00, /* EOZ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#endif /* } */

101
arm9/source/lua/lctype.h Normal file
View File

@ -0,0 +1,101 @@
/*
** $Id: lctype.h $
** 'ctype' functions for Lua
** See Copyright Notice in lua.h
*/
#ifndef lctype_h
#define lctype_h
#include "lua.h"
/*
** WARNING: the functions defined here do not necessarily correspond
** to the similar functions in the standard C ctype.h. They are
** optimized for the specific needs of Lua.
*/
#if !defined(LUA_USE_CTYPE)
#if 'A' == 65 && '0' == 48
/* ASCII case: can use its own tables; faster and fixed */
#define LUA_USE_CTYPE 0
#else
/* must use standard C ctype */
#define LUA_USE_CTYPE 1
#endif
#endif
#if !LUA_USE_CTYPE /* { */
#include <limits.h>
#include "llimits.h"
#define ALPHABIT 0
#define DIGITBIT 1
#define PRINTBIT 2
#define SPACEBIT 3
#define XDIGITBIT 4
#define MASK(B) (1 << (B))
/*
** add 1 to char to allow index -1 (EOZ)
*/
#define testprop(c,p) (luai_ctype_[(c)+1] & (p))
/*
** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
*/
#define lislalpha(c) testprop(c, MASK(ALPHABIT))
#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
#define lisdigit(c) testprop(c, MASK(DIGITBIT))
#define lisspace(c) testprop(c, MASK(SPACEBIT))
#define lisprint(c) testprop(c, MASK(PRINTBIT))
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
/*
** In ASCII, this 'ltolower' is correct for alphabetic characters and
** for '.'. That is enough for Lua needs. ('check_exp' ensures that
** the character either is an upper-case letter or is unchanged by
** the transformation, which holds for lower-case letters and '.'.)
*/
#define ltolower(c) \
check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \
(c) | ('A' ^ 'a'))
/* one entry for each character and for -1 (EOZ) */
LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];)
#else /* }{ */
/*
** use standard C ctypes
*/
#include <ctype.h>
#define lislalpha(c) (isalpha(c) || (c) == '_')
#define lislalnum(c) (isalnum(c) || (c) == '_')
#define lisdigit(c) (isdigit(c))
#define lisspace(c) (isspace(c))
#define lisprint(c) (isprint(c))
#define lisxdigit(c) (isxdigit(c))
#define ltolower(c) (tolower(c))
#endif /* } */
#endif

483
arm9/source/lua/ldblib.c Normal file
View File

@ -0,0 +1,483 @@
/*
** $Id: ldblib.c $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
#define ldblib_c
#define LUA_LIB
#include "lprefix.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/*
** The hook table at registry[HOOKKEY] maps threads to their current
** hook function.
*/
static const char *const HOOKKEY = "_HOOKKEY";
/*
** If L1 != L, L1 can be in any state, and therefore there are no
** guarantees about its stack space; any push in L1 must be
** checked.
*/
static void checkstack (lua_State *L, lua_State *L1, int n) {
if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
luaL_error(L, "stack overflow");
}
static int db_getregistry (lua_State *L) {
lua_pushvalue(L, LUA_REGISTRYINDEX);
return 1;
}
static int db_getmetatable (lua_State *L) {
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L); /* no metatable */
}
return 1;
}
static int db_setmetatable (lua_State *L) {
int t = lua_type(L, 2);
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
lua_settop(L, 2);
lua_setmetatable(L, 1);
return 1; /* return 1st argument */
}
static int db_getuservalue (lua_State *L) {
int n = (int)luaL_optinteger(L, 2, 1);
if (lua_type(L, 1) != LUA_TUSERDATA)
luaL_pushfail(L);
else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
lua_pushboolean(L, 1);
return 2;
}
return 1;
}
static int db_setuservalue (lua_State *L) {
int n = (int)luaL_optinteger(L, 3, 1);
luaL_checktype(L, 1, LUA_TUSERDATA);
luaL_checkany(L, 2);
lua_settop(L, 2);
if (!lua_setiuservalue(L, 1, n))
luaL_pushfail(L);
return 1;
}
/*
** Auxiliary function used by several library functions: check for
** an optional thread as function's first argument and set 'arg' with
** 1 if this argument is present (so that functions can skip it to
** access their other arguments)
*/
static lua_State *getthread (lua_State *L, int *arg) {
if (lua_isthread(L, 1)) {
*arg = 1;
return lua_tothread(L, 1);
}
else {
*arg = 0;
return L; /* function will operate over current thread */
}
}
/*
** Variations of 'lua_settable', used by 'db_getinfo' to put results
** from 'lua_getinfo' into result table. Key is always a string;
** value can be a string, an int, or a boolean.
*/
static void settabss (lua_State *L, const char *k, const char *v) {
lua_pushstring(L, v);
lua_setfield(L, -2, k);
}
static void settabsi (lua_State *L, const char *k, int v) {
lua_pushinteger(L, v);
lua_setfield(L, -2, k);
}
static void settabsb (lua_State *L, const char *k, int v) {
lua_pushboolean(L, v);
lua_setfield(L, -2, k);
}
/*
** In function 'db_getinfo', the call to 'lua_getinfo' may push
** results on the stack; later it creates the result table to put
** these objects. Function 'treatstackoption' puts the result from
** 'lua_getinfo' on top of the result table so that it can call
** 'lua_setfield'.
*/
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
if (L == L1)
lua_rotate(L, -2, 1); /* exchange object and table */
else
lua_xmove(L1, L, 1); /* move object to the "main" stack */
lua_setfield(L, -2, fname); /* put object into table */
}
/*
** Calls 'lua_getinfo' and collects all results in a new table.
** L1 needs stack space for an optional input (function) plus
** two optional outputs (function and line table) from function
** 'lua_getinfo'.
*/
static int db_getinfo (lua_State *L) {
lua_Debug ar;
int arg;
lua_State *L1 = getthread(L, &arg);
const char *options = luaL_optstring(L, arg+2, "flnSrtu");
checkstack(L, L1, 3);
luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
lua_xmove(L, L1, 1);
}
else { /* stack level */
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
luaL_pushfail(L); /* level out of range */
return 1;
}
}
if (!lua_getinfo(L1, options, &ar))
return luaL_argerror(L, arg+2, "invalid option");
lua_newtable(L); /* table to collect results */
if (strchr(options, 'S')) {
lua_pushlstring(L, ar.source, ar.srclen);
lua_setfield(L, -2, "source");
settabss(L, "short_src", ar.short_src);
settabsi(L, "linedefined", ar.linedefined);
settabsi(L, "lastlinedefined", ar.lastlinedefined);
settabss(L, "what", ar.what);
}
if (strchr(options, 'l'))
settabsi(L, "currentline", ar.currentline);
if (strchr(options, 'u')) {
settabsi(L, "nups", ar.nups);
settabsi(L, "nparams", ar.nparams);
settabsb(L, "isvararg", ar.isvararg);
}
if (strchr(options, 'n')) {
settabss(L, "name", ar.name);
settabss(L, "namewhat", ar.namewhat);
}
if (strchr(options, 'r')) {
settabsi(L, "ftransfer", ar.ftransfer);
settabsi(L, "ntransfer", ar.ntransfer);
}
if (strchr(options, 't'))
settabsb(L, "istailcall", ar.istailcall);
if (strchr(options, 'L'))
treatstackoption(L, L1, "activelines");
if (strchr(options, 'f'))
treatstackoption(L, L1, "func");
return 1; /* return table */
}
static int db_getlocal (lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
if (lua_isfunction(L, arg + 1)) { /* function argument? */
lua_pushvalue(L, arg + 1); /* push function */
lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
return 1; /* return only name (there is no value) */
}
else { /* stack-level argument */
lua_Debug ar;
const char *name;
int level = (int)luaL_checkinteger(L, arg + 1);
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
return luaL_argerror(L, arg+1, "level out of range");
checkstack(L, L1, 1);
name = lua_getlocal(L1, &ar, nvar);
if (name) {
lua_xmove(L1, L, 1); /* move local value */
lua_pushstring(L, name); /* push name */
lua_rotate(L, -2, 1); /* re-order */
return 2;
}
else {
luaL_pushfail(L); /* no name (nor value) */
return 1;
}
}
}
static int db_setlocal (lua_State *L) {
int arg;
const char *name;
lua_State *L1 = getthread(L, &arg);
lua_Debug ar;
int level = (int)luaL_checkinteger(L, arg + 1);
int nvar = (int)luaL_checkinteger(L, arg + 2);
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
return luaL_argerror(L, arg+1, "level out of range");
luaL_checkany(L, arg+3);
lua_settop(L, arg+3);
checkstack(L, L1, 1);
lua_xmove(L, L1, 1);
name = lua_setlocal(L1, &ar, nvar);
if (name == NULL)
lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
lua_pushstring(L, name);
return 1;
}
/*
** get (if 'get' is true) or set an upvalue from a closure
*/
static int auxupvalue (lua_State *L, int get) {
const char *name;
int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
if (name == NULL) return 0;
lua_pushstring(L, name);
lua_insert(L, -(get+1)); /* no-op if get is false */
return get + 1;
}
static int db_getupvalue (lua_State *L) {
return auxupvalue(L, 1);
}
static int db_setupvalue (lua_State *L) {
luaL_checkany(L, 3);
return auxupvalue(L, 0);
}
/*
** Check whether a given upvalue from a given closure exists and
** returns its index
*/
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
void *id;
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
id = lua_upvalueid(L, argf, nup);
if (pnup) {
luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
*pnup = nup;
}
return id;
}
static int db_upvalueid (lua_State *L) {
void *id = checkupval(L, 1, 2, NULL);
if (id != NULL)
lua_pushlightuserdata(L, id);
else
luaL_pushfail(L);
return 1;
}
static int db_upvaluejoin (lua_State *L) {
int n1, n2;
checkupval(L, 1, 2, &n1);
checkupval(L, 3, 4, &n2);
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
lua_upvaluejoin(L, 1, n1, 3, n2);
return 0;
}
/*
** Call hook function registered at hook table for the current
** thread (if there is one)
*/
static void hookf (lua_State *L, lua_Debug *ar) {
static const char *const hooknames[] =
{"call", "return", "line", "count", "tail call"};
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
lua_pushthread(L);
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
if (ar->currentline >= 0)
lua_pushinteger(L, ar->currentline); /* push current line */
else lua_pushnil(L);
lua_assert(lua_getinfo(L, "lS", ar));
lua_call(L, 2, 0); /* call hook function */
}
}
/*
** Convert a string mask (for 'sethook') into a bit mask
*/
static int makemask (const char *smask, int count) {
int mask = 0;
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
if (count > 0) mask |= LUA_MASKCOUNT;
return mask;
}
/*
** Convert a bit mask (for 'gethook') into a string mask
*/
static char *unmakemask (int mask, char *smask) {
int i = 0;
if (mask & LUA_MASKCALL) smask[i++] = 'c';
if (mask & LUA_MASKRET) smask[i++] = 'r';
if (mask & LUA_MASKLINE) smask[i++] = 'l';
smask[i] = '\0';
return smask;
}
static int db_sethook (lua_State *L) {
int arg, mask, count;
lua_Hook func;
lua_State *L1 = getthread(L, &arg);
if (lua_isnoneornil(L, arg+1)) { /* no hook? */
lua_settop(L, arg+1);
func = NULL; mask = 0; count = 0; /* turn off hooks */
}
else {
const char *smask = luaL_checkstring(L, arg+2);
luaL_checktype(L, arg+1, LUA_TFUNCTION);
count = (int)luaL_optinteger(L, arg + 3, 0);
func = hookf; mask = makemask(smask, count);
}
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
/* table just created; initialize it */
lua_pushliteral(L, "k");
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
lua_pushvalue(L, -1);
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
}
checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
lua_pushvalue(L, arg + 1); /* value (hook function) */
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
lua_sethook(L1, func, mask, count);
return 0;
}
static int db_gethook (lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
char buff[5];
int mask = lua_gethookmask(L1);
lua_Hook hook = lua_gethook(L1);
if (hook == NULL) { /* no hook? */
luaL_pushfail(L);
return 1;
}
else if (hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook");
else { /* hook table must exist */
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1);
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
lua_remove(L, -2); /* remove hook table */
}
lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
return 3;
}
static int db_debug (lua_State *L) {
for (;;) {
char buffer[250];
lua_writestringerror("%s", "lua_debug> ");
if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
strcmp(buffer, "cont\n") == 0)
return 0;
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
lua_pcall(L, 0, 0, 0))
lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
lua_settop(L, 0); /* remove eventual returns */
}
}
static int db_traceback (lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
const char *msg = lua_tostring(L, arg + 1);
if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
lua_pushvalue(L, arg + 1); /* return it untouched */
else {
int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
luaL_traceback(L, L1, msg, level);
}
return 1;
}
static int db_setcstacklimit (lua_State *L) {
int limit = (int)luaL_checkinteger(L, 1);
int res = lua_setcstacklimit(L, limit);
lua_pushinteger(L, res);
return 1;
}
static const luaL_Reg dblib[] = {
{"debug", db_debug},
{"getuservalue", db_getuservalue},
{"gethook", db_gethook},
{"getinfo", db_getinfo},
{"getlocal", db_getlocal},
{"getregistry", db_getregistry},
{"getmetatable", db_getmetatable},
{"getupvalue", db_getupvalue},
{"upvaluejoin", db_upvaluejoin},
{"upvalueid", db_upvalueid},
{"setuservalue", db_setuservalue},
{"sethook", db_sethook},
{"setlocal", db_setlocal},
{"setmetatable", db_setmetatable},
{"setupvalue", db_setupvalue},
{"traceback", db_traceback},
{"setcstacklimit", db_setcstacklimit},
{NULL, NULL}
};
LUAMOD_API int luaopen_debug (lua_State *L) {
luaL_newlib(L, dblib);
return 1;
}

962
arm9/source/lua/ldebug.c Normal file
View File

@ -0,0 +1,962 @@
/*
** $Id: ldebug.c $
** Debug Interface
** See Copyright Notice in lua.h
*/
#define ldebug_c
#define LUA_CORE
#include "lprefix.h"
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include "lua.h"
#include "lapi.h"
#include "lcode.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvm.h"
#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL)
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
const char **name);
static int currentpc (CallInfo *ci) {
lua_assert(isLua(ci));
return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
}
/*
** Get a "base line" to find the line corresponding to an instruction.
** Base lines are regularly placed at MAXIWTHABS intervals, so usually
** an integer division gets the right place. When the source file has
** large sequences of empty/comment lines, it may need extra entries,
** so the original estimate needs a correction.
** If the original estimate is -1, the initial 'if' ensures that the
** 'while' will run at least once.
** The assertion that the estimate is a lower bound for the correct base
** is valid as long as the debug info has been generated with the same
** value for MAXIWTHABS or smaller. (Previous releases use a little
** smaller value.)
*/
static int getbaseline (const Proto *f, int pc, int *basepc) {
if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
*basepc = -1; /* start from the beginning */
return f->linedefined;
}
else {
int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
/* estimate must be a lower bound of the correct base */
lua_assert(i < 0 ||
(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
i++; /* low estimate; adjust it */
*basepc = f->abslineinfo[i].pc;
return f->abslineinfo[i].line;
}
}
/*
** Get the line corresponding to instruction 'pc' in function 'f';
** first gets a base line and from there does the increments until
** the desired instruction.
*/
int luaG_getfuncline (const Proto *f, int pc) {
if (f->lineinfo == NULL) /* no debug information? */
return -1;
else {
int basepc;
int baseline = getbaseline(f, pc, &basepc);
while (basepc++ < pc) { /* walk until given instruction */
lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
baseline += f->lineinfo[basepc]; /* correct line */
}
return baseline;
}
}
static int getcurrentline (CallInfo *ci) {
return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
}
/*
** Set 'trap' for all active Lua frames.
** This function can be called during a signal, under "reasonable"
** assumptions. A new 'ci' is completely linked in the list before it
** becomes part of the "active" list, and we assume that pointers are
** atomic; see comment in next function.
** (A compiler doing interprocedural optimizations could, theoretically,
** reorder memory writes in such a way that the list could be
** temporarily broken while inserting a new element. We simply assume it
** has no good reasons to do that.)
*/
static void settraps (CallInfo *ci) {
for (; ci != NULL; ci = ci->previous)
if (isLua(ci))
ci->u.l.trap = 1;
}
/*
** This function can be called during a signal, under "reasonable"
** assumptions.
** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount')
** are for debug only, and it is no problem if they get arbitrary
** values (causes at most one wrong hook call). 'hookmask' is an atomic
** value. We assume that pointers are atomic too (e.g., gcc ensures that
** for all platforms where it runs). Moreover, 'hook' is always checked
** before being called (see 'luaD_hook').
*/
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
if (func == NULL || mask == 0) { /* turn off hooks? */
mask = 0;
func = NULL;
}
L->hook = func;
L->basehookcount = count;
resethookcount(L);
L->hookmask = cast_byte(mask);
if (mask)
settraps(L->ci); /* to trace inside 'luaV_execute' */
}
LUA_API lua_Hook lua_gethook (lua_State *L) {
return L->hook;
}
LUA_API int lua_gethookmask (lua_State *L) {
return L->hookmask;
}
LUA_API int lua_gethookcount (lua_State *L) {
return L->basehookcount;
}
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
int status;
CallInfo *ci;
if (level < 0) return 0; /* invalid (negative) level */
lua_lock(L);
for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
level--;
if (level == 0 && ci != &L->base_ci) { /* level found? */
status = 1;
ar->i_ci = ci;
}
else status = 0; /* no such level */
lua_unlock(L);
return status;
}
static const char *upvalname (const Proto *p, int uv) {
TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
if (s == NULL) return "?";
else return getstr(s);
}
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
int nextra = ci->u.l.nextraargs;
if (n >= -nextra) { /* 'n' is negative */
*pos = ci->func.p - nextra - (n + 1);
return "(vararg)"; /* generic name for any vararg */
}
}
return NULL; /* no such vararg */
}
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
StkId base = ci->func.p + 1;
const char *name = NULL;
if (isLua(ci)) {
if (n < 0) /* access to vararg values? */
return findvararg(ci, n, pos);
else
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
}
if (name == NULL) { /* no 'standard' name? */
StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
/* generic name for any valid slot */
name = isLua(ci) ? "(temporary)" : "(C temporary)";
}
else
return NULL; /* no name */
}
if (pos)
*pos = base + (n - 1);
return name;
}
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
const char *name;
lua_lock(L);
if (ar == NULL) { /* information about non-active function? */
if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */
name = NULL;
else /* consider live variables at function start (parameters) */
name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0);
}
else { /* active function; get information through 'ar' */
StkId pos = NULL; /* to avoid warnings */
name = luaG_findlocal(L, ar->i_ci, n, &pos);
if (name) {
setobjs2s(L, L->top.p, pos);
api_incr_top(L);
}
}
lua_unlock(L);
return name;
}
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
StkId pos = NULL; /* to avoid warnings */
const char *name;
lua_lock(L);
name = luaG_findlocal(L, ar->i_ci, n, &pos);
if (name) {
setobjs2s(L, pos, L->top.p - 1);
L->top.p--; /* pop value */
}
lua_unlock(L);
return name;
}
static void funcinfo (lua_Debug *ar, Closure *cl) {
if (!LuaClosure(cl)) {
ar->source = "=[C]";
ar->srclen = LL("=[C]");
ar->linedefined = -1;
ar->lastlinedefined = -1;
ar->what = "C";
}
else {
const Proto *p = cl->l.p;
if (p->source) {
ar->source = getstr(p->source);
ar->srclen = tsslen(p->source);
}
else {
ar->source = "=?";
ar->srclen = LL("=?");
}
ar->linedefined = p->linedefined;
ar->lastlinedefined = p->lastlinedefined;
ar->what = (ar->linedefined == 0) ? "main" : "Lua";
}
luaO_chunkid(ar->short_src, ar->source, ar->srclen);
}
static int nextline (const Proto *p, int currentline, int pc) {
if (p->lineinfo[pc] != ABSLINEINFO)
return currentline + p->lineinfo[pc];
else
return luaG_getfuncline(p, pc);
}
static void collectvalidlines (lua_State *L, Closure *f) {
if (!LuaClosure(f)) {
setnilvalue(s2v(L->top.p));
api_incr_top(L);
}
else {
const Proto *p = f->l.p;
int currentline = p->linedefined;
Table *t = luaH_new(L); /* new table to store active lines */
sethvalue2s(L, L->top.p, t); /* push it on stack */
api_incr_top(L);
if (p->lineinfo != NULL) { /* proto with debug information? */
int i;
TValue v;
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
if (!p->is_vararg) /* regular function? */
i = 0; /* consider all instructions */
else { /* vararg function */
lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
currentline = nextline(p, currentline, 0);
i = 1; /* skip first instruction (OP_VARARGPREP) */
}
for (; i < p->sizelineinfo; i++) { /* for each instruction */
currentline = nextline(p, currentline, i); /* get its line */
luaH_setint(L, t, currentline, &v); /* table[line] = true */
}
}
}
}
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
/* calling function is a known function? */
if (ci != NULL && !(ci->callstatus & CIST_TAIL))
return funcnamefromcall(L, ci->previous, name);
else return NULL; /* no way to find a name */
}
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
Closure *f, CallInfo *ci) {
int status = 1;
for (; *what; what++) {
switch (*what) {
case 'S': {
funcinfo(ar, f);
break;
}
case 'l': {
ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
break;
}
case 'u': {
ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
if (!LuaClosure(f)) {
ar->isvararg = 1;
ar->nparams = 0;
}
else {
ar->isvararg = f->l.p->is_vararg;
ar->nparams = f->l.p->numparams;
}
break;
}
case 't': {
ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
break;
}
case 'n': {
ar->namewhat = getfuncname(L, ci, &ar->name);
if (ar->namewhat == NULL) {
ar->namewhat = ""; /* not found */
ar->name = NULL;
}
break;
}
case 'r': {
if (ci == NULL || !(ci->callstatus & CIST_TRAN))
ar->ftransfer = ar->ntransfer = 0;
else {
ar->ftransfer = ci->u2.transferinfo.ftransfer;
ar->ntransfer = ci->u2.transferinfo.ntransfer;
}
break;
}
case 'L':
case 'f': /* handled by lua_getinfo */
break;
default: status = 0; /* invalid option */
}
}
return status;
}
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
int status;
Closure *cl;
CallInfo *ci;
TValue *func;
lua_lock(L);
if (*what == '>') {
ci = NULL;
func = s2v(L->top.p - 1);
api_check(L, ttisfunction(func), "function expected");
what++; /* skip the '>' */
L->top.p--; /* pop function */
}
else {
ci = ar->i_ci;
func = s2v(ci->func.p);
lua_assert(ttisfunction(func));
}
cl = ttisclosure(func) ? clvalue(func) : NULL;
status = auxgetinfo(L, what, ar, cl, ci);
if (strchr(what, 'f')) {
setobj2s(L, L->top.p, func);
api_incr_top(L);
}
if (strchr(what, 'L'))
collectvalidlines(L, cl);
lua_unlock(L);
return status;
}
/*
** {======================================================
** Symbolic Execution
** =======================================================
*/
static int filterpc (int pc, int jmptarget) {
if (pc < jmptarget) /* is code conditional (inside a jump)? */
return -1; /* cannot know who sets that register */
else return pc; /* current position sets that register */
}
/*
** Try to find last instruction before 'lastpc' that modified register 'reg'.
*/
static int findsetreg (const Proto *p, int lastpc, int reg) {
int pc;
int setreg = -1; /* keep last instruction that changed 'reg' */
int jmptarget = 0; /* any code before this address is conditional */
if (testMMMode(GET_OPCODE(p->code[lastpc])))
lastpc--; /* previous instruction was not actually executed */
for (pc = 0; pc < lastpc; pc++) {
Instruction i = p->code[pc];
OpCode op = GET_OPCODE(i);
int a = GETARG_A(i);
int change; /* true if current instruction changed 'reg' */
switch (op) {
case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */
int b = GETARG_B(i);
change = (a <= reg && reg <= a + b);
break;
}
case OP_TFORCALL: { /* affect all regs above its base */
change = (reg >= a + 2);
break;
}
case OP_CALL:
case OP_TAILCALL: { /* affect all registers above base */
change = (reg >= a);
break;
}
case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */
int b = GETARG_sJ(i);
int dest = pc + 1 + b;
/* jump does not skip 'lastpc' and is larger than current one? */
if (dest <= lastpc && dest > jmptarget)
jmptarget = dest; /* update 'jmptarget' */
change = 0;
break;
}
default: /* any instruction that sets A */
change = (testAMode(op) && reg == a);
break;
}
if (change)
setreg = filterpc(pc, jmptarget);
}
return setreg;
}
/*
** Find a "name" for the constant 'c'.
*/
static const char *kname (const Proto *p, int index, const char **name) {
TValue *kvalue = &p->k[index];
if (ttisstring(kvalue)) {
*name = getstr(tsvalue(kvalue));
return "constant";
}
else {
*name = "?";
return NULL;
}
}
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
const char **name) {
int pc = *ppc;
*name = luaF_getlocalname(p, reg + 1, pc);
if (*name) /* is a local? */
return "local";
/* else try symbolic execution */
*ppc = pc = findsetreg(p, pc, reg);
if (pc != -1) { /* could find instruction? */
Instruction i = p->code[pc];
OpCode op = GET_OPCODE(i);
switch (op) {
case OP_MOVE: {
int b = GETARG_B(i); /* move from 'b' to 'a' */
if (b < GETARG_A(i))
return basicgetobjname(p, ppc, b, name); /* get name for 'b' */
break;
}
case OP_GETUPVAL: {
*name = upvalname(p, GETARG_B(i));
return "upvalue";
}
case OP_LOADK: return kname(p, GETARG_Bx(i), name);
case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
default: break;
}
}
return NULL; /* could not find reasonable name */
}
/*
** Find a "name" for the register 'c'.
*/
static void rname (const Proto *p, int pc, int c, const char **name) {
const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
if (!(what && *what == 'c')) /* did not find a constant name? */
*name = "?";
}
/*
** Find a "name" for a 'C' value in an RK instruction.
*/
static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
int c = GETARG_C(i); /* key index */
if (GETARG_k(i)) /* is 'c' a constant? */
kname(p, c, name);
else /* 'c' is a register */
rname(p, pc, c, name);
}
/*
** Check whether table being indexed by instruction 'i' is the
** environment '_ENV'
*/
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
int t = GETARG_B(i); /* table index */
const char *name; /* name of indexed variable */
if (isup) /* is 't' an upvalue? */
name = upvalname(p, t);
else /* 't' is a register */
basicgetobjname(p, &pc, t, &name);
return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
}
/*
** Extend 'basicgetobjname' to handle table accesses
*/
static const char *getobjname (const Proto *p, int lastpc, int reg,
const char **name) {
const char *kind = basicgetobjname(p, &lastpc, reg, name);
if (kind != NULL)
return kind;
else if (lastpc != -1) { /* could find instruction? */
Instruction i = p->code[lastpc];
OpCode op = GET_OPCODE(i);
switch (op) {
case OP_GETTABUP: {
int k = GETARG_C(i); /* key index */
kname(p, k, name);
return isEnv(p, lastpc, i, 1);
}
case OP_GETTABLE: {
int k = GETARG_C(i); /* key index */
rname(p, lastpc, k, name);
return isEnv(p, lastpc, i, 0);
}
case OP_GETI: {
*name = "integer index";
return "field";
}
case OP_GETFIELD: {
int k = GETARG_C(i); /* key index */
kname(p, k, name);
return isEnv(p, lastpc, i, 0);
}
case OP_SELF: {
rkname(p, lastpc, i, name);
return "method";
}
default: break; /* go through to return NULL */
}
}
return NULL; /* could not find reasonable name */
}
/*
** Try to find a name for a function based on the code that called it.
** (Only works when function was called by a Lua function.)
** Returns what the name is (e.g., "for iterator", "method",
** "metamethod") and sets '*name' to point to the name.
*/
static const char *funcnamefromcode (lua_State *L, const Proto *p,
int pc, const char **name) {
TMS tm = (TMS)0; /* (initial value avoids warnings) */
Instruction i = p->code[pc]; /* calling instruction */
switch (GET_OPCODE(i)) {
case OP_CALL:
case OP_TAILCALL:
return getobjname(p, pc, GETARG_A(i), name); /* get function name */
case OP_TFORCALL: { /* for iterator */
*name = "for iterator";
return "for iterator";
}
/* other instructions can do calls through metamethods */
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
case OP_GETI: case OP_GETFIELD:
tm = TM_INDEX;
break;
case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
tm = TM_NEWINDEX;
break;
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
tm = cast(TMS, GETARG_C(i));
break;
}
case OP_UNM: tm = TM_UNM; break;
case OP_BNOT: tm = TM_BNOT; break;
case OP_LEN: tm = TM_LEN; break;
case OP_CONCAT: tm = TM_CONCAT; break;
case OP_EQ: tm = TM_EQ; break;
/* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
default:
return NULL; /* cannot find a reasonable name */
}
*name = getshrstr(G(L)->tmname[tm]) + 2;
return "metamethod";
}
/*
** Try to find a name for a function based on how it was called.
*/
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
const char **name) {
if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
*name = "?";
return "hook";
}
else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */
*name = "__gc";
return "metamethod"; /* report it as such */
}
else if (isLua(ci))
return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
else
return NULL;
}
/* }====================================================== */
/*
** Check whether pointer 'o' points to some value in the stack frame of
** the current function and, if so, returns its index. Because 'o' may
** not point to a value in this stack, we cannot compare it with the
** region boundaries (undefined behavior in ISO C).
*/
static int instack (CallInfo *ci, const TValue *o) {
int pos;
StkId base = ci->func.p + 1;
for (pos = 0; base + pos < ci->top.p; pos++) {
if (o == s2v(base + pos))
return pos;
}
return -1; /* not found */
}
/*
** Checks whether value 'o' came from an upvalue. (That can only happen
** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
** upvalues.)
*/
static const char *getupvalname (CallInfo *ci, const TValue *o,
const char **name) {
LClosure *c = ci_func(ci);
int i;
for (i = 0; i < c->nupvalues; i++) {
if (c->upvals[i]->v.p == o) {
*name = upvalname(c->p, i);
return "upvalue";
}
}
return NULL;
}
static const char *formatvarinfo (lua_State *L, const char *kind,
const char *name) {
if (kind == NULL)
return ""; /* no information */
else
return luaO_pushfstring(L, " (%s '%s')", kind, name);
}
/*
** Build a string with a "description" for the value 'o', such as
** "variable 'x'" or "upvalue 'y'".
*/
static const char *varinfo (lua_State *L, const TValue *o) {
CallInfo *ci = L->ci;
const char *name = NULL; /* to avoid warnings */
const char *kind = NULL;
if (isLua(ci)) {
kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
if (!kind) { /* not an upvalue? */
int reg = instack(ci, o); /* try a register */
if (reg >= 0) /* is 'o' a register? */
kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
}
}
return formatvarinfo(L, kind, name);
}
/*
** Raise a type error
*/
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
const char *extra) {
const char *t = luaT_objtypename(L, o);
luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
}
/*
** Raise a type error with "standard" information about the faulty
** object 'o' (using 'varinfo').
*/
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
typeerror(L, o, op, varinfo(L, o));
}
/*
** Raise an error for calling a non-callable object. Try to find a name
** for the object based on how it was called ('funcnamefromcall'); if it
** cannot get a name there, try 'varinfo'.
*/
l_noret luaG_callerror (lua_State *L, const TValue *o) {
CallInfo *ci = L->ci;
const char *name = NULL; /* to avoid warnings */
const char *kind = funcnamefromcall(L, ci, &name);
const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
typeerror(L, o, "call", extra);
}
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
what, luaT_objtypename(L, o));
}
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
luaG_typeerror(L, p1, "concatenate");
}
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
const TValue *p2, const char *msg) {
if (!ttisnumber(p1)) /* first operand is wrong? */
p2 = p1; /* now second is wrong */
luaG_typeerror(L, p2, msg);
}
/*
** Error when both values are convertible to numbers, but not to integers
*/
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
lua_Integer temp;
if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
p2 = p1;
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
}
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
const char *t1 = luaT_objtypename(L, p1);
const char *t2 = luaT_objtypename(L, p2);
if (strcmp(t1, t2) == 0)
luaG_runerror(L, "attempt to compare two %s values", t1);
else
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
}
/* add src:line information to 'msg' */
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
int line) {
char buff[LUA_IDSIZE];
if (src)
luaO_chunkid(buff, getstr(src), tsslen(src));
else { /* no source available; use "?" instead */
buff[0] = '?'; buff[1] = '\0';
}
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
}
l_noret luaG_errormsg (lua_State *L) {
if (L->errfunc != 0) { /* is there an error handling function? */
StkId errfunc = restorestack(L, L->errfunc);
lua_assert(ttisfunction(s2v(errfunc)));
setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */
setobjs2s(L, L->top.p - 1, errfunc); /* push function */
L->top.p++; /* assume EXTRA_STACK */
luaD_callnoyield(L, L->top.p - 2, 1); /* call it */
}
luaD_throw(L, LUA_ERRRUN);
}
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
CallInfo *ci = L->ci;
const char *msg;
va_list argp;
luaC_checkGC(L); /* error message uses memory */
va_start(argp, fmt);
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
va_end(argp);
if (isLua(ci)) { /* if Lua function, add source:line information */
luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */
L->top.p--;
}
luaG_errormsg(L);
}
/*
** Check whether new instruction 'newpc' is in a different line from
** previous instruction 'oldpc'. More often than not, 'newpc' is only
** one or a few instructions after 'oldpc' (it must be after, see
** caller), so try to avoid calling 'luaG_getfuncline'. If they are
** too far apart, there is a good chance of a ABSLINEINFO in the way,
** so it goes directly to 'luaG_getfuncline'.
*/
static int changedline (const Proto *p, int oldpc, int newpc) {
if (p->lineinfo == NULL) /* no debug information? */
return 0;
if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */
int delta = 0; /* line difference */
int pc = oldpc;
for (;;) {
int lineinfo = p->lineinfo[++pc];
if (lineinfo == ABSLINEINFO)
break; /* cannot compute delta; fall through */
delta += lineinfo;
if (pc == newpc)
return (delta != 0); /* delta computed successfully */
}
}
/* either instructions are too far apart or there is an absolute line
info in the way; compute line difference explicitly */
return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
}
/*
** Traces Lua calls. If code is running the first instruction of a function,
** and function is not vararg, and it is not coming from an yield,
** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall'
** after adjusting its variable arguments; otherwise, they could call
** a line/count hook before the call hook. Functions coming from
** an yield already called 'luaD_hookcall' before yielding.)
*/
int luaG_tracecall (lua_State *L) {
CallInfo *ci = L->ci;
Proto *p = ci_func(ci)->p;
ci->u.l.trap = 1; /* ensure hooks will be checked */
if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */
if (p->is_vararg)
return 0; /* hooks will start at VARARGPREP instruction */
else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */
luaD_hookcall(L, ci); /* check 'call' hook */
}
return 1; /* keep 'trap' on */
}
/*
** Traces the execution of a Lua function. Called before the execution
** of each opcode, when debug is on. 'L->oldpc' stores the last
** instruction traced, to detect line changes. When entering a new
** function, 'npci' will be zero and will test as a new line whatever
** the value of 'oldpc'. Some exceptional conditions may return to
** a function without setting 'oldpc'. In that case, 'oldpc' may be
** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
** at most causes an extra call to a line hook.)
** This function is not "Protected" when called, so it should correct
** 'L->top.p' before calling anything that can run the GC.
*/
int luaG_traceexec (lua_State *L, const Instruction *pc) {
CallInfo *ci = L->ci;
lu_byte mask = L->hookmask;
const Proto *p = ci_func(ci)->p;
int counthook;
if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
ci->u.l.trap = 0; /* don't need to stop again */
return 0; /* turn off 'trap' */
}
pc++; /* reference is always next instruction */
ci->u.l.savedpc = pc; /* save 'pc' */
counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
if (counthook)
resethookcount(L); /* reset count */
else if (!(mask & LUA_MASKLINE))
return 1; /* no line hook and count != 0; nothing to be done now */
if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
return 1; /* do not call hook again (VM yielded, so it did not move) */
}
if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
L->top.p = ci->top.p; /* correct top */
if (counthook)
luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
if (mask & LUA_MASKLINE) {
/* 'L->oldpc' may be invalid; use zero in this case */
int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
int npci = pcRel(pc, p);
if (npci <= oldpc || /* call hook when jump back (loop), */
changedline(p, oldpc, npci)) { /* or when enter new line */
int newline = luaG_getfuncline(p, npci);
luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
}
L->oldpc = npci; /* 'pc' of last call to line hook */
}
if (L->status == LUA_YIELD) { /* did hook yield? */
if (counthook)
L->hookcount = 1; /* undo decrement to zero */
ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
luaD_throw(L, LUA_YIELD);
}
return 1; /* keep 'trap' on */
}

64
arm9/source/lua/ldebug.h Normal file
View File

@ -0,0 +1,64 @@
/*
** $Id: ldebug.h $
** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h
*/
#ifndef ldebug_h
#define ldebug_h
#include "lstate.h"
#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1)
/* Active Lua function (given call info) */
#define ci_func(ci) (clLvalue(s2v((ci)->func.p)))
#define resethookcount(L) (L->hookcount = L->basehookcount)
/*
** mark for entries in 'lineinfo' array that has absolute information in
** 'abslineinfo' array
*/
#define ABSLINEINFO (-0x80)
/*
** MAXimum number of successive Instructions WiTHout ABSolute line
** information. (A power of two allows fast divisions.)
*/
#if !defined(MAXIWTHABS)
#define MAXIWTHABS 128
#endif
LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc);
LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n,
StkId *pos);
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
const char *opname);
LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o);
LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o,
const char *what);
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
const TValue *p2,
const char *msg);
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
TString *src, int line);
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
LUAI_FUNC int luaG_tracecall (lua_State *L);
#endif

1028
arm9/source/lua/ldo.c Normal file

File diff suppressed because it is too large Load Diff

87
arm9/source/lua/ldo.h Normal file
View File

@ -0,0 +1,87 @@
/*
** $Id: ldo.h $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
#ifndef ldo_h
#define ldo_h
#include "llimits.h"
#include "lobject.h"
#include "lstate.h"
#include "lzio.h"
/*
** Macro to check stack size and grow stack if needed. Parameters
** 'pre'/'pos' allow the macro to preserve a pointer into the
** stack across reallocations, doing the work only when needed.
** It also allows the running of one GC step when the stack is
** reallocated.
** 'condmovestack' is used in heavy tests to force a stack reallocation
** at every check.
*/
#define luaD_checkstackaux(L,n,pre,pos) \
if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
{ pre; luaD_growstack(L, n, 1); pos; } \
else { condmovestack(L,pre,pos); }
/* In general, 'pre'/'pos' are empty (nothing to save) */
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
#define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p))
#define restorestack(L,n) castp(StkId, cast_charp(L->stack.p) + (n))
/* macro to check stack size, preserving 'p' */
#define checkstackp(L,n,p) \
luaD_checkstackaux(L, n, \
ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
/* macro to check stack size and GC, preserving 'p' */
#define checkstackGCp(L,n,p) \
luaD_checkstackaux(L, n, \
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
luaC_checkGC(L), /* stack grow uses memory */ \
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
/* macro to check stack size and GC */
#define checkstackGC(L,fsize) \
luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
/* type of protected functions, to be ran by 'runprotected' */
typedef void (*Pfunc) (lua_State *L, void *ud);
LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
const char *mode);
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
int fTransfer, int nTransfer);
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
int narg1, int delta);
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
LUAI_FUNC void luaD_inctop (lua_State *L);
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
#endif

230
arm9/source/lua/ldump.c Normal file
View File

@ -0,0 +1,230 @@
/*
** $Id: ldump.c $
** save precompiled Lua chunks
** See Copyright Notice in lua.h
*/
#define ldump_c
#define LUA_CORE
#include "lprefix.h"
#include <limits.h>
#include <stddef.h>
#include "lua.h"
#include "lobject.h"
#include "lstate.h"
#include "lundump.h"
typedef struct {
lua_State *L;
lua_Writer writer;
void *data;
int strip;
int status;
} DumpState;
/*
** All high-level dumps go through dumpVector; you can change it to
** change the endianness of the result
*/
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
static void dumpBlock (DumpState *D, const void *b, size_t size) {
if (D->status == 0 && size > 0) {
lua_unlock(D->L);
D->status = (*D->writer)(D->L, b, size, D->data);
lua_lock(D->L);
}
}
#define dumpVar(D,x) dumpVector(D,&x,1)
static void dumpByte (DumpState *D, int y) {
lu_byte x = (lu_byte)y;
dumpVar(D, x);
}
/*
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
** rounds up the division.)
*/
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
static void dumpSize (DumpState *D, size_t x) {
lu_byte buff[DIBS];
int n = 0;
do {
buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
x >>= 7;
} while (x != 0);
buff[DIBS - 1] |= 0x80; /* mark last byte */
dumpVector(D, buff + DIBS - n, n);
}
static void dumpInt (DumpState *D, int x) {
dumpSize(D, x);
}
static void dumpNumber (DumpState *D, lua_Number x) {
dumpVar(D, x);
}
static void dumpInteger (DumpState *D, lua_Integer x) {
dumpVar(D, x);
}
static void dumpString (DumpState *D, const TString *s) {
if (s == NULL)
dumpSize(D, 0);
else {
size_t size = tsslen(s);
const char *str = getstr(s);
dumpSize(D, size + 1);
dumpVector(D, str, size);
}
}
static void dumpCode (DumpState *D, const Proto *f) {
dumpInt(D, f->sizecode);
dumpVector(D, f->code, f->sizecode);
}
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
static void dumpConstants (DumpState *D, const Proto *f) {
int i;
int n = f->sizek;
dumpInt(D, n);
for (i = 0; i < n; i++) {
const TValue *o = &f->k[i];
int tt = ttypetag(o);
dumpByte(D, tt);
switch (tt) {
case LUA_VNUMFLT:
dumpNumber(D, fltvalue(o));
break;
case LUA_VNUMINT:
dumpInteger(D, ivalue(o));
break;
case LUA_VSHRSTR:
case LUA_VLNGSTR:
dumpString(D, tsvalue(o));
break;
default:
lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
}
}
}
static void dumpProtos (DumpState *D, const Proto *f) {
int i;
int n = f->sizep;
dumpInt(D, n);
for (i = 0; i < n; i++)
dumpFunction(D, f->p[i], f->source);
}
static void dumpUpvalues (DumpState *D, const Proto *f) {
int i, n = f->sizeupvalues;
dumpInt(D, n);
for (i = 0; i < n; i++) {
dumpByte(D, f->upvalues[i].instack);
dumpByte(D, f->upvalues[i].idx);
dumpByte(D, f->upvalues[i].kind);
}
}
static void dumpDebug (DumpState *D, const Proto *f) {
int i, n;
n = (D->strip) ? 0 : f->sizelineinfo;
dumpInt(D, n);
dumpVector(D, f->lineinfo, n);
n = (D->strip) ? 0 : f->sizeabslineinfo;
dumpInt(D, n);
for (i = 0; i < n; i++) {
dumpInt(D, f->abslineinfo[i].pc);
dumpInt(D, f->abslineinfo[i].line);
}
n = (D->strip) ? 0 : f->sizelocvars;
dumpInt(D, n);
for (i = 0; i < n; i++) {
dumpString(D, f->locvars[i].varname);
dumpInt(D, f->locvars[i].startpc);
dumpInt(D, f->locvars[i].endpc);
}
n = (D->strip) ? 0 : f->sizeupvalues;
dumpInt(D, n);
for (i = 0; i < n; i++)
dumpString(D, f->upvalues[i].name);
}
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
if (D->strip || f->source == psource)
dumpString(D, NULL); /* no debug info or same source as its parent */
else
dumpString(D, f->source);
dumpInt(D, f->linedefined);
dumpInt(D, f->lastlinedefined);
dumpByte(D, f->numparams);
dumpByte(D, f->is_vararg);
dumpByte(D, f->maxstacksize);
dumpCode(D, f);
dumpConstants(D, f);
dumpUpvalues(D, f);
dumpProtos(D, f);
dumpDebug(D, f);
}
static void dumpHeader (DumpState *D) {
dumpLiteral(D, LUA_SIGNATURE);
dumpByte(D, LUAC_VERSION);
dumpByte(D, LUAC_FORMAT);
dumpLiteral(D, LUAC_DATA);
dumpByte(D, sizeof(Instruction));
dumpByte(D, sizeof(lua_Integer));
dumpByte(D, sizeof(lua_Number));
dumpInteger(D, LUAC_INT);
dumpNumber(D, LUAC_NUM);
}
/*
** dump Lua function as precompiled chunk
*/
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
int strip) {
DumpState D;
D.L = L;
D.writer = w;
D.data = data;
D.strip = strip;
D.status = 0;
dumpHeader(&D);
dumpByte(&D, f->sizeupvalues);
dumpFunction(&D, f, NULL);
return D.status;
}

294
arm9/source/lua/lfunc.c Normal file
View File

@ -0,0 +1,294 @@
/*
** $Id: lfunc.c $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
#define lfunc_c
#define LUA_CORE
#include "lprefix.h"
#include <stddef.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
CClosure *c = gco2ccl(o);
c->nupvalues = cast_byte(nupvals);
return c;
}
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
LClosure *c = gco2lcl(o);
c->p = NULL;
c->nupvalues = cast_byte(nupvals);
while (nupvals--) c->upvals[nupvals] = NULL;
return c;
}
/*
** fill a closure with new closed upvalues
*/
void luaF_initupvals (lua_State *L, LClosure *cl) {
int i;
for (i = 0; i < cl->nupvalues; i++) {
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
UpVal *uv = gco2upv(o);
uv->v.p = &uv->u.value; /* make it closed */
setnilvalue(uv->v.p);
cl->upvals[i] = uv;
luaC_objbarrier(L, cl, uv);
}
}
/*
** Create a new upvalue at the given level, and link it to the list of
** open upvalues of 'L' after entry 'prev'.
**/
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
UpVal *uv = gco2upv(o);
UpVal *next = *prev;
uv->v.p = s2v(level); /* current value lives in the stack */
uv->u.open.next = next; /* link it to list of open upvalues */
uv->u.open.previous = prev;
if (next)
next->u.open.previous = &uv->u.open.next;
*prev = uv;
if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
L->twups = G(L)->twups; /* link it to the list */
G(L)->twups = L;
}
return uv;
}
/*
** Find and reuse, or create if it does not exist, an upvalue
** at the given level.
*/
UpVal *luaF_findupval (lua_State *L, StkId level) {
UpVal **pp = &L->openupval;
UpVal *p;
lua_assert(isintwups(L) || L->openupval == NULL);
while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
lua_assert(!isdead(G(L), p));
if (uplevel(p) == level) /* corresponding upvalue? */
return p; /* return it */
pp = &p->u.open.next;
}
/* not found: create a new upvalue after 'pp' */
return newupval(L, level, pp);
}
/*
** Call closing method for object 'obj' with error message 'err'. The
** boolean 'yy' controls whether the call is yieldable.
** (This function assumes EXTRA_STACK.)
*/
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
StkId top = L->top.p;
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
setobj2s(L, top, tm); /* will call metamethod... */
setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
L->top.p = top + 3; /* add function and arguments */
if (yy)
luaD_call(L, top, 0);
else
luaD_callnoyield(L, top, 0);
}
/*
** Check whether object at given level has a close metamethod and raise
** an error if not.
*/
static void checkclosemth (lua_State *L, StkId level) {
const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
if (ttisnil(tm)) { /* no metamethod? */
int idx = cast_int(level - L->ci->func.p); /* variable index */
const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
if (vname == NULL) vname = "?";
luaG_runerror(L, "variable '%s' got a non-closable value", vname);
}
}
/*
** Prepare and call a closing method.
** If status is CLOSEKTOP, the call to the closing method will be pushed
** at the top of the stack. Otherwise, values can be pushed right after
** the 'level' of the upvalue being closed, as everything after that
** won't be used again.
*/
static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
TValue *uv = s2v(level); /* value being closed */
TValue *errobj;
if (status == CLOSEKTOP)
errobj = &G(L)->nilvalue; /* error object is nil */
else { /* 'luaD_seterrorobj' will set top to level + 2 */
errobj = s2v(level + 1); /* error object goes after 'uv' */
luaD_seterrorobj(L, status, level + 1); /* set error object */
}
callclosemethod(L, uv, errobj, yy);
}
/*
** Maximum value for deltas in 'tbclist', dependent on the type
** of delta. (This macro assumes that an 'L' is in scope where it
** is used.)
*/
#define MAXDELTA \
((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
/*
** Insert a variable in the list of to-be-closed variables.
*/
void luaF_newtbcupval (lua_State *L, StkId level) {
lua_assert(level > L->tbclist.p);
if (l_isfalse(s2v(level)))
return; /* false doesn't need to be closed */
checkclosemth(L, level); /* value must have a close method */
while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */
L->tbclist.p->tbclist.delta = 0;
}
level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
L->tbclist.p = level;
}
void luaF_unlinkupval (UpVal *uv) {
lua_assert(upisopen(uv));
*uv->u.open.previous = uv->u.open.next;
if (uv->u.open.next)
uv->u.open.next->u.open.previous = uv->u.open.previous;
}
/*
** Close all upvalues up to the given stack level.
*/
void luaF_closeupval (lua_State *L, StkId level) {
UpVal *uv;
StkId upl; /* stack index pointed by 'uv' */
while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
TValue *slot = &uv->u.value; /* new position for value */
lua_assert(uplevel(uv) < L->top.p);
luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
setobj(L, slot, uv->v.p); /* move value to upvalue slot */
uv->v.p = slot; /* now current value lives here */
if (!iswhite(uv)) { /* neither white nor dead? */
nw2black(uv); /* closed upvalues cannot be gray */
luaC_barrier(L, uv, slot);
}
}
}
/*
** Remove first element from the tbclist plus its dummy nodes.
*/
static void poptbclist (lua_State *L) {
StkId tbc = L->tbclist.p;
lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
tbc -= tbc->tbclist.delta;
while (tbc > L->stack.p && tbc->tbclist.delta == 0)
tbc -= MAXDELTA; /* remove dummy nodes */
L->tbclist.p = tbc;
}
/*
** Close all upvalues and to-be-closed variables up to the given stack
** level. Return restored 'level'.
*/
StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
ptrdiff_t levelrel = savestack(L, level);
luaF_closeupval(L, level); /* first, close the upvalues */
while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
StkId tbc = L->tbclist.p; /* get variable index */
poptbclist(L); /* remove it from list */
prepcallclosemth(L, tbc, status, yy); /* close variable */
level = restorestack(L, levelrel);
}
return level;
}
Proto *luaF_newproto (lua_State *L) {
GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
Proto *f = gco2p(o);
f->k = NULL;
f->sizek = 0;
f->p = NULL;
f->sizep = 0;
f->code = NULL;
f->sizecode = 0;
f->lineinfo = NULL;
f->sizelineinfo = 0;
f->abslineinfo = NULL;
f->sizeabslineinfo = 0;
f->upvalues = NULL;
f->sizeupvalues = 0;
f->numparams = 0;
f->is_vararg = 0;
f->maxstacksize = 0;
f->locvars = NULL;
f->sizelocvars = 0;
f->linedefined = 0;
f->lastlinedefined = 0;
f->source = NULL;
return f;
}
void luaF_freeproto (lua_State *L, Proto *f) {
luaM_freearray(L, f->code, f->sizecode);
luaM_freearray(L, f->p, f->sizep);
luaM_freearray(L, f->k, f->sizek);
luaM_freearray(L, f->lineinfo, f->sizelineinfo);
luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
luaM_freearray(L, f->locvars, f->sizelocvars);
luaM_freearray(L, f->upvalues, f->sizeupvalues);
luaM_free(L, f);
}
/*
** Look for n-th local variable at line 'line' in function 'func'.
** Returns NULL if not found.
*/
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
int i;
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
if (pc < f->locvars[i].endpc) { /* is variable active? */
local_number--;
if (local_number == 0)
return getstr(f->locvars[i].varname);
}
}
return NULL; /* not found */
}

64
arm9/source/lua/lfunc.h Normal file
View File

@ -0,0 +1,64 @@
/*
** $Id: lfunc.h $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
#ifndef lfunc_h
#define lfunc_h
#include "lobject.h"
#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \
cast_int(sizeof(TValue)) * (n))
#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \
cast_int(sizeof(TValue *)) * (n))
/* test whether thread is in 'twups' list */
#define isintwups(L) (L->twups != L)
/*
** maximum number of upvalues in a closure (both C and Lua). (Value
** must fit in a VM register.)
*/
#define MAXUPVAL 255
#define upisopen(up) ((up)->v.p != &(up)->u.value)
#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p))
/*
** maximum number of misses before giving up the cache of closures
** in prototypes
*/
#define MAXMISS 10
/* special status to close upvalues preserving the top of the stack */
#define CLOSEKTOP (-1)
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals);
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals);
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level);
LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy);
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
int pc);
#endif

1743
arm9/source/lua/lgc.c Normal file

File diff suppressed because it is too large Load Diff

202
arm9/source/lua/lgc.h Normal file
View File

@ -0,0 +1,202 @@
/*
** $Id: lgc.h $
** Garbage Collector
** See Copyright Notice in lua.h
*/
#ifndef lgc_h
#define lgc_h
#include "lobject.h"
#include "lstate.h"
/*
** Collectable objects may have one of three colors: white, which means
** the object is not marked; gray, which means the object is marked, but
** its references may be not marked; and black, which means that the
** object and all its references are marked. The main invariant of the
** garbage collector, while marking objects, is that a black object can
** never point to a white one. Moreover, any gray object must be in a
** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
** can be visited again before finishing the collection cycle. (Open
** upvalues are an exception to this rule.) These lists have no meaning
** when the invariant is not being enforced (e.g., sweep phase).
*/
/*
** Possible states of the Garbage Collector
*/
#define GCSpropagate 0
#define GCSenteratomic 1
#define GCSatomic 2
#define GCSswpallgc 3
#define GCSswpfinobj 4
#define GCSswptobefnz 5
#define GCSswpend 6
#define GCScallfin 7
#define GCSpause 8
#define issweepphase(g) \
(GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
/*
** macro to tell when main invariant (white objects cannot point to black
** ones) must be kept. During a collection, the sweep
** phase may break the invariant, as objects turned white may point to
** still-black objects. The invariant is restored when sweep ends and
** all objects are white again.
*/
#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
/*
** some useful bit tricks
*/
#define resetbits(x,m) ((x) &= cast_byte(~(m)))
#define setbits(x,m) ((x) |= (m))
#define testbits(x,m) ((x) & (m))
#define bitmask(b) (1<<(b))
#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
#define l_setbit(x,b) setbits(x, bitmask(b))
#define resetbit(x,b) resetbits(x, bitmask(b))
#define testbit(x,b) testbits(x, bitmask(b))
/*
** Layout for bit use in 'marked' field. First three bits are
** used for object "age" in generational mode. Last bit is used
** by tests.
*/
#define WHITE0BIT 3 /* object is white (type 0) */
#define WHITE1BIT 4 /* object is white (type 1) */
#define BLACKBIT 5 /* object is black */
#define FINALIZEDBIT 6 /* object has been marked for finalization */
#define TESTBIT 7
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
#define iswhite(x) testbits((x)->marked, WHITEBITS)
#define isblack(x) testbit((x)->marked, BLACKBIT)
#define isgray(x) /* neither white nor black */ \
(!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
#define isdeadm(ow,m) ((m) & (ow))
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
#define changewhite(x) ((x)->marked ^= WHITEBITS)
#define nw2black(x) \
check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
/* object age in generational mode */
#define G_NEW 0 /* created in current cycle */
#define G_SURVIVAL 1 /* created in previous cycle */
#define G_OLD0 2 /* marked old by frw. barrier in this cycle */
#define G_OLD1 3 /* first full cycle as old */
#define G_OLD 4 /* really old object (not to be visited) */
#define G_TOUCHED1 5 /* old object touched this cycle */
#define G_TOUCHED2 6 /* old object touched in previous cycle */
#define AGEBITS 7 /* all age bits (111) */
#define getage(o) ((o)->marked & AGEBITS)
#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
#define isold(o) (getage(o) > G_SURVIVAL)
#define changeage(o,f,t) \
check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
/* Default Values for GC parameters */
#define LUAI_GENMAJORMUL 100
#define LUAI_GENMINORMUL 20
/* wait memory to double before starting new cycle */
#define LUAI_GCPAUSE 200
/*
** some gc parameters are stored divided by 4 to allow a maximum value
** up to 1023 in a 'lu_byte'.
*/
#define getgcparam(p) ((p) * 4)
#define setgcparam(p,v) ((p) = (v) / 4)
#define LUAI_GCMUL 100
/* how much to allocate before next GC step (log2) */
#define LUAI_GCSTEPSIZE 13 /* 8 KB */
/*
** Check whether the declared GC mode is generational. While in
** generational mode, the collector can go temporarily to incremental
** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
*/
#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
/*
** Control when GC is running:
*/
#define GCSTPUSR 1 /* bit true when GC stopped by user */
#define GCSTPGC 2 /* bit true when GC stopped by itself */
#define GCSTPCLS 4 /* bit true when closing Lua state */
#define gcrunning(g) ((g)->gcstp == 0)
/*
** Does one step of collection when debt becomes positive. 'pre'/'pos'
** allows some adjustments to be done only when needed. macro
** 'condchangemem' is used only for heavy tests (forcing a full
** GC cycle on every opportunity)
*/
#define luaC_condGC(L,pre,pos) \
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
condchangemem(L,pre,pos); }
/* more often than not, 'pre'/'pos' are empty */
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
#define luaC_objbarrier(L,p,o) ( \
(isblack(p) && iswhite(o)) ? \
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
#define luaC_barrier(L,p,v) ( \
iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
#define luaC_objbarrierback(L,p,o) ( \
(isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
#define luaC_barrierback(L,p,v) ( \
iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
LUAI_FUNC void luaC_step (lua_State *L);
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
size_t offset);
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
#endif

65
arm9/source/lua/linit.c Normal file
View File

@ -0,0 +1,65 @@
/*
** $Id: linit.c $
** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h
*/
#define linit_c
#define LUA_LIB
/*
** If you embed Lua in your program and need to open the standard
** libraries, call luaL_openlibs in your program. If you need a
** different set of libraries, copy this file to your project and edit
** it to suit your needs.
**
** You can also *preload* libraries, so that a later 'require' can
** open the library, which is already linked to the application.
** For that, do the following code:
**
** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
** lua_pushcfunction(L, luaopen_modname);
** lua_setfield(L, -2, modname);
** lua_pop(L, 1); // remove PRELOAD table
*/
#include "lprefix.h"
#include <stddef.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/*
** these libs are loaded by lua.c and are readily available to any Lua
** program
*/
static const luaL_Reg loadedlibs[] = {
{LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
{LUA_DBLIBNAME, luaopen_debug},
{NULL, NULL}
};
LUALIB_API void luaL_openlibs (lua_State *L) {
const luaL_Reg *lib;
/* "require" functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
}

112
arm9/source/lua/ljumptab.h Normal file
View File

@ -0,0 +1,112 @@
/*
** $Id: ljumptab.h $
** Jump Table for the Lua interpreter
** See Copyright Notice in lua.h
*/
#undef vmdispatch
#undef vmcase
#undef vmbreak
#define vmdispatch(x) goto *disptab[x];
#define vmcase(l) L_##l:
#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i));
static const void *const disptab[NUM_OPCODES] = {
#if 0
** you can update the following list with this command:
**
** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h
**
#endif
&&L_OP_MOVE,
&&L_OP_LOADI,
&&L_OP_LOADF,
&&L_OP_LOADK,
&&L_OP_LOADKX,
&&L_OP_LOADFALSE,
&&L_OP_LFALSESKIP,
&&L_OP_LOADTRUE,
&&L_OP_LOADNIL,
&&L_OP_GETUPVAL,
&&L_OP_SETUPVAL,
&&L_OP_GETTABUP,
&&L_OP_GETTABLE,
&&L_OP_GETI,
&&L_OP_GETFIELD,
&&L_OP_SETTABUP,
&&L_OP_SETTABLE,
&&L_OP_SETI,
&&L_OP_SETFIELD,
&&L_OP_NEWTABLE,
&&L_OP_SELF,
&&L_OP_ADDI,
&&L_OP_ADDK,
&&L_OP_SUBK,
&&L_OP_MULK,
&&L_OP_MODK,
&&L_OP_POWK,
&&L_OP_DIVK,
&&L_OP_IDIVK,
&&L_OP_BANDK,
&&L_OP_BORK,
&&L_OP_BXORK,
&&L_OP_SHRI,
&&L_OP_SHLI,
&&L_OP_ADD,
&&L_OP_SUB,
&&L_OP_MUL,
&&L_OP_MOD,
&&L_OP_POW,
&&L_OP_DIV,
&&L_OP_IDIV,
&&L_OP_BAND,
&&L_OP_BOR,
&&L_OP_BXOR,
&&L_OP_SHL,
&&L_OP_SHR,
&&L_OP_MMBIN,
&&L_OP_MMBINI,
&&L_OP_MMBINK,
&&L_OP_UNM,
&&L_OP_BNOT,
&&L_OP_NOT,
&&L_OP_LEN,
&&L_OP_CONCAT,
&&L_OP_CLOSE,
&&L_OP_TBC,
&&L_OP_JMP,
&&L_OP_EQ,
&&L_OP_LT,
&&L_OP_LE,
&&L_OP_EQK,
&&L_OP_EQI,
&&L_OP_LTI,
&&L_OP_LEI,
&&L_OP_GTI,
&&L_OP_GEI,
&&L_OP_TEST,
&&L_OP_TESTSET,
&&L_OP_CALL,
&&L_OP_TAILCALL,
&&L_OP_RETURN,
&&L_OP_RETURN0,
&&L_OP_RETURN1,
&&L_OP_FORLOOP,
&&L_OP_FORPREP,
&&L_OP_TFORPREP,
&&L_OP_TFORCALL,
&&L_OP_TFORLOOP,
&&L_OP_SETLIST,
&&L_OP_CLOSURE,
&&L_OP_VARARG,
&&L_OP_VARARGPREP,
&&L_OP_EXTRAARG
};

581
arm9/source/lua/llex.c Normal file
View File

@ -0,0 +1,581 @@
/*
** $Id: llex.c $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
#define llex_c
#define LUA_CORE
#include "lprefix.h"
#include <locale.h>
#include <string.h>
#include "lua.h"
#include "lctype.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
#include "llex.h"
#include "lobject.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lzio.h"
#define next(ls) (ls->current = zgetc(ls->z))
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
/* ORDER RESERVED */
static const char *const luaX_tokens [] = {
"and", "break", "do", "else", "elseif",
"end", "false", "for", "function", "goto", "if",
"in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "until", "while",
"//", "..", "...", "==", ">=", "<=", "~=",
"<<", ">>", "::", "<eof>",
"<number>", "<integer>", "<name>", "<string>"
};
#define save_and_next(ls) (save(ls, ls->current), next(ls))
static l_noret lexerror (LexState *ls, const char *msg, int token);
static void save (LexState *ls, int c) {
Mbuffer *b = ls->buff;
if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
size_t newsize;
if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
lexerror(ls, "lexical element too long", 0);
newsize = luaZ_sizebuffer(b) * 2;
luaZ_resizebuffer(ls->L, b, newsize);
}
b->buffer[luaZ_bufflen(b)++] = cast_char(c);
}
void luaX_init (lua_State *L) {
int i;
TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */
luaC_fix(L, obj2gco(e)); /* never collect this name */
for (i=0; i<NUM_RESERVED; i++) {
TString *ts = luaS_new(L, luaX_tokens[i]);
luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */
ts->extra = cast_byte(i+1); /* reserved word */
}
}
const char *luaX_token2str (LexState *ls, int token) {
if (token < FIRST_RESERVED) { /* single-byte symbols? */
if (lisprint(token))
return luaO_pushfstring(ls->L, "'%c'", token);
else /* control character */
return luaO_pushfstring(ls->L, "'<\\%d>'", token);
}
else {
const char *s = luaX_tokens[token - FIRST_RESERVED];
if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
return luaO_pushfstring(ls->L, "'%s'", s);
else /* names, strings, and numerals */
return s;
}
}
static const char *txtToken (LexState *ls, int token) {
switch (token) {
case TK_NAME: case TK_STRING:
case TK_FLT: case TK_INT:
save(ls, '\0');
return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff));
default:
return luaX_token2str(ls, token);
}
}
static l_noret lexerror (LexState *ls, const char *msg, int token) {
msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
if (token)
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
luaD_throw(ls->L, LUA_ERRSYNTAX);
}
l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
lexerror(ls, msg, ls->t.token);
}
/*
** Creates a new string and anchors it in scanner's table so that it
** will not be collected until the end of the compilation; by that time
** it should be anchored somewhere. It also internalizes long strings,
** ensuring there is only one copy of each unique string. The table
** here is used as a set: the string enters as the key, while its value
** is irrelevant. We use the string itself as the value only because it
** is a TValue readily available. Later, the code generation can change
** this value.
*/
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
lua_State *L = ls->L;
TString *ts = luaS_newlstr(L, str, l); /* create new string */
const TValue *o = luaH_getstr(ls->h, ts);
if (!ttisnil(o)) /* string already present? */
ts = keystrval(nodefromval(o)); /* get saved copy */
else { /* not in use yet */
TValue *stv = s2v(L->top.p++); /* reserve stack space for string */
setsvalue(L, stv, ts); /* temporarily anchor the string */
luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */
/* table is not a metatable, so it does not need to invalidate cache */
luaC_checkGC(L);
L->top.p--; /* remove string from stack */
}
return ts;
}
/*
** increment line number and skips newline sequence (any of
** \n, \r, \n\r, or \r\n)
*/
static void inclinenumber (LexState *ls) {
int old = ls->current;
lua_assert(currIsNewline(ls));
next(ls); /* skip '\n' or '\r' */
if (currIsNewline(ls) && ls->current != old)
next(ls); /* skip '\n\r' or '\r\n' */
if (++ls->linenumber >= MAX_INT)
lexerror(ls, "chunk has too many lines", 0);
}
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
int firstchar) {
ls->t.token = 0;
ls->L = L;
ls->current = firstchar;
ls->lookahead.token = TK_EOS; /* no look-ahead token */
ls->z = z;
ls->fs = NULL;
ls->linenumber = 1;
ls->lastline = 1;
ls->source = source;
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
}
/*
** =======================================================
** LEXICAL ANALYZER
** =======================================================
*/
static int check_next1 (LexState *ls, int c) {
if (ls->current == c) {
next(ls);
return 1;
}
else return 0;
}
/*
** Check whether current char is in set 'set' (with two chars) and
** saves it
*/
static int check_next2 (LexState *ls, const char *set) {
lua_assert(set[2] == '\0');
if (ls->current == set[0] || ls->current == set[1]) {
save_and_next(ls);
return 1;
}
else return 0;
}
/* LUA_NUMBER */
/*
** This function is quite liberal in what it accepts, as 'luaO_str2num'
** will reject ill-formed numerals. Roughly, it accepts the following
** pattern:
**
** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))*
**
** The only tricky part is to accept [+-] only after a valid exponent
** mark, to avoid reading '3-4' or '0xe+1' as a single number.
**
** The caller might have already read an initial dot.
*/
static int read_numeral (LexState *ls, SemInfo *seminfo) {
TValue obj;
const char *expo = "Ee";
int first = ls->current;
lua_assert(lisdigit(ls->current));
save_and_next(ls);
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
expo = "Pp";
for (;;) {
if (check_next2(ls, expo)) /* exponent mark? */
check_next2(ls, "-+"); /* optional exponent sign */
else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */
save_and_next(ls);
else break;
}
if (lislalpha(ls->current)) /* is numeral touching a letter? */
save_and_next(ls); /* force an error */
save(ls, '\0');
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
lexerror(ls, "malformed number", TK_FLT);
if (ttisinteger(&obj)) {
seminfo->i = ivalue(&obj);
return TK_INT;
}
else {
lua_assert(ttisfloat(&obj));
seminfo->r = fltvalue(&obj);
return TK_FLT;
}
}
/*
** read a sequence '[=*[' or ']=*]', leaving the last bracket. If
** sequence is well formed, return its number of '='s + 2; otherwise,
** return 1 if it is a single bracket (no '='s and no 2nd bracket);
** otherwise (an unfinished '[==...') return 0.
*/
static size_t skip_sep (LexState *ls) {
size_t count = 0;
int s = ls->current;
lua_assert(s == '[' || s == ']');
save_and_next(ls);
while (ls->current == '=') {
save_and_next(ls);
count++;
}
return (ls->current == s) ? count + 2
: (count == 0) ? 1
: 0;
}
static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
int line = ls->linenumber; /* initial line (for error message) */
save_and_next(ls); /* skip 2nd '[' */
if (currIsNewline(ls)) /* string starts with a newline? */
inclinenumber(ls); /* skip it */
for (;;) {
switch (ls->current) {
case EOZ: { /* error */
const char *what = (seminfo ? "string" : "comment");
const char *msg = luaO_pushfstring(ls->L,
"unfinished long %s (starting at line %d)", what, line);
lexerror(ls, msg, TK_EOS);
break; /* to avoid warnings */
}
case ']': {
if (skip_sep(ls) == sep) {
save_and_next(ls); /* skip 2nd ']' */
goto endloop;
}
break;
}
case '\n': case '\r': {
save(ls, '\n');
inclinenumber(ls);
if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
break;
}
default: {
if (seminfo) save_and_next(ls);
else next(ls);
}
}
} endloop:
if (seminfo)
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
luaZ_bufflen(ls->buff) - 2 * sep);
}
static void esccheck (LexState *ls, int c, const char *msg) {
if (!c) {
if (ls->current != EOZ)
save_and_next(ls); /* add current to buffer for error message */
lexerror(ls, msg, TK_STRING);
}
}
static int gethexa (LexState *ls) {
save_and_next(ls);
esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected");
return luaO_hexavalue(ls->current);
}
static int readhexaesc (LexState *ls) {
int r = gethexa(ls);
r = (r << 4) + gethexa(ls);
luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */
return r;
}
static unsigned long readutf8esc (LexState *ls) {
unsigned long r;
int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */
save_and_next(ls); /* skip 'u' */
esccheck(ls, ls->current == '{', "missing '{'");
r = gethexa(ls); /* must have at least one digit */
while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) {
i++;
esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
r = (r << 4) + luaO_hexavalue(ls->current);
}
esccheck(ls, ls->current == '}', "missing '}'");
next(ls); /* skip '}' */
luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */
return r;
}
static void utf8esc (LexState *ls) {
char buff[UTF8BUFFSZ];
int n = luaO_utf8esc(buff, readutf8esc(ls));
for (; n > 0; n--) /* add 'buff' to string */
save(ls, buff[UTF8BUFFSZ - n]);
}
static int readdecesc (LexState *ls) {
int i;
int r = 0; /* result accumulator */
for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
r = 10*r + ls->current - '0';
save_and_next(ls);
}
esccheck(ls, r <= UCHAR_MAX, "decimal escape too large");
luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */
return r;
}
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
save_and_next(ls); /* keep delimiter (for error messages) */
while (ls->current != del) {
switch (ls->current) {
case EOZ:
lexerror(ls, "unfinished string", TK_EOS);
break; /* to avoid warnings */
case '\n':
case '\r':
lexerror(ls, "unfinished string", TK_STRING);
break; /* to avoid warnings */
case '\\': { /* escape sequences */
int c; /* final character to be saved */
save_and_next(ls); /* keep '\\' for error messages */
switch (ls->current) {
case 'a': c = '\a'; goto read_save;
case 'b': c = '\b'; goto read_save;
case 'f': c = '\f'; goto read_save;
case 'n': c = '\n'; goto read_save;
case 'r': c = '\r'; goto read_save;
case 't': c = '\t'; goto read_save;
case 'v': c = '\v'; goto read_save;
case 'x': c = readhexaesc(ls); goto read_save;
case 'u': utf8esc(ls); goto no_save;
case '\n': case '\r':
inclinenumber(ls); c = '\n'; goto only_save;
case '\\': case '\"': case '\'':
c = ls->current; goto read_save;
case EOZ: goto no_save; /* will raise an error next loop */
case 'z': { /* zap following span of spaces */
luaZ_buffremove(ls->buff, 1); /* remove '\\' */
next(ls); /* skip the 'z' */
while (lisspace(ls->current)) {
if (currIsNewline(ls)) inclinenumber(ls);
else next(ls);
}
goto no_save;
}
default: {
esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
c = readdecesc(ls); /* digital escape '\ddd' */
goto only_save;
}
}
read_save:
next(ls);
/* go through */
only_save:
luaZ_buffremove(ls->buff, 1); /* remove '\\' */
save(ls, c);
/* go through */
no_save: break;
}
default:
save_and_next(ls);
}
}
save_and_next(ls); /* skip delimiter */
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
luaZ_bufflen(ls->buff) - 2);
}
static int llex (LexState *ls, SemInfo *seminfo) {
luaZ_resetbuffer(ls->buff);
for (;;) {
switch (ls->current) {
case '\n': case '\r': { /* line breaks */
inclinenumber(ls);
break;
}
case ' ': case '\f': case '\t': case '\v': { /* spaces */
next(ls);
break;
}
case '-': { /* '-' or '--' (comment) */
next(ls);
if (ls->current != '-') return '-';
/* else is a comment */
next(ls);
if (ls->current == '[') { /* long comment? */
size_t sep = skip_sep(ls);
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
if (sep >= 2) {
read_long_string(ls, NULL, sep); /* skip long comment */
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
break;
}
}
/* else short comment */
while (!currIsNewline(ls) && ls->current != EOZ)
next(ls); /* skip until end of line (or end of file) */
break;
}
case '[': { /* long string or simply '[' */
size_t sep = skip_sep(ls);
if (sep >= 2) {
read_long_string(ls, seminfo, sep);
return TK_STRING;
}
else if (sep == 0) /* '[=...' missing second bracket? */
lexerror(ls, "invalid long string delimiter", TK_STRING);
return '[';
}
case '=': {
next(ls);
if (check_next1(ls, '=')) return TK_EQ; /* '==' */
else return '=';
}
case '<': {
next(ls);
if (check_next1(ls, '=')) return TK_LE; /* '<=' */
else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */
else return '<';
}
case '>': {
next(ls);
if (check_next1(ls, '=')) return TK_GE; /* '>=' */
else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */
else return '>';
}
case '/': {
next(ls);
if (check_next1(ls, '/')) return TK_IDIV; /* '//' */
else return '/';
}
case '~': {
next(ls);
if (check_next1(ls, '=')) return TK_NE; /* '~=' */
else return '~';
}
case ':': {
next(ls);
if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */
else return ':';
}
case '"': case '\'': { /* short literal strings */
read_string(ls, ls->current, seminfo);
return TK_STRING;
}
case '.': { /* '.', '..', '...', or number */
save_and_next(ls);
if (check_next1(ls, '.')) {
if (check_next1(ls, '.'))
return TK_DOTS; /* '...' */
else return TK_CONCAT; /* '..' */
}
else if (!lisdigit(ls->current)) return '.';
else return read_numeral(ls, seminfo);
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
return read_numeral(ls, seminfo);
}
case EOZ: {
return TK_EOS;
}
default: {
if (lislalpha(ls->current)) { /* identifier or reserved word? */
TString *ts;
do {
save_and_next(ls);
} while (lislalnum(ls->current));
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
luaZ_bufflen(ls->buff));
seminfo->ts = ts;
if (isreserved(ts)) /* reserved word? */
return ts->extra - 1 + FIRST_RESERVED;
else {
return TK_NAME;
}
}
else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */
int c = ls->current;
next(ls);
return c;
}
}
}
}
}
void luaX_next (LexState *ls) {
ls->lastline = ls->linenumber;
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
ls->t = ls->lookahead; /* use this one */
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else
ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
}
int luaX_lookahead (LexState *ls) {
lua_assert(ls->lookahead.token == TK_EOS);
ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
return ls->lookahead.token;
}

91
arm9/source/lua/llex.h Normal file
View File

@ -0,0 +1,91 @@
/*
** $Id: llex.h $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
#ifndef llex_h
#define llex_h
#include <limits.h>
#include "lobject.h"
#include "lzio.h"
/*
** Single-char tokens (terminal symbols) are represented by their own
** numeric code. Other tokens start at the following value.
*/
#define FIRST_RESERVED (UCHAR_MAX + 1)
#if !defined(LUA_ENV)
#define LUA_ENV "_ENV"
#endif
/*
* WARNING: if you change the order of this enumeration,
* grep "ORDER RESERVED"
*/
enum RESERVED {
/* terminal symbols denoted by reserved words */
TK_AND = FIRST_RESERVED, TK_BREAK,
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
/* other terminal symbols */
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
TK_SHL, TK_SHR,
TK_DBCOLON, TK_EOS,
TK_FLT, TK_INT, TK_NAME, TK_STRING
};
/* number of reserved words */
#define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1))
typedef union {
lua_Number r;
lua_Integer i;
TString *ts;
} SemInfo; /* semantics information */
typedef struct Token {
int token;
SemInfo seminfo;
} Token;
/* state of the lexer plus state of the parser when shared by all
functions */
typedef struct LexState {
int current; /* current character (charint) */
int linenumber; /* input line counter */
int lastline; /* line of last token 'consumed' */
Token t; /* current token */
Token lookahead; /* look ahead token */
struct FuncState *fs; /* current function (parser) */
struct lua_State *L;
ZIO *z; /* input stream */
Mbuffer *buff; /* buffer for tokens */
Table *h; /* to avoid collection/reuse strings */
struct Dyndata *dyd; /* dynamic structures used by the parser */
TString *source; /* current source name */
TString *envn; /* environment variable name */
} LexState;
LUAI_FUNC void luaX_init (lua_State *L);
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
TString *source, int firstchar);
LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
LUAI_FUNC void luaX_next (LexState *ls);
LUAI_FUNC int luaX_lookahead (LexState *ls);
LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
#endif

381
arm9/source/lua/llimits.h Normal file
View File

@ -0,0 +1,381 @@
/*
** $Id: llimits.h $
** Limits, basic types, and some other 'installation-dependent' definitions
** See Copyright Notice in lua.h
*/
#ifndef llimits_h
#define llimits_h
#include <limits.h>
#include <stddef.h>
#include "lua.h"
/*
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
** the total memory used by Lua (in bytes). Usually, 'size_t' and
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
*/
#if defined(LUAI_MEM) /* { external definitions? */
typedef LUAI_UMEM lu_mem;
typedef LUAI_MEM l_mem;
#elif LUAI_IS32INT /* }{ */
typedef size_t lu_mem;
typedef ptrdiff_t l_mem;
#else /* 16-bit ints */ /* }{ */
typedef unsigned long lu_mem;
typedef long l_mem;
#endif /* } */
/* chars used as small naturals (so that 'char' is reserved for characters) */
typedef unsigned char lu_byte;
typedef signed char ls_byte;
/* maximum value for size_t */
#define MAX_SIZET ((size_t)(~(size_t)0))
/* maximum size visible for Lua (must be representable in a lua_Integer) */
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
: (size_t)(LUA_MAXINTEGER))
#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
#define MAX_INT INT_MAX /* maximum value of an int */
/*
** floor of the log2 of the maximum signed value for integral type 't'.
** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
*/
#define log2maxs(t) (sizeof(t) * 8 - 2)
/*
** test whether an unsigned value is a power of 2 (or zero)
*/
#define ispow2(x) (((x) & ((x) - 1)) == 0)
/* number of chars of a literal string without the ending \0 */
#define LL(x) (sizeof(x)/sizeof(char) - 1)
/*
** conversion of pointer to unsigned integer: this is for hashing only;
** there is no problem if the integer cannot hold the whole pointer
** value. (In strict ISO C this may cause undefined behavior, but no
** actual machine seems to bother.)
*/
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
__STDC_VERSION__ >= 199901L
#include <stdint.h>
#if defined(UINTPTR_MAX) /* even in C99 this type is optional */
#define L_P2I uintptr_t
#else /* no 'intptr'? */
#define L_P2I uintmax_t /* use the largest available integer */
#endif
#else /* C89 option */
#define L_P2I size_t
#endif
#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))
/* types of 'usual argument conversions' for lua_Number and lua_Integer */
typedef LUAI_UACNUMBER l_uacNumber;
typedef LUAI_UACINT l_uacInt;
/*
** Internal assertions for in-house debugging
*/
#if defined LUAI_ASSERT
#undef NDEBUG
#include <assert.h>
#define lua_assert(c) assert(c)
#endif
#if defined(lua_assert)
#define check_exp(c,e) (lua_assert(c), (e))
/* to avoid problems with conditions too long */
#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
#else
#define lua_assert(c) ((void)0)
#define check_exp(c,e) (e)
#define lua_longassert(c) ((void)0)
#endif
/*
** assertion for checking API calls
*/
#if !defined(luai_apicheck)
#define luai_apicheck(l,e) ((void)l, lua_assert(e))
#endif
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
/* macro to avoid warnings about unused variables */
#if !defined(UNUSED)
#define UNUSED(x) ((void)(x))
#endif
/* type casts (a macro highlights casts in the code) */
#define cast(t, exp) ((t)(exp))
#define castp(t, exp) ((t)(void *)(exp))
#define cast_void(i) cast(void, (i))
#define cast_voidp(i) cast(void *, (i))
#define cast_num(i) cast(lua_Number, (i))
#define cast_int(i) cast(int, (i))
#define cast_uint(i) cast(unsigned int, (i))
#define cast_byte(i) cast(lu_byte, (i))
#define cast_uchar(i) cast(unsigned char, (i))
#define cast_char(i) cast(char, (i))
#define cast_charp(i) castp(char *, (i))
#define cast_sizet(i) cast(size_t, (i))
/* cast a signed lua_Integer to lua_Unsigned */
#if !defined(l_castS2U)
#define l_castS2U(i) ((lua_Unsigned)(i))
#endif
/*
** cast a lua_Unsigned to a signed lua_Integer; this cast is
** not strict ISO C, but two-complement architectures should
** work fine.
*/
#if !defined(l_castU2S)
#define l_castU2S(i) ((lua_Integer)(i))
#endif
/*
** non-return type
*/
#if !defined(l_noret)
#if defined(__GNUC__)
#define l_noret void __attribute__((noreturn))
#elif defined(_MSC_VER) && _MSC_VER >= 1200
#define l_noret void __declspec(noreturn)
#else
#define l_noret void
#endif
#endif
/*
** Inline functions
*/
#if !defined(LUA_USE_C89)
#define l_inline inline
#elif defined(__GNUC__)
#define l_inline __inline__
#else
#define l_inline /* empty */
#endif
#define l_sinline static l_inline
/*
** type for virtual-machine instructions;
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
*/
#if LUAI_IS32INT
typedef unsigned int l_uint32;
#else
typedef unsigned long l_uint32;
#endif
typedef l_uint32 Instruction;
/*
** Maximum length for short strings, that is, strings that are
** internalized. (Cannot be smaller than reserved words or tags for
** metamethods, as these strings must be internalized;
** #("function") = 8, #("__newindex") = 10.)
*/
#if !defined(LUAI_MAXSHORTLEN)
#define LUAI_MAXSHORTLEN 40
#endif
/*
** Initial size for the string table (must be power of 2).
** The Lua core alone registers ~50 strings (reserved words +
** metaevent keys + a few others). Libraries would typically add
** a few dozens more.
*/
#if !defined(MINSTRTABSIZE)
#define MINSTRTABSIZE 128
#endif
/*
** Size of cache for strings in the API. 'N' is the number of
** sets (better be a prime) and "M" is the size of each set (M == 1
** makes a direct cache.)
*/
#if !defined(STRCACHE_N)
#define STRCACHE_N 53
#define STRCACHE_M 2
#endif
/* minimum size for string buffer */
#if !defined(LUA_MINBUFFER)
#define LUA_MINBUFFER 32
#endif
/*
** Maximum depth for nested C calls, syntactical nested non-terminals,
** and other features implemented through recursion in C. (Value must
** fit in a 16-bit unsigned integer. It must also be compatible with
** the size of the C stack.)
*/
#if !defined(LUAI_MAXCCALLS)
#define LUAI_MAXCCALLS 200
#endif
/*
** macros that are executed whenever program enters the Lua core
** ('lua_lock') and leaves the core ('lua_unlock')
*/
#if !defined(lua_lock)
#define lua_lock(L) ((void) 0)
#define lua_unlock(L) ((void) 0)
#endif
/*
** macro executed during Lua functions at points where the
** function can yield.
*/
#if !defined(luai_threadyield)
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
#endif
/*
** these macros allow user-specific actions when a thread is
** created/deleted/resumed/yielded.
*/
#if !defined(luai_userstateopen)
#define luai_userstateopen(L) ((void)L)
#endif
#if !defined(luai_userstateclose)
#define luai_userstateclose(L) ((void)L)
#endif
#if !defined(luai_userstatethread)
#define luai_userstatethread(L,L1) ((void)L)
#endif
#if !defined(luai_userstatefree)
#define luai_userstatefree(L,L1) ((void)L)
#endif
#if !defined(luai_userstateresume)
#define luai_userstateresume(L,n) ((void)L)
#endif
#if !defined(luai_userstateyield)
#define luai_userstateyield(L,n) ((void)L)
#endif
/*
** The luai_num* macros define the primitive operations over numbers.
*/
/* floor division (defined as 'floor(a/b)') */
#if !defined(luai_numidiv)
#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
#endif
/* float division */
#if !defined(luai_numdiv)
#define luai_numdiv(L,a,b) ((a)/(b))
#endif
/*
** modulo: defined as 'a - floor(a/b)*b'; the direct computation
** using this definition has several problems with rounding errors,
** so it is better to use 'fmod'. 'fmod' gives the result of
** 'a - trunc(a/b)*b', and therefore must be corrected when
** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
** non-integer negative result: non-integer result is equivalent to
** a non-zero remainder 'm'; negative result is equivalent to 'a' and
** 'b' with different signs, or 'm' and 'b' with different signs
** (as the result 'm' of 'fmod' has the same sign of 'a').
*/
#if !defined(luai_nummod)
#define luai_nummod(L,a,b,m) \
{ (void)L; (m) = l_mathop(fmod)(a,b); \
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
#endif
/* exponentiation */
#if !defined(luai_numpow)
#define luai_numpow(L,a,b) \
((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
#endif
/* the others are quite standard operations */
#if !defined(luai_numadd)
#define luai_numadd(L,a,b) ((a)+(b))
#define luai_numsub(L,a,b) ((a)-(b))
#define luai_nummul(L,a,b) ((a)*(b))
#define luai_numunm(L,a) (-(a))
#define luai_numeq(a,b) ((a)==(b))
#define luai_numlt(a,b) ((a)<(b))
#define luai_numle(a,b) ((a)<=(b))
#define luai_numgt(a,b) ((a)>(b))
#define luai_numge(a,b) ((a)>=(b))
#define luai_numisnan(a) (!luai_numeq((a), (a)))
#endif
/*
** macro to control inclusion of some hard tests on stack reallocation
*/
#if !defined(HARDSTACKTESTS)
#define condmovestack(L,pre,pos) ((void)0)
#else
/* realloc stack keeping its size */
#define condmovestack(L,pre,pos) \
{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
#endif
#if !defined(HARDMEMTESTS)
#define condchangemem(L,pre,pos) ((void)0)
#else
#define condchangemem(L,pre,pos) \
{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
#endif
#endif

781
arm9/source/lua/lmathlib.c Normal file
View File

@ -0,0 +1,781 @@
/*
** $Id: lmathlib.c $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
#define lmathlib_c
#define LUA_LIB
#include "lprefix.h"
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#undef PI
#define PI (l_mathop(3.141592653589793238462643383279502884))
static int math_abs (lua_State *L) {
if (lua_isinteger(L, 1)) {
lua_Integer n = lua_tointeger(L, 1);
if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
lua_pushinteger(L, n);
}
else
lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
return 1;
}
static int math_sin (lua_State *L) {
lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
return 1;
}
static int math_cos (lua_State *L) {
lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
return 1;
}
static int math_tan (lua_State *L) {
lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
return 1;
}
static int math_asin (lua_State *L) {
lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
return 1;
}
static int math_acos (lua_State *L) {
lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
return 1;
}
static int math_atan (lua_State *L) {
lua_Number y = luaL_checknumber(L, 1);
lua_Number x = luaL_optnumber(L, 2, 1);
lua_pushnumber(L, l_mathop(atan2)(y, x));
return 1;
}
static int math_toint (lua_State *L) {
int valid;
lua_Integer n = lua_tointegerx(L, 1, &valid);
if (l_likely(valid))
lua_pushinteger(L, n);
else {
luaL_checkany(L, 1);
luaL_pushfail(L); /* value is not convertible to integer */
}
return 1;
}
static void pushnumint (lua_State *L, lua_Number d) {
lua_Integer n;
if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
lua_pushinteger(L, n); /* result is integer */
else
lua_pushnumber(L, d); /* result is float */
}
static int math_floor (lua_State *L) {
if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own floor */
else {
lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
pushnumint(L, d);
}
return 1;
}
static int math_ceil (lua_State *L) {
if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own ceil */
else {
lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
pushnumint(L, d);
}
return 1;
}
static int math_fmod (lua_State *L) {
if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
lua_Integer d = lua_tointeger(L, 2);
if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
luaL_argcheck(L, d != 0, 2, "zero");
lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
}
else
lua_pushinteger(L, lua_tointeger(L, 1) % d);
}
else
lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
luaL_checknumber(L, 2)));
return 1;
}
/*
** next function does not use 'modf', avoiding problems with 'double*'
** (which is not compatible with 'float*') when lua_Number is not
** 'double'.
*/
static int math_modf (lua_State *L) {
if (lua_isinteger(L ,1)) {
lua_settop(L, 1); /* number is its own integer part */
lua_pushnumber(L, 0); /* no fractional part */
}
else {
lua_Number n = luaL_checknumber(L, 1);
/* integer part (rounds toward zero) */
lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
pushnumint(L, ip);
/* fractional part (test needed for inf/-inf) */
lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
}
return 2;
}
static int math_sqrt (lua_State *L) {
lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
return 1;
}
static int math_ult (lua_State *L) {
lua_Integer a = luaL_checkinteger(L, 1);
lua_Integer b = luaL_checkinteger(L, 2);
lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
return 1;
}
static int math_log (lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number res;
if (lua_isnoneornil(L, 2))
res = l_mathop(log)(x);
else {
lua_Number base = luaL_checknumber(L, 2);
#if !defined(LUA_USE_C89)
if (base == l_mathop(2.0))
res = l_mathop(log2)(x);
else
#endif
if (base == l_mathop(10.0))
res = l_mathop(log10)(x);
else
res = l_mathop(log)(x)/l_mathop(log)(base);
}
lua_pushnumber(L, res);
return 1;
}
static int math_exp (lua_State *L) {
lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
return 1;
}
static int math_deg (lua_State *L) {
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
return 1;
}
static int math_rad (lua_State *L) {
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
return 1;
}
static int math_min (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int imin = 1; /* index of current minimum value */
int i;
luaL_argcheck(L, n >= 1, 1, "value expected");
for (i = 2; i <= n; i++) {
if (lua_compare(L, i, imin, LUA_OPLT))
imin = i;
}
lua_pushvalue(L, imin);
return 1;
}
static int math_max (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int imax = 1; /* index of current maximum value */
int i;
luaL_argcheck(L, n >= 1, 1, "value expected");
for (i = 2; i <= n; i++) {
if (lua_compare(L, imax, i, LUA_OPLT))
imax = i;
}
lua_pushvalue(L, imax);
return 1;
}
static int math_type (lua_State *L) {
if (lua_type(L, 1) == LUA_TNUMBER)
lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
else {
luaL_checkany(L, 1);
luaL_pushfail(L);
}
return 1;
}
/*
** {==================================================================
** Pseudo-Random Number Generator based on 'xoshiro256**'.
** ===================================================================
*/
/*
** This code uses lots of shifts. ANSI C does not allow shifts greater
** than or equal to the width of the type being shifted, so some shifts
** are written in convoluted ways to match that restriction. For
** preprocessor tests, it assumes a width of 32 bits, so the maximum
** shift there is 31 bits.
*/
/* number of binary digits in the mantissa of a float */
#define FIGS l_floatatt(MANT_DIG)
#if FIGS > 64
/* there are only 64 random bits; use them all */
#undef FIGS
#define FIGS 64
#endif
/*
** LUA_RAND32 forces the use of 32-bit integers in the implementation
** of the PRN generator (mainly for testing).
*/
#if !defined(LUA_RAND32) && !defined(Rand64)
/* try to find an integer type with at least 64 bits */
#if ((ULONG_MAX >> 31) >> 31) >= 3
/* 'long' has at least 64 bits */
#define Rand64 unsigned long
#define SRand64 long
#elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
/* there is a 'long long' type (which must have at least 64 bits) */
#define Rand64 unsigned long long
#define SRand64 long long
#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3
/* 'lua_Unsigned' has at least 64 bits */
#define Rand64 lua_Unsigned
#define SRand64 lua_Integer
#endif
#endif
#if defined(Rand64) /* { */
/*
** Standard implementation, using 64-bit integers.
** If 'Rand64' has more than 64 bits, the extra bits do not interfere
** with the 64 initial bits, except in a right shift. Moreover, the
** final result has to discard the extra bits.
*/
/* avoid using extra bits when needed */
#define trim64(x) ((x) & 0xffffffffffffffffu)
/* rotate left 'x' by 'n' bits */
static Rand64 rotl (Rand64 x, int n) {
return (x << n) | (trim64(x) >> (64 - n));
}
static Rand64 nextrand (Rand64 *state) {
Rand64 state0 = state[0];
Rand64 state1 = state[1];
Rand64 state2 = state[2] ^ state0;
Rand64 state3 = state[3] ^ state1;
Rand64 res = rotl(state1 * 5, 7) * 9;
state[0] = state0 ^ state3;
state[1] = state1 ^ state2;
state[2] = state2 ^ (state1 << 17);
state[3] = rotl(state3, 45);
return res;
}
/*
** Convert bits from a random integer into a float in the
** interval [0,1), getting the higher FIG bits from the
** random unsigned integer and converting that to a float.
** Some old Microsoft compilers cannot cast an unsigned long
** to a floating-point number, so we use a signed long as an
** intermediary. When lua_Number is float or double, the shift ensures
** that 'sx' is non negative; in that case, a good compiler will remove
** the correction.
*/
/* must throw out the extra (64 - FIGS) bits */
#define shift64_FIG (64 - FIGS)
/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */
#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
static lua_Number I2d (Rand64 x) {
SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG);
lua_Number res = (lua_Number)(sx) * scaleFIG;
if (sx < 0)
res += l_mathop(1.0); /* correct the two's complement if negative */
lua_assert(0 <= res && res < 1);
return res;
}
/* convert a 'Rand64' to a 'lua_Unsigned' */
#define I2UInt(x) ((lua_Unsigned)trim64(x))
/* convert a 'lua_Unsigned' to a 'Rand64' */
#define Int2I(x) ((Rand64)(x))
#else /* no 'Rand64' }{ */
/* get an integer with at least 32 bits */
#if LUAI_IS32INT
typedef unsigned int lu_int32;
#else
typedef unsigned long lu_int32;
#endif
/*
** Use two 32-bit integers to represent a 64-bit quantity.
*/
typedef struct Rand64 {
lu_int32 h; /* higher half */
lu_int32 l; /* lower half */
} Rand64;
/*
** If 'lu_int32' has more than 32 bits, the extra bits do not interfere
** with the 32 initial bits, except in a right shift and comparisons.
** Moreover, the final result has to discard the extra bits.
*/
/* avoid using extra bits when needed */
#define trim32(x) ((x) & 0xffffffffu)
/*
** basic operations on 'Rand64' values
*/
/* build a new Rand64 value */
static Rand64 packI (lu_int32 h, lu_int32 l) {
Rand64 result;
result.h = h;
result.l = l;
return result;
}
/* return i << n */
static Rand64 Ishl (Rand64 i, int n) {
lua_assert(n > 0 && n < 32);
return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
}
/* i1 ^= i2 */
static void Ixor (Rand64 *i1, Rand64 i2) {
i1->h ^= i2.h;
i1->l ^= i2.l;
}
/* return i1 + i2 */
static Rand64 Iadd (Rand64 i1, Rand64 i2) {
Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
if (trim32(result.l) < trim32(i1.l)) /* carry? */
result.h++;
return result;
}
/* return i * 5 */
static Rand64 times5 (Rand64 i) {
return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
}
/* return i * 9 */
static Rand64 times9 (Rand64 i) {
return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
}
/* return 'i' rotated left 'n' bits */
static Rand64 rotl (Rand64 i, int n) {
lua_assert(n > 0 && n < 32);
return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
(trim32(i.h) >> (32 - n)) | (i.l << n));
}
/* for offsets larger than 32, rotate right by 64 - offset */
static Rand64 rotl1 (Rand64 i, int n) {
lua_assert(n > 32 && n < 64);
n = 64 - n;
return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
(i.h << (32 - n)) | (trim32(i.l) >> n));
}
/*
** implementation of 'xoshiro256**' algorithm on 'Rand64' values
*/
static Rand64 nextrand (Rand64 *state) {
Rand64 res = times9(rotl(times5(state[1]), 7));
Rand64 t = Ishl(state[1], 17);
Ixor(&state[2], state[0]);
Ixor(&state[3], state[1]);
Ixor(&state[1], state[2]);
Ixor(&state[0], state[3]);
Ixor(&state[2], t);
state[3] = rotl1(state[3], 45);
return res;
}
/*
** Converts a 'Rand64' into a float.
*/
/* an unsigned 1 with proper type */
#define UONE ((lu_int32)1)
#if FIGS <= 32
/* 2^(-FIGS) */
#define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1)))
/*
** get up to 32 bits from higher half, shifting right to
** throw out the extra bits.
*/
static lua_Number I2d (Rand64 x) {
lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS));
return h * scaleFIG;
}
#else /* 32 < FIGS <= 64 */
/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
#define scaleFIG \
(l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33)))
/*
** use FIGS - 32 bits from lower half, throwing out the other
** (32 - (FIGS - 32)) = (64 - FIGS) bits
*/
#define shiftLOW (64 - FIGS)
/*
** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
*/
#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0))
static lua_Number I2d (Rand64 x) {
lua_Number h = (lua_Number)trim32(x.h) * shiftHI;
lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW);
return (h + l) * scaleFIG;
}
#endif
/* convert a 'Rand64' to a 'lua_Unsigned' */
static lua_Unsigned I2UInt (Rand64 x) {
return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l);
}
/* convert a 'lua_Unsigned' to a 'Rand64' */
static Rand64 Int2I (lua_Unsigned n) {
return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n);
}
#endif /* } */
/*
** A state uses four 'Rand64' values.
*/
typedef struct {
Rand64 s[4];
} RanState;
/*
** Project the random integer 'ran' into the interval [0, n].
** Because 'ran' has 2^B possible values, the projection can only be
** uniform when the size of the interval is a power of 2 (exact
** division). Otherwise, to get a uniform projection into [0, n], we
** first compute 'lim', the smallest Mersenne number not smaller than
** 'n'. We then project 'ran' into the interval [0, lim]. If the result
** is inside [0, n], we are done. Otherwise, we try with another 'ran',
** until we have a result inside the interval.
*/
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
RanState *state) {
if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
return ran & n; /* no bias */
else {
lua_Unsigned lim = n;
/* compute the smallest (2^b - 1) not smaller than 'n' */
lim |= (lim >> 1);
lim |= (lim >> 2);
lim |= (lim >> 4);
lim |= (lim >> 8);
lim |= (lim >> 16);
#if (LUA_MAXUNSIGNED >> 31) >= 3
lim |= (lim >> 32); /* integer type has more than 32 bits */
#endif
lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
&& lim >= n /* not smaller than 'n', */
&& (lim >> 1) < n); /* and it is the smallest one */
while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
return ran;
}
}
static int math_random (lua_State *L) {
lua_Integer low, up;
lua_Unsigned p;
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
Rand64 rv = nextrand(state->s); /* next pseudo-random value */
switch (lua_gettop(L)) { /* check number of arguments */
case 0: { /* no arguments */
lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
return 1;
}
case 1: { /* only upper limit */
low = 1;
up = luaL_checkinteger(L, 1);
if (up == 0) { /* single 0 as argument? */
lua_pushinteger(L, I2UInt(rv)); /* full random integer */
return 1;
}
break;
}
case 2: { /* lower and upper limits */
low = luaL_checkinteger(L, 1);
up = luaL_checkinteger(L, 2);
break;
}
default: return luaL_error(L, "wrong number of arguments");
}
/* random integer in the interval [low, up] */
luaL_argcheck(L, low <= up, 1, "interval is empty");
/* project random integer into the interval [0, up - low] */
p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
lua_pushinteger(L, p + (lua_Unsigned)low);
return 1;
}
static void setseed (lua_State *L, Rand64 *state,
lua_Unsigned n1, lua_Unsigned n2) {
int i;
state[0] = Int2I(n1);
state[1] = Int2I(0xff); /* avoid a zero state */
state[2] = Int2I(n2);
state[3] = Int2I(0);
for (i = 0; i < 16; i++)
nextrand(state); /* discard initial values to "spread" seed */
lua_pushinteger(L, n1);
lua_pushinteger(L, n2);
}
/*
** Set a "random" seed. To get some randomness, use the current time
** and the address of 'L' (in case the machine does address space layout
** randomization).
*/
static void randseed (lua_State *L, RanState *state) {
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
setseed(L, state->s, seed1, seed2);
}
static int math_randomseed (lua_State *L) {
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
if (lua_isnone(L, 1)) {
randseed(L, state);
}
else {
lua_Integer n1 = luaL_checkinteger(L, 1);
lua_Integer n2 = luaL_optinteger(L, 2, 0);
setseed(L, state->s, n1, n2);
}
return 2; /* return seeds */
}
static const luaL_Reg randfuncs[] = {
{"random", math_random},
{"randomseed", math_randomseed},
{NULL, NULL}
};
/*
** Register the random functions and initialize their state.
*/
static void setrandfunc (lua_State *L) {
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
randseed(L, state); /* initialize with a "random" seed */
lua_pop(L, 2); /* remove pushed seeds */
luaL_setfuncs(L, randfuncs, 1);
}
/* }================================================================== */
/*
** {==================================================================
** Deprecated functions (for compatibility only)
** ===================================================================
*/
#if defined(LUA_COMPAT_MATHLIB)
static int math_cosh (lua_State *L) {
lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_sinh (lua_State *L) {
lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_tanh (lua_State *L) {
lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_pow (lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2);
lua_pushnumber(L, l_mathop(pow)(x, y));
return 1;
}
static int math_frexp (lua_State *L) {
int e;
lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
lua_pushinteger(L, e);
return 2;
}
static int math_ldexp (lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
int ep = (int)luaL_checkinteger(L, 2);
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
return 1;
}
static int math_log10 (lua_State *L) {
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
return 1;
}
#endif
/* }================================================================== */
static const luaL_Reg mathlib[] = {
{"abs", math_abs},
{"acos", math_acos},
{"asin", math_asin},
{"atan", math_atan},
{"ceil", math_ceil},
{"cos", math_cos},
{"deg", math_deg},
{"exp", math_exp},
{"tointeger", math_toint},
{"floor", math_floor},
{"fmod", math_fmod},
{"ult", math_ult},
{"log", math_log},
{"max", math_max},
{"min", math_min},
{"modf", math_modf},
{"rad", math_rad},
{"sin", math_sin},
{"sqrt", math_sqrt},
{"tan", math_tan},
{"type", math_type},
#if defined(LUA_COMPAT_MATHLIB)
{"atan2", math_atan},
{"cosh", math_cosh},
{"sinh", math_sinh},
{"tanh", math_tanh},
{"pow", math_pow},
{"frexp", math_frexp},
{"ldexp", math_ldexp},
{"log10", math_log10},
#endif
/* placeholders */
{"random", NULL},
{"randomseed", NULL},
{"pi", NULL},
{"huge", NULL},
{"maxinteger", NULL},
{"mininteger", NULL},
{NULL, NULL}
};
/*
** Open math library
*/
LUAMOD_API int luaopen_math (lua_State *L) {
luaL_newlib(L, mathlib);
lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi");
lua_pushnumber(L, (lua_Number)HUGE_VAL);
lua_setfield(L, -2, "huge");
lua_pushinteger(L, LUA_MAXINTEGER);
lua_setfield(L, -2, "maxinteger");
lua_pushinteger(L, LUA_MININTEGER);
lua_setfield(L, -2, "mininteger");
setrandfunc(L);
return 1;
}

215
arm9/source/lua/lmem.c Normal file
View File

@ -0,0 +1,215 @@
/*
** $Id: lmem.c $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
#define lmem_c
#define LUA_CORE
#include "lprefix.h"
#include <stddef.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
/*
** About the realloc function:
** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
** ('osize' is the old size, 'nsize' is the new size)
**
** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
** which is equivalent to free(NULL) in ISO C.
**
** - frealloc(ud, NULL, x, s) creates a new block of size 's'
** (no matter 'x'). Returns NULL if it cannot create the new block.
**
** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
** block to the new size.
*/
/*
** Macro to call the allocation function.
*/
#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
/*
** When an allocation fails, it will try again after an emergency
** collection, except when it cannot run a collection. The GC should
** not be called while the state is not fully built, as the collector
** is not yet fully initialized. Also, it should not be called when
** 'gcstopem' is true, because then the interpreter is in the middle of
** a collection step.
*/
#define cantryagain(g) (completestate(g) && !g->gcstopem)
#if defined(EMERGENCYGCTESTS)
/*
** First allocation will fail except when freeing a block (frees never
** fail) and when it cannot try again; this fail will trigger 'tryagain'
** and a full GC cycle at every allocation.
*/
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
if (ns > 0 && cantryagain(g))
return NULL; /* fail */
else /* normal allocation */
return callfrealloc(g, block, os, ns);
}
#else
#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns)
#endif
/*
** {==================================================================
** Functions to allocate/deallocate arrays for the Parser
** ===================================================================
*/
/*
** Minimum size for arrays during parsing, to avoid overhead of
** reallocating to size 1, then 2, and then 4. All these arrays
** will be reallocated to exact sizes or erased when parsing ends.
*/
#define MINSIZEARRAY 4
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
int size_elems, int limit, const char *what) {
void *newblock;
int size = *psize;
if (nelems + 1 <= size) /* does one extra element still fit? */
return block; /* nothing to be done */
if (size >= limit / 2) { /* cannot double it? */
if (l_unlikely(size >= limit)) /* cannot grow even a little? */
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
size = limit; /* still have at least one free place */
}
else {
size *= 2;
if (size < MINSIZEARRAY)
size = MINSIZEARRAY; /* minimum size */
}
lua_assert(nelems + 1 <= size && size <= limit);
/* 'limit' ensures that multiplication will not overflow */
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
cast_sizet(size) * size_elems);
*psize = size; /* update only when everything else is OK */
return newblock;
}
/*
** In prototypes, the size of the array is also its number of
** elements (to save memory). So, if it cannot shrink an array
** to its number of elements, the only option is to raise an
** error.
*/
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
int final_n, int size_elem) {
void *newblock;
size_t oldsize = cast_sizet((*size) * size_elem);
size_t newsize = cast_sizet(final_n * size_elem);
lua_assert(newsize <= oldsize);
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
*size = final_n;
return newblock;
}
/* }================================================================== */
l_noret luaM_toobig (lua_State *L) {
luaG_runerror(L, "memory allocation error: block too big");
}
/*
** Free memory
*/
void luaM_free_ (lua_State *L, void *block, size_t osize) {
global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL));
callfrealloc(g, block, osize, 0);
g->GCdebt -= osize;
}
/*
** In case of allocation fail, this function will do an emergency
** collection to free some memory and then try the allocation again.
*/
static void *tryagain (lua_State *L, void *block,
size_t osize, size_t nsize) {
global_State *g = G(L);
if (cantryagain(g)) {
luaC_fullgc(L, 1); /* try to free some memory... */
return callfrealloc(g, block, osize, nsize); /* try again */
}
else return NULL; /* cannot run an emergency collection */
}
/*
** Generic allocation routine.
*/
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
void *newblock;
global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL));
newblock = firsttry(g, block, osize, nsize);
if (l_unlikely(newblock == NULL && nsize > 0)) {
newblock = tryagain(L, block, osize, nsize);
if (newblock == NULL) /* still no memory? */
return NULL; /* do not update 'GCdebt' */
}
lua_assert((nsize == 0) == (newblock == NULL));
g->GCdebt = (g->GCdebt + nsize) - osize;
return newblock;
}
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
size_t nsize) {
void *newblock = luaM_realloc_(L, block, osize, nsize);
if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
luaM_error(L);
return newblock;
}
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
if (size == 0)
return NULL; /* that's all */
else {
global_State *g = G(L);
void *newblock = firsttry(g, NULL, tag, size);
if (l_unlikely(newblock == NULL)) {
newblock = tryagain(L, NULL, tag, size);
if (newblock == NULL)
luaM_error(L);
}
g->GCdebt += size;
return newblock;
}
}

93
arm9/source/lua/lmem.h Normal file
View File

@ -0,0 +1,93 @@
/*
** $Id: lmem.h $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
#ifndef lmem_h
#define lmem_h
#include <stddef.h>
#include "llimits.h"
#include "lua.h"
#define luaM_error(L) luaD_throw(L, LUA_ERRMEM)
/*
** This macro tests whether it is safe to multiply 'n' by the size of
** type 't' without overflows. Because 'e' is always constant, it avoids
** the runtime division MAX_SIZET/(e).
** (The macro is somewhat complex to avoid warnings: The 'sizeof'
** comparison avoids a runtime comparison when overflow cannot occur.
** The compiler should be able to optimize the real test by itself, but
** when it does it, it may give a warning about "comparison is always
** false due to limited range of data type"; the +1 tricks the compiler,
** avoiding this warning but also this optimization.)
*/
#define luaM_testsize(n,e) \
(sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
#define luaM_checksize(L,n,e) \
(luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
/*
** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
** the result is not larger than 'n' and cannot overflow a 'size_t'
** when multiplied by the size of type 't'. (Assumes that 'n' is an
** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
*/
#define luaM_limitN(n,t) \
((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
cast_uint((MAX_SIZET/sizeof(t))))
/*
** Arrays of chars do not need any test
*/
#define luaM_reallocvchar(L,b,on,n) \
cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s))
#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b)))
#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
#define luaM_newvectorchecked(L,n,t) \
(luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
#define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag)
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
luaM_limitN(limit,t),e)))
#define luaM_reallocvector(L, v,oldn,n,t) \
(cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
cast_sizet(n) * sizeof(t))))
#define luaM_shrinkvector(L,v,size,fs,t) \
((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
LUAI_FUNC l_noret luaM_toobig (lua_State *L);
/* not to be called directly */
LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
size_t size);
LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
size_t size);
LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
int *size, int size_elem, int limit,
const char *what);
LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
int final_n, int size_elem);
LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
#endif

758
arm9/source/lua/loadlib.c Normal file
View File

@ -0,0 +1,758 @@
/*
** $Id: loadlib.c $
** Dynamic library loader for Lua
** See Copyright Notice in lua.h
**
** This module contains an implementation of loadlib for Unix systems
** that have dlfcn, an implementation for Windows, and a stub for other
** systems.
*/
#define loadlib_c
#define LUA_LIB
#include "lprefix.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/*
** LUA_CSUBSEP is the character that replaces dots in submodule names
** when searching for a C loader.
** LUA_LSUBSEP is the character that replaces dots in submodule names
** when searching for a Lua loader.
*/
#if !defined(LUA_CSUBSEP)
#define LUA_CSUBSEP LUA_DIRSEP
#endif
#if !defined(LUA_LSUBSEP)
#define LUA_LSUBSEP LUA_DIRSEP
#endif
/* prefix for open functions in C libraries */
#define LUA_POF "luaopen_"
/* separator for open functions in C libraries */
#define LUA_OFSEP "_"
/*
** key for table in the registry that keeps handles
** for all loaded C libraries
*/
static const char *const CLIBS = "_CLIBS";
#define LIB_FAIL "open"
#define setprogdir(L) ((void)0)
/*
** Special type equivalent to '(void*)' for functions in gcc
** (to suppress warnings when converting function pointers)
*/
typedef void (*voidf)(void);
/*
** system-dependent functions
*/
/*
** unload library 'lib'
*/
static void lsys_unloadlib (void *lib);
/*
** load C library in file 'path'. If 'seeglb', load with all names in
** the library global.
** Returns the library; in case of error, returns NULL plus an
** error string in the stack.
*/
static void *lsys_load (lua_State *L, const char *path, int seeglb);
/*
** Try to find a function named 'sym' in library 'lib'.
** Returns the function; in case of error, returns NULL plus an
** error string in the stack.
*/
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
#if defined(LUA_USE_DLOPEN) /* { */
/*
** {========================================================================
** This is an implementation of loadlib based on the dlfcn interface.
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
** as an emulation layer on top of native functions.
** =========================================================================
*/
#include <dlfcn.h>
/*
** Macro to convert pointer-to-void* to pointer-to-function. This cast
** is undefined according to ISO C, but POSIX assumes that it works.
** (The '__extension__' in gnu compilers is only to avoid warnings.)
*/
#if defined(__GNUC__)
#define cast_func(p) (__extension__ (lua_CFunction)(p))
#else
#define cast_func(p) ((lua_CFunction)(p))
#endif
static void lsys_unloadlib (void *lib) {
dlclose(lib);
}
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
if (l_unlikely(lib == NULL))
lua_pushstring(L, dlerror());
return lib;
}
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
lua_CFunction f = cast_func(dlsym(lib, sym));
if (l_unlikely(f == NULL))
lua_pushstring(L, dlerror());
return f;
}
/* }====================================================== */
#elif defined(LUA_DL_DLL) /* }{ */
/*
** {======================================================================
** This is an implementation of loadlib for Windows using native functions.
** =======================================================================
*/
#include <windows.h>
/*
** optional flags for LoadLibraryEx
*/
#if !defined(LUA_LLE_FLAGS)
#define LUA_LLE_FLAGS 0
#endif
#undef setprogdir
/*
** Replace in the path (on the top of the stack) any occurrence
** of LUA_EXEC_DIR with the executable's path.
*/
static void setprogdir (lua_State *L) {
char buff[MAX_PATH + 1];
char *lb;
DWORD nsize = sizeof(buff)/sizeof(char);
DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
luaL_error(L, "unable to get ModuleFileName");
else {
*lb = '\0'; /* cut name on the last '\\' to get the path */
luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
lua_remove(L, -2); /* remove original string */
}
}
static void pusherror (lua_State *L) {
int error = GetLastError();
char buffer[128];
if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
lua_pushstring(L, buffer);
else
lua_pushfstring(L, "system error %d\n", error);
}
static void lsys_unloadlib (void *lib) {
FreeLibrary((HMODULE)lib);
}
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
(void)(seeglb); /* not used: symbols are 'global' by default */
if (lib == NULL) pusherror(L);
return lib;
}
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
if (f == NULL) pusherror(L);
return f;
}
/* }====================================================== */
#else /* }{ */
/*
** {======================================================
** Fallback for other systems
** =======================================================
*/
#undef LIB_FAIL
#define LIB_FAIL "absent"
#define DLMSG "dynamic libraries not enabled; check your Lua installation"
static void lsys_unloadlib (void *lib) {
(void)(lib); /* not used */
}
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
(void)(path); (void)(seeglb); /* not used */
lua_pushliteral(L, DLMSG);
return NULL;
}
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
(void)(lib); (void)(sym); /* not used */
lua_pushliteral(L, DLMSG);
return NULL;
}
/* }====================================================== */
#endif /* } */
/*
** {==================================================================
** Set Paths
** ===================================================================
*/
/*
** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
** variables that Lua check to set its paths.
*/
#if !defined(LUA_PATH_VAR)
#define LUA_PATH_VAR "LUA_PATH"
#endif
#if !defined(LUA_CPATH_VAR)
#define LUA_CPATH_VAR "LUA_CPATH"
#endif
/*
** return registry.LUA_NOENV as a boolean
*/
static int noenv (lua_State *L) {
int b;
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
b = lua_toboolean(L, -1);
lua_pop(L, 1); /* remove value */
return b;
}
/*
** Set a path
*/
static void setpath (lua_State *L, const char *fieldname,
const char *envname,
const char *dft) {
const char *dftmark;
const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
const char *path = getenv(nver); /* try versioned name */
if (path == NULL) /* no versioned environment variable? */
path = getenv(envname); /* try unversioned name */
if (path == NULL || noenv(L)) /* no environment variable? */
lua_pushstring(L, dft); /* use default */
else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
lua_pushstring(L, path); /* nothing to change */
else { /* path contains a ";;": insert default path in its place */
size_t len = strlen(path);
luaL_Buffer b;
luaL_buffinit(L, &b);
if (path < dftmark) { /* is there a prefix before ';;'? */
luaL_addlstring(&b, path, dftmark - path); /* add it */
luaL_addchar(&b, *LUA_PATH_SEP);
}
luaL_addstring(&b, dft); /* add default */
if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
luaL_addchar(&b, *LUA_PATH_SEP);
luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
}
luaL_pushresult(&b);
}
setprogdir(L);
lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
lua_pop(L, 1); /* pop versioned variable name ('nver') */
}
/* }================================================================== */
/*
** return registry.CLIBS[path]
*/
static void *checkclib (lua_State *L, const char *path) {
void *plib;
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
lua_getfield(L, -1, path);
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
return plib;
}
/*
** registry.CLIBS[path] = plib -- for queries
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
*/
static void addtoclib (lua_State *L, const char *path, void *plib) {
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
lua_pushlightuserdata(L, plib);
lua_pushvalue(L, -1);
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
lua_pop(L, 1); /* pop CLIBS table */
}
/*
** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
** handles in list CLIBS
*/
static int gctm (lua_State *L) {
lua_Integer n = luaL_len(L, 1);
for (; n >= 1; n--) { /* for each handle, in reverse order */
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
lsys_unloadlib(lua_touserdata(L, -1));
lua_pop(L, 1); /* pop handle */
}
return 0;
}
/* error codes for 'lookforfunc' */
#define ERRLIB 1
#define ERRFUNC 2
/*
** Look for a C function named 'sym' in a dynamically loaded library
** 'path'.
** First, check whether the library is already loaded; if not, try
** to load it.
** Then, if 'sym' is '*', return true (as library has been loaded).
** Otherwise, look for symbol 'sym' in the library and push a
** C function with that symbol.
** Return 0 and 'true' or a function in the stack; in case of
** errors, return an error code and an error message in the stack.
*/
static int lookforfunc (lua_State *L, const char *path, const char *sym) {
void *reg = checkclib(L, path); /* check loaded C libraries */
if (reg == NULL) { /* must load library? */
reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
if (reg == NULL) return ERRLIB; /* unable to load library */
addtoclib(L, path, reg);
}
if (*sym == '*') { /* loading only library (no function)? */
lua_pushboolean(L, 1); /* return 'true' */
return 0; /* no errors */
}
else {
lua_CFunction f = lsys_sym(L, reg, sym);
if (f == NULL)
return ERRFUNC; /* unable to find function */
lua_pushcfunction(L, f); /* else create new function */
return 0; /* no errors */
}
}
static int ll_loadlib (lua_State *L) {
const char *path = luaL_checkstring(L, 1);
const char *init = luaL_checkstring(L, 2);
int stat = lookforfunc(L, path, init);
if (l_likely(stat == 0)) /* no errors? */
return 1; /* return the loaded function */
else { /* error; error message is on stack top */
luaL_pushfail(L);
lua_insert(L, -2);
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
return 3; /* return fail, error message, and where */
}
}
/*
** {======================================================
** 'require' function
** =======================================================
*/
static int readable (const char *filename) {
FILE *f = fopen(filename, "r"); /* try to open file */
if (f == NULL) return 0; /* open failed */
fclose(f);
return 1;
}
/*
** Get the next name in '*path' = 'name1;name2;name3;...', changing
** the ending ';' to '\0' to create a zero-terminated string. Return
** NULL when list ends.
*/
static const char *getnextfilename (char **path, char *end) {
char *sep;
char *name = *path;
if (name == end)
return NULL; /* no more names */
else if (*name == '\0') { /* from previous iteration? */
*name = *LUA_PATH_SEP; /* restore separator */
name++; /* skip it */
}
sep = strchr(name, *LUA_PATH_SEP); /* find next separator */
if (sep == NULL) /* separator not found? */
sep = end; /* name goes until the end */
*sep = '\0'; /* finish file name */
*path = sep; /* will start next search from here */
return name;
}
/*
** Given a path such as ";blabla.so;blublu.so", pushes the string
**
** no file 'blabla.so'
** no file 'blublu.so'
*/
static void pusherrornotfound (lua_State *L, const char *path) {
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addstring(&b, "no file '");
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
luaL_addstring(&b, "'");
luaL_pushresult(&b);
}
static const char *searchpath (lua_State *L, const char *name,
const char *path,
const char *sep,
const char *dirsep) {
luaL_Buffer buff;
char *pathname; /* path with name inserted */
char *endpathname; /* its end */
const char *filename;
/* separator is non-empty and appears in 'name'? */
if (*sep != '\0' && strchr(name, *sep) != NULL)
name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
luaL_buffinit(L, &buff);
/* add path to the buffer, replacing marks ('?') with the file name */
luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
luaL_addchar(&buff, '\0');
pathname = luaL_buffaddr(&buff); /* writable list of file names */
endpathname = pathname + luaL_bufflen(&buff) - 1;
while ((filename = getnextfilename(&pathname, endpathname)) != NULL) {
if (readable(filename)) /* does file exist and is readable? */
return lua_pushstring(L, filename); /* save and return name */
}
luaL_pushresult(&buff); /* push path to create error message */
pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */
return NULL; /* not found */
}
static int ll_searchpath (lua_State *L) {
const char *f = searchpath(L, luaL_checkstring(L, 1),
luaL_checkstring(L, 2),
luaL_optstring(L, 3, "."),
luaL_optstring(L, 4, LUA_DIRSEP));
if (f != NULL) return 1;
else { /* error message is on top of the stack */
luaL_pushfail(L);
lua_insert(L, -2);
return 2; /* return fail + error message */
}
}
static const char *findfile (lua_State *L, const char *name,
const char *pname,
const char *dirsep) {
const char *path;
lua_getfield(L, lua_upvalueindex(1), pname);
path = lua_tostring(L, -1);
if (l_unlikely(path == NULL))
luaL_error(L, "'package.%s' must be a string", pname);
return searchpath(L, name, path, ".", dirsep);
}
static int checkload (lua_State *L, int stat, const char *filename) {
if (l_likely(stat)) { /* module loaded successfully? */
lua_pushstring(L, filename); /* will be 2nd argument to module */
return 2; /* return open function and file name */
}
else
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1));
}
static int searcher_Lua (lua_State *L) {
const char *filename;
const char *name = luaL_checkstring(L, 1);
filename = findfile(L, name, "path", LUA_LSUBSEP);
if (filename == NULL) return 1; /* module not found in this path */
return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
}
/*
** Try to find a load function for module 'modname' at file 'filename'.
** First, change '.' to '_' in 'modname'; then, if 'modname' has
** the form X-Y (that is, it has an "ignore mark"), build a function
** name "luaopen_X" and look for it. (For compatibility, if that
** fails, it also tries "luaopen_Y".) If there is no ignore mark,
** look for a function named "luaopen_modname".
*/
static int loadfunc (lua_State *L, const char *filename, const char *modname) {
const char *openfunc;
const char *mark;
modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
mark = strchr(modname, *LUA_IGMARK);
if (mark) {
int stat;
openfunc = lua_pushlstring(L, modname, mark - modname);
openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
stat = lookforfunc(L, filename, openfunc);
if (stat != ERRFUNC) return stat;
modname = mark + 1; /* else go ahead and try old-style name */
}
openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
return lookforfunc(L, filename, openfunc);
}
static int searcher_C (lua_State *L) {
const char *name = luaL_checkstring(L, 1);
const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
if (filename == NULL) return 1; /* module not found in this path */
return checkload(L, (loadfunc(L, filename, name) == 0), filename);
}
static int searcher_Croot (lua_State *L) {
const char *filename;
const char *name = luaL_checkstring(L, 1);
const char *p = strchr(name, '.');
int stat;
if (p == NULL) return 0; /* is root */
lua_pushlstring(L, name, p - name);
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
if (filename == NULL) return 1; /* root not found */
if ((stat = loadfunc(L, filename, name)) != 0) {
if (stat != ERRFUNC)
return checkload(L, 0, filename); /* real error */
else { /* open function not found */
lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
return 1;
}
}
lua_pushstring(L, filename); /* will be 2nd argument to module */
return 2;
}
static int searcher_preload (lua_State *L) {
const char *name = luaL_checkstring(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
lua_pushfstring(L, "no field package.preload['%s']", name);
return 1;
}
else {
lua_pushliteral(L, ":preload:");
return 2;
}
}
static void findloader (lua_State *L, const char *name) {
int i;
luaL_Buffer msg; /* to build error message */
/* push 'package.searchers' to index 3 in the stack */
if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers")
!= LUA_TTABLE))
luaL_error(L, "'package.searchers' must be a table");
luaL_buffinit(L, &msg);
/* iterate over available searchers to find a loader */
for (i = 1; ; i++) {
luaL_addstring(&msg, "\n\t"); /* error-message prefix */
if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */
lua_pop(L, 1); /* remove nil */
luaL_buffsub(&msg, 2); /* remove prefix */
luaL_pushresult(&msg); /* create error message */
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
}
lua_pushstring(L, name);
lua_call(L, 1, 2); /* call it */
if (lua_isfunction(L, -2)) /* did it find a loader? */
return; /* module loader found */
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
lua_pop(L, 1); /* remove extra return */
luaL_addvalue(&msg); /* concatenate error message */
}
else { /* no error message */
lua_pop(L, 2); /* remove both returns */
luaL_buffsub(&msg, 2); /* remove prefix */
}
}
}
static int ll_require (lua_State *L) {
const char *name = luaL_checkstring(L, 1);
lua_settop(L, 1); /* LOADED table will be at index 2 */
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, 2, name); /* LOADED[name] */
if (lua_toboolean(L, -1)) /* is it there? */
return 1; /* package is already loaded */
/* else must load package */
lua_pop(L, 1); /* remove 'getfield' result */
findloader(L, name);
lua_rotate(L, -2, 1); /* function <-> loader data */
lua_pushvalue(L, 1); /* name is 1st argument to module loader */
lua_pushvalue(L, -3); /* loader data is 2nd argument */
/* stack: ...; loader data; loader function; mod. name; loader data */
lua_call(L, 2, 1); /* run loader to load module */
/* stack: ...; loader data; result from loader */
if (!lua_isnil(L, -1)) /* non-nil return? */
lua_setfield(L, 2, name); /* LOADED[name] = returned value */
else
lua_pop(L, 1); /* pop nil */
if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
lua_pushboolean(L, 1); /* use true as result */
lua_copy(L, -1, -2); /* replace loader result */
lua_setfield(L, 2, name); /* LOADED[name] = true */
}
lua_rotate(L, -2, 1); /* loader data <-> module result */
return 2; /* return module result and loader data */
}
/* }====================================================== */
static const luaL_Reg pk_funcs[] = {
{"loadlib", ll_loadlib},
{"searchpath", ll_searchpath},
/* placeholders */
{"preload", NULL},
{"cpath", NULL},
{"path", NULL},
{"searchers", NULL},
{"loaded", NULL},
{NULL, NULL}
};
static const luaL_Reg ll_funcs[] = {
{"require", ll_require},
{NULL, NULL}
};
static void createsearcherstable (lua_State *L) {
static const lua_CFunction searchers[] = {
searcher_preload,
searcher_Lua,
searcher_C,
searcher_Croot,
NULL
};
int i;
/* create 'searchers' table */
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
/* fill it with predefined searchers */
for (i=0; searchers[i] != NULL; i++) {
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
lua_pushcclosure(L, searchers[i], 1);
lua_rawseti(L, -2, i+1);
}
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
}
/*
** create table CLIBS to keep track of loaded C libraries,
** setting a finalizer to close all libraries when closing state.
*/
static void createclibstable (lua_State *L) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
lua_pushcfunction(L, gctm);
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
lua_setmetatable(L, -2);
}
LUAMOD_API int luaopen_package (lua_State *L) {
createclibstable(L);
luaL_newlib(L, pk_funcs); /* create 'package' table */
createsearcherstable(L);
/* set paths */
setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
/* store config information */
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
lua_setfield(L, -2, "config");
/* set field 'loaded' */
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_setfield(L, -2, "loaded");
/* set field 'preload' */
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
lua_setfield(L, -2, "preload");
lua_pushglobaltable(L);
lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
lua_pop(L, 1); /* pop global table */
return 1; /* return 'package' table */
}

602
arm9/source/lua/lobject.c Normal file
View File

@ -0,0 +1,602 @@
/*
** $Id: lobject.c $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
#define lobject_c
#define LUA_CORE
#include "lprefix.h"
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lctype.h"
#include "ldebug.h"
#include "ldo.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "lvm.h"
/*
** Computes ceil(log2(x))
*/
int luaO_ceillog2 (unsigned int x) {
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
int l = 0;
x--;
while (x >= 256) { l += 8; x >>= 8; }
return l + log_2[x];
}
static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
lua_Integer v2) {
switch (op) {
case LUA_OPADD: return intop(+, v1, v2);
case LUA_OPSUB:return intop(-, v1, v2);
case LUA_OPMUL:return intop(*, v1, v2);
case LUA_OPMOD: return luaV_mod(L, v1, v2);
case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
case LUA_OPBAND: return intop(&, v1, v2);
case LUA_OPBOR: return intop(|, v1, v2);
case LUA_OPBXOR: return intop(^, v1, v2);
case LUA_OPSHL: return luaV_shiftl(v1, v2);
case LUA_OPSHR: return luaV_shiftr(v1, v2);
case LUA_OPUNM: return intop(-, 0, v1);
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
default: lua_assert(0); return 0;
}
}
static lua_Number numarith (lua_State *L, int op, lua_Number v1,
lua_Number v2) {
switch (op) {
case LUA_OPADD: return luai_numadd(L, v1, v2);
case LUA_OPSUB: return luai_numsub(L, v1, v2);
case LUA_OPMUL: return luai_nummul(L, v1, v2);
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
case LUA_OPPOW: return luai_numpow(L, v1, v2);
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
case LUA_OPUNM: return luai_numunm(L, v1);
case LUA_OPMOD: return luaV_modf(L, v1, v2);
default: lua_assert(0); return 0;
}
}
int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
TValue *res) {
switch (op) {
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
case LUA_OPSHL: case LUA_OPSHR:
case LUA_OPBNOT: { /* operate only on integers */
lua_Integer i1; lua_Integer i2;
if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
setivalue(res, intarith(L, op, i1, i2));
return 1;
}
else return 0; /* fail */
}
case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
lua_Number n1; lua_Number n2;
if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; /* fail */
}
default: { /* other operations */
lua_Number n1; lua_Number n2;
if (ttisinteger(p1) && ttisinteger(p2)) {
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
return 1;
}
else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; /* fail */
}
}
}
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
StkId res) {
if (!luaO_rawarith(L, op, p1, p2, s2v(res))) {
/* could not perform raw operation; try metamethod */
luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
}
}
int luaO_hexavalue (int c) {
if (lisdigit(c)) return c - '0';
else return (ltolower(c) - 'a') + 10;
}
static int isneg (const char **s) {
if (**s == '-') { (*s)++; return 1; }
else if (**s == '+') (*s)++;
return 0;
}
/*
** {==================================================================
** Lua's implementation for 'lua_strx2number'
** ===================================================================
*/
#if !defined(lua_strx2number)
/* maximum number of significant digits to read (to avoid overflows
even with single floats) */
#define MAXSIGDIG 30
/*
** convert a hexadecimal numeric string to a number, following
** C99 specification for 'strtod'
*/
static lua_Number lua_strx2number (const char *s, char **endptr) {
int dot = lua_getlocaledecpoint();
lua_Number r = l_mathop(0.0); /* result (accumulator) */
int sigdig = 0; /* number of significant digits */
int nosigdig = 0; /* number of non-significant digits */
int e = 0; /* exponent correction */
int neg; /* 1 if number is negative */
int hasdot = 0; /* true after seen a dot */
*endptr = cast_charp(s); /* nothing is valid yet */
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
neg = isneg(&s); /* check sign */
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
return l_mathop(0.0); /* invalid format (no '0x') */
for (s += 2; ; s++) { /* skip '0x' and read numeral */
if (*s == dot) {
if (hasdot) break; /* second dot? stop loop */
else hasdot = 1;
}
else if (lisxdigit(cast_uchar(*s))) {
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
nosigdig++;
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
r = (r * l_mathop(16.0)) + luaO_hexavalue(*s);
else e++; /* too many digits; ignore, but still count for exponent */
if (hasdot) e--; /* decimal digit? correct exponent */
}
else break; /* neither a dot nor a digit */
}
if (nosigdig + sigdig == 0) /* no digits? */
return l_mathop(0.0); /* invalid format */
*endptr = cast_charp(s); /* valid up to here */
e *= 4; /* each digit multiplies/divides value by 2^4 */
if (*s == 'p' || *s == 'P') { /* exponent part? */
int exp1 = 0; /* exponent value */
int neg1; /* exponent sign */
s++; /* skip 'p' */
neg1 = isneg(&s); /* sign */
if (!lisdigit(cast_uchar(*s)))
return l_mathop(0.0); /* invalid; must have at least one digit */
while (lisdigit(cast_uchar(*s))) /* read exponent */
exp1 = exp1 * 10 + *(s++) - '0';
if (neg1) exp1 = -exp1;
e += exp1;
*endptr = cast_charp(s); /* valid up to here */
}
if (neg) r = -r;
return l_mathop(ldexp)(r, e);
}
#endif
/* }====================================================== */
/* maximum length of a numeral to be converted to a number */
#if !defined (L_MAXLENNUM)
#define L_MAXLENNUM 200
#endif
/*
** Convert string 's' to a Lua number (put in 'result'). Return NULL on
** fail or the address of the ending '\0' on success. ('mode' == 'x')
** means a hexadecimal numeral.
*/
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
char *endptr;
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
: lua_str2number(s, &endptr);
if (endptr == s) return NULL; /* nothing recognized? */
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
}
/*
** Convert string 's' to a Lua number (put in 'result') handling the
** current locale.
** This function accepts both the current locale or a dot as the radix
** mark. If the conversion fails, it may mean number has a dot but
** locale accepts something else. In that case, the code copies 's'
** to a buffer (because 's' is read-only), changes the dot to the
** current locale radix mark, and tries to convert again.
** The variable 'mode' checks for special characters in the string:
** - 'n' means 'inf' or 'nan' (which should be rejected)
** - 'x' means a hexadecimal numeral
** - '.' just optimizes the search for the common case (no special chars)
*/
static const char *l_str2d (const char *s, lua_Number *result) {
const char *endptr;
const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
if (mode == 'n') /* reject 'inf' and 'nan' */
return NULL;
endptr = l_str2dloc(s, result, mode); /* try to convert */
if (endptr == NULL) { /* failed? may be a different locale */
char buff[L_MAXLENNUM + 1];
const char *pdot = strchr(s, '.');
if (pdot == NULL || strlen(s) > L_MAXLENNUM)
return NULL; /* string too long or no dot; fail */
strcpy(buff, s); /* copy string to buffer */
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
endptr = l_str2dloc(buff, result, mode); /* try again */
if (endptr != NULL)
endptr = s + (endptr - buff); /* make relative to 's' */
}
return endptr;
}
#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
#define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
static const char *l_str2int (const char *s, lua_Integer *result) {
lua_Unsigned a = 0;
int empty = 1;
int neg;
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
neg = isneg(&s);
if (s[0] == '0' &&
(s[1] == 'x' || s[1] == 'X')) { /* hex? */
s += 2; /* skip '0x' */
for (; lisxdigit(cast_uchar(*s)); s++) {
a = a * 16 + luaO_hexavalue(*s);
empty = 0;
}
}
else { /* decimal */
for (; lisdigit(cast_uchar(*s)); s++) {
int d = *s - '0';
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
return NULL; /* do not accept it (as integer) */
a = a * 10 + d;
empty = 0;
}
}
while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
else {
*result = l_castU2S((neg) ? 0u - a : a);
return s;
}
}
size_t luaO_str2num (const char *s, TValue *o) {
lua_Integer i; lua_Number n;
const char *e;
if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
setivalue(o, i);
}
else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
setfltvalue(o, n);
}
else
return 0; /* conversion failed */
return (e - s) + 1; /* success; return string size */
}
int luaO_utf8esc (char *buff, unsigned long x) {
int n = 1; /* number of bytes put in buffer (backwards) */
lua_assert(x <= 0x7FFFFFFFu);
if (x < 0x80) /* ascii? */
buff[UTF8BUFFSZ - 1] = cast_char(x);
else { /* need continuation bytes */
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
do { /* add continuation bytes */
buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
x >>= 6; /* remove added bits */
mfb >>= 1; /* now there is one less bit available in first byte */
} while (x > mfb); /* still needs continuation byte? */
buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */
}
return n;
}
/*
** Maximum length of the conversion of a number to a string. Must be
** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT.
** (For a long long int, this is 19 digits plus a sign and a final '\0',
** adding to 21. For a long double, it can go to a sign, 33 digits,
** the dot, an exponent letter, an exponent sign, 5 exponent digits,
** and a final '\0', adding to 43.)
*/
#define MAXNUMBER2STR 44
/*
** Convert a number object to a string, adding it to a buffer
*/
static int tostringbuff (TValue *obj, char *buff) {
int len;
lua_assert(ttisnumber(obj));
if (ttisinteger(obj))
len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj));
else {
len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj));
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
buff[len++] = lua_getlocaledecpoint();
buff[len++] = '0'; /* adds '.0' to result */
}
}
return len;
}
/*
** Convert a number object to a Lua string, replacing the value at 'obj'
*/
void luaO_tostring (lua_State *L, TValue *obj) {
char buff[MAXNUMBER2STR];
int len = tostringbuff(obj, buff);
setsvalue(L, obj, luaS_newlstr(L, buff, len));
}
/*
** {==================================================================
** 'luaO_pushvfstring'
** ===================================================================
*/
/*
** Size for buffer space used by 'luaO_pushvfstring'. It should be
** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages,
** so that 'luaG_addinfo' can work directly on the buffer.
*/
#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95)
/* buffer used by 'luaO_pushvfstring' */
typedef struct BuffFS {
lua_State *L;
int pushed; /* true if there is a part of the result on the stack */
int blen; /* length of partial string in 'space' */
char space[BUFVFS]; /* holds last part of the result */
} BuffFS;
/*
** Push given string to the stack, as part of the result, and
** join it to previous partial result if there is one.
** It may call 'luaV_concat' while using one slot from EXTRA_STACK.
** This call cannot invoke metamethods, as both operands must be
** strings. It can, however, raise an error if the result is too
** long. In that case, 'luaV_concat' frees the extra slot before
** raising the error.
*/
static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
lua_State *L = buff->L;
setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
L->top.p++; /* may use one slot from EXTRA_STACK */
if (!buff->pushed) /* no previous string on the stack? */
buff->pushed = 1; /* now there is one */
else /* join previous string with new one */
luaV_concat(L, 2);
}
/*
** empty the buffer space into the stack
*/
static void clearbuff (BuffFS *buff) {
pushstr(buff, buff->space, buff->blen); /* push buffer contents */
buff->blen = 0; /* space now is empty */
}
/*
** Get a space of size 'sz' in the buffer. If buffer has not enough
** space, empty it. 'sz' must fit in an empty buffer.
*/
static char *getbuff (BuffFS *buff, int sz) {
lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
if (sz > BUFVFS - buff->blen) /* not enough space? */
clearbuff(buff);
return buff->space + buff->blen;
}
#define addsize(b,sz) ((b)->blen += (sz))
/*
** Add 'str' to the buffer. If string is larger than the buffer space,
** push the string directly to the stack.
*/
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
if (slen <= BUFVFS) { /* does string fit into buffer? */
char *bf = getbuff(buff, cast_int(slen));
memcpy(bf, str, slen); /* add string to buffer */
addsize(buff, cast_int(slen));
}
else { /* string larger than buffer */
clearbuff(buff); /* string comes after buffer's content */
pushstr(buff, str, slen); /* push string */
}
}
/*
** Add a numeral to the buffer.
*/
static void addnum2buff (BuffFS *buff, TValue *num) {
char *numbuff = getbuff(buff, MAXNUMBER2STR);
int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
addsize(buff, len);
}
/*
** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
conventional formats, plus Lua-specific '%I' and '%U'
*/
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
BuffFS buff; /* holds last part of the result */
const char *e; /* points to next '%' */
buff.pushed = buff.blen = 0;
buff.L = L;
while ((e = strchr(fmt, '%')) != NULL) {
addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
switch (*(e + 1)) { /* conversion specifier */
case 's': { /* zero-terminated string */
const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)";
addstr2buff(&buff, s, strlen(s));
break;
}
case 'c': { /* an 'int' as a character */
char c = cast_uchar(va_arg(argp, int));
addstr2buff(&buff, &c, sizeof(char));
break;
}
case 'd': { /* an 'int' */
TValue num;
setivalue(&num, va_arg(argp, int));
addnum2buff(&buff, &num);
break;
}
case 'I': { /* a 'lua_Integer' */
TValue num;
setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
addnum2buff(&buff, &num);
break;
}
case 'f': { /* a 'lua_Number' */
TValue num;
setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
addnum2buff(&buff, &num);
break;
}
case 'p': { /* a pointer */
const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
char *bf = getbuff(&buff, sz);
void *p = va_arg(argp, void *);
int len = lua_pointer2str(bf, sz, p);
addsize(&buff, len);
break;
}
case 'U': { /* a 'long' as a UTF-8 sequence */
char bf[UTF8BUFFSZ];
int len = luaO_utf8esc(bf, va_arg(argp, long));
addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
break;
}
case '%': {
addstr2buff(&buff, "%", 1);
break;
}
default: {
luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
*(e + 1));
}
}
fmt = e + 2; /* skip '%' and the specifier */
}
addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
clearbuff(&buff); /* empty buffer into the stack */
lua_assert(buff.pushed == 1);
return getstr(tsvalue(s2v(L->top.p - 1)));
}
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = luaO_pushvfstring(L, fmt, argp);
va_end(argp);
return msg;
}
/* }================================================================== */
#define RETS "..."
#define PRE "[string \""
#define POS "\"]"
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
void luaO_chunkid (char *out, const char *source, size_t srclen) {
size_t bufflen = LUA_IDSIZE; /* free space in buffer */
if (*source == '=') { /* 'literal' source */
if (srclen <= bufflen) /* small enough? */
memcpy(out, source + 1, srclen * sizeof(char));
else { /* truncate it */
addstr(out, source + 1, bufflen - 1);
*out = '\0';
}
}
else if (*source == '@') { /* file name */
if (srclen <= bufflen) /* small enough? */
memcpy(out, source + 1, srclen * sizeof(char));
else { /* add '...' before rest of name */
addstr(out, RETS, LL(RETS));
bufflen -= LL(RETS);
memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
}
}
else { /* string; format as [string "source"] */
const char *nl = strchr(source, '\n'); /* find first new line (if any) */
addstr(out, PRE, LL(PRE)); /* add prefix */
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
if (srclen < bufflen && nl == NULL) { /* small one-line source? */
addstr(out, source, srclen); /* keep it */
}
else {
if (nl != NULL) srclen = nl - source; /* stop at first newline */
if (srclen > bufflen) srclen = bufflen;
addstr(out, source, srclen);
addstr(out, RETS, LL(RETS));
}
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
}
}

813
arm9/source/lua/lobject.h Normal file
View File

@ -0,0 +1,813 @@
/*
** $Id: lobject.h $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
#ifndef lobject_h
#define lobject_h
#include <stdarg.h>
#include "llimits.h"
#include "lua.h"
/*
** Extra types for collectable non-values
*/
#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */
#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */
#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */
/*
** number of all possible types (including LUA_TNONE but excluding DEADKEY)
*/
#define LUA_TOTALTYPES (LUA_TPROTO + 2)
/*
** tags for Tagged Values have the following use of bits:
** bits 0-3: actual tag (a LUA_T* constant)
** bits 4-5: variant bits
** bit 6: whether value is collectable
*/
/* add variant bits to a type */
#define makevariant(t,v) ((t) | ((v) << 4))
/*
** Union of all Lua values
*/
typedef union Value {
struct GCObject *gc; /* collectable objects */
void *p; /* light userdata */
lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
/* not used, but may avoid warnings for uninitialized value */
lu_byte ub;
} Value;
/*
** Tagged Values. This is the basic representation of values in Lua:
** an actual value plus a tag with its type.
*/
#define TValuefields Value value_; lu_byte tt_
typedef struct TValue {
TValuefields;
} TValue;
#define val_(o) ((o)->value_)
#define valraw(o) (val_(o))
/* raw type tag of a TValue */
#define rawtt(o) ((o)->tt_)
/* tag with no variants (bits 0-3) */
#define novariant(t) ((t) & 0x0F)
/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
#define withvariant(t) ((t) & 0x3F)
#define ttypetag(o) withvariant(rawtt(o))
/* type of a TValue */
#define ttype(o) (novariant(rawtt(o)))
/* Macros to test type */
#define checktag(o,t) (rawtt(o) == (t))
#define checktype(o,t) (ttype(o) == (t))
/* Macros for internal tests */
/* collectable object has the same tag as the original value */
#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
/*
** Any value being manipulated by the program either is non
** collectable, or the collectable object has the right tag
** and it is not dead. The option 'L == NULL' allows other
** macros using this one to be used where L is not available.
*/
#define checkliveness(L,obj) \
((void)L, lua_longassert(!iscollectable(obj) || \
(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
/* Macros to set values */
/* set a value's tag */
#define settt_(o,t) ((o)->tt_=(t))
/* main macro to copy values (from 'obj2' to 'obj1') */
#define setobj(L,obj1,obj2) \
{ TValue *io1=(obj1); const TValue *io2=(obj2); \
io1->value_ = io2->value_; settt_(io1, io2->tt_); \
checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
/*
** Different types of assignments, according to source and destination.
** (They are mostly equal now, but may be different in the future.)
*/
/* from stack to stack */
#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2))
/* to stack (not from same stack) */
#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2)
/* from table to same table */
#define setobjt2t setobj
/* to new object */
#define setobj2n setobj
/* to table */
#define setobj2t setobj
/*
** Entries in a Lua stack. Field 'tbclist' forms a list of all
** to-be-closed variables active in this stack. Dummy entries are
** used when the distance between two tbc variables does not fit
** in an unsigned short. They are represented by delta==0, and
** their real delta is always the maximum value that fits in
** that field.
*/
typedef union StackValue {
TValue val;
struct {
TValuefields;
unsigned short delta;
} tbclist;
} StackValue;
/* index to stack elements */
typedef StackValue *StkId;
/*
** When reallocating the stack, change all pointers to the stack into
** proper offsets.
*/
typedef union {
StkId p; /* actual pointer */
ptrdiff_t offset; /* used while the stack is being reallocated */
} StkIdRel;
/* convert a 'StackValue' to a 'TValue' */
#define s2v(o) (&(o)->val)
/*
** {==================================================================
** Nil
** ===================================================================
*/
/* Standard nil */
#define LUA_VNIL makevariant(LUA_TNIL, 0)
/* Empty slot (which might be different from a slot containing nil) */
#define LUA_VEMPTY makevariant(LUA_TNIL, 1)
/* Value returned for a key not found in a table (absent key) */
#define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
/* macro to test for (any kind of) nil */
#define ttisnil(v) checktype((v), LUA_TNIL)
/* macro to test for a standard nil */
#define ttisstrictnil(o) checktag((o), LUA_VNIL)
#define setnilvalue(obj) settt_(obj, LUA_VNIL)
#define isabstkey(v) checktag((v), LUA_VABSTKEY)
/*
** macro to detect non-standard nils (used only in assertions)
*/
#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))
/*
** By default, entries with any kind of nil are considered empty.
** (In any definition, values associated with absent keys must also
** be accepted as empty.)
*/
#define isempty(v) ttisnil(v)
/* macro defining a value corresponding to an absent key */
#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
/* mark an entry as empty */
#define setempty(v) settt_(v, LUA_VEMPTY)
/* }================================================================== */
/*
** {==================================================================
** Booleans
** ===================================================================
*/
#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
#define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
#define ttisfalse(o) checktag((o), LUA_VFALSE)
#define ttistrue(o) checktag((o), LUA_VTRUE)
#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
#define setbfvalue(obj) settt_(obj, LUA_VFALSE)
#define setbtvalue(obj) settt_(obj, LUA_VTRUE)
/* }================================================================== */
/*
** {==================================================================
** Threads
** ===================================================================
*/
#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)
#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
#define setthvalue(L,obj,x) \
{ TValue *io = (obj); lua_State *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
checkliveness(L,io); }
#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
/* }================================================================== */
/*
** {==================================================================
** Collectable Objects
** ===================================================================
*/
/*
** Common Header for all collectable objects (in macro form, to be
** included in other objects)
*/
#define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked
/* Common type for all collectable objects */
typedef struct GCObject {
CommonHeader;
} GCObject;
/* Bit mark for collectable types */
#define BIT_ISCOLLECTABLE (1 << 6)
#define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE)
/* mark a tag as collectable */
#define ctb(t) ((t) | BIT_ISCOLLECTABLE)
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
#define gcvalueraw(v) ((v).gc)
#define setgcovalue(L,obj,x) \
{ TValue *io = (obj); GCObject *i_g=(x); \
val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
/* }================================================================== */
/*
** {==================================================================
** Numbers
** ===================================================================
*/
/* Variant tags for numbers */
#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */
#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */
#define ttisnumber(o) checktype((o), LUA_TNUMBER)
#define ttisfloat(o) checktag((o), LUA_VNUMFLT)
#define ttisinteger(o) checktag((o), LUA_VNUMINT)
#define nvalue(o) check_exp(ttisnumber(o), \
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
#define fltvalueraw(v) ((v).n)
#define ivalueraw(v) ((v).i)
#define setfltvalue(obj,x) \
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
#define chgfltvalue(obj,x) \
{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
#define setivalue(obj,x) \
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
#define chgivalue(obj,x) \
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
/* }================================================================== */
/*
** {==================================================================
** Strings
** ===================================================================
*/
/* Variant tags for strings */
#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */
#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */
#define ttisstring(o) checktype((o), LUA_TSTRING)
#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))
#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))
#define tsvalueraw(v) (gco2ts((v).gc))
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
#define setsvalue(L,obj,x) \
{ TValue *io = (obj); TString *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
checkliveness(L,io); }
/* set a string to the stack */
#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s)
/* set a string to a new object */
#define setsvalue2n setsvalue
/*
** Header for a string value.
*/
typedef struct TString {
CommonHeader;
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
lu_byte shrlen; /* length for short strings, 0xFF for long strings */
unsigned int hash;
union {
size_t lnglen; /* length for long strings */
struct TString *hnext; /* linked list for hash table */
} u;
char contents[1];
} TString;
/*
** Get the actual string (array of bytes) from a 'TString'. (Generic
** version and specialized versions for long and short strings.)
*/
#define getstr(ts) ((ts)->contents)
#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents)
#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents)
/* get string length from 'TString *s' */
#define tsslen(s) \
((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)
/* }================================================================== */
/*
** {==================================================================
** Userdata
** ===================================================================
*/
/*
** Light userdata should be a variant of userdata, but for compatibility
** reasons they are also different types.
*/
#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)
#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)
#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
#define pvalueraw(v) ((v).p)
#define setpvalue(obj,x) \
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
#define setuvalue(L,obj,x) \
{ TValue *io = (obj); Udata *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
checkliveness(L,io); }
/* Ensures that addresses after this type are always fully aligned. */
typedef union UValue {
TValue uv;
LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */
} UValue;
/*
** Header for userdata with user values;
** memory area follows the end of this structure.
*/
typedef struct Udata {
CommonHeader;
unsigned short nuvalue; /* number of user values */
size_t len; /* number of bytes */
struct Table *metatable;
GCObject *gclist;
UValue uv[1]; /* user values */
} Udata;
/*
** Header for userdata with no user values. These userdata do not need
** to be gray during GC, and therefore do not need a 'gclist' field.
** To simplify, the code always use 'Udata' for both kinds of userdata,
** making sure it never accesses 'gclist' on userdata with no user values.
** This structure here is used only to compute the correct size for
** this representation. (The 'bindata' field in its end ensures correct
** alignment for binary data following this header.)
*/
typedef struct Udata0 {
CommonHeader;
unsigned short nuvalue; /* number of user values */
size_t len; /* number of bytes */
struct Table *metatable;
union {LUAI_MAXALIGN;} bindata;
} Udata0;
/* compute the offset of the memory area of a userdata */
#define udatamemoffset(nuv) \
((nuv) == 0 ? offsetof(Udata0, bindata) \
: offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
/* get the address of the memory block inside 'Udata' */
#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
/* compute the size of a userdata */
#define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb))
/* }================================================================== */
/*
** {==================================================================
** Prototypes
** ===================================================================
*/
#define LUA_VPROTO makevariant(LUA_TPROTO, 0)
/*
** Description of an upvalue for function prototypes
*/
typedef struct Upvaldesc {
TString *name; /* upvalue name (for debug information) */
lu_byte instack; /* whether it is in stack (register) */
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
lu_byte kind; /* kind of corresponding variable */
} Upvaldesc;
/*
** Description of a local variable for function prototypes
** (used for debug information)
*/
typedef struct LocVar {
TString *varname;
int startpc; /* first point where variable is active */
int endpc; /* first point where variable is dead */
} LocVar;
/*
** Associates the absolute line source for a given instruction ('pc').
** The array 'lineinfo' gives, for each instruction, the difference in
** lines from the previous instruction. When that difference does not
** fit into a byte, Lua saves the absolute line for that instruction.
** (Lua also saves the absolute line periodically, to speed up the
** computation of a line number: we can use binary search in the
** absolute-line array, but we must traverse the 'lineinfo' array
** linearly to compute a line.)
*/
typedef struct AbsLineInfo {
int pc;
int line;
} AbsLineInfo;
/*
** Function Prototypes
*/
typedef struct Proto {
CommonHeader;
lu_byte numparams; /* number of fixed (named) parameters */
lu_byte is_vararg;
lu_byte maxstacksize; /* number of registers needed by this function */
int sizeupvalues; /* size of 'upvalues' */
int sizek; /* size of 'k' */
int sizecode;
int sizelineinfo;
int sizep; /* size of 'p' */
int sizelocvars;
int sizeabslineinfo; /* size of 'abslineinfo' */
int linedefined; /* debug information */
int lastlinedefined; /* debug information */
TValue *k; /* constants used by the function */
Instruction *code; /* opcodes */
struct Proto **p; /* functions defined inside the function */
Upvaldesc *upvalues; /* upvalue information */
ls_byte *lineinfo; /* information about source lines (debug information) */
AbsLineInfo *abslineinfo; /* idem */
LocVar *locvars; /* information about local variables (debug information) */
TString *source; /* used for debug information */
GCObject *gclist;
} Proto;
/* }================================================================== */
/*
** {==================================================================
** Functions
** ===================================================================
*/
#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)
/* Variant tags for functions */
#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */
#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */
#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */
#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
#define ttislcf(o) checktag((o), LUA_VLCF)
#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o))
#define isLfunction(o) ttisLclosure(o)
#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
#define fvalue(o) check_exp(ttislcf(o), val_(o).f)
#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
#define fvalueraw(v) ((v).f)
#define setclLvalue(L,obj,x) \
{ TValue *io = (obj); LClosure *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
checkliveness(L,io); }
#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
#define setfvalue(obj,x) \
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
#define setclCvalue(L,obj,x) \
{ TValue *io = (obj); CClosure *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
checkliveness(L,io); }
/*
** Upvalues for Lua closures
*/
typedef struct UpVal {
CommonHeader;
union {
TValue *p; /* points to stack or to its own value */
ptrdiff_t offset; /* used while the stack is being reallocated */
} v;
union {
struct { /* (when open) */
struct UpVal *next; /* linked list */
struct UpVal **previous;
} open;
TValue value; /* the value (when closed) */
} u;
} UpVal;
#define ClosureHeader \
CommonHeader; lu_byte nupvalues; GCObject *gclist
typedef struct CClosure {
ClosureHeader;
lua_CFunction f;
TValue upvalue[1]; /* list of upvalues */
} CClosure;
typedef struct LClosure {
ClosureHeader;
struct Proto *p;
UpVal *upvals[1]; /* list of upvalues */
} LClosure;
typedef union Closure {
CClosure c;
LClosure l;
} Closure;
#define getproto(o) (clLvalue(o)->p)
/* }================================================================== */
/*
** {==================================================================
** Tables
** ===================================================================
*/
#define LUA_VTABLE makevariant(LUA_TTABLE, 0)
#define ttistable(o) checktag((o), ctb(LUA_VTABLE))
#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
#define sethvalue(L,obj,x) \
{ TValue *io = (obj); Table *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
checkliveness(L,io); }
#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
/*
** Nodes for Hash tables: A pack of two TValue's (key-value pairs)
** plus a 'next' field to link colliding entries. The distribution
** of the key's fields ('key_tt' and 'key_val') not forming a proper
** 'TValue' allows for a smaller size for 'Node' both in 4-byte
** and 8-byte alignments.
*/
typedef union Node {
struct NodeKey {
TValuefields; /* fields for value */
lu_byte key_tt; /* key type */
int next; /* for chaining */
Value key_val; /* key value */
} u;
TValue i_val; /* direct access to node's value as a proper 'TValue' */
} Node;
/* copy a value into a key */
#define setnodekey(L,node,obj) \
{ Node *n_=(node); const TValue *io_=(obj); \
n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
checkliveness(L,io_); }
/* copy a value from a key */
#define getnodekey(L,obj,node) \
{ TValue *io_=(obj); const Node *n_=(node); \
io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
checkliveness(L,io_); }
/*
** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
** real size of 'array'. Otherwise, the real size of 'array' is the
** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
** is zero); 'alimit' is then used as a hint for #t.
*/
#define BITRAS (1 << 7)
#define isrealasize(t) (!((t)->flags & BITRAS))
#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))
#define setnorealasize(t) ((t)->flags |= BITRAS)
typedef struct Table {
CommonHeader;
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
lu_byte lsizenode; /* log2 of size of 'node' array */
unsigned int alimit; /* "limit" of 'array' array */
TValue *array; /* array part */
Node *node;
Node *lastfree; /* any free position is before this position */
struct Table *metatable;
GCObject *gclist;
} Table;
/*
** Macros to manipulate keys inserted in nodes
*/
#define keytt(node) ((node)->u.key_tt)
#define keyval(node) ((node)->u.key_val)
#define keyisnil(node) (keytt(node) == LUA_TNIL)
#define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
#define keyival(node) (keyval(node).i)
#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
#define keystrval(node) (gco2ts(keyval(node).gc))
#define setnilkey(node) (keytt(node) = LUA_TNIL)
#define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
#define gckey(n) (keyval(n).gc)
#define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)
/*
** Dead keys in tables have the tag DEADKEY but keep their original
** gcvalue. This distinguishes them from regular keys but allows them to
** be found when searched in a special way. ('next' needs that to find
** keys removed from a table during a traversal.)
*/
#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY)
#define keyisdead(node) (keytt(node) == LUA_TDEADKEY)
/* }================================================================== */
/*
** 'module' operation for hashing (size is always a power of 2)
*/
#define lmod(s,size) \
(check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
#define twoto(x) (1<<(x))
#define sizenode(t) (twoto((t)->lsizenode))
/* size of buffer for 'luaO_utf8esc' function */
#define UTF8BUFFSZ 8
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
const TValue *p2, TValue *res);
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
const TValue *p2, StkId res);
LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
LUAI_FUNC int luaO_hexavalue (int c);
LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
va_list argp);
LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
#endif

104
arm9/source/lua/lopcodes.c Normal file
View File

@ -0,0 +1,104 @@
/*
** $Id: lopcodes.c $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
#define lopcodes_c
#define LUA_CORE
#include "lprefix.h"
#include "lopcodes.h"
/* ORDER OP */
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
/* MM OT IT T A mode opcode */
opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/
,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */
,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */
,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */
,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */
,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */
,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */
,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
};

405
arm9/source/lua/lopcodes.h Normal file
View File

@ -0,0 +1,405 @@
/*
** $Id: lopcodes.h $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
#ifndef lopcodes_h
#define lopcodes_h
#include "llimits.h"
/*===========================================================================
We assume that instructions are unsigned 32-bit integers.
All instructions have an opcode in the first 7 bits.
Instructions can have the following formats:
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
iABC C(8) | B(8) |k| A(8) | Op(7) |
iABx Bx(17) | A(8) | Op(7) |
iAsBx sBx (signed)(17) | A(8) | Op(7) |
iAx Ax(25) | Op(7) |
isJ sJ (signed)(25) | Op(7) |
A signed argument is represented in excess K: the represented value is
the written unsigned value minus K, where K is half the maximum for the
corresponding unsigned argument.
===========================================================================*/
enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
/*
** size and position of opcode arguments.
*/
#define SIZE_C 8
#define SIZE_B 8
#define SIZE_Bx (SIZE_C + SIZE_B + 1)
#define SIZE_A 8
#define SIZE_Ax (SIZE_Bx + SIZE_A)
#define SIZE_sJ (SIZE_Bx + SIZE_A)
#define SIZE_OP 7
#define POS_OP 0
#define POS_A (POS_OP + SIZE_OP)
#define POS_k (POS_A + SIZE_A)
#define POS_B (POS_k + 1)
#define POS_C (POS_B + SIZE_B)
#define POS_Bx POS_k
#define POS_Ax POS_A
#define POS_sJ POS_A
/*
** limits for opcode arguments.
** we use (signed) 'int' to manipulate most arguments,
** so they must fit in ints.
*/
/* Check whether type 'int' has at least 'b' bits ('b' < 32) */
#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1)
#if L_INTHASBITS(SIZE_Bx)
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
#else
#define MAXARG_Bx MAX_INT
#endif
#define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
#if L_INTHASBITS(SIZE_Ax)
#define MAXARG_Ax ((1<<SIZE_Ax)-1)
#else
#define MAXARG_Ax MAX_INT
#endif
#if L_INTHASBITS(SIZE_sJ)
#define MAXARG_sJ ((1 << SIZE_sJ) - 1)
#else
#define MAXARG_sJ MAX_INT
#endif
#define OFFSET_sJ (MAXARG_sJ >> 1)
#define MAXARG_A ((1<<SIZE_A)-1)
#define MAXARG_B ((1<<SIZE_B)-1)
#define MAXARG_C ((1<<SIZE_C)-1)
#define OFFSET_sC (MAXARG_C >> 1)
#define int2sC(i) ((i) + OFFSET_sC)
#define sC2int(i) ((i) - OFFSET_sC)
/* creates a mask with 'n' 1 bits at position 'p' */
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
/* creates a mask with 'n' 0 bits at position 'p' */
#define MASK0(n,p) (~MASK1(n,p))
/*
** the following macros help to manipulate instructions
*/
#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
((cast(Instruction, v)<<pos)&MASK1(size,pos))))
#define GETARG_A(i) getarg(i, POS_A, SIZE_A)
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
#define GETARG_sB(i) sC2int(GETARG_B(i))
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
#define GETARG_sC(i) sC2int(GETARG_C(i))
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
#define GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
#define GETARG_sBx(i) \
check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
#define SETARG_sBx(i,b) SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))
#define GETARG_sJ(i) \
check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
#define SETARG_sJ(i,j) \
setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, b)<<POS_B) \
| (cast(Instruction, c)<<POS_C) \
| (cast(Instruction, k)<<POS_k))
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, bc)<<POS_Bx))
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_Ax))
#define CREATE_sJ(o,j,k) ((cast(Instruction, o) << POS_OP) \
| (cast(Instruction, j) << POS_sJ) \
| (cast(Instruction, k) << POS_k))
#if !defined(MAXINDEXRK) /* (for debugging only) */
#define MAXINDEXRK MAXARG_B
#endif
/*
** invalid register that fits in 8 bits
*/
#define NO_REG MAXARG_A
/*
** R[x] - register
** K[x] - constant (in constant table)
** RK(x) == if k(i) then K[x] else R[x]
*/
/*
** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
** has extra descriptions in the notes after the enumeration.
*/
typedef enum {
/*----------------------------------------------------------------------
name args description
------------------------------------------------------------------------*/
OP_MOVE,/* A B R[A] := R[B] */
OP_LOADI,/* A sBx R[A] := sBx */
OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
OP_LOADK,/* A Bx R[A] := K[Bx] */
OP_LOADKX,/* A R[A] := K[extra arg] */
OP_LOADFALSE,/* A R[A] := false */
OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */
OP_LOADTRUE,/* A R[A] := true */
OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
OP_GETUPVAL,/* A B R[A] := UpValue[B] */
OP_SETUPVAL,/* A B UpValue[B] := R[A] */
OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */
OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */
OP_GETI,/* A B C R[A] := R[B][C] */
OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */
OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */
OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
OP_SETI,/* A B C R[A][B] := RK(C) */
OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */
OP_NEWTABLE,/* A B C k R[A] := {} */
OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
OP_ADDI,/* A B sC R[A] := R[B] + sC */
OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
OP_SHRI,/* A B sC R[A] := R[B] >> sC */
OP_SHLI,/* A B sC R[A] := sC << R[B] */
OP_ADD,/* A B C R[A] := R[B] + R[C] */
OP_SUB,/* A B C R[A] := R[B] - R[C] */
OP_MUL,/* A B C R[A] := R[B] * R[C] */
OP_MOD,/* A B C R[A] := R[B] % R[C] */
OP_POW,/* A B C R[A] := R[B] ^ R[C] */
OP_DIV,/* A B C R[A] := R[B] / R[C] */
OP_IDIV,/* A B C R[A] := R[B] // R[C] */
OP_BAND,/* A B C R[A] := R[B] & R[C] */
OP_BOR,/* A B C R[A] := R[B] | R[C] */
OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
OP_SHL,/* A B C R[A] := R[B] << R[C] */
OP_SHR,/* A B C R[A] := R[B] >> R[C] */
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
OP_UNM,/* A B R[A] := -R[B] */
OP_BNOT,/* A B R[A] := ~R[B] */
OP_NOT,/* A B R[A] := not R[B] */
OP_LEN,/* A B R[A] := #R[B] (length operator) */
OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
OP_CLOSE,/* A close all upvalues >= R[A] */
OP_TBC,/* A mark variable A "to be closed" */
OP_JMP,/* sJ pc += sJ */
OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */
OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */
OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */
OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */
OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */
OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */
OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
OP_TEST,/* A k if (not R[A] == k) then pc++ */
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
OP_RETURN0,/* return */
OP_RETURN1,/* A return R[A] */
OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */
OP_FORPREP,/* A Bx <check values and prepare counters>;
if not to run then pc+=Bx+1; */
OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */
OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
OP_VARARGPREP,/*A (adjust vararg parameters) */
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
} OpCode;
#define NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
/*===========================================================================
Notes:
(*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
value, in a code equivalent to (not cond ? false : true). (It
produces false and skips the next instruction producing true.)
(*) Opcodes OP_MMBIN and variants follow each arithmetic and
bitwise opcode. If the operation succeeds, it skips this next
opcode. Otherwise, this opcode calls the corresponding metamethod.
(*) Opcode OP_TESTSET is used in short-circuit expressions that need
both to jump and to produce a value, such as (a = b or c).
(*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
'top' is set to last_result+1, so next open instruction (OP_CALL,
OP_RETURN*, OP_SETLIST) may use 'top'.
(*) In OP_VARARG, if (C == 0) then use actual number of varargs and
set top (like in OP_CALL with C == 0).
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
(*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
OP_EXTRAARG.
(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
bits of C).
(*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
power of 2) plus 1, or zero for size zero. If not k, the array size
is C. Otherwise, the array size is EXTRAARG _ C.
(*) For comparisons, k specifies what condition the test should accept
(true or false).
(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
(the constant is the first operand).
(*) All 'skips' (pc++) assume that next instruction is a jump.
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
function builds upvalues, which may need to be closed. C > 0 means
the function is vararg, so that its 'func' must be corrected before
returning; in this case, (C - 1) is its number of fixed parameters.
(*) In comparisons with an immediate operand, C signals whether the
original operand was a float. (It must be corrected in case of
metamethods.)
===========================================================================*/
/*
** masks for instruction properties. The format is:
** bits 0-2: op mode
** bit 3: instruction set register A
** bit 4: operator is a test (next instruction must be a jump)
** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
** bit 7: instruction is an MM instruction (call a metamethod)
*/
LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
#define testAMode(m) (luaP_opmodes[m] & (1 << 3))
#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
/* "out top" (set top for next instruction) */
#define isOT(i) \
((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \
GET_OPCODE(i) == OP_TAILCALL)
/* "in top" (uses top from previous instruction) */
#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
#define opmode(mm,ot,it,t,a,m) \
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
/* number of list items to accumulate before a SETLIST instruction */
#define LFIELDS_PER_FLUSH 50
#endif

103
arm9/source/lua/lopnames.h Normal file
View File

@ -0,0 +1,103 @@
/*
** $Id: lopnames.h $
** Opcode names
** See Copyright Notice in lua.h
*/
#if !defined(lopnames_h)
#define lopnames_h
#include <stddef.h>
/* ORDER OP */
static const char *const opnames[] = {
"MOVE",
"LOADI",
"LOADF",
"LOADK",
"LOADKX",
"LOADFALSE",
"LFALSESKIP",
"LOADTRUE",
"LOADNIL",
"GETUPVAL",
"SETUPVAL",
"GETTABUP",
"GETTABLE",
"GETI",
"GETFIELD",
"SETTABUP",
"SETTABLE",
"SETI",
"SETFIELD",
"NEWTABLE",
"SELF",
"ADDI",
"ADDK",
"SUBK",
"MULK",
"MODK",
"POWK",
"DIVK",
"IDIVK",
"BANDK",
"BORK",
"BXORK",
"SHRI",
"SHLI",
"ADD",
"SUB",
"MUL",
"MOD",
"POW",
"DIV",
"IDIV",
"BAND",
"BOR",
"BXOR",
"SHL",
"SHR",
"MMBIN",
"MMBINI",
"MMBINK",
"UNM",
"BNOT",
"NOT",
"LEN",
"CONCAT",
"CLOSE",
"TBC",
"JMP",
"EQ",
"LT",
"LE",
"EQK",
"EQI",
"LTI",
"LEI",
"GTI",
"GEI",
"TEST",
"TESTSET",
"CALL",
"TAILCALL",
"RETURN",
"RETURN0",
"RETURN1",
"FORLOOP",
"FORPREP",
"TFORPREP",
"TFORCALL",
"TFORLOOP",
"SETLIST",
"CLOSURE",
"VARARG",
"VARARGPREP",
"EXTRAARG",
NULL
};
#endif

1967
arm9/source/lua/lparser.c Normal file

File diff suppressed because it is too large Load Diff

171
arm9/source/lua/lparser.h Normal file
View File

@ -0,0 +1,171 @@
/*
** $Id: lparser.h $
** Lua Parser
** See Copyright Notice in lua.h
*/
#ifndef lparser_h
#define lparser_h
#include "llimits.h"
#include "lobject.h"
#include "lzio.h"
/*
** Expression and variable descriptor.
** Code generation for variables and expressions can be delayed to allow
** optimizations; An 'expdesc' structure describes a potentially-delayed
** variable/expression. It has a description of its "main" value plus a
** list of conditional jumps that can also produce its value (generated
** by short-circuit operators 'and'/'or').
*/
/* kinds of variables/expressions */
typedef enum {
VVOID, /* when 'expdesc' describes the last expression of a list,
this kind means an empty list (so, no expression) */
VNIL, /* constant nil */
VTRUE, /* constant true */
VFALSE, /* constant false */
VK, /* constant in 'k'; info = index of constant in 'k' */
VKFLT, /* floating constant; nval = numerical float value */
VKINT, /* integer constant; ival = numerical integer value */
VKSTR, /* string constant; strval = TString address;
(string is fixed by the lexer) */
VNONRELOC, /* expression has its value in a fixed register;
info = result register */
VLOCAL, /* local variable; var.ridx = register index;
var.vidx = relative index in 'actvar.arr' */
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
VCONST, /* compile-time <const> variable;
info = absolute index in 'actvar.arr' */
VINDEXED, /* indexed variable;
ind.t = table register;
ind.idx = key's R index */
VINDEXUP, /* indexed upvalue;
ind.t = table upvalue;
ind.idx = key's K index */
VINDEXI, /* indexed variable with constant integer;
ind.t = table register;
ind.idx = key's value */
VINDEXSTR, /* indexed variable with literal string;
ind.t = table register;
ind.idx = key's K index */
VJMP, /* expression is a test/comparison;
info = pc of corresponding jump instruction */
VRELOC, /* expression can put result in any register;
info = instruction pc */
VCALL, /* expression is a function call; info = instruction pc */
VVARARG /* vararg expression; info = instruction pc */
} expkind;
#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)
#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)
typedef struct expdesc {
expkind k;
union {
lua_Integer ival; /* for VKINT */
lua_Number nval; /* for VKFLT */
TString *strval; /* for VKSTR */
int info; /* for generic use */
struct { /* for indexed variables */
short idx; /* index (R or "long" K) */
lu_byte t; /* table (register or upvalue) */
} ind;
struct { /* for local variables */
lu_byte ridx; /* register holding the variable */
unsigned short vidx; /* compiler index (in 'actvar.arr') */
} var;
} u;
int t; /* patch list of 'exit when true' */
int f; /* patch list of 'exit when false' */
} expdesc;
/* kinds of variables */
#define VDKREG 0 /* regular */
#define RDKCONST 1 /* constant */
#define RDKTOCLOSE 2 /* to-be-closed */
#define RDKCTC 3 /* compile-time constant */
/* description of an active local variable */
typedef union Vardesc {
struct {
TValuefields; /* constant value (if it is a compile-time constant) */
lu_byte kind;
lu_byte ridx; /* register holding the variable */
short pidx; /* index of the variable in the Proto's 'locvars' array */
TString *name; /* variable name */
} vd;
TValue k; /* constant value (if any) */
} Vardesc;
/* description of pending goto statements and label statements */
typedef struct Labeldesc {
TString *name; /* label identifier */
int pc; /* position in code */
int line; /* line where it appeared */
lu_byte nactvar; /* number of active variables in that position */
lu_byte close; /* goto that escapes upvalues */
} Labeldesc;
/* list of labels or gotos */
typedef struct Labellist {
Labeldesc *arr; /* array */
int n; /* number of entries in use */
int size; /* array size */
} Labellist;
/* dynamic structures used by the parser */
typedef struct Dyndata {
struct { /* list of all active local variables */
Vardesc *arr;
int n;
int size;
} actvar;
Labellist gt; /* list of pending gotos */
Labellist label; /* list of active labels */
} Dyndata;
/* control of blocks */
struct BlockCnt; /* defined in lparser.c */
/* state needed to generate code for a given function */
typedef struct FuncState {
Proto *f; /* current function header */
struct FuncState *prev; /* enclosing function */
struct LexState *ls; /* lexical state */
struct BlockCnt *bl; /* chain of current blocks */
int pc; /* next position to code (equivalent to 'ncode') */
int lasttarget; /* 'label' of last 'jump label' */
int previousline; /* last line that was saved in 'lineinfo' */
int nk; /* number of elements in 'k' */
int np; /* number of elements in 'p' */
int nabslineinfo; /* number of elements in 'abslineinfo' */
int firstlocal; /* index of first local var (in Dyndata array) */
int firstlabel; /* index of first label (in 'dyd->label->arr') */
short ndebugvars; /* number of elements in 'f->locvars' */
lu_byte nactvar; /* number of active local variables */
lu_byte nups; /* number of upvalues */
lu_byte freereg; /* first free register */
lu_byte iwthabs; /* instructions issued since last absolute line info */
lu_byte needclose; /* function needs to close upvalues when returning */
} FuncState;
LUAI_FUNC int luaY_nvarstack (FuncState *fs);
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar);
#endif

45
arm9/source/lua/lprefix.h Normal file
View File

@ -0,0 +1,45 @@
/*
** $Id: lprefix.h $
** Definitions for Lua code that must come before any other header file
** See Copyright Notice in lua.h
*/
#ifndef lprefix_h
#define lprefix_h
/*
** Allows POSIX/XSI stuff
*/
#if !defined(LUA_USE_C89) /* { */
#if !defined(_XOPEN_SOURCE)
#define _XOPEN_SOURCE 600
#elif _XOPEN_SOURCE == 0
#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
#endif
/*
** Allows manipulation of large files in gcc and some other compilers
*/
#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#endif
#endif /* } */
/*
** Windows stuff
*/
#if defined(_WIN32) /* { */
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
#endif
#endif /* } */
#endif

445
arm9/source/lua/lstate.c Normal file
View File

@ -0,0 +1,445 @@
/*
** $Id: lstate.c $
** Global State
** See Copyright Notice in lua.h
*/
#define lstate_c
#define LUA_CORE
#include "lprefix.h"
#include <stddef.h>
#include <string.h>
#include "lua.h"
#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "llex.h"
#include "lmem.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
/*
** thread state + extra space
*/
typedef struct LX {
lu_byte extra_[LUA_EXTRASPACE];
lua_State l;
} LX;
/*
** Main thread combines a thread state and the global state
*/
typedef struct LG {
LX l;
global_State g;
} LG;
#define fromstate(L) (castp(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
/*
** A macro to create a "random" seed when a state is created;
** the seed is used to randomize string hashes.
*/
#if !defined(luai_makeseed)
#include <time.h>
/*
** Compute an initial seed with some level of randomness.
** Rely on Address Space Layout Randomization (if present) and
** current time.
*/
#define addbuff(b,p,e) \
{ size_t t = cast_sizet(e); \
memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
static unsigned int luai_makeseed (lua_State *L) {
char buff[3 * sizeof(size_t)];
unsigned int h = cast_uint(time(NULL));
int p = 0;
addbuff(buff, p, L); /* heap variable */
addbuff(buff, p, &h); /* local variable */
addbuff(buff, p, &lua_newstate); /* public function */
lua_assert(p == sizeof(buff));
return luaS_hash(buff, p, h);
}
#endif
/*
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
** invariant (and avoiding underflows in 'totalbytes')
*/
void luaE_setdebt (global_State *g, l_mem debt) {
l_mem tb = gettotalbytes(g);
lua_assert(tb > 0);
if (debt < tb - MAX_LMEM)
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
g->totalbytes = tb - debt;
g->GCdebt = debt;
}
LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
UNUSED(L); UNUSED(limit);
return LUAI_MAXCCALLS; /* warning?? */
}
CallInfo *luaE_extendCI (lua_State *L) {
CallInfo *ci;
lua_assert(L->ci->next == NULL);
ci = luaM_new(L, CallInfo);
lua_assert(L->ci->next == NULL);
L->ci->next = ci;
ci->previous = L->ci;
ci->next = NULL;
ci->u.l.trap = 0;
L->nci++;
return ci;
}
/*
** free all CallInfo structures not in use by a thread
*/
static void freeCI (lua_State *L) {
CallInfo *ci = L->ci;
CallInfo *next = ci->next;
ci->next = NULL;
while ((ci = next) != NULL) {
next = ci->next;
luaM_free(L, ci);
L->nci--;
}
}
/*
** free half of the CallInfo structures not in use by a thread,
** keeping the first one.
*/
void luaE_shrinkCI (lua_State *L) {
CallInfo *ci = L->ci->next; /* first free CallInfo */
CallInfo *next;
if (ci == NULL)
return; /* no extra elements */
while ((next = ci->next) != NULL) { /* two extra elements? */
CallInfo *next2 = next->next; /* next's next */
ci->next = next2; /* remove next from the list */
L->nci--;
luaM_free(L, next); /* free next */
if (next2 == NULL)
break; /* no more elements */
else {
next2->previous = ci;
ci = next2; /* continue */
}
}
}
/*
** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
** If equal, raises an overflow error. If value is larger than
** LUAI_MAXCCALLS (which means it is handling an overflow) but
** not much larger, does not report an error (to allow overflow
** handling to work).
*/
void luaE_checkcstack (lua_State *L) {
if (getCcalls(L) == LUAI_MAXCCALLS)
luaG_runerror(L, "C stack overflow");
else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
}
LUAI_FUNC void luaE_incCstack (lua_State *L) {
L->nCcalls++;
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
luaE_checkcstack(L);
}
static void stack_init (lua_State *L1, lua_State *L) {
int i; CallInfo *ci;
/* initialize stack array */
L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
L1->tbclist.p = L1->stack.p;
for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
L1->top.p = L1->stack.p;
L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
/* initialize first ci */
ci = &L1->base_ci;
ci->next = ci->previous = NULL;
ci->callstatus = CIST_C;
ci->func.p = L1->top.p;
ci->u.c.k = NULL;
ci->nresults = 0;
setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */
L1->top.p++;
ci->top.p = L1->top.p + LUA_MINSTACK;
L1->ci = ci;
}
static void freestack (lua_State *L) {
if (L->stack.p == NULL)
return; /* stack not completely built yet */
L->ci = &L->base_ci; /* free the entire 'ci' list */
freeCI(L);
lua_assert(L->nci == 0);
luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */
}
/*
** Create registry table and its predefined values
*/
static void init_registry (lua_State *L, global_State *g) {
/* create registry */
Table *registry = luaH_new(L);
sethvalue(L, &g->l_registry, registry);
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
/* registry[LUA_RIDX_MAINTHREAD] = L */
setthvalue(L, &registry->array[LUA_RIDX_MAINTHREAD - 1], L);
/* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
sethvalue(L, &registry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
}
/*
** open parts of the state that may cause memory-allocation errors.
*/
static void f_luaopen (lua_State *L, void *ud) {
global_State *g = G(L);
UNUSED(ud);
stack_init(L, L); /* init stack */
init_registry(L, g);
luaS_init(L);
luaT_init(L);
luaX_init(L);
g->gcstp = 0; /* allow gc */
setnilvalue(&g->nilvalue); /* now state is complete */
luai_userstateopen(L);
}
/*
** preinitialize a thread with consistent values without allocating
** any memory (to avoid errors)
*/
static void preinit_thread (lua_State *L, global_State *g) {
G(L) = g;
L->stack.p = NULL;
L->ci = NULL;
L->nci = 0;
L->twups = L; /* thread has no upvalues */
L->nCcalls = 0;
L->errorJmp = NULL;
L->hook = NULL;
L->hookmask = 0;
L->basehookcount = 0;
L->allowhook = 1;
resethookcount(L);
L->openupval = NULL;
L->status = LUA_OK;
L->errfunc = 0;
L->oldpc = 0;
}
static void close_state (lua_State *L) {
global_State *g = G(L);
if (!completestate(g)) /* closing a partially built state? */
luaC_freeallobjects(L); /* just collect its objects */
else { /* closing a fully built state */
L->ci = &L->base_ci; /* unwind CallInfo list */
luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
luaC_freeallobjects(L); /* collect all objects */
luai_userstateclose(L);
}
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
freestack(L);
lua_assert(gettotalbytes(g) == sizeof(LG));
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
}
LUA_API lua_State *lua_newthread (lua_State *L) {
global_State *g = G(L);
GCObject *o;
lua_State *L1;
lua_lock(L);
luaC_checkGC(L);
/* create new thread */
o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
L1 = gco2th(o);
/* anchor it on L stack */
setthvalue2s(L, L->top.p, L1);
api_incr_top(L);
preinit_thread(L1, g);
L1->hookmask = L->hookmask;
L1->basehookcount = L->basehookcount;
L1->hook = L->hook;
resethookcount(L1);
/* initialize L1 extra space */
memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
LUA_EXTRASPACE);
luai_userstatethread(L, L1);
stack_init(L1, L); /* init stack */
lua_unlock(L);
return L1;
}
void luaE_freethread (lua_State *L, lua_State *L1) {
LX *l = fromstate(L1);
luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
lua_assert(L1->openupval == NULL);
luai_userstatefree(L, L1);
freestack(L1);
luaM_free(L, l);
}
int luaE_resetthread (lua_State *L, int status) {
CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */
setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */
ci->func.p = L->stack.p;
ci->callstatus = CIST_C;
if (status == LUA_YIELD)
status = LUA_OK;
L->status = LUA_OK; /* so it can run __close metamethods */
status = luaD_closeprotected(L, 1, status);
if (status != LUA_OK) /* errors? */
luaD_seterrorobj(L, status, L->stack.p + 1);
else
L->top.p = L->stack.p + 1;
ci->top.p = L->top.p + LUA_MINSTACK;
luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
return status;
}
LUA_API int lua_closethread (lua_State *L, lua_State *from) {
int status;
lua_lock(L);
L->nCcalls = (from) ? getCcalls(from) : 0;
status = luaE_resetthread(L, L->status);
lua_unlock(L);
return status;
}
/*
** Deprecated! Use 'lua_closethread' instead.
*/
LUA_API int lua_resetthread (lua_State *L) {
return lua_closethread(L, NULL);
}
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
int i;
lua_State *L;
global_State *g;
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
if (l == NULL) return NULL;
L = &l->l.l;
g = &l->g;
L->tt = LUA_VTHREAD;
g->currentwhite = bitmask(WHITE0BIT);
L->marked = luaC_white(g);
preinit_thread(L, g);
g->allgc = obj2gco(L); /* by now, only object is the main thread */
L->next = NULL;
incnny(L); /* main thread is always non yieldable */
g->frealloc = f;
g->ud = ud;
g->warnf = NULL;
g->ud_warn = NULL;
g->mainthread = L;
g->seed = luai_makeseed(L);
g->gcstp = GCSTPGC; /* no GC while building state */
g->strt.size = g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(&g->l_registry);
g->panic = NULL;
g->gcstate = GCSpause;
g->gckind = KGC_INC;
g->gcstopem = 0;
g->gcemergency = 0;
g->finobj = g->tobefnz = g->fixedgc = NULL;
g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
g->sweepgc = NULL;
g->gray = g->grayagain = NULL;
g->weak = g->ephemeron = g->allweak = NULL;
g->twups = NULL;
g->totalbytes = sizeof(LG);
g->GCdebt = 0;
g->lastatomic = 0;
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
setgcparam(g->gcpause, LUAI_GCPAUSE);
setgcparam(g->gcstepmul, LUAI_GCMUL);
g->gcstepsize = LUAI_GCSTEPSIZE;
setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
g->genminormul = LUAI_GENMINORMUL;
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
/* memory allocation error: free partial state */
close_state(L);
L = NULL;
}
return L;
}
LUA_API void lua_close (lua_State *L) {
lua_lock(L);
L = G(L)->mainthread; /* only the main thread can be closed */
close_state(L);
}
void luaE_warning (lua_State *L, const char *msg, int tocont) {
lua_WarnFunction wf = G(L)->warnf;
if (wf != NULL)
wf(G(L)->ud_warn, msg, tocont);
}
/*
** Generate a warning from an error message
*/
void luaE_warnerror (lua_State *L, const char *where) {
TValue *errobj = s2v(L->top.p - 1); /* error object */
const char *msg = (ttisstring(errobj))
? getstr(tsvalue(errobj))
: "error object is not a string";
/* produce warning "error in %s (%s)" (where, msg) */
luaE_warning(L, "error in ", 1);
luaE_warning(L, where, 1);
luaE_warning(L, " (", 1);
luaE_warning(L, msg, 1);
luaE_warning(L, ")", 0);
}

Some files were not shown because too many files have changed in this diff Show More