/** * private native void init0(int sender, int receiver); */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_links_Link_init0(void) { int sender; int receiver; rendezvous *rp; KNI_StartHandles(1); KNI_DeclareHandle(thisObj); sender = KNI_GetParameterAsInt(1); receiver = KNI_GetParameterAsInt(2); KNI_GetThisPointer(thisObj); rp = rp_create(sender, receiver); if (rp == NULL) { KNI_ThrowNew(midpOutOfMemoryError, NULL); } else { setNativePointer(thisObj, rp); rp_incref(rp); } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * private static native void getLinks0(Link[] linkarray); */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_links_LinkPortal_getLinks0(void) { int targetIsolate; jsize len; int i; KNI_StartHandles(2); KNI_DeclareHandle(linkArray); KNI_DeclareHandle(linkObj); targetIsolate = JVM_CurrentIsolateID(); KNI_GetParameterAsObject(1, linkArray); len = KNI_GetArrayLength(linkArray); if (portals != NULL) { if (portals[targetIsolate].count > 0) { rendezvous **rpp = portals[targetIsolate].rppa; for (i = 0; i < len; i++) { KNI_GetObjectArrayElement(linkArray, i, linkObj); setNativePointer(linkObj, rpp[i]); rp_incref(rpp[i]); } } portal_free(&portals[targetIsolate]); } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * public native void close(); */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_links_Link_close(void) { rendezvous *rp; KNI_StartHandles(1); KNI_DeclareHandle(thisObj); KNI_GetThisPointer(thisObj); rp = getNativePointer(thisObj); /* ignore if closed twice */ if (rp != NULL) { if (rp->sender == JVM_CurrentIsolateID() && rp->msg != INVALID_REFERENCE_ID) { /* we're the sender, make sure to clean out our message */ SNI_DeleteReference(rp->msg); rp->msg = INVALID_REFERENCE_ID; } rp->state = CLOSED; midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0); setNativePointer(thisObj, NULL); rp_decref(rp); } KNI_EndHandles(); KNI_ReturnVoid(); }
// Binding int w_LocalStorage_constructor(duk_context *ctx) { LocalStorage *inst = new LocalStorage(); setNativePointer(ctx, inst); // Create an object property to save data duk_push_this(ctx); /* this */ duk_push_object(ctx); /* this, emptyObj */ duk_put_prop_string(ctx, -2, "__MURAL_DATA__"); /* this */ duk_pop(ctx); return 1; }
/** * private native void send0(LinkMessage msg) * throws ClosedLinkException, * InterruptedIOException, * IOException; */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_links_Link_send0(void) { rendezvous *rp; KNI_StartHandles(3); KNI_DeclareHandle(thisObj); KNI_DeclareHandle(messageObj); KNI_DeclareHandle(otherMessageObj); KNI_GetThisPointer(thisObj); KNI_GetParameterAsObject(1, messageObj); rp = getNativePointer(thisObj); if (rp == NULL) { if (SNI_GetReentryData(NULL) == NULL) { KNI_ThrowNew(midpClosedLinkException, NULL); } else { KNI_ThrowNew(midpInterruptedIOException, NULL); } } else if (JVM_CurrentIsolateID() != rp->sender) { KNI_ThrowNew(midpIllegalArgumentException, NULL); } else { switch (rp->state) { case IDLE: rp->msg = SNI_AddStrongReference(messageObj); rp->state = SENDING; midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL); break; case RECEIVING: rp->msg = SNI_AddStrongReference(messageObj); rp->state = RENDEZVOUS; midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0); midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL); break; case SENDING: case RENDEZVOUS: midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL); break; case DONE: getReference(rp->msg, "send0/DONE", otherMessageObj); if (KNI_IsSameObject(messageObj, otherMessageObj)) { /* it's our message, finish processing */ SNI_DeleteReference(rp->msg); rp->msg = INVALID_REFERENCE_ID; rp->state = IDLE; midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0); if (rp->retcode != OK) { KNI_ThrowNew(midpIOException, NULL); } } else { /* somebody else's message, just go back to sleep */ midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL); } break; case CLOSED: setNativePointer(thisObj, NULL); if (rp->msg != INVALID_REFERENCE_ID) { /* a message was stranded in the link; clean it out */ SNI_DeleteReference(rp->msg); rp->msg = INVALID_REFERENCE_ID; } rp_decref(rp); if (SNI_GetReentryData(NULL) == NULL) { KNI_ThrowNew(midpClosedLinkException, NULL); } else { KNI_ThrowNew(midpInterruptedIOException, NULL); } break; } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * private native void receive0(LinkMessage msg, Link link) * throws ClosedLinkException, * InterruptedIOException, * IOException; */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_links_Link_receive0(void) { rendezvous *rp; KNI_StartHandles(4); KNI_DeclareHandle(thisObj); KNI_DeclareHandle(recvMessageObj); KNI_DeclareHandle(sendMessageObj); KNI_DeclareHandle(linkObj); KNI_GetThisPointer(thisObj); KNI_GetParameterAsObject(1, recvMessageObj); KNI_GetParameterAsObject(2, linkObj); rp = getNativePointer(thisObj); if (rp == NULL) { if (SNI_GetReentryData(NULL) == NULL) { KNI_ThrowNew(midpClosedLinkException, NULL); } else { KNI_ThrowNew(midpInterruptedIOException, NULL); } } else if (JVM_CurrentIsolateID() != rp->receiver) { KNI_ThrowNew(midpIllegalArgumentException, NULL); } else { jboolean ok; switch (rp->state) { case IDLE: rp->state = RECEIVING; midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL); break; case SENDING: getReference(rp->msg, "receive0/SENDING", sendMessageObj); ok = copy(sendMessageObj, recvMessageObj, linkObj); if (ok) { rp->retcode = OK; } else { rp->retcode = ERROR; KNI_ThrowNew(midpIOException, NULL); } rp->state = DONE; midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0); break; case RENDEZVOUS: getReference(rp->msg, "receive0/RENDEZVOUS", sendMessageObj); ok = copy(sendMessageObj, recvMessageObj, linkObj); if (ok) { rp->retcode = OK; } else { rp->retcode = ERROR; KNI_ThrowNew(midpIOException, NULL); } rp->state = DONE; midp_thread_signal(LINK_READY_SIGNAL, (int)rp, 0); break; case RECEIVING: case DONE: midp_thread_wait(LINK_READY_SIGNAL, (int)rp, NULL); break; case CLOSED: setNativePointer(thisObj, NULL); rp_decref(rp); if (SNI_GetReentryData(NULL) == NULL) { KNI_ThrowNew(midpClosedLinkException, NULL); } else { KNI_ThrowNew(midpInterruptedIOException, NULL); } break; } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Copies the contents of fromMsg to the contents of toMsg. Both must be * instances of LinkMessage. The toLink object must be an instance of Link. * It's filled in if the contents of fromMsg are a Link. Returns KNI_TRUE if * successful, otherwise KNI_FALSE. */ static jboolean copy(jobject fromMsg, jobject toMsg, jobject toLink) { jboolean retval; KNI_StartHandles(6); KNI_DeclareHandle(byteArrayClass); KNI_DeclareHandle(stringClass); KNI_DeclareHandle(linkClass); KNI_DeclareHandle(fromContents); KNI_DeclareHandle(newString); KNI_DeclareHandle(newByteArray); KNI_FindClass("[B", byteArrayClass); KNI_FindClass("java/lang/String", stringClass); KNI_FindClass("com/sun/midp/links/Link", linkClass); getContents(fromMsg, fromContents); if (KNI_IsInstanceOf(fromContents, byteArrayClass)) { /* do a byte array copy */ jint fromOffset; jint fromLength; getRange(fromMsg, &fromOffset, &fromLength); SNI_NewArray(SNI_BYTE_ARRAY, fromLength, newByteArray); if (KNI_IsNullHandle(newByteArray)) { retval = KNI_FALSE; } else { KNI_GetRawArrayRegion(fromContents, fromOffset, fromLength, SNI_GetRawArrayPointer(newByteArray)); setContents(toMsg, newByteArray); setRange(toMsg, 0, fromLength); retval = KNI_TRUE; } } else if (KNI_IsInstanceOf(fromContents, stringClass)) { /* do a string copy */ jchar *buf; jsize slen = KNI_GetStringLength(fromContents); SNI_NewArray(SNI_BYTE_ARRAY, slen*sizeof(jchar), newByteArray); if (KNI_IsNullHandle(newByteArray)) { retval = KNI_FALSE; } else { buf = SNI_GetRawArrayPointer(newByteArray); KNI_GetStringRegion(fromContents, 0, slen, buf); KNI_NewString(buf, slen, newString); setContents(toMsg, newString); retval = KNI_TRUE; } } else if (KNI_IsInstanceOf(fromContents, linkClass)) { /* copy the link */ rendezvous *rp = getNativePointer(fromContents); setNativePointer(toLink, rp); rp_incref(rp); setContents(toMsg, toLink); retval = KNI_TRUE; } else { retval = KNI_FALSE; } KNI_EndHandles(); return retval; }