internal.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,089 行 · 第 1/3 页
CPP
1,089 行
prev = tos->prev; delete tos; tos = prev; }}// ------------------------------ ContextImp -----------------------------------// ECMA 10.2ContextImp::ContextImp(Object &glob, InterpreterImp *interpreter, Object &thisV, int _sourceId, CodeType type, ContextImp *callingCon, FunctionImp *func, const List *args) : _interpreter(interpreter), _function(func), _arguments(args){ m_codeType = type; _callingContext = callingCon; tryCatch = 0; sourceId = _sourceId; line0 = 1; line1 = 1; if (func && func->inherits(&DeclaredFunctionImp::info)) functionName = static_cast<DeclaredFunctionImp*>(func)->name(); else functionName = Identifier::null(); // create and initialize activation object (ECMA 10.1.6) if (type == FunctionCode) { activation = Object(new ActivationImp(func,*args)); variable = activation; } else { activation = Object(); variable = glob; } // ECMA 10.2 switch(type) { case EvalCode: if (_callingContext) { scope = _callingContext->scopeChain();#ifndef KJS_PURE_ECMA if (thisV.imp() != glob.imp()) scope.push(thisV.imp()); // for deprecated Object.prototype.eval()#endif variable = _callingContext->variableObject(); thisVal = _callingContext->thisValue(); break; } // else same as GlobalCode case GlobalCode: scope.clear(); scope.push(glob.imp());#ifndef KJS_PURE_ECMA if (thisV.isValid()) thisVal = thisV; else#endif thisVal = glob; break; case FunctionCode: scope = func->scope(); scope.push(activation.imp()); variable = activation; // TODO: DontDelete ? (ECMA 10.2.3) thisVal = thisV; break; } _interpreter->setContext(this);}ContextImp::~ContextImp(){ _interpreter->setContext(_callingContext);}void ContextImp::mark(){ for (ContextImp *context = this; context; context = context->_callingContext) { context->scope.mark(); }}bool ContextImp::inTryCatch() const{ const ContextImp *c = this; while (c && !c->tryCatch) c = c->_callingContext; return (c && c->tryCatch);}// ---------------------------- SourceCode -------------------------------------void SourceCode::cleanup(){ if (interpreter && interpreter->debugger()) interpreter->debugger()->sourceUnused(interpreter->globalExec(),sid); if (interpreter) interpreter->removeSourceCode(this); delete this;}// ------------------------------ Parser ---------------------------------------FunctionBodyNode *Parser::progNode = 0;int Parser::sid = 0;SourceCode *Parser::source = 0;FunctionBodyNode *Parser::parse(const UChar *code, unsigned int length, SourceCode **src, int *errLine, UString *errMsg){ if (errLine) *errLine = -1; if (errMsg) *errMsg = 0; Lexer::curr()->setCode(code, length); progNode = 0; sid++; source = new SourceCode(sid); source->ref(); *src = source; // Enable this (and the #define YYDEBUG in grammar.y) to debug a parse error //extern int kjsyydebug; //kjsyydebug=1; int parseError = kjsyyparse(); if (Lexer::curr()->hadError()) parseError = 1; Lexer::curr()->doneParsing(); FunctionBodyNode *prog = progNode; progNode = 0; //sid = -1; source = 0; if (parseError) { int eline = Lexer::curr()->lineNo(); if (errLine) *errLine = eline; if (errMsg) *errMsg = "Parse error at line " + UString::from(eline);#ifdef KJS_VERBOSE fprintf( stderr, "%s\n", UString(code,length).ascii() );#endif#ifndef NDEBUG fprintf(stderr, "KJS: JavaScript parse error at line %d.\n", eline);#endif delete prog; return 0; }#ifdef KJS_VERBOSE fprintf( stderr, "%s\n", prog->toCode().ascii() );#endif return prog;}// ------------------------------ InterpreterImp -------------------------------InterpreterImp* InterpreterImp::s_hook = 0L;void InterpreterImp::globalInit(){ //fprintf( stderr, "InterpreterImp::globalInit()\n" ); UndefinedImp::staticUndefined = new UndefinedImp(); UndefinedImp::staticUndefined->ref(); NullImp::staticNull = new NullImp(); NullImp::staticNull->ref(); BooleanImp::staticTrue = new BooleanImp(true); BooleanImp::staticTrue->ref(); BooleanImp::staticFalse = new BooleanImp(false); BooleanImp::staticFalse->ref(); NumberImp::staticNaN = new NumberImp(NaN); NumberImp::staticNaN->ref();}void InterpreterImp::globalClear(){ //fprintf( stderr, "InterpreterImp::globalClear()\n" ); UndefinedImp::staticUndefined->deref(); UndefinedImp::staticUndefined->setGcAllowed(); UndefinedImp::staticUndefined = 0L; NullImp::staticNull->deref(); NullImp::staticNull->setGcAllowed(); NullImp::staticNull = 0L; BooleanImp::staticTrue->deref(); BooleanImp::staticTrue->setGcAllowed(); BooleanImp::staticTrue = 0L; BooleanImp::staticFalse->deref(); BooleanImp::staticFalse->setGcAllowed(); BooleanImp::staticFalse = 0L; NumberImp::staticNaN->deref(); NumberImp::staticNaN->setGcAllowed(); NumberImp::staticNaN = 0;}InterpreterImp::InterpreterImp(Interpreter *interp, const Object &glob) : m_interpreter(interp), global(glob), dbg(0), m_compatMode(Interpreter::NativeMode), _context(0), recursion(0), sources(0){ // add this interpreter to the global chain // as a root set for garbage collection lockInterpreter(); if (s_hook) { prev = s_hook; next = s_hook->next; s_hook->next->prev = this; s_hook->next = this; } else { // This is the first interpreter s_hook = next = prev = this; globalInit(); } unlockInterpreter(); globExec = new ExecState(m_interpreter,0); // initialize properties of the global object initGlobalObject();}void InterpreterImp::lock(){ lockInterpreter();}void InterpreterImp::unlock(){ unlockInterpreter();}void InterpreterImp::initGlobalObject(){ // Contructor prototype objects (Object.prototype, Array.prototype etc) FunctionPrototypeImp *funcProto = new FunctionPrototypeImp(globExec); b_FunctionPrototype = Object(funcProto); ObjectPrototypeImp *objProto = new ObjectPrototypeImp(globExec,funcProto); b_ObjectPrototype = Object(objProto); funcProto->setPrototype(b_ObjectPrototype); ArrayPrototypeImp *arrayProto = new ArrayPrototypeImp(globExec,objProto); b_ArrayPrototype = Object(arrayProto); StringPrototypeImp *stringProto = new StringPrototypeImp(globExec,objProto); b_StringPrototype = Object(stringProto); BooleanPrototypeImp *booleanProto = new BooleanPrototypeImp(globExec,objProto,funcProto); b_BooleanPrototype = Object(booleanProto); NumberPrototypeImp *numberProto = new NumberPrototypeImp(globExec,objProto,funcProto); b_NumberPrototype = Object(numberProto); DatePrototypeImp *dateProto = new DatePrototypeImp(globExec,objProto); b_DatePrototype = Object(dateProto); RegExpPrototypeImp *regexpProto = new RegExpPrototypeImp(globExec,objProto,funcProto); b_RegExpPrototype = Object(regexpProto); ErrorPrototypeImp *errorProto = new ErrorPrototypeImp(globExec,objProto,funcProto); b_ErrorPrototype = Object(errorProto); static_cast<ObjectImp*>(global.imp())->setPrototype(b_ObjectPrototype); // Constructors (Object, Array, etc.) b_Object = Object(new ObjectObjectImp(globExec, objProto, funcProto)); b_Function = Object(new FunctionObjectImp(globExec, funcProto)); b_Array = Object(new ArrayObjectImp(globExec, funcProto, arrayProto)); b_String = Object(new StringObjectImp(globExec, funcProto, stringProto)); b_Boolean = Object(new BooleanObjectImp(globExec, funcProto, booleanProto)); b_Number = Object(new NumberObjectImp(globExec, funcProto, numberProto)); b_Date = Object(new DateObjectImp(globExec, funcProto, dateProto)); b_RegExp = Object(new RegExpObjectImp(globExec, funcProto, regexpProto)); b_Error = Object(new ErrorObjectImp(globExec, funcProto, errorProto)); // Error object prototypes b_evalErrorPrototype = Object(new NativeErrorPrototypeImp(globExec,errorProto,EvalError, "EvalError","EvalError")); b_rangeErrorPrototype = Object(new NativeErrorPrototypeImp(globExec,errorProto,RangeError, "RangeError","RangeError")); b_referenceErrorPrototype = Object(new NativeErrorPrototypeImp(globExec,errorProto,ReferenceError, "ReferenceError","ReferenceError")); b_syntaxErrorPrototype = Object(new NativeErrorPrototypeImp(globExec,errorProto,SyntaxError, "SyntaxError","SyntaxError")); b_typeErrorPrototype = Object(new NativeErrorPrototypeImp(globExec,errorProto,TypeError, "TypeError","TypeError")); b_uriErrorPrototype = Object(new NativeErrorPrototypeImp(globExec,errorProto,URIError, "URIError","URIError")); // Error objects b_evalError = Object(new NativeErrorImp(globExec,funcProto,b_evalErrorPrototype)); b_rangeError = Object(new NativeErrorImp(globExec,funcProto,b_rangeErrorPrototype)); b_referenceError = Object(new NativeErrorImp(globExec,funcProto,b_referenceErrorPrototype)); b_syntaxError = Object(new NativeErrorImp(globExec,funcProto,b_syntaxErrorPrototype)); b_typeError = Object(new NativeErrorImp(globExec,funcProto,b_typeErrorPrototype)); b_uriError = Object(new NativeErrorImp(globExec,funcProto,b_uriErrorPrototype)); // ECMA 15.3.4.1 funcProto->put(globExec,constructorPropertyName, b_Function, DontEnum); global.put(globExec,"Object", b_Object, DontEnum); global.put(globExec,"Function", b_Function, DontEnum); global.put(globExec,"Array", b_Array, DontEnum); global.put(globExec,"Boolean", b_Boolean, DontEnum); global.put(globExec,"String", b_String, DontEnum); global.put(globExec,"Number", b_Number, DontEnum); global.put(globExec,"Date", b_Date, DontEnum); global.put(globExec,"RegExp", b_RegExp, DontEnum); global.put(globExec,"Error", b_Error, DontEnum); // Using Internal for those to have something != 0 // (see kjs_window). Maybe DontEnum would be ok too ? global.put(globExec,"EvalError",b_evalError, Internal); global.put(globExec,"RangeError",b_rangeError, Internal); global.put(globExec,"ReferenceError",b_referenceError, Internal); global.put(globExec,"SyntaxError",b_syntaxError, Internal); global.put(globExec,"TypeError",b_typeError, Internal); global.put(globExec,"URIError",b_uriError, Internal); // Set the "constructor" property of all builtin constructors objProto->put(globExec, constructorPropertyName, b_Object, DontEnum | DontDelete | ReadOnly); funcProto->put(globExec, constructorPropertyName, b_Function, DontEnum | DontDelete | ReadOnly); arrayProto->put(globExec, constructorPropertyName, b_Array, DontEnum | DontDelete | ReadOnly); booleanProto->put(globExec, constructorPropertyName, b_Boolean, DontEnum | DontDelete | ReadOnly); stringProto->put(globExec, constructorPropertyName, b_String, DontEnum | DontDelete | ReadOnly); numberProto->put(globExec, constructorPropertyName, b_Number, DontEnum | DontDelete | ReadOnly); dateProto->put(globExec, constructorPropertyName, b_Date, DontEnum | DontDelete | ReadOnly); regexpProto->put(globExec, constructorPropertyName, b_RegExp, DontEnum | DontDelete | ReadOnly); errorProto->put(globExec, constructorPropertyName, b_Error, DontEnum | DontDelete | ReadOnly); b_evalErrorPrototype.put(globExec, constructorPropertyName, b_evalError, DontEnum | DontDelete | ReadOnly); b_rangeErrorPrototype.put(globExec, constructorPropertyName, b_rangeError, DontEnum | DontDelete | ReadOnly); b_referenceErrorPrototype.put(globExec, constructorPropertyName, b_referenceError, DontEnum | DontDelete | ReadOnly); b_syntaxErrorPrototype.put(globExec, constructorPropertyName, b_syntaxError, DontEnum | DontDelete | ReadOnly); b_typeErrorPrototype.put(globExec, constructorPropertyName, b_typeError, DontEnum | DontDelete | ReadOnly); b_uriErrorPrototype.put(globExec, constructorPropertyName, b_uriError, DontEnum | DontDelete | ReadOnly); // built-in values global.put(globExec, "NaN", Number(NaN), DontEnum|DontDelete); global.put(globExec, "Infinity", Number(Inf), DontEnum|DontDelete); global.put(globExec, "undefined", Undefined(), DontEnum|DontDelete); // built-in functions#ifdef KJS_PURE_ECMA // otherwise as deprecated Object.prototype property global.put(globExec,"eval", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::Eval,1,"eval")), DontEnum);#endif global.put(globExec,"parseInt", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::ParseInt,2,"parseInt")), DontEnum); global.put(globExec,"parseFloat", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::ParseFloat,1,"parseFloat")), DontEnum); global.put(globExec,"isNaN", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::IsNaN,1,"isNaN")), DontEnum); global.put(globExec,"isFinite", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::IsFinite,1,"isFinite")), DontEnum); global.put(globExec,"decodeURI", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::DecodeURI,1,"decodeURI")), DontEnum); global.put(globExec,"decodeURIComponent", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::DecodeURIComponent,1,"decodeURIComponent")), DontEnum); global.put(globExec,"encodeURI", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::EncodeURI,1,"encodeURI")), DontEnum); global.put(globExec,"encodeURIComponent",
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?