ReactOS  0.4.14-dev-49-gfb4591c
json.c File Reference
#include <math.h>
#include <assert.h>
#include "jscript.h"
#include "parser.h"
#include "wine/debug.h"
#include "wine/unicode.h"
Include dependency graph for json.c:

Go to the source code of this file.

Classes

struct  json_parse_ctx_t
 
struct  stringify_ctx_t
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (jscript)
 
static BOOL is_json_space (WCHAR c)
 
static WCHAR skip_spaces (json_parse_ctx_t *ctx)
 
static BOOL is_keyword (json_parse_ctx_t *ctx, const WCHAR *keyword)
 
static HRESULT parse_json_string (json_parse_ctx_t *ctx, WCHAR **r)
 
static HRESULT parse_json_value (json_parse_ctx_t *ctx, jsval_t *r)
 
static HRESULT JSON_parse (script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
 
static BOOL stringify_push_obj (stringify_ctx_t *ctx, jsdisp_t *obj)
 
static void stringify_pop_obj (stringify_ctx_t *ctx)
 
static BOOL is_on_stack (stringify_ctx_t *ctx, jsdisp_t *obj)
 
static BOOL append_string_len (stringify_ctx_t *ctx, const WCHAR *str, size_t len)
 
static BOOL append_string (stringify_ctx_t *ctx, const WCHAR *str)
 
static BOOL append_char (stringify_ctx_t *ctx, WCHAR c)
 
static BOOL append_simple_quote (stringify_ctx_t *ctx, WCHAR c)
 
static HRESULT maybe_to_primitive (script_ctx_t *ctx, jsval_t val, jsval_t *r)
 
static HRESULT json_quote (stringify_ctx_t *ctx, const WCHAR *ptr, size_t len)
 
static BOOL is_callable (jsdisp_t *obj)
 
static HRESULT stringify (stringify_ctx_t *ctx, jsval_t val)
 
static HRESULT stringify_array (stringify_ctx_t *ctx, jsdisp_t *obj)
 
static HRESULT stringify_object (stringify_ctx_t *ctx, jsdisp_t *obj)
 
static HRESULT JSON_stringify (script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
 
HRESULT create_json (script_ctx_t *ctx, jsdisp_t **ret)
 

Variables

typedef helpstring
 
static const WCHAR parseW [] = {'p','a','r','s','e',0}
 
static const WCHAR stringifyW [] = {'s','t','r','i','n','g','i','f','y',0}
 
static const WCHAR nullW [] = {'n','u','l','l',0}
 
static const WCHAR trueW [] = {'t','r','u','e',0}
 
static const WCHAR falseW [] = {'f','a','l','s','e',0}
 
static const WCHAR toJSONW [] = {'t','o','J','S','O','N',0}
 
static const builtin_prop_t JSON_props []
 
static const builtin_info_t JSON_info
 

Function Documentation

◆ append_char()

static BOOL append_char ( stringify_ctx_t ctx,
WCHAR  c 
)
inlinestatic

Definition at line 408 of file json.c.

409 {
410  return append_string_len(ctx, &c, 1);
411 }
static BOOL append_string_len(stringify_ctx_t *ctx, const WCHAR *str, size_t len)
Definition: json.c:378
const GLubyte * c
Definition: glext.h:8905

Referenced by json_quote(), stringify_array(), and stringify_object().

◆ append_simple_quote()

static BOOL append_simple_quote ( stringify_ctx_t ctx,
WCHAR  c 
)
inlinestatic

Definition at line 413 of file json.c.

414 {
415  WCHAR str[] = {'\\',c};
416  return append_string_len(ctx, str, 2);
417 }
static BOOL append_string_len(stringify_ctx_t *ctx, const WCHAR *str, size_t len)
Definition: json.c:378
const WCHAR * str
__wchar_t WCHAR
Definition: xmlstorage.h:180
const GLubyte * c
Definition: glext.h:8905

Referenced by json_quote().

◆ append_string()

static BOOL append_string ( stringify_ctx_t ctx,
const WCHAR str 
)
inlinestatic

Definition at line 403 of file json.c.

404 {
405  return append_string_len(ctx, str, strlenW(str));
406 }
WINE_UNICODE_INLINE unsigned int strlenW(const WCHAR *str)
Definition: unicode.h:212
static BOOL append_string_len(stringify_ctx_t *ctx, const WCHAR *str, size_t len)
Definition: json.c:378
const WCHAR * str

Referenced by json_quote(), stringify(), stringify_array(), and stringify_object().

◆ append_string_len()

static BOOL append_string_len ( stringify_ctx_t ctx,
const WCHAR str,
size_t  len 
)
static

Definition at line 378 of file json.c.

379 {
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;
388 
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;
395  }
396 
397  if(len)
398  memcpy(ctx->buf + ctx->buf_len, str, len*sizeof(WCHAR));
399  ctx->buf_len += len;
400  return TRUE;
401 }
size_t buf_size
Definition: json.c:332
#define TRUE
Definition: types.h:120
static void * heap_realloc(void *mem, size_t len)
Definition: appwiz.h:70
static void * heap_alloc(size_t len)
Definition: appwiz.h:65
size_t buf_len
Definition: json.c:333
const WCHAR * str
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
WCHAR * buf
Definition: json.c:331

Referenced by append_char(), append_simple_quote(), append_string(), and stringify().

◆ create_json()

HRESULT create_json ( script_ctx_t ctx,
jsdisp_t **  ret 
)

Definition at line 845 of file json.c.

846 {
847  jsdisp_t *json;
848  HRESULT hres;
849 
850  json = heap_alloc_zero(sizeof(*json));
851  if(!json)
852  return E_OUTOFMEMORY;
853 
855  if(FAILED(hres)) {
856  heap_free(json);
857  return hres;
858  }
859 
860  *ret = json;
861  return S_OK;
862 }
static const builtin_info_t JSON_info
Definition: json.c:836
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:77
jsdisp_t * object_constr
Definition: jscript.h:452
int ret
#define S_OK
Definition: intsafe.h:59
HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
Definition: dispex.c:1051
static BOOL heap_free(void *mem)
Definition: appwiz.h:75

Referenced by init_global().

◆ is_callable()

static BOOL is_callable ( jsdisp_t obj)
inlinestatic

Definition at line 506 of file json.c.

507 {
508  return is_class(obj, JSCLASS_FUNCTION);
509 }
static BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
Definition: jscript.h:500

Referenced by stringify().

◆ is_json_space()

static BOOL is_json_space ( WCHAR  c)
static

Definition at line 45 of file json.c.

46 {
47  return c == ' ' || c == '\t' || c == '\n' || c == '\r';
48 }
const GLubyte * c
Definition: glext.h:8905

Referenced by skip_spaces().

◆ is_keyword()

static BOOL is_keyword ( json_parse_ctx_t ctx,
const WCHAR keyword 
)
static

Definition at line 57 of file json.c.

58 {
59  unsigned i;
60  for(i=0; keyword[i]; i++) {
61  if(!ctx->ptr[i] || keyword[i] != ctx->ptr[i])
62  return FALSE;
63  }
64  if(is_identifier_char(ctx->ptr[i]))
65  return FALSE;
66  ctx->ptr += i;
67  return TRUE;
68 }
#define TRUE
Definition: types.h:120
BOOL is_identifier_char(WCHAR c)
Definition: lex.c:123
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
const WCHAR * ptr
Definition: json.c:40

Referenced by parse_json_value().

◆ is_on_stack()

static BOOL is_on_stack ( stringify_ctx_t ctx,
jsdisp_t obj 
)
static

Definition at line 368 of file json.c.

369 {
370  size_t i = ctx->stack_top;
371  while(i--) {
372  if(ctx->stack[i] == obj)
373  return TRUE;
374  }
375  return FALSE;
376 }
jsdisp_t ** stack
Definition: json.c:335
#define TRUE
Definition: types.h:120
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
size_t stack_top
Definition: json.c:336

Referenced by stringify_array(), and stringify_object().

◆ JSON_parse()

static HRESULT JSON_parse ( script_ctx_t ctx,
vdisp_t jsthis,
WORD  flags,
unsigned  argc,
jsval_t argv,
jsval_t r 
)
static

Definition at line 288 of file json.c.

289 {
290  json_parse_ctx_t parse_ctx;
291  const WCHAR *buf;
292  jsstr_t *str;
293  jsval_t ret;
294  HRESULT hres;
295 
296  if(argc != 1) {
297  FIXME("Unsupported args\n");
298  return E_INVALIDARG;
299  }
300 
301  hres = to_flat_string(ctx, argv[0], &str, &buf);
302  if(FAILED(hres))
303  return hres;
304 
305  TRACE("%s\n", debugstr_w(buf));
306 
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);
312  if(FAILED(hres))
313  return hres;
314 
315  if(skip_spaces(&parse_ctx)) {
316  FIXME("syntax error\n");
318  return E_FAIL;
319  }
320 
321  if(r)
322  *r = ret;
323  else
325  return S_OK;
326 }
Definition: jsval.h:54
static int argc
Definition: ServiceArgs.c:12
void jsval_release(jsval_t val)
Definition: jsutils.c:189
Definition: jsstr.h:39
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
static unsigned jsstr_length(jsstr_t *str)
Definition: jsstr.h:58
static void jsstr_release(jsstr_t *str)
Definition: jsstr.h:110
#define argv
Definition: mplay32.c:18
#define E_FAIL
Definition: ddrawi.h:102
static HRESULT parse_json_value(json_parse_ctx_t *ctx, jsval_t *r)
Definition: json.c:106
#define debugstr_w
Definition: kernel32.h:32
#define FIXME(fmt,...)
Definition: debug.h:110
#define E_INVALIDARG
Definition: ddrawi.h:101
const WCHAR * str
#define TRACE(s)
Definition: solgame.cpp:4
HRESULT hres
Definition: protocol.c:465
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
static WCHAR skip_spaces(json_parse_ctx_t *ctx)
Definition: json.c:50
int ret
const WCHAR * ptr
Definition: json.c:40
#define S_OK
Definition: intsafe.h:59
script_ctx_t * ctx
Definition: json.c:42
const WCHAR * end
Definition: json.c:41
HRESULT to_flat_string(script_ctx_t *, jsval_t, jsstr_t **, const WCHAR **) DECLSPEC_HIDDEN
Definition: jsutils.c:790

◆ json_quote()

static HRESULT json_quote ( stringify_ctx_t ctx,
const WCHAR ptr,
size_t  len 
)
static

Definition at line 456 of file json.c.

457 {
458  if(!ptr || !append_char(ctx, '"'))
459  return E_OUTOFMEMORY;
460 
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  static 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;
498  }
499  }
500  ptr++;
501  }
502 
503  return append_char(ctx, '"') ? S_OK : E_OUTOFMEMORY;
504 }
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
static BOOL append_string(stringify_ctx_t *ctx, const WCHAR *str)
Definition: json.c:403
static PVOID ptr
Definition: dispmode.c:27
static BOOL append_simple_quote(stringify_ctx_t *ctx, WCHAR c)
Definition: json.c:413
__wchar_t WCHAR
Definition: xmlstorage.h:180
GLenum GLsizei len
Definition: glext.h:6722
#define S_OK
Definition: intsafe.h:59
#define sprintfW
Definition: unicode.h:58
static BOOL append_char(stringify_ctx_t *ctx, WCHAR c)
Definition: json.c:408

Referenced by stringify(), and stringify_object().

◆ JSON_stringify()

static HRESULT JSON_stringify ( script_ctx_t ctx,
vdisp_t jsthis,
WORD  flags,
unsigned  argc,
jsval_t argv,
jsval_t r 
)
static

Definition at line 764 of file json.c.

765 {
766  stringify_ctx_t stringify_ctx = {ctx, NULL,0,0, NULL,0,0, {0}};
767  HRESULT hres;
768 
769  TRACE("\n");
770 
771  if(!argc) {
772  if(r)
773  *r = jsval_undefined();
774  return S_OK;
775  }
776 
777  if(argc >= 2 && is_object_instance(argv[1])) {
778  FIXME("Replacer %s not yet supported\n", debugstr_jsval(argv[1]));
779  return E_NOTIMPL;
780  }
781 
782  if(argc >= 3) {
783  jsval_t space_val;
784 
785  hres = maybe_to_primitive(ctx, argv[2], &space_val);
786  if(FAILED(hres))
787  return hres;
788 
789  if(is_number(space_val)) {
790  double n = get_number(space_val);
791  if(n >= 1) {
792  int i, len;
793  if(n > 10)
794  n = 10;
795  len = floor(n);
796  for(i=0; i < len; i++)
797  stringify_ctx.gap[i] = ' ';
798  stringify_ctx.gap[len] = 0;
799  }
800  }else if(is_string(space_val)) {
801  jsstr_t *space_str = get_string(space_val);
802  size_t len = jsstr_length(space_str);
803  if(len > 10)
804  len = 10;
805  jsstr_extract(space_str, 0, len, stringify_ctx.gap);
806  }
807 
808  jsval_release(space_val);
809  }
810 
811  hres = stringify(&stringify_ctx, argv[0]);
812  if(SUCCEEDED(hres) && r) {
813  assert(!stringify_ctx.stack_top);
814 
815  if(hres == S_OK) {
816  jsstr_t *ret = jsstr_alloc_len(stringify_ctx.buf, stringify_ctx.buf_len);
817  if(ret)
818  *r = jsval_string(ret);
819  else
821  }else {
822  *r = jsval_undefined();
823  }
824  }
825 
826  heap_free(stringify_ctx.buf);
827  heap_free(stringify_ctx.stack);
828  return hres;
829 }
jsstr_t * jsstr_alloc_len(const WCHAR *buf, unsigned len)
Definition: jsstr.c:86
Definition: jsval.h:54
static int argc
Definition: ServiceArgs.c:12
void jsval_release(jsval_t val)
Definition: jsutils.c:189
jsdisp_t ** stack
Definition: json.c:335
Definition: jsstr.h:39
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble n
Definition: glext.h:7729
#define assert(x)
Definition: debug.h:53
static BOOL is_string(parse_buffer *buf)
Definition: parsing.c:601
static unsigned jsstr_length(jsstr_t *str)
Definition: jsstr.h:58
#define argv
Definition: mplay32.c:18
static BOOL is_number(jsval_t v)
Definition: jsval.h:191
static HRESULT maybe_to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *r)
Definition: json.c:419
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
size_t buf_len
Definition: json.c:333
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
static jsstr_t * get_string(jsval_t v)
Definition: jsval.h:229
#define FIXME(fmt,...)
Definition: debug.h:110
smooth NULL
Definition: ftsmooth.c:416
static BOOL is_object_instance(jsval_t v)
Definition: jsval.h:166
static jsval_t jsval_string(jsstr_t *str)
Definition: jsval.h:109
#define TRACE(s)
Definition: solgame.cpp:4
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:77
static double get_number(jsval_t v)
Definition: jsval.h:224
int ret
size_t stack_top
Definition: json.c:336
GLenum GLsizei len
Definition: glext.h:6722
WCHAR * buf
Definition: json.c:331
static jsval_t jsval_undefined(void)
Definition: jsval.h:137
#define S_OK
Definition: intsafe.h:59
WCHAR gap[11]
Definition: json.c:339
#define E_NOTIMPL
Definition: ddrawi.h:99
void jsstr_extract(jsstr_t *str, unsigned off, unsigned len, WCHAR *buf)
Definition: jsstr.c:113
static HRESULT stringify(stringify_ctx_t *ctx, jsval_t val)
Definition: json.c:669
_Check_return_ _CRTIMP double __cdecl floor(_In_ double x)
#define SUCCEEDED(hr)
Definition: intsafe.h:57
const char * debugstr_jsval(const jsval_t) DECLSPEC_HIDDEN
Definition: jsutils.c:33
static BOOL heap_free(void *mem)
Definition: appwiz.h:75

◆ maybe_to_primitive()

static HRESULT maybe_to_primitive ( script_ctx_t ctx,
jsval_t  val,
jsval_t r 
)
static

Definition at line 419 of file json.c.

