Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "test/unittests/test-helpers.h"
6 :
7 : #include "include/v8.h"
8 : #include "src/api.h"
9 : #include "src/base/template-utils.h"
10 : #include "src/handles.h"
11 : #include "src/isolate.h"
12 : #include "src/objects-inl.h"
13 : #include "src/objects.h"
14 : #include "src/parsing/scanner-character-streams.h"
15 : #include "src/parsing/scanner.h"
16 :
17 : namespace v8 {
18 : namespace internal {
19 : namespace test {
20 :
21 19 : Handle<String> CreateSource(Isolate* isolate,
22 : ExternalOneByteString::Resource* maybe_resource) {
23 19 : if (!maybe_resource) {
24 : static const char test_script[] = "(x) { x*x; }";
25 14 : maybe_resource = new test::ScriptResource(test_script, strlen(test_script));
26 : }
27 : return isolate->factory()
28 38 : ->NewExternalStringFromOneByte(maybe_resource)
29 19 : .ToHandleChecked();
30 : }
31 :
32 19 : Handle<SharedFunctionInfo> CreateSharedFunctionInfo(
33 : Isolate* isolate,
34 : v8::String::ExternalOneByteStringResource* maybe_resource) {
35 : HandleScope scope(isolate);
36 19 : Handle<String> source = CreateSource(isolate, maybe_resource);
37 19 : Handle<Script> script = isolate->factory()->NewScript(source);
38 19 : Handle<WeakFixedArray> infos = isolate->factory()->NewWeakFixedArray(3);
39 19 : script->set_shared_function_infos(*infos);
40 : Handle<SharedFunctionInfo> shared =
41 : isolate->factory()->NewSharedFunctionInfoForBuiltin(
42 : isolate->factory()->NewStringFromAsciiChecked("f"),
43 38 : Builtins::kCompileLazy);
44 : int function_literal_id = 1;
45 : // Ensure that the function can be compiled lazily.
46 38 : shared->set_uncompiled_data(
47 38 : *isolate->factory()->NewUncompiledDataWithoutPreparseData(
48 : ReadOnlyRoots(isolate).empty_string_handle(), 0, source->length(),
49 : function_literal_id));
50 : // Make sure we have an outer scope info, even though it's empty
51 38 : shared->set_raw_outer_scope_info_or_feedback_metadata(
52 57 : ScopeInfo::Empty(isolate));
53 19 : SharedFunctionInfo::SetScript(shared, script, function_literal_id);
54 19 : return scope.CloseAndEscape(shared);
55 : }
56 :
57 25 : std::unique_ptr<ParseInfo> OuterParseInfoForShared(
58 : Isolate* isolate, Handle<SharedFunctionInfo> shared) {
59 : Handle<Script> script =
60 75 : Handle<Script>::cast(handle(shared->script(), isolate));
61 : std::unique_ptr<ParseInfo> result =
62 25 : base::make_unique<ParseInfo>(isolate, script);
63 :
64 : // Create a character stream to simulate the parser having done so for the
65 : // to-level ParseProgram.
66 25 : Handle<String> source(String::cast(script->source()), isolate);
67 : std::unique_ptr<Utf16CharacterStream> stream(
68 25 : ScannerStream::For(isolate, source));
69 50 : result->set_character_stream(std::move(stream));
70 :
71 25 : return result;
72 : }
73 :
74 : } // namespace test
75 : } // namespace internal
76 6176 : } // namespace v8
|