From c0a53f4e5321cea14a4edd163ead35d9df0da5e5 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 --- .../shopping/arktsconfig-run-unmemoized.json | 3 + arkoala-arkts/shopping/package.json | 3 +- .../src/ets/arkcompilerPOC/concurrency.ets | 10 ++ .../src/ets/arkcompilerPOC/concurrency.sts | 121 ++++++++++++++++++ .../src/ets/pages/homePage/informance.ets | 41 +++++- interop/src/cpp/vmloader.cc | 20 ++- 6 files changed, 190 insertions(+), 8 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/arktsconfig-run-unmemoized.json b/arkoala-arkts/shopping/arktsconfig-run-unmemoized.json index 0ff90ba8a..4136addb2 100644 --- a/arkoala-arkts/shopping/arktsconfig-run-unmemoized.json +++ b/arkoala-arkts/shopping/arktsconfig-run-unmemoized.json @@ -36,6 +36,9 @@ "#arkcompat": [ "../../arkoala/arkui-common/build/unmemoized/src/arkts" ] + }, + "dynamicPaths": { + "@ohos.hilog": {"language" : "js", "hasDecl" : false} } } } diff --git a/arkoala-arkts/shopping/package.json b/arkoala-arkts/shopping/package.json index c007e709c..ab51cabfe 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 unmemoize:all && npm run build:shopping:inc", + "build:shopping": "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..200d9cf3e --- /dev/null +++ b/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.ets @@ -0,0 +1,10 @@ +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..35f332c1d --- /dev/null +++ b/arkoala-arkts/shopping/src/ets/arkcompilerPOC/concurrency.sts @@ -0,0 +1,121 @@ +import { NativeModule } from '#components' +import { info } from '@ohos.hilog' + +const LOG_TAG = "Shopping_2.0_Example " + +function GetPandaString(): String { + return "Panda"; +} + +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() { + info(0x0000, "shopping2.0Sample", "callFetchWithCoroutine log from hilog"); + 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() { + info(0x0000, "shopping2.0Sample", "callFetchWithTaskPool log from hilog"); + 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() { + info(0x0000, "shopping2.0Sample", "callFetchWithEACoroutine log from hilog"); + 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..eb44797d5 100644 --- a/arkoala-arkts/shopping/src/ets/pages/homePage/informance.ets +++ b/arkoala-arkts/shopping/src/ets/pages/homePage/informance.ets @@ -15,16 +15,49 @@ 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 state: number = 1 @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(); + }) + Image("/resources/glass.png").width(400) + Text("Set Button onClick! #" + this.state) + } + .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..abbf9c256 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -13,6 +13,8 @@ * limitations under the License. */ +#include +#include #include #include #include @@ -135,6 +137,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) @@ -174,12 +177,11 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP #elif defined(KOALA_OHOS_ARM32) libName(thisVM->lib) #elif defined (KOALA_OHOS_ARM64) - "/system/lib64/" + libName(thisVM->lib) + "/data/storage/el1/bundle/libs/arm64/" + libName(thisVM->lib) #else #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 +189,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 +198,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 +241,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);//JNI_VERSION_1_8 + env = static_cast(pEnv); + } else { + result = createVM(&vm, &env, &pandaVMArgs); + } + } #endif -- Gitee