Skip to content

Commit

Permalink
Make builtins' source origin use a valid url (oven-sh#4152)
Browse files Browse the repository at this point in the history
* Make source origin use a valid url

* disable minifyWhiteSpace

* the change
  • Loading branch information
paperclover authored Aug 15, 2023
1 parent eab7b4c commit 47450ed
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 275 deletions.
72 changes: 37 additions & 35 deletions src/bun.js/bindings/InternalModuleRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,35 @@ static void maybeAddCodeCoverage(JSC::VM& vm, const JSC::SourceCode& code)
// JS builtin that acts as a module. In debug mode, we use a different implementation that reads
// from the developer's filesystem. This allows reloading code without recompiling bindings.

#define INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, SOURCE, moduleName) \
auto throwScope = DECLARE_THROW_SCOPE(vm); \
auto&& origin = SourceOrigin(WTF::URL(makeString("builtin://"_s, moduleName))); \
SourceCode source = JSC::makeSource(SOURCE, origin, moduleName); \
maybeAddCodeCoverage(vm, source); \
JSFunction* func \
= JSFunction::create( \
vm, \
createBuiltinExecutable( \
vm, source, \
Identifier(), \
ImplementationVisibility::Public, \
ConstructorKind::None, \
ConstructAbility::CannotConstruct) \
->link(vm, nullptr, source), \
static_cast<JSC::JSGlobalObject*>(globalObject)); \
\
RETURN_IF_EXCEPTION(throwScope, {}); \
\
JSC::MarkedArgumentBuffer argList; \
JSValue result = JSC::call( \
globalObject, \
func, \
JSC::getCallData(func), \
globalObject, JSC::MarkedArgumentBuffer()); \
\
RETURN_IF_EXCEPTION(throwScope, {}); \
ASSERT_INTERNAL_MODULE(result, moduleName); \
#define INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, SOURCE, moduleName, urlString) \
auto throwScope = DECLARE_THROW_SCOPE(vm); \
auto&& origin = SourceOrigin(WTF::URL(urlString)); \
SourceCode source = JSC::makeSource(SOURCE, origin, moduleName); \
maybeAddCodeCoverage(vm, source); \
JSFunction* func \
= JSFunction::create( \
vm, \
createBuiltinExecutable( \
vm, source, \
Identifier(), \
ImplementationVisibility::Public, \
ConstructorKind::None, \
ConstructAbility::CannotConstruct) \
->link(vm, nullptr, source), \
static_cast<JSC::JSGlobalObject*>(globalObject)); \
\
RETURN_IF_EXCEPTION(throwScope, {}); \
\
JSC::MarkedArgumentBuffer argList; \
JSValue result = JSC::profiledCall( \
globalObject, \
ProfilingReason::Other, \
func, \
JSC::getCallData(func), \
globalObject, JSC::MarkedArgumentBuffer()); \
\
RETURN_IF_EXCEPTION(throwScope, {}); \
ASSERT_INTERNAL_MODULE(result, moduleName); \
return result;

#if BUN_DEBUG
Expand All @@ -71,29 +72,30 @@ JSValue initializeInternalModuleFromDisk(
VM& vm,
WTF::String moduleName,
WTF::String fileBase,
WTF::String fallback)
WTF::String fallback,
WTF::String urlString)
{
WTF::String file = makeString(BUN_DYNAMIC_JS_LOAD_PATH, "modules_dev/"_s, fileBase);
if (auto contents = WTF::FileSystemImpl::readEntireFile(file)) {
auto string = WTF::String::fromUTF8(contents.value());
INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, string, moduleName);
INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, string, moduleName, urlString);
} else {
printf("bun-debug failed to load bundled version of \"%s\" at \"%s\" (was it deleted?)\n"
"Please run `make js` to rebundle these builtins.\n",
moduleName.utf8().data(), file.utf8().data());
// Fallback to embedded source
INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, fallback, moduleName);
INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, fallback, moduleName, urlString);
}
}
#define INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, moduleId, filename, SOURCE) \
return initializeInternalModuleFromDisk(globalObject, vm, moduleId, filename, SOURCE)
#define INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, moduleId, filename, SOURCE, urlString) \
return initializeInternalModuleFromDisk(globalObject, vm, moduleId, filename, SOURCE, urlString)
#else

#define ASSERT_INTERNAL_MODULE(result, moduleName) \
{ \
}
#define INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, moduleId, filename, SOURCE) \
INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, SOURCE, moduleId)
#define INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, moduleId, filename, SOURCE, urlString) \
INTERNAL_MODULE_REGISTRY_GENERATE_(globalObject, vm, SOURCE, moduleId, urlString)
#endif

const ClassInfo InternalModuleRegistry::s_info = { "InternalModuleRegistry"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(InternalModuleRegistry) };
Expand Down
25 changes: 15 additions & 10 deletions src/js/_codegen/build-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ mark("Preprocess modules");

const config = ({ platform, debug }: { platform: string; debug?: boolean }) => ({
entrypoints: bundledEntryPoints,
minify: { syntax: true, whitespace: !debug },
// Whitespace and identifiers are not minified to give better error messages when an error happens in our builtins
minify: { syntax: true, whitespace: false },
root: TMP,
define: {
IS_BUN_DEVELOPMENT: String(!!debug),
Expand Down Expand Up @@ -250,7 +251,8 @@ fs.writeFileSync(
// This code slice is used in InternalModuleRegistry.cpp. It defines the loading function for modules.
fs.writeFileSync(
path.join(BASE, "out/InternalModuleRegistry+createInternalModuleById.h"),
`JSValue InternalModuleRegistry::createInternalModuleById(JSGlobalObject* globalObject, VM& vm, Field id)
`// clang-format off
JSValue InternalModuleRegistry::createInternalModuleById(JSGlobalObject* globalObject, VM& vm, Field id)
{
switch (id) {
// JS internal modules
Expand All @@ -259,7 +261,9 @@ fs.writeFileSync(
return `case Field::${idToEnumName(id)}: {
INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, "${idToPublicSpecifierOrEnumName(id)}"_s, ${JSON.stringify(
id.replace(/\.[mc]?[tj]s$/, ".js"),
)}_s, InternalModuleRegistryConstants::${idToEnumName(id)}Code);
)}_s, InternalModuleRegistryConstants::${idToEnumName(id)}Code, "builtin://${id
.replace(/\.[mc]?[tj]s$/, "")
.replace(/[^a-zA-Z0-9]+/g, "/")}"_s);
}`;
})
.join("\n ")}
Expand All @@ -272,12 +276,13 @@ fs.writeFileSync(
// It inlines all the strings for the module IDs.
fs.writeFileSync(
path.join(BASE, "out/InternalModuleRegistryConstants.h"),
`#pragma once
`// clang-format off
#pragma once
namespace Bun {
namespace InternalModuleRegistryConstants {
namespace Bun {
namespace InternalModuleRegistryConstants {
#if __APPLE__
#if __APPLE__
${moduleList
.map(
(id, n) =>
Expand Down Expand Up @@ -308,10 +313,10 @@ static constexpr ASCIILiteral ${idToEnumName(id)}Code = ${fmtCPPString(bundledOu
`,
)
.join("\n")}
#endif
#endif
}
}`,
}
}`,
);

// This is a generated enum for zig code (exports.zig)
Expand Down
Loading

0 comments on commit 47450ed

Please sign in to comment.