// 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 = base::Vector::cast( base::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(), reusable_state_(isolate()), parse_info_( isolate(), UnoptimizedCompileFlags::ForToplevelCompile( isolate(), true, construct_language_mode(v8_flags.use_strict), REPLMode::kNo, ScriptType::kClassic, v8_flags.lazy), &state_, &reusable_state_), local_isolate_(isolate()->main_thread_local_isolate()) {} 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(base::CStrVector(source)) .ToHandleChecked(); script_ = parse_info_.CreateScript(local_isolate(), local_factory()->empty_string(), kNullMaybeHandle, ScriptOriginOptions()); parse_info_.set_character_stream( ScannerStream::ForTesting(utf16_source.data(), utf16_source.size())); Parser parser(local_isolate(), parse_info(), script_); parser.InitializeEmptyScopeChain(parse_info()); parser.ParseOnBackground(local_isolate(), parse_info(), 0, 0, kFunctionLiteralIdTopLevel); DeclarationScope::AllocateScopeInfos(parse_info(), local_isolate()); // 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