From 08b401f33708e77de3cd0a56ef2d3125a5b81316 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 1 Apr 2008 17:24:43 -0600 Subject: [PATCH] implement GetStringLength, GetStringChars, and ReleaseStringChars --- src/jnienv.cpp | 34 ++++++++++++++++++++++++++++++++++ src/machine.cpp | 18 ++++++++++++++++++ src/machine.h | 3 +++ 3 files changed, 55 insertions(+) diff --git a/src/jnienv.cpp b/src/jnienv.cpp index c9f983bdef..536c59ed2e 100644 --- a/src/jnienv.cpp +++ b/src/jnienv.cpp @@ -87,6 +87,35 @@ GetEnv(Machine* m, Thread** t, jint version) } } +jsize JNICALL +GetStringLength(Thread* t, jstring s) +{ + ENTER(t, Thread::ActiveState); + + return stringLength(t, *s); +} + +const jchar* JNICALL +GetStringChars(Thread* t, jstring s, jboolean* isCopy) +{ + ENTER(t, Thread::ActiveState); + + jchar* chars = static_cast + (t->m->heap->allocate((stringLength(t, *s) + 1) * sizeof(jchar), false)); + stringChars(t, *s, reinterpret_cast(chars)); + + if (isCopy) *isCopy = true; + return chars; +} + +void JNICALL +ReleaseStringChars(Thread* t, jstring s, const jchar* chars) +{ + ENTER(t, Thread::ActiveState); + + t->m->heap->free(chars, (stringLength(t, *s) + 1) * sizeof(jchar), false); +} + jsize JNICALL GetStringUTFLength(Thread* t, jstring s) { @@ -111,6 +140,8 @@ GetStringUTFChars(Thread* t, jstring s, jboolean* isCopy) void JNICALL ReleaseStringUTFChars(Thread* t, jstring s, const char* chars) { + ENTER(t, Thread::ActiveState); + t->m->heap->free(chars, stringLength(t, *s) + 1, false); } @@ -1854,6 +1885,9 @@ populateJNITables(JavaVMVTable* vmTable, JNIEnvVTable* envTable) memset(envTable, 0, sizeof(JNIEnvVTable)); + envTable->GetStringLength = ::GetStringLength; + envTable->GetStringChars = ::GetStringChars; + envTable->ReleaseStringChars = ::ReleaseStringChars; envTable->GetStringUTFLength = ::GetStringUTFLength; envTable->GetStringUTFChars = ::GetStringUTFChars; envTable->ReleaseStringUTFChars = ::ReleaseStringUTFChars; diff --git a/src/machine.cpp b/src/machine.cpp index e6f391ee32..ec82898214 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -2152,6 +2152,24 @@ stringChars(Thread* t, object string, char* chars) chars[stringLength(t, string)] = 0; } +void +stringChars(Thread* t, object string, wchar_t* chars) +{ + object data = stringData(t, string); + if (objectClass(t, data) + == arrayBody(t, t->m->types, Machine::ByteArrayType)) + { + for (unsigned i = 0; i < stringLength(t, string); ++i) { + chars[i] = byteArrayBody(t, data, stringOffset(t, string) + i); + } + } else { + memcpy(chars, + &charArrayBody(t, data, stringOffset(t, string)), + stringLength(t, string) * sizeof(wchar_t)); + } + chars[stringLength(t, string)] = 0; +} + bool isAssignableFrom(Thread* t, object a, object b) { diff --git a/src/machine.h b/src/machine.h index 33af90fa2e..5758e086a3 100644 --- a/src/machine.h +++ b/src/machine.h @@ -1702,6 +1702,9 @@ makeString(Thread* t, const char* format, ...); void stringChars(Thread* t, object string, char* chars); +void +stringChars(Thread* t, object string, wchar_t* chars); + bool isAssignableFrom(Thread* t, object a, object b);