Skip to content

Commit 6440e42

Browse files
committed
Modules: added "js_shared_array_zone" directive.
The directive makes a flat shared memory available as SharedArrayBuffer to JS code.
1 parent a9d53f4 commit 6440e42

9 files changed

Lines changed: 1154 additions & 2 deletions

nginx/config

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ NJS_QUICKJS=${NJS_QUICKJS:-YES}
88
NJS_DEPS="$ngx_addon_dir/ngx_js.h \
99
$ngx_addon_dir/ngx_js_http.h \
1010
$ngx_addon_dir/ngx_js_fetch.h \
11-
$ngx_addon_dir/ngx_js_shared_dict.h"
11+
$ngx_addon_dir/ngx_js_shared_dict.h \
12+
$ngx_addon_dir/ngx_js_shared_array.h"
1213
NJS_SRCS="$ngx_addon_dir/ngx_js.c \
1314
$ngx_addon_dir/ngx_js_http.c \
1415
$ngx_addon_dir/ngx_js_fetch.c \
1516
$ngx_addon_dir/ngx_js_regex.c \
16-
$ngx_addon_dir/ngx_js_shared_dict.c"
17+
$ngx_addon_dir/ngx_js_shared_dict.c \
18+
$ngx_addon_dir/ngx_js_shared_array.c"
1719

1820
QJS_DEPS=""
1921
QJS_SRCS=""

