mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Strengthened constness for cstrings and bytecode
This commit is contained in:
@@ -506,13 +506,13 @@ static int nativeToLower(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
|
||||
}
|
||||
|
||||
Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral);
|
||||
char* self = Toy_toCString(selfRefString);
|
||||
const char* self = Toy_toCString(selfRefString);
|
||||
|
||||
//allocate buffer space for the result
|
||||
char* result = TOY_ALLOCATE(char, Toy_lengthRefString(selfRefString) + 1);
|
||||
|
||||
//set each new character
|
||||
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) {
|
||||
for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
|
||||
result[i] = tolower(self[i]);
|
||||
}
|
||||
result[Toy_lengthRefString(selfRefString)] = '\0'; //end the string
|
||||
@@ -533,7 +533,7 @@ static int nativeToLower(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
|
||||
|
||||
static char* toStringUtilObject = NULL;
|
||||
static void toStringUtil(const char* input) {
|
||||
int len = strlen(input) + 1;
|
||||
size_t len = strlen(input) + 1;
|
||||
|
||||
if (len > TOY_MAX_STRING_LENGTH) {
|
||||
len = TOY_MAX_STRING_LENGTH; //TODO: don't truncate
|
||||
@@ -606,13 +606,13 @@ static int nativeToUpper(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
|
||||
}
|
||||
|
||||
Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral);
|
||||
char* self = Toy_toCString(selfRefString);
|
||||
const char* self = Toy_toCString(selfRefString);
|
||||
|
||||
//allocate buffer space for the result
|
||||
char* result = TOY_ALLOCATE(char, Toy_lengthRefString(selfRefString) + 1);
|
||||
|
||||
//set each new character
|
||||
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) {
|
||||
for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
|
||||
result[i] = toupper(self[i]);
|
||||
}
|
||||
result[Toy_lengthRefString(selfRefString)] = '\0'; //end the string
|
||||
@@ -675,10 +675,10 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
int bufferEnd = Toy_lengthRefString(selfRefString);
|
||||
|
||||
//for each character in self, check it against each character in trimChars - on a fail, go to end
|
||||
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) {
|
||||
for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
|
||||
int trimIndex = 0;
|
||||
|
||||
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
//there is a match - DON'T increment anymore
|
||||
if (Toy_toCString(selfRefString)[i] == Toy_toCString(trimCharsRefString)[trimIndex]) {
|
||||
break;
|
||||
@@ -688,7 +688,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
}
|
||||
|
||||
//if the match is found, increment buffer begin
|
||||
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
bufferBegin++;
|
||||
continue;
|
||||
}
|
||||
@@ -701,7 +701,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
for (int i = Toy_lengthRefString(selfRefString); i >= 0; i--) {
|
||||
int trimIndex = 0;
|
||||
|
||||
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
//there is a match - DON'T increment anymore
|
||||
if (Toy_toCString(selfRefString)[i-1] == Toy_toCString(trimCharsRefString)[trimIndex]) {
|
||||
break;
|
||||
@@ -711,7 +711,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
}
|
||||
|
||||
//if the match is found, increment buffer begin
|
||||
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
bufferEnd--;
|
||||
continue;
|
||||
}
|
||||
@@ -786,10 +786,10 @@ static int nativeTrimBegin(Toy_Interpreter* interpreter, Toy_LiteralArray* argum
|
||||
int bufferEnd = Toy_lengthRefString(selfRefString);
|
||||
|
||||
//for each character in self, check it against each character in trimChars - on a fail, go to end
|
||||
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) {
|
||||
for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
|
||||
int trimIndex = 0;
|
||||
|
||||
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
//there is a match - DON'T increment anymore
|
||||
if (Toy_toCString(selfRefString)[i] == Toy_toCString(trimCharsRefString)[trimIndex]) {
|
||||
break;
|
||||
@@ -799,7 +799,7 @@ static int nativeTrimBegin(Toy_Interpreter* interpreter, Toy_LiteralArray* argum
|
||||
}
|
||||
|
||||
//if the match is found, increment buffer begin
|
||||
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
bufferBegin++;
|
||||
continue;
|
||||
}
|
||||
@@ -874,10 +874,10 @@ static int nativeTrimEnd(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
|
||||
int bufferEnd = Toy_lengthRefString(selfRefString);
|
||||
|
||||
//again, from the back
|
||||
for (int i = Toy_lengthRefString(selfRefString); i >= 0; i--) {
|
||||
for (int i = (int)Toy_lengthRefString(selfRefString); i >= 0; i--) {
|
||||
int trimIndex = 0;
|
||||
|
||||
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
//there is a match - DON'T increment anymore
|
||||
if (Toy_toCString(selfRefString)[i-1] == Toy_toCString(trimCharsRefString)[trimIndex]) {
|
||||
break;
|
||||
@@ -887,7 +887,7 @@ static int nativeTrimEnd(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
|
||||
}
|
||||
|
||||
//if the match is found, increment buffer begin
|
||||
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) {
|
||||
if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
|
||||
bufferEnd--;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
typedef struct Toy_Runner {
|
||||
Toy_Interpreter interpreter;
|
||||
unsigned char* bytecode;
|
||||
const unsigned char* bytecode;
|
||||
size_t size;
|
||||
|
||||
bool dirty;
|
||||
@@ -43,12 +43,12 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
||||
Toy_freeLiteral(drivePathLiteral);
|
||||
|
||||
//use raw types - easier
|
||||
char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral));
|
||||
const char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral));
|
||||
int filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral));
|
||||
|
||||
//load and compile the bytecode
|
||||
size_t fileSize = 0;
|
||||
char* source = Toy_readFile(filePath, &fileSize);
|
||||
const char* source = Toy_readFile(filePath, &fileSize);
|
||||
|
||||
if (!source) {
|
||||
interpreter->errorOutput("Failed to load source file\n");
|
||||
@@ -56,7 +56,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char* bytecode = Toy_compileString(source, &fileSize);
|
||||
const unsigned char* bytecode = Toy_compileString(source, &fileSize);
|
||||
free((void*)source);
|
||||
|
||||
if (!bytecode) {
|
||||
@@ -105,7 +105,7 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr
|
||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(drivePathLiteral));
|
||||
|
||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
||||
int driveLength = 0;
|
||||
size_t driveLength = 0;
|
||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
||||
interpreter->errorOutput("Incorrect drive path format given to loadScriptBytecode\n");
|
||||
@@ -492,7 +492,7 @@ static int nativeCheckScriptDirty(Toy_Interpreter* interpreter, Toy_LiteralArray
|
||||
|
||||
//call the hook
|
||||
typedef struct Natives {
|
||||
char* name;
|
||||
const char* name;
|
||||
Toy_NativeFn fn;
|
||||
} Natives;
|
||||
|
||||
@@ -585,7 +585,7 @@ Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* dr
|
||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(*drivePathLiteral));
|
||||
|
||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
||||
int driveLength = 0;
|
||||
size_t driveLength = 0;
|
||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
||||
interpreter->errorOutput("Incorrect drive path format given to Toy_getFilePathLiteral\n");
|
||||
|
||||
@@ -151,11 +151,11 @@ int main(int argc, const char* argv[]) {
|
||||
//compile source file
|
||||
if (Toy_commandLine.compilefile && Toy_commandLine.outfile) {
|
||||
size_t size = 0;
|
||||
char* source = Toy_readFile(Toy_commandLine.compilefile, &size);
|
||||
const char* source = Toy_readFile(Toy_commandLine.compilefile, &size);
|
||||
if (!source) {
|
||||
return 1;
|
||||
}
|
||||
unsigned char* tb = Toy_compileString(source, &size);
|
||||
const unsigned char* tb = Toy_compileString(source, &size);
|
||||
if (!tb) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
//IO functions
|
||||
char* Toy_readFile(char* path, size_t* fileSize) {
|
||||
const char* Toy_readFile(const char* path, size_t* fileSize) {
|
||||
FILE* file = fopen(path, "rb");
|
||||
|
||||
if (file == NULL) {
|
||||
@@ -49,7 +49,7 @@ char* Toy_readFile(char* path, size_t* fileSize) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int Toy_writeFile(char* path, unsigned char* bytes, size_t size) {
|
||||
int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size) {
|
||||
FILE* file = fopen(path, "wb");
|
||||
|
||||
if (file == NULL) {
|
||||
@@ -70,7 +70,7 @@ int Toy_writeFile(char* path, unsigned char* bytes, size_t size) {
|
||||
}
|
||||
|
||||
//repl functions
|
||||
unsigned char* Toy_compileString(char* source, size_t* size) {
|
||||
const unsigned char* Toy_compileString(const char* source, size_t* size) {
|
||||
Toy_Lexer lexer;
|
||||
Toy_Parser parser;
|
||||
Toy_Compiler compiler;
|
||||
@@ -96,7 +96,7 @@ unsigned char* Toy_compileString(char* source, size_t* size) {
|
||||
}
|
||||
|
||||
//get the bytecode dump
|
||||
unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
|
||||
const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
|
||||
|
||||
//cleanup
|
||||
Toy_freeCompiler(&compiler);
|
||||
@@ -107,7 +107,7 @@ unsigned char* Toy_compileString(char* source, size_t* size) {
|
||||
return tb;
|
||||
}
|
||||
|
||||
void Toy_runBinary(unsigned char* tb, size_t size) {
|
||||
void Toy_runBinary(const unsigned char* tb, size_t size) {
|
||||
Toy_Interpreter interpreter;
|
||||
Toy_initInterpreter(&interpreter);
|
||||
|
||||
@@ -122,9 +122,9 @@ void Toy_runBinary(unsigned char* tb, size_t size) {
|
||||
Toy_freeInterpreter(&interpreter);
|
||||
}
|
||||
|
||||
void Toy_runBinaryFile(char* fname) {
|
||||
void Toy_runBinaryFile(const char* fname) {
|
||||
size_t size = 0; //not used
|
||||
unsigned char* tb = (unsigned char*)Toy_readFile(fname, &size);
|
||||
const unsigned char* tb = (const unsigned char*)Toy_readFile(fname, &size);
|
||||
if (!tb) {
|
||||
return;
|
||||
}
|
||||
@@ -132,9 +132,9 @@ void Toy_runBinaryFile(char* fname) {
|
||||
//interpreter takes ownership of the binary data
|
||||
}
|
||||
|
||||
void Toy_runSource(char* source) {
|
||||
void Toy_runSource(const char* source) {
|
||||
size_t size = 0;
|
||||
unsigned char* tb = Toy_compileString(source, &size);
|
||||
const unsigned char* tb = Toy_compileString(source, &size);
|
||||
if (!tb) {
|
||||
return;
|
||||
}
|
||||
@@ -142,9 +142,9 @@ void Toy_runSource(char* source) {
|
||||
Toy_runBinary(tb, size);
|
||||
}
|
||||
|
||||
void Toy_runSourceFile(char* fname) {
|
||||
void Toy_runSourceFile(const char* fname) {
|
||||
size_t size = 0; //not used
|
||||
char* source = Toy_readFile(fname, &size);
|
||||
const char* source = Toy_readFile(fname, &size);
|
||||
if (!source) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
#include "toy_common.h"
|
||||
|
||||
char* Toy_readFile(char* path, size_t* fileSize);
|
||||
int Toy_writeFile(char* path, unsigned char* bytes, size_t size);
|
||||
const char* Toy_readFile(const char* path, size_t* fileSize);
|
||||
int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size);
|
||||
|
||||
unsigned char* Toy_compileString(char* source, size_t* size);
|
||||
const unsigned char* Toy_compileString(const char* source, size_t* size);
|
||||
|
||||
void Toy_runBinary(unsigned char* tb, size_t size);
|
||||
void Toy_runBinaryFile(char* fname);
|
||||
void Toy_runSource(char* source);
|
||||
void Toy_runSourceFile(char* fname);
|
||||
void Toy_runBinary(const unsigned char* tb, size_t size);
|
||||
void Toy_runBinaryFile(const char* fname);
|
||||
void Toy_runSource(const char* source);
|
||||
void Toy_runSourceFile(const char* fname);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user