Add ClassCastException thrower, small refactoring. (#67)

This commit is contained in:
Nikolay Igotti
2016-11-16 18:34:35 +03:00
committed by GitHub
parent 16a44ef765
commit 060163d1c4
5 changed files with 38 additions and 18 deletions
+14 -2
View File
@@ -1,4 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "Assert.h"
#include "Exceptions.h"
@@ -10,7 +12,7 @@ void ThrowNullPointerException() {
void* pc = __builtin_return_address(0);
char message[100];
snprintf(message, sizeof(message), "NullPointerException at %p", pc);
fprintf(stderr, "%s\n", message);
write(STDOUT_FILENO, message, strlen(message));
// TODO: throw it for real.
RuntimeAssert(false, "Throwing is unsupported");
}
@@ -19,11 +21,21 @@ void ThrowArrayIndexOutOfBoundsException() {
void* pc = __builtin_return_address(0);
char message[100];
snprintf(message, sizeof(message), "ArrayIndexOutOfBoundsException at %p", pc);
fprintf(stderr, "%s\n", message);
write(STDOUT_FILENO, message, strlen(message));
// TODO: throw it for real.
RuntimeAssert(false, "Throwing is unsupported");
}
void ThrowClassCastException() {
void* pc = __builtin_return_address(0);
char message[100];
snprintf(message, sizeof(message), "ClassCastException at %p", pc);
write(STDOUT_FILENO, message, strlen(message));
// TODO: throw it for real.
RuntimeAssert(false, "Throwing is unsupported");
}
#ifdef __cplusplus
} // extern "C"
#endif
+3 -1
View File
@@ -1,7 +1,7 @@
#ifndef RUNTIME_EXCEPTIONS_H
#define RUNTIME_EXCEPTIONS_H
#include <stdint.h>
#include "Types.h"
#ifdef __cplusplus
extern "C" {
@@ -11,6 +11,8 @@ void ThrowNullPointerException();
// Throws array index out of bounds exception.
// Context is evaluated from caller's address.
void ThrowArrayIndexOutOfBoundsException();
// Throws class cast exception.
void ThrowClassCastException();
#ifdef __cplusplus
} // extern "C"
+6
View File
@@ -85,6 +85,12 @@ inline void* AddressOfElementAt(ArrayHeader* obj, int32_t index) {
obj->type_info()->instanceSize_ * index;
}
inline const void* AddressOfElementAt(const ArrayHeader* obj, int32_t index) {
// Instance size is negative.
return reinterpret_cast<const uint8_t*>(obj + 1) -
obj->type_info()->instanceSize_ * index;
}
inline uint32_t ArrayDataSizeBytes(const ArrayHeader* obj) {
// Instance size is negative.
return -obj->type_info()->instanceSize_ * obj->count_;
-15
View File
@@ -1,23 +1,8 @@
#ifndef RUNTIME_NATIVES_H
#define RUNTIME_NATIVES_H
#include "Memory.h"
#include "Types.h"
typedef uint8_t KBoolean;
typedef int8_t KByte;
typedef uint16_t KChar;
// Note that it is signed.
typedef int16_t KShort;
typedef int32_t KInt;
typedef int64_t KLong;
typedef float KFloat;
typedef double KDouble;
typedef ObjHeader* KRef;
typedef const ObjHeader* KConstRef;
typedef const ArrayHeader* KString;
// Optimized versions not accessing type info.
inline KByte* ByteArrayAddressOfElementAt(ArrayHeader* obj, KInt index) {
return reinterpret_cast<KByte*>(obj + 1) + index;
+15
View File
@@ -1,8 +1,23 @@
#ifndef RUNTIME_TYPES_H
#define RUNTIME_TYPES_H
#include "Memory.h"
#include "TypeInfo.h"
// Note that almost all types are signed.
typedef uint8_t KBoolean;
typedef int8_t KByte;
typedef uint16_t KChar;
typedef int16_t KShort;
typedef int32_t KInt;
typedef int64_t KLong;
typedef float KFloat;
typedef double KDouble;
typedef ObjHeader* KRef;
typedef const ObjHeader* KConstRef;
typedef const ArrayHeader* KString;
#ifdef __cplusplus
extern "C" {
#endif