420 {
421  jsdisp_t *obj;
422  HRESULT hres;
423 
425  return jsval_copy(val, r);
426 
427  if(is_class(obj, JSCLASS_NUMBER)) {
428  double n;
429  hres = to_number(ctx, val, &n);
431  if(SUCCEEDED(hres))
432  *r = jsval_number(n);
433  return hres;
434  }
435 
436  if(is_class(obj, JSCLASS_STRING)) {
437  jsstr_t *str;
438  hres = to_string(ctx, val, &str);
440  if(SUCCEEDED(hres))
441  *r = jsval_string(str);
442  return hres;
443  }
444 
448  return S_OK;
449  }
450 
451  *r = jsval_obj(obj);
452  return S_OK;
453 }
Definition: jsstr.h:39
static IDispatch * get_object(jsval_t v)
Definition: jsval.h:219
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble n
Definition: glext.h:7729
GLsizei GLsizei GLuint * obj
Definition: glext.h:6042
HRESULT to_number(script_ctx_t *, jsval_t, double *) DECLSPEC_HIDDEN
Definition: jsutils.c:601
HRESULT jsval_copy(jsval_t v, jsval_t *r)
Definition: jsutils.c:229
static BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
Definition: jscript.h:500
static HRESULT to_string(VARIANT *src, BSTR *dst)
Definition: host.c:48
const WCHAR * str
static BOOL is_object_instance(jsval_t v)
Definition: jsval.h:166
static jsval_t jsval_string(jsstr_t *str)
Definition: jsval.h:109
GLuint GLfloat * val
Definition: glext.h:7180
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:77
jsdisp_t * iface_to_jsdisp(IDispatch *iface)
Definition: dispex.c:1081
#define S_OK
Definition: intsafe.h:59
static void jsdisp_release(jsdisp_t *jsdisp)
Definition: jscript.h:265
static jsval_t jsval_obj(jsdisp_t *obj)
Definition: jsval.h:125
static jsval_t jsval_bool(BOOL b)
Definition: jsval.h:101
#define SUCCEEDED(hr)
Definition: intsafe.h:57
static jsval_t jsval_number(double n)
Definition: jsval.h:144
BOOL bool_obj_value(jsdisp_t *obj)
Definition: bool.c:52

Referenced by JSON_stringify(), and stringify().

◆ parse_json_string()

static HRESULT parse_json_string ( json_parse_ctx_t ctx,
WCHAR **  r 
)
static

Definition at line 71 of file json.c.

72 {
73  const WCHAR *ptr = ++ctx->ptr;
74  size_t len;
75  WCHAR *buf;
76 
77  while(*ctx->ptr && *ctx->ptr != '"') {
78  if(*ctx->ptr++ == '\\')
79  ctx->ptr++;
80  }
81  if(!*ctx->ptr) {
82  FIXME("unterminated string\n");
83  return E_FAIL;
84  }
85 
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;
93 
94  if(!unescape(buf)) {
95  FIXME("unescape failed\n");
96  heap_free(buf);
97  return E_FAIL;
98  }
99 
100  ctx->ptr++;
101  *r = buf;
102  return S_OK;
103 }
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define E_FAIL
Definition: ddrawi.h:102
static void * heap_alloc(size_t len)
Definition: appwiz.h:65
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define FIXME(fmt,...)
Definition: debug.h:110
static PVOID ptr
Definition: dispmode.c:27
BOOL unescape(WCHAR *str)
Definition: lex.c:269
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * ptr
Definition: json.c:40
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
#define S_OK
Definition: intsafe.h:59
static BOOL heap_free(void *mem)
Definition: appwiz.h:75

Referenced by parse_json_value().

◆ parse_json_value()

static HRESULT parse_json_value ( json_parse_ctx_t ctx,
jsval_t r 
)
static

Definition at line 106 of file json.c.

