From ff635551436123022ba3980b39580d53973c80a2 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sat, 24 Jun 2023 06:02:16 -0700 Subject: Rewrite Bun's runtime CommonJS loader (#3379) * wip changes for CommonJS * this rewrite is almost complete * even more code * wip * Remove usages of `import.meta.require` from builtins * Remove usages of require * Regenerate * :scissors: builtin rewrite commonjs in printer * Use lazy custom getters for import.meta * fixups * Remove depd * ugh * still crashing * fixup undici * comment out import.meta.require.resolve temporarily not a real solution but it stops the crashes * Redo import.meta.primordials * Builtins now have a `builtin://` protocol in source origin * Seems to work? * Finsih getting rid of primordials * switcharoo * No more function * just one more bug * Update launch.json * Implement `require.main` * :scissors: * Bump WebKit * Fixup import cycles * Fixup improt cycles * export more things * Implement `createCommonJSModule` builtin * More exports * regenerate * i broke some stuff * some of these tests work now * We lost the encoding * Sort of fix zlib * Sort of fix util * Update events.js * bump * bump * bump * Fix missing export in fs * fix some bugs with builtin esm modules (stream, worker_threads, events). its not perfect yet. * fix some other internal module bugs * oops * fix some extra require default stuff * uncomment this file but it crsahes on my machine * tidy code here * fixup tls exports * make simdutf happier * Add hasPrefix binding * Add test for `require.main` * Fix CommonJS evaluation order race condition * Make node:http load faster * Add missing exports to tls.js * Use the getter * Regenerate builtins * Fix assertion failure in Bun.write() * revamp dotEnv parser (#3347) - fixes `strings.indexOfAny()` - fixes OOB array access fixes #411 fixes #2823 fixes #3042 * fix tests for `expect()` (#3384) - extend test job time-out for `darwin-aarch64` * `expect().resolves` and `expect().rejects` (#3318) * Move expect and snapshots to their own files * expect().resolves and expect().rejects * Fix promise being added to unhandled rejection list * Handle timeouts in expect() * wip merge * Fix merge issue --------- Co-authored-by: Jarred Sumner Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> * fixup min/memcopy (#3388) * Fix crash in builtins * Don't attempt to evaluate modules with no source code * Update WebCoreJSBuiltins.cpp * Update WebCoreJSBuiltins.cpp * Update WebCoreJSBuiltins.cpp * Fix crash * cleanup * Fix test cc @paperdave * Fixup Undici * Fix issue in node:http * Create util-deprecate.mjs * Fix several bugs * Use the identifier * Support error.code in `util.deprecate` * make the CJs loader slightly more resilient * Update WebCoreJSBuiltins.cpp * Fix macros --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: dave caruso Co-authored-by: Alex Lam S.L Co-authored-by: Ashcon Partovi Co-authored-by: Ciro Spaciari --- src/bun.js/bindings/CommonJSModuleRecord.cpp | 994 ++++++++++++++++++--------- 1 file changed, 659 insertions(+), 335 deletions(-) (limited to 'src/bun.js/bindings/CommonJSModuleRecord.cpp') diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index 1cee1091b..8d4fe0a1e 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -59,458 +59,782 @@ #include #include +#include "ModuleLoader.h" #include #include #include #include "ZigSourceProvider.h" +#include "JavaScriptCore/FunctionPrototype.h" +#include "CommonJSModuleRecord.h" +#include +#include +#include namespace Bun { using namespace JSC; -class JSCommonJSModule final : public JSC::JSNonFinalObject { -public: - using Base = JSC::JSNonFinalObject; - static constexpr unsigned StructureFlags = Base::StructureFlags | JSC::OverridesPut; +JSC_DECLARE_HOST_FUNCTION(jsFunctionRequireCommonJS); - mutable JSC::WriteBarrier m_exportsObject; - mutable JSC::WriteBarrier m_id; +static bool canPerformFastEnumeration(Structure* s) +{ + if (s->typeInfo().overridesGetOwnPropertySlot()) + return false; + if (s->typeInfo().overridesAnyFormOfGetOwnPropertyNames()) + return false; + if (hasIndexedProperties(s->indexingType())) + return false; + if (s->hasAnyKindOfGetterSetterProperties()) + return false; + if (s->isUncacheableDictionary()) + return false; + if (s->hasUnderscoreProtoPropertyExcludingOriginalProto()) + return false; + return true; +} - void finishCreation(JSC::VM& vm, JSC::JSValue exportsObject, JSC::JSString* id, JSC::JSString* filename, JSC::JSString* dirname, JSC::JSValue requireFunction) - { - Base::finishCreation(vm); - ASSERT(inherits(vm, info())); - m_exportsObject.set(vm, this, exportsObject); - m_id.set(vm, this, id); +static bool evaluateCommonJSModuleOnce(JSC::VM& vm, Zig::GlobalObject* globalObject, JSCommonJSModule* moduleObject, JSString* dirname, JSString* filename, WTF::NakedPtr& exception) +{ + JSC::Structure* thisObjectStructure = globalObject->commonJSFunctionArgumentsStructure(); + JSC::JSObject* thisObject = JSC::constructEmptyObject( + vm, + thisObjectStructure); + thisObject->putDirectOffset( + vm, + 0, + moduleObject); - this->putDirectOffset( - vm, - 0, - exportsObject); + thisObject->putDirectOffset( + vm, + 1, + dirname); - this->putDirectOffset( - vm, - 1, - id); + thisObject->putDirectOffset( + vm, + 2, + filename); - this->putDirectOffset( - vm, - 2, - filename); + moduleObject->hasEvaluated = true; + globalObject->m_BunCommonJSModuleValue.set(vm, globalObject, thisObject); - this->putDirectOffset( - vm, - 3, - jsBoolean(false)); + JSValue empty = JSC::evaluate(globalObject, moduleObject->sourceCode.get()->sourceCode(), thisObject, exception); + moduleObject->sourceCode.clear(); - this->putDirectOffset( - vm, - 4, - dirname); + return exception.get() == nullptr; +} - this->putDirectOffset( - vm, - 5, - jsUndefined()); +JSC_DEFINE_HOST_FUNCTION(jsFunctionLoadModule, (JSGlobalObject * lexicalGlobalObject, CallFrame* callframe)) +{ + auto* globalObject = jsCast(lexicalGlobalObject); + auto throwScope = DECLARE_THROW_SCOPE(globalObject->vm()); + JSCommonJSModule* moduleObject = jsDynamicCast(callframe->argument(0)); + if (!moduleObject) { + RELEASE_AND_RETURN(throwScope, JSValue::encode(jsBoolean(true))); + } + + if (moduleObject->hasEvaluated || !moduleObject->sourceCode) { + RELEASE_AND_RETURN(throwScope, JSValue::encode(jsBoolean(true))); } - static JSC::Structure* createStructure( + WTF::NakedPtr exception; + + evaluateCommonJSModuleOnce( + globalObject->vm(), + jsCast(globalObject), + moduleObject, + moduleObject->m_dirname.get(), + moduleObject->m_filename.get(), + exception); + + if (exception.get()) { + // On error, remove the module from the require map/ + // so that it can be re-evaluated on the next require. + globalObject->requireMap()->remove(globalObject, moduleObject->id()); + + throwException(globalObject, throwScope, exception.get()); + exception.clear(); + return JSValue::encode({}); + } + + RELEASE_AND_RETURN(throwScope, JSValue::encode(jsBoolean(true))); +} + +JSC_DEFINE_HOST_FUNCTION(requireResolvePathsFunction, (JSGlobalObject * globalObject, CallFrame* callframe)) +{ + return JSValue::encode(JSC::constructEmptyArray(globalObject, nullptr, 0)); +} + +static const HashTableValue RequireResolveFunctionPrototypeValues[] = { + { "paths"_s, static_cast(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(); - JSC::Structure* structure = JSC::Structure::create( - vm, - globalObject, - globalObject->objectPrototype(), - JSC::TypeInfo(JSC::ObjectType, JSCommonJSModule::StructureFlags), - JSCommonJSModule::info(), - JSC::NonArray, - 6); - JSC::PropertyOffset offset; - auto clientData = WebCore::clientData(vm); + auto* structure = RequireResolveFunctionPrototype::createStructure(vm, globalObject, globalObject->functionPrototype()); + RequireResolveFunctionPrototype* prototype = new (NotNull, JSC::allocateCell(vm)) RequireResolveFunctionPrototype(vm, structure); + prototype->finishCreation(vm); + return prototype; + } - structure = structure->addPropertyTransition( - vm, - structure, - JSC::Identifier::fromString(vm, "exports"_s), - 0, - offset); + DECLARE_INFO; - structure = structure->addPropertyTransition( - vm, - structure, - JSC::Identifier::fromString(vm, "id"_s), - 0, - offset); + RequireResolveFunctionPrototype( + JSC::VM& vm, + JSC::Structure* structure) + : Base(vm, structure) + { + } - structure = structure->addPropertyTransition( - vm, - structure, - JSC::Identifier::fromString(vm, "filename"_s), - 0, - offset); + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + return &vm.plainObjectSpace(); + } - structure = structure->addPropertyTransition( - vm, - structure, - JSC::Identifier::fromString(vm, "loaded"_s), - 0, - offset); + void finishCreation(JSC::VM& vm); +}; - structure = structure->addPropertyTransition( - vm, - structure, - JSC::Identifier::fromString(vm, "path"_s), - 0, - offset); +static const HashTableValue RequireFunctionPrototypeValues[] = { + { "cache"_s, static_cast(JSC::PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, Zig::jsRequireCacheGetter, Zig::jsRequireCacheSetter } }, +}; - structure = structure->addPropertyTransition( - vm, - structure, - JSC::Identifier::fromString(vm, "require"_s), - 0, - offset); +class RequireFunctionPrototype final : public JSC::JSNonFinalObject { +public: + using Base = JSC::JSNonFinalObject; + static RequireFunctionPrototype* create( + JSC::JSGlobalObject* globalObject) + { + auto& vm = globalObject->vm(); + + auto* structure = RequireFunctionPrototype::createStructure(vm, globalObject, globalObject->functionPrototype()); + RequireFunctionPrototype* prototype = new (NotNull, JSC::allocateCell(vm)) RequireFunctionPrototype(vm, structure); + prototype->finishCreation(vm); - return structure; + 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); + + return prototype; } - static JSCommonJSModule* create( + RequireFunctionPrototype( JSC::VM& vm, - JSC::Structure* structure, - JSC::JSValue exportsObject, - JSC::JSString* id, - JSC::JSString* filename, - JSC::JSString* dirname, - JSC::JSValue requireFunction) + JSC::Structure* structure) + : Base(vm, structure) { - JSCommonJSModule* cell = new (NotNull, JSC::allocateCell(vm)) JSCommonJSModule(vm, structure); - cell->finishCreation(vm, exportsObject, id, filename, dirname, requireFunction); - return cell; } - JSValue exportsObject() + DECLARE_INFO; + + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) { - return m_exportsObject.get(); + return &vm.plainObjectSpace(); } - JSValue id() + void finishCreation(JSC::VM& vm) { - return m_id.get(); + Base::finishCreation(vm); + ASSERT(inherits(vm, info())); + + 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); } +}; - DECLARE_VISIT_CHILDREN; +JSC_DEFINE_CUSTOM_GETTER(getterFilename, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (UNLIKELY(!thisObject)) { + return JSValue::encode(jsUndefined()); + } + return JSValue::encode(thisObject->m_filename.get()); +} +JSC_DEFINE_CUSTOM_GETTER(getterId, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (UNLIKELY(!thisObject)) { + return JSValue::encode(jsUndefined()); + } + return JSValue::encode(thisObject->m_id.get()); +} - static bool put( - JSC::JSCell* cell, - JSC::JSGlobalObject* globalObject, - JSC::PropertyName propertyName, - JSC::JSValue value, - JSC::PutPropertySlot& slot) - { +JSC_DEFINE_CUSTOM_GETTER(getterPath, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (UNLIKELY(!thisObject)) { + return JSValue::encode(jsUndefined()); + } + return JSValue::encode(thisObject->m_id.get()); +} - auto& vm = globalObject->vm(); - auto* clientData = WebCore::clientData(vm); - auto throwScope = DECLARE_THROW_SCOPE(vm); +JSC_DEFINE_CUSTOM_SETTER(setterPath, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName propertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (!thisObject) + return false; - if (propertyName == clientData->builtinNames().exportsPublicName()) { - JSCommonJSModule* thisObject = jsCast(cell); - ASSERT_GC_OBJECT_INHERITS(thisObject, info()); + thisObject->m_id.set(globalObject->vm(), thisObject, JSValue::decode(value).toString(globalObject)); + return true; +} - // It will crash if we attempt to assign Object.defineProperty() result to a JSMap*. - if (UNLIKELY(slot.thisValue() != thisObject)) - RELEASE_AND_RETURN(throwScope, JSObject::definePropertyOnReceiver(globalObject, propertyName, value, slot)); +JSC_DEFINE_CUSTOM_SETTER(setterFilename, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName propertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (!thisObject) + return false; - JSValue prevValue = thisObject->m_exportsObject.get(); + thisObject->m_filename.set(globalObject->vm(), thisObject, JSValue::decode(value).toString(globalObject)); + return true; +} - // TODO: refactor this to not go through ESM path and we don't need to do this check. - // IF we do this on every call, it causes GC to happen in a place that it may not be able to. - // This breaks loading Bluebird in some cases, for example. - // We need to update the require map "live" because otherwise the code in Discord.js will break - // The bug is something to do with exception handling which causes GC to happen in the error path and then boom. - if (prevValue != value && (!prevValue.isCell() || !value.isCell() || prevValue.asCell()->type() != value.asCell()->type())) { - jsCast(globalObject)->requireMap()->set(globalObject, thisObject->id(), value); - } +JSC_DEFINE_CUSTOM_SETTER(setterId, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName propertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (!thisObject) + return false; - thisObject->m_exportsObject.set(vm, thisObject, value); - } + thisObject->m_id.set(globalObject->vm(), thisObject, JSValue::decode(value).toString(globalObject)); + return true; +} + +static JSValue createLoaded(VM& vm, JSObject* object) +{ + JSCommonJSModule* cjs = jsCast(object); + return jsBoolean(cjs->hasEvaluated); +} +static JSValue createParent(VM& vm, JSObject* object) +{ + return jsUndefined(); +} +static JSValue createChildren(VM& vm, JSObject* object) +{ + return constructEmptyArray(object->globalObject(), nullptr, 0); +} + +static const struct HashTableValue JSCommonJSModulePrototypeTableValues[] = { + { "children"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createChildren } }, + { "filename"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterFilename, setterFilename } }, + { "id"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterId, setterId } }, + { "loaded"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createLoaded } }, + { "parent"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createParent } }, + { "path"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPath, setterPath } }, +}; - RELEASE_AND_RETURN(throwScope, Base::put(cell, globalObject, propertyName, value, slot)); +class JSCommonJSModulePrototype final : public JSC::JSNonFinalObject { +public: + using Base = JSC::JSNonFinalObject; + static JSCommonJSModulePrototype* create( + JSC::VM& vm, + JSC::JSGlobalObject* globalObject, + JSC::Structure* structure) + { + JSCommonJSModulePrototype* prototype = new (NotNull, JSC::allocateCell(vm)) JSCommonJSModulePrototype(vm, structure); + prototype->finishCreation(vm, globalObject); + return prototype; } DECLARE_INFO; - template static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + + JSCommonJSModulePrototype( + JSC::VM& vm, + JSC::Structure* structure) + : Base(vm, structure) { - if constexpr (mode == JSC::SubspaceAccess::Concurrently) - return nullptr; - return WebCore::subspaceForImpl( - vm, - [](auto& spaces) { return spaces.m_clientSubspaceForCommonJSModuleRecord.get(); }, - [](auto& spaces, auto&& space) { spaces.m_clientSubspaceForCommonJSModuleRecord = std::forward(space); }, - [](auto& spaces) { return spaces.m_subspaceForCommonJSModuleRecord.get(); }, - [](auto& spaces, auto&& space) { spaces.m_subspaceForCommonJSModuleRecord = std::forward(space); }); } - JSCommonJSModule(JSC::VM& vm, JSC::Structure* structure) - : Base(vm, structure) + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) { + return &vm.plainObjectSpace(); + } + + void finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject) + { + Base::finishCreation(vm); + 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->putDirectNativeFunction( + vm, + globalObject, + clientData(vm)->builtinNames().requirePrivateName(), + 2, + jsFunctionRequireCommonJS, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0); } }; -Structure* createCommonJSModuleStructure( - Zig::GlobalObject* globalObject) +const JSC::ClassInfo JSCommonJSModulePrototype::s_info = { "Module"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCommonJSModulePrototype) }; + +void JSCommonJSModule::finishCreation(JSC::VM& vm, JSC::JSString* id, JSC::JSString* filename, JSC::JSString* dirname, JSC::JSSourceCode* sourceCode) { - return JSCommonJSModule::createStructure(globalObject); + Base::finishCreation(vm); + ASSERT(inherits(vm, info())); + m_id.set(vm, this, id); + m_filename.set(vm, this, filename); + m_dirname.set(vm, this, dirname); + this->sourceCode.set(vm, this, sourceCode); } -template -void JSCommonJSModule::visitChildrenImpl(JSCell* cell, Visitor& visitor) +JSC::Structure* JSCommonJSModule::createStructure( + JSC::JSGlobalObject* globalObject) { - JSCommonJSModule* thisObject = jsCast(cell); - ASSERT_GC_OBJECT_INHERITS(thisObject, info()); - Base::visitChildren(thisObject, visitor); - visitor.append(thisObject->m_exportsObject); - visitor.append(thisObject->m_id); -} + auto& vm = globalObject->vm(); -DEFINE_VISIT_CHILDREN(JSCommonJSModule); -const JSC::ClassInfo JSCommonJSModule::s_info = { "Module"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCommonJSModule) }; + auto* prototype = JSCommonJSModulePrototype::create(vm, globalObject, JSCommonJSModulePrototype::createStructure(vm, globalObject, globalObject->objectPrototype())); -static bool canPerformFastEnumeration(Structure* s) + // Do not set the number of inline properties on this structure + // there may be an off-by-one error in the Structure which causes `require.id` to become the require + return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info(), NonArray); +} + +JSCommonJSModule* JSCommonJSModule::create( + JSC::VM& vm, + JSC::Structure* structure, + JSC::JSString* id, + JSC::JSString* filename, + JSC::JSString* dirname, + JSC::JSSourceCode* sourceCode) { - if (s->typeInfo().overridesGetOwnPropertySlot()) - return false; - if (s->typeInfo().overridesAnyFormOfGetOwnPropertyNames()) - return false; - if (hasIndexedProperties(s->indexingType())) - return false; - if (s->hasAnyKindOfGetterSetterProperties()) - return false; - if (s->isUncacheableDictionary()) - return false; - if (s->hasUnderscoreProtoPropertyExcludingOriginalProto()) - return false; - return true; + JSCommonJSModule* cell = new (NotNull, JSC::allocateCell(vm)) JSCommonJSModule(vm, structure); + cell->finishCreation(vm, id, filename, dirname, sourceCode); + return cell; } -JSValue evaluateCommonJSModule( - Zig::GlobalObject* globalObject, - Ref sourceProvider, - const WTF::String& sourceURL, - ResolvedSource source) +JSC_DEFINE_HOST_FUNCTION(jsFunctionCreateCommonJSModule, (JSGlobalObject * globalObject, CallFrame* callframe)) { auto& vm = globalObject->vm(); - auto throwScope = DECLARE_THROW_SCOPE(vm); - auto* requireMapKey = jsString(vm, sourceURL); + auto id = callframe->argument(0).toWTFString(globalObject); - JSC::JSObject* exportsObject = source.commonJSExportsLen < 64 - ? JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), source.commonJSExportsLen) - : JSC::constructEmptyObject(globalObject, globalObject->objectPrototype()); - auto index = sourceURL.reverseFind('/', sourceURL.length()); + JSValue object = callframe->argument(1); + + return JSValue::encode( + JSCommonJSModule::create( + jsCast(globalObject), + id, + object, callframe->argument(2).isBoolean() && callframe->argument(2).asBoolean())); +} + +JSCommonJSModule* JSCommonJSModule::create( + Zig::GlobalObject* globalObject, + const WTF::String& key, + JSValue exportsObject, + bool hasEvaluated) +{ + auto& vm = globalObject->vm(); + 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); } - globalObject->requireMap()->set(globalObject, requireMapKey, exportsObject); - auto* requireFunction = Zig::ImportMetaObject::createRequireFunction(vm, globalObject, sourceURL); - - JSC::SourceCode inputSource( - WTFMove(sourceProvider)); - - auto* moduleObject = JSCommonJSModule::create( + auto* out = JSCommonJSModule::create( vm, globalObject->CommonJSModuleObjectStructure(), - exportsObject, - requireMapKey, filename, dirname, requireFunction); + requireMapKey, filename, dirname, nullptr); - if (UNLIKELY(throwScope.exception())) { - globalObject->requireMap()->remove(globalObject, requireMapKey); - RELEASE_AND_RETURN(throwScope, JSValue()); - } + out->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), exportsObject, exportsObject.isCell() && exportsObject.isCallable() ? JSC::PropertyAttribute::Function | 0 : 0); + out->hasEvaluated = hasEvaluated; + return out; +} - JSC::Structure* thisObjectStructure = globalObject->commonJSFunctionArgumentsStructure(); - JSC::JSObject* thisObject = JSC::constructEmptyObject( - vm, - thisObjectStructure); - thisObject->putDirectOffset( - vm, - 0, - moduleObject); +void JSCommonJSModule::destroy(JSC::JSCell* cell) +{ + static_cast(cell)->JSCommonJSModule::~JSCommonJSModule(); +} - thisObject->putDirectOffset( - vm, - 1, - exportsObject); +JSCommonJSModule::~JSCommonJSModule() +{ +} - thisObject->putDirectOffset( - vm, - 2, - dirname); +bool JSCommonJSModule::evaluate( + Zig::GlobalObject* globalObject, + const WTF::String& key, + const SyntheticSourceProvider::SyntheticSourceGenerator& generator) +{ + Vector propertyNames; + JSC::MarkedArgumentBuffer arguments; + auto& vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + generator(globalObject, JSC::Identifier::fromString(vm, key), propertyNames, arguments); + RETURN_IF_EXCEPTION(throwScope, false); + + bool needsPut = false; + auto getDefaultValue = [&]() -> JSValue { + size_t defaultValueIndex = propertyNames.find(vm.propertyNames->defaultKeyword); + auto cjsSymbol = Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); + + if (defaultValueIndex != notFound && propertyNames.contains(cjsSymbol)) { + JSValue current = arguments.at(defaultValueIndex); + needsPut = true; + return current; + } - thisObject->putDirectOffset( - vm, - 3, - filename); + size_t count = propertyNames.size(); + JSValue existingDefaultObject = this->getIfPropertyExists(globalObject, WebCore::clientData(vm)->builtinNames().exportsPublicName()); + JSObject* defaultObject; - thisObject->putDirectOffset( - vm, - 4, - requireFunction); + if (existingDefaultObject && existingDefaultObject.isObject()) { + defaultObject = jsCast(existingDefaultObject); + } else { + defaultObject = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype()); + needsPut = true; + } - { - WTF::NakedPtr exception; - globalObject->m_BunCommonJSModuleValue.set(vm, globalObject, thisObject); - JSC::evaluate(globalObject, inputSource, globalObject->globalThis(), exception); - - if (exception.get()) { - throwScope.throwException(globalObject, exception->value()); - exception.clear(); - RELEASE_AND_RETURN(throwScope, JSValue()); + for (size_t i = 0; i < count; ++i) { + auto prop = propertyNames[i]; + unsigned attributes = 0; + + JSValue value = arguments.at(i); + + if (prop.isSymbol()) { + attributes |= JSC::PropertyAttribute::DontEnum; + } + + if (value.isCell() && value.isCallable()) { + attributes |= JSC::PropertyAttribute::Function; + } + + defaultObject->putDirect(vm, prop, value, attributes); } - } - if (UNLIKELY(throwScope.exception())) { - globalObject->requireMap()->remove(globalObject, requireMapKey); - RELEASE_AND_RETURN(throwScope, JSValue()); + return defaultObject; + }; + + JSValue defaultValue = getDefaultValue(); + if (needsPut) { + unsigned attributes = 0; + + if (defaultValue.isCell() && defaultValue.isCallable()) { + attributes |= JSC::PropertyAttribute::Function; + } + + this->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), defaultValue, attributes); } - JSValue result = moduleObject->exportsObject(); - - // The developer can do something like: - // - // Object.defineProperty(module, 'exports', {get: getter}) - // - // In which case, the exports object is now a GetterSetter object. - // - // We can't return a GetterSetter object to ESM code, so we need to call it. - if (!result.isEmpty() && (result.isGetterSetter() || result.isCustomGetterSetter())) { - auto* clientData = WebCore::clientData(vm); - - // TODO: is there a faster way to call these getters? We shouldn't need to do a full property lookup. - // - // we use getIfPropertyExists just incase a pathological devleoper did: - // - // - Object.defineProperty(module, 'exports', {get: getter}) - // - delete module.exports - // - if (result.isGetterSetter()) { - JSC::GetterSetter* getter = jsCast(result); - result = getter->callGetter(globalObject, moduleObject); + this->hasEvaluated = true; + RELEASE_AND_RETURN(throwScope, true); +} + +void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject, + JSC::Identifier moduleKey, + Vector& exportNames, + JSC::MarkedArgumentBuffer& exportValues) +{ + auto result = this->exportsObject(); + + auto& vm = globalObject->vm(); + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(result); + + // This exists to tell ImportMetaObject.ts that this is a CommonJS module. + exportNames.append(Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))); + exportValues.append(jsNumber(0)); + + if (result.isObject()) { + auto* exports = asObject(result); + + auto* structure = exports->structure(); + uint32_t size = structure->inlineSize() + structure->outOfLineSize(); + exportNames.reserveCapacity(size + 2); + exportValues.ensureCapacity(size + 2); + + if (canPerformFastEnumeration(structure)) { + exports->structure()->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { + auto key = entry.key(); + if (key->isSymbol() || key == vm.propertyNames->defaultKeyword || entry.attributes() & PropertyAttribute::DontEnum) + return true; + + exportNames.append(Identifier::fromUid(vm, key)); + + JSValue value = exports->getDirect(entry.offset()); + + exportValues.append(value); + return true; + }); } else { - result = moduleObject->getIfPropertyExists(globalObject, clientData->builtinNames().exportsPublicName()); - } + auto catchScope = DECLARE_CATCH_SCOPE(vm); + JSC::PropertyNameArray properties(vm, JSC::PropertyNameMode::Strings, JSC::PrivateSymbolMode::Exclude); + exports->methodTable()->getOwnPropertyNames(exports, globalObject, properties, DontEnumPropertiesMode::Exclude); + if (catchScope.exception()) { + catchScope.clearExceptionExceptTermination(); + return; + } + + for (auto property : properties) { + if (UNLIKELY(property.isEmpty() || property.isNull() || property.isPrivateName() || property.isSymbol())) + continue; + + // ignore constructor + if (property == vm.propertyNames->constructor || property == vm.propertyNames->defaultKeyword) + continue; + + JSC::PropertySlot slot(exports, PropertySlot::InternalMethodType::Get); + if (!exports->getPropertySlot(globalObject, property, slot)) + continue; + + exportNames.append(property); - if (UNLIKELY(throwScope.exception())) { - // Unlike getters on properties of the exports object - // When the exports object itself is a getter and it throws - // There's not a lot we can do - // so we surface that error - globalObject->requireMap()->remove(globalObject, requireMapKey); - RELEASE_AND_RETURN(throwScope, JSValue()); + JSValue getterResult = slot.getValue(globalObject, property); + + // If it throws, we keep them in the exports list, but mark it as undefined + // This is consistent with what Node.js does. + if (catchScope.exception()) { + catchScope.clearException(); + getterResult = jsUndefined(); + } + + exportValues.append(getterResult); + } } } +} - globalObject->requireMap()->set(globalObject, requireMapKey, result); +JSValue JSCommonJSModule::exportsObject() +{ + return this->get(globalObject(), JSC::PropertyName(clientData(vm())->builtinNames().exportsPublicName())); +} + +JSValue JSCommonJSModule::id() +{ + return m_id.get(); +} + +bool JSCommonJSModule::put( + JSC::JSCell* cell, + JSC::JSGlobalObject* globalObject, + JSC::PropertyName propertyName, + JSC::JSValue value, + JSC::PutPropertySlot& slot) +{ + + auto& vm = globalObject->vm(); + auto* clientData = WebCore::clientData(vm); + auto throwScope = DECLARE_THROW_SCOPE(vm); + + RELEASE_AND_RETURN(throwScope, Base::put(cell, globalObject, propertyName, value, slot)); +} + +template JSC::GCClient::IsoSubspace* JSCommonJSModule::subspaceFor(JSC::VM& vm) +{ + if constexpr (mode == JSC::SubspaceAccess::Concurrently) + return nullptr; + return WebCore::subspaceForImpl( + vm, + [](auto& spaces) { return spaces.m_clientSubspaceForCommonJSModuleRecord.get(); }, + [](auto& spaces, auto&& space) { spaces.m_clientSubspaceForCommonJSModuleRecord = std::forward(space); }, + [](auto& spaces) { return spaces.m_subspaceForCommonJSModuleRecord.get(); }, + [](auto& spaces, auto&& space) { spaces.m_subspaceForCommonJSModuleRecord = std::forward(space); }); +} + +Structure* createCommonJSModuleStructure( + Zig::GlobalObject* globalObject) +{ + return JSCommonJSModule::createStructure(globalObject); +} + +template +void JSCommonJSModule::visitChildrenImpl(JSCell* cell, Visitor& visitor) +{ + JSCommonJSModule* thisObject = jsCast(cell); + ASSERT_GC_OBJECT_INHERITS(thisObject, info()); + Base::visitChildren(thisObject, visitor); + visitor.append(thisObject->m_id); + visitor.append(thisObject->sourceCode); + visitor.append(thisObject->m_filename); + visitor.append(thisObject->m_dirname); +} + +DEFINE_VISIT_CHILDREN(JSCommonJSModule); +const JSC::ClassInfo JSCommonJSModule::s_info = { "Module"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCommonJSModule) }; +const JSC::ClassInfo RequireResolveFunctionPrototype::s_info = { "resolve"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(RequireResolveFunctionPrototype) }; +const JSC::ClassInfo RequireFunctionPrototype::s_info = { "require"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(RequireFunctionPrototype) }; + +JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireCommonJS, (JSGlobalObject * lexicalGlobalObject, CallFrame* callframe)) +{ + auto* globalObject = jsCast(lexicalGlobalObject); + auto& vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + + JSCommonJSModule* thisObject = jsDynamicCast(callframe->thisValue()); + if (!thisObject) + return throwVMTypeError(globalObject, throwScope); + + JSValue specifierValue = callframe->argument(0); + WTF::String specifier = specifierValue.toWTFString(globalObject); + RETURN_IF_EXCEPTION(throwScope, {}); + + WTF::String referrer = thisObject->id().toWTFString(globalObject); + RETURN_IF_EXCEPTION(throwScope, {}); + + BunString specifierStr = Bun::toString(specifier); + BunString referrerStr = Bun::toString(referrer); + + JSValue fetchResult = Bun::fetchCommonJSModule( + globalObject, + jsDynamicCast(callframe->argument(1)), + specifierValue, + &specifierStr, + &referrerStr); + + RELEASE_AND_RETURN(throwScope, JSValue::encode(fetchResult)); +} + +void RequireResolveFunctionPrototype::finishCreation(JSC::VM& vm) +{ + Base::finishCreation(vm); + ASSERT(inherits(vm, info())); - return result; + reifyStaticProperties(vm, RequireResolveFunctionPrototype::info(), RequireResolveFunctionPrototypeValues, *this); + JSC_TO_STRING_TAG_WITHOUT_TRANSITION(); } -JSC::SourceCode createCommonJSModule( +bool JSCommonJSModule::evaluate( Zig::GlobalObject* globalObject, + const WTF::String& key, ResolvedSource source) { - auto sourceURL = Zig::toStringCopy(source.source_url); - auto sourceProvider = Zig::SourceProvider::create(globalObject, source, JSC::SourceProviderSourceType::Program); + auto& vm = globalObject->vm(); + auto sourceProvider = Zig::SourceProvider::create(jsCast(globalObject), source, JSC::SourceProviderSourceType::Program); + JSC::SourceCode rawInputSource( + WTFMove(sourceProvider)); - return JSC::SourceCode( - JSC::SyntheticSourceProvider::create( - [source, sourceProvider = WTFMove(sourceProvider), sourceURL](JSC::JSGlobalObject* globalObject, - JSC::Identifier moduleKey, - Vector& exportNames, - JSC::MarkedArgumentBuffer& exportValues) -> void { - JSValue result = evaluateCommonJSModule( - jsCast(globalObject), - WTFMove(sourceProvider), - sourceURL, - source); - - if (!result) { - return; - } + if (this->hasEvaluated) + return true; - auto& vm = globalObject->vm(); + this->sourceCode.set(vm, this, JSC::JSSourceCode::create(vm, WTFMove(rawInputSource))); + WTF::NakedPtr exception; - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(result); - - // This exists to tell ImportMetaObject.ts that this is a CommonJS module. - exportNames.append(Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))); - exportValues.append(jsNumber(0)); - - if (result.isObject()) { - DeferGCForAWhile deferGC(vm); - auto* exports = asObject(result); - - auto* structure = exports->structure(); - uint32_t size = structure->inlineSize() + structure->outOfLineSize(); - exportNames.reserveCapacity(size + 2); - exportValues.ensureCapacity(size + 2); - - if (canPerformFastEnumeration(structure)) { - exports->structure()->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { - auto key = entry.key(); - if (key->isSymbol() || key == vm.propertyNames->defaultKeyword || entry.attributes() & PropertyAttribute::DontEnum) - return true; - - exportNames.append(Identifier::fromUid(vm, key)); - - JSValue value = exports->getDirect(entry.offset()); - - exportValues.append(value); - return true; - }); - } else { - auto catchScope = DECLARE_CATCH_SCOPE(vm); - JSC::PropertyNameArray properties(vm, JSC::PropertyNameMode::Strings, JSC::PrivateSymbolMode::Exclude); - exports->methodTable()->getOwnPropertyNames(exports, globalObject, properties, DontEnumPropertiesMode::Exclude); - if (catchScope.exception()) { - catchScope.clearExceptionExceptTermination(); - return; - } + evaluateCommonJSModuleOnce(vm, globalObject, this, this->m_dirname.get(), this->m_filename.get(), exception); - for (auto property : properties) { - if (UNLIKELY(property.isEmpty() || property.isNull() || property.isPrivateName() || property.isSymbol())) - continue; + if (exception.get()) { + // On error, remove the module from the require map/ + // so that it can be re-evaluated on the next require. + globalObject->requireMap()->remove(globalObject, this->id()); - // ignore constructor - if (property == vm.propertyNames->constructor || property == vm.propertyNames->defaultKeyword) - continue; + auto throwScope = DECLARE_THROW_SCOPE(vm); + throwException(globalObject, throwScope, exception.get()); + exception.clear(); - JSC::PropertySlot slot(exports, PropertySlot::InternalMethodType::Get); - if (!exports->getPropertySlot(globalObject, property, slot)) - continue; + return false; + } - exportNames.append(property); + return true; +} - JSValue getterResult = slot.getValue(globalObject, property); +std::optional createCommonJSModule( + Zig::GlobalObject* globalObject, + ResolvedSource source) +{ + JSCommonJSModule* moduleObject; + WTF::String sourceURL = toStringCopy(source.source_url); - // If it throws, we keep them in the exports list, but mark it as undefined - // This is consistent with what Node.js does. - if (catchScope.exception()) { - catchScope.clearException(); - getterResult = jsUndefined(); - } + JSValue specifierValue = Bun::toJS(globalObject, source.specifier); + JSValue entry = globalObject->requireMap()->get(globalObject, specifierValue); - exportValues.append(getterResult); + auto sourceProvider = Zig::SourceProvider::create(jsCast(globalObject), source, JSC::SourceProviderSourceType::Program); + SourceOrigin sourceOrigin = sourceProvider->sourceOrigin(); + + if (entry) { + moduleObject = jsDynamicCast(entry); + } + + if (!moduleObject) { + auto& vm = globalObject->vm(); + auto* requireMapKey = jsStringWithCache(vm, sourceURL); + auto index = sourceURL.reverseFind('/', sourceURL.length()); + JSString* dirname = jsEmptyString(vm); + JSString* filename = requireMapKey; + if (index != WTF::notFound) { + dirname = JSC::jsSubstring(globalObject, requireMapKey, 0, index); + } + + JSC::SourceCode rawInputSource( + WTFMove(sourceProvider)); + + moduleObject = JSCommonJSModule::create( + vm, + globalObject->CommonJSModuleObjectStructure(), + requireMapKey, filename, dirname, JSC::JSSourceCode::create(vm, WTFMove(rawInputSource))); + + moduleObject->putDirect(vm, + WebCore::clientData(vm)->builtinNames().exportsPublicName(), + JSC::constructEmptyObject(globalObject, globalObject->objectPrototype()), 0); + + globalObject->requireMap()->set(globalObject, requireMapKey, moduleObject); + } + + return JSC::SourceCode( + JSC::SyntheticSourceProvider::create( + [](JSC::JSGlobalObject* lexicalGlobalObject, + JSC::Identifier moduleKey, + Vector& exportNames, + JSC::MarkedArgumentBuffer& exportValues) -> void { + auto* globalObject = jsCast(lexicalGlobalObject); + auto& vm = globalObject->vm(); + + JSValue keyValue = identifierToJSValue(vm, moduleKey); + JSValue entry = globalObject->requireMap()->get(globalObject, keyValue); + + if (entry) { + if (auto* moduleObject = jsDynamicCast(entry)) { + if (!moduleObject->hasEvaluated) { + WTF::NakedPtr exception; + if (!evaluateCommonJSModuleOnce( + vm, + globalObject, + moduleObject, + moduleObject->m_dirname.get(), + moduleObject->m_filename.get(), exception)) { + + // On error, remove the module from the require map + // so that it can be re-evaluated on the next require. + globalObject->requireMap()->remove(globalObject, moduleObject->id()); + + auto scope = DECLARE_THROW_SCOPE(vm); + throwException(globalObject, scope, exception.get()); + exception.clear(); + return; + } } + + moduleObject->toSyntheticSource(globalObject, moduleKey, exportNames, exportValues); } } }, - SourceOrigin(WTF::URL::fileURLWithFileSystemPath(sourceURL)), + sourceOrigin, sourceURL)); } - } \ No newline at end of file -- cgit v1.2.3 From ec3ed67bc9ad8cbb0e59234564d57265d5423fce Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Mon, 26 Jun 2023 08:12:37 -0700 Subject: implement `_nodeModulePaths` and `require.main.paths` (#3411) * tests in progress * add `require.main.paths`, add every dir up to root * remove imports --- src/bun.js/bindings/CommonJSModuleRecord.cpp | 31 +++++++++ src/bun.js/bindings/CommonJSModuleRecord.h | 1 + src/bun.js/modules/NodeModuleModule.cpp | 15 ++-- src/resolver/resolver.zig | 95 ++++++++++++++++++++++++++ test/js/node/module/node-module-module.test.js | 19 ++++++ 5 files changed, 150 insertions(+), 11 deletions(-) (limited to 'src/bun.js/bindings/CommonJSModuleRecord.cpp') diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index 8d4fe0a1e..3615db774 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -295,6 +295,35 @@ JSC_DEFINE_CUSTOM_SETTER(setterPath, return true; } +extern "C" EncodedJSValue Resolver__propForRequireMainPaths(JSGlobalObject*); + +JSC_DEFINE_CUSTOM_GETTER(getterPaths, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (UNLIKELY(!thisObject)) { + return JSValue::encode(jsUndefined()); + } + + if (!thisObject->m_paths) { + JSValue paths = JSValue::decode(Resolver__propForRequireMainPaths(globalObject)); + thisObject->m_paths.set(globalObject->vm(), thisObject, paths); + } + + return JSValue::encode(thisObject->m_paths.get()); +} + +JSC_DEFINE_CUSTOM_SETTER(setterPaths, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName propertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (!thisObject) + return false; + + thisObject->m_paths.set(globalObject->vm(), thisObject, JSValue::decode(value)); + return true; +} + JSC_DEFINE_CUSTOM_SETTER(setterFilename, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName propertyName)) @@ -340,6 +369,7 @@ static const struct HashTableValue JSCommonJSModulePrototypeTableValues[] = { { "loaded"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createLoaded } }, { "parent"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createParent } }, { "path"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPath, setterPath } }, + { "paths"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPaths, setterPaths } }, }; class JSCommonJSModulePrototype final : public JSC::JSNonFinalObject { @@ -675,6 +705,7 @@ void JSCommonJSModule::visitChildrenImpl(JSCell* cell, Visitor& visitor) visitor.append(thisObject->sourceCode); visitor.append(thisObject->m_filename); visitor.append(thisObject->m_dirname); + visitor.append(thisObject->m_paths); } DEFINE_VISIT_CHILDREN(JSCommonJSModule); diff --git a/src/bun.js/bindings/CommonJSModuleRecord.h b/src/bun.js/bindings/CommonJSModuleRecord.h index 48f14b39c..a96ab5f75 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.h +++ b/src/bun.js/bindings/CommonJSModuleRecord.h @@ -24,6 +24,7 @@ public: mutable JSC::WriteBarrier m_id; mutable JSC::WriteBarrier m_filename; mutable JSC::WriteBarrier m_dirname; + mutable JSC::WriteBarrier m_paths; mutable JSC::WriteBarrier sourceCode; static void destroy(JSC::JSCell*); diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index 8b278ddd8..34d45698f 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -26,15 +26,8 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire, scope, JSValue::encode(Zig::ImportMetaObject::createRequireFunction( vm, globalObject, val))); } -JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePaths, - (JSC::JSGlobalObject * globalObject, - JSC::CallFrame *callFrame)) { - return JSC::JSValue::encode(JSC::JSArray::create( - globalObject->vm(), - globalObject->arrayStructureForIndexingTypeDuringAllocation( - ArrayWithContiguous), - 0)); -} +extern "C" EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *, + CallFrame *); JSC_DEFINE_HOST_FUNCTION(jsFunctionFindSourceMap, (JSGlobalObject * globalObject, @@ -114,7 +107,7 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, vm, globalObject, 1, String("createRequire"_s), jsFunctionNodeModuleCreateRequire, ImplementationVisibility::Public)); exportValues.append(JSFunction::create(vm, globalObject, 1, String("paths"_s), - jsFunctionNodeModulePaths, + Resolver__nodeModulePathsForJS, ImplementationVisibility::Public)); exportValues.append(JSFunction::create( vm, globalObject, 1, String("findSourceMap"_s), jsFunctionFindSourceMap, @@ -143,7 +136,7 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, exportNames.append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s)); exportValues.append(JSFunction::create( vm, globalObject, 0, String("_nodeModulePaths"_s), - jsFunctionNodeModulePaths, ImplementationVisibility::Public)); + Resolver__nodeModulePathsForJS, ImplementationVisibility::Public)); exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s)); exportValues.append( diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 14bc358d0..40b106f3a 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -94,6 +94,7 @@ const bufs = struct { threadlocal var remap_path_trailing_slash: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var path_in_global_disk_cache: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var abs_to_rel: [bun.MAX_PATH_BYTES]u8 = undefined; + threadlocal var node_modules_paths_buf: [bun.MAX_PATH_BYTES]u8 = undefined; pub inline fn bufs(comptime field: std.meta.DeclEnum(@This())) *@TypeOf(@field(@This(), @tagName(field))) { return &@field(@This(), @tagName(field)); @@ -3107,6 +3108,93 @@ pub const Resolver = struct { }; } + pub export fn Resolver__nodeModulePathsForJS(globalThis: *bun.JSC.JSGlobalObject, callframe: *bun.JSC.CallFrame) callconv(.C) bun.JSC.JSValue { + bun.JSC.markBinding(@src()); + const argument: bun.JSC.JSValue = callframe.argument(0); + + if (argument.isEmpty() or !argument.isString()) { + globalThis.throwInvalidArgumentType("nodeModulePaths", "path", "string"); + return .zero; + } + + const in_str = argument.toBunString(globalThis); + var r = &globalThis.bunVM().bundler.resolver; + return nodeModulePathsJSValue(r, in_str, globalThis); + } + + pub export fn Resolver__propForRequireMainPaths(globalThis: *bun.JSC.JSGlobalObject) callconv(.C) bun.JSC.JSValue { + bun.JSC.markBinding(@src()); + + const in_str = bun.String.create("."); + var r = &globalThis.bunVM().bundler.resolver; + return nodeModulePathsJSValue(r, in_str, globalThis); + } + + pub fn nodeModulePathsJSValue( + r: *ThisResolver, + in_str: bun.String, + globalObject: *bun.JSC.JSGlobalObject, + ) bun.JSC.JSValue { + var list = std.ArrayList(bun.String).init(bun.default_allocator); + defer list.deinit(); + + const sliced = in_str.toUTF8(bun.default_allocator); + defer sliced.deinit(); + + const str = brk: { + if (std.fs.path.isAbsolute(sliced.slice())) break :brk sliced.slice(); + var dir_path_buf = bufs(.node_modules_paths_buf); + break :brk r.fs.joinBuf(&[_]string{ r.fs.top_level_dir, sliced.slice() }, dir_path_buf); + }; + var arena = std.heap.ArenaAllocator.init(bun.default_allocator); + defer arena.deinit(); + var stack_fallback_allocator = std.heap.stackFallback(1024, arena.allocator()); + + if (r.readDirInfo(strings.withoutTrailingSlash(str)) catch null) |result| { + var dir_info = result; + + while (true) { + const path_without_trailing_slash = strings.withoutTrailingSlash(dir_info.abs_path); + const path_parts = brk: { + if (path_without_trailing_slash.len == 1 and path_without_trailing_slash[0] == '/') { + break :brk [2]string{ "", "/node_modules" }; + } + + break :brk [2]string{ path_without_trailing_slash, "/node_modules" }; + }; + list.append( + bun.String.create( + bun.strings.concat(stack_fallback_allocator.get(), &path_parts) catch unreachable, + ), + ) catch unreachable; + dir_info = (r.readDirInfo(std.fs.path.dirname(path_without_trailing_slash) orelse break) catch null) orelse break; + } + } else { + // does not exist + const full_path = std.fs.path.resolve(r.allocator, &[1][]const u8{str}) catch unreachable; + var path = full_path; + while (true) { + const path_without_trailing_slash = strings.withoutTrailingSlash(path); + + list.append( + bun.String.create( + bun.strings.concat( + stack_fallback_allocator.get(), + &[_]string{ + path_without_trailing_slash, + "/node_modules", + }, + ) catch unreachable, + ), + ) catch unreachable; + + path = path[0 .. strings.lastIndexOfChar(path, '/') orelse break]; + } + } + + return bun.String.toJSArray(globalObject, list.items); + } + pub fn loadAsIndex(r: *ThisResolver, dir_info: *DirInfo, extension_order: []const string) ?MatchResult { var rfs = &r.fs.fs; // Try the "index" file with extensions @@ -3892,3 +3980,10 @@ pub const GlobalCache = enum { }; } }; + +comptime { + if (!bun.JSC.is_bindgen) { + _ = Resolver.Resolver__nodeModulePathsForJS; + _ = Resolver.Resolver__propForRequireMainPaths; + } +} diff --git a/test/js/node/module/node-module-module.test.js b/test/js/node/module/node-module-module.test.js index 549b5e085..3ced63da1 100644 --- a/test/js/node/module/node-module-module.test.js +++ b/test/js/node/module/node-module-module.test.js @@ -1,5 +1,24 @@ import { expect, test } from "bun:test"; +import { _nodeModulePaths } from "module"; test("module.globalPaths exists", () => { expect(Array.isArray(require("module").globalPaths)).toBe(true); }); + +test("_nodeModulePaths() works", () => { + expect(() => { + _nodeModulePaths(); + }).toThrow(); + expect(_nodeModulePaths(".").length).toBeGreaterThan(0); + expect(_nodeModulePaths(".").pop()).toBe("/node_modules"); + expect(_nodeModulePaths("")).toEqual(_nodeModulePaths(".")); + expect(_nodeModulePaths("/")).toEqual(["/node_modules"]); + expect(_nodeModulePaths("/a/b/c/d")).toEqual([ + "/a/b/c/d/node_modules", + "/a/b/c/node_modules", + "/a/b/node_modules", + "/a/node_modules", + "/node_modules", + ]); + expect(_nodeModulePaths("/a/b/../d")).toEqual(["/a/d/node_modules", "/a/node_modules", "/node_modules"]); +}); -- cgit v1.2.3 From a732999da578ca92a1d9e633036225a32e77529d Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 26 Jun 2023 12:49:20 -0700 Subject: Runtime support for `__esModule` annotations (#3393) * Runtime support for `__esModule` annotations * Ignore `__esModule` annotation when `"type": "module"` is set --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/bindings/CommonJSModuleRecord.cpp | 113 +++++++++++++++++++-- src/bun.js/bindings/CommonJSModuleRecord.h | 1 + src/bun.js/bindings/exports.zig | 7 +- src/bun.js/bindings/headers-handwritten.h | 1 + src/bun.js/module_loader.zig | 21 ++++ src/js/builtins/BunBuiltinNames.h | 7 +- test/js/bun/resolve/esModule-annotation.test.js | 68 +++++++++++++ .../export-esModule-annotation-empty.cjs | 1 + .../export-esModule-annotation-no-default.cjs | 1 + .../export-esModule-annotation.cjs | 2 + .../export-esModule-no-annotation.cjs | 1 + test/js/bun/resolve/with-type-module/package.json | 4 + .../export-esModule-annotation-empty.cjs | 1 + .../export-esModule-annotation-no-default.cjs | 1 + .../export-esModule-annotation.cjs | 2 + .../export-esModule-no-annotation.cjs | 1 + .../bun/resolve/without-type-module/package.json | 4 + 17 files changed, 221 insertions(+), 15 deletions(-) create mode 100644 test/js/bun/resolve/esModule-annotation.test.js create mode 100644 test/js/bun/resolve/with-type-module/export-esModule-annotation-empty.cjs create mode 100644 test/js/bun/resolve/with-type-module/export-esModule-annotation-no-default.cjs create mode 100644 test/js/bun/resolve/with-type-module/export-esModule-annotation.cjs create mode 100644 test/js/bun/resolve/with-type-module/export-esModule-no-annotation.cjs create mode 100644 test/js/bun/resolve/with-type-module/package.json create mode 100644 test/js/bun/resolve/without-type-module/export-esModule-annotation-empty.cjs create mode 100644 test/js/bun/resolve/without-type-module/export-esModule-annotation-no-default.cjs create mode 100644 test/js/bun/resolve/without-type-module/export-esModule-annotation.cjs create mode 100644 test/js/bun/resolve/without-type-module/export-esModule-no-annotation.cjs create mode 100644 test/js/bun/resolve/without-type-module/package.json (limited to 'src/bun.js/bindings/CommonJSModuleRecord.cpp') diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index 3615db774..c7dac89c2 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -586,13 +586,41 @@ void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject, auto result = this->exportsObject(); auto& vm = globalObject->vm(); - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(result); // This exists to tell ImportMetaObject.ts that this is a CommonJS module. exportNames.append(Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))); exportValues.append(jsNumber(0)); + // Bun's intepretation of the "__esModule" annotation: + // + // - If a "default" export does not exist OR the __esModule annotation is not present, then we + // set the default export to the exports object + // + // - If a "default" export also exists, then we set the default export + // to the value of it (matching Babel behavior) + // + // https://stackoverflow.com/questions/50943704/whats-the-purpose-of-object-definepropertyexports-esmodule-value-0 + // https://github.com/nodejs/node/issues/40891 + // https://github.com/evanw/bundler-esm-cjs-tests + // https://github.com/evanw/esbuild/issues/1591 + // https://github.com/oven-sh/bun/issues/3383 + // + // Note that this interpretation is slightly different + // + // - We do not ignore when "type": "module" or when the file + // extension is ".mjs". Build tools determine that based on the + // caller's behavior, but in a JS runtime, there is only one ModuleNamespaceObject. + // + // It would be possible to match the behavior at runtime, but + // it would need further engine changes which do not match the ES Module spec + // + // - We ignore the value of the annotation. We only look for the + // existence of the value being set. This is for performance reasons, but also + // this annotation is meant for tooling and the only usages of setting + // it to something that does NOT evaluate to "true" I could find were in + // unit tests of build tools. Happy to revisit this if users file an issue. + bool needsToAssignDefault = true; + if (result.isObject()) { auto* exports = asObject(result); @@ -601,21 +629,78 @@ void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject, exportNames.reserveCapacity(size + 2); exportValues.ensureCapacity(size + 2); - if (canPerformFastEnumeration(structure)) { + auto catchScope = DECLARE_CATCH_SCOPE(vm); + + Identifier esModuleMarker = builtinNames(vm).__esModulePublicName(); + bool hasESModuleMarker = !this->ignoreESModuleAnnotation && exports->hasProperty(globalObject, esModuleMarker); + if (catchScope.exception()) { + catchScope.clearException(); + } + + if (hasESModuleMarker) { + if (canPerformFastEnumeration(structure)) { + exports->structure()->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { + auto key = entry.key(); + if (key->isSymbol() || entry.attributes() & PropertyAttribute::DontEnum || key == esModuleMarker) + return true; + + needsToAssignDefault = needsToAssignDefault && key != vm.propertyNames->defaultKeyword; + + JSValue value = exports->getDirect(entry.offset()); + exportNames.append(Identifier::fromUid(vm, key)); + exportValues.append(value); + return true; + }); + } else { + JSC::PropertyNameArray properties(vm, JSC::PropertyNameMode::Strings, JSC::PrivateSymbolMode::Exclude); + exports->methodTable()->getOwnPropertyNames(exports, globalObject, properties, DontEnumPropertiesMode::Exclude); + if (catchScope.exception()) { + catchScope.clearExceptionExceptTermination(); + return; + } + + for (auto property : properties) { + if (UNLIKELY(property.isEmpty() || property.isNull() || property == esModuleMarker || property.isPrivateName() || property.isSymbol())) + continue; + + // ignore constructor + if (property == vm.propertyNames->constructor) + continue; + + JSC::PropertySlot slot(exports, PropertySlot::InternalMethodType::Get); + if (!exports->getPropertySlot(globalObject, property, slot)) + continue; + + exportNames.append(property); + + JSValue getterResult = slot.getValue(globalObject, property); + + // If it throws, we keep them in the exports list, but mark it as undefined + // This is consistent with what Node.js does. + if (catchScope.exception()) { + catchScope.clearException(); + getterResult = jsUndefined(); + } + + exportValues.append(getterResult); + + needsToAssignDefault = needsToAssignDefault && property != vm.propertyNames->defaultKeyword; + } + } + + } else if (canPerformFastEnumeration(structure)) { exports->structure()->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { auto key = entry.key(); - if (key->isSymbol() || key == vm.propertyNames->defaultKeyword || entry.attributes() & PropertyAttribute::DontEnum) + if (key->isSymbol() || entry.attributes() & PropertyAttribute::DontEnum || key == vm.propertyNames->defaultKeyword) return true; - exportNames.append(Identifier::fromUid(vm, key)); - JSValue value = exports->getDirect(entry.offset()); + exportNames.append(Identifier::fromUid(vm, key)); exportValues.append(value); return true; }); } else { - auto catchScope = DECLARE_CATCH_SCOPE(vm); JSC::PropertyNameArray properties(vm, JSC::PropertyNameMode::Strings, JSC::PrivateSymbolMode::Exclude); exports->methodTable()->getOwnPropertyNames(exports, globalObject, properties, DontEnumPropertiesMode::Exclude); if (catchScope.exception()) { @@ -624,11 +709,11 @@ void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject, } for (auto property : properties) { - if (UNLIKELY(property.isEmpty() || property.isNull() || property.isPrivateName() || property.isSymbol())) + if (UNLIKELY(property.isEmpty() || property.isNull() || property == vm.propertyNames->defaultKeyword || property.isPrivateName() || property.isSymbol())) continue; // ignore constructor - if (property == vm.propertyNames->constructor || property == vm.propertyNames->defaultKeyword) + if (property == vm.propertyNames->constructor) continue; JSC::PropertySlot slot(exports, PropertySlot::InternalMethodType::Get); @@ -650,6 +735,11 @@ void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject, } } } + + if (needsToAssignDefault) { + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(result); + } } JSValue JSCommonJSModule::exportsObject() @@ -759,6 +849,7 @@ bool JSCommonJSModule::evaluate( { auto& vm = globalObject->vm(); auto sourceProvider = Zig::SourceProvider::create(jsCast(globalObject), source, JSC::SourceProviderSourceType::Program); + this->ignoreESModuleAnnotation = source.tag == ResolvedSourceTagPackageJSONTypeModule; JSC::SourceCode rawInputSource( WTFMove(sourceProvider)); @@ -766,6 +857,7 @@ bool JSCommonJSModule::evaluate( return true; this->sourceCode.set(vm, this, JSC::JSSourceCode::create(vm, WTFMove(rawInputSource))); + WTF::NakedPtr exception; evaluateCommonJSModuleOnce(vm, globalObject, this, this->m_dirname.get(), this->m_filename.get(), exception); @@ -796,6 +888,7 @@ std::optional createCommonJSModule( JSValue entry = globalObject->requireMap()->get(globalObject, specifierValue); auto sourceProvider = Zig::SourceProvider::create(jsCast(globalObject), source, JSC::SourceProviderSourceType::Program); + bool ignoreESModuleAnnotation = source.tag == ResolvedSourceTagPackageJSONTypeModule; SourceOrigin sourceOrigin = sourceProvider->sourceOrigin(); if (entry) { @@ -827,6 +920,8 @@ std::optional createCommonJSModule( globalObject->requireMap()->set(globalObject, requireMapKey, moduleObject); } + moduleObject->ignoreESModuleAnnotation = ignoreESModuleAnnotation; + return JSC::SourceCode( JSC::SyntheticSourceProvider::create( [](JSC::JSGlobalObject* lexicalGlobalObject, diff --git a/src/bun.js/bindings/CommonJSModuleRecord.h b/src/bun.js/bindings/CommonJSModuleRecord.h index a96ab5f75..15792f9da 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.h +++ b/src/bun.js/bindings/CommonJSModuleRecord.h @@ -26,6 +26,7 @@ public: mutable JSC::WriteBarrier m_dirname; mutable JSC::WriteBarrier m_paths; mutable JSC::WriteBarrier sourceCode; + bool ignoreESModuleAnnotation { false }; static void destroy(JSC::JSCell*); ~JSCommonJSModule(); diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index f77b57216..213291e7b 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -216,9 +216,10 @@ pub const ResolvedSource = extern struct { pub const Tag = enum(u64) { javascript = 0, - wasm = 1, - object = 2, - file = 3, + package_json_type_module = 1, + wasm = 2, + object = 3, + file = 4, @"node:buffer" = 1024, @"node:process" = 1025, diff --git a/src/bun.js/bindings/headers-handwritten.h b/src/bun.js/bindings/headers-handwritten.h index 57940550f..db1e38d3e 100644 --- a/src/bun.js/bindings/headers-handwritten.h +++ b/src/bun.js/bindings/headers-handwritten.h @@ -72,6 +72,7 @@ typedef struct ResolvedSource { void* allocator; uint64_t tag; } ResolvedSource; +static const uint64_t ResolvedSourceTagPackageJSONTypeModule = 1; typedef union ErrorableResolvedSourceResult { ResolvedSource value; ZigErrorType err; diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index 5838d8a49..b25bb4b10 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -1225,6 +1225,25 @@ pub const ModuleLoader = struct { return resolved_source; } + // Pass along package.json type "module" if set. + const tag = brk: { + if (parse_result.ast.exports_kind == .cjs and parse_result.source.path.isFile()) { + var actual_package_json: *PackageJSON = package_json orelse brk2: { + // this should already be cached virtually always so it's fine to do this + var dir_info = (jsc_vm.bundler.resolver.readDirInfo(parse_result.source.path.name.dir) catch null) orelse + break :brk .javascript; + + break :brk2 dir_info.package_json orelse dir_info.enclosing_package_json; + } orelse break :brk .javascript; + + if (actual_package_json.module_type == .esm) { + break :brk ResolvedSource.Tag.package_json_type_module; + } + } + + break :brk ResolvedSource.Tag.javascript; + }; + return .{ .allocator = null, .source_code = bun.String.createLatin1(printer.ctx.getWritten()), @@ -1245,6 +1264,8 @@ pub const ModuleLoader = struct { // having JSC own the memory causes crashes .hash = 0, + + .tag = tag, }; }, // provideFetch() should be called diff --git a/src/js/builtins/BunBuiltinNames.h b/src/js/builtins/BunBuiltinNames.h index af9291918..1897f939e 100644 --- a/src/js/builtins/BunBuiltinNames.h +++ b/src/js/builtins/BunBuiltinNames.h @@ -37,6 +37,7 @@ using namespace JSC; macro(WritableStream) \ macro(WritableStreamDefaultController) \ macro(WritableStreamDefaultWriter) \ + macro(__esModule) \ macro(_events) \ macro(abortAlgorithm) \ macro(abortSteps) \ @@ -59,11 +60,11 @@ using namespace JSC; macro(cloneArrayBuffer) \ macro(close) \ macro(closeAlgorithm) \ + macro(closeRequest) \ + macro(closeRequested) \ macro(closed) \ macro(closedPromise) \ macro(closedPromiseCapability) \ - macro(closeRequest) \ - macro(closeRequested) \ macro(code) \ macro(commonJSSymbol) \ macro(connect) \ @@ -94,6 +95,7 @@ using namespace JSC; macro(end) \ macro(errno) \ macro(errorSteps) \ + macro(evaluateCommonJSModule) \ macro(execArgv) \ macro(exports) \ macro(extname) \ @@ -137,7 +139,6 @@ using namespace JSC; macro(lazyLoad) \ macro(lazyStreamPrototypeMap) \ macro(loadCJS2ESM) \ - macro(evaluateCommonJSModule) \ macro(localStreams) \ macro(main) \ macro(makeDOMException) \ diff --git a/test/js/bun/resolve/esModule-annotation.test.js b/test/js/bun/resolve/esModule-annotation.test.js new file mode 100644 index 000000000..33c84be5d --- /dev/null +++ b/test/js/bun/resolve/esModule-annotation.test.js @@ -0,0 +1,68 @@ +import { test, expect, describe } from "bun:test"; +import * as WithTypeModuleExportEsModuleAnnotationMissingDefault from "./with-type-module/export-esModule-annotation-empty.cjs"; +import * as WithTypeModuleExportEsModuleAnnotationNoDefault from "./with-type-module/export-esModule-annotation-no-default.cjs"; +import * as WithTypeModuleExportEsModuleAnnotation from "./with-type-module/export-esModule-annotation.cjs"; +import * as WithTypeModuleExportEsModuleNoAnnotation from "./with-type-module/export-esModule-no-annotation.cjs"; +import * as WithoutTypeModuleExportEsModuleAnnotationMissingDefault from "./without-type-module/export-esModule-annotation-empty.cjs"; +import * as WithoutTypeModuleExportEsModuleAnnotationNoDefault from "./without-type-module/export-esModule-annotation-no-default.cjs"; +import * as WithoutTypeModuleExportEsModuleAnnotation from "./without-type-module/export-esModule-annotation.cjs"; +import * as WithoutTypeModuleExportEsModuleNoAnnotation from "./without-type-module/export-esModule-no-annotation.cjs"; + +describe('without type: "module"', () => { + test("module.exports = {}", () => { + expect(WithoutTypeModuleExportEsModuleAnnotationMissingDefault.default).toEqual({}); + expect(WithoutTypeModuleExportEsModuleAnnotationMissingDefault.__esModule).toBeUndefined(); + }); + + test("exports.__esModule = true", () => { + expect(WithoutTypeModuleExportEsModuleAnnotationNoDefault.default).toEqual({ + __esModule: true, + }); + + // The module namespace object will not have the __esModule property. + expect(WithoutTypeModuleExportEsModuleAnnotationNoDefault).not.toHaveProperty("__esModule"); + }); + + test("exports.default = true; exports.__esModule = true;", () => { + expect(WithoutTypeModuleExportEsModuleAnnotation.default).toBeTrue(); + expect(WithoutTypeModuleExportEsModuleAnnotation.__esModule).toBeUndefined(); + }); + + test("exports.default = true;", () => { + expect(WithoutTypeModuleExportEsModuleNoAnnotation.default).toEqual({ + default: true, + }); + expect(WithoutTypeModuleExportEsModuleAnnotation.__esModule).toBeUndefined(); + }); +}); + +describe('with type: "module"', () => { + test("module.exports = {}", () => { + expect(WithTypeModuleExportEsModuleAnnotationMissingDefault.default).toEqual({}); + expect(WithTypeModuleExportEsModuleAnnotationMissingDefault.__esModule).toBeUndefined(); + }); + + test("exports.__esModule = true", () => { + expect(WithTypeModuleExportEsModuleAnnotationNoDefault.default).toEqual({ + __esModule: true, + }); + + // The module namespace object WILL have the __esModule property. + expect(WithTypeModuleExportEsModuleAnnotationNoDefault).toHaveProperty("__esModule"); + }); + + test("exports.default = true; exports.__esModule = true;", () => { + expect(WithTypeModuleExportEsModuleAnnotation.default).toEqual({ + default: true, + __esModule: true, + }); + expect(WithTypeModuleExportEsModuleAnnotation.__esModule).toBeTrue(); + }); + + test("exports.default = true;", () => { + expect(WithTypeModuleExportEsModuleNoAnnotation.default).toEqual({ + default: true, + }); + expect(WithTypeModuleExportEsModuleAnnotation.__esModule).toBeTrue(); + }); +}); diff --git a/test/js/bun/resolve/with-type-module/export-esModule-annotation-empty.cjs b/test/js/bun/resolve/with-type-module/export-esModule-annotation-empty.cjs new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/test/js/bun/resolve/with-type-module/export-esModule-annotation-empty.cjs @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/js/bun/resolve/with-type-module/export-esModule-annotation-no-default.cjs b/test/js/bun/resolve/with-type-module/export-esModule-annotation-no-default.cjs new file mode 100644 index 000000000..32b83d4a5 --- /dev/null +++ b/test/js/bun/resolve/with-type-module/export-esModule-annotation-no-default.cjs @@ -0,0 +1 @@ +exports.__esModule = true; diff --git a/test/js/bun/resolve/with-type-module/export-esModule-annotation.cjs b/test/js/bun/resolve/with-type-module/export-esModule-annotation.cjs new file mode 100644 index 000000000..bc0625a0c --- /dev/null +++ b/test/js/bun/resolve/with-type-module/export-esModule-annotation.cjs @@ -0,0 +1,2 @@ +exports.default = true; +exports.__esModule = true; diff --git a/test/js/bun/resolve/with-type-module/export-esModule-no-annotation.cjs b/test/js/bun/resolve/with-type-module/export-esModule-no-annotation.cjs new file mode 100644 index 000000000..a4b65815f --- /dev/null +++ b/test/js/bun/resolve/with-type-module/export-esModule-no-annotation.cjs @@ -0,0 +1 @@ +exports.default = true; diff --git a/test/js/bun/resolve/with-type-module/package.json b/test/js/bun/resolve/with-type-module/package.json new file mode 100644 index 000000000..f1863a426 --- /dev/null +++ b/test/js/bun/resolve/with-type-module/package.json @@ -0,0 +1,4 @@ +{ + "name": "with-type-module", + "type": "module" +} diff --git a/test/js/bun/resolve/without-type-module/export-esModule-annotation-empty.cjs b/test/js/bun/resolve/without-type-module/export-esModule-annotation-empty.cjs new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/test/js/bun/resolve/without-type-module/export-esModule-annotation-empty.cjs @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/js/bun/resolve/without-type-module/export-esModule-annotation-no-default.cjs b/test/js/bun/resolve/without-type-module/export-esModule-annotation-no-default.cjs new file mode 100644 index 000000000..32b83d4a5 --- /dev/null +++ b/test/js/bun/resolve/without-type-module/export-esModule-annotation-no-default.cjs @@ -0,0 +1 @@ +exports.__esModule = true; diff --git a/test/js/bun/resolve/without-type-module/export-esModule-annotation.cjs b/test/js/bun/resolve/without-type-module/export-esModule-annotation.cjs new file mode 100644 index 000000000..bc0625a0c --- /dev/null +++ b/test/js/bun/resolve/without-type-module/export-esModule-annotation.cjs @@ -0,0 +1,2 @@ +exports.default = true; +exports.__esModule = true; diff --git a/test/js/bun/resolve/without-type-module/export-esModule-no-annotation.cjs b/test/js/bun/resolve/without-type-module/export-esModule-no-annotation.cjs new file mode 100644 index 000000000..a4b65815f --- /dev/null +++ b/test/js/bun/resolve/without-type-module/export-esModule-no-annotation.cjs @@ -0,0 +1 @@ +exports.default = true; diff --git a/test/js/bun/resolve/without-type-module/package.json b/test/js/bun/resolve/without-type-module/package.json new file mode 100644 index 000000000..5b290db1c --- /dev/null +++ b/test/js/bun/resolve/without-type-module/package.json @@ -0,0 +1,4 @@ +{ + "name": "without-type-module", + "type": "commonjs" +} -- cgit v1.2.3 From ca42c820d29400d8bd93f493064db029f6d4420d Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 9 Jul 2023 16:33:49 -0700 Subject: Implement nearly all of `process` object methods and properties (#3581) * Add special case * Make process object load faster * Fix openStdin --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/bindings/CommonJSModuleRecord.cpp | 6 + src/bun.js/bindings/Process.cpp | 960 +++++++++++++++------------ src/bun.js/bindings/Process.h | 2 +- src/bun.js/bindings/Process.lut.h | 208 ++++++ src/bun.js/bindings/ZigGlobalObject.cpp | 6 +- src/bun.js/modules/ProcessModule.h | 8 + src/js/builtins/ProcessObjectInternals.ts | 22 +- src/js/builtins/codegen/index.ts | 30 + src/js/out/WebCoreJSBuiltins.cpp | 16 +- src/js/out/WebCoreJSBuiltins.h | 4 +- 10 files changed, 812 insertions(+), 450 deletions(-) create mode 100644 src/bun.js/bindings/Process.lut.h (limited to 'src/bun.js/bindings/CommonJSModuleRecord.cpp') diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index c7dac89c2..8adba197c 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -817,6 +817,12 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireCommonJS, (JSGlobalObject * lexicalGlo WTF::String specifier = specifierValue.toWTFString(globalObject); RETURN_IF_EXCEPTION(throwScope, {}); + // Special-case for "process" to just return the process object directly. + if (UNLIKELY(specifier == "process"_s || specifier == "node:process"_s)) { + jsDynamicCast(callframe->argument(1))->putDirect(vm, builtinNames(vm).exportsPublicName(), globalObject->processObject(), 0); + return JSValue::encode(globalObject->processObject()); + } + WTF::String referrer = thisObject->id().toWTFString(globalObject); RETURN_IF_EXCEPTION(throwScope, {}); diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index 1d6b5d33a..8d94594cc 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -11,13 +11,24 @@ #include #include "ZigConsoleClient.h" #include +#include #pragma mark - Node.js Process +#if !defined(_MSC_VER) +#include // setuid, getuid +#endif + namespace Zig { using namespace JSC; #define REPORTED_NODE_VERSION "18.15.0" +#define processObjectBindingCodeGenerator processObjectInternalsBindingCodeGenerator +#define processObjectMainModuleCodeGenerator moduleMainCodeGenerator + +#if !defined(BUN_WEBKIT_VERSION) +#define BUN_WEBKIT_VERSION "unknown" +#endif using JSGlobalObject = JSC::JSGlobalObject; using Exception = JSC::Exception; @@ -31,23 +42,20 @@ using JSObject = JSC::JSObject; using JSNonFinalObject = JSC::JSNonFinalObject; namespace JSCastingHelpers = JSC::JSCastingHelpers; -static JSC_DECLARE_CUSTOM_SETTER(Process_setTitle); -static JSC_DECLARE_CUSTOM_GETTER(Process_getArgv); -static JSC_DECLARE_CUSTOM_SETTER(Process_setArgv); -static JSC_DECLARE_CUSTOM_GETTER(Process_getTitle); -static JSC_DECLARE_CUSTOM_GETTER(Process_getVersionsLazy); -static JSC_DECLARE_CUSTOM_SETTER(Process_setVersionsLazy); - -static JSC_DECLARE_CUSTOM_GETTER(Process_getPID); -static JSC_DECLARE_CUSTOM_GETTER(Process_getPPID); - -static JSC_DECLARE_HOST_FUNCTION(Process_functionCwd); +JSC_DECLARE_CUSTOM_SETTER(Process_setTitle); +JSC_DECLARE_CUSTOM_GETTER(Process_getArgv); +JSC_DECLARE_CUSTOM_SETTER(Process_setArgv); +JSC_DECLARE_CUSTOM_GETTER(Process_getTitle); +JSC_DECLARE_CUSTOM_GETTER(Process_getPID); +JSC_DECLARE_CUSTOM_GETTER(Process_getPPID); +JSC_DECLARE_HOST_FUNCTION(Process_functionCwd); static bool processIsExiting = false; extern "C" uint8_t Bun__getExitCode(void*); extern "C" uint8_t Bun__setExitCode(void*, uint8_t); extern "C" void* Bun__getVM(); extern "C" Zig::GlobalObject* Bun__getDefaultGlobal(); +extern "C" const char* Bun__githubURL; static void dispatchExitInternal(JSC::JSGlobalObject* globalObject, Process* process, int exitCode) { @@ -72,107 +80,6 @@ static void dispatchExitInternal(JSC::JSGlobalObject* globalObject, Process* pro emitter.emit(event, arguments); } -static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, int fd) -{ - auto& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - auto* thisObject = reinterpret_cast(globalObject); - JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdioWriteStreamCodeGenerator(vm), globalObject); - JSC::MarkedArgumentBuffer args; - WTF::String process = WTF::String("node:process"_s); - JSC::JSValue requireFunction = Zig::ImportMetaObject::createRequireFunction( - vm, - globalObject, - process); - - args.append(JSC::jsNumber(fd)); - args.append(requireFunction); - - auto clientData = WebCore::clientData(vm); - JSC::CallData callData = JSC::getCallData(getStdioWriteStream); - - NakedPtr returnedException = nullptr; - auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject->globalThis(), args, returnedException); - RETURN_IF_EXCEPTION(scope, {}); - - if (returnedException) { - throwException(globalObject, scope, returnedException.get()); - return {}; - } - - return result; -} - -JSC_DEFINE_CUSTOM_GETTER( - Process_lazyStdinGetter, - (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property)) -{ - auto& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - JSC::JSValue value = JSC::JSValue::decode(thisValue); - if (!value || value.isUndefinedOrNull() || !value.isObject()) - return JSValue::encode(jsUndefined()); - - auto* thisObject = reinterpret_cast(globalObject); - JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdinStreamCodeGenerator(vm), globalObject); - JSC::MarkedArgumentBuffer args; - WTF::String process = WTF::String("node:process"_s); - JSC::JSValue requireFunction = Zig::ImportMetaObject::createRequireFunction( - vm, - globalObject, - process); - - args.append(JSC::jsNumber(STDIN_FILENO)); - args.append(requireFunction); - args.append(thisObject->get(globalObject, PropertyName(JSC::Identifier::fromString(vm, "Bun"_s)))); - - auto clientData = WebCore::clientData(vm); - JSC::CallData callData = JSC::getCallData(getStdioWriteStream); - - NakedPtr returnedException = nullptr; - auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject->globalThis(), args, returnedException); - RETURN_IF_EXCEPTION(scope, {}); - - if (UNLIKELY(returnedException)) { - throwException(globalObject, scope, returnedException.get()); - return {}; - } - - if (LIKELY(result)) - value.getObject()->putDirect(vm, property, result, 0); - - return JSValue::encode(result); -} - -JSC_DEFINE_CUSTOM_GETTER( - Process_lazyStdoutGetter, - (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property)) -{ - JSValue value = JSValue::decode(thisValue); - auto& vm = globalObject->vm(); - JSC::JSObject* thisObject = value.toObject(globalObject); - JSC::JSValue stream = constructStdioWriteStream(globalObject, 1); - - if (stream) - thisObject->putDirect(vm, property, stream, 0); - - return JSValue::encode(stream); -} - -JSC_DEFINE_CUSTOM_GETTER( - Process_lazyStderrGetter, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property)) -{ - JSValue value = JSValue::decode(thisValue); - auto& vm = globalObject->vm(); - JSC::JSObject* thisObject = value.toObject(globalObject); - JSC::JSValue stream = constructStdioWriteStream(globalObject, 2); - - if (stream) - thisObject->putDirect(vm, property, stream, 0); - - return JSValue::encode(stream); -} - JSC_DEFINE_CUSTOM_SETTER(Process_defaultSetter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName propertyName)) @@ -420,6 +327,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionExit, Process__dispatchOnExit(zigGlobal, exitCode); Bun__Process__exit(zigGlobal, exitCode); + __builtin_unreachable(); } extern "C" uint64_t Bun__readOriginTimer(void*); @@ -497,34 +405,6 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionChdir, return JSC::JSValue::encode(result); } -extern "C" const char* Bun__githubURL; - -JSC_DEFINE_CUSTOM_GETTER(Process_getterRelease, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName)) -{ - auto& vm = globalObject->vm(); - - auto* release = JSC::constructEmptyObject(globalObject); - release->putDirect(vm, Identifier::fromString(vm, "name"_s), jsString(vm, WTF::String("bun"_s)), 0); - release->putDirect(vm, Identifier::fromString(vm, "lts"_s), jsBoolean(false), 0); - release->putDirect(vm, Identifier::fromString(vm, "sourceUrl"_s), jsString(vm, WTF::String(Bun__githubURL, strlen(Bun__githubURL))), 0); - release->putDirect(vm, Identifier::fromString(vm, "headersUrl"_s), jsEmptyString(vm), 0); - release->putDirect(vm, Identifier::fromString(vm, "libUrl"_s), jsEmptyString(vm), 0); - - return JSValue::encode(release); -} - -JSC_DEFINE_CUSTOM_SETTER(Process_setterRelease, - (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, - JSC::EncodedJSValue value, JSC::PropertyName)) -{ - JSC::VM& vm = globalObject->vm(); - - JSC::JSObject* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); - thisObject->putDirect(vm, JSC::Identifier::fromString(vm, "release"_s), JSValue::decode(value), 0); - - return true; -} - // static const NeverDestroyed signalNames[] = { // MAKE_STATIC_STRING_IMPL("SIGHUP"), // MAKE_STATIC_STRING_IMPL("SIGINT"), @@ -660,31 +540,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_emitWarning, (JSGlobalObject * lexicalGlobalObj return JSValue::encode(jsUndefined()); } -JSC_DEFINE_CUSTOM_GETTER(Process_lazyArgv0Getter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name)) -{ - JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject(); - EncodedJSValue ret = Bun__Process__getArgv0(globalObject); - - if (LIKELY(thisObject)) { - thisObject->putDirect(globalObject->vm(), name, JSValue::decode(ret), 0); - } - - return ret; -} - -JSC_DEFINE_CUSTOM_GETTER(Process_lazyExecArgvGetter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name)) -{ - JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject(); - EncodedJSValue ret = Bun__Process__getExecArgv(globalObject); - - if (LIKELY(thisObject)) { - thisObject->putDirect(globalObject->vm(), name, JSValue::decode(ret), 0); - } - - return ret; -} - -JSC_DEFINE_CUSTOM_GETTER(Process__getExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name)) +JSC_DEFINE_CUSTOM_GETTER(processExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name)) { Process* process = jsDynamicCast(JSValue::decode(thisValue)); if (!process) { @@ -693,7 +549,7 @@ JSC_DEFINE_CUSTOM_GETTER(Process__getExitCode, (JSC::JSGlobalObject * lexicalGlo return JSValue::encode(jsNumber(Bun__getExitCode(jsCast(process->globalObject())->bunVM()))); } -JSC_DEFINE_CUSTOM_SETTER(Process__setExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName)) +JSC_DEFINE_CUSTOM_SETTER(setProcessExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName)) { Process* process = jsDynamicCast(JSValue::decode(thisValue)); if (!process) { @@ -724,158 +580,56 @@ JSC_DEFINE_CUSTOM_SETTER(Process__setExitCode, (JSC::JSGlobalObject * lexicalGlo return true; } -JSC_DEFINE_CUSTOM_GETTER(Process_lazyExecPathGetter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name)) +static JSValue constructVersions(VM& vm, JSObject* processObject) { - JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject(); - EncodedJSValue ret = Bun__Process__getExecPath(globalObject); - - if (LIKELY(thisObject)) { - thisObject->putDirect(globalObject->vm(), name, JSValue::decode(ret), 0); - } - - return ret; -} - -void Process::finishCreation(JSC::VM& vm) -{ - Base::finishCreation(vm); - auto clientData = WebCore::clientData(vm); - auto* globalObject = reinterpret_cast(this->globalObject()); - - putDirectCustomAccessor(vm, clientData->builtinNames().pidPublicName(), - JSC::CustomGetterSetter::create(vm, Process_getPID, nullptr), - static_cast(JSC::PropertyAttribute::CustomValue)); - - putDirectCustomAccessor(vm, clientData->builtinNames().ppidPublicName(), - JSC::CustomGetterSetter::create(vm, Process_getPPID, nullptr), - static_cast(JSC::PropertyAttribute::CustomValue)); - - putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "title"_s), - JSC::CustomGetterSetter::create(vm, Process_getTitle, Process_setTitle), - static_cast(JSC::PropertyAttribute::CustomValue)); - - putDirectCustomAccessor(vm, clientData->builtinNames().argvPublicName(), - JSC::CustomGetterSetter::create(vm, Process_getArgv, Process_setArgv), - static_cast(JSC::PropertyAttribute::CustomValue)); - - putDirect(vm, JSC::Identifier::fromString(vm, "revision"_s), - JSC::jsString(vm, makeAtomString(Bun__version_sha)), 0); - - this->putDirect(vm, clientData->builtinNames().nextTickPublicName(), - JSC::JSFunction::create(vm, globalObject, 1, - MAKE_STATIC_STRING_IMPL("nextTick"), Process_functionNextTick, ImplementationVisibility::Public), - PropertyAttribute::Function | 0); - - this->putDirect(vm, JSC::Identifier::fromString(vm, "dlopen"_s), - JSC::JSFunction::create(vm, globalObject, 1, - MAKE_STATIC_STRING_IMPL("dlopen"), Process_functionDlopen, ImplementationVisibility::Public), - PropertyAttribute::Function | 0); - - this->putDirect(vm, clientData->builtinNames().cwdPublicName(), - JSC::JSFunction::create(vm, globalObject, 0, - MAKE_STATIC_STRING_IMPL("cwd"), Process_functionCwd, ImplementationVisibility::Public), - PropertyAttribute::Function | 0); - - this->putDirect(vm, clientData->builtinNames().chdirPublicName(), - JSC::JSFunction::create(vm, globalObject, 0, - MAKE_STATIC_STRING_IMPL("chdir"), Process_functionChdir, ImplementationVisibility::Public), - PropertyAttribute::Function | 0); - - this->putDirect(vm, JSC::Identifier::fromString(vm, "exit"_s), - JSC::JSFunction::create(vm, globalObject, 0, - MAKE_STATIC_STRING_IMPL("exit"), Process_functionExit, ImplementationVisibility::Public), - PropertyAttribute::Function | 0); - - putDirectCustomAccessor( - vm, clientData->builtinNames().versionsPublicName(), - JSC::CustomGetterSetter::create(vm, Process_getVersionsLazy, Process_setVersionsLazy), 0); - // this should be transpiled out, but just incase - this->putDirect(vm, JSC::Identifier::fromString(vm, "browser"_s), - JSC::JSValue(false), PropertyAttribute::DontEnum | 0); - - this->putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "exitCode"_s), - JSC::CustomGetterSetter::create(vm, - Process__getExitCode, - Process__setExitCode), - 0); - - this->putDirect(vm, clientData->builtinNames().versionPublicName(), - JSC::jsString(vm, makeString("v", REPORTED_NODE_VERSION))); - - // this gives some way of identifying at runtime whether the SSR is happening in node or not. - // this should probably be renamed to what the name of the bundler is, instead of "notNodeJS" - // but it must be something that won't evaluate to truthy in Node.js - this->putDirect(vm, JSC::Identifier::fromString(vm, "isBun"_s), JSC::JSValue(true)); -#if defined(__APPLE__) - this->putDirect(vm, JSC::Identifier::fromString(vm, "platform"_s), - JSC::jsString(vm, makeAtomString("darwin"))); -#else - this->putDirect(vm, JSC::Identifier::fromString(vm, "platform"_s), - JSC::jsString(vm, makeAtomString("linux"))); -#endif - -#if defined(__x86_64__) - this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s), - JSC::jsString(vm, makeAtomString("x64"))); -#elif defined(__i386__) - this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s), - JSC::jsString(vm, makeAtomString("x86"))); -#elif defined(__arm__) - this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s), - JSC::jsString(vm, makeAtomString("arm"))); -#elif defined(__aarch64__) - this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s), - JSC::jsString(vm, makeAtomString("arm64"))); -#endif - - JSC::JSFunction* hrtime = JSC::JSFunction::create(vm, globalObject, 0, - MAKE_STATIC_STRING_IMPL("hrtime"), Process_functionHRTime, ImplementationVisibility::Public); - - JSC::JSFunction* hrtimeBigInt = JSC::JSFunction::create(vm, globalObject, 0, - MAKE_STATIC_STRING_IMPL("bigint"), Process_functionHRTimeBigInt, ImplementationVisibility::Public); - - hrtime->putDirect(vm, JSC::Identifier::fromString(vm, "bigint"_s), hrtimeBigInt); - this->putDirect(vm, JSC::Identifier::fromString(vm, "hrtime"_s), hrtime); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "release"_s)), - JSC::CustomGetterSetter::create(vm, Process_getterRelease, Process_setterRelease), 0); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "stdout"_s)), - JSC::CustomGetterSetter::create(vm, Process_lazyStdoutGetter, Process_defaultSetter), 0); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "stderr"_s)), - JSC::CustomGetterSetter::create(vm, Process_lazyStderrGetter, Process_defaultSetter), 0); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "stdin"_s)), - JSC::CustomGetterSetter::create(vm, Process_lazyStdinGetter, Process_defaultSetter), 0); - - this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "abort"_s), - 0, Process_functionAbort, ImplementationVisibility::Public, NoIntrinsic, 0); - - this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "abort"_s), - 0, Process_functionAbort, ImplementationVisibility::Public, NoIntrinsic, 0); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "argv0"_s)), - JSC::CustomGetterSetter::create(vm, Process_lazyArgv0Getter, Process_defaultSetter), 0); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "execPath"_s)), - JSC::CustomGetterSetter::create(vm, Process_lazyExecPathGetter, Process_defaultSetter), 0); - - this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "execArgv"_s)), - JSC::CustomGetterSetter::create(vm, Process_lazyExecArgvGetter, Process_defaultSetter), 0); + auto* globalObject = processObject->globalObject(); + JSC::JSObject* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 19); - this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "uptime"_s), - 0, Process_functionUptime, ImplementationVisibility::Public, NoIntrinsic, 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "node"_s), + JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(REPORTED_NODE_VERSION)))); + object->putDirect( + vm, JSC::Identifier::fromString(vm, "bun"_s), + JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(Bun__version + 1 /* prefix with v */)))); + object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s), + JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(BUN_WEBKIT_VERSION)))); + object->putDirect(vm, JSC::Identifier::fromString(vm, "boringssl"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_boringssl))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "libarchive"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_libarchive))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "mimalloc"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_mimalloc))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "picohttpparser"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_picohttpparser))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "uwebsockets"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_uws))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_webkit))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "zig"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zig))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "zlib"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zlib))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "tinycc"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_tinycc))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "lolhtml"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_lolhtml))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "ares"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_c_ares))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "usockets"_s), + JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_usockets))), 0); - this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "umask"_s), - 1, Process_functionUmask, ImplementationVisibility::Public, NoIntrinsic, 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "v8"_s), JSValue(JSC::jsString(vm, makeString("10.8.168.20-node.8"_s))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "uv"_s), JSValue(JSC::jsString(vm, makeString("1.44.2"_s))), 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "napi"_s), JSValue(JSC::jsString(vm, makeString("8"_s))), 0); - this->putDirectBuiltinFunction(vm, globalObject, JSC::Identifier::fromString(vm, "binding"_s), - processObjectInternalsBindingCodeGenerator(vm), - 0); + object->putDirect(vm, JSC::Identifier::fromString(vm, "modules"_s), + JSC::JSValue(JSC::jsString(vm, makeAtomString("108")))); - this->putDirect(vm, vm.propertyNames->toStringTagSymbol, jsString(vm, String("process"_s)), 0); + return object; +} +static JSValue constructProcessConfigObject(VM& vm, JSObject* processObject) +{ + auto* globalObject = processObject->globalObject(); // target_defaults: // { cflags: [], // default_configuration: 'Release', @@ -905,179 +659,455 @@ void Process::finishCreation(JSC::VM& vm) JSC::jsNumber(1), 0); config->putDirect(vm, JSC::Identifier::fromString(vm, "target_defaults"_s), JSC::constructEmptyObject(globalObject), 0); config->putDirect(vm, JSC::Identifier::fromString(vm, "variables"_s), variables, 0); - this->putDirect(vm, JSC::Identifier::fromString(vm, "config"_s), config, 0); - - this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "emitWarning"_s), - 1, Process_emitWarning, ImplementationVisibility::Public, NoIntrinsic, 0); - - JSC::JSFunction* requireDotMainFunction = JSFunction::create( - vm, - moduleMainCodeGenerator(vm), - globalObject->globalScope()); - // https://nodejs.org/api/process.html#processmainmodule - this->putDirect( - vm, - JSC::Identifier::fromString(vm, "mainModule"_s), - JSC::GetterSetter::create(vm, globalObject, requireDotMainFunction, JSValue()), - PropertyAttribute::Builtin | PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | 0); + + return config; } -const JSC::ClassInfo Process::s_info = { "Process"_s, &Base::s_info, nullptr, nullptr, - CREATE_METHOD_TABLE(Process) }; +static JSValue constructProcessReleaseObject(VM& vm, JSObject* processObject) +{ + auto* globalObject = processObject->globalObject(); + auto* release = JSC::constructEmptyObject(globalObject); + release->putDirect(vm, Identifier::fromString(vm, "name"_s), jsString(vm, WTF::String("bun"_s)), 0); + release->putDirect(vm, Identifier::fromString(vm, "lts"_s), jsBoolean(false), 0); + release->putDirect(vm, Identifier::fromString(vm, "sourceUrl"_s), jsString(vm, WTF::String(Bun__githubURL, strlen(Bun__githubURL))), 0); + release->putDirect(vm, Identifier::fromString(vm, "headersUrl"_s), jsEmptyString(vm), 0); + release->putDirect(vm, Identifier::fromString(vm, "libUrl"_s), jsEmptyString(vm), 0); + + return release; +} -JSC_DEFINE_CUSTOM_GETTER(Process_getTitle, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +static JSValue constructProcessHrtimeObject(VM& vm, JSObject* processObject) { - ZigString str; - Bun__Process__getTitle(globalObject, &str); - return JSValue::encode(Zig::toJSStringValue(str, globalObject)); + auto* globalObject = processObject->globalObject(); + JSC::JSFunction* hrtime = JSC::JSFunction::create(vm, globalObject, 0, + MAKE_STATIC_STRING_IMPL("hrtime"), Process_functionHRTime, ImplementationVisibility::Public); + + JSC::JSFunction* hrtimeBigInt = JSC::JSFunction::create(vm, globalObject, 0, + MAKE_STATIC_STRING_IMPL("bigint"), Process_functionHRTimeBigInt, ImplementationVisibility::Public); + + hrtime->putDirect(vm, JSC::Identifier::fromString(vm, "bigint"_s), hrtimeBigInt); + + return hrtime; } -JSC_DEFINE_CUSTOM_SETTER(Process_setTitle, - (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, - JSC::EncodedJSValue value, JSC::PropertyName)) +static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, int fd) { - JSC::VM& vm = globalObject->vm(); + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdioWriteStreamCodeGenerator(vm), globalObject); + JSC::MarkedArgumentBuffer args; + args.append(JSC::jsNumber(fd)); - JSC::JSObject* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); - JSC::JSString* jsString = JSC::jsDynamicCast(JSValue::decode(value)); - if (!thisObject || !jsString) { - return false; + auto clientData = WebCore::clientData(vm); + JSC::CallData callData = JSC::getCallData(getStdioWriteStream); + + NakedPtr returnedException = nullptr; + auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject->globalThis(), args, returnedException); + RETURN_IF_EXCEPTION(scope, {}); + + if (returnedException) { + throwException(globalObject, scope, returnedException.get()); + return {}; } - ZigString str = Zig::toZigString(jsString, globalObject); - Bun__Process__setTitle(globalObject, &str); + return result; +} - return true; +static JSValue constructStdout(VM& vm, JSObject* processObject) +{ + auto* globalObject = Bun__getDefaultGlobal(); + return constructStdioWriteStream(globalObject, 1); } -JSC_DEFINE_CUSTOM_GETTER(Process_getArgv, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +static JSValue constructStderr(VM& vm, JSObject* processObject) { - JSC::VM& vm = globalObject->vm(); + auto* globalObject = Bun__getDefaultGlobal(); + return constructStdioWriteStream(globalObject, 2); +} - Zig::Process* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); - if (!thisObject) { - return JSValue::encode(JSC::jsUndefined()); - } +static JSValue constructStdin(VM& vm, JSObject* processObject) +{ + auto* globalObject = Bun__getDefaultGlobal(); + auto scope = DECLARE_THROW_SCOPE(vm); + auto* thisObject = reinterpret_cast(globalObject); + JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdinStreamCodeGenerator(vm), globalObject); + JSC::MarkedArgumentBuffer args; + args.append(JSC::jsNumber(STDIN_FILENO)); - JSC::EncodedJSValue argv_ = Bun__Process__getArgv(globalObject); auto clientData = WebCore::clientData(vm); + JSC::CallData callData = JSC::getCallData(getStdioWriteStream); + + NakedPtr returnedException = nullptr; + auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject, args, returnedException); + RETURN_IF_EXCEPTION(scope, {}); - thisObject->putDirect(vm, clientData->builtinNames().argvPublicName(), - JSC::JSValue::decode(argv_), 0); + if (UNLIKELY(returnedException)) { + throwException(globalObject, scope, returnedException.get()); + return {}; + } - return argv_; + RELEASE_AND_RETURN(scope, result); } -JSC_DEFINE_CUSTOM_SETTER(Process_setArgv, - (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, - JSC::EncodedJSValue value, JSC::PropertyName)) +static JSValue constructPid(VM& vm, JSObject* processObject) { - JSC::VM& vm = globalObject->vm(); + return jsNumber(getpid()); +} - JSC::JSObject* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); - if (!thisObject) { - return false; - } +static JSValue constructPpid(VM& vm, JSObject* processObject) +{ + return jsNumber(getppid()); +} - auto clientData = WebCore::clientData(vm); +static JSValue constructArgv0(VM& vm, JSObject* processObject) +{ + auto* globalObject = processObject->globalObject(); + return JSValue::decode(Bun__Process__getArgv0(globalObject)); +} - return thisObject->putDirect(vm, clientData->builtinNames().argvPublicName(), - JSC::JSValue::decode(value)); +static JSValue constructExecArgv(VM& vm, JSObject* processObject) +{ + auto* globalObject = processObject->globalObject(); + return JSValue::decode(Bun__Process__getExecArgv(globalObject)); } -JSC_DEFINE_CUSTOM_GETTER(Process_getPID, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +static JSValue constructExecPath(VM& vm, JSObject* processObject) { - return JSC::JSValue::encode(JSC::JSValue(getpid())); + auto* globalObject = processObject->globalObject(); + return JSValue::decode(Bun__Process__getExecPath(globalObject)); } -JSC_DEFINE_CUSTOM_GETTER(Process_getPPID, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +static JSValue constructArgv(VM& vm, JSObject* processObject) { - return JSC::JSValue::encode(JSC::JSValue(getppid())); + auto* globalObject = processObject->globalObject(); + return JSValue::decode(Bun__Process__getArgv(globalObject)); } -#if !defined(BUN_WEBKIT_VERSION) -#define BUN_WEBKIT_VERSION "unknown" +static JSValue constructArch(VM& vm, JSObject* processObject) +{ +#if defined(__x86_64__) + return JSC::jsString(vm, makeAtomString("x64")); +#elif defined(__i386__) + return JSC::jsString(vm, makeAtomString("x86")); +#elif defined(__arm__) + return JSC::jsString(vm, makeAtomString("arm")); +#elif defined(__aarch64__) + return JSC::jsString(vm, makeAtomString("arm64")); +#else +#error "Unknown architecture" #endif +} -JSC_DEFINE_CUSTOM_GETTER(Process_getVersionsLazy, - (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, - JSC::PropertyName)) +static JSValue constructPlatform(VM& vm, JSObject* processObject) { - JSC::VM& vm = globalObject->vm(); - auto clientData = WebCore::clientData(vm); +#if defined(__APPLE__) + return JSC::jsString(vm, makeAtomString("darwin")); +#elif defined(__linux__) + return JSC::jsString(vm, makeAtomString("linux")); +#else +#error "Unknown platform" +#endif +} - Zig::Process* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); - if (!thisObject) { - return JSValue::encode(JSC::jsUndefined()); +static JSValue constructBrowser(VM& vm, JSObject* processObject) +{ + return jsBoolean(false); +} + +static JSValue constructVersion(VM& vm, JSObject* processObject) +{ + return JSC::jsString(vm, makeString("v", REPORTED_NODE_VERSION)); +} + +static JSValue constructIsBun(VM& vm, JSObject* processObject) +{ + return jsBoolean(true); +} + +static JSValue constructRevision(VM& vm, JSObject* processObject) +{ + return JSC::jsString(vm, makeAtomString(Bun__version_sha)); +} + +static JSValue constructEnv(VM& vm, JSObject* processObject) +{ + auto* globalObject = jsCast(processObject->globalObject()); + return globalObject->processEnvObject(); +} + +JSC_DEFINE_HOST_FUNCTION(Process_functiongetuid, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + return JSValue::encode(jsNumber(getuid())); +} + +JSC_DEFINE_HOST_FUNCTION(Process_functiongeteuid, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + return JSValue::encode(jsNumber(geteuid())); +} + +JSC_DEFINE_HOST_FUNCTION(Process_functiongetegid, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + return JSValue::encode(jsNumber(getegid())); +} + +JSC_DEFINE_HOST_FUNCTION(Process_functiongetgid, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + return JSValue::encode(jsNumber(getgid())); +} + +JSC_DEFINE_HOST_FUNCTION(Process_functiongetgroups, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + int ngroups = getgroups(0, nullptr); + auto throwScope = DECLARE_THROW_SCOPE(vm); + if (ngroups == -1) { + SystemError error; + error.errno_ = errno; + error.syscall = Bun::toString("getgroups"_s); + throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); + return JSValue::encode(jsUndefined()); } - auto scope = DECLARE_THROW_SCOPE(vm); - JSC::JSObject* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 19); + gid_t egid = getegid(); + JSArray* groups = constructEmptyArray(globalObject, nullptr, static_cast(ngroups)); + Vector groupVector(ngroups); + getgroups(1, &egid); + bool needsEgid = true; + for (unsigned i = 0; i < ngroups; i++) { + auto current = groupVector[i]; + if (current == needsEgid) { + needsEgid = false; + } - object->putDirect(vm, JSC::Identifier::fromString(vm, "node"_s), - JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(REPORTED_NODE_VERSION)))); - object->putDirect( - vm, JSC::Identifier::fromString(vm, "bun"_s), - JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(Bun__version + 1 /* prefix with v */)))); - object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s), - JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(BUN_WEBKIT_VERSION)))); - object->putDirect(vm, JSC::Identifier::fromString(vm, "boringssl"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_boringssl))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "libarchive"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_libarchive))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "mimalloc"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_mimalloc))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "picohttpparser"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_picohttpparser))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "uwebsockets"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_uws))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_webkit))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "zig"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zig))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "zlib"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zlib))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "tinycc"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_tinycc))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "lolhtml"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_lolhtml))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "ares"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_c_ares))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "usockets"_s), - JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_usockets))), 0); + groups->putDirectIndex(globalObject, i, jsNumber(current)); + } - object->putDirect(vm, JSC::Identifier::fromString(vm, "v8"_s), JSValue(JSC::jsString(vm, makeString("10.8.168.20-node.8"_s))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "uv"_s), JSValue(JSC::jsString(vm, makeString("1.44.2"_s))), 0); - object->putDirect(vm, JSC::Identifier::fromString(vm, "napi"_s), JSValue(JSC::jsString(vm, makeString("8"_s))), 0); + if (needsEgid) + groups->push(globalObject, jsNumber(egid)); - object->putDirect(vm, JSC::Identifier::fromString(vm, "modules"_s), - JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString("108")))); + return JSValue::encode(groups); +} - thisObject->putDirect(vm, clientData->builtinNames().versionsPublicName(), object, 0); +JSC_DEFINE_HOST_FUNCTION(Process_functionAssert, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); - RETURN_IF_EXCEPTION(scope, {}); + JSValue arg0 = callFrame->argument(0); + bool condition = arg0.toBoolean(globalObject); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + if (condition) { + return JSValue::encode(jsUndefined()); + } - return JSValue::encode(object); + JSValue arg1 = callFrame->argument(1); + String message = arg1.isUndefined() ? String() : arg1.toWTFString(globalObject); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + auto error = createError(globalObject, makeString("Assertion failed: "_s, message)); + error->putDirect(vm, Identifier::fromString(vm, "code"_s), jsString(vm, makeString("ERR_ASSERTION"_s))); + throwException(globalObject, throwScope, error); + return JSValue::encode(jsUndefined()); } -JSC_DEFINE_CUSTOM_SETTER(Process_setVersionsLazy, + +JSC_DEFINE_HOST_FUNCTION(Process_functionReallyExit, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + uint8_t exitCode = 0; + JSValue arg0 = callFrame->argument(0); + if (arg0.isNumber()) { + if (!arg0.isInt32()) { + throwRangeError(globalObject, throwScope, "The \"code\" argument must be an integer"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + int extiCode32 = arg0.toInt32(globalObject); + RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::JSValue {})); + + if (extiCode32 < 0 || extiCode32 > 127) { + throwRangeError(globalObject, throwScope, "The \"code\" argument must be an integer between 0 and 127"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + exitCode = static_cast(extiCode32); + } else if (!arg0.isUndefinedOrNull()) { + throwTypeError(globalObject, throwScope, "The \"code\" argument must be an integer"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } else { + exitCode = Bun__getExitCode(Bun__getVM()); + } + + auto* zigGlobal = jsDynamicCast(globalObject); + if (UNLIKELY(!zigGlobal)) { + zigGlobal = Bun__getDefaultGlobal(); + } + Bun__Process__exit(zigGlobal, exitCode); + __builtin_unreachable(); +} + +JSC_DEFINE_HOST_FUNCTION(Process_functionOpenStdin, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + Zig::GlobalObject* global = jsDynamicCast(globalObject); + if (UNLIKELY(!global)) { + global = Bun__getDefaultGlobal(); + } + auto throwScope = DECLARE_THROW_SCOPE(vm); + + if (JSValue stdin = global->processObject()->getIfPropertyExists(globalObject, Identifier::fromString(vm, "stdin"_s))) { + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + + if (!stdin.isObject()) { + throwTypeError(globalObject, throwScope, "stdin is not an object"_s); + return JSValue::encode(jsUndefined()); + } + + JSValue resumeValue = stdin.getObject()->getIfPropertyExists(globalObject, Identifier::fromString(vm, "resume"_s)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + if (!resumeValue.isUndefinedOrNull()) { + auto resumeFunction = jsDynamicCast(resumeValue); + if (UNLIKELY(!resumeFunction)) { + throwTypeError(globalObject, throwScope, "stdin.resume is not a function"_s); + return JSValue::encode(jsUndefined()); + } + + auto callData = getCallData(resumeFunction); + + MarkedArgumentBuffer args; + JSC::call(globalObject, resumeFunction, callData, stdin, args); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + + RELEASE_AND_RETURN(throwScope, JSValue::encode(stdin)); + } + + RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined())); +} + +JSC_DEFINE_HOST_FUNCTION(Process_stubEmptyFunction, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + return JSValue::encode(jsUndefined()); +} + +JSC_DEFINE_HOST_FUNCTION(Process_stubFunctionReturningArray, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + return JSValue::encode(JSC::constructEmptyArray(globalObject, nullptr)); +} + +static JSValue Process_stubEmptyObject(VM& vm, JSObject* processObject) +{ + return JSC::constructEmptyObject(processObject->globalObject()); +} + +static JSValue Process_stubEmptyArray(VM& vm, JSObject* processObject) +{ + return JSC::constructEmptyArray(processObject->globalObject(), nullptr); +} + +static JSValue Process_stubEmptySet(VM& vm, JSObject* processObject) +{ + auto* globalObject = processObject->globalObject(); + return JSSet::create(vm, globalObject->setStructure()); +} + +static JSValue constructFeatures(VM& vm, JSObject* processObject) +{ + // { + // inspector: true, + // debug: false, + // uv: true, + // ipv6: true, + // tls_alpn: true, + // tls_sni: true, + // tls_ocsp: true, + // tls: true, + // cached_builtins: [Getter] + // } + auto* globalObject = processObject->globalObject(); + auto* object = constructEmptyObject(globalObject); + + object->putDirect(vm, Identifier::fromString(vm, "inspector"_s), jsBoolean(true)); +#ifdef BUN_DEBUG + object->putDirect(vm, Identifier::fromString(vm, "debug"_s), jsBoolean(true)); +#else + object->putDirect(vm, Identifier::fromString(vm, "debug"_s), jsBoolean(false)); +#endif + // lying + object->putDirect(vm, Identifier::fromString(vm, "uv"_s), jsBoolean(true)); + + object->putDirect(vm, Identifier::fromString(vm, "ipv6"_s), jsBoolean(true)); + object->putDirect(vm, Identifier::fromString(vm, "tls_alpn"_s), jsBoolean(true)); + object->putDirect(vm, Identifier::fromString(vm, "tls_sni"_s), jsBoolean(true)); + object->putDirect(vm, Identifier::fromString(vm, "tls_ocsp"_s), jsBoolean(true)); + object->putDirect(vm, Identifier::fromString(vm, "tls"_s), jsBoolean(true)); + object->putDirect(vm, Identifier::fromString(vm, "cached_builtins"_s), jsBoolean(true)); + + return object; +} + +static int _debugPort; + +JSC_DEFINE_CUSTOM_GETTER(processDebugPort, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + if (_debugPort == 0) { + _debugPort = 9229; + } + + return JSC::JSValue::encode(jsNumber(_debugPort)); +} + +JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue encodedValue, JSC::PropertyName)) +{ + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + JSValue value = JSValue::decode(encodedValue); + + if (!value.isInt32()) { + throwRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s); + return false; + } + + int port = value.asInt32(); + + if (port != 0) { + if (port < 1024 || port > 65535) { + throwRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s); + return false; + } + } + + _debugPort = port; + return true; +} + +JSC_DEFINE_CUSTOM_GETTER(processTitle, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + ZigString str; + Bun__Process__getTitle(globalObject, &str); + return JSValue::encode(Zig::toJSStringValue(str, globalObject)); +} + +JSC_DEFINE_CUSTOM_SETTER(setProcessTitle, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName)) { JSC::VM& vm = globalObject->vm(); - auto clientData = WebCore::clientData(vm); - Zig::Process* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); - if (!thisObject) { - return JSValue::encode(JSC::jsUndefined()); + JSC::JSObject* thisObject = JSC::jsDynamicCast(JSValue::decode(thisValue)); + JSC::JSString* jsString = JSC::jsDynamicCast(JSValue::decode(value)); + if (!thisObject || !jsString) { + return false; } - thisObject->putDirect(vm, clientData->builtinNames().versionsPublicName(), - JSC::JSValue::decode(value), 0); + ZigString str = Zig::toZigString(jsString, globalObject); + Bun__Process__setTitle(globalObject, &str); return true; } -static JSC_DEFINE_HOST_FUNCTION(Process_functionCwd, +JSC_DEFINE_HOST_FUNCTION(Process_functionCwd, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); @@ -1091,4 +1121,78 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionCwd, return JSC::JSValue::encode(result); } +/* Source for Process.lut.h +@begin processObjectTable + abort Process_functionAbort Function 1 + allowedNodeEnvironmentFlags Process_stubEmptySet PropertyCallback + arch constructArch PropertyCallback + argv constructArgv PropertyCallback + argv0 constructArgv0 PropertyCallback + assert Process_functionAssert Function 1 + binding JSBuiltin Function 1 + browser constructBrowser PropertyCallback + chdir Process_functionChdir Function 1 + config constructProcessConfigObject PropertyCallback + debugPort processDebugPort CustomAccessor + exitCode processExitCode CustomAccessor + title processTitle CustomAccessor + cwd Process_functionCwd Function 1 + dlopen Process_functionDlopen Function 1 + emitWarning Process_emitWarning Function 1 + env constructEnv PropertyCallback + execArgv constructExecArgv PropertyCallback + execPath constructExecPath PropertyCallback + exit Process_functionExit Function 1 + features constructFeatures PropertyCallback + getActiveResourcesInfo Process_stubFunctionReturningArray Function 0 + getegid Process_functiongetegid Function 0 + geteuid Process_functiongeteuid Function 0 + getgid Process_functiongetgid Function 0 + getgroups Process_functiongetgroups Function 0 + getuid Process_functiongetuid Function 0 + hrtime constructProcessHrtimeObject PropertyCallback + isBun constructIsBun PropertyCallback + mainModule JSBuiltin ReadOnly|Builtin|Accessor|Function 0 + moduleLoadList Process_stubEmptyArray PropertyCallback + nextTick Process_functionNextTick Function 1 + openStdin Process_functionOpenStdin Function 0 + pid constructPid PropertyCallback + platform constructPlatform PropertyCallback + ppid constructPpid PropertyCallback + reallyExit Process_functionReallyExit Function 1 + release constructProcessReleaseObject PropertyCallback + revision constructRevision PropertyCallback + setSourceMapsEnabled Process_stubEmptyFunction Function 1 + stderr constructStderr PropertyCallback + stdin constructStdin PropertyCallback + stdout constructStdout PropertyCallback + umask Process_functionUmask Function 1 + uptime Process_functionUptime Function 1 + version constructVersion PropertyCallback + versions constructVersions PropertyCallback + _debugEnd Process_stubEmptyFunction Function 0 + _debugProcess Process_stubEmptyFunction Function 0 + _fatalException Process_stubEmptyFunction Function 1 + _getActiveRequests Process_stubFunctionReturningArray Function 0 + _getActiveHandles Process_stubFunctionReturningArray Function 0 + _linkedBinding Process_stubEmptyFunction Function 0 + _preload_modules Process_stubEmptyObject PropertyCallback + _rawDebug Process_stubEmptyFunction Function 0 + _startProfilerIdleNotifier Process_stubEmptyFunction Function 0 + _stopProfilerIdleNotifier Process_stubEmptyFunction Function 0 + _tickCallback Process_stubEmptyFunction Function 0 +@end +*/ + +#include "Process.lut.h" +const JSC::ClassInfo Process::s_info = { "Process"_s, &Base::s_info, &processObjectTable, nullptr, + CREATE_METHOD_TABLE(Process) }; + +void Process::finishCreation(JSC::VM& vm) +{ + Base::finishCreation(vm); + + this->putDirect(vm, vm.propertyNames->toStringTagSymbol, jsString(vm, String("process"_s)), 0); +} + } // namespace Zig diff --git a/src/bun.js/bindings/Process.h b/src/bun.js/bindings/Process.h index 322b39078..8b703b8b1 100644 --- a/src/bun.js/bindings/Process.h +++ b/src/bun.js/bindings/Process.h @@ -28,7 +28,7 @@ public: ~Process(); - static constexpr unsigned StructureFlags = Base::StructureFlags; + static constexpr unsigned StructureFlags = Base::StructureFlags | HasStaticPropertyTable; static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) diff --git a/src/bun.js/bindings/Process.lut.h b/src/bun.js/bindings/Process.lut.h new file mode 100644 index 000000000..5c0bd2c5a --- /dev/null +++ b/src/bun.js/bindings/Process.lut.h @@ -0,0 +1,208 @@ +// File generated via `make generate-builtins` +static const struct CompactHashIndex processObjectTableIndex[141] = { + { 42, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 16, 130 }, + { -1, -1 }, + { -1, -1 }, + { 19, 137 }, + { -1, -1 }, + { 43, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 3, 140 }, + { 1, 128 }, + { -1, -1 }, + { 57, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 30, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 50, -1 }, + { 27, -1 }, + { 10, -1 }, + { -1, -1 }, + { 11, -1 }, + { -1, -1 }, + { 15, 136 }, + { -1, -1 }, + { 35, -1 }, + { -1, -1 }, + { 37, -1 }, + { 53, -1 }, + { 34, -1 }, + { 6, 138 }, + { -1, -1 }, + { 49, -1 }, + { 4, -1 }, + { 45, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 2, -1 }, + { 7, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 39, -1 }, + { -1, -1 }, + { 36, -1 }, + { -1, -1 }, + { 0, -1 }, + { 12, 129 }, + { 17, 131 }, + { 38, -1 }, + { -1, -1 }, + { 23, -1 }, + { 13, -1 }, + { -1, -1 }, + { -1, -1 }, + { 56, -1 }, + { -1, -1 }, + { -1, -1 }, + { 47, -1 }, + { -1, -1 }, + { 29, -1 }, + { 22, -1 }, + { -1, -1 }, + { -1, -1 }, + { 24, -1 }, + { -1, -1 }, + { -1, -1 }, + { 20, -1 }, + { -1, -1 }, + { 5, -1 }, + { -1, -1 }, + { -1, -1 }, + { 46, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 14, 132 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 9, -1 }, + { 25, 134 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 21, 135 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 44, 139 }, + { -1, -1 }, + { 18, -1 }, + { 8, -1 }, + { 26, -1 }, + { 28, -1 }, + { 31, 133 }, + { 32, -1 }, + { 33, -1 }, + { 40, -1 }, + { 41, -1 }, + { 48, -1 }, + { 51, -1 }, + { 52, -1 }, + { 54, -1 }, + { 55, -1 }, +}; + +static const struct HashTableValue processObjectTableValues[58] = { + { "abort"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionAbort, 1 } }, + { "allowedNodeEnvironmentFlags"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptySet } }, + { "arch"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArch } }, + { "argv"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArgv } }, + { "argv0"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArgv0 } }, + { "assert"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionAssert, 1 } }, + { "binding"_s, ((static_cast(PropertyAttribute::Function)) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, NoIntrinsic, { HashTableValue::BuiltinGeneratorType, processObjectBindingCodeGenerator, 1 } }, + { "browser"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructBrowser } }, + { "chdir"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionChdir, 1 } }, + { "config"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessConfigObject } }, + { "debugPort"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processDebugPort, setProcessDebugPort } }, + { "exitCode"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processExitCode, setProcessExitCode } }, + { "title"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processTitle, setProcessTitle } }, + { "cwd"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionCwd, 1 } }, + { "dlopen"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionDlopen, 1 } }, + { "emitWarning"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_emitWarning, 1 } }, + { "env"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructEnv } }, + { "execArgv"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructExecArgv } }, + { "execPath"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructExecPath } }, + { "exit"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionExit, 1 } }, + { "features"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructFeatures } }, + { "getActiveResourcesInfo"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubFunctionReturningArray, 0 } }, + { "getegid"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetegid, 0 } }, + { "geteuid"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongeteuid, 0 } }, + { "getgid"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetgid, 0 } }, + { "getgroups"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetgroups, 0 } }, + { "getuid"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetuid, 0 } }, + { "hrtime"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessHrtimeObject } }, + { "isBun"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructIsBun } }, + { "mainModule"_s, ((static_cast(PropertyAttribute::ReadOnly|PropertyAttribute::Builtin|PropertyAttribute::Accessor|PropertyAttribute::Function)) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, NoIntrinsic, { HashTableValue::BuiltinGeneratorType, processObjectMainModuleCodeGenerator, 0 } }, + { "moduleLoadList"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptyArray } }, + { "nextTick"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionNextTick, 1 } }, + { "openStdin"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionOpenStdin, 0 } }, + { "pid"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructPid } }, + { "platform"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructPlatform } }, + { "ppid"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructPpid } }, + { "reallyExit"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionReallyExit, 1 } }, + { "release"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessReleaseObject } }, + { "revision"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructRevision } }, + { "setSourceMapsEnabled"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 1 } }, + { "stderr"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructStderr } }, + { "stdin"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructStdin } }, + { "stdout"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructStdout } }, + { "umask"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionUmask, 1 } }, + { "uptime"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionUptime, 1 } }, + { "version"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructVersion } }, + { "versions"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructVersions } }, + { "_debugEnd"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, + { "_debugProcess"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, + { "_fatalException"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 1 } }, + { "_getActiveRequests"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubFunctionReturningArray, 0 } }, + { "_getActiveHandles"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubFunctionReturningArray, 0 } }, + { "_linkedBinding"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, + { "_preload_modules"_s, static_cast(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptyObject } }, + { "_rawDebug"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, + { "_startProfilerIdleNotifier"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, + { "_stopProfilerIdleNotifier"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, + { "_tickCallback"_s, static_cast(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } }, +}; + +static const struct HashTable processObjectTable = + { 58, 127, true, nullptr, processObjectTableValues, processObjectTableIndex }; diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 4bb5445e1..3291b204e 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -3160,11 +3160,7 @@ void GlobalObject::finishCreation(VM& vm) Zig::GlobalObject* globalObject = reinterpret_cast(init.owner); auto* process = Zig::Process::create( *globalObject, Zig::Process::createStructure(init.vm, init.owner, WebCore::JSEventEmitter::prototype(init.vm, *globalObject))); - process->putDirectCustomAccessor(init.vm, JSC::Identifier::fromString(init.vm, "env"_s), - JSC::CustomGetterSetter::create(init.vm, lazyProcessEnvGetter, lazyProcessEnvSetter), - JSC::PropertyAttribute::DontDelete - | JSC::PropertyAttribute::CustomValue - | 0); + init.set(process); }); diff --git a/src/bun.js/modules/ProcessModule.h b/src/bun.js/modules/ProcessModule.h index 2df74598a..fab0298ae 100644 --- a/src/bun.js/modules/ProcessModule.h +++ b/src/bun.js/modules/ProcessModule.h @@ -44,11 +44,19 @@ inline void generateProcessSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, reinterpret_cast(lexicalGlobalObject); JSC::JSObject *process = globalObject->processObject(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!process->staticPropertiesReified()) { + process->reifyAllStaticProperties(globalObject); + if (scope.exception()) + return; + } PropertyNameArray properties(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude); process->getPropertyNames(globalObject, properties, DontEnumPropertiesMode::Exclude); + if (scope.exception()) + return; exportNames.append(vm.propertyNames->defaultKeyword); exportValues.append(process); diff --git a/src/js/builtins/ProcessObjectInternals.ts b/src/js/builtins/ProcessObjectInternals.ts index 8b24e68ba..8e2449a43 100644 --- a/src/js/builtins/ProcessObjectInternals.ts +++ b/src/js/builtins/ProcessObjectInternals.ts @@ -49,9 +49,14 @@ export function binding(bindingName) { return constants; } -export function getStdioWriteStream(fd_, rawRequire) { - var module = { path: "node:process", require: rawRequire }; - var require = path => module.require(path); +export function getStdioWriteStream(fd_) { + var require = path => { + var existing = $requireMap.get(path); + if (existing) return existing.exports; + + return $internalRequire(path); + }; + var module = { path: "node:process", require }; function createStdioWriteStream(fd_) { var { Duplex, eos, destroy } = require("node:stream"); @@ -472,10 +477,15 @@ export function getStdioWriteStream(fd_, rawRequire) { return new FastStdioWriteStream(fd_); } -export function getStdinStream(fd_, rawRequire, Bun) { - var module = { path: "node:process", require: rawRequire }; - var require = path => module.require(path); +export function getStdinStream(fd_) { + var require = path => { + var existing = $requireMap.get(path); + if (existing) return existing.exports; + + return $internalRequire(path); + }; + var module = { path: "node:process", require: require }; var { Duplex, eos, destroy } = require("node:stream"); var StdinStream = class StdinStream extends Duplex { diff --git a/src/js/builtins/codegen/index.ts b/src/js/builtins/codegen/index.ts index e20601a15..a5d3c2dc8 100644 --- a/src/js/builtins/codegen/index.ts +++ b/src/js/builtins/codegen/index.ts @@ -3,7 +3,35 @@ import path from "path"; import { sliceSourceCode } from "./builtin-parser"; import { applyGlobalReplacements, enums, globalsToPrefix } from "./replacements"; import { cap, fmtCPPString, low } from "./helpers"; +import { spawn, spawnSync } from "bun"; + +async function createStaticHashtables() { + const STATIC_HASH_TABLES = ["src/bun.js/bindings/Process.cpp"]; + console.time("Creating static hash tables..."); + const create_hash_table = path.join( + import.meta.dir, + "../../../../bun-webkit/Source/JavaScriptCore/create_hash_table", + ); + for (let cpp of STATIC_HASH_TABLES) { + cpp = path.join(import.meta.dir, "../../../../", cpp); + const { stdout, exited } = spawn({ + cmd: [create_hash_table, cpp], + stdout: "pipe", + stderr: "inherit", + }); + await exited; + let str = await new Response(stdout).text(); + str = str.replaceAll(/^\/\/.*$/gm, ""); + str = str.replaceAll(/^#include.*$/gm, ""); + str = str.replaceAll(`namespace JSC {`, ""); + str = str.replaceAll(`} // namespace JSC`, ""); + str = "// File generated via `make generate-builtins`\n" + str.trim() + "\n"; + await Bun.write(cpp.replace(/\.cpp$/, ".lut.h"), str); + } + console.timeEnd("Creating static hash tables..."); +} +const staticHashTablePromise = createStaticHashtables(); console.log("Bundling Bun builtins..."); const MINIFY = process.argv.includes("--minify") || process.argv.includes("-m"); @@ -618,6 +646,8 @@ if (!KEEP_TMP) { await rmSync(TMP_DIR, { recursive: true }); } +await staticHashTablePromise; + console.log( `Embedded JS size: %s bytes (across %s functions, %s files)`, totalJSSize, diff --git a/src/js/out/WebCoreJSBuiltins.cpp b/src/js/out/WebCoreJSBuiltins.cpp index 5ad79fa10..f20f626d4 100644 --- a/src/js/out/WebCoreJSBuiltins.cpp +++ b/src/js/out/WebCoreJSBuiltins.cpp @@ -372,7 +372,7 @@ const JSC::ConstructorKind s_writableStreamInternalsSetUpWritableStreamDefaultCo const JSC::ImplementationVisibility s_writableStreamInternalsSetUpWritableStreamDefaultControllerFromUnderlyingSinkCodeImplementationVisibility = JSC::ImplementationVisibility::Public; const int s_writableStreamInternalsSetUpWritableStreamDefaultControllerFromUnderlyingSinkCodeLength = 561; static const JSC::Intrinsic s_writableStreamInternalsSetUpWritableStreamDefaultControllerFromUnderlyingSinkCodeIntrinsic = JSC::NoIntrinsic; -const char* const s_writableStreamInternalsSetUpWritableStreamDefaultControllerFromUnderlyingSinkCode = "(function (C,_,v,E,F){\"use strict\";const f=new @WritableStreamDefaultController;let p=()=>{},q=()=>{return @Promise.@resolve()},x=()=>{return @Promise.@resolve()},B=()=>{return @Promise.@resolve()};if(\"start\"in v){const P=v.start;p=()=>@promiseInvokeOrNoopMethodNoCatch(_,P,[f])}if(\"write\"in v){const P=v.write;q=(j)=>@promiseInvokeOrNoopMethod(_,P,[j,f])}if(\"close\"in v){const P=v.close;x=()=>@promiseInvokeOrNoopMethod(_,P,[])}if(\"abort\"in v){const P=v.abort;B=(j)=>@promiseInvokeOrNoopMethod(_,P,[j])}@setUpWritableStreamDefaultController(C,f,p,q,x,B,E,F)})\n"; +const char* const s_writableStreamInternalsSetUpWritableStreamDefaultControllerFromUnderlyingSinkCode = "(function (C,O,_,E,F){\"use strict\";const f=new @WritableStreamDefaultController;let q=()=>{},v=()=>{return @Promise.@resolve()},x=()=>{return @Promise.@resolve()},B=()=>{return @Promise.@resolve()};if(\"start\"in _){const p=_.start;q=()=>@promiseInvokeOrNoopMethodNoCatch(O,p,[f])}if(\"write\"in _){const p=_.write;v=(j)=>@promiseInvokeOrNoopMethod(O,p,[j,f])}if(\"close\"in _){const p=_.close;x=()=>@promiseInvokeOrNoopMethod(O,p,[])}if(\"abort\"in _){const p=_.abort;B=(j)=>@promiseInvokeOrNoopMethod(O,p,[j])}@setUpWritableStreamDefaultController(C,f,q,v,x,B,E,F)})\n"; // writableStreamDefaultControllerAdvanceQueueIfNeeded const JSC::ConstructAbility s_writableStreamInternalsWritableStreamDefaultControllerAdvanceQueueIfNeededCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; @@ -646,17 +646,17 @@ const char* const s_processObjectInternalsBindingCode = "(function (I){\"use str const JSC::ConstructAbility s_processObjectInternalsGetStdioWriteStreamCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; const JSC::ConstructorKind s_processObjectInternalsGetStdioWriteStreamCodeConstructorKind = JSC::ConstructorKind::None; const JSC::ImplementationVisibility s_processObjectInternalsGetStdioWriteStreamCodeImplementationVisibility = JSC::ImplementationVisibility::Public; -const int s_processObjectInternalsGetStdioWriteStreamCodeLength = 4250; +const int s_processObjectInternalsGetStdioWriteStreamCodeLength = 4311; static const JSC::Intrinsic s_processObjectInternalsGetStdioWriteStreamCodeIntrinsic = JSC::NoIntrinsic; -const char* const s_processObjectInternalsGetStdioWriteStreamCode = "(function (Z,Y){\"use strict\";var P={path:\"node:process\",require:Y},L=(M)=>P.require(M);function A(M){var{Duplex:B,eos:j,destroy:z}=L(\"node:stream\"),J=class O extends B{#j;#$;#M=!0;#N=!0;#J;#z;#B;#G;#H;#K;get isTTY(){return this.#K\?\?=L(\"node:tty\").isatty(M)}get fd(){return M}constructor(G){super({readable:!0,writable:!0});this.#J=`/dev/fd/${G}`}#L(G){const H=this.#z;if(this.#z=null,H)H(G);else if(G)this.destroy(G);else if(!this.#M&&!this.#N)this.destroy()}_destroy(G,H){if(!G&&this.#z!==null){var N=class X extends Error{code;name;constructor(Q=\"The operation was aborted\",K=void 0){if(K!==void 0&&typeof K!==\"object\")throw new Error(`Invalid AbortError options:\\n\\n${JSON.stringify(K,null,2)}`);super(Q,K);this.code=\"ABORT_ERR\",this.name=\"AbortError\"}};G=new N}if(this.#B=null,this.#G=null,this.#z===null)H(G);else{if(this.#z=H,this.#j)z(this.#j,G);if(this.#$)z(this.#$,G)}}_write(G,H,N){if(!this.#j){var{createWriteStream:X}=L(\"node:fs\"),Q=this.#j=X(this.#J);Q.on(\"finish\",()=>{if(this.#G){const K=this.#G;this.#G=null,K()}}),Q.on(\"drain\",()=>{if(this.#B){const K=this.#B;this.#B=null,K()}}),j(Q,(K)=>{if(this.#N=!1,K)z(Q,K);this.#L(K)})}if(Q.write(G,H))N();else this.#B=N}_final(G){this.#j&&this.#j.end(),this.#G=G}#O(){var{createReadStream:G}=L(\"node:fs\"),H=this.#$=G(this.#J);return H.on(\"readable\",()=>{if(this.#H){const N=this.#H;this.#H=null,N()}else this.read()}),H.on(\"end\",()=>{this.push(null)}),j(H,(N)=>{if(this.#M=!1,N)z(H,N);this.#L(N)}),H}_read(){var G=this.#$;if(!G)G=this.#O();while(!0){const H=G.read();if(H===null||!this.push(H))return}}};return new J(M)}var{EventEmitter:T}=L(\"node:events\");function V(M){if(!M)return!0;var B=M.toLowerCase();return B===\"utf8\"||B===\"utf-8\"||B===\"buffer\"||B===\"binary\"}var U,x=class M extends T{#j;#$;#M;#N;bytesWritten=0;setDefaultEncoding(B){if(this.#$||!V(B))return this.#B(),this.#$.setDefaultEncoding(B)}#J(){switch(this.#j){case 1:{var B=@Bun.stdout.writer({highWaterMark:0});return B.unref(),B}case 2:{var B=@Bun.stderr.writer({highWaterMark:0});return B.unref(),B}default:throw new Error(\"Unsupported writer\")}}#z(){return this.#M\?\?=this.#J()}constructor(B){super();this.#j=B}get fd(){return this.#j}get isTTY(){return this.#N\?\?=L(\"node:tty\").isatty(this.#j)}cursorTo(B,j,z){return(U\?\?=L(\"readline\")).cursorTo(this,B,j,z)}moveCursor(B,j,z){return(U\?\?=L(\"readline\")).moveCursor(this,B,j,z)}clearLine(B,j){return(U\?\?=L(\"readline\")).clearLine(this,B,j)}clearScreenDown(B){return(U\?\?=L(\"readline\")).clearScreenDown(this,B)}ref(){this.#z().ref()}unref(){this.#z().unref()}on(B,j){if(B===\"close\"||B===\"finish\")return this.#B(),this.#$.on(B,j);if(B===\"drain\")return super.on(\"drain\",j);if(B===\"error\")return super.on(\"error\",j);return super.on(B,j)}get _writableState(){return this.#B(),this.#$._writableState}get _readableState(){return this.#B(),this.#$._readableState}pipe(B){return this.#B(),this.#$.pipe(B)}unpipe(B){return this.#B(),this.#$.unpipe(B)}#B(){if(this.#$)return;this.#$=A(this.#j);const B=this.eventNames();for(let j of B)this.#$.on(j,(...z)=>{this.emit(j,...z)})}#G(B){var j=this.#z();const z=j.write(B);this.bytesWritten+=z;const J=j.flush(!1);return!!(z||J)}#H(B,j){if(!V(j))return this.#B(),this.#$.write(B,j);return this.#G(B)}#K(B,j){if(j)this.emit(\"error\",j);try{B(j\?j:null)}catch(z){this.emit(\"error\",z)}}#L(B,j,z){if(!V(j))return this.#B(),this.#$.write(B,j,z);var J=this.#z();const O=J.write(B),G=J.flush(!0);if(G\?.then)return G.then(()=>{this.#K(z),this.emit(\"drain\")},(H)=>this.#K(z,H)),!1;return queueMicrotask(()=>{this.#K(z)}),!!(O||G)}write(B,j,z){const J=this._write(B,j,z);if(J)this.emit(\"drain\");return J}get hasColors(){return @Bun.tty[this.#j].hasColors}_write(B,j,z){var J=this.#$;if(J)return J.write(B,j,z);switch(arguments.length){case 0:{var O=new Error(\"Invalid arguments\");throw O.code=\"ERR_INVALID_ARG_TYPE\",O}case 1:return this.#G(B);case 2:if(typeof j===\"function\")return this.#L(B,\"\",j);else if(typeof j===\"string\")return this.#H(B,j);default:{if(typeof j!==\"undefined\"&&typeof j!==\"string\"||typeof z!==\"undefined\"&&typeof z!==\"function\"){var O=new Error(\"Invalid arguments\");throw O.code=\"ERR_INVALID_ARG_TYPE\",O}if(typeof z===\"undefined\")return this.#H(B,j);return this.#L(B,j,z)}}}destroy(){return this}end(){return this}};return new x(Z)})\n"; +const char* const s_processObjectInternalsGetStdioWriteStreamCode = "(function (Z){\"use strict\";var L=(M)=>{var G=@requireMap.get(M);if(G)return G.exports;return @internalRequire(M)},D={path:\"node:process\",require:L};function Y(M){var{Duplex:G,eos:j,destroy:z}=L(\"node:stream\"),J=class O extends G{#j;#$;#M=!0;#N=!0;#J;#z;#G;#B;#H;#K;get isTTY(){return this.#K\?\?=L(\"node:tty\").isatty(M)}get fd(){return M}constructor(B){super({readable:!0,writable:!0});this.#J=`/dev/fd/${B}`}#L(B){const H=this.#z;if(this.#z=null,H)H(B);else if(B)this.destroy(B);else if(!this.#M&&!this.#N)this.destroy()}_destroy(B,H){if(!B&&this.#z!==null){var N=class X extends Error{code;name;constructor(Q=\"The operation was aborted\",K=void 0){if(K!==void 0&&typeof K!==\"object\")throw new Error(`Invalid AbortError options:\\n\\n${JSON.stringify(K,null,2)}`);super(Q,K);this.code=\"ABORT_ERR\",this.name=\"AbortError\"}};B=new N}if(this.#G=null,this.#B=null,this.#z===null)H(B);else{if(this.#z=H,this.#j)z(this.#j,B);if(this.#$)z(this.#$,B)}}_write(B,H,N){if(!this.#j){var{createWriteStream:X}=L(\"node:fs\"),Q=this.#j=X(this.#J);Q.on(\"finish\",()=>{if(this.#B){const K=this.#B;this.#B=null,K()}}),Q.on(\"drain\",()=>{if(this.#G){const K=this.#G;this.#G=null,K()}}),j(Q,(K)=>{if(this.#N=!1,K)z(Q,K);this.#L(K)})}if(Q.write(B,H))N();else this.#G=N}_final(B){this.#j&&this.#j.end(),this.#B=B}#O(){var{createReadStream:B}=L(\"node:fs\"),H=this.#$=B(this.#J);return H.on(\"readable\",()=>{if(this.#H){const N=this.#H;this.#H=null,N()}else this.read()}),H.on(\"end\",()=>{this.push(null)}),j(H,(N)=>{if(this.#M=!1,N)z(H,N);this.#L(N)}),H}_read(){var B=this.#$;if(!B)B=this.#O();while(!0){const H=B.read();if(H===null||!this.push(H))return}}};return new J(M)}var{EventEmitter:P}=L(\"node:events\");function V(M){if(!M)return!0;var G=M.toLowerCase();return G===\"utf8\"||G===\"utf-8\"||G===\"buffer\"||G===\"binary\"}var U,A=class M extends P{#j;#$;#M;#N;bytesWritten=0;setDefaultEncoding(G){if(this.#$||!V(G))return this.#G(),this.#$.setDefaultEncoding(G)}#J(){switch(this.#j){case 1:{var G=@Bun.stdout.writer({highWaterMark:0});return G.unref(),G}case 2:{var G=@Bun.stderr.writer({highWaterMark:0});return G.unref(),G}default:throw new Error(\"Unsupported writer\")}}#z(){return this.#M\?\?=this.#J()}constructor(G){super();this.#j=G}get fd(){return this.#j}get isTTY(){return this.#N\?\?=L(\"node:tty\").isatty(this.#j)}cursorTo(G,j,z){return(U\?\?=L(\"readline\")).cursorTo(this,G,j,z)}moveCursor(G,j,z){return(U\?\?=L(\"readline\")).moveCursor(this,G,j,z)}clearLine(G,j){return(U\?\?=L(\"readline\")).clearLine(this,G,j)}clearScreenDown(G){return(U\?\?=L(\"readline\")).clearScreenDown(this,G)}ref(){this.#z().ref()}unref(){this.#z().unref()}on(G,j){if(G===\"close\"||G===\"finish\")return this.#G(),this.#$.on(G,j);if(G===\"drain\")return super.on(\"drain\",j);if(G===\"error\")return super.on(\"error\",j);return super.on(G,j)}get _writableState(){return this.#G(),this.#$._writableState}get _readableState(){return this.#G(),this.#$._readableState}pipe(G){return this.#G(),this.#$.pipe(G)}unpipe(G){return this.#G(),this.#$.unpipe(G)}#G(){if(this.#$)return;this.#$=Y(this.#j);const G=this.eventNames();for(let j of G)this.#$.on(j,(...z)=>{this.emit(j,...z)})}#B(G){var j=this.#z();const z=j.write(G);this.bytesWritten+=z;const J=j.flush(!1);return!!(z||J)}#H(G,j){if(!V(j))return this.#G(),this.#$.write(G,j);return this.#B(G)}#K(G,j){if(j)this.emit(\"error\",j);try{G(j\?j:null)}catch(z){this.emit(\"error\",z)}}#L(G,j,z){if(!V(j))return this.#G(),this.#$.write(G,j,z);var J=this.#z();const O=J.write(G),B=J.flush(!0);if(B\?.then)return B.then(()=>{this.#K(z),this.emit(\"drain\")},(H)=>this.#K(z,H)),!1;return queueMicrotask(()=>{this.#K(z)}),!!(O||B)}write(G,j,z){const J=this._write(G,j,z);if(J)this.emit(\"drain\");return J}get hasColors(){return @Bun.tty[this.#j].hasColors}_write(G,j,z){var J=this.#$;if(J)return J.write(G,j,z);switch(arguments.length){case 0:{var O=new Error(\"Invalid arguments\");throw O.code=\"ERR_INVALID_ARG_TYPE\",O}case 1:return this.#B(G);case 2:if(typeof j===\"function\")return this.#L(G,\"\",j);else if(typeof j===\"string\")return this.#H(G,j);default:{if(typeof j!==\"undefined\"&&typeof j!==\"string\"||typeof z!==\"undefined\"&&typeof z!==\"function\"){var O=new Error(\"Invalid arguments\");throw O.code=\"ERR_INVALID_ARG_TYPE\",O}if(typeof z===\"undefined\")return this.#H(G,j);return this.#L(G,j,z)}}}destroy(){return this}end(){return this}};return new A(Z)})\n"; // getStdinStream const JSC::ConstructAbility s_processObjectInternalsGetStdinStreamCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; const JSC::ConstructorKind s_processObjectInternalsGetStdinStreamCodeConstructorKind = JSC::ConstructorKind::None; const JSC::ImplementationVisibility s_processObjectInternalsGetStdinStreamCodeImplementationVisibility = JSC::ImplementationVisibility::Public; -const int s_processObjectInternalsGetStdinStreamCodeLength = 1799; +const int s_processObjectInternalsGetStdinStreamCodeLength = 1861; static const JSC::Intrinsic s_processObjectInternalsGetStdinStreamCodeIntrinsic = JSC::NoIntrinsic; -const char* const s_processObjectInternalsGetStdinStreamCode = "(function (L,N,P){\"use strict\";var Q={path:\"node:process\",require:N},J=(K)=>Q.require(K),{Duplex:T,eos:U,destroy:M}=J(\"node:stream\"),V=class K extends T{#K;#j;#Y;#H=!0;#I=!1;#L=!0;#z;#$;#G;get isTTY(){return J(\"tty\").isatty(L)}get fd(){return L}constructor(){super({readable:!0,writable:!0})}#J(Y){const j=this.#$;if(this.#$=null,j)j(Y);else if(Y)this.destroy(Y);else if(!this.#H&&!this.#L)this.destroy()}_destroy(Y,j){if(!Y&&this.#$!==null){var z=class G extends Error{constructor(H=\"The operation was aborted\",I=void 0){if(I!==void 0&&typeof I!==\"object\")throw new Error(`Invalid AbortError options:\\n\\n${JSON.stringify(I,null,2)}`);super(H,I);this.code=\"ABORT_ERR\",this.name=\"AbortError\"}};Y=new z}if(this.#$===null)j(Y);else if(this.#$=j,this.#Y)M(this.#Y,Y)}setRawMode(Y){}on(Y,j){if(Y===\"readable\")this.ref(),this.#I=!0;return super.on(Y,j)}pause(){return this.unref(),super.pause()}resume(){return this.ref(),super.resume()}ref(){this.#K\?\?=P.stdin.stream().getReader(),this.#j\?\?=setInterval(()=>{},1<<30)}unref(){if(this.#j)clearInterval(this.#j),this.#j=null}async#M(){try{var Y,j;const z=this.#K.readMany();if(!z\?.then)({done:Y,value:j}=z);else({done:Y,value:j}=await z);if(!Y){this.push(j[0]);const G=j.length;for(let H=1;H{if(this.#z){const z=this.#z;this.#z=null,z()}}),j.on(\"drain\",()=>{if(this.#G){const z=this.#G;this.#G=null,z()}}),U(j,(z)=>{if(this.#L=!1,z)M(j,z);this.#J(z)}),j}_write(Y,j,z){var G=this.#Y;if(!G)G=this.#N();if(G.write(Y,j))z();else this.#G=z}_final(Y){this.#Y.end(),this.#z=(...j)=>Y(...j)}};return new V})\n"; +const char* const s_processObjectInternalsGetStdinStreamCode = "(function (K){\"use strict\";var H=(I)=>{var N=@requireMap.get(I);if(N)return N.exports;return @internalRequire(I)},T={path:\"node:process\",require:H},{Duplex:M,eos:P,destroy:L}=H(\"node:stream\"),Q=class I extends M{#J;#j;#$;#G=!0;#H=!1;#K=!0;#z;#N;#B;get isTTY(){return H(\"tty\").isatty(K)}get fd(){return K}constructor(){super({readable:!0,writable:!0})}#I(N){const j=this.#N;if(this.#N=null,j)j(N);else if(N)this.destroy(N);else if(!this.#G&&!this.#K)this.destroy()}_destroy(N,j){if(!N&&this.#N!==null){var z=class B extends Error{constructor(G=\"The operation was aborted\",J=void 0){if(J!==void 0&&typeof J!==\"object\")throw new Error(`Invalid AbortError options:\\n\\n${JSON.stringify(J,null,2)}`);super(G,J);this.code=\"ABORT_ERR\",this.name=\"AbortError\"}};N=new z}if(this.#N===null)j(N);else if(this.#N=j,this.#$)L(this.#$,N)}setRawMode(N){}on(N,j){if(N===\"readable\")this.ref(),this.#H=!0;return super.on(N,j)}pause(){return this.unref(),super.pause()}resume(){return this.ref(),super.resume()}ref(){this.#J\?\?=@Bun.stdin.stream().getReader(),this.#j\?\?=setInterval(()=>{},1<<30)}unref(){if(this.#j)clearInterval(this.#j),this.#j=null}async#L(){try{var N,j;const z=this.#J.readMany();if(!z\?.then)({done:N,value:j}=z);else({done:N,value:j}=await z);if(!N){this.push(j[0]);const B=j.length;for(let G=1;G{if(this.#z){const z=this.#z;this.#z=null,z()}}),j.on(\"drain\",()=>{if(this.#B){const z=this.#B;this.#B=null,z()}}),P(j,(z)=>{if(this.#K=!1,z)L(j,z);this.#I(z)}),j}_write(N,j,z){var B=this.#$;if(!B)B=this.#M();if(B.write(N,j))z();else this.#B=z}_final(N){this.#$.end(),this.#z=(...j)=>N(...j)}};return new Q})\n"; #define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \ JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \ @@ -2028,7 +2028,7 @@ const JSC::ConstructorKind s_readableStreamDefaultReaderReadManyCodeConstructorK const JSC::ImplementationVisibility s_readableStreamDefaultReaderReadManyCodeImplementationVisibility = JSC::ImplementationVisibility::Public; const int s_readableStreamDefaultReaderReadManyCodeLength = 2598; static const JSC::Intrinsic s_readableStreamDefaultReaderReadManyCodeIntrinsic = JSC::NoIntrinsic; -const char* const s_readableStreamDefaultReaderReadManyCode = "(function (){\"use strict\";if(!@isReadableStreamDefaultReader(this))@throwTypeError(\"ReadableStreamDefaultReader.readMany() should not be called directly\");const k=@getByIdDirectPrivate(this,\"ownerReadableStream\");if(!k)@throwTypeError(\"readMany() called on a reader owned by no readable stream\");const H=@getByIdDirectPrivate(k,\"state\");if(@putByIdDirectPrivate(k,\"disturbed\",!0),H===@streamClosed)return{value:[],size:0,done:!0};else if(H===@streamErrored)throw @getByIdDirectPrivate(k,\"storedError\");var w=@getByIdDirectPrivate(k,\"readableStreamController\"),E=@getByIdDirectPrivate(w,\"queue\");if(!E)return w.@pull(w).@then(function({done:_,value:B}){return _\?{done:!0,value:[],size:0}:{value:[B],size:1,done:!1}});const N=E.content;var O=E.size,A=N.toArray(!1),C=A.length;if(C>0){var j=@newArrayWithSize(C);if(@isReadableByteStreamController(w)){{const _=A[0];if(!(@ArrayBuffer.@isView(_)||_ instanceof @ArrayBuffer))@putByValDirect(j,0,new @Uint8Array(_.buffer,_.byteOffset,_.byteLength));else @putByValDirect(j,0,_)}for(var d=1;d{if(_.done)return{value:[],size:0,done:!0};var B=@getByIdDirectPrivate(k,\"readableStreamController\"),F=@getByIdDirectPrivate(B,\"queue\"),x=[_.value].concat(F.content.toArray(!1)),K=x.length;if(@isReadableByteStreamController(B))for(var I=0;I0){var D=@newArrayWithSize(C);if(@isReadableByteStreamController(B)){{const _=x[0];if(!(@ArrayBuffer.@isView(_)||_ instanceof @ArrayBuffer))@putByValDirect(D,0,new @Uint8Array(_.buffer,_.byteOffset,_.byteLength));else @putByValDirect(D,0,_)}for(var d=1;d{if(_.done)return{value:[],size:0,done:!0};var w=@getByIdDirectPrivate(j,\"readableStreamController\"),G=@getByIdDirectPrivate(w,\"queue\"),k=[_.value].concat(G.content.toArray(!1)),K=k.length;if(@isReadableByteStreamController(w))for(var A=0;A0}shift(){var{_head:v,_tail:b,_list:g,_capacityMask:k}=this;if(v===b)return @undefined;var w=g[v];if(@putByValDirect(g,v,@undefined),v=this._head=v+1&k,v<2&&b>1e4&&b<=g.length>>>2)this._shrinkArray();return w}peek(){if(this._head===this._tail)return @undefined;return this._list[this._head]}push(v){var b=this._tail;if(@putByValDirect(this._list,b,v),this._tail=b+1&this._capacityMask,this._tail===this._head)this._growArray()}toArray(v){var b=this._list,g=@toLength(b.length);if(v||this._head>this._tail){var k=@toLength(this._head),w=@toLength(this._tail),F=@toLength(g-k+w),z=@newArrayWithSize(F),B=0;for(var x=k;x>>=1,this._capacityMask>>>=1}}return new A})\n"; +const char* const s_streamInternalsCreateFIFOCode = "(function (){\"use strict\";var E=@Array.prototype.slice;class A{constructor(){this._head=0,this._tail=0,this._capacityMask=3,this._list=@newArrayWithSize(4)}_head;_tail;_capacityMask;_list;size(){if(this._head===this._tail)return 0;if(this._head0}shift(){var{_head:g,_tail:b,_list:x,_capacityMask:M}=this;if(g===b)return @undefined;var w=x[g];if(@putByValDirect(x,g,@undefined),g=this._head=g+1&M,g<2&&b>1e4&&b<=x.length>>>2)this._shrinkArray();return w}peek(){if(this._head===this._tail)return @undefined;return this._list[this._head]}push(g){var b=this._tail;if(@putByValDirect(this._list,b,g),this._tail=b+1&this._capacityMask,this._tail===this._head)this._growArray()}toArray(g){var b=this._list,x=@toLength(b.length);if(g||this._head>this._tail){var M=@toLength(this._head),w=@toLength(this._tail),F=@toLength(x-M+w),z=@newArrayWithSize(F),B=0;for(var v=M;v>>=1,this._capacityMask>>>=1}}return new A})\n"; // newQueue const JSC::ConstructAbility s_streamInternalsNewQueueCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; diff --git a/src/js/out/WebCoreJSBuiltins.h b/src/js/out/WebCoreJSBuiltins.h index d00ae735e..42d14b154 100644 --- a/src/js/out/WebCoreJSBuiltins.h +++ b/src/js/out/WebCoreJSBuiltins.h @@ -1181,8 +1181,8 @@ extern const JSC::ImplementationVisibility s_processObjectInternalsGetStdinStrea #define WEBCORE_FOREACH_PROCESSOBJECTINTERNALS_BUILTIN_DATA(macro) \ macro(binding, processObjectInternalsBinding, 1) \ - macro(getStdioWriteStream, processObjectInternalsGetStdioWriteStream, 2) \ - macro(getStdinStream, processObjectInternalsGetStdinStream, 3) \ + macro(getStdioWriteStream, processObjectInternalsGetStdioWriteStream, 1) \ + macro(getStdinStream, processObjectInternalsGetStdinStream, 1) \ #define WEBCORE_FOREACH_PROCESSOBJECTINTERNALS_BUILTIN_CODE(macro) \ macro(processObjectInternalsBindingCode, binding, ASCIILiteral(), s_processObjectInternalsBindingCodeLength) \ -- cgit v1.2.3