• 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
16globalThis.ASSERT_TRUE = function assertTrue(v, msg = '') {
17	if (v !== true) {
18		msg = 'ASSERTION FAILED: ' + msg;
19		throw Error(msg);
20	}
21};
22
23globalThis.ASSERT_EQ = function assertEq(v0, v1) {
24	if (!Object.is(v0, v1)) {
25		let msg = `ASSERTION FAILED: ${v0}[${typeof v0}] !== ${v1}[${typeof v1}]`;
26		throw Error(msg);
27	}
28};
29
30globalThis.ASSERT_THROWS = function assertThrows(ctor, fn) {
31	ASSERT_TRUE(ctor !== undefined && fn !== undefined);
32	try {
33		fn();
34	} catch (exc) {
35		if (exc instanceof ctor) {
36			return;
37		}
38		throw Error('ASSERT_THROWS: expected instance of ' + ctor.name);
39	}
40	throw Error('ASSERT_THROWS: nothing was thrown');
41};
42
43globalThis.LOG_PROTO_CHAIN = function logProtoChain(o) {
44	print('===== LOG_PROTO_CHAIN of ' + o + ' =====');
45	for (let p = o.__proto__; p !== null; p = p.__proto__) {
46		print(p.constructor + '\n[' + Object.getOwnPropertyNames(p) + ']');
47	}
48	print('==========');
49};
50
51function main() {
52    const helper = requireNapiPreview('lib/libinterop_test_helper.so', false);
53    if (helper === undefined) {
54		print(`Failed to call requireNapiPreview(lib/libinterop_test_helper.so, false)`);
55		return 1;
56    }
57
58	// Add 'gtest' object to global space.
59	// This object is used by gtests as storage to save and restore variables
60	globalThis.gtest = {};
61
62	globalThis.gtest.etsVm = requireNapiPreview('lib/ets_interop_js_napi.so', false);
63    globalThis.gtest.helper = helper;
64
65	let stdlibPath = helper.getEnvironmentVar('ARK_ETS_STDLIB_PATH');
66	let gtestAbcPath = helper.getEnvironmentVar('ARK_ETS_INTEROP_JS_GTEST_ABC_PATH');
67
68
69	let argv = helper.getArgv();
70	const arkJsNapiCliLastArgIdx = 5;
71
72	let gtestName = argv[arkJsNapiCliLastArgIdx];
73	if (gtestName === undefined) {
74		print(`Usage: ${argv[0]} ${argv[1]} ${argv[2]} ${argv[3]} ${argv[4]} <test name>`);
75		return 1;
76	}
77
78	let createRuntimeOptions = {
79		'log-level': 'info',
80		'log-components': 'ets_interop_js',
81		'boot-panda-files': stdlibPath + ':' + gtestAbcPath,
82		'panda-files': gtestAbcPath,
83		'gc-trigger-type': 'heap-trigger',
84		'compiler-enable-jit': 'false',
85	};
86
87	if (gtestName === 'ets_interop_ts_to_ets_taskpool') {
88		createRuntimeOptions['taskpool-support-interop'] = 'true';
89	}
90
91	const etsVmRes = globalThis.gtest.etsVm.createRuntime(createRuntimeOptions);
92
93	if (!etsVmRes) {
94		print('Failed to create ETS runtime');
95		return 1;
96	}
97
98	globalThis.require = require;
99
100	let gtestDir = helper.getEnvironmentVar('ARK_ETS_INTEROP_JS_GTEST_DIR');
101	if (gtestDir === undefined) {
102		print('ARK_ETS_INTEROP_JS_GTEST_DIR is not set');
103		return 1;
104	}
105
106	// Run gtest
107	print(`Run ets_interop_js_gtest module: ${gtestName}`);
108	const etsGtest = requireNapiPreview(`lib/${gtestName}.so`, false);
109	if (etsGtest === undefined) {
110		print(`Failed to call requireNapiPreview(lib/${gtestName}.so, false)`);
111		return 1;
112	}
113
114	let args = argv.slice(arkJsNapiCliLastArgIdx);
115	try {
116		return etsGtest.main(args);
117	} catch (e) {
118		print(`${gtestName}: uncaught exception: ${e}`);
119		print('exception.toString():\n', e.toString());
120	}
121	return 1;
122}
123
124let res = main();
125if (res !== 0) {
126	throw Error('gtest_launcher.js main return 1');
127}
128