Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7641 views
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_CITERATOR,
40
JS_CUSERDATA,
41
};
42
43
/*
44
Short strings abuse the js_Value struct. By putting the type tag in the
45
last byte, and using 0 as the tag for short strings, we can use the
46
entire js_Value as string storage by letting the type tag serve double
47
purpose as the string zero terminator.
48
*/
49
50
struct js_Value
51
{
52
union {
53
int boolean;
54
double number;
55
char shrstr[8];
56
const char *litstr;
57
js_String *memstr;
58
js_Object *object;
59
} u;
60
char pad[7]; /* extra storage for shrstr */
61
char type; /* type tag and zero terminator for shrstr */
62
};
63
64
struct js_String
65
{
66
js_String *gcnext;
67
char gcmark;
68
char p[1];
69
};
70
71
struct js_Regexp
72
{
73
void *prog;
74
const char *source;
75
unsigned short flags;
76
unsigned short last;
77
};
78
79
struct js_Object
80
{
81
enum js_Class type;
82
int extensible;
83
js_Property *properties;
84
js_Property *head, **tailp; /* for enumeration */
85
unsigned 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
unsigned int length;
93
} s;
94
struct {
95
unsigned 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
unsigned 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_Finalize finalize;
116
} user;
117
} u;
118
js_Object *gcnext;
119
int gcmark;
120
};
121
122
struct js_Property
123
{
124
const char *name;
125
js_Property *left, *right;
126
js_Property *next, **prevp; /* for enumeration */
127
int level;
128
int atts;
129
js_Value value;
130
js_Object *getter;
131
js_Object *setter;
132
};
133
134
struct js_Iterator
135
{
136
const char *name;
137
js_Iterator *next;
138
};
139
140
/* jsrun.c */
141
js_String *jsV_newmemstring(js_State *J, const char *s, int n);
142
js_Value *js_tovalue(js_State *J, int idx);
143
void js_toprimitive(js_State *J, int idx, int hint);
144
js_Object *js_toobject(js_State *J, int idx);
145
void js_pushvalue(js_State *J, js_Value v);
146
void js_pushobject(js_State *J, js_Object *v);
147
148
/* jsvalue.c */
149
int jsV_toboolean(js_State *J, js_Value *v);
150
double jsV_tonumber(js_State *J, js_Value *v);
151
double jsV_tointeger(js_State *J, js_Value *v);
152
const char *jsV_tostring(js_State *J, js_Value *v);
153
js_Object *jsV_toobject(js_State *J, js_Value *v);
154
void jsV_toprimitive(js_State *J, js_Value *v, int preferred);
155
156
const char *js_itoa(char buf[32], unsigned int a);
157
double js_stringtofloat(const char *s, char **ep);
158
double jsV_numbertointeger(double n);
159
int jsV_numbertoint32(double n);
160
unsigned int jsV_numbertouint32(double n);
161
short jsV_numbertoint16(double n);
162
unsigned short jsV_numbertouint16(double n);
163
const char *jsV_numbertostring(js_State *J, char buf[32], double number);
164
double jsV_stringtonumber(js_State *J, const char *string);
165
166
/* jsproperty.c */
167
js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype);
168
js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name);
169
js_Property *jsV_getpropertyx(js_State *J, js_Object *obj, const char *name, int *own);
170
js_Property *jsV_getproperty(js_State *J, js_Object *obj, const char *name);
171
js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name);
172
js_Property *jsV_nextproperty(js_State *J, js_Object *obj, const char *name);
173
void jsV_delproperty(js_State *J, js_Object *obj, const char *name);
174
175
js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own);
176
const char *jsV_nextiterator(js_State *J, js_Object *iter);
177
178
void jsV_resizearray(js_State *J, js_Object *obj, unsigned int newlen);
179
180
/* jsdump.c */
181
void js_dumpobject(js_State *J, js_Object *obj);
182
void js_dumpvalue(js_State *J, js_Value v);
183
184
#endif
185
186