Skip to content

Instantly share code, notes, and snippets.

@xeioex
Created November 4, 2025 02:17
Show Gist options
  • Save xeioex/1f6ed08bc15ffa47f2dfd64a17059f42 to your computer and use it in GitHub Desktop.
Save xeioex/1f6ed08bc15ffa47f2dfd64a17059f42 to your computer and use it in GitHub Desktop.
commit fa623f37e5237eab2d6d39eeba32b696b05a7036
Author: Dmitry Volyntsev <[email protected]>
Date: Mon Nov 3 15:36:47 2025 -0800
Added API to work with promises.
diff --git a/src/njs.h b/src/njs.h
index c1f4a27a..ac0678fd 100644
--- 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,
...);
diff --git a/src/njs_promise.c b/src/njs_promise.c
index a0ca8516..86ea334d 100644
--- a/src/njs_promise.c
+++ b/src/njs_promise.c
@@ -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)
diff --git a/src/njs_promise.h b/src/njs_promise.h
index 53d179a9..6a942545 100644
--- a/src/njs_promise.h
+++ b/src/njs_promise.h
@@ -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,
diff --git a/src/njs_value.c b/src/njs_value.c
index 7959c4ed..09aa9dfc 100644
--- a/src/njs_value.c
+++ b/src/njs_value.c
@@ -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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment