Skip to content

Commit

Permalink
feat(ohos): add jsi for jsvm
Browse files Browse the repository at this point in the history
  • Loading branch information
zealotchen0 committed Dec 10, 2024
1 parent 67447eb commit 2ee0add
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 106 deletions.
1 change: 1 addition & 0 deletions driver/js/include/driver/napi/jsh/jsh_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ constexpr static int kJSHExternalIndex = 0;
constexpr static int kJSHScopeWrapperIndex = 1;
constexpr static int kJSHWeakCallbackWrapperInvalidIndex = 2;
constexpr static int kJSHExternalDataNum = 3;
constexpr static int KJSHTurboFunctionGetIndex = 4;

extern void* GetPointerInInstanceData(JSVM_Env env, int index);

Expand Down
90 changes: 86 additions & 4 deletions driver/js/src/napi/jsh/jsh_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using JSHVM = hippy::vm::JSHVM;
using CallbackInfo = hippy::CallbackInfo;

void* GetPointerInInstanceData(JSVM_Env env, int index) {
if (index < 0 || index >= kJSHExternalDataNum) {
if (index < 0 || index > KJSHTurboFunctionGetIndex) {
return nullptr;
}

Expand Down Expand Up @@ -143,6 +143,8 @@ JSVM_Value InvokeJsCallbackOnConstruct(JSVM_Env env, JSVM_CallbackInfo info) {
cb_info.AddValue(std::make_shared<JSHCtxValue>(env, argv[i]));
}

void *wrap_result = nullptr;
status = OH_JSVM_RemoveWrap(env, thisArg, &wrap_result);
status = OH_JSVM_Wrap(env, thisArg, cb_info.GetData(), nullptr, nullptr, nullptr);
FOOTSTONE_DCHECK(status == JSVM_OK);

Expand Down Expand Up @@ -213,7 +215,7 @@ std::shared_ptr<ClassDefinition> JSHCtx::GetClassDefinition(const string_view& n
}

void JSHCtx::SetPointerInInstanceData(int index, void* address) {
if (index < 0 || index >= kJSHExternalDataNum) {
if (index < 0 || index > KJSHTurboFunctionGetIndex) {
return;
}

Expand Down Expand Up @@ -1241,9 +1243,89 @@ void* JSHCtx::GetObjectExternalData(const std::shared_ptr<CtxValue>& object) {
return nullptr;
}

static JSVM_Value GetPropertyCbInfo(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data) {
char strValue[100];
size_t size;
OH_JSVM_GetValueStringUtf8(env, name, strValue, 300, &size);
CallbackInfo cb_info;

void *scope_data = GetPointerInInstanceData(env, kJSHScopeWrapperIndex);
cb_info.SetSlot(scope_data);

void *internal_data = nullptr;
auto status = OH_JSVM_Unwrap(env, thisArg, &internal_data);
if (status == JSVM_OK) {
if (internal_data) {
cb_info.SetData(internal_data);
}
}

cb_info.SetReceiver(std::make_shared<JSHCtxValue>(env, thisArg));
auto function_name = std::make_shared<JSHCtxValue>(env, name);
cb_info.AddValue(function_name);

void* turbo_function_get = GetPointerInInstanceData(env, KJSHTurboFunctionGetIndex);
auto function_wrapper = reinterpret_cast<FunctionWrapper*>(turbo_function_get);
FOOTSTONE_CHECK(function_wrapper);
auto js_cb = function_wrapper->callback;
auto external_data = function_wrapper->data;

js_cb(cb_info, external_data);
auto exception = std::static_pointer_cast<JSHCtxValue>(cb_info.GetExceptionValue()->Get());
if (exception) {
OH_JSVM_Throw(env, exception->GetValue());
JSVM_Value result = nullptr;
OH_JSVM_GetUndefined(env, &result);
return result;
}

auto ret_value = std::static_pointer_cast<JSHCtxValue>(cb_info.GetReturnValue()->Get());
if (!ret_value) {
JSVM_Value result = nullptr;
OH_JSVM_GetUndefined(env, &result);
return result;
}

return ret_value->GetValue();
}

std::shared_ptr<CtxValue> JSHCtx::DefineProxy(const std::unique_ptr<FunctionWrapper>& constructor_wrapper) {
// JSVM impl
return CreateFunction(constructor_wrapper);
JSHHandleScope handleScope(env_);
JSVM_Value func_tpl;

callback_structs_.push_back(new JSVM_CallbackStruct());
JSVM_CallbackStruct* constructorParam = callback_structs_.back();
constructorParam->data = constructor_wrapper.get();
constructorParam->callback = InvokeJsCallbackOnConstruct;

callback_structs_.push_back(new JSVM_CallbackStruct());
JSVM_CallbackStruct* callbackParam = callback_structs_.back();
callbackParam->data = constructor_wrapper.get();
callbackParam->callback = InvokeJsCallback;

SetPointerInInstanceData(KJSHTurboFunctionGetIndex, constructor_wrapper.get());

JSVM_Value res = nullptr;
OH_JSVM_CreateBigintWords(env_, 1, 2, reinterpret_cast<const uint64_t *>(constructor_wrapper.get()), &res);
JSVM_PropertyHandlerConfigurationStruct* propertyHandlerCfg = new JSVM_PropertyHandlerConfigurationStruct();
propertyHandlerCfg->genericNamedPropertyGetterCallback = GetPropertyCbInfo;
propertyHandlerCfg->namedPropertyData = res;
propertyHandlerCfg->indexedPropertyData = res;
JSVM_PropertyHandlerCfg propertyHandlerConfig = propertyHandlerCfg;

JSVM_Status status = OH_JSVM_DefineClassWithPropertyHandler(
env_,
"ProxyWrap",
JSVM_AUTO_LENGTH,
constructorParam,
0,
nullptr,
propertyHandlerConfig,
callbackParam,
&func_tpl);
FOOTSTONE_DCHECK(status == JSVM_OK);

return std::make_shared<JSHCtxValue>(env_, func_tpl);
}

std::shared_ptr<CtxValue> JSHCtx::DefineClass(const string_view& name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
* limitations under the License.
*/
import { HippyEngineContext } from 'hippy';
import { HippyModulePromise } from 'hippy/src/main/ets/hippy_framework/modules/HippyModulePromise';
import { HippyNativeModuleBase } from 'hippy/src/main/ets/hippy_framework/modules/native';
import { HippyNativeModule } from 'hippy/src/main/ets/support/annotation/HippyNativeModule';
import { HippyAny } from 'hippy/src/main/ets/support/common/HippyTypes';
import { LogUtils } from 'hippy/src/main/ets/support/utils/LogUtils';
import { HippyAny, HippyArray, HippyMap } from 'hippy/src/main/ets/support/common/HippyTypes';
import { TurboConfig } from './turbo/TurboConfig';


@HippyNativeModule({ name: "demoTurbo" })
Expand All @@ -37,19 +36,35 @@ export class ExampleNativeTurboModule extends HippyNativeModuleBase {
return true
}

public call(method: string, params: Array<HippyAny>, promise: HippyModulePromise): HippyAny {
switch (method) {
case 'getString': {
this.getString(params[0] as string);
break;
}
default:
super.call(method, params, promise);
}
return null;
}

public getString(info: string): string {
return 'demoTurbo' + info;
}

public getNum(num: number): number {
return num;
}

public getBoolean(b: boolean): boolean {
return b;
}

public getMap(map: HippyMap): HippyMap {
return map
}

public getArray(array: HippyArray): HippyArray {
return array
}

public getObject(obj: HippyAny): HippyAny {
return obj
}

public getTurboConfig(): TurboConfig {
return new TurboConfig();
}

public printTurboConfig(turboConfig: TurboConfig): string {
return turboConfig.info;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2022 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* 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 class TurboConfig {
public info = "info from turboConfig"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include <string>

//#include "connector/convert_utils.h"
#include "connector/turbo.h"
#include "driver/napi/js_ctx.h"

Expand All @@ -38,7 +37,7 @@ class ArkTsTurboModule {
using CtxValue = hippy::napi::CtxValue;
using FunctionWrapper = hippy::napi::FunctionWrapper;
using PropertyDescriptor = hippy::napi::PropertyDescriptor;
//

struct TurboWrapper {
ArkTsTurboModule* module;
std::shared_ptr<CtxValue> name;
Expand All @@ -57,15 +56,14 @@ class ArkTsTurboModule {

ArkTsTurboModule(const std::string& name,
std::shared_ptr<Turbo>& impl,
const std::shared_ptr<Ctx>& ctx);
const std::shared_ptr<Ctx>& ctx,
napi_env env);

std::string name;
std::shared_ptr<Turbo> impl;
napi_env env;
std::unique_ptr<hippy::napi::FunctionWrapper> wrapper_holder_;
//
// // methodName, signature
// std::unordered_map<std::string, MethodInfo> method_map_;
//

std::shared_ptr<CtxValue> constructor;
std::unique_ptr<FunctionWrapper> constructor_wrapper;
std::unordered_map<std::shared_ptr<CtxValue>, std::unique_ptr<TurboWrapper>> turbo_wrapper_map;
Expand All @@ -77,9 +75,6 @@ class ArkTsTurboModule {
hippy::napi::CallbackInfo& info,
void* data);

// static void Init(JNIEnv* j_env);
//
// static void Destroy(JNIEnv* j_env);
};

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Created on 2024/7/10.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* 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.
*
*/

#pragma once

#include "driver/napi/js_ctx_value.h"
#include "driver/napi/js_ctx.h"
#include "oh_napi/ark_ts.h"

using CtxValue = hippy::napi::CtxValue;
using Ctx = hippy::napi::Ctx;

namespace hippy {
inline namespace framework {
inline namespace turbo {

class TurboUtils {
public:
static std::shared_ptr<CtxValue> NapiValue2CtxValue(
napi_env env,
napi_value value,
const std::shared_ptr<Ctx>& ctx);
static napi_value CtxValue2NapiValue(
napi_env env,
const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue>& value);
};

}
}
}
Loading

0 comments on commit 2ee0add

Please sign in to comment.