A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3** save precompiled Lua chunks
4** See Copyright Notice in lua.h
5*/
6
7#include <stddef.h>
8
9#define ldump_c
10#define LUA_CORE
11
12#include "lua.h"
13
14#include "lobject.h"
15#include "lstate.h"
16#include "lundump.h"
17
18#ifndef LUA_DISABLE_BYTECODE
19
20typedef struct {
21 lua_State* L;
22 lua_Writer writer;
23 void* data;
24 int strip;
25 int status;
26} DumpState;
27
28#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
29#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
30
31static void DumpBlock(const void* b, size_t size, DumpState* D)
32{
33 if (D->status==0)
34 {
35 lua_unlock(D->L);
36 D->status=(*D->writer)(D->L,b,size,D->data);
37 lua_lock(D->L);
38 }
39}
40
41static void DumpChar(int y, DumpState* D)
42{
43 char x=(char)y;
44 DumpVar(x,D);
45}
46
47static void DumpInt(int x, DumpState* D)
48{
49 DumpVar(x,D);
50}
51
52static void DumpNumber(lua_Number x, DumpState* D)
53{
54 DumpVar(x,D);
55}
56
57static void DumpVector(const void* b, int n, size_t size, DumpState* D)
58{
59 DumpInt(n,D);
60 DumpMem(b,n,size,D);
61}
62
63static void DumpString(const TString* s, DumpState* D)
64{
65 if (s==NULL || getstr(s)==NULL)
66 {
67 size_t size=0;
68 DumpVar(size,D);
69 }
70 else
71 {
72 size_t size=s->tsv.len+1; /* include trailing '\0' */
73 DumpVar(size,D);
74 DumpBlock(getstr(s),size,D);
75 }
76}
77
78#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
79
80static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
81
82static void DumpConstants(const Proto* f, DumpState* D)
83{
84 int i,n=f->sizek;
85 DumpInt(n,D);
86 for (i=0; i<n; i++)
87 {
88 const TValue* o=&f->k[i];
89 DumpChar(ttype(o),D);
90 switch (ttype(o))
91 {
92 case LUA_TNIL:
93 break;
94 case LUA_TBOOLEAN:
95 DumpChar(bvalue(o),D);
96 break;
97 case LUA_TNUMBER:
98 DumpNumber(nvalue(o),D);
99 break;
100 case LUA_TSTRING:
101 DumpString(rawtsvalue(o),D);
102 break;
103 default:
104 lua_assert(0); /* cannot happen */
105 break;
106 }
107 }
108 n=f->sizep;
109 DumpInt(n,D);
110 for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
111}
112
113static void DumpDebug(const Proto* f, DumpState* D)
114{
115 int i,n;
116#ifdef LUA_OPTIMIZE_DEBUG
117 n = (D->strip || f->packedlineinfo == NULL) ? 0: f->sizelineinfo;
118 DumpInt(n,D);
119 if (n)
120 {
121 DumpBlock(f->packedlineinfo, n, D);
122 }
123#else
124 n= (D->strip) ? 0 : f->sizelineinfo;
125 DumpVector(f->lineinfo,n,sizeof(int),D);
126#endif
127 n= (D->strip) ? 0 : f->sizelocvars;
128 DumpInt(n,D);
129 for (i=0; i<n; i++)
130 {
131 DumpString(f->locvars[i].varname,D);
132 DumpInt(f->locvars[i].startpc,D);
133 DumpInt(f->locvars[i].endpc,D);
134 }
135 n= (D->strip) ? 0 : f->sizeupvalues;
136 DumpInt(n,D);
137 for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
138}
139
140static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
141{
142 DumpString((f->source==p || D->strip) ? NULL : f->source,D);
143 DumpInt(f->linedefined,D);
144 DumpInt(f->lastlinedefined,D);
145 DumpChar(f->nups,D);
146 DumpChar(f->numparams,D);
147 DumpChar(f->is_vararg,D);
148 DumpChar(f->maxstacksize,D);
149 DumpCode(f,D);
150 DumpConstants(f,D);
151 DumpDebug(f,D);
152}
153
154static void DumpHeader(DumpState* D)
155{
156 char h[LUAC_HEADERSIZE];
157 luaU_header(h);
158 DumpBlock(h,LUAC_HEADERSIZE,D);
159}
160
161/*
162** dump Lua function as precompiled chunk
163*/
164int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
165{
166 DumpState D;
167 D.L=L;
168 D.writer=w;
169 D.data=data;
170 D.strip=strip;
171 D.status=0;
172 DumpHeader(&D);
173 DumpFunction(f,NULL,&D);
174 return D.status;
175}
176#else /* LUA_DISABLE_BYTECODE */
177#include "lauxlib.h"
178int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
179{
180 (void) f;
181 (void) w;
182 (void) data;
183 (void) strip;
184 return luaL_error(L, " bytecode not supported");
185}
186#endif