📄 factory.cc.svn-base
字号:
*shared, Heap::the_hole_value()), JSFunction);}Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, InstanceType type, int instance_size, Handle<JSObject> prototype, Handle<Code> code, bool force_initial_map) { // Allocate the function Handle<JSFunction> function = NewFunction(name, prototype); function->set_code(*code); if (force_initial_map || type != JS_OBJECT_TYPE || instance_size != JSObject::kHeaderSize) { Handle<Map> initial_map = NewMap(type, instance_size); function->set_initial_map(*initial_map); initial_map->set_constructor(*function); } // Set function.prototype and give the prototype a constructor // property that refers to the function. SetPrototypeProperty(function, prototype); SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM); return function;}Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo, Code::Flags flags) { CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags), Code);}Handle<Code> Factory::CopyCode(Handle<Code> code) { CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);}#define CALL_GC(RETRY) \ do { \ if (!Heap::CollectGarbage(Failure::cast(RETRY)->requested(), \ Failure::cast(RETRY)->allocation_space())) { \ /* TODO(1181417): Fix this. */ \ V8::FatalProcessOutOfMemory("Factory CALL_GC"); \ } \ } while (false)// Allocate the new array. We cannot use the CALL_HEAP_FUNCTION macro here,// because the stack-allocated CallbacksDescriptor instance is not GC safe.Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor( Handle<DescriptorArray> array, Handle<String> key, Handle<Object> value, PropertyAttributes attributes) { GC_GREEDY_CHECK(); CallbacksDescriptor desc(*key, *value, attributes); Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); if (obj->IsRetryAfterGC()) { CALL_GC(obj); CallbacksDescriptor desc(*key, *value, attributes); obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); if (obj->IsFailure()) { // TODO(1181417): Fix this. V8::FatalProcessOutOfMemory("CopyAppendProxyDescriptor"); } } return Handle<DescriptorArray>(DescriptorArray::cast(obj));}#undef CALL_GCHandle<String> Factory::SymbolFromString(Handle<String> value) { CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);}Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( Handle<DescriptorArray> array, Handle<Object> descriptors) { v8::NeanderArray callbacks(descriptors); int nof_callbacks = callbacks.length(); Handle<DescriptorArray> result = NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); // Number of descriptors added to the result so far. int descriptor_count = 0; // Copy the descriptors from the array. DescriptorWriter w(*result); for (DescriptorReader r(*array); !r.eos(); r.advance()) { w.WriteFrom(&r); descriptor_count++; } // Number of duplicates detected. int duplicates = 0; // Fill in new callback descriptors. Process the callbacks from // back to front so that the last callback with a given name takes // precedence over previously added callbacks with that name. for (int i = nof_callbacks - 1; i >= 0; i--) { Handle<AccessorInfo> entry = Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); // Ensure the key is a symbol before writing into the instance descriptor. Handle<String> key = SymbolFromString(Handle<String>(String::cast(entry->name()))); // Check if a descriptor with this name already exists before writing. if (result->BinarySearch(*key, 0, descriptor_count - 1) == DescriptorArray::kNotFound) { CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); w.Write(&desc); descriptor_count++; } else { duplicates++; } } // If duplicates were detected, allocate a result of the right size // and transfer the elements. if (duplicates > 0) { Handle<DescriptorArray> new_result = NewDescriptorArray(result->number_of_descriptors() - duplicates); DescriptorWriter w(*new_result); DescriptorReader r(*result); while (!w.eos()) { w.WriteFrom(&r); r.advance(); } result = new_result; } // Sort the result before returning. result->Sort(); return result;}Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, PretenureFlag pretenure) { CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);}Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED), JSObject);}Handle<JSObject> Factory::NewObjectLiteral(int expected_number_of_properties) { Handle<Map> map = Handle<Map>(Top::object_function()->initial_map()); map = Factory::CopyMap(map); map->set_instance_descriptors(Heap::empty_descriptor_array()); map->set_unused_property_fields(expected_number_of_properties); CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, TENURED), JSObject);}Handle<JSArray> Factory::NewArrayLiteral(int length) { return NewJSArrayWithElements(NewFixedArray(length), TENURED);}Handle<JSArray> Factory::NewJSArray(int length, PretenureFlag pretenure) { Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure); CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);}Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, PretenureFlag pretenure) { Handle<JSArray> result = Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure)); result->SetContent(*elements); return result;}Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name), SharedFunctionInfo);}Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary, uint32_t key, Handle<Object> value) { CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);}Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, Handle<Object> prototype) { Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(), *function_share, *prototype), JSFunction);}Handle<JSFunction> Factory::NewFunction(Handle<String> name, Handle<Object> prototype) { Handle<JSFunction> fun = NewFunctionHelper(name, prototype); fun->set_context(Top::context()->global_context()); return fun;}Handle<Object> Factory::ToObject(Handle<Object> object, Handle<Context> global_context) { CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);}Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { // Get the original code of the function. Handle<Code> code(shared->code()); // Create a copy of the code before allocating the debug info object to avoid // allocation while setting up the debug info object. Handle<Code> original_code(*Factory::CopyCode(code)); // Allocate initial fixed array for active break points before allocating the // debug info object to avoid allocation while setting up the debug info // object. Handle<FixedArray> break_points( Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); // Create and set up the debug info object. Debug info contains function, a // copy of the original code, the executing code and initial fixed array for // active break points. Handle<DebugInfo> debug_info = Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE)); debug_info->set_shared(*shared); debug_info->set_original_code(*original_code); debug_info->set_code(*code); debug_info->set_break_points(*break_points); // Link debug info to function. shared->set_debug_info(*debug_info); return debug_info;}Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, int length) { CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);}Handle<JSFunction> Factory::CreateApiFunction( Handle<FunctionTemplateInfo> obj, bool is_global) { Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall)); int internal_field_count = 0; if (!obj->instance_template()->IsUndefined()) { Handle<ObjectTemplateInfo> instance_template = Handle<ObjectTemplateInfo>( ObjectTemplateInfo::cast(obj->instance_template())); internal_field_count = Smi::cast(instance_template->internal_field_count())->value(); } int instance_size = kPointerSize * internal_field_count; if (is_global) { instance_size += JSGlobalObject::kSize; } else { instance_size += JSObject::kHeaderSize; } InstanceType type = is_global ? JS_GLOBAL_OBJECT_TYPE : JS_OBJECT_TYPE; Handle<JSFunction> result = Factory::NewFunction(Factory::empty_symbol(), type, instance_size, code, true); // Set class name. Handle<Object> class_name = Handle<Object>(obj->class_name()); if (class_name->IsString()) { result->shared()->set_instance_class_name(*class_name); result->shared()->set_name(*class_name); } Handle<Map> map = Handle<Map>(result->initial_map()); // Mark as undetectable if needed. if (obj->undetectable()) { map->set_is_undetectable(); } // Mark as hidden for the __proto__ accessor if needed. if (obj->hidden_prototype()) { map->set_is_hidden_prototype(); } // Mark as needs_access_check if needed. if (obj->needs_access_check()) { map->set_needs_access_check(); } // Set interceptor information in the map. if (!obj->named_property_handler()->IsUndefined()) { map->set_has_named_interceptor(); } if (!obj->indexed_property_handler()->IsUndefined()) { map->set_has_indexed_interceptor(); } // Set instance call-as-function information in the map. if (!obj->instance_call_handler()->IsUndefined()) { map->set_has_instance_call_handler(); } result->shared()->set_function_data(*obj); result->shared()->DontAdaptArguments(); // Recursively copy parent templates' accessors, 'data' may be modified. Handle<DescriptorArray> array = Handle<DescriptorArray>(map->instance_descriptors()); while (true) { Handle<Object> props = Handle<Object>(obj->property_accessors()); if (!props->IsUndefined()) { array = Factory::CopyAppendCallbackDescriptors(array, props); } Handle<Object> parent = Handle<Object>(obj->parent_template()); if (parent->IsUndefined()) break; obj = Handle<FunctionTemplateInfo>::cast(parent); } if (!array->IsEmpty()) { map->set_instance_descriptors(*array); } return result;}Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);}static Object* UpdateMapCacheWith(Context* context, FixedArray* keys, Map* map) { Object* result = MapCache::cast(context->map_cache())->Put(keys, map); if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result)); return result;}Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, Handle<FixedArray> keys, Handle<Map> map) { CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);}Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, Handle<FixedArray> keys) { if (context->map_cache()->IsUndefined()) { // Allocate the new map cache for the global context. Handle<MapCache> new_cache = NewMapCache(24); context->set_map_cache(*new_cache); } // Check to see whether there is a maching element in the cache. Handle<MapCache> cache = Handle<MapCache>(MapCache::cast(context->map_cache())); Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); if (result->IsMap()) return Handle<Map>::cast(result); // Create a new map and add it to the cache. Handle<Map> map = CopyMap(Handle<Map>(context->object_function()->initial_map())); AddToMapCache(context, keys, map); return Handle<Map>(map);}void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, Handle<JSObject> instance, bool* pending_exception) { // Configure the instance by adding the properties specified by the // instance template. Handle<Object> instance_template = Handle<Object>(desc->instance_template()); if (!instance_template->IsUndefined()) { Execution::ConfigureInstance(instance, instance_template, pending_exception); } else { *pending_exception = false; }}} } // namespace v8::internal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -