📄 nativecore.c
字号:
* TYPE: virtual native function * OVERVIEW: print class, message and stack trace * INTERFACE (operand stack manipulation): * parameters: this * stream to print to (unused) * returns: void *====================================================================*/void Java_java_lang_Throwable_printStackTrace0() { START_TEMPORARY_ROOTS OBJECT unused = popStackAsType(OBJECT); DECLARE_TEMPORARY_ROOT(THROWABLE_INSTANCE, throwable, popStackAsType(THROWABLE_INSTANCE)); unused = unused; /* If PRINT_BACKTRACE is 0, this will print a message saying * that stack tracing isn't available. */ printExceptionStackTrace(&throwable); END_TEMPORARY_ROOTS}/*========================================================================= * Native functions of classes java.util.String and StringBuffer *=======================================================================*//*========================================================================= * FUNCTION: charAt() * CLASS: java.lang.String * TYPE: virtual native function * OVERVIEW: returns the character at a particular index * INTERFACE * parameters: this, index * returns: character *====================================================================*/void Java_java_lang_String_charAt() { long index = popStack(); STRING_INSTANCE this = popStackAsType(STRING_INSTANCE); long length = this->length; if (index < 0 || index >= length) { raiseException("java/lang/StringIndexOutOfBoundsException"); } else { long result = ((unsigned short *)(this->array->sdata))[this->offset + index]; pushStack(result); }}/*========================================================================= * FUNCTION: string_indexOf * CLASS: java.lang.String * TYPE: helper function * OVERVIEW: this function is common code used by both * String.indexOf(ch) and String.indexOf(ch, from) * INTERFACE * parameters: fromIndex * returns: index *====================================================================*/static void string_indexOf(long fromIndex) { long ch = popStack(); STRING_INSTANCE this = popStackAsType(STRING_INSTANCE); long offset = this->offset; long length = this->length; long result = -1; long i; if (fromIndex < length) { SHORTARRAY array = this->array; /* Max is the largest position in the underlying char array */ long max = offset + length; for (i = offset + fromIndex; i < max; i++) { if (((unsigned short *)(array->sdata))[i] == ch) { /* i is the position in the underlying char array. We must * normalize it to the position in the String */ result = i - offset; break; } } } pushStack(result);}/*========================================================================= * FUNCTION: indexOf(I) * CLASS: java.lang.String * TYPE: virtual native function * OVERVIEW: returns index of a character * parameters: this * character * returns: index of the first occurrence of that character *====================================================================*/void Java_java_lang_String_indexOf__I(void) { string_indexOf(0);}/*========================================================================= * FUNCTION: indexOf(II) * CLASS: java.lang.String * TYPE: virtual native function * OVERVIEW: returns index of a character * parameters: this * character * fromIndex: first location to look for that character * returns: index of the first occurrence of that character *====================================================================*/void Java_java_lang_String_indexOf__II(void) { long fromIndex = popStackAsType(long); if (fromIndex < 0) { fromIndex = 0; } string_indexOf(fromIndex);}/*========================================================================= * FUNCTION: equals(Object) * CLASS: java.lang.String * TYPE: virtual native function * OVERVIEW: returns index of a character * parameters: this * otherObject * returns: returns TRUE if the other object is a String with * the identical characters *==================================================================== */void Java_java_lang_String_equals() { OBJECT otherX = popStackAsType(OBJECT); STRING_INSTANCE this = popStackAsType(STRING_INSTANCE); bool_t result = FALSE; if ((OBJECT)this == otherX) { /* This case happens often enough that we should handle it quickly */ result = TRUE; } else if ((otherX != NULL) && (otherX->ofClass == (CLASS)JavaLangString)){ /* The other object must be a java.lang.String */ STRING_INSTANCE other = (STRING_INSTANCE)otherX; unsigned long length = this->length; if (length == other->length) { /* Both objects must have the same length */ if (0 == memcmp(&this->array->sdata[this->offset], &other->array->sdata[other->offset], length * sizeof(this->array->sdata[0]))) { /* Both objects must have the same characters */ result = TRUE; } } } pushStack(result);}/*========================================================================= * FUNCTION: append(String) * CLASS: java.lang.StringBuffer * TYPE: virtual native function * OVERVIEW: appends a String to a StringBuffer * parameters: this * String * returns: the "this" argument *==================================================================== */typedef struct stringBufferInstanceStruct { COMMON_OBJECT_INFO(INSTANCE_CLASS) SHORTARRAY array; long count; bool_t shared;} *STRINGBUFFER_INSTANCE, **STRINGBUFFER_HANDLE;static void expandStringBufferCapacity(STRINGBUFFER_HANDLE, int size);void Java_java_lang_StringBuffer_append__Ljava_lang_String_2(void) { STRING_INSTANCE string = popStackAsType(STRING_INSTANCE); STRINGBUFFER_INSTANCE this = popStackAsType(STRINGBUFFER_INSTANCE); unsigned long count, newCount, stringLength; if (string == NULL) { START_TEMPORARY_ROOTS /* This seems to be the right thing to do */ DECLARE_TEMPORARY_ROOT(STRINGBUFFER_INSTANCE, thisX, this); string = (STRING_INSTANCE)internString("null", 4); this = thisX; END_TEMPORARY_ROOTS } count = this->count; stringLength = string->length; newCount = count + stringLength; if (newCount > this->array->length) { /* We have to expand the string buffer array. This may GC */ START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(STRING_INSTANCE, stringX, string); DECLARE_TEMPORARY_ROOT(STRINGBUFFER_INSTANCE, thisX, this); expandStringBufferCapacity(&thisX, newCount); string = stringX; this = thisX; END_TEMPORARY_ROOTS } if (newCount <= this->array->length) { memcpy(&this->array->sdata[count], &string->array->sdata[string->offset], stringLength * sizeof(short)); this->count = newCount; pushStackAsType(STRINGBUFFER_INSTANCE, this); } else { /* We will have gotten an OutOfMemoryException from the * expandStringBufferCapacity */ }}/*========================================================================= * FUNCTION: append(int) * CLASS: java.lang.StringBuffer * TYPE: virtual native function * OVERVIEW: appends an integer to a string buffer * parameters: this * int * returns: the "this" argument *==================================================================== */voidJava_java_lang_StringBuffer_append__I(void) { long value = popStack(); STRINGBUFFER_INSTANCE this = popStackAsType(STRINGBUFFER_INSTANCE); unsigned long count, newCount, stringLength; char buffer[16]; /* 12 is enough, but I'm paranoid */ long i; /* Put the value into a buffer */ sprintf(buffer, "%ld", value); count = this->count; stringLength = strlen(buffer); newCount = count + stringLength; if (newCount > this->array->length) { /* Need to expand the string buffer */ START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(STRINGBUFFER_INSTANCE, thisX, this); expandStringBufferCapacity(&thisX, newCount); this = thisX; END_TEMPORARY_ROOTS } if (newCount <= this->array->length) { for (i = stringLength; --i >= 0; ) { this->array->sdata[count + i] = ((unsigned char *)buffer)[i]; } this->count = newCount; pushStackAsType(STRINGBUFFER_INSTANCE, this); } else { /* We will have gotten an OutOfMemoryException from the * expandStringBufferCapacity */ }}/*========================================================================= * FUNCTION: toString() * CLASS: java.lang.StringBuffer * TYPE: virtual native function * OVERVIEW: returns the String representation of a StringBuffer * parameters: this * returns: the corresponding String *==================================================================== */void Java_java_lang_StringBuffer_toString(void) { STRINGBUFFER_INSTANCE this = popStackAsType(STRINGBUFFER_INSTANCE); STRING_INSTANCE result; START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(STRINGBUFFER_INSTANCE, thisX, this); result = (STRING_INSTANCE)instantiate(JavaLangString); this = thisX; END_TEMPORARY_ROOTS if (result != NULL) { this->shared = TRUE; result->offset = 0; result->length = this->count; result->array = this->array; pushStackAsType(STRING_INSTANCE, result); } else { /* We will already have thrown an appropriate error */ }}static void expandStringBufferCapacity(STRINGBUFFER_HANDLE sbH, int newLength){ STRINGBUFFER_INSTANCE sb = unhand(sbH); long oldLength = sb->array->length; SHORTARRAY newArray; if (newLength < oldLength * 2) { newLength = oldLength * 2; } newArray = (SHORTARRAY)instantiateArray(PrimitiveArrayClasses[T_CHAR], newLength); if (newArray != NULL) { sb = unhand(sbH); /* in case of GC */ memcpy(&newArray->sdata[0], &sb->array->sdata[0], sb->count * sizeof(short)); sb->array = newArray; sb->shared = FALSE; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -