Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/njs/src/njs_async.c
Line
Count
Source
1
2
/*
3
 * Copyright (C) Alexander Borisov
4
 * Copyright (C) Nginx, Inc.
5
 */
6
7
#include <njs_main.h>
8
9
10
static void
11
njs_async_context_free(njs_vm_t *vm, njs_async_ctx_t *ctx);
12
13
14
njs_int_t
15
njs_async_function_frame_invoke(njs_vm_t *vm, njs_value_t *retval)
16
693k
{
17
693k
    njs_int_t                 ret;
18
693k
    njs_value_t               ctor;
19
693k
    njs_promise_capability_t  *capability;
20
21
693k
    njs_set_function(&ctor, &njs_vm_ctor(vm, NJS_OBJ_TYPE_PROMISE));
22
23
693k
    capability = njs_promise_new_capability(vm, &ctor);
24
693k
    if (njs_slow_path(capability == NULL)) {
25
1.38k
        return NJS_ERROR;
26
1.38k
    }
27
28
692k
    ret = njs_function_lambda_call(vm, retval, capability);
29
30
692k
    if (ret == NJS_OK) {
31
337k
        ret = njs_function_call(vm, njs_function(&capability->resolve),
32
337k
                                &njs_value_undefined, retval, 1, retval);
33
34
354k
    } else if (ret == NJS_AGAIN) {
35
4.43k
        ret = NJS_OK;
36
37
349k
    } else if (ret == NJS_ERROR) {
38
349k
        if (njs_is_memory_error(vm, &vm->exception)) {
39
0
            return NJS_ERROR;
40
0
        }
41
42
349k
        *retval = njs_vm_exception(vm);
43
44
349k
        ret = njs_function_call(vm, njs_function(&capability->reject),
45
349k
                                &njs_value_undefined, retval, 1,
46
349k
                                retval);
47
349k
    }
48
49
692k
    njs_value_assign(retval, &capability->promise);
50
51
692k
    return ret;
52
692k
}
53
54
55
njs_int_t
56
njs_await_fulfilled(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
57
    njs_index_t exception, njs_value_t *retval)
58
5.38k
{
59
5.38k
    njs_int_t           ret;
60
5.38k
    njs_value_t         **cur_local, **cur_closures, *value, result;
61
5.38k
    njs_frame_t         *frame, *async_frame;
62
5.38k
    njs_async_ctx_t     *ctx;
63
5.38k
    njs_native_frame_t  *top, *async;
64
65
5.38k
    ctx = vm->top_frame->function->context;
66
67
5.38k
    value = njs_arg(args, nargs, 1);
68
69
5.38k
    async_frame = ctx->await;
70
5.38k
    async = &async_frame->native;
71
5.38k
    async->previous = vm->top_frame;
72
73
5.38k
    cur_local = vm->levels[NJS_LEVEL_LOCAL];
74
5.38k
    cur_closures = vm->levels[NJS_LEVEL_CLOSURE];
75
5.38k
    top = vm->top_frame;
76
5.38k
    frame = vm->active_frame;
77
78
5.38k
    vm->levels[NJS_LEVEL_LOCAL] = async->local;
79
5.38k
    vm->levels[NJS_LEVEL_CLOSURE] = njs_function_closures(async->function);
80
81
5.38k
    vm->top_frame = async;
82
5.38k
    vm->active_frame = async_frame;
83
84
5.38k
    if (exception) {
85
1.43k
        ctx->throw_flag = 1;
86
1.43k
        njs_vm_throw(vm, value);
87
88
3.95k
    } else {
89
3.95k
        *njs_scope_value(vm, ctx->index) = *value;
90
3.95k
    }
91
92
5.38k
    ret = njs_vmcode_interpreter(vm, ctx->pc, &result, ctx->capability, ctx);
93
94
5.38k
    vm->levels[NJS_LEVEL_LOCAL] = cur_local;
95
5.38k
    vm->levels[NJS_LEVEL_CLOSURE] = cur_closures;
96
97
5.38k
    vm->top_frame = top;
98
5.38k
    vm->active_frame = frame;
99
100
5.38k
    if (ret == NJS_OK) {
101
2.48k
        ret = njs_function_call(vm, njs_function(&ctx->capability->resolve),
102
2.48k
                                &njs_value_undefined, &result, 1, retval);
103
104
2.48k
        njs_async_context_free(vm, ctx);
105
106
2.90k
    } else if (ret == NJS_AGAIN) {
107
955
        ret = NJS_OK;
108
109
1.94k
    } else if (ret == NJS_ERROR) {
110
1.94k
        if (njs_is_memory_error(vm, &vm->exception)) {
111
0
            return NJS_ERROR;
112
0
        }
113
114
1.94k
        result = njs_vm_exception(vm);
115
116
1.94k
        (void) njs_function_call(vm, njs_function(&ctx->capability->reject),
117
1.94k
                                 &njs_value_undefined, &result, 1, retval);
118
119
1.94k
        njs_async_context_free(vm, ctx);
120
121
1.94k
        return NJS_ERROR;
122
1.94k
    }
123
124
3.44k
    return ret;
125
5.38k
}
126
127
128
njs_int_t
129
njs_await_rejected(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
130
    njs_index_t unused, njs_value_t *retval)
