null problems are now not more

This commit is contained in:
nilspolek 2024-01-28 00:14:19 +01:00
parent 9155216fd2
commit f3829559d4
9 changed files with 20 additions and 12 deletions

1
SDA.c
View File

@ -15,6 +15,7 @@ ObjRef getSDA(int i, struct sda s) {
} }
void setSDA(int point, ObjRef val, struct sda s) { void setSDA(int point, ObjRef val, struct sda s) {
if (val == NULL) perror("Value is null");
s.sda[point] = val; s.sda[point] = val;
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
njvm.o

Binary file not shown.

View File

@ -20,29 +20,35 @@ ObjRef newRecord(int size){
return record; return record;
} }
int getSize(ObjRef arr){ int getSize(ObjRef arr){
if(arr == NULL) return 0;
return GET_ELEMENT_COUNT(arr); return GET_ELEMENT_COUNT(arr);
} }
ObjRef getField(ObjRef arr, int point){ ObjRef getField(ObjRef arr, int point){
if(arr == NULL) perror("Record is null");
if(0 > point || point > getSize(arr)){ if(0 > point || point > getSize(arr)){
printf("Index %i out of bounds for length %i\n",point, getSize(arr)); printf("Index %i out of bounds for length %i\n",point, getSize(arr));
} }
return *(ObjRef *)GET_REFS_PTR(arr)[point]->data; return *(ObjRef *)GET_REFS_PTR(arr)[point]->data;
} }
void setField(ObjRef arr, int point, ObjRef value){ void setField(ObjRef arr, int point, ObjRef value){
bool isNull = false;
if(value == NULL) isNull = true;
if(0 > point || point >= getSize(arr)){ if(0 > point || point >= getSize(arr)){
printf("Index %i out of bounds for length %i\n",point, getSize(arr)); printf("Index %i out of bounds for length %i\n",point, getSize(arr));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(arr == NULL) perror("Record is null");
if(IS_PRIMITIVE(arr)) perror("Record is a primitive"); if(IS_PRIMITIVE(arr)) perror("Record is a primitive");
if(IS_PRIMITIVE(value)){ int size;
int size = value->size; if (isNull) size = sizeof(void *);
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc"); else {
GET_REFS_PTR(arr)[point]->size = size; if (IS_PRIMITIVE(value))
}else{ size = value->size;
int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *)); else
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc"); size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
GET_REFS_PTR(arr)[point] ->size = size;
} }
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
GET_REFS_PTR(arr)[point] ->size = size;
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
} }

View File

@ -8,9 +8,6 @@
#include <stdlib.h> #include <stdlib.h>
#include "stackslot.c" #include "stackslot.c"
struct stack { struct stack {
int* size; int* size;
int* current; int* current;
@ -26,6 +23,8 @@ void printStack(struct stack stack, int fp) {
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
for (int i = *stack.current -1; i >= 0; --i) { for (int i = *stack.current -1; i >= 0; --i) {
printf("%i\t",i); printf("%i\t",i);
if(stack.stack[i].u.objRef == NULL) printf("|NULL|");
else
if(stack.stack[i].isObjRef){ if(stack.stack[i].isObjRef){
printf("|%p|", (void *)stack.stack[i].u.objRef); printf("|%p|", (void *)stack.stack[i].u.objRef);
if(stack.stack[i].u.objRef->size == sizeof(int)) if(stack.stack[i].u.objRef->size == sizeof(int))

View File

@ -30,6 +30,7 @@ ObjRef getIntObj(int val) {
} }
int getValFromIntObj(ObjRef iref) { int getValFromIntObj(ObjRef iref) {
if (iref == NULL) perror("ObjRef is null");
return *(int *) iref->data; return *(int *) iref->data;
} }
@ -41,6 +42,7 @@ int getIntValfromStackSlot(StackSlot s) {
} }
void setValIntObj(ObjRef iref, int val) { void setValIntObj(ObjRef iref, int val) {
if (iref == NULL) perror("ObjRef is null");
iref->size = sizeof(int)+sizeof(ObjRef); iref->size = sizeof(int)+sizeof(ObjRef);
*(int *) iref->data = val; *(int *) iref->data = val;
} }