lib/tldr/src/profiling/collect/sources/lua/stubs.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

  1 const collect = @import("../../root.zig");
  2 const collect_base = collect.base;
  3 const std = collect_base.std;
  4 const sys = collect_base.sys;
  5 
  6 const model = collect.model;
  7 
  8 pub fn writeLuaStubs(allocator: model.Allocator, path: []const u8, generation: u32) !void {
  9     var out: std.Io.Writer.Allocating = .init(allocator);
 10     defer out.deinit();
 11     try out.writer.writeAll(
 12         \\typedef unsigned long size_t;
 13         \\typedef long time_t;
 14         \\typedef struct FILE FILE;
 15         \\struct FILE { int unused; };
 16         \\FILE stdin_storage, stdout_storage, stderr_storage;
 17         \\FILE *stdin = &stdin_storage;
 18         \\FILE *stdout = &stdout_storage;
 19         \\FILE *stderr = &stderr_storage;
 20     );
 21     try out.writer.writeAll("\nint err");
 22     try out.writer.writeAll("no;\n");
 23     try out.writer.print("int tldr_relink_generation = {d};\n", .{generation});
 24     try out.writer.writeAll(
 25         \\static unsigned short ctype_table[384];
 26         \\static int ctype_int_table[384];
 27     );
 28     try out.writer.writeAll("\nint *__err");
 29     try out.writer.writeAll("no_location(void) { return &err");
 30     try out.writer.writeAll("no; }\n");
 31     try out.writer.writeAll(
 32         \\const unsigned short int **__ctype_b_loc(void) { static const unsigned short int *p = ctype_table; return &p; }
 33         \\const int **__ctype_tolower_loc(void) { static const int *p = ctype_int_table; return &p; }
 34         \\const int **__ctype_toupper_loc(void) { static const int *p = ctype_int_table; return &p; }
 35         \\int _setjmp(void *env) { (void)env; return 0; }
 36         \\void __longjmp_chk(void *env, int value) { (void)env; (void)value; for (;;) {} }
 37         \\void longjmp(void *env, int value) { (void)env; (void)value; for (;;) {} }
 38         \\void abort(void) { for (;;) {} }
 39         \\void exit(int code) { (void)code; for (;;) {} }
 40         \\int abs(int x) { return x < 0 ? -x : x; }
 41         \\int tolower(int c) { return c; }
 42         \\int toupper(int c) { return c; }
 43         \\double acos(double x) { return x; }
 44         \\double asin(double x) { return x; }
 45         \\double atan2(double y, double x) { return y + x; }
 46         \\double ceil(double x) { return x; }
 47         \\double cos(double x) { return x; }
 48         \\double difftime(time_t a, time_t b) { return (double)(a - b); }
 49         \\double exp(double x) { return x; }
 50         \\double fabs(double x) { return x < 0 ? -x : x; }
 51         \\double floor(double x) { return x; }
 52         \\double fmod(double x, double y) { return y == 0 ? x : x; }
 53         \\double frexp(double x, int *e) { if (e) *e = 0; return x; }
 54         \\double ldexp(double x, int e) { (void)e; return x; }
 55         \\double log(double x) { return x; }
 56         \\double log10(double x) { return x; }
 57         \\double pow(double x, double y) { return x + y; }
 58         \\double sin(double x) { return x; }
 59         \\double sqrt(double x) { return x; }
 60         \\double tan(double x) { return x; }
 61         \\void clearerr(FILE *f) { (void)f; }
 62         \\long clock(void) { return (long)tldr_relink_generation; }
 63         \\int fclose(FILE *f) { (void)f; return 0; }
 64         \\int feof(FILE *f) { (void)f; return 1; }
 65         \\int ferror(FILE *f) { (void)f; return 0; }
 66         \\int fflush(FILE *f) { (void)f; return 0; }
 67         \\char *fgets(char *s, int n, FILE *f) { (void)f; if (n > 0) s[0] = 0; return 0; }
 68         \\FILE *fopen(const char *p, const char *m) { (void)p; (void)m; return 0; }
 69         \\FILE *freopen(const char *p, const char *m, FILE *f) { (void)p; (void)m; return f; }
 70         \\int fputs(const char *s, FILE *f) { (void)s; (void)f; return 0; }
 71         \\size_t fread(void *p, size_t s, size_t n, FILE *f) { (void)p; (void)s; (void)n; (void)f; return 0; }
 72         \\int fseek(FILE *f, long o, int w) { (void)f; (void)o; (void)w; return 0; }
 73         \\long ftell(FILE *f) { (void)f; return 0; }
 74         \\size_t fwrite(const void *p, size_t s, size_t n, FILE *f) { (void)p; (void)s; (void)f; return n; }
 75         \\int getc(FILE *f) { (void)f; return -1; }
 76         \\char *getenv(const char *n) { (void)n; return 0; }
 77         \\void *gmtime(const time_t *t) { (void)t; return 0; }
 78         \\void *localtime(const time_t *t) { (void)t; return 0; }
 79         \\void *localeconv(void) { return 0; }
 80         \\time_t mktime(void *tm) { (void)tm; return 0; }
 81         \\int remove(const char *p) { (void)p; return 0; }
 82         \\int rename(const char *a, const char *b) { (void)a; (void)b; return 0; }
 83         \\char *setlocale(int c, const char *l) { (void)c; (void)l; return 0; }
 84         \\int setvbuf(FILE *f, char *b, int m, size_t s) { (void)f; (void)b; (void)m; (void)s; return 0; }
 85         \\void (*signal(int s, void (*h)(int)))(int) { (void)s; return h; }
 86         \\int strftime(char *s, size_t n, const char *f, const void *tm) { (void)f; (void)tm; if (n) s[0] = 0; return 0; }
 87         \\int system(const char *c) { (void)c; return 0; }
 88         \\time_t time(time_t *t) { if (t) *t = 0; return 0; }
 89         \\FILE *tmpfile(void) { return 0; }
 90         \\char *tmpnam(char *s) { if (s) s[0] = 0; return s; }
 91         \\int ungetc(int c, FILE *f) { (void)f; return c; }
 92         \\void free(void *p) { (void)p; }
 93         \\void *realloc(void *p, size_t n) { (void)n; return p; }
 94         \\void *memchr(const void *s, int c, size_t n) { const unsigned char *p = s; for (size_t i = 0; i < n; i++) if (p[i] == (unsigned char)c) return (void *)(p + i); return 0; }
 95         \\int memcmp(const void *a, const void *b, size_t n) { const unsigned char *x = a, *y = b; for (size_t i = 0; i < n; i++) if (x[i] != y[i]) return (int)x[i] - (int)y[i]; return 0; }
 96         \\void *memcpy(void *d, const void *s, size_t n) { unsigned char *x = d; const unsigned char *y = s; for (size_t i = 0; i < n; i++) x[i] = y[i]; return d; }
 97         \\void *memset(void *d, int c, size_t n) { unsigned char *x = d; for (size_t i = 0; i < n; i++) x[i] = (unsigned char)c; return d; }
 98         \\char *strchr(const char *s, int c) { while (*s) { if (*s == (char)c) return (char *)s; s++; } return c == 0 ? (char *)s : 0; }
 99         \\int strcmp(const char *a, const char *b) { while (*a && *a == *b) { a++; b++; } return (unsigned char)*a - (unsigned char)*b; }
100         \\int strcoll(const char *a, const char *b) { return strcmp(a, b); }
101         \\char *strcpy(char *d, const char *s) { char *r = d; while ((*d++ = *s++)) {} return r; }
102         \\char *strerror(int e) { (void)e; return ""; }
103         \\size_t strlen(const char *s) { size_t n = 0; while (s[n]) n++; return n; }
104         \\int strncmp(const char *a, const char *b, size_t n) { for (size_t i = 0; i < n; i++) { if (a[i] != b[i] || !a[i]) return (unsigned char)a[i] - (unsigned char)b[i]; } return 0; }
105         \\char *strpbrk(const char *s, const char *a) { for (; *s; s++) for (const char *p = a; *p; p++) if (*s == *p) return (char *)s; return 0; }
106         \\size_t strspn(const char *s, const char *a) { size_t n = 0; for (; s[n]; n++) { int ok = 0; for (const char *p = a; *p; p++) if (s[n] == *p) ok = 1; if (!ok) break; } return n; }
107         \\char *strstr(const char *h, const char *n) { if (!*n) return (char *)h; for (; *h; h++) { const char *a = h, *b = n; while (*a && *b && *a == *b) { a++; b++; } if (!*b) return (char *)h; } return 0; }
108         \\double strtod(const char *s, char **e) { if (e) *e = (char *)s; return 0; }
109         \\int fprintf(FILE *f, const char *fmt, ...) { (void)f; (void)fmt; return 0; }
110         \\int sprintf(char *s, const char *fmt, ...) { (void)fmt; if (s) s[0] = 0; return 0; }
111         \\int __fprintf_chk(FILE *f, int flag, const char *fmt, ...) { (void)f; (void)flag; (void)fmt; return 0; }
112         \\int __sprintf_chk(char *s, int flag, size_t len, const char *fmt, ...) { (void)flag; (void)len; (void)fmt; if (s) s[0] = 0; return 0; }
113         \\void *__memcpy_chk(void *d, const void *s, size_t n, size_t os) { (void)os; return memcpy(d, s, n); }
114         \\char *__strcpy_chk(char *d, const char *s, size_t os) { (void)os; return strcpy(d, s); }
115         \\
116     );
117     try sys.fs.writeFile(path, out.written());
118 }