107 {
108  HRESULT hres;
109 
110  switch(skip_spaces(ctx)) {
111 
112  /* JSONNullLiteral */
113  case 'n':
114  if(!is_keyword(ctx, nullW))
115  break;
116  *r = jsval_null();
117  return S_OK;
118 
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;
130 
131  /* JSONObject */
132  case '{': {
133  WCHAR *prop_name;
134  jsdisp_t *obj;
135  jsval_t val;
136 
137  hres = create_object(ctx->ctx, NULL, &obj);
138  if(FAILED(hres))
139  return hres;
140 
141  ctx->ptr++;
142  if(skip_spaces(ctx) == '}') {
143  ctx->ptr++;
144  *r = jsval_obj(obj);
145  return S_OK;
146  }
147 
148  while(1) {
149  if(*ctx->ptr != '"')
150  break;
151  hres = parse_json_string(ctx, &prop_name);
152  if(FAILED(hres))
153  break;
154 
155  if(skip_spaces(ctx) != ':') {
156  FIXME("missing ':'\n");
157  heap_free(prop_name);
158  break;
159  }
160 
161  ctx->ptr++;
162  hres = parse_json_value(ctx, &val);
163  if(SUCCEEDED(hres)) {
164  hres = jsdisp_propput_name(obj, prop_name, val);
166  }
167  heap_free(prop_name);
168  if(FAILED(hres))
169  break;
170 
171  if(skip_spaces(ctx) == '}') {
172  ctx->ptr++;
173  *r = jsval_obj(obj);
174  return S_OK;
175  }
176 
177  if(*ctx->ptr++ != ',') {
178  FIXME("expected ','\n");
179  break;
180  }
181  skip_spaces(ctx);
182  }
183 
185  break;
186  }
187 
188  /* JSONString */
189  case '"': {
190  WCHAR *string;
191  jsstr_t *str;
192 
193  hres = parse_json_string(ctx, &string);
194  if(FAILED(hres))
195  return hres;
196 
197  /* FIXME: avoid reallocation */
198  str = jsstr_alloc(string);
199  heap_free(string);
200  if(!str)
201  return E_OUTOFMEMORY;
202 
203  *r = jsval_string(str);
204  return S_OK;
205  }
206 
207  /* JSONArray */
208  case '[': {
209  jsdisp_t *array;
210  unsigned i = 0;
211  jsval_t val;
212 
213  hres = create_array(ctx->ctx, 0, &array);
214  if(FAILED(hres))
215  return hres;
216 
217  ctx->ptr++;
218  if(skip_spaces(ctx) == ']') {
219  ctx->ptr++;
220  *r = jsval_obj(array);
221  return S_OK;
222  }
223 
224  while(1) {
225  hres = parse_json_value(ctx, &val);
226  if(FAILED(hres))
227  break;
228 
231  if(FAILED(hres))
232  break;
233 
234  if(skip_spaces(ctx) == ']') {
235  ctx->ptr++;
236  *r = jsval_obj(array);
237  return S_OK;
238  }
239 
240  if(*ctx->ptr != ',') {
241  FIXME("expected ','\n");
242  break;
243  }
244 
245  ctx->ptr++;
246  i++;
247  }
248 
250  break;
251  }
252 
253  /* JSONNumber */
254  default: {
255  int sign = 1;
256  double n;
257 
258  if(*ctx->ptr == '-') {
259  sign = -1;
260  ctx->ptr++;
261  skip_spaces(ctx);
262  }
263 
264  if(!isdigitW(*ctx->ptr))
265  break;
266 
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;
276  }
277 
278  *r = jsval_number(sign*n);
279  return S_OK;
280  }
281  }
282 
283  FIXME("Syntax error at %s\n", debugstr_w(ctx->ptr));
284  return E_FAIL;
285 }
static size_t double int int int * sign
Definition: printf.c:64
Definition: jsval.h:54
static jsstr_t * jsstr_alloc(const WCHAR *str)
Definition: jsstr.h:103
void jsval_release(jsval_t val)
Definition: jsutils.c:189
#define TRUE
Definition: types.h:120
static BOOL is_keyword(json_parse_ctx_t *ctx, const WCHAR *keyword)
Definition: json.c:57
Definition: jsstr.h:39
BOOL is_identifier_char(WCHAR c)
Definition: lex.c:123
static jsval_t jsval_null(void)
Definition: jsval.h:130
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
HRESULT parse_decimal(const WCHAR **iter, const WCHAR *end, double *ret)
Definition: lex.c:426
GLdouble n
Definition: glext.h:7729
#define E_FAIL
Definition: ddrawi.h:102
GLsizei GLsizei GLuint * obj
Definition: glext.h:6042
static HRESULT parse_json_value(json_parse_ctx_t *ctx, jsval_t *r)
Definition: json.c:106
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define debugstr_w
Definition: kernel32.h:32
#define FIXME(fmt,...)
Definition: debug.h:110
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
static jsval_t jsval_string(jsstr_t *str)
Definition: jsval.h:109
GLuint GLfloat * val
Definition: glext.h:7180
HRESULT hres
Definition: protocol.c:465
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
static const WCHAR falseW[]
Definition: json.c:35
static WCHAR skip_spaces(json_parse_ctx_t *ctx)
Definition: json.c:50
const WCHAR * ptr
Definition: json.c:40
char string[160]
Definition: util.h:11
HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, jsval_t val)
Definition: dispex.c:1370
#define S_OK
Definition: intsafe.h:59
HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
Definition: array.c:1310
script_ctx_t * ctx
Definition: json.c:42
WINE_UNICODE_INLINE int isdigitW(WCHAR wc)
Definition: unicode.h:170
static HRESULT parse_json_string(json_parse_ctx_t *ctx, WCHAR **r)
Definition: json.c:71
static void jsdisp_release(jsdisp_t *jsdisp)
Definition: jscript.h:265
static jsval_t jsval_obj(jsdisp_t *obj)
Definition: jsval.h:125
HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, jsval_t val)
Definition: dispex.c:1365
const WCHAR * end
Definition: json.c:41
HRESULT create_object(script_ctx_t *, jsdisp_t *, jsdisp_t **) DECLSPEC_HIDDEN
Definition: object.c:597
static jsval_t jsval_bool(BOOL b)
Definition: jsval.h:101
static const WCHAR trueW[]
Definition: json.c:34
static const WCHAR nullW[]
Definition: json.c:33
#define SUCCEEDED(hr)
Definition: intsafe.h:57
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint GLenum GLenum GLenum GLint GLuint GLenum GLenum GLfloat GLenum GLfloat GLenum GLint const GLfloat GLenum GLint const GLushort GLint GLint GLsizei GLsizei GLenum GLsizei GLsizei GLenum GLenum const GLvoid GLenum GLdouble GLenum GLint GLenum GLenum GLint GLenum GLenum GLfloat GLenum GLenum GLfloat GLenum GLfloat GLenum GLushort const GLubyte GLenum GLenum GLenum GLint GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLvoid GLenum GLenum GLint GLenum GLint GLenum GLint GLuint GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble const GLfloat GLenum const GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLint GLint GLsizei GLsizei GLenum GLuint GLenum array
Definition: glfuncs.h:320
static jsval_t jsval_number(double n)
Definition: jsval.h:144
static BOOL heap_free(void *mem)
Definition: appwiz.h:75

