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

[WIP]: feat: add v8 Support. #586

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e52752f
feat: add v8 release and atomicString impl
andycall Mar 30, 2024
3c56237
Committing clang-format changes
Mar 30, 2024
06629e0
update submodule url
Apr 3, 2024
b9e311e
feat: split v8 and quickjs templates.
andycall Apr 18, 2024
baf64ff
test: add atomic string testing.
andycall Apr 18, 2024
21af632
fix: fix build scripts.
andycall Apr 19, 2024
89d1a75
feat: add script_value
Apr 21, 2024
91894ab
feat: fixed v8_per_context_data.h compilation errors
May 23, 2024
28b94a4
feat: Implement v8_per_context_data.cc
May 27, 2024
0f4518e
feat:resolve link error to compile webf_unit_test
May 28, 2024
8af4f8b
feat: add v8_initializer.cc
Jun 5, 2024
7e2d932
feat: USING_FAST_MALLOC uses the util namespace
Jun 14, 2024
c22ba1b
feat: PartitionAlloc standalone only support Linux
Jun 27, 2024
6d88145
feat: script_value.h compiled successful
Jun 28, 2024
7f76256
feat: delete some wtf
Jul 2, 2024
4f41f0b
feat: add dart_isolate_context.cc
Jul 3, 2024
a90f056
feat: support ScopedPersistent<v8::Context>
Jul 5, 2024
721205d
feat: add v8_member_installer
Aug 16, 2024
8ca3d35
feat: call v8 InstallFunctions
Aug 23, 2024
21c3429
feat: v8 generateMethodArgumentsCheck
Aug 23, 2024
f427c1c
feat: ExecutingContext from v8 isolate
Aug 23, 2024
1488ef5
feat: add V8ThrowException
Aug 23, 2024
7ffbcc5
feat: declare WindowOrWorkerGlobalScope::setTimeout
Aug 23, 2024
408766c
feat: add Converter<IDLCallback>
Aug 25, 2024
2fc2e61
feat: generate the setTimeout binding code
Aug 27, 2024
c20ba19
feat: implement setTimeout
Sep 7, 2024
11d72fd
fix: compile error
Sep 7, 2024
a8920c4
feat: update -mmacosx-version-min=13.3
Sep 7, 2024
b689056
feat: support ExecutingContext::Timers()
Sep 7, 2024
00c3ef8
feat: update macos x86_64 v8 git submodule
Sep 17, 2024
d100480
feat: cmake support x86_64
Sep 17, 2024
4515004
fix: Embedder-vs-V8 build configuration mismatch.
Sep 21, 2024
0302879
fix: Context::Global() need Context::Scope
Sep 21, 2024
03e354e
feat: v8 upgrade to 13.3.206
Dec 1, 2024
5ec877e
fix: reset bridge/scripts to main branch
Dec 1, 2024
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
Prev Previous commit
Next Next commit
feat: add v8_member_installer
  • Loading branch information
david committed Dec 1, 2024
commit 721205d56eda97a0e9debb679161cfabe232b650
14 changes: 11 additions & 3 deletions bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,9 @@ elseif (${WEBF_JS_ENGINE} MATCHES "v8")
bindings/v8/wrapper_type_info.cc
bindings/v8/v8_interface_bridge_base.cc
bindings/v8/v8_initializer.cc
bindings/v8/v8_member_installer.cc
bindings/v8/v8_binding_initializer.cc
bindings/v8/generated_code_helper.h
bindings/v8/platform/scoped_persistent.h
bindings/v8/platform/v8_per_context_data.cc
bindings/v8/platform/v8_per_isolate_data.cc
Expand Down Expand Up @@ -724,9 +727,14 @@ elseif (${WEBF_JS_ENGINE} MATCHES "v8")
bindings/v8/gin/gin_export.h
bindings/v8/gin/per_context_data.cc
bindings/v8/gin/context_holder.cc
out/built_in_string.cc

)

