1 | /* |
2 | ** Buffer library. |
3 | ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h |
4 | */ |
5 | |
6 | #define lib_buffer_c |
7 | #define LUA_LIB |
8 | |
9 | #include "lua.h" |
10 | #include "lauxlib.h" |
11 | #include "lualib.h" |
12 | |
13 | #include "lj_obj.h" |
14 | |
15 | #if LJ_HASBUFFER |
16 | #include "lj_gc.h" |
17 | #include "lj_buf.h" |
18 | #include "lj_serialize.h" |
19 | #include "lj_lib.h" |
20 | |
21 | /* ------------------------------------------------------------------------ */ |
22 | |
23 | #define LJLIB_MODULE_buffer |
24 | |
25 | /* Note: this uses interim structs until the SBuf reorg. */ |
26 | |
27 | LJLIB_CF(buffer_encode) |
28 | { |
29 | cTValue *o = lj_lib_checkany(L, 1); |
30 | StrBuf sbuf; |
31 | sbuf.sb = lj_buf_tmp_(L); |
32 | lj_serialize_put(&sbuf, o); |
33 | setstrV(L, L->top++, lj_buf_str(L, sbuf.sb)); |
34 | lj_gc_check(L); |
35 | return 1; |
36 | } |
37 | |
38 | LJLIB_CF(buffer_decode) |
39 | { |
40 | GCstr *str = lj_lib_checkstr(L, 1); |
41 | const char *p = strdata(str); |
42 | SBuf sb; |
43 | StrBuf sbuf; |
44 | setsbufL(&sb, L); |
45 | setmref(sb.b, p); |
46 | setmref(sb.p, p + str->len); |
47 | setmref(sb.e, p + str->len); |
48 | sbuf.sb = &sb; |
49 | sbuf.r = (char *)p; |
50 | setnilV(L->top++); |
51 | lj_serialize_get(&sbuf, L->top-1); |
52 | lj_gc_check(L); |
53 | return 1; |
54 | } |
55 | |
56 | /* ------------------------------------------------------------------------ */ |
57 | |
58 | #include "lj_libdef.h" |
59 | |
60 | int luaopen_string_buffer(lua_State *L) |
61 | { |
62 | LJ_LIB_REG(L, NULL, buffer); |
63 | return 1; |
64 | } |
65 | |
66 | #endif |
67 | |