JS \u65ad\u70b9\u7684\u529f\u80fd\u76f8\u4fe1\u5927\u5bb6\u90fd\u7528\u8fc7\uff0c\u5f53\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\uff0c\u7136\u540e\u4ee3\u7801\u6267\u884c\u5230\u8fd9\u4e2a\u65ad\u70b9\u65f6\uff0c\u7ebf\u7a0b\u5c31\u4f1a\u505c\u4f4f\uff0c\u7136\u540e\u6211\u4eec\u70b9\u51fb\u4e0b\u4e00\u6b65\u7684\u65f6\u5019\uff0c\u53c8\u4f1a\u518d\u4e0b\u4e00\u4e2a\u65ad\u70b9\u505c\u4f4f\u3002\u90a3\u4e48\u8fd9\u4e2a\u505c\u4f4f\u5230\u5e95\u610f\u5473\u7740\u4ec0\u4e48\u5462\uff1f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u65ad\u70b9\u7684\u5b9e\u73b0\u975e\u5e38\u590d\u6742\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u8bf4\u8981\u957f\u7bc7\u5927\u8bba\u8bb2\u89e3 JS \u65ad\u70b9\u5728 V8 \u4e2d\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u800c\u662f\u60f3\u4ece\u5b8f\u89c2\u4e0a\u804a\u4e00\u4e0b\u65ad\u70b9\u7684\u5b9e\u73b0\u3002\u8fd9\u4e2a\u95ee\u9898\u6765\u6e90\u4e8e\u6700\u8fd1\u548c\u540c\u4e8b\u8ba8\u8bba\u7684\u5173\u4e8e V8 Inspector \u5b9e\u73b0\u7684\u4e00\u4e9b\u4e8b\u60c5\u3002<\/p>\n
JS \u65ad\u70b9\u7684\u529f\u80fd\u76f8\u4fe1\u5927\u5bb6\u90fd\u7528\u8fc7\uff0c\u5f53\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\uff0c\u7136\u540e\u4ee3\u7801\u6267\u884c\u5230\u8fd9\u4e2a\u65ad\u70b9\u65f6\uff0c\u7ebf\u7a0b\u5c31\u4f1a\u505c\u4f4f\uff0c\u7136\u540e\u6211\u4eec\u70b9\u51fb\u4e0b\u4e00\u6b65\u7684\u65f6\u5019\uff0c\u53c8\u4f1a\u518d\u4e0b\u4e00\u4e2a\u65ad\u70b9\u505c\u4f4f\u3002\u90a3\u4e48\u8fd9\u4e2a\u505c\u4f4f\u5230\u5e95\u610f\u5473\u7740\u4ec0\u4e48\u5462\uff1f\u4e0b\u9762\u8fd9\u4e2a\u56fe\u662f\u6267\u884c\u5230\u4e00\u4e2a\u65ad\u70b9\u65f6 Node.js \u7684\u8c03\u7528\u6808\u3002<\/p>\n
<\/p>\n
\u6211\u4eec\u77e5\u9053 V8 \u6709\u4e00\u4e2a\u8c03\u8bd5\u534f\u8bae\uff0c\u5ba2\u6237\u7aef\u662f\u548c V8 \u901a\u8fc7\u8fd9\u4e2a\u534f\u8bae\u901a\u4fe1\u5b8c\u6210\u8c03\u8bd5\u7684\uff0c\u5f53 V8 \u6536\u5230\u5ba2\u6237\u7aef\u7684\u4fe1\u606f\u5e76\u4e14\u5904\u7406\u5b8c\u4e4b\u540e\uff0c\u5c31\u4f1a\u8c03\u7528 runMessageLoopOnPause\u3002runMessageLoopOnPause \u662f V8 \u63d0\u4f9b\u7684\u4e00\u4e2a\u7ea6\u5b9a\u7684 API\uff0c\u5f53\u6267\u884c\u5230 JS \u65ad\u70b9\u65f6\u5c31\u4f1a\u8c03\u7528\uff0c\u5177\u4f53\u5728 runMessageLoopOnPause \u91cc\u505a\u4ec0\u4e48\u4e8b\u60c5\u7531 V8 \u7684\u4f7f\u7528\u65b9\u5b9e\u73b0\u3002\u5728\u770b\u5b9e\u73b0\u4e4b\u524d\uff0c\u5148\u6765\u601d\u8003\u4e00\u4e0b\uff0c\u5e94\u8be5\u600e\u4e48\u5904\u7406\u3002\u9996\u5148\u6267\u884c\u5230\u4e86 JS \u65ad\u70b9\uff0c\u663e\u7136\u7ebf\u7a0b\u5c31\u8981\u8fdb\u5165\u505c\u4f4f\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u8fd9\u4e2a\u505c\u4f4f\u7684\u72b6\u6001\u5177\u4f53\u662f\u6307\u4ec0\u4e48\uff0c\u5e94\u8be5\u600e\u4e48\u5b9e\u73b0\u662f\u4e00\u4e2a\u6700\u5173\u952e\u7684\u95ee\u9898\u3002\u8fd9\u4e2a\u4e8b\u4ef6\u5faa\u73af\u7684\u5b9e\u73b0\u6709\u70b9\u7c7b\u4f3c\uff0c\u90a3\u5c31\u662f\u5f53\u7ebf\u7a0b\u6ca1\u6709\u4efb\u52a1\u5904\u7406\u7684\u65f6\u5019\uff0c\u5b83\u5e94\u8be5\u5728\u505a\u4ec0\u4e48\uff0c\u8f6e\u8be2\u663e\u7136\u592a\u4e0d\u53ef\u601d\u8bae\u4e86\uff0c\u90a3\u53e6\u4e00\u79cd\u5c31\u662f\u57fa\u4e8e\u8ba2\u9605 \/ \u53d1\u5e03\u673a\u5236\u5b9e\u73b0\u7761\u7720 \/ \u5524\u9192\uff0c\u6bd4\u5982 Node.js \u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u6a21\u5757\u5b9e\u73b0\u4e86\u7761\u7720 \/ \u5524\u9192\u673a\u5236\u3002\u7c7b\u4f3c\u7684 Inspector \u4e5f\u662f\u8fd9\u6837\u5b9e\u73b0\uff0c\u4f46\u662f\u5177\u4f53\u7ec6\u8282\u4e0d\u4e00\u6837\uff0c\u56e0\u4e3a\u5982\u679c\u60c5\u51b5\u4e0d\u4e00\u6837\uff0c\u5f53 Node.js \u5904\u4e8e\u4e8b\u4ef6\u5faa\u73af\u7684\u963b\u585e\u72b6\u6001\u65f6\uff0c\u4efb\u4f55\u6ce8\u518c\u5230\u4e8b\u4ef6\u9a71\u52a8\u6a21\u5757\u7684\u4e8b\u4ef6\u90fd\u53ef\u4ee5\u5524\u9192 Node.js\uff0c\u4f46\u662f\u65ad\u70b9\u4e0d\u4e00\u6837\uff0c\u5f53\u7ebf\u7a0b\u5904\u4e8e\u65ad\u70b9\u65f6\uff0c\u9664\u4e86\u4fe1\u53f7\u5916\uff0c\u4e00\u822c\u7684\u4efb\u52a1\uff0c\u6bd4\u5982\u6587\u4ef6 IO\u3001\u7f51\u7edc IO \u7b49\uff0c\u662f\u4e0d\u80fd\u4e5f\u4e0d\u5e94\u8be5\u80fd\u5524\u9192\u7ebf\u7a0b\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u7b80\u5355\u7684\u7761\u7720 \/ \u5524\u9192\u65b9\u5f0f\uff0c\u90a3\u5c31\u662f\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u7ebf\u7a0b\u963b\u585e\u4e8e\u6761\u4ef6\u53d8\u91cf\u65f6\uff0c\u53ea\u6709\u901a\u8fc7\u8be5\u6761\u4ef6\u53d8\u91cf\u624d\u80fd\u5524\u9192\u7ebf\u7a0b\u3002\u56de\u5230\u65ad\u70b9\u7684\u573a\u666f\uff0c\u90a3\u5c31\u662f\u5ba2\u6237\u7aef\u7ee7\u7eed\u6267\u884c\u65f6\u624d\u80fd\u5524\u9192\u7ebf\u7a0b\u3002<\/p>\n
\u5206\u6790\u5b8c\u4e4b\u540e\uff0c\u6765\u770b\u770b Node.js \u7684\u5b9e\u73b0\u3002<\/p>\n
\r\nvoid runMessageLoopOnPause(int context_group_id) override {\r\n waiting_for_resume_ = true;\r\n runMessageLoop();\r\n}\r\n\r\nvoid runMessageLoop() {\r\n if (running_nested_loop_)\r\n return;\r\n\r\n running_nested_loop_ = true;\r\n\r\n while (shouldRunMessageLoop()) {\r\n if (interface_) interface_->WaitForFrontendEvent();\r\n env_->RunAndClearInterrupts();\r\n }\r\n running_nested_loop_ = false;\r\n}\r\n<\/pre>\n\u91cd\u70b9\u5728 WaitForFrontendEvent\u3002<\/p>\n
\r\nbool MainThreadInterface::WaitForFrontendEvent() {\r\n dispatching_messages_ = false;\r\n \/\/ \u4efb\u52a1\u961f\u5217\u4e3a\u7a7a\u5219\u963b\u585e\r\n if (dispatching_message_queue_.empty()) {\r\n Mutex::ScopedLock scoped_lock(requests_lock_);\r\n while (requests_.empty()) incoming_message_cond_.Wait(scoped_lock);\r\n }\r\n return true;\r\n}\r\n<\/pre>\n\u6211\u4eec\u5047\u8bbe\u8fd9\u65f6\u5019\u961f\u5217\u4e3a\u7a7a\uff0c\u90a3\u4e48\u7ebf\u7a0b\u5c31\u4f1a\u963b\u585e\u5728\u6761\u4ef6\u53d8\u91cf incoming_message_cond_ \u4e2d\u3002\u63a5\u4e0b\u6765\u770b\u770b\u5982\u804a\u804a\u7b2c\u4e8c\u4e2a\u95ee\u9898\u3002\u7ebf\u7a0b\u8fd9\u65f6\u5019\u963b\u585e\u4e86\uff0c\u90a3\u4e48\u5ba2\u6237\u7aef\u70b9\u51fb\u6267\u884c\u4e0b\u4e00\u6b65\u7684\u65f6\u5019\uff0cNode.js \u8fd8\u8fd8\u600e\u4e48\u5904\u7406\uff1f\u8fd9\u91cc\u5c31\u9700\u8981\u5b50\u7ebf\u7a0b\u5e2e\u5fd9\u4e86\uff0c\u6240\u4ee5 Node.js \u4e2d\uff0c\u548c\u5ba2\u6237\u7aef\u7684\u6570\u636e\u901a\u4fe1\u662f\u5728\u5b50\u7ebf\u7a0b\u5b8c\u6210\u7684\uff0c\u4e0d\u8bb2\u592a\u591a\u4ee3\u7801\u548c\u7ec6\u8282\uff0c\u76f4\u63a5\u770b\u4e00\u4e2a\u8c03\u7528\u6808\u3002<\/p>\n
<\/p>\n
\u8fd9\u662f\u5ba2\u6237\u7aef\u548c Node.js \u5b50\u7ebf\u7a0b\u5efa\u7acb websocket \u8fde\u63a5\u6210\u529f\u540e\u7684\u8c03\u7528\u6808\uff0c\u540e\u7eed\u7684\u6570\u636e\u901a\u4fe1\u4e5f\u662f\u7c7b\u4f3c\u3002\u6765\u770b\u4e00\u4e0b Post\u3002<\/p>\n
\r\nvoid MainThreadInterface::Post(std::unique_ptr request) {\r\n Mutex::ScopedLock scoped_lock(requests_lock_);\r\n bool needs_notify = requests_.empty();\r\n requests_.push_back(std::move(request));\r\n if (needs_notify) {\r\n std::weak_ptr weak_self {shared_from_this()};\r\n agent_->env()->RequestInterrupt([weak_self](Environment*) {\r\n if (auto iface = weak_self.lock()) iface->DispatchMessages();\r\n });\r\n }\r\n incoming_message_cond_.Broadcast(scoped_lock);\r\n}\r\n<\/mainthreadinterface><\/request><\/pre>\n\u8fd9\u91cc\u770b\u5230\u4e86\u521a\u624d\u719f\u6089\u7684\u6570\u636e\u7ed3\u6784\uff0cPost \u5c31\u662f\u5f80\u4e3b\u7ebf\u7a0b\u4e2d\u63d2\u5165\u4e00\u4e2a\u4efb\u52a1\uff0c\u7136\u540e\u5524\u9192\u4e3b\u7ebf\u7a0b\u3002\u63a5\u7740\u56de\u5230 runMessageLoop\u3002<\/p>\n
\r\nwhile (shouldRunMessageLoop()) {\r\n if (interface_) interface_->WaitForFrontendEvent();\r\n env_->RunAndClearInterrupts();\r\n}\r\n<\/pre>\nWaitForFrontendEvent \u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u63a5\u7740\u6267\u884c RunAndClearInterrupts\uff0cRunAndClearInterrupts \u6b63\u662f\u5904\u7406 RequestInterrupt \u63d2\u5165\u7684\u4efb\u52a1\u7684\u3002\u521a\u624d\u63d2\u5165\u4efb\u52a1\u65f6\u6211\u4eec\u770b\u5230\u63d2\u5165\u4e86\u4e24\u4e2a\u4efb\u52a1 agent_->env()->RequestInterrupt \u548c requests_.push_back(std::move(request)) \uff0cRequestInterrupt \u63d2\u5165\u7684\u4efb\u52a1\u4e2d\u4f1a\u8c03\u7528 DispatchMessages\uff0c\u800c DispatchMessages \u5c31\u662f\u5904\u7406 requests_ \u4e2d\u7684\u4efb\u52a1\u7684\u3002<\/p>\n
\r\nvoid MainThreadInterface::DispatchMessages() {\r\n dispatching_messages_ = true;\r\n bool had_messages = false;\r\n do {\r\n if (dispatching_message_queue_.empty()) {\r\n Mutex::ScopedLock scoped_lock(requests_lock_);\r\n requests_.swap(dispatching_message_queue_);\r\n }\r\n had_messages = !dispatching_message_queue_.empty();\r\n while (!dispatching_message_queue_.empty()) {\r\n MessageQueue::value_type task;\r\n std::swap(dispatching_message_queue_.front(), task);\r\n dispatching_message_queue_.pop_front();\r\n\r\n v8::SealHandleScope seal_handle_scope(agent_->env()->isolate());\r\n task->Call(this);\r\n }\r\n } while (had_messages);\r\n dispatching_messages_ = false;\r\n}\r\n<\/pre>\n\u6267\u884c\u4efb\u52a1\u7684\u65f6\u5019\uff0c\u5177\u4f53\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u628a\u5ba2\u6237\u7aef\u4f20\u8fc7\u6765\u7684\u6570\u636e\u6295\u4f20\u7ed9 V8 Inspector\uff0c\u5982\u679c\u53c8\u6267\u884c\u5230\u4e86\u4e00\u4e2a\u65ad\u70b9\uff0c\u90a3\u4e48\u7ee7\u7eed\u672c\u6587\u5206\u6790\u5230\u8fd9\u4e2a\u903b\u8f91\uff0c\u5426\u5219\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u7ee7\u7eed\u8dd1\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u65ad\u70b9\u7684\u5b9e\u73b0\u975e\u5e38\u590d\u6742\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u8bf4\u8981\u957f\u7bc7\u5927\u8bba\u8bb2\u89e3 JS \u65ad\u70b9\u5728 V8 \u4e2d\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u800c\u662f\u60f3\u4ece\u5b8f\u89c2\u4e0a\u804a\u4e00\u4e0b\u65ad\u70b9\u7684 […]<\/p>\n","protected":false},"author":1898,"featured_media":254260,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[937],"class_list":["post-254255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-js"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/254255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=254255"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/254255\/revisions"}],"predecessor-version":[{"id":254259,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/254255\/revisions\/254259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/254260"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=254255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=254255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=254255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}