From 9faa43f1bb2517c9ae9cbed869e240d86a812172 Mon Sep 17 00:00:00 2001 From: Sergey Platov Date: Fri, 8 Dec 2023 16:43:23 +0300 Subject: [PATCH] Interoperability scenarios: primitives as argument and return values Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/I8W7QL Test: build Signed-off-by: sergey_platov --- ...ion_arg_type_optional_primitive_default.js | 24 +++++++++++++++++++ ...on_arg_type_optional_primitive_explicit.js | 24 +++++++++++++++++++ .../test_function_arg_type_primitive.js | 24 +++++++++++++++++++ .../test_function_return_type_primitive.js | 24 +++++++++++++++++++ .../tests/scenarios/ets_to_js/scenarios.cpp | 21 ++++++++++++++++ .../tests/scenarios/ets_to_js/scenarios.ets | 12 ++++++++++ .../tests/scenarios/js_to_ets/scenarios.cpp | 24 +++++++++++++++++++ .../tests/scenarios/js_to_ets/scenarios.ets | 21 +++++++++++++++- .../tests/scenarios/js_to_ets/scenarios.js | 20 +++++++++++++++- 9 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_default.js create mode 100644 static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_explicit.js create mode 100644 static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_primitive.js create mode 100644 static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_return_type_primitive.js diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_default.js b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_default.js new file mode 100644 index 0000000000..62a0fdc4c5 --- /dev/null +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_default.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2021-2024 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 { etsVm, getTestModule } = require('scenarios.test.js'); + +const etsMod = getTestModule('scenarios_test'); +const GCJSRuntimeCleanup = etsMod.getFunction('GCJSRuntimeCleanup'); +const functionArgTypeOptionalPrimitiveEts = etsMod.getFunction('function_arg_type_optional_primitive'); + +{ + let ret = functionArgTypeOptionalPrimitiveEts(); + ASSERT_EQ(ret, undefined); +} diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_explicit.js b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_explicit.js new file mode 100644 index 0000000000..2b1f850236 --- /dev/null +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_optional_primitive_explicit.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2021-2024 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 { etsVm, getTestModule } = require('scenarios.test.js'); + +const etsMod = getTestModule('scenarios_test'); +const GCJSRuntimeCleanup = etsMod.getFunction('GCJSRuntimeCleanup'); +const functionArgTypeOptionalPrimitiveEts = etsMod.getFunction('function_arg_type_optional_primitive'); + +{ + let ret = functionArgTypeOptionalPrimitiveEts(1); + ASSERT_EQ(ret, 1); +} diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_primitive.js b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_primitive.js new file mode 100644 index 0000000000..a5977e862a --- /dev/null +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_arg_type_primitive.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2021-2024 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 { etsVm, getTestModule } = require('scenarios.test.js'); + +const etsMod = getTestModule('scenarios_test'); +const GCJSRuntimeCleanup = etsMod.getFunction('GCJSRuntimeCleanup'); +const functionArgTypePrimitiveEts = etsMod.getFunction('function_arg_type_primitive'); + +{ + let ret = functionArgTypePrimitiveEts(1); + ASSERT_EQ(ret, 1); +} diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_return_type_primitive.js b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_return_type_primitive.js new file mode 100644 index 0000000000..5d93640718 --- /dev/null +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/js_suites/test_function_return_type_primitive.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2021-2024 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 { etsVm, getTestModule } = require('scenarios.test.js'); + +const etsMod = getTestModule('scenarios_test'); +const GCJSRuntimeCleanup = etsMod.getFunction('GCJSRuntimeCleanup'); +const functionReturnTypePrimitiveEts = etsMod.getFunction('function_return_type_primitive'); + +{ + let ret = functionReturnTypePrimitiveEts(); + ASSERT_EQ(typeof ret, 'number'); +} diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.cpp b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.cpp index 49b9098ba3..7b54d1e7ff 100644 --- a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.cpp +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.cpp @@ -115,4 +115,25 @@ TEST_F(EtsInteropScenariosEtsToJs, DISABLED_test_default_value_define_for_parame ASSERT_EQ(true, RunJsTestSuite("js_suites/test_default_value_define_for_parameter_undefine.js")); } +// NOTE (splatov) #15925 enable this after interop is implemented in this direction +TEST_F(EtsInteropScenariosEtsToJs, DISABLED_test_function_arg_type_optional_primitive_explicit) +{ + ASSERT_EQ(true, RunJsTestSuite("js_suites/test_function_arg_type_optional_primitive_explicit.js")); +} + +TEST_F(EtsInteropScenariosEtsToJs, test_function_arg_type_optional_primitive_default) +{ + ASSERT_EQ(true, RunJsTestSuite("js_suites/test_function_arg_type_optional_primitive_default.js")); +} + +TEST_F(EtsInteropScenariosEtsToJs, test_function_arg_type_primitive) +{ + ASSERT_EQ(true, RunJsTestSuite("js_suites/test_function_arg_type_primitive.js")); +} + +TEST_F(EtsInteropScenariosEtsToJs, test_function_return_type_primitive) +{ + ASSERT_EQ(true, RunJsTestSuite("js_suites/test_function_return_type_primitive.js")); +} + } // namespace ark::ets::interop::js::testing diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.ets b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.ets index 31a7bce9fb..536195d18b 100644 --- a/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.ets +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/ets_to_js/scenarios.ets @@ -114,6 +114,18 @@ function default_undefined_parameter_function(arg: JSValue|undefined = undefined return arg; } +function function_arg_type_optional_primitive(arg?: Number): Number | undefined { + return arg; +} + +function function_arg_type_primitive(arg: number): number { + return arg; +} + +function function_return_type_primitive(): number { + return 1; +} + function GCJSRuntimeCleanup(): void { try { // trigger FinalizationRegistry cleanup diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.cpp b/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.cpp index 36cc0ef5de..42f640eea8 100644 --- a/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.cpp +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.cpp @@ -138,4 +138,28 @@ TEST_F(EtsInteropScenariosJsToEts, Test_generic_type_as_return_value) ASSERT_EQ(ret, true); } +TEST_F(EtsInteropScenariosJsToEts, Test_function_arg_type_optional_primitive_explicit) +{ + auto ret = CallEtsMethod("Test_function_arg_type_optional_primitive_explicit"); + ASSERT_EQ(ret, true); +} + +TEST_F(EtsInteropScenariosJsToEts, Test_function_arg_type_optional_primitive_default) +{ + auto ret = CallEtsMethod("Test_function_arg_type_optional_primitive_default"); + ASSERT_EQ(ret, true); +} + +TEST_F(EtsInteropScenariosJsToEts, Test_function_arg_type_primitive) +{ + auto ret = CallEtsMethod("Test_function_arg_type_primitive"); + ASSERT_EQ(ret, true); +} + +TEST_F(EtsInteropScenariosJsToEts, Test_function_return_type_primitive) +{ + auto ret = CallEtsMethod("Test_function_return_type_primitive"); + ASSERT_EQ(ret, true); +} + } // namespace ark::ets::interop::js::testing diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.ets b/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.ets index 03613c6b8a..570c202fd5 100644 --- a/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.ets +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.ets @@ -18,7 +18,8 @@ import { standaloneFunctionJs, ClassWithMethodJs, newInterfaceWithMethod, ClassW functionArgTypeTuple, functionReturnTypeAny, functionReturnTypeUnknown, functionReturnTypeUndefined, functionArgTypeCallable, functionDefaultParameterFunction, functionDefaultIntParameterFunction, functionDefaultStringParameterFunction, functionDefaultFloatParameterFunction, genericTypeParameter, - genericTypeReturnValue } from "/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.js" + genericTypeReturnValue, functionArgTypeOptionalPrimitive, functionArgTypePrimitive, + functionReturnTypePrimitive } from "/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.js" function Test_standalone_function_call(): boolean { return standaloneFunctionJs() as int == 1; @@ -123,3 +124,21 @@ function Test_generic_type_as_parameter(): boolean { function Test_generic_type_as_return_value(): boolean { return genericTypeReturnValue(1) as int == 1; } + +function Test_function_arg_type_optional_primitive_explicit(): boolean { + return functionArgTypeOptionalPrimitive(1) as int == 1; +} + +function Test_function_arg_type_optional_primitive_default(): boolean { + return functionArgTypeOptionalPrimitive() as int == 1; +} + +function Test_function_arg_type_primitive(): boolean { + let arg: int = 1; + return functionArgTypePrimitive(arg) as int == 1; +} + +function Test_function_return_type_primitive(): boolean { + let res: boolean = functionReturnTypePrimitive() as boolean; + return typeof res === "boolean"; +} diff --git a/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.js b/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.js index 2a1e1ccd45..70df5829b4 100644 --- a/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.js +++ b/static_core/plugins/ets/tests/interop_js/tests/scenarios/js_to_ets/scenarios.js @@ -152,6 +152,21 @@ function functionDefaultFloatParameterFunction(arg = FLOAT_VALUE) { // transpiled from Typescript code: function default_float_parameter_function(arg: JSValue = FLOAT_VALUE): JSValue{ } +function functionArgTypeOptionalPrimitive(arg) { + if (typeof arg !== 'undefined') { + return arg; + } + return INT_VALUE; +} + +function functionArgTypePrimitive(arg) { + return arg; +} + +function functionReturnTypePrimitive() { + return true; +} + exports.standaloneFunctionJs = standaloneFunctionJs; exports.ClassWithMethodJs = ClassWithMethodJs; exports.newInterfaceWithMethod = newInterfaceWithMethod; @@ -172,4 +187,7 @@ exports.functionArgTypeCallable = functionArgTypeCallable; exports.functionDefaultParameterFunction = functionDefaultParameterFunction; exports.functionDefaultIntParameterFunction = functionDefaultIntParameterFunction; exports.functionDefaultStringParameterFunction = functionDefaultStringParameterFunction; -exports.functionDefaultFloatParameterFunction = functionDefaultFloatParameterFunction; \ No newline at end of file +exports.functionDefaultFloatParameterFunction = functionDefaultFloatParameterFunction; +exports.functionArgTypeOptionalPrimitive = functionArgTypeOptionalPrimitive; +exports.functionArgTypePrimitive = functionArgTypePrimitive; +exports.functionReturnTypePrimitive = functionReturnTypePrimitive; -- Gitee