1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import os 19import random 20 21from typing import List, Tuple, Final, Dict 22 23import stress_common 24 25from stress_test import StressTest, Test, Result 26from stress_common import SCRIPT_DIR, TMP_DIR, collect_from 27 28TEST262_GIT_URL = "https://gitee.com/hufeng20/test262.git" 29TEST262_GIT_HASH = "6f4601d095a3899d6102f2c320b671495cbe8757" 30 31EXCLUDED_TESTS = [ 32 'regress-4595.js', 33 'module-jit-reachability.js', 34] 35 36 37class Test262StressTest(StressTest): 38 39 def __init__(self, args): 40 rc_lib_path: Final[str] = f'{args.build_dir}/arkcompiler/runtime_core' 41 ets_rc_lib_path: Final[ 42 str] = f'{args.build_dir}/arkcompiler/ets_runtime' 43 icu_lib_path: Final[str] = f'{args.build_dir}/thirdparty/icu/' 44 zlib_lib_path: Final[str] = f'{args.build_dir}/thirdparty/zlib/' 45 self.runtime_env: Dict[str, str] = { 46 'LD_LIBRARY_PATH': 47 f'{rc_lib_path}:{ets_rc_lib_path}:{icu_lib_path}:{zlib_lib_path}' 48 } 49 50 self.js_dir = os.path.join(TMP_DIR, 'abckit_test262') 51 self.cp = self.get_compiler_path(args.build_dir) 52 self.jvm = os.path.join(args.build_dir, 53 'arkcompiler/ets_runtime/ark_js_vm') 54 self.timeout = 10 55 self.repeats = 1 56 self.build_dir = args.build_dir 57 58 def get_fail_list_path(self) -> str: 59 return os.path.join(SCRIPT_DIR, 'fail_list_test262.json') 60 61 def run_single(self, test: Test) -> Result: 62 result = stress_common.run_abckit(self.build_dir, test.source, 63 test.abc, '/tmp/tmp.abc') 64 if result.returncode != 0: 65 error = stress_common.parse_stdout(str(result.returncode), 66 result.stdout) 67 return Result(test.source, error) 68 return Result(test.source, "0") 69 70 def compile_single(self, src: str) -> Tuple[str, str, int]: 71 logger = stress_common.create_logger() 72 abc_path = src + '.abc' 73 abc_path = abc_path.replace("[", "_").replace("]", "_") 74 cmd = [self.cp, '--module', '--merge-abc', '--output', abc_path, src] 75 result = stress_common.stress_exec(cmd, 76 allow_error=True, 77 print_command=True) 78 if result.returncode == 0 and not os.path.exists(abc_path): 79 logger.debug( 80 'WARNING: for %s es2abc has %s return code, but has no output', 81 src, result.returncode) 82 return src, abc_path, result.returncode 83 84 def prepare(self) -> None: 85 self.download_262() 86 87 def get_compiler_path(self, build_dir) -> str: 88 return os.path.join(build_dir, 'arkcompiler/ets_frontend/es2abc') 89 90 def download_262(self) -> None: 91 if not os.path.exists(self.js_dir): 92 stress_common.stress_exec( 93 ['git', 'clone', TEST262_GIT_URL, self.js_dir], repeats=5) 94 stress_common.stress_exec( 95 ['git', '-C', self.js_dir, 'checkout', TEST262_GIT_HASH]) 96 97 def collect(self) -> List[str]: 98 logger = stress_common.create_logger() 99 tests: List[str] = [] 100 tests.extend( 101 collect_from( 102 self.js_dir, lambda name: name.endswith('.js') and not name. 103 startswith('.'))) 104 random.shuffle(tests) 105 106 logger.debug('Total tests: %s', len(tests)) 107 for excluded in EXCLUDED_TESTS: 108 tests = list(filter(lambda name: excluded not in name, tests)) 109 logger.debug('Tests after exclude: %s', len(tests)) 110 111 return tests 112 113 def run_js_test_single(self, test: Test, repeats: int = 1) -> Result: 114 entry: str = f'--entry={os.path.basename(test.source)}' 115 cmd = [self.jvm, entry, test.abc] 116 result = stress_common.stress_exec(cmd, 117 allow_error=True, 118 print_command=True, 119 env=self.runtime_env, 120 timeout=self.timeout, 121 print_output=False, 122 repeats=repeats) 123 if result.returncode == 0: 124 return Result(test.source, 0) 125 126 return Result(test.source, result.returncode, result.stdout, 127 result.stderr) 128