-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathnjs_async.c
222 lines (154 loc) · 5.75 KB
/
njs_async.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (C) Alexander Borisov
* Copyright (C) Nginx, Inc.
*/
#include <njs_main.h>
static void
njs_async_context_free(njs_vm_t *vm, njs_async_ctx_t *ctx);
njs_int_t
njs_async_function_frame_invoke(njs_vm_t *vm, njs_value_t *retval)
{
njs_int_t ret;
njs_value_t ctor;
njs_promise_capability_t *capability;
njs_set_function(&ctor, &njs_vm_ctor(vm, NJS_OBJ_TYPE_PROMISE));
capability = njs_promise_new_capability(vm, &ctor);
if (njs_slow_path(capability == NULL)) {
return NJS_ERROR;
}
ret = njs_function_lambda_call(vm, retval, capability);
if (ret == NJS_OK) {
ret = njs_function_call(vm, njs_function(&capability->resolve),
&njs_value_undefined, retval, 1, retval);
} else if (ret == NJS_AGAIN) {
ret = NJS_OK;
} else if (ret == NJS_ERROR) {
if (njs_is_memory_error(vm, &vm->exception)) {
return NJS_ERROR;
}
*retval = njs_vm_exception(vm);
ret = njs_function_call(vm, njs_function(&capability->reject),
&njs_value_undefined, retval, 1,
retval);
}
njs_value_assign(retval, &capability->promise);
return ret;
}
njs_int_t
njs_await_fulfilled(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t exception, njs_value_t *retval)
{
njs_int_t ret;
njs_value_t **cur_local, **cur_closures, *value, result;
njs_frame_t *frame, *async_frame;
njs_async_ctx_t *ctx;
njs_native_frame_t *top, *async;
ctx = vm->top_frame->function->context;
value = njs_arg(args, nargs, 1);
async_frame = ctx->await;
async = &async_frame->native;
async->previous = vm->top_frame;
cur_local = vm->levels[NJS_LEVEL_LOCAL];
cur_closures = vm->levels[NJS_LEVEL_CLOSURE];
top = vm->top_frame;
frame = vm->active_frame;
vm->levels[NJS_LEVEL_LOCAL] = async->local;
vm->levels[NJS_LEVEL_CLOSURE] = njs_function_closures(async->function);
vm->top_frame = async;
vm->active_frame = async_frame;
if (exception) {
njs_vm_throw(vm, value);
} else {
*njs_scope_value(vm, ctx->index) = *value;
}
ret = njs_vmcode_interpreter(vm, ctx->pc, &result, ctx->capability, ctx);
vm->levels[NJS_LEVEL_LOCAL] = cur_local;
vm->levels[NJS_LEVEL_CLOSURE] = cur_closures;
vm->top_frame = top;
vm->active_frame = frame;
if (ret == NJS_OK) {
ret = njs_function_call(vm, njs_function(&ctx->capability->resolve),
&njs_value_undefined, &result, 1, retval);
njs_async_context_free(vm, ctx);
} else if (ret == NJS_AGAIN) {
ret = NJS_OK;
} else if (ret == NJS_ERROR) {
if (njs_is_memory_error(vm, &vm->exception)) {
return NJS_ERROR;
}
result = njs_vm_exception(vm);
(void) njs_function_call(vm, njs_function(&ctx->capability->reject),
&njs_value_undefined, &result, 1, retval);
njs_async_context_free(vm, ctx);
return NJS_ERROR;
}
return ret;
}
njs_int_t
njs_await_rejected(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
njs_value_t *value;
njs_async_ctx_t *ctx;
ctx = vm->top_frame->function->context;
value = njs_arg(args, nargs, 1);
if (ctx->await->native.pc == ctx->pc) {
/* No catch block was set before await. */
(void) njs_function_call(vm, njs_function(&ctx->capability->reject),
&njs_value_undefined, value, 1, retval);
njs_async_context_free(vm, ctx);
return NJS_ERROR;
}
/* ctx->await->native.pc points to a catch block here. */
ctx->pc = ctx->await->native.pc;
return njs_await_fulfilled(vm, args, nargs, 1, retval);
}
static void
njs_async_context_free(njs_vm_t *vm, njs_async_ctx_t *ctx)
{
njs_mp_free(vm->mem_pool, ctx->capability);
njs_mp_free(vm->mem_pool, ctx);
}
static const njs_object_prop_t njs_async_constructor_properties[] =
{
NJS_DECLARE_PROP_LENGTH(1),
NJS_DECLARE_PROP_NAME("AsyncFunction"),
NJS_DECLARE_PROP_HANDLER("prototype", njs_object_prototype_create, 0, 0, 0),
};
const njs_object_init_t njs_async_constructor_init = {
njs_async_constructor_properties,
njs_nitems(njs_async_constructor_properties),
};
static const njs_object_prop_t njs_async_prototype_properties[] =
{
{
.type = NJS_PROPERTY,
.name = njs_wellknown_symbol(NJS_SYMBOL_TO_STRING_TAG),
.u.value = njs_string("AsyncFunction"),
.configurable = 1,
},
NJS_DECLARE_PROP_HANDLER("constructor",
njs_object_prototype_create_constructor,
0, 0, NJS_OBJECT_PROP_VALUE_CW),
};
const njs_object_init_t njs_async_prototype_init = {
njs_async_prototype_properties,
njs_nitems(njs_async_prototype_properties),
};
const njs_object_type_init_t njs_async_function_type_init = {
.constructor = njs_native_ctor(njs_function_constructor, 1, 1),
.constructor_props = &njs_async_constructor_init,
.prototype_props = &njs_async_prototype_init,
.prototype_value = { .object = { .type = NJS_OBJECT } },
};
const njs_object_prop_t njs_async_function_instance_properties[] =
{
NJS_DECLARE_PROP_HANDLER("length", njs_function_instance_length, 0, 0,
NJS_OBJECT_PROP_VALUE_C),
NJS_DECLARE_PROP_HANDLER("name", njs_function_instance_name, 0, 0,
NJS_OBJECT_PROP_VALUE_C),
};
const njs_object_init_t njs_async_function_instance_init = {
njs_async_function_instance_properties,
njs_nitems(njs_async_function_instance_properties),
};