Referenced by JSON_parse().

◆ skip_spaces()

static WCHAR skip_spaces ( json_parse_ctx_t ctx)
static

Definition at line 50 of file json.c.

51 {
52  while(is_json_space(*ctx->ptr))
53  ctx->ptr++;
54  return *ctx->ptr;
55 }
const WCHAR * ptr
Definition: json.c:40
static BOOL is_json_space(WCHAR c)
Definition: json.c:45

Referenced by JSON_parse(), and parse_json_value().

◆ stringify()

static HRESULT stringify ( stringify_ctx_t ctx,
jsval_t  val 
)
static

Definition at line 669 of file json.c.

670 {
671  jsval_t value;
672  HRESULT hres;
673 
675  jsdisp_t *obj;
676  DISPID id;
677 
679  if(!obj)
680  return S_FALSE;
681 
682  hres = jsdisp_get_id(obj, toJSONW, 0, &id);
684  if(hres == S_OK)
685  FIXME("Use toJSON.\n");
686  }
687 
688  /* FIXME: Support replacer replacer. */
689 
690  hres = maybe_to_primitive(ctx->ctx, val, &value);
691  if(FAILED(hres))
692  return hres;
693 
694  switch(jsval_type(value)) {
695  case JSV_NULL:
696  if(!append_string(ctx, nullW))
698  break;
699  case JSV_BOOL:
700  if(!append_string(ctx, get_bool(value) ? trueW : falseW))
702  break;
703  case JSV_STRING: {
705  const WCHAR *ptr = jsstr_flatten(str);
706  if(ptr)
707  hres = json_quote(ctx, ptr, jsstr_length(str));
708  else
710  break;
711  }
712  case JSV_NUMBER: {
713  double n = get_number(value);
714  if(is_finite(n)) {
715  const WCHAR *ptr;
716  jsstr_t *str;
717 
718  /* FIXME: Optimize. There is no need for jsstr_t here. */
719  hres = double_to_string(n, &str);
720  if(FAILED(hres))
721  break;
722 
723  ptr = jsstr_flatten(str);
724  assert(ptr != NULL);
727  }else {
728  if(!append_string(ctx, nullW))
730  }
731  break;
732  }
733  case JSV_OBJECT: {
734  jsdisp_t *obj;
735 
737  if(!obj) {
738  hres = S_FALSE;
739  break;
740  }
741 
742  if(!is_callable(obj))
744  else
745  hres = S_FALSE;
746 
748  break;
749  }
750  case JSV_UNDEFINED:
751  hres = S_FALSE;
752  break;
753  case JSV_VARIANT:
754  FIXME("VARIANT\n");
755  hres = E_NOTIMPL;
756  break;
757  }
758 
760  return hres;
761 }
Definition: jsval.h:54
static const WCHAR * jsstr_flatten(jsstr_t *str)
Definition: jsstr.h:139
void jsval_release(jsval_t val)
Definition: jsutils.c:189
Definition: jsstr.h:39
static IDispatch * get_object(jsval_t v)
Definition: jsval.h:219
GLdouble n
Definition: glext.h:7729
#define assert(x)
Definition: debug.h:53
static unsigned jsstr_length(jsstr_t *str)
Definition: jsstr.h:58
static void jsstr_release(jsstr_t *str)
Definition: jsstr.h:110
HRESULT double_to_string(double, jsstr_t **) DECLSPEC_HIDDEN
Definition: jsutils.c:719
static HRESULT maybe_to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *r)
Definition: json.c:419
GLsizei GLsizei GLuint * obj
Definition: glext.h:6042
static BOOL append_string_len(stringify_ctx_t *ctx, const WCHAR *str, size_t len)
Definition: json.c:378
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
static BOOL append_string(stringify_ctx_t *ctx, const WCHAR *str)
Definition: json.c:403
static BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
Definition: jscript.h:500
static jsstr_t * get_string(jsval_t v)
Definition: jsval.h:229
static BOOL get_bool(D3DXPARAMETER_TYPE type, const void *data)
static BOOL is_callable(jsdisp_t *obj)
Definition: json.c:506
#define FIXME(fmt,...)
Definition: debug.h:110
static PVOID ptr
Definition: dispmode.c:27
#define S_FALSE
Definition: winerror.h:2357
static HRESULT stringify_array(stringify_ctx_t *ctx, jsdisp_t *obj)
Definition: json.c:514
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
static BOOL is_object_instance(jsval_t v)
Definition: jsval.h:166
GLuint GLfloat * val
Definition: glext.h:7180
static HRESULT json_quote(stringify_ctx_t *ctx, const WCHAR *ptr, size_t len)
Definition: json.c:456
Definition: jsval.h:46
HRESULT hres
Definition: protocol.c:465
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
static const WCHAR falseW[]
Definition: json.c:35
static double get_number(jsval_t v)
Definition: jsval.h:224
static jsval_type_t jsval_type(jsval_t v)
Definition: jsval.h:210
static const WCHAR toJSONW[]
Definition: json.c:37
HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
Definition: dispex.c:1088
GLsizei const GLfloat * value
Definition: glext.h:6069
BOOL is_finite(double) DECLSPEC_HIDDEN
Definition: jsutils.c:56
jsdisp_t * iface_to_jsdisp(IDispatch *iface)
Definition: dispex.c:1081
#define S_OK
Definition: intsafe.h:59
static VARIANTARG static DISPID
Definition: ordinal.c:49
#define E_NOTIMPL
Definition: ddrawi.h:99
script_ctx_t * ctx
Definition: json.c:329
static void jsdisp_release(jsdisp_t *jsdisp)
Definition: jscript.h:265
static HRESULT stringify_object(stringify_ctx_t *ctx, jsdisp_t *obj)
Definition: json.c:570
GLenum GLuint id
Definition: glext.h:5579
Definition: jsval.h:50
static const WCHAR trueW[]
Definition: json.c:34
static const WCHAR nullW[]
Definition: json.c:33

