#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 > Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2022-06-30Ci: improve times, reduce delay (#3780)Gravatar Fred K. Schott 1-14/+10
2022-06-30await error reporter (#3779)Gravatar Fred K. Schott 2-10/+25
2022-06-30[ci] formatGravatar natemoo-re 14-99/+95
2022-06-30MDX support (#3706)Gravatar Nate Moore 63-57/+1153
2022-06-30Fix integration name (`prefetch` instead of `lit`) (#3778)Gravatar hippotastic 2-1/+6
2022-06-30[ci] update lockfile (#3771)Gravatar Fred K. Bot 1-114/+112
2022-06-30Integration Docs Next Steps (#3677)Gravatar Dan Jutan 11-314/+666
2022-06-30[ci] formatGravatar tony-sull 1-2/+2
2022-06-30refactor to provide better cli error handling (#3768)Gravatar Fred K. Schott 2-43/+37
2022-06-30[ci] release (#3772)@astrojs/preact@0.3.1Gravatar Fred K. Bot 12-22/+23
2022-06-30Added Cloudflare adapter to README.md (#3773)Gravatar Isaac McFadyen 1-0/+1
2022-06-30[ci] formatGravatar hippotastic 1-5/+4
2022-06-30Fix "Invalid hook call" warning (#3769)Gravatar hippotastic 2-9/+79
2022-06-29[ci] release (#3759)astro@1.0.0-beta.59@astrojs/telemetry@0.2.2@astrojs/preact@0.3.0Gravatar Fred K. Bot 42-121/+117
2022-06-29[ci] formatGravatar FredKSchott 8-35/+36
2022-06-29manual lockfile update (#3751)Gravatar Fred K. Schott 3-2659/+2871
2022-06-29add error event to telemetry (#3750)Gravatar Fred K. Schott 16-85/+270