Added new global api to get the current moduleName and bundleName

Signed-off-by: chenlincl3 <chenlin201@huawei.com>
Change-Id: I09d6325b54714c1ff8440e7bfec988157474fc76
This commit is contained in:
chenlincl3 2024-05-27 20:46:06 +08:00
parent 27765ae398
commit 8cbaeb942f
6 changed files with 92 additions and 23 deletions

View File

@ -501,6 +501,8 @@ void Builtins::InitializeGlobalObject(const JSHandle<GlobalEnv> &env, const JSHa
SetFunction(env, globalObject, "unescape", Global::Unescape, FunctionLength::ONE);
SetFunction(env, globalObject, "decodeURIComponent", Global::DecodeURIComponent, FunctionLength::ONE);
SetFunction(env, globalObject, "encodeURIComponent", Global::EncodeURIComponent, FunctionLength::ONE);
SetFunction(env, globalObject, "__getCurrentModuleName__", Global::GetCurrentModuleName, FunctionLength::ZERO);
SetFunction(env, globalObject, "__getCurrentBundleName__", Global::GetCurrentBundleName, FunctionLength::ZERO);
// Global object property
SetGlobalThis(globalObject, "globalThis", JSHandle<JSTaggedValue>::Cast(globalObject));

View File

@ -23,9 +23,11 @@
#include "ecmascript/base/number_helper.h"
#include "ecmascript/base/string_helper.h"
#include "ecmascript/ecma_macros.h"
#include "ecmascript/interpreter/interpreter.h"
#include "ecmascript/js_function.h"
#include "ecmascript/mem/c_containers.h"
#include "ecmascript/module/js_module_deregister.h"
#include "ecmascript/module/module_path_helper.h"
#include "ecmascript/stubs/runtime_stubs.h"
#include "ecmascript/tagged_array-inl.h"
@ -908,4 +910,41 @@ JSTaggedValue BuiltinsGlobal::Unescape(EcmaRuntimeCallInfo *msg)
uint32_t retSize = r.size();
return factory->NewFromUtf16Literal(returnData, retSize).GetTaggedValue();
}
JSTaggedValue BuiltinsGlobal::GetCurrentModuleName(EcmaRuntimeCallInfo *msg)
{
ASSERT(msg);
JSThread *thread = msg->GetThread();
BUILTINS_API_TRACE(thread, Global, GetCurrentModuleName);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
std::pair<JSTaggedValue, JSTaggedValue> moduleInfo = EcmaInterpreter::GetCurrentEntryPoint(thread);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
EcmaVM *vm = thread->GetEcmaVM();
CString recordName = ConvertToString(moduleInfo.first);
CString moduleName;
if (vm->IsNormalizedOhmUrlPack()) {
moduleName = ModulePathHelper::GetModuleNameWithNormalizedName(recordName);
} else {
moduleName = ModulePathHelper::GetModuleName(recordName);
}
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<EcmaString> result = factory->NewFromUtf8(moduleName.c_str());
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsGlobal::GetCurrentBundleName(EcmaRuntimeCallInfo *msg)
{
ASSERT(msg);
JSThread *thread = msg->GetThread();
BUILTINS_API_TRACE(thread, Global, GetCurrentBundleName);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
std::pair<JSTaggedValue, JSTaggedValue> moduleInfo = EcmaInterpreter::GetCurrentEntryPoint(thread);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
EcmaVM *vm = thread->GetEcmaVM();
CString recordName = ConvertToString(moduleInfo.first);
CString bundleName = ModulePathHelper::GetBundleNameWithRecordName(vm, recordName);
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<EcmaString> result = factory->NewFromUtf8(bundleName.c_str());
return result.GetTaggedValue();
}
} // namespace panda::ecmascript::builtins

View File

