/* Js: setHeader(String,String): 是一个通用的标头设定方法,您可以用它来设定任何「名称/值」的标头. setStatus(Number): 状态码 write(String) : 写入返回文本 */ struct JsObject* JsCreateResponse(struct MHD_Connection *connection){ struct JsObject* b = JsAllocObject(JS_RESPONSE_FLOOR); JsCreateStandardObject(b); b->Class = "Response"; //初始化自己的Sb struct JsResponse* p = (struct JsResponse*)malloc(sizeof(struct JsResponse)); p->connection = connection; p->code = 200; p->bUsed = 0; p->bTotal = 128; p->body = calloc(sizeof(char),p->bTotal); p->hTotal = 2; p->hUsed = 0; p->header = (struct JsHeader* )calloc(sizeof(struct JsHeader) ,p->hTotal); b->sb[JS_RESPONSE_FLOOR] = p; //初始化Object方法函数 JsInitResponse(b); return b; }
//---------------------------------------------------------------------------- //创建一个Array对象, prototype 为NULL表示指向 Object_prototype //如果argv == NULL 表示不需要添加具体的元素 static struct JsObject* JsCreateArray(struct JsObject* prototype, int length, struct JsValue** argv){ //创建一个标准的对象 struct JsObject* array = JsCreateStandardObject(NULL); if(prototype != NULL) array->Prototype = prototype; struct JsValue* vLength = JsCreateValue(); vLength->type = JS_NUMBER; vLength->u.number = length; array->Class = "Array"; (*array->Put)(array,"length",vLength,JS_OBJECT_ATTR_DONTENUM | JS_OBJECT_ATTR_DONTDELETE); //构建[item...] int i; for( i = 0 ; i < length && argv != NULL ;++i){ //计算bit int number = i; int bit = 0; while(number){ number /=10; bit++; } char* buf = (char*)JsGcMalloc(bit+4,NULL,NULL); sprintf(buf,"%d",i); (*array->Put)(array,buf,argv[i],JS_OBJECT_ATTR_DEFAULT); } //修改Put array->Put = &JsArrayInstPut; return array; }
void JsECMAScriptObjectInit(struct JsVm* vm) { struct JsValue t ; struct JsValue* v; //初始化vm->Global对象 struct JsObject* Global = JsCreateStandardObject(NULL); vm->Global = Global; JsOFInit(vm); JsArrayInit(vm); JsStringInit(vm); JsBooleanInit(vm); JsNumberInit(vm); //配置Global //Gloabl->Prototype (*vm->Global->Get)(vm->Global,"Object",NULL,&t); if(t.type != JS_OBJECT) JsAssert(FALSE); (*t.u.object->Get)(t.u.object,"prototype",NULL,&t); vm->Global->Prototype = t.u.object; //Global->Class vm->Global->Class = "Global"; //NaN v = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); v->type = JS_NUMBER; v->u.number = JS_VALUE_NUMBER_NAN; (*vm->Global->Put)(vm->Global,"NaN",v,JS_OBJECT_ATTR_STRICT); //undefined v = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); v->type = JS_UNDEFINED; (*vm->Global->Put)(vm->Global,"undefined",v,JS_OBJECT_ATTR_STRICT); //eval v = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); v->type = JS_OBJECT; v->u.object = JsCreateStandardFunctionObject(NULL,NULL,FALSE); v->u.object->Call = &JsGlobalEval; (*vm->Global->Put)(vm->Global,"eval",v,JS_OBJECT_ATTR_STRICT); //isNaN v = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); v->type = JS_OBJECT; v->u.object = JsCreateStandardFunctionObject(NULL,NULL,FALSE); v->u.object->Call = &JsIsNaN; (*vm->Global->Put)(vm->Global,"isNaN",v,JS_OBJECT_ATTR_STRICT); }
/*15.2.2.1*/ static void JsObjectCall(struct JsObject *obj, struct JsObject *thisobj, int argc, struct JsValue **argv, struct JsValue *res){ if(argc > 0){ if(argv[0]->type == JS_OBJECT){ //TODO rule4 *res = *argv[0]; return; } if(argv[0]->type == JS_STRING ||argv[0]->type == JS_BOOLEAN || argv[0]->type == JS_NUMBER){ JsToObject(argv[0],res); return; } } struct JsObject* t = JsCreateStandardObject(NULL); res->type = JS_OBJECT; res->u.object = t; return; }
void JsOFInit(struct JsVm* vm){ struct JsValue* vPrototype; struct JsObject* obj = JsCreateStandardFunctionObject(NULL,NULL,TRUE); struct JsObject* obj_proto = JsCreateStandardObject(NULL); struct JsObject* fun = JsCreateStandardFunctionObject(NULL,NULL,TRUE); //不存在construct和prototype属性 struct JsObject* fun_proto = JsCreateStandardFunctionObject(NULL,NULL,FALSE); //预先配置他们的prototype属性, 给CreateObject类型函数使用 vPrototype = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); vPrototype->type = JS_OBJECT; vPrototype->u.object = obj_proto; (*obj->Put)(obj,"prototype",vPrototype,JS_OBJECT_ATTR_STRICT); vPrototype = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); vPrototype->type = JS_OBJECT; vPrototype->u.object = fun_proto; (*fun->Put)(fun,"prototype",vPrototype,JS_OBJECT_ATTR_STRICT); //向Global添加Object和Function struct JsValue* vObject = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); vObject->type = JS_OBJECT; vObject->u.object = obj; struct JsValue* vFunction = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); vFunction->type = JS_OBJECT; vFunction->u.object = fun; (*vm->Global->Put)(vm->Global,"Object",vObject,JS_OBJECT_ATTR_STRICT); (*vm->Global->Put)(vm->Global,"Function",vFunction,JS_OBJECT_ATTR_STRICT); //到现在已经可以正常的使用CreateObject 类型函数, Prototype属性不会缺失. //注意 上述四个对象的Prototype属性依旧为NULL, 且obj_proto->Prototype = NULL; JsObjectInit(obj,obj_proto,fun,fun_proto); JsObjectProtoInit(obj,obj_proto,fun,fun_proto); JsFunctionInit(obj,obj_proto,fun,fun_proto); JsFunctionProtoInit(obj,obj_proto,fun,fun_proto); }
static struct JsObject* JsCreateStringObject(struct JsObject* prototype,char* str){ struct JsObject* string = JsAllocObject(JS_STRING_FLOOR); JsCreateStandardObject(string); string->Class = "String"; string->Get = &JsStringInstGet; if(str == NULL) str = ""; //初始化自己的Sb char* p = (char*)JsMalloc(strlen(str)+4); strcpy(p,str); string->sb[JS_STRING_FLOOR] = p; //length struct JsValue* vLength = (struct JsValue*)JsMalloc(sizeof(struct JsValue)); vLength->type = JS_NUMBER; vLength->u.number = strlen(str); (*string->Put)(string,"length",vLength,JS_OBJECT_ATTR_STRICT); //为了能把length Put进去, 把prototype延迟放入. if(prototype != NULL) string->Prototype = prototype; return string; }