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