internal.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,089 行 · 第 1/3 页
CPP
1,089 行
Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::EncodeURIComponent,1,"encodeURIComponent")), DontEnum); global.put(globExec,"escape", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::Escape,1,"escape")), DontEnum); global.put(globExec,"unescape", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::UnEscape,1,"unescape")), DontEnum);#ifndef NDEBUG global.put(globExec,"kjsprint", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::KJSPrint,1,"kjsprint")), DontEnum);#endif // built-in objects global.put(globExec,"Math", Object(new MathObjectImp(globExec,objProto)), DontEnum);}InterpreterImp::~InterpreterImp(){ if (dbg) dbg->detach(m_interpreter); for (SourceCode *s = sources; s; s = s->next) s->interpreter = 0; delete globExec; globExec = 0L; clear();}void InterpreterImp::clear(){ //fprintf(stderr,"InterpreterImp::clear\n"); // remove from global chain (see init()) lockInterpreter(); next->prev = prev; prev->next = next; s_hook = next; if (s_hook == this) { // This was the last interpreter s_hook = 0L; globalClear(); } unlockInterpreter();}void InterpreterImp::mark(){ //if (exVal && !exVal->marked()) // exVal->mark(); //if (retVal && !retVal->marked()) // retVal->mark(); if (UndefinedImp::staticUndefined && !UndefinedImp::staticUndefined->marked()) UndefinedImp::staticUndefined->mark(); if (NullImp::staticNull && !NullImp::staticNull->marked()) NullImp::staticNull->mark(); if (NumberImp::staticNaN && !NumberImp::staticNaN->marked()) NumberImp::staticNaN->mark(); if (BooleanImp::staticTrue && !BooleanImp::staticTrue->marked()) BooleanImp::staticTrue->mark(); if (BooleanImp::staticFalse && !BooleanImp::staticFalse->marked()) BooleanImp::staticFalse->mark(); //fprintf( stderr, "InterpreterImp::mark this=%p global.imp()=%p\n", this, global.imp() ); if (global.imp()) global.imp()->mark(); if (m_interpreter) m_interpreter->mark(); if (_context) _context->mark();}bool InterpreterImp::checkSyntax(const UString &code, int *errLine, UString *errMsg){ // Parser::parse() returns 0 in a syntax error occurs, so we just check for that SourceCode *source; FunctionBodyNode *progNode = Parser::parse(code.data(),code.size(),&source,errLine,errMsg); source->deref(); bool ok = (progNode != 0); delete progNode; return ok;}bool InterpreterImp::checkSyntax(const UString &code){ // Parser::parse() returns 0 in a syntax error occurs, so we just check for that SourceCode *source; FunctionBodyNode *progNode = Parser::parse(code.data(),code.size(),&source,0,0); source->deref(); bool ok = (progNode != 0); delete progNode; return ok;}Completion InterpreterImp::evaluate(const UString &code, const Value &thisV){ lockInterpreter(); // prevent against infinite recursion if (recursion >= 20) { Completion result = Completion(Throw,Error::create(globExec,GeneralError,"Recursion too deep")); unlockInterpreter(); return result; } // parse the source code int errLine; UString errMsg; SourceCode *source; FunctionBodyNode *progNode = Parser::parse(code.data(),code.size(),&source,&errLine,&errMsg); // notify debugger that source has been parsed if (dbg) { bool cont = dbg->sourceParsed(globExec,source->sid,code,errLine); if (!cont) { source->deref(); if (progNode) delete progNode; unlockInterpreter(); return Completion(Break); } } addSourceCode(source); // no program node means a syntax error occurred if (!progNode) { Object err = Error::create(globExec,SyntaxError,errMsg.ascii(),errLine); err.put(globExec,"sid",Number(source->sid)); globExec->setException(err); // required to notify the debugger globExec->clearException(); source->deref(); unlockInterpreter(); return Completion(Throw,err); } source->deref(); globExec->clearException(); recursion++; progNode->ref(); Object &globalObj = globalObject(); Object thisObj = globalObject(); if (thisV.isValid()) { // "this" must be an object... use same rules as Function.prototype.apply() if (thisV.isA(NullType) || thisV.isA(UndefinedType)) thisObj = globalObject(); else { thisObj = thisV.toObject(globExec); } } Completion res; if (globExec->hadException()) { // the thisArg.toObject() conversion above might have thrown an exception - if so, // propagate it back res = Completion(Throw,globExec->exception()); } else { // execute the code ContextImp ctx(globalObj, this, thisObj, source->sid); ExecState newExec(m_interpreter,&ctx); // create variables (initialized to undefined until var statements // with optional initializers are executed) progNode->processVarDecls(&newExec); ctx.setLines(progNode->firstLine(),progNode->firstLine()); bool abort = false; if (dbg) { if (!dbg->enterContext(&newExec)) { // debugger requested we stop execution dbg->imp()->abort(); abort = true; } } if (!abort) { ctx.setLines(progNode->lastLine(),progNode->lastLine()); res = progNode->execute(&newExec); if (dbg && !dbg->exitContext(&newExec,res)) { // debugger requested we stop execution dbg->imp()->abort(); unlockInterpreter(); res = Completion(ReturnValue,Undefined()); } } } if (progNode->deref()) delete progNode; recursion--; if (globExec->hadException()) { res = Completion(Throw,globExec->exception()); globExec->clearException(); } unlockInterpreter(); return res;}void InterpreterImp::setDebugger(Debugger *d){ if (d == dbg) return; // avoid recursion Debugger *old = dbg; dbg = d; if ( old ) old->detach(m_interpreter);}void InterpreterImp::addSourceCode(SourceCode *code){ assert(!code->next); assert(!code->interpreter); code->next = sources; code->interpreter = this; sources = code;}void InterpreterImp::removeSourceCode(SourceCode *code){ assert(code); assert(sources); if (code == sources) { sources = sources->next; return; } SourceCode *prev = sources; SourceCode *cur = sources->next; while (cur != code) { assert(cur); prev = cur; cur = cur->next; } prev->next = cur->next;}// ------------------------------ InternalFunctionImp --------------------------const ClassInfo InternalFunctionImp::info = {"Function", 0, 0, 0};InternalFunctionImp::InternalFunctionImp(FunctionPrototypeImp *funcProto) : ObjectImp(funcProto){}InternalFunctionImp::InternalFunctionImp(ExecState *exec) : ObjectImp(static_cast<FunctionPrototypeImp*>(exec->interpreter()->builtinFunctionPrototype().imp())){}bool InternalFunctionImp::implementsHasInstance() const{ return true;}Boolean InternalFunctionImp::hasInstance(ExecState *exec, const Value &value){ if (value.type() != ObjectType) return Boolean(false); Value prot = get(exec,prototypePropertyName); if (prot.type() != ObjectType && prot.type() != NullType) { Object err = Error::create(exec, TypeError, "Invalid prototype encountered " "in instanceof operation."); exec->setException(err); return Boolean(false); } Object v = Object(static_cast<ObjectImp*>(value.imp())); while ((v = Object::dynamicCast(v.prototype())).imp()) { if (v.imp() == prot.imp()) return Boolean(true); } return Boolean(false);}// ------------------------------ global functions -----------------------------double KJS::roundValue(ExecState *exec, const Value &v){ double n = v.toNumber(exec); if (isNaN(n) || isInf(n)) return n; double an = fabs(n); if (an == 0.0) return n; double d = floor(an); if (n < 0) d *= -1; return d;}#ifndef NDEBUG#include <stdio.h>void KJS::printInfo(ExecState *exec, const char *s, const Value &o, int lineno){ if (!o.isValid()) fprintf(stderr, "KJS: %s: (null)", s); else { Value v = o; unsigned int arrayLength = 0; bool hadExcep = exec->hadException(); UString name; switch ( v.type() ) { case UnspecifiedType: name = "Unspecified"; break; case UndefinedType: name = "Undefined"; break; case NullType: name = "Null"; break; case BooleanType: name = "Boolean"; break; case StringType: name = "String"; break; case NumberType: name = "Number"; break; case ObjectType: { Object obj = Object::dynamicCast(v); name = obj.className(); if (name.isNull()) name = "(unknown class)"; if ( obj.inherits(&ArrayInstanceImp::info) ) arrayLength = obj.get(exec,lengthPropertyName).toUInt32(exec); } break; } UString vString; // Avoid calling toString on a huge array (e.g. 4 billion elements, in mozilla/js/js1_5/Array/array-001.js) if ( arrayLength > 100 ) vString = UString( "[ Array with " ) + UString::from( arrayLength ) + " elements ]"; else vString = v.toString(exec); if ( !hadExcep ) exec->clearException(); if ( vString.size() > 50 ) vString = vString.substr( 0, 50 ) + "..."; // Can't use two UString::ascii() in the same fprintf call CString tempString( vString.cstring() ); fprintf(stderr, "KJS: %s: %s : %s (%p)", s, tempString.c_str(), name.ascii(), (void*)v.imp()); if (lineno >= 0) fprintf(stderr, ", line %d\n",lineno); else fprintf(stderr, "\n"); }}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?