misc/liblua/ldump.c
author unC0Rr
Tue, 03 Sep 2024 11:16:52 +0200
branchtransitional_engine
changeset 16028 509ecce37522
parent 10017 de822cd3df3a
permissions -rw-r--r--
Make outline template generator behave more like old engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** save precompiled Lua chunks
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
#include <stddef.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#define ldump_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "lundump.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
typedef struct {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
 lua_State* L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
 lua_Writer writer;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
 void* data;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
 int strip;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
 int status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
} DumpState;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    26
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    27
#define DumpVar(x,D)        DumpMem(&x,1,sizeof(x),D)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
static void DumpBlock(const void* b, size_t size, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
 if (D->status==0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
 {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
  lua_unlock(D->L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
  D->status=(*D->writer)(D->L,b,size,D->data);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
  lua_lock(D->L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
 }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
static void DumpChar(int y, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
 char x=(char)y;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
 DumpVar(x,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
static void DumpInt(int x, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
 DumpVar(x,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
static void DumpNumber(lua_Number x, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
 DumpVar(x,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
static void DumpVector(const void* b, int n, size_t size, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
 DumpInt(n,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
 DumpMem(b,n,size,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
static void DumpString(const TString* s, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
 if (s==NULL || getstr(s)==NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
 {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
  size_t size=0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
  DumpVar(size,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
 }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
 else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
 {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    70
  size_t size=s->tsv.len+1;     /* include trailing '\0' */
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
  DumpVar(size,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
  DumpBlock(getstr(s),size,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
 }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    76
#define DumpCode(f,D)    DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
static void DumpConstants(const Proto* f, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
 int i,n=f->sizek;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
 DumpInt(n,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
 for (i=0; i<n; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
 {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
  const TValue* o=&f->k[i];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
  DumpChar(ttype(o),D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
  switch (ttype(o))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
  {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
   case LUA_TNIL:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    91
    break;
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
   case LUA_TBOOLEAN:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    93
    DumpChar(bvalue(o),D);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    94
    break;
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
   case LUA_TNUMBER:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    96
    DumpNumber(nvalue(o),D);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    97
    break;
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
   case LUA_TSTRING:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    99
    DumpString(rawtsvalue(o),D);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   100
    break;
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
   default:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   102
    lua_assert(0);          /* cannot happen */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
   103
    break;
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
 }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
 n=f->sizep;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
 DumpInt(n,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
 for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
static void DumpDebug(const Proto* f, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
 int i,n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
 n= (D->strip) ? 0 : f->sizelineinfo;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
 DumpVector(f->lineinfo,n,sizeof(int),D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
 n= (D->strip) ? 0 : f->sizelocvars;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
 DumpInt(n,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
 for (i=0; i<n; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
 {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
  DumpString(f->locvars[i].varname,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
  DumpInt(f->locvars[i].startpc,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
  DumpInt(f->locvars[i].endpc,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
 }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
 n= (D->strip) ? 0 : f->sizeupvalues;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
 DumpInt(n,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
 for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
 DumpString((f->source==p || D->strip) ? NULL : f->source,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
 DumpInt(f->linedefined,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
 DumpInt(f->lastlinedefined,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
 DumpChar(f->nups,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
 DumpChar(f->numparams,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
 DumpChar(f->is_vararg,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
 DumpChar(f->maxstacksize,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
 DumpCode(f,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
 DumpConstants(f,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
 DumpDebug(f,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
static void DumpHeader(DumpState* D)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
 char h[LUAC_HEADERSIZE];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
 luaU_header(h);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
 DumpBlock(h,LUAC_HEADERSIZE,D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
** dump Lua function as precompiled chunk
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
{
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
 DumpState D;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
 D.L=L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
 D.writer=w;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
 D.data=data;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
 D.strip=strip;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
 D.status=0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
 DumpHeader(&D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
 DumpFunction(f,NULL,&D);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
 return D.status;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
}