Skip to content

Commit

Permalink
[nfc] Disentangle and Modularize JSG
Browse files Browse the repository at this point in the history
- Factor several files out of the main JSG target
- Significantly reduce header interdependencies
  • Loading branch information
fhanau committed Feb 26, 2025
1 parent ebfedb9 commit 1a22071
Show file tree
Hide file tree
Showing 41 changed files with 285 additions and 116 deletions.
4 changes: 3 additions & 1 deletion build/kj_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ def kj_test(
data = [],
deps = [],
tags = [],
size = "medium"):
size = "medium",
**kwargs):
test_name = src.removesuffix(".c++")
native.cc_test(
name = test_name,
Expand All @@ -18,4 +19,5 @@ def kj_test(
data = data,
tags = tags,
size = size,
**kwargs
)
1 change: 1 addition & 0 deletions src/rust/gen-compile-cache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ wd_cc_library(
deps = [
"//src/rust/cxx-integration",
"//src/workerd/jsg",
"//src/workerd/jsg:compile-cache",
"@capnp-cpp//src/kj",
],
)
2 changes: 1 addition & 1 deletion src/rust/gen-compile-cache/cxx-bridge.c++
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cxx-bridge.h"

#include <workerd/jsg/compile-cache.h>
#include <workerd/jsg/setup.h>
#include <workerd/jsg/type-wrapper.h>

#include <workerd/rust/cxx-integration/lib.rs.h>

Expand Down
2 changes: 2 additions & 0 deletions src/workerd/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ wd_cc_library(
"//src/workerd/api:url",
"//src/workerd/api:urlpattern",
"//src/workerd/jsg",
"//src/workerd/jsg:inspector",
"//src/workerd/jsg:script",
"//src/workerd/util:autogate",
"//src/workerd/util:completion-membrane",
"//src/workerd/util:exception",
Expand Down
141 changes: 120 additions & 21 deletions src/workerd/jsg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,86 @@ exports_files(["modules.capnp"])

wd_cc_library(
name = "jsg",
srcs = [
"modules-new.c++",
],
hdrs = [
"jsg-test.h",
"modules-new.h",
"type-wrapper.h",
],
local_defines = ["JSG_IMPLEMENTATION"],
visibility = ["//visibility:public"],
deps = [
":exception",
":iterator",
":jsg-core",
":memory-tracker",
":url",
"//src/workerd/util",
"//src/workerd/util:sentry",
"//src/workerd/util:thread-scopes",
"@capnp-cpp//src/kj",
"@workerd-v8//:v8",
],
)

wd_cc_library(
name = "iterator",
srcs = [
"iterator.c++",
],
hdrs = [
"iterator.h",
"struct.h",
"value.h",
"web-idl.h",
],
local_defines = ["JSG_IMPLEMENTATION"],
deps = [
":exception",
":jsg-core",
":memory-tracker",
"//src/workerd/util",
"//src/workerd/util:sentry",
"//src/workerd/util:thread-scopes",
"@capnp-cpp//src/kj",
"@simdutf",
"@workerd-v8//:v8",
],
)

wd_cc_library(
name = "script",
srcs = ["script.c++"],
hdrs = ["script.h"],
local_defines = ["JSG_IMPLEMENTATION"],
visibility = ["//visibility:public"],
deps = [
":jsg-core",
"@capnp-cpp//src/kj",
"@workerd-v8//:v8",
],
)

# Subset of JSG that includes core JSG interfaces, but does not depend on type-wrapper API.
# Factoring this out reduces the need for circular includes and will make future refactoring of JSG
# much easier.
# This is only visible in this package for now – to avoid extensive include changes in code using
# the JSG type wrapper, the main JSG target should be used instead for now.
# Avoid adding dependencies or new files to this when possible.
wd_cc_library(
name = "jsg-core",
srcs = [
"async-context.c++",
"buffersource.c++",
"commonjs.c++",
"compile-cache.c++",
"dom-exception.c++",
"inspector.c++",
"iterator.c++",
"jsg.c++",
"jsvalue.c++",
"modules.c++",
"modules-new.c++",
"promise.c++",
"resource.c++",
"script.c++",
"ser.c++",
"setup.c++",
"util.c++",
Expand All @@ -33,52 +98,75 @@ wd_cc_library(
"async-context.h",
"buffersource.h",
"commonjs.h",
"compile-cache.h",
"dom-exception.h",
"function.h",
"inspector.h",
"iterator.h",
"jsg.h",
"jsg-test.h",
"jsvalue.h",
"macro-meta.h",
"meta.h",
"modules.h",
"modules-new.h",
"promise.h",
"resource.h",
"script.h",
"ser.h",
"setup.h",
"struct.h",
"type-wrapper.h",
"util.h",
"v8-platform-wrapper.h",
"value.h",
"web-idl.h",
"wrappable.h",
],
# Some JSG headers can't be compiled on their own
features = ["-parse_headers"],
visibility = ["//visibility:public"],
local_defines = ["JSG_IMPLEMENTATION"],
deps = [
":exception",
":macro-meta",
":memory-tracker",
":meta",
":modules_capnp",
":observer",
":url",
"//src/workerd/util",
"//src/workerd/util:autogate",
"//src/workerd/util:sentry",
"//src/workerd/util:thread-scopes",
"//src/workerd/util:uuid",
"@capnp-cpp//src/kj",
"@simdutf",
"@ssl",
"@workerd-v8//:v8",
],
)

wd_cc_library(
name = "compile-cache",
srcs = [
"compile-cache.c++",
],
hdrs = [
"compile-cache.h",
],
local_defines = ["JSG_IMPLEMENTATION"],
visibility = ["//visibility:public"],
deps = [
"@capnp-cpp//src/kj",
"@workerd-v8//:v8",
],
)

wd_cc_library(
name = "inspector",
srcs = [
"inspector.c++",
],
hdrs = [
"inspector.h",
],
local_defines = ["JSG_IMPLEMENTATION"],
# Some JSG headers can't be compiled on their own
visibility = ["//visibility:public"],
deps = [
":jsg-core",
"@capnp-cpp//src/kj",
"@simdutf",
"@workerd-v8//:v8",
],
)

wd_cc_library(
name = "memory-tracker",
srcs = ["memory.c++"],
Expand All @@ -90,6 +178,16 @@ wd_cc_library(
],
)

wd_cc_library(
name = "meta",
hdrs = ["meta.h"],
visibility = ["//visibility:public"],
deps = [
"@capnp-cpp//src/kj",
"@workerd-v8//:v8",
],
)

wd_cc_library(
name = "url",
srcs = ["url.c++"],
Expand Down Expand Up @@ -176,6 +274,7 @@ wd_cc_library(

[kj_test(
src = f,
local_defines = ["JSG_IMPLEMENTATION"],
deps = [
":jsg",
"//src/workerd/util:autogate",
Expand Down Expand Up @@ -243,7 +342,7 @@ kj_test(
kj_test(
src = "multiple-typewrappers-test.c++",
deps = [
"//src/workerd/io",
"//src/workerd/io:compatibility-date_capnp",
"//src/workerd/jsg",
],
)
2 changes: 2 additions & 0 deletions src/workerd/jsg/buffersource.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "jsg.h"

#include <v8-typed-array.h>

namespace workerd::jsg {

#define JSG_ARRAY_BUFFER_VIEW_TYPES(V) \
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/jsg/commonjs.c++
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "commonjs.h"

#include "jsvalue.h"
#include "modules.h"
#include "resource.h"

namespace workerd::jsg {

Expand Down
7 changes: 3 additions & 4 deletions src/workerd/jsg/compile-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// https://opensource.org/licenses/Apache-2.0
#pragma once

#include "jsg.h"
#include "setup.h"

#include <v8.h>
#include <v8-script.h>

#include <kj/map.h>
#include <kj/mutex.h>
#include <kj/string.h>

namespace workerd::jsg {
Expand Down
1 change: 1 addition & 0 deletions src/workerd/jsg/dom-exception.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "dom-exception.h"

#include "jsvalue.h"
#include "ser.h"

#include <workerd/jsg/memory.h>
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/jsg/dom-exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma once

#include "jsg.h"
#include "ser.h"

#define JSG_DOM_EXCEPTION_FOR_EACH_ERROR_NAME(f) \
f(INDEX_SIZE_ERR, 1, "IndexSizeError") f(DOMSTRING_SIZE_ERR, 2, "DOMStringSizeError") \
Expand All @@ -28,6 +27,8 @@
f(DATA_CLONE_ERR, 25, "DataCloneError")

namespace workerd::jsg {
class Serializer;
class Deserializer;

// JSG allows DOMExceptions to be tunneled through kj::Exceptions (see makeInternalError() for
// details). While this feature is activated conditionally at run-time, and thus does not depend
Expand Down
6 changes: 5 additions & 1 deletion src/workerd/jsg/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
// Handles wrapping a C++ function so that it can be called from JavaScript, and vice versa.

#include "jsg.h"
#include "meta.h"
#include "wrappable.h"

#include <workerd/jsg/meta.h>

#include <v8-context.h>
#include <v8-function.h>

#include <kj/function.h>

namespace workerd::jsg {
Expand Down
1 change: 1 addition & 0 deletions src/workerd/jsg/inspector.c++
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "inspector.h"

#include "jsg.h"
#include "jsvalue.h"
#include "simdutf.h"
#include "util.h"

Expand Down
5 changes: 2 additions & 3 deletions src/workerd/jsg/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

#pragma once

#include "jsg.h"
#include "struct.h"

#include <workerd/jsg/jsg.h>
#include <workerd/jsg/memory.h>
#include <workerd/jsg/struct.h>

#include <concepts>
#include <deque>
Expand Down
6 changes: 4 additions & 2 deletions src/workerd/jsg/jsg-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#pragma once
// Common JSG testing infrastructure

#include "jsg.h"
#include "setup.h"
#include <workerd/jsg/jsg.h>
#include <workerd/jsg/resource.h>
#include <workerd/jsg/setup.h>
#include <workerd/jsg/type-wrapper.h>

#include <kj/test.h>

Expand Down
Loading

0 comments on commit 1a22071

Please sign in to comment.