📄 runtime.cc.svn-base
字号:
// global object add the property locally. We take special // precautions to always add it as a local property even in case // of callbacks in the prototype chain (this rules out using // SetProperty). Also, we must use the handle-based version to // avoid GC issues. AddProperty(global, name, value, attributes); } } // Done. return Heap::undefined_value();}static Object* Runtime_DeclareContextSlot(Arguments args) { HandleScope scope; ASSERT(args.length() == 5); // args[0] is result (TOS) CONVERT_ARG_CHECKED(Context, context, 1); Handle<String> name(String::cast(args[2])); PropertyAttributes mode = static_cast<PropertyAttributes>(Smi::cast(args[3])->value()); ASSERT(mode == READ_ONLY || mode == NONE); Handle<Object> initial_value(args[4]); // Declarations are always done in the function context. context = Handle<Context>(context->fcontext()); int index; PropertyAttributes attributes; ContextLookupFlags flags = DONT_FOLLOW_CHAINS; Handle<Object> context_obj = context->Lookup(name, flags, &index, &attributes); if (attributes != ABSENT) { // The name was declared before; check for conflicting // re-declarations: This is similar to the code in parser.cc in // the AstBuildingParser::Declare function. if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { // Functions are not read-only. ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; return ThrowRedeclarationError(type, name); } // Initialize it if necessary. if (*initial_value != NULL) { if (index >= 0) { // The variable or constant context slot should always be in // the function context; not in any outer context nor in the // arguments object. ASSERT(context_obj.is_identical_to(context)); if (((attributes & READ_ONLY) == 0) || context->get(index)->IsTheHole()) { context->set(index, *initial_value); } } else { // Slow case: The property is not in the FixedArray part of the context. Handle<JSObject> context_ext = Handle<JSObject>::cast(context_obj); SetProperty(context_ext, name, initial_value, mode); } } return args[0]; // return TOS } // The property is not in the function context. It needs to be "declared" // in the function context's extension context, or in the global context. Handle<JSObject> context_ext; if (context->extension() != NULL) { // The function context's extension context exists - use it. context_ext = Handle<JSObject>(context->extension()); } else { // The function context's extension context does not exists - allocate it. context_ext = Factory::NewJSObject(Top::context_extension_function()); // And store it in the extension slot. context->set_extension(*context_ext); } ASSERT(*context_ext != NULL); // Declare the property by setting it to the initial value if provided, // or undefined, and use the correct mode (e.g. READ_ONLY attribute for // constant declarations). ASSERT(!context_ext->HasLocalProperty(*name)); Handle<Object> value(Heap::undefined_value()); if (*initial_value != NULL) value = initial_value; SetProperty(context_ext, name, value, mode); ASSERT(context_ext->GetLocalPropertyAttribute(*name) == mode); return args[0]; // return TOS}static Object* Runtime_InitializeVarGlobal(Arguments args) { NoHandleAllocation nha; // Determine if we need to assign to the variable if it already // exists (based on the number of arguments). RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); bool assign = args.length() == 2; CONVERT_ARG_CHECKED(String, name, 0); GlobalObject* global = Top::context()->global(); // According to ECMA-262, section 12.2, page 62, the property must // not be deletable. PropertyAttributes attributes = DONT_DELETE; // Lookup the property locally in the global object. If it isn't // there, we add the property and take special precautions to always // add it as a local property even in case of callbacks in the // prototype chain (this rules out using SetProperty). LookupResult lookup; global->LocalLookup(*name, &lookup); if (!lookup.IsProperty()) { Object* value = (assign) ? args[1] : Heap::undefined_value(); return global->AddProperty(*name, value, attributes); } // Determine if this is a redeclaration of something read-only. if (lookup.IsReadOnly()) { return ThrowRedeclarationError("const", name); } // Determine if this is a redeclaration of an intercepted read-only // property and figure out if the property exists at all. bool found = true; PropertyType type = lookup.type(); if (type == INTERCEPTOR) { PropertyAttributes intercepted = global->GetPropertyAttribute(*name); if (intercepted == ABSENT) { // The interceptor claims the property isn't there. We need to // make sure to introduce it. found = false; } else if ((intercepted & READ_ONLY) != 0) { // The property is present, but read-only. Since we're trying to // overwrite it with a variable declaration we must throw a // re-declaration error. return ThrowRedeclarationError("const", name); } // Restore global object from context (in case of GC). global = Top::context()->global(); } if (found && !assign) { // The global property is there and we're not assigning any value // to it. Just return. return Heap::undefined_value(); } // Assign the value (or undefined) to the property. Object* value = (assign) ? args[1] : Heap::undefined_value(); return global->SetProperty(&lookup, *name, value, attributes);}static Object* Runtime_InitializeConstGlobal(Arguments args) { // All constants are declared with an initial value. The name // of the constant is the first argument and the initial value // is the second. RUNTIME_ASSERT(args.length() == 2); CONVERT_ARG_CHECKED(String, name, 0); Handle<Object> value = args.at<Object>(1); // Get the current global object from top. GlobalObject* global = Top::context()->global(); // According to ECMA-262, section 12.2, page 62, the property must // not be deletable. Since it's a const, it must be READ_ONLY too. PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); // Lookup the property locally in the global object. If it isn't // there, we add the property and take special precautions to always // add it as a local property even in case of callbacks in the // prototype chain (this rules out using SetProperty). LookupResult lookup; global->LocalLookup(*name, &lookup); if (!lookup.IsProperty()) { return global->AddProperty(*name, *value, attributes); } // Determine if this is a redeclaration of something not // read-only. In case the result is hidden behind an interceptor we // need to ask it for the property attributes. if (!lookup.IsReadOnly()) { if (lookup.type() != INTERCEPTOR) { return ThrowRedeclarationError("var", name); } PropertyAttributes intercepted = global->GetPropertyAttribute(*name); // Throw re-declaration error if the intercepted property is present // but not read-only. if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { return ThrowRedeclarationError("var", name); } // Restore global object from context (in case of GC) and continue // with setting the value because the property is either absent or // read-only. We also have to do redo the lookup. global = Top::context()->global(); // BUG 1213579: Handle the case where we have to set a read-only // property through an interceptor and only do it if it's // uninitialized, e.g. the hole. Nirk... global->SetProperty(*name, *value, attributes); return *value; } // Set the value, but only we're assigning the initial value to a // constant. For now, we determine this by checking if the // current value is the hole. PropertyType type = lookup.type(); if (type == FIELD) { FixedArray* properties = global->properties(); int index = lookup.GetFieldIndex(); if (properties->get(index)->IsTheHole()) { properties->set(index, *value); } } else if (type == NORMAL) { Dictionary* dictionary = global->property_dictionary(); int entry = lookup.GetDictionaryEntry(); if (dictionary->ValueAt(entry)->IsTheHole()) { dictionary->ValueAtPut(entry, *value); } } else { // Ignore re-initialization of constants that have already been // assigned a function value. ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); } // Use the set value as the result of the operation. return *value;}static Object* Runtime_InitializeConstContextSlot(Arguments args) { HandleScope scope; ASSERT(args.length() == 3); Handle<Object> value(args[0]); ASSERT(!value->IsTheHole()); CONVERT_ARG_CHECKED(Context, context, 1); Handle<String> name(String::cast(args[2])); // Initializations are always done in the function context. context = Handle<Context>(context->fcontext()); int index; PropertyAttributes attributes; ContextLookupFlags flags = DONT_FOLLOW_CHAINS; Handle<Object> context_obj = context->Lookup(name, flags, &index, &attributes); // The property should always be present. It is always declared // before being initialized through DeclareContextSlot. ASSERT(attributes != ABSENT && (attributes & READ_ONLY) != 0); // If the slot is in the context, we set it but only if it hasn't // been set before. if (index >= 0) { // The constant context slot should always be in the function // context; not in any outer context nor in the arguments object. ASSERT(context_obj.is_identical_to(context)); if (context->get(index)->IsTheHole()) { context->set(index, *value); } return *value; } // Otherwise, the slot must be in a JS object extension. Handle<JSObject> context_ext(JSObject::cast(*context_obj)); // We must initialize the value only if it wasn't initialized // before, e.g. for const declarations in a loop. The property has // the hole value if it wasn't initialized yet. NOTE: We cannot use // GetProperty() to get the current value as it 'unholes' the value. LookupResult lookup; context_ext->LocalLookupRealNamedProperty(*name, &lookup); ASSERT(lookup.IsProperty()); // the property was declared ASSERT(lookup.IsReadOnly()); // and it was declared as read-only PropertyType type = lookup.type(); if (type == FIELD) { FixedArray* properties = context_ext->properties(); int index = lookup.GetFieldIndex(); if (properties->get(index)->IsTheHole()) { properties->set(index, *value); } } else if (type == NORMAL) { Dictionary* dictionary = context_ext->property_dictionary(); int entry = lookup.GetDictionaryEntry(); if (dictionary->ValueAt(entry)->IsTheHole()) { dictionary->ValueAtPut(entry, *value); } } else { // We should not reach here. Any real, named property should be // either a field or a dictionary slot. UNREACHABLE(); } return *value;}static Object* Runtime_RegExpExec(Arguments args) { HandleScope scope; ASSERT(args.length() == 3); CONVERT_CHECKED(JSRegExp, raw_regexp, args[0]); Handle<JSRegExp> regexp(raw_regexp); CONVERT_CHECKED(String, raw_subject, args[1]); Handle<String> subject(raw_subject); Handle<Object> index(args[2]); ASSERT(index->IsNumber()); return *RegExpImpl::JsreExec(regexp, subject, index);}static Object* Runtime_RegExpExecGlobal(Arguments args) { HandleScope scope; ASSERT(args.length() == 2); CONVERT_CHECKED(JSRegExp, raw_regexp, args[0]); Handle<JSRegExp> regexp(raw_regexp); CONVERT_CHECKED(String, raw_subject, args[1]); Handle<String> subject(raw_subject); return *RegExpImpl::JsreExecGlobal(regexp, subject);}static Object* Runtime_MaterializeRegExpLiteral(Arguments args) { HandleScope scope; ASSERT(args.length() == 4); CONVERT_ARG_CHECKED(FixedArray, literals, 0); int index = Smi::cast(args[1])->value(); Handle<String> pattern = args.at<String>(2); Handle<String> flags = args.at<String>(3); // Get the RegExp function from the literals array. This is the // RegExp function from the context in which the function was // created. We do not use the RegExp function from the current // global context because this might be the RegExp function from // another context which we should not have access to. Handle<JSFunction> constructor = Handle<JSFunction>( JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); // Compute the regular expression literal. bool has_pending_exception; Handle<Object> regexp = RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, &has_pending_exception); if (has_pending_exception) { ASSERT(Top::has_pending_exception()); return Failure::Exception(); } literals->set(index, *regexp); return *regexp;}static Object* Runtime_FunctionGetName(Arguments args) { NoHandleAllocation ha; ASSERT(args.length() == 1); CONVERT_CHECKED(JSFunction, f, args[0]); return f->shared()->name();}static Object* Runtime_FunctionSetName(Arguments args) { NoHandleAllocation ha; ASSERT(args.length() == 2); CONVERT_CHECKED(JSFunction, f, args[0]); CONVERT_CHECKED(String, name, args[1]); f->shared()->set_name(name); return Heap::undefined_value();}static Object* Runtime_FunctionGetScript(Arguments args) { HandleScope scope; ASSERT(args.length() == 1); CONVERT_CHECKED(JSFunction, fun, args[0]); Handle<Object> script = Handle<Object>(fun->shared()->script()); if (!script->IsScript()) return Heap::undefined_value(); return *GetScriptWrapper(Handle<Script>::cast(script));}static Object* Runtime_FunctionGetSourceCode(Arguments args) { NoHandleAllocation ha; ASSERT(args.length() == 1); CONVERT_CHECKED(JSFunction, f, args[0]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -