Skip to content

Commit

Permalink
Merge branch 'Tencent:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoChenFx authored Nov 1, 2024
2 parents f4ce0dc + 66097b1 commit da22c09
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 65 deletions.
62 changes: 31 additions & 31 deletions .github/workflows/unity_unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,37 @@ jobs:
echo "testresult in v2"
cat log2.txt | grep "Failed\|Passed"
# unittest-osx-unity:
# runs-on: macos-latest
# steps:
# - uses: actions/checkout@v3
# - name: Setup Unity
# uses: ./.github/workflows/composites/unity-setup/
# with:
# os: 'osx'
# version: '2021.3.16f1'
# cachekey: '2021'
# install_path: /Applications/UNITY
# unity_modules: mac-il2cpp
# architecture: x86_64
# UNITY_USERNAME: ${{ secrets.UNITY_USERNAME }}
# UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
# UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
# - name: UnitTest
# run: |
# cd unity
# npm i
# cd test/unity
# node ../../cli unity-test --unity /Applications/UNITY/2021.3.16f1/Unity.app/Contents/MacOS/Unity
# - name: TestResult
# if: always()
# shell: bash
# run: |
# cd unity/test/unity
# echo "testresult in v1"
# cat log1.txt | grep "Failed\|Passed"
# echo "testresult in v2"
# cat log2.txt | grep "Failed\|Passed"
unittest-osx-unity:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Setup Unity
uses: ./.github/workflows/composites/unity-setup/
with:
os: 'osx'
version: '2021.3.16f1'
cachekey: '2021'
install_path: /Applications/UNITY
unity_modules: mac-il2cpp
architecture: x86_64
UNITY_USERNAME: ${{ secrets.UNITY_USERNAME }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
- name: UnitTest
run: |
cd unity
npm i
cd test/unity
node ../../cli unity-test --unity /Applications/UNITY/2021.3.16f1/Unity.app/Contents/MacOS/Unity
- name: TestResult
if: always()
shell: bash
run: |
cd unity/test/unity
echo "testresult in v1"
cat log1.txt | grep "Failed\|Passed"
echo "testresult in v2"
cat log2.txt | grep "Failed\|Passed"
unittest-win-dotnet:
runs-on: windows-2019
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unreal_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
env:
Engine: "https://github.com/chexiongsheng/Mini427/releases/download/v1.0.1/Mini427_new_xcode.tgz"
V8_URL: "https://github.com/puerts/backend-v8/releases/download/V8_9.4.146.24__241009/v8_bin_9.4.146.24.tgz"
QJS_URL: "https://github.com/puerts/backend-quickjs/releases/download/QJS_240603/qjs_v8_bin.tgz"
QJS_URL: "https://github.com/puerts/backend-quickjs/releases/download/QJS_241030/qjs_v8_bin.tgz"
NODE_URL: "https://github.com/puerts/backend-nodejs/releases/download/NodeJS_16.16.0_221228/nodejs_bin_16.16.0.tgz"

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unreal_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
V8_84: "https://github.com/puerts/backend-v8/releases/download/V8_8.4.371.19_230822/v8_bin_8.4.371.19.tgz"
V8_94: "https://github.com/puerts/backend-v8/releases/download/V8_9.4.146.24__241009/v8_bin_9.4.146.24.tgz"
V8_106: "https://github.com/puerts/backend-v8/releases/download/V8_10.6.194__241009/v8_bin_10.6.194.tgz"
quickjs: "https://github.com/puerts/backend-quickjs/releases/download/QJS_240604/qjs_v8_bin.tgz"
quickjs: "https://github.com/puerts/backend-quickjs/releases/download/QJS_241030/qjs_v8_bin.tgz"
nodejs: "https://github.com/puerts/backend-nodejs/releases/download/NodeJS_16.16.0_221228/nodejs_bin_16.16.0.tgz"

jobs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ export function CSValToJSVal(signature, CSName) {
const elemSignature = signature.substring(1);
if (elemSignature in PrimitiveSignatureCppTypeMap) {
return `converter::Converter<std::reference_wrapper<${PrimitiveSignatureCppTypeMap[elemSignature]}>>::toScript(env, *${CSName})`;
} else if (isStruct(elemSignature)) {
return `pesapi_native_object_to_value(env, ${TIName}, ${CSName}, false)`;
} else if (isStruct(elemSignature) || signature == 'Po' || signature == 'PO' || signature == 'Pa') {
return `pesapi_boxing(env, pesapi_native_object_to_value(env, ${TIName}, ${CSName}, false))`;
}
}
//TODO: 能处理的就处理, DateTime是否要处理呢?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function genBridgeArgs(parameterSignatures) {
if (parameterSignatures.length != 0) {
if (parameterSignatures[parameterSignatures.length -1][0] != 'V') {
return `pesapi_value argv[${parameterSignatures.length}]{
${parameterSignatures.map((ps, i)=> il2cpp_snippets.CSValToJSVal(ps, `p${i}`) || 'pesapi_create_undefined(env)').join(`,
${parameterSignatures.map((ps, i)=> il2cpp_snippets.CSValToJSVal(ps[0] == 'D' ? ps.substring(1) : ps, `p${i}`) || 'pesapi_create_undefined(env)').join(`,
`)}
};`
} else {
Expand Down
25 changes: 17 additions & 8 deletions unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class EventTarget {

const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];

const poll_ws_objects = [];

class WebSocket extends EventTarget {
constructor(url, protocols) {
super();
Expand All @@ -72,9 +70,7 @@ class WebSocket extends EventTarget {
this._addPendingEvent({type:'close', code:code, reason: reason});
},
() => {
this._addPendingEvent({type:'error'});
this._cleanup();
this._addPendingEvent({type:'close', code:1006, reason: ""});
this._fail();
});

this._readyState = WebSocket.CONNECTING;
Expand All @@ -92,9 +88,21 @@ class WebSocket extends EventTarget {

send(data) {
if (this._readyState !== WebSocket.OPEN) {
throw new Error(`WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`);
//throw new Error(`WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`);
this.dispatchEvent({type:'error'}); //dispatchEvent immediately
return;
}
try {
this._raw.send(data);
} catch {
this._fail();
}
this._raw.send(data);
}

_fail() {
this._addPendingEvent({type:'error'});
this._cleanup();
this._addPendingEvent({type:'close', code:1006, reason: ""});
}

_cleanup() {
Expand All @@ -111,10 +119,11 @@ class WebSocket extends EventTarget {
}
const ev = this._pendingEvents.shift();
if (ev) this.dispatchEvent(ev);
if (this._pendingEvents.length === 0 && this._readyState == WebSocket.CLOSING) {
if ((this._pendingEvents.length === 0 && this._readyState == WebSocket.CLOSING) || (ev && ev.type === 'close')) {
this._raw = undefined;
clearInterval(this._tid);
this._readyState = WebSocket.CLOSED;
this._pendingEvents = [];
}
}

Expand Down
25 changes: 25 additions & 0 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Puerts
[UnityEngine.Scripting.Preserve]
public class JsEnv : IDisposable
{
private static List<JsEnv> jsEnvs = new List<JsEnv>();
private readonly int Idx;
IntPtr nativeJsEnv;
IntPtr nativePesapiEnv;
IntPtr nativeScriptObjectsRefsMgr;
Expand Down Expand Up @@ -124,6 +126,25 @@ public JsEnv(ILoader loader, int debugPort = -1)
#endif

this.debugPort = debugPort;
// prevent c# gc, manual call Dispose if you release this
lock (jsEnvs)
{
Idx = -1;
for (int i = 0; i < jsEnvs.Count; i++)
{
if (jsEnvs[i] == null)
{
Idx = i;
jsEnvs[Idx] = this;
break;
}
}
if (Idx == -1)
{
Idx = jsEnvs.Count;
jsEnvs.Add(this);
}
}
if (loader is IBuiltinLoadedListener)
(loader as IBuiltinLoadedListener).OnBuiltinLoaded(this);
}
Expand Down Expand Up @@ -235,6 +256,10 @@ protected virtual void Dispose(bool dispose)
PuertsIl2cpp.NativeAPI.DestroyNativeJSEnv(nativeJsEnv);
disposed = true;
}
lock (jsEnvs)
{
jsEnvs[Idx] = null;
}
}

public void UsingAction<T1>() { }
Expand Down
4 changes: 2 additions & 2 deletions unity/cli/backends.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"quickjs": {
"url": "https://github.com/puerts/backend-quickjs/releases/download/QJS_240625/qjs_v8_bin.tgz",
"url": "https://github.com/puerts/backend-quickjs/releases/download/QJS_241030/qjs_v8_bin.tgz",
"config": {
"definition": [
"V8_94_OR_NEWER",
Expand Down Expand Up @@ -453,7 +453,7 @@
}
},
"mult": {
"url": "https://github.com/puerts/backend-mult/releases/download/MULT_241009/mult_backends.tgz",
"url": "https://github.com/puerts/backend-mult/releases/download/MULT_241030/mult_backends.tgz",
"config": {
"definition": [
"V8_94_OR_NEWER"
Expand Down
2 changes: 1 addition & 1 deletion unity/native_src_il2cpp/Src/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ pesapi_value pesapi_eval(pesapi_env env, const uint8_t* code, size_t code_size,
std::vector<char> buff;
buff.reserve(code_size + 1);
memcpy(buff.data(), code, code_size);
buff[code_size] = '\0';
buff.data()[code_size] = '\0';
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, buff.data(), v8::NewStringType::kNormal).ToLocalChecked();
#if V8_MAJOR_VERSION > 8
v8::ScriptOrigin origin(isolate, url);
Expand Down
22 changes: 22 additions & 0 deletions unity/test/Src/Cases/ExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,27 @@ function t() {
});
jsEnv.Tick();
}

[Test]
public void QuickjsStackOverflowTest()
{
var jsEnv = UnitTestEnv.GetEnv();
ConsumeStackAndCallJs(8 * 1024, jsEnv);
}

void ConsumeStackAndCallJs(int loop, JsEnv jsEnv)
{
if (loop > 0)
{
ConsumeStackAndCallJs(loop - 1, jsEnv);
}
else
{
jsEnv.Eval(@"
function foo(p) { return p;}
foo(1);
");
}
}
}
}
30 changes: 26 additions & 4 deletions unity/test/Src/Cases/WebsocketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class WebsocketTest
[Test]
public async Task SmokeTest()
{
WebSocketServer wss = new WebSocketServer("http://localhost:5000/");
WebSocketServer wss = new WebSocketServer("http://localhost:5123/");
#if PUERTS_GENERAL
var jsEnv = new JsEnv(new TxtLoader());
#else
Expand All @@ -96,15 +96,21 @@ public async Task SmokeTest()

jsEnv.Eval(@"
(function() {
let con = new WebSocket('ws://localhost:5000');
global.con = new WebSocket('ws://localhost:5123');
con.addEventListener('open', (ev) => {
console.log(`on open`);
con.send('puerts websocket');
});
con.addEventListener('message', (ev) => {
console.log(`on message: ${ev.data}`);
global.webSocketMessage = ev.data;
con.close();
//con.close();
});
con.addEventListener('close', (ev) => {
global.onclose_called = true;
});
con.addEventListener('error', (ev) => {
global.onerror_called = true;
});
})();
");
Expand All @@ -118,9 +124,25 @@ public async Task SmokeTest()
await wss.SendAsync(msg);

waitJsEnv();
wss.Stop();

var res = jsEnv.Eval<string>("global.webSocketMessage");
Assert.AreEqual(res, "puerts websocket");

waitJsEnv();

jsEnv.Eval(@"
con._raw.send = () => {throw new Error()};
con.send('some message');
");

waitJsEnv();

var flag = jsEnv.Eval<bool>("global.onclose_called");
Assert.AreEqual(flag, true);
flag = jsEnv.Eval<bool>("global.onerror_called");
Assert.AreEqual(flag, true);

wss.Stop();
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions unreal/Puerts/Content/JavaScript/puerts/websocketpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ var global = global || (function () { return this; }());

const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];

const poll_ws_objects = [];

class WebSocket extends EventTarget {
constructor(url, protocols) {
super();
Expand All @@ -72,9 +70,7 @@ var global = global || (function () { return this; }());
this._addPendingEvent({type:'close', code:code, reason: reason});
},
() => {
this._addPendingEvent({type:'error'});
this._cleanup();
this._addPendingEvent({type:'close', code:1006, reason: ""});
this._fail();
});

this._readyState = WebSocket.CONNECTING;
Expand All @@ -92,9 +88,21 @@ var global = global || (function () { return this; }());

send(data) {
if (this._readyState !== WebSocket.OPEN) {
throw new Error(`WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`);
//throw new Error(`WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`);
this.dispatchEvent({type:'error'}); //dispatchEvent immediately
return;
}
try {
this._raw.send(data);
} catch {
this._fail();
}
this._raw.send(data);
}

_fail() {
this._addPendingEvent({type:'error'});
this._cleanup();
this._addPendingEvent({type:'close', code:1006, reason: ""});
}

_cleanup() {
Expand All @@ -111,10 +119,11 @@ var global = global || (function () { return this; }());
}
const ev = this._pendingEvents.shift();
if (ev) this.dispatchEvent(ev);
if (this._pendingEvents.length === 0 && this._readyState == WebSocket.CLOSING) {
if ((this._pendingEvents.length === 0 && this._readyState == WebSocket.CLOSING) || (ev && ev.type === 'close')) {
this._raw = undefined;
clearInterval(this._tid);
this._readyState = WebSocket.CLOSED;
this._pendingEvents = [];
}
}

Expand Down
10 changes: 7 additions & 3 deletions unreal/Puerts/Source/JsEnv/JsEnv.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,14 @@ void ThirdPartyQJS(ReadOnlyTargetRules Target)
PublicDefinitions.Add("WITH_QJS_NAMESPACE_SUFFIX=1");
PublicDefinitions.Add("QJSV8NAMESPACE=v8_qjs");
}

string ThirdPartyPath = Path.GetFullPath(Path.Combine(ModuleDirectory, "..", "..", "ThirdParty"));
string HeaderPath = Path.GetFullPath(Path.Combine(ThirdPartyPath, "Include"));
PublicIncludePaths.AddRange(new string[] { Path.Combine(HeaderPath, "websocketpp") });
PublicIncludePaths.AddRange(new string[] { Path.Combine(HeaderPath, "asio") });
PublicIncludePaths.AddRange(new string[] { Path.Combine(ThirdPartyPath, "quickjs", "Inc") });

PublicIncludePaths.AddRange(new string[] { Path.Combine(ModuleDirectory, "..", "..", "ThirdParty", "quickjs", "Inc") });

string LibraryPath = Path.GetFullPath(Path.Combine(ModuleDirectory, "..", "..", "ThirdParty", "quickjs", "Lib"));
string LibraryPath = Path.GetFullPath(Path.Combine(ThirdPartyPath, "quickjs", "Lib"));
if (Target.Platform == UnrealTargetPlatform.Win64)
{
string V8LibraryPath = Path.Combine(LibraryPath, "Win64MD");
Expand Down
Loading

0 comments on commit da22c09

Please sign in to comment.