• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16const CROSS_REFS_NUMBER: number = 10;
17
18function loadStaticVM() {
19    let etsVm = requireNapiPreview('ets_interop_js_napi', true);
20    if (!etsVm.createRuntime({
21        'log-level': 'debug',
22        'load-runtimes': 'ets',
23        'log-components': 'ets_interop_js:gc_trigger',
24        'boot-panda-files': 'etsstdlib.abc:gc_test_sts_common.abc',
25        'gc-trigger-type': 'heap-trigger',
26        'compiler-enable-jit': 'false',
27        'coroutine-workers-count': '1',
28        'run-gc-in-place': 'true', // Required option to check XGC call by trigger
29        'xgc-min-trigger-threshold': CROSS_REFS_NUMBER.toString()
30    })) {
31        throw new Error('Failed to create ETS runtime');
32    }
33    return etsVm;
34}
35
36function getSTSWeakRef(etsVm): WeakRef<Object> {
37    return new WeakRef(etsVm.getFunction('LPandaGC/ETSGLOBAL;', 'GetSTSObjectWithWeakRef')());
38}
39
40function main(): void {
41    let etsVm = loadStaticVM();
42    let wr = new WeakRef<Object>(new Object);
43    for (let i = 0; i < CROSS_REFS_NUMBER; ++i) {
44        wr = getSTSWeakRef(etsVm);
45    }
46    globalThis.ArkTools.GC.clearWeakRefForTest();
47    // It should trigger XGC trigger
48    let stsObject = etsVm.getFunction('LPandaGC/ETSGLOBAL;', 'GetSTSObject')();
49    print('--- Start JS GC Full ---');
50    globalThis.ArkTools.GC.clearWeakRefForTest();
51    let gcId = globalThis.ArkTools.GC.startGC('full');
52    globalThis.ArkTools.GC.waitForFinishGC(gcId);
53    print('--- Finish JS GC Full ---');
54    etsVm.getFunction('LPandaGC/ETSGLOBAL;', 'RunPandaGC')();
55    //
56    if (wr.deref() !== undefined) {
57        throw new Error('XGC trigger was not called');
58    }
59    if (stsObject === undefined) {
60        throw new Error('nullish stsObject');
61    }
62    return;
63}
64
65main();
66