tagged release 0.6.4
[parrot.git] / languages / lua / src / lib / luabasic.pir
blob7dc35b16ed4b8f2f6c34e6fcc56065c239a82629
1 # Copyright (C) 2005-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 lib/luabasic.pir - Lua Basic Library
8 =head1 DESCRIPTION
10 The basic library provides some core functions to Lua.
12 See "Lua 5.1 Reference Manual", section 5.1 "Basic Functions",
13 L<http://www.lua.org/manual/5.1/manual.html#5.1>.
15 =head2 Variables
17 =over 4
19 =cut
21 .HLL 'Lua', 'lua_group'
22 .namespace [ 'Lua::basic' ]
24 .sub 'luaopen_basic'
25 #    print "init Lua Basic\n"
27     .local pmc _lua__REGISTRY
28     new _lua__REGISTRY, 'LuaTable'
29     set_hll_global '_REGISTRY', _lua__REGISTRY
31     .local pmc _lua__GLOBAL
32     new _lua__GLOBAL, 'LuaTable'
33     set_hll_global '_G', _lua__GLOBAL
34     new $P1, 'LuaString'
36     set $P1, '_LOADED'
37     new $P0, 'LuaTable'
38     _lua__REGISTRY[$P1] = $P0
40 =item C<_G>
42 A global variable (not a function) that holds the global environment (that is,
43 C<_G._G = _G>). Lua itself does not use this variable; changing its value
44 does not affect any environment, nor vice-versa. (Use C<setfenv> to change
45 environments.)
47 =cut
49     set $P1, '_G'
50     _lua__GLOBAL[$P1] = _lua__GLOBAL
52     lua_register($P1, _lua__GLOBAL)
54 =item C<_VERSION>
56 A global variable (not a function) that holds a string containing the current
57 interpreter version. The current contents of this variable is C<"Lua 5.1">.
59 =cut
61     new $P0, 'LuaString'
62     set $P0, "Lua 5.1 (on Parrot)"
63     set $P1, '_VERSION'
64     _lua__GLOBAL[$P1] = $P0
66 =back
68 =head2 Functions
70 =over 4
72 =cut
74     .const .Sub _lua_assert = 'assert'
75     _lua_assert.'setfenv'(_lua__GLOBAL)
76     set $P1, 'assert'
77     _lua__GLOBAL[$P1] = _lua_assert
79     .const .Sub _lua_collectgarbage = 'collectgarbage'
80     _lua_collectgarbage.'setfenv'(_lua__GLOBAL)
81     set $P1, 'collectgarbage'
82     _lua__GLOBAL[$P1] = _lua_collectgarbage
84     .const .Sub _lua_dofile = 'dofile'
85     _lua_dofile.'setfenv'(_lua__GLOBAL)
86     set $P1, 'dofile'
87     _lua__GLOBAL[$P1] = _lua_dofile
89     .const .Sub _lua_error = 'error'
90     _lua_error.'setfenv'(_lua__GLOBAL)
91     set $P1, 'error'
92     _lua__GLOBAL[$P1] = _lua_error
94     .const .Sub _lua_getfenv = 'getfenv'
95     _lua_getfenv.'setfenv'(_lua__GLOBAL)
96     set $P1, 'getfenv'
97     _lua__GLOBAL[$P1] = _lua_getfenv
99     .const .Sub _lua_getmetatable = 'getmetatable'
100     _lua_getmetatable.'setfenv'(_lua__GLOBAL)
101     set $P1, 'getmetatable'
102     _lua__GLOBAL[$P1] = _lua_getmetatable
104     .const .Sub _lua_ipairs = 'ipairs'
105     _lua_ipairs.'setfenv'(_lua__GLOBAL)
106     set $P1, 'ipairs'
107     _lua__GLOBAL[$P1] = _lua_ipairs
109     .const .Sub _lua_load = 'load'
110     _lua_load.'setfenv'(_lua__GLOBAL)
111     set $P1, 'load'
112     _lua__GLOBAL[$P1] = _lua_load
114     .const .Sub _lua_loadfile = 'loadfile'
115     _lua_loadfile.'setfenv'(_lua__GLOBAL)
116     set $P1, 'loadfile'
117     _lua__GLOBAL[$P1] = _lua_loadfile
119     .const .Sub _lua_loadstring = 'loadstring'
120     _lua_loadstring.'setfenv'(_lua__GLOBAL)
121     set $P1, 'loadstring'
122     _lua__GLOBAL[$P1] = _lua_loadstring
124     .const .Sub _lua_next = 'next'
125     _lua_next.'setfenv'(_lua__GLOBAL)
126     set $P1, 'next'
127     _lua__GLOBAL[$P1] = _lua_next
129     .const .Sub _lua_pairs = 'pairs'
130     _lua_pairs.'setfenv'(_lua__GLOBAL)
131     set $P1, 'pairs'
132     _lua__GLOBAL[$P1] = _lua_pairs
134     .const .Sub _lua_pcall = 'pcall'
135     _lua_pcall.'setfenv'(_lua__GLOBAL)
136     set $P1, 'pcall'
137     _lua__GLOBAL[$P1] = _lua_pcall
139     .const .Sub _lua_print = 'print'
140     _lua_print.'setfenv'(_lua__GLOBAL)
141     set $P1, 'print'
142     _lua__GLOBAL[$P1] = _lua_print
144     .const .Sub _lua_rawequal = 'rawequal'
145     _lua_rawequal.'setfenv'(_lua__GLOBAL)
146     set $P1, 'rawequal'
147     _lua__GLOBAL[$P1] = _lua_rawequal
149     .const .Sub _lua_rawget = 'rawget'
150     _lua_rawget.'setfenv'(_lua__GLOBAL)
151     set $P1, 'rawget'
152     _lua__GLOBAL[$P1] = _lua_rawget
154     .const .Sub _lua_rawset = 'rawset'
155     _lua_rawset.'setfenv'(_lua__GLOBAL)
156     set $P1, 'rawset'
157     _lua__GLOBAL[$P1] = _lua_rawset
159     .const .Sub _lua_select = 'select'
160     _lua_select.'setfenv'(_lua__GLOBAL)
161     set $P1, 'select'
162     _lua__GLOBAL[$P1] = _lua_select
164     .const .Sub _lua_setfenv = 'setfenv'
165     _lua_setfenv.'setfenv'(_lua__GLOBAL)
166     set $P1, 'setfenv'
167     _lua__GLOBAL[$P1] = _lua_setfenv
169     .const .Sub _lua_setmetatable = 'setmetatable'
170     _lua_setmetatable.'setfenv'(_lua__GLOBAL)
171     set $P1, 'setmetatable'
172     _lua__GLOBAL[$P1] = _lua_setmetatable
174     .const .Sub _lua_tonumber = 'tonumber'
175     _lua_tonumber.'setfenv'(_lua__GLOBAL)
176     set $P1, 'tonumber'
177     _lua__GLOBAL[$P1] = _lua_tonumber
179     .const .Sub _lua_tostring = 'tostring'
180     _lua_tostring.'setfenv'(_lua__GLOBAL)
181     set $P1, 'tostring'
182     _lua__GLOBAL[$P1] = _lua_tostring
184     .const .Sub _lua_type = 'type'
185     _lua_type.'setfenv'(_lua__GLOBAL)
186     set $P1, 'type'
187     _lua__GLOBAL[$P1] = _lua_type
189     .const .Sub _lua_unpack = 'unpack'
190     _lua_unpack.'setfenv'(_lua__GLOBAL)
191     set $P1, 'unpack'
192     _lua__GLOBAL[$P1] = _lua_unpack
194     .const .Sub _lua_xpcall = 'xpcall'
195     _lua_xpcall.'setfenv'(_lua__GLOBAL)
196     set $P1, 'xpcall'
197     _lua__GLOBAL[$P1] = _lua_xpcall
199 .end
202 =item C<assert (v [, message])>
204 Issues an error when the value of its argument C<v> is false (i.e., B<nil>
205 or B<false>); otherwise, returns all its arguments. C<message> is an error
206 message; when absent, it defaults to "assertion failed!"
208 =cut
210 .sub 'assert' :anon
211     .param pmc v :optional
212     .param pmc message :optional
213     .param pmc extra :slurpy
214     lua_checkany(1, v)
215     $I0 = istrue v
216     if $I0 goto L1
217     $S2 = lua_optstring(2, message, "assertion failed!")
218     lua_error($S2)
219   L1:
220     .return (v, message, extra :flat)
221 .end
224 =item C<collectgarbage (opt [, arg])>
226 This function is a generic interface to the garbage collector. It performs
227 different functions according to its first argument, C<opt>:
229 =over 4
231 =item B<"stop">
233 stops the garbage collector.
235 =item B<"restart">
237 restarts the garbage collector.
239 =item B<"collect">
241 performs a full garbage-collection cycle.
243 =item B<"count">
245 returns the total memory in use by Lua (in Kbytes).
247 =item B<"step">
249 performs a garbage-collection step. The step C<"size"> is controlled
250 by C<arg> (larger values mean more steps) in a non-specified way.
251 If you want to control the step size you must tune experimentally the value
252 of C<arg>. Returns B<true> if the step finished a collection cycle.
254 =item B<"steppause">
256 sets C<arg>/100 as the new value for the I<pause> of the collector.
258 =item B<"setstepmul">
260 sets C<arg>/100 as the new value for the I<step multiplier> of the collector.
262 =back
264 STILL INCOMPLETE (see lua_gc).
266 =cut
268 .sub 'collectgarbage' :anon
269     .param pmc opt :optional
270     .param pmc arg :optional
271     .param pmc extra :slurpy
272     .local pmc res
273     $S1 = lua_optstring(1, opt, 'collect')
274     lua_checkoption(1, $S1, 'stop restart collect count step setpause setstepmul')
275     $I2 = lua_optint(2, arg, 0)
276     $N0 = lua_gc($S1, $I2)
277     unless $S1 == 'step' goto L1
278     new res, 'LuaBoolean'
279     $I0 = $N0
280     set res, $I0
281     goto L2
282   L1:
283     new res, 'LuaNumber'
284     set res, $N0
285   L2:
286     .return (res)
287 .end
290 =item C<dofile (filename)>
292 Opens the named file and executes its contents as a Lua chunk. When called
293 without arguments, C<dofile> executes the contents of the standard input
294 (C<stdin>). Returns all values returned by the chunk. In case of errors,
295 C<dofile> propagates the error to its caller (that is, dofile does not run in
296 protected mode).
298 =cut
300 .sub 'dofile' :anon
301     .param pmc filename :optional
302     .param pmc extra :slurpy
303     $S1 = lua_optstring(1, filename, '')
304     ($P0, $S0) = lua_loadfile($S1)
305     if null $P0 goto L1
306     .return $P0()
307   L1:
308     lua_error($S0)
309 .end
312 =item C<error (message [, level])>
314 Terminates the last protected function called and returns C<message> as the
315 error message. Function C<error> never returns.
317 Usually, C<error> adds some information about the error position at the
318 beginning of the message. The C<level> argument specifies how to get the
319 error position. With level 1 (the default), the error position is where the
320 C<error> function was called. Level 2 points the error to where the function
321 that called C<error> was called; and so on. Passing a level 0 avoids the
322 addition of error position information to the message.
324 STILL INCOMPLETE.
326 =cut
328 .sub 'error' :anon
329     .param pmc message :optional
330     .param pmc level :optional
331     .param pmc extra :slurpy
332     $I2 = lua_optint(2, level, 1)
333     lua_checkany(1, message)
334     #
335     $S1 = message
336     lua_error($S1)
337 .end
340 =item C<getfenv (f)>
342 Returns the current environment in use by the function. C<f> can be a Lua
343 function or a number that specifies the function at that stack level: Level
344 1 is the function calling C<getfenv>. If the given function is not a Lua
345 function, or if C<f> is 0, C<getfenv> returns the global environment. The
346 default for C<f> is 1.
348 =cut
350 .sub 'getfenv' :anon
351     .param pmc f :optional
352     .param pmc extra :slurpy
353     .local pmc res
354     if null f goto L1
355     .const .LuaNumber zero = '0'
356     if f == zero goto L2
357   L1:
358     f = getfunc(f, 1)
359     $I0 = isa f, 'LuaClosure'
360     if $I0 goto L3
361   L2:
362     res = get_hll_global '_G'
363     .return (res)
364   L3:
365     .return lua_getfenv(f)
366 .end
368 .sub 'getfunc' :anon
369     .param pmc f
370     .param int opt
371     if null f goto L1
372     $I0 = isa f, 'LuaFunction'
373     if $I0 goto L2
374     $I0 = isa f, 'LuaClosure'
375     if $I0 goto L2
376   L1:
377     .local int level
378     unless opt goto L3
379     level = lua_optint(1, f, 1)
380     goto L4
381   L3:
382     level = lua_checknumber(1, f)
383   L4:
384     if level >= 0 goto L5
385     lua_argerror(1, "level must be non-negative")
386   L5:
387     $P0 = getinterp
388     inc level
389     push_eh _handler
390     f = $P0['sub'; level]
391   L2:
392     .return (f)
393   _handler:
394     lua_argerror(1, "invalid level")
395 .end
397 =item C<getmetatable (object)>
399 If C<object> does not have a metatable, returns B<nil>. Otherwise, if the
400 object's metatable has a C<"__metatable"> field, returns the associated value.
401 Otherwise, returns the metatable of the given object.
403 =cut
405 .sub 'getmetatable' :anon
406     .param pmc obj :optional
407     .param pmc extra :slurpy
408     .local pmc res
409     lua_checkany(1, obj)
410     res = obj.'get_metatable'()
411     if res goto L1
412     .return (res)
413   L1:
414     .local pmc prot
415     .const .LuaString mt = '__metatable'
416     prot = res.'rawget'(mt)
417     unless prot goto L2
418     .return (prot)
419   L2:
420     .return (res)
421 .end
424 =item C<ipairs (t)>
426 Returns three values: an iterator function, the table C<t>, and 0, so that
427 the construction
429     for i,v in ipairs(t) do ... end
431 will iterate over the pairs (C<1,t[1]>), (C<2,t[2]>), ..., up to the first
432 integer key with a nil value in the table.
434 See C<next> for the caveats of modifying the table during its traversal.
436 =cut
438 .sub 'ipairs' :anon
439     .param pmc t :optional
440     .param pmc i :optional
441     .param pmc extra :slurpy
442     lua_checktype(1, t, 'table')
443     unless null i goto L1
444     .local pmc _G
445     _G = get_hll_global '_G'
446     .const .LuaString key_ipairs = 'ipairs'
447     .local pmc ipairs
448     ipairs = _G.'rawget'(key_ipairs)
449     .local pmc zero
450     new zero, 'LuaNumber'
451     set zero, 0.0
452     .return (ipairs, t, zero)
453   L1:
454     $P2 = lua_checknumber(2, i)
455     $P0 = clone $P2
456     inc $P0
457     .local pmc res
458     res = t.'rawget'($P0)
459     unless res goto L2
460     .return ($P0, res)
461   L2:
462     .return ()
463 .end
466 =item C<load (func [, chunkname])>
468 Loads a chunk using function C<func> to get its pieces. Each call to C<func>
469 must return a string that concatenates with previous results. A return of
470 B<nil> (or no value) signals the end of the chunk.
472 If there are no errors, returns the compiled chunk as a function; otherwise,
473 returns B<nil> plus the error message. The environment of the returned
474 function is the global environment.
476 C<chunkname> is used as the chunk name for error messages and debug
477 information.
479 NOT YET IMPLEMENTED.
481 =cut
483 .sub 'load' :anon
484     .param pmc func :optional
485     .param pmc chunkname :optional
486     .param pmc extra :slurpy
487     lua_checktype(1, func, 'function')
488     $S2 = lua_optstring(2, chunkname, '=(load)')
489     not_implemented()
490     .return load_aux($P0, $S0)
491 .end
493 .sub 'load_aux' :anon
494     .param pmc func
495     .param string error
496     if null func goto L1
497     .return (func)
498   L1:
499     .local pmc msg
500     new msg, 'LuaString'
501     set msg, error
502     new func, 'LuaNil'
503     .return (func, msg)
504 .end
507 =item C<loadfile ([filename])>
509 Similar to C<load>, but gets the chunk from file C<filename> or from the
510 standard input, if no file name is given.
512 =cut
514 .sub 'loadfile' :anon
515     .param pmc filename :optional
516     .param pmc extra :slurpy
517     $S1 = lua_optstring(1, filename, '')
518     ($P0, $S0) = lua_loadfile($S1)
519     .return load_aux($P0, $S0)
520 .end
523 =item C<loadstring (string [, chunkname])>
525 Similar to C<load>, but gets the chunk from the given string.
527 To load and run a given string, use the idiom
529     assert(loadstring(s))()
531 =cut
533 .sub 'loadstring' :anon
534     .param pmc s :optional
535     .param pmc chunkname :optional
536     .param pmc extra :slurpy
537     $S1 = lua_checkstring(1, s)
538     $S2 = lua_optstring(2, chunkname, $S1)
539     ($P0, $S0) = lua_loadbuffer($S1, $S2)
540     .return load_aux($P0, $S0)
541 .end
544 =item C<next (table [, index])>
546 Allows a program to traverse all fields of a table. Its first argument is a
547 table and its second argument is an index in this table. C<next> returns the
548 next index of the table and its associated value. When called
549 with B<nil> as its second argument, C<next> returns an initial index
550 and its associated value. When called with the last index, or with
551 B<nil> in an empty table, C<next> returns B<nil>. If the second argument is
552 absent, then it is interpreted as B<nil>. In particular, you can use
553 C<next(t)> to check whether a table is empty.
555 Lua has no declaration of fields. There is no difference between a field
556 not present in a table or a field with value B<nil>. Therefore, C<next> only
557 considers fields with non-B<nil> values. The order in which the indices are
558 enumerated is not specified, I<even for numeric indices>. (To traverse a table
559 in numeric order, use a numerical for or the C<ipairs> function.)
561 The behavior of C<next> is I<undefined> if, during the traversal, you assign
562 any value to a non-existent field in the table. You may however modify
563 existing fields. In particular, you may clear existing fields.
565 =cut
567 .sub 'next' :anon
568     .param pmc table :optional
569     .param pmc idx :optional
570     .param pmc extra :slurpy
571     lua_checktype(1, table, 'table')
572     $P0 = table.'next'(idx)
573     unless $P0 goto L1
574     .return ($P0 :flat)
575   L1:
576     .return ($P0)   # nil
577 .end
580 =item C<pairs (t)>
582 Returns three values: the C<next> function, the table C<t>, and B<nil>, so
583 that the construction
585     for k,v in pairs(t) do ... end
587 will iterate over all key--value pairs of table C<t>.
589 See C<next> for the caveats of modifying the table during its traversal.
591 =cut
593 .sub 'pairs' :anon
594     .param pmc t :optional
595     .param pmc extra :slurpy
596     lua_checktype(1, t, 'table')
597     .local pmc _G
598     _G = get_hll_global '_G'
599     .const .LuaString key_next = 'next'
600     .local pmc next
601     next = _G.'rawget'(key_next)
602     .local pmc nil
603     new nil, 'LuaNil'
604     .return (next, t, nil)
605 .end
608 =item C<pcall (f, arg1, arg2, ...)>
610 Calls function C<f> with the given arguments in protected mode. This means
611 that any error inside C<f> is not propagated; instead, C<pcall> catches the
612 error and returns a status code. Its first result is the status code (a
613 boolean), which is B<true> if the call succeeds without errors. In such case,
614 C<pcall> also returns all results from the call, after this first result.
615 In case of any error, C<pcall> returns B<false> plus the error message.
617 =cut
619 .sub 'pcall' :anon
620     .param pmc f :optional
621     .param pmc argv :slurpy
622     .local pmc res
623     .local pmc status
624     new status, 'LuaBoolean'
625     lua_checkany(1, f)
626     push_eh _handler
627     (res :slurpy) = f(argv :flat)
628     set status, 1
629     .return (status, res :flat)
630   _handler:
631     .local pmc e
632     .local string s
633     .local pmc msg
634     .get_results (e, s)
635     set status, 0
636     new msg, 'LuaString'
637     set msg, s
638     .return (status, msg)
639 .end
642 =item C<print (e1, e2, ...)>
644 Receives any number of arguments, and prints their values to C<stdout>, using
645 the C<tostring> function to convert them to strings. C<print> is not intended
646 for formatted output, but only as a quick way to show a value, typically for
647 debugging. For formatted output, use C<string.format>.
649 =cut
651 .sub 'print' :anon
652     .param pmc argv :slurpy
653     .local int argc
654     .local int i
655     argc = argv
656     i = 0
657   L1:
658     if i >= argc goto L3
659     if i == 0 goto L2
660     print "\t"
661   L2:
662     $P0 = argv[i]
663     $P0 = $P0.'tostring'()
664     print $P0
665     inc i
666     goto L1
667   L3:
668     print "\n"
669 .end
672 =item C<rawequal (v1, v2)>
674 Checks whether C<v1> is equal to C<v2>, without invoking any metamethod.
675 Returns a boolean.
677 =cut
679 .sub 'rawequal' :anon
680     .param pmc v1 :optional
681     .param pmc v2 :optional
682     .param pmc extra :slurpy
683     .local pmc res
684     lua_checkany(1, v1)
685     lua_checkany(2, v2)
686     res = v1.'rawequal'(v2)
687     .return (res)
688 .end
691 =item C<rawget (table, index)>
693 Gets the real value of C<table[index]>, without invoking any metamethod.
694 C<table> must be a table; C<index> is any value different from B<nil>.
696 =cut
698 .sub 'rawget' :anon
699     .param pmc table :optional
700     .param pmc idx :optional
701     .param pmc extra :slurpy
702     .local pmc res
703     lua_checktype(1, table, 'table')
704     lua_checkany(2, idx)
705     res = table.'rawget'(idx)
706     .return (res)
707 .end
710 =item C<rawset (table, index, value)>
712 Sets the real value of C<table[index]> to value, without invoking any
713 metamethod. C<table> must be a table, C<index> is any value different from
714 B<nil>, and C<value> is any Lua value.
716 This function returns C<table>.
718 =cut
720 .sub 'rawset' :anon
721     .param pmc table :optional
722     .param pmc idx :optional
723     .param pmc value :optional
724     .param pmc extra :slurpy
725     lua_checktype(1, table, 'table')
726     lua_checkany(2, idx)
727     lua_checkany(3, value)
728     table.'rawset'(idx, value)
729     .return (table)
730 .end
733 =item C<select (index, ...)>
735 If C<index> is a number, returns all arguments after argument number C<index>.
736 Otherwise, C<index> must be the string C<"#">, and C<select> returns the
737 total number of extra arguments it received.
739 =cut
741 .sub 'select' :anon
742     .param pmc idx :optional
743     .param pmc argv :slurpy
744     .local pmc res
745     unless idx goto L1
746     $I0 = isa idx, 'LuaString'
747     unless $I0 goto L1
748     $S0 = idx
749     unless $S0 == '#' goto L1
750     $I1 = argv
751     new res, 'LuaNumber'
752     res = $I1
753     .return (res)
754   L1:
755     .local int i
756     i = lua_checknumber(1, idx)
757     .local int n
758     n = argv
759     inc n
760     unless i < 0 goto L2
761     i = n + i
762     goto L3
763   L2:
764     unless i > n goto L3
765     i = n
766   L3:
767     if 1 <= i goto L4
768     lua_argerror(1, "index out of range")
769   L4:
770     $I0 = n - i
771     new res, 'FixedPMCArray'
772     set res, $I0
773     $I1 = 0
774     dec i
775   L5:
776     unless $I1 < $I0 goto L6
777     $P0 = argv[i]
778     res[$I1] = $P0
779     inc i
780     inc $I1
781     goto L5
782   L6:
783     .return (res :flat)
784 .end
787 =item C<setfenv (f, table)>
789 Sets the environment to be used by the given function. C<f> can be a Lua
790 function or a number that specifies the function at that stack level: Level
791 1 is the function calling C<setfenv>. C<setfenv> returns the given function.
793 As a special case, when C<f> is 0 C<setfenv> changes the environment of the
794 running thread. In this case, C<setfenv> returns no values.
796 STILL INCOMPLETE.
798 =cut
800 .sub 'setfenv' :anon
801     .param pmc f :optional
802     .param pmc table :optional
803     .param pmc extra :slurpy
804     .const .LuaNumber zero = '0'
805     lua_checktype(2, table, 'table')
806     unless f == zero goto L1
807     # change environment of current thread
808     not_implemented()
809     .return ()
810   L1:
811     f = getfunc(f, 0)
812     $I0 = isa f, 'LuaFunction'
813     if $I0 goto L2
814     $I0 = lua_setfenv(f, table)
815     unless $I0 goto L2
816     .return (f)
817   L2:
818     lua_error("'setfenv' cannot change environment of given object")
819 .end
822 =item C<setmetatable (table, metatable)>
824 Sets the metatable for the given table. (You cannot change the metatable of
825 other types from Lua.) If metatable is B<nil>, removes the metatable of the
826 given table. If the original metatable has a C<"__metatable"> field, raises
827 an error.
829 This function returns C<table>.
831 =cut
833 .sub 'setmetatable' :anon
834     .param pmc table :optional
835     .param pmc metatable :optional
836     .param pmc extra :slurpy
837     lua_checktype(1, table, 'table')
838     if null metatable goto L1
839     $I0 = isa metatable, 'LuaNil'
840     if $I0 goto L2
841     $I0 = isa metatable, 'LuaTable'
842     if $I0 goto L2
843   L1:
844     lua_argerror(2, "nil or table expected")
845   L2:
846     .local pmc meta
847     meta = table.'get_metatable'()
848     unless meta goto L3
849     .local pmc prot
850     .const .LuaString mt = '__metatable'
851     prot = meta.'rawget'(mt)
852     unless prot goto L3
853     lua_error("cannot change a protected metatable")
854   L3:
855     table.'set_metatable'(metatable)
856     .return (table)
857 .end
860 =item C<tonumber (e [, base])>
862 Tries to convert its argument to a number. If the argument is already a number
863 or a string convertible to a number, then C<tonumber> returns that number;
864 otherwise, it returns B<nil>.
866 An optional argument specifies the base to interpret the numeral. The base may
867 be any integer between 2 and 36, inclusive. In bases above 10, the letter ‘A’
868 (in either upper or lower case) represents 10, ‘B’ represents 11, and so forth,
869 with ‘Z’ representing 35. In base 10 (the default), the number may have a
870 decimal part, as well as an optional exponent part. In other bases, only
871 unsigned integers are accepted.
873 =cut
875 .sub 'tonumber' :anon
876     .param pmc e :optional
877     .param pmc base :optional
878     .param pmc extra :slurpy
879     .local pmc res
880     lua_checkany(1, e)
881     $I2 = lua_optint(2, base, 10)
882     unless $I2 == 10 goto L1
883     res = e.'tonumber'()
884     .return (res)
885   L1:
886     $P0 = lua_checkstring(1, e)
887     unless 2 <= $I2 goto L2
888     unless $I2 <= 36 goto L2
889     goto L3
890   L2:
891     lua_argerror(2, "base out of range")
892   L3:
893     res = $P0.'tobase'($I2)
894     .return (res)
895 .end
898 =item C<tostring (e)>
900 Receives an argument of any type and converts it to a string in a reasonable
901 format. For complete control of how numbers are converted, use C<format>.
903 If the metatable of e has a C<"__tostring"> field, C<tostring> calls the
904 corresponding value with C<e> as argument, and uses the result of the call
905 as its result.
907 =cut
909 .sub 'tostring' :anon
910     .param pmc e :optional
911     .param pmc extra :slurpy
912     .local pmc res
913     lua_checkany(1, e)
914     res = e.'tostring'()
915     .return (res)
916 .end
919 =item C<type (v)>
921 Returns the type of its only argument, coded as a string. The possible results
922 of this function are C<"nil"> (a string, not the value B<nil>), C<"number">,
923 C<"string">, C<"boolean">, C<"table">, C<"function">, C<"thread">, and
924 C<"userdata">.
926 =cut
928 .sub 'type' :anon
929     .param pmc v :optional
930     .param pmc extra :slurpy
931     .local pmc res
932     lua_checkany(1, v)
933     $S0 = typeof v
934     new res, 'LuaString'
935     set res, $S0
936     .return (res)
937 .end
940 =item C<unpack (list [, i [, j]])>
942 Returns the elements from the given table. This function is equivalent to
944     return list[i], list[i+1], ..., list[j]
946 except that the above code can be written only for a fixed number of elements.
947 By default, C<i> is 1 and C<j> is the length of the list, as defined by the
948 length operator.
950 =cut
952 .sub 'unpack' :anon
953     .param pmc list :optional
954     .param pmc i :optional
955     .param pmc j :optional
956     .param pmc extra :slurpy
957     .local pmc res
958     .local pmc index_
959     .local int idx
960     .local int e
961     .local int n
962     lua_checktype(1, list, 'table')
963     $I0 = list.'len'()
964     $I2 = lua_optint(2, i, 1)
965     e = lua_optint(3, j, $I0)
966     n = e - $I2
967     inc n
968     new res, 'FixedPMCArray'
969     set res, n
970     new index_, 'LuaNumber'
971     set index_, $I2
972     idx = 0
973   L1:
974     unless idx < n goto L2
975     $P0 = list.'rawget'(index_)
976     res[idx] = $P0
977     inc index_
978     inc idx
979     goto L1
980   L2:
981     .return (res :flat)
982 .end
985 =item C<xpcall (f, err)>
987 This function is similar to C<pcall>, except that you can set a new error
988 handler.
990 C<xpcall> calls function C<f> in protected mode, using C<err> as the error
991 handler. Any error inside C<f> is not propagated; instead, C<xpcall> catches
992 the error, calls the C<err> function with the original error object, and
993 returns a status code. Its first result is the status code (a boolean), which
994 is true if the call succeeds without errors. In this case, C<xpcall> also
995 returns all results from the call, after this first result. In case of any
996 error, C<xpcall> returns false plus the result from C<err>.
998 =cut
1000 .sub 'xpcall' :anon
1001     .param pmc f :optional
1002     .param pmc err_ :optional
1003     .param pmc extra :slurpy
1004     .local pmc res
1005     .local pmc status
1006     new status, 'LuaBoolean'
1007     lua_checkany(1, f)
1008     lua_checkany(2, err_)
1009     push_eh _handler
1010     (res :slurpy) = f()
1011     set status, 1
1012     .return (status, res :flat)
1013   _handler:
1014     set status, 0
1015     $I0 = isa err_, 'LuaFunction'
1016     if $I0 goto L1
1017     $I0 = isa err_, 'LuaClosure'
1018     unless $I0 goto L2
1019   L1:
1020     (res :slurpy) = err_()
1021     .return (status, res :flat)
1022   L2:
1023     .return (status)
1024 .end
1026 =back
1028 =head1 AUTHORS
1030 Francois Perrad.
1032 =cut
1035 # Local Variables:
1036 #   mode: pir
1037 #   fill-column: 100
1038 # End:
1039 # vim: expandtab shiftwidth=4 ft=pir: