// Copyright 2020 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include #include #include #include #include "src/ast/ast-value-factory.h" #include "src/ast/ast.h" #include "src/ast/scopes.h" #include "src/common/assert-scope.h" #include "src/common/globals.h" #include "src/handles/handles-inl.h" #include "src/handles/handles.h" #include "src/handles/maybe-handles.h" #include "src/heap/local-factory-inl.h" #include "src/objects/fixed-array.h" #include "src/objects/script.h" #include "src/objects/shared-function-info.h" #include "src/objects/string.h" #include "src/parsing/parse-info.h" #include "src/parsing/parser.h" #include "src/parsing/rewriter.h" #include "src/parsing/scanner-character-streams.h" #include "src/parsing/scanner.h" #include "src/strings/unicode-inl.h" #include "src/utils/utils.h" #include "test/unittests/test-utils.h" namespace v8 { namespace internal { class LocalIsolate; namespace { std::vector DecodeUtf8(const std::string& string) { if (string.empty()) return {}; auto utf8_data = Vector::cast(VectorOf(string.data(), string.length())); Utf8Decoder decoder(utf8_data); std::vector utf16(decoder.utf16_length()); decoder.Decode(&utf16[0], utf8_data); return utf16; } } // namespace class LocalFactoryTest : public TestWithIsolateAndZone { public: LocalFactoryTest() : TestWithIsolateAndZone(), state_(isolate()), parse_info_( isolate(), UnoptimizedCompileFlags::ForToplevelCompile( isolate(), true, construct_language_mode(FLAG_use_strict), REPLMode::kNo), &state_), local_isolate_(isolate()->main_thread_local_isolate()) { FLAG_concurrent_allocation = true; } FunctionLiteral* ParseProgram(const char* source) { auto utf16_source = DecodeUtf8(source); // Normally this would be an external string or whatever, we don't have to // worry about it for now. source_string_ = factory()->NewStringFromUtf8(CStrVector(source)).ToHandleChecked(); parse_info_.set_character_stream( ScannerStream::ForTesting(utf16_source.data(), utf16_source.size())); { DisallowGarbageCollection no_gc; DisallowHeapAccess no_heap_access; Parser parser(parse_info()); parser.InitializeEmptyScopeChain(parse_info()); parser.ParseOnBackground(parse_info(), 0, 0, kFunctionLiteralIdTopLevel); } parse_info()->ast_value_factory()->Internalize(local_isolate()); DeclarationScope::AllocateScopeInfos(parse_info(), local_isolate()); script_ = parse_info_.CreateScript(local_isolate(), local_factory()->empty_string(), kNullMaybeHandle, ScriptOriginOptions()); // Create the SFI list on the script so that SFI SetScript works. Handle infos = local_factory()->NewWeakFixedArray( parse_info()->max_function_literal_id() + 1, AllocationType::kOld); script_->set_shared_function_infos(*infos); return parse_info()->literal(); } ParseInfo* parse_info() { return &parse_info_; } Handle