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

Add Python Support (WIP) #94

Draft
wants to merge 199 commits into
base: feature/python
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
199 commits
Select commit Hold shift + click to select a range
1d6035e
add python backend
LanderlYoung May 1, 2021
eebd4c2
include
LanderlYoung May 1, 2021
62ddd0b
wip
LanderlYoung Sep 21, 2021
c6463a8
update
LanderlYoung Dec 12, 2021
d261af0
x
LanderlYoung Dec 18, 2021
b5470e9
py local ref-count
LanderlYoung Dec 18, 2021
0a8edaa
py scope
LanderlYoung Dec 18, 2021
7ca2557
py equals
LanderlYoung Dec 19, 2021
e3fe67f
py arguments & newFunction
LanderlYoung Dec 19, 2021
d5a89cc
wip
LanderlYoung Dec 19, 2021
2cded51
Merge branch 'Tencent:main' into python
Aug 1, 2022
b8b92b8
add eval and many basic things
Aug 1, 2022
843645e
新增类注册,函数注册,类函数注册
Aug 2, 2022
2385a1b
新增prop和static
Aug 3, 2022
3d64d80
修复eval
Aug 3, 2022
8d91b5f
fix Global and Weak
Aug 4, 2022
95ebbdf
Fork TestLibs from Tencent
yqs112358 Aug 4, 2022
c582765
change devops unit test to python backend
yqs112358 Aug 4, 2022
586ea59
add getScriptEngine
yqs112358 Aug 4, 2022
2f56d36
fix
Aug 4, 2022
584e80f
fix Native
Aug 4, 2022
18666df
Small fix about Global ref
yqs112358 Aug 5, 2022
8de1ba9
add Exception Translate
Aug 5, 2022
083b2d3
Fix for unittest devops
yqs112358 Aug 5, 2022
39fc157
Update unit_tests.yml
yqs112358 Aug 5, 2022
e1f0e75
add loadFile to engine (first step)
yqs112358 Aug 5, 2022
1de6cc0
Merge branch 'Add-LoadFile' into python
yqs112358 Aug 5, 2022
3dcd8e2
Add loadFile for python backend
yqs112358 Aug 5, 2022
513ef11
Polish PyEngine
Aug 6, 2022
bb3040f
tmp
Aug 6, 2022
99cf1b6
Try to fix python GIL scope
yqs112358 Aug 7, 2022
c78b13a
Fix GIL problem
yqs112358 Aug 7, 2022
f65a91c
tmp
Aug 7, 2022
0c1f993
Fix exception deadlock
yqs112358 Aug 7, 2022
d1e591b
Fix Scope for multi-instance GIL dead-lock
yqs112358 Aug 8, 2022
1b5804c
Fix comment
yqs112358 Aug 8, 2022
82ee90b
modified: backend/Python/PyEngine.cc
Aug 8, 2022
9ab607e
Remove pybind11
Aug 8, 2022
e0eb455
Add Class register and construct
Aug 8, 2022
4d46a4e
Revert "modified: backend/Python/PyEngine.cc"
yqs112358 Aug 9, 2022
44a1590
tmp
Aug 9, 2022
4a41d1f
Function warpper
Aug 9, 2022
580ac7c
Fix a bug
Aug 9, 2022
e35cb42
Fix getGlobalDict crash
yqs112358 Aug 9, 2022
695281e
warpFunction新增参数
Aug 10, 2022
e02d41b
fix static function register & other small problems
yqs112358 Aug 22, 2022
e552388
All is fileinput
Aug 22, 2022
9346bca
Merge branch 'python-try-to-remove-bind11' of https://github.com/Lite…
Aug 22, 2022
9546422
debug version
Aug 22, 2022
14027c0
fix ref count
Aug 22, 2022
d1b32d2
picture a cake
Aug 22, 2022
5de82fa
picture a cake 2
Aug 23, 2022
2911da8
fix problem of getGlobalDict
yqs112358 Aug 23, 2022
cf16e6c
Fix a reference count problem
yqs112358 Aug 23, 2022
4046210
picture a cake 3
Aug 23, 2022
64d80a4
cake finishied!!!!!!!!!!
Aug 23, 2022
4cf5275
Merge branch 'python-try-to-remove-bind11' into python
yqs112358 Aug 23, 2022
0a77d98
fix static property
Aug 23, 2022
ec34e1c
Finish exception system of python backend
yqs112358 Aug 24, 2022
cabeaa0
Fix a global reference bug
yqs112358 Aug 24, 2022
21e7284
Fix a memory leak
Aug 24, 2022
cd29471
better format of traceback
yqs112358 Aug 24, 2022
27c6d50
Add two impl
Aug 24, 2022
0dd7b5a
Polish code
Aug 24, 2022
d2120c0
New py_interop
Aug 24, 2022
0e1c24a
New py_interop
Aug 24, 2022
c9d885d
Fix Weak
Aug 24, 2022
5453582
Fix memory leak
Aug 24, 2022
c3ca669
Fix alllllllllllllllllll memory leak!!!!
Aug 24, 2022
332b2af
tmp
Aug 24, 2022
106071d
Add namespace support
Aug 24, 2022
e6246bd
Fix isInstanceOf
Aug 24, 2022
23d55fa
pass basic multi-thread test & more work need to do
yqs112358 Aug 24, 2022
41ad30a
change oldThreadStateStack_ to TLS
yqs112358 Aug 25, 2022
cfa6408
Fix GIL & thread state in multi-thread env thoroughly
yqs112358 Aug 25, 2022
01b6c09
Fix comment
yqs112358 Aug 25, 2022
0b13064
avoid exit not-my-managed engine
yqs112358 Aug 25, 2022
0f77e74
Add module
Aug 25, 2022
ec2a02c
Fix destroy engine
yqs112358 Aug 25, 2022
945d23f
Merge branch 'fix-for-unit-test' of https://github.com/LiteLDev/Scrip…
yqs112358 Aug 25, 2022
b38acca
Fix GIL & thread state in PyEngine creation
yqs112358 Aug 25, 2022
c34e67f
disable Py_EndInterpreter temporarily to fix it later
yqs112358 Aug 25, 2022
dc5e67f
Fix Global Local Weak
Aug 25, 2022
bf9ec60
Fix Local<Value> constructor
Aug 25, 2022
a42067f
Add code modification for python
Aug 29, 2022
35c9444
Fix exception crash bug
yqs112358 Aug 29, 2022
0bb717f
Fix eval expression check (temp fix)
yqs112358 Aug 29, 2022
e0a1f38
Fix an string error
Aug 29, 2022
6a49112
Merge branch 'fix-for-unit-test' of https://github.com/LiteLDev/Scrip…
Aug 29, 2022
13eab2d
add document for python
yqs112358 Aug 29, 2022
74c5d21
Fix Array::set
Aug 29, 2022
a870a90
Merge branch 'fix-for-unit-test' of https://github.com/LiteLDev/Scrip…
Aug 29, 2022
0fa618d
Fix isByteBuffer
Aug 29, 2022
33ce503
Fix type judgement
Aug 29, 2022
ce8a803
Fix unsupported
Aug 29, 2022
2b06432
Polish the namespace support
Aug 30, 2022
397559c
Fix namespace waining
Aug 30, 2022
6bd3b25
Modify class register
Aug 31, 2022
1bd08c9
Uncompeleted code
Sep 1, 2022
beaf472
Python warp
Sep 2, 2022
7b4c8d0
Merge pull request #3 from LiteLDev/fix-for-unit-test
Sep 2, 2022
f213e60
delete incRef and decRef
Sep 3, 2022
aca2f79
Fix static property
Sep 3, 2022
90bb6e2
Polish warp code
Sep 3, 2022
79efe9f
Fix setAttr and some functions memory leak
Sep 4, 2022
0353c50
Fix extra Py_DECREF
Sep 4, 2022
ede2c4f
detail modified
Sep 4, 2022
874784a
Add more Nativetest
Sep 6, 2022
7ba6814
Simple modification
Sep 6, 2022
919e896
Try to fix memory leak
Sep 6, 2022
9aca2b2
Fix wrong decref
yqs112358 Feb 26, 2023
c0a2767
Fix ref count problems about PyTuple_SetItem
yqs112358 Feb 26, 2023
83fa81b
skip unsolved problem and leave todo
yqs112358 Feb 26, 2023
20aedc5
fix array set ref count bug and some other small bugs
yqs112358 Feb 26, 2023
08fd119
Fix ref count bug caused function return crash
yqs112358 Feb 27, 2023
4a556e2
add native test code for python
yqs112358 Feb 27, 2023
0b8a9e2
add 2 todos
yqs112358 Feb 28, 2023
6147b80
Fix test problems
yqs112358 Feb 28, 2023
8a5b97a
Fix callImpl to consider thiz
yqs112358 Feb 28, 2023
ac74f5b
Check exception after call function
yqs112358 Feb 28, 2023
08ee0d6
Finish demo unit test for py
yqs112358 Feb 28, 2023
9e6316a
change BytesBuffer type to bytearray and adapt unittest
yqs112358 Feb 28, 2023
1909428
finish ByteBuffer and pass UnitTest
yqs112358 Feb 28, 2023
43d15ac
fix and finish Weak and Global class
yqs112358 Mar 1, 2023
ef66684
Check and fix all ref count bugs about Local class
yqs112358 Mar 1, 2023
d2fd37c
reconstruct Global&Weak storage to support cleanup
yqs112358 Mar 2, 2023
69a5f07
fix construct from cpp
yqs112358 Mar 2, 2023
3b42fab
finish more tests for python
yqs112358 Mar 2, 2023
c638484
support CPP exception -> Python
yqs112358 Mar 2, 2023
d6d9a9d
remove message in queue when engine shutdown
yqs112358 Mar 3, 2023
fec7d72
Reconstruct exception class
yqs112358 Mar 3, 2023
c735487
finish getInternalStore and support custom object __dict__
yqs112358 Mar 5, 2023
f0fe777
rename a helper func
yqs112358 Mar 5, 2023
bbee6f5
small fix
yqs112358 Mar 5, 2023
7d0e340
Add docs about weak ref problem
yqs112358 Mar 5, 2023
790bf14
May fix wrong thiz
yqs112358 Mar 6, 2023
c7e3f9d
disable thiz redirction in Function because Py does not support
yqs112358 Mar 6, 2023
b77ae8e
Add some docs
yqs112358 Mar 6, 2023
2fba154
Fix EngineScope completely and add more comments
yqs112358 Mar 6, 2023
a9cff1c
Fix a bug in exitscope
yqs112358 Mar 7, 2023
b0f33fa
Finish basic logic
yqs112358 Mar 7, 2023
3d6359e
try to fix
yqs112358 Mar 8, 2023
1860bfa
Miss-understand EngineScope! Finally fix it
yqs112358 Mar 8, 2023
581b846
Fix ref bug of Py_None
yqs112358 Mar 8, 2023
773743c
add some comments
yqs112358 Mar 8, 2023
f806e66
More fix about refs of Global and Weak
yqs112358 Mar 8, 2023
620c6fa
fix namespace bug cause crash
yqs112358 Mar 9, 2023
7bfcc24
Fix unittest and leave a todo
yqs112358 Mar 9, 2023
41ab348
Fix instanceOf
yqs112358 Mar 9, 2023
35e0d2b
add some comment
Mar 9, 2023
20446c1
small fix
yqs112358 Mar 9, 2023
da3b0c4
Fix constructor from CPP
yqs112358 Mar 9, 2023
8326c38
Fix Global and Weak for unexpected no destroy
yqs112358 Mar 10, 2023
e196720
Fix name of unittest
yqs112358 Mar 10, 2023
fb2203b
Fix destroy bug
yqs112358 Mar 10, 2023
dfca1fb
Add weakref callback to avoid refusing creation
yqs112358 Mar 10, 2023
d0a7fb5
Fix function callback leak
yqs112358 Mar 10, 2023
7165385
Fix error of Local ref
yqs112358 Mar 11, 2023
f630b6c
Fix bad UnitTest
yqs112358 Mar 11, 2023
327ab1f
Fix a serious bug of creating custom type objects
yqs112358 Mar 11, 2023
53ff696
add docs
yqs112358 Mar 11, 2023
b6b9374
Add tracers and some doc
yqs112358 Mar 11, 2023
be04b00
Small fix about eval judgement
yqs112358 Mar 11, 2023
29f9949
Add coverage test for Python
yqs112358 Mar 12, 2023
682757b
Add test libs for linux64 (apple needs adaptation)
yqs112358 Mar 12, 2023
9d94546
Fix include to pass compile on linux
yqs112358 Mar 12, 2023
f7f051b
Fix a pre-declare problem
yqs112358 Mar 12, 2023
2020334
Fix some warnings in GCC
yqs112358 Mar 13, 2023
f2e0545
Fix some warnings in GCC
yqs112358 Mar 13, 2023
a7c465d
Add a missing file
yqs112358 Mar 13, 2023
1b0b25f
Some fix to avoid include circular references
yqs112358 Mar 13, 2023
6cb894d
Change PyInternalHelper.cc to .c to pass compile
yqs112358 Mar 13, 2023
492d083
Add SCRIPTX_BEGIN_INCLUDE_LIBRARY to avoid warnings in python
yqs112358 Mar 13, 2023
21e9395
Fix some for GCC warnings
yqs112358 Mar 13, 2023
495b30c
Add setPythonHomePath api for PyEngine
yqs112358 Mar 13, 2023
0268e4c
Finish runtime settings and pass unittest on Ubuntu
yqs112358 Mar 14, 2023
057a0a9
Finish adaptation on win platform
yqs112358 Mar 14, 2023
93c1ceb
Fix link problem on GCC
yqs112358 Mar 14, 2023
73160bf
ignore -Wdeprecated-declarations for clang
yqs112358 Mar 14, 2023
f941c69
Add addModuleSearchPath
yqs112358 Mar 14, 2023
40c0616
Add docs about embed env
yqs112358 Mar 14, 2023
f8afebf
Recover workflow and test gh actions
yqs112358 Mar 14, 2023
213aba1
Fix
yqs112358 Mar 14, 2023
c108f5e
Fix bugs
yqs112358 Mar 14, 2023
70314c8
Better testlib and default settings
yqs112358 Mar 16, 2023
d40ce39
Move python runtime APIs to py_interop
yqs112358 Mar 17, 2023
eae0969
Fix crash on no-setter property
yqs112358 Mar 17, 2023
cbebbd6
Add locker for engine, threadstate and GIL operations
yqs112358 Mar 18, 2023
79df72f
Allow re-enter in the same thread
yqs112358 Mar 18, 2023
886db05
Fix eval
yqs112358 Mar 19, 2023
b8080bf
Improve exception and add apis to py_interop
yqs112358 Mar 19, 2023
8dc4330
Fix compile warning for GCC
yqs112358 Mar 19, 2023
951854c
Fix exception ref count problem and fix builtin set
yqs112358 Mar 21, 2023
7cce100
Fix engine->get
yqs112358 Mar 21, 2023
d7cdc91
Small fix about engine scope
yqs112358 Mar 23, 2023
5e738d6
Fix bug of mainInterpreterState & mainThreadState
yqs112358 Mar 23, 2023
7ce7e3b
Fix gcc linking error
yqs112358 Mar 23, 2023
e5bfb66
Change EngineLockerHelper to work like GIL
yqs112358 Mar 23, 2023
2fcd812
Fix ConstructFromCpp and nullptr constructor
yqs112358 Mar 25, 2023
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
3 changes: 2 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- main
- python
paths-ignore:
- 'docs/**'

