return 1 if a message was handled, 0 if no message */
| 2673 | |
| 2674 | /* return 1 if a message was handled, 0 if no message */ |
| 2675 | static int handle_posted_message(JSRuntime *rt, JSContext *ctx, |
| 2676 | JSWorkerMessageHandler *port) |
| 2677 | { |
| 2678 | JSWorkerMessagePipe *ps = port->recv_pipe; |
| 2679 | int ret; |
| 2680 | struct list_head *el; |
| 2681 | JSWorkerMessage *msg; |
| 2682 | JSValue obj, data_obj, func, retval; |
| 2683 | |
| 2684 | js_mutex_lock(&ps->mutex); |
| 2685 | if (!list_empty(&ps->msg_queue)) { |
| 2686 | el = ps->msg_queue.next; |
| 2687 | msg = list_entry(el, JSWorkerMessage, link); |
| 2688 | |
| 2689 | /* remove the message from the queue */ |
| 2690 | list_del(&msg->link); |
| 2691 | |
| 2692 | // drain read end of pipe |
| 2693 | if (list_empty(&ps->msg_queue)) |
| 2694 | js_waker_clear(&ps->waker); |
| 2695 | |
| 2696 | js_mutex_unlock(&ps->mutex); |
| 2697 | |
| 2698 | data_obj = JS_ReadObject(ctx, msg->data, msg->data_len, |
| 2699 | JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE); |
| 2700 | |
| 2701 | js_free_message(msg); |
| 2702 | |
| 2703 | if (JS_IsException(data_obj)) |
| 2704 | goto fail; |
| 2705 | obj = JS_NewObject(ctx); |
| 2706 | if (JS_IsException(obj)) { |
| 2707 | JS_FreeValue(ctx, data_obj); |
| 2708 | goto fail; |
| 2709 | } |
| 2710 | JS_DefinePropertyValueStr(ctx, obj, "data", data_obj, JS_PROP_C_W_E); |
| 2711 | |
| 2712 | /* 'func' might be destroyed when calling itself (if it frees the |
| 2713 | handler), so must take extra care */ |
| 2714 | func = JS_DupValue(ctx, port->on_message_func); |
| 2715 | retval = JS_Call(ctx, func, JS_UNDEFINED, 1, (JSValueConst *)&obj); |
| 2716 | JS_FreeValue(ctx, obj); |
| 2717 | JS_FreeValue(ctx, func); |
| 2718 | if (JS_IsException(retval)) { |
| 2719 | fail: |
| 2720 | js_std_dump_error(ctx); |
| 2721 | } else { |
| 2722 | JS_FreeValue(ctx, retval); |
| 2723 | } |
| 2724 | ret = 1; |
| 2725 | } else { |
| 2726 | js_mutex_unlock(&ps->mutex); |
| 2727 | ret = 0; |
| 2728 | } |
| 2729 | return ret; |
| 2730 | } |
| 2731 | |
| 2732 | #endif // USE_WORKER |
no test coverage detected