Skip to content

Commit 0957f32

Browse files
authored
Refactor pointers to const
1 parent 76d579a commit 0957f32

26 files changed

+865
-833
lines changed

playground/umka.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/umka.cbp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Option object_output="obj/Debug/" />
1212
<Option type="1" />
1313
<Option compiler="gcc" />
14-
<Option parameters="-warn ../test.um" />
14+
<Option parameters="-warn ../tests/all.um" />
1515
<Compiler>
1616
<Add option="-g" />
1717
</Compiler>

src/umka.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ void printCompileWarning(UmkaError *warning)
7171

7272
void printCompileError(void *umka)
7373
{
74-
UmkaError *error = umkaGetError(umka);
74+
const UmkaError *error = umkaGetError(umka);
7575
fprintf(stderr, "Error %s (%d, %d): %s\n", error->fileName, error->line, error->pos, error->msg);
7676
}
7777

7878

7979
void printRuntimeError(void *umka)
8080
{
81-
UmkaError *error = umkaGetError(umka);
81+
const UmkaError *error = umkaGetError(umka);
8282

8383
if (error->msg[0])
8484
{

src/umka_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define UMKA_VERSION "1.5.4"
1212

1313

14-
static void compileWarning(void *context, DebugInfo *debug, const char *format, ...)
14+
static void compileWarning(void *context, const DebugInfo *debug, const char *format, ...)
1515
{
1616
va_list args;
1717
va_start(args, format);
@@ -55,7 +55,7 @@ static void runtimeError(void *context, int code, const char *format, ...)
5555
va_start(args, format);
5656

5757
Compiler *comp = context;
58-
DebugInfo *debug = &comp->vm.fiber->debugPerInstr[comp->vm.fiber->ip];
58+
const DebugInfo *debug = &comp->vm.fiber->debugPerInstr[comp->vm.fiber->ip];
5959
errorReportInit(&comp->error.report, &comp->storage, debug->fileName, debug->fnName, debug->line, 1, code, format, args);
6060

6161
vmKill(&comp->vm);

src/umka_api.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ typedef struct
9494

9595
typedef struct
9696
{
97-
char *fileName;
98-
char *fnName;
97+
const char *fileName;
98+
const char *fnName;
9999
int line, pos, code;
100-
char *msg;
100+
const char *msg;
101101
} UmkaError;
102102

103103

src/umka_common.c

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
void errorReportInit(ErrorReport *report, Storage *storage, const char *fileName, const char *fnName, int line, int pos, int code, const char *format, va_list args)
2424
{
25-
report->fileName = storageAdd(storage, strlen(fileName) + 1);
26-
strcpy(report->fileName, fileName);
25+
char *reportFileName = storageAdd(storage, strlen(fileName) + 1);
26+
strcpy(reportFileName, fileName);
27+
report->fileName = reportFileName;
2728

28-
report->fnName = storageAdd(storage, strlen(fnName) + 1);
29-
strcpy(report->fnName, fnName);
29+
char *reportFnName = storageAdd(storage, strlen(fnName) + 1);
30+
strcpy(reportFnName, fnName);
31+
report->fnName = reportFnName;
3032

3133
report->line = line;
3234
report->pos = pos;
@@ -36,8 +38,9 @@ void errorReportInit(ErrorReport *report, Storage *storage, const char *fileName
3638
va_copy(argsCopy, args);
3739

3840
const int msgLen = vsnprintf(NULL, 0, format, args);
39-
report->msg = storageAdd(storage, msgLen + 1);
40-
vsnprintf(report->msg, msgLen + 1, format, argsCopy);
41+
char *reportMsg = storageAdd(storage, msgLen + 1);
42+
vsnprintf(reportMsg, msgLen + 1, format, argsCopy);
43+
report->msg = reportMsg;
4144

4245
va_end(argsCopy);
4346
}
@@ -96,7 +99,7 @@ char *storageAddStr(Storage *storage, int64_t len)
9699
}
97100

98101

99-
DynArray *storageAddDynArray(Storage *storage, struct tagType *type, int64_t len)
102+
DynArray *storageAddDynArray(Storage *storage, const struct tagType *type, int64_t len)
100103
{
101104
DynArray *array = storageAdd(storage, sizeof(DynArray));
102105

@@ -132,7 +135,7 @@ void storageRemove(Storage *storage, void *data)
132135

133136
void *storageRealloc(Storage *storage, void *data, int64_t size)
134137
{
135-
StorageChunk *chunk = (StorageChunk *)((char *)data - sizeof(StorageChunk));
138+
const StorageChunk *chunk = (const StorageChunk *)((char *)data - sizeof(StorageChunk));
136139

137140
void *newData = storageAdd(storage, size);
138141
memcpy(newData, data, chunk->size);
@@ -154,8 +157,9 @@ static const char *moduleImplLibSuffix()
154157
#else
155158
return "_linux";
156159
#endif
157-
#endif
160+
#else
158161
return "";
162+
#endif
159163
}
160164

161165

@@ -167,8 +171,9 @@ static void *moduleLoadImplLib(const char *path)
167171
#else
168172
return dlopen(path, RTLD_LOCAL | RTLD_LAZY);
169173
#endif
170-
#endif
174+
#else
171175
return NULL;
176+
#endif
172177
}
173178

174179

@@ -192,8 +197,9 @@ static void *moduleLoadImplLibFunc(void *lib, const char *name)
192197
#else
193198
return dlsym(lib, name);
194199
#endif
195-
#endif
200+
#else
196201
return NULL;
202+
#endif
197203
}
198204

199205

@@ -223,7 +229,7 @@ void moduleFree(Modules *modules)
223229
}
224230

225231

226-
void moduleNameFromPath(Modules *modules, const char *path, char *folder, char *name, int size)
232+
void moduleNameFromPath(const Modules *modules, const char *path, char *folder, char *name, int size)
227233
{
228234
const char *slash = strrchr(path, '/');
229235
const char *backslash = strrchr(path, '\\');
@@ -247,17 +253,17 @@ void moduleNameFromPath(Modules *modules, const char *path, char *folder, char *
247253
}
248254

249255

250-
int moduleFind(Modules *modules, const char *path)
256+
int moduleFind(const Modules *modules, const char *path)
251257
{
252-
unsigned int pathHash = hash(path);
258+
const unsigned int pathHash = hash(path);
253259
for (int i = 0; i < modules->numModules; i++)
254260
if (modules->module[i]->pathHash == pathHash && strcmp(modules->module[i]->path, path) == 0)
255261
return i;
256262
return -1;
257263
}
258264

259265

260-
int moduleFindImported(Modules *modules, Blocks *blocks, const char *alias)
266+
int moduleFindImported(const Modules *modules, const Blocks *blocks, const char *alias)
261267
{
262268
for (int i = 0; i < modules->numModules; i++)
263269
{
@@ -329,9 +335,9 @@ int moduleAdd(Modules *modules, const char *path)
329335
}
330336

331337

332-
ModuleSource *moduleFindSource(Modules *modules, const char *path)
338+
const ModuleSource *moduleFindSource(const Modules *modules, const char *path)
333339
{
334-
unsigned int pathHash = hash(path);
340+
const unsigned int pathHash = hash(path);
335341
for (int i = 0; i < modules->numModuleSources; i++)
336342
if (modules->moduleSource[i]->pathHash == pathHash && strcmp(modules->moduleSource[i]->path, path) == 0)
337343
return modules->moduleSource[i];
@@ -371,7 +377,7 @@ void moduleAddSource(Modules *modules, const char *path, const char *source, boo
371377
}
372378

373379

374-
void *moduleGetImplLibFunc(Module *module, const char *name)
380+
void *moduleGetImplLibFunc(const Module *module, const char *name)
375381
{
376382
if (module->implLib)
377383
return moduleLoadImplLibFunc(module->implLib, name);
@@ -419,15 +425,16 @@ bool modulePathIsAbsolute(const char *path)
419425
}
420426

421427

422-
bool moduleRegularizePath(Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size)
428+
bool moduleRegularizePath(const Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size)
423429
{
424430
char *absolutePath = storageAdd(modules->storage, size);
425431
snprintf(absolutePath, size, "%s%s", modulePathIsAbsolute(path) ? "" : curFolder, path);
426432

427433
char **separators = storageAdd(modules->storage, size * sizeof(char *));
428434
int numSeparators = 0;
429435

430-
char *readCh = absolutePath, *writeCh = regularizedPath;
436+
const char *readCh = absolutePath;
437+
char *writeCh = regularizedPath;
431438
int numDots = 0;
432439

433440
while (*readCh)
@@ -499,7 +506,7 @@ bool moduleRegularizePath(Modules *modules, const char *path, const char *curFol
499506
}
500507

501508

502-
void moduleAssertRegularizePath(Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size)
509+
void moduleAssertRegularizePath(const Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size)
503510
{
504511
if (!moduleRegularizePath(modules, path, curFolder, regularizedPath, size))
505512
modules->error->handler(modules->error->context, "Invalid module path %s", path);
@@ -519,7 +526,7 @@ void blocksInit(Blocks *blocks, Error *error)
519526
}
520527

521528

522-
void blocksEnterFn(Blocks *blocks, struct tagIdent *fn, bool hasUpvalues)
529+
void blocksEnterFn(Blocks *blocks, const struct tagIdent *fn, bool hasUpvalues)
523530
{
524531
if (blocks->top >= MAX_BLOCK_NESTING)
525532
blocks->error->handler(blocks->error->context, "Block nesting is too deep");
@@ -553,7 +560,7 @@ void blocksReenter(Blocks *blocks)
553560
}
554561

555562

556-
int blocksCurrent(Blocks *blocks)
563+
int blocksCurrent(const Blocks *blocks)
557564
{
558565
return blocks->item[blocks->top].block;
559566
}
@@ -568,9 +575,9 @@ void externalInit(Externals *externals, Storage *storage)
568575
}
569576

570577

571-
External *externalFind(Externals *externals, const char *name)
578+
External *externalFind(const Externals *externals, const char *name)
572579
{
573-
unsigned int nameHash = hash(name);
580+
const unsigned int nameHash = hash(name);
574581

575582
for (External *external = externals->first; external; external = external->next)
576583
if (external->hash == nameHash && strcmp(external->name, name) == 0)

src/umka_common.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef StrDimensions DynArrayDimensions;
4141
typedef struct
4242
{
4343
// Must have 8 byte alignment
44-
struct tagType *type;
44+
const struct tagType *type;
4545
int64_t itemSize; // Duplicates information contained in type, but useful for better performance
4646
void *data; // Allocated chunk should start at (char *)data - sizeof(DynArrayDimensions)
4747
} DynArray;
@@ -51,7 +51,7 @@ typedef struct
5151
{
5252
// The C equivalent of the Umka interface type
5353
void *self;
54-
struct tagType *selfType;
54+
const struct tagType *selfType;
5555
// Methods are omitted - do not use sizeof() for non-empty interfaces
5656
} Interface;
5757

@@ -76,15 +76,15 @@ typedef struct tagMapNode
7676
typedef struct
7777
{
7878
// Must have 8 byte alignment
79-
struct tagType *type;
79+
const struct tagType *type;
8080
MapNode *root;
8181
} Map;
8282

8383

8484
typedef struct
8585
{
86-
char *fileName;
87-
char *fnName;
86+
const char *fileName;
87+
const char *fnName;
8888
int line;
8989
} DebugInfo;
9090

@@ -94,18 +94,18 @@ typedef void (*WarningCallback)(void * /*UmkaError*/ warning);
9494

9595
typedef struct // Must be identical to UmkaError
9696
{
97-
char *fileName;
98-
char *fnName;
97+
const char *fileName;
98+
const char *fnName;
9999
int line, pos, code;
100-
char *msg;
100+
const char *msg;
101101
} ErrorReport;
102102

103103

104104
typedef struct
105105
{
106106
void (*handler)(void *context, const char *format, ...);
107107
void (*runtimeHandler)(void *context, int code, const char *format, ...);
108-
void (*warningHandler)(void *context, DebugInfo *debug, const char *format, ...);
108+
void (*warningHandler)(void *context, const DebugInfo *debug, const char *format, ...);
109109
WarningCallback warningCallback;
110110
void *context;
111111
jmp_buf jumper;
@@ -151,7 +151,7 @@ typedef struct
151151
typedef struct
152152
{
153153
Module *module[MAX_MODULES];
154-
ModuleSource *moduleSource[MAX_MODULES];
154+
const ModuleSource *moduleSource[MAX_MODULES];
155155
int numModules, numModuleSources;
156156
char curFolder[DEFAULT_STR_LEN + 1];
157157
bool implLibsEnabled;
@@ -163,7 +163,7 @@ typedef struct
163163
typedef struct
164164
{
165165
int block;
166-
struct tagIdent *fn;
166+
const struct tagIdent *fn;
167167
int localVarSize; // For function blocks only
168168
bool hasReturn;
169169
bool hasUpvalues;
@@ -207,7 +207,7 @@ typedef struct
207207

208208
typedef struct
209209
{
210-
ParamLayout *paramLayout;
210+
const ParamLayout *paramLayout;
211211
int64_t localVarSlots;
212212
} ParamAndLocalVarLayout;
213213

@@ -218,33 +218,33 @@ void storageInit (Storage *storage, Error *error);
218218
void storageFree (Storage *storage);
219219
void *storageAdd (Storage *storage, int64_t size);
220220
char *storageAddStr (Storage *storage, int64_t len);
221-
DynArray *storageAddDynArray (Storage *storage, struct tagType *type, int64_t len);
221+
DynArray *storageAddDynArray (Storage *storage, const struct tagType *type, int64_t len);
222222
void storageRemove (Storage *storage, void *data);
223223
void *storageRealloc (Storage *storage, void *data, int64_t size);
224224

225225
void moduleInit (Modules *modules, Storage *storage, bool implLibsEnabled, Error *error);
226226
void moduleFree (Modules *modules);
227-
void moduleNameFromPath (Modules *modules, const char *path, char *folder, char *name, int size);
228-
int moduleFind (Modules *modules, const char *path);
229-
int moduleFindImported (Modules *modules, Blocks *blocks, const char *name);
227+
void moduleNameFromPath (const Modules *modules, const char *path, char *folder, char *name, int size);
228+
int moduleFind (const Modules *modules, const char *path);
229+
int moduleFindImported (const Modules *modules, const Blocks *blocks, const char *alias);
230230
int moduleAdd (Modules *modules, const char *path);
231-
ModuleSource *moduleFindSource (Modules *modules, const char *path);
231+
const ModuleSource *moduleFindSource(const Modules *modules, const char *path);
232232
void moduleAddSource (Modules *modules, const char *path, const char *source, bool trusted);
233-
void *moduleGetImplLibFunc (Module *module, const char *name);
233+
void *moduleGetImplLibFunc (const Module *module, const char *name);
234234
char *moduleCurFolder (char *buf, int size);
235235
bool modulePathIsAbsolute (const char *path);
236-
bool moduleRegularizePath (Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size);
237-
void moduleAssertRegularizePath(Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size);
236+
bool moduleRegularizePath (const Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size);
237+
void moduleAssertRegularizePath(const Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size);
238238

239239
void blocksInit (Blocks *blocks, Error *error);
240-
void blocksEnterFn(Blocks *blocks, struct tagIdent *fn, bool hasUpvalues);
240+
void blocksEnterFn(Blocks *blocks, const struct tagIdent *fn, bool hasUpvalues);
241241
void blocksEnter (Blocks *blocks);
242242
void blocksReenter(Blocks *blocks);
243243
void blocksLeave (Blocks *blocks);
244-
int blocksCurrent(Blocks *blocks);
244+
int blocksCurrent(const Blocks *blocks);
245245

246246
void externalInit (Externals *externals, Storage *storage);
247-
External *externalFind (Externals *externals, const char *name);
247+
External *externalFind (const Externals *externals, const char *name);
248248
External *externalAdd (Externals *externals, const char *name, void *entry, bool resolveInTrusted);
249249

250250

0 commit comments

Comments
 (0)