diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-29 16:05:11 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-29 16:05:11 -0300 |
| commit | 4135f4f5862b57d9dbf3e9b7c6507f5c86516327 (patch) | |
| tree | 09854bc05e179296b709ba1cffae1d2507d00a9e | |
| parent | 808525e4173d9f8ec1d1f2665cb4d551d0f4075b (diff) | |
| download | lua-4135f4f5862b57d9dbf3e9b7c6507f5c86516327.tar.gz lua-4135f4f5862b57d9dbf3e9b7c6507f5c86516327.tar.bz2 lua-4135f4f5862b57d9dbf3e9b7c6507f5c86516327.zip | |
new API does not need tests that complex
| -rw-r--r-- | ltests.c | 217 |
1 files changed, 101 insertions, 116 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 1.35 2000/08/28 17:57:04 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 1.36 2000/08/29 14:57:10 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -239,6 +239,89 @@ static int string_query (lua_State *L) { | |||
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | 241 | ||
| 242 | static int tref (lua_State *L) { | ||
| 243 | luaL_checktype(L, 1, "any"); | ||
| 244 | lua_pushobject(L, 1); | ||
| 245 | lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1))); | ||
| 246 | return 1; | ||
| 247 | } | ||
| 248 | |||
| 249 | static int getref (lua_State *L) { | ||
| 250 | if (lua_getref(L, luaL_check_int(L, 1))) | ||
| 251 | return 1; | ||
| 252 | else | ||
| 253 | return 0; | ||
| 254 | } | ||
| 255 | |||
| 256 | static int unref (lua_State *L) { | ||
| 257 | lua_unref(L, luaL_check_int(L, 1)); | ||
| 258 | return 0; | ||
| 259 | } | ||
| 260 | |||
| 261 | static int newuserdata (lua_State *L) { | ||
| 262 | lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2)); | ||
| 263 | return 1; | ||
| 264 | } | ||
| 265 | |||
| 266 | static int udataval (lua_State *L) { | ||
| 267 | luaL_checktype(L, 1, "userdata"); | ||
| 268 | lua_pushnumber(L, (int)lua_touserdata(L, 1)); | ||
| 269 | return 1; | ||
| 270 | } | ||
| 271 | |||
| 272 | static int newstate (lua_State *L) { | ||
| 273 | lua_State *L1 = lua_newstate(luaL_check_int(L, 1), luaL_check_int(L, 2)); | ||
| 274 | if (L1) | ||
| 275 | lua_pushuserdata(L, L1); | ||
| 276 | else | ||
| 277 | lua_pushnil(L); | ||
| 278 | return 1; | ||
| 279 | } | ||
| 280 | |||
| 281 | static int closestate (lua_State *L) { | ||
| 282 | luaL_checktype(L, 1, "userdata"); | ||
| 283 | lua_close((lua_State *)lua_touserdata(L, 1)); | ||
| 284 | return 0; | ||
| 285 | } | ||
| 286 | |||
| 287 | static int doremote (lua_State *L) { | ||
| 288 | lua_State *L1; | ||
| 289 | const char *code = luaL_check_string(L, 2); | ||
| 290 | int status; | ||
| 291 | luaL_checktype(L, 1, "userdata"); | ||
| 292 | L1 = (lua_State *)lua_touserdata(L, 1); | ||
| 293 | status = lua_dostring(L1, code); | ||
| 294 | if (status != 0) { | ||
| 295 | lua_pushnil(L); | ||
| 296 | lua_pushnumber(L, status); | ||
| 297 | return 2; | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | int i = 0; | ||
| 301 | while (!lua_isnull(L1, ++i)) | ||
| 302 | lua_pushstring(L, lua_tostring(L1, i)); | ||
| 303 | return i-1; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | static int settagmethod (lua_State *L) { | ||
| 308 | luaL_checktype(L, 3, "any"); | ||
| 309 | lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2)); | ||
| 310 | return 1; | ||
| 311 | } | ||
| 312 | |||
| 313 | static int pushbool (lua_State *L, int b) { | ||
| 314 | if (b) lua_pushnumber(L, 1); | ||
| 315 | else lua_pushnil(L); | ||
| 316 | return 1; | ||
| 317 | } | ||
| 318 | |||
| 319 | static int equal (lua_State *L) { | ||
| 320 | return pushbool(L, lua_equal(L, 1, 2)); | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | |||
| 242 | /* | 325 | /* |
| 243 | ** {====================================================== | 326 | ** {====================================================== |
| 244 | ** function to test the API with C. It interprets a kind of "assembler" | 327 | ** function to test the API with C. It interprets a kind of "assembler" |
| @@ -321,34 +404,9 @@ static int testC (lua_State *L) { | |||
| 321 | else if EQ("pushnum") { | 404 | else if EQ("pushnum") { |
| 322 | lua_pushnumber(L, getnum); | 405 | lua_pushnumber(L, getnum); |
| 323 | } | 406 | } |
| 324 | else if EQ("newtable") { | ||
| 325 | lua_newtable(L); | ||
| 326 | } | ||
| 327 | else if EQ("pushobject") { | 407 | else if EQ("pushobject") { |
| 328 | lua_pushobject(L, getnum); | 408 | lua_pushobject(L, getnum); |
| 329 | } | 409 | } |
| 330 | else if EQ("getglobal") { | ||
| 331 | lua_getglobal(L, getname); | ||
| 332 | } | ||
| 333 | else if EQ("getglobals") { | ||
| 334 | lua_getglobals(L); | ||
| 335 | } | ||
| 336 | else if EQ("ref") { | ||
| 337 | reg[getreg] = lua_ref(L, 0); | ||
| 338 | } | ||
| 339 | else if EQ("reflock") { | ||
| 340 | reg[getreg] = lua_ref(L, 1); | ||
| 341 | } | ||
| 342 | else if EQ("getref") { | ||
| 343 | int n = getreg; | ||
| 344 | reg[n] = lua_getref(L, getnum); | ||
| 345 | } | ||
| 346 | else if EQ("unref") { | ||
| 347 | lua_unref(L, getnum); | ||
| 348 | } | ||
| 349 | else if EQ("setglobal") { | ||
| 350 | lua_setglobal(L, getname); | ||
| 351 | } | ||
| 352 | else if EQ("pushstring") { | 410 | else if EQ("pushstring") { |
| 353 | lua_pushstring(L, getname); | 411 | lua_pushstring(L, getname); |
| 354 | } | 412 | } |
| @@ -357,98 +415,9 @@ static int testC (lua_State *L) { | |||
| 357 | int nres = getnum; | 415 | int nres = getnum; |
| 358 | if (lua_call(L, narg, nres)) lua_error(L, NULL); | 416 | if (lua_call(L, narg, nres)) lua_error(L, NULL); |
| 359 | } | 417 | } |
| 360 | else if EQ("gettable") { | ||
| 361 | lua_gettable(L); | ||
| 362 | } | ||
| 363 | else if EQ("rawget") { | ||
| 364 | lua_rawget(L); | ||
| 365 | } | ||
| 366 | else if EQ("settable") { | ||
| 367 | lua_settable(L); | ||
| 368 | } | ||
| 369 | else if EQ("rawset") { | ||
| 370 | lua_rawset(L); | ||
| 371 | } | ||
| 372 | else if EQ("tag") { | ||
| 373 | int n = getreg; | ||
| 374 | reg[n] = lua_tag(L, getnum); | ||
| 375 | } | ||
| 376 | else if EQ("type") { | 418 | else if EQ("type") { |
| 377 | lua_pushstring(L, lua_type(L, getnum)); | 419 | lua_pushstring(L, lua_type(L, getnum)); |
| 378 | } | 420 | } |
| 379 | else if EQ("isnil") { | ||
| 380 | lua_pushnumber(L, lua_isnil(L, getnum)); | ||
| 381 | } | ||
| 382 | else if EQ("isnull") { | ||
| 383 | lua_pushnumber(L, lua_isnull(L, getnum)); | ||
| 384 | } | ||
| 385 | else if EQ("isnumber") { | ||
| 386 | lua_pushnumber(L, lua_isnumber(L, getnum)); | ||
| 387 | } | ||
| 388 | else if EQ("isstring") { | ||
| 389 | lua_pushnumber(L, lua_isstring(L, getnum)); | ||
| 390 | } | ||
| 391 | else if EQ("istable") { | ||
| 392 | lua_pushnumber(L, lua_istable(L, getnum)); | ||
| 393 | } | ||
| 394 | else if EQ("isfunction") { | ||
| 395 | lua_pushnumber(L, lua_isfunction(L, getnum)); | ||
| 396 | } | ||
| 397 | else if EQ("iscfunction") { | ||
| 398 | lua_pushnumber(L, lua_iscfunction(L, getnum)); | ||
| 399 | } | ||
| 400 | else if EQ("isuserdata") { | ||
| 401 | lua_pushnumber(L, lua_isuserdata(L, getnum)); | ||
| 402 | } | ||
| 403 | else if EQ("equal") { | ||
| 404 | int n1 = getreg; | ||
| 405 | int n2 = getnum; | ||
| 406 | int n3 = getnum; | ||
| 407 | reg[n1] = lua_equal(L, n2, n3); | ||
| 408 | } | ||
| 409 | else if EQ("pushusertag") { | ||
| 410 | int val = getnum; | ||
| 411 | int tag = getnum; | ||
| 412 | lua_pushusertag(L, (void *)val, tag); | ||
| 413 | } | ||
| 414 | else if EQ("udataval") { | ||
| 415 | int n = getreg; | ||
| 416 | reg[n] = (int)lua_touserdata(L, getnum); | ||
| 417 | } | ||
| 418 | else if EQ("settagmethod") { | ||
| 419 | int n = getnum; | ||
| 420 | lua_settagmethod(L, n, getname); | ||
| 421 | } | ||
| 422 | else if EQ("newstate") { | ||
| 423 | int stacksize = getnum; | ||
| 424 | lua_State *L1 = lua_newstate(stacksize, getnum); | ||
| 425 | if (L1) | ||
| 426 | lua_pushuserdata(L, L1); | ||
| 427 | else | ||
| 428 | lua_pushnil(L); | ||
| 429 | } | ||
| 430 | else if EQ("closestate") { | ||
| 431 | (lua_close)((lua_State *)lua_touserdata(L, getnum)); | ||
| 432 | } | ||
| 433 | else if EQ("doremote") { | ||
| 434 | int ol1 = getnum; | ||
| 435 | int str = getnum; | ||
| 436 | lua_State *L1; | ||
| 437 | int status; | ||
| 438 | if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str)) | ||
| 439 | lua_error(L, "bad arguments for `doremote'"); | ||
| 440 | L1 = (lua_State *)lua_touserdata(L, ol1); | ||
| 441 | status = lua_dostring(L1, lua_tostring(L, str)); | ||
| 442 | if (status != 0) { | ||
| 443 | lua_pushnil(L); | ||
| 444 | lua_pushnumber(L, status); | ||
| 445 | } | ||
| 446 | else { | ||
| 447 | int i = 0; | ||
| 448 | while (!lua_isnull(L1, ++i)) | ||
| 449 | lua_pushstring(L, lua_tostring(L1, i)); | ||
| 450 | } | ||
| 451 | } | ||
| 452 | else luaL_verror(L, "unknown instruction %.30s", buff); | 421 | else luaL_verror(L, "unknown instruction %.30s", buff); |
| 453 | } | 422 | } |
| 454 | return 0; | 423 | return 0; |
| @@ -467,12 +436,28 @@ static const struct luaL_reg tests_funcs[] = { | |||
| 467 | {"querystr", string_query}, | 436 | {"querystr", string_query}, |
| 468 | {"querytab", table_query}, | 437 | {"querytab", table_query}, |
| 469 | {"testC", testC}, | 438 | {"testC", testC}, |
| 439 | {"ref", tref}, | ||
| 440 | {"getref", getref}, | ||
| 441 | {"unref", unref}, | ||
| 442 | {"newuserdata", newuserdata}, | ||
| 443 | {"udataval", udataval}, | ||
| 444 | {"newstate", newstate}, | ||
| 445 | {"closestate", closestate}, | ||
| 446 | {"doremote", doremote}, | ||
| 447 | {"settagmethod", settagmethod}, | ||
| 448 | {"equal", equal}, | ||
| 470 | {"totalmem", mem_query} | 449 | {"totalmem", mem_query} |
| 471 | }; | 450 | }; |
| 472 | 451 | ||
| 473 | 452 | ||
| 474 | void luaB_opentests (lua_State *L) { | 453 | void luaB_opentests (lua_State *L) { |
| 475 | luaL_openl(L, tests_funcs); | 454 | lua_newtable(L); |
| 455 | lua_getglobals(L); | ||
| 456 | lua_pushobject(L, -2); | ||
| 457 | lua_setglobals(L); | ||
| 458 | luaL_openl(L, tests_funcs); /* open functions inside new table */ | ||
| 459 | lua_setglobals(L); /* restore old table of globals */ | ||
| 460 | lua_setglobal(L, "T"); /* set new table as global T */ | ||
| 476 | } | 461 | } |
| 477 | 462 | ||
| 478 | #endif | 463 | #endif |
