Skip to content

Commit e99854a

Browse files
committed
Wasm: Wire up Wasm language module support to the config system.
This exposes various WebAssembly language module specific options. The application type is "wasm". There is a "module" option that is required, this specifies the full path to the WebAssembly module to be run. This module should be in binary format, i.e a .wasm file. There are also currently eight function handlers that can be specified. Three of them are _required_ 1) request_handler The main driving function. This may be called multiple times for a single HTTP request if the request is larger than the shared memory. 2) malloc_handler Used to allocate a chunk of memory at language module startup. This memory is allocated from the WASM modules address space and is what is sued for communicating between the WASM module (the guest) and Unit (the host). 3) free_handler Used to free the memory from above at language module shutdown. Then there are the following five _optional_ handlers 1) module_init_handler If set, called at language module startup. 2) module_end_handler If set, called at language module shutdown. 3) request_init_handler If set, called at the start of request. Called only once per HTTP request. 4) request_end_handler If set, called once all of a request has been sent to the WASM module. 5) response_end_handler If set, called at the end of a request, once the WASM module has sent all its headers and data. Example config "applications": { "luw-echo-request": { "type": "wasm", "module": "/path/to/unit-wasm/examples/c/luw-echo-request.wasm", "request_handler": "luw_request_handler", "malloc_handler": "luw_malloc_handler", "free_handler": "luw_free_handler", "module_init_handler": "luw_module_init_handler", "module_end_handler": "luw_module_end_handler", } } Reviewed-by: Alejandro Colomar <[email protected]> Signed-off-by: Andrew Clayton <[email protected]>
1 parent 2b4a7ee commit e99854a

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/nxt_application.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,9 @@ nxt_app_parse_type(u_char *p, size_t length)
10991099

11001100
} else if (nxt_str_eq(&str, "java", 4)) {
11011101
return NXT_APP_JAVA;
1102+
1103+
} else if (nxt_str_eq(&str, "wasm", 4)) {
1104+
return NXT_APP_WASM;
11021105
}
11031106

11041107
return NXT_APP_UNKNOWN;

src/nxt_conf_validation.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,44 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_java_members[] = {
10491049
};
10501050

10511051

1052+
static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_members[] = {
1053+
{
1054+
.name = nxt_string("module"),
1055+
.type = NXT_CONF_VLDT_STRING,
1056+
.flags = NXT_CONF_VLDT_REQUIRED,
1057+
}, {
1058+
.name = nxt_string("request_handler"),
1059+
.type = NXT_CONF_VLDT_STRING,
1060+
.flags = NXT_CONF_VLDT_REQUIRED,
1061+
},{
1062+
.name = nxt_string("malloc_handler"),
1063+
.type = NXT_CONF_VLDT_STRING,
1064+
.flags = NXT_CONF_VLDT_REQUIRED,
1065+
}, {
1066+
.name = nxt_string("free_handler"),
1067+
.type = NXT_CONF_VLDT_STRING,
1068+
.flags = NXT_CONF_VLDT_REQUIRED,
1069+
}, {
1070+
.name = nxt_string("module_init_handler"),
1071+
.type = NXT_CONF_VLDT_STRING,
1072+
}, {
1073+
.name = nxt_string("module_end_handler"),
1074+
.type = NXT_CONF_VLDT_STRING,
1075+
}, {
1076+
.name = nxt_string("request_init_handler"),
1077+
.type = NXT_CONF_VLDT_STRING,
1078+
}, {
1079+
.name = nxt_string("request_end_handler"),
1080+
.type = NXT_CONF_VLDT_STRING,
1081+
}, {
1082+
.name = nxt_string("response_end_handler"),
1083+
.type = NXT_CONF_VLDT_STRING,
1084+
},
1085+
1086+
NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)
1087+
};
1088+
1089+
10521090
static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] = {
10531091
{
10541092
.name = nxt_string("type"),
@@ -2562,6 +2600,7 @@ nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name,
25622600
{ nxt_conf_vldt_object, nxt_conf_vldt_perl_members },
25632601
{ nxt_conf_vldt_object, nxt_conf_vldt_ruby_members },
25642602
{ nxt_conf_vldt_object, nxt_conf_vldt_java_members },
2603+
{ nxt_conf_vldt_object, nxt_conf_vldt_wasm_members },
25652604
};
25662605

25672606
ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT);

src/nxt_main_process.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,63 @@ static nxt_conf_map_t nxt_java_app_conf[] = {
323323
};
324324

325325

326+
static nxt_conf_map_t nxt_wasm_app_conf[] = {
327+
{
328+
nxt_string("module"),
329+
NXT_CONF_MAP_CSTRZ,
330+
offsetof(nxt_common_app_conf_t, u.wasm.module),
331+
},
332+
{
333+
nxt_string("request_handler"),
334+
NXT_CONF_MAP_CSTRZ,
335+
offsetof(nxt_common_app_conf_t, u.wasm.request_handler),
336+
},
337+
{
338+
nxt_string("malloc_handler"),
339+
NXT_CONF_MAP_CSTRZ,
340+
offsetof(nxt_common_app_conf_t, u.wasm.malloc_handler),
341+
},
342+
{
343+
nxt_string("free_handler"),
344+
NXT_CONF_MAP_CSTRZ,
345+
offsetof(nxt_common_app_conf_t, u.wasm.free_handler),
346+
},
347+
{
348+
nxt_string("module_init_handler"),
349+
NXT_CONF_MAP_CSTRZ,
350+
offsetof(nxt_common_app_conf_t, u.wasm.module_init_handler),
351+
},
352+
{
353+
nxt_string("module_end_handler"),
354+
NXT_CONF_MAP_CSTRZ,
355+
offsetof(nxt_common_app_conf_t, u.wasm.module_end_handler),
356+
},
357+
{
358+
nxt_string("request_init_handler"),
359+
NXT_CONF_MAP_CSTRZ,
360+
offsetof(nxt_common_app_conf_t, u.wasm.request_init_handler),
361+
},
362+
{
363+
nxt_string("request_end_handler"),
364+
NXT_CONF_MAP_CSTRZ,
365+
offsetof(nxt_common_app_conf_t, u.wasm.request_end_handler),
366+
},
367+
{
368+
nxt_string("response_end_handler"),
369+
NXT_CONF_MAP_CSTRZ,
370+
offsetof(nxt_common_app_conf_t, u.wasm.response_end_handler),
371+
},
372+
};
373+
374+
326375
static nxt_conf_app_map_t nxt_app_maps[] = {
327376
{ nxt_nitems(nxt_external_app_conf), nxt_external_app_conf },
328377
{ nxt_nitems(nxt_python_app_conf), nxt_python_app_conf },
329378
{ nxt_nitems(nxt_php_app_conf), nxt_php_app_conf },
330379
{ nxt_nitems(nxt_perl_app_conf), nxt_perl_app_conf },
331380
{ nxt_nitems(nxt_ruby_app_conf), nxt_ruby_app_conf },
332381
{ nxt_nitems(nxt_java_app_conf), nxt_java_app_conf },
382+
{ nxt_nitems(nxt_wasm_app_conf), nxt_wasm_app_conf },
333383
};
334384

335385

0 commit comments

Comments
 (0)