MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

expose JSON.(values) properly

+50 -10
+5
include/modules/json.h
··· 1 1 #ifndef JSON_H 2 2 #define JSON_H 3 3 4 + #include "ant.h" 5 + 4 6 void init_json_module(); 7 + 8 + jsval_t js_json_parse(struct js *js, jsval_t *args, int nargs); 9 + jsval_t js_json_stringify(struct js *js, jsval_t *args, int nargs); 5 10 6 11 #endif
+3
maidfile.toml
··· 8 8 9 9 [tasks.run] 10 10 script = ["maid build -q", "./build/ant %{arg.1}"] 11 + 12 + [tasks.debug] 13 + script = ["maid build -q", "./build/ant -d %{arg.1}"]
+1 -1
meson.build
··· 41 41 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 42 42 43 43 version_conf = configuration_data() 44 - version_conf.set('ANT_VERSION', '0.0.5.37') 44 + version_conf.set('ANT_VERSION', '0.0.5.38') 45 45 version_conf.set('ANT_GIT_HASH', git_hash) 46 46 version_conf.set('ANT_BUILD_DATE', build_date) 47 47
+2
src/ant.c
··· 3735 3735 if (nargs == 0) { 3736 3736 struct timeval tv; 3737 3737 gettimeofday(&tv, NULL); 3738 + // NOLINTNEXTLINE(bugprone-integer-division) 3738 3739 timestamp_ms = (double)tv.tv_sec * 1000.0 + (double)(tv.tv_usec / 1000); 3739 3740 } else if (nargs == 1) { 3740 3741 if (vtype(args[0]) == T_NUM) { ··· 3761 3762 3762 3763 struct timeval tv; 3763 3764 gettimeofday(&tv, NULL); 3765 + // NOLINTNEXTLINE(bugprone-integer-division) 3764 3766 double timestamp_ms = (double)tv.tv_sec * 1000.0 + (double)(tv.tv_usec / 1000); 3765 3767 3766 3768 return tov(timestamp_ms);
+2 -2
src/modules/json.c
··· 142 142 } 143 143 } 144 144 145 - static jsval_t js_json_parse(struct js *js, jsval_t *args, int nargs) { 145 + jsval_t js_json_parse(struct js *js, jsval_t *args, int nargs) { 146 146 if (nargs < 1) { 147 147 return js_mkerr(js, "JSON.parse() requires at least 1 argument"); 148 148 } ··· 167 167 return result; 168 168 } 169 169 170 - static jsval_t js_json_stringify(struct js *js, jsval_t *args, int nargs) { 170 + jsval_t js_json_stringify(struct js *js, jsval_t *args, int nargs) { 171 171 if (nargs < 1) { 172 172 return js_mkerr(js, "JSON.stringify() requires at least 1 argument"); 173 173 }
+11 -3
src/modules/server.c
··· 6 6 #include "mongoose.h" 7 7 #include "modules/server.h" 8 8 #include "modules/timer.h" 9 + #include "modules/json.h" 9 10 #include "ant.h" 10 11 11 12 typedef struct { ··· 147 148 response_ctx_t *ctx = (response_ctx_t *)(unsigned long)js_getnum(ctx_val); 148 149 if (!ctx) return js_mkundef(); 149 150 150 - const char *json_str = js_str(js, args[0]); 151 - if (json_str) { 152 - ctx->body = (char *)json_str; 151 + jsval_t stringify_args[1] = { args[0] }; 152 + jsval_t result = js_json_stringify(js, stringify_args, 1); 153 + 154 + if (js_type(result) == JS_STR) { 155 + ctx->body = js_getstr(js, result, NULL); 156 + } else if (js_type(result) == JS_ERR) { 157 + const char *json_str = js_str(js, args[0]); 158 + if (json_str) { 159 + ctx->body = (char *)json_str; 160 + } 153 161 } 154 162 155 163 if (nargs >= 2 && js_type(args[1]) == JS_NUM) {
+26 -4
tests/server/server.cjs
··· 14 14 GET /users/:id/posts 15 15 GET /files/*path 16 16 GET /api/v1/users 17 - GET /api/v2/users`); 17 + GET /api/v2/demo`); 18 18 }); 19 19 20 20 router.insert('/hello', async c => { ··· 34 34 }); 35 35 36 36 router.insert('/api/v1/users', async c => { 37 - return c.res.json({ users: null }); 37 + return c.res.json({ users: [] }); 38 38 }); 39 39 40 - router.insert('/api/v2/users', async c => { 41 - return c.res.json({ users: [] }); 40 + router.insert('/api/v2/demo', async c => { 41 + return c.res.json({ 42 + slideshow: { 43 + author: 'Yours Truly', 44 + date: 'date of publication', 45 + slides: [ 46 + { 47 + title: 'Wake up to WonderWidgets!', 48 + type: 'all' 49 + }, 50 + { 51 + items: ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 52 + title: 'Overview', 53 + type: 'all' 54 + } 55 + ], 56 + metadata: { 57 + title: 'Sample Slide Show', 58 + isFavorite: true, 59 + viewCount: 105407, 60 + createdAt: '2024-12-01T22:51:49.000Z' 61 + } 62 + } 63 + }); 42 64 }); 43 65 44 66 router.insert('/files/*path', async c => {