Referenced by JSON_stringify(), stringify_array(), and stringify_object().

◆ stringify_array()

static HRESULT stringify_array ( stringify_ctx_t ctx,
jsdisp_t obj 
)
static

Definition at line 514 of file json.c.

515 {
516  unsigned length, i, j;
517  jsval_t val;
518  HRESULT hres;
519 
520  if(is_on_stack(ctx, obj)) {
521  FIXME("Found a cycle\n");
522  return E_FAIL;
523  }
524 
525  if(!stringify_push_obj(ctx, obj))
526  return E_OUTOFMEMORY;
527 
528  if(!append_char(ctx, '['))
529  return E_OUTOFMEMORY;
530 
532 
533  for(i=0; i < length; i++) {
534  if(i && !append_char(ctx, ','))
535  return E_OUTOFMEMORY;
536 
537  if(*ctx->gap) {
538  if(!append_char(ctx, '\n'))
539  return E_OUTOFMEMORY;
540 
541  for(j=0; j < ctx->stack_top; j++) {
542  if(!append_string(ctx, ctx->gap))
543  return E_OUTOFMEMORY;
544  }
545  }
546 
547  hres = jsdisp_get_idx(obj, i, &val);
548  if(SUCCEEDED(hres)) {
549  hres = stringify(ctx, val);
550  if(FAILED(hres))
551  return hres;
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;
557  }else {
558  return hres;
559  }
560  }
561 
562  if((length && *ctx->gap && !append_char(ctx, '\n')) || !append_char(ctx, ']'))
563  return E_OUTOFMEMORY;
564 
565  stringify_pop_obj(ctx);
566  return S_OK;
567 }
Definition: jsval.h:54
#define DISP_E_UNKNOWNNAME
Definition: winerror.h:2515
static void stringify_pop_obj(stringify_ctx_t *ctx)
Definition: json.c:363
unsigned array_get_length(jsdisp_t *array)
Definition: array.c:70
#define E_FAIL
Definition: ddrawi.h:102
static BOOL is_on_stack(stringify_ctx_t *ctx, jsdisp_t *obj)
Definition: json.c:368
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
static BOOL append_string(stringify_ctx_t *ctx, const WCHAR *str)
Definition: json.c:403
#define FIXME(fmt,...)
Definition: debug.h:110
#define S_FALSE
Definition: winerror.h:2357
GLuint GLfloat * val
Definition: glext.h:7180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:77
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
size_t stack_top
Definition: json.c:336
#define S_OK
Definition: intsafe.h:59
WCHAR gap[11]
Definition: json.c:339
HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, jsval_t *r)
Definition: dispex.c:1446
static BOOL append_char(stringify_ctx_t *ctx, WCHAR c)
Definition: json.c:408
static HRESULT stringify(stringify_ctx_t *ctx, jsval_t val)
Definition: json.c:669
static BOOL stringify_push_obj(stringify_ctx_t *ctx, jsdisp_t *obj)
Definition: json.c:342
static const WCHAR nullW[]
Definition: json.c:33
#define SUCCEEDED(hr)
Definition: intsafe.h:57

Referenced by stringify().

◆ stringify_object()

static HRESULT stringify_object ( stringify_ctx_t ctx,
jsdisp_t obj 
)
static

Definition at line 570 of file json.c.

