Skip to content

Commit 8f21c40

Browse files
committed
Add js_mkstr() and js_mknum()
1 parent 2ac8e99 commit 8f21c40

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ int main(void) {
272272
}
273273
```
274274
275-
### js\_set(), js\_glob(), js\_mkobj()
275+
### js\_set(), js\_glob(), js\_mkobj(), js\_mknum(), js\_mkstr()
276276
277277
```c
278278
jsval_t js_glob(struct js *); // Return global object
279279
jsval_t js_mkobj(struct js *); // Create a new object
280+
jsval_t js_mkstr(struct js *, const void *, size_t); // Create a string
281+
jsval_t js_mknum(struct js *, double); // Create a number
280282
void js_set(struct js *, jsval_t obj, const char *key, jsval_t val); // Assign property to an object
281283
```
282284

elk.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static jsval_t mkentity(struct js *js, jsoff_t b, const void *buf, size_t len) {
297297
return mkval(b & 3, ofs);
298298
}
299299

300-
static jsval_t mkstr(struct js *js, const void *ptr, size_t len) {
300+
jsval_t js_mkstr(struct js *js, const void *ptr, size_t len) {
301301
// printf("MKSTR: [%.*s] -> off %u\n", (int) len, (char *) ptr, js->brk);
302302
return mkentity(js, (jsoff_t) (((len + 1) << 2) | T_STR), ptr, len + 1);
303303
}
@@ -653,7 +653,7 @@ static jsval_t do_string_op(struct js *js, uint8_t op, jsval_t l, jsval_t r) {
653653
jsoff_t n1, off1 = vstr(js, l, &n1);
654654
jsoff_t n2, off2 = vstr(js, r, &n2);
655655
if (op == TOK_PLUS) {
656-
jsval_t res = mkstr(js, NULL, n1 + n2);
656+
jsval_t res = js_mkstr(js, NULL, n1 + n2);
657657
if (vtype(res) == T_STR) {
658658
jsoff_t n, off = vstr(js, res, &n);
659659
memmove(&js->mem[off], &js->mem[off1], n1);
@@ -844,7 +844,7 @@ static jsval_t call_c(struct js *js, const char *fn, int fnlen, jsoff_t fnoff) {
844844
case 'i': return tov((int) res.u64);
845845
case 'd': return tov(res.d);
846846
case 'b': return mkval(T_BOOL, res.w ? 1 : 0);
847-
case 's': return mkstr(js, (char *) (intptr_t) res.w, strlen((char *) (intptr_t) res.w));
847+
case 's': return js_mkstr(js, (char *) (intptr_t) res.w, strlen((char *) (intptr_t) res.w));
848848
case 'v': return mkval(T_UNDEF, 0);
849849
case 'j': return (jsval_t) res.u64;
850850
}
@@ -873,7 +873,7 @@ static jsval_t call_js(struct js *js, const char *fn, int fnlen) {
873873
: js_expr(js, TOK_COMMA, TOK_RPAREN);
874874
// printf("[%s]\n", js_str(js, v));
875875
// Set argument in the function scope
876-
setprop(js, js->scope, mkstr(js, &fn[fnpos], identlen), v);
876+
setprop(js, js->scope, js_mkstr(js, &fn[fnpos], identlen), v);
877877
js->pos = skiptonext(js->code, js->clen, js->pos);
878878
if (js->pos < js->clen && js->code[js->pos] == ',') js->pos++;
879879
fnpos = skiptonext(fn, fnlen, fnpos + identlen); // Skip past identifier
@@ -924,7 +924,7 @@ static jsval_t do_op(struct js *js, uint8_t op, jsval_t lhs, jsval_t rhs) {
924924
switch (op) {
925925
case TOK_LAND: return mkval(T_BOOL, js_truthy(js, l) && js_truthy(js, r) ? 1 : 0);
926926
case TOK_LOR: return do_logical_or(js, l, r);
927-
case TOK_TYPEOF: return mkstr(js, typestr(vtype(r)), strlen(typestr(vtype(r))));
927+
case TOK_TYPEOF: return js_mkstr(js, typestr(vtype(r)), strlen(typestr(vtype(r))));
928928
case TOK_CALL: return do_call_op(js, l, r);
929929
case TOK_ASSIGN: return assign(js, lhs, r);
930930
case TOK_POSTINC: { do_assign_op(js, TOK_PLUS_ASSIGN, lhs, tov(1)); return l; }
@@ -998,7 +998,7 @@ static jsval_t js_str_literal(struct js *js) {
998998
out[n1++] = js->code[js->toff + n2];
999999
}
10001000
}
1001-
return mkstr(js, NULL, n1);
1001+
return js_mkstr(js, NULL, n1);
10021002
}
10031003

10041004
static jsval_t js_obj_literal(struct js *js) {
@@ -1014,7 +1014,7 @@ static jsval_t js_obj_literal(struct js *js) {
10141014
if (exe) {
10151015
// printf("XXXX [%s] scope: %lu\n", js_str(js, val), vdata(js->scope));
10161016
if (is_err(val)) return val;
1017-
jsval_t key = mkstr(js, js->code + koff, klen);
1017+
jsval_t key = js_mkstr(js, js->code + koff, klen);
10181018
if (is_err(key)) return key;
10191019
jsval_t res = setprop(js, obj, key, resolveprop(js, val));
10201020
if (is_err(res)) return res;
@@ -1043,7 +1043,7 @@ static jsval_t js_func_literal(struct js *js) {
10431043
jsval_t res = js_block(js, false); // Skip function body - no exec
10441044
if (is_err(res)) return res; // But fail short on parse error
10451045
js->flags = flags; // Restore flags
1046-
jsval_t str = mkstr(js, &js->code[pos], js->pos - pos);
1046+
jsval_t str = js_mkstr(js, &js->code[pos], js->pos - pos);
10471047
// printf("FUNC: %u [%.*s]\n", pos, js->pos - pos, &js->code[pos]);
10481048
return mkval(T_FUNC, vdata(str));
10491049
}
@@ -1155,7 +1155,7 @@ static jsval_t js_let(struct js *js) {
11551155
if (lkp(js, js->scope, name, nlen) > 0)
11561156
return js_err(js, "'%.*s' already declared", (int) nlen, name);
11571157
jsval_t x =
1158-
setprop(js, js->scope, mkstr(js, name, nlen), resolveprop(js, v));
1158+
setprop(js, js->scope, js_mkstr(js, name, nlen), resolveprop(js, v));
11591159
if (is_err(x)) return x;
11601160
}
11611161
if (js->tok == TOK_SEMICOLON || js->tok == TOK_EOF) break; // Stop
@@ -1285,6 +1285,11 @@ struct js *js_create(void *buf, size_t len) {
12851285
return js;
12861286
}
12871287

1288+
jsval_t js_mknum(struct js *js, double value) {
1289+
(void) js;
1290+
return tov(value);
1291+
}
1292+
12881293
jsval_t js_mkobj(struct js *js) {
12891294
return mkobj(js, 0);
12901295
}
@@ -1295,7 +1300,7 @@ jsval_t js_glob(struct js *js) {
12951300
}
12961301

12971302
void js_set(struct js *js, jsval_t obj, const char *key, jsval_t val) {
1298-
is_err(setprop(js, obj, mkstr(js, key, strlen(key)), val));
1303+
is_err(setprop(js, obj, js_mkstr(js, key, strlen(key)), val));
12991304
}
13001305

13011306
int js_usage(struct js *js) {
@@ -1305,7 +1310,7 @@ int js_usage(struct js *js) {
13051310
jsval_t js_import(struct js *js, uintptr_t fn, const char *signature) {
13061311
char buf[64];
13071312
size_t n = snprintf(buf, sizeof(buf), "%s@%" PRIxPTR, signature, fn);
1308-
jsval_t str = mkstr(js, buf, n);
1313+
jsval_t str = js_mkstr(js, buf, n);
13091314
return mkval(T_FUNC, vdata(str));
13101315
}
13111316

elk.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
struct js; // JS engine (opaque)
2323
typedef uint64_t jsval_t; // JS value placeholder
2424

25-
struct js *js_create(void *buf, size_t len); // Create JS instance
26-
const char *js_str(struct js *, jsval_t val); // Stringify JS value
27-
jsval_t js_eval(struct js *, const char *, size_t); // Execute JS code
28-
jsval_t js_glob(struct js *); // Return global object
29-
jsval_t js_mkobj(struct js *); // Create a new object
25+
struct js *js_create(void *buf, size_t len); // Create JS instance
26+
const char *js_str(struct js *, jsval_t val); // Stringify JS value
27+
jsval_t js_eval(struct js *, const char *, size_t); // Execute JS code
28+
jsval_t js_glob(struct js *); // Return global object
29+
jsval_t js_mkobj(struct js *); // Create a new object
30+
jsval_t js_mkstr(struct js *, const void *, size_t); // Create a string
31+
jsval_t js_mknum(struct js *, double); // Create a number
3032
jsval_t js_import(struct js *, uintptr_t, const char *); // Import native func
3133
void js_set(struct js *, jsval_t, const char *, jsval_t); // Set obj attribute
3234
int js_usage(struct js *); // Return mem usage

0 commit comments

Comments
 (0)