131
1.43k
{
132
1.43k
    return njs_await_fulfilled(vm, args, nargs, 1, retval);
133
1.43k
}
134
135
136
static void
137
njs_async_context_free(njs_vm_t *vm, njs_async_ctx_t *ctx)
138
4.43k
{
139
4.43k
    njs_mp_free(vm->mem_pool, ctx->capability);
140
4.43k
    njs_mp_free(vm->mem_pool, ctx);
141
4.43k
}
142
143
144
static const njs_object_prop_init_t  njs_async_constructor_properties[] =
145
{
146
    NJS_DECLARE_PROP_LENGTH(1),
147
148
    NJS_DECLARE_PROP_NAME("AsyncFunction"),
149
150
    NJS_DECLARE_PROP_HANDLER(STRING_prototype, njs_object_prototype_create,
151
                             0, 0),
152
};
153
154
155
const njs_object_init_t  njs_async_constructor_init = {
156
    njs_async_constructor_properties,
157
    njs_nitems(njs_async_constructor_properties),
158
};
159
160
161
static const njs_object_prop_init_t  njs_async_prototype_properties[] =
162
{
163
    NJS_DECLARE_PROP_VALUE(SYMBOL_toStringTag,
164
                           njs_ascii_strval("AsyncFunction"),
165
                           NJS_OBJECT_PROP_VALUE_C),
166
167
    NJS_DECLARE_PROP_HANDLER(STRING_constructor,
168
                             njs_object_prototype_create_constructor, 0,
169
                             NJS_OBJECT_PROP_VALUE_CW),
170
};
171
172
173
const njs_object_init_t  njs_async_prototype_init = {
174
    njs_async_prototype_properties,
175
    njs_nitems(njs_async_prototype_properties),
176
};
177
178
179
const njs_object_type_init_t  njs_async_function_type_init = {
180
    .constructor = njs_native_ctor(njs_function_constructor, 1, 1),
181
    .constructor_props = &njs_async_constructor_init,
182
    .prototype_props = &njs_async_prototype_init,
183
    .prototype_value = { .object = { .type = NJS_OBJECT } },
184
};
185
186
187
const njs_object_prop_init_t  njs_async_function_instance_properties[] =
188
{
189
    NJS_DECLARE_PROP_HANDLER(STRING_length, njs_function_instance_length, 0,
190
                             NJS_OBJECT_PROP_VALUE_C),
191
192
    NJS_DECLARE_PROP_HANDLER(STRING_name, njs_function_instance_name, 0,
193
                             NJS_OBJECT_PROP_VALUE_C),
194
};
195
196
197
const njs_object_init_t  njs_async_function_instance_init = {
198
    njs_async_function_instance_properties,
199
    njs_nitems(njs_async_function_instance_properties),
200
};