571 {
572  DISPID dispid = DISPID_STARTENUM;
574  unsigned prop_cnt = 0, i;
575  size_t stepback;
576  BSTR prop_name;
577  HRESULT hres;
578 
579  if(is_on_stack(ctx, obj)) {
580  FIXME("Found a cycle\n");
581  return E_FAIL;
582  }
583 
584  if(!stringify_push_obj(ctx, obj))
585  return E_OUTOFMEMORY;
586 
587  if(!append_char(ctx, '{'))
588  return E_OUTOFMEMORY;
589 
590  while((hres = IDispatchEx_GetNextDispID(&obj->IDispatchEx_iface, fdexEnumDefault, dispid, &dispid)) == S_OK) {
592  hres = jsdisp_propget(obj, dispid, &val);
593  if(FAILED(hres))
594  return hres;
595 
596  if(is_undefined(val))
597  continue;
598 
599  stepback = ctx->buf_len;
600 
601  if(prop_cnt && !append_char(ctx, ',')) {
603  break;
604  }
605 
606  if(*ctx->gap) {
607  if(!append_char(ctx, '\n')) {
609  break;
610  }
611 
612  for(i=0; i < ctx->stack_top; i++) {
613  if(!append_string(ctx, ctx->gap)) {
615  break;
616  }
617  }
618  }
619 
620  hres = IDispatchEx_GetMemberName(&obj->IDispatchEx_iface, dispid, &prop_name);
621  if(FAILED(hres))
622  break;
623 
624  hres = json_quote(ctx, prop_name, SysStringLen(prop_name));
625  SysFreeString(prop_name);
626  if(FAILED(hres))
627  break;
628 
629  if(!append_char(ctx, ':') || (*ctx->gap && !append_char(ctx, ' '))) {
631  break;
632  }
633 
634  hres = stringify(ctx, val);
635  if(FAILED(hres))
636  break;
637 
638  if(hres == S_FALSE) {
639  ctx->buf_len = stepback;
640  continue;
641  }
642 
643  prop_cnt++;
644  }
646  if(FAILED(hres))
647  return hres;
648 
649  if(prop_cnt && *ctx->gap) {
650  if(!append_char(ctx, '\n'))
651  return E_OUTOFMEMORY;
652 
653  for(i=1; i < ctx->stack_top; i++) {
654  if(!append_string(ctx, ctx->gap)) {
656  break;
657  }
658  }
659  }
660 
661  if(!append_char(ctx, '}'))
662  return E_OUTOFMEMORY;
663 
664  stringify_pop_obj(ctx);
665  return S_OK;
666 }
Definition: jsval.h:54
void jsval_release(jsval_t val)
Definition: jsutils.c:189
static void stringify_pop_obj(stringify_ctx_t *ctx)
Definition: json.c:363
HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, jsval_t *val)
Definition: dispex.c:1468
static BOOL is_undefined(jsval_t v)
Definition: jsval.h:171
OLECHAR * BSTR
Definition: compat.h:1934
#define E_FAIL
Definition: ddrawi.h:102
static BOOL is_on_stack(stringify_ctx_t *ctx, jsdisp_t *obj)
Definition: json.c:368
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
size_t buf_len
Definition: json.c:333
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
static BOOL append_string(stringify_ctx_t *ctx, const WCHAR *str)
Definition: json.c:403
#define FIXME(fmt,...)
Definition: debug.h:110
#define S_FALSE
Definition: winerror.h:2357
GLuint GLfloat * val
Definition: glext.h:7180
static HRESULT json_quote(stringify_ctx_t *ctx, const WCHAR *ptr, size_t len)
Definition: json.c:456
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:77
size_t stack_top
Definition: json.c:336
UINT WINAPI SysStringLen(BSTR str)
Definition: oleaut.c:199
static jsval_t jsval_undefined(void)
Definition: jsval.h:137
#define S_OK
Definition: intsafe.h:59
static VARIANTARG static DISPID
Definition: ordinal.c:49
WCHAR gap[11]
Definition: json.c:339
void WINAPI DECLSPEC_HOTPATCH SysFreeString(BSTR str)
Definition: oleaut.c:274
static BOOL append_char(stringify_ctx_t *ctx, WCHAR c)
Definition: json.c:408
static HRESULT stringify(stringify_ctx_t *ctx, jsval_t val)
Definition: json.c:669
static BOOL stringify_push_obj(stringify_ctx_t *ctx, jsdisp_t *obj)
Definition: json.c:342

Referenced by stringify().

◆ stringify_pop_obj()

static void stringify_pop_obj ( stringify_ctx_t ctx)
static

Definition at line 363 of file json.c.

364 {
365  ctx->stack_top--;
366 }
size_t stack_top
Definition: json.c:336

Referenced by stringify_array(), and stringify_object().

◆ stringify_push_obj()

static BOOL stringify_push_obj ( stringify_ctx_t ctx,
jsdisp_t obj 
)
static

Definition at line 342 of file json.c.

343 {
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;
351 
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;
357  }
358 
359  ctx->stack[ctx->stack_top++] = obj;
360  return TRUE;
361 }
jsdisp_t ** stack
Definition: json.c:335
#define TRUE
Definition: types.h:120
static void * heap_realloc(void *mem, size_t len)
Definition: appwiz.h:70
static void * heap_alloc(size_t len)
Definition: appwiz.h:65
GLsizei GLsizei GLuint * obj
Definition: glext.h:6042
size_t stack_top
Definition: json.c:336
size_t stack_size
Definition: json.c:337

Referenced by stringify_array(), and stringify_object().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( jscript  )

Variable Documentation

◆ falseW

◆ helpstring

typedef helpstring

◆ JSON_info

const builtin_info_t JSON_info
static
Initial value:
= {
{NULL, NULL, 0},
}
static const builtin_prop_t JSON_props[]
Definition: json.c:831
smooth NULL
Definition: ftsmooth.c:416
#define ARRAY_SIZE(a)
Definition: main.h:24

Definition at line 836 of file json.c.

Referenced by create_json().

◆ JSON_props

const builtin_prop_t JSON_props[]
static
Initial value:
= {
}
static const WCHAR parseW[]
Definition: json.c:30
static HRESULT JSON_stringify(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: json.c:764
static HRESULT JSON_parse(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: json.c:288
#define PROPF_METHOD
Definition: jscript.h:94
static const WCHAR stringifyW[]
Definition: json.c:31

Definition at line 831 of file json.c.

◆ nullW

const WCHAR nullW[] = {'n','u','l','l',0}
static

◆ parseW

const WCHAR parseW[] = {'p','a','r','s','e',0}
static

Definition at line 30 of file json.c.

◆ stringifyW

const WCHAR stringifyW[] = {'s','t','r','i','n','g','i','f','y',0}
static

Definition at line 31 of file json.c.

◆ toJSONW

const WCHAR toJSONW[] = {'t','o','J','S','O','N',0}
static

Definition at line 37 of file json.c.

Referenced by stringify().

◆ trueW