From 1486f4412dc812edf798a679f61d7de0ec7e2bbe Mon Sep 17 00:00:00 2001 From: zhuangkudecha Date: Tue, 24 Dec 2024 11:39:19 +0800 Subject: [PATCH] Add Arkcompile POC demos to shopping Signed-off-by: zhuangkudecha --- arkoala-arkts/shopping/package.json | 3 +- .../src/ets/arkcompilerPOC/concurrency.ets | 24 ++++ .../src/ets/arkcompilerPOC/concurrency.sts | 127 ++++++++++++++++++ .../src/ets/pages/homePage/informance.ets | 38 +++++- interop/src/cpp/vmloader.cc | 16 ++- 5 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.ets create mode 100644 arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.sts diff --git a/arkoala-arkts/shopping/package.json b/arkoala-arkts/shopping/package.json index 518ddaa40..8dfd086f0 100644 --- a/arkoala-arkts/shopping/package.json +++ b/arkoala-arkts/shopping/package.json @@ -11,7 +11,8 @@ "unmemoize:arkui-no-common": "npm run unmemoize --prefix ../arkui", "unmemoize:arkui-common": "npm run unmemoize --prefix ../../arkoala/arkui-common", "unmemoize:all": "npm run unmemoize:runtime && npm run unmemoize:arkui-no-common && npm run unmemoize:arkui-common && npm run unmemoize", - "build:shopping": "npm run interface-sdk:download --prefix ../../arkoala/arkui-common && npm run unmemoize:all && npm run build:shopping:inc", + "build:shopping": "npm run interface-sdk:download --prefix ../../arkoala/arkui-common && npm run unmemoize:all && npm run build:shopping:prepare-non-subset && npm run build:shopping:inc", + "build:shopping:prepare-non-subset": "rm ./build/unmemoized/build/generated/arkcompilerPOC/concurrency.ts && cp ./src/ets/arkcompilerPOC/concurrency.sts ./build/unmemoized/build/generated/arkcompilerPOC/concurrency.ts", "build:shopping:inc": "fast-arktsc --input-files ./arktsconfig-run-unmemoized.json --output-dir ./build --compiler ../../incremental/tools/panda/arkts/arktsc --link-name shopping && ninja -f build/build.ninja", "pack": "npm run cli-tools:check && cd app && DEVECO_SDK_HOME=../../../arkoala/ohos-sdk/ohos-sdk ../command-line-tools/hvigor/bin/hvigorw --no-daemon --mode module -p product=default -p module=shopping@default assembleHar", diff --git a/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.ets b/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.ets new file mode 100644 index 000000000..98ac489fe --- /dev/null +++ b/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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. + */ +export function callFetchWithCoroutine() { + +} + +export function callFetchWithTaskPool() { + +} + +export function callFetchWithEACoroutine() { +} \ No newline at end of file diff --git a/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.sts b/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.sts new file mode 100644 index 000000000..5f797ad2a --- /dev/null +++ b/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.sts @@ -0,0 +1,127 @@ +/* + * Copyright (c) 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. + */ +import { NativeModule } from '#components' + +const LOG_TAG = "Shopping_2.0_Example " + +export class InformationModel { + public uri: string + public title: string + public notice: string + public time: string + constructor(uri: string, title: string, notice: string, time: string) { + this.uri = uri; + this.title = title; + this.notice = notice; + this.time = time; + } +} + +function blockingSleep(ms: number): void { + const start = Date.now(); + while (Date.now() - start < ms) { + } +} + +function getRandomString(length: number): string { + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let result = ""; + for (let i = 0; i < length; i++) { + result += chars.charAt(Math.floor(Math.random() * chars.length)); + } + return result; +} + +function getRandomTime(): string { + const now = new Date(); + return now.toUTCString(); +} + + +function simulateFetchAndParse(length: number): Array { + blockingSleep(1000); + + let res: Array = new Array(); + for (let i = 0; i < length; i++) { + res.push(new InformationModel( + `https://randomsite${i}.com/${getRandomString(10)}`, + `Title ${getRandomString(5)}`, + `Notice ${getRandomString(15)}`, + getRandomTime() + )); + } + return res; +} + +export function fetch(): Array { + return simulateFetchAndParse(5); +} + + +export async function asyncFetch() : Promise> { + NativeModule._NativeLog(LOG_TAG + "asyncFetch start"); + return simulateFetchAndParse(5); +} + +export function callAsyncFetch() { + NativeModule._NativeLog(LOG_TAG + "call asyncFetch"); + let p = asyncFetch(); + NativeModule._NativeLog(LOG_TAG + "before await res from asyncFetch"); + let first : Array = await p; + NativeModule._NativeLog(LOG_TAG + "asyncFetch done: " + first[0].title); +} + +function coroutineFetchData() { + NativeModule._NativeLog(LOG_TAG + "main thread request data from coroutine"); + + let resCoroutine = launch fetch(); + let first : Array = await resCoroutine; + NativeModule._NativeLog(LOG_TAG + "Coroutine fetch data done: " + first[0].title); +} + +export function callFetchWithCoroutine() { + launch coroutineFetchData(); +} + +function fetchWithTaskPool() : Array { + NativeModule._NativeLog(LOG_TAG + `taskpool fetch data`) + return fetch(); +} + +function taskPoolFetchData(){ + NativeModule._NativeLog(LOG_TAG + "main thread request data from taskpool") + let resTaskPool = taskpool.execute(fetchWithTaskPool); + let obj = (await resTaskPool) as Array; + NativeModule._NativeLog(LOG_TAG + "TaskPool fetch data done: " + obj[0].title); +} + +export function callFetchWithTaskPool() { + launch taskPoolFetchData(); +} + +function eaTask() { + NativeModule._NativeLog(LOG_TAG + "EACoroutine start to fetch data"); + NativeModule._NativeLog(LOG_TAG + "EACoroutine fetch data end"); +} + +function EACoroutineFetchData() { + NativeModule._NativeLog(LOG_TAG + "request data from EACoroutine"); + // todo need add EACoroutine Demo when code merged to yz. + NativeModule._NativeLog(LOG_TAG + "Post eaTask done"); +} + +export function callFetchWithEACoroutine() { + launch EACoroutineFetchData(); +} \ No newline at end of file diff --git a/arkoala-arkts/shopping/src/ets/pages/homePage/informance.ets b/arkoala-arkts/shopping/src/ets/pages/homePage/informance.ets index 0bf3348dc..d564f974e 100644 --- a/arkoala-arkts/shopping/src/ets/pages/homePage/informance.ets +++ b/arkoala-arkts/shopping/src/ets/pages/homePage/informance.ets @@ -15,16 +15,46 @@ import { InformationModel } from '../../model/homeModel' import { informationData } from '../../data/homeData' +import { callFetchWithEACoroutine, callFetchWithCoroutine, callFetchWithTaskPool} from '../../arkcompilerPOC/concurrency' @Component export struct Information { + @State color1: string = '#ff0000' + @State color2: string = '#00ff00' @State information: Array = informationData @Prop ratio: number build() { - Column() { - Text("Hello world") - } - .width('100%') + Column() { + Button("CallFetchWithCoroutine") + .backgroundColor(this.color1) + .width(200).height(100) + .onClick((e?: ClickEvent) => { + this.swap(); + callFetchWithCoroutine(); + }) + Button("CallFetchWithTaskPool") + .backgroundColor(this.color2) + .width(200).height(100) + .onClick((e?: ClickEvent) => { + this.swap() + callFetchWithTaskPool(); + }) + Button("CallFetchWithEACoroutine") + .width(200).height(100) + .onClick((e?: ClickEvent) => { + callFetchWithEACoroutine(); + }) + } + .width('100%').height('100%') + .backgroundColor(Color.Gray) + .justifyContent(FlexAlign.Center) + } + + swap(): void { + console.log("#### Swap") + let tmp = this.color1 + this.color1 = this.color2 + this.color2 = tmp } } \ No newline at end of file diff --git a/interop/src/cpp/vmloader.cc b/interop/src/cpp/vmloader.cc index 148f08e85..cff2ae4b7 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -135,6 +135,7 @@ struct VMEntry { VMEntry g_vmEntry; typedef int (*createVM_t)(void** pVM, void** pEnv, void* vmInitArgs); +typedef int (*getVMs_t)(void** pVM, int32_t bufLen, int32_t* nVMs); #ifdef KOALA_WINDOWS #define DLL_EXPORT __declspec(dllexport) @@ -179,7 +180,6 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP #error "Library path not specified for this platform" #endif ; - void *handle = loadLibrary(libPath); if (!handle) { LOGE("Cannot load library %" LOG_PUBLIC "s: %" LOG_PUBLIC "s\n", libPath.c_str(), libraryError()); @@ -187,6 +187,7 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP } createVM_t createVM = (createVM_t)findSymbol(handle, thisVM->createVM); + getVMs_t getVMs = (getVMs_t)findSymbol(handle, "ETS_GetCreatedVMs"); if (!createVM) { LOGE("Cannot find %" LOG_PUBLIC "s\n", thisVM->createVM); @@ -195,6 +196,7 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP void* vm = nullptr; void* env = nullptr; + int32_t nVMs = 0; int result = 0; #ifdef KOALA_JNI @@ -237,7 +239,17 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP pandaVMArgs.nOptions = etsVMOptions.size(); pandaVMArgs.options = etsVMOptions.data(); g_vmEntry.vmKind = PANDA_VM_KIND; - result = createVM(&vm, &env, &pandaVMArgs); + + result = getVMs(&vm, 1, &nVMs); + if (nVMs != 0) { + __EtsVM* vmInstance = (__EtsVM*)vm; + EtsEnv* pEnv = nullptr; + vmInstance->GetEnv(&pEnv, ETS_NAPI_VERSION_1_0); + env = static_cast(pEnv); + } else { + result = createVM(&vm, &env, &pandaVMArgs); + } + } #endif -- Gitee