2007-07-06 23:50:26 +00:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "machine.h"
|
2007-07-07 18:09:16 +00:00
|
|
|
#include "run.h"
|
2007-07-06 15:24:06 +00:00
|
|
|
|
|
|
|
namespace vm {
|
|
|
|
namespace builtin {
|
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
jstring
|
|
|
|
toString(Thread* t, jobject this_)
|
|
|
|
{
|
|
|
|
object s = makeString
|
|
|
|
(t, "%s@%p",
|
|
|
|
&byteArrayBody(t, className(t, objectClass(t, *this_)), 0),
|
|
|
|
*this_);
|
|
|
|
|
|
|
|
return pushReference(t, s);
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:46:08 +00:00
|
|
|
jclass
|
|
|
|
getClass(Thread* t, jobject this_)
|
|
|
|
{
|
|
|
|
return pushReference(t, objectClass(t, *this_));
|
|
|
|
}
|
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
void
|
|
|
|
wait(Thread* t, jobject this_, jlong milliseconds)
|
|
|
|
{
|
|
|
|
vm::wait(t, *this_, milliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
notify(Thread* t, jobject this_)
|
|
|
|
{
|
|
|
|
vm::notify(t, *this_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
notifyAll(Thread* t, jobject this_)
|
|
|
|
{
|
|
|
|
vm::notifyAll(t, *this_);
|
|
|
|
}
|
|
|
|
|
2007-07-11 01:38:06 +00:00
|
|
|
void
|
|
|
|
sleep(Thread* t, jlong milliseconds)
|
|
|
|
{
|
|
|
|
if (milliseconds == 0) milliseconds = INT64_MAX;
|
|
|
|
|
|
|
|
ENTER(t, Thread::IdleState);
|
|
|
|
|
|
|
|
t->vm->system->sleep(milliseconds);
|
|
|
|
}
|
|
|
|
|
2007-07-06 15:24:06 +00:00
|
|
|
void
|
2007-07-06 23:18:40 +00:00
|
|
|
loadLibrary(Thread* t, jstring nameString)
|
2007-07-06 15:24:06 +00:00
|
|
|
{
|
|
|
|
if (LIKELY(nameString)) {
|
|
|
|
object n = *nameString;
|
|
|
|
char name[stringLength(t, n) + 1];
|
2007-07-08 01:06:32 +00:00
|
|
|
stringChars(t, n, name);
|
2007-07-06 15:24:06 +00:00
|
|
|
|
|
|
|
System::Library* lib;
|
|
|
|
if (LIKELY(t->vm->system->success
|
|
|
|
(t->vm->system->load(&lib, name, t->vm->libraries))))
|
|
|
|
{
|
|
|
|
t->vm->libraries = lib;
|
|
|
|
} else {
|
|
|
|
object message = makeString(t, "library not found: %s", name);
|
|
|
|
t->exception = makeRuntimeException(t, message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t->exception = makeNullPointerException(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
void
|
|
|
|
arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset,
|
|
|
|
jint length)
|
2007-07-06 15:24:06 +00:00
|
|
|
{
|
2007-07-07 23:47:35 +00:00
|
|
|
if (LIKELY(src and dst)) {
|
|
|
|
object s = *src;
|
|
|
|
object d = *dst;
|
|
|
|
|
|
|
|
if (LIKELY(objectClass(t, s) == objectClass(t, d))) {
|
|
|
|
unsigned elementSize = classArrayElementSize(t, objectClass(t, s));
|
|
|
|
|
|
|
|
if (LIKELY(elementSize)) {
|
2007-07-11 01:38:06 +00:00
|
|
|
unsigned offset = 1;
|
2007-07-07 23:47:35 +00:00
|
|
|
|
|
|
|
if (objectClass(t, s)
|
|
|
|
== arrayBody(t, t->vm->types, Machine::ObjectArrayType))
|
|
|
|
{
|
|
|
|
if (LIKELY(objectArrayElementClass(t, s)
|
|
|
|
== objectArrayElementClass(t, d)))
|
|
|
|
{
|
2007-07-11 01:38:06 +00:00
|
|
|
offset = 2;
|
2007-07-07 23:47:35 +00:00
|
|
|
} else {
|
|
|
|
t->exception = makeArrayStoreException(t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-10 01:43:43 +00:00
|
|
|
intptr_t sl = cast<uintptr_t>(s, offset * BytesPerWord);
|
|
|
|
intptr_t dl = cast<uintptr_t>(d, offset * BytesPerWord);
|
2007-07-07 23:47:35 +00:00
|
|
|
if (LIKELY(srcOffset >= 0 and srcOffset + length <= sl and
|
2007-07-11 01:38:06 +00:00
|
|
|
dstOffset >= 0 and dstOffset + length <= dl))
|
2007-07-07 23:47:35 +00:00
|
|
|
{
|
2007-07-11 01:38:06 +00:00
|
|
|
uint8_t* sbody = &cast<uint8_t>(s, (offset + 1) * BytesPerWord);
|
|
|
|
uint8_t* dbody = &cast<uint8_t>(d, (offset + 1) * BytesPerWord);
|
|
|
|
memcpy(dbody + (dstOffset * elementSize),
|
|
|
|
sbody + (srcOffset * elementSize),
|
2007-07-07 23:47:35 +00:00
|
|
|
length * elementSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t->exception = makeNullPointerException(t);
|
|
|
|
return;
|
|
|
|
}
|
2007-07-06 15:24:06 +00:00
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
t->exception = makeArrayStoreException(t);
|
2007-07-06 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jarray
|
2007-07-06 23:18:40 +00:00
|
|
|
trace(Thread* t, jint skipCount)
|
2007-07-06 15:24:06 +00:00
|
|
|
{
|
|
|
|
int frame = t->frame;
|
|
|
|
while (skipCount-- and frame >= 0) {
|
|
|
|
frame = frameNext(t, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (methodClass(t, frameMethod(t, frame))
|
|
|
|
== arrayBody(t, t->vm->types, Machine::ThrowableType))
|
|
|
|
{
|
|
|
|
// skip Throwable constructors
|
|
|
|
while (strcmp(reinterpret_cast<const int8_t*>("<init>"),
|
|
|
|
&byteArrayBody(t, methodName(t, frameMethod(t, frame)), 0))
|
|
|
|
== 0)
|
|
|
|
{
|
|
|
|
frame = frameNext(t, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pushReference(t, makeTrace(t, frame));
|
|
|
|
}
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
void
|
|
|
|
start(Thread* t, jobject this_)
|
|
|
|
{
|
|
|
|
Thread* p = reinterpret_cast<Thread*>(threadPeer(t, *this_));
|
|
|
|
if (p) {
|
|
|
|
object message = makeString(t, "thread already started");
|
|
|
|
t->exception = makeIllegalStateException(t, message);
|
|
|
|
} else {
|
|
|
|
p = new (t->vm->system->allocate(sizeof(Thread)))
|
|
|
|
Thread(t->vm, t->vm->system, *this_, t);
|
|
|
|
|
|
|
|
enter(p, Thread::ActiveState);
|
|
|
|
|
|
|
|
class Runnable: public System::Runnable {
|
|
|
|
public:
|
2007-07-11 01:38:06 +00:00
|
|
|
Runnable(System* s, Thread* t): s(s), t(t) { }
|
2007-07-07 18:09:16 +00:00
|
|
|
|
|
|
|
virtual void run(System::Thread* st) {
|
|
|
|
t->systemThread = st;
|
|
|
|
|
|
|
|
vm::run(t, "java/lang/Thread", "run", "()V", t->javaThread);
|
|
|
|
|
|
|
|
t->exit();
|
|
|
|
}
|
|
|
|
|
2007-07-11 01:38:06 +00:00
|
|
|
virtual void dispose() {
|
|
|
|
s->free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
System* s;
|
2007-07-07 18:09:16 +00:00
|
|
|
Thread* t;
|
2007-07-11 01:38:06 +00:00
|
|
|
}* r = new (t->vm->system->allocate(sizeof(Runnable)))
|
|
|
|
Runnable(t->vm->system, p);
|
2007-07-07 18:09:16 +00:00
|
|
|
|
2007-07-11 01:38:06 +00:00
|
|
|
if (not t->vm->system->success(t->vm->system->start(r))) {
|
2007-07-07 18:09:16 +00:00
|
|
|
p->exit();
|
|
|
|
|
|
|
|
object message = makeString(t, "unable to start native thread");
|
|
|
|
t->exception = makeRuntimeException(t, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-06 15:24:06 +00:00
|
|
|
void
|
|
|
|
populate(Thread* t, object map)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
const char* key;
|
|
|
|
void* value;
|
|
|
|
} builtins[] = {
|
|
|
|
{ "Java_java_lang_Object_toString",
|
|
|
|
reinterpret_cast<void*>(toString) },
|
2007-07-12 23:46:08 +00:00
|
|
|
{ "Java_java_lang_Object_getClass",
|
|
|
|
reinterpret_cast<void*>(getClass) },
|
2007-07-07 23:47:35 +00:00
|
|
|
{ "Java_java_lang_Object_wait",
|
|
|
|
reinterpret_cast<void*>(wait) },
|
|
|
|
{ "Java_java_lang_Object_notify",
|
|
|
|
reinterpret_cast<void*>(notify) },
|
|
|
|
{ "Java_java_lang_Object_notifyAll",
|
|
|
|
reinterpret_cast<void*>(notifyAll) },
|
2007-07-11 01:38:06 +00:00
|
|
|
{ "Java_java_lang_Thread_sleep",
|
|
|
|
reinterpret_cast<void*>(sleep) },
|
2007-07-06 15:24:06 +00:00
|
|
|
{ "Java_java_lang_System_loadLibrary",
|
|
|
|
reinterpret_cast<void*>(loadLibrary) },
|
2007-07-07 23:47:35 +00:00
|
|
|
{ "Java_java_lang_System_arraycopy",
|
|
|
|
reinterpret_cast<void*>(arraycopy) },
|
2007-07-06 15:24:06 +00:00
|
|
|
{ "Java_java_lang_Throwable_trace",
|
|
|
|
reinterpret_cast<void*>(trace) },
|
2007-07-07 18:09:16 +00:00
|
|
|
{ "Java_java_lang_Thread_start",
|
|
|
|
reinterpret_cast<void*>(start) },
|
2007-07-06 15:24:06 +00:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned i = 0; builtins[i].key; ++i) {
|
|
|
|
object key = makeByteArray(t, builtins[i].key);
|
|
|
|
PROTECT(t, key);
|
|
|
|
object value = makePointer(t, builtins[i].value);
|
|
|
|
|
|
|
|
hashMapInsert(t, map, key, value, byteArrayHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace builtin
|
|
|
|
} // namespace vm
|