-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into refactor/remove_host_…
…class
- Loading branch information
Showing
34 changed files
with
583 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2021 Alibaba Inc. All rights reserved. | ||
* Author: Kraken Team. | ||
*/ | ||
|
||
#include "rejected_promises.h" | ||
#include "executing_context.h" | ||
|
||
namespace kraken::binding::qjs { | ||
|
||
RejectedPromises::Message::Message(ExecutionContext* context, JSValue promise, JSValue reason) | ||
: m_runtime(context->runtime()), m_promise(JS_DupValue(context->ctx(), promise)), m_reason(JS_DupValue(context->ctx(), reason)) {} | ||
|
||
RejectedPromises::Message::~Message() { | ||
JS_FreeValueRT(m_runtime, m_promise); | ||
JS_FreeValueRT(m_runtime, m_reason); | ||
} | ||
|
||
void RejectedPromises::trackUnhandledPromiseRejection(ExecutionContext* context, JSValue promise, JSValue reason) { | ||
void* ptr = JS_VALUE_GET_PTR(promise); | ||
if (m_unhandledRejections.count(ptr) == 0) { | ||
m_unhandledRejections[ptr] = std::make_unique<Message>(context, promise, reason); | ||
} | ||
// One promise will never have more than one unhandled rejection. | ||
} | ||
|
||
void RejectedPromises::trackHandledPromiseRejection(ExecutionContext* context, JSValue promise, JSValue reason) { | ||
void* ptr = JS_VALUE_GET_PTR(promise); | ||
|
||
// Unhandled promise are handled in a sync script call. It's file so we remove the recording of this promise. | ||
if (m_unhandledRejections.count(ptr) > 0) { | ||
m_unhandledRejections.erase(ptr); | ||
} else { | ||
// This promise are handled in the next script call, we save this operation to trigger handledRejection event. | ||
m_reportHandledRejection.push_back(std::make_unique<Message>(context, promise, reason)); | ||
} | ||
} | ||
|
||
void RejectedPromises::process(ExecutionContext* context) { | ||
// Copy m_unhandledRejections to avoid endless recursion call. | ||
std::unordered_map<void*, std::unique_ptr<Message>> unhandledRejections; | ||
for (auto& entry : m_unhandledRejections) { | ||
unhandledRejections[entry.first] = std::unique_ptr<Message>(m_unhandledRejections[entry.first].release()); | ||
} | ||
m_unhandledRejections.clear(); | ||
|
||
// Copy m_reportHandledRejection to avoid endless recursion call. | ||
std::vector<std::unique_ptr<Message>> reportHandledRejection; | ||
reportHandledRejection.reserve(reportHandledRejection.size()); | ||
for (auto& entry : m_reportHandledRejection) { | ||
reportHandledRejection.push_back(std::unique_ptr<Message>(entry.release())); | ||
} | ||
m_reportHandledRejection.clear(); | ||
|
||
// Dispatch unhandled rejectionEvents. | ||
for (auto& entry : unhandledRejections) { | ||
context->reportError(entry.second->m_reason); | ||
context->dispatchGlobalUnhandledRejectionEvent(context, entry.second->m_promise, entry.second->m_reason); | ||
} | ||
|
||
// Dispatch handledRejection events. | ||
for (auto& entry : reportHandledRejection) { | ||
context->dispatchGlobalRejectionHandledEvent(context, entry->m_promise, entry->m_reason); | ||
} | ||
} | ||
|
||
} // namespace kraken::binding::qjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2021 Alibaba Inc. All rights reserved. | ||
* Author: Kraken Team. | ||
*/ | ||
|
||
#ifndef KRAKENBRIDGE_BINDINGS_QJS_REJECTED_PROMISES_H_ | ||
#define KRAKENBRIDGE_BINDINGS_QJS_REJECTED_PROMISES_H_ | ||
|
||
#include <quickjs/quickjs.h> | ||
#include <memory> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
namespace kraken::binding::qjs { | ||
|
||
class ExecutionContext; | ||
|
||
class RejectedPromises { | ||
public: | ||
class Message { | ||
public: | ||
Message(ExecutionContext* context, JSValue promise, JSValue reason); | ||
~Message(); | ||
|
||
JSRuntime* m_runtime{nullptr}; | ||
JSValue m_promise{JS_NULL}; | ||
JSValue m_reason{JS_NULL}; | ||
}; | ||
|
||
// Keeping track unhandled promise rejection in current context, and throw unhandledRejection error | ||
void trackUnhandledPromiseRejection(ExecutionContext* context, JSValue promise, JSValue reason); | ||
// When unhandled promise are handled in the future, should trigger a handledRejection event. | ||
void trackHandledPromiseRejection(ExecutionContext* context, JSValue promise, JSValue reason); | ||
// Trigger events after promise executed. | ||
void process(ExecutionContext* context); | ||
|
||
private: | ||
std::unordered_map<void*, std::unique_ptr<Message>> m_unhandledRejections; | ||
std::vector<std::unique_ptr<Message>> m_reportHandledRejection; | ||
}; | ||
|
||
} // namespace kraken::binding::qjs | ||
|
||
#endif // KRAKENBRIDGE_BINDINGS_QJS_REJECTED_PROMISES_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.