diff options
Diffstat (limited to 'src/bun.js/modules')
-rw-r--r-- | src/bun.js/modules/BunJSCModule.h | 731 | ||||
-rw-r--r-- | src/bun.js/modules/ConstantsModule.h | 262 | ||||
-rw-r--r-- | src/bun.js/modules/EventsModule.h | 58 | ||||
-rw-r--r-- | src/bun.js/modules/NodeBufferModule.h (renamed from src/bun.js/modules/BufferModule.h) | 92 | ||||
-rw-r--r-- | src/bun.js/modules/NodeConstantsModule.h | 916 | ||||
-rw-r--r-- | src/bun.js/modules/NodeModuleModule.cpp | 297 | ||||
-rw-r--r-- | src/bun.js/modules/NodeModuleModule.h | 324 | ||||
-rw-r--r-- | src/bun.js/modules/NodeProcessModule.h (renamed from src/bun.js/modules/ProcessModule.h) | 10 | ||||
-rw-r--r-- | src/bun.js/modules/NodeStringDecoderModule.h | 16 | ||||
-rw-r--r-- | src/bun.js/modules/NodeTTYModule.h | 50 | ||||
-rw-r--r-- | src/bun.js/modules/NodeUtilTypesModule.h | 364 | ||||
-rw-r--r-- | src/bun.js/modules/StringDecoderModule.h | 36 | ||||
-rw-r--r-- | src/bun.js/modules/TTYModule.h | 81 | ||||
-rw-r--r-- | src/bun.js/modules/_NativeModule.h | 90 |
14 files changed, 2516 insertions, 811 deletions
diff --git a/src/bun.js/modules/BunJSCModule.h b/src/bun.js/modules/BunJSCModule.h new file mode 100644 index 000000000..c5350fcd7 --- /dev/null +++ b/src/bun.js/modules/BunJSCModule.h @@ -0,0 +1,731 @@ +#include "_NativeModule.h" + +#include "ExceptionOr.h" +#include "JavaScriptCore/APICast.h" +#include "JavaScriptCore/AggregateError.h" +#include "JavaScriptCore/BytecodeIndex.h" +#include "JavaScriptCore/CallFrameInlines.h" +#include "JavaScriptCore/ClassInfo.h" +#include "JavaScriptCore/CodeBlock.h" +#include "JavaScriptCore/Completion.h" +#include "JavaScriptCore/DeferTermination.h" +#include "JavaScriptCore/Error.h" +#include "JavaScriptCore/ErrorInstance.h" +#include "JavaScriptCore/HeapSnapshotBuilder.h" +#include "JavaScriptCore/JIT.h" +#include "JavaScriptCore/JSBasePrivate.h" +#include "JavaScriptCore/JSCInlines.h" +#include "JavaScriptCore/JSONObject.h" +#include "JavaScriptCore/JavaScript.h" +#include "JavaScriptCore/ObjectConstructor.h" +#include "JavaScriptCore/SamplingProfiler.h" +#include "JavaScriptCore/TestRunnerUtils.h" +#include "JavaScriptCore/VMTrapsInlines.h" +#include "MessagePort.h" +#include "SerializedScriptValue.h" +#include "wtf/FileSystem.h" +#include "wtf/MemoryFootprint.h" +#include "wtf/text/WTFString.h" + +#include "Process.h" + +#if ENABLE(REMOTE_INSPECTOR) +#include "JavaScriptCore/RemoteInspectorServer.h" +#endif + +#include "JSDOMConvertBase.h" +#include "mimalloc.h" + +using namespace JSC; +using namespace WTF; +using namespace WebCore; + +JSC_DECLARE_HOST_FUNCTION(functionStartRemoteDebugger); +JSC_DEFINE_HOST_FUNCTION(functionStartRemoteDebugger, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { +#if ENABLE(REMOTE_INSPECTOR) + static const char *defaultHost = "127.0.0.1\0"; + static uint16_t defaultPort = 9230; // node + 1 + auto &vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + JSC::JSValue hostValue = callFrame->argument(0); + JSC::JSValue portValue = callFrame->argument(1); + const char *host = defaultHost; + if (hostValue.isString()) { + + auto str = hostValue.toWTFString(globalObject); + if (!str.isEmpty()) + host = toCString(str).data(); + } else if (!hostValue.isUndefined()) { + throwVMError(globalObject, scope, + createTypeError(globalObject, "host must be a string"_s)); + return JSC::JSValue::encode(JSC::jsUndefined()); + } + + uint16_t port = defaultPort; + if (portValue.isNumber()) { + auto port_int = portValue.toUInt32(globalObject); + if (!(port_int > 0 && port_int < 65536)) { + throwVMError( + globalObject, scope, + createRangeError(globalObject, "port must be between 0 and 65535"_s)); + return JSC::JSValue::encode(JSC::jsUndefined()); + } + port = port_int; + } else if (!portValue.isUndefined()) { + throwVMError( + globalObject, scope, + createTypeError(globalObject, + "port must be a number between 0 and 65535"_s)); + return JSC::JSValue::encode(JSC::jsUndefined()); + } + + globalObject->setInspectable(true); + auto &server = Inspector::RemoteInspectorServer::singleton(); + if (!server.start(reinterpret_cast<const char *>(host), port)) { + throwVMError( + globalObject, scope, + createError(globalObject, "Failed to start server \""_s + host + ":"_s + + port + "\". Is port already in use?"_s)); + return JSC::JSValue::encode(JSC::jsUndefined()); + } + + RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsUndefined())); +#else + auto &vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + throwVMError(globalObject, scope, + createTypeError( + globalObject, + "Remote inspector is not enabled in this build of Bun"_s)); + return JSC::JSValue::encode(JSC::jsUndefined()); +#endif +} + +JSC_DECLARE_HOST_FUNCTION(functionDescribe); +JSC_DEFINE_HOST_FUNCTION(functionDescribe, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + if (callFrame->argumentCount() < 1) + return JSValue::encode(jsUndefined()); + return JSValue::encode(jsString(vm, toString(callFrame->argument(0)))); +} + +JSC_DECLARE_HOST_FUNCTION(functionDescribeArray); +JSC_DEFINE_HOST_FUNCTION(functionDescribeArray, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + if (callFrame->argumentCount() < 1) + return JSValue::encode(jsUndefined()); + VM &vm = globalObject->vm(); + JSObject *object = jsDynamicCast<JSObject *>(callFrame->argument(0)); + if (!object) + return JSValue::encode(jsNontrivialString(vm, "<not object>"_s)); + return JSValue::encode(jsNontrivialString( + vm, toString("<Butterfly: ", RawPointer(object->butterfly()), + "; public length: ", object->getArrayLength(), + "; vector length: ", object->getVectorLength(), ">"))); +} + +JSC_DECLARE_HOST_FUNCTION(functionGCAndSweep); +JSC_DEFINE_HOST_FUNCTION(functionGCAndSweep, + (JSGlobalObject * globalObject, CallFrame *)) { + VM &vm = globalObject->vm(); + JSLockHolder lock(vm); + vm.heap.collectNow(Sync, CollectionScope::Full); + return JSValue::encode(jsNumber(vm.heap.sizeAfterLastFullCollection())); +} + +JSC_DECLARE_HOST_FUNCTION(functionFullGC); +JSC_DEFINE_HOST_FUNCTION(functionFullGC, + (JSGlobalObject * globalObject, CallFrame *)) { + VM &vm = globalObject->vm(); + JSLockHolder lock(vm); + vm.heap.collectSync(CollectionScope::Full); + return JSValue::encode(jsNumber(vm.heap.sizeAfterLastFullCollection())); +} + +JSC_DECLARE_HOST_FUNCTION(functionEdenGC); +JSC_DEFINE_HOST_FUNCTION(functionEdenGC, + (JSGlobalObject * globalObject, CallFrame *)) { + VM &vm = globalObject->vm(); + JSLockHolder lock(vm); + vm.heap.collectSync(CollectionScope::Eden); + return JSValue::encode(jsNumber(vm.heap.sizeAfterLastEdenCollection())); +} + +JSC_DECLARE_HOST_FUNCTION(functionHeapSize); +JSC_DEFINE_HOST_FUNCTION(functionHeapSize, + (JSGlobalObject * globalObject, CallFrame *)) { + VM &vm = globalObject->vm(); + JSLockHolder lock(vm); + return JSValue::encode(jsNumber(vm.heap.size())); +} + +JSC::Structure * +createMemoryFootprintStructure(JSC::VM &vm, JSC::JSGlobalObject *globalObject) { + + JSC::Structure *structure = + globalObject->structureCache().emptyObjectStructureForPrototype( + globalObject, globalObject->objectPrototype(), 5); + JSC::PropertyOffset offset; + + structure = structure->addPropertyTransition( + vm, structure, Identifier::fromString(vm, "current"_s), 0, offset); + structure = structure->addPropertyTransition( + vm, structure, Identifier::fromString(vm, "peak"_s), 0, offset); + structure = structure->addPropertyTransition( + vm, structure, Identifier::fromString(vm, "currentCommit"_s), 0, offset); + structure = structure->addPropertyTransition( + vm, structure, Identifier::fromString(vm, "peakCommit"_s), 0, offset); + structure = structure->addPropertyTransition( + vm, structure, Identifier::fromString(vm, "pageFaults"_s), 0, offset); + + return structure; +} + +JSC_DECLARE_HOST_FUNCTION(functionMemoryUsageStatistics); +JSC_DEFINE_HOST_FUNCTION(functionMemoryUsageStatistics, + (JSGlobalObject * globalObject, CallFrame *)) { + + auto &vm = globalObject->vm(); + JSC::DisallowGC disallowGC; + + // this is a C API function + auto *stats = toJS(JSGetMemoryUsageStatistics(toRef(globalObject))); + + if (JSValue heapSizeValue = + stats->getDirect(vm, Identifier::fromString(vm, "heapSize"_s))) { + ASSERT(heapSizeValue.isNumber()); + if (heapSizeValue.toInt32(globalObject) == 0) { + vm.heap.collectNow(Sync, CollectionScope::Full); + stats = toJS(JSGetMemoryUsageStatistics(toRef(globalObject))); + } + } + + // This is missing from the C API + JSC::JSObject *protectedCounts = constructEmptyObject(globalObject); + auto typeCounts = *vm.heap.protectedObjectTypeCounts(); + for (auto &it : typeCounts) + protectedCounts->putDirect(vm, Identifier::fromLatin1(vm, it.key), + jsNumber(it.value)); + + stats->putDirect(vm, + Identifier::fromLatin1(vm, "protectedObjectTypeCounts"_s), + protectedCounts); + return JSValue::encode(stats); +} + +JSC_DECLARE_HOST_FUNCTION(functionCreateMemoryFootprint); +JSC_DEFINE_HOST_FUNCTION(functionCreateMemoryFootprint, + (JSGlobalObject * globalObject, CallFrame *)) { + + size_t elapsed_msecs = 0; + size_t user_msecs = 0; + size_t system_msecs = 0; + size_t current_rss = 0; + size_t peak_rss = 0; + size_t current_commit = 0; + size_t peak_commit = 0; + size_t page_faults = 0; + + mi_process_info(&elapsed_msecs, &user_msecs, &system_msecs, ¤t_rss, + &peak_rss, ¤t_commit, &peak_commit, &page_faults); + + // mi_process_info produces incorrect rss size on linux. + Zig::getRSS(¤t_rss); + + VM &vm = globalObject->vm(); + JSC::JSObject *object = JSC::constructEmptyObject( + vm, JSC::jsCast<Zig::GlobalObject *>(globalObject) + ->memoryFootprintStructure()); + + object->putDirectOffset(vm, 0, jsNumber(current_rss)); + object->putDirectOffset(vm, 1, jsNumber(peak_rss)); + object->putDirectOffset(vm, 2, jsNumber(current_commit)); + object->putDirectOffset(vm, 3, jsNumber(peak_commit)); + object->putDirectOffset(vm, 4, jsNumber(page_faults)); + + return JSValue::encode(object); +} + +JSC_DECLARE_HOST_FUNCTION(functionNeverInlineFunction); +JSC_DEFINE_HOST_FUNCTION(functionNeverInlineFunction, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + return JSValue::encode(setNeverInline(globalObject, callFrame)); +} + +extern "C" bool Bun__mkdirp(JSC::JSGlobalObject *, const char *); + +JSC_DECLARE_HOST_FUNCTION(functionStartSamplingProfiler); +JSC_DEFINE_HOST_FUNCTION(functionStartSamplingProfiler, + (JSC::JSGlobalObject * globalObject, + JSC::CallFrame *callFrame)) { + JSC::VM &vm = globalObject->vm(); + JSC::SamplingProfiler &samplingProfiler = + vm.ensureSamplingProfiler(WTF::Stopwatch::create()); + + JSC::JSValue directoryValue = callFrame->argument(0); + JSC::JSValue sampleValue = callFrame->argument(1); + + auto scope = DECLARE_THROW_SCOPE(vm); + if (directoryValue.isString()) { + auto path = directoryValue.toWTFString(globalObject); + if (!path.isEmpty()) { + StringPrintStream pathOut; + auto pathCString = toCString(String(path)); + if (!Bun__mkdirp(globalObject, pathCString.data())) { + throwVMError( + globalObject, scope, + createTypeError(globalObject, "directory couldn't be created"_s)); + return JSC::JSValue::encode(jsUndefined()); + } + + Options::samplingProfilerPath() = pathCString.data(); + samplingProfiler.registerForReportAtExit(); + } + } + if (sampleValue.isNumber()) { + unsigned sampleInterval = sampleValue.toUInt32(globalObject); + samplingProfiler.setTimingInterval( + Seconds::fromMicroseconds(sampleInterval)); + } + + samplingProfiler.noticeCurrentThreadAsJSCExecutionThread(); + samplingProfiler.start(); + return JSC::JSValue::encode(jsUndefined()); +} + +JSC_DECLARE_HOST_FUNCTION(functionSamplingProfilerStackTraces); +JSC_DEFINE_HOST_FUNCTION(functionSamplingProfilerStackTraces, + (JSC::JSGlobalObject * globalObject, + JSC::CallFrame *)) { + JSC::VM &vm = globalObject->vm(); + JSC::DeferTermination deferScope(vm); + auto scope = DECLARE_THROW_SCOPE(vm); + + if (!vm.samplingProfiler()) + return JSC::JSValue::encode(throwException( + globalObject, scope, + createError(globalObject, "Sampling profiler was never started"_s))); + + WTF::String jsonString = vm.samplingProfiler()->stackTracesAsJSON(); + JSC::EncodedJSValue result = + JSC::JSValue::encode(JSONParse(globalObject, jsonString)); + scope.releaseAssertNoException(); + return result; +} + +JSC_DECLARE_HOST_FUNCTION(functionGetRandomSeed); +JSC_DEFINE_HOST_FUNCTION(functionGetRandomSeed, + (JSGlobalObject * globalObject, CallFrame *)) { + return JSValue::encode(jsNumber(globalObject->weakRandom().seed())); +} + +JSC_DECLARE_HOST_FUNCTION(functionSetRandomSeed); +JSC_DEFINE_HOST_FUNCTION(functionSetRandomSeed, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + unsigned seed = callFrame->argument(0).toUInt32(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + globalObject->weakRandom().setSeed(seed); + return JSValue::encode(jsUndefined()); +} + +JSC_DECLARE_HOST_FUNCTION(functionIsRope); +JSC_DEFINE_HOST_FUNCTION(functionIsRope, + (JSGlobalObject *, CallFrame *callFrame)) { + JSValue argument = callFrame->argument(0); + if (!argument.isString()) + return JSValue::encode(jsBoolean(false)); + const StringImpl *impl = asString(argument)->tryGetValueImpl(); + return JSValue::encode(jsBoolean(!impl)); +} + +JSC_DECLARE_HOST_FUNCTION(functionCallerSourceOrigin); +JSC_DEFINE_HOST_FUNCTION(functionCallerSourceOrigin, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + SourceOrigin sourceOrigin = callFrame->callerSourceOrigin(vm); + if (sourceOrigin.url().isNull()) + return JSValue::encode(jsNull()); + return JSValue::encode(jsString(vm, sourceOrigin.string())); +} + +JSC_DECLARE_HOST_FUNCTION(functionNoFTL); +JSC_DEFINE_HOST_FUNCTION(functionNoFTL, + (JSGlobalObject *, CallFrame *callFrame)) { + if (callFrame->argumentCount()) { + FunctionExecutable *executable = + getExecutableForFunction(callFrame->argument(0)); + if (executable) + executable->setNeverFTLOptimize(true); + } + return JSValue::encode(jsUndefined()); +} + +JSC_DECLARE_HOST_FUNCTION(functionNoOSRExitFuzzing); +JSC_DEFINE_HOST_FUNCTION(functionNoOSRExitFuzzing, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + return JSValue::encode(setCannotUseOSRExitFuzzing(globalObject, callFrame)); +} + +JSC_DECLARE_HOST_FUNCTION(functionOptimizeNextInvocation); +JSC_DEFINE_HOST_FUNCTION(functionOptimizeNextInvocation, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + return JSValue::encode(optimizeNextInvocation(globalObject, callFrame)); +} + +JSC_DECLARE_HOST_FUNCTION(functionNumberOfDFGCompiles); +JSC_DEFINE_HOST_FUNCTION(functionNumberOfDFGCompiles, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + return JSValue::encode(numberOfDFGCompiles(globalObject, callFrame)); +} + +JSC_DECLARE_HOST_FUNCTION(functionReleaseWeakRefs); +JSC_DEFINE_HOST_FUNCTION(functionReleaseWeakRefs, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + globalObject->vm().finalizeSynchronousJSExecution(); + return JSValue::encode(jsUndefined()); +} + +JSC_DECLARE_HOST_FUNCTION(functionTotalCompileTime); +JSC_DEFINE_HOST_FUNCTION(functionTotalCompileTime, + (JSGlobalObject *, CallFrame *)) { + return JSValue::encode(jsNumber(JIT::totalCompileTime().milliseconds())); +} + +JSC_DECLARE_HOST_FUNCTION(functionGetProtectedObjects); +JSC_DEFINE_HOST_FUNCTION(functionGetProtectedObjects, + (JSGlobalObject * globalObject, CallFrame *)) { + MarkedArgumentBuffer list; + size_t result = 0; + globalObject->vm().heap.forEachProtectedCell( + [&](JSCell *cell) { list.append(cell); }); + RELEASE_ASSERT(!list.hasOverflowed()); + return JSC::JSValue::encode(constructArray( + globalObject, static_cast<JSC::ArrayAllocationProfile *>(nullptr), list)); +} + +JSC_DECLARE_HOST_FUNCTION(functionReoptimizationRetryCount); +JSC_DEFINE_HOST_FUNCTION(functionReoptimizationRetryCount, + (JSGlobalObject *, CallFrame *callFrame)) { + if (callFrame->argumentCount() < 1) + return JSValue::encode(jsUndefined()); + + CodeBlock *block = + getSomeBaselineCodeBlockForFunction(callFrame->argument(0)); + if (!block) + return JSValue::encode(jsNumber(0)); + + return JSValue::encode(jsNumber(block->reoptimizationRetryCounter())); +} + +extern "C" void Bun__drainMicrotasks(); + +JSC_DECLARE_HOST_FUNCTION(functionDrainMicrotasks); +JSC_DEFINE_HOST_FUNCTION(functionDrainMicrotasks, + (JSGlobalObject * globalObject, CallFrame *)) { + VM &vm = globalObject->vm(); + vm.drainMicrotasks(); + Bun__drainMicrotasks(); + return JSValue::encode(jsUndefined()); +} + +JSC_DEFINE_HOST_FUNCTION(functionSetTimeZone, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + if (callFrame->argumentCount() < 1) { + throwTypeError(globalObject, scope, + "setTimeZone requires a timezone string"_s); + return encodedJSValue(); + } + + if (!callFrame->argument(0).isString()) { + throwTypeError(globalObject, scope, + "setTimeZone requires a timezone string"_s); + return encodedJSValue(); + } + + String timeZoneName = callFrame->argument(0).toWTFString(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + double time = callFrame->argument(1).toNumber(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + if (!WTF::setTimeZoneOverride(timeZoneName)) { + throwTypeError(globalObject, scope, + makeString("Invalid timezone: \""_s, timeZoneName, "\""_s)); + return encodedJSValue(); + } + vm.dateCache.resetIfNecessarySlow(); + WTF::Vector<UChar, 32> buffer; + WTF::getTimeZoneOverride(buffer); + WTF::String timeZoneString(buffer.data(), buffer.size()); + return JSValue::encode(jsString(vm, timeZoneString)); +} + +JSC_DEFINE_HOST_FUNCTION(functionRunProfiler, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + JSC::VM &vm = globalObject->vm(); + JSC::SamplingProfiler &samplingProfiler = + vm.ensureSamplingProfiler(WTF::Stopwatch::create()); + + JSC::JSValue callbackValue = callFrame->argument(0); + auto throwScope = DECLARE_THROW_SCOPE(vm); + if (callbackValue.isUndefinedOrNull() || !callbackValue.isCallable()) { + throwException( + globalObject, throwScope, + createTypeError(globalObject, "First argument must be a function."_s)); + return JSValue::encode(JSValue{}); + } + + JSC::JSFunction *function = jsCast<JSC::JSFunction *>(callbackValue); + + JSC::JSValue sampleValue = callFrame->argument(1); + if (sampleValue.isNumber()) { + unsigned sampleInterval = sampleValue.toUInt32(globalObject); + samplingProfiler.setTimingInterval( + Seconds::fromMicroseconds(sampleInterval)); + } + + JSC::CallData callData = JSC::getCallData(function); + MarkedArgumentBuffer args; + + samplingProfiler.noticeCurrentThreadAsJSCExecutionThread(); + samplingProfiler.start(); + JSC::call(globalObject, function, callData, JSC::jsUndefined(), args); + samplingProfiler.pause(); + if (throwScope.exception()) { + samplingProfiler.shutdown(); + samplingProfiler.clearData(); + return JSValue::encode(JSValue{}); + } + + StringPrintStream topFunctions; + samplingProfiler.reportTopFunctions(topFunctions); + + StringPrintStream byteCodes; + samplingProfiler.reportTopBytecodes(byteCodes); + + JSValue stackTraces = + JSONParse(globalObject, samplingProfiler.stackTracesAsJSON()); + + samplingProfiler.shutdown(); + samplingProfiler.clearData(); + + JSObject *result = + constructEmptyObject(globalObject, globalObject->objectPrototype(), 3); + result->putDirect(vm, Identifier::fromString(vm, "functions"_s), + jsString(vm, topFunctions.toString())); + result->putDirect(vm, Identifier::fromString(vm, "bytecodes"_s), + jsString(vm, byteCodes.toString())); + result->putDirect(vm, Identifier::fromString(vm, "stackTraces"_s), + stackTraces); + + return JSValue::encode(result); +} + +JSC_DECLARE_HOST_FUNCTION(functionGenerateHeapSnapshotForDebugging); +JSC_DEFINE_HOST_FUNCTION(functionGenerateHeapSnapshotForDebugging, + (JSGlobalObject * globalObject, CallFrame *)) { + VM &vm = globalObject->vm(); + JSLockHolder lock(vm); + DeferTermination deferScope(vm); + auto scope = DECLARE_THROW_SCOPE(vm); + String jsonString; + { + DeferGCForAWhile deferGC(vm); // Prevent concurrent GC from interfering with + // the full GC that the snapshot does. + + HeapSnapshotBuilder snapshotBuilder( + vm.ensureHeapProfiler(), + HeapSnapshotBuilder::SnapshotType::GCDebuggingSnapshot); + snapshotBuilder.buildSnapshot(); + + jsonString = snapshotBuilder.json(); + } + scope.releaseAssertNoException(); + + return JSValue::encode(JSONParse(globalObject, WTFMove(jsonString))); +} + +JSC_DEFINE_HOST_FUNCTION(functionSerialize, + (JSGlobalObject * lexicalGlobalObject, + CallFrame *callFrame)) { + auto *globalObject = jsCast<JSDOMGlobalObject *>(lexicalGlobalObject); + JSC::VM &vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + + JSValue value = callFrame->argument(0); + JSValue optionsObject = callFrame->argument(1); + bool asNodeBuffer = false; + if (optionsObject.isObject()) { + JSC::JSObject *options = optionsObject.getObject(); + if (JSC::JSValue binaryTypeValue = options->getIfPropertyExists( + globalObject, JSC::Identifier::fromString(vm, "binaryType"_s))) { + if (!binaryTypeValue.isString()) { + throwTypeError(globalObject, throwScope, + "binaryType must be a string"_s); + return JSValue::encode(jsUndefined()); + } + + asNodeBuffer = + binaryTypeValue.toWTFString(globalObject) == "nodebuffer"_s; + RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); + } + } + + Vector<JSC::Strong<JSC::JSObject>> transferList; + Vector<RefPtr<MessagePort>> dummyPorts; + ExceptionOr<Ref<SerializedScriptValue>> serialized = + SerializedScriptValue::create(*globalObject, value, WTFMove(transferList), + dummyPorts); + + if (serialized.hasException()) { + WebCore::propagateException(*globalObject, throwScope, + serialized.releaseException()); + return JSValue::encode(jsUndefined()); + } + + auto serializedValue = serialized.releaseReturnValue(); + auto arrayBuffer = serializedValue->toArrayBuffer(); + + if (asNodeBuffer) { + size_t byteLength = arrayBuffer->byteLength(); + JSC::JSUint8Array *uint8Array = JSC::JSUint8Array::create( + lexicalGlobalObject, globalObject->JSBufferSubclassStructure(), + WTFMove(arrayBuffer), 0, byteLength); + return JSValue::encode(uint8Array); + } + + if (arrayBuffer->isShared()) { + return JSValue::encode( + JSArrayBuffer::create(vm, + globalObject->arrayBufferStructureWithSharingMode< + ArrayBufferSharingMode::Shared>(), + WTFMove(arrayBuffer))); + } + + return JSValue::encode(JSArrayBuffer::create( + vm, globalObject->arrayBufferStructure(), WTFMove(arrayBuffer))); +} +JSC_DEFINE_HOST_FUNCTION(functionDeserialize, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + JSC::VM &vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + JSValue value = callFrame->argument(0); + + JSValue result; + + if (auto *jsArrayBuffer = jsDynamicCast<JSArrayBuffer *>(value)) { + result = SerializedScriptValue::fromArrayBuffer( + *globalObject, globalObject, jsArrayBuffer->impl(), 0, + jsArrayBuffer->impl()->byteLength()); + } else if (auto *view = jsDynamicCast<JSArrayBufferView *>(value)) { + auto arrayBuffer = view->possiblySharedImpl()->possiblySharedBuffer(); + result = SerializedScriptValue::fromArrayBuffer( + *globalObject, globalObject, arrayBuffer.get(), view->byteOffset(), + view->byteLength()); + } else { + throwTypeError(globalObject, throwScope, + "First argument must be an ArrayBuffer"_s); + return JSValue::encode(jsUndefined()); + } + + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + RELEASE_AND_RETURN(throwScope, JSValue::encode(result)); +} + +// clang-format off +/* Source for BunJSCModuleTable.lut.h +@begin BunJSCModuleTable + callerSourceOrigin functionCallerSourceOrigin Function 0 + jscDescribe functionDescribe Function 0 + jscDescribeArray functionDescribeArray Function 0 + drainMicrotasks functionDrainMicrotasks Function 0 + edenGC functionEdenGC Function 0 + fullGC functionFullGC Function 0 + gcAndSweep functionGCAndSweep Function 0 + getRandomSeed functionGetRandomSeed Function 0 + heapSize functionHeapSize Function 0 + heapStats functionMemoryUsageStatistics Function 0 + startSamplingProfiler functionStartSamplingProfiler Function 0 + samplingProfilerStackTraces functionSamplingProfilerStackTraces Function 0 + noInline functionNeverInlineFunction Function 0 + isRope functionIsRope Function 0 + memoryUsage functionCreateMemoryFootprint Function 0 + noFTL functionNoFTL Function 0 + noOSRExitFuzzing functionNoOSRExitFuzzing Function 0 + numberOfDFGCompiles functionNumberOfDFGCompiles Function 0 + optimizeNextInvocation functionOptimizeNextInvocation Function 0 + releaseWeakRefs functionReleaseWeakRefs Function 0 + reoptimizationRetryCount functionReoptimizationRetryCount Function 0 + setRandomSeed functionSetRandomSeed Function 0 + startRemoteDebugger functionStartRemoteDebugger Function 0 + totalCompileTime functionTotalCompileTime Function 0 + getProtectedObjects functionGetProtectedObjects Function 0 + generateHeapSnapshotForDebugging functionGenerateHeapSnapshotForDebugging Function 0 + profile functionRunProfiler Function 0 + setTimeZone functionSetTimeZone Function 0 + serialize functionSerialize Function 0 + deserialize functionDeserialize Function 0 +@end +*/ + +namespace Zig { +DEFINE_NATIVE_MODULE(BunJSC) +{ + INIT_NATIVE_MODULE(33); + + putNativeFn(Identifier::fromString(vm, "callerSourceOrigin"_s), functionCallerSourceOrigin); + putNativeFn(Identifier::fromString(vm, "jscDescribe"_s), functionDescribe); + putNativeFn(Identifier::fromString(vm, "jscDescribeArray"_s), functionDescribeArray); + putNativeFn(Identifier::fromString(vm, "drainMicrotasks"_s), functionDrainMicrotasks); + putNativeFn(Identifier::fromString(vm, "edenGC"_s), functionEdenGC); + putNativeFn(Identifier::fromString(vm, "fullGC"_s), functionFullGC); + putNativeFn(Identifier::fromString(vm, "gcAndSweep"_s), functionGCAndSweep); + putNativeFn(Identifier::fromString(vm, "getRandomSeed"_s), functionGetRandomSeed); + putNativeFn(Identifier::fromString(vm, "heapSize"_s), functionHeapSize); + putNativeFn(Identifier::fromString(vm, "heapStats"_s), functionMemoryUsageStatistics); + putNativeFn(Identifier::fromString(vm, "startSamplingProfiler"_s), functionStartSamplingProfiler); + putNativeFn(Identifier::fromString(vm, "samplingProfilerStackTraces"_s), functionSamplingProfilerStackTraces); + putNativeFn(Identifier::fromString(vm, "noInline"_s), functionNeverInlineFunction); + putNativeFn(Identifier::fromString(vm, "isRope"_s), functionIsRope); + putNativeFn(Identifier::fromString(vm, "memoryUsage"_s), functionCreateMemoryFootprint); + putNativeFn(Identifier::fromString(vm, "noFTL"_s), functionNoFTL); + putNativeFn(Identifier::fromString(vm, "noOSRExitFuzzing"_s), functionNoOSRExitFuzzing); + putNativeFn(Identifier::fromString(vm, "numberOfDFGCompiles"_s), functionNumberOfDFGCompiles); + putNativeFn(Identifier::fromString(vm, "optimizeNextInvocation"_s), functionOptimizeNextInvocation); + putNativeFn(Identifier::fromString(vm, "releaseWeakRefs"_s), functionReleaseWeakRefs); + putNativeFn(Identifier::fromString(vm, "reoptimizationRetryCount"_s), functionReoptimizationRetryCount); + putNativeFn(Identifier::fromString(vm, "setRandomSeed"_s), functionSetRandomSeed); + putNativeFn(Identifier::fromString(vm, "startRemoteDebugger"_s), functionStartRemoteDebugger); + putNativeFn(Identifier::fromString(vm, "totalCompileTime"_s), functionTotalCompileTime); + putNativeFn(Identifier::fromString(vm, "getProtectedObjects"_s), functionGetProtectedObjects); + putNativeFn(Identifier::fromString(vm, "generateHeapSnapshotForDebugging"_s), functionGenerateHeapSnapshotForDebugging); + putNativeFn(Identifier::fromString(vm, "profile"_s), functionRunProfiler); + putNativeFn(Identifier::fromString(vm, "setTimeZone"_s), functionSetTimeZone); + putNativeFn(Identifier::fromString(vm, "serialize"_s), functionSerialize); + putNativeFn(Identifier::fromString(vm, "deserialize"_s), functionDeserialize); + + // Deprecated + putNativeFn(Identifier::fromString(vm, "describe"_s), functionDescribe); + putNativeFn(Identifier::fromString(vm, "describeArray"_s), functionDescribeArray); + putNativeFn(Identifier::fromString(vm, "setTimezone"_s), functionSetTimeZone); + + RETURN_NATIVE_MODULE(); +} + +} // namespace Zig diff --git a/src/bun.js/modules/ConstantsModule.h b/src/bun.js/modules/ConstantsModule.h deleted file mode 100644 index 8d7c1602b..000000000 --- a/src/bun.js/modules/ConstantsModule.h +++ /dev/null @@ -1,262 +0,0 @@ -#include "JavaScriptCore/JSGlobalObject.h" -#include "ZigGlobalObject.h" - -namespace Zig { -using namespace WebCore; - -inline void generateConstantsSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { - JSC::VM &vm = lexicalGlobalObject->vm(); - GlobalObject *globalObject = reinterpret_cast<GlobalObject *>(lexicalGlobalObject); - - auto* defaultObject = JSC::constructEmptyObject(globalObject); - - - auto exportProperty = [&](JSC::Identifier name, JSC::JSValue value) { - exportNames.append(name); - exportValues.append(value); - defaultObject->putDirect(vm, name, value, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | 0); - }; - - exportProperty(JSC::Identifier::fromString(vm, "RTLD_LAZY"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "RTLD_NOW"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "RTLD_GLOBAL"_s), JSC::jsNumber(256)); - exportProperty(JSC::Identifier::fromString(vm, "RTLD_LOCAL"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "RTLD_DEEPBIND"_s), JSC::jsNumber(8)); - exportProperty(JSC::Identifier::fromString(vm, "E2BIG"_s), JSC::jsNumber(7)); - exportProperty(JSC::Identifier::fromString(vm, "EACCES"_s), JSC::jsNumber(13)); - exportProperty(JSC::Identifier::fromString(vm, "EADDRINUSE"_s), JSC::jsNumber(98)); - exportProperty(JSC::Identifier::fromString(vm, "EADDRNOTAVAIL"_s), JSC::jsNumber(99)); - exportProperty(JSC::Identifier::fromString(vm, "EAFNOSUPPORT"_s), JSC::jsNumber(97)); - exportProperty(JSC::Identifier::fromString(vm, "EAGAIN"_s), JSC::jsNumber(11)); - exportProperty(JSC::Identifier::fromString(vm, "EALREADY"_s), JSC::jsNumber(114)); - exportProperty(JSC::Identifier::fromString(vm, "EBADF"_s), JSC::jsNumber(9)); - exportProperty(JSC::Identifier::fromString(vm, "EBADMSG"_s), JSC::jsNumber(74)); - exportProperty(JSC::Identifier::fromString(vm, "EBUSY"_s), JSC::jsNumber(16)); - exportProperty(JSC::Identifier::fromString(vm, "ECANCELED"_s), JSC::jsNumber(125)); - exportProperty(JSC::Identifier::fromString(vm, "ECHILD"_s), JSC::jsNumber(10)); - exportProperty(JSC::Identifier::fromString(vm, "ECONNABORTED"_s), JSC::jsNumber(103)); - exportProperty(JSC::Identifier::fromString(vm, "ECONNREFUSED"_s), JSC::jsNumber(111)); - exportProperty(JSC::Identifier::fromString(vm, "ECONNRESET"_s), JSC::jsNumber(104)); - exportProperty(JSC::Identifier::fromString(vm, "EDEADLK"_s), JSC::jsNumber(35)); - exportProperty(JSC::Identifier::fromString(vm, "EDESTADDRREQ"_s), JSC::jsNumber(89)); - exportProperty(JSC::Identifier::fromString(vm, "EDOM"_s), JSC::jsNumber(33)); - exportProperty(JSC::Identifier::fromString(vm, "EDQUOT"_s), JSC::jsNumber(122)); - exportProperty(JSC::Identifier::fromString(vm, "EEXIST"_s), JSC::jsNumber(17)); - exportProperty(JSC::Identifier::fromString(vm, "EFAULT"_s), JSC::jsNumber(14)); - exportProperty(JSC::Identifier::fromString(vm, "EFBIG"_s), JSC::jsNumber(27)); - exportProperty(JSC::Identifier::fromString(vm, "EHOSTUNREACH"_s), JSC::jsNumber(113)); - exportProperty(JSC::Identifier::fromString(vm, "EIDRM"_s), JSC::jsNumber(43)); - exportProperty(JSC::Identifier::fromString(vm, "EILSEQ"_s), JSC::jsNumber(84)); - exportProperty(JSC::Identifier::fromString(vm, "EINPROGRESS"_s), JSC::jsNumber(115)); - exportProperty(JSC::Identifier::fromString(vm, "EINTR"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "EINVAL"_s), JSC::jsNumber(22)); - exportProperty(JSC::Identifier::fromString(vm, "EIO"_s), JSC::jsNumber(5)); - exportProperty(JSC::Identifier::fromString(vm, "EISCONN"_s), JSC::jsNumber(106)); - exportProperty(JSC::Identifier::fromString(vm, "EISDIR"_s), JSC::jsNumber(21)); - exportProperty(JSC::Identifier::fromString(vm, "ELOOP"_s), JSC::jsNumber(40)); - exportProperty(JSC::Identifier::fromString(vm, "EMFILE"_s), JSC::jsNumber(24)); - exportProperty(JSC::Identifier::fromString(vm, "EMLINK"_s), JSC::jsNumber(31)); - exportProperty(JSC::Identifier::fromString(vm, "EMSGSIZE"_s), JSC::jsNumber(90)); - exportProperty(JSC::Identifier::fromString(vm, "EMULTIHOP"_s), JSC::jsNumber(72)); - exportProperty(JSC::Identifier::fromString(vm, "ENAMETOOLONG"_s), JSC::jsNumber(36)); - exportProperty(JSC::Identifier::fromString(vm, "ENETDOWN"_s), JSC::jsNumber(100)); - exportProperty(JSC::Identifier::fromString(vm, "ENETRESET"_s), JSC::jsNumber(102)); - exportProperty(JSC::Identifier::fromString(vm, "ENETUNREACH"_s), JSC::jsNumber(101)); - exportProperty(JSC::Identifier::fromString(vm, "ENFILE"_s), JSC::jsNumber(23)); - exportProperty(JSC::Identifier::fromString(vm, "ENOBUFS"_s), JSC::jsNumber(105)); - exportProperty(JSC::Identifier::fromString(vm, "ENODATA"_s), JSC::jsNumber(61)); - exportProperty(JSC::Identifier::fromString(vm, "ENODEV"_s), JSC::jsNumber(19)); - exportProperty(JSC::Identifier::fromString(vm, "ENOENT"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "ENOEXEC"_s), JSC::jsNumber(8)); - exportProperty(JSC::Identifier::fromString(vm, "ENOLCK"_s), JSC::jsNumber(37)); - exportProperty(JSC::Identifier::fromString(vm, "ENOLINK"_s), JSC::jsNumber(67)); - exportProperty(JSC::Identifier::fromString(vm, "ENOMEM"_s), JSC::jsNumber(12)); - exportProperty(JSC::Identifier::fromString(vm, "ENOMSG"_s), JSC::jsNumber(42)); - exportProperty(JSC::Identifier::fromString(vm, "ENOPROTOOPT"_s), JSC::jsNumber(92)); - exportProperty(JSC::Identifier::fromString(vm, "ENOSPC"_s), JSC::jsNumber(28)); - exportProperty(JSC::Identifier::fromString(vm, "ENOSR"_s), JSC::jsNumber(63)); - exportProperty(JSC::Identifier::fromString(vm, "ENOSTR"_s), JSC::jsNumber(60)); - exportProperty(JSC::Identifier::fromString(vm, "ENOSYS"_s), JSC::jsNumber(38)); - exportProperty(JSC::Identifier::fromString(vm, "ENOTCONN"_s), JSC::jsNumber(107)); - exportProperty(JSC::Identifier::fromString(vm, "ENOTDIR"_s), JSC::jsNumber(20)); - exportProperty(JSC::Identifier::fromString(vm, "ENOTEMPTY"_s), JSC::jsNumber(39)); - exportProperty(JSC::Identifier::fromString(vm, "ENOTSOCK"_s), JSC::jsNumber(88)); - exportProperty(JSC::Identifier::fromString(vm, "ENOTSUP"_s), JSC::jsNumber(95)); - exportProperty(JSC::Identifier::fromString(vm, "ENOTTY"_s), JSC::jsNumber(25)); - exportProperty(JSC::Identifier::fromString(vm, "ENXIO"_s), JSC::jsNumber(6)); - exportProperty(JSC::Identifier::fromString(vm, "EOPNOTSUPP"_s), JSC::jsNumber(95)); - exportProperty(JSC::Identifier::fromString(vm, "EOVERFLOW"_s), JSC::jsNumber(75)); - exportProperty(JSC::Identifier::fromString(vm, "EPERM"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "EPIPE"_s), JSC::jsNumber(32)); - exportProperty(JSC::Identifier::fromString(vm, "EPROTO"_s), JSC::jsNumber(71)); - exportProperty(JSC::Identifier::fromString(vm, "EPROTONOSUPPORT"_s), JSC::jsNumber(93)); - exportProperty(JSC::Identifier::fromString(vm, "EPROTOTYPE"_s), JSC::jsNumber(91)); - exportProperty(JSC::Identifier::fromString(vm, "ERANGE"_s), JSC::jsNumber(34)); - exportProperty(JSC::Identifier::fromString(vm, "EROFS"_s), JSC::jsNumber(30)); - exportProperty(JSC::Identifier::fromString(vm, "ESPIPE"_s), JSC::jsNumber(29)); - exportProperty(JSC::Identifier::fromString(vm, "ESRCH"_s), JSC::jsNumber(3)); - exportProperty(JSC::Identifier::fromString(vm, "ESTALE"_s), JSC::jsNumber(116)); - exportProperty(JSC::Identifier::fromString(vm, "ETIME"_s), JSC::jsNumber(62)); - exportProperty(JSC::Identifier::fromString(vm, "ETIMEDOUT"_s), JSC::jsNumber(110)); - exportProperty(JSC::Identifier::fromString(vm, "ETXTBSY"_s), JSC::jsNumber(26)); - exportProperty(JSC::Identifier::fromString(vm, "EWOULDBLOCK"_s), JSC::jsNumber(11)); - exportProperty(JSC::Identifier::fromString(vm, "EXDEV"_s), JSC::jsNumber(18)); - exportProperty(JSC::Identifier::fromString(vm, "PRIORITY_LOW"_s), JSC::jsNumber(19)); - exportProperty(JSC::Identifier::fromString(vm, "PRIORITY_BELOW_NORMAL"_s), JSC::jsNumber(10)); - exportProperty(JSC::Identifier::fromString(vm, "PRIORITY_NORMAL"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "PRIORITY_ABOVE_NORMAL"_s), JSC::jsNumber(-7)); - exportProperty(JSC::Identifier::fromString(vm, "PRIORITY_HIGH"_s), JSC::jsNumber(-14)); - exportProperty(JSC::Identifier::fromString(vm, "PRIORITY_HIGHEST"_s), JSC::jsNumber(-20)); - exportProperty(JSC::Identifier::fromString(vm, "SIGHUP"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "SIGINT"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "SIGQUIT"_s), JSC::jsNumber(3)); - exportProperty(JSC::Identifier::fromString(vm, "SIGILL"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "SIGTRAP"_s), JSC::jsNumber(5)); - exportProperty(JSC::Identifier::fromString(vm, "SIGABRT"_s), JSC::jsNumber(6)); - exportProperty(JSC::Identifier::fromString(vm, "SIGIOT"_s), JSC::jsNumber(6)); - exportProperty(JSC::Identifier::fromString(vm, "SIGBUS"_s), JSC::jsNumber(7)); - exportProperty(JSC::Identifier::fromString(vm, "SIGFPE"_s), JSC::jsNumber(8)); - exportProperty(JSC::Identifier::fromString(vm, "SIGKILL"_s), JSC::jsNumber(9)); - exportProperty(JSC::Identifier::fromString(vm, "SIGUSR1"_s), JSC::jsNumber(10)); - exportProperty(JSC::Identifier::fromString(vm, "SIGSEGV"_s), JSC::jsNumber(11)); - exportProperty(JSC::Identifier::fromString(vm, "SIGUSR2"_s), JSC::jsNumber(12)); - exportProperty(JSC::Identifier::fromString(vm, "SIGPIPE"_s), JSC::jsNumber(13)); - exportProperty(JSC::Identifier::fromString(vm, "SIGALRM"_s), JSC::jsNumber(14)); - exportProperty(JSC::Identifier::fromString(vm, "SIGTERM"_s), JSC::jsNumber(15)); - exportProperty(JSC::Identifier::fromString(vm, "SIGCHLD"_s), JSC::jsNumber(17)); - exportProperty(JSC::Identifier::fromString(vm, "SIGSTKFLT"_s), JSC::jsNumber(16)); - exportProperty(JSC::Identifier::fromString(vm, "SIGCONT"_s), JSC::jsNumber(18)); - exportProperty(JSC::Identifier::fromString(vm, "SIGSTOP"_s), JSC::jsNumber(19)); - exportProperty(JSC::Identifier::fromString(vm, "SIGTSTP"_s), JSC::jsNumber(20)); - exportProperty(JSC::Identifier::fromString(vm, "SIGTTIN"_s), JSC::jsNumber(21)); - exportProperty(JSC::Identifier::fromString(vm, "SIGTTOU"_s), JSC::jsNumber(22)); - exportProperty(JSC::Identifier::fromString(vm, "SIGURG"_s), JSC::jsNumber(23)); - exportProperty(JSC::Identifier::fromString(vm, "SIGXCPU"_s), JSC::jsNumber(24)); - exportProperty(JSC::Identifier::fromString(vm, "SIGXFSZ"_s), JSC::jsNumber(25)); - exportProperty(JSC::Identifier::fromString(vm, "SIGVTALRM"_s), JSC::jsNumber(26)); - exportProperty(JSC::Identifier::fromString(vm, "SIGPROF"_s), JSC::jsNumber(27)); - exportProperty(JSC::Identifier::fromString(vm, "SIGWINCH"_s), JSC::jsNumber(28)); - exportProperty(JSC::Identifier::fromString(vm, "SIGIO"_s), JSC::jsNumber(29)); - exportProperty(JSC::Identifier::fromString(vm, "SIGPOLL"_s), JSC::jsNumber(29)); - exportProperty(JSC::Identifier::fromString(vm, "SIGPWR"_s), JSC::jsNumber(30)); - exportProperty(JSC::Identifier::fromString(vm, "SIGSYS"_s), JSC::jsNumber(31)); - exportProperty(JSC::Identifier::fromString(vm, "UV_FS_SYMLINK_DIR"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "UV_FS_SYMLINK_JUNCTION"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "O_RDONLY"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "O_WRONLY"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "O_RDWR"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_UNKNOWN"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_FILE"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_DIR"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_LINK"_s), JSC::jsNumber(3)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_FIFO"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_SOCKET"_s), JSC::jsNumber(5)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_CHAR"_s), JSC::jsNumber(6)); - exportProperty(JSC::Identifier::fromString(vm, "UV_DIRENT_BLOCK"_s), JSC::jsNumber(7)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFMT"_s), JSC::jsNumber(61440)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFREG"_s), JSC::jsNumber(32768)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFDIR"_s), JSC::jsNumber(16384)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFCHR"_s), JSC::jsNumber(8192)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFBLK"_s), JSC::jsNumber(24576)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFIFO"_s), JSC::jsNumber(4096)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFLNK"_s), JSC::jsNumber(40960)); - exportProperty(JSC::Identifier::fromString(vm, "S_IFSOCK"_s), JSC::jsNumber(49152)); - exportProperty(JSC::Identifier::fromString(vm, "O_CREAT"_s), JSC::jsNumber(64)); - exportProperty(JSC::Identifier::fromString(vm, "O_EXCL"_s), JSC::jsNumber(128)); - exportProperty(JSC::Identifier::fromString(vm, "UV_FS_O_FILEMAP"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "O_NOCTTY"_s), JSC::jsNumber(256)); - exportProperty(JSC::Identifier::fromString(vm, "O_TRUNC"_s), JSC::jsNumber(512)); - exportProperty(JSC::Identifier::fromString(vm, "O_APPEND"_s), JSC::jsNumber(1024)); - exportProperty(JSC::Identifier::fromString(vm, "O_DIRECTORY"_s), JSC::jsNumber(65536)); - exportProperty(JSC::Identifier::fromString(vm, "O_NOATIME"_s), JSC::jsNumber(262144)); - exportProperty(JSC::Identifier::fromString(vm, "O_NOFOLLOW"_s), JSC::jsNumber(131072)); - exportProperty(JSC::Identifier::fromString(vm, "O_SYNC"_s), JSC::jsNumber(1052672)); - exportProperty(JSC::Identifier::fromString(vm, "O_DSYNC"_s), JSC::jsNumber(4096)); - exportProperty(JSC::Identifier::fromString(vm, "O_DIRECT"_s), JSC::jsNumber(16384)); - exportProperty(JSC::Identifier::fromString(vm, "O_NONBLOCK"_s), JSC::jsNumber(2048)); - exportProperty(JSC::Identifier::fromString(vm, "S_IRWXU"_s), JSC::jsNumber(448)); - exportProperty(JSC::Identifier::fromString(vm, "S_IRUSR"_s), JSC::jsNumber(256)); - exportProperty(JSC::Identifier::fromString(vm, "S_IWUSR"_s), JSC::jsNumber(128)); - exportProperty(JSC::Identifier::fromString(vm, "S_IXUSR"_s), JSC::jsNumber(64)); - exportProperty(JSC::Identifier::fromString(vm, "S_IRWXG"_s), JSC::jsNumber(56)); - exportProperty(JSC::Identifier::fromString(vm, "S_IRGRP"_s), JSC::jsNumber(32)); - exportProperty(JSC::Identifier::fromString(vm, "S_IWGRP"_s), JSC::jsNumber(16)); - exportProperty(JSC::Identifier::fromString(vm, "S_IXGRP"_s), JSC::jsNumber(8)); - exportProperty(JSC::Identifier::fromString(vm, "S_IRWXO"_s), JSC::jsNumber(7)); - exportProperty(JSC::Identifier::fromString(vm, "S_IROTH"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "S_IWOTH"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "S_IXOTH"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "F_OK"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "R_OK"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "W_OK"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "X_OK"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "UV_FS_COPYFILE_EXCL"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "COPYFILE_EXCL"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "UV_FS_COPYFILE_FICLONE"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "COPYFILE_FICLONE"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "UV_FS_COPYFILE_FICLONE_FORCE"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "COPYFILE_FICLONE_FORCE"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "OPENSSL_VERSION_NUMBER"_s), JSC::jsNumber(805306496)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_ALL"_s), JSC::jsNumber(2147485776)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_ALLOW_NO_DHE_KEX"_s), JSC::jsNumber(1024)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION"_s), JSC::jsNumber(262144)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_CIPHER_SERVER_PREFERENCE"_s), JSC::jsNumber(4194304)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_CISCO_ANYCONNECT"_s), JSC::jsNumber(32768)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_COOKIE_EXCHANGE"_s), JSC::jsNumber(8192)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_CRYPTOPRO_TLSEXT_BUG"_s), JSC::jsNumber(2147483648)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS"_s), JSC::jsNumber(2048)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_LEGACY_SERVER_CONNECT"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_COMPRESSION"_s), JSC::jsNumber(131072)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_ENCRYPT_THEN_MAC"_s), JSC::jsNumber(524288)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_QUERY_MTU"_s), JSC::jsNumber(4096)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_RENEGOTIATION"_s), JSC::jsNumber(1073741824)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION"_s), JSC::jsNumber(65536)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_SSLv2"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_SSLv3"_s), JSC::jsNumber(33554432)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_TICKET"_s), JSC::jsNumber(16384)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_TLSv1"_s), JSC::jsNumber(67108864)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_TLSv1_1"_s), JSC::jsNumber(268435456)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_TLSv1_2"_s), JSC::jsNumber(134217728)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_NO_TLSv1_3"_s), JSC::jsNumber(536870912)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_PRIORITIZE_CHACHA"_s), JSC::jsNumber(2097152)); - exportProperty(JSC::Identifier::fromString(vm, "SSL_OP_TLS_ROLLBACK_BUG"_s), JSC::jsNumber(8388608)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_RSA"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_DSA"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_DH"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_RAND"_s), JSC::jsNumber(8)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_EC"_s), JSC::jsNumber(2048)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_CIPHERS"_s), JSC::jsNumber(64)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_DIGESTS"_s), JSC::jsNumber(128)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_PKEY_METHS"_s), JSC::jsNumber(512)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_PKEY_ASN1_METHS"_s), JSC::jsNumber(1024)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_ALL"_s), JSC::jsNumber(65535)); - exportProperty(JSC::Identifier::fromString(vm, "ENGINE_METHOD_NONE"_s), JSC::jsNumber(0)); - exportProperty(JSC::Identifier::fromString(vm, "DH_CHECK_P_NOT_SAFE_PRIME"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "DH_CHECK_P_NOT_PRIME"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "DH_UNABLE_TO_CHECK_GENERATOR"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "DH_NOT_SUITABLE_GENERATOR"_s), JSC::jsNumber(8)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_PKCS1_PADDING"_s), JSC::jsNumber(1)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_NO_PADDING"_s), JSC::jsNumber(3)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_PKCS1_OAEP_PADDING"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_X931_PADDING"_s), JSC::jsNumber(5)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_PKCS1_PSS_PADDING"_s), JSC::jsNumber(6)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_PSS_SALTLEN_DIGEST"_s), JSC::jsNumber(-1)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_PSS_SALTLEN_MAX_SIGN"_s), JSC::jsNumber(-2)); - exportProperty(JSC::Identifier::fromString(vm, "RSA_PSS_SALTLEN_AUTO"_s), JSC::jsNumber(-2)); - exportProperty(JSC::Identifier::fromString(vm, "defaultCoreCipherList"_s), JSC::jsString(vm, WTF::String::fromUTF8("DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256"))); - exportProperty(JSC::Identifier::fromString(vm, "TLS1_VERSION"_s), JSC::jsNumber(769)); - exportProperty(JSC::Identifier::fromString(vm, "TLS1_1_VERSION"_s), JSC::jsNumber(770)); - exportProperty(JSC::Identifier::fromString(vm, "TLS1_2_VERSION"_s), JSC::jsNumber(771)); - exportProperty(JSC::Identifier::fromString(vm, "TLS1_3_VERSION"_s), JSC::jsNumber(772)); - exportProperty(JSC::Identifier::fromString(vm, "POINT_CONVERSION_COMPRESSED"_s), JSC::jsNumber(2)); - exportProperty(JSC::Identifier::fromString(vm, "POINT_CONVERSION_UNCOMPRESSED"_s), JSC::jsNumber(4)); - exportProperty(JSC::Identifier::fromString(vm, "POINT_CONVERSION_HYBRID"_s), JSC::jsNumber(6)); - - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(defaultObject); -} - -} // namespace Zig diff --git a/src/bun.js/modules/EventsModule.h b/src/bun.js/modules/EventsModule.h deleted file mode 100644 index 7d53ff838..000000000 --- a/src/bun.js/modules/EventsModule.h +++ /dev/null @@ -1,58 +0,0 @@ -#include "JavaScriptCore/JSGlobalObject.h" -#include "ZigGlobalObject.h" - -namespace Zig { -using namespace WebCore; - -inline void generateEventsSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { - JSC::VM &vm = lexicalGlobalObject->vm(); - GlobalObject *globalObject = - reinterpret_cast<GlobalObject *>(lexicalGlobalObject); - - exportNames.append(JSC::Identifier::fromString(vm, "EventEmitter"_s)); - exportValues.append( - WebCore::JSEventEmitter::getConstructor(vm, globalObject)); - - exportNames.append(JSC::Identifier::fromString(vm, "getEventListeners"_s)); - exportValues.append(JSC::JSFunction::create( - vm, lexicalGlobalObject, 0, MAKE_STATIC_STRING_IMPL("getEventListeners"), - Events_functionGetEventListeners, ImplementationVisibility::Public)); - exportNames.append(JSC::Identifier::fromString(vm, "listenerCount"_s)); - exportValues.append(JSC::JSFunction::create( - vm, lexicalGlobalObject, 0, MAKE_STATIC_STRING_IMPL("listenerCount"), - Events_functionListenerCount, ImplementationVisibility::Public)); - exportNames.append(JSC::Identifier::fromString(vm, "once"_s)); - exportValues.append(JSC::JSFunction::create( - vm, lexicalGlobalObject, 0, MAKE_STATIC_STRING_IMPL("once"), - Events_functionOnce, ImplementationVisibility::Public)); - exportNames.append(JSC::Identifier::fromString(vm, "on"_s)); - exportValues.append(JSC::JSFunction::create( - vm, lexicalGlobalObject, 0, MAKE_STATIC_STRING_IMPL("on"), - Events_functionOn, ImplementationVisibility::Public)); - exportNames.append( - JSC::Identifier::fromString(vm, "captureRejectionSymbol"_s)); - exportValues.append(Symbol::create( - vm, vm.symbolRegistry().symbolForKey("nodejs.rejection"_s))); - - JSFunction *eventEmitterModuleCJS = - jsCast<JSFunction *>(WebCore::JSEventEmitter::getConstructor( - vm, reinterpret_cast<Zig::GlobalObject *>(globalObject))); - - eventEmitterModuleCJS->putDirect( - vm, - PropertyName( - Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))), - jsNumber(0), 0); - - for (size_t i = 0; i < exportNames.size(); i++) { - eventEmitterModuleCJS->putDirect(vm, exportNames[i], exportValues.at(i), 0); - } - - exportNames.append(JSC::Identifier::fromString(vm, "default"_s)); - exportValues.append(eventEmitterModuleCJS); -} - -} // namespace Zig diff --git a/src/bun.js/modules/BufferModule.h b/src/bun.js/modules/NodeBufferModule.h index 6e6e39e9c..5c6acd48e 100644 --- a/src/bun.js/modules/BufferModule.h +++ b/src/bun.js/modules/NodeBufferModule.h @@ -1,7 +1,5 @@ #include "../bindings/JSBuffer.h" -#include "../bindings/ZigGlobalObject.h" -#include "JavaScriptCore/JSGlobalObject.h" -#include "JavaScriptCore/ObjectConstructor.h" +#include "_NativeModule.h" #include "simdutf.h" namespace Zig { @@ -134,33 +132,11 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplemented, return JSValue::encode(jsUndefined()); } -inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { - JSC::VM &vm = lexicalGlobalObject->vm(); - GlobalObject *globalObject = - reinterpret_cast<GlobalObject *>(lexicalGlobalObject); +DEFINE_NATIVE_MODULE(NodeBuffer) { + INIT_NATIVE_MODULE(12); - JSC::JSObject *defaultObject = JSC::constructEmptyObject( - globalObject, globalObject->objectPrototype(), 12); - - auto CommonJS = - Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); - - defaultObject->putDirect(vm, PropertyName(CommonJS), jsNumber(0), 0); - - exportNames.append(CommonJS); - exportValues.append(jsNumber(0)); - - auto exportProperty = [&](JSC::Identifier name, JSC::JSValue value) { - exportNames.append(name); - exportValues.append(value); - defaultObject->putDirect(vm, name, value, 0); - }; - - exportProperty(JSC::Identifier::fromString(vm, "Buffer"_s), - globalObject->JSBufferConstructor()); + put(JSC::Identifier::fromString(vm, "Buffer"_s), + globalObject->JSBufferConstructor()); auto *slowBuffer = JSC::JSFunction::create( vm, globalObject, 0, "SlowBuffer"_s, WebCore::constructSlowBuffer, @@ -170,24 +146,24 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, vm, vm.propertyNames->prototype, globalObject->JSBufferPrototype(), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete); - exportProperty(JSC::Identifier::fromString(vm, "SlowBuffer"_s), slowBuffer); + put(JSC::Identifier::fromString(vm, "SlowBuffer"_s), slowBuffer); auto blobIdent = JSC::Identifier::fromString(vm, "Blob"_s); JSValue blobValue = lexicalGlobalObject->get(globalObject, PropertyName(blobIdent)); - exportProperty(blobIdent, blobValue); + put(blobIdent, blobValue); // TODO: implement File - exportProperty(JSC::Identifier::fromString(vm, "File"_s), blobValue); + put(JSC::Identifier::fromString(vm, "File"_s), blobValue); - exportProperty(JSC::Identifier::fromString(vm, "INSPECT_MAX_BYTES"_s), - JSC::jsNumber(50)); + put(JSC::Identifier::fromString(vm, "INSPECT_MAX_BYTES"_s), + JSC::jsNumber(50)); - exportProperty(JSC::Identifier::fromString(vm, "kMaxLength"_s), - JSC::jsNumber(4294967296LL)); + put(JSC::Identifier::fromString(vm, "kMaxLength"_s), + JSC::jsNumber(4294967296LL)); - exportProperty(JSC::Identifier::fromString(vm, "kStringMaxLength"_s), - JSC::jsNumber(536870888)); + put(JSC::Identifier::fromString(vm, "kStringMaxLength"_s), + JSC::jsNumber(536870888)); JSC::JSObject *constants = JSC::constructEmptyObject( lexicalGlobalObject, globalObject->objectPrototype(), 2); @@ -197,7 +173,7 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, JSC::Identifier::fromString(vm, "MAX_STRING_LENGTH"_s), JSC::jsNumber(536870888)); - exportProperty(JSC::Identifier::fromString(vm, "constants"_s), constants); + put(JSC::Identifier::fromString(vm, "constants"_s), constants); JSC::Identifier atobI = JSC::Identifier::fromString(vm, "atob"_s); JSC::JSValue atobV = @@ -207,37 +183,31 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, JSC::JSValue btoaV = lexicalGlobalObject->get(globalObject, PropertyName(btoaI)); - exportProperty(atobI, atobV); - exportProperty(btoaI, btoaV); + put(atobI, atobV); + put(btoaI, btoaV); auto *transcode = InternalFunction::createFunctionThatMasqueradesAsUndefined( vm, globalObject, 1, "transcode"_s, jsFunctionNotImplemented); - exportProperty(JSC::Identifier::fromString(vm, "transcode"_s), transcode); + put(JSC::Identifier::fromString(vm, "transcode"_s), transcode); auto *resolveObjectURL = InternalFunction::createFunctionThatMasqueradesAsUndefined( vm, globalObject, 1, "resolveObjectURL"_s, jsFunctionNotImplemented); - exportProperty(JSC::Identifier::fromString(vm, "resolveObjectURL"_s), - resolveObjectURL); - - exportProperty(JSC::Identifier::fromString(vm, "isAscii"_s), - JSC::JSFunction::create(vm, globalObject, 1, "isAscii"_s, - jsBufferConstructorFunction_isAscii, - ImplementationVisibility::Public, - NoIntrinsic, - jsBufferConstructorFunction_isUtf8)); - - exportProperty(JSC::Identifier::fromString(vm, "isUtf8"_s), - JSC::JSFunction::create(vm, globalObject, 1, "isUtf8"_s, - jsBufferConstructorFunction_isUtf8, - ImplementationVisibility::Public, - NoIntrinsic, - jsBufferConstructorFunction_isUtf8)); - - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(defaultObject); + put(JSC::Identifier::fromString(vm, "resolveObjectURL"_s), resolveObjectURL); + + put(JSC::Identifier::fromString(vm, "isAscii"_s), + JSC::JSFunction::create(vm, globalObject, 1, "isAscii"_s, + jsBufferConstructorFunction_isAscii, + ImplementationVisibility::Public, NoIntrinsic, + jsBufferConstructorFunction_isUtf8)); + + put(JSC::Identifier::fromString(vm, "isUtf8"_s), + JSC::JSFunction::create(vm, globalObject, 1, "isUtf8"_s, + jsBufferConstructorFunction_isUtf8, + ImplementationVisibility::Public, NoIntrinsic, + jsBufferConstructorFunction_isUtf8)); } } // namespace Zig diff --git a/src/bun.js/modules/NodeConstantsModule.h b/src/bun.js/modules/NodeConstantsModule.h new file mode 100644 index 000000000..c1e324b0a --- /dev/null +++ b/src/bun.js/modules/NodeConstantsModule.h @@ -0,0 +1,916 @@ +#include "_NativeModule.h" +// Modelled off of https://github.com/nodejs/node/blob/main/src/node_constants.cc +// Note that if you change any of this code, you probably also have to change ProcessBindingConstants.cpp + +// require('constants') is implemented in node as a spread of: +// - constants.os.dlopen +// - constants.os.errno +// - constants.os.priority +// - constants.os.signals +// - constants.fs +// - constants.crypto +// Instead of loading $processBindingConstants, we just inline it + +// These headers may not all be needed, but they are the ones node references. +// Most of the constants are defined with #if checks on existing #defines, instead of platform-checks +#include <openssl/ec.h> +#include <openssl/ssl.h> +#include <zlib.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <cerrno> +#include <csignal> +#include <limits> + +#ifndef OPENSSL_NO_ENGINE +#include <openssl/engine.h> +#endif + +#if !defined(_MSC_VER) +#include <unistd.h> +#endif + +#if defined(_WIN32) +#include <io.h> // _S_IREAD _S_IWRITE +#ifndef S_IRUSR +#define S_IRUSR _S_IREAD +#endif // S_IRUSR +#ifndef S_IWUSR +#define S_IWUSR _S_IWRITE +#endif // S_IWUSR +#else +#include <dlfcn.h> +#endif + +namespace Zig { +using namespace WebCore; + +DEFINE_NATIVE_MODULE(NodeConstants) { + INIT_NATIVE_MODULE(63); + +#ifdef RTLD_LAZY + put(Identifier::fromString(vm, "RTLD_LAZY"_s), jsNumber(RTLD_LAZY)); +#endif +#ifdef RTLD_NOW + put(Identifier::fromString(vm, "RTLD_NOW"_s), jsNumber(RTLD_NOW)); +#endif +#ifdef RTLD_GLOBAL + put(Identifier::fromString(vm, "RTLD_GLOBAL"_s), jsNumber(RTLD_GLOBAL)); +#endif +#ifdef RTLD_LOCAL + put(Identifier::fromString(vm, "RTLD_LOCAL"_s), jsNumber(RTLD_LOCAL)); +#endif +#ifdef RTLD_DEEPBIND + put(Identifier::fromString(vm, "RTLD_DEEPBIND"_s), jsNumber(RTLD_DEEPBIND)); +#endif +#ifdef E2BIG + put(Identifier::fromString(vm, "E2BIG"_s), jsNumber(E2BIG)); +#endif +#ifdef EACCES + put(Identifier::fromString(vm, "EACCES"_s), jsNumber(EACCES)); +#endif +#ifdef EADDRINUSE + put(Identifier::fromString(vm, "EADDRINUSE"_s), jsNumber(EADDRINUSE)); +#endif +#ifdef EADDRNOTAVAIL + put(Identifier::fromString(vm, "EADDRNOTAVAIL"_s), jsNumber(EADDRNOTAVAIL)); +#endif +#ifdef EAFNOSUPPORT + put(Identifier::fromString(vm, "EAFNOSUPPORT"_s), jsNumber(EAFNOSUPPORT)); +#endif +#ifdef EAGAIN + put(Identifier::fromString(vm, "EAGAIN"_s), jsNumber(EAGAIN)); +#endif +#ifdef EALREADY + put(Identifier::fromString(vm, "EALREADY"_s), jsNumber(EALREADY)); +#endif +#ifdef EBADF + put(Identifier::fromString(vm, "EBADF"_s), jsNumber(EBADF)); +#endif +#ifdef EBADMSG + put(Identifier::fromString(vm, "EBADMSG"_s), jsNumber(EBADMSG)); +#endif +#ifdef EBUSY + put(Identifier::fromString(vm, "EBUSY"_s), jsNumber(EBUSY)); +#endif +#ifdef ECANCELED + put(Identifier::fromString(vm, "ECANCELED"_s), jsNumber(ECANCELED)); +#endif +#ifdef ECHILD + put(Identifier::fromString(vm, "ECHILD"_s), jsNumber(ECHILD)); +#endif +#ifdef ECONNABORTED + put(Identifier::fromString(vm, "ECONNABORTED"_s), jsNumber(ECONNABORTED)); +#endif +#ifdef ECONNREFUSED + put(Identifier::fromString(vm, "ECONNREFUSED"_s), jsNumber(ECONNREFUSED)); +#endif +#ifdef ECONNRESET + put(Identifier::fromString(vm, "ECONNRESET"_s), jsNumber(ECONNRESET)); +#endif +#ifdef EDEADLK + put(Identifier::fromString(vm, "EDEADLK"_s), jsNumber(EDEADLK)); +#endif +#ifdef EDESTADDRREQ + put(Identifier::fromString(vm, "EDESTADDRREQ"_s), jsNumber(EDESTADDRREQ)); +#endif +#ifdef EDOM + put(Identifier::fromString(vm, "EDOM"_s), jsNumber(EDOM)); +#endif +#ifdef EDQUOT + put(Identifier::fromString(vm, "EDQUOT"_s), jsNumber(EDQUOT)); +#endif +#ifdef EEXIST + put(Identifier::fromString(vm, "EEXIST"_s), jsNumber(EEXIST)); +#endif +#ifdef EFAULT + put(Identifier::fromString(vm, "EFAULT"_s), jsNumber(EFAULT)); +#endif +#ifdef EFBIG + put(Identifier::fromString(vm, "EFBIG"_s), jsNumber(EFBIG)); +#endif +#ifdef EHOSTUNREACH + put(Identifier::fromString(vm, "EHOSTUNREACH"_s), jsNumber(EHOSTUNREACH)); +#endif +#ifdef EIDRM + put(Identifier::fromString(vm, "EIDRM"_s), jsNumber(EIDRM)); +#endif +#ifdef EILSEQ + put(Identifier::fromString(vm, "EILSEQ"_s), jsNumber(EILSEQ)); +#endif +#ifdef EINPROGRESS + put(Identifier::fromString(vm, "EINPROGRESS"_s), jsNumber(EINPROGRESS)); +#endif +#ifdef EINTR + put(Identifier::fromString(vm, "EINTR"_s), jsNumber(EINTR)); +#endif +#ifdef EINVAL + put(Identifier::fromString(vm, "EINVAL"_s), jsNumber(EINVAL)); +#endif +#ifdef EIO + put(Identifier::fromString(vm, "EIO"_s), jsNumber(EIO)); +#endif +#ifdef EISCONN + put(Identifier::fromString(vm, "EISCONN"_s), jsNumber(EISCONN)); +#endif +#ifdef EISDIR + put(Identifier::fromString(vm, "EISDIR"_s), jsNumber(EISDIR)); +#endif +#ifdef ELOOP + put(Identifier::fromString(vm, "ELOOP"_s), jsNumber(ELOOP)); +#endif +#ifdef EMFILE + put(Identifier::fromString(vm, "EMFILE"_s), jsNumber(EMFILE)); +#endif +#ifdef EMLINK + put(Identifier::fromString(vm, "EMLINK"_s), jsNumber(EMLINK)); +#endif +#ifdef EMSGSIZE + put(Identifier::fromString(vm, "EMSGSIZE"_s), jsNumber(EMSGSIZE)); +#endif +#ifdef EMULTIHOP + put(Identifier::fromString(vm, "EMULTIHOP"_s), jsNumber(EMULTIHOP)); +#endif +#ifdef ENAMETOOLONG + put(Identifier::fromString(vm, "ENAMETOOLONG"_s), jsNumber(ENAMETOOLONG)); +#endif +#ifdef ENETDOWN + put(Identifier::fromString(vm, "ENETDOWN"_s), jsNumber(ENETDOWN)); +#endif +#ifdef ENETRESET + put(Identifier::fromString(vm, "ENETRESET"_s), jsNumber(ENETRESET)); +#endif +#ifdef ENETUNREACH + put(Identifier::fromString(vm, "ENETUNREACH"_s), jsNumber(ENETUNREACH)); +#endif +#ifdef ENFILE + put(Identifier::fromString(vm, "ENFILE"_s), jsNumber(ENFILE)); +#endif +#ifdef ENOBUFS + put(Identifier::fromString(vm, "ENOBUFS"_s), jsNumber(ENOBUFS)); +#endif +#ifdef ENODATA + put(Identifier::fromString(vm, "ENODATA"_s), jsNumber(ENODATA)); +#endif +#ifdef ENODEV + put(Identifier::fromString(vm, "ENODEV"_s), jsNumber(ENODEV)); +#endif +#ifdef ENOENT + put(Identifier::fromString(vm, "ENOENT"_s), jsNumber(ENOENT)); +#endif +#ifdef ENOEXEC + put(Identifier::fromString(vm, "ENOEXEC"_s), jsNumber(ENOEXEC)); +#endif +#ifdef ENOLCK + put(Identifier::fromString(vm, "ENOLCK"_s), jsNumber(ENOLCK)); +#endif +#ifdef ENOLINK + put(Identifier::fromString(vm, "ENOLINK"_s), jsNumber(ENOLINK)); +#endif +#ifdef ENOMEM + put(Identifier::fromString(vm, "ENOMEM"_s), jsNumber(ENOMEM)); +#endif +#ifdef ENOMSG + put(Identifier::fromString(vm, "ENOMSG"_s), jsNumber(ENOMSG)); +#endif +#ifdef ENOPROTOOPT + put(Identifier::fromString(vm, "ENOPROTOOPT"_s), jsNumber(ENOPROTOOPT)); +#endif +#ifdef ENOSPC + put(Identifier::fromString(vm, "ENOSPC"_s), jsNumber(ENOSPC)); +#endif +#ifdef ENOSR + put(Identifier::fromString(vm, "ENOSR"_s), jsNumber(ENOSR)); +#endif +#ifdef ENOSTR + put(Identifier::fromString(vm, "ENOSTR"_s), jsNumber(ENOSTR)); +#endif +#ifdef ENOSYS + put(Identifier::fromString(vm, "ENOSYS"_s), jsNumber(ENOSYS)); +#endif +#ifdef ENOTCONN + put(Identifier::fromString(vm, "ENOTCONN"_s), jsNumber(ENOTCONN)); +#endif +#ifdef ENOTDIR + put(Identifier::fromString(vm, "ENOTDIR"_s), jsNumber(ENOTDIR)); +#endif +#ifdef ENOTEMPTY + put(Identifier::fromString(vm, "ENOTEMPTY"_s), jsNumber(ENOTEMPTY)); +#endif +#ifdef ENOTSOCK + put(Identifier::fromString(vm, "ENOTSOCK"_s), jsNumber(ENOTSOCK)); +#endif +#ifdef ENOTSUP + put(Identifier::fromString(vm, "ENOTSUP"_s), jsNumber(ENOTSUP)); +#endif +#ifdef ENOTTY + put(Identifier::fromString(vm, "ENOTTY"_s), jsNumber(ENOTTY)); +#endif +#ifdef ENXIO + put(Identifier::fromString(vm, "ENXIO"_s), jsNumber(ENXIO)); +#endif +#ifdef EOPNOTSUPP + put(Identifier::fromString(vm, "EOPNOTSUPP"_s), jsNumber(EOPNOTSUPP)); +#endif +#ifdef EOVERFLOW + put(Identifier::fromString(vm, "EOVERFLOW"_s), jsNumber(EOVERFLOW)); +#endif +#ifdef EPERM + put(Identifier::fromString(vm, "EPERM"_s), jsNumber(EPERM)); +#endif +#ifdef EPIPE + put(Identifier::fromString(vm, "EPIPE"_s), jsNumber(EPIPE)); +#endif +#ifdef EPROTO + put(Identifier::fromString(vm, "EPROTO"_s), jsNumber(EPROTO)); +#endif +#ifdef EPROTONOSUPPORT + put(Identifier::fromString(vm, "EPROTONOSUPPORT"_s), jsNumber(EPROTONOSUPPORT)); +#endif +#ifdef EPROTOTYPE + put(Identifier::fromString(vm, "EPROTOTYPE"_s), jsNumber(EPROTOTYPE)); +#endif +#ifdef ERANGE + put(Identifier::fromString(vm, "ERANGE"_s), jsNumber(ERANGE)); +#endif +#ifdef EROFS + put(Identifier::fromString(vm, "EROFS"_s), jsNumber(EROFS)); +#endif +#ifdef ESPIPE + put(Identifier::fromString(vm, "ESPIPE"_s), jsNumber(ESPIPE)); +#endif +#ifdef ESRCH + put(Identifier::fromString(vm, "ESRCH"_s), jsNumber(ESRCH)); +#endif +#ifdef ESTALE + put(Identifier::fromString(vm, "ESTALE"_s), jsNumber(ESTALE)); +#endif +#ifdef ETIME + put(Identifier::fromString(vm, "ETIME"_s), jsNumber(ETIME)); +#endif +#ifdef ETIMEDOUT + put(Identifier::fromString(vm, "ETIMEDOUT"_s), jsNumber(ETIMEDOUT)); +#endif +#ifdef ETXTBSY + put(Identifier::fromString(vm, "ETXTBSY"_s), jsNumber(ETXTBSY)); +#endif +#ifdef EWOULDBLOCK + put(Identifier::fromString(vm, "EWOULDBLOCK"_s), jsNumber(EWOULDBLOCK)); +#endif +#ifdef EXDEV + put(Identifier::fromString(vm, "EXDEV"_s), jsNumber(EXDEV)); +#endif +#ifdef WSAEINTR + put(Identifier::fromString(vm, "WSAEINTR"_s), jsNumber(WSAEINTR)); +#endif +#ifdef WSAEBADF + put(Identifier::fromString(vm, "WSAEBADF"_s), jsNumber(WSAEBADF)); +#endif +#ifdef WSAEACCES + put(Identifier::fromString(vm, "WSAEACCES"_s), jsNumber(WSAEACCES)); +#endif +#ifdef WSAEFAULT + put(Identifier::fromString(vm, "WSAEFAULT"_s), jsNumber(WSAEFAULT)); +#endif +#ifdef WSAEINVAL + put(Identifier::fromString(vm, "WSAEINVAL"_s), jsNumber(WSAEINVAL)); +#endif +#ifdef WSAEMFILE + put(Identifier::fromString(vm, "WSAEMFILE"_s), jsNumber(WSAEMFILE)); +#endif +#ifdef WSAEWOULDBLOCK + put(Identifier::fromString(vm, "WSAEWOULDBLOCK"_s), jsNumber(WSAEWOULDBLOCK)); +#endif +#ifdef WSAEINPROGRESS + put(Identifier::fromString(vm, "WSAEINPROGRESS"_s), jsNumber(WSAEINPROGRESS)); +#endif +#ifdef WSAEALREADY + put(Identifier::fromString(vm, "WSAEALREADY"_s), jsNumber(WSAEALREADY)); +#endif +#ifdef WSAENOTSOCK + put(Identifier::fromString(vm, "WSAENOTSOCK"_s), jsNumber(WSAENOTSOCK)); +#endif +#ifdef WSAEDESTADDRREQ + put(Identifier::fromString(vm, "WSAEDESTADDRREQ"_s), jsNumber(WSAEDESTADDRREQ)); +#endif +#ifdef WSAEMSGSIZE + put(Identifier::fromString(vm, "WSAEMSGSIZE"_s), jsNumber(WSAEMSGSIZE)); +#endif +#ifdef WSAEPROTOTYPE + put(Identifier::fromString(vm, "WSAEPROTOTYPE"_s), jsNumber(WSAEPROTOTYPE)); +#endif +#ifdef WSAENOPROTOOPT + put(Identifier::fromString(vm, "WSAENOPROTOOPT"_s), jsNumber(WSAENOPROTOOPT)); +#endif +#ifdef WSAEPROTONOSUPPORT + put(Identifier::fromString(vm, "WSAEPROTONOSUPPORT"_s), jsNumber(WSAEPROTONOSUPPORT)); +#endif +#ifdef WSAESOCKTNOSUPPORT + put(Identifier::fromString(vm, "WSAESOCKTNOSUPPORT"_s), jsNumber(WSAESOCKTNOSUPPORT)); +#endif +#ifdef WSAEOPNOTSUPP + put(Identifier::fromString(vm, "WSAEOPNOTSUPP"_s), jsNumber(WSAEOPNOTSUPP)); +#endif +#ifdef WSAEPFNOSUPPORT + put(Identifier::fromString(vm, "WSAEPFNOSUPPORT"_s), jsNumber(WSAEPFNOSUPPORT)); +#endif +#ifdef WSAEAFNOSUPPORT + put(Identifier::fromString(vm, "WSAEAFNOSUPPORT"_s), jsNumber(WSAEAFNOSUPPORT)); +#endif +#ifdef WSAEADDRINUSE + put(Identifier::fromString(vm, "WSAEADDRINUSE"_s), jsNumber(WSAEADDRINUSE)); +#endif +#ifdef WSAEADDRNOTAVAIL + put(Identifier::fromString(vm, "WSAEADDRNOTAVAIL"_s), jsNumber(WSAEADDRNOTAVAIL)); +#endif +#ifdef WSAENETDOWN + put(Identifier::fromString(vm, "WSAENETDOWN"_s), jsNumber(WSAENETDOWN)); +#endif +#ifdef WSAENETUNREACH + put(Identifier::fromString(vm, "WSAENETUNREACH"_s), jsNumber(WSAENETUNREACH)); +#endif +#ifdef WSAENETRESET + put(Identifier::fromString(vm, "WSAENETRESET"_s), jsNumber(WSAENETRESET)); +#endif +#ifdef WSAECONNABORTED + put(Identifier::fromString(vm, "WSAECONNABORTED"_s), jsNumber(WSAECONNABORTED)); +#endif +#ifdef WSAECONNRESET + put(Identifier::fromString(vm, "WSAECONNRESET"_s), jsNumber(WSAECONNRESET)); +#endif +#ifdef WSAENOBUFS + put(Identifier::fromString(vm, "WSAENOBUFS"_s), jsNumber(WSAENOBUFS)); +#endif +#ifdef WSAEISCONN + put(Identifier::fromString(vm, "WSAEISCONN"_s), jsNumber(WSAEISCONN)); +#endif +#ifdef WSAENOTCONN + put(Identifier::fromString(vm, "WSAENOTCONN"_s), jsNumber(WSAENOTCONN)); +#endif +#ifdef WSAESHUTDOWN + put(Identifier::fromString(vm, "WSAESHUTDOWN"_s), jsNumber(WSAESHUTDOWN)); +#endif +#ifdef WSAETOOMANYREFS + put(Identifier::fromString(vm, "WSAETOOMANYREFS"_s), jsNumber(WSAETOOMANYREFS)); +#endif +#ifdef WSAETIMEDOUT + put(Identifier::fromString(vm, "WSAETIMEDOUT"_s), jsNumber(WSAETIMEDOUT)); +#endif +#ifdef WSAECONNREFUSED + put(Identifier::fromString(vm, "WSAECONNREFUSED"_s), jsNumber(WSAECONNREFUSED)); +#endif +#ifdef WSAELOOP + put(Identifier::fromString(vm, "WSAELOOP"_s), jsNumber(WSAELOOP)); +#endif +#ifdef WSAENAMETOOLONG + put(Identifier::fromString(vm, "WSAENAMETOOLONG"_s), jsNumber(WSAENAMETOOLONG)); +#endif +#ifdef WSAEHOSTDOWN + put(Identifier::fromString(vm, "WSAEHOSTDOWN"_s), jsNumber(WSAEHOSTDOWN)); +#endif +#ifdef WSAEHOSTUNREACH + put(Identifier::fromString(vm, "WSAEHOSTUNREACH"_s), jsNumber(WSAEHOSTUNREACH)); +#endif +#ifdef WSAENOTEMPTY + put(Identifier::fromString(vm, "WSAENOTEMPTY"_s), jsNumber(WSAENOTEMPTY)); +#endif +#ifdef WSAEPROCLIM + put(Identifier::fromString(vm, "WSAEPROCLIM"_s), jsNumber(WSAEPROCLIM)); +#endif +#ifdef WSAEUSERS + put(Identifier::fromString(vm, "WSAEUSERS"_s), jsNumber(WSAEUSERS)); +#endif +#ifdef WSAEDQUOT + put(Identifier::fromString(vm, "WSAEDQUOT"_s), jsNumber(WSAEDQUOT)); +#endif +#ifdef WSAESTALE + put(Identifier::fromString(vm, "WSAESTALE"_s), jsNumber(WSAESTALE)); +#endif +#ifdef WSAEREMOTE + put(Identifier::fromString(vm, "WSAEREMOTE"_s), jsNumber(WSAEREMOTE)); +#endif +#ifdef WSASYSNOTREADY + put(Identifier::fromString(vm, "WSASYSNOTREADY"_s), jsNumber(WSASYSNOTREADY)); +#endif +#ifdef WSAVERNOTSUPPORTED + put(Identifier::fromString(vm, "WSAVERNOTSUPPORTED"_s), jsNumber(WSAVERNOTSUPPORTED)); +#endif +#ifdef WSANOTINITIALISED + put(Identifier::fromString(vm, "WSANOTINITIALISED"_s), jsNumber(WSANOTINITIALISED)); +#endif +#ifdef WSAEDISCON + put(Identifier::fromString(vm, "WSAEDISCON"_s), jsNumber(WSAEDISCON)); +#endif +#ifdef WSAENOMORE + put(Identifier::fromString(vm, "WSAENOMORE"_s), jsNumber(WSAENOMORE)); +#endif +#ifdef WSAECANCELLED + put(Identifier::fromString(vm, "WSAECANCELLED"_s), jsNumber(WSAECANCELLED)); +#endif +#ifdef WSAEINVALIDPROCTABLE + put(Identifier::fromString(vm, "WSAEINVALIDPROCTABLE"_s), jsNumber(WSAEINVALIDPROCTABLE)); +#endif +#ifdef WSAEINVALIDPROVIDER + put(Identifier::fromString(vm, "WSAEINVALIDPROVIDER"_s), jsNumber(WSAEINVALIDPROVIDER)); +#endif +#ifdef WSAEPROVIDERFAILEDINIT + put(Identifier::fromString(vm, "WSAEPROVIDERFAILEDINIT"_s), jsNumber(WSAEPROVIDERFAILEDINIT)); +#endif +#ifdef WSASYSCALLFAILURE + put(Identifier::fromString(vm, "WSASYSCALLFAILURE"_s), jsNumber(WSASYSCALLFAILURE)); +#endif +#ifdef WSASERVICE_NOT_FOUND + put(Identifier::fromString(vm, "WSASERVICE_NOT_FOUND"_s), jsNumber(WSASERVICE_NOT_FOUND)); +#endif +#ifdef WSATYPE_NOT_FOUND + put(Identifier::fromString(vm, "WSATYPE_NOT_FOUND"_s), jsNumber(WSATYPE_NOT_FOUND)); +#endif +#ifdef WSA_E_NO_MORE + put(Identifier::fromString(vm, "WSA_E_NO_MORE"_s), jsNumber(WSA_E_NO_MORE)); +#endif +#ifdef WSA_E_CANCELLED + put(Identifier::fromString(vm, "WSA_E_CANCELLED"_s), jsNumber(WSA_E_CANCELLED)); +#endif +#ifdef WSAEREFUSED + put(Identifier::fromString(vm, "WSAEREFUSED"_s), jsNumber(WSAEREFUSED)); +#endif + put(Identifier::fromString(vm, "PRIORITY_LOW"_s), jsNumber(19)); + put(Identifier::fromString(vm, "PRIORITY_BELOW_NORMAL"_s), jsNumber(10)); + put(Identifier::fromString(vm, "PRIORITY_NORMAL"_s), jsNumber(0)); + put(Identifier::fromString(vm, "PRIORITY_ABOVE_NORMAL"_s), jsNumber(-7)); + put(Identifier::fromString(vm, "PRIORITY_HIGH"_s), jsNumber(-14)); + put(Identifier::fromString(vm, "PRIORITY_HIGHEST"_s), jsNumber(-20)); +#ifdef SIGHUP + put(Identifier::fromString(vm, "SIGHUP"_s), jsNumber(SIGHUP)); +#endif +#ifdef SIGINT + put(Identifier::fromString(vm, "SIGINT"_s), jsNumber(SIGINT)); +#endif +#ifdef SIGQUIT + put(Identifier::fromString(vm, "SIGQUIT"_s), jsNumber(SIGQUIT)); +#endif +#ifdef SIGILL + put(Identifier::fromString(vm, "SIGILL"_s), jsNumber(SIGILL)); +#endif +#ifdef SIGTRAP + put(Identifier::fromString(vm, "SIGTRAP"_s), jsNumber(SIGTRAP)); +#endif +#ifdef SIGABRT + put(Identifier::fromString(vm, "SIGABRT"_s), jsNumber(SIGABRT)); +#endif +#ifdef SIGIOT + put(Identifier::fromString(vm, "SIGIOT"_s), jsNumber(SIGIOT)); +#endif +#ifdef SIGBUS + put(Identifier::fromString(vm, "SIGBUS"_s), jsNumber(SIGBUS)); +#endif +#ifdef SIGFPE + put(Identifier::fromString(vm, "SIGFPE"_s), jsNumber(SIGFPE)); +#endif +#ifdef SIGKILL + put(Identifier::fromString(vm, "SIGKILL"_s), jsNumber(SIGKILL)); +#endif +#ifdef SIGUSR1 + put(Identifier::fromString(vm, "SIGUSR1"_s), jsNumber(SIGUSR1)); +#endif +#ifdef SIGSEGV + put(Identifier::fromString(vm, "SIGSEGV"_s), jsNumber(SIGSEGV)); +#endif +#ifdef SIGUSR2 + put(Identifier::fromString(vm, "SIGUSR2"_s), jsNumber(SIGUSR2)); +#endif +#ifdef SIGPIPE + put(Identifier::fromString(vm, "SIGPIPE"_s), jsNumber(SIGPIPE)); +#endif +#ifdef SIGALRM + put(Identifier::fromString(vm, "SIGALRM"_s), jsNumber(SIGALRM)); +#endif +#ifdef SIGTERM + put(Identifier::fromString(vm, "SIGTERM"_s), jsNumber(SIGTERM)); +#endif +#ifdef SIGCHLD + put(Identifier::fromString(vm, "SIGCHLD"_s), jsNumber(SIGCHLD)); +#endif +#ifdef SIGSTKFLT + put(Identifier::fromString(vm, "SIGSTKFLT"_s), jsNumber(SIGSTKFLT)); +#endif +#ifdef SIGCONT + put(Identifier::fromString(vm, "SIGCONT"_s), jsNumber(SIGCONT)); +#endif +#ifdef SIGSTOP + put(Identifier::fromString(vm, "SIGSTOP"_s), jsNumber(SIGSTOP)); +#endif +#ifdef SIGTSTP + put(Identifier::fromString(vm, "SIGTSTP"_s), jsNumber(SIGTSTP)); +#endif +#ifdef SIGBREAK + put(Identifier::fromString(vm, "SIGBREAK"_s), jsNumber(SIGBREAK)); +#endif +#ifdef SIGTTIN + put(Identifier::fromString(vm, "SIGTTIN"_s), jsNumber(SIGTTIN)); +#endif +#ifdef SIGTTOU + put(Identifier::fromString(vm, "SIGTTOU"_s), jsNumber(SIGTTOU)); +#endif +#ifdef SIGURG + put(Identifier::fromString(vm, "SIGURG"_s), jsNumber(SIGURG)); +#endif +#ifdef SIGXCPU + put(Identifier::fromString(vm, "SIGXCPU"_s), jsNumber(SIGXCPU)); +#endif +#ifdef SIGXFSZ + put(Identifier::fromString(vm, "SIGXFSZ"_s), jsNumber(SIGXFSZ)); +#endif +#ifdef SIGVTALRM + put(Identifier::fromString(vm, "SIGVTALRM"_s), jsNumber(SIGVTALRM)); +#endif +#ifdef SIGPROF + put(Identifier::fromString(vm, "SIGPROF"_s), jsNumber(SIGPROF)); +#endif +#ifdef SIGWINCH + put(Identifier::fromString(vm, "SIGWINCH"_s), jsNumber(SIGWINCH)); +#endif +#ifdef SIGIO + put(Identifier::fromString(vm, "SIGIO"_s), jsNumber(SIGIO)); +#endif +#ifdef SIGPOLL + put(Identifier::fromString(vm, "SIGPOLL"_s), jsNumber(SIGPOLL)); +#endif +#ifdef SIGLOST + put(Identifier::fromString(vm, "SIGLOST"_s), jsNumber(SIGLOST)); +#endif +#ifdef SIGPWR + put(Identifier::fromString(vm, "SIGPWR"_s), jsNumber(SIGPWR)); +#endif +#ifdef SIGINFO + put(Identifier::fromString(vm, "SIGINFO"_s), jsNumber(SIGINFO)); +#endif +#ifdef SIGSYS + put(Identifier::fromString(vm, "SIGSYS"_s), jsNumber(SIGSYS)); +#endif +#ifdef SIGUNUSED + put(Identifier::fromString(vm, "SIGUNUSED"_s), jsNumber(SIGUNUSED)); +#endif + put(Identifier::fromString(vm, "UV_FS_SYMLINK_DIR"_s), jsNumber(1)); + put(Identifier::fromString(vm, "UV_FS_SYMLINK_JUNCTION"_s), jsNumber(2)); + put(Identifier::fromString(vm, "O_RDONLY"_s), jsNumber(O_RDONLY)); + put(Identifier::fromString(vm, "O_WRONLY"_s), jsNumber(O_WRONLY)); + put(Identifier::fromString(vm, "O_RDWR"_s), jsNumber(O_RDWR)); + + put(Identifier::fromString(vm, "UV_DIRENT_UNKNOWN"_s), jsNumber(0)); + put(Identifier::fromString(vm, "UV_DIRENT_FILE"_s), jsNumber(1)); + put(Identifier::fromString(vm, "UV_DIRENT_DIR"_s), jsNumber(2)); + put(Identifier::fromString(vm, "UV_DIRENT_LINK"_s), jsNumber(3)); + put(Identifier::fromString(vm, "UV_DIRENT_FIFO"_s), jsNumber(4)); + put(Identifier::fromString(vm, "UV_DIRENT_SOCKET"_s), jsNumber(5)); + put(Identifier::fromString(vm, "UV_DIRENT_CHAR"_s), jsNumber(6)); + put(Identifier::fromString(vm, "UV_DIRENT_BLOCK"_s), jsNumber(7)); + + put(Identifier::fromString(vm, "S_IFMT"_s), jsNumber(S_IFMT)); + put(Identifier::fromString(vm, "S_IFREG"_s), jsNumber(S_IFREG)); + put(Identifier::fromString(vm, "S_IFDIR"_s), jsNumber(S_IFDIR)); + put(Identifier::fromString(vm, "S_IFCHR"_s), jsNumber(S_IFCHR)); +#ifdef S_IFBLK + put(Identifier::fromString(vm, "S_IFBLK"_s), jsNumber(S_IFBLK)); +#endif +#ifdef S_IFIFO + put(Identifier::fromString(vm, "S_IFIFO"_s), jsNumber(S_IFIFO)); +#endif +#ifdef S_IFLNK + put(Identifier::fromString(vm, "S_IFLNK"_s), jsNumber(S_IFLNK)); +#endif +#ifdef S_IFSOCK + put(Identifier::fromString(vm, "S_IFSOCK"_s), jsNumber(S_IFSOCK)); +#endif +#ifdef O_CREAT + put(Identifier::fromString(vm, "O_CREAT"_s), jsNumber(O_CREAT)); +#endif +#ifdef O_EXCL + put(Identifier::fromString(vm, "O_EXCL"_s), jsNumber(O_EXCL)); +#endif + put(Identifier::fromString(vm, "UV_FS_O_FILEMAP"_s), jsNumber(0)); + +#ifdef O_NOCTTY + put(Identifier::fromString(vm, "O_NOCTTY"_s), jsNumber(O_NOCTTY)); +#endif +#ifdef O_TRUNC + put(Identifier::fromString(vm, "O_TRUNC"_s), jsNumber(O_TRUNC)); +#endif +#ifdef O_APPEND + put(Identifier::fromString(vm, "O_APPEND"_s), jsNumber(O_APPEND)); +#endif +#ifdef O_DIRECTORY + put(Identifier::fromString(vm, "O_DIRECTORY"_s), jsNumber(O_DIRECTORY)); +#endif +#ifdef O_EXCL + put(Identifier::fromString(vm, "O_EXCL"_s), jsNumber(O_EXCL)); +#endif +#ifdef O_NOATIME + put(Identifier::fromString(vm, "O_NOATIME"_s), jsNumber(O_NOATIME)); +#endif +#ifdef O_NOFOLLOW + put(Identifier::fromString(vm, "O_NOFOLLOW"_s), jsNumber(O_NOFOLLOW)); +#endif +#ifdef O_SYNC + put(Identifier::fromString(vm, "O_SYNC"_s), jsNumber(O_SYNC)); +#endif +#ifdef O_DSYNC + put(Identifier::fromString(vm, "O_DSYNC"_s), jsNumber(O_DSYNC)); +#endif +#ifdef O_SYMLINK + put(Identifier::fromString(vm, "O_SYMLINK"_s), jsNumber(O_SYMLINK)); +#endif +#ifdef O_DIRECT + put(Identifier::fromString(vm, "O_DIRECT"_s), jsNumber(O_DIRECT)); +#endif +#ifdef O_NONBLOCK + put(Identifier::fromString(vm, "O_NONBLOCK"_s), jsNumber(O_NONBLOCK)); +#endif +#ifdef S_IRWXU + put(Identifier::fromString(vm, "S_IRWXU"_s), jsNumber(S_IRWXU)); +#endif +#ifdef S_IRUSR + put(Identifier::fromString(vm, "S_IRUSR"_s), jsNumber(S_IRUSR)); +#endif +#ifdef S_IWUSR + put(Identifier::fromString(vm, "S_IWUSR"_s), jsNumber(S_IWUSR)); +#endif +#ifdef S_IXUSR + put(Identifier::fromString(vm, "S_IXUSR"_s), jsNumber(S_IXUSR)); +#endif +#ifdef S_IRWXG + put(Identifier::fromString(vm, "S_IRWXG"_s), jsNumber(S_IRWXG)); +#endif +#ifdef S_IRGRP + put(Identifier::fromString(vm, "S_IRGRP"_s), jsNumber(S_IRGRP)); +#endif +#ifdef S_IWGRP + put(Identifier::fromString(vm, "S_IWGRP"_s), jsNumber(S_IWGRP)); +#endif +#ifdef S_IXGRP + put(Identifier::fromString(vm, "S_IXGRP"_s), jsNumber(S_IXGRP)); +#endif +#ifdef S_IRWXO + put(Identifier::fromString(vm, "S_IRWXO"_s), jsNumber(S_IRWXO)); +#endif +#ifdef S_IROTH + put(Identifier::fromString(vm, "S_IROTH"_s), jsNumber(S_IROTH)); +#endif +#ifdef S_IWOTH + put(Identifier::fromString(vm, "S_IWOTH"_s), jsNumber(S_IWOTH)); +#endif +#ifdef S_IXOTH + put(Identifier::fromString(vm, "S_IXOTH"_s), jsNumber(S_IXOTH)); +#endif +#ifdef F_OK + put(Identifier::fromString(vm, "F_OK"_s), jsNumber(F_OK)); +#endif +#ifdef R_OK + put(Identifier::fromString(vm, "R_OK"_s), jsNumber(R_OK)); +#endif +#ifdef W_OK + put(Identifier::fromString(vm, "W_OK"_s), jsNumber(W_OK)); +#endif +#ifdef X_OK + put(Identifier::fromString(vm, "X_OK"_s), jsNumber(X_OK)); +#endif + put(Identifier::fromString(vm, "UV_FS_COPYFILE_EXCL"_s), jsNumber(1)); + put(Identifier::fromString(vm, "COPYFILE_EXCL"_s), jsNumber(1)); + put(Identifier::fromString(vm, "UV_FS_COPYFILE_FICLONE"_s), jsNumber(2)); + put(Identifier::fromString(vm, "COPYFILE_FICLONE"_s), jsNumber(2)); + put(Identifier::fromString(vm, "UV_FS_COPYFILE_FICLONE_FORCE"_s), jsNumber(4)); + put(Identifier::fromString(vm, "COPYFILE_FICLONE_FORCE"_s), jsNumber(4)); +#ifdef OPENSSL_VERSION_NUMBER + put(Identifier::fromString(vm, "OPENSSL_VERSION_NUMBER"_s), jsNumber(OPENSSL_VERSION_NUMBER)); +#endif +#ifdef SSL_OP_ALL + put(Identifier::fromString(vm, "SSL_OP_ALL"_s), jsNumber(SSL_OP_ALL)); +#endif +#ifdef SSL_OP_ALLOW_NO_DHE_KEX + put(Identifier::fromString(vm, "SSL_OP_ALLOW_NO_DHE_KEX"_s), jsNumber(SSL_OP_ALLOW_NO_DHE_KEX)); +#endif +#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION + put(Identifier::fromString(vm, "SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION"_s), jsNumber(SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)); +#endif +#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE + put(Identifier::fromString(vm, "SSL_OP_CIPHER_SERVER_PREFERENCE"_s), jsNumber(SSL_OP_CIPHER_SERVER_PREFERENCE)); +#endif +#ifdef SSL_OP_CISCO_ANYCONNECT + put(Identifier::fromString(vm, "SSL_OP_CISCO_ANYCONNECT"_s), jsNumber(SSL_OP_CISCO_ANYCONNECT)); +#endif +#ifdef SSL_OP_COOKIE_EXCHANGE + put(Identifier::fromString(vm, "SSL_OP_COOKIE_EXCHANGE"_s), jsNumber(SSL_OP_COOKIE_EXCHANGE)); +#endif +#ifdef SSL_OP_CRYPTOPRO_TLSEXT_BUG + put(Identifier::fromString(vm, "SSL_OP_CRYPTOPRO_TLSEXT_BUG"_s), jsNumber(SSL_OP_CRYPTOPRO_TLSEXT_BUG)); +#endif +#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS + put(Identifier::fromString(vm, "SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS"_s), jsNumber(SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)); +#endif +#ifdef SSL_OP_LEGACY_SERVER_CONNECT + put(Identifier::fromString(vm, "SSL_OP_LEGACY_SERVER_CONNECT"_s), jsNumber(SSL_OP_LEGACY_SERVER_CONNECT)); +#endif +#ifdef SSL_OP_NO_COMPRESSION + put(Identifier::fromString(vm, "SSL_OP_NO_COMPRESSION"_s), jsNumber(SSL_OP_NO_COMPRESSION)); +#endif +#ifdef SSL_OP_NO_ENCRYPT_THEN_MAC + put(Identifier::fromString(vm, "SSL_OP_NO_ENCRYPT_THEN_MAC"_s), jsNumber(SSL_OP_NO_ENCRYPT_THEN_MAC)); +#endif +#ifdef SSL_OP_NO_QUERY_MTU + put(Identifier::fromString(vm, "SSL_OP_NO_QUERY_MTU"_s), jsNumber(SSL_OP_NO_QUERY_MTU)); +#endif +#ifdef SSL_OP_NO_RENEGOTIATION + put(Identifier::fromString(vm, "SSL_OP_NO_RENEGOTIATION"_s), jsNumber(SSL_OP_NO_RENEGOTIATION)); +#endif +#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION + put(Identifier::fromString(vm, "SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION"_s), jsNumber(SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)); +#endif +#ifdef SSL_OP_NO_SSLv2 + put(Identifier::fromString(vm, "SSL_OP_NO_SSLv2"_s), jsNumber(SSL_OP_NO_SSLv2)); +#endif +#ifdef SSL_OP_NO_SSLv3 + put(Identifier::fromString(vm, "SSL_OP_NO_SSLv3"_s), jsNumber(SSL_OP_NO_SSLv3)); +#endif +#ifdef SSL_OP_NO_TICKET + put(Identifier::fromString(vm, "SSL_OP_NO_TICKET"_s), jsNumber(SSL_OP_NO_TICKET)); +#endif +#ifdef SSL_OP_NO_TLSv1 + put(Identifier::fromString(vm, "SSL_OP_NO_TLSv1"_s), jsNumber(SSL_OP_NO_TLSv1)); +#endif +#ifdef SSL_OP_NO_TLSv1_1 + put(Identifier::fromString(vm, "SSL_OP_NO_TLSv1_1"_s), jsNumber(SSL_OP_NO_TLSv1_1)); +#endif +#ifdef SSL_OP_NO_TLSv1_2 + put(Identifier::fromString(vm, "SSL_OP_NO_TLSv1_2"_s), jsNumber(SSL_OP_NO_TLSv1_2)); +#endif +#ifdef SSL_OP_NO_TLSv1_3 + put(Identifier::fromString(vm, "SSL_OP_NO_TLSv1_3"_s), jsNumber(SSL_OP_NO_TLSv1_3)); +#endif +#ifdef SSL_OP_PRIORITIZE_CHACHA + put(Identifier::fromString(vm, "SSL_OP_PRIORITIZE_CHACHA"_s), jsNumber(SSL_OP_PRIORITIZE_CHACHA)); +#endif +#ifdef SSL_OP_TLS_ROLLBACK_BUG + put(Identifier::fromString(vm, "SSL_OP_TLS_ROLLBACK_BUG"_s), jsNumber(SSL_OP_TLS_ROLLBACK_BUG)); +#endif +#ifndef OPENSSL_NO_ENGINE +#ifdef ENGINE_METHOD_RSA + put(Identifier::fromString(vm, "ENGINE_METHOD_RSA"_s), jsNumber(ENGINE_METHOD_RSA)); +#endif +#ifdef ENGINE_METHOD_DSA + put(Identifier::fromString(vm, "ENGINE_METHOD_DSA"_s), jsNumber(ENGINE_METHOD_DSA)); +#endif +#ifdef ENGINE_METHOD_DH + put(Identifier::fromString(vm, "ENGINE_METHOD_DH"_s), jsNumber(ENGINE_METHOD_DH)); +#endif +#ifdef ENGINE_METHOD_RAND + put(Identifier::fromString(vm, "ENGINE_METHOD_RAND"_s), jsNumber(ENGINE_METHOD_RAND)); +#endif +#ifdef ENGINE_METHOD_EC + put(Identifier::fromString(vm, "ENGINE_METHOD_EC"_s), jsNumber(ENGINE_METHOD_EC)); +#endif +#ifdef ENGINE_METHOD_CIPHERS + put(Identifier::fromString(vm, "ENGINE_METHOD_CIPHERS"_s), jsNumber(ENGINE_METHOD_CIPHERS)); +#endif +#ifdef ENGINE_METHOD_DIGESTS + put(Identifier::fromString(vm, "ENGINE_METHOD_DIGESTS"_s), jsNumber(ENGINE_METHOD_DIGESTS)); +#endif +#ifdef ENGINE_METHOD_PKEY_METHS + put(Identifier::fromString(vm, "ENGINE_METHOD_PKEY_METHS"_s), jsNumber(ENGINE_METHOD_PKEY_METHS)); +#endif +#ifdef ENGINE_METHOD_PKEY_ASN1_METHS + put(Identifier::fromString(vm, "ENGINE_METHOD_PKEY_ASN1_METHS"_s), jsNumber(ENGINE_METHOD_PKEY_ASN1_METHS)); +#endif +#ifdef ENGINE_METHOD_ALL + put(Identifier::fromString(vm, "ENGINE_METHOD_ALL"_s), jsNumber(ENGINE_METHOD_ALL)); +#endif +#ifdef ENGINE_METHOD_NONE + put(Identifier::fromString(vm, "ENGINE_METHOD_NONE"_s), jsNumber(ENGINE_METHOD_NONE)); +#endif +#endif // !OPENSSL_NO_ENGINE +#ifdef DH_CHECK_P_NOT_SAFE_PRIME + put(Identifier::fromString(vm, "DH_CHECK_P_NOT_SAFE_PRIME"_s), jsNumber(DH_CHECK_P_NOT_SAFE_PRIME)); +#endif +#ifdef DH_CHECK_P_NOT_PRIME + put(Identifier::fromString(vm, "DH_CHECK_P_NOT_PRIME"_s), jsNumber(DH_CHECK_P_NOT_PRIME)); +#endif +#ifdef DH_UNABLE_TO_CHECK_GENERATOR + put(Identifier::fromString(vm, "DH_UNABLE_TO_CHECK_GENERATOR"_s), jsNumber(DH_UNABLE_TO_CHECK_GENERATOR)); +#endif +#ifdef DH_NOT_SUITABLE_GENERATOR + put(Identifier::fromString(vm, "DH_NOT_SUITABLE_GENERATOR"_s), jsNumber(DH_NOT_SUITABLE_GENERATOR)); +#endif +#ifdef RSA_PKCS1_PADDING + put(Identifier::fromString(vm, "RSA_PKCS1_PADDING"_s), jsNumber(RSA_PKCS1_PADDING)); +#endif +#ifdef RSA_SSLV23_PADDING + put(Identifier::fromString(vm, "RSA_SSLV23_PADDING"_s), jsNumber(RSA_SSLV23_PADDING)); +#endif +#ifdef RSA_NO_PADDING + put(Identifier::fromString(vm, "RSA_NO_PADDING"_s), jsNumber(RSA_NO_PADDING)); +#endif +#ifdef RSA_PKCS1_OAEP_PADDING + put(Identifier::fromString(vm, "RSA_PKCS1_OAEP_PADDING"_s), jsNumber(RSA_PKCS1_OAEP_PADDING)); +#endif +#ifdef RSA_X931_PADDING + put(Identifier::fromString(vm, "RSA_X931_PADDING"_s), jsNumber(RSA_X931_PADDING)); +#endif +#ifdef RSA_PKCS1_PSS_PADDING + put(Identifier::fromString(vm, "RSA_PKCS1_PSS_PADDING"_s), jsNumber(RSA_PKCS1_PSS_PADDING)); +#endif +#ifdef RSA_PSS_SALTLEN_DIGEST + put(Identifier::fromString(vm, "RSA_PSS_SALTLEN_DIGEST"_s), jsNumber(RSA_PSS_SALTLEN_DIGEST)); +#endif +#ifdef RSA_PSS_SALTLEN_MAX_SIGN + put(Identifier::fromString(vm, "RSA_PSS_SALTLEN_MAX_SIGN"_s), jsNumber(RSA_PSS_SALTLEN_MAX_SIGN)); +#endif +#ifdef RSA_PSS_SALTLEN_AUTO + put(Identifier::fromString(vm, "RSA_PSS_SALTLEN_AUTO"_s), jsNumber(RSA_PSS_SALTLEN_AUTO)); +#endif + auto cipherList = String("TLS_AES_256_GCM_SHA384:" + "TLS_CHACHA20_POLY1305_SHA256:" + "TLS_AES_128_GCM_SHA256:" + "ECDHE-RSA-AES128-GCM-SHA256:" + "ECDHE-ECDSA-AES128-GCM-SHA256:" + "ECDHE-RSA-AES256-GCM-SHA384:" + "ECDHE-ECDSA-AES256-GCM-SHA384:" + "DHE-RSA-AES128-GCM-SHA256:" + "ECDHE-RSA-AES128-SHA256:" + "DHE-RSA-AES128-SHA256:" + "ECDHE-RSA-AES256-SHA384:" + "DHE-RSA-AES256-SHA384:" + "ECDHE-RSA-AES256-SHA256:" + "DHE-RSA-AES256-SHA256:" + "HIGH:" + "!aNULL:" + "!eNULL:" + "!EXPORT:" + "!DES:" + "!RC4:" + "!MD5:" + "!PSK:" + "!SRP:" + "!CAMELLIA"_s); + put(Identifier::fromString(vm, "defaultCoreCipherList"_s), + jsString(vm, cipherList)); + put(Identifier::fromString(vm, "defaultCipherList"_s), + jsString(vm, cipherList)); +#ifdef TLS1_VERSION + put(Identifier::fromString(vm, "TLS1_VERSION"_s), jsNumber(TLS1_VERSION)); +#endif +#ifdef TLS1_1_VERSION + put(Identifier::fromString(vm, "TLS1_1_VERSION"_s), jsNumber(TLS1_1_VERSION)); +#endif +#ifdef TLS1_2_VERSION + put(Identifier::fromString(vm, "TLS1_2_VERSION"_s), jsNumber(TLS1_2_VERSION)); +#endif +#ifdef TLS1_3_VERSION + put(Identifier::fromString(vm, "TLS1_3_VERSION"_s), jsNumber(TLS1_3_VERSION)); +#endif + put(Identifier::fromString(vm, "POINT_CONVERSION_COMPRESSED"_s), jsNumber(POINT_CONVERSION_COMPRESSED)); + put(Identifier::fromString(vm, "POINT_CONVERSION_UNCOMPRESSED"_s), jsNumber(POINT_CONVERSION_UNCOMPRESSED)); + put(Identifier::fromString(vm, "POINT_CONVERSION_HYBRID"_s), jsNumber(POINT_CONVERSION_HYBRID)); + RETURN_NATIVE_MODULE(); +} + +} // namespace Zig diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp deleted file mode 100644 index 476ee95dc..000000000 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ /dev/null @@ -1,297 +0,0 @@ -#include "root.h" - -#include "./NodeModuleModule.h" - -#include "CommonJSModuleRecord.h" -#include "ImportMetaObject.h" -#include "JavaScriptCore/JSBoundFunction.h" -#include "JavaScriptCore/ObjectConstructor.h" -using namespace Zig; -using namespace JSC; - -// This is a mix of bun's builtin module names and also the ones reported by -// node v20.4.0 -static constexpr ASCIILiteral builtinModuleNames[] = { - "_http_agent"_s, - "_http_client"_s, - "_http_common"_s, - "_http_incoming"_s, - "_http_outgoing"_s, - "_http_server"_s, - "_stream_duplex"_s, - "_stream_passthrough"_s, - "_stream_readable"_s, - "_stream_transform"_s, - "_stream_wrap"_s, - "_stream_writable"_s, - "_tls_common"_s, - "_tls_wrap"_s, - "assert"_s, - "assert/strict"_s, - "async_hooks"_s, - "buffer"_s, - "bun"_s, - "bun:events_native"_s, - "bun:ffi"_s, - "bun:jsc"_s, - "bun:sqlite"_s, - "bun:wrap"_s, - "child_process"_s, - "cluster"_s, - "console"_s, - "constants"_s, - "crypto"_s, - "detect-libc"_s, - "dgram"_s, - "diagnostics_channel"_s, - "dns"_s, - "dns/promises"_s, - "domain"_s, - "events"_s, - "fs"_s, - "fs/promises"_s, - "http"_s, - "http2"_s, - "https"_s, - "inspector"_s, - "inspector/promises"_s, - "module"_s, - "net"_s, - "os"_s, - "path"_s, - "path/posix"_s, - "path/win32"_s, - "perf_hooks"_s, - "process"_s, - "punycode"_s, - "querystring"_s, - "readline"_s, - "readline/promises"_s, - "repl"_s, - "stream"_s, - "stream/consumers"_s, - "stream/promises"_s, - "stream/web"_s, - "string_decoder"_s, - "sys"_s, - "timers"_s, - "timers/promises"_s, - "tls"_s, - "trace_events"_s, - "tty"_s, - "undici"_s, - "url"_s, - "util"_s, - "util/types"_s, - "v8"_s, - "vm"_s, - "wasi"_s, - "worker_threads"_s, - "ws"_s, - "zlib"_s, -}; - -static bool isBuiltinModule(const String &namePossiblyWithNodePrefix) { - String name = namePossiblyWithNodePrefix; - if (name.startsWith("node:"_s)) - name = name.substringSharingImpl(5); - - for (auto &builtinModule : builtinModuleNames) { - if (name == builtinModule) - return true; - } - return false; -} - -JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBuiltinModule, - (JSC::JSGlobalObject * globalObject, - JSC::CallFrame *callFrame)) { - JSC::VM &vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - JSValue moduleName = callFrame->argument(0); - if (!moduleName.isString()) { - return JSValue::encode(jsBoolean(false)); - } - - auto moduleStr = moduleName.toWTFString(globalObject); - RETURN_IF_EXCEPTION(scope, JSValue::encode(jsBoolean(false))); - - return JSValue::encode(jsBoolean(isBuiltinModule(moduleStr))); -} - -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); - RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsUndefined())); - } - - auto val = callFrame->uncheckedArgument(0).toWTFString(globalObject); - RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); - RELEASE_AND_RETURN( - scope, JSValue::encode(Bun::JSCommonJSModule::createBoundRequireFunction( - vm, globalObject, val))); -} -extern "C" EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *, - CallFrame *); - -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; - } - } -} -template <std::size_t N, class T> consteval std::size_t countof(T (&)[N]) { - return N; -} - -namespace Zig { -void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { - JSC::VM &vm = globalObject->vm(); - - JSObject *defaultObject = JSC::constructEmptyObject( - vm, globalObject->nullPrototypeObjectStructure()); - auto append = [&](Identifier name, JSValue value) { - defaultObject->putDirect(vm, name, value); - exportNames.append(name); - exportValues.append(value); - }; - - append(Identifier::fromString(vm, "createRequire"_s), - JSFunction::create(vm, globalObject, 1, String("createRequire"_s), - jsFunctionNodeModuleCreateRequire, - ImplementationVisibility::Public)); - - append(Identifier::fromString(vm, "paths"_s), - JSFunction::create(vm, globalObject, 1, String("paths"_s), - Resolver__nodeModulePathsForJS, - ImplementationVisibility::Public)); - - append(Identifier::fromString(vm, "findSourceMap"_s), - JSFunction::create(vm, globalObject, 1, String("findSourceMap"_s), - jsFunctionFindSourceMap, - ImplementationVisibility::Public)); - append(Identifier::fromString(vm, "syncBuiltinExports"_s), - JSFunction::create(vm, globalObject, 0, String("syncBuiltinExports"_s), - jsFunctionSyncBuiltinExports, - ImplementationVisibility::Public)); - append(Identifier::fromString(vm, "SourceMap"_s), - JSFunction::create(vm, globalObject, 1, String("SourceMap"_s), - jsFunctionSourceMap, - ImplementationVisibility::Public, NoIntrinsic, - jsFunctionSourceMap, nullptr)); - - append(Identifier::fromString(vm, "isBuiltin"_s), - JSFunction::create(vm, globalObject, 1, String("isBuiltin"_s), - jsFunctionIsBuiltinModule, - ImplementationVisibility::Public, NoIntrinsic, - jsFunctionIsBuiltinModule, nullptr)); - - append(JSC::Identifier::fromString(vm, "_resolveFilename"_s), - JSFunction::create(vm, globalObject, 3, String("_resolveFilename"_s), - jsFunctionResolveFileName, - ImplementationVisibility::Public)); - - append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s), - JSFunction::create(vm, globalObject, 0, String("_nodeModulePaths"_s), - Resolver__nodeModulePathsForJS, - ImplementationVisibility::Public)); - - append(JSC::Identifier::fromString(vm, "_cache"_s), - jsCast<Zig::GlobalObject *>(globalObject)->lazyRequireCacheObject()); - - append(JSC::Identifier::fromString(vm, "globalPaths"_s), - JSC::constructEmptyArray(globalObject, nullptr, 0)); - - append(JSC::Identifier::fromString(vm, "prototype"_s), - JSC::constructEmptyObject(globalObject)); - - JSC::JSArray *builtinModules = JSC::JSArray::create( - vm, - globalObject->arrayStructureForIndexingTypeDuringAllocation( - ArrayWithContiguous), - countof(builtinModuleNames)); - - for (unsigned i = 0; i < countof(builtinModuleNames); ++i) { - builtinModules->putDirectIndex( - globalObject, i, JSC::jsString(vm, String(builtinModuleNames[i]))); - } - - append(JSC::Identifier::fromString(vm, "builtinModules"_s), builtinModules); - - defaultObject->putDirect(vm, - JSC::PropertyName(Identifier::fromUid( - vm.symbolRegistry().symbolForKey("CommonJS"_s))), - jsNumber(0), 0); - - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(defaultObject); -} -} // namespace Zig diff --git a/src/bun.js/modules/NodeModuleModule.h b/src/bun.js/modules/NodeModuleModule.h index 0aefdef12..e51f2ac86 100644 --- a/src/bun.js/modules/NodeModuleModule.h +++ b/src/bun.js/modules/NodeModuleModule.h @@ -1,12 +1,320 @@ -#include "../bindings/ZigGlobalObject.h" -#include "JavaScriptCore/JSGlobalObject.h" +#include "CommonJSModuleRecord.h" +#include "ImportMetaObject.h" +#include "JavaScriptCore/JSBoundFunction.h" +#include "JavaScriptCore/ObjectConstructor.h" +#include "_NativeModule.h" + +using namespace Zig; +using namespace JSC; + +// This is a mix of bun's builtin module names and also the ones reported by +// node v20.4.0 +static constexpr ASCIILiteral builtinModuleNames[] = { + "_http_agent"_s, + "_http_client"_s, + "_http_common"_s, + "_http_incoming"_s, + "_http_outgoing"_s, + "_http_server"_s, + "_stream_duplex"_s, + "_stream_passthrough"_s, + "_stream_readable"_s, + "_stream_transform"_s, + "_stream_wrap"_s, + "_stream_writable"_s, + "_tls_common"_s, + "_tls_wrap"_s, + "assert"_s, + "assert/strict"_s, + "async_hooks"_s, + "buffer"_s, + "bun"_s, + "bun:ffi"_s, + "bun:jsc"_s, + "bun:sqlite"_s, + "bun:wrap"_s, + "child_process"_s, + "cluster"_s, + "console"_s, + "constants"_s, + "crypto"_s, + "detect-libc"_s, + "dgram"_s, + "diagnostics_channel"_s, + "dns"_s, + "dns/promises"_s, + "domain"_s, + "events"_s, + "fs"_s, + "fs/promises"_s, + "http"_s, + "http2"_s, + "https"_s, + "inspector"_s, + "inspector/promises"_s, + "module"_s, + "net"_s, + "os"_s, + "path"_s, + "path/posix"_s, + "path/win32"_s, + "perf_hooks"_s, + "process"_s, + "punycode"_s, + "querystring"_s, + "readline"_s, + "readline/promises"_s, + "repl"_s, + "stream"_s, + "stream/consumers"_s, + "stream/promises"_s, + "stream/web"_s, + "string_decoder"_s, + "sys"_s, + "timers"_s, + "timers/promises"_s, + "tls"_s, + "trace_events"_s, + "tty"_s, + "undici"_s, + "url"_s, + "util"_s, + "util/types"_s, + "v8"_s, + "vm"_s, + "wasi"_s, + "worker_threads"_s, + "ws"_s, + "zlib"_s, +}; + +static bool isBuiltinModule(const String &namePossiblyWithNodePrefix) { + String name = namePossiblyWithNodePrefix; + if (name.startsWith("node:"_s)) + name = name.substringSharingImpl(5); + + for (auto &builtinModule : builtinModuleNames) { + if (name == builtinModule) + return true; + } + return false; +} + +JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleModuleConstructor, + (JSC::JSGlobalObject * globalObject, + JSC::CallFrame *callFrame)) { + // In node, this is supposed to be the actual CommonJSModule constructor. + // We are cutting a huge corner by not doing all that work. + // This code is only to support babel. + JSC::VM &vm = globalObject->vm(); + JSString *idString = JSC::jsString(vm, WTF::String("."_s)); + + JSString *dirname = jsEmptyString(vm); + + // TODO: handle when JSGlobalObject !== Zig::GlobalObject, such as in node:vm + Structure *structure = static_cast<Zig::GlobalObject *>(globalObject) + ->CommonJSModuleObjectStructure(); + + // TODO: handle ShadowRealm, node:vm, new.target, subclasses + JSValue idValue = callFrame->argument(0); + JSValue parentValue = callFrame->argument(1); + + auto scope = DECLARE_THROW_SCOPE(vm); + if (idValue.isString()) { + idString = idValue.toString(globalObject); + RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); + + auto index = idString->tryGetValue().reverseFind('/', idString->length()); + + if (index != WTF::notFound) { + dirname = JSC::jsSubstring(globalObject, idString, 0, index); + } + } + + auto *out = Bun::JSCommonJSModule::create(vm, structure, idString, jsNull(), + dirname, nullptr); + + if (!parentValue.isUndefined()) + out->putDirect(vm, JSC::Identifier::fromString(vm, "parent"_s), parentValue, + 0); + + return JSValue::encode(out); +} + +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBuiltinModule, + (JSC::JSGlobalObject * globalObject, + JSC::CallFrame *callFrame)) { + JSC::VM &vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + JSValue moduleName = callFrame->argument(0); + if (!moduleName.isString()) { + return JSValue::encode(jsBoolean(false)); + } + + auto moduleStr = moduleName.toWTFString(globalObject); + RETURN_IF_EXCEPTION(scope, JSValue::encode(jsBoolean(false))); + + return JSValue::encode(jsBoolean(isBuiltinModule(moduleStr))); +} + +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); + RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsUndefined())); + } + + auto val = callFrame->uncheckedArgument(0).toWTFString(globalObject); + RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); + RELEASE_AND_RETURN( + scope, JSValue::encode(Bun::JSCommonJSModule::createBoundRequireFunction( + vm, globalObject, val))); +} +extern "C" EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *, + CallFrame *); + +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; + } + } +} +template <std::size_t N, class T> consteval std::size_t countof(T (&)[N]) { + return N; +} namespace Zig { -// node:module -void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues); +DEFINE_NATIVE_MODULE(NodeModule) { + // the default object here is a function, so we cant use the INIT_NATIVE_MODULE helper + + Zig::GlobalObject *globalObject = reinterpret_cast<Zig::GlobalObject *>(lexicalGlobalObject); + JSC::VM &vm = globalObject->vm(); + JSC::JSObject *defaultObject = JSC::JSFunction::create( + vm, globalObject, 0, "Module"_s, jsFunctionNodeModuleModuleConstructor, + JSC::ImplementationVisibility::Public, JSC::NoIntrinsic, + jsFunctionNodeModuleModuleConstructor); + auto put = [&](JSC::Identifier name, JSC::JSValue value) { + defaultObject->putDirect(vm, name, value); + exportNames.append(name); + exportValues.append(value); + }; + auto putNativeFn = [&](JSC::Identifier name, JSC::NativeFunction ptr) { + JSC::JSFunction *value = JSC::JSFunction::create( + vm, globalObject, 1, name.string(), ptr, + JSC::ImplementationVisibility::Public, JSC::NoIntrinsic, ptr); + defaultObject->putDirect(vm, name, value); + exportNames.append(name); + exportValues.append(value); + }; + exportNames.reserveCapacity(13); + exportValues.ensureCapacity(13); + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(defaultObject); + + putNativeFn(Identifier::fromString(vm, "createRequire"_s), + jsFunctionNodeModuleCreateRequire); + putNativeFn(Identifier::fromString(vm, "paths"_s), + Resolver__nodeModulePathsForJS); + putNativeFn(Identifier::fromString(vm, "findSourceMap"_s), + jsFunctionFindSourceMap); + putNativeFn(Identifier::fromString(vm, "syncBuiltinExports"_s), + jsFunctionSyncBuiltinExports); + putNativeFn(Identifier::fromString(vm, "SourceMap"_s), jsFunctionSourceMap); + putNativeFn(Identifier::fromString(vm, "isBuiltin"_s), + jsFunctionIsBuiltinModule); + putNativeFn(Identifier::fromString(vm, "_resolveFilename"_s), + jsFunctionResolveFileName); + putNativeFn(Identifier::fromString(vm, "_nodeModulePaths"_s), + Resolver__nodeModulePathsForJS); + + put(Identifier::fromString(vm, "_cache"_s), + jsCast<Zig::GlobalObject *>(globalObject)->lazyRequireCacheObject()); + + put(Identifier::fromString(vm, "globalPaths"_s), + constructEmptyArray(globalObject, nullptr, 0)); + + put(Identifier::fromString(vm, "prototype"_s), + constructEmptyObject(globalObject)); + + JSC::JSArray *builtinModules = JSC::JSArray::create( + vm, + globalObject->arrayStructureForIndexingTypeDuringAllocation( + ArrayWithContiguous), + countof(builtinModuleNames)); + + for (unsigned i = 0; i < countof(builtinModuleNames); ++i) { + builtinModules->putDirectIndex( + globalObject, i, JSC::jsString(vm, String(builtinModuleNames[i]))); + } + + put(JSC::Identifier::fromString(vm, "builtinModules"_s), builtinModules); + + RETURN_NATIVE_MODULE(); +} -} // namespace Zig
\ No newline at end of file +} // namespace Zig diff --git a/src/bun.js/modules/ProcessModule.h b/src/bun.js/modules/NodeProcessModule.h index fab0298ae..6c3db0731 100644 --- a/src/bun.js/modules/ProcessModule.h +++ b/src/bun.js/modules/NodeProcessModule.h @@ -1,6 +1,7 @@ #include "../bindings/ZigGlobalObject.h" #include "JavaScriptCore/CustomGetterSetter.h" #include "JavaScriptCore/JSGlobalObject.h" +#include "_NativeModule.h" namespace Zig { @@ -35,10 +36,7 @@ JSC_DEFINE_CUSTOM_SETTER(jsFunctionProcessModuleCommonJSSetter, ->putDirect(vm, propertyName, JSValue::decode(encodedValue), 0); } -inline void generateProcessSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { +DEFINE_NATIVE_MODULE(NodeProcess) { JSC::VM &vm = lexicalGlobalObject->vm(); GlobalObject *globalObject = reinterpret_cast<GlobalObject *>(lexicalGlobalObject); @@ -61,10 +59,6 @@ inline void generateProcessSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, exportNames.append(vm.propertyNames->defaultKeyword); exportValues.append(process); - exportNames.append( - Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))); - exportValues.append(jsNumber(0)); - for (auto &entry : properties) { exportNames.append(entry); auto catchScope = DECLARE_CATCH_SCOPE(vm); diff --git a/src/bun.js/modules/NodeStringDecoderModule.h b/src/bun.js/modules/NodeStringDecoderModule.h new file mode 100644 index 000000000..3f5258cc1 --- /dev/null +++ b/src/bun.js/modules/NodeStringDecoderModule.h @@ -0,0 +1,16 @@ +#include "../bindings/JSStringDecoder.h" +#include "../bindings/ZigGlobalObject.h" +#include "JavaScriptCore/JSGlobalObject.h" + +namespace Zig { + +DEFINE_NATIVE_MODULE(NodeStringDecoder) { + INIT_NATIVE_MODULE(1); + + put(JSC::Identifier::fromString(vm, "StringDecoder"_s), + globalObject->JSStringDecoder()); + + RETURN_NATIVE_MODULE(); +} + +} // namespace Zig diff --git a/src/bun.js/modules/NodeTTYModule.h b/src/bun.js/modules/NodeTTYModule.h new file mode 100644 index 000000000..18a8f59a9 --- /dev/null +++ b/src/bun.js/modules/NodeTTYModule.h @@ -0,0 +1,50 @@ +#include "JSBuffer.h" +#include "_NativeModule.h" + +namespace Zig { +using namespace WebCore; + +JSC_DEFINE_HOST_FUNCTION(jsFunctionTty_isatty, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + if (callFrame->argumentCount() < 1) { + return JSValue::encode(jsBoolean(false)); + } + + auto scope = DECLARE_CATCH_SCOPE(vm); + int fd = callFrame->argument(0).toInt32(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + return JSValue::encode(jsBoolean(isatty(fd))); +} + +JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplementedYet, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + throwException(globalObject, throwScope, + createError(globalObject, "Not implemented yet"_s)); + return JSValue::encode(jsUndefined()); +} + +DEFINE_NATIVE_MODULE(NodeTTY) { + INIT_NATIVE_MODULE(3); + + auto *isattyFunction = + JSFunction::create(vm, globalObject, 1, "isatty"_s, jsFunctionTty_isatty, + ImplementationVisibility::Public); + + auto *notimpl = JSFunction::create(vm, globalObject, 0, "notimpl"_s, + jsFunctionNotImplementedYet, + ImplementationVisibility::Public, + NoIntrinsic, jsFunctionNotImplementedYet); + + putNativeFn(Identifier::fromString(vm, "isatty"_s), jsFunctionTty_isatty); + put(Identifier::fromString(vm, "ReadStream"_s), notimpl); + put(Identifier::fromString(vm, "WriteStream"_s), notimpl); + + RETURN_NATIVE_MODULE(); +} + +} // namespace Zig diff --git a/src/bun.js/modules/NodeUtilTypesModule.h b/src/bun.js/modules/NodeUtilTypesModule.h new file mode 100644 index 000000000..586743fea --- /dev/null +++ b/src/bun.js/modules/NodeUtilTypesModule.h @@ -0,0 +1,364 @@ +#include "_NativeModule.h" + +#include "webcrypto/JSCryptoKey.h" +#include "napi_external.h" +#include "JavaScriptCore/CallFrame.h" +#include "JavaScriptCore/CallFrameInlines.h" +#include "JavaScriptCore/AggregateError.h" +#include "JavaScriptCore/JSArrayBuffer.h" +#include "webcrypto/JSJsonWebKey.h" +#include "JavaScriptCore/ObjectConstructor.h" +#include "JavaScriptCore/GeneratorFunctionPrototype.h" +#include "JavaScriptCore/AsyncFunctionPrototype.h" +#include "JavaScriptCore/ErrorPrototype.h" + +using namespace JSC; + +#define GET_FIRST_VALUE \ + if (callframe->argumentCount() < 1) \ + return JSValue::encode(jsBoolean(false)); \ + JSValue value = callframe->uncheckedArgument(0); + +#define GET_FIRST_CELL \ + if (callframe->argumentCount() < 1) \ + return JSValue::encode(jsBoolean(false)); \ + JSValue value = callframe->uncheckedArgument(0); \ + if (!value.isCell()) \ + return JSValue::encode(jsBoolean(false)); \ + JSCell* cell = value.asCell(); + +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsExternal, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + return JSValue::encode(jsBoolean(value.isCell() && jsDynamicCast<Bun::NapiExternal*>(value) != nullptr)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsDate, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSDateType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsArgumentsObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + if (!value.isCell()) + return JSValue::encode(jsBoolean(false)); + + auto type = value.asCell()->type(); + switch (type) { + case DirectArgumentsType: + case ScopedArgumentsType: + case ClonedArgumentsType: + return JSValue::encode(jsBoolean(true)); + default: + return JSValue::encode(jsBoolean(false)); + } + + __builtin_unreachable(); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBigIntObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(globalObject->bigIntObjectStructure() == cell->structure())); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBooleanObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + return JSValue::encode(jsBoolean(value.isCell() && value.asCell()->type() == BooleanObjectType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsNumberObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + return JSValue::encode(jsBoolean(value.isCell() && value.asCell()->type() == NumberObjectType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsStringObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + return JSValue::encode(jsBoolean(value.isCell() && (value.asCell()->type() == StringObjectType || value.asCell()->type() == DerivedStringObjectType))); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsSymbolObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + + return JSValue::encode(jsBoolean(globalObject->symbolObjectStructure() == cell->structure())); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsNativeError, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + if (value.isCell()) { + if (value.inherits<JSC::ErrorInstance>() || value.asCell()->type() == ErrorInstanceType) + return JSValue::encode(jsBoolean(true)); + + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + JSObject* object = value.toObject(globalObject); + + // node util.isError relies on toString + // https://github.com/nodejs/node/blob/cf8c6994e0f764af02da4fa70bc5962142181bf3/doc/api/util.md#L2923 + PropertySlot slot(object, PropertySlot::InternalMethodType::VMInquiry, &vm); + if (object->getPropertySlot(globalObject, vm.propertyNames->toStringTagSymbol, slot)) { + EXCEPTION_ASSERT(!scope.exception()); + if (slot.isValue()) { + JSValue value = slot.getValue(globalObject, vm.propertyNames->toStringTagSymbol); + if (value.isString()) { + String tag = asString(value)->value(globalObject); + if (UNLIKELY(scope.exception())) + scope.clearException(); + if (tag == "Error"_s) + return JSValue::encode(jsBoolean(true)); + } + } + } + + JSValue proto = object->getPrototype(vm, globalObject); + if (proto.isCell() && (proto.inherits<JSC::ErrorInstance>() || proto.asCell()->type() == ErrorInstanceType || proto.inherits<JSC::ErrorPrototype>())) + return JSValue::encode(jsBoolean(true)); + } + + return JSValue::encode(jsBoolean(false)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsRegExp, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + return JSValue::encode(jsBoolean(value.isCell() && value.asCell()->type() == RegExpObjectType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsAsyncFunction, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(JSValue::strictEqual(globalObject, JSValue(globalObject->asyncFunctionPrototype()), cell->getObject()->getPrototype(cell->getObject(), globalObject)))); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsGeneratorFunction, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_VALUE + auto* function = jsDynamicCast<JSFunction*>(value); + if (!function) + return JSValue::encode(jsBoolean(false)); + + auto* executable = function->jsExecutable(); + if (!executable) + return JSValue::encode(jsBoolean(false)); + + return JSValue::encode(jsBoolean(executable->isGenerator())); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsGeneratorObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + + return JSValue::encode(jsBoolean(cell->type() == JSGeneratorType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsPromise, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSPromiseType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsMap, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSMapType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsSet, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSSetType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsMapIterator, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSMapIteratorType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsSetIterator, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSSetIteratorType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsWeakMap, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSWeakMapType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsWeakSet, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == JSWeakSetType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsArrayBuffer, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(jsDynamicCast<JSArrayBuffer*>(cell) != nullptr)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsDataView, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == DataViewType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsSharedArrayBuffer, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + auto* arrayBuffer = jsDynamicCast<JSArrayBuffer*>(cell); + if (!arrayBuffer) + return JSValue::encode(jsBoolean(false)); + return JSValue::encode(jsBoolean(arrayBuffer->isShared())); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsProxy, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == GlobalProxyType || cell->type() == ProxyObjectType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsModuleNamespaceObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == ModuleNamespaceObjectType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsAnyArrayBuffer, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + auto* arrayBuffer = jsDynamicCast<JSArrayBuffer*>(cell); + return JSValue::encode(jsBoolean(arrayBuffer != nullptr)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBoxedPrimitive, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + switch (cell->type()) { + case JSC::BooleanObjectType: + case JSC::NumberObjectType: + case JSC::StringObjectType: + case JSC::DerivedStringObjectType: + return JSValue::encode(jsBoolean(true)); + + default: { + if (cell->structure() == globalObject->symbolObjectStructure()) + return JSValue::encode(jsBoolean(true)); + + if (cell->structure() == globalObject->bigIntObjectStructure()) + return JSValue::encode(jsBoolean(true)); + } + } + + return JSValue::encode(jsBoolean(false)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsArrayBufferView, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() >= Int8ArrayType && cell->type() <= DataViewType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsTypedArray, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() >= Int8ArrayType && cell->type() <= BigUint64ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsUint8Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Uint8ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsUint8ClampedArray, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Uint8ClampedArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsUint16Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Uint16ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsUint32Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Uint32ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsInt8Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Int8ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsInt16Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Int16ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsInt32Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Int32ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsFloat32Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Float32ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsFloat64Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == Float64ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBigInt64Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == BigInt64ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBigUint64Array, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->type() == BigUint64ArrayType)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsKeyObject, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + // Not implemented + return JSValue::encode(jsBoolean(false)); +} +JSC_DEFINE_HOST_FUNCTION(jsFunctionIsCryptoKey, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callframe)) +{ + GET_FIRST_CELL + return JSValue::encode(jsBoolean(cell->inherits<WebCore::JSCryptoKey>())); +} + +namespace Zig { +DEFINE_NATIVE_MODULE(NodeUtilTypes) +{ + INIT_NATIVE_MODULE(42); + + putNativeFn(Identifier::fromString(vm, "isExternal"_s), jsFunctionIsExternal); + putNativeFn(Identifier::fromString(vm, "isDate"_s), jsFunctionIsDate); + putNativeFn(Identifier::fromString(vm, "isArgumentsObject"_s), jsFunctionIsArgumentsObject); + putNativeFn(Identifier::fromString(vm, "isBigIntObject"_s), jsFunctionIsBigIntObject); + putNativeFn(Identifier::fromString(vm, "isBooleanObject"_s), jsFunctionIsBooleanObject); + putNativeFn(Identifier::fromString(vm, "isNumberObject"_s), jsFunctionIsNumberObject); + putNativeFn(Identifier::fromString(vm, "isStringObject"_s), jsFunctionIsStringObject); + putNativeFn(Identifier::fromString(vm, "isSymbolObject"_s), jsFunctionIsSymbolObject); + putNativeFn(Identifier::fromString(vm, "isNativeError"_s), jsFunctionIsNativeError); + putNativeFn(Identifier::fromString(vm, "isRegExp"_s), jsFunctionIsRegExp); + putNativeFn(Identifier::fromString(vm, "isAsyncFunction"_s), jsFunctionIsAsyncFunction); + putNativeFn(Identifier::fromString(vm, "isGeneratorFunction"_s), jsFunctionIsGeneratorFunction); + putNativeFn(Identifier::fromString(vm, "isGeneratorObject"_s), jsFunctionIsGeneratorObject); + putNativeFn(Identifier::fromString(vm, "isPromise"_s), jsFunctionIsPromise); + putNativeFn(Identifier::fromString(vm, "isMap"_s), jsFunctionIsMap); + putNativeFn(Identifier::fromString(vm, "isSet"_s), jsFunctionIsSet); + putNativeFn(Identifier::fromString(vm, "isMapIterator"_s), jsFunctionIsMapIterator); + putNativeFn(Identifier::fromString(vm, "isSetIterator"_s), jsFunctionIsSetIterator); + putNativeFn(Identifier::fromString(vm, "isWeakMap"_s), jsFunctionIsWeakMap); + putNativeFn(Identifier::fromString(vm, "isWeakSet"_s), jsFunctionIsWeakSet); + putNativeFn(Identifier::fromString(vm, "isArrayBuffer"_s), jsFunctionIsArrayBuffer); + putNativeFn(Identifier::fromString(vm, "isDataView"_s), jsFunctionIsDataView); + putNativeFn(Identifier::fromString(vm, "isSharedArrayBuffer"_s), jsFunctionIsSharedArrayBuffer); + putNativeFn(Identifier::fromString(vm, "isProxy"_s), jsFunctionIsProxy); + putNativeFn(Identifier::fromString(vm, "isModuleNamespaceObject"_s), jsFunctionIsModuleNamespaceObject); + putNativeFn(Identifier::fromString(vm, "isAnyArrayBuffer"_s), jsFunctionIsAnyArrayBuffer); + putNativeFn(Identifier::fromString(vm, "isBoxedPrimitive"_s), jsFunctionIsBoxedPrimitive); + putNativeFn(Identifier::fromString(vm, "isArrayBufferView"_s), jsFunctionIsArrayBufferView); + putNativeFn(Identifier::fromString(vm, "isTypedArray"_s), jsFunctionIsTypedArray); + putNativeFn(Identifier::fromString(vm, "isUint8Array"_s), jsFunctionIsUint8Array); + putNativeFn(Identifier::fromString(vm, "isUint8ClampedArray"_s), jsFunctionIsUint8ClampedArray); + putNativeFn(Identifier::fromString(vm, "isUint16Array"_s), jsFunctionIsUint16Array); + putNativeFn(Identifier::fromString(vm, "isUint32Array"_s), jsFunctionIsUint32Array); + putNativeFn(Identifier::fromString(vm, "isInt8Array"_s), jsFunctionIsInt8Array); + putNativeFn(Identifier::fromString(vm, "isInt16Array"_s), jsFunctionIsInt16Array); + putNativeFn(Identifier::fromString(vm, "isInt32Array"_s), jsFunctionIsInt32Array); + putNativeFn(Identifier::fromString(vm, "isFloat32Array"_s), jsFunctionIsFloat32Array); + putNativeFn(Identifier::fromString(vm, "isFloat64Array"_s), jsFunctionIsFloat64Array); + putNativeFn(Identifier::fromString(vm, "isBigInt64Array"_s), jsFunctionIsBigInt64Array); + putNativeFn(Identifier::fromString(vm, "isBigUint64Array"_s), jsFunctionIsBigUint64Array); + putNativeFn(Identifier::fromString(vm, "isKeyObject"_s), jsFunctionIsKeyObject); + putNativeFn(Identifier::fromString(vm, "isCryptoKey"_s), jsFunctionIsCryptoKey); + + RETURN_NATIVE_MODULE(); +} +} // namespace Zig diff --git a/src/bun.js/modules/StringDecoderModule.h b/src/bun.js/modules/StringDecoderModule.h deleted file mode 100644 index 1dbf5ef8e..000000000 --- a/src/bun.js/modules/StringDecoderModule.h +++ /dev/null @@ -1,36 +0,0 @@ -#include "../bindings/JSStringDecoder.h" -#include "../bindings/ZigGlobalObject.h" -#include "JavaScriptCore/JSGlobalObject.h" - -namespace Zig { - -inline void -generateStringDecoderSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { - JSC::VM &vm = lexicalGlobalObject->vm(); - GlobalObject *globalObject = - reinterpret_cast<GlobalObject *>(lexicalGlobalObject); - - exportNames.append(JSC::Identifier::fromString(vm, "StringDecoder"_s)); - exportValues.append(globalObject->JSStringDecoder()); - - auto CommonJS = - Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); - - JSC::JSObject *defaultObject = constructEmptyObject(globalObject); - defaultObject->putDirect(vm, PropertyName(CommonJS), jsNumber(0), 0); - - for (size_t i = 0; i < exportNames.size(); i++) { - defaultObject->putDirect(vm, exportNames[i], exportValues.at(i), 0); - } - - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(defaultObject); - - exportNames.append(CommonJS); - exportValues.append(jsNumber(0)); -} - -} // namespace Zig diff --git a/src/bun.js/modules/TTYModule.h b/src/bun.js/modules/TTYModule.h deleted file mode 100644 index 79bc8c871..000000000 --- a/src/bun.js/modules/TTYModule.h +++ /dev/null @@ -1,81 +0,0 @@ -#include "../bindings/JSBuffer.h" -#include "../bindings/ZigGlobalObject.h" -#include "JavaScriptCore/JSGlobalObject.h" - -#include "JavaScriptCore/ObjectConstructor.h" - -namespace Zig { -using namespace WebCore; - -JSC_DEFINE_HOST_FUNCTION(jsFunctionTty_isatty, (JSGlobalObject * globalObject, - CallFrame *callFrame)) { - VM &vm = globalObject->vm(); - if (callFrame->argumentCount() < 1) { - return JSValue::encode(jsBoolean(false)); - } - - auto scope = DECLARE_CATCH_SCOPE(vm); - int fd = callFrame->argument(0).toInt32(globalObject); - RETURN_IF_EXCEPTION(scope, encodedJSValue()); - - return JSValue::encode(jsBoolean(isatty(fd))); -} - -JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplementedYet, - (JSGlobalObject * globalObject, - CallFrame *callFrame)) { - VM &vm = globalObject->vm(); - auto throwScope = DECLARE_THROW_SCOPE(vm); - throwException(globalObject, throwScope, - createError(globalObject, "Not implemented yet"_s)); - return JSValue::encode(jsUndefined()); -} - -inline void generateTTYSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, - JSC::Identifier moduleKey, - Vector<JSC::Identifier, 4> &exportNames, - JSC::MarkedArgumentBuffer &exportValues) { - JSC::VM &vm = lexicalGlobalObject->vm(); - GlobalObject *globalObject = - reinterpret_cast<GlobalObject *>(lexicalGlobalObject); - - auto *tty = JSC::constructEmptyObject(globalObject, - globalObject->objectPrototype(), 3); - - auto *isattyFunction = - JSFunction::create(vm, globalObject, 1, "isatty"_s, jsFunctionTty_isatty, - ImplementationVisibility::Public); - - auto *notimpl = JSFunction::create(vm, globalObject, 0, "notimpl"_s, - jsFunctionNotImplementedYet, - ImplementationVisibility::Public, - NoIntrinsic, jsFunctionNotImplementedYet); - - exportNames.append(JSC::Identifier::fromString(vm, "isatty"_s)); - exportValues.append(isattyFunction); - - exportNames.append(JSC::Identifier::fromString(vm, "ReadStream"_s)); - tty->putDirect(vm, JSC::Identifier::fromString(vm, "ReadStream"_s), notimpl); - exportValues.append(notimpl); - - exportNames.append(JSC::Identifier::fromString(vm, "WriteStream"_s)); - tty->putDirect(vm, JSC::Identifier::fromString(vm, "WriteStream"_s), notimpl); - exportValues.append(notimpl); - - for (size_t i = 0; i < exportNames.size(); i++) { - tty->putDirect(vm, exportNames[i], exportValues.at(i), 0); - } - - exportNames.append(vm.propertyNames->defaultKeyword); - exportValues.append(tty); - - auto CommonJS = - Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); - - exportNames.append(CommonJS); - exportValues.append(jsNumber(0)); - - tty->putDirect(vm, PropertyName(CommonJS), jsNumber(0), 0); -} - -} // namespace Zig diff --git a/src/bun.js/modules/_NativeModule.h b/src/bun.js/modules/_NativeModule.h new file mode 100644 index 000000000..01a112d0b --- /dev/null +++ b/src/bun.js/modules/_NativeModule.h @@ -0,0 +1,90 @@ +// clang-format off +#pragma once +#include "JSBuffer.h" +#include "JavaScriptCore/JSGlobalObject.h" +#include "JavaScriptCore/ObjectConstructor.h" +#include "ZigGlobalObject.h" + +// These modules are implemented in native code as a function which writes ESM +// export key+value pairs. The following macros help simplify the implementation +// of these functions. + +// To add a new native module +// 1. Add a new line to `BUN_FOREACH_NATIVE_MODULE` +// 2. Add a case to `module_loader.zig` that resolves the import. +// 3. Add a new file in this folder named after the module, camelcase and suffixed with Module, +// like "NodeBufferModule.h" or "BunJSCModule.h". It should call DEFINE_NATIVE_MODULE(name). +// +// The native module function is called to create the module object: +// - INIT_NATIVE_MODULE(n) is called with the number of exports +// - put(id, jsvalue) adds an export +// - putNativeFn(id, nativefn) lets you quickly add from `JSC_DEFINE_HOST_FUNCTION` +// - NATIVE_MODULE_FINISH() do asserts and finalize everything. +// If you decide to not use INIT_NATIVE_MODULE. make sure the first property +// given is the default export + +#define BUN_FOREACH_NATIVE_MODULE(macro) \ + macro("bun:jsc"_s, BunJSC) \ + macro("node:buffer"_s, NodeBuffer) \ + macro("node:constants"_s, NodeConstants) \ + macro("node:module"_s, NodeModule) \ + macro("node:process"_s, NodeProcess) \ + macro("node:string_decoder"_s, NodeStringDecoder) \ + macro("node:tty"_s, NodeTTY) \ + macro("node:util/types"_s, NodeUtilTypes) \ + +#if ASSERT_ENABLED + +// This function is a lie. It doesnt return, but rather it performs an assertion +// that what you passed to INIT_NATIVE_MODULE is indeed correct. +#define RETURN_NATIVE_MODULE() \ + ASSERT_WITH_MESSAGE(numberOfActualExportNames == passedNumberOfExportNames, \ + "NATIVE_MODULE_START() was given the incorrect value."); + +#define __NATIVE_MODULE_ASSERT_DECL \ + int numberOfActualExportNames = 0; \ + int passedNumberOfExportNames = numberOfExportNames; \ +#define __NATIVE_MODULE_ASSERT_INCR numberOfActualExportNames++; + +#else + +#define RETURN_NATIVE_MODULE() ; +#define __NATIVE_MODULE_ASSERT_INCR ; +#define __NATIVE_MODULE_ASSERT_DECL ; + +#endif + +#define DEFINE_NATIVE_MODULE(name) \ + inline void generateNativeModule_##name( \ + JSC::JSGlobalObject *lexicalGlobalObject, JSC::Identifier moduleKey, \ + Vector<JSC::Identifier, 4> &exportNames, \ + JSC::MarkedArgumentBuffer &exportValues) + +#define INIT_NATIVE_MODULE(numberOfExportNames) \ + Zig::GlobalObject *globalObject = \ + reinterpret_cast<Zig::GlobalObject *>(lexicalGlobalObject); \ + JSC::VM &vm = globalObject->vm(); \ + JSC::JSObject *defaultObject = JSC::constructEmptyObject( \ + globalObject, globalObject->objectPrototype(), numberOfExportNames); \ + __NATIVE_MODULE_ASSERT_DECL \ + auto put = [&](JSC::Identifier name, JSC::JSValue value) { \ + defaultObject->putDirect(vm, name, value); \ + exportNames.append(name); \ + exportValues.append(value); \ + __NATIVE_MODULE_ASSERT_INCR \ + }; \ + auto putNativeFn = [&](JSC::Identifier name, JSC::NativeFunction ptr) { \ + JSC::JSFunction *value = JSC::JSFunction::create( \ + vm, globalObject, 1, name.string(), ptr, \ + JSC::ImplementationVisibility::Public, JSC::NoIntrinsic, ptr); \ + defaultObject->putDirect(vm, name, value); \ + exportNames.append(name); \ + exportValues.append(value); \ + __NATIVE_MODULE_ASSERT_INCR \ + }; \ + exportNames.reserveCapacity(numberOfExportNames + 1); \ + exportValues.ensureCapacity(numberOfExportNames + 1); \ + exportNames.append(vm.propertyNames->defaultKeyword); \ + exportValues.append(defaultObject); \ + while (0) { \ + } |