diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 9dde3405e1d..6dff5eb7a09 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -177,7 +177,7 @@ if(CI_FORCE_MINIMAL_FEATURES) set(USE_DEBUG_RENDERER OFF) set(USE_GEOMETRY_RENDERER OFF) set(USE_WEBP OFF) - set(USE_WORKER OFF) + set(USE_WORKER ON) endif() ################################# external source code ################################ diff --git a/native/cocos/bindings/manual/Worker.cpp b/native/cocos/bindings/manual/Worker.cpp index 254d4c370b0..858ceed12de 100644 --- a/native/cocos/bindings/manual/Worker.cpp +++ b/native/cocos/bindings/manual/Worker.cpp @@ -1,6 +1,6 @@ /** * Created by ihowe@outlook.com on 2023/5/31. - * Worker on Native (only v8) + * Worker on Native */ #include @@ -10,10 +10,10 @@ #include "Worker.h" // include cocos -#include "cocos/base/Macros.h" -#include "cocos/base/Log.h" -#include "cocos/platform/FileUtils.h" -#include "cocos/bindings/jswrapper/config.h" +#include +#include +#include +#include #define __NANOSECONDS_PER_SECOND 1000000000 #define __NANOSECONDS_60FPS 16666667L @@ -47,13 +47,6 @@ root.setTimeout=function(cb){return createTimeoutInfo(arguments,false);};root.cl *valptr.assign(*utf8); \ } while (0) -#define SET_PROPERTY(object, key, jsval) \ - object->Set( \ - context, \ - v8::String::NewFromUtf8(isolate, key, v8::NewStringType::kNormal) \ - .ToLocalChecked(), \ - jsval); - namespace ccex { namespace helper { std::unordered_map workers; @@ -329,6 +322,14 @@ namespace ccex { v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked(); that->Set(name_string, t); } + + void SetProperty(v8::Isolate* isolate,v8::Local context, v8::Local obj,const std::string &key, v8::Local value ){ + v8::MaybeLocal maybeKey = v8::String::NewFromUtf8(isolate, key.c_str(), v8::NewStringType::kNormal); + if (maybeKey.IsEmpty()) { + return; + } + obj->Set(context, v8::Local::Cast(maybeKey.ToLocalChecked()),value); + } /*** * postMessage To worker thread * @param isolate @@ -344,11 +345,11 @@ namespace ccex { v8::Local json = v8::String::NewFromUtf8(isolate, message.c_str()).ToLocalChecked(); v8::Local value = v8::JSON::Parse(context, json).ToLocalChecked(); if (value->IsObject()) { - v8::Local object = value->ToObject(context).ToLocalChecked(); + v8::Local params = value->ToObject(context).ToLocalChecked(); v8::Local lastEventId = v8::String::NewFromUtf8(isolate, worker->getEventID().c_str(), v8::NewStringType::kNormal).ToLocalChecked(); - SET_PROPERTY(object, "lastEventId",lastEventId); - std::vector> argv = {object}; + SetProperty(isolate,context, params, "lastEventId",lastEventId ); + std::vector> argv = {params}; callFunction(isolate, context, "onmessage", argv); } } @@ -360,10 +361,11 @@ namespace ccex { USE_HANDLE_SCOPE; auto context = isolate->GetCurrentContext(); auto params = v8::Object::New(isolate); - SET_PROPERTY(params, "data", args[0]); - v8::Local lastEventId = v8::String::NewFromUtf8(isolate, worker->getEventID().c_str(), + auto tag_data = v8::String::NewFromUtf8(isolate, "data",v8::NewStringType::kNormal).ToLocalChecked(); + SetProperty(isolate,context, params, "data",args[0] ); + auto lastEventId = v8::String::NewFromUtf8(isolate, worker->getEventID().c_str(), v8::NewStringType::kNormal).ToLocalChecked(); - SET_PROPERTY(params, "lastEventId",lastEventId); + SetProperty(isolate,context, params, "lastEventId",lastEventId ); std::string message; CONVERT_TO_STRING(v8::JSON::Stringify(context, params).ToLocalChecked(), &message); @@ -596,7 +598,7 @@ namespace ccex{ void Worker::start(){ } Worker::~Worker(){ - CC_LOG_INFO("[worker](ID#%d) Descontructor",id); + CC_LOG_INFO("[worker] descontructor (ID#%d)",id); } void Worker::terminate(){ } diff --git a/native/cocos/bindings/manual/jsb_worker_maunal.cpp b/native/cocos/bindings/manual/jsb_worker_maunal.cpp index 92fa9e391cd..ce9efc16430 100644 --- a/native/cocos/bindings/manual/jsb_worker_maunal.cpp +++ b/native/cocos/bindings/manual/jsb_worker_maunal.cpp @@ -50,7 +50,7 @@ namespace { if (!_scheduler || !workerImpObj) { return; } - // CC_LOG_DEBUG("worker : postMessageToGame workerid = %d",id); + // CC_LOG_DEBUG("worker : postMessageToGame workerid = %d",id); auto func = [id,type,data,this]() { se::AutoHandleScope hs; // fix v8 if (globalMessage.isUndefined() && workerImpObj) { @@ -175,7 +175,7 @@ bool jsb_register_worker(se::Object* g){ workerImpObj->defineFunction("js_constructor", _SE(js_worker_constructor)); workerImpObj->defineFunction("js_postMessage", _SE(js_worker_postMessage)); workerImpObj->defineFunction("js_terminate", _SE(js_worker_terminate)); - se->evalString(workerjs.c_str(), workerjs.size(), nullptr,"worker.js"); + se->evalString(workerjs.c_str(), (uint32_t)(workerjs.size()), nullptr,"worker.js"); se->addBeforeCleanupHook([]() { ccex::Worker::destroyAll(); delete _workerImp; diff --git a/native/tests/sebind-tests/cfg.cmake b/native/tests/sebind-tests/cfg.cmake index c36935be67e..1553983e61e 100644 --- a/native/tests/sebind-tests/cfg.cmake +++ b/native/tests/sebind-tests/cfg.cmake @@ -17,5 +17,5 @@ set(USE_VIDEO ON) set(USE_WEBVIEW ON) set(USE_SPINE ON) set(USE_DRAGONBONES ON) -set(USE_WORKER ON) +set(USE_WORKER OFF) set(CC_EXECUTABLE_NAME "") diff --git a/native/tests/sebind-tests/common/CMakeLists.txt b/native/tests/sebind-tests/common/CMakeLists.txt index 6da951945a6..57607538fa9 100644 --- a/native/tests/sebind-tests/common/CMakeLists.txt +++ b/native/tests/sebind-tests/common/CMakeLists.txt @@ -23,7 +23,7 @@ option(USE_WEBSOCKET_SERVER "Enable WebSocket Server" OFF) option(USE_JOB_SYSTEM_TASKFLOW "Use taskflow as job system backend" OFF) option(USE_JOB_SYSTEM_TBB "Use tbb as job system backend" OFF) option(USE_PHYSICS_PHYSX "USE PhysX Physics" ON) -option(USE_WORKER "Enable Worker" ON) +option(USE_WORKER "Enable Worker" OFF) if(NOT RES_DIR) message(FATAL_ERROR "RES_DIR is not set!") diff --git a/native/tools/simulator/frameworks/runtime-src/CMakeLists.txt b/native/tools/simulator/frameworks/runtime-src/CMakeLists.txt index 6210799b40b..131836e1664 100644 --- a/native/tools/simulator/frameworks/runtime-src/CMakeLists.txt +++ b/native/tools/simulator/frameworks/runtime-src/CMakeLists.txt @@ -21,7 +21,7 @@ option(USE_MIDDLEWARE "Enable Middleware" ON) option(USE_DRAGONBONES "Enable Dragonbones" ON) option(USE_SPINE "Enable Spine" ON) option(USE_PHYSICS_PHYSX "USE PhysX Physics" ON) -option(USE_WORKER "Enable Worker" ON) +option(USE_WORKER "Enable Worker" OFF) if(USE_SE_JSC AND APPLE) set(USE_SE_V8 OFF)