Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

合入Dev #2

Merged
merged 70 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
ee406de
框架
chexiongsheng Jan 24, 2025
bbe2ce5
简化代码
chexiongsheng Jan 24, 2025
ef792d6
改为用bigint来支持64位
chexiongsheng Jan 24, 2025
b89e613
把pesapi_get_value_bool改为用pesapi_get_value_generic实现
chexiongsheng Feb 7, 2025
87e67b3
实现pesapi_get_value_string_utf8、pesapi_get_value_binary、pesapi_get_arr…
chexiongsheng Feb 7, 2025
c521056
window报错
chexiongsheng Feb 18, 2025
1c0dc1b
添加框架性代码
chexiongsheng Feb 18, 2025
23e65bf
添加gtest
chexiongsheng Feb 18, 2025
00103c0
mac下的错误
chexiongsheng Feb 18, 2025
9302949
只有window、linux、mac才编译测试
chexiongsheng Feb 18, 2025
64641b9
不手动加test
chexiongsheng Feb 18, 2025
915a94f
添加CppObjectMapper.cpp到编译
chexiongsheng Feb 18, 2025
63ba54b
调整一些头文件的目录
chexiongsheng Feb 18, 2025
73eb78e
提交类注册器
chexiongsheng Feb 18, 2025
a6991d6
移动目录
chexiongsheng Feb 18, 2025
24de916
register改为用eastl
chexiongsheng Feb 19, 2025
45ac7a5
移除register对libstdc++/libc++的依赖
chexiongsheng Feb 19, 2025
7bf3a60
去掉主模块对libstdc++/libc++的依赖
chexiongsheng Feb 19, 2025
d0de5aa
重构
chexiongsheng Feb 19, 2025
340ebf3
去掉析构导致的libstdc++的引用
chexiongsheng Feb 19, 2025
e3857e4
禁用异常后,析构函数调用不会导致libc++引用,所以可以改回placement new方式
chexiongsheng Feb 20, 2025
43005cf
添加GetEnvLifeCycleTracker,并添加env创建和销毁用例
chexiongsheng Feb 20, 2025
0d365b3
调整测试名称
chexiongsheng Feb 20, 2025
8ba1b37
虚析构会导致对_ZdlPvm@CXXABI_1.3.9符号的引用,进而依赖libc++,看上去子类都没覆盖该析构函数,先去掉
chexiongsheng Feb 20, 2025
0e61358
添加注册api的实现
chexiongsheng Feb 20, 2025
adb9b73
添加注册api的测试
chexiongsheng Feb 20, 2025
f2a5a31
解决linux找不到符号的问题
chexiongsheng Feb 20, 2025
0c6ab1b
g_pesapi_ffi声明移动到头文件,便于测试
chexiongsheng Feb 21, 2025
e61b0b4
eval,scope的实现和基础测试
chexiongsheng Feb 21, 2025
dfbef9d
内存分配大小错误
chexiongsheng Feb 21, 2025
20bc7e6
执行测试用例
chexiongsheng Feb 21, 2025
8e2243b
mac的测试用例路径
chexiongsheng Feb 21, 2025
5445f38
eval异常测试
chexiongsheng Feb 21, 2025
092ae5d
pesapi_get_exception_as_string及其测试,不过with_stack参数由于用得qjs版本不支持JS_ATOM_…
chexiongsheng Feb 21, 2025
408b0ea
支持错误栈信息
chexiongsheng Feb 21, 2025
1ce463b
一些api的实现
chexiongsheng Feb 21, 2025
edd7d35
编译错误
chexiongsheng Feb 21, 2025
eef406a
create_function的实现和测试
chexiongsheng Feb 21, 2025
12f7a8b
window系统调试版本的字符串同样的字面值,其指针值可能不一样
chexiongsheng Feb 24, 2025
89260ef
脚本里释放create_function创建的函数崩溃的问题
chexiongsheng Feb 24, 2025
432118b
用debug版本测试
chexiongsheng Feb 24, 2025
7c16899
ut程序的路径
chexiongsheng Feb 24, 2025
496b72f
释放虚拟机
chexiongsheng Feb 24, 2025
67558a2
Assertion failed: list_empty(&rt->gc_obj_list)
chexiongsheng Feb 24, 2025
cc14d55
加上create_function的finalize测试
chexiongsheng Feb 24, 2025
84174c5
属性设置和获取测试
chexiongsheng Feb 24, 2025
4bb220a
原生对象部分代码
chexiongsheng Feb 24, 2025
2a14242
loadClass测试用例
chexiongsheng Feb 24, 2025
0efbe50
处理好对应函数的引用计数类型
chexiongsheng Feb 24, 2025
7d4e735
增加注释
chexiongsheng Feb 24, 2025
730e8e4
调通构造和析构
chexiongsheng Feb 24, 2025
6bca519
去掉多余的runtime变量
chexiongsheng Feb 24, 2025
9ee2cd6
设置函数名字
chexiongsheng Feb 25, 2025
5dc7218
用例加上检查
chexiongsheng Feb 25, 2025
036c89b
优化测试,对类只注册一次,多个用例共享
chexiongsheng Feb 25, 2025
7e458bf
重构用例
chexiongsheng Feb 25, 2025
fa14d4c
静态函数
chexiongsheng Feb 25, 2025
a9740ce
添加DUMP_LEAKS, DUMP_ATOM_LEAKS
chexiongsheng Feb 25, 2025
d9318bf
pesapi_get_native_object_ptr的实现,成员方法的支持
chexiongsheng Feb 25, 2025
21956ba
成员方法测试,以及把scope放在Setup和TearDown,避免用例断言导致close_scope未调用,进而引发list_empty…
chexiongsheng Feb 25, 2025
e816754
支持Data
chexiongsheng Feb 25, 2025
9974ebe
属性
chexiongsheng Feb 25, 2025
fff141b
静态变量的测试
chexiongsheng Feb 25, 2025
7f5a4fc
native_object_to_value实现以及对象返回测试
chexiongsheng Feb 25, 2025
82eb977
pesapi_get_native_object_typeid
chexiongsheng Feb 25, 2025
df5415d
pesapi_is_instance_of
chexiongsheng Feb 25, 2025
f7f80c3
fix Atom leaks
chexiongsheng Feb 25, 2025
bc632b5
先不用这个api了
chexiongsheng Feb 25, 2025
017f3dc
pesapi_get_private、pesapi_set_private
chexiongsheng Feb 25, 2025
8ecdf67
装箱相关api
chexiongsheng Feb 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ jobs:
run: |
cd $GITHUB_WORKSPACE
chmod +x make_osx.sh
sh ./make_osx.sh
sh ./make_osx.sh Debug
./build/Debug/papi_qjs_test

