in src/couch_quickjs/quickjs/quickjs-libc.c [3626:3692]
static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessagePipe *ps;
size_t data_len, sab_tab_len, i;
uint8_t *data;
JSWorkerMessage *msg;
uint8_t **sab_tab;
if (!worker)
return JS_EXCEPTION;
data = JS_WriteObject2(ctx, &data_len, argv[0],
JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE,
&sab_tab, &sab_tab_len);
if (!data)
return JS_EXCEPTION;
msg = malloc(sizeof(*msg));
if (!msg)
goto fail;
msg->data = NULL;
msg->sab_tab = NULL;
/* must reallocate because the allocator may be different */
msg->data = malloc(data_len);
if (!msg->data)
goto fail;
memcpy(msg->data, data, data_len);
msg->data_len = data_len;
if (sab_tab_len > 0) {
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
if (!msg->sab_tab)
goto fail;
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
}
msg->sab_tab_len = sab_tab_len;
js_free(ctx, data);
js_free(ctx, sab_tab);
/* increment the SAB reference counts */
for(i = 0; i < msg->sab_tab_len; i++) {
js_sab_dup(NULL, msg->sab_tab[i]);
}
ps = worker->send_pipe;
pthread_mutex_lock(&ps->mutex);
/* indicate that data is present */
if (list_empty(&ps->msg_queue))
js_waker_signal(&ps->waker);
list_add_tail(&msg->link, &ps->msg_queue);
pthread_mutex_unlock(&ps->mutex);
return JS_UNDEFINED;
fail:
if (msg) {
free(msg->data);
free(msg->sab_tab);
free(msg);
}
js_free(ctx, data);
js_free(ctx, sab_tab);
return JS_EXCEPTION;
}