wined3d: Use the texture dimension helpers in wined3d_texture_update_overlay().
[wine.git] / dlls / jscript / json.c
blob25f620e8e42fd4c7dd841a08d18c66384ad56245
1 /*
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
19 #include <math.h>
20 #include <assert.h>
22 #include "jscript.h"
23 #include "parser.h"
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};
39 typedef struct {
40 const WCHAR *ptr;
41 const WCHAR *end;
42 script_ctx_t *ctx;
43 } json_parse_ctx_t;
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))
53 ctx->ptr++;
54 return *ctx->ptr;
57 static BOOL is_keyword(json_parse_ctx_t *ctx, const WCHAR *keyword)
59 unsigned i;
60 for(i=0; keyword[i]; i++) {
61 if(!ctx->ptr[i] || keyword[i] != ctx->ptr[i])
62 return FALSE;
64 if(is_identifier_char(ctx->ptr[i]))
65 return FALSE;
66 ctx->ptr += i;
67 return TRUE;
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;
74 size_t len;
75 WCHAR *buf;
77 while(*ctx->ptr && *ctx->ptr != '"') {
78 if(*ctx->ptr++ == '\\')
79 ctx->ptr++;
81 if(!*ctx->ptr) {
82 FIXME("unterminated string\n");
83 return E_FAIL;
86 len = ctx->ptr-ptr;
87 buf = heap_alloc((len+1)*sizeof(WCHAR));
88 if(!buf)
89 return E_OUTOFMEMORY;
90 if(len)
91 memcpy(buf, ptr, len*sizeof(WCHAR));
92 buf[len] = 0;
94 if(!unescape(buf)) {
95 FIXME("unescape failed\n");
96 heap_free(buf);
97 return E_FAIL;
100 ctx->ptr++;
101 *r = buf;
102 return S_OK;
105 /* ECMA-262 5.1 Edition 15.12.1.2 */
106 static HRESULT parse_json_value(json_parse_ctx_t *ctx, jsval_t *r)
108 HRESULT hres;
110 switch(skip_spaces(ctx)) {
112 /* JSONNullLiteral */
113 case 'n':
114 if(!is_keyword(ctx, nullW))
115 break;
116 *r = jsval_null();
117 return S_OK;
119 /* JSONBooleanLiteral */
120 case 't':
121 if(!is_keyword(ctx, trueW))
122 break;
123 *r = jsval_bool(TRUE);
124 return S_OK;
125 case 'f':
126 if(!is_keyword(ctx, falseW))
127 break;
128 *r = jsval_bool(FALSE);
129 return S_OK;
131 /* JSONObject */
132 case '{': {
133 WCHAR *prop_name;
134 jsdisp_t *obj;
135 jsval_t val;
137 hres = create_object(ctx->ctx, NULL, &obj);
138 if(FAILED(hres))
139 return hres;
141 ctx->ptr++;
142 if(skip_spaces(ctx) == '}') {
143 ctx->ptr++;
144 *r = jsval_obj(obj);
145 return S_OK;
148 while(1) {
149 if(*ctx->ptr != '"')
150 break;
151 hres = parse_json_string(ctx, &prop_name);
152 if(FAILED(hres))
153 break;
155 if(skip_spaces(ctx) != ':') {
156 FIXME("missing ':'\n");
157 heap_free(prop_name);
158 break;
161 ctx->ptr++;
162 hres = parse_json_value(ctx, &val);
163 if(SUCCEEDED(hres)) {
164 hres = jsdisp_propput_name(obj, prop_name, val);
165 jsval_release(val);
167 heap_free(prop_name);
168 if(FAILED(hres))
169 break;
171 if(skip_spaces(ctx) == '}') {
172 ctx->ptr++;
173 *r = jsval_obj(obj);
174 return S_OK;
177 if(*ctx->ptr++ != ',') {
178 FIXME("expected ','\n");
179 break;
181 skip_spaces(ctx);
184 jsdisp_release(obj);
185 break;
188 /* JSONString */
189 case '"': {
190 WCHAR *string;
191 jsstr_t *str;
193 hres = parse_json_string(ctx, &string);
194 if(FAILED(hres))
195 return hres;
197 /* FIXME: avoid reallocation */
198 str = jsstr_alloc(string);
199 heap_free(string);
200 if(!str)
201 return E_OUTOFMEMORY;
203 *r = jsval_string(str);
204 return S_OK;
207 /* JSONArray */
208 case '[': {
209 jsdisp_t *array;
210 unsigned i = 0;
211 jsval_t val;
213 hres = create_array(ctx->ctx, 0, &array);
214 if(FAILED(hres))
215 return hres;
217 ctx->ptr++;
218 if(skip_spaces(ctx) == ']') {
219 ctx->ptr++;
220 *r = jsval_obj(array);
221 return S_OK;
224 while(1) {
225 hres = parse_json_value(ctx, &val);
226 if(FAILED(hres))
227 break;
229 hres = jsdisp_propput_idx(array, i, val);
230 jsval_release(val);
231 if(FAILED(hres))
232 break;
234 if(skip_spaces(ctx) == ']') {
235 ctx->ptr++;
236 *r = jsval_obj(array);
237 return S_OK;
240 if(*ctx->ptr != ',') {
241 FIXME("expected ','\n");
242 break;
245 ctx->ptr++;
246 i++;
249 jsdisp_release(array);
250 break;
253 /* JSONNumber */
254 default: {
255 int sign = 1;
256 double n;
258 if(*ctx->ptr == '-') {
259 sign = -1;
260 ctx->ptr++;
261 skip_spaces(ctx);
264 if(!isdigitW(*ctx->ptr))
265 break;
267 if(*ctx->ptr == '0') {
268 ctx->ptr++;
269 n = 0;
270 if(is_identifier_char(*ctx->ptr))
271 break;
272 }else {
273 hres = parse_decimal(&ctx->ptr, ctx->end, &n);
274 if(FAILED(hres))
275 return hres;
278 *r = jsval_number(sign*n);
279 return S_OK;
283 FIXME("Syntax error at %s\n", debugstr_w(ctx->ptr));
284 return E_FAIL;
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;
291 const WCHAR *buf;
292 jsstr_t *str;
293 jsval_t ret;
294 HRESULT hres;
296 if(argc != 1) {
297 FIXME("Unsupported args\n");
298 return E_INVALIDARG;
301 hres = to_flat_string(ctx, argv[0], &str, &buf);
302 if(FAILED(hres))
303 return hres;
305 TRACE("%s\n", debugstr_w(buf));
307 parse_ctx.ptr = buf;
308 parse_ctx.end = buf + jsstr_length(str);
309 parse_ctx.ctx = ctx;
310 hres = parse_json_value(&parse_ctx, &ret);
311 jsstr_release(str);
312 if(FAILED(hres))
313 return hres;
315 if(skip_spaces(&parse_ctx)) {
316 FIXME("syntax error\n");
317 jsval_release(ret);
318 return E_FAIL;
321 if(r)
322 *r = ret;
323 else
324 jsval_release(ret);
325 return S_OK;
328 typedef struct {
329 script_ctx_t *ctx;
331 WCHAR *buf;
332 size_t buf_size;
333 size_t buf_len;
335 jsdisp_t **stack;
336 size_t stack_top;
337 size_t stack_size;
339 WCHAR gap[11]; /* according to the spec, it's no longer than 10 chars */
340 } stringify_ctx_t;
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));
346 if(!ctx->stack)
347 return FALSE;
348 ctx->stack_size = 4;
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));
353 if(!new_stack)
354 return FALSE;
355 ctx->stack = new_stack;
356 ctx->stack_size *= 2;
359 ctx->stack[ctx->stack_top++] = obj;
360 return TRUE;
363 static void stringify_pop_obj(stringify_ctx_t *ctx)
365 ctx->stack_top--;
368 static BOOL is_on_stack(stringify_ctx_t *ctx, jsdisp_t *obj)
370 size_t i = ctx->stack_top;
371 while(i--) {
372 if(ctx->stack[i] == obj)
373 return TRUE;
375 return FALSE;
378 static BOOL append_string_len(stringify_ctx_t *ctx, const WCHAR *str, size_t len)
380 if(!ctx->buf_size) {
381 ctx->buf = heap_alloc(len*2*sizeof(WCHAR));
382 if(!ctx->buf)
383 return FALSE;
384 ctx->buf_size = len*2;
385 }else if(ctx->buf_len + len > ctx->buf_size) {
386 WCHAR *new_buf;
387 size_t new_size;
389 new_size = ctx->buf_size * 2 + len;
390 new_buf = heap_realloc(ctx->buf, new_size*sizeof(WCHAR));
391 if(!new_buf)
392 return FALSE;
393 ctx->buf = new_buf;
394 ctx->buf_size = new_size;
397 if(len)
398 memcpy(ctx->buf + ctx->buf_len, str, len*sizeof(WCHAR));
399 ctx->buf_len += len;
400 return TRUE;
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)
421 jsdisp_t *obj;
422 HRESULT hres;
424 if(!is_object_instance(val) || !get_object(val) || !(obj = iface_to_jsdisp((IUnknown*)get_object(val))))
425 return jsval_copy(val, r);
427 if(is_class(obj, JSCLASS_NUMBER)) {
428 double n;
429 hres = to_number(ctx, val, &n);
430 jsdisp_release(obj);
431 if(SUCCEEDED(hres))
432 *r = jsval_number(n);
433 return hres;
436 if(is_class(obj, JSCLASS_STRING)) {
437 jsstr_t *str;
438 hres = to_string(ctx, val, &str);
439 jsdisp_release(obj);
440 if(SUCCEEDED(hres))
441 *r = jsval_string(str);
442 return hres;
445 if(is_class(obj, JSCLASS_BOOLEAN)) {
446 *r = jsval_bool(bool_obj_value(obj));
447 jsdisp_release(obj);
448 return S_OK;
451 *r = jsval_obj(obj);
452 return S_OK;
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;
461 while(len--) {
462 switch(*ptr) {
463 case '"':
464 case '\\':
465 if(!append_simple_quote(ctx, *ptr))
466 return E_OUTOFMEMORY;
467 break;
468 case '\b':
469 if(!append_simple_quote(ctx, 'b'))
470 return E_OUTOFMEMORY;
471 break;
472 case '\f':
473 if(!append_simple_quote(ctx, 'f'))
474 return E_OUTOFMEMORY;
475 break;
476 case '\n':
477 if(!append_simple_quote(ctx, 'n'))
478 return E_OUTOFMEMORY;
479 break;
480 case '\r':
481 if(!append_simple_quote(ctx, 'r'))
482 return E_OUTOFMEMORY;
483 break;
484 case '\t':
485 if(!append_simple_quote(ctx, 't'))
486 return E_OUTOFMEMORY;
487 break;
488 default:
489 if(*ptr < ' ') {
490 const WCHAR formatW[] = {'\\','u','%','0','4','x',0};
491 WCHAR buf[7];
492 sprintfW(buf, formatW, *ptr);
493 if(!append_string(ctx, buf))
494 return E_OUTOFMEMORY;
495 }else {
496 if(!append_char(ctx, *ptr))
497 return E_OUTOFMEMORY;
500 ptr++;
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;
517 jsval_t val;
518 HRESULT hres;
520 if(is_on_stack(ctx, obj)) {
521 FIXME("Found a cycle\n");
522 return E_FAIL;
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;
537 if(*ctx->gap) {
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(FAILED(hres))
549 return hres;
551 hres = stringify(ctx, val);
552 if(FAILED(hres))
553 return hres;
555 if(hres == S_FALSE && !append_string(ctx, nullW))
556 return E_OUTOFMEMORY;
559 if((length && *ctx->gap && !append_char(ctx, '\n')) || !append_char(ctx, ']'))
560 return E_OUTOFMEMORY;
562 stringify_pop_obj(ctx);
563 return S_OK;
566 /* ECMA-262 5.1 Edition 15.12.3 (abstract operation JO) */
567 static HRESULT stringify_object(stringify_ctx_t *ctx, jsdisp_t *obj)
569 DISPID dispid = DISPID_STARTENUM;
570 jsval_t val = jsval_undefined();
571 unsigned prop_cnt = 0, i;
572 size_t stepback;
573 BSTR prop_name;
574 HRESULT hres;
576 if(is_on_stack(ctx, obj)) {
577 FIXME("Found a cycle\n");
578 return E_FAIL;
581 if(!stringify_push_obj(ctx, obj))
582 return E_OUTOFMEMORY;
584 if(!append_char(ctx, '{'))
585 return E_OUTOFMEMORY;
587 while((hres = IDispatchEx_GetNextDispID(&obj->IDispatchEx_iface, fdexEnumDefault, dispid, &dispid)) == S_OK) {
588 jsval_release(val);
589 hres = jsdisp_propget(obj, dispid, &val);
590 if(FAILED(hres))
591 return hres;
593 if(is_undefined(val))
594 continue;
596 stepback = ctx->buf_len;
598 if(prop_cnt && !append_char(ctx, ',')) {
599 hres = E_OUTOFMEMORY;
600 break;
603 if(*ctx->gap) {
604 if(!append_char(ctx, '\n')) {
605 hres = E_OUTOFMEMORY;
606 break;
609 for(i=0; i < ctx->stack_top; i++) {
610 if(!append_string(ctx, ctx->gap)) {
611 hres = E_OUTOFMEMORY;
612 break;
617 hres = IDispatchEx_GetMemberName(&obj->IDispatchEx_iface, dispid, &prop_name);
618 if(FAILED(hres))
619 break;
621 hres = json_quote(ctx, prop_name, SysStringLen(prop_name));
622 SysFreeString(prop_name);
623 if(FAILED(hres))
624 break;
626 if(!append_char(ctx, ':') || (*ctx->gap && !append_char(ctx, ' '))) {
627 hres = E_OUTOFMEMORY;
628 break;
631 hres = stringify(ctx, val);
632 if(FAILED(hres))
633 break;
635 if(hres == S_FALSE) {
636 ctx->buf_len = stepback;
637 continue;
640 prop_cnt++;
642 jsval_release(val);
643 if(FAILED(hres))
644 return hres;
646 if(prop_cnt && *ctx->gap) {
647 if(!append_char(ctx, '\n'))
648 return E_OUTOFMEMORY;
650 for(i=1; i < ctx->stack_top; i++) {
651 if(!append_string(ctx, ctx->gap)) {
652 hres = E_OUTOFMEMORY;
653 break;
658 if(!append_char(ctx, '}'))
659 return E_OUTOFMEMORY;
661 stringify_pop_obj(ctx);
662 return S_OK;
665 /* ECMA-262 5.1 Edition 15.12.3 (abstract operation Str) */
666 static HRESULT stringify(stringify_ctx_t *ctx, jsval_t val)
668 jsval_t value;
669 HRESULT hres;
671 if(is_object_instance(val) && get_object(val)) {
672 jsdisp_t *obj;
673 DISPID id;
675 obj = iface_to_jsdisp((IUnknown*)get_object(val));
676 if(!obj)
677 return S_FALSE;
679 hres = jsdisp_get_id(obj, toJSONW, 0, &id);
680 jsdisp_release(obj);
681 if(hres == S_OK)
682 FIXME("Use toJSON.\n");
685 /* FIXME: Support replacer replacer. */
687 hres = maybe_to_primitive(ctx->ctx, val, &value);
688 if(FAILED(hres))
689 return hres;
691 switch(jsval_type(value)) {
692 case JSV_NULL:
693 if(!append_string(ctx, nullW))
694 hres = E_OUTOFMEMORY;
695 break;
696 case JSV_BOOL:
697 if(!append_string(ctx, get_bool(value) ? trueW : falseW))
698 hres = E_OUTOFMEMORY;
699 break;
700 case JSV_STRING: {
701 jsstr_t *str = get_string(value);
702 const WCHAR *ptr = jsstr_flatten(str);
703 if(ptr)
704 hres = json_quote(ctx, ptr, jsstr_length(str));
705 else
706 hres = E_OUTOFMEMORY;
707 break;
709 case JSV_NUMBER: {
710 double n = get_number(value);
711 if(is_finite(n)) {
712 const WCHAR *ptr;
713 jsstr_t *str;
715 /* FIXME: Optimize. There is no need for jsstr_t here. */
716 hres = double_to_string(n, &str);
717 if(FAILED(hres))
718 break;
720 ptr = jsstr_flatten(str);
721 assert(ptr != NULL);
722 hres = ptr && !append_string_len(ctx, ptr, jsstr_length(str)) ? E_OUTOFMEMORY : S_OK;
723 jsstr_release(str);
724 }else {
725 if(!append_string(ctx, nullW))
726 hres = E_OUTOFMEMORY;
728 break;
730 case JSV_OBJECT: {
731 jsdisp_t *obj;
733 obj = iface_to_jsdisp((IUnknown*)get_object(value));
734 if(!obj) {
735 hres = S_FALSE;
736 break;
739 if(!is_callable(obj))
740 hres = is_class(obj, JSCLASS_ARRAY) ? stringify_array(ctx, obj) : stringify_object(ctx, obj);
741 else
742 hres = S_FALSE;
744 jsdisp_release(obj);
745 break;
747 case JSV_UNDEFINED:
748 hres = S_FALSE;
749 break;
750 case JSV_VARIANT:
751 FIXME("VARIANT\n");
752 hres = E_NOTIMPL;
753 break;
756 jsval_release(value);
757 return hres;
760 /* ECMA-262 5.1 Edition 15.12.3 */
761 static HRESULT JSON_stringify(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
763 stringify_ctx_t stringify_ctx = {ctx, NULL,0,0, NULL,0,0, {0}};
764 HRESULT hres;
766 TRACE("\n");
768 if(argc >= 2 && is_object_instance(argv[1])) {
769 FIXME("Replacer %s not yet supported\n", debugstr_jsval(argv[1]));
770 return E_NOTIMPL;
773 if(argc >= 3) {
774 jsval_t space_val;
776 hres = maybe_to_primitive(ctx, argv[2], &space_val);
777 if(FAILED(hres))
778 return hres;
780 if(is_number(space_val)) {
781 double n = get_number(space_val);
782 if(n >= 1) {
783 int i, len;
784 if(n > 10)
785 n = 10;
786 len = floor(n);
787 for(i=0; i < len; i++)
788 stringify_ctx.gap[i] = ' ';
789 stringify_ctx.gap[len] = 0;
791 }else if(is_string(space_val)) {
792 jsstr_t *space_str = get_string(space_val);
793 size_t len = jsstr_length(space_str);
794 if(len > 10)
795 len = 10;
796 jsstr_extract(space_str, 0, len, stringify_ctx.gap);
799 jsval_release(space_val);
802 hres = stringify(&stringify_ctx, argv[0]);
803 if(SUCCEEDED(hres) && r) {
804 assert(!stringify_ctx.stack_top);
806 if(hres == S_OK) {
807 jsstr_t *ret = jsstr_alloc_len(stringify_ctx.buf, stringify_ctx.buf_len);
808 if(ret)
809 *r = jsval_string(ret);
810 else
811 hres = E_OUTOFMEMORY;
812 }else {
813 *r = jsval_undefined();
817 heap_free(stringify_ctx.buf);
818 heap_free(stringify_ctx.stack);
819 return hres;
822 static const builtin_prop_t JSON_props[] = {
823 {parseW, JSON_parse, PROPF_METHOD|2},
824 {stringifyW, JSON_stringify, PROPF_METHOD|3}
827 static const builtin_info_t JSON_info = {
828 JSCLASS_JSON,
829 {NULL, NULL, 0},
830 sizeof(JSON_props)/sizeof(*JSON_props),
831 JSON_props,
832 NULL,
833 NULL
836 HRESULT create_json(script_ctx_t *ctx, jsdisp_t **ret)
838 jsdisp_t *json;
839 HRESULT hres;
841 json = heap_alloc_zero(sizeof(*json));
842 if(!json)
843 return E_OUTOFMEMORY;
845 hres = init_dispex_from_constr(json, ctx, &JSON_info, ctx->object_constr);
846 if(FAILED(hres)) {
847 heap_free(json);
848 return hres;
851 *ret = json;
852 return S_OK;