Expand All @@ -17,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
backends: [ V8, JavaScriptCore, QuickJs, Lua ]
backends: [ V8, JavaScriptCore, QuickJs, Lua, Python ]
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- main
- python
paths-ignore:
- 'docs/**'
workflow_dispatch:
Expand Down Expand Up @@ -56,7 +57,7 @@ jobs:
strategy:
fail-fast: false
matrix:
backends: [ V8, JavaScriptCore, Lua, Empty ]
backends: [ V8, JavaScriptCore, Lua, Python, Empty ]
build_type:
- Debug
- Release
Expand Down Expand Up @@ -106,7 +107,7 @@ jobs:
strategy:
fail-fast: false
matrix:
backends: [ V8, JavaScriptCore, QuickJs, Lua, Empty ]
backends: [ V8, JavaScriptCore, QuickJs, Lua, Python, Empty ]
build_type:
- Debug
- Release
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ set(SCRIPTX_BACKEND_LIST
)

# set options, choose which ScriptX Backend to use, V8 or JSC or etc...
set(SCRIPTX_BACKEND "" CACHE STRING "choose which ScriptX Backend(Implementation) to use, V8 or JavaScriptCore or etc...")
set(SCRIPTX_BACKEND "${SCRIPTX_BACKEND}" CACHE STRING "choose which ScriptX Backend(Implementation) to use, V8 or JavaScriptCore or etc...")
set_property(CACHE SCRIPTX_BACKEND PROPERTY STRINGS "${SCRIPTX_BACKEND_LIST}")
option(SCRIPTX_NO_EXCEPTION_ON_BIND_FUNCTION "don't throw exception on defineClass generated bound function/get/set, return null & log instead. default to OFF" OFF)
option(SCRIPTX_FEATURE_INSPECTOR "enable inspector feature, default to OFF" OFF)
Expand Down Expand Up @@ -177,4 +177,4 @@ message(STATUS "Configuring ScriptX using backend ${SCRIPTX_BACKEND}.")
message(STATUS "Configuring ScriptX option SCRIPTX_NO_EXCEPTION_ON_BIND_FUNCTION ${SCRIPTX_NO_EXCEPTION_ON_BIND_FUNCTION}.")
message(STATUS "Configuring ScriptX feature SCRIPTX_FEATURE_INSPECTOR ${SCRIPTX_FEATURE_INSPECTOR}.")

