njs_promise_alloc(njs_vm_t *vm)
{
    njs_promise_t       *promise;
    njs_promise_data_t  *data;

    promise = njs_mp_alloc(vm->mem_pool, sizeof(njs_promise_t)
                           + sizeof(njs_promise_data_t));
    if (njs_slow_path(promise == NULL)) {
        goto memory_error;
    }

    njs_lvlhsh_init(&promise->object.hash);
    njs_lvlhsh_init(&promise->object.shared_hash);
    promise->object.type = NJS_PROMISE;
    promise->object.shared = 0;
    promise->object.extensible = 1;
    promise->object.error_data = 0;
    promise->object.fast_array = 0;
    promise->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_PROMISE].object;
    promise->object.slots = NULL;

    data = (njs_promise_data_t *) ((uint8_t *) promise + sizeof(njs_promise_t));

    data->state = NJS_PROMISE_PENDING;
    data->is_handled = 0;

    njs_queue_init(&data->fulfill_queue);
    njs_queue_init(&data->reject_queue);

    njs_set_data(&promise->value, data, 0);

    return promise;

memory_error:

    njs_memory_error(vm);

    return NULL;
}
njs_promise_alloc(njs_vm_t *vm)
{
    njs_promise_t       *promise;
    njs_promise_data_t  *data;

    promise = njs_mp_alloc(vm->mem_pool, sizeof(njs_promise_t)
                           + sizeof(njs_promise_data_t));
    if (njs_slow_path(promise == NULL)) {
        goto memory_error;
    }

    njs_lvlhsh_init(&promise->object.hash);
    njs_lvlhsh_init(&promise->object.shared_hash);
    promise->object.type = NJS_PROMISE;
    promise->object.shared = 0;
    promise->object.extensible = 1;
    promise->object.error_data = 0;
    promise->object.fast_array = 0;
    promise->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_PROMISE].object;
    promise->object.slots = NULL;

    data = (njs_promise_data_t *) ((uint8_t *) promise + sizeof(njs_promise_t));

    data->state = NJS_PROMISE_PENDING;
    data->is_handled = 0;

    njs_queue_init(&data->fulfill_queue);
    njs_queue_init(&data->reject_queue);

    njs_set_data(&promise->value, data, 0);

    return promise;

memory_error:

    njs_memory_error(vm);

    return NULL;
}