/*15.2.4.7*/ static void JsObjectProtoPropertyIsEnumerableCall(struct JsObject *obj, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ res->type = JS_BOOLEAN; char* prop = "undefined"; int flag; struct JsValue v; //prop查询 if(argc >= 0){ JsToString(argv[0],&v); prop = v.u.string; } (*thisobj->HasOwnProperty)(thisobj,prop,&v); if(v.u.boolean == TRUE){ //存在属性 (*thisobj->Get)(thisobj,prop,&flag,&v); if(v.type == JS_UNDEFINED) res->u.boolean = FALSE; else res->u.boolean = ((flag & JS_OBJECT_ATTR_DONTENUM) == 0 ? TRUE : FALSE); } //该对象不存在该属性 res->u.boolean = FALSE; }
static void JsStringProtoCharAt(struct JsObject *self, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsValue v; struct JsValue pos; struct JsValue shell; shell.type = JS_OBJECT; shell.u.object = thisobj; JsToString(&shell,&v); if(argv <= 0){ pos.u.number = 0; }else{ JsToInteger(argv[0],&pos); } int length =strlen(v.u.string); if(pos.u.number < 0 || pos.u.number >= length){ //return empty res->type = JS_STRING; res->u.string = ""; }else{ res->type =JS_STRING; res->u.string = (char*)JsMalloc(4); res->u.string[0] = v.u.string[(int)(pos.u.number)]; res->u.string[1] = '\0'; } }
static void JsStringConstCall(struct JsObject *self, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ if(argc <= 0){ res->type = JS_STRING; res->u.string =""; return; } JsToString(argv[0],res); }
/*15.2.4.5*/ static void JsObjectProtoHasOwnPropertyCall(struct JsObject *obj, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsValue v; char* prop = "undefined"; if(argc >0 ){ JsToString(argv[0],&v); (*thisobj->HasOwnProperty)(thisobj,v.u.string,res); prop = v.u.string; } (*thisobj->HasOwnProperty)(thisobj,prop,res); }
static void JsStringProtoConcat(struct JsObject *self, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsValue v; struct JsValue r; struct JsValue shell; shell.type = JS_OBJECT; shell.u.object =thisobj; JsToString(&shell,&v); r.type = JS_STRING; int sizeOfMem = strlen(v.u.string)+4; r.u.string = (char*)JsMalloc(sizeOfMem); strcpy(r.u.string,v.u.string); int i; for(i=0;i< argc;++i){ JsToString(argv[i],&v); sizeOfMem += strlen(v.u.string); r.u.string = (char*)JsReAlloc(r.u.string,sizeOfMem); strcat(r.u.string,v.u.string); } *res = r; }
static void JsStringConstConstruct(struct JsObject *self, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsValue v; struct JsValue v0; (*self->Get)(self,"prototype",NULL,&v); struct JsObject* string; if(argc<=0){ string = JsCreateStringObject(v.u.object,NULL); }else{ JsToString(argv[0],&v0); string = JsCreateStringObject(v.u.object,v0.u.string); } res->type = JS_OBJECT; res->u.object = string; }
void JsToString(struct JsValue *val, struct JsValue *res){ res->type = JS_STRING; switch (val->type) { case JS_UNDEFINED: res->u.string = "undefined"; break; case JS_NULL: res->u.string = "null"; break; case JS_BOOLEAN:{ if(val->u.boolean == TRUE) res->u.string ="true"; else res->u.string = "false"; break; } case JS_NUMBER:{ /* 9.8.1 */ char* buf; double value ; value = val->u.number; //最大长度为128位 buf = (char*)JsMalloc(128); if(value - (int)value == 0){ //整数 sprintf(buf,"%d",(int)value); }else{ sprintf(buf,"%f",value); } res->u.string = buf; break; } case JS_STRING: *res = *val; break; case JS_OBJECT:{ struct JsValue prim; JsToPrimitive(val,JS_OBJECT_HIT_TYPE_STRING, &prim); JsToString(&prim, res); break; } default: JsThrowString("JsToStringError"); } }
/* 最后的格式为 string,string...,string */ static void JsArrayProtoJoin(struct JsObject *self, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsValue r; struct JsValue s; struct JsValue v; int length; char* sep = ","; //长度获取 (*thisobj->Get)(thisobj,"length",NULL,&v); if(v.type == JS_UNDEFINED || v.u.number <= 0){ res->type = JS_STRING; res->u.string = ""; return; } length = v.u.number; if(argc >= 1 && argv[0]->type != JS_UNDEFINED){ JsToString(argv[0],&v); sep = v.u.string; } //sep 的长度 int sepLength = strlen(sep); // "0" (*thisobj->Get)(thisobj,"0",NULL,&v); if(v.type == JS_NULL || v.type == JS_UNDEFINED){ s.type = JS_STRING; s.u.string =""; }else{ JsToString(&v,&s); } r.type = JS_STRING; int sizeOfMem = strlen(s.u.string)+sepLength+4; r.u.string = (char*)JsGcMalloc(sizeOfMem,NULL,NULL); strcpy(r.u.string,s.u.string); int k = 1; r10: if(length == k){ *res = r; return; } int number = k; int bit = 0 ; while(number){ number /= 10; bit ++; } char* cur = (char*)JsGcMalloc(bit+4,NULL,NULL); sprintf(cur,"%d",k); (*thisobj->Get)(thisobj,cur,NULL,&v); if(v.type == JS_UNDEFINED|| v.type == JS_NULL){ }else{ JsToString(&v,&s); sizeOfMem = sizeOfMem + sepLength + strlen(s.u.string); r.u.string = (char*)JsGcReAlloc(r.u.string,sizeOfMem); strcat(r.u.string,sep); strcat(r.u.string,s.u.string); } k++; goto r10; }