Skip to content

Commit

Permalink
WIP: native_value
Browse files Browse the repository at this point in the history
  • Loading branch information
LeuisKen committed Nov 23, 2024
1 parent 93f6ff3 commit aecbd80
Show file tree
Hide file tree
Showing 17 changed files with 834 additions and 524 deletions.
68 changes: 68 additions & 0 deletions bridge/core/api/executing_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "core/executing_context.h"
#include "core/frame/window.h"
#include "core/frame/window_or_worker_global_scope.h"
#include "core/frame/module_manager.h"
#include "foundation/native_value_converter.h"

namespace webf {

Expand All @@ -32,6 +34,72 @@ 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);

if (params->tag == NativeTag::TAG_LIST) {
NativeValue* ptr = static_cast<NativeValue*>(params->u.ptr);
auto value_xxx = NativeValueConverter<NativeTypeBool>::FromNativeValue(*ptr);
WEBF_LOG(VERBOSE) << "Value: " << value_xxx;
}

ScriptValue params_value = ScriptValue(context->ctx(), *params);

ScriptValue result = ModuleManager::__webf_invoke_module__(context, module_name_atomic, method_atomic, params_value, 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::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);

ScriptValue params_value = ScriptValue(context->ctx(), *params);

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

ScriptValue result = ModuleManager::__webf_invoke_module__(context, module_name_atomic, method_atomic, params_value, callback_impl, 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;
}

int32_t ExecutingContextWebFMethods::SetTimeout(ExecutingContext* context,
WebFNativeFunctionContext* callback_context,
int32_t timeout,
Expand Down
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

0 comments on commit aecbd80

Please sign in to comment.