1 | #ifndef js_value_h |
2 | #define js_value_h |
3 | |
4 | typedef struct js_Property js_Property; |
5 | typedef struct js_Iterator js_Iterator; |
6 | |
7 | /* Hint to ToPrimitive() */ |
8 | enum { |
9 | JS_HNONE, |
10 | JS_HNUMBER, |
11 | JS_HSTRING |
12 | }; |
13 | |
14 | enum js_Type { |
15 | JS_TSHRSTR, /* type tag doubles as string zero-terminator */ |
16 | JS_TUNDEFINED, |
17 | JS_TNULL, |
18 | JS_TBOOLEAN, |
19 | JS_TNUMBER, |
20 | JS_TLITSTR, |
21 | JS_TMEMSTR, |
22 | JS_TOBJECT, |
23 | }; |
24 | |
25 | enum js_Class { |
26 | JS_COBJECT, |
27 | JS_CARRAY, |
28 | JS_CFUNCTION, |
29 | JS_CSCRIPT, /* function created from global/eval code */ |
30 | JS_CCFUNCTION, /* built-in function */ |
31 | JS_CERROR, |
32 | JS_CBOOLEAN, |
33 | JS_CNUMBER, |
34 | JS_CSTRING, |
35 | JS_CREGEXP, |
36 | JS_CDATE, |
37 | JS_CMATH, |
38 | JS_CJSON, |
39 | JS_CARGUMENTS, |
40 | JS_CITERATOR, |
41 | JS_CUSERDATA, |
42 | }; |
43 | |
44 | /* |
45 | Short strings abuse the js_Value struct. By putting the type tag in the |
46 | last byte, and using 0 as the tag for short strings, we can use the |
47 | entire js_Value as string storage by letting the type tag serve double |
48 | purpose as the string zero terminator. |
49 | */ |
50 | |
51 | struct js_Value |
52 | { |
53 | union { |
54 | int boolean; |
55 | double number; |
56 | char shrstr[8]; |
57 | const char *litstr; |
58 | js_String *memstr; |
59 | js_Object *object; |
60 | } u; |
61 | char pad[7]; /* extra storage for shrstr */ |
62 | char type; /* type tag and zero terminator for shrstr */ |
63 | }; |
64 | |
65 | struct js_String |
66 | { |
67 | js_String *gcnext; |
68 | char gcmark; |
69 | char p[1]; |
70 | }; |
71 | |
72 | struct js_Regexp |
73 | { |
74 | void *prog; |
75 | char *source; |
76 | unsigned short flags; |
77 | unsigned short last; |
78 | }; |
79 | |
80 | struct js_Object |
81 | { |
82 | enum js_Class type; |
83 | int extensible; |
84 | js_Property *properties; |
85 | int count; /* number of properties, for array sparseness check */ |
86 | js_Object *prototype; |
87 | union { |
88 | int boolean; |
89 | double number; |
90 | struct { |
91 | const char *string; |
92 | int length; |
93 | } s; |
94 | struct { |
95 | int length; |
96 | } a; |
97 | struct { |
98 | js_Function *function; |
99 | js_Environment *scope; |
100 | } f; |
101 | struct { |
102 | const char *name; |
103 | js_CFunction function; |
104 | js_CFunction constructor; |
105 | int length; |
106 | } c; |
107 | js_Regexp r; |
108 | struct { |
109 | js_Object *target; |
110 | js_Iterator *head; |
111 | } iter; |
112 | struct { |
113 | const char *tag; |
114 | void *data; |
115 | js_HasProperty has; |
116 | js_Put put; |
117 | js_Delete delete; |
118 | js_Finalize finalize; |
119 | } user; |
120 | } u; |
121 | js_Object *gcnext; |
122 | int gcmark; |
123 | }; |
124 | |
125 | struct js_Property |
126 | { |
127 | const char *name; |
128 | js_Property *left, *right; |
129 | int level; |
130 | int atts; |
131 | js_Value value; |
132 | js_Object *getter; |
133 | js_Object *setter; |
134 | }; |
135 | |
136 | struct js_Iterator |
137 | { |
138 | const char *name; |
139 | js_Iterator *next; |
140 | }; |
141 | |
142 | /* jsrun.c */ |
143 | js_String *jsV_newmemstring(js_State *J, const char *s, int n); |
144 | js_Value *js_tovalue(js_State *J, int idx); |
145 | void js_toprimitive(js_State *J, int idx, int hint); |
146 | js_Object *js_toobject(js_State *J, int idx); |
147 | void js_pushvalue(js_State *J, js_Value v); |
148 | void js_pushobject(js_State *J, js_Object *v); |
149 | |
150 | /* jsvalue.c */ |
151 | int jsV_toboolean(js_State *J, js_Value *v); |
152 | double jsV_tonumber(js_State *J, js_Value *v); |
153 | double jsV_tointeger(js_State *J, js_Value *v); |
154 | const char *jsV_tostring(js_State *J, js_Value *v); |
155 | js_Object *jsV_toobject(js_State *J, js_Value *v); |
156 | void jsV_toprimitive(js_State *J, js_Value *v, int preferred); |
157 | |
158 | const char *js_itoa(char buf[32], int a); |
159 | double js_stringtofloat(const char *s, char **ep); |
160 | int jsV_numbertointeger(double n); |
161 | int jsV_numbertoint32(double n); |
162 | unsigned int jsV_numbertouint32(double n); |
163 | short jsV_numbertoint16(double n); |
164 | unsigned short jsV_numbertouint16(double n); |
165 | const char *jsV_numbertostring(js_State *J, char buf[32], double number); |
166 | double jsV_stringtonumber(js_State *J, const char *string); |
167 | |
168 | /* jsproperty.c */ |
169 | js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype); |
170 | js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name); |
171 | js_Property *jsV_getpropertyx(js_State *J, js_Object *obj, const char *name, int *own); |
172 | js_Property *jsV_getproperty(js_State *J, js_Object *obj, const char *name); |
173 | js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name); |
174 | js_Property *jsV_nextproperty(js_State *J, js_Object *obj, const char *name); |
175 | void jsV_delproperty(js_State *J, js_Object *obj, const char *name); |
176 | |
177 | js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own); |
178 | const char *jsV_nextiterator(js_State *J, js_Object *iter); |
179 | |
180 | void jsV_resizearray(js_State *J, js_Object *obj, int newlen); |
181 | |
182 | /* jsdump.c */ |
183 | void js_dumpobject(js_State *J, js_Object *obj); |
184 | void js_dumpvalue(js_State *J, js_Value v); |
185 | |
186 | #endif |
187 | |