# Gen sources.
list(APPEND BRIDGE_SOURCE
out/built_in_string.cc
out/v8_window_or_worker_global_scope.cc
)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
add_library(v8 SHARED IMPORTED)
set_target_properties(v8 PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/third_party/v8/lib/macos/arm64/libv8.dylib")
Expand All @@ -745,7 +753,7 @@ endif()
list(APPEND BRIDGE_SOURCE
# Core sources
webf_bridge.cc
# core/api/api.cc
core/api/api.cc
core/executing_context.cc
# core/script_forbidden_scope.cc
core/script_state.cc
Expand All @@ -760,7 +768,7 @@ list(APPEND BRIDGE_SOURCE
# 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
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_GENERATED_CODE_HELPER_H
#define WEBF_GENERATED_CODE_HELPER_H

#include "bindings/v8/atomic_string.h"
#include "bindings/v8/script_value.h"

#endif // WEBF_GENERATED_CODE_HELPER_H
6 changes: 0 additions & 6 deletions bridge/bindings/v8/platform/heap/garbage_collected.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@

#include <type_traits>

//#include "base/functional/unretained_traits.h"
//#include "third_party/blink/renderer/platform/heap/thread_state_storage.h"
//#include "v8/include/cppgc/allocation.h"
//#include "v8/include/cppgc/garbage-collected.h"
//#include "v8/include/cppgc/liveness-broker.h"
//#include "v8/include/cppgc/type-traits.h"
#include <v8/v8.h>
#include <v8/v8-cppgc.h>
#include <v8/cppgc/allocation.h>
Expand Down
17 changes: 17 additions & 0 deletions bridge/bindings/v8/v8_binding_initializer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/

#include "v8_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);
}

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

#ifndef WEBF_V8_BINDING_INITIALIZER_H
#define WEBF_V8_BINDING_INITIALIZER_H

namespace webf {

class ExecutingContext;

void InstallBindings(ExecutingContext* context);

} // namespace webf

#endif // WEBF_V8_BINDING_INITIALIZER_H
25 changes: 25 additions & 0 deletions bridge/bindings/v8/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 "v8_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/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_V8_MEMBER_INSTALLER_H
#define WEBF_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_V8_MEMBER_INSTALLER_H
14 changes: 12 additions & 2 deletions bridge/core/dart_isolate_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ void DartIsolateContext::FinalizeJSRuntime() {
if (running_dart_isolates > 0 || runtime_ == nullptr) {
return;
}
#if WEBF_QUICKJS_JS_ENGINE
if (runtime_ == nullptr) {
return;
}
#elif WEBF_V8_JS_ENGINE
if (isolate_ == nullptr) {
return;
}
#endif


// Prebuilt strings stored in JSRuntime. Only needs to dispose when runtime disposed.
// names_installer::Dispose();
Expand Down Expand Up @@ -235,10 +245,10 @@ void* DartIsolateContext::AddNewPage(double thread_identity,
std::unique_ptr<WebFPage> DartIsolateContext::InitializeNewPageSync(DartIsolateContext* dart_isolate_context,
size_t sync_buffer_size,
double page_context_id) {
dart_isolate_context->profiler()->StartTrackInitialize();
// dart_isolate_context->profiler()->StartTrackInitialize();
DartIsolateContext::InitializeJSRuntime();
auto page = std::make_unique<WebFPage>(dart_isolate_context, false, sync_buffer_size, page_context_id, nullptr);
dart_isolate_context->profiler()->FinishTrackInitialize();
// dart_isolate_context->profiler()->FinishTrackInitialize();

return page;
}
Expand Down
3 changes: 2 additions & 1 deletion bridge/core/executing_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "foundation/logging.h"
#include "polyfill.h"
//#include "qjs_window.h"
#include "bindings/v8/v8_binding_initializer.h"
#include "script_forbidden_scope.h"
//#include "timing/performance.h"

Expand Down Expand Up @@ -72,7 +73,7 @@ ExecutingContext::ExecutingContext(DartIsolateContext* dart_isolate_context,
// dart_isolate_context->profiler()->StartTrackSteps("ExecutingContext::InstallBindings");

// Register all built-in native bindings.
// InstallBindings(this);
InstallBindings(this);

// dart_isolate_context->profiler()->FinishTrackSteps();
// dart_isolate_context->profiler()->StartTrackSteps("ExecutingContext::InstallDocument");
Expand Down
2 changes: 1 addition & 1 deletion bridge/core/executing_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ExecutingContext {
#elif WEBF_V8_JS_ENGINE
static ExecutingContext* From(v8::Isolate* isolate);
v8::Local<v8::Value> Global();
v8::Isolate ctx();
v8::Isolate* ctx();
bool HandleException(v8::Local<v8::Value> exc);
void ReportError(v8::Local<v8::Value> error);
void DefineGlobalProperty(const char* prop, v8::Local<v8::Value> value);
Expand Down
5 changes: 3 additions & 2 deletions bridge/scripts/code_generator/bin/code_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ function genCodeFromTypeDefine() {
});

let blobs = typeFiles.map(file => {
let filename = generatePlatformPrefix() + '_' + file.split('/').slice(-1)[0].replace('.d.ts', '');
let platformPrefix = generatePlatformPrefix();
let filename = platformPrefix + '_' + file.split('/').slice(-1)[0].replace('.d.ts', '');
let implement = file.replace(path.join(__dirname, '../../')).replace('.d.ts', '');
return new IDLBlob(path.join(source, file), dist, filename, implement);
return new IDLBlob(path.join(source, file), dist, filename, implement, platformPrefix);
});

ClassObject.globalClassMap = Object.create(null);
Expand Down
4 changes: 3 additions & 1 deletion bridge/scripts/code_generator/src/idl/IDLBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export class IDLBlob {
source: string;
filename: string;
implement: string;
platformPrefix: string;
objects: (ClassObject | FunctionObject)[];

constructor(source: string, dist: string, filename: string, implement: string) {
constructor(source: string, dist: string, filename: string, implement: string, platformPrefix: string) {
this.source = source;
this.raw = fs.readFileSync(source, {encoding: 'utf-8'});
this.dist = dist;
this.filename = filename;
this.implement = implement;
this.platformPrefix = platformPrefix;
}
}
103 changes: 103 additions & 0 deletions bridge/scripts/code_generator/src/idl/generate/v8/generateHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {ClassObject, ClassObjectKind, FunctionObject} from "../../declaration";
import _ from "lodash";
import {IDLBlob} from "../../IDLBlob";
import {getClassName} from "../../utils";
import fs from 'fs';
import path from 'path';
import {
generateCoreTypeValue,
generateRawTypeValue,
getPointerType,
isPointerType,
isTypeHaveNull
} from "./generateSource";
import {GenerateOptions, generateUnionTypeFileName, readTemplate} from "../../generator";
import {ParameterType} from "../../analyzer";
import {
generateUnionConstructor,
generateUnionContentType, generateUnionMemberName, generateUnionMemberType, generateUnionPropertyHeaders,
generateUnionTypeClassName,
} from "./generateUnionTypes";

export enum TemplateKind {
globalFunction,
Dictionary,
Interface,
null
}

export function getTemplateKind(object: ClassObject | FunctionObject | null): TemplateKind {
if (object instanceof FunctionObject) {
return TemplateKind.globalFunction;
} else if (object instanceof ClassObject) {
if (object.kind === ClassObjectKind.dictionary) {
return TemplateKind.Dictionary;
} else if(object.kind === ClassObjectKind.mixin) {
return TemplateKind.null;
}
return TemplateKind.Interface;
}
return TemplateKind.null;
}

export function generateV8CppHeader(blob: IDLBlob, options: GenerateOptions) {
const baseTemplate = fs.readFileSync(path.join(__dirname, '../../../../templates/idl_templates/v8/base.h.tpl'), {encoding: 'utf-8'});
let headerOptions = {
interface: false,
dictionary: false,
global_function: false,
};
const contents = blob.objects.map(object => {
const templateKind = getTemplateKind(object);
if (templateKind === TemplateKind.null) return '';

switch(templateKind) {
case TemplateKind.Interface: {
if (!headerOptions.interface) {
object = (object as ClassObject);
headerOptions.interface = true;
return _.template(readTemplate('v8', 'interface'))({
className: getClassName(blob),
parentClassName: object.parent,
blob: blob,
object,
generateRawTypeValue,
...options
});
}
return '';
}
case TemplateKind.Dictionary: {
if (!headerOptions.dictionary) {
headerOptions.dictionary = true;
let props = (object as ClassObject).props;
return _.template(readTemplate('v8', 'dictionary'))({
className: getClassName(blob),
blob: blob,
object: object,
props,
generateCoreTypeValue
});
}
return '';
}
case TemplateKind.globalFunction: {
if (!headerOptions.global_function) {
headerOptions.global_function = true;
return _.template(readTemplate('v8', 'global_function'))({
className: getClassName(blob),
blob: blob
});
}
return '';
}
}
});

return _.template(baseTemplate)({
content: contents.join('\n'),
blob: blob
}).split('\n').filter(str => {
return str.trim().length > 0;
}).join('\n');
}
Loading