static void CreatePrintFn(){ char** argv = JsGcMalloc(sizeof(char*) * 1,NULL,NULL); argv[0] = "value"; //创建PrintFunction struct JsObject* print = JsCreateStandardSpecFunction(NULL,NULL,1,argv, &JsPrintFn,NULL,"print",FALSE); struct JsValue* vPrint = JsCreateValue(); vPrint->type = JS_OBJECT; vPrint->u.object = print; (*JsGetVm()->Global->Put)(JsGetVm()->Global,"print",vPrint,JS_OBJECT_ATTR_STRICT); }
/*15.3.4.4*/ static void JsFunctionProtoCallCall(struct JsObject *obj, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsObject* this0; int argc0; struct JsValue** argv0; if(thisobj == NULL){ JsThrowString("null point this"); return; } if(thisobj->Call == NULL){ JsThrowString("TypeError"); return; } if(argc == 0){ this0 = JsGetVm()->Global; argc0 = 0; argv0 = NULL; }else if(argc == 1){ if(argv[0]->type == JS_NULL || argv[0]->type == JS_UNDEFINED){ this0 = JsGetVm()->Global; }else{ struct JsValue o; JsToObject(argv[0],&o); this0 = o.u.object; } argc0 = 0; argv0 = NULL; }else if(argc >= 2){ //this if(argv[0]->type != JS_NULL && argv[0]->type != JS_UNDEFINED){ this0 = JsGetVm()->Global; }else{ struct JsValue o; JsToObject(argv[0],&o); this0 = o.u.object; } argc0 = argc - 1; argv0 = &argv[1]; }else{ //argc大小错误 JsAssert(FALSE); } (*thisobj->Call)(thisobj,this0,argc0,argv0,res); }
void JsPutValue(struct JsValue* v,struct JsValue* w,struct JsValue* res){ struct JsObject *target; res->type = JS_BOOLEAN; if (v->type != JS_REFERENCE){ JsThrowString("ReferenceError"); return; } target = v->u.reference.base; if (target == NULL) target = JsGetVm()->Global; (*target->Put)(target, v->u.reference.name, w, JS_OBJECT_ATTR_DEFAULT); res->u.boolean = TRUE; return; }
void JsToObject(struct JsValue *val, struct JsValue *res){ struct JsValue *argv[1]; struct JsValue con0; struct JsValue *con; argv[0] = val; res->type = JS_OBJECT; switch (val->type) { case JS_UNDEFINED: case JS_NULL: JsThrowString("TypeError"); break; case JS_OBJECT: *res = *val; //直接返回, 不需要构建了 return; case JS_BOOLEAN: JsFindValue(NULL,"Boolean",&con0); break; case JS_NUMBER: JsFindValue(NULL,"Number",&con0); break; case JS_STRING: JsFindValue(NULL,"String",&con0); break; default: JsThrowString("JsToObjectError"); return; } //变换 con = &con0; if( con->type != JS_OBJECT || con->u.object == NULL || con->u.object->Construct == NULL){ JsPrintString("TypeError"); //JsThrowString("TypeError"); //直接中断程序 JsAssert(FALSE); return; } //构造 (*con->u.object->Construct)(con->u.object,JsGetVm()->Global, 1, argv, res); }
/*15.3.4.3*/ static void JsFunctionProtoApplyCall(struct JsObject *obj, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ struct JsObject* this0; int argc0; struct JsValue** argv0; if(thisobj == NULL){ JsThrowString("null point this"); return; } if(thisobj->Call == NULL){ JsThrowString("TypeError"); return; } if(argc == 0){ this0 = JsGetVm()->Global; argc0 = 0; argv0 = NULL; }else if(argc == 1){ if(argv[0]->type == JS_NULL || argv[0]->type == JS_UNDEFINED){ this0 = JsGetVm()->Global; }else{ struct JsValue o; JsToObject(argv[0],&o); this0 = o.u.object; } argc0 = 0; argv0 = NULL; }else if(argc >= 2){ //this if(argv[0]->type == JS_NULL || argv[0]->type == JS_UNDEFINED){ this0 = JsGetVm()->Global; }else{ struct JsValue o; JsToObject(argv[0],&o); this0 = o.u.object; } //argc argv if(argv[1]->type == JS_NULL || argv[1]->type == JS_UNDEFINED){ argc0 = 0; argv0 = NULL; }else{ //测试argv[1]是否为OBJECT并且为Array类型 if(argv[1]->type != JS_OBJECT || strcmp(argv[1]->u.object->Class,"Array") !=0 ){ JsThrowString("TypeError"); return; } struct JsValue v ; (*argv[1]->u.object->Get)(argv[1]->u.object,"length",NULL,&v); if(v.type != JS_NUMBER){ JsThrowString("TypeError"); return; } argc0 = v.u.number; argv0 = (struct JsValue**)JsMalloc(sizeof(struct JsValue*)* argc0); //计算最大位数 int number = argc0; int size = 0; int i = 0; while(number){ number /=10; size++; } char* buf = (char*)JsMalloc(size + 4); //组建Argv0 for(i =0 ; i < argc0;++i){ sprintf(buf,"%d",i); (*argv[1]->u.object->Get)(argv[1]->u.object,buf,NULL,&v); argv0[i] = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); *argv0[i] = v; } } }else{ //argc大小错误 JsAssert(FALSE); } (*thisobj->Call)(thisobj,this0,argc0,argv0,res); }
/*15.2.4.4*/ static void JsObjectProtoValueOfCall(struct JsObject *obj, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ res->type = JS_OBJECT; res->u.object = thisobj == NULL? JsGetVm()->Global : thisobj; }