nginx/ngx_http_js_module.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ static char *ngx_http_js_content(ngx_conf_t *cf, ngx_command_t *cmd,
382382
void *conf);
383383
static char *ngx_http_js_shared_dict_zone(ngx_conf_t *cf, ngx_command_t *cmd,
384384
void *conf);
385+
static char *ngx_http_js_shared_array_zone(ngx_conf_t *cf, ngx_command_t *cmd,
386+
void *conf);
385387
static char *ngx_http_js_fetch_proxy(ngx_conf_t *cf, ngx_command_t *cmd,
386388
void *conf);
387389
static char *ngx_http_js_body_filter_set(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -569,6 +571,13 @@ static ngx_command_t ngx_http_js_commands[] = {
569571
0,
570572
NULL },
571573

574+
{ ngx_string("js_shared_array_zone"),
575+
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
576+
ngx_http_js_shared_array_zone,
577+
0,
578+
0,
579+
NULL },
580+
572581
{ ngx_string("js_fetch_keepalive"),
573582
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
574583
ngx_conf_set_num_slot,
@@ -1079,6 +1088,7 @@ njs_module_t *njs_http_js_addon_modules[] = {
10791088
&ngx_js_ngx_module,
10801089
&ngx_js_fetch_module,
10811090
&ngx_js_shared_dict_module,
1091+
&ngx_js_shared_array_module,
10821092
#ifdef NJS_HAVE_OPENSSL
10831093
&njs_webcrypto_module,
10841094
#endif
@@ -1219,6 +1229,7 @@ static JSClassDef ngx_http_qjs_headers_out_class = {
12191229
qjs_module_t *njs_http_qjs_addon_modules[] = {
12201230
&ngx_qjs_ngx_module,
12211231
&ngx_qjs_ngx_shared_dict_module,
1232+
&ngx_qjs_ngx_shared_array_module,
12221233
#ifdef NJS_HAVE_QUICKJS
12231234
&ngx_qjs_ngx_fetch_module,
12241235
#endif
@@ -8217,6 +8228,14 @@ ngx_http_js_shared_dict_zone(ngx_conf_t *cf, ngx_command_t *cmd,
82178228
}
82188229

82198230

8231+
static char *
8232+
ngx_http_js_shared_array_zone(ngx_conf_t *cf, ngx_command_t *cmd,
8233+
void *conf)
8234+
{
8235+
return ngx_js_shared_array_zone(cf, cmd, conf, &ngx_http_js_module);
8236+
}
8237+
8238+
82208239
static char *
82218240
ngx_http_js_body_filter_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
82228241
{

nginx/ngx_js.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ static njs_external_t ngx_js_ext_global_shared[] = {
179179
};
180180

181181

182+
static njs_external_t ngx_js_ext_global_shared_array[] = {
183+
184+
{
185+
.flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL,
186+
.name.symbol = NJS_SYMBOL_TO_STRING_TAG,
187+
.u.property = {
188+
.value = "GlobalSharedArray",
189+
}
190+
},
191+
192+
{
193+
.flags = NJS_EXTERN_SELF,
194+
.u.object = {
195+
.enumerable = 1,
196+
.prop_handler = njs_js_ext_global_shared_array_prop,
197+
.keys = njs_js_ext_global_shared_array_keys,
198+
}
199+
},
200+
201+
};
202+
203+
182204
static njs_external_t ngx_js_ext_core[] = {
183205

184206
{
@@ -271,6 +293,18 @@ static njs_external_t ngx_js_ext_core[] = {
271293
}
272294
},
273295

296+
{
297+
.flags = NJS_EXTERN_OBJECT,
298+
.name.string = njs_str("sharedArray"),
299+
.enumerable = 1,
300+
.writable = 1,
301+
.u.object = {
302+
.enumerable = 1,
303+
.properties = ngx_js_ext_global_shared_array,
304+
.nproperties = njs_nitems(ngx_js_ext_global_shared_array),
305+
}
306+
},
307+
274308
{
275309
.flags = NJS_EXTERN_PROPERTY,
276310
.name.string = njs_str("prefix"),

nginx/ngx_js.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ enum {
6363
NGX_QJS_CLASS_ID_SHARED,
6464
NGX_QJS_CLASS_ID_SHARED_DICT,
6565
NGX_QJS_CLASS_ID_SHARED_DICT_ERROR,
66+
NGX_QJS_CLASS_ID_SHARED_ARRAY_EXOTIC,
67+
NGX_QJS_CLASS_ID_SHARED_ARRAY_INSTANCE,
6668
NGX_QJS_CLASS_ID_FETCH_HEADERS,
6769
NGX_QJS_CLASS_ID_FETCH_REQUEST,
6870
NGX_QJS_CLASS_ID_FETCH_RESPONSE,
@@ -73,6 +75,7 @@ enum {
7375
typedef struct ngx_js_loc_conf_s ngx_js_loc_conf_t;
7476
typedef struct ngx_js_event_s ngx_js_event_t;
7577
typedef struct ngx_js_dict_s ngx_js_dict_t;
78+
typedef struct ngx_js_shared_array_s ngx_js_shared_array_t;
7679
typedef struct ngx_js_ctx_s ngx_js_ctx_t;
7780
typedef struct ngx_engine_s ngx_engine_t;
7881

@@ -122,6 +125,7 @@ typedef struct {
122125

123126
#define NGX_JS_COMMON_MAIN_CONF \
124127
ngx_js_dict_t *dicts; \
128+
ngx_js_shared_array_t *shared_arrays; \
125129
ngx_array_t *periodics \
126130

127131

@@ -436,6 +440,7 @@ extern qjs_module_t qjs_xml_module;
436440
extern qjs_module_t qjs_zlib_module;
437441
extern qjs_module_t ngx_qjs_ngx_module;
438442
extern qjs_module_t ngx_qjs_ngx_shared_dict_module;
443+
extern qjs_module_t ngx_qjs_ngx_shared_array_module;
439444
extern qjs_module_t ngx_qjs_ngx_fetch_module;
440445

441446
#endif
@@ -495,5 +500,6 @@ extern njs_module_t njs_zlib_module;
495500

496501
#include "ngx_js_fetch.h"
497502
#include "ngx_js_shared_dict.h"
503+
#include "ngx_js_shared_array.h"
498504

499505
#endif /* _NGX_JS_H_INCLUDED_ */

0 commit comments

Comments
 (0)