function.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,022 行 · 第 1/2 页
CPP
1,022 行
} return InternalFunctionImp::get(exec, propertyName);}void FunctionImp::put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr){ if (propertyName == argumentsPropertyName || propertyName == lengthPropertyName) return; InternalFunctionImp::put(exec, propertyName, value, attr);}bool FunctionImp::hasProperty(ExecState *exec, const Identifier &propertyName) const{ if (propertyName == argumentsPropertyName || propertyName == lengthPropertyName) return true; return InternalFunctionImp::hasProperty(exec, propertyName);}bool FunctionImp::deleteProperty(ExecState *exec, const Identifier &propertyName){ if (propertyName == argumentsPropertyName || propertyName == lengthPropertyName) return false; return InternalFunctionImp::deleteProperty(exec, propertyName);}// ------------------------------ DeclaredFunctionImp --------------------------// ### is "Function" correct here?const ClassInfo DeclaredFunctionImp::info = {"Function", &FunctionImp::info, 0, 0};DeclaredFunctionImp::DeclaredFunctionImp(ExecState *exec, const Identifier &n, FunctionBodyNode *b, const ScopeChain &sc) : FunctionImp(exec,n), body(b){ Value protect(this); body->ref(); setScope(sc); line0 = body->firstLine(); line1 = body->lastLine(); sid = body->sourceId();}DeclaredFunctionImp::~DeclaredFunctionImp(){ if ( body->deref() ) delete body;}bool DeclaredFunctionImp::implementsConstruct() const{ return true;}// ECMA 13.2.2 [[Construct]]Object DeclaredFunctionImp::construct(ExecState *exec, const List &args){ Object proto; Value p = get(exec,prototypePropertyName); if (p.type() == ObjectType) proto = Object(static_cast<ObjectImp*>(p.imp())); else proto = exec->lexicalInterpreter()->builtinObjectPrototype(); Object obj(new ObjectImp(proto)); Value res = call(exec,obj,args); if (res.type() == ObjectType) return Object::dynamicCast(res); else return obj;}Completion DeclaredFunctionImp::execute(ExecState *exec){ Completion result = body->execute(exec); if (result.complType() == Throw || result.complType() == ReturnValue) return result; return Completion(Normal, Undefined()); // TODO: or ReturnValue ?}void DeclaredFunctionImp::processVarDecls(ExecState *exec){ body->processVarDecls(exec);}// ------------------------------- ShadowImp -----------------------------------namespace KJS {// Acts as a placeholder value to indicate that the actual value is kept// in the activation objectclass ShadowImp : public ObjectImp {public: ShadowImp(ObjectImp *_obj, Identifier _prop) : obj(_obj), prop(_prop) {} virtual void mark(); virtual const ClassInfo *classInfo() const { return &info; } static const ClassInfo info; ObjectImp *obj; Identifier prop;};/*KDE_NOEXPORT*/ const ClassInfo ShadowImp::info = {"Shadow", 0, 0, 0};void ShadowImp::mark(){ ObjectImp::mark(); if (!obj->marked()) obj->mark();}}// ------------------------------ ArgumentsImp ---------------------------------const ClassInfo ArgumentsImp::info = {"Arguments", 0, 0, 0};// ECMA 10.1.8ArgumentsImp::ArgumentsImp(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act) : ObjectImp(exec->lexicalInterpreter()->builtinObjectPrototype()), activation(act){ Value protect(this); putDirect(calleePropertyName, func, DontEnum); putDirect(lengthPropertyName, args.size(), DontEnum); if (!args.isEmpty()) { ListIterator arg = args.begin(); for (int i = 0; arg != args.end(); arg++, i++) { Identifier prop = func->parameterProperty(i); if (!prop.isEmpty()) { Object shadow(new ShadowImp(act,prop)); ObjectImp::put(exec,Identifier::from(i), shadow, DontEnum); } else { ObjectImp::put(exec,Identifier::from(i), *arg, DontEnum); } } }}void ArgumentsImp::mark(){ ObjectImp::mark(); if (!activation->marked()) activation->mark();}Value ArgumentsImp::get(ExecState *exec, const Identifier &propertyName) const{ Value val = ObjectImp::get(exec,propertyName); assert(SimpleNumber::is(val.imp()) || !val.imp()->isDestroyed()); Object obj = Object::dynamicCast(val); if (obj.isValid() && obj.inherits(&ShadowImp::info)) { ShadowImp *shadow = static_cast<ShadowImp*>(val.imp()); return activation->get(exec,shadow->prop); } else { return val; }}void ArgumentsImp::put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr){ Value val = ObjectImp::get(exec,propertyName); Object obj = Object::dynamicCast(val); if (obj.isValid() && obj.inherits(&ShadowImp::info)) { ShadowImp *shadow = static_cast<ShadowImp*>(val.imp()); activation->put(exec,shadow->prop,value,attr); } else { ObjectImp::put(exec,propertyName,value,attr); }}// ------------------------------ ActivationImp --------------------------------const ClassInfo ActivationImp::info = {"Activation", 0, 0, 0};// ECMA 10.1.6ActivationImp::ActivationImp(FunctionImp *function, const List &arguments) : _function(function), _arguments(true), _argumentsObject(0){ _arguments = arguments.copy(); // FIXME: Do we need to support enumerating the arguments property?}Value ActivationImp::get(ExecState *exec, const Identifier &propertyName) const{ if (propertyName == argumentsPropertyName) { // check for locally declared arguments property ValueImp *v = getDirect(propertyName); if (v) return Value(v); // default: return builtin arguments array if (!_argumentsObject) _argumentsObject = new ArgumentsImp(exec, _function, _arguments, const_cast<ActivationImp*>(this)); return Value(_argumentsObject); } return ObjectImp::get(exec, propertyName);}bool ActivationImp::hasProperty(ExecState *exec, const Identifier &propertyName) const{ if (propertyName == argumentsPropertyName) return true; return ObjectImp::hasProperty(exec, propertyName);}bool ActivationImp::deleteProperty(ExecState *exec, const Identifier &propertyName){ if (propertyName == argumentsPropertyName) return false; return ObjectImp::deleteProperty(exec, propertyName);}void ActivationImp::mark(){ ObjectImp::mark(); if (_function && !_function->marked()) _function->mark(); _arguments.mark(); if (_argumentsObject && !_argumentsObject->marked()) _argumentsObject->mark();}// ------------------------------ GlobalFunc -----------------------------------GlobalFuncImp::GlobalFuncImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto, int i, int len, const Identifier &_ident) : InternalFunctionImp(funcProto), id(i){ Value protect(this); putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum); ident = _ident;}CodeType GlobalFuncImp::codeType() const{ return id == Eval ? EvalCode : codeType();}bool GlobalFuncImp::implementsCall() const{ return true;}Value GlobalFuncImp::call(ExecState *exec, Object &thisObj, const List &args){ Value res; static const char do_not_escape[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" "*+-./@_"; switch (id) { case Eval: { // eval() Value x = args[0]; if (x.type() != StringType) return x; else { UString s = x.toString(exec); int errLine; UString errMsg;#ifdef KJS_VERBOSE fprintf(stderr, "eval(): %s\n", s.ascii());#endif SourceCode *source; FunctionBodyNode *progNode = Parser::parse(s.data(),s.size(),&source,&errLine,&errMsg); // notify debugger that source has been parsed Debugger *dbg = exec->interpreter()->imp()->debugger(); if (dbg) { bool cont = dbg->sourceParsed(exec,source->sid,s,errLine); if (!cont) { source->deref(); dbg->imp()->abort(); if (progNode) delete progNode; return Undefined(); } } exec->interpreter()->imp()->addSourceCode(source); // no program node means a syntax occurred if (!progNode) { Object err = Error::create(exec,SyntaxError,errMsg.ascii(),errLine); err.put(exec,"sid",Number(source->sid)); exec->setException(err); source->deref(); return err; } source->deref(); progNode->ref(); // enter a new execution context ContextImp ctx(exec->dynamicInterpreter()->globalObject(), exec->dynamicInterpreter()->imp(), thisObj, source->sid, EvalCode, exec->context().imp()); ExecState newExec(exec->dynamicInterpreter(), &ctx); newExec.setException(exec->exception()); // could be null ctx.setLines(progNode->firstLine(),progNode->firstLine()); if (dbg) { if (!dbg->enterContext(&newExec)) { // debugger requested we stop execution dbg->imp()->abort(); if (progNode->deref()) delete progNode; return Undefined(); } } // execute the code progNode->processVarDecls(&newExec); Completion c = progNode->execute(&newExec); res = Undefined(); ctx.setLines(progNode->lastLine(),progNode->lastLine()); if (dbg && !dbg->exitContext(&newExec,c)) // debugger requested we stop execution dbg->imp()->abort(); else if (newExec.hadException()) // propagate back to parent context exec->setException(newExec.exception()); else if (c.complType() == Throw) exec->setException(c.value()); else if (c.isValueCompletion()) res = c.value(); if (progNode->deref()) delete progNode; return res; } break; } case ParseInt: { // ECMA 15.1.2.2 CString cstr = args[0].toString(exec).cstring(); const char* startptr = cstr.c_str(); while ( *startptr && isspace( *startptr ) ) // first, skip leading spaces ++startptr; int base = 0; if (args.size() > 1) base = args[1].toInt32(exec); double sign = 1; if (*startptr == '-') { sign = -1; startptr++; } else if (*startptr == '+') { sign = 1; startptr++; } bool leading0 = false; if ((base == 0 || base == 16) && (*startptr == '0' && (startptr[1] == 'x' || startptr[1] == 'X'))) { startptr += 2; base = 16; } else if (base == 0 && *startptr == '0') { base = 8; leading0 = true; startptr++; } else if (base == 0) { base = 10; } if (base < 2 || base > 36) { res = Number(NaN); } else { long double val = 0; int index = 0; for (; *startptr; startptr++) { int thisval = -1; if (*startptr >= '0' && *startptr <= '9') thisval = *startptr - '0'; else if (*startptr >= 'a' && *startptr <= 'z') thisval = 10 + *startptr - 'a'; else if (*startptr >= 'A' && *startptr <= 'Z') thisval = 10 + *startptr - 'A'; if (thisval < 0 || thisval >= base) break; val *= base; val += thisval; index++; } if (index == 0 && !leading0) res = Number(NaN); else res = Number(double(val)*sign); } break; } case ParseFloat: { UString str = args[0].toString(exec); // don't allow hex numbers here bool isHex = false; if (str.is8Bit()) { const char *c = str.ascii(); while (isspace(*c)) c++; isHex = (c[0] == '0' && (c[1] == 'x' || c[1] == 'X')); } if (isHex) res = Number(0); else res = Number(str.toDouble( true /*tolerant*/, false )); } break; case IsNaN: res = Boolean(isNaN(args[0].toNumber(exec))); break; case IsFinite: { double n = args[0].toNumber(exec); res = Boolean(!isNaN(n) && !isInf(n)); break; } case DecodeURI: res = String(decodeURI(exec,args[0].toString(exec),uriReserved+"#")); break; case DecodeURIComponent: res = String(decodeURI(exec,args[0].toString(exec),"")); break; case EncodeURI: res = String(encodeURI(exec,args[0].toString(exec),uriReserved+uriUnescaped+"#")); break; case EncodeURIComponent: res = String(encodeURI(exec,args[0].toString(exec),uriUnescaped)); break; case Escape: { UString r = "", s, str = args[0].toString(exec); const UChar *c = str.data(); for (int k = 0; k < str.size(); k++, c++) { int u = c->uc; if (u > 255) { char tmp[7]; sprintf(tmp, "%%u%04X", u); s = UString(tmp); } else if (u != 0 && strchr(do_not_escape, (char)u)) { s = UString(c, 1); } else { char tmp[4]; sprintf(tmp, "%%%02X", u); s = UString(tmp); } r += s; } res = String(r); break; } case UnEscape: { UString s = "", str = args[0].toString(exec); int k = 0, len = str.size(); while (k < len) { const UChar *c = str.data() + k; UChar u; if (*c == UChar('%') && k <= len - 6 && *(c+1) == UChar('u')) { if (Lexer::isHexDigit((c+2)->uc) && Lexer::isHexDigit((c+3)->uc) && Lexer::isHexDigit((c+4)->uc) && Lexer::isHexDigit((c+5)->uc)) { u = Lexer::convertUnicode((c+2)->uc, (c+3)->uc, (c+4)->uc, (c+5)->uc); c = &u; k += 5; } } else if (*c == UChar('%') && k <= len - 3 && Lexer::isHexDigit((c+1)->uc) && Lexer::isHexDigit((c+2)->uc)) { u = UChar(Lexer::convertHex((c+1)->uc, (c+2)->uc)); c = &u; k += 2; } k++; s += UString(c, 1); } res = String(s); break; } case KJSPrint:#ifndef NDEBUG puts(args[0].toString(exec).ascii());#endif break; } return res;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?