diff options
author | Mike Pall <mike> | 2013-02-22 01:40:41 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2013-02-22 01:40:41 +0100 |
commit | e20157c6e68472e7c4d82d9ed4c0bb5be029c388 (patch) | |
tree | b26bfadc10b7301d73b84293630c44e7d1fb37f9 /src/lj_lib.c | |
parent | c3219b7d177f6722b9de808cfd3d3dbfc6808e6f (diff) | |
download | luajit-e20157c6e68472e7c4d82d9ed4c0bb5be029c388.tar.gz luajit-e20157c6e68472e7c4d82d9ed4c0bb5be029c388.tar.bz2 luajit-e20157c6e68472e7c4d82d9ed4c0bb5be029c388.zip |
Add support for embedding LuaJIT bytecode for builtins.
Diffstat (limited to 'src/lj_lib.c')
-rw-r--r-- | src/lj_lib.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/lj_lib.c b/src/lj_lib.c index 331eaa6a..be3ee004 100644 --- a/src/lj_lib.c +++ b/src/lj_lib.c | |||
@@ -18,6 +18,8 @@ | |||
18 | #include "lj_dispatch.h" | 18 | #include "lj_dispatch.h" |
19 | #include "lj_vm.h" | 19 | #include "lj_vm.h" |
20 | #include "lj_strscan.h" | 20 | #include "lj_strscan.h" |
21 | #include "lj_lex.h" | ||
22 | #include "lj_bcdump.h" | ||
21 | #include "lj_lib.h" | 23 | #include "lj_lib.h" |
22 | 24 | ||
23 | /* -- Library initialization ---------------------------------------------- */ | 25 | /* -- Library initialization ---------------------------------------------- */ |
@@ -43,6 +45,28 @@ static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize) | |||
43 | return tabV(L->top-1); | 45 | return tabV(L->top-1); |
44 | } | 46 | } |
45 | 47 | ||
48 | static const uint8_t *lib_read_lfunc(lua_State *L, const uint8_t *p, GCtab *tab) | ||
49 | { | ||
50 | int len = *p++; | ||
51 | GCstr *name = lj_str_new(L, (const char *)p, len); | ||
52 | LexState ls; | ||
53 | GCproto *pt; | ||
54 | GCfunc *fn; | ||
55 | memset(&ls, 0, sizeof(ls)); | ||
56 | ls.L = L; | ||
57 | ls.p = (const char *)(p+len); | ||
58 | ls.n = ~(MSize)0; | ||
59 | ls.current = -1; | ||
60 | ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE)); | ||
61 | ls.chunkname = name; | ||
62 | pt = lj_bcread_proto(&ls); | ||
63 | pt->firstline = ~(BCLine)0; | ||
64 | fn = lj_func_newL_empty(L, pt, tabref(L->env)); | ||
65 | /* NOBARRIER: See below for common barrier. */ | ||
66 | setfuncV(L, lj_tab_setstr(L, tab, name), fn); | ||
67 | return (const uint8_t *)ls.p; | ||
68 | } | ||
69 | |||
46 | void lj_lib_register(lua_State *L, const char *libname, | 70 | void lj_lib_register(lua_State *L, const char *libname, |
47 | const uint8_t *p, const lua_CFunction *cf) | 71 | const uint8_t *p, const lua_CFunction *cf) |
48 | { | 72 | { |
@@ -87,6 +111,9 @@ void lj_lib_register(lua_State *L, const char *libname, | |||
87 | ofn = fn; | 111 | ofn = fn; |
88 | } else { | 112 | } else { |
89 | switch (tag | len) { | 113 | switch (tag | len) { |
114 | case LIBINIT_LUA: | ||
115 | p = lib_read_lfunc(L, p, tab); | ||
116 | break; | ||
90 | case LIBINIT_SET: | 117 | case LIBINIT_SET: |
91 | L->top -= 2; | 118 | L->top -= 2; |
92 | if (tvisstr(L->top+1) && strV(L->top+1)->len == 0) | 119 | if (tvisstr(L->top+1) && strV(L->top+1)->len == 0) |