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

Merge import.meta.require and require to be the same thing #3732

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion .vscode/launch.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/bun.js/bindings/BunPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,10 @@ EncodedJSValue BunPlugin::OnLoad::run(JSC::JSGlobalObject* globalObject, BunStri

if (auto* promise = JSC::jsDynamicCast<JSPromise*>(result)) {
switch (promise->status(vm)) {
case JSPromise::Status::Rejected:
case JSPromise::Status::Pending: {
return JSValue::encode(promise);
}
case JSPromise::Status::Rejected: {
promise->internalField(JSC::JSPromise::Field::Flags).set(vm, promise, jsNumber(static_cast<unsigned>(JSC::JSPromise::Status::Fulfilled)));
result = promise->result(vm);
return JSValue::encode(result);
}
case JSPromise::Status::Fulfilled: {
result = promise->result(vm);
break;
Expand Down
177 changes: 91 additions & 86 deletions src/bun.js/bindings/CommonJSModuleRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,98 +164,74 @@ JSC_DEFINE_HOST_FUNCTION(requireResolvePathsFunction, (JSGlobalObject * globalOb
return JSValue::encode(JSC::constructEmptyArray(globalObject, nullptr, 0));
}

static const HashTableValue RequireResolveFunctionPrototypeValues[] = {
{ "paths"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, requireResolvePathsFunction, 1 } },
};

class RequireResolveFunctionPrototype final : public JSC::JSNonFinalObject {
public:
using Base = JSC::JSNonFinalObject;
static RequireResolveFunctionPrototype* create(
JSC::JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();

auto* structure = RequireResolveFunctionPrototype::createStructure(vm, globalObject, globalObject->functionPrototype());
RequireResolveFunctionPrototype* prototype = new (NotNull, JSC::allocateCell<RequireResolveFunctionPrototype>(vm)) RequireResolveFunctionPrototype(vm, structure);
prototype->finishCreation(vm);
return prototype;
}

DECLARE_INFO;
JSC_DEFINE_CUSTOM_GETTER(jsRequireCacheGetter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
{
Zig::GlobalObject* thisObject = jsCast<Zig::GlobalObject*>(globalObject);
return JSValue::encode(thisObject->lazyRequireCacheObject());
}

RequireResolveFunctionPrototype(
JSC::VM& vm,
JSC::Structure* structure)
: Base(vm, structure)
{
}
JSC_DEFINE_CUSTOM_SETTER(jsRequireCacheSetter,
(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
JSC::EncodedJSValue value, JSC::PropertyName propertyName))
{
JSObject* thisObject = jsDynamicCast<JSObject*>(JSValue::decode(thisValue));
if (!thisObject)
return false;

template<typename CellType, JSC::SubspaceAccess>
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
return &vm.plainObjectSpace();
}
thisObject->putDirect(globalObject->vm(), propertyName, JSValue::decode(value), 0);
return true;
}

void finishCreation(JSC::VM& vm);
static const HashTableValue RequireResolveFunctionPrototypeValues[] = {
{ "paths"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, requireResolvePathsFunction, 1 } },
};

static const HashTableValue RequireFunctionPrototypeValues[] = {
{ "cache"_s, static_cast<unsigned>(JSC::PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, Zig::jsRequireCacheGetter, Zig::jsRequireCacheSetter } },
{ "cache"_s, static_cast<unsigned>(JSC::PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, jsRequireCacheGetter, jsRequireCacheSetter } },
};

class RequireFunctionPrototype final : public JSC::JSNonFinalObject {
public:
using Base = JSC::JSNonFinalObject;
static RequireFunctionPrototype* create(
JSC::JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();
RequireResolveFunctionPrototype* RequireResolveFunctionPrototype::create(JSC::JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();

auto* structure = RequireFunctionPrototype::createStructure(vm, globalObject, globalObject->functionPrototype());
RequireFunctionPrototype* prototype = new (NotNull, JSC::allocateCell<RequireFunctionPrototype>(vm)) RequireFunctionPrototype(vm, structure);
prototype->finishCreation(vm);
auto* structure = RequireResolveFunctionPrototype::createStructure(vm, globalObject, globalObject->functionPrototype());
RequireResolveFunctionPrototype* prototype = new (NotNull, JSC::allocateCell<RequireResolveFunctionPrototype>(vm)) RequireResolveFunctionPrototype(vm, structure);
prototype->finishCreation(vm);
return prototype;
}

JSFunction* resolveFunction = JSFunction::create(vm, moduleRequireResolveCodeGenerator(vm), globalObject->globalScope(), JSFunction::createStructure(vm, globalObject, RequireResolveFunctionPrototype::create(globalObject)));
prototype->putDirect(vm, JSC::Identifier::fromString(vm, "resolve"_s), resolveFunction, PropertyAttribute::Function | 0);
RequireFunctionPrototype* RequireFunctionPrototype::create(
JSC::JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();

return prototype;
}
auto* structure = RequireFunctionPrototype::createStructure(vm, globalObject, globalObject->functionPrototype());
RequireFunctionPrototype* prototype = new (NotNull, JSC::allocateCell<RequireFunctionPrototype>(vm)) RequireFunctionPrototype(vm, structure);
prototype->finishCreation(vm);

RequireFunctionPrototype(
JSC::VM& vm,
JSC::Structure* structure)
: Base(vm, structure)
{
}
prototype->putDirect(vm, JSC::Identifier::fromString(vm, "resolve"_s), static_cast<Zig::GlobalObject*>(globalObject)->requireResolveFunctionUnbound(), PropertyAttribute::Function | 0);

DECLARE_INFO;

template<typename CellType, JSC::SubspaceAccess>
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
return &vm.plainObjectSpace();
}
return prototype;
}

void finishCreation(JSC::VM& vm)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
void RequireFunctionPrototype::finishCreation(JSC::VM& vm)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));

reifyStaticProperties(vm, info(), RequireFunctionPrototypeValues, *this);
JSC::JSFunction* requireDotMainFunction = JSFunction::create(
vm,
moduleMainCodeGenerator(vm),
globalObject()->globalScope());
reifyStaticProperties(vm, info(), RequireFunctionPrototypeValues, *this);
JSC::JSFunction* requireDotMainFunction = JSFunction::create(
vm,
moduleMainCodeGenerator(vm),
globalObject()->globalScope());

this->putDirect(
vm,
JSC::Identifier::fromString(vm, "main"_s),
JSC::GetterSetter::create(vm, globalObject(), requireDotMainFunction, JSValue()),
PropertyAttribute::Builtin | PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | 0);
this->putDirect(vm, JSC::Identifier::fromString(vm, "extensions"_s), constructEmptyObject(globalObject()), 0);
}
};
this->putDirect(
vm,
JSC::Identifier::fromString(vm, "main"_s),
JSC::GetterSetter::create(vm, globalObject(), requireDotMainFunction, JSValue()),
PropertyAttribute::Builtin | PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | 0);
this->putDirect(vm, JSC::Identifier::fromString(vm, "extensions"_s), constructEmptyObject(globalObject()), 0);
}

JSC_DEFINE_CUSTOM_GETTER(getterFilename, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
{
Expand Down Expand Up @@ -406,13 +382,7 @@ class JSCommonJSModulePrototype final : public JSC::JSNonFinalObject {
ASSERT(inherits(vm, info()));
reifyStaticProperties(vm, JSCommonJSModule::info(), JSCommonJSModulePrototypeTableValues, *this);

JSFunction* requireFunction = JSFunction::create(
vm,
moduleRequireCodeGenerator(vm),
globalObject->globalScope(),
JSFunction::createStructure(vm, globalObject, RequireFunctionPrototype::create(globalObject)));

this->putDirect(vm, clientData(vm)->builtinNames().requirePublicName(), requireFunction, PropertyAttribute::Builtin | PropertyAttribute::Function | 0);
this->putDirect(vm, clientData(vm)->builtinNames().requirePublicName(), (static_cast<Zig::GlobalObject*>(globalObject))->requireFunctionUnbound(), PropertyAttribute::Builtin | PropertyAttribute::Function | 0);

this->putDirectNativeFunction(
vm,
Expand Down Expand Up @@ -485,15 +455,14 @@ JSCommonJSModule* JSCommonJSModule::create(
JSString* requireMapKey = JSC::jsStringWithCache(vm, key);
auto index = key.reverseFind('/', key.length());
JSString* dirname = jsEmptyString(vm);
JSString* filename = requireMapKey;
if (index != WTF::notFound) {
dirname = JSC::jsSubstring(globalObject, requireMapKey, 0, index);
}

auto* out = JSCommonJSModule::create(
vm,
globalObject->CommonJSModuleObjectStructure(),
requireMapKey, filename, dirname, nullptr);
requireMapKey, requireMapKey, dirname, nullptr);

out->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), exportsObject, exportsObject.isCell() && exportsObject.isCallable() ? JSC::PropertyAttribute::Function | 0 : 0);
out->hasEvaluated = hasEvaluated;
Expand Down Expand Up @@ -969,4 +938,40 @@ std::optional<JSC::SourceCode> createCommonJSModule(
sourceOrigin,
sourceURL));
}
}