@ -35,29 +35,30 @@
// The following global object properties are not listed here:
// - parseFloat ( string ), listed in builtins_number.h instead.
// - parseInt ( string ), listed in builtins_number.h instead.
#define BUILTIN_GLOBAL_FUNCTIONS_COMMON(V) \
/* decodeURI ( encodedURI ) */ \
V("decodeURI", DecodeURI, 1, INVALID) \
/* decodeURIComponent ( encodedURIComponent ) */ \
V("decodeURIComponent", DecodeURIComponent, 1, INVALID) \
/* encodeURI ( uri ) */ \
V("encodeURI", EncodeURI, 1, INVALID) \
/* encodeURIComponent ( uriComponent ) */ \
V("encodeURIComponent", EncodeURIComponent, 1, INVALID) \
/* escape ( string ), defined in B.2.1 */ \
V("escape", Escape, 1, INVALID) \
/* eval ( x ), which is NOT supported in ArkTS engine */ \
V("eval", NotSupportEval, 1, INVALID) \
/* isFinite ( number ) */ \
V("isFinite", IsFinite, 1, GlobalIsFinite) \
/* isNaN ( number ) */ \
V("isNaN", IsNaN, 1, GlobalIsNan) \
/* unescape ( string )*/ \
V("unescape", Unescape, 1, INVALID) \
/* The following are ArkTS extensions */ \
V("markModuleCollectable", MarkModuleCollectable, 0, INVALID) \
V("print", PrintEntrypoint, 0, INVALID)
#define BUILTIN_GLOBAL_FUNCTIONS_COMMON(V) \
/* decodeURI ( encodedURI ) */ \
V("decodeURI", DecodeURI, 1, INVALID) \
/* decodeURIComponent ( encodedURIComponent ) */ \
V("decodeURIComponent", DecodeURIComponent, 1, INVALID) \
/* encodeURI ( uri ) */ \
V("encodeURI", EncodeURI, 1, INVALID) \
/* encodeURIComponent ( uriComponent ) */ \
V("encodeURIComponent", EncodeURIComponent, 1, INVALID) \
/* escape ( string ), defined in B.2.1 */ \
V("escape", Escape, 1, INVALID) \
/* eval ( x ), which is NOT supported in ArkTS engine */ \
V("eval", NotSupportEval, 1, INVALID) \
/* isFinite ( number ) */ \
V("isFinite", IsFinite, 1, GlobalIsFinite) \
/* isNaN ( number ) */ \
V("isNaN", IsNaN, 1, GlobalIsNan) \
/* unescape ( string )*/ \
V("unescape", Unescape, 1, INVALID) \
/* The following are ArkTS extensions */ \
V("markModuleCollectable", MarkModuleCollectable, 0, INVALID) \
V("print", PrintEntrypoint, 0, INVALID) \
V("__getCurrentModuleName__", GetCurrentModuleName, 0, INVALID) \
V("__getCurrentBundleName__", GetCurrentBundleName, 0, INVALID)
#if ECMASCRIPT_ENABLE_RUNTIME_STAT
#define BUILTIN_GLOBAL_FUNCTIONS_RUNTIME_STAT(V) \
V("startRuntimeStat", StartRuntimeStat, 0, INVALID) \
@ -122,6 +123,9 @@ public:
static JSTaggedValue MarkModuleCollectable(EcmaRuntimeCallInfo *msg);
static JSTaggedValue CallJsBoundFunction(EcmaRuntimeCallInfo *msg);
static JSTaggedValue CallJsProxy(EcmaRuntimeCallInfo *msg);
static JSTaggedValue GetCurrentModuleName(EcmaRuntimeCallInfo *msg);
static JSTaggedValue GetCurrentBundleName(EcmaRuntimeCallInfo *msg);
#if ECMASCRIPT_ENABLE_RUNTIME_STAT
static JSTaggedValue StartRuntimeStat(EcmaRuntimeCallInfo *msg);
static JSTaggedValue StopRuntimeStat(EcmaRuntimeCallInfo *msg);

View File

@ -1013,4 +1013,25 @@ CVector<CString> ModulePathHelper::SplitNormalizedRecordName(const CString &reco
}
return res;
}
/*
* Before: 1. bundleName/moduleName/xxx/xxx
2. bunldeName&moduleName/xxx/xxx&
* After: bundleName
*/
CString ModulePathHelper::GetBundleNameWithRecordName(EcmaVM *vm, const CString &recordName)
{
CString bundleName;
if (vm->IsNormalizedOhmUrlPack()) {
CVector<CString> res = ModulePathHelper::SplitNormalizedRecordName(recordName);
bundleName = res[ModulePathHelper::NORMALIZED_BUNDLE_NAME_INDEX];
if (bundleName.size() == 0) {
bundleName = vm->GetBundleName();
}
} else {
size_t pos = recordName.find(PathHelper::SLASH_TAG);
bundleName = recordName.substr(0, pos);
}
return bundleName;
}
} // namespace panda::ecmascript

View File

@ -189,6 +189,7 @@ public:
static void ConcatOtherNormalizedOhmurl(EcmaVM *vm, const JSPandaFile *jsPandaFile,
[[maybe_unused]] CString &baseFileName, CString &requestPath);
static CString ConcatNormalizedOhmurlWithData(CVector<CString> &data, CString &pkgName, CString &entryPath);
static CString GetBundleNameWithRecordName(EcmaVM *vm, const CString &recordName);
static inline bool IsSandboxPath(const CString &moduleFileName)
{
return base::StringHelper::StringStartWith(moduleFileName, ModulePathHelper::BUNDLE_INSTALL_PATH);

View File

@ -492,6 +492,8 @@ namespace panda::ecmascript {
V(Global, EncodeURIComponent) \
V(Global, Escape) \
V(Global, Unescape) \
V(Global, GetCurrentModuleName) \
V(Global, GetCurrentBundleName) \
V(Intl, GetCanonicalLocales) \
V(Iterator, Constructor) \
V(Iterator, Next) \