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 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 int count; /* number of properties, for array sparseness check */ 85 js_Object *prototype; 86 union { 87 int boolean; 88 double number; 89 struct { 90 const char *string; 91 int length; 92 } s; 93 struct { 94 int length; 95 } a; 96 struct { 97 js_Function *function; 98 js_Environment *scope; 99 } f; 100 struct { 101 const char *name; 102 js_CFunction function; 103 js_CFunction constructor; 104 int length; 105 } c; 106 js_Regexp r; 107 struct { 108 js_Object *target; 109 js_Iterator *head; 110 } iter; 111 struct { 112 const char *tag; 113 void *data; 114 js_HasProperty has; 115 js_Put put; 116 js_Delete delete; 117 js_Finalize finalize; 118 } user; 119 } u; 120 js_Object *gcnext; 121 int gcmark; 122 }; 123 124 struct js_Property 125 { 126 const char *name; 127 js_Property *left, *right; 128 int level; 129 int atts; 130 js_Value value; 131 js_Object *getter; 132 js_Object *setter; 133 }; 134 135 struct js_Iterator 136 { 137 const char *name; 138 js_Iterator *next; 139 }; 140 141 /* jsrun.c */ 142 js_String *jsV_newmemstring(js_State *J, const char *s, int n); 143 js_Value *js_tovalue(js_State *J, int idx); 144 void js_toprimitive(js_State *J, int idx, int hint); 145 js_Object *js_toobject(js_State *J, int idx); 146 void js_pushvalue(js_State *J, js_Value v); 147 void js_pushobject(js_State *J, js_Object *v); 148 149 /* jsvalue.c */ 150 int jsV_toboolean(js_State *J, js_Value *v); 151 double jsV_tonumber(js_State *J, js_Value *v); 152 double jsV_tointeger(js_State *J, js_Value *v); 153 const char *jsV_tostring(js_State *J, js_Value *v); 154 js_Object *jsV_toobject(js_State *J, js_Value *v); 155 void jsV_toprimitive(js_State *J, js_Value *v, int preferred); 156 157 const char *js_itoa(char buf[32], int a); 158 double js_stringtofloat(const char *s, char **ep); 159 int jsV_numbertointeger(double n); 160 int jsV_numbertoint32(double n); 161 unsigned int jsV_numbertouint32(double n); 162 short jsV_numbertoint16(double n); 163 unsigned short jsV_numbertouint16(double n); 164 const char *jsV_numbertostring(js_State *J, char buf[32], double number); 165 double jsV_stringtonumber(js_State *J, const char *string); 166 167 /* jsproperty.c */ 168 js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype); 169 js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name); 170 js_Property *jsV_getpropertyx(js_State *J, js_Object *obj, const char *name, int *own); 171 js_Property *jsV_getproperty(js_State *J, js_Object *obj, const char *name); 172 js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name); 173 js_Property *jsV_nextproperty(js_State *J, js_Object *obj, const char *name); 174 void jsV_delproperty(js_State *J, js_Object *obj, const char *name); 175 176 js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own); 177 const char *jsV_nextiterator(js_State *J, js_Object *iter); 178 179 void jsV_resizearray(js_State *J, js_Object *obj, int newlen); 180 181 /* jsdump.c */ 182 void js_dumpobject(js_State *J, js_Object *obj); 183 void js_dumpvalue(js_State *J, js_Value v); 184 185 #endif 186