📄 internal.cpp
字号:
delete tos; tos = prev; }}LabelStack::~LabelStack(){ clear();}void LabelStack::clear(){ StackElem *prev; while (tos) { prev = tos->prev; delete tos; tos = prev; }}// ------------------------------ ContextImp -----------------------------------// ECMA 10.2ContextImp::ContextImp(Object &glob, InterpreterImp *interpreter, Object &thisV, CodeType type, ContextImp *callingCon, FunctionImp *func, const List *args) : _interpreter(interpreter), _function(func), _arguments(args){ codeType = type; _callingContext = callingCon; // create and initialize activation object (ECMA 10.1.6) if (type == FunctionCode || type == AnonymousCode ) { 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(); variable = _callingContext->variableObject(); thisVal = _callingContext->thisValue(); break; } // else same as GlobalCode case GlobalCode: scope.clear(); scope.push(glob.imp()); thisVal = Object(static_cast<ObjectImp*>(glob.imp())); break; case FunctionCode: case AnonymousCode: if (type == FunctionCode) { scope = func->scope(); scope.push(activation.imp()); } else { scope.clear(); scope.push(glob.imp()); 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(); }}// ------------------------------ Parser ---------------------------------------ProgramNode *Parser::progNode = 0;int Parser::sid = 0;ProgramNode *Parser::parse(const UString &sourceURL, int startingLineNumber, const UChar *code, unsigned int length, int *sourceId, int *errLine, UString *errMsg){ if (errLine) *errLine = -1; if (errMsg) *errMsg = 0; Lexer::curr()->setCode(sourceURL, startingLineNumber, code, length); progNode = 0; sid++; if (sourceId) *sourceId = sid; // Enable this (and the #define YYDEBUG in grammar.y) to debug a parse error //extern int kjsyydebug; //kjsyydebug=1; int parseError = kjsyyparse(); Lexer::curr()->doneParsing(); ProgramNode *prog = progNode; progNode = 0; sid = -1; if (parseError) { int eline = Lexer::curr()->lineNo(); if (errLine) *errLine = eline; if (errMsg) *errMsg = "Parse error"; if (prog) { // must ref and deref to clean up properly prog->ref(); prog->deref(); delete prog; } return 0; } 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) : _context(0){ // add this interpreter to the global chain // as a root set for garbage collection lockInterpreter(); m_interpreter = interp; 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(); } InterpreterMap::setInterpreterForGlobalObject(this, glob.imp()); global = glob; globExec = new ExecState(m_interpreter,0); dbg = 0; m_compatMode = Interpreter::NativeMode; // initialize properties of the global object initGlobalObject(); recursion = 0; unlockInterpreter();}void InterpreterImp::lock(){ lockInterpreter();}int InterpreterImp::lockCount(){ return interpreterLockCount;}void InterpreterImp::unlock(){ unlockInterpreter();} void InterpreterImp::initGlobalObject(){ Identifier::init(); // 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,"constructor", 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, "constructor", b_Object, DontEnum | DontDelete | ReadOnly); funcProto->put(globExec, "constructor", b_Function, DontEnum | DontDelete | ReadOnly); arrayProto->put(globExec, "constructor", b_Array, DontEnum | DontDelete | ReadOnly); booleanProto->put(globExec, "constructor", b_Boolean, DontEnum | DontDelete | ReadOnly); stringProto->put(globExec, "constructor", b_String, DontEnum | DontDelete | ReadOnly); numberProto->put(globExec, "constructor", b_Number, DontEnum | DontDelete | ReadOnly); dateProto->put(globExec, "constructor", b_Date, DontEnum | DontDelete | ReadOnly); regexpProto->put(globExec, "constructor", b_RegExp, DontEnum | DontDelete | ReadOnly); errorProto->put(globExec, "constructor", b_Error, DontEnum | DontDelete | ReadOnly); b_evalErrorPrototype.put(globExec, "constructor", b_evalError, DontEnum | DontDelete | ReadOnly); b_rangeErrorPrototype.put(globExec, "constructor", b_rangeError, DontEnum | DontDelete | ReadOnly); b_referenceErrorPrototype.put(globExec, "constructor", b_referenceError, DontEnum | DontDelete | ReadOnly); b_syntaxErrorPrototype.put(globExec, "constructor", b_syntaxError, DontEnum | DontDelete | ReadOnly); b_typeErrorPrototype.put(globExec, "constructor", b_typeError, DontEnum | DontDelete | ReadOnly); b_uriErrorPrototype.put(globExec, "constructor", 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 global.put(globExec,"eval", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::Eval, 1)), DontEnum); global.put(globExec,"parseInt", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::ParseInt, 2)), DontEnum); global.put(globExec,"parseFloat", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::ParseFloat, 1)), DontEnum); global.put(globExec,"isNaN", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::IsNaN, 1)), DontEnum); global.put(globExec,"isFinite", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::IsFinite, 1)), DontEnum); global.put(globExec,"escape", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::Escape, 1)), DontEnum); global.put(globExec,"unescape", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::UnEscape, 1)), DontEnum); global.put(globExec,"decodeURI", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::DecodeURI, 1)), DontEnum); global.put(globExec,"decodeURIComponent", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::DecodeURIComponent, 1)), DontEnum); global.put(globExec,"encodeURI", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::EncodeURI, 1)), DontEnum); global.put(globExec,"encodeURIComponent", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::EncodeURIComponent, 1)), DontEnum);#ifndef NDEBUG global.put(globExec,"kjsprint", Object(new GlobalFuncImp(globExec,funcProto,GlobalFuncImp::KJSPrint, 1)), DontEnum);#endif // built-in objects global.put(globExec,"Math", Object(new MathObjectImp(globExec,objProto)), DontEnum);}InterpreterImp::~InterpreterImp(){ if (dbg)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -