]> git.kaiwu.me - njs.git/commitdiff
Added API to work with promises.
authorVadim Zhestikov <v.zhestikov@f5.com>
Tue, 4 Nov 2025 16:23:39 +0000 (08:23 -0800)
committerVadimZhestikov <108960056+VadimZhestikov@users.noreply.github.com>
Wed, 5 Nov 2025 00:49:03 +0000 (16:49 -0800)
src/njs.h
src/njs_promise.c
src/njs_promise.h
src/njs_value.c

index 2523747ba1c9b1db05ac81b11705dd10e9fe209f..1592a234177b5d7d9233e4f9f67c76508a17e647 100644 (file)
--- a/src/njs.h
+++ b/src/njs.h
@@ -34,6 +34,7 @@ typedef struct njs_function_s         njs_function_t;
 typedef struct njs_vm_shared_s        njs_vm_shared_t;
 typedef struct njs_object_init_s      njs_object_init_t;
 typedef struct njs_object_prop_s      njs_object_prop_t;
+typedef struct njs_promise_data_s     njs_promise_data_t;
 typedef struct njs_object_prop_init_s njs_object_prop_init_t;
 typedef struct njs_object_type_init_s njs_object_type_init_t;
 typedef struct njs_external_s         njs_external_t;
@@ -137,6 +138,13 @@ typedef enum {
 } njs_wellknown_symbol_t;
 
 
+typedef enum {
+    NJS_PROMISE_PENDING = 0,
+    NJS_PROMISE_FULFILL,
+    NJS_PROMISE_REJECTED
+} njs_promise_type_t;
+
+
 typedef enum {
 #define njs_object_enum_kind(flags) (flags & 7)
     NJS_ENUM_KEYS = 1,
@@ -505,6 +513,9 @@ NJS_EXPORT njs_int_t njs_value_is_array(const njs_value_t *value);
 NJS_EXPORT njs_int_t njs_value_is_function(const njs_value_t *value);
 NJS_EXPORT njs_int_t njs_value_is_buffer(const njs_value_t *value);
 NJS_EXPORT njs_int_t njs_value_is_data_view(const njs_value_t *value);
+NJS_EXPORT njs_int_t njs_value_is_promise(const njs_value_t *value);
+NJS_EXPORT njs_promise_type_t njs_promise_state(const njs_value_t *value);
+NJS_EXPORT njs_value_t *njs_promise_result(const njs_value_t *value);
 
 NJS_EXPORT njs_int_t njs_vm_object_alloc(njs_vm_t *vm, njs_value_t *retval,
     ...);
index a0ca85162617017d6f0a0f3527a1e8dab20398bd..86ea334dd632a5f276c926d57fd67cd4b4e99a68 100644 (file)
@@ -912,6 +912,28 @@ njs_promise_perform_then(njs_vm_t *vm, njs_value_t *value,
 }
 
 
+njs_promise_type_t
+njs_promise_state(const njs_value_t *value)
+{
+    njs_promise_data_t  *promise_data;
+
+    promise_data = njs_data(&njs_promise(value)->value);
+
+    return promise_data->state;
+}
+
+
+njs_value_t *
+njs_promise_result(const njs_value_t *value)
+{
+    njs_promise_data_t  *promise_data;
+
+    promise_data = njs_data(&njs_promise(value)->value);
+
+    return &promise_data->result;
+}
+
+
 static njs_int_t
 njs_promise_prototype_catch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused, njs_value_t *retval)
index 53d179a9d6d61bee56c163cc491a896886b57f42..6a942545aadb63227328ce66f6ddc7b82a1090fa 100644 (file)
@@ -7,25 +7,20 @@
 #define _NJS_PROMISE_H_INCLUDED_
 
 
-typedef enum {
-    NJS_PROMISE_PENDING = 0,
-    NJS_PROMISE_FULFILL,
-    NJS_PROMISE_REJECTED
-} njs_promise_type_t;
-
 typedef struct {
     njs_value_t               promise;
     njs_value_t               resolve;
     njs_value_t               reject;
 } njs_promise_capability_t;
 
-typedef struct {
+
+struct njs_promise_data_s {
     njs_promise_type_t        state;
     njs_value_t               result;
     njs_queue_t               fulfill_queue;
     njs_queue_t               reject_queue;
     njs_bool_t                is_handled;
-} njs_promise_data_t;
+};
 
 
 njs_int_t njs_promise_constructor(njs_vm_t *vm, njs_value_t *args,
index 7959c4ed8dabe1970e4bb9c0076c8eb65f20ee83..09aa9dfcd867a6ae5a8b866a4ed9033be4150e71 100644 (file)
@@ -538,6 +538,13 @@ njs_value_is_data_view(const njs_value_t *value)
 }
 
 
+njs_int_t
+njs_value_is_promise(const njs_value_t *value)
+{
+    return njs_is_promise(value);
+}
+
+
 /*
  * ES5.1, 8.12.1: [[GetOwnProperty]], [[GetProperty]].
  * The njs_property_query() returns values