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/handles.h"
10 : #include "src/isolate.h"
11 : #include "src/objects-inl.h"
12 : #include "src/objects.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 : namespace test {
17 :
18 36 : Handle<Object> RunJS(v8::Isolate* isolate, const char* script) {
19 : return Utils::OpenHandle(
20 : *v8::Script::Compile(
21 : isolate->GetCurrentContext(),
22 : v8::String::NewFromUtf8(isolate, script, v8::NewStringType::kNormal)
23 72 : .ToLocalChecked())
24 36 : .ToLocalChecked()
25 36 : ->Run(isolate->GetCurrentContext())
26 72 : .ToLocalChecked());
27 : }
28 :
29 6 : Handle<String> CreateSource(Isolate* isolate,
30 : ExternalOneByteString::Resource* maybe_resource) {
31 : static const char test_script[] = "(x) { x*x; }";
32 6 : if (maybe_resource) {
33 : return isolate->factory()
34 : ->NewExternalStringFromOneByte(maybe_resource)
35 8 : .ToHandleChecked();
36 : }
37 2 : return isolate->factory()->NewStringFromAsciiChecked(test_script);
38 : }
39 :
40 6 : Handle<SharedFunctionInfo> CreateSharedFunctionInfo(
41 : Isolate* isolate,
42 : v8::String::ExternalOneByteStringResource* maybe_resource) {
43 : HandleScope scope(isolate);
44 6 : Handle<String> source = CreateSource(isolate, maybe_resource);
45 6 : Handle<Script> script = isolate->factory()->NewScript(source);
46 6 : Handle<FixedArray> infos = isolate->factory()->NewFixedArray(3);
47 6 : script->set_shared_function_infos(*infos);
48 : Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo(
49 : isolate->factory()->NewStringFromAsciiChecked("f"),
50 18 : BUILTIN_CODE(isolate, CompileLazy), false);
51 : shared->set_end_position(source->length());
52 12 : shared->set_outer_scope_info(ScopeInfo::Empty(isolate));
53 : shared->set_function_literal_id(1);
54 6 : SharedFunctionInfo::SetScript(shared, script);
55 12 : return scope.CloseAndEscape(shared);
56 : }
57 :
58 : } // namespace test
59 : } // namespace internal
60 : } // namespace v8
|