#include "root.h" #include "./NodeModuleModule.h" #include "ImportMetaObject.h" #include "JavaScriptCore/JSBoundFunction.h" #include "JavaScriptCore/ObjectConstructor.h" using namespace Zig; using namespace JSC; JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire, (JSC::JSGlobalObject * globalObject, JSC::CallFrame *callFrame)) { JSC::VM &vm = globalObject->vm(); auto scope = DECLARE_THROW_SCOPE(vm); if (callFrame->argumentCount() < 1) { throwTypeError(globalObject, scope, "createRequire() requires at least one argument"_s); return JSC::JSValue::encode(JSC::jsUndefined()); } auto str = callFrame->uncheckedArgument(0).toStringOrNull(globalObject); RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); WTF::String val = str->value(globalObject); auto *meta = Zig::ImportMetaObject::create(globalObject, str); auto clientData = WebCore::clientData(vm); auto requireFunction = Zig::ImportMetaObject::createRequireFunction(vm, globalObject, val); auto nameStr = jsCast(requireFunction)->name(vm); JSC::JSBoundFunction *boundRequireFunction = JSC::JSBoundFunction::create(vm, globalObject, requireFunction, meta, nullptr, 0, jsString(vm, nameStr)); boundRequireFunction->putDirect( vm, clientData->builtinNames().resolvePublicName(), requireFunction->getDirect( vm, clientData->builtinNames().resolvePublicName()), 0); RELEASE_AND_RETURN(scope, JSValue::encode(boundRequireFunction)); } JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePaths, (JSC::JSGlobalObject * globalObject, JSC::CallFrame *callFrame)) { return JSC::JSValue::encode(JSC::JSArray::create( globalObject->vm(), globalObject->arrayStructureForIndexingTypeDuringAllocation( ArrayWithContiguous), 0)); } JSC_DEFINE_HOST_FUNCTION(jsFunctionFindSourceMap, (JSGlobalObject * globalObject, CallFrame *callFrame)) { auto &vm = globalObject->vm(); auto scope = DECLARE_THROW_SCOPE(vm); throwException(globalObject, scope, createError(globalObject, "Not implemented"_s)); return JSValue::encode(jsUndefined()); } JSC_DEFINE_HOST_FUNCTION(jsFunctionSyncBuiltinExports, (JSGlobalObject * globalObject, CallFrame *callFrame)) { return JSValue::encode(jsUndefined()); } JSC_DEFINE_HOST_FUNCTION(jsFunctionSourceMap, (JSGlobalObject * globalObject, CallFrame *callFrame)) { auto &vm = globalObject->vm(); auto scope = DECLARE_THROW_SCOPE(vm); throwException(globalObject, scope, createError(globalObject, "Not implemented"_s)); return JSValue::encode(jsUndefined()); } JSC_DEFINE_HOST_FUNCTION(jsFunctionResolveFileName, (JSC::JSGlobalObject * globalObject, JSC::CallFrame *callFrame)) { JSC::VM &vm = globalObject->vm(); switch (callFrame->argumentCount()) { case 0: { auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); // not "requires" because "require" could be confusing JSC::throwTypeError( globalObject, scope, "Module._resolveFileName needs 2+ arguments (a string)"_s); scope.release(); return JSC::JSValue::encode(JSC::JSValue{}); } default: { JSC::JSValue moduleName = callFrame->argument(0); if (moduleName.isUndefinedOrNull()) { auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); JSC::throwTypeError(globalObject, scope, "Module._resolveFileName expects a string"_s); scope.release(); return JSC::JSValue::encode(JSC::JSValue{}); } auto result = Bun__resolveSync(globalObject, JSC::JSValue::encode(moduleName), JSValue::encode(callFrame->argument(1)), false); auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); if (!JSC::JSValue::decode(result).isString()) { JSC::throwException(globalObject, scope, JSC::JSValue::decode(result)); return JSC::JSValue::encode(JSC::JSValue{}); } scope.release(); return result; } } } namespace Zig { void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, JSC::Identifier moduleKey, Vector &exportNames, JSC::MarkedArgumentBuffer &exportValues) { JSC::VM &vm = globalObject->vm(); exportValues.append(JSFunction::create( vm, globalObject, 1, String("createRequire"_s), jsFunctionNodeModuleCreateRequire, ImplementationVisibility::Public)); exportValues.append(JSFunction::create(vm, globalObject, 1, String("paths"_s), jsFunctionNodeModulePaths, ImplementationVisibility::Public)); exportValues.append(JSFunction::create( vm, globalObject, 1, String("findSourceMap"_s), jsFunctionFindSourceMap, ImplementationVisibility::Public)); exportValues.append(JSFunction::create( vm, globalObject, 0, String("syncBuiltinExports"_s), jsFunctionSyncBuiltinExports, ImplementationVisibility::Public)); exportValues.append( JSFunction::create(vm, globalObject, 1, String("SourceMap"_s), jsFunctionSourceMap, ImplementationVisibility::Public, NoIntrinsic, jsFunctionSourceMap, nullptr)); exportNames.append(JSC::Identifier::fromString(vm, "createRequire"_s)); exportNames.append(JSC::Identifier::fromString(vm, "paths"_s)); exportNames.append(JSC::Identifier::fromString(vm, "findSourceMap"_s)); exportNames.append(JSC::Identifier::fromString(vm, "syncBuiltinExports"_s)); exportNames.append(JSC::Identifier::fromString(vm, "SourceMap"_s)); // note: this is not technically correct // it doesn't set process.mainModule exportNames.append(JSC::Identifier::fromString(vm, "_resolveFileName"_s)); exportValues.append(JSFunction::create( vm, globalObject, 3, String("_resolveFileName"_s), jsFunctionResolveFileName, ImplementationVisibility::Public)); exportNames.append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s)); exportValues.append(JSFunction::create( vm, globalObject, 0, String("_nodeModulePaths"_s), jsFunctionNodeModulePaths, ImplementationVisibility::Public)); exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s)); exportValues.append(JSC::constructEmptyObject(globalObject)); exportNames.append(JSC::Identifier::fromString(vm, "builtinModules"_s)); exportNames.append(JSC::Identifier::fromString(vm, "globalPaths"_s)); exportValues.append(JSC::constructEmptyArray(globalObject, 0)); JSC::JSArray *builtinModules = JSC::JSArray::create( vm, globalObject->arrayStructureForIndexingTypeDuringAllocation( ArrayWithContiguous), 7); builtinModules->putDirectIndex(globalObject, 0, JSC::jsString(vm, String("node:assert"_s))); builtinModules->putDirectIndex(globalObject, 1, JSC::jsString(vm, String("node:buffer"_s))); builtinModules->putDirectIndex(globalObject, 2, JSC::jsString(vm, String("node:events"_s))); builtinModules->putDirectIndex(globalObject, 3, JSC::jsString(vm, String("node:util"_s))); builtinModules->putDirectIndex(globalObject, 4, JSC::jsString(vm, String("node:path"_s))); builtinModules->putDirectIndex(globalObject, 5, JSC::jsString(vm, String("bun:ffi"_s))); builtinModules->putDirectIndex(globalObject, 6, JSC::jsString(vm, String("bun:sqlite"_s))); exportValues.append(builtinModules); } } // namespace Zig esting'>jarred/improve-testing Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2023-10-09fix(AbortSignal/fetch) fix AbortSignal.timeout, fetch lock behavior and fetch...Gravatar Ciro Spaciari 29-61/+303
2023-10-09Fix npm tag for canary bun-types, againGravatar Ashcon Partovi 2-56/+10
2023-10-09Add Fedora build instructions to development.md (#6359)Gravatar otterDeveloper 1-0/+10
2023-10-09added commands (#6314)Gravatar babar 1-1/+2
2023-10-09Update README.md (#6291)Gravatar TPLJ 1-1/+1
2023-10-09docs: fixing a couple typos (#6331)Gravatar Michael Di Prisco 2-2/+2
2023-10-09fix: support uint8 exit code range (#6303)Gravatar Liz 2-2/+11
2023-10-09Fix array variables preview in debugger (#6379)Gravatar 2hu 1-1/+4
2023-10-07feat(KeyObject) (#5940)Gravatar Ciro Spaciari 106-67/+9342
2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+1
2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+2
2023-10-07Update settings.jsonGravatar Jarred Sumner 1-1/+2
2023-10-07Update settings.jsonGravatar Jarred Sumner 1-2/+3
2023-10-06fix a couple install testsGravatar Dylan Conway 1-8/+8
2023-10-06formatGravatar Dylan Conway 1-1/+2
2023-10-06Fix memory leak in fetch() (#6350)Gravatar Jarred Sumner 1-2/+0
2023-10-06[types] allow onLoad plugin callbacks to return undefined (#6346)Gravatar Silver 1-1/+1
2023-10-06docs: `file.stream()` is not a promise (#6337)Gravatar Paul Nodet 1-1/+1