1'use strict'; 2/* 3 * Copyright (c) 2024 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16Object.defineProperty(exports, '__esModule', { value: true }); 17exports.jsRespectsStaticModifier = 18 exports.jsRespectsProtectedModifier = 19 exports.extendNativeClass = 20 exports.extendUserClass = 21 exports.TestNativeClass = 22 exports.TestUserClass = 23 exports.helperIsJSInstanceOf = 24 exports.DEFAULT_NUMERIC_VALUE = 25 exports.DEFAULT_STRING_VALUE = 26 void 0; 27//@ts-ignore -- to avoid @types/node missing 28const stream1 = require('stream'); 29//@ts-ignore -- to avoid @types/node missing 30const etsVm = require('lib/module/ets_interop_js_napi'); 31const ETS_MODULE_NAME = 'interop_class_extension'; 32const makePrefix = (packageName) => 'L' + packageName.replace('.', '/') + '/'; 33const getClass = (name, packageName) => etsVm.getClass(makePrefix(packageName ?? ETS_MODULE_NAME) + name + ';'); 34const getFunction = (name, packageName) => etsVm.getFunction(makePrefix(packageName ?? ETS_MODULE_NAME) + 'ETSGLOBAL;', name); 35exports.DEFAULT_STRING_VALUE = 'Panda'; 36//@ts-ignore -- to avoid @types/node missing 37exports.DEFAULT_NUMERIC_VALUE = Math.round(Math.random() * Number.MAX_SAFE_INTEGER); 38const helperIsJSInstanceOf = (obj, classObj) => { 39 return obj instanceof classObj; 40}; 41exports.helperIsJSInstanceOf = helperIsJSInstanceOf; 42class TestUserClass { 43 static isMyInstance(obj) { 44 return obj instanceof TestUserClass; 45 } 46 constructor(constructorArg) { 47 this.rivateValue = exports.DEFAULT_STRING_VALUE; 48 this.protectedValue = exports.DEFAULT_STRING_VALUE; 49 this.publicValue = exports.DEFAULT_STRING_VALUE; 50 this.readonlyValue = exports.DEFAULT_STRING_VALUE; 51 this.constructorSetValue = constructorArg; 52 } 53} 54exports.TestUserClass = TestUserClass; 55exports.TestNativeClass = stream1.EventEmitter; 56const extendUserClass = () => { 57 const TSTestUserClass = getClass('TSTestUserClass'); 58 class ExtendedEtsUserClass extends TSTestUserClass {} 59 const classInstance = new ExtendedEtsUserClass(); 60 return classInstance instanceof ExtendedEtsUserClass; 61}; 62exports.extendUserClass = extendUserClass; 63const extendNativeClass = () => { 64 const TSTestNativeClass = getClass('ArrayBuffer', 'escompat'); 65 class ExtendedEtsNativeClass extends TSTestNativeClass { 66 constructor(length) { 67 super(length); 68 } 69 } 70 const classInstance = new ExtendedEtsNativeClass(2); 71 return classInstance instanceof TSTestNativeClass; 72}; 73exports.extendNativeClass = extendNativeClass; 74const jsRespectsProtectedModifier = () => { 75 const TSTestUserClass = getClass('TSTestUserClass'); 76 try { 77 class ExtendedEtsUserClass extends TSTestUserClass { 78 constructor() { 79 super(); 80 //@ts-ignore -- to ignore compile-time check, as TSTestUserClass is only acquired in runtime 81 this.protectedProperty = 'redefinedProtected'; 82 } 83 validProtectedGetter() { 84 //@ts-ignore -- to ignore compile-time check, as TSTestUserClass is only acquired in runtime 85 return this.protectedProperty; 86 } 87 } 88 const classInstance = new ExtendedEtsUserClass(); 89 return classInstance.validProtectedGetter() === 'redefinedProtected'; 90 } catch (e) { 91 return false; 92 } 93}; 94exports.jsRespectsProtectedModifier = jsRespectsProtectedModifier; 95const jsRespectsStaticModifier = () => { 96 const TSTestUserClass = getClass('TSTestUserClass'); 97 class ExtendedEtsUserClass extends TSTestUserClass {} 98 //@ts-ignore -- to ignore compile-time check, as TSTestUserClass is only acquired in runtime 99 return ExtendedEtsUserClass?.staticProperty === 'static'; 100}; 101exports.jsRespectsStaticModifier = jsRespectsStaticModifier; 102