Skip to content

Commit

Permalink
feat: add InstallBindings
Browse files Browse the repository at this point in the history
  • Loading branch information
david committed Dec 13, 2024
1 parent 1a85319 commit e7c24cc
Show file tree
Hide file tree
Showing 33 changed files with 1,486 additions and 114 deletions.
34 changes: 21 additions & 13 deletions bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ list(APPEND BRIDGE_SOURCE
foundation/logging.cc
foundation/native_string.cc
# foundation/ui_task_queue.cc
# foundation/shared_ui_command.cc
foundation/shared_ui_command.cc
# foundation/inspector_task_queue.cc
# foundation/task_queue.cc
foundation/string_view.cc
# foundation/native_value.cc
foundation/native_value.cc
# foundation/native_type.cc
# foundation/stop_watch.cc
# foundation/profiler.cc
# foundation/dart_readable.cc
foundation/dart_readable.cc
foundation/rust_readable.cc
# foundation/ui_command_buffer.cc
# foundation/ui_command_strategy.cc
foundation/ui_command_buffer.cc
foundation/ui_command_strategy.cc
# polyfill/dist/polyfill.cc
# multiple_threading/dispatcher.cc
# multiple_threading/looper.cc
multiple_threading/dispatcher.cc
multiple_threading/looper.cc
${CMAKE_CURRENT_LIST_DIR}/third_party/dart/include/dart_api_dl.c
)

Expand Down Expand Up @@ -650,8 +650,16 @@ elseif (${WEBF_JS_ENGINE} MATCHES "v8")
bindings/v8/scoped_persistent.h
bindings/v8/v8_per_context_data.cc
bindings/v8/v8_script_state.cc
bindings/v8/binding_initializer.cc
bindings/v8/generated_code_helper.h
bindings/v8/member_installer.cc
)

# Gen sources.
list(APPEND BRIDGE_SOURCE
out/names_installer.cc
out/built_in_string.cc
out/v8_window_or_worker_global_scope.cc
)

if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Expand All @@ -671,11 +679,11 @@ endif()

