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

View File

@ -8,9 +8,6 @@
#include <stdlib.h>
#include "stackslot.c"
struct stack {
int* size;
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);
for (int i = *stack.current -1; i >= 0; --i) {
printf("%i\t",i);
if(stack.stack[i].u.objRef == NULL) printf("|NULL|");
else
if(stack.stack[i].isObjRef){
printf("|%p|", (void *)stack.stack[i].u.objRef);
if(stack.stack[i].u.objRef->size == sizeof(int))

View File

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