Skip to content

Commit

Permalink
stfdsafsd
Browse files Browse the repository at this point in the history
sadffdsa

stuff

finish commonjs stuff

asdf

not done but work

not done but work

not done yet but this is how far i am

remove files

lol

update built files

uncomment everything in events lol

export default

stuff
  • Loading branch information
paperdave committed Jul 30, 2023
1 parent 092ada6 commit 0ed39c7
Show file tree
Hide file tree
Showing 175 changed files with 3,985 additions and 15,248 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ cold-jsc-start.d

/test.ts

src/js/out/modules_dev
src/js/out/modules*
src/js/out/functions*
src/js/out/tmp

make-dev-stats.csv
3 changes: 2 additions & 1 deletion .vscode/launch.json

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

8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ BUN_AUTO_UPDATER_REPO = Jarred-Sumner/bun-releases-for-updater

CMAKE_CXX_COMPILER_LAUNCHER_FLAG :=



# 'make' command will trigger the help target
.DEFAULT_GOAL := help

Expand Down Expand Up @@ -561,8 +559,8 @@ builtins:
NODE_ENV=production bun src/js/builtins/codegen/index.ts --minify

.PHONY: esm
esm:
NODE_ENV=production bun src/js/build-esm.ts
js:
NODE_ENV=production bun src/js/_codegen/index.ts

esm-debug:
BUN_DEBUG_QUIET_LOGS=1 NODE_ENV=production bun-debug src/js/build-esm.ts
Expand Down Expand Up @@ -1118,7 +1116,7 @@ dev-obj-linux:
$(ZIG) build obj -Dtarget=x86_64-linux-gnu -Dcpu="$(CPU_TARGET)"

.PHONY: dev
dev: mkdir-dev esm dev-obj link ## compile zig changes + link bun
dev: mkdir-dev dev-obj link ## compile zig changes + link bun

mkdir-dev:
mkdir -p $(DEBUG_PACKAGE_DIR)
Expand Down
7 changes: 0 additions & 7 deletions src/bun.js/bindings/BunJSCModule.h

This file was deleted.

99 changes: 37 additions & 62 deletions src/bun.js/bindings/CommonJSModuleRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,62 +487,12 @@ bool JSCommonJSModule::evaluate(
JSC::MarkedArgumentBuffer arguments;
auto& vm = globalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
// TODO: remove second arg, we do not use it ???
// was it object loader?
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;
}

size_t count = propertyNames.size();
JSValue existingDefaultObject = this->getIfPropertyExists(globalObject, WebCore::clientData(vm)->builtinNames().exportsPublicName());
JSObject* defaultObject;

if (existingDefaultObject && existingDefaultObject.isObject()) {
defaultObject = jsCast<JSObject*>(existingDefaultObject);
} else {
defaultObject = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype());
needsPut = true;
}

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);
}

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 defaultValue = arguments.at(0);
this->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), defaultValue, 0);
this->hasEvaluated = true;
RELEASE_AND_RETURN(throwScope, true);
}
Expand All @@ -556,10 +506,6 @@ void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject,

auto& vm = globalObject->vm();

// 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
Expand Down Expand Up @@ -808,6 +754,33 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireCommonJS, (JSGlobalObject * lexicalGlo
RELEASE_AND_RETURN(throwScope, JSValue::encode(fetchResult));
}

// Used by $requireBuiltin(...) (Module.ts)
JSC_DEFINE_HOST_FUNCTION(jsFunctionCreateAndLoadBuiltinModule, (JSGlobalObject * lexicalGlobalObject, CallFrame* callframe))
{
auto* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject);
auto& vm = globalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);

JSValue specifierValue = callframe->argument(0);
WTF::String specifier = specifierValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});

BunString specifierStr = Bun::toString(specifier);
BunString referrerStr = Bun::toString(""_s);

JSValue fetchResult = Bun::fetchCommonJSModule(
globalObject,
JSCommonJSModule::create(
jsCast<Zig::GlobalObject*>(globalObject),
specifier,
constructEmptyObject(globalObject), false),
specifierValue,
&specifierStr,
&referrerStr);

RELEASE_AND_RETURN(throwScope, JSValue::encode(fetchResult));
}

void RequireResolveFunctionPrototype::finishCreation(JSC::VM& vm)
{
Base::finishCreation(vm);
Expand All @@ -820,10 +793,11 @@ void RequireResolveFunctionPrototype::finishCreation(JSC::VM& vm)
bool JSCommonJSModule::evaluate(
Zig::GlobalObject* globalObject,
const WTF::String& key,
ResolvedSource source)
ResolvedSource source,
bool isBuiltIn)
{
auto& vm = globalObject->vm();
auto sourceProvider = Zig::SourceProvider::create(jsCast<Zig::GlobalObject*>(globalObject), source, JSC::SourceProviderSourceType::Program);
auto sourceProvider = Zig::SourceProvider::create(jsCast<Zig::GlobalObject*>(globalObject), source, JSC::SourceProviderSourceType::Program, isBuiltIn);
this->ignoreESModuleAnnotation = source.tag == ResolvedSourceTagPackageJSONTypeModule;
JSC::SourceCode rawInputSource(
WTFMove(sourceProvider));
Expand Down Expand Up @@ -854,15 +828,16 @@ bool JSCommonJSModule::evaluate(

std::optional<JSC::SourceCode> createCommonJSModule(
Zig::GlobalObject* globalObject,
ResolvedSource source)
ResolvedSource source,
bool isBuiltIn)
{
JSCommonJSModule* moduleObject;
WTF::String sourceURL = toStringCopy(source.source_url);

JSValue specifierValue = Bun::toJS(globalObject, source.specifier);
JSValue entry = globalObject->requireMap()->get(globalObject, specifierValue);

auto sourceProvider = Zig::SourceProvider::create(jsCast<Zig::GlobalObject*>(globalObject), source, JSC::SourceProviderSourceType::Program);
auto sourceProvider = Zig::SourceProvider::create(jsCast<Zig::GlobalObject*>(globalObject), source, JSC::SourceProviderSourceType::Program, isBuiltIn);
bool ignoreESModuleAnnotation = source.tag == ResolvedSourceTagPackageJSONTypeModule;
SourceOrigin sourceOrigin = sourceProvider->sourceOrigin();

Expand Down
18 changes: 16 additions & 2 deletions src/bun.js/bindings/CommonJSModuleRecord.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include "root.h"
#include "headers-handwritten.h"

Expand All @@ -15,6 +16,7 @@ namespace Bun {

JSC_DECLARE_HOST_FUNCTION(jsFunctionCreateCommonJSModule);
JSC_DECLARE_HOST_FUNCTION(jsFunctionLoadModule);
JSC_DECLARE_HOST_FUNCTION(jsFunctionCreateAndLoadBuiltinModule);

class JSCommonJSModule final : public JSC::JSDestructibleObject {
public:
Expand All @@ -37,7 +39,11 @@ class JSCommonJSModule final : public JSC::JSDestructibleObject {

static JSC::Structure* createStructure(JSC::JSGlobalObject* globalObject);

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

Expand Down Expand Up @@ -96,7 +102,15 @@ JSC::Structure* createCommonJSModuleStructure(

std::optional<JSC::SourceCode> createCommonJSModule(
Zig::GlobalObject* globalObject,
ResolvedSource source);
ResolvedSource source,
bool isBuiltIn);

inline std::optional<JSC::SourceCode> createCommonJSModule(
Zig::GlobalObject* globalObject,
ResolvedSource source)
{
return createCommonJSModule(globalObject, source, false);
}

class RequireResolveFunctionPrototype final : public JSC::JSNonFinalObject {
public:
Expand Down
127 changes: 127 additions & 0 deletions src/bun.js/bindings/InternalModuleRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "InternalModuleRegistry.h"

#include "ZigGlobalObject.h"
#include "JavaScriptCore/BuiltinUtils.h"
#include "JavaScriptCore/JSFunction.h"
#include "JavaScriptCore/LazyProperty.h"
#include "JavaScriptCore/LazyPropertyInlines.h"
#include "JavaScriptCore/VMTrapsInlines.h"

#include "InternalModuleRegistryConstants.h"

namespace Bun {

#define INTERNAL_MODULE_REGISTRY_GENERATE_(init, SOURCE) \
SourceCode source = JSC::makeSource(SOURCE, {}); \
\
JSFunction* func \
= JSFunction::create( \
init.vm, \
createBuiltinExecutable( \
init.vm, source, \
Identifier(), \
ImplementationVisibility::Public, \
ConstructorKind::None, \
ConstructAbility::CannotConstruct) \
->link(init.vm, nullptr, source), \
static_cast<JSC::JSGlobalObject*>(init.owner)); \
\
JSC::MarkedArgumentBuffer argList; \
\
auto scope = DECLARE_CATCH_SCOPE(init.vm); \
\
JSValue result = JSC::call( \
init.owner, \
func, \
JSC::getCallData(func), \
init.owner, JSC::MarkedArgumentBuffer()); \
\
if (UNLIKELY(scope.exception())) { \
init.set(scope.exception()); \
} else { \
init.set(result.asCell()); \
}

#if BUN_DEBUG
void initializeInternalModuleFromDisk(
const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSCell>::Initializer& init,
WTF::String moduleId,
WTF::String file,
WTF::String source)
{
if (auto contents = WTF::FileSystemImpl::readEntireFile(file)) {
auto string = WTF::String::fromUTF8(contents.value());
INTERNAL_MODULE_REGISTRY_GENERATE_(init, string);
} else {
printf("bun-debug failed to load bundled version of \"%s\" (was it deleted?)\n" file.utf8().data(),
moduleId.utf8().data());
INTERNAL_MODULE_REGISTRY_GENERATE_(init, source);
}
}
#define INTERNAL_MODULE_REGISTRY_GENERATE(init, moduleId, filename, SOURCE) \
initializeInternalModuleFromDisk(init, moduleId, filename, SOURCE)
#else
#define INTERNAL_MODULE_REGISTRY_GENERATE(init, moduleId, filename, SOURCE) \
INTERNAL_MODULE_REGISTRY_GENERATE_(init, SOURCE)
#endif

const ClassInfo InternalModuleRegistry::s_info = { "InternalModuleRegistry"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(InternalModuleRegistry) };

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

template<typename Visitor>
void InternalModuleRegistry::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
auto* thisObject = jsCast<InternalModuleRegistry*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
}

DEFINE_VISIT_CHILDREN_WITH_MODIFIER(JS_EXPORT_PRIVATE, InternalFieldTuple);

InternalModuleRegistry* InternalModuleRegistry::create(VM& vm, Structure* structure)
{
InternalModuleRegistry* registry = new (NotNull, allocateCell<InternalModuleRegistry>(vm)) InternalModuleRegistry(vm, structure);
#include "../../../src/js/out/InternalModuleRegistry+create.h"
return registry;
}

JSCell* InternalModuleRegistry::get(JSGlobalObject* globalObject, ModuleID id)
{
return m_internalModule[id].get(globalObject);
}

JSCell* InternalModuleRegistry::get(JSGlobalObject* globalObject, unsigned id)
{
return m_internalModule[id].get(globalObject);
}

template<typename Visitor>
void InternalModuleRegistry::visitImpl(Visitor& visitor)
{
#include "../../../src/js/out/InternalModuleRegistry+visitImpl.h"
}

void InternalModuleRegistry::visit(AbstractSlotVisitor& visitor)
{
this->visitImpl(visitor);
}
void InternalModuleRegistry::visit(SlotVisitor& visitor)
{
this->visitImpl(visitor);
}

JSC_DEFINE_HOST_FUNCTION(InternalModuleRegistry::jsRequireId, (JSGlobalObject * lexicalGlobalObject, CallFrame* callframe))
{
auto id = callframe->argument(0).toUInt32(lexicalGlobalObject);
auto module = static_cast<Zig::GlobalObject*>(lexicalGlobalObject)->internalModuleRegistry.get(lexicalGlobalObject, id);
return JSValue::encode(module);
}

} // namespace Bun

#undef INTERNAL_MODULE_REGISTRY_GENERATE_
#undef INTERNAL_MODULE_REGISTRY_GENERATE
Loading

0 comments on commit 0ed39c7

Please sign in to comment.