list(APPEND BRIDGE_SOURCE
# Core sources
# webf_bridge.cc
# core/executing_context.cc
webf_bridge.cc
core/executing_context.cc
# core/script_forbidden_scope.cc
# core/script_state.cc
# core/page.cc
core/page.cc
# core/dart_methods.cc
# core/api/exception_state.cc
# core/api/event_target.cc
Expand All @@ -693,16 +701,16 @@ list(APPEND BRIDGE_SOURCE
# core/api/comment.cc
# core/api/character_data.cc
# core/api/script_value_ref.cc
# core/dart_isolate_context.cc
# core/dart_context_data.cc
core/dart_isolate_context.cc
core/dart_context_data.cc
# core/executing_context_data.cc
# core/fileapi/blob.cc
# core/fileapi/blob_part.cc
# core/fileapi/blob_property_bag.cc
# core/frame/console.cc
# core/frame/dom_timer.cc
# core/frame/dom_timer_coordinator.cc
# core/frame/window_or_worker_global_scope.cc
core/frame/window_or_worker_global_scope.cc
# core/frame/module_listener.cc
# core/frame/module_listener_container.cc
# core/frame/module_manager.cc
Expand Down
120 changes: 120 additions & 0 deletions bridge/bindings/v8/binding_initializer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/


#include "binding_initializer.h"
#include "v8_window_or_worker_global_scope.h"

namespace webf {

void InstallBindings(ExecutingContext* context) {
// Must follow the inheritance order when install.
// Exp: Node extends EventTarget, EventTarget must be install first.
V8WindowOrWorkerGlobalScope::Install(context);
/*TODO support DOM API
QJSLocation::Install(context);
QJSModuleManager::Install(context);
QJSConsole::Install(context);
QJSEventTarget::Install(context);
QJSWindow::Install(context);
QJSEvent::Install(context);
QJSUIEvent::Install(context);
QJSErrorEvent::Install(context);
QJSPromiseRejectionEvent::Install(context);
QJSMessageEvent::Install(context);
QJSAnimationEvent::Install(context);
QJSCloseEvent::Install(context);
QJSHybridRouterChangeEvent::Install(context);
QJSFocusEvent::Install(context);
QJSGestureEvent::Install(context);
QJSHashchangeEvent::Install(context);
QJSInputEvent::Install(context);
QJSCustomEvent::Install(context);
QJSMouseEvent::Install(context);
QJSPointerEvent::Install(context);
QJSTouchEvent::Install(context);
QJSPopStateEvent::Install(context);
QJSTransitionEvent::Install(context);
QJSIntersectionChangeEvent::Install(context);
QJSKeyboardEvent::Install(context);
QJSNode::Install(context);
QJSNodeList::Install(context);
QJSDocument::Install(context);
QJSDocumentFragment::Install(context);
QJSCharacterData::Install(context);
QJSText::Install(context);
QJSComment::Install(context);
QJSElement::Install(context);
QJSHTMLElement::Install(context);
QJSWidgetElement::Install(context);
QJSHTMLDivElement::Install(context);
QJSHTMLHeadElement::Install(context);
QJSHTMLBodyElement::Install(context);
QJSHTMLHtmlElement::Install(context);
QJSHTMLIFrameElement::Install(context);
QJSHTMLAnchorElement::Install(context);
QJSHTMLImageElement::Install(context);
QJSHTMLInputElement::Install(context);
QJSHTMLTextareaElement::Install(context);
QJSHTMLButtonElement::Install(context);
QJSHTMLFormElement::Install(context);
QJSImage::Install(context);
QJSHTMLScriptElement::Install(context);
QJSHTMLLinkElement::Install(context);
QJSHTMLUnknownElement::Install(context);
QJSHTMLTemplateElement::Install(context);
QJSHTMLCanvasElement::Install(context);
QJSCanvasRenderingContext::Install(context);
QJSCanvasRenderingContext2D::Install(context);
QJSCanvasPattern::Install(context);
QJSCanvasGradient::Install(context);
QJSPath2D::Install(context);
QJSDOMMatrixReadOnly::Install(context);
QJSDOMMatrix::Install(context);
QJSDOMPointReadOnly::Install(context);
QJSDOMPoint::Install(context);
QJSCSSStyleDeclaration::Install(context);
QJSInlineCssStyleDeclaration::Install(context);
QJSComputedCssStyleDeclaration::Install(context);
QJSBoundingClientRect::Install(context);
QJSScreen::Install(context);
QJSBlob::Install(context);
QJSTouch::Install(context);
QJSTouchList::Install(context);
QJSDOMStringMap::Install(context);
QJSMutationObserver::Install(context);
QJSMutationRecord::Install(context);
QJSMutationObserverRegistration::Install(context);
QJSDOMTokenList::Install(context);
QJSPerformance::Install(context);
QJSPerformanceEntry::Install(context);
QJSPerformanceMark::Install(context);
QJSPerformanceMeasure::Install(context);
QJSHTMLCollection::Install(context);
QJSHTMLAllCollection::Install(context);
// SVG
QJSSVGElement::Install(context);
QJSSVGGraphicsElement::Install(context);
QJSSVGGeometryElement::Install(context);
QJSSVGSVGElement::Install(context);
QJSSVGRectElement::Install(context);
QJSSVGTextContentElement::Install(context);
QJSSVGTextPositioningElement::Install(context);
QJSSVGPathElement::Install(context);
QJSSVGTextElement::Install(context);
QJSSVGGElement::Install(context);
QJSSVGCircleElement::Install(context);
QJSSVGEllipseElement::Install(context);
QJSSVGStyleElement::Install(context);
QJSSVGLineElement::Install(context);
QJSNativeLoader::Install(context);
// Legacy bindings, not standard.
QJSElementAttributes::Install(context);
*/
}

} // namespace webf
18 changes: 18 additions & 0 deletions bridge/bindings/v8/binding_initializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/


#ifndef WEBF_BINDING_INITIALIZER_H
#define WEBF_BINDING_INITIALIZER_H

namespace webf {

class ExecutingContext;

void InstallBindings(ExecutingContext* context);

} // namespace webf

#endif //WEBF_BINDING_INITIALIZER_H
12 changes: 12 additions & 0 deletions bridge/bindings/v8/generated_code_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/

#ifndef WEBF_BRIDGE_BINDINGS_V8_GENERATED_CODE_HELPER_H_
#define WEBF_BRIDGE_BINDINGS_V8_GENERATED_CODE_HELPER_H_

#include "atomic_string.h"
#include "script_value.h"

#endif //WEBF_BRIDGE_BINDINGS_V8_GENERATED_CODE_HELPER_H_
25 changes: 25 additions & 0 deletions bridge/bindings/v8/member_installer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/

#include "member_installer.h"
#include "core/executing_context.h"

namespace webf {

void MemberInstaller::InstallFunctions(ExecutingContext* context,
std::initializer_list<FunctionConfig> config) {
v8::Isolate* isolate = context->ctx();
v8::Local<v8::Context> v8_context = isolate->GetCurrentContext();
v8::Local<v8::Object> global = v8_context->Global();

for (const auto& function : config) {
v8::Local<v8::FunctionTemplate> function_template = v8::FunctionTemplate::New(isolate, function.callback);
v8::Local<v8::Function> v8_function = function_template->GetFunction(v8_context).ToLocalChecked();

global->Set(v8_context, v8::String::NewFromUtf8(isolate, function.name).ToLocalChecked(), v8_function).Check();
}
}

} // namespace webf
30 changes: 30 additions & 0 deletions bridge/bindings/v8/member_installer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/

#ifndef WEBF_BRIDGE_BINDINGS_V8_MEMBER_INSTALLER_H_
#define WEBF_BRIDGE_BINDINGS_V8_MEMBER_INSTALLER_H_

#include <initializer_list>
#include <v8/v8.h>

namespace webf {

class ExecutingContext;

// A set of utility functions to define attributes members as ES properties.
class MemberInstaller {
public:
struct FunctionConfig {
FunctionConfig& operator=(const FunctionConfig&) = delete;
const char* name;
v8::FunctionCallback callback;
};

static void InstallFunctions(ExecutingContext* context, std::initializer_list<FunctionConfig> config);
};

} // namespace webf

#endif //WEBF_BRIDGE_BINDINGS_V8_MEMBER_INSTALLER_H_
14 changes: 9 additions & 5 deletions bridge/core/dart_isolate_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#endif
#include <unordered_set>
#include "dart_isolate_context.h"
#include "event_factory.h"
#include "html_element_factory.h"
//#include "event_factory.h"
//#include "html_element_factory.h"
#include "names_installer.h"
#include "page.h"
#include "svg_element_factory.h"
//#include "svg_element_factory.h"

namespace webf {

Expand Down Expand Up @@ -129,9 +129,11 @@ void DartIsolateContext::FinalizeJSRuntime() {

// Prebuilt strings stored in JSRuntime. Only needs to dispose when runtime disposed.
names_installer::Dispose();
/*TODO support HTMLElementFactory SVGElementFactory EventFactory
HTMLElementFactory::Dispose();
SVGElementFactory::Dispose();
EventFactory::Dispose();
*/

#if WEBF_QUICKJS_JS_ENGINE
ClearUpWires(runtime_);
Expand All @@ -147,9 +149,11 @@ void DartIsolateContext::FinalizeJSRuntime() {

DartIsolateContext::DartIsolateContext(const uint64_t* dart_methods, int32_t dart_methods_length, bool profile_enabled)
: is_valid_(true),
running_thread_(std::this_thread::get_id()),
// TODO v8 suppport profiler_(std::make_unique<WebFProfiler>(profile_enabled)),
running_thread_(std::this_thread::get_id()) {
/* TODO v8 suppport
profiler_(std::make_unique<WebFProfiler>(profile_enabled)),
dart_method_ptr_(std::make_unique<DartMethodPointer>(this, dart_methods, dart_methods_length)) {
*/
is_valid_ = true;
running_dart_isolates++;
}
Expand Down
6 changes: 4 additions & 2 deletions bridge/core/dart_isolate_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#include <set>
#include "dart_context_data.h"
#include "dart_methods.h"
/* TODO support V8
#include "dart_methods.h"
#include "foundation/profiler.h"
*/
#include "multiple_threading/dispatcher.h"
Expand Down Expand Up @@ -67,7 +67,9 @@ class DartIsolateContext {
v8::Isolate* isolate();
#endif
FORCE_INLINE bool valid() { return is_valid_; }
/* TODO support
FORCE_INLINE DartMethodPointer* dartMethodPtr() const { return dart_method_ptr_.get(); }
*/
FORCE_INLINE const std::unique_ptr<multi_threading::Dispatcher>& dispatcher() const { return dispatcher_; }
FORCE_INLINE void SetDispatcher(std::unique_ptr<multi_threading::Dispatcher>&& dispatcher) {
dispatcher_ = std::move(dispatcher);
Expand Down Expand Up @@ -128,7 +130,7 @@ class DartIsolateContext {
std::unordered_set<std::unique_ptr<WebFPage>> pages_in_ui_thread_;
std::unique_ptr<multi_threading::Dispatcher> dispatcher_ = nullptr;
// Dart methods ptr should keep alive when ExecutingContext is disposing.
const std::unique_ptr<DartMethodPointer> dart_method_ptr_ = nullptr;
// TODO const std::unique_ptr<DartMethodPointer> dart_method_ptr_ = nullptr;
};

} // namespace webf
Expand Down
Loading

0 comments on commit e7c24cc

Please sign in to comment.