Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support rust native value #693

Merged
merged 19 commits into from
Dec 28, 2024
Merged
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/code_linter.yml
Original file line number Diff line number Diff line change
@@ -50,11 +50,9 @@ jobs:
extensions: 'h,cc,c'
clangFormatVersion: 12
inplace: True
- uses: EndBug/add-and-commit@v4
- uses: EndBug/add-and-commit@v9
with:
author_name: openwebf-bot
author_email: openwebf@openwebf.com
message: 'Committing clang-format changes'
default_author: github_actions
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

68 changes: 68 additions & 0 deletions bridge/core/api/executing_context.cc
Original file line number Diff line number Diff line change
@@ -7,8 +7,11 @@
#include "core/api/exception_state.h"
#include "core/dom/document.h"
#include "core/executing_context.h"
#include "core/frame/legacy/location.h"
#include "core/frame/module_manager.h"
#include "core/frame/window.h"
#include "core/frame/window_or_worker_global_scope.h"
#include "foundation/native_value_converter.h"

namespace webf {

@@ -32,6 +35,71 @@ void ExecutingContextWebFMethods::FinishRecordingUIOperations(webf::ExecutingCon
context->uiCommandBuffer()->AddCommand(UICommand::kFinishRecordingCommand, nullptr, nullptr, nullptr, false);
}

NativeValue ExecutingContextWebFMethods::WebFInvokeModule(ExecutingContext* context,
const char* module_name,
const char* method,
SharedExceptionState* shared_exception_state) {
AtomicString module_name_atomic = AtomicString(context->ctx(), module_name);
AtomicString method_atomic = webf::AtomicString(context->ctx(), method);

ScriptValue result = ModuleManager::__webf_invoke_module__(context, module_name_atomic, method_atomic,
shared_exception_state->exception_state);
NativeValue return_result = result.ToNative(context->ctx(), shared_exception_state->exception_state);

if (shared_exception_state->exception_state.HasException()) {
return Native_NewNull();
}

return return_result;
}

NativeValue ExecutingContextWebFMethods::WebFInvokeModuleWithParams(ExecutingContext* context,
const char* module_name,
const char* method,
NativeValue* params,
SharedExceptionState* shared_exception_state) {
AtomicString module_name_atomic = AtomicString(context->ctx(), module_name);
AtomicString method_atomic = webf::AtomicString(context->ctx(), method);

const NativeValue* result = ModuleManager::__webf_invoke_module__(context, module_name_atomic, method_atomic, *params,
nullptr, shared_exception_state->exception_state);

if (shared_exception_state->exception_state.HasException() || result == nullptr) {
return Native_NewNull();
}

NativeValue return_result = *result;
return return_result;
}

NativeValue ExecutingContextWebFMethods::WebFInvokeModuleWithParamsAndCallback(
ExecutingContext* context,
const char* module_name,
const char* method,
NativeValue* params,
WebFNativeFunctionContext* callback_context,
SharedExceptionState* shared_exception_state) {
AtomicString module_name_atomic = AtomicString(context->ctx(), module_name);
AtomicString method_atomic = webf::AtomicString(context->ctx(), method);

auto callback_impl = WebFNativeFunction::Create(callback_context, shared_exception_state);

const NativeValue* result = ModuleManager::__webf_invoke_module__(
context, module_name_atomic, method_atomic, *params, callback_impl, shared_exception_state->exception_state);

if (shared_exception_state->exception_state.HasException()) {
return Native_NewNull();
}

NativeValue return_result = *result;
return return_result;
}

void ExecutingContextWebFMethods::WebFLocationReload(ExecutingContext* context,
SharedExceptionState* shared_exception_state) {
Location::__webf_location_reload__(context, shared_exception_state->exception_state);
}

int32_t ExecutingContextWebFMethods::SetTimeout(ExecutingContext* context,
WebFNativeFunctionContext* callback_context,
int32_t timeout,
42 changes: 21 additions & 21 deletions bridge/core/frame/module_callback.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "module_callback.h"

#include <utility>

namespace webf {

std::shared_ptr<ModuleCallback> ModuleCallback::Create(const std::shared_ptr<QJSFunction>& function) {
return std::make_shared<ModuleCallback>(function);
}

ModuleCallback::ModuleCallback(std::shared_ptr<QJSFunction> function) : function_(std::move(function)) {}

std::shared_ptr<QJSFunction> ModuleCallback::value() {
return function_;
}

} // namespace webf
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "module_callback.h"
#include <utility>
namespace webf {
std::shared_ptr<ModuleCallback> ModuleCallback::Create(const std::shared_ptr<Function>& function) {
return std::make_shared<ModuleCallback>(function);
}
ModuleCallback::ModuleCallback(std::shared_ptr<Function> function) : function_(std::move(function)) {}
std::shared_ptr<Function> ModuleCallback::value() {
return function_;
}
} // namespace webf
58 changes: 29 additions & 29 deletions bridge/core/frame/module_callback.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#ifndef BRIDGE_MODULE_CALLBACK_H
#define BRIDGE_MODULE_CALLBACK_H

#include <quickjs/list.h>
#include "bindings/qjs/qjs_function.h"

namespace webf {

// ModuleCallback is an asynchronous callback function, usually from the 4th parameter of `webf.invokeModule`
// function. When the asynchronous operation on the Dart side ends, the callback is will called and to return to the JS
// executing environment.
class ModuleCallback {
public:
static std::shared_ptr<ModuleCallback> Create(const std::shared_ptr<QJSFunction>& function);
explicit ModuleCallback(std::shared_ptr<QJSFunction> function);

std::shared_ptr<QJSFunction> value();

private:
std::shared_ptr<QJSFunction> function_{nullptr};
};

} // namespace webf

#endif // BRIDGE_MODULE_CALLBACK_H
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#ifndef BRIDGE_MODULE_CALLBACK_H
#define BRIDGE_MODULE_CALLBACK_H
#include <quickjs/list.h>
#include "bindings/qjs/qjs_function.h"
namespace webf {
// ModuleCallback is an asynchronous callback function, usually from the 4th parameter of `webf.invokeModule`
// function. When the asynchronous operation on the Dart side ends, the callback is will called and to return to the JS
// executing environment.
class ModuleCallback {
public:
static std::shared_ptr<ModuleCallback> Create(const std::shared_ptr<Function>& function);
explicit ModuleCallback(std::shared_ptr<Function> function);
std::shared_ptr<Function> value();
private:
std::shared_ptr<Function> function_{nullptr};
};
} // namespace webf
#endif // BRIDGE_MODULE_CALLBACK_H
42 changes: 21 additions & 21 deletions bridge/core/frame/module_listener.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "module_listener.h"

#include <utility>

namespace webf {

std::shared_ptr<ModuleListener> ModuleListener::Create(const std::shared_ptr<QJSFunction>& function) {
return std::make_shared<ModuleListener>(function);
}

ModuleListener::ModuleListener(std::shared_ptr<QJSFunction> function) : function_(std::move(function)) {}

const std::shared_ptr<QJSFunction>& ModuleListener::value() {
return function_;
}

} // namespace webf
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "module_listener.h"
#include <utility>
namespace webf {
std::shared_ptr<ModuleListener> ModuleListener::Create(const std::shared_ptr<Function>& function) {
return std::make_shared<ModuleListener>(function);
}
ModuleListener::ModuleListener(std::shared_ptr<Function> function) : function_(std::move(function)) {}
const std::shared_ptr<Function>& ModuleListener::value() {
return function_;
}
} // namespace webf
68 changes: 34 additions & 34 deletions bridge/core/frame/module_listener.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#ifndef BRIDGE_MODULE_LISTENER_H
#define BRIDGE_MODULE_LISTENER_H

#include "bindings/qjs/qjs_function.h"

namespace webf {

class ModuleContextCoordinator;
class ModuleListenerContainer;

// ModuleListener is an persistent callback function. Registered from user with `webf.addModuleListener` method.
// When module event triggered at dart side, All module listener will be invoked and let user to dispatch further
// operations.
class ModuleListener {
public:
static std::shared_ptr<ModuleListener> Create(const std::shared_ptr<QJSFunction>& function);
explicit ModuleListener(std::shared_ptr<QJSFunction> function);

const std::shared_ptr<QJSFunction>& value();

private:
std::shared_ptr<QJSFunction> function_{nullptr};

friend ModuleListenerContainer;
friend ModuleContextCoordinator;
};

} // namespace webf

#endif // BRIDGE_MODULE_LISTENER_H
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#ifndef BRIDGE_MODULE_LISTENER_H
#define BRIDGE_MODULE_LISTENER_H
#include "bindings/qjs/qjs_function.h"
namespace webf {
class ModuleContextCoordinator;
class ModuleListenerContainer;
// ModuleListener is an persistent callback function. Registered from user with `webf.addModuleListener` method.
// When module event triggered at dart side, All module listener will be invoked and let user to dispatch further
// operations.
class ModuleListener {
public:
static std::shared_ptr<ModuleListener> Create(const std::shared_ptr<Function>& function);
explicit ModuleListener(std::shared_ptr<Function> function);
const std::shared_ptr<Function>& value();
private:
std::shared_ptr<Function> function_{nullptr};
friend ModuleListenerContainer;
friend ModuleContextCoordinator;
};
} // namespace webf
#endif // BRIDGE_MODULE_LISTENER_H
Loading