Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 7 additions & 5 deletions build/wd_js_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

CAPNP_TEMPLATE = """@{schema_id};

# generated by @workerd//build/wd_js_bundle.bzl

using Modules = import "/workerd/jsg/modules.capnp";

const {const_name} :Modules.Bundle = (
Expand All @@ -11,7 +13,7 @@ const {const_name} :Modules.Bundle = (
]);
"""

MODULE_TEMPLATE = """(name = "{name}", src = embed "{path}", {ts_declaration}internal = {internal})"""
MODULE_TEMPLATE = """ (name = "{name}", src = embed "{path}", type = {type}, {ts_declaration})"""

def _to_d_ts(file_name):
return file_name.removesuffix(".js") + ".d.ts"
Expand All @@ -24,7 +26,7 @@ def _relative_path(file_path, dir_path):
def _gen_api_bundle_capnpn_impl(ctx):
output_dir = ctx.outputs.out.dirname + "/"

def _render_module(name, label, internal):
def _render_module(name, label, type):
return MODULE_TEMPLATE.format(
name = name,
# capnp doesn't allow ".." dir escape, make paths relative.
Expand All @@ -39,15 +41,15 @@ def _gen_api_bundle_capnpn_impl(ctx):
output_dir,
) + "\", "
) if name in ctx.attr.declarations else "",
internal = "true" if internal else "false",
type = type,
)

modules = [
_render_module(ctx.attr.builtin_modules[m], m.label, False)
_render_module(ctx.attr.builtin_modules[m], m.label, "builtin")
for m in ctx.attr.builtin_modules
]
modules += [
_render_module(ctx.attr.internal_modules[m], m.label, True)
_render_module(ctx.attr.internal_modules[m], m.label, "internal")
for m in ctx.attr.internal_modules
]

Expand Down
13 changes: 6 additions & 7 deletions src/workerd/api/rtti.c++
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,30 @@ struct EncoderModuleRegistryImpl {
kj::StringPtr tsDeclarations;
};
struct ModuleInfo {
ModuleInfo(kj::StringPtr specifier, bool internal, kj::OneOf<CppModuleContents,
ModuleInfo(kj::StringPtr specifier, jsg::ModuleType type, kj::OneOf<CppModuleContents,
TypeScriptModuleContents> contents)
: specifier(specifier),
internal(internal),
type(type),
contents(kj::mv(contents)) {}

kj::StringPtr specifier;
bool internal;
jsg::ModuleType type;
kj::OneOf<CppModuleContents, TypeScriptModuleContents> contents;
};

void addBuiltinBundle(jsg::Bundle::Reader bundle) {
for (auto module: bundle.getModules()) {
TypeScriptModuleContents contents (module.getTsDeclaration());
ModuleInfo info (module.getName(), module.getInternal(), kj::mv(contents));
ModuleInfo info (module.getName(), module.getType(), kj::mv(contents));
modules.add(kj::mv(info));
}
}

template <typename T>
void addBuiltinModule(kj::StringPtr specifier, jsg::ModuleRegistry::Type type = jsg::ModuleRegistry::Type::BUILTIN) {
auto internal = type == jsg::ModuleRegistry::Type::INTERNAL;
auto structureName = jsg::fullyQualifiedTypeName(typeid(T));
CppModuleContents contents (kj::mv(structureName));
ModuleInfo info (specifier, internal, kj::mv(contents));
ModuleInfo info (specifier, type, kj::mv(contents));
modules.add(kj::mv(info));
}

Expand Down Expand Up @@ -181,7 +180,7 @@ public:
for (auto moduleBuilder: modulesBuilder) {
auto& module = registry.modules[i++];
moduleBuilder.setSpecifier(module.specifier);
moduleBuilder.setInternal(module.internal);
// moduleBuilder.setType(module.type);
KJ_SWITCH_ONEOF(module.contents) {
KJ_CASE_ONEOF(contents, EncoderModuleRegistryImpl::CppModuleContents) {
moduleBuilder.setStructureName(contents.structureName);
Expand Down
9 changes: 9 additions & 0 deletions src/workerd/jsg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ wd_cc_capnp_library(
name = "rtti_capnp",
srcs = ["rtti.capnp"],
visibility = ["//visibility:public"],
deps = [":modules_capnp"],
)

js_capnp_library(
Expand All @@ -58,6 +59,8 @@ js_capnp_library(
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [":modules_capnp_js"],
src_prefix = "src/",
)

npm_package(
Expand Down Expand Up @@ -87,6 +90,12 @@ wd_cc_capnp_library(
visibility = ["//visibility:public"],
)

js_capnp_library(
name = "modules_capnp_js",
srcs = ["modules.capnp"],
src_prefix = "src",
)

wd_cc_library(
name = "observer",
hdrs = ["observer.h"],
Expand Down
16 changes: 14 additions & 2 deletions src/workerd/jsg/modules.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ struct Module {
src @1 :Data;
tsDeclaration @3 :Text;

internal @2 :Bool;
# internal modules can't be imported by user's code
type @2 :ModuleType;
}


enum ModuleType {
bundle @0;
# Provided by the worker bundle.

builtin @1;
# Provided by the runtime and can be imported by the worker bundle.
# Can be overridden by modules in the worker bundle.

internal @2;
# Provided by runtime but can only imported by builtin modules.
}
17 changes: 2 additions & 15 deletions src/workerd/jsg/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,7 @@ class ModuleRegistry {

ModuleRegistry() { }

enum class Type {
// BUNDLE is for modules provided by the worker bundle.
// BUILTIN is for modules that are provided by the runtime and can be
// imported by the worker bundle. These can be overridden by modules
// in the worker bundle.
// INTERNAL is for BUILTIN modules that can only be imported by other
// BUILTIN modules. These cannot be overriden by modules in the worker
// bundle.

BUNDLE,
BUILTIN,
INTERNAL,
};
using Type = ModuleType;

enum class ResolveOption {
// Default resolution. Check the worker bundle first, then builtins.
Expand Down Expand Up @@ -400,8 +388,7 @@ class ModuleRegistryImpl final: public ModuleRegistry {
void addBuiltinBundle(Bundle::Reader bundle) {
for (auto module: bundle.getModules()) {
// TODO: asChars() might be wrong for wide characters
addBuiltinModule(module.getName(), module.getSrc().asChars(),
module.getInternal() ? Type::INTERNAL : Type::BUILTIN);
addBuiltinModule(module.getName(), module.getSrc().asChars(), module.getType());
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/workerd/jsg/rtti.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using Cxx = import "/capnp/c++.capnp";
$Cxx.namespace("workerd::jsg::rtti");
$Cxx.allowCancellation;
# TODO: I can't figure out how to make both capnpc-ts and capnpc-cpp generators to see this import
# without code changes. capnpc-ts code is weird:
# https://github.com/jdiaz5513/capnp-ts/blob/master/packages/capnpc-ts/src/generators.ts#L92
# using Modules = import "/workerd/jsg/modules.capnp";

struct Type {
# A description of the C++ type.
Expand Down Expand Up @@ -277,10 +281,12 @@ struct Constructor {

struct Module {
specifier @0 :Text;
internal @1 :Bool;
# if anyone ever needs module type, it can be implemented by either fixing the Modules reference
# problem above or copying the original enum.
# type @1 :Modules.ModuleType;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as of now it is not used anywhere so I wanted to stop banging my head on this.

union {
structureName @2 :Text;
tsDeclarations @3 :Text;
structureName @1 :Text;
tsDeclarations @2 :Text;
}
}

Expand Down