dub 0.2.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dub might be problematic. Click here for more details.
- data/History.txt +92 -0
- data/LICENSE +20 -0
- data/README.rdoc +115 -0
- data/Rakefile +59 -0
- data/dub.gemspec +197 -0
- data/lib/dub/argument.rb +286 -0
- data/lib/dub/entities_unescape.rb +9 -0
- data/lib/dub/function.rb +177 -0
- data/lib/dub/function_group.rb +72 -0
- data/lib/dub/generator.rb +15 -0
- data/lib/dub/group.rb +20 -0
- data/lib/dub/klass.rb +338 -0
- data/lib/dub/lua/class.cpp.erb +114 -0
- data/lib/dub/lua/class_gen.rb +96 -0
- data/lib/dub/lua/function.cpp.erb +15 -0
- data/lib/dub/lua/function_gen.rb +329 -0
- data/lib/dub/lua/group.cpp.erb +9 -0
- data/lib/dub/lua/lua_cpp_helper.cpp +259 -0
- data/lib/dub/lua/lua_cpp_helper.h +219 -0
- data/lib/dub/lua/lua_object.cpp +158 -0
- data/lib/dub/lua/lua_object.h +69 -0
- data/lib/dub/lua/namespace.cpp.erb +42 -0
- data/lib/dub/lua/namespace_gen.rb +69 -0
- data/lib/dub/lua.rb +24 -0
- data/lib/dub/member_extraction.rb +128 -0
- data/lib/dub/namespace.rb +295 -0
- data/lib/dub/opts_parser.rb +30 -0
- data/lib/dub/parser.rb +46 -0
- data/lib/dub/templates/lua_template.erb +21 -0
- data/lib/dub/version.rb +3 -0
- data/lib/dub.rb +24 -20
- data/test/argument_test.rb +581 -0
- data/test/fixtures/app/CMakeLists.txt +54 -0
- data/test/fixtures/app/Doxyfile +1600 -0
- data/test/fixtures/app/bindings/all_lua.cpp +299 -0
- data/test/fixtures/app/include/matrix.h +283 -0
- data/test/fixtures/app/make_lua_bindings.rb +13 -0
- data/test/fixtures/app/vendor/lua/CMakeLists.txt +25 -0
- data/test/fixtures/app/vendor/lua/COPYRIGHT +34 -0
- data/test/fixtures/app/vendor/lua/HISTORY +183 -0
- data/test/fixtures/app/vendor/lua/INSTALL +99 -0
- data/test/fixtures/app/vendor/lua/Makefile +183 -0
- data/test/fixtures/app/vendor/lua/README +37 -0
- data/test/fixtures/app/vendor/lua/lapi.c +1080 -0
- data/test/fixtures/app/vendor/lua/lapi.h +16 -0
- data/test/fixtures/app/vendor/lua/lauxlib.c +653 -0
- data/test/fixtures/app/vendor/lua/lauxlib.h +174 -0
- data/test/fixtures/app/vendor/lua/lbaselib.c +643 -0
- data/test/fixtures/app/vendor/lua/lcode.c +839 -0
- data/test/fixtures/app/vendor/lua/lcode.h +76 -0
- data/test/fixtures/app/vendor/lua/ldblib.c +397 -0
- data/test/fixtures/app/vendor/lua/ldebug.c +622 -0
- data/test/fixtures/app/vendor/lua/ldebug.h +33 -0
- data/test/fixtures/app/vendor/lua/ldo.c +516 -0
- data/test/fixtures/app/vendor/lua/ldo.h +57 -0
- data/test/fixtures/app/vendor/lua/ldump.c +164 -0
- data/test/fixtures/app/vendor/lua/lfunc.c +174 -0
- data/test/fixtures/app/vendor/lua/lfunc.h +34 -0
- data/test/fixtures/app/vendor/lua/lgc.c +711 -0
- data/test/fixtures/app/vendor/lua/lgc.h +110 -0
- data/test/fixtures/app/vendor/lua/liblua.a +0 -0
- data/test/fixtures/app/vendor/lua/linit.c +38 -0
- data/test/fixtures/app/vendor/lua/liolib.c +532 -0
- data/test/fixtures/app/vendor/lua/llex.c +461 -0
- data/test/fixtures/app/vendor/lua/llex.h +81 -0
- data/test/fixtures/app/vendor/lua/llimits.h +128 -0
- data/test/fixtures/app/vendor/lua/lmathlib.c +263 -0
- data/test/fixtures/app/vendor/lua/lmem.c +86 -0
- data/test/fixtures/app/vendor/lua/lmem.h +49 -0
- data/test/fixtures/app/vendor/lua/loadlib.c +664 -0
- data/test/fixtures/app/vendor/lua/lobject.c +214 -0
- data/test/fixtures/app/vendor/lua/lobject.h +381 -0
- data/test/fixtures/app/vendor/lua/lopcodes.c +102 -0
- data/test/fixtures/app/vendor/lua/lopcodes.h +268 -0
- data/test/fixtures/app/vendor/lua/loslib.c +244 -0
- data/test/fixtures/app/vendor/lua/lparser.c +1337 -0
- data/test/fixtures/app/vendor/lua/lparser.h +82 -0
- data/test/fixtures/app/vendor/lua/lstate.c +214 -0
- data/test/fixtures/app/vendor/lua/lstate.h +168 -0
- data/test/fixtures/app/vendor/lua/lstring.c +111 -0
- data/test/fixtures/app/vendor/lua/lstring.h +31 -0
- data/test/fixtures/app/vendor/lua/lstrlib.c +868 -0
- data/test/fixtures/app/vendor/lua/ltable.c +588 -0
- data/test/fixtures/app/vendor/lua/ltable.h +40 -0
- data/test/fixtures/app/vendor/lua/ltablib.c +278 -0
- data/test/fixtures/app/vendor/lua/ltm.c +75 -0
- data/test/fixtures/app/vendor/lua/ltm.h +54 -0
- data/test/fixtures/app/vendor/lua/lua.c +695 -0
- data/test/fixtures/app/vendor/lua/lua.h +385 -0
- data/test/fixtures/app/vendor/lua/lua_dub_helper.h +77 -0
- data/test/fixtures/app/vendor/lua/luac +0 -0
- data/test/fixtures/app/vendor/lua/luac.c +200 -0
- data/test/fixtures/app/vendor/lua/luaconf.h +762 -0
- data/test/fixtures/app/vendor/lua/lualib.h +53 -0
- data/test/fixtures/app/vendor/lua/lundump.c +223 -0
- data/test/fixtures/app/vendor/lua/lundump.h +36 -0
- data/test/fixtures/app/vendor/lua/lvm.c +765 -0
- data/test/fixtures/app/vendor/lua/lvm.h +36 -0
- data/test/fixtures/app/vendor/lua/lzio.c +82 -0
- data/test/fixtures/app/vendor/lua/lzio.h +67 -0
- data/test/fixtures/app/vendor/lua/matrix.h +102 -0
- data/test/fixtures/app/vendor/lua/print.c +227 -0
- data/test/fixtures/app/vendor/lua/test/README +26 -0
- data/test/fixtures/app/vendor/lua/test/bisect.lua +27 -0
- data/test/fixtures/app/vendor/lua/test/cf.lua +16 -0
- data/test/fixtures/app/vendor/lua/test/echo.lua +5 -0
- data/test/fixtures/app/vendor/lua/test/env.lua +7 -0
- data/test/fixtures/app/vendor/lua/test/factorial.lua +32 -0
- data/test/fixtures/app/vendor/lua/test/fib.lua +40 -0
- data/test/fixtures/app/vendor/lua/test/fibfor.lua +13 -0
- data/test/fixtures/app/vendor/lua/test/globals.lua +13 -0
- data/test/fixtures/app/vendor/lua/test/hello.lua +3 -0
- data/test/fixtures/app/vendor/lua/test/life.lua +111 -0
- data/test/fixtures/app/vendor/lua/test/luac.lua +7 -0
- data/test/fixtures/app/vendor/lua/test/printf.lua +7 -0
- data/test/fixtures/app/vendor/lua/test/readonly.lua +12 -0
- data/test/fixtures/app/vendor/lua/test/sieve.lua +29 -0
- data/test/fixtures/app/vendor/lua/test/sort.lua +66 -0
- data/test/fixtures/app/vendor/lua/test/table.lua +12 -0
- data/test/fixtures/app/vendor/lua/test/trace-calls.lua +32 -0
- data/test/fixtures/app/vendor/lua/test/trace-globals.lua +38 -0
- data/test/fixtures/app/vendor/lua/test/xd.lua +14 -0
- data/test/fixtures/app/xml/classdub_1_1_base.xml +85 -0
- data/test/fixtures/app/xml/classdub_1_1_custom_destructor.xml +67 -0
- data/test/fixtures/app/xml/classdub_1_1_deletable_out_of_lua.xml +43 -0
- data/test/fixtures/app/xml/classdub_1_1_matrix.xml +482 -0
- data/test/fixtures/app/xml/classdub_1_1_no_destructor.xml +49 -0
- data/test/fixtures/app/xml/classdub_1_1_priv_sub_base.xml +89 -0
- data/test/fixtures/app/xml/classdub_1_1_private_constr.xml +68 -0
- data/test/fixtures/app/xml/classdub_1_1_static_constr.xml +69 -0
- data/test/fixtures/app/xml/classdub_1_1_sub_base.xml +89 -0
- data/test/fixtures/app/xml/classdub_1_1_t_mat.xml +252 -0
- data/test/fixtures/app/xml/combine.xslt +15 -0
- data/test/fixtures/app/xml/compound.xsd +814 -0
- data/test/fixtures/app/xml/dir_53661a2bdeb1d55e60581a7e15deb763.xml +12 -0
- data/test/fixtures/app/xml/index.xml +91 -0
- data/test/fixtures/app/xml/index.xsd +66 -0
- data/test/fixtures/app/xml/matrix_8h.xml +310 -0
- data/test/fixtures/app/xml/namespacedub.xml +48 -0
- data/test/fixtures/classcv_1_1_mat.xml +1996 -0
- data/test/fixtures/classcv_1_1_point__.xml +341 -0
- data/test/fixtures/classcv_1_1_scalar__.xml +269 -0
- data/test/fixtures/classcv_1_1_size__.xml +270 -0
- data/test/fixtures/dummy_class.cpp.erb +1 -0
- data/test/fixtures/dummy_function.cpp.erb +1 -0
- data/test/fixtures/group___magic_type.xml +406 -0
- data/test/fixtures/namespacecv.xml +12659 -0
- data/test/function_group_test.rb +43 -0
- data/test/function_test.rb +405 -0
- data/test/group_test.rb +241 -0
- data/test/helper.rb +34 -0
- data/test/klass_test.rb +551 -0
- data/test/lua_function_gen_test.rb +242 -0
- data/test/namespace_test.rb +220 -0
- data/test/parser_test.rb +36 -0
- metadata +229 -272
- checksums.yaml +0 -7
- data/lib/open_api_sdk/analytics.rb +0 -99
- data/lib/open_api_sdk/domains.rb +0 -353
- data/lib/open_api_sdk/dub.rb +0 -88
- data/lib/open_api_sdk/links.rb +0 -766
- data/lib/open_api_sdk/metatags.rb +0 -54
- data/lib/open_api_sdk/models/operations/bulkcreatelinks_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/bulkupdatelinks_requestbody.rb +0 -27
- data/lib/open_api_sdk/models/operations/bulkupdatelinks_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/color.rb +0 -24
- data/lib/open_api_sdk/models/operations/createdomain_requestbody.rb +0 -33
- data/lib/open_api_sdk/models/operations/createdomain_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/createlink_requestbody.rb +0 -95
- data/lib/open_api_sdk/models/operations/createlink_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/createtag_requestbody.rb +0 -32
- data/lib/open_api_sdk/models/operations/createtag_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/data.rb +0 -83
- data/lib/open_api_sdk/models/operations/deletedomain_request.rb +0 -24
- data/lib/open_api_sdk/models/operations/deletedomain_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/deletedomain_responsebody.rb +0 -24
- data/lib/open_api_sdk/models/operations/deletelink_request.rb +0 -24
- data/lib/open_api_sdk/models/operations/deletelink_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/deletelink_responsebody.rb +0 -24
- data/lib/open_api_sdk/models/operations/event.rb +0 -21
- data/lib/open_api_sdk/models/operations/getlinkinfo_request.rb +0 -33
- data/lib/open_api_sdk/models/operations/getlinkinfo_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/getlinks_request.rb +0 -51
- data/lib/open_api_sdk/models/operations/getlinks_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/getlinkscount_request.rb +0 -48
- data/lib/open_api_sdk/models/operations/getlinkscount_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/getmetatags_request.rb +0 -24
- data/lib/open_api_sdk/models/operations/getmetatags_response.rb +0 -33
- data/lib/open_api_sdk/models/operations/getmetatags_responsebody.rb +0 -30
- data/lib/open_api_sdk/models/operations/getqrcode_request.rb +0 -39
- data/lib/open_api_sdk/models/operations/getqrcode_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/gettags_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/getworkspace_request.rb +0 -24
- data/lib/open_api_sdk/models/operations/getworkspace_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/groupby.rb +0 -28
- data/lib/open_api_sdk/models/operations/interval.rb +0 -25
- data/lib/open_api_sdk/models/operations/level.rb +0 -21
- data/lib/open_api_sdk/models/operations/listdomains_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/paymentprocessor.rb +0 -20
- data/lib/open_api_sdk/models/operations/requestbody.rb +0 -95
- data/lib/open_api_sdk/models/operations/retrieveanalytics_request.rb +0 -81
- data/lib/open_api_sdk/models/operations/retrieveanalytics_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/sort.rb +0 -20
- data/lib/open_api_sdk/models/operations/trackcustomer_requestbody.rb +0 -33
- data/lib/open_api_sdk/models/operations/trackcustomer_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/trackcustomer_responsebody.rb +0 -33
- data/lib/open_api_sdk/models/operations/tracklead_requestbody.rb +0 -42
- data/lib/open_api_sdk/models/operations/tracklead_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/tracklead_responsebody.rb +0 -42
- data/lib/open_api_sdk/models/operations/tracksale_requestbody.rb +0 -42
- data/lib/open_api_sdk/models/operations/tracksale_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/tracksale_responsebody.rb +0 -42
- data/lib/open_api_sdk/models/operations/updatedomain_request.rb +0 -27
- data/lib/open_api_sdk/models/operations/updatedomain_requestbody.rb +0 -33
- data/lib/open_api_sdk/models/operations/updatedomain_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/updatelink_request.rb +0 -27
- data/lib/open_api_sdk/models/operations/updatelink_requestbody.rb +0 -95
- data/lib/open_api_sdk/models/operations/updatelink_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/updatetag_color.rb +0 -24
- data/lib/open_api_sdk/models/operations/updatetag_request.rb +0 -27
- data/lib/open_api_sdk/models/operations/updatetag_requestbody.rb +0 -32
- data/lib/open_api_sdk/models/operations/updatetag_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/updateworkspace_request.rb +0 -27
- data/lib/open_api_sdk/models/operations/updateworkspace_requestbody.rb +0 -27
- data/lib/open_api_sdk/models/operations/updateworkspace_response.rb +0 -60
- data/lib/open_api_sdk/models/operations/upsertlink_requestbody.rb +0 -95
- data/lib/open_api_sdk/models/operations/upsertlink_response.rb +0 -60
- data/lib/open_api_sdk/models/operations.rb +0 -74
- data/lib/open_api_sdk/models/shared/badrequest.rb +0 -24
- data/lib/open_api_sdk/models/shared/code.rb +0 -18
- data/lib/open_api_sdk/models/shared/color.rb +0 -24
- data/lib/open_api_sdk/models/shared/conflict.rb +0 -24
- data/lib/open_api_sdk/models/shared/conflict_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/conflict_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/countrycode.rb +0 -267
- data/lib/open_api_sdk/models/shared/domains.rb +0 -27
- data/lib/open_api_sdk/models/shared/domainschema.rb +0 -48
- data/lib/open_api_sdk/models/shared/error.rb +0 -30
- data/lib/open_api_sdk/models/shared/forbidden.rb +0 -24
- data/lib/open_api_sdk/models/shared/forbidden_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/forbidden_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/geo.rb +0 -771
- data/lib/open_api_sdk/models/shared/internalservererror.rb +0 -24
- data/lib/open_api_sdk/models/shared/internalservererror_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/internalservererror_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/inviteexpired.rb +0 -24
- data/lib/open_api_sdk/models/shared/inviteexpired_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/inviteexpired_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/linkgeotargeting.rb +0 -771
- data/lib/open_api_sdk/models/shared/linkschema.rb +0 -142
- data/lib/open_api_sdk/models/shared/notfound.rb +0 -24
- data/lib/open_api_sdk/models/shared/notfound_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/notfound_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/plan.rb +0 -24
- data/lib/open_api_sdk/models/shared/ratelimitexceeded.rb +0 -24
- data/lib/open_api_sdk/models/shared/ratelimitexceeded_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/ratelimitexceeded_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/role.rb +0 -19
- data/lib/open_api_sdk/models/shared/security.rb +0 -24
- data/lib/open_api_sdk/models/shared/tagschema.rb +0 -30
- data/lib/open_api_sdk/models/shared/unauthorized.rb +0 -24
- data/lib/open_api_sdk/models/shared/unauthorized_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/unauthorized_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/unprocessableentity.rb +0 -24
- data/lib/open_api_sdk/models/shared/unprocessableentity_code.rb +0 -18
- data/lib/open_api_sdk/models/shared/unprocessableentity_error.rb +0 -30
- data/lib/open_api_sdk/models/shared/users.rb +0 -24
- data/lib/open_api_sdk/models/shared/workspaceschema.rb +0 -81
- data/lib/open_api_sdk/models/shared.rb +0 -49
- data/lib/open_api_sdk/qr_codes.rb +0 -97
- data/lib/open_api_sdk/sdkconfiguration.rb +0 -52
- data/lib/open_api_sdk/tags.rb +0 -272
- data/lib/open_api_sdk/track.rb +0 -276
- data/lib/open_api_sdk/utils/metadata_fields.rb +0 -150
- data/lib/open_api_sdk/utils/t.rb +0 -59
- data/lib/open_api_sdk/utils/utils.rb +0 -772
- data/lib/open_api_sdk/workspaces.rb +0 -192
@@ -0,0 +1,110 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lgc.h,v 2.15 2005/08/24 16:15:49 roberto Exp $
|
3
|
+
** Garbage Collector
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
#ifndef lgc_h
|
8
|
+
#define lgc_h
|
9
|
+
|
10
|
+
|
11
|
+
#include "lobject.h"
|
12
|
+
|
13
|
+
|
14
|
+
/*
|
15
|
+
** Possible states of the Garbage Collector
|
16
|
+
*/
|
17
|
+
#define GCSpause 0
|
18
|
+
#define GCSpropagate 1
|
19
|
+
#define GCSsweepstring 2
|
20
|
+
#define GCSsweep 3
|
21
|
+
#define GCSfinalize 4
|
22
|
+
|
23
|
+
|
24
|
+
/*
|
25
|
+
** some userful bit tricks
|
26
|
+
*/
|
27
|
+
#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
|
28
|
+
#define setbits(x,m) ((x) |= (m))
|
29
|
+
#define testbits(x,m) ((x) & (m))
|
30
|
+
#define bitmask(b) (1<<(b))
|
31
|
+
#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
|
32
|
+
#define l_setbit(x,b) setbits(x, bitmask(b))
|
33
|
+
#define resetbit(x,b) resetbits(x, bitmask(b))
|
34
|
+
#define testbit(x,b) testbits(x, bitmask(b))
|
35
|
+
#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
|
36
|
+
#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
|
37
|
+
#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
/*
|
42
|
+
** Layout for bit use in `marked' field:
|
43
|
+
** bit 0 - object is white (type 0)
|
44
|
+
** bit 1 - object is white (type 1)
|
45
|
+
** bit 2 - object is black
|
46
|
+
** bit 3 - for userdata: has been finalized
|
47
|
+
** bit 3 - for tables: has weak keys
|
48
|
+
** bit 4 - for tables: has weak values
|
49
|
+
** bit 5 - object is fixed (should not be collected)
|
50
|
+
** bit 6 - object is "super" fixed (only the main thread)
|
51
|
+
*/
|
52
|
+
|
53
|
+
|
54
|
+
#define WHITE0BIT 0
|
55
|
+
#define WHITE1BIT 1
|
56
|
+
#define BLACKBIT 2
|
57
|
+
#define FINALIZEDBIT 3
|
58
|
+
#define KEYWEAKBIT 3
|
59
|
+
#define VALUEWEAKBIT 4
|
60
|
+
#define FIXEDBIT 5
|
61
|
+
#define SFIXEDBIT 6
|
62
|
+
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
63
|
+
|
64
|
+
|
65
|
+
#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
|
66
|
+
#define isblack(x) testbit((x)->gch.marked, BLACKBIT)
|
67
|
+
#define isgray(x) (!isblack(x) && !iswhite(x))
|
68
|
+
|
69
|
+
#define otherwhite(g) (g->currentwhite ^ WHITEBITS)
|
70
|
+
#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
|
71
|
+
|
72
|
+
#define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
|
73
|
+
#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
|
74
|
+
|
75
|
+
#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
|
76
|
+
|
77
|
+
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
78
|
+
|
79
|
+
|
80
|
+
#define luaC_checkGC(L) { \
|
81
|
+
condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \
|
82
|
+
if (G(L)->totalbytes >= G(L)->GCthreshold) \
|
83
|
+
luaC_step(L); }
|
84
|
+
|
85
|
+
|
86
|
+
#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
|
87
|
+
luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
|
88
|
+
|
89
|
+
#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
|
90
|
+
luaC_barrierback(L,t); }
|
91
|
+
|
92
|
+
#define luaC_objbarrier(L,p,o) \
|
93
|
+
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
|
94
|
+
luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
|
95
|
+
|
96
|
+
#define luaC_objbarriert(L,t,o) \
|
97
|
+
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
|
98
|
+
|
99
|
+
LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
|
100
|
+
LUAI_FUNC void luaC_callGCTM (lua_State *L);
|
101
|
+
LUAI_FUNC void luaC_freeall (lua_State *L);
|
102
|
+
LUAI_FUNC void luaC_step (lua_State *L);
|
103
|
+
LUAI_FUNC void luaC_fullgc (lua_State *L);
|
104
|
+
LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
|
105
|
+
LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
|
106
|
+
LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
|
107
|
+
LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
|
108
|
+
|
109
|
+
|
110
|
+
#endif
|
Binary file
|
@@ -0,0 +1,38 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: linit.c,v 1.14 2005/12/29 15:32:11 roberto Exp $
|
3
|
+
** Initialization of libraries for lua.c
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#define linit_c
|
9
|
+
#define LUA_LIB
|
10
|
+
|
11
|
+
#include "lua.h"
|
12
|
+
|
13
|
+
#include "lualib.h"
|
14
|
+
#include "lauxlib.h"
|
15
|
+
|
16
|
+
|
17
|
+
static const luaL_Reg lualibs[] = {
|
18
|
+
{"", luaopen_base},
|
19
|
+
{LUA_LOADLIBNAME, luaopen_package},
|
20
|
+
{LUA_TABLIBNAME, luaopen_table},
|
21
|
+
{LUA_IOLIBNAME, luaopen_io},
|
22
|
+
{LUA_OSLIBNAME, luaopen_os},
|
23
|
+
{LUA_STRLIBNAME, luaopen_string},
|
24
|
+
{LUA_MATHLIBNAME, luaopen_math},
|
25
|
+
{LUA_DBLIBNAME, luaopen_debug},
|
26
|
+
{NULL, NULL}
|
27
|
+
};
|
28
|
+
|
29
|
+
|
30
|
+
LUALIB_API void luaL_openlibs (lua_State *L) {
|
31
|
+
const luaL_Reg *lib = lualibs;
|
32
|
+
for (; lib->func; lib++) {
|
33
|
+
lua_pushcfunction(L, lib->func);
|
34
|
+
lua_pushstring(L, lib->name);
|
35
|
+
lua_call(L, 1, 0);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
@@ -0,0 +1,532 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: liolib.c,v 2.73 2006/05/08 20:14:16 roberto Exp $
|
3
|
+
** Standard I/O (and system) library
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#include <errno.h>
|
9
|
+
#include <stdio.h>
|
10
|
+
#include <stdlib.h>
|
11
|
+
#include <string.h>
|
12
|
+
|
13
|
+
#define liolib_c
|
14
|
+
#define LUA_LIB
|
15
|
+
|
16
|
+
#include "lua.h"
|
17
|
+
|
18
|
+
#include "lauxlib.h"
|
19
|
+
#include "lualib.h"
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
#define IO_INPUT 1
|
24
|
+
#define IO_OUTPUT 2
|
25
|
+
|
26
|
+
|
27
|
+
static const char *const fnames[] = {"input", "output"};
|
28
|
+
|
29
|
+
|
30
|
+
static int pushresult (lua_State *L, int i, const char *filename) {
|
31
|
+
int en = errno; /* calls to Lua API may change this value */
|
32
|
+
if (i) {
|
33
|
+
lua_pushboolean(L, 1);
|
34
|
+
return 1;
|
35
|
+
}
|
36
|
+
else {
|
37
|
+
lua_pushnil(L);
|
38
|
+
if (filename)
|
39
|
+
lua_pushfstring(L, "%s: %s", filename, strerror(en));
|
40
|
+
else
|
41
|
+
lua_pushfstring(L, "%s", strerror(en));
|
42
|
+
lua_pushinteger(L, en);
|
43
|
+
return 3;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
static void fileerror (lua_State *L, int arg, const char *filename) {
|
49
|
+
lua_pushfstring(L, "%s: %s", filename, strerror(errno));
|
50
|
+
luaL_argerror(L, arg, lua_tostring(L, -1));
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
|
55
|
+
|
56
|
+
|
57
|
+
static int io_type (lua_State *L) {
|
58
|
+
void *ud;
|
59
|
+
luaL_checkany(L, 1);
|
60
|
+
ud = lua_touserdata(L, 1);
|
61
|
+
lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
|
62
|
+
if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
|
63
|
+
lua_pushnil(L); /* not a file */
|
64
|
+
else if (*((FILE **)ud) == NULL)
|
65
|
+
lua_pushliteral(L, "closed file");
|
66
|
+
else
|
67
|
+
lua_pushliteral(L, "file");
|
68
|
+
return 1;
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
static FILE *tofile (lua_State *L) {
|
73
|
+
FILE **f = topfile(L);
|
74
|
+
if (*f == NULL)
|
75
|
+
luaL_error(L, "attempt to use a closed file");
|
76
|
+
return *f;
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
/*
|
82
|
+
** When creating file handles, always creates a `closed' file handle
|
83
|
+
** before opening the actual file; so, if there is a memory error, the
|
84
|
+
** file is not left opened.
|
85
|
+
*/
|
86
|
+
static FILE **newfile (lua_State *L) {
|
87
|
+
FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
|
88
|
+
*pf = NULL; /* file handle is currently `closed' */
|
89
|
+
luaL_getmetatable(L, LUA_FILEHANDLE);
|
90
|
+
lua_setmetatable(L, -2);
|
91
|
+
return pf;
|
92
|
+
}
|
93
|
+
|
94
|
+
|
95
|
+
/*
|
96
|
+
** this function has a separated environment, which defines the
|
97
|
+
** correct __close for 'popen' files
|
98
|
+
*/
|
99
|
+
static int io_pclose (lua_State *L) {
|
100
|
+
FILE **p = topfile(L);
|
101
|
+
int ok = lua_pclose(L, *p);
|
102
|
+
*p = NULL;
|
103
|
+
return pushresult(L, ok, NULL);
|
104
|
+
}
|
105
|
+
|
106
|
+
|
107
|
+
static int io_fclose (lua_State *L) {
|
108
|
+
FILE **p = topfile(L);
|
109
|
+
int ok = (fclose(*p) == 0);
|
110
|
+
*p = NULL;
|
111
|
+
return pushresult(L, ok, NULL);
|
112
|
+
}
|
113
|
+
|
114
|
+
|
115
|
+
static int aux_close (lua_State *L) {
|
116
|
+
lua_getfenv(L, 1);
|
117
|
+
lua_getfield(L, -1, "__close");
|
118
|
+
return (lua_tocfunction(L, -1))(L);
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
static int io_close (lua_State *L) {
|
123
|
+
if (lua_isnone(L, 1))
|
124
|
+
lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
|
125
|
+
tofile(L); /* make sure argument is a file */
|
126
|
+
return aux_close(L);
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
static int io_gc (lua_State *L) {
|
131
|
+
FILE *f = *topfile(L);
|
132
|
+
/* ignore closed files and standard files */
|
133
|
+
if (f != NULL && f != stdin && f != stdout && f != stderr)
|
134
|
+
aux_close(L);
|
135
|
+
return 0;
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
static int io_tostring (lua_State *L) {
|
140
|
+
FILE *f = *topfile(L);
|
141
|
+
if (f == NULL)
|
142
|
+
lua_pushstring(L, "file (closed)");
|
143
|
+
else
|
144
|
+
lua_pushfstring(L, "file (%p)", f);
|
145
|
+
return 1;
|
146
|
+
}
|
147
|
+
|
148
|
+
|
149
|
+
static int io_open (lua_State *L) {
|
150
|
+
const char *filename = luaL_checkstring(L, 1);
|
151
|
+
const char *mode = luaL_optstring(L, 2, "r");
|
152
|
+
FILE **pf = newfile(L);
|
153
|
+
*pf = fopen(filename, mode);
|
154
|
+
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
static int io_popen (lua_State *L) {
|
159
|
+
const char *filename = luaL_checkstring(L, 1);
|
160
|
+
const char *mode = luaL_optstring(L, 2, "r");
|
161
|
+
FILE **pf = newfile(L);
|
162
|
+
*pf = lua_popen(L, filename, mode);
|
163
|
+
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
|
164
|
+
}
|
165
|
+
|
166
|
+
|
167
|
+
static int io_tmpfile (lua_State *L) {
|
168
|
+
FILE **pf = newfile(L);
|
169
|
+
*pf = tmpfile();
|
170
|
+
return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
static FILE *getiofile (lua_State *L, int findex) {
|
175
|
+
FILE *f;
|
176
|
+
lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
|
177
|
+
f = *(FILE **)lua_touserdata(L, -1);
|
178
|
+
if (f == NULL)
|
179
|
+
luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
|
180
|
+
return f;
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
static int g_iofile (lua_State *L, int f, const char *mode) {
|
185
|
+
if (!lua_isnoneornil(L, 1)) {
|
186
|
+
const char *filename = lua_tostring(L, 1);
|
187
|
+
if (filename) {
|
188
|
+
FILE **pf = newfile(L);
|
189
|
+
*pf = fopen(filename, mode);
|
190
|
+
if (*pf == NULL)
|
191
|
+
fileerror(L, 1, filename);
|
192
|
+
}
|
193
|
+
else {
|
194
|
+
tofile(L); /* check that it's a valid file handle */
|
195
|
+
lua_pushvalue(L, 1);
|
196
|
+
}
|
197
|
+
lua_rawseti(L, LUA_ENVIRONINDEX, f);
|
198
|
+
}
|
199
|
+
/* return current value */
|
200
|
+
lua_rawgeti(L, LUA_ENVIRONINDEX, f);
|
201
|
+
return 1;
|
202
|
+
}
|
203
|
+
|
204
|
+
|
205
|
+
static int io_input (lua_State *L) {
|
206
|
+
return g_iofile(L, IO_INPUT, "r");
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
static int io_output (lua_State *L) {
|
211
|
+
return g_iofile(L, IO_OUTPUT, "w");
|
212
|
+
}
|
213
|
+
|
214
|
+
|
215
|
+
static int io_readline (lua_State *L);
|
216
|
+
|
217
|
+
|
218
|
+
static void aux_lines (lua_State *L, int idx, int toclose) {
|
219
|
+
lua_pushvalue(L, idx);
|
220
|
+
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
221
|
+
lua_pushcclosure(L, io_readline, 2);
|
222
|
+
}
|
223
|
+
|
224
|
+
|
225
|
+
static int f_lines (lua_State *L) {
|
226
|
+
tofile(L); /* check that it's a valid file handle */
|
227
|
+
aux_lines(L, 1, 0);
|
228
|
+
return 1;
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
static int io_lines (lua_State *L) {
|
233
|
+
if (lua_isnoneornil(L, 1)) { /* no arguments? */
|
234
|
+
/* will iterate over default input */
|
235
|
+
lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
|
236
|
+
return f_lines(L);
|
237
|
+
}
|
238
|
+
else {
|
239
|
+
const char *filename = luaL_checkstring(L, 1);
|
240
|
+
FILE **pf = newfile(L);
|
241
|
+
*pf = fopen(filename, "r");
|
242
|
+
if (*pf == NULL)
|
243
|
+
fileerror(L, 1, filename);
|
244
|
+
aux_lines(L, lua_gettop(L), 1);
|
245
|
+
return 1;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
/*
|
251
|
+
** {======================================================
|
252
|
+
** READ
|
253
|
+
** =======================================================
|
254
|
+
*/
|
255
|
+
|
256
|
+
|
257
|
+
static int read_number (lua_State *L, FILE *f) {
|
258
|
+
lua_Number d;
|
259
|
+
if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
|
260
|
+
lua_pushnumber(L, d);
|
261
|
+
return 1;
|
262
|
+
}
|
263
|
+
else return 0; /* read fails */
|
264
|
+
}
|
265
|
+
|
266
|
+
|
267
|
+
static int test_eof (lua_State *L, FILE *f) {
|
268
|
+
int c = getc(f);
|
269
|
+
ungetc(c, f);
|
270
|
+
lua_pushlstring(L, NULL, 0);
|
271
|
+
return (c != EOF);
|
272
|
+
}
|
273
|
+
|
274
|
+
|
275
|
+
static int read_line (lua_State *L, FILE *f) {
|
276
|
+
luaL_Buffer b;
|
277
|
+
luaL_buffinit(L, &b);
|
278
|
+
for (;;) {
|
279
|
+
size_t l;
|
280
|
+
char *p = luaL_prepbuffer(&b);
|
281
|
+
if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
|
282
|
+
luaL_pushresult(&b); /* close buffer */
|
283
|
+
return (lua_strlen(L, -1) > 0); /* check whether read something */
|
284
|
+
}
|
285
|
+
l = strlen(p);
|
286
|
+
if (l == 0 || p[l-1] != '\n')
|
287
|
+
luaL_addsize(&b, l);
|
288
|
+
else {
|
289
|
+
luaL_addsize(&b, l - 1); /* do not include `eol' */
|
290
|
+
luaL_pushresult(&b); /* close buffer */
|
291
|
+
return 1; /* read at least an `eol' */
|
292
|
+
}
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
|
297
|
+
static int read_chars (lua_State *L, FILE *f, size_t n) {
|
298
|
+
size_t rlen; /* how much to read */
|
299
|
+
size_t nr; /* number of chars actually read */
|
300
|
+
luaL_Buffer b;
|
301
|
+
luaL_buffinit(L, &b);
|
302
|
+
rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
|
303
|
+
do {
|
304
|
+
char *p = luaL_prepbuffer(&b);
|
305
|
+
if (rlen > n) rlen = n; /* cannot read more than asked */
|
306
|
+
nr = fread(p, sizeof(char), rlen, f);
|
307
|
+
luaL_addsize(&b, nr);
|
308
|
+
n -= nr; /* still have to read `n' chars */
|
309
|
+
} while (n > 0 && nr == rlen); /* until end of count or eof */
|
310
|
+
luaL_pushresult(&b); /* close buffer */
|
311
|
+
return (n == 0 || lua_strlen(L, -1) > 0);
|
312
|
+
}
|
313
|
+
|
314
|
+
|
315
|
+
static int g_read (lua_State *L, FILE *f, int first) {
|
316
|
+
int nargs = lua_gettop(L) - 1;
|
317
|
+
int success;
|
318
|
+
int n;
|
319
|
+
clearerr(f);
|
320
|
+
if (nargs == 0) { /* no arguments? */
|
321
|
+
success = read_line(L, f);
|
322
|
+
n = first+1; /* to return 1 result */
|
323
|
+
}
|
324
|
+
else { /* ensure stack space for all results and for auxlib's buffer */
|
325
|
+
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
|
326
|
+
success = 1;
|
327
|
+
for (n = first; nargs-- && success; n++) {
|
328
|
+
if (lua_type(L, n) == LUA_TNUMBER) {
|
329
|
+
size_t l = (size_t)lua_tointeger(L, n);
|
330
|
+
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
331
|
+
}
|
332
|
+
else {
|
333
|
+
const char *p = lua_tostring(L, n);
|
334
|
+
luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
|
335
|
+
switch (p[1]) {
|
336
|
+
case 'n': /* number */
|
337
|
+
success = read_number(L, f);
|
338
|
+
break;
|
339
|
+
case 'l': /* line */
|
340
|
+
success = read_line(L, f);
|
341
|
+
break;
|
342
|
+
case 'a': /* file */
|
343
|
+
read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
|
344
|
+
success = 1; /* always success */
|
345
|
+
break;
|
346
|
+
default:
|
347
|
+
return luaL_argerror(L, n, "invalid format");
|
348
|
+
}
|
349
|
+
}
|
350
|
+
}
|
351
|
+
}
|
352
|
+
if (ferror(f))
|
353
|
+
return pushresult(L, 0, NULL);
|
354
|
+
if (!success) {
|
355
|
+
lua_pop(L, 1); /* remove last result */
|
356
|
+
lua_pushnil(L); /* push nil instead */
|
357
|
+
}
|
358
|
+
return n - first;
|
359
|
+
}
|
360
|
+
|
361
|
+
|
362
|
+
static int io_read (lua_State *L) {
|
363
|
+
return g_read(L, getiofile(L, IO_INPUT), 1);
|
364
|
+
}
|
365
|
+
|
366
|
+
|
367
|
+
static int f_read (lua_State *L) {
|
368
|
+
return g_read(L, tofile(L), 2);
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
static int io_readline (lua_State *L) {
|
373
|
+
FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
|
374
|
+
int sucess;
|
375
|
+
if (f == NULL) /* file is already closed? */
|
376
|
+
luaL_error(L, "file is already closed");
|
377
|
+
sucess = read_line(L, f);
|
378
|
+
if (ferror(f))
|
379
|
+
return luaL_error(L, "%s", strerror(errno));
|
380
|
+
if (sucess) return 1;
|
381
|
+
else { /* EOF */
|
382
|
+
if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
|
383
|
+
lua_settop(L, 0);
|
384
|
+
lua_pushvalue(L, lua_upvalueindex(1));
|
385
|
+
aux_close(L); /* close it */
|
386
|
+
}
|
387
|
+
return 0;
|
388
|
+
}
|
389
|
+
}
|
390
|
+
|
391
|
+
/* }====================================================== */
|
392
|
+
|
393
|
+
|
394
|
+
static int g_write (lua_State *L, FILE *f, int arg) {
|
395
|
+
int nargs = lua_gettop(L) - 1;
|
396
|
+
int status = 1;
|
397
|
+
for (; nargs--; arg++) {
|
398
|
+
if (lua_type(L, arg) == LUA_TNUMBER) {
|
399
|
+
/* optimization: could be done exactly as for strings */
|
400
|
+
status = status &&
|
401
|
+
fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
|
402
|
+
}
|
403
|
+
else {
|
404
|
+
size_t l;
|
405
|
+
const char *s = luaL_checklstring(L, arg, &l);
|
406
|
+
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
407
|
+
}
|
408
|
+
}
|
409
|
+
return pushresult(L, status, NULL);
|
410
|
+
}
|
411
|
+
|
412
|
+
|
413
|
+
static int io_write (lua_State *L) {
|
414
|
+
return g_write(L, getiofile(L, IO_OUTPUT), 1);
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
static int f_write (lua_State *L) {
|
419
|
+
return g_write(L, tofile(L), 2);
|
420
|
+
}
|
421
|
+
|
422
|
+
|
423
|
+
static int f_seek (lua_State *L) {
|
424
|
+
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
425
|
+
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
426
|
+
FILE *f = tofile(L);
|
427
|
+
int op = luaL_checkoption(L, 2, "cur", modenames);
|
428
|
+
long offset = luaL_optlong(L, 3, 0);
|
429
|
+
op = fseek(f, offset, mode[op]);
|
430
|
+
if (op)
|
431
|
+
return pushresult(L, 0, NULL); /* error */
|
432
|
+
else {
|
433
|
+
lua_pushinteger(L, ftell(f));
|
434
|
+
return 1;
|
435
|
+
}
|
436
|
+
}
|
437
|
+
|
438
|
+
|
439
|
+
static int f_setvbuf (lua_State *L) {
|
440
|
+
static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
|
441
|
+
static const char *const modenames[] = {"no", "full", "line", NULL};
|
442
|
+
FILE *f = tofile(L);
|
443
|
+
int op = luaL_checkoption(L, 2, NULL, modenames);
|
444
|
+
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
445
|
+
int res = setvbuf(f, NULL, mode[op], sz);
|
446
|
+
return pushresult(L, res == 0, NULL);
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
static int io_flush (lua_State *L) {
|
452
|
+
return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
static int f_flush (lua_State *L) {
|
457
|
+
return pushresult(L, fflush(tofile(L)) == 0, NULL);
|
458
|
+
}
|
459
|
+
|
460
|
+
|
461
|
+
static const luaL_Reg iolib[] = {
|
462
|
+
{"close", io_close},
|
463
|
+
{"flush", io_flush},
|
464
|
+
{"input", io_input},
|
465
|
+
{"lines", io_lines},
|
466
|
+
{"open", io_open},
|
467
|
+
{"output", io_output},
|
468
|
+
{"popen", io_popen},
|
469
|
+
{"read", io_read},
|
470
|
+
{"tmpfile", io_tmpfile},
|
471
|
+
{"type", io_type},
|
472
|
+
{"write", io_write},
|
473
|
+
{NULL, NULL}
|
474
|
+
};
|
475
|
+
|
476
|
+
|
477
|
+
static const luaL_Reg flib[] = {
|
478
|
+
{"close", io_close},
|
479
|
+
{"flush", f_flush},
|
480
|
+
{"lines", f_lines},
|
481
|
+
{"read", f_read},
|
482
|
+
{"seek", f_seek},
|
483
|
+
{"setvbuf", f_setvbuf},
|
484
|
+
{"write", f_write},
|
485
|
+
{"__gc", io_gc},
|
486
|
+
{"__tostring", io_tostring},
|
487
|
+
{NULL, NULL}
|
488
|
+
};
|
489
|
+
|
490
|
+
|
491
|
+
static void createmeta (lua_State *L) {
|
492
|
+
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
493
|
+
lua_pushvalue(L, -1); /* push metatable */
|
494
|
+
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
495
|
+
luaL_register(L, NULL, flib); /* file methods */
|
496
|
+
}
|
497
|
+
|
498
|
+
|
499
|
+
static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
|
500
|
+
*newfile(L) = f;
|
501
|
+
if (k > 0) {
|
502
|
+
lua_pushvalue(L, -1);
|
503
|
+
lua_rawseti(L, LUA_ENVIRONINDEX, k);
|
504
|
+
}
|
505
|
+
lua_setfield(L, -2, fname);
|
506
|
+
}
|
507
|
+
|
508
|
+
|
509
|
+
LUALIB_API int luaopen_io (lua_State *L) {
|
510
|
+
createmeta(L);
|
511
|
+
/* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
|
512
|
+
lua_createtable(L, 2, 1);
|
513
|
+
lua_replace(L, LUA_ENVIRONINDEX);
|
514
|
+
/* open library */
|
515
|
+
luaL_register(L, LUA_IOLIBNAME, iolib);
|
516
|
+
/* create (and set) default files */
|
517
|
+
createstdfile(L, stdin, IO_INPUT, "stdin");
|
518
|
+
createstdfile(L, stdout, IO_OUTPUT, "stdout");
|
519
|
+
createstdfile(L, stderr, 0, "stderr");
|
520
|
+
/* create environment for 'popen' */
|
521
|
+
lua_getfield(L, -1, "popen");
|
522
|
+
lua_createtable(L, 0, 1);
|
523
|
+
lua_pushcfunction(L, io_pclose);
|
524
|
+
lua_setfield(L, -2, "__close");
|
525
|
+
lua_setfenv(L, -2);
|
526
|
+
lua_pop(L, 1); /* pop 'popen' */
|
527
|
+
/* set default close function */
|
528
|
+
lua_pushcfunction(L, io_fclose);
|
529
|
+
lua_setfield(L, LUA_ENVIRONINDEX, "__close");
|
530
|
+
return 1;
|
531
|
+
}
|
532
|
+
|