From 975df344957b8ff251e7ca1c9b181e11201c4e65 Mon Sep 17 00:00:00 2001 From: kurnevichstanislav Date: Thu, 10 Jul 2025 12:26:00 +0300 Subject: [PATCH] Remove Reflect.* API Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICL846?from=project-issue Signed-off-by: kurnevichstanislav Change-Id: I64d5c2cb36a2f9164d50d66af09458d4c80f953e --- .../ets/runtime/ets_libbase_runtime.yaml | 10 - .../plugins/ets/sdk/api/@ohos.util.json.ets | 42 +- .../plugins/ets/stdlib/escompat/Reflect.ets | 661 ------------------ .../plugins/ets/stdlib/std/core/Proxy.ets | 129 ++-- .../plugins/ets/stdlib/std/core/Type.ets | 10 + .../escompat/ObjectLiteralTest.ets | 12 +- .../ets_func_tests/escompat/ProxyTest.ets | 16 +- .../ets_func_tests/escompat/ReflectGet.ets | 109 --- .../escompat/ReflectGetBadCases.ets | 176 ----- .../ets_func_tests/escompat/ReflectHas.ets | 115 --- .../escompat/ReflectOwnKeys.ets | 155 ---- .../ets_func_tests/escompat/ReflectSet.ets | 162 ----- .../std/core/ReflectLambdaTest.ets | 11 +- .../std/core/TypedArrayReflectSetTest.ets | 251 ------- 14 files changed, 161 insertions(+), 1698 deletions(-) delete mode 100644 static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGet.ets delete mode 100644 static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGetBadCases.ets delete mode 100644 static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectHas.ets delete mode 100644 static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectOwnKeys.ets delete mode 100644 static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectSet.ets delete mode 100644 static_core/plugins/ets/tests/ets_func_tests/std/core/TypedArrayReflectSetTest.ets diff --git a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml index 4b2e15d89c..fe27ee5904 100644 --- a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml +++ b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml @@ -7708,16 +7708,6 @@ intrinsics: args: [ std.core.Object ] impl: ark::ets::intrinsics::IsLiteralInitializedInterfaceImpl - - name: StdCoreDoubleNumberFromStringForReflect - space: ets - class_name: escompat.Reflect - method_name: numberFromString - static: true - signature: - ret: f64 - args: [ std.core.String ] - impl: ark::ets::intrinsics::StdCoreDoubleNumberFromString - ###################################### # std.concurrency.ConcurrencyHelpers # ###################################### diff --git a/static_core/plugins/ets/sdk/api/@ohos.util.json.ets b/static_core/plugins/ets/sdk/api/@ohos.util.json.ets index fb642f65e1..a635f81c49 100644 --- a/static_core/plugins/ets/sdk/api/@ohos.util.json.ets +++ b/static_core/plugins/ets/sdk/api/@ohos.util.json.ets @@ -37,7 +37,43 @@ * @returns boolean */ export function has(obj: Object, property: string): boolean { - return Reflect.has(obj, property) + if (!Object.hasOwn(obj, property)) { + const t = Type.of(obj) + if (t instanceof ClassType) { + const ct = t as ClassType + const mnum = ct.getMethodsNum() + for (let i = 0; i < mnum; ++i) { + const m = ct.getMethod(i) + if (m.isStatic()) { + continue + } + + const methodName = m.getName() + if (methodName == property) { + return true + } else if (methodName == "" + property) { + return true + } else if (methodName == "" + property) { + return true + } + } + } else if (t instanceof StringType || t instanceof ArrayType || t instanceof LambdaType) { + return (property == OBJECT_TO_STRING_MEMBER_NAME || + property == OBJECT_TO_LOCALE_STRING_MEMBER_NAME || + property == OBJECT_HAS_OWN_PROPERTY_MEMBER_NAME) + } else if (t instanceof EnumType) { + // NOTE(shumilov-petr): Not implemented + throw new Error("Not implemented") + } else if (t instanceof UnionType) { + // NOTE(shumilov-petr): Not implemented + throw new Error("Not implemented") + } else if (t instanceof TupleType) { + // NOTE(shumilov-petr): Not implemented + throw new Error("Not implemented") + } + return false + } + return true } /** @@ -45,7 +81,7 @@ * * @param obj: NullishType An object to be converted. * - * @param replacer An array with elements indicating names of the properties in the object + * @param replacer An array with elements indicating names of the properties in the object * or Transformer function changing field values * that should be included in the resulting JSON string * @@ -82,4 +118,4 @@ return JSON.parse(text, reviver, type, opt) } - } \ No newline at end of file + } diff --git a/static_core/plugins/ets/stdlib/escompat/Reflect.ets b/static_core/plugins/ets/stdlib/escompat/Reflect.ets index 56b41afb46..a7a97bc440 100644 --- a/static_core/plugins/ets/stdlib/escompat/Reflect.ets +++ b/static_core/plugins/ets/stdlib/escompat/Reflect.ets @@ -16,671 +16,10 @@ package escompat export class Reflect { - - private static readonly GETTER_PREFIX = "" - private static readonly SETTER_PREFIX = "" private static native isLiteralInitializedInterfaceImpl(target: Object): boolean private constructor () {} - /** - * Gets the field of target, equivalent to target.key - * - * @param target the target object on which to get the field - * - * @param key the string name of the field to get - * - * @returns the value of the field - */ - public static get(target: Object, key: PropertyKey): NullishType { - if (target instanceof Char || - target instanceof Boolean || - target instanceof Byte || - target instanceof Short || - target instanceof Int || - target instanceof Long || - target instanceof Float || - target instanceof Double || - target instanceof String) { - throw new Error("`target` argument of Reflect.get must have fields") - } - if (!Reflect.has(target, key)) { - return undefined - } - const t = Type.of(target) - if (t instanceof ClassType) { - if (t.hasField(key)) { - const classVal = reflect.Value.of(target) as ClassValue - return classVal.getFieldByName(key).getData() - } else { - const getter = Reflect.findMethod(t, Reflect.GETTER_PREFIX + key) - if (getter !== undefined) { - return getter.invoke(target, []) - } else { - return undefined - } - } - } else if (t instanceof ArrayType) { - if (key == ARRAY_LENGTH_MEMBER_NAME) { - return new Number((reflect.Value.of(target) as ArrayValue).getLength()) - } - } else if (t instanceof LambdaType) { - if (key == FUNCTION_LENGTH_MEMBER_NAME) { - return new Number((t as FunctionType).getParametersNum()) - } else if (key == FUNCTION_NAME_MEMBER_NAME) { - return "" - } - } else if (t instanceof EnumType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } else if (t instanceof UnionType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } else if (t instanceof TupleType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } - return undefined - } - - /** - * Gets the property of the record - * - * @param target Record that contains the property - * - * @param key name of the property - * - * @returns property value - */ - public static get(target: Record, key: string): NullishType { - return target[key] - } - - /** - * Gets the element of target, equivalent to target[index] - * - * @param target the target object on which to get the element - * - * @param key the number index of the element to get - * - * @returns the value of the element - */ - public static get(target: Object, index: number): Object | undefined { - if (target instanceof Char || - target instanceof Boolean || - target instanceof Byte || - target instanceof Short || - target instanceof Int || - target instanceof Long || - target instanceof Float || - target instanceof Double || - target instanceof String) { - throw new Error("`target` argument of Reflect.get must be indexed") - } - if (!Reflect.has(target, index)) { - return undefined - } - let t = Type.of(target) - if (t instanceof ArrayType) { - let av = reflect.Value.of(target) as ArrayValue - return __narrowAny(av.getElement(index.toLong()).getData()) - } else if (t instanceof EnumType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } else if (t instanceof UnionType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } else if (t instanceof TupleType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } - return undefined - } - - // same as Double.numberFromString - private static native numberFromString(s: String): double; - - private static toNumberInt(value: NullishType): double { - if (value instanceof double || value instanceof int) { - return value as double; - } - if (value instanceof bigint) { - throw new TypeError("Cannot convert a BigInt value to a number"); - } - if (value === undefined) { - return NaN; - } - if (value === null || value === false) { - return 0; - } - if (value === true) { - return 1; - } - if (value instanceof string) { - return Reflect.numberFromString(value); - } - return NaN; - } - - private static toNumberDouble(value: NullishType): double { - if (value instanceof double || value instanceof int) { - return value as double; - } - if (value instanceof bigint) { - throw new TypeError("Cannot convert a BigInt value to a number"); - } - if (value === undefined) { - return NaN; - } - if (value === null || value === false) { - return 0; - } - if (value === true) { - return 1; - } - if (value instanceof string) { - return Double.parseFloat(value); - } - return NaN; - } - - private static isValidBigIntDigits(digits: string, radix: int): boolean { - let temp: string = digits.toLowerCase(); - for (let i = 0; i < digits.length; i++) { - let ch = temp.charAt(i); - switch (radix) { - case 2: - if (ch !== c'0' && ch !== c'1') { - return false; - } - break; - case 8: - if (ch < c'0' || ch > c'7') { - return false; - } - break; - case 10: - if (ch < c'0' || ch > c'9') { - return false; - } - break; - case 16: - if (!( - (ch >= c'0' && ch <= c'9') || - (ch >= c'a' && ch <= c'f') - )) { - return false; - } - break; - default: - return false; - } - } - return true; - } - - private static parseBigIntFromString(value: string): bigint | long | double { - const POSITIVE_SIGN = 1; - const NEGATIVE_SIGN = 2; - const NO_SIGN = 0; - - let trimmed: string = value.trim(); - let len: int = trimmed.length.toInt(); - let index: int = 0; - - if (len === 0) { - return 0n; - } - - let sign: byte = NO_SIGN; - let firstChar = trimmed.charAt(index); - if (firstChar === c'+') { - if (len === 1) { - return NaN; - } - sign = POSITIVE_SIGN; - index++; - } else if (firstChar === c'-') { - if (len === 1) { - return NaN; - } - sign = NEGATIVE_SIGN; - index++; - } - - if (index >= len) { - return NaN; - } - - let radix: int = 10; - - if (trimmed.charAt(index) === c'0') { - index++; - if (index >= len) { - return 0n; - } - let prefix: char = trimmed.charAt(index); - prefix.toLowerCase(); - switch (prefix) { - case c'x': - radix = 16; - index++; - if (index >= len) { - return NaN; - } - break; - case c'o': - radix = 8; - index++; - if (index >= len) { - return NaN; - } - break; - case c'b': - radix = 2; - index++; - if (index >= len) { - return NaN; - } - break; - default: - break; - } - } - - // Skip leading zeroes - while (trimmed.charAt(index) === c'0') { - index++; - if (index === len) { - return 0n; - } - } - - let digits = trimmed.substring(index); - if (!Reflect.isValidBigIntDigits(digits, radix)) { - return NaN; - } - - let result: long|double = Reflect.stringToLongWithRadix(digits, radix); - if (result == NaN) { - return NaN; - } - let temp: long = result as long; - return sign === NEGATIVE_SIGN ? -temp : temp; - } - - private static stringToLongWithRadix(digits: string, radix: int): long|double { - if (digits.length === 0) { - let temp: long = 0; - return temp; - } - - let result: long = 0; - const ZERO_CODE: int = c'0'.toInt(); - const A_CODE: int = c'a'.toInt(); - - let temp: string = digits.toLocaleLowerCase(); - let len: int = digits.length.toInt(); - for (let i: int = 0; i < len; i++) { - let ch: char = temp.charAt(i); - let code: int = ch.toInt(); - - let digit: int; - if (code >= ZERO_CODE && code <= ZERO_CODE + 9) { - digit = code - ZERO_CODE; - } else if (code >= A_CODE && code <= A_CODE + 25) { - digit = code - A_CODE + 10; - } else { - return NaN; - } - - if (digit >= radix) { - return NaN; - } - - // Multiply previous result by radix and add current digit - result = result * radix + digit; - } - - return result; - } - - private static toBigInt(value: NullishType): long|bigint|double { - if (value instanceof long || value instanceof bigint) { - return value; - } - if (value === undefined || value === null) { - throw new TypeError("Cannot convert a undefine or null value to a BigInt"); - } - if (value instanceof boolean) { - return value === true ? 1n : 0n; - } - if (value instanceof double || value instanceof int) { - throw new TypeError("Cannot convert a Number value to a BigInt"); - } - if (value instanceof string) { - let result = Reflect.parseBigIntFromString(value); - if (result === NaN) { - throw new SyntaxError("Cannot convert string to a BigInt, because not allow Infinity, decimal points, or exponents"); - } - if (result instanceof long) { - return result; - } else if (result instanceof bigint) { - return result.getLong(); - } - } - return NaN; - } - - private static getIndexForTypedArray(target: ArrayLike | ArrayLike, key: PropertyKey): int { - for (let i: int = 0; i < key.length; i++) { - let c: char = key.charAt(i).unboxed(); - if (c > c'9' || c < c'0') { - return -1; - } - } - let temp: double = Double.parseInt(key, 10); - if (temp === NaN || temp === -NaN || temp === Double.POSITIVE_INFINITY || temp === Double.NEGATIVE_INFINITY) { - return -1; - } - let index: int = Double.toInt(temp); - if (index < 0 || index >= Double.toInt(target.length)) { - return -1; - } - return index; - } - - private static setIntTypedArray(target: ArrayLike, key: PropertyKey, value: NullishType): boolean { - let index: int = Reflect.getIndexForTypedArray(target, key); - if (index < 0) { - return true; - } - let temp: double = Reflect.toNumberInt(value); - if (temp === NaN) { - return true; - } - let tempInt: int = temp.toInt(); - if (target instanceof Int8Array) { - (target as Int8Array)[index] = tempInt; - } else if (target instanceof Int16Array) { - (target as Int16Array)[index] = tempInt; - } else if (target instanceof Int32Array) { - (target as Int32Array)[index] = tempInt; - } else if (target instanceof Uint8Array) { - (target as Uint8Array)[index] = tempInt; - } else if (target instanceof Uint16Array) { - (target as Uint16Array)[index] = tempInt; - } else if (target instanceof Uint32Array) { - (target as Uint32Array)[index] = tempInt; - } - return true; - } - - private static setBigIntTypedArray(target: ArrayLike, key: PropertyKey, value: NullishType): boolean { - let index: int = Reflect.getIndexForTypedArray(target, key); - if (index < 0) { - return true; - } - let temp = Reflect.toBigInt(value); - if (temp === NaN) { - return true; - } - let result: long = 0; - if (temp instanceof long) { - result = temp; - } else if (temp instanceof bigint) { - result = temp.getLong(); - } - if (target instanceof BigInt64Array) { - (target as BigInt64Array)[index] = result; - } else if (target instanceof BigUint64Array) { - (target as BigUint64Array)[index] = result; - } - return true; - } - - private static setFloatTypedArray(target: ArrayLike, key: PropertyKey, value: NullishType): boolean { - let index: int = Reflect.getIndexForTypedArray(target, key); - if (index < 0) { - return true; - } - let temp: double = Reflect.toNumberDouble(value); - if (temp === NaN) { - return true; - } - - if (target instanceof Float32Array) { - (target as Float32Array)[index] = temp; - } else if (target instanceof Float64Array) { - (target as Float64Array)[index] = temp; - } - return true; - } - - private static checkIntTypedArray(target: Object): boolean { - return (target instanceof Int8Array - || target instanceof Int16Array - || target instanceof Int32Array - || target instanceof Uint8Array - || target instanceof Uint16Array - || target instanceof Uint32Array); - } - - private static checkBigIntTypedArray(target: Object): boolean { - return (target instanceof BigInt64Array - || target instanceof BigUint64Array); - } - - private static checkFloatTypedArray(target: Object): boolean { - return (target instanceof Float32Array - || target instanceof Float64Array); - } - - /** - * Sets the field of target, equivalent to target.key = value - * - * @param target the target object on which to set the field - * - * @param key the name of the field to set - * - * @param value the value to set. - * - * @returns a Boolean indicating whether or not setting the field was successful - */ - public static set(target: Object, key: PropertyKey, value: NullishType): boolean { - let typedArrayFlag: boolean = Reflect.checkIntTypedArray(target); - if (typedArrayFlag) { - return Reflect.setIntTypedArray(target as ArrayLike, key, value); - } - typedArrayFlag = Reflect.checkBigIntTypedArray(target); - if (typedArrayFlag) { - return Reflect.setBigIntTypedArray(target as ArrayLike, key, value); - } - typedArrayFlag = Reflect.checkFloatTypedArray(target); - if (typedArrayFlag) { - return Reflect.setFloatTypedArray(target as ArrayLike, key, value); - } - - if (!Reflect.has(target, key)) { - return false - } - let v = reflect.Value.of(target) - let vt = v.getType() - if (vt instanceof ClassType) { - let cv = v as ClassValue - if (vt.hasField(key)) { - try { - cv.setFieldByName(key, reflect.Value.of(value)) - return true - } catch (e: Error) { - return false - } - } else { - const setter = Reflect.findMethod(vt, Reflect.SETTER_PREFIX + key) - if (setter !== undefined) { - setter.invoke(target, [value]) - return true - } else { - return false - } - } - } - return false - } - - private static findMethod(type: ClassType, name: string): Method | undefined { - for (let methodIdx = 0; methodIdx < type.getMethodsNum(); methodIdx++) { - const method = type.getMethod(methodIdx) - if (method.getName() == name) { - return method - } - } - - return undefined - } - - /** - * Sets the property of the record - * - * @param target Record that contains the property - * - * @param key name of the property - * - * @param value of the property - * - * @returns a Boolean indicating whether or not setting the property was successful - */ - public static set(target: Record, key: string, value: Object): Boolean { - target[key] = value - return true - } - - /** - * Sets the element of target, equivalent to target[index] = value - * - * @param target the target object on which to set the element - * - * @param index the index of the element to set - * - * @param value the value to set. - * - * @returns a boolean indicating whether or not setting the element was successful - */ - public static set(target: Object, index: number, value: Object): Boolean { - let v = reflect.Value.of(target) - let vt = v.getType() - if (vt instanceof ArrayType) { - if (!Reflect.has(target, index)) { - return false - } - let av = v as ArrayValue - try { - av.setElement(index.toLong(), reflect.Value.of(value)) - } catch (e: Error) { - return false - } - return true - } - return false - } - - /** - * Returns the names of the own fields of an object - * - * @param o object that contains the own fields - * - * @returns array representation of names - */ - public static ownKeys(target: Object): string[] { - return Object.getOwnPropertyNames(target) - } - - /** - * Returns the string keys of the properties of a record - * - * @param target Record that contains the properties - * - * @returns as array of the target Record property names - */ - public static ownKeys(target: Record): string[] { - return Object.getOwnPropertyNames(target) - } - - /** - * Determines whether an object has a member with the specified name - * - * @param target an object - * - * @param key a member name - * - * @returns a boolean indicating whether or not the target has the field - */ - public static has(target: Object, key: string): boolean { - if (!Object.hasOwn(target, key)) { - const t = Type.of(target) - if (t instanceof ClassType) { - const ct = t as ClassType - const mnum = ct.getMethodsNum() - for (let i = 0; i < mnum; ++i) { - const m = ct.getMethod(i) - if (m.isStatic()) { - continue - } - - const methodName = m.getName() - if (methodName == key) { - return true - } else if (methodName == Reflect.GETTER_PREFIX + key) { - return true - } else if (methodName == Reflect.SETTER_PREFIX + key) { - return true - } - } - } else if (t instanceof StringType || t instanceof ArrayType || t instanceof LambdaType) { - return (key == OBJECT_TO_STRING_MEMBER_NAME || - key == OBJECT_TO_LOCALE_STRING_MEMBER_NAME || - key == OBJECT_HAS_OWN_PROPERTY_MEMBER_NAME) - } else if (t instanceof EnumType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } else if (t instanceof UnionType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } else if (t instanceof TupleType) { - // NOTE(shumilov-petr): Not implemented - throw new Error("Not implemented") - } - return false - } - return true - } - - /** - * Determines whether a record has a property with the specified name - * - * @param target Record that contains a property - * - * @param key name of the property - * - * @returns a boolean indicating whether or not the record has the specified property - */ - public static has(target: Record, key: string): boolean { - return target.has(key) - } - - /** - * Determines whether an object has a field with the specified number - * - * @param target an object - * - * @param index an element index - * - * @returns a boolean indicating whether or not the target has the element - */ - public static has(target: Object, index: number): boolean { - return Object.hasOwn(target, index) - } - /** * Determines whether an object is an interface initialized with a literal * diff --git a/static_core/plugins/ets/stdlib/std/core/Proxy.ets b/static_core/plugins/ets/stdlib/std/core/Proxy.ets index afa483ca29..a76a6f0793 100644 --- a/static_core/plugins/ets/stdlib/std/core/Proxy.ets +++ b/static_core/plugins/ets/stdlib/std/core/Proxy.ets @@ -36,11 +36,11 @@ export final class Proxy { private static readonly SETTER_METHOD_PREFIX_LENGTH = Proxy.SETTER_METHOD_PREFIX.getLength() static tryGetTarget(obj: Object): NullishType { - return Reflect.get(obj, Proxy.PROXY_INSTANCE_TARGET_FIELD) + return (reflect.Value.of(obj) as ClassValue).getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() } static tryGetHandler(obj: Object): NullishType { - return Reflect.get(obj, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + return (reflect.Value.of(obj) as ClassValue).getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() } static create(target: T, handler: ProxyHandler): T { @@ -56,8 +56,9 @@ export final class Proxy { const proxyType = Proxy.INSTANCE.createProxyType(targetType, handlerType, targetLinker) const proxyInstance = proxyType.make() as T - Reflect.set(proxyInstance, Proxy.PROXY_INSTANCE_TARGET_FIELD, target) - Reflect.set(proxyInstance, Proxy.PROXY_INSTANCE_HANDLER_FIELD, handler) + const proxyValue = reflect.Value.of(proxyInstance) as ClassValue + proxyValue.setFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD, reflect.Value.of(target)) + proxyValue.setFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD, reflect.Value.of(handler)) return proxyInstance } @@ -246,9 +247,10 @@ export final class Proxy { if (resultType.isPrimitive()) { if (resultType instanceof IntType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Int => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return (this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Numeric).toInt() } @@ -257,9 +259,10 @@ export final class Proxy { })) } else if (resultType instanceof DoubleType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Double => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return (this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Numeric).toDouble() } @@ -268,9 +271,10 @@ export final class Proxy { })) } else if (resultType instanceof ShortType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Short => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return (this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Numeric).toShort() } @@ -279,9 +283,10 @@ export final class Proxy { })) } else if (resultType instanceof FloatType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Float => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return (this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Numeric).toFloat() } @@ -290,9 +295,10 @@ export final class Proxy { })) } else if (resultType instanceof ByteType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Byte => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return (this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Numeric).toByte() } @@ -301,9 +307,10 @@ export final class Proxy { })) } else if (resultType instanceof CharType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Char => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Char } @@ -312,9 +319,10 @@ export final class Proxy { })) } else if (resultType instanceof LongType) { getterCreator.addBody(new CallableBodyFunction((proxy: Object): Long => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return (this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) as Numeric).toLong() } @@ -323,9 +331,10 @@ export final class Proxy { })) } else { getterCreator.addBody(new CallableBodyFunction((proxy: Object): NullishType => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) } @@ -335,9 +344,10 @@ export final class Proxy { } } else { getterCreator.addBody(new CallableBodyFunction((proxy: Object): NullishType => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast return this.handlerGet(__narrowAny(target), __narrowAny>(handler), propertyName) } @@ -367,9 +377,10 @@ export final class Proxy { if (setterParamType.isPrimitive()) { if (setterParamType instanceof IntType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Int): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -381,9 +392,10 @@ export final class Proxy { })) } else if (setterParamType instanceof DoubleType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Double): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -395,9 +407,10 @@ export final class Proxy { })) } else if (setterParamType instanceof ShortType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Short): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -409,9 +422,10 @@ export final class Proxy { })) } else if (setterParamType instanceof FloatType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Float): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -423,9 +437,10 @@ export final class Proxy { })) } else if (setterParamType instanceof ByteType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Byte): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -437,9 +452,10 @@ export final class Proxy { })) } else if (setterParamType instanceof CharType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Char): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -451,9 +467,10 @@ export final class Proxy { })) } else if (setterParamType instanceof LongType) { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: Long): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -465,9 +482,10 @@ export final class Proxy { })) } else { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: NullishType): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -480,9 +498,10 @@ export final class Proxy { } } else { setterCreator.addBody(new CallableBodyFunction((proxy: Object, value: NullishType): void => { - const handler = Reflect.get(proxy, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxy) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxy, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() // using "wrapper" method to "capture" after smartcast const success = this.handlerSet(__narrowAny(target), __narrowAny>(handler), propertyName, value as object) if (!success) { @@ -513,9 +532,10 @@ export final class Proxy { .addResult(methodType.getResultType()) .addBody(new CallableBodyErasedFunction((proxy: NullishType, args: FixedArray): NullishType => { const proxyInstance = proxy as Object - const handler = Reflect.get(proxyInstance, Proxy.PROXY_INSTANCE_HANDLER_FIELD) + const proxyValue = reflect.Value.of(proxyInstance) as ClassValue + const handler = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_HANDLER_FIELD).getData() if (handler instanceof ProxyHandler) { - const target = Reflect.get(proxyInstance, Proxy.PROXY_INSTANCE_TARGET_FIELD) + const target = proxyValue.getFieldByName(Proxy.PROXY_INSTANCE_TARGET_FIELD).getData() return this.handlerInvoke(__narrowAny(target), __narrowAny>(handler), method, args) } @@ -611,11 +631,32 @@ export interface ProxyHandler { export abstract class DefaultProxyHandler implements ProxyHandler { override get(target: T, name: string): NullishType { - return Reflect.get(target, name) + const GETTER_PREFIX = ""; + const targetType = Type.of(target) as ClassType + + try { + const getter = targetType.getMethodByName(GETTER_PREFIX + name) + return getter.invoke(target, []) + } catch(e: Error) { + console.error(e) + } + + return false } override set(target: T, name: string, value: NullishType): boolean { - return Reflect.set(target, name, value) + const SETTER_PREFIX = ""; + const targetType = Type.of(target) as ClassType + + try { + const setter = targetType.getMethodByName(SETTER_PREFIX + name) + setter.invoke(target, [value]) + return true + } catch(e: Error) { + console.error(e) + } + + return false } override invoke(target: T, method: Method, args: FixedArray): NullishType { diff --git a/static_core/plugins/ets/stdlib/std/core/Type.ets b/static_core/plugins/ets/stdlib/std/core/Type.ets index 927b3a6c26..4a2e65b215 100644 --- a/static_core/plugins/ets/stdlib/std/core/Type.ets +++ b/static_core/plugins/ets/stdlib/std/core/Type.ets @@ -1747,6 +1747,16 @@ export final class ClassType extends Type { throw new TypeError("no field \"" + name + "\"") } + public getMethodByName(name: string): Method{ + for (let methodIdx = 0; methodIdx < this.getMethodsNum(); ++methodIdx) { + const method = this.getMethod(methodIdx) + if (method.getName() == name && !method.isStatic()) { + return method + } + } + throw new TypeError("no instance method \"" + name + "\"") + } + private getFieldImpl(fieldPtr: long): Field { const fieldDesc = TypeAPI.getFieldDescriptor(fieldPtr) const fieldType = Type.resolve(fieldDesc, this.getContextLinker())! diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ObjectLiteralTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ObjectLiteralTest.ets index e9cf336e8a..7c135912f9 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ObjectLiteralTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/ObjectLiteralTest.ets @@ -25,15 +25,17 @@ function testObjectAssignKeys(): void { const objKeys = Object.keys(obj) const newObjKeys = Object.keys(newObj) - arktest.assertEQ(objKeys.length, newObjKeys.length, "unexpected keys number") + arktest.assertEQ(objKeys.length, newObjKeys.length, "unexpected keys number") for (let i = 0; i < objKeys.length; i++) { - arktest.assertEQ(objKeys[i], newObjKeys[i], "unexpected property name: " + newObjKeys[i]) + arktest.assertEQ(objKeys[i], newObjKeys[i], "unexpected property name: " + newObjKeys[i]) const propName = objKeys[i] - const propVal = Reflect.get(newObj, propName) + const newObjVal = reflect.Value.of(newObj) as ClassValue + const propVal = newObj[propName] - arktest.assertEQ(Reflect.get(obj, propName), propVal, `unexpected '${propName}' property value: ${propVal}`) + arktest.assertEQ((reflect.Value.of(obj) as ClassValue).getFieldByName(propName).getData(), + propVal, `unexpected '${propName}' property value: ${propVal}`) } } @@ -111,4 +113,4 @@ function main(): int { suite.addTest("Object.assign(Record) result property names", testObjectAssignGetOwnPropNames) return suite.run() -} \ No newline at end of file +} diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ProxyTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ProxyTest.ets index e9462b4f55..03701d3de6 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ProxyTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/ProxyTest.ets @@ -96,14 +96,20 @@ interface ProxyTest { class interfaceProxyHandler implements proxy.ProxyHandler{ public get(target:T, name:string):NullishType { - const value = Reflect.get(target, name) as Any - return value + return (reflect.Value.of(target) as ClassValue).getFieldByName(name).getData() } public set(target:T, name:string, newValue:NullishType):boolean { - if (Reflect.get(target, name) !== newValue) { - const result = Reflect.set(target, name, newValue) - return result + const targetValue = reflect.Value.of(target) as ClassValue + const value = targetValue.getFieldByName(name).getData() + + if (value !== newValue) { + try { + targetValue.setFieldByName(name, reflect.Value.of(newValue)) + } catch(e) { + console.error(e) + return false + } } return true } diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGet.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGet.ets deleted file mode 100644 index 052bd1957f..0000000000 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGet.ets +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function main(): int { - let failures: int = 0; - - // get - failures += test(reflectGetClass(), "Reflect.get on class type"); - failures += test(reflectGetArray(), "Reflect.get on array type"); - failures += test(reflectGetLambda(), "Reflect.get on function type"); - - return test(failures, "All tests run"); -} - -class Point2D { - static axisnum: number = 2 - x: number - y: number - constructor (x_: number, y_: number) { - this.x = x_ - this.y = y_ - } -} - -class Point3D extends Point2D { - static axisnum: number = 3 - z: number - constructor (x_: number, y_: number, z_: number) { - super(x_, y_) - this.z = z_ - } -} - -function reflectGetClass(): int { - let result: int = 0 - - let p: Point2D = new Point3D(10, 20, 30) - - result += (Reflect.get(p, "x") as Number == 10) ? 0 : 1 - result += (Reflect.get(p, "y") as Number == 20) ? 0 : 1 - result += (Reflect.get(p, "z") as Number == 30) ? 0 : 1 - - result += (Reflect.get(p, "axisnum") == undefined) ? 0 : 1 - result += (Reflect.get(p, "qwerty") == undefined) ? 0 : 1 - result += (Reflect.get(p, 1) == null) ? 0 : 1 - - return result -} - -function reflectGetArray(): int { - let result: int = 0 - - let arr: FixedArray = [10, 20, 30] - let brr: FixedArray = ["p", "q", "t", "w"] - - result += (Reflect.get(arr, 0) as Number == 10) ? 0 : 1 - result += (Reflect.get(arr, 1) as Number == 20) ? 0 : 1 - result += (Reflect.get(arr, 2) as Number == 30) ? 0 : 1 - - result += (Reflect.get(brr, 0) == "p") ? 0 : 1 - result += (Reflect.get(brr, 1) == "q") ? 0 : 1 - result += (Reflect.get(brr, 2) == "t") ? 0 : 1 - - result += (Reflect.get(arr, "length") as Number == 3) ? 0 : 1 - result += (Reflect.get(brr, "length") as Number == 4) ? 0 : 1 - - result += (Reflect.get(arr, "qwerty") == undefined) ? 0 : 1 - result += (Reflect.get(arr, 10) == undefined) ? 0 : 1 - result += (Reflect.get(brr, 4) == undefined) ? 0 : 1 - - return result -} - -function reflectGetLambda(): int { - let result: int = 0 - - let lambda: (a: number) => number = (a: number): number => { - return a + 1 - } - - result += (Reflect.get(lambda, 0) == null) ? 0 : 1 - result += (Reflect.get(lambda, "length") as Number == 1) ? 0 : 1 - result += (Reflect.get(lambda, "name") == "") ? 0 : 1 - - result += (Reflect.get(lambda, "qwerty") == undefined) ? 0 : 1 - - return result -} - -function test(result: int, message: String ): int { - if (result == 0) { - console.log('PASSED: ' + message); - return 0; - } - console.log('FAILED: ' + message); - return 1; -} diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGetBadCases.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGetBadCases.ets deleted file mode 100644 index 815f3296a0..0000000000 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectGetBadCases.ets +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function main(): int { - let failures: int = 0; - - // get - failures += test(reflectGetBadCases(), "Reflect.get on types without fields and elements"); - - return test(failures, "All tests run"); -} - -class Point2D { - static axisnum: number = 2 - x: number - y: number - constructor (x_: number, y_: number) { - this.x = x_ - this.y = y_ - } -} - -class Point3D extends Point2D { - static axisnum: number = 3 - z: number - constructor (x_: number, y_: number, z_: number) { - super(x_, y_) - this.z = z_ - } -} - -function reflectGetBadCases(): int { - let c: char = c'c' - let bo: boolean = true - let bt: byte = 10 - let sh: short = 20 - let i: int = 30 - let lo: long = 40 - let fl: float = 50.0f - let dou: double = 60.0 - let s: string = "abc" - - let result: int = 0 - try { - Reflect.get(c, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(bo, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(bt, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(sh, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(i, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(lo, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(fl, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(dou, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - try { - Reflect.get(s, "a") - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must have fields", 0) ? 0 : 1 - } - - // ---------- - - try { - Reflect.get(c, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(bo, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(bt, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(sh, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(i, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(lo, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(fl, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(dou, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - try { - Reflect.get(s, 1) - } catch (e: Error) { - result += e.toString().contains("`target` argument of Reflect.get must be indexed", 0) ? 0 : 1 - } - - return result -} - -function test(result: int, message: String ): int { - if (result == 0) { - console.log('PASSED: ' + message); - return 0; - } - console.log('FAILED: ' + message); - return 1; -} diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectHas.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectHas.ets deleted file mode 100644 index 850e8fb286..0000000000 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectHas.ets +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function main(): int { - let failures: int = 0; - // has - failures += test(reflectHas(), "Reflect.has"); - - return test(failures, "All tests run"); -} - -class Point2D { - static axisnum: number = 2 - x: number - y: number - constructor (x_: number, y_: number) { - this.x = x_ - this.y = y_ - } - public norm(): number { - return this.x + this.y - } -} - -class Point3D extends Point2D { - static axisnum: number = 3 - z: number - constructor (x_: number, y_: number, z_: number) { - super(x_, y_) - this.z = z_ - } -} - -function arraysAreEqual(a: FixedArray, b: FixedArray): boolean { - let alen = a.length - if (alen != b.length) { - return false - } - for (let i = 0; i < alen; i++) { - if (a[i] != b[i]) { - return false - } - } - return true -} - -function reflectHas(): int { - let result: int = 0 - - let arr: FixedArray = [10, 20, 30] - let str: string = "abc" - - let cl: Point2D = new Point3D(10, 20, 30) - - let lambda: (a: number) => number = (a: number): number => { - return a + 1 - } - - result += (Reflect.has(arr, 0) == true) ? 0 : 1 - result += (Reflect.has(arr, 3) == false) ? 0 : 1 - result += (Reflect.has(arr, "length") == true) ? 0 : 1 - result += (Reflect.has(arr, "qwerty") == false) ? 0 : 1 - result += (Reflect.has(arr, "toString") == true) ? 0 : 1 - result += (Reflect.has(arr, "toLocaleString") == true) ? 0 : 1 - result += (Reflect.has(arr, "keys") == false) ? 0 : 1 - - result += (Reflect.has(str, 0) == true) ? 0 : 1 - result += (Reflect.has(str, 3) == false) ? 0 : 1 - result += (Reflect.has(str, "length") == true) ? 0 : 1 - result += (Reflect.has(str, "qwerty") == false) ? 0 : 1 - result += (Reflect.has(str, "toString") == true) ? 0 : 1 - result += (Reflect.has(str, "hasOwnProperty") == true) ? 0 : 1 - result += (Reflect.has(str, "entries") == false) ? 0 : 1 - - result += (Reflect.has(cl, "x") == true) ? 0 : 1 - result += (Reflect.has(cl, "y") == true) ? 0 : 1 - result += (Reflect.has(cl, "z") == true) ? 0 : 1 - result += (Reflect.has(cl, "axisnum") == false) ? 0 : 1 - result += (Reflect.has(cl, "asdasd") == false) ? 0 : 1 - result += (Reflect.has(cl, 0) == false) ? 0 : 1 - result += (Reflect.has(cl, "norm") == true) ? 0 : 1 - result += (Reflect.has(cl, "toString") == true) ? 0 : 1 - result += (Reflect.has(cl, "hasOwnProperty") == true) ? 0 : 1 - result += (Reflect.has(cl, "getOwnPropertyNames") == false) ? 0 : 1 - - result += (Reflect.has(lambda, "length") == true) ? 0 : 1 - result += (Reflect.has(lambda, "name") == true) ? 0 : 1 - result += (Reflect.has(lambda, 0) == false) ? 0 : 1 - result += (Reflect.has(lambda, "toString") == true) ? 0 : 1 - result += (Reflect.has(lambda, "toLocaleString") == true) ? 0 : 1 - result += (Reflect.has(lambda, "hasOwnProperty") == true) ? 0 : 1 - - return result -} - -function test(result: int, message: String ): int { - if (result == 0) { - console.log('PASSED: ' + message); - return 0; - } - console.log('FAILED: ' + message); - return 1; -} diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectOwnKeys.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectOwnKeys.ets deleted file mode 100644 index 2c96cc31f0..0000000000 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectOwnKeys.ets +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const emptyArray: string[] = [] - -function compareArray(first: string[], second: string[]) { - arktest.assertEQ(first.length, second.length) - for (let i = 0; i < first.length; i++) { - arktest.assertEQ(first[i], second[i]) - } -} - -function booleanTest() { - compareArray(Reflect.ownKeys(true), emptyArray) - compareArray(Reflect.ownKeys(false), emptyArray) -} - -function byteTest() { - compareArray(Reflect.ownKeys(0 as byte), emptyArray) - compareArray(Reflect.ownKeys(Byte.MIN_VALUE), emptyArray) - compareArray(Reflect.ownKeys(Byte.MAX_VALUE), emptyArray) -} - -function charTest() { - compareArray(Reflect.ownKeys(c'\0'), emptyArray) - compareArray(Reflect.ownKeys(c'1'), emptyArray) -} - -function shortTest() { - compareArray(Reflect.ownKeys(0 as short), emptyArray) - compareArray(Reflect.ownKeys(Short.MIN_VALUE), emptyArray) - compareArray(Reflect.ownKeys(Short.MAX_VALUE), emptyArray) -} - -function intTest() { - compareArray(Reflect.ownKeys(0), emptyArray) - compareArray(Reflect.ownKeys(Int.MIN_VALUE), emptyArray) - compareArray(Reflect.ownKeys(Int.MAX_VALUE), emptyArray) -} - -function longTest() { - compareArray(Reflect.ownKeys(0 as long), emptyArray) - compareArray(Reflect.ownKeys(Long.MIN_VALUE), emptyArray) - compareArray(Reflect.ownKeys(Long.MAX_VALUE), emptyArray) -} - -function floatTest() { - compareArray(Reflect.ownKeys(Double.toFloat(0.0)), emptyArray) - compareArray(Reflect.ownKeys(Float.MIN_VALUE), emptyArray) - compareArray(Reflect.ownKeys(Float.MAX_VALUE), emptyArray) -} - -function doubleTest() { - compareArray(Reflect.ownKeys(0.0), emptyArray) - compareArray(Reflect.ownKeys(Double.MIN_VALUE), emptyArray) - compareArray(Reflect.ownKeys(Double.MAX_VALUE), emptyArray) -} - -function stringTest() { - let str = '' - compareArray(Reflect.ownKeys(str), ['length']) - str = 'abc' - compareArray(Reflect.ownKeys(str), ['0', '1', '2', 'length']) -} - -class Point2D { - public static axisnum: number = 2 - public x: number - public y: number - constructor (x_: number, y_: number) { - this.x = x_ - this.y = y_ - } -} - -class Point3D extends Point2D { - public static axisnum: number = 3 - public z: number - constructor (x_: number, y_: number, z_: number) { - super(x_, y_) - this.z = z_ - } -} - -function classTest() { - let p1 = new Point2D(1, 2) - compareArray(Reflect.ownKeys(p1), ['x', 'y']) - let p2 = new Point3D(3, 4, 5) - compareArray(Reflect.ownKeys(p2), ['x', 'y', 'z']) -} - -function fixedArrayTest() { - let arr: FixedArray = [] - compareArray(Reflect.ownKeys(arr), ['length']) - arr = [1, 20, 30] - compareArray(Reflect.ownKeys(arr), ['0', '1', '2', 'length']) - - let arr2: FixedArray = [new Point2D(7, 9), new Point3D(0, 0, 1)] - compareArray(Reflect.ownKeys(arr2), ['0', '1', 'length']) -} - -function arrayTest() { - let arr: Array = [] - compareArray(Reflect.ownKeys(arr), ['length']) - arr = [1.0, 12.5, 5.0] - compareArray(Reflect.ownKeys(arr), ['0', '1', '2', 'length']) - - let arr2: Array = [new Point2D(0, 1), new Point3D(0, 1, 2)] - compareArray(Reflect.ownKeys(arr2), ['0', '1', 'length']) - - let arr3 = new Array() - compareArray(Reflect.ownKeys(arr3), ['length']) - - let arr4 = new Array(5) - compareArray(Reflect.ownKeys(arr4), ['0', '1', '2', '3', '4', 'length']) -} - -function lambdaTest() { - let lambda: (a: number) => number = (a: number): number => { - return a + 1 - } - compareArray(Reflect.ownKeys(lambda), ["length", "name"]) - - compareArray(Reflect.ownKeys(arrayTest), ["length", "name"]) -} - -function main(): int { - const suite = new arktest.ArkTestsuite('Reflect.ownKeys tests') - suite.addTest('for boolean', booleanTest) - suite.addTest('for byte', byteTest) - suite.addTest('for char', charTest) - suite.addTest('for short', shortTest) - suite.addTest('for int', intTest) - suite.addTest('for long', longTest) - suite.addTest('for float', floatTest) - suite.addTest('for double', doubleTest) - suite.addTest('for string', stringTest) - suite.addTest('for class', classTest) - suite.addTest('for FixedArray', fixedArrayTest) - suite.addTest('for Array', arrayTest) - suite.addTest('for lambda', lambdaTest) - return suite.run() -} diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectSet.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectSet.ets deleted file mode 100644 index 46b7f5c026..0000000000 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ReflectSet.ets +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function main(): int { - let failures: int = 0; - - // set - failures += test(reflectSetClass(), "Reflect.set on class type"); - failures += test(reflectSetArray(), "Reflect.set on array type"); - failures += test(reflectSetTypeConvert(), "Reflect.set convert type"); - - return test(failures, "All tests run"); -} - -class Point2D { - static axisnum: number = 2 - x: number - y: number - constructor (x_: number, y_: number) { - this.x = x_ - this.y = y_ - } -} - -class Point3D extends Point2D { - static axisnum: number = 3 - z: number - constructor (x_: number, y_: number, z_: number) { - super(x_, y_) - this.z = z_ - } -} - -class C { - public p1:number = 0; - public p2:float = 0.0f; - public p3:long = 0; - public p4:int = 0; - public p5:short = 0; -} - -function arraysAreEqual(a: FixedArray, b: FixedArray): boolean { - let alen = a.length - if (alen != b.length) { - return false - } - for (let i = 0; i < alen; i++) { - if (a[i] != b[i]) { - return false - } - } - return true -} - -function reflectSetClass(): int { - let result: int = 0 - - let p: Point2D = new Point3D(10, 20, 30) - - result += (Reflect.set(p, "x", 40 as number) == true) ? 0 : 1 - result += (Reflect.set(p, "y", 50 as number) == true) ? 0 : 1 - result += (Reflect.set(p, "z", 60 as number) == true) ? 0 : 1 - - result += (Reflect.get(p, "x") as Number == 40) ? 0 : 1 - result += (Reflect.get(p, "y") as Number == 50) ? 0 : 1 - result += (Reflect.get(p, "z") as Number == 60) ? 0 : 1 - - result += (Reflect.set(p, "axisnum", 10 as number) == false) ? 0 : 1 - result += (Reflect.set(p, "x", "string") == false) ? 0 : 1 - - return result -} - -function reflectSetTypeConvert(): int { - let result: int = 0 - - let sample = new C(); - - let a: byte = 1; - let b: short = 2; - let c: int = 3; - let d: long = 4; - let e: float = 5.0f; - - Reflect.set(sample, 'p1', a); - result += (sample.p1 == a) ? 0 : 1 - Reflect.set(sample, 'p1', b); - result += (sample.p1 == b) ? 0 : 1 - Reflect.set(sample, 'p1', c); - result += (sample.p1 == c) ? 0 : 1 - Reflect.set(sample, 'p1', d); - result += (sample.p1 == d) ? 0 : 1 - Reflect.set(sample, 'p1', e); - result += (sample.p1 == e) ? 0 : 1 - - Reflect.set(sample, 'p2', a); - result += (sample.p2 == a) ? 0 : 1 - Reflect.set(sample, 'p2', b); - result += (sample.p2 == b) ? 0 : 1 - Reflect.set(sample, 'p2', c); - result += (sample.p2 == c) ? 0 : 1 - Reflect.set(sample, 'p2', d); - result += (sample.p2 == d) ? 0 : 1 - - Reflect.set(sample, 'p3', a); - result += (sample.p3 == a) ? 0 : 1 - Reflect.set(sample, 'p3', b); - result += (sample.p3 == b) ? 0 : 1 - Reflect.set(sample, 'p3', c); - result += (sample.p3 == c) ? 0 : 1 - - Reflect.set(sample, 'p4', a); - result += (sample.p4 == a) ? 0 : 1 - Reflect.set(sample, 'p4', b); - result += (sample.p4 == b) ? 0 : 1 - - Reflect.set(sample, 'p5', a); - result += (sample.p5 == a) ? 0 : 1 - - return result -} - -function reflectSetArray(): int { - let result: int = 0 - - let arr: FixedArray = [10, 20, 30] - let brr: FixedArray = ["p", "q", "t", "w"] - - result += (Reflect.set(arr, 0, 40 as number) == true) ? 0 : 1 - result += (Reflect.set(arr, 1, 50 as number) == true) ? 0 : 1 - result += (Reflect.set(arr, 2, 60 as number) == true) ? 0 : 1 - - result += (Reflect.get(arr, 0) as Number == 40) ? 0 : 1 - result += (Reflect.get(arr, 1) as Number == 50) ? 0 : 1 - result += (Reflect.get(arr, 2) as Number == 60) ? 0 : 1 - - result += (Reflect.set(arr, 100, 10 as number) == false) ? 0 : 1 - result += (Reflect.set(arr, 0, "string") == false) ? 0 : 1 - - return result -} - -function test(result: int, message: String ): int { - if (result == 0) { - console.log('PASSED: ' + message); - return 0; - } - console.log('FAILED: ' + message); - return 1; -} diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/core/ReflectLambdaTest.ets b/static_core/plugins/ets/tests/ets_func_tests/std/core/ReflectLambdaTest.ets index d73ba2df2f..e91a6712f3 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/std/core/ReflectLambdaTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/std/core/ReflectLambdaTest.ets @@ -22,7 +22,14 @@ interface A { function testReflectLamdaTest() { let obja: A = { a:(index: number, str:ClassA) => {return [1, 2] as [number, number]} } let objb: A = { a:(index: number, str:ClassA) => {return [3, 4] as [number, number]} } - let ret = Reflect.set(obja, "a", objb.a) + let ret = false + try { + (reflect.Value.of(obja) as ClassValue).setFieldByName("a", reflect.Value.of(objb.a)) + ret = true + } catch(e) { + console.error(e) + ret = false + } arktest.assertEQ(ret, true); } @@ -30,4 +37,4 @@ function main(): int { const suite = new arktest.ArkTestsuite("Reflect.LambdaTest"); suite.addTest("Test testReflectLamdaTest", testReflectLamdaTest); return suite.run() -} \ No newline at end of file +} diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/core/TypedArrayReflectSetTest.ets b/static_core/plugins/ets/tests/ets_func_tests/std/core/TypedArrayReflectSetTest.ets deleted file mode 100644 index f9590693af..0000000000 --- a/static_core/plugins/ets/tests/ets_func_tests/std/core/TypedArrayReflectSetTest.ets +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function testInt8Array() { - let sample = new Int8Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '127'), true); - arktest.assertEQ(sample[0], 127); - - arktest.assertEQ(Reflect.set(sample, '0', '0x7F'), true); - arktest.assertEQ(sample[0], 127); - - arktest.assertEQ(Reflect.set(sample, '0', '0o77'), true); - arktest.assertEQ(sample[0], 63); - - arktest.assertEQ(Reflect.set(sample, '0', '0b101010'), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '3.9'), true); - arktest.assertEQ(sample[0], 3); - - arktest.assertEQ(Reflect.set(sample, '0', 7.8), true); - arktest.assertEQ(sample[0], 7); - - arktest.assertEQ(Reflect.set(sample, '0', '-0x1F'), true); - arktest.assertEQ(sample[0], 0); -} - -function testInt16Array() { - let sample = new Int16Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '32767'), true); - arktest.assertEQ(sample[0], 32767); - - arktest.assertEQ(Reflect.set(sample, '0', '0x7FFF'), true); - arktest.assertEQ(sample[0], 32767); - - arktest.assertEQ(Reflect.set(sample, '0', '0o77777'), true); - arktest.assertEQ(sample[0], 32767); - - arktest.assertEQ(Reflect.set(sample, '0', '0b111111111111111'), true); - arktest.assertEQ(sample[0], 32767); - - arktest.assertEQ(Reflect.set(sample, '0', '123.4'), true); - arktest.assertEQ(sample[0], 123); - - arktest.assertEQ(Reflect.set(sample, '0', 456.789), true); - arktest.assertEQ(sample[0], 456); -} - -function testInt32Array() { - let sample = new Int32Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '2147483647'), true); - arktest.assertEQ(sample[0], 2147483647); - - arktest.assertEQ(Reflect.set(sample, '0', '0x7FFFFFFF'), true); - arktest.assertEQ(sample[0], 2147483647); - - arktest.assertEQ(Reflect.set(sample, '0', '0o17777777777'), true); - arktest.assertEQ(sample[0], 2147483647); - - arktest.assertEQ(Reflect.set(sample, '0', '0b1111111111111111111111111111111'), true); - arktest.assertEQ(sample[0], 2147483647); - - arktest.assertEQ(Reflect.set(sample, '0', '789.123'), true); - arktest.assertEQ(sample[0], 789); - - arktest.assertEQ(Reflect.set(sample, '0', 1000.456), true); - arktest.assertEQ(sample[0], 1000); -} - -function testBigInt64Array() { - let sample = new BigInt64Array([42n]); - arktest.assertEQ(Reflect.set(sample, '1.1', new Long(1)), true); - arktest.assertEQ(sample[0], 42n); - - arktest.assertEQ(Reflect.set(sample, '0.0001', new Long(1)), true); - arktest.assertEQ(sample[0], 42n); - - arktest.assertEQ(Reflect.set(sample, '0', 99n), true); - arktest.assertEQ(sample[0], 99n); - - arktest.assertEQ(Reflect.set(sample, '0', '0x2A'), true); - arktest.assertEQ(sample[0], 42n); - - arktest.assertEQ(Reflect.set(sample, '0', '0b101010'), true); - arktest.assertEQ(sample[0], 42n); -} - -function testUint8Array() { - let sample = new Uint8Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '255'), true); - arktest.assertEQ(sample[0], 255); - - arktest.assertEQ(Reflect.set(sample, '0', '0xFF'), true); - arktest.assertEQ(sample[0], 255); - - arktest.assertEQ(Reflect.set(sample, '0', '0b11111111'), true); - arktest.assertEQ(sample[0], 255); - - arktest.assertEQ(Reflect.set(sample, '0', '0o377'), true); - arktest.assertEQ(sample[0], 255); - - arktest.assertEQ(Reflect.set(sample, '0', '3.14'), true); - arktest.assertEQ(sample[0], 3); - - arktest.assertEQ(Reflect.set(sample, '0', 5.9), true); - arktest.assertEQ(sample[0], 5); -} - -function testUint16Array() { - let sample = new Uint16Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '65535'), true); - arktest.assertEQ(sample[0], 65535); - - arktest.assertEQ(Reflect.set(sample, '0', '0xFFFF'), true); - arktest.assertEQ(sample[0], 65535); - - arktest.assertEQ(Reflect.set(sample, '0', '0o177777'), true); - arktest.assertEQ(sample[0], 65535); - - arktest.assertEQ(Reflect.set(sample, '0', '0b1111111111111111'), true); - arktest.assertEQ(sample[0], 65535); - - arktest.assertEQ(Reflect.set(sample, '0', '1234.56'), true); - arktest.assertEQ(sample[0], 1234); - - arktest.assertEQ(Reflect.set(sample, '0', 2345.78), true); - arktest.assertEQ(sample[0], 2345); -} - -function testUint32Array() { - let sample = new Uint32Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1), true); - arktest.assertEQ(sample[0], 42); - - arktest.assertEQ(Reflect.set(sample, '0', '8765.4321'), true); - arktest.assertEQ(sample[0], 8765); - - arktest.assertEQ(Reflect.set(sample, '0', 123456.789), true); - arktest.assertEQ(sample[0], 123456); -} - -function testBigUint64Array() { - let sample = new BigUint64Array([42n]); - arktest.assertEQ(Reflect.set(sample, '1.1', new Long(1)), true); - arktest.assertEQ(sample[0], 42n); - - arktest.assertEQ(Reflect.set(sample, '0.0001', new Long(1)), true); - arktest.assertEQ(sample[0], 42n); - - arktest.assertEQ(Reflect.set(sample, '0', BigInt('123456789')), true); - arktest.assertEQ(sample[0], 123456789n); -} - -function testFloat32Array() { - let sample = new Float32Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1.0), true); - arktest.assertDoubleEQ(sample[0], 42.0, 0.001); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1.0), true); - arktest.assertDoubleEQ(sample[0], 42.0, 0.001); - - arktest.assertEQ(Reflect.set(sample, '0', '3.14'), true); - arktest.assertDoubleEQ(sample[0], 3.14, 0.001); - - arktest.assertEQ(Reflect.set(sample, '0', 2.718), true); - arktest.assertDoubleEQ(sample[0], 2.718, 0.001); - - arktest.assertEQ(Reflect.set(sample, '0', '123.456'), true); - arktest.assertDoubleEQ(sample[0], 123.456, 0.001); - - arktest.assertEQ(Reflect.set(sample, '0', '123'), true); - arktest.assertDoubleEQ(sample[0], 123, 0.001); -} - -function testFloat64Array() { - let sample = new Float64Array([42]); - arktest.assertEQ(Reflect.set(sample, '1.1', 1.0), true); - arktest.assertDoubleEQ(sample[0], 42.0, 0.00001); - - arktest.assertEQ(Reflect.set(sample, '0.0001', 1.0), true); - arktest.assertDoubleEQ(sample[0], 42.0, 0.00001); - - arktest.assertEQ(Reflect.set(sample, '0', '3.14159'), true); - arktest.assertDoubleEQ(sample[0], 3.14159, 0.00001); - - arktest.assertEQ(Reflect.set(sample, '0', 2.71828), true); - arktest.assertDoubleEQ(sample[0], 2.71828, 0.00001); - - arktest.assertEQ(Reflect.set(sample, '0', '123'), true); - arktest.assertDoubleEQ(sample[0], 123, 0.00001); -} - -function main() { - let myTestsuite = new arktest.ArkTestsuite("TypedArray ReflectSet Suite"); - myTestsuite.addTest("testInt8Array", testInt8Array); - myTestsuite.addTest("testInt16Array", testInt16Array); - myTestsuite.addTest("testInt32Array", testInt32Array); - myTestsuite.addTest("testBigInt64Array", testBigInt64Array); - myTestsuite.addTest("testUint8Array", testUint8Array); - myTestsuite.addTest("testUint16Array", testUint16Array); - myTestsuite.addTest("testUint32Array", testUint32Array); - myTestsuite.addTest("testBigUint64Array", testBigUint64Array); - myTestsuite.addTest("testFloat32Array", testFloat32Array); - myTestsuite.addTest("testFloat64Array", testFloat64Array); - myTestsuite.run(); -} -- Gitee