aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-04-30 18:13:55 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-04-30 18:13:55 -0300
commit3ec9ee0d0f81fd0aabaef1303f971b2f7ee12315 (patch)
tree07c3b5bdd856340429a3567212fc86fe80fdd005
parent21c9ebf4a9891786d5683537868cc348340ca89d (diff)
downloadlua-3ec9ee0d0f81fd0aabaef1303f971b2f7ee12315.tar.gz
lua-3ec9ee0d0f81fd0aabaef1303f971b2f7ee12315.tar.bz2
lua-3ec9ee0d0f81fd0aabaef1303f971b2f7ee12315.zip
new function "luaI_openlib" to help open libs.
-rw-r--r--iolib.c44
-rw-r--r--lualib.h11
-rw-r--r--mathlib.c51
-rw-r--r--strlib.c28
4 files changed, 79 insertions, 55 deletions
diff --git a/iolib.c b/iolib.c
index cdbe3447..a5c26a0e 100644
--- a/iolib.c
+++ b/iolib.c
@@ -3,7 +3,7 @@
3** Input/output library to LUA 3** Input/output library to LUA
4*/ 4*/
5 5
6char *rcs_iolib="$Id: iolib.c,v 1.41 1996/04/22 19:28:37 roberto Exp roberto $"; 6char *rcs_iolib="$Id: iolib.c,v 1.42 1996/04/23 12:43:07 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <ctype.h> 9#include <ctype.h>
@@ -593,27 +593,27 @@ static void errorfb (void)
593} 593}
594 594
595 595
596/* 596static struct lua_reg iolib[] = {
597** Open io library 597{"readfrom", io_readfrom},
598*/ 598{"writeto", io_writeto},
599{"appendto", io_appendto},
600{"read", io_read},
601{"readuntil",io_readuntil},
602{"write", io_write},
603{"execute", io_execute},
604{"remove", io_remove},
605{"rename", io_rename},
606{"tmpname", io_tmpname},
607{"ioerror", io_errorno},
608{"getenv", io_getenv},
609{"date", io_date},
610{"exit", io_exit},
611{"debug", io_debug},
612{"print_stack", errorfb}
613};
614
599void iolib_open (void) 615void iolib_open (void)
600{ 616{
601 lua_register ("readfrom", io_readfrom); 617 luaI_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
602 lua_register ("writeto", io_writeto); 618 lua_setfallback("error", errorfb);
603 lua_register ("appendto", io_appendto);
604 lua_register ("read", io_read);
605 lua_register ("readuntil",io_readuntil);
606 lua_register ("write", io_write);
607 lua_register ("execute", io_execute);
608 lua_register ("remove", io_remove);
609 lua_register ("rename", io_rename);
610 lua_register ("tmpname", io_tmpname);
611 lua_register ("ioerror", io_errorno);
612 lua_register ("getenv", io_getenv);
613 lua_register ("date", io_date);
614 lua_register ("exit", io_exit);
615 lua_register ("debug", io_debug);
616 lua_register ("print_stack", errorfb);
617 lua_setfallback("error", errorfb);
618} 619}
619
diff --git a/lualib.h b/lualib.h
index d7403b5f..567b0269 100644
--- a/lualib.h
+++ b/lualib.h
@@ -2,18 +2,27 @@
2** Libraries to be used in LUA programs 2** Libraries to be used in LUA programs
3** Grupo de Tecnologia em Computacao Grafica 3** Grupo de Tecnologia em Computacao Grafica
4** TeCGraf - PUC-Rio 4** TeCGraf - PUC-Rio
5** $Id: lualib.h,v 1.6 1996/02/09 19:00:23 roberto Exp roberto $ 5** $Id: lualib.h,v 1.7 1996/03/14 15:53:09 roberto Exp roberto $
6*/ 6*/
7 7
8#ifndef lualib_h 8#ifndef lualib_h
9#define lualib_h 9#define lualib_h
10 10
11#include "lua.h"
12
11void iolib_open (void); 13void iolib_open (void);
12void strlib_open (void); 14void strlib_open (void);
13void mathlib_open (void); 15void mathlib_open (void);
14 16
15 17
16/* auxiliar functions (private) */ 18/* auxiliar functions (private) */
19
20struct lua_reg {
21 char *name;
22 lua_CFunction func;
23};
24
25void luaI_openlib (struct lua_reg *l, int n);
17void lua_arg_error(char *funcname); 26void lua_arg_error(char *funcname);
18char *lua_check_string (int numArg, char *funcname); 27char *lua_check_string (int numArg, char *funcname);
19double lua_check_number (int numArg, char *funcname); 28double lua_check_number (int numArg, char *funcname);
diff --git a/mathlib.c b/mathlib.c
index 5e02908e..f01e149d 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,7 +3,7 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.15 1996/04/22 18:00:37 roberto Exp roberto $"; 6char *rcs_mathlib="$Id: mathlib.c,v 1.16 1996/04/25 14:10:00 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <math.h> 9#include <math.h>
@@ -195,33 +195,36 @@ static void math_randomseed (void)
195} 195}
196 196
197 197
198static struct lua_reg mathlib[] = {
199{"abs", math_abs},
200{"sin", math_sin},
201{"cos", math_cos},
202{"tan", math_tan},
203{"asin", math_asin},
204{"acos", math_acos},
205{"atan", math_atan},
206{"atan2", math_atan2},
207{"ceil", math_ceil},
208{"floor", math_floor},
209{"mod", math_mod},
210{"sqrt", math_sqrt},
211{"min", math_min},
212{"max", math_max},
213{"log", math_log},
214{"log10", math_log10},
215{"exp", math_exp},
216{"deg", math_deg},
217{"rad", math_rad},
218{"random", math_random},
219{"randomseed", math_randomseed}
220};
198 221
199/* 222/*
200** Open math library 223** Open math library
201*/ 224*/
202void mathlib_open (void) 225void mathlib_open (void)
203{ 226{
204 lua_register ("abs", math_abs); 227 luaI_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
205 lua_register ("sin", math_sin); 228 old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1);
206 lua_register ("cos", math_cos);
207 lua_register ("tan", math_tan);
208 lua_register ("asin", math_asin);
209 lua_register ("acos", math_acos);
210 lua_register ("atan", math_atan);
211 lua_register ("atan2", math_atan2);
212 lua_register ("ceil", math_ceil);
213 lua_register ("floor", math_floor);
214 lua_register ("mod", math_mod);
215 lua_register ("sqrt", math_sqrt);
216 lua_register ("min", math_min);
217 lua_register ("max", math_max);
218 lua_register ("log", math_log);
219 lua_register ("log10", math_log10);
220 lua_register ("exp", math_exp);
221 lua_register ("deg", math_deg);
222 lua_register ("rad", math_rad);
223 lua_register ("random", math_random);
224 lua_register ("randomseed", math_randomseed);
225
226 old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1);
227} 229}
230
diff --git a/strlib.c b/strlib.c
index 9d084b36..a3467050 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,7 +3,7 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.21 1996/03/21 22:18:08 roberto Exp roberto $"; 6char *rcs_strlib="$Id: strlib.c,v 1.22 1996/03/22 17:57:24 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -249,16 +249,28 @@ static void str_format (void)
249} 249}
250 250
251 251
252void luaI_openlib (struct lua_reg *l, int n)
253{
254 int i;
255 for (i=0; i<n; i++)
256 lua_register(l[i].name, l[i].func);
257}
258
259static struct lua_reg strlib[] = {
260{"strfind", str_find},
261{"strlen", str_len},
262{"strsub", str_sub},
263{"strlower", str_lower},
264{"strupper", str_upper},
265{"ascii", str_ascii},
266{"format", str_format}
267};
268
269
252/* 270/*
253** Open string library 271** Open string library
254*/ 272*/
255void strlib_open (void) 273void strlib_open (void)
256{ 274{
257 lua_register ("strfind", str_find); 275 luaI_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));
258 lua_register ("strlen", str_len);
259 lua_register ("strsub", str_sub);
260 lua_register ("strlower", str_lower);
261 lua_register ("strupper", str_upper);
262 lua_register ("ascii", str_ascii);
263 lua_register ("format", str_format);
264} 276}