ReactOS 0.4.17-dev-357-ga8f14ff
jsval.h
Go to the documentation of this file.
1/*
2 * Copyright 2012 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef JSVAL_H
20#define JSVAL_H
21
22#include "jsstr.h"
23
24/*
25 * jsval_t structure is used to represent JavaScript dynamically-typed values.
26 * It's a (type,value) pair, usually represented as a structure of enum (type)
27 * and union (value of given type). For both memory and speed performance, we
28 * use tricks allowing storing both values as a struct with size equal to
29 * size of double (that is 64-bit) on 32-bit systems. For that, we use the fact
30 * that NaN value representation has 52 (almost) free bits.
31 */
32
33#ifdef __i386__
34#define JSVAL_DOUBLE_LAYOUT_PTR32
35#endif
36
37#ifdef JSVAL_DOUBLE_LAYOUT_PTR32
38/* NaN exponent, quiet bit 0x80000 and our 0x10000 marker */
39#define JSV_VAL(x) (0x7ff90000|x)
40#else
41#define JSV_VAL(x) x
42#endif
43
44typedef enum {
53
54struct _jsval_t {
55#ifdef JSVAL_DOUBLE_LAYOUT_PTR32
56 union {
57 double n;
58 struct {
59 union {
61 jsstr_t *str;
62 BOOL b;
63 VARIANT *v;
64 UINT_PTR as_uintptr;
65 } u;
67 } s;
68 } u;
69#else
71 union {
74 double n;
77 } u;
78#endif
79};
80
81#ifdef JSVAL_DOUBLE_LAYOUT_PTR32
82
83C_ASSERT(sizeof(jsval_t) == sizeof(double));
84
85#define __JSVAL_TYPE(x) ((x).u.s.tag)
86#define __JSVAL_BOOL(x) ((x).u.s.u.b)
87#define __JSVAL_STR(x) ((x).u.s.u.str)
88#define __JSVAL_OBJ(x) ((x).u.s.u.obj)
89#define __JSVAL_VAR(x) ((x).u.s.u.v)
90
91#else
92
93#define __JSVAL_TYPE(x) ((x).type)
94#define __JSVAL_BOOL(x) ((x).u.b)
95#define __JSVAL_STR(x) ((x).u.str)
96#define __JSVAL_OBJ(x) ((x).u.obj)
97#define __JSVAL_VAR(x) ((x).u.v)
98
99#endif
100
101static inline jsval_t jsval_bool(BOOL b)
102{
103 jsval_t ret;
105 __JSVAL_BOOL(ret) = b;
106 return ret;
107}
108
110{
111 jsval_t ret;
114 return ret;
115}
116
118{
119 jsval_t ret;
122 return ret;
123}
124
126{
127 return jsval_disp(to_disp(obj));
128}
129
130static inline jsval_t jsval_null(void)
131{
132 jsval_t ret;
135 return ret;
136}
137
138static inline jsval_t jsval_null_disp(void)
139{
140 jsval_t ret;
143 return ret;
144}
145
146static inline jsval_t jsval_undefined(void)
147{
148 jsval_t ret;
150 return ret;
151}
152
153static inline jsval_t jsval_number(double n)
154{
155 jsval_t ret;
156#ifdef JSVAL_DOUBLE_LAYOUT_PTR32
157 ret.u.n = n;
158 /* normalize NaN value */
159 if((ret.u.s.tag & 0x7ff00000) == 0x7ff00000) {
160 /* isinf */
161 if(ret.u.s.tag & 0xfffff) {
162 ret.u.s.tag = 0x7ff80000;
163 ret.u.s.u.as_uintptr = ~0;
164 }else if(ret.u.s.u.as_uintptr) {
165 ret.u.s.tag = 0x7ff80000;
166 }
167 }
168#else
169 ret.type = JSV_NUMBER;
170 ret.u.n = n;
171#endif
172 return ret;
173}
174
176{
177 return __JSVAL_TYPE(v) == JSV_OBJECT;
178}
179
181{
182 return __JSVAL_TYPE(v) == JSV_UNDEFINED;
183}
184
185static inline BOOL is_null(jsval_t v)
186{
187 return __JSVAL_TYPE(v) == JSV_NULL;
188}
189
191{
192 return is_null(v) && __JSVAL_BOOL(v);
193}
194
195static inline BOOL is_string(jsval_t v)
196{
197 return __JSVAL_TYPE(v) == JSV_STRING;
198}
199
200static inline BOOL is_number(jsval_t v)
201{
202#ifdef JSVAL_DOUBLE_LAYOUT_PTR32
203 return (v.u.s.tag & 0x7ff10000) != 0x7ff10000;
204#else
205 return v.type == JSV_NUMBER;
206#endif
207}
208
209static inline BOOL is_variant(jsval_t v)
210{
211 return __JSVAL_TYPE(v) == JSV_VARIANT;
212}
213
214static inline BOOL is_bool(jsval_t v)
215{
216 return __JSVAL_TYPE(v) == JSV_BOOL;
217}
218
220{
221#ifdef JSVAL_DOUBLE_LAYOUT_PTR32
222 return is_number(v) ? JSV_NUMBER : v.u.s.tag;
223#else
224 return v.type;
225#endif
226}
227
229{
230 return __JSVAL_OBJ(v);
231}
232
233static inline double get_number(jsval_t v)
234{
235 return v.u.n;
236}
237
239{
240 return __JSVAL_STR(v);
241}
242
244{
245 return __JSVAL_VAR(v);
246}
247
248static inline BOOL get_bool(jsval_t v)
249{
250 return __JSVAL_BOOL(v);
251}
252
257
258#endif
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
return ret
Definition: mutex.c:146
unsigned int BOOL
Definition: ntddk_ex.h:94
const GLdouble * v
Definition: gl.h:2040
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
#define C_ASSERT(e)
Definition: intsafe.h:73
static IDispatch * to_disp(jsdisp_t *jsdisp)
Definition: jscript.h:222
#define __JSVAL_STR(x)
Definition: jsval.h:95
static BOOL is_string(jsval_t v)
Definition: jsval.h:195
static BOOL is_number(jsval_t v)
Definition: jsval.h:200
HRESULT jsval_to_variant(jsval_t, VARIANT *)
Definition: jsutils.c:367
static VARIANT * get_variant(jsval_t v)
Definition: jsval.h:243
jsval_type_t
Definition: jsval.h:44
@ JSV_STRING
Definition: jsval.h:48
@ JSV_NUMBER
Definition: jsval.h:49
@ JSV_VARIANT
Definition: jsval.h:51
@ JSV_OBJECT
Definition: jsval.h:47
@ JSV_NULL
Definition: jsval.h:46
@ JSV_UNDEFINED
Definition: jsval.h:45
@ JSV_BOOL
Definition: jsval.h:50
static jsval_t jsval_null(void)
Definition: jsval.h:130
#define __JSVAL_TYPE(x)
Definition: jsval.h:93
#define __JSVAL_OBJ(x)
Definition: jsval.h:96
static BOOL is_variant(jsval_t v)
Definition: jsval.h:209
void jsval_release(jsval_t)
Definition: jsutils.c:186
#define JSV_VAL(x)
Definition: jsval.h:41
static jsval_t jsval_string(jsstr_t *str)
Definition: jsval.h:109
static jsval_t jsval_undefined(void)
Definition: jsval.h:146
static jsval_t jsval_obj(jsdisp_t *obj)
Definition: jsval.h:125
static jsval_t jsval_bool(BOOL b)
Definition: jsval.h:101
#define __JSVAL_VAR(x)
Definition: jsval.h:97
static jsval_type_t jsval_type(jsval_t v)
Definition: jsval.h:219
HRESULT variant_to_jsval(script_ctx_t *, VARIANT *, jsval_t *)
Definition: jsutils.c:251
static BOOL is_null_disp(jsval_t v)
Definition: jsval.h:190
static jsstr_t * get_string(jsval_t v)
Definition: jsval.h:238
static jsval_t jsval_null_disp(void)
Definition: jsval.h:138
static BOOL is_undefined(jsval_t v)
Definition: jsval.h:180
static double get_number(jsval_t v)
Definition: jsval.h:233
static jsval_t jsval_disp(IDispatch *obj)
Definition: jsval.h:117
#define __JSVAL_BOOL(x)
Definition: jsval.h:94
static IDispatch * get_object(jsval_t v)
Definition: jsval.h:228
static BOOL is_object_instance(jsval_t v)
Definition: jsval.h:175
static BOOL is_null(jsval_t v)
Definition: jsval.h:185
HRESULT jsval_copy(jsval_t, jsval_t *)
Definition: jsutils.c:225
static jsval_t jsval_number(double n)
Definition: jsval.h:153
static BOOL get_bool(jsval_t v)
Definition: jsval.h:248
static BOOL is_bool(jsval_t v)
Definition: jsval.h:214
#define b
Definition: ke_i.h:79
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
const WCHAR * str
Definition: jsstr.h:36
Definition: jsval.h:54
double n
Definition: jsval.h:74
union _jsval_t::@441 u
jsval_type_t type
Definition: jsval.h:70
VARIANT * v
Definition: jsval.h:76
jsstr_t * str
Definition: jsval.h:73
BOOL b
Definition: jsval.h:75
IDispatch * obj
Definition: jsval.h:72
Definition: ecma_167.h:138