Skip to content

Commit c1ed688

Browse files
committed
Drop non-standard JSON extension
Ref: #20
1 parent 5d5b3cc commit c1ed688

File tree

5 files changed

+10
-143
lines changed

5 files changed

+10
-143
lines changed

doc/quickjs.texi

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,20 +451,6 @@ optional properties:
451451

452452
@end table
453453

454-
@item parseExtJSON(str)
455-
456-
Parse @code{str} using a superset of @code{JSON.parse}. The
457-
following extensions are accepted:
458-
459-
@itemize
460-
@item Single line and multiline comments
461-
@item unquoted properties (ASCII-only Javascript identifiers)
462-
@item trailing comma in array and object definitions
463-
@item single quoted strings
464-
@item @code{\f} and @code{\v} are accepted as space characters
465-
@item leading plus in numbers
466-
@item octal (@code{0o} prefix) and hexadecimal (@code{0x} prefix) numbers
467-
@end itemize
468454
@end table
469455

470456
FILE prototype:

quickjs-libc.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -822,21 +822,6 @@ static JSValue js_std_strerror(JSContext *ctx, JSValueConst this_val,
822822
return JS_NewString(ctx, strerror(err));
823823
}
824824

825-
static JSValue js_std_parseExtJSON(JSContext *ctx, JSValueConst this_val,
826-
int argc, JSValueConst *argv)
827-
{
828-
JSValue obj;
829-
const char *str;
830-
size_t len;
831-
832-
str = JS_ToCStringLen(ctx, &len, argv[0]);
833-
if (!str)
834-
return JS_EXCEPTION;
835-
obj = JS_ParseJSON2(ctx, str, len, "<input>", JS_PARSE_JSON_EXT);
836-
JS_FreeCString(ctx, str);
837-
return obj;
838-
}
839-
840825
static JSValue js_new_std_file(JSContext *ctx, FILE *f,
841826
BOOL close_in_finalizer,
842827
BOOL is_popen)
@@ -1492,7 +1477,6 @@ static const JSCFunctionListEntry js_std_funcs[] = {
14921477
JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ),
14931478
JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ),
14941479
JS_CFUNC_DEF("strerror", 1, js_std_strerror ),
1495-
JS_CFUNC_DEF("parseExtJSON", 1, js_std_parseExtJSON ),
14961480

14971481
/* FILE I/O */
14981482
JS_CFUNC_DEF("open", 2, js_std_open ),

quickjs.c

Lines changed: 9 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -19464,7 +19464,6 @@ typedef struct JSParseState {
1946419464
JSFunctionDef *cur_func;
1946519465
BOOL is_module; /* parsing a module */
1946619466
BOOL allow_html_comments;
19467-
BOOL ext_json; /* true if accepting JSON superset */
1946819467
} JSParseState;
1946919468

1947019469
typedef struct JSOpCode {
@@ -20563,11 +20562,8 @@ static __exception int json_next_token(JSParseState *s)
2056320562
}
2056420563
break;
2056520564
case '\'':
20566-
if (!s->ext_json) {
20567-
/* JSON does not accept single quoted strings */
20568-
goto def_token;
20569-
}
20570-
/* fall through */
20565+
/* JSON does not accept single quoted strings */
20566+
goto def_token;
2057120567
case '\"':
2057220568
if (js_parse_string(s, c, TRUE, p + 1, &s->token, &p))
2057320569
goto fail;
@@ -20583,72 +20579,15 @@ static __exception int json_next_token(JSParseState *s)
2058320579
goto redo;
2058420580
case '\f':
2058520581
case '\v':
20586-
if (!s->ext_json) {
20587-
/* JSONWhitespace does not match <VT>, nor <FF> */
20588-
goto def_token;
20589-
}
20590-
/* fall through */
20582+
/* JSONWhitespace does not match <VT>, nor <FF> */
20583+
goto def_token;
2059120584
case ' ':
2059220585
case '\t':
2059320586
p++;
2059420587
goto redo;
2059520588
case '/':
20596-
if (!s->ext_json) {
20597-
/* JSON does not accept comments */
20598-
goto def_token;
20599-
}
20600-
if (p[1] == '*') {
20601-
/* comment */
20602-
p += 2;
20603-
for(;;) {
20604-
if (*p == '\0' && p >= s->buf_end) {
20605-
js_parse_error(s, "unexpected end of comment");
20606-
goto fail;
20607-
}
20608-
if (p[0] == '*' && p[1] == '/') {
20609-
p += 2;
20610-
break;
20611-
}
20612-
if (*p == '\n') {
20613-
s->line_num++;
20614-
p++;
20615-
} else if (*p == '\r') {
20616-
p++;
20617-
} else if (*p >= 0x80) {
20618-
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
20619-
if (c == -1) {
20620-
p++; /* skip invalid UTF-8 */
20621-
}
20622-
} else {
20623-
p++;
20624-
}
20625-
}
20626-
goto redo;
20627-
} else if (p[1] == '/') {
20628-
/* line comment */
20629-
p += 2;
20630-
for(;;) {
20631-
if (*p == '\0' && p >= s->buf_end)
20632-
break;
20633-
if (*p == '\r' || *p == '\n')
20634-
break;
20635-
if (*p >= 0x80) {
20636-
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
20637-
/* LS or PS are considered as line terminator */
20638-
if (c == CP_LS || c == CP_PS) {
20639-
break;
20640-
} else if (c == -1) {
20641-
p++; /* skip invalid UTF-8 */
20642-
}
20643-
} else {
20644-
p++;
20645-
}
20646-
}
20647-
goto redo;
20648-
} else {
20649-
goto def_token;
20650-
}
20651-
break;
20589+
/* JSON does not accept comments */
20590+
goto def_token;
2065220591
case 'a': case 'b': case 'c': case 'd':
2065320592
case 'e': case 'f': case 'g': case 'h':
2065420593
case 'i': case 'j': case 'k': case 'l':
@@ -20676,7 +20615,7 @@ static __exception int json_next_token(JSParseState *s)
2067620615
s->token.val = TOK_IDENT;
2067720616
break;
2067820617
case '+':
20679-
if (!s->ext_json || !is_digit(p[1]))
20618+
if (!is_digit(p[1]))
2068020619
goto def_token;
2068120620
goto parse_number;
2068220621
case '0':
@@ -20693,17 +20632,7 @@ static __exception int json_next_token(JSParseState *s)
2069320632
/* number */
2069420633
parse_number:
2069520634
{
20696-
JSValue ret;
20697-
int flags, radix;
20698-
if (!s->ext_json) {
20699-
flags = 0;
20700-
radix = 10;
20701-
} else {
20702-
flags = ATOD_ACCEPT_BIN_OCT;
20703-
radix = 0;
20704-
}
20705-
ret = js_atof(s->ctx, (const char *)p, (const char **)&p, radix,
20706-
flags);
20635+
JSValue ret = js_atof(s->ctx, (const char *)p, (const char **)&p, 10, 0);
2070720636
if (JS_IsException(ret))
2070820637
goto fail;
2070920638
s->token.val = TOK_NUMBER;
@@ -42935,8 +42864,6 @@ static JSValue json_parse_value(JSParseState *s)
4293542864
prop_name = JS_ValueToAtom(ctx, s->token.u.str.str);
4293642865
if (prop_name == JS_ATOM_NULL)
4293742866
goto fail;
42938-
} else if (s->ext_json && s->token.val == TOK_IDENT) {
42939-
prop_name = JS_DupAtom(ctx, s->token.u.ident.atom);
4294042867
} else {
4294142868
js_parse_error(s, "expecting property name");
4294242869
goto fail;
@@ -42961,8 +42888,6 @@ static JSValue json_parse_value(JSParseState *s)
4296142888
break;
4296242889
if (json_next_token(s))
4296342890
goto fail;
42964-
if (s->ext_json && s->token.val == '}')
42965-
break;
4296642891
}
4296742892
}
4296842893
if (json_parse_expect(s, '}'))
@@ -42993,8 +42918,6 @@ static JSValue json_parse_value(JSParseState *s)
4299342918
if (json_next_token(s))
4299442919
goto fail;
4299542920
idx++;
42996-
if (s->ext_json && s->token.val == ']')
42997-
break;
4299842921
}
4299942922
}
4300042923
if (json_parse_expect(s, ']'))
@@ -43039,14 +42962,12 @@ static JSValue json_parse_value(JSParseState *s)
4303942962
return JS_EXCEPTION;
4304042963
}
4304142964

