diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-02-11 09:40:01 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-02-11 09:40:01 -0200 |
| commit | 205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3 (patch) | |
| tree | 1e2683e673e5fbf2230343df4faac3b252eee38c /opcode.c | |
| parent | b48847c5fac055f0d6120029f6fe1a50c852a8ac (diff) | |
| download | lua-205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3.tar.gz lua-205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3.tar.bz2 lua-205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3.zip | |
userdata can handle arbitrary binary data;
user tag is stored with data;
Diffstat (limited to 'opcode.c')
| -rw-r--r-- | opcode.c | 46 |
1 files changed, 36 insertions, 10 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_opcode="$Id: opcode.c,v 3.78 1996/11/22 13:08:28 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.79 1997/01/31 14:27:11 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -709,6 +709,20 @@ char *lua_getstring (lua_Object object) | |||
| 709 | else return (svalue(Address(object))); | 709 | else return (svalue(Address(object))); |
| 710 | } | 710 | } |
| 711 | 711 | ||
| 712 | void *lua_getbinarydata (lua_Object object) | ||
| 713 | { | ||
| 714 | if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_USERDATA) | ||
| 715 | lua_error("getbinarydata: object is not binary data"); | ||
| 716 | return svalue(Address(object)); | ||
| 717 | } | ||
| 718 | |||
| 719 | int lua_getbindatasize (lua_Object object) | ||
| 720 | { | ||
| 721 | if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_USERDATA) | ||
| 722 | return 0; | ||
| 723 | else return (Address(object))->value.ts->size; | ||
| 724 | } | ||
| 725 | |||
| 712 | /* | 726 | /* |
| 713 | ** Given an object handle, return its cfuntion pointer. On error, return NULL. | 727 | ** Given an object handle, return its cfuntion pointer. On error, return NULL. |
| 714 | */ | 728 | */ |
| @@ -725,9 +739,7 @@ lua_CFunction lua_getcfunction (lua_Object object) | |||
| 725 | */ | 739 | */ |
| 726 | void *lua_getuserdata (lua_Object object) | 740 | void *lua_getuserdata (lua_Object object) |
| 727 | { | 741 | { |
| 728 | if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA) | 742 | return *(void **)lua_getbinarydata(object); |
| 729 | return NULL; | ||
| 730 | else return (uvalue(Address(object))); | ||
| 731 | } | 743 | } |
| 732 | 744 | ||
| 733 | 745 | ||
| @@ -825,15 +837,25 @@ void lua_pushcfunction (lua_CFunction fn) | |||
| 825 | incr_top; | 837 | incr_top; |
| 826 | } | 838 | } |
| 827 | 839 | ||
| 840 | void lua_pushbinarydata (void *buff, int size, int tag) | ||
| 841 | { | ||
| 842 | if (buff == NULL) | ||
| 843 | tag(top) = LUA_T_NIL; | ||
| 844 | else { | ||
| 845 | tsvalue(top) = luaI_createuserdata(buff, size, tag); | ||
| 846 | tag(top) = LUA_T_USERDATA; | ||
| 847 | } | ||
| 848 | incr_top; | ||
| 849 | } | ||
| 850 | |||
| 828 | /* | 851 | /* |
| 829 | ** Push an object (tag=userdata) to stack. | 852 | ** Push an object (tag=userdata) to stack. |
| 830 | */ | 853 | */ |
| 831 | void lua_pushusertag (void *u, int tag) | 854 | void lua_pushusertag (void *u, int tag) |
| 832 | { | 855 | { |
| 833 | if (tag < LUA_T_USERDATA) | 856 | if (tag < LUA_T_USERDATA) |
| 834 | lua_error("invalid tag in `lua_pushusertag'"); | 857 | lua_error("invalid tag in `lua_pushusertag'"); |
| 835 | tag(top) = tag; uvalue(top) = u; | 858 | lua_pushbinarydata(&u, sizeof(void *), tag); |
| 836 | incr_top; | ||
| 837 | } | 859 | } |
| 838 | 860 | ||
| 839 | /* | 861 | /* |
| @@ -862,8 +884,12 @@ int lua_type (lua_Object o) | |||
| 862 | { | 884 | { |
| 863 | if (o == LUA_NOOBJECT) | 885 | if (o == LUA_NOOBJECT) |
| 864 | return LUA_T_NIL; | 886 | return LUA_T_NIL; |
| 865 | else | 887 | else { |
| 866 | return tag(Address(o)); | 888 | lua_Type t = tag(Address(o)); |
| 889 | if (t == LUA_T_USERDATA) | ||
| 890 | return (Address(o))->value.ts->tag; | ||
| 891 | else return tag(Address(o)); | ||
| 892 | } | ||
| 867 | } | 893 | } |
| 868 | 894 | ||
| 869 | 895 | ||