build_ios:
name: iOS
Expand Down Expand Up @@ -84,7 +85,8 @@ jobs:
run: |
cd $GITHUB_WORKSPACE
chmod +x make_linux64.sh
./make_linux64.sh
./make_linux64.sh Debug
./build_linux64/papi_qjs_test

build_wasm:
name: wasm
Expand Down Expand Up @@ -113,6 +115,7 @@ jobs:
uses: microsoft/[email protected]
- name: Build
run: |
.\make_win64.bat
.\make_win64.bat Debug
.\build\Debug\papi_qjs_test.exe


116 changes: 109 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ macro(xcheck_add_c_compiler_flag FLAG)
endif()
endmacro()

xcheck_add_c_compiler_flag(-Wall)
if(NOT MSVC AND NOT IOS)
xcheck_add_c_compiler_flag(-Werror)
xcheck_add_c_compiler_flag(-Wextra)
endif()
#xcheck_add_c_compiler_flag(-Wall)
#if(NOT MSVC AND NOT IOS)
# xcheck_add_c_compiler_flag(-Werror)
# xcheck_add_c_compiler_flag(-Wextra)
#endif()
xcheck_add_c_compiler_flag(-Wno-implicit-fallthrough)
xcheck_add_c_compiler_flag(-Wno-sign-compare)
xcheck_add_c_compiler_flag(-Wno-missing-field-initializers)
Expand Down Expand Up @@ -114,14 +114,17 @@ set(qjs_sources

set(papi_sources
source/PapiQuickjsImpl.cpp
source/CppObjectMapper.cpp
)

add_definitions(-D_CHAR16T)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
add_definitions(-DEASTL_OPENSOURCE=1)

include_directories(include quickjs)
include_directories(EASTL/include)
include_directories(papi-register/include)

file(GLOB eastl_sources "EASTL/source/*.cpp")
file(GLOB_RECURSE eastl_headers "EASTL/include/EASTL/**.h")
Expand All @@ -139,21 +142,120 @@ if(NOT MSVC)
list(APPEND qjs_libs m)
endif()

set(papi_register_sources
papi-register/source/JSClassRegister.cpp
papi-register/source/RapiImpl.cpp
)

if ( APPLE AND IOS )
add_library(PapiQjs STATIC ${qjs_sources} ${eastl_sources} ${papi_sources})
add_library(PapiRegister STATIC ${papi_register_sources})
else ()
# 在Windows上需要显式生成导入库
add_library(PapiQjs SHARED ${qjs_sources} ${eastl_sources} ${papi_sources}) # if add eastl_headers will undefined symbol: __udivti3 __udivmodti4
# 设置导出符号
#set_target_properties(PapiQjs PROPERTIES
# WINDOWS_EXPORT_ALL_SYMBOLS TRUE
# CXX_VISIBILITY_PRESET hidden
# VISIBILITY_INLINES_HIDDEN TRUE
#)
# 确保生成导入库路径正确
#set_target_properties(PapiQjs PROPERTIES
# ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
# LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
#)
add_library(PapiRegister SHARED ${eastl_sources} ${papi_register_sources})
#set_target_properties(PapiRegister PROPERTIES
# WINDOWS_EXPORT_ALL_SYMBOLS TRUE
# CXX_VISIBILITY_PRESET hidden
# VISIBILITY_INLINES_HIDDEN TRUE
#)
endif()
target_compile_definitions(PapiQjs PRIVATE ${qjs_defines})
target_include_directories(PapiQjs PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(PapiQjs PUBLIC ${qjs_libs})
target_link_libraries(PapiQjs PUBLIC ${qjs_libs} PapiRegister)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib")

target_compile_definitions(PapiQjs PUBLIC EA_DEPRECATIONS_FOR_2024_APRIL=EA_DISABLED)
target_compile_definitions(PapiQjs PUBLIC EA_DEPRECATIONS_FOR_2024_SEPT=EA_DISABLED)
target_compile_definitions(PapiQjs PUBLIC EA_DEPRECATIONS_FOR_2025_APRIL=EA_DISABLED)
target_compile_definitions(PapiQjs PUBLIC EASTL_DLL)
#target_compile_definitions(PapiQjs PUBLIC EASTL_DLL)
target_compile_definitions(PapiQjs PUBLIC USING_REGISTER_API_SHARED)
target_compile_options(PapiQjs PRIVATE
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:
-fno-exceptions
-fno-unwind-tables
-fno-asynchronous-unwind-tables
-fno-rtti
>
$<$<CXX_COMPILER_ID:MSVC>:
/EHa-
/EHsc
/EHsc-
/GR- # 禁用RTTI
>
)
#if(MSVC)
#target_compile_options(PapiQjs PRIVATE /EHsc)
#endif()

target_compile_definitions(PapiRegister PUBLIC BUILDING_REGISTER_API_SHARED)
target_compile_options(PapiRegister PRIVATE
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:
-fno-exceptions
-fno-unwind-tables
-fno-asynchronous-unwind-tables
-fno-rtti
>
$<$<CXX_COMPILER_ID:MSVC>:
/EHa-
/EHsc-
/GR- # 禁用RTTI
>
)

if ( APPLE AND NOT IOS OR MSVC OR LINUX )

# 配置GTest
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()
include(GoogleTest)


add_executable(papi_qjs_test
test/papi_qjs_base_test.cpp
)

target_link_libraries(papi_qjs_test
PRIVATE
PapiQjs
PapiRegister
gtest
gtest_main
)

target_include_directories(papi_qjs_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/source
${CMAKE_CURRENT_SOURCE_DIR}/quickjs
${googletest_SOURCE_DIR}/googletest/include
)

gtest_discover_tests(papi_qjs_test)

endif()
2 changes: 1 addition & 1 deletion EASTL/include/EASTL/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace eastl

public:
ref_count_sp(int32_t refCount = 1, int32_t weakRefCount = 1) EA_NOEXCEPT;
virtual ~ref_count_sp() EA_NOEXCEPT {}
//virtual ~ref_count_sp() EA_NOEXCEPT {}

int32_t use_count() const EA_NOEXCEPT;
void addref() EA_NOEXCEPT;
Expand Down
148 changes: 148 additions & 0 deletions include/CppObjectMapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#pragma once

#include "pesapi.h"
#include "quickjs.h"
#include <EASTL/unordered_map.h>
#include <EASTL/allocator_malloc.h>
#include <EASTL/shared_ptr.h>
#include "ObjectCacheNode.h"
#include "JSClassRegister.h"

#define JS_TAG_EXTERNAL (JS_TAG_FLOAT64 + 1)

namespace pesapi
{
namespace qjsimpl
{

struct ObjectUserData
{
const puerts::JSClassDefinition* typeInfo;
const void* ptr;
bool callFinalize;
};

struct CppObjectMapper
{
inline static CppObjectMapper* Get(JSContext* ctx)
{
return reinterpret_cast<CppObjectMapper*>(JS_GetRuntimeOpaque(JS_GetRuntime(ctx)));
}

void Initialize(JSContext* ctx_);

void Cleanup();

CppObjectMapper(const CppObjectMapper&) = delete;
CppObjectMapper& operator=(const CppObjectMapper&) = delete;

//void* operator new(size_t) = delete;
void operator delete(void*) = delete;

CppObjectMapper() = default;
~CppObjectMapper() = default;

eastl::unordered_map<const void*, FObjectCacheNode, eastl::hash<const void*>, eastl::equal_to<const void*>, eastl::allocator_malloc> CDataCache;
eastl::unordered_map<const void*, JSValue, eastl::hash<const void*>, eastl::equal_to<const void*>, eastl::allocator_malloc> TypeIdToFunctionMap;

JSRuntime* rt;
JSContext* ctx;
JSClassID classId;
JSClassID funcTracerClassId;
const void* envPrivate = nullptr;
eastl::shared_ptr<int> ref = eastl::allocate_shared<int>(eastl::allocator_malloc("shared_ptr"), 0);

puerts::JSClassDefinition PtrClassDef = JSClassEmptyDefinition;

JSAtom privateDataKey;

inline static eastl::weak_ptr<int> GetEnvLifeCycleTracker(JSContext* ctx)
{
JSRuntime* rt = JS_GetRuntime(ctx);
CppObjectMapper* mapper = reinterpret_cast<CppObjectMapper*>(JS_GetRuntimeOpaque(rt));
return mapper->GetEnvLifeCycleTracker();
}

inline eastl::weak_ptr<int> GetEnvLifeCycleTracker()
{
return eastl::weak_ptr<int>(ref);
}

inline const void* GetEnvPrivate() const
{
return envPrivate;
}

inline void SetEnvPrivate(const void* envPrivate_)
{
envPrivate = envPrivate_;
}

inline const void* GetNativeObjectPtr(JSValue val)
{
ObjectUserData* object_udata = (ObjectUserData*)JS_GetOpaque(val, classId);
return object_udata ? object_udata->ptr : nullptr;
}

inline const void* GetNativeObjectTypeId(JSValue val)
{
ObjectUserData* object_udata = (ObjectUserData*)JS_GetOpaque(val, classId);
return object_udata ? object_udata->typeInfo->TypeId : nullptr;
}

inline bool GetPrivate(JSValue val, void** outPrr) const
{
if (!JS_IsObject(val))
{
return false;
}

JSValue data = JS_GetProperty(ctx, val, privateDataKey);
if (JS_VALUE_GET_TAG(data) == JS_TAG_EXTERNAL)
{
*outPrr = JS_VALUE_GET_PTR(data);
}
else
{
JS_FreeValue(ctx, data);
}
return true;
}

inline bool SetPrivate(JSValue val, void* ptr)
{
if (!JS_IsObject(val))
{
return false;
}
JS_SetProperty(ctx, val, privateDataKey, JS_MKPTR(JS_TAG_EXTERNAL, ptr));
return true;
}

JSValue CreateFunction(pesapi_callback Callback, void* Data, pesapi_function_finalize Finalize);

JSValue CreateClassByID(const void* typeId);

static JSValue CreateError(JSContext* ctx, const char* message);

JSValue MakeMethod(pesapi_callback Callback, void* Data);
void InitMethod(puerts::JSFunctionInfo* FuncInfo, JSValue Obj);
void InitProperty(puerts::JSPropertyInfo* PropInfo, JSValue Obj);

JSValue CreateClass(const puerts::JSClassDefinition* ClassDefinition);

void BindAndAddToCache(const puerts::JSClassDefinition* typeInfo, const void* ptr, JSValue value, bool callFinalize);

void RemoveFromCache(const puerts::JSClassDefinition* typeInfo, const void* ptr);

JSValue PushNativeObject(const void* TypeId, void* ObjectPtr, bool callFinalize);
};

} // namespace qjsimpl
} // namespace pesapi

// ----------------begin test interface----------------
PESAPI_MODULE_EXPORT pesapi_env_ref create_qjs_env();
PESAPI_MODULE_EXPORT void destroy_qjs_env(pesapi_env_ref env_ref);
PESAPI_MODULE_EXPORT struct pesapi_ffi* get_papi_ffi();
// ----------------end test interface----------------
Loading
Loading