2 * Copyright 2016 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 static const WCHAR parseW
[] = {'p','a','r','s','e',0};
31 static const WCHAR stringifyW
[] = {'s','t','r','i','n','g','i','f','y',0};
33 static const WCHAR nullW
[] = {'n','u','l','l',0};
34 static const WCHAR trueW
[] = {'t','r','u','e',0};
35 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
37 static const WCHAR toJSONW
[] = {'t','o','J','S','O','N',0};
45 static BOOL
is_json_space(WCHAR c
)
47 return c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r';
50 static WCHAR
skip_spaces(json_parse_ctx_t
*ctx
)
52 while(is_json_space(*ctx
->ptr
))
57 static BOOL
is_keyword(json_parse_ctx_t
*ctx
, const WCHAR
*keyword
)
60 for(i
=0; keyword
[i
]; i
++) {
61 if(!ctx
->ptr
[i
] || keyword
[i
] != ctx
->ptr
[i
])
64 if(is_identifier_char(ctx
->ptr
[i
]))
70 /* ECMA-262 5.1 Edition 15.12.1.1 */
71 static HRESULT
parse_json_string(json_parse_ctx_t
*ctx
, WCHAR
**r
)
73 const WCHAR
*ptr
= ++ctx
->ptr
;
77 while(*ctx
->ptr
&& *ctx
->ptr
!= '"') {
78 if(*ctx
->ptr
++ == '\\')
82 FIXME("unterminated string\n");
87 buf
= heap_alloc((len
+1)*sizeof(WCHAR
));
91 memcpy(buf
, ptr
, len
*sizeof(WCHAR
));
95 FIXME("unescape failed\n");
105 /* ECMA-262 5.1 Edition 15.12.1.2 */
106 static HRESULT
parse_json_value(json_parse_ctx_t
*ctx
, jsval_t
*r
)
110 switch(skip_spaces(ctx
)) {
112 /* JSONNullLiteral */
114 if(!is_keyword(ctx
, nullW
))
119 /* JSONBooleanLiteral */
121 if(!is_keyword(ctx
, trueW
))
123 *r
= jsval_bool(TRUE
);
126 if(!is_keyword(ctx
, falseW
))
128 *r
= jsval_bool(FALSE
);
137 hres
= create_object(ctx
->ctx
, NULL
, &obj
);
142 if(skip_spaces(ctx
) == '}') {
151 hres
= parse_json_string(ctx
, &prop_name
);
155 if(skip_spaces(ctx
) != ':') {
156 FIXME("missing ':'\n");
157 heap_free(prop_name
);
162 hres
= parse_json_value(ctx
, &val
);
163 if(SUCCEEDED(hres
)) {
164 hres
= jsdisp_propput_name(obj
, prop_name
, val
);
167 heap_free(prop_name
);
171 if(skip_spaces(ctx
) == '}') {
177 if(*ctx
->ptr
++ != ',') {
178 FIXME("expected ','\n");
193 hres
= parse_json_string(ctx
, &string
);
197 /* FIXME: avoid reallocation */
198 str
= jsstr_alloc(string
);
201 return E_OUTOFMEMORY
;
203 *r
= jsval_string(str
);
213 hres
= create_array(ctx
->ctx
, 0, &array
);
218 if(skip_spaces(ctx
) == ']') {
220 *r
= jsval_obj(array
);
225 hres
= parse_json_value(ctx
, &val
);
229 hres
= jsdisp_propput_idx(array
, i
, val
);
234 if(skip_spaces(ctx
) == ']') {
236 *r
= jsval_obj(array
);
240 if(*ctx
->ptr
!= ',') {
241 FIXME("expected ','\n");
249 jsdisp_release(array
);
258 if(*ctx
->ptr
== '-') {
264 if(!isdigitW(*ctx
->ptr
))
267 if(*ctx
->ptr
== '0') {
270 if(is_identifier_char(*ctx
->ptr
))
273 hres
= parse_decimal(&ctx
->ptr
, ctx
->end
, &n
);
278 *r
= jsval_number(sign
*n
);
283 FIXME("Syntax error at %s\n", debugstr_w(ctx
->ptr
));
287 /* ECMA-262 5.1 Edition 15.12.2 */
288 static HRESULT
JSON_parse(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
290 json_parse_ctx_t parse_ctx
;
297 FIXME("Unsupported args\n");
301 hres
= to_flat_string(ctx
, argv
[0], &str
, &buf
);
305 TRACE("%s\n", debugstr_w(buf
));
308 parse_ctx
.end
= buf
+ jsstr_length(str
);
310 hres
= parse_json_value(&parse_ctx
, &ret
);
315 if(skip_spaces(&parse_ctx
)) {
316 FIXME("syntax error\n");
339 WCHAR gap
[11]; /* according to the spec, it's no longer than 10 chars */
342 static BOOL
stringify_push_obj(stringify_ctx_t
*ctx
, jsdisp_t
*obj
)
344 if(!ctx
->stack_size
) {
345 ctx
->stack
= heap_alloc(4*sizeof(*ctx
->stack
));
349 }else if(ctx
->stack_top
== ctx
->stack_size
) {
350 jsdisp_t
**new_stack
;
352 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
355 ctx
->stack
= new_stack
;
356 ctx
->stack_size
*= 2;
359 ctx
->stack
[ctx
->stack_top
++] = obj
;
363 static void stringify_pop_obj(stringify_ctx_t
*ctx
)
368 static BOOL
is_on_stack(stringify_ctx_t
*ctx
, jsdisp_t
*obj
)
370 size_t i
= ctx
->stack_top
;
372 if(ctx
->stack
[i
] == obj
)
378 static BOOL
append_string_len(stringify_ctx_t
*ctx
, const WCHAR
*str
, size_t len
)
381 ctx
->buf
= heap_alloc(len
*2*sizeof(WCHAR
));
384 ctx
->buf_size
= len
*2;
385 }else if(ctx
->buf_len
+ len
> ctx
->buf_size
) {
389 new_size
= ctx
->buf_size
* 2 + len
;
390 new_buf
= heap_realloc(ctx
->buf
, new_size
*sizeof(WCHAR
));
394 ctx
->buf_size
= new_size
;
398 memcpy(ctx
->buf
+ ctx
->buf_len
, str
, len
*sizeof(WCHAR
));
403 static inline BOOL
append_string(stringify_ctx_t
*ctx
, const WCHAR
*str
)
405 return append_string_len(ctx
, str
, strlenW(str
));
408 static inline BOOL
append_char(stringify_ctx_t
*ctx
, WCHAR c
)
410 return append_string_len(ctx
, &c
, 1);
413 static inline BOOL
append_simple_quote(stringify_ctx_t
*ctx
, WCHAR c
)
415 WCHAR str
[] = {'\\',c
};
416 return append_string_len(ctx
, str
, 2);
419 static HRESULT
maybe_to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*r
)
424 if(!is_object_instance(val
) || !get_object(val
) || !(obj
= iface_to_jsdisp(get_object(val
))))
425 return jsval_copy(val
, r
);
427 if(is_class(obj
, JSCLASS_NUMBER
)) {
429 hres
= to_number(ctx
, val
, &n
);
432 *r
= jsval_number(n
);
436 if(is_class(obj
, JSCLASS_STRING
)) {
438 hres
= to_string(ctx
, val
, &str
);
441 *r
= jsval_string(str
);
445 if(is_class(obj
, JSCLASS_BOOLEAN
)) {
446 *r
= jsval_bool(bool_obj_value(obj
));
455 /* ECMA-262 5.1 Edition 15.12.3 (abstract operation Quote) */
456 static HRESULT
json_quote(stringify_ctx_t
*ctx
, const WCHAR
*ptr
, size_t len
)
458 if(!ptr
|| !append_char(ctx
, '"'))
459 return E_OUTOFMEMORY
;
465 if(!append_simple_quote(ctx
, *ptr
))
466 return E_OUTOFMEMORY
;
469 if(!append_simple_quote(ctx
, 'b'))
470 return E_OUTOFMEMORY
;
473 if(!append_simple_quote(ctx
, 'f'))
474 return E_OUTOFMEMORY
;
477 if(!append_simple_quote(ctx
, 'n'))
478 return E_OUTOFMEMORY
;
481 if(!append_simple_quote(ctx
, 'r'))
482 return E_OUTOFMEMORY
;
485 if(!append_simple_quote(ctx
, 't'))
486 return E_OUTOFMEMORY
;
490 const WCHAR formatW
[] = {'\\','u','%','0','4','x',0};
492 sprintfW(buf
, formatW
, *ptr
);
493 if(!append_string(ctx
, buf
))
494 return E_OUTOFMEMORY
;
496 if(!append_char(ctx
, *ptr
))
497 return E_OUTOFMEMORY
;
503 return append_char(ctx
, '"') ? S_OK
: E_OUTOFMEMORY
;
506 static inline BOOL
is_callable(jsdisp_t
*obj
)
508 return is_class(obj
, JSCLASS_FUNCTION
);
511 static HRESULT
stringify(stringify_ctx_t
*ctx
, jsval_t val
);
513 /* ECMA-262 5.1 Edition 15.12.3 (abstract operation JA) */
514 static HRESULT
stringify_array(stringify_ctx_t
*ctx
, jsdisp_t
*obj
)
516 unsigned length
, i
, j
;
520 if(is_on_stack(ctx
, obj
)) {
521 FIXME("Found a cycle\n");
525 if(!stringify_push_obj(ctx
, obj
))
526 return E_OUTOFMEMORY
;
528 if(!append_char(ctx
, '['))
529 return E_OUTOFMEMORY
;
531 length
= array_get_length(obj
);
533 for(i
=0; i
< length
; i
++) {
534 if(i
&& !append_char(ctx
, ','))
535 return E_OUTOFMEMORY
;
538 if(!append_char(ctx
, '\n'))
539 return E_OUTOFMEMORY
;
541 for(j
=0; j
< ctx
->stack_top
; j
++) {
542 if(!append_string(ctx
, ctx
->gap
))
543 return E_OUTOFMEMORY
;
547 hres
= jsdisp_get_idx(obj
, i
, &val
);
548 if(SUCCEEDED(hres
)) {
549 hres
= stringify(ctx
, val
);
552 if(hres
== S_FALSE
&& !append_string(ctx
, nullW
))
553 return E_OUTOFMEMORY
;
554 }else if(hres
== DISP_E_UNKNOWNNAME
) {
555 if(!append_string(ctx
, nullW
))
556 return E_OUTOFMEMORY
;
562 if((length
&& *ctx
->gap
&& !append_char(ctx
, '\n')) || !append_char(ctx
, ']'))
563 return E_OUTOFMEMORY
;
565 stringify_pop_obj(ctx
);
569 /* ECMA-262 5.1 Edition 15.12.3 (abstract operation JO) */
570 static HRESULT
stringify_object(stringify_ctx_t
*ctx
, jsdisp_t
*obj
)
572 DISPID dispid
= DISPID_STARTENUM
;
573 jsval_t val
= jsval_undefined();
574 unsigned prop_cnt
= 0, i
;
579 if(is_on_stack(ctx
, obj
)) {
580 FIXME("Found a cycle\n");
584 if(!stringify_push_obj(ctx
, obj
))
585 return E_OUTOFMEMORY
;
587 if(!append_char(ctx
, '{'))
588 return E_OUTOFMEMORY
;
590 while((hres
= IDispatchEx_GetNextDispID(&obj
->IDispatchEx_iface
, fdexEnumDefault
, dispid
, &dispid
)) == S_OK
) {
592 hres
= jsdisp_propget(obj
, dispid
, &val
);
596 if(is_undefined(val
))
599 stepback
= ctx
->buf_len
;
601 if(prop_cnt
&& !append_char(ctx
, ',')) {
602 hres
= E_OUTOFMEMORY
;
607 if(!append_char(ctx
, '\n')) {
608 hres
= E_OUTOFMEMORY
;
612 for(i
=0; i
< ctx
->stack_top
; i
++) {
613 if(!append_string(ctx
, ctx
->gap
)) {
614 hres
= E_OUTOFMEMORY
;
620 hres
= IDispatchEx_GetMemberName(&obj
->IDispatchEx_iface
, dispid
, &prop_name
);
624 hres
= json_quote(ctx
, prop_name
, SysStringLen(prop_name
));
625 SysFreeString(prop_name
);
629 if(!append_char(ctx
, ':') || (*ctx
->gap
&& !append_char(ctx
, ' '))) {
630 hres
= E_OUTOFMEMORY
;
634 hres
= stringify(ctx
, val
);
638 if(hres
== S_FALSE
) {
639 ctx
->buf_len
= stepback
;
649 if(prop_cnt
&& *ctx
->gap
) {
650 if(!append_char(ctx
, '\n'))
651 return E_OUTOFMEMORY
;
653 for(i
=1; i
< ctx
->stack_top
; i
++) {
654 if(!append_string(ctx
, ctx
->gap
)) {
655 hres
= E_OUTOFMEMORY
;
661 if(!append_char(ctx
, '}'))
662 return E_OUTOFMEMORY
;
664 stringify_pop_obj(ctx
);
668 /* ECMA-262 5.1 Edition 15.12.3 (abstract operation Str) */
669 static HRESULT
stringify(stringify_ctx_t
*ctx
, jsval_t val
)
674 if(is_object_instance(val
) && get_object(val
)) {
678 obj
= iface_to_jsdisp(get_object(val
));
682 hres
= jsdisp_get_id(obj
, toJSONW
, 0, &id
);
685 FIXME("Use toJSON.\n");
688 /* FIXME: Support replacer replacer. */
690 hres
= maybe_to_primitive(ctx
->ctx
, val
, &value
);
694 switch(jsval_type(value
)) {
696 if(!append_string(ctx
, nullW
))
697 hres
= E_OUTOFMEMORY
;
700 if(!append_string(ctx
, get_bool(value
) ? trueW
: falseW
))
701 hres
= E_OUTOFMEMORY
;
704 jsstr_t
*str
= get_string(value
);
705 const WCHAR
*ptr
= jsstr_flatten(str
);
707 hres
= json_quote(ctx
, ptr
, jsstr_length(str
));
709 hres
= E_OUTOFMEMORY
;
713 double n
= get_number(value
);
718 /* FIXME: Optimize. There is no need for jsstr_t here. */
719 hres
= double_to_string(n
, &str
);
723 ptr
= jsstr_flatten(str
);
725 hres
= ptr
&& !append_string_len(ctx
, ptr
, jsstr_length(str
)) ? E_OUTOFMEMORY
: S_OK
;
728 if(!append_string(ctx
, nullW
))
729 hres
= E_OUTOFMEMORY
;
736 obj
= iface_to_jsdisp(get_object(value
));
742 if(!is_callable(obj
))
743 hres
= is_class(obj
, JSCLASS_ARRAY
) ? stringify_array(ctx
, obj
) : stringify_object(ctx
, obj
);
759 jsval_release(value
);
763 /* ECMA-262 5.1 Edition 15.12.3 */
764 static HRESULT
JSON_stringify(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
766 stringify_ctx_t stringify_ctx
= {ctx
, NULL
,0,0, NULL
,0,0, {0}};
773 *r
= jsval_undefined();
777 if(argc
>= 2 && is_object_instance(argv
[1])) {
778 FIXME("Replacer %s not yet supported\n", debugstr_jsval(argv
[1]));
785 hres
= maybe_to_primitive(ctx
, argv
[2], &space_val
);
789 if(is_number(space_val
)) {
790 double n
= get_number(space_val
);
796 for(i
=0; i
< len
; i
++)
797 stringify_ctx
.gap
[i
] = ' ';
798 stringify_ctx
.gap
[len
] = 0;
800 }else if(is_string(space_val
)) {
801 jsstr_t
*space_str
= get_string(space_val
);
802 size_t len
= jsstr_length(space_str
);
805 jsstr_extract(space_str
, 0, len
, stringify_ctx
.gap
);
808 jsval_release(space_val
);
811 hres
= stringify(&stringify_ctx
, argv
[0]);
812 if(SUCCEEDED(hres
) && r
) {
813 assert(!stringify_ctx
.stack_top
);
816 jsstr_t
*ret
= jsstr_alloc_len(stringify_ctx
.buf
, stringify_ctx
.buf_len
);
818 *r
= jsval_string(ret
);
820 hres
= E_OUTOFMEMORY
;
822 *r
= jsval_undefined();
826 heap_free(stringify_ctx
.buf
);
827 heap_free(stringify_ctx
.stack
);
831 static const builtin_prop_t JSON_props
[] = {
832 {parseW
, JSON_parse
, PROPF_METHOD
|2},
833 {stringifyW
, JSON_stringify
, PROPF_METHOD
|3}
836 static const builtin_info_t JSON_info
= {
839 ARRAY_SIZE(JSON_props
),
845 HRESULT
create_json(script_ctx_t
*ctx
, jsdisp_t
**ret
)
850 json
= heap_alloc_zero(sizeof(*json
));
852 return E_OUTOFMEMORY
;
854 hres
= init_dispex_from_constr(json
, ctx
, &JSON_info
, ctx
->object_constr
);