Skip to content

Commit

Permalink
fix(core): support onerror for js
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao committed Sep 5, 2023
1 parent 300f114 commit 8b70d7d
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 94 deletions.
9 changes: 2 additions & 7 deletions driver/js/include/driver/vm/js_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,8 @@ class VM {
return uncaught_exception_callback_;
}

static void HandleUncaughtException(const std::shared_ptr<Ctx>& ctx, const std::shared_ptr<CtxValue>& exception);
static void HandleError(const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue>& event,
const std::shared_ptr<CtxValue>& source,
const std::shared_ptr<CtxValue>& lineno,
const std::shared_ptr<CtxValue>& colno,
const std::shared_ptr<CtxValue>& error);
static void HandleException(const std::shared_ptr<Ctx>& ctx, const string_view& event_name, const std::shared_ptr<CtxValue>& exception);

virtual std::shared_ptr<CtxValue> ParseJson(const std::shared_ptr<Ctx>& ctx, const string_view& json) = 0;
virtual std::shared_ptr<Ctx> CreateContext() = 0;
private:
Expand Down
15 changes: 10 additions & 5 deletions driver/js/lib/global/Others.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ function emit(eventName, ...args) {
let isErr = eventName === 'error';
let errObj = new Error();
if (isErr) {
if (args.length !== 5) {
throw new TypeError('Hippy.emit error only accept 5 params');
let arr = args[0];
if (!(arr instanceof Array)) {
throw new TypeError('Hippy.emit() error event, args0 must be array');
}
errObj.message = JSON.stringify(args[4]);
if (arr.length !== 5) {
throw new TypeError('Hippy.emit() error event, args0 length must be 5');
}
errObj.message = JSON.stringify(arr[4]);
if (Hippy.onerror) {
Hippy.onerror(args[0], args[1], args[2], args[3], errObj);
Hippy.onerror(arr[0], arr[1], arr[2], arr[3], errObj);
}
}

Expand All @@ -131,7 +135,8 @@ function emit(eventName, ...args) {
}
try {
if (isErr) {
let event = new ErrorEvent(args[0], args[1], args[2], args[3], errObj);
let arr = args[0];
let event = new ErrorEvent(arr[0], arr[1], arr[2], arr[3], errObj);
eventListeners.forEach(listener => listener(event));
} else {
eventListeners.forEach(listener => listener(...args));
Expand Down
28 changes: 0 additions & 28 deletions driver/js/lib/modules/ErrorHandle.js

This file was deleted.

6 changes: 3 additions & 3 deletions driver/js/lib/modules/ExceptionHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
* limitations under the License.
*/

(function exceptionHandler(eventName, err) {
(function exceptionHandler(eventName, exception) {
if (global.Hippy) {
global.Hippy.emit('uncaughtException', err);
global.Hippy.emit(eventName, exception);
} else {
/* eslint-disable-next-line no-console */
console.error(eventName, err);
console.error(eventName, exception);
}
});
3 changes: 1 addition & 2 deletions driver/js/scripts/build-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ function getAllRequiredFiles(platform) {
getAbsolutePath('../lib/bootstrap.js'),
getAbsolutePath(`../lib/entry/${platform}/hippy.js`),
getAbsolutePath('../lib/modules/ExceptionHandle.js'),
getAbsolutePath('../lib/modules/ErrorHandle.js'),
];

rl.on('line', (line) => {
Expand Down Expand Up @@ -242,7 +241,7 @@ function generateCpp(platform, buildDirPath) {
for (let i = 0; i < fileBuffer.length; i += 1) {
byteArr.push(fileBuffer[i]);
}
if (fileName === 'bootstrap' || fileName === 'ExceptionHandle' || fileName === 'ErrorHandle') {
if (fileName === 'bootstrap' || fileName === 'ExceptionHandle') {
code += `
const uint8_t k_${fileName}[] = { ${byteArr.join(',')},0 }; // NOLINT`;
} else {
Expand Down
2 changes: 1 addition & 1 deletion driver/js/src/js_driver_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void AsyncInitializeEngine(const std::shared_ptr<Engine>& engine,
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto exception = info[0];
V8VM::HandleUncaughtException(scope->GetContext(), exception);
V8VM::HandleException(scope->GetContext(), "uncaughtException", exception);
auto engine = scope->GetEngine().lock();
FOOTSTONE_CHECK(engine);
auto callback = engine->GetVM()->GetUncaughtExceptionCallback();
Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ void Scope::HandleUriLoaderError(const string_view& uri, const int32_t ret_code,
auto lineno = context_->CreateNumber(0);
auto colno = context_->CreateNumber(0);
auto error = context_->CreateObject(error_map);
VM::HandleError(context_, event, source, lineno, colno, error);
std::shared_ptr<CtxValue> arr[5] = {event, source, lineno, colno, error};
auto exception = context_->CreateArray(5, arr);
VM::HandleException(context_, "error", exception);
}

}
Expand Down
39 changes: 4 additions & 35 deletions driver/js/src/vm/js_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ namespace hippy {
inline namespace driver {
inline namespace vm {

constexpr char kEventName[] = "uncaughtException";
constexpr char kExceptionHandlerJSName[] = "ExceptionHandle.js";
constexpr char kHippyExceptionHandlerName[] = "HippyExceptionHandler";

constexpr char kErrorHandlerJSName[] = "ErrorHandle.js";
constexpr char kHippyErrorHandlerName[] = "HippyErrorHandler";

using Ctx = hippy::Ctx;
using CtxValue = hippy::CtxValue;

void VM::HandleUncaughtException(const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue>& exception) {
void VM::HandleException(const std::shared_ptr<Ctx>& ctx,
const string_view& event_name,
const std::shared_ptr<CtxValue>& exception) {
auto global_object = ctx->GetGlobalObject();
string_view error_handle_name(kHippyExceptionHandlerName);
auto error_handle_key = ctx->CreateString(error_handle_name);
Expand All @@ -55,7 +52,7 @@ void VM::HandleUncaughtException(const std::shared_ptr<Ctx>& ctx,
}

std::shared_ptr<CtxValue> argv[2];
argv[0] = ctx->CreateString(kEventName);
argv[0] = ctx->CreateString(event_name);
argv[1] = exception;

auto try_catch = CreateTryCatchScope(true, ctx);
Expand All @@ -66,34 +63,6 @@ void VM::HandleUncaughtException(const std::shared_ptr<Ctx>& ctx,
}
}

void VM::HandleError(const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue>& event,
const std::shared_ptr<CtxValue>& source,
const std::shared_ptr<CtxValue>& lineno,
const std::shared_ptr<CtxValue>& colno,
const std::shared_ptr<CtxValue>& error) {
auto global_object = ctx->GetGlobalObject();
string_view error_handle_name(kHippyErrorHandlerName);
auto error_handle_key = ctx->CreateString(error_handle_name);
auto error_handler = ctx->GetProperty(global_object, error_handle_key);
if (!ctx->IsFunction(error_handler)) {
const auto& source_code = hippy::GetNativeSourceCode(kErrorHandlerJSName);
FOOTSTONE_DCHECK(source_code.data_ && source_code.length_);
string_view str_view(source_code.data_, source_code.length_);
error_handler = ctx->RunScript(str_view, error_handle_name);
ctx->SetProperty(global_object, error_handle_key, error_handler);
}

std::shared_ptr<CtxValue> argv[5] = {event, source, lineno, colno, error};

auto try_catch = CreateTryCatchScope(true, ctx);
auto ret_value = ctx->CallFunction(error_handler, ctx->GetGlobalObject(), 5, argv);
if (try_catch->HasCaught()) {
auto message = try_catch->GetExceptionMessage();
FOOTSTONE_LOG(WARNING) << "hippy errorHandler error, description = " << message;
}
}

}
}
}
Loading

0 comments on commit 8b70d7d

Please sign in to comment.