Skip to content

Commit

Permalink
fix worker compile error on some platform
Browse files Browse the repository at this point in the history
  • Loading branch information
hehao committed Nov 30, 2023
1 parent 2c5201a commit aed90a1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ################################
Expand Down
40 changes: 21 additions & 19 deletions native/cocos/bindings/manual/Worker.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Created by [email protected] on 2023/5/31.
* Worker on Native (only v8)
* Worker on Native
*/

#include <chrono>
Expand All @@ -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 <cocos/base/Macros.h>
#include <cocos/base/Log.h>
#include <cocos/platform/FileUtils.h>
#include <cocos/bindings/jswrapper/config.h>

#define __NANOSECONDS_PER_SECOND 1000000000
#define __NANOSECONDS_60FPS 16666667L
Expand Down Expand Up @@ -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<uintptr_t, Worker*> workers;
Expand Down Expand Up @@ -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<v8::Context> context, v8::Local<v8::Object> obj,const std::string &key, v8::Local<v8::Value> value ){
v8::MaybeLocal<v8::String> maybeKey = v8::String::NewFromUtf8(isolate, key.c_str(), v8::NewStringType::kNormal);
if (maybeKey.IsEmpty()) {
return;
}
obj->Set(context, v8::Local<v8::Name>::Cast(maybeKey.ToLocalChecked()),value);
}
/***
* postMessage To worker thread
* @param isolate
Expand All @@ -344,11 +345,11 @@ namespace ccex {
v8::Local<v8::String> json = v8::String::NewFromUtf8(isolate, message.c_str()).ToLocalChecked();
v8::Local<v8::Value> value = v8::JSON::Parse(context, json).ToLocalChecked();
if (value->IsObject()) {
v8::Local<v8::Object> object = value->ToObject(context).ToLocalChecked();
v8::Local<v8::Object> params = value->ToObject(context).ToLocalChecked();
v8::Local<v8::String> lastEventId = v8::String::NewFromUtf8(isolate, worker->getEventID().c_str(),
v8::NewStringType::kNormal).ToLocalChecked();
SET_PROPERTY(object, "lastEventId",lastEventId);
std::vector<v8::Local<v8::Value>> argv = {object};
SetProperty(isolate,context, params, "lastEventId",lastEventId );
std::vector<v8::Local<v8::Value>> argv = {params};
callFunction(isolate, context, "onmessage", argv);
}
}
Expand All @@ -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<v8::String> 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);
Expand Down Expand Up @@ -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(){
}
Expand Down
4 changes: 2 additions & 2 deletions native/cocos/bindings/manual/jsb_worker_maunal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion native/tests/sebind-tests/cfg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
2 changes: 1 addition & 1 deletion native/tests/sebind-tests/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aed90a1

Please sign in to comment.