JSObject* JSCommonJSModule::createBoundRequireFunction(VM& vm, JSGlobalObject* lexicalGlobalObject, const WTF::String& pathString)
{
auto* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject);

JSString* filename = JSC::jsStringWithCache(vm, pathString);
auto index = pathString.reverseFind('/', pathString.length());
JSString* dirname = jsEmptyString(vm);
if (index != WTF::notFound) {
dirname = JSC::jsSubstring(globalObject, filename, 0, index);
}

auto moduleObject = Bun::JSCommonJSModule::create(
vm,
globalObject->CommonJSModuleObjectStructure(),
filename, filename, dirname, nullptr);

auto& builtinNames = WebCore::builtinNames(vm);

JSFunction* requireFunction = JSC::JSBoundFunction::create(vm,
globalObject,
globalObject->requireFunctionUnbound(),
moduleObject,
ArgList(), 1, jsString(vm, String("require"_s)));

JSFunction* resolveFunction = JSC::JSBoundFunction::create(vm,
globalObject,
globalObject->requireResolveFunctionUnbound(),
moduleObject,
ArgList(), 1, jsString(vm, String("require"_s)));

requireFunction->putDirect(vm, builtinNames.resolvePublicName(), resolveFunction, PropertyAttribute::Function | 0);

return requireFunction;
}

} // namespace Bun
49 changes: 49 additions & 0 deletions src/bun.js/bindings/CommonJSModuleRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class JSCommonJSModule final : public JSC::JSDestructibleObject {

bool evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, ResolvedSource resolvedSource);
bool evaluate(Zig::GlobalObject* globalObject, const WTF::String& key, const SyntheticSourceProvider::SyntheticSourceGenerator& generator);
bool evaluate(Zig::GlobalObject* globalObject, const WTF::String& key, JSSourceCode* sourceCode);

static JSCommonJSModule* create(JSC::VM& vm, JSC::Structure* structure,
JSC::JSString* id,
Expand All @@ -56,6 +57,8 @@ class JSCommonJSModule final : public JSC::JSDestructibleObject {
const WTF::String& key,
ResolvedSource resolvedSource);

static JSObject* createBoundRequireFunction(VM& vm, JSGlobalObject* lexicalGlobalObject, const WTF::String& pathString);

void toSyntheticSource(JSC::JSGlobalObject* globalObject,
JSC::Identifier moduleKey,
Vector<JSC::Identifier, 4>& exportNames,
Expand Down Expand Up @@ -95,4 +98,50 @@ std::optional<JSC::SourceCode> createCommonJSModule(
Zig::GlobalObject* globalObject,
ResolvedSource source);

class RequireResolveFunctionPrototype final : public JSC::JSNonFinalObject {
public:
using Base = JSC::JSNonFinalObject;
static RequireResolveFunctionPrototype* create(JSC::JSGlobalObject* globalObject);

DECLARE_INFO;

RequireResolveFunctionPrototype(
JSC::VM& vm,
JSC::Structure* structure)
: Base(vm, structure)
{
}

template<typename CellType, JSC::SubspaceAccess>
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
return &vm.plainObjectSpace();
}

void finishCreation(JSC::VM& vm);
};

class RequireFunctionPrototype final : public JSC::JSNonFinalObject {
public:
using Base = JSC::JSNonFinalObject;
static RequireFunctionPrototype* create(JSC::JSGlobalObject* globalObject);

RequireFunctionPrototype(
JSC::VM& vm,
JSC::Structure* structure)
: Base(vm, structure)
{
}

template<typename CellType, JSC::SubspaceAccess>
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
return &vm.plainObjectSpace();
}

DECLARE_INFO;

void finishCreation(JSC::VM& vm);
};

} // namespace Bun
Loading