1 /**
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstring>
17 #include <algorithm>
18 #include "util.h"
19
20 // NOLINTBEGIN
21
22 static std::string g_source = R"(
23 function main() {}
24 )";
25
CreateFunctionTypeIr(es2panda_Context * context,es2panda_AstNode * program)26 es2panda_AstNode *CreateFunctionTypeIr(es2panda_Context *context, es2panda_AstNode *program)
27 {
28 auto impl = GetImpl();
29 size_t sizeOfStatements = 0;
30 auto *statements = impl->BlockStatementStatements(context, program, &sizeOfStatements);
31 es2panda_AstNode **newStatements =
32 static_cast<es2panda_AstNode **>(impl->AllocMemory(context, sizeOfStatements + 1, sizeof(es2panda_AstNode *)));
33 for (size_t i = 0; i < sizeOfStatements; i++) {
34 newStatements[i + 1] = statements[i];
35 }
36 auto signature = impl->CreateFunctionSignature(context, nullptr, nullptr, 0,
37 impl->CreateETSPrimitiveType(context, PRIMITIVE_TYPE_INT), false);
38
39 auto *id = impl->CreateIdentifier1(context, const_cast<char *>("foo"));
40
41 auto *functionTypeIr = impl->CreateETSFunctionTypeIr(context, signature, SCRIPT_FUNCTION_FLAGS_NONE);
42 auto *typeAliasDeclaration = impl->CreateTSTypeAliasDeclaration(context, id, nullptr, functionTypeIr);
43 impl->AstNodeSetParent(context, id, typeAliasDeclaration);
44 impl->AstNodeSetParent(context, functionTypeIr, typeAliasDeclaration);
45 newStatements[0] = typeAliasDeclaration;
46 impl->BlockStatementSetStatements(context, program, newStatements, sizeOfStatements + 1);
47 impl->AstNodeSetParent(context, typeAliasDeclaration, program);
48 return functionTypeIr;
49 }
50
UpdateFunctionTypeIr(es2panda_Context * context,es2panda_AstNode * original)51 bool UpdateFunctionTypeIr(es2panda_Context *context, es2panda_AstNode *original)
52 {
53 auto impl = GetImpl();
54 auto signature = impl->CreateFunctionSignature(context, nullptr, nullptr, 0,
55 impl->CreateETSPrimitiveType(context, PRIMITIVE_TYPE_DOUBLE), false);
56 auto node = impl->UpdateETSFunctionTypeIr(context, original, signature, SCRIPT_FUNCTION_FLAGS_NONE);
57 impl->TSTypeAliasDeclarationSetTsTypeAnnotation(context, impl->AstNodeParent(context, node), node);
58 return strcmp(impl->AstNodeDumpEtsSrcConst(context, impl->AstNodeParent(context, node)),
59 "type foo = (()=> double);\n") == 0;
60 }
61
main(int argc,char ** argv)62 int main(int argc, char **argv)
63 {
64 if (argc < MIN_ARGC) {
65 return 1;
66 }
67
68 if (GetImpl() == nullptr) {
69 return NULLPTR_IMPL_ERROR_CODE;
70 }
71 auto impl = GetImpl();
72 const char **args = const_cast<const char **>(&(argv[1]));
73 auto config = impl->CreateConfig(argc - 1, args);
74 auto context = impl->CreateContextFromString(config, g_source.data(), argv[argc - 1]);
75 if (context == nullptr) {
76 return NULLPTR_CONTEXT_ERROR_CODE;
77 }
78
79 impl->ProceedToState(context, ES2PANDA_STATE_PARSED);
80
81 if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) {
82 return PROCEED_ERROR_CODE;
83 }
84
85 auto ast = impl->ProgramAst(context, impl->ContextProgram(context));
86 auto original = CreateFunctionTypeIr(context, ast);
87 if (!UpdateFunctionTypeIr(context, original)) {
88 return TEST_ERROR_CODE;
89 }
90
91 impl->ProceedToState(context, ES2PANDA_STATE_CHECKED);
92 if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) {
93 return PROCEED_ERROR_CODE;
94 }
95
96 impl->AstNodeRecheck(context, ast);
97
98 impl->ProceedToState(context, ES2PANDA_STATE_BIN_GENERATED);
99 if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) {
100 return PROCEED_ERROR_CODE;
101 }
102
103 impl->DestroyContext(context);
104 impl->DestroyConfig(config);
105
106 return 0;
107 }
108
109 // NOLINTEND
110