43042-
JSValue JS_ParseJSON2(JSContext *ctx, const char *buf, size_t buf_len,
43043-
const char *filename, int flags)
42965+
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len, const char *filename)
4304442966
{
4304542967
JSParseState s1, *s = &s1;
4304642968
JSValue val = JS_UNDEFINED;
4304742969

4304842970
js_parse_init(ctx, s, buf, buf_len, filename);
43049-
s->ext_json = ((flags & JS_PARSE_JSON_EXT) != 0);
4305042971
if (json_next_token(s))
4305142972
goto fail;
4305242973
val = json_parse_value(s);
@@ -43063,12 +42984,6 @@ JSValue JS_ParseJSON2(JSContext *ctx, const char *buf, size_t buf_len,
4306342984
return JS_EXCEPTION;
4306442985
}
4306542986

43066-
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
43067-
const char *filename)
43068-
{
43069-
return JS_ParseJSON2(ctx, buf, buf_len, filename, 0);
43070-
}
43071-
4307242987
static JSValue internalize_json_property(JSContext *ctx, JSValueConst holder,
4307342988
JSAtom name, JSValueConst reviver)
4307442989
{

quickjs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,6 @@ void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id);
777777
/* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
778778
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
779779
const char *filename);
780-
#define JS_PARSE_JSON_EXT (1 << 0) /* allow extended JSON */
781-
JSValue JS_ParseJSON2(JSContext *ctx, const char *buf, size_t buf_len,
782-
const char *filename, int flags);
783780
JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
784781
JSValueConst replacer, JSValueConst space0);
785782

tests/test_std.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function test_getline()
104104

105105
f.close();
106106
}
107-
107+
108108
function test_popen()
109109
{
110110
var str, f, fname = "tmp_file.txt";
@@ -127,20 +127,6 @@ function test_popen()
127127
os.remove(fname);
128128
}
129129

130-
function test_ext_json()
131-
{
132-
var expected, input, obj;
133-
expected = '{"x":false,"y":true,"z2":null,"a":[1,8,160],"s":"str"}';
134-
input = `{ "x":false, /*comments are allowed */
135-
"y":true, // also a comment
136-
z2:null, // unquoted property names
137-
"a":[+1,0o10,0xa0,], // plus prefix, octal, hexadecimal
138-
"s":"str",} // trailing comma in objects and arrays
139-
`;
140-
obj = std.parseExtJSON(input);
141-
assert(JSON.stringify(obj), expected);
142-
}
143-
144130
function test_os()
145131
{
146132
var fd, fpath, fname, fdir, buf, buf2, i, files, err, fdate, st, link_path;
@@ -283,4 +269,3 @@ test_popen();
283269
test_os();
284270
!isWin && test_os_exec();
285271
test_timer();
286-
test_ext_json();

0 commit comments

Comments
 (0)