include(${SCRIPTX_DIR}/docs/doxygen/CMakeLists.txt)
include(${SCRIPTX_DIR}/docs/doxygen/CMakeLists.txt)
4 changes: 2 additions & 2 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ ScriptX通过一系列的技术手段实现了脚本的异常和C++异常相互

ScriptX 设计的时候充分考虑到API的易用性,包括操作友好简单,不易出错,错误信息明显,便于定位问题等。在这样的指导思想之下ScriptX做了很多原生引擎做不了的事情。

比如:V8在destory的时候是不执行GC的,导致很多绑定的native类不能释放。ScriptX做了额外的逻辑处理这个情况。
比如:V8在destroy的时候是不执行GC的,导致很多绑定的native类不能释放。ScriptX做了额外的逻辑处理这个情况。

V8和JSCore要求在finalize回调中不能调用ScriptX的其他API,否则会crash,这也导致代码逻辑很难实现。ScriptX借助MessageQueue完美规避这个问题。

V8和JSCore的全局引用都必须在engine destory之前全部释放掉,否则就会引起crash、不能destory等问题。ScriptX则保证在Engine destory的时候主动reset所有 Global / Weak 引用。
V8和JSCore的全局引用都必须在engine destroy之前全部释放掉,否则就会引起crash、不能destroy等问题。ScriptX则保证在Engine destroy的时候主动reset所有 Global / Weak 引用。

## 6. 简单高效的绑定API

Expand Down
22 changes: 22 additions & 0 deletions backend/JavaScriptCore/JscEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../../src/Native.hpp"
#include "JscEngine.hpp"
#include "JscHelper.h"
#include "../../src/utils/Helper.hpp"

namespace script::jsc_backend {

Expand Down Expand Up @@ -177,6 +178,27 @@ script::Local<script::Value> JscEngine::eval(const script::Local<script::String>
return eval(script, {});
}

Local<Value> JscEngine::loadFile(const Local<String>& scriptFile) {
if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != std::string::npos)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != std::string::npos)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);
return eval(content.asString(), sourceFileName);
}

std::shared_ptr<utils::MessageQueue> JscEngine::messageQueue() { return messageQueue_; }

void JscEngine::gc() {
Expand Down
2 changes: 2 additions & 0 deletions backend/JavaScriptCore/JscEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class JscEngine : public ::script::ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

std::shared_ptr<utils::MessageQueue> messageQueue() override;

void gc() override;
Expand Down
2 changes: 1 addition & 1 deletion backend/JavaScriptCore/JscUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ std::string_view StringHolder::stringView() const {
std::string StringHolder::string() const {
jsc_backend::initString(internalHolder_);
internalHolder_.inited = false;
return std::move(internalHolder_.stringContent);
return internalHolder_.stringContent;
}

#if defined(__cpp_char8_t)
Expand Down
22 changes: 22 additions & 0 deletions backend/Lua/LuaEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "LuaHelper.hpp"
#include "LuaReference.hpp"
#include "LuaScope.hpp"
#include "../../src/utils/Helper.hpp"

// ref https://www.lua.org/manual/5.1/manual.html
// https://www.lua.org/wshop14/Zykov.pdf
Expand Down Expand Up @@ -259,6 +260,27 @@ Local<Value> LuaEngine::eval(const Local<String>& script, const Local<Value>& so
return lua_backend::callFunction({}, {}, 0, nullptr);
}

Local<Value> LuaEngine::loadFile(const Local<String>& scriptFile) {
if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != std::string::npos)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != std::string::npos)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);
return eval(content.asString(), sourceFileName);
}

Arguments LuaEngine::makeArguments(LuaEngine* engine, int stackBase, size_t paramCount,
bool isInstanceFunc) {
lua_backend::ArgumentsData argumentsData{engine, stackBase, paramCount, isInstanceFunc};
Expand Down
2 changes: 2 additions & 0 deletions backend/Lua/LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class LuaEngine : public ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

std::shared_ptr<utils::MessageQueue> messageQueue() override;

void gc() override;
Expand Down
37 changes: 33 additions & 4 deletions backend/Lua/LuaLocalReference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ void ensureNonnull(int index) {
throw Exception("NullPointerException");
}

bool judgeIsArray(int index)
{
auto lua = currentLua();
int currectArrIndex = 0;

lua_pushnil(lua);

while (lua_next(lua, index))
{
// Copy current key and judge it's type
lua_pushvalue(lua, -2);
if(!lua_isnumber(lua,-1) || lua_tonumber(lua,-1) != ++currectArrIndex)
{
lua_pop(lua, 3);
return false;
}
lua_pop(lua, 2);
}
return true;
}


} // namespace lua_backend

#define REF_IMPL_BASIC_FUNC(ValueType) \
Expand Down Expand Up @@ -149,8 +171,11 @@ ValueKind Local<Value>::getKind() const {
} else if (isByteBuffer()) {
return ValueKind::kByteBuffer;
} else if (type == LUA_TTABLE) {
// lua don't have array type, the are all tables
return ValueKind::kObject;
// Traverse the table to judge whether it is an array
if(isArray())
return ValueKind::kArray;
else
return ValueKind::kObject;
} else {
return ValueKind::kUnsupported;
}
Expand All @@ -176,7 +201,11 @@ bool Local<Value>::isFunction() const {
return val_ != 0 && lua_type(lua_backend::currentLua(), val_) == LUA_TFUNCTION;
}

bool Local<Value>::isArray() const { return isObject(); }
bool Local<Value>::isArray() const {
if(val_ == 0 || lua_type(lua_backend::currentLua(), val_) != LUA_TTABLE)
return false;
return lua_backend::judgeIsArray(val_);
}

bool Local<Value>::isByteBuffer() const {
auto engine = lua_backend::currentEngine();
Expand Down Expand Up @@ -437,4 +466,4 @@ void Local<ByteBuffer>::commit() const {}

void Local<ByteBuffer>::sync() const {}

} // namespace script
} // namespace script
22 changes: 21 additions & 1 deletion backend/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
message(FATAL_ERROR "${SCRIPTX_BACKEND} is to be implemented.")
target_sources(ScriptX PRIVATE
${CMAKE_CURRENT_LIST_DIR}/PyEngine.cc
${CMAKE_CURRENT_LIST_DIR}/PyEngine.h
${CMAKE_CURRENT_LIST_DIR}/PyException.cc
${CMAKE_CURRENT_LIST_DIR}/PyHelper.cc
${CMAKE_CURRENT_LIST_DIR}/PyHelper.h
${CMAKE_CURRENT_LIST_DIR}/PyHelper.hpp
${CMAKE_CURRENT_LIST_DIR}/PyInternalHelper.c
${CMAKE_CURRENT_LIST_DIR}/PyInternalHelper.h
${CMAKE_CURRENT_LIST_DIR}/PyLocalReference.cc
${CMAKE_CURRENT_LIST_DIR}/PyNative.cc
${CMAKE_CURRENT_LIST_DIR}/PyNative.hpp
${CMAKE_CURRENT_LIST_DIR}/PyRuntimeSettings.cc
${CMAKE_CURRENT_LIST_DIR}/PyRuntimeSettings.h
${CMAKE_CURRENT_LIST_DIR}/PyReference.cc
${CMAKE_CURRENT_LIST_DIR}/PyReference.hpp
${CMAKE_CURRENT_LIST_DIR}/PyScope.cc
${CMAKE_CURRENT_LIST_DIR}/PyScope.h
${CMAKE_CURRENT_LIST_DIR}/PyUtils.cc
${CMAKE_CURRENT_LIST_DIR}/PyValue.cc
)
Loading