aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Hyro <generalkubo@gmail.com> 2022-08-26 04:38:28 +0200
committerGravatar GitHub <noreply@github.com> 2022-08-25 19:38:28 -0700
commit7a734e0a28d8782de404de13362f0b822ea91c11 (patch)
treec56944346223fe9996c4144ab3ae6b85433909a1 /src/bun.js
parente0b35b3086b00fb27f950a72a082b360a3dad891 (diff)
downloadbun-7a734e0a28d8782de404de13362f0b822ea91c11.tar.gz
bun-7a734e0a28d8782de404de13362f0b822ea91c11.tar.zst
bun-7a734e0a28d8782de404de13362f0b822ea91c11.zip
feat: implement native os module (#1115)
* feat: implement os module * revert(bun-error): package-lock.json * feat: implement EOL * feat: implement EOL * feat: implement arch function * feat: implement homedir function * add comptime to homedir if * feat: add platform function * feat: implement type function * feat: add hostname (not sure iff works on all platforms) * chore: make requested changes * fix: change toValueGC into toValue in arch function * add EOL, devNull constant variables * fix: store only string * feat: add tests * feat: add endianness * ususe toValue for devNull & EOL * feat: implement endianness & fix release * revert javascript.zig fix * feat: implement tmpdir * feat: implement totalmem, freemem, uptime * feat: loadavg * feat: implement version * feat: add userInfo * feat: implement totalmem for macos * feat: add getPriority (not finished, waiting for dc response) * feat: finish userInfo implementation * feat: implement cpus, not done * feat: finish getPriority implementation & stats cpus() * feat: implement setPriority * reemove test.c * feat: implement constants.dlope.xn, constants.priority.x, constants.UV_UDP_REUSEADDR * feat: implement signals, errno * updated cpus function, stilldoesnt work * increase object length * feat: add t more ests * feat: add cpus() * remov some files * fix: handle empty array * started working on interface addresses (in C) ill use C++ after it work * fix interface_addresses * Improved getCpuInfo code, decreased ram usage, increased speed * getCpuTime optimizations * started working on networkInterfaces * feat: networkInterfaces implementation * delete unnecessary files * add benchs & fix code * add tests * add missing benchs * Increased buffer of getCpuInfo to 2048, it should skip few iterations * remove hardcoded constants * feat: implement errno constants * fix getWindowsErrorConstant * feat: add signal constants * feat: implement priority constants * remove duplications * fix: change value to ?i16 * fix(__defineConstant): make some parameters comptime, change value to ?i32 * fix: add E to ERRNO constants, add SIG to SIG constants * feat: add dlopen constants * change functions to private * feat: add UV_UDP_REUSEADDR * fix typo * simplify functions * rename functions in bindings * feat: implement loadavg for darwin * feat: implement system uptime * feat: implement get free memory for darwin * fix system memory bindings * feat: implement network interfaces for darwin * code cleanup * feat: support for macos version & release * some fixes * MacOS support for CpuInfo * Fixed minor mistake in getCpuInfo_B * Delete test.zig * Update c_cpp_properties.json * Update tasks.json * feat: implement scopeid * feat: implement cidr 🚀 * remove unnecesarry changes * remove unnecesarry changes * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * chore: requested changes Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * fix: import string * fix: change IPV6 to IPv6 * fix some things * chore: make requested changes * revert: launch.json * fix test * Fixed few memory leaks * testing * again * added len * another test * improved parsing * added small check * Bonk * FINISH 🚀🚀🚀🚀🚀🚀 * Update tasks.json * NetworkInterface test * FINISH 🚀🚀🚀 Co-authored-by: Fire-The-Fox <gajdos.jan77@gmail.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/api/bun.zig15
-rw-r--r--src/bun.js/bindings/headers-cpp.h25
-rw-r--r--src/bun.js/bindings/headers.h2
-rw-r--r--src/bun.js/bindings/node_os/cpuinfo.cpp222
-rw-r--r--src/bun.js/bindings/node_os/cpuinfo.h23
-rw-r--r--src/bun.js/bindings/node_os/interface_addresses.cpp171
-rw-r--r--src/bun.js/bindings/node_os/interface_addresses.h24
-rw-r--r--src/bun.js/bindings/node_os/system_memory.cpp27
-rw-r--r--src/bun.js/bindings/node_os/system_memory.h10
-rw-r--r--src/bun.js/javascript.zig14
-rw-r--r--src/bun.js/node/node_os.zig419
-rw-r--r--src/bun.js/node/os/constants.zig295
-rw-r--r--src/bun.js/os.exports.js53
13 files changed, 1292 insertions, 8 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index 7c2dd58aa..0c22a5d9b 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -486,6 +486,17 @@ pub fn getImportedStyles(
return JSValue.createStringArray(ctx.ptr(), styles.ptr, styles.len, true).asRef();
}
+pub fn newOs(
+ _: void,
+ ctx: js.JSContextRef,
+ _: js.JSObjectRef,
+ _: js.JSObjectRef,
+ _: []const js.JSValueRef,
+ _: js.ExceptionRef,
+) js.JSValueRef {
+ return Node.Os.create(ctx.ptr()).asObjectRef();
+}
+
pub fn newPath(
_: void,
ctx: js.JSContextRef,
@@ -1032,6 +1043,10 @@ pub const Class = NewClass(
.@"return" = "string[]",
},
},
+ ._Os = .{
+ .rfn = Bun.newOs,
+ .ts = d.ts{},
+ },
._Path = .{
.rfn = Bun.newPath,
.ts = d.ts{},
diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h
index bd0174d42..0be24924b 100644
--- a/src/bun.js/bindings/headers-cpp.h
+++ b/src/bun.js/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1660480861
+//-- AUTOGENERATED FILE -- 1661187638
// clang-format off
#pragma once
@@ -210,27 +210,38 @@ extern "C" const size_t WTF__StringView_object_align_ = alignof(WTF::StringView)
#ifndef INCLUDED__ZigGlobalObject_h_
#define INCLUDED__ZigGlobalObject_h_
-#include "ZigGlobalObject.h"
+#include ""ZigGlobalObject.h""
#endif
extern "C" const size_t Zig__GlobalObject_object_size_ = sizeof(Zig::GlobalObject);
extern "C" const size_t Zig__GlobalObject_object_align_ = alignof(Zig::GlobalObject);
+#ifndef INCLUDED_Path_h
+#define INCLUDED_Path_h
+#include "Path.h"
+#endif
+extern "C" const size_t Bun__Path_object_size_ = sizeof(Bun__Path);
+extern "C" const size_t Bun__Path_object_align_ = alignof(Bun__Path);
#ifndef INCLUDED__ZigConsoleClient_h_
#define INCLUDED__ZigConsoleClient_h_
-#include "ZigConsoleClient.h"
+#include ""ZigConsoleClient.h""
#endif
extern "C" const size_t Zig__ConsoleClient_object_size_ = sizeof(Zig::ConsoleClient);
extern "C" const size_t Zig__ConsoleClient_object_align_ = alignof(Zig::ConsoleClient);
-#include "JSSink.h"
+#ifndef INCLUDED_
+#define INCLUDED_
+#include ""
+#endif
+extern "C" const size_t Bun__Timer_object_size_ = sizeof(Bun__Timer);
+extern "C" const size_t Bun__Timer_object_align_ = alignof(Bun__Timer);
-const size_t sizes[33] = {sizeof(JSC::JSObject), sizeof(WebCore::DOMURL), sizeof(WebCore::FetchHeaders), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView),sizeof(Zig::GlobalObject), sizeof(WebCore::ArrayBufferSink), sizeof(WebCore::HTTPSResponseSink), sizeof(WebCore::HTTPResponseSink)};
+const size_t sizes[33] = {sizeof(JSC::JSObject), sizeof(WebCore::DOMURL), sizeof(WebCore::FetchHeaders), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView), sizeof(FFI__ptr), sizeof(Zig::GlobalObject), sizeof(Bun__Path), sizeof(ArrayBufferSink), sizeof(HTTPSResponseSink), sizeof(HTTPResponseSink)};
-const char* names[33] = {"JSC__JSObject", "WebCore__DOMURL", "WebCore__FetchHeaders", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "Zig__GlobalObject", "WebCore__ArrayBufferSink", "WebCore__HTTPSResponseSink", "WebCore__HTTPResponseSink"};
+const char* names[33] = {"JSC__JSObject", "WebCore__DOMURL", "WebCore__FetchHeaders", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "FFI__ptr", "Zig__GlobalObject", "Bun__Path", "ArrayBufferSink", "HTTPSResponseSink", "HTTPResponseSink"};
-const size_t aligns[33] = {alignof(JSC::JSObject), alignof(WebCore::DOMURL), alignof(WebCore::FetchHeaders), alignof(SystemError), alignof(JSC::JSCell), alignof(JSC::JSString), alignof(Inspector::ScriptArguments), alignof(JSC::JSModuleLoader), alignof(JSC::JSModuleRecord), alignof(JSC::JSPromise), alignof(JSC::JSInternalPromise), alignof(JSC::SourceOrigin), alignof(JSC::SourceCode), alignof(JSC::JSFunction), alignof(JSC::JSGlobalObject), alignof(WTF::URL), alignof(WTF::String), alignof(JSC::JSValue), alignof(JSC::PropertyName), alignof(JSC::Exception), alignof(JSC::VM), alignof(JSC::ThrowScope), alignof(JSC::CatchScope), alignof(JSC::Identifier), alignof(WTF::StringImpl), alignof(WTF::ExternalStringImpl), alignof(WTF::StringView),alignof(Zig::GlobalObject), alignof(WebCore::ArrayBufferSink), alignof(WebCore::HTTPSResponseSink), alignof(WebCore::HTTPResponseSink)};
+const size_t aligns[33] = {alignof(JSC::JSObject), alignof(WebCore::DOMURL), alignof(WebCore::FetchHeaders), alignof(SystemError), alignof(JSC::JSCell), alignof(JSC::JSString), alignof(Inspector::ScriptArguments), alignof(JSC::JSModuleLoader), alignof(JSC::JSModuleRecord), alignof(JSC::JSPromise), alignof(JSC::JSInternalPromise), alignof(JSC::SourceOrigin), alignof(JSC::SourceCode), alignof(JSC::JSFunction), alignof(JSC::JSGlobalObject), alignof(WTF::URL), alignof(WTF::String), alignof(JSC::JSValue), alignof(JSC::PropertyName), alignof(JSC::Exception), alignof(JSC::VM), alignof(JSC::ThrowScope), alignof(JSC::CatchScope), alignof(JSC::Identifier), alignof(WTF::StringImpl), alignof(WTF::ExternalStringImpl), alignof(WTF::StringView), alignof(FFI__ptr), alignof(Zig::GlobalObject), alignof(Bun__Path), alignof(ArrayBufferSink), alignof(HTTPSResponseSink), alignof(HTTPResponseSink)};
diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h
index cf74bd819..657b11b4f 100644
--- a/src/bun.js/bindings/headers.h
+++ b/src/bun.js/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format off
-//-- AUTOGENERATED FILE -- 1661062541
+//-- AUTOGENERATED FILE -- 1661187638
#pragma once
#include <stddef.h>
diff --git a/src/bun.js/bindings/node_os/cpuinfo.cpp b/src/bun.js/bindings/node_os/cpuinfo.cpp
new file mode 100644
index 000000000..f0f8b47eb
--- /dev/null
+++ b/src/bun.js/bindings/node_os/cpuinfo.cpp
@@ -0,0 +1,222 @@
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cctype>
+#include <cstddef>
+#include <unistd.h>
+
+#ifdef __APPLE__
+#include <dlfcn.h>
+#include <mach/mach.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#endif
+
+#include "cpuinfo.h"
+
+#ifdef __linux__
+extern "C" CpuInfo *getCpuInfo()
+{
+ CpuInfo *cores = (CpuInfo*) malloc(sizeof(CpuInfo));
+ FILE *file = fopen("/proc/cpuinfo", "r");
+ if (file == NULL) return NULL;
+
+ char buff[2048];
+ int coresIndex = -1;
+
+ while (fgets(buff, 2048, file)) {
+ if (strlen(buff) == 0) continue;
+
+ short columnSplit = 0;
+ for (int i = 0; i < (int) strlen(buff); i++) {
+ if (buff[i] == ':') {
+ columnSplit = i;
+ break;
+ }
+ }
+ char *columnName = strndup(buff, columnSplit);
+ if (columnName == NULL) return NULL;
+
+ if (!strncmp("processor", columnName, strlen("processor"))) {
+ coresIndex++;
+ if (coresIndex > 0) {
+ cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
+ if (cores == NULL) return NULL;
+ }
+#ifdef __PPC__
+ } else if(!strncmp("cpu", columnName, 3)) {
+#else
+ } else if(!strncmp("model name", columnName, strlen("model name"))) {
+#endif
+ cores[coresIndex].manufacturer = strndup((buff+columnSplit+2), strlen(buff) - 3 - columnSplit);
+ if (cores[coresIndex].manufacturer == NULL) return NULL;
+#ifdef __PPC__
+ } else if(!strncmp("clock", columnName, strlen("clock"))) {
+#else
+ } else if(!strncmp("cpu MHz", columnName, strlen("cpu MHz"))) {
+#endif
+ char *columnData = strndup((buff+columnSplit+2), strlen(buff) - 3 - columnSplit);
+ if (columnData == NULL) return NULL;
+ cores[coresIndex].clockSpeed = atof(columnData);
+ free(columnData);
+ }
+ free(columnName);
+ }
+
+ coresIndex++;
+ cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
+ if (cores == NULL) return NULL;
+ cores[coresIndex] = (CpuInfo) {NULL, 0, 0, 0, 0, 0, 0, 0};
+ fclose(file);
+ return cores;
+}
+#elif __APPLE__
+extern "C" CpuInfo *getCpuInfo()
+{
+ unsigned int ticks = (unsigned int) sysconf(_SC_CLK_TCK), multiplier = ((uint64_t)1000L / ticks);
+ char model[512];
+
+ unsigned int freq;
+ int mib[] = { CTL_HW, HW_CPU_FREQ };
+ size_t freqSize = sizeof(freq);
+ sysctl(mib, 2, &freq, &freqSize, NULL, 0);
+
+ size_t size;
+ unsigned int i;
+ natural_t numcpus;
+ mach_msg_type_number_t msg_type;
+ processor_cpu_load_info_data_t *info;
+ CpuInfo *cores;
+
+ size = sizeof(model);
+ if (sysctlbyname("machdep.cpu.brand_string", model, &size, NULL, 0) &&
+ sysctlbyname("hw.model", model, &size, NULL, 0)) {
+ return NULL;
+ }
+
+ if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus,
+ (processor_info_array_t*) &info,
+ &msg_type) != KERN_SUCCESS) {
+ return NULL;
+ }
+
+ freq = freq / 1000000; // Hz to MHz
+ cores = (CpuInfo*) malloc(sizeof(CpuInfo) * numcpus);
+
+ if (cores == NULL) return NULL;
+
+ for (i = 0; i < numcpus; i++) {
+
+ cores[i].manufacturer = (char*) malloc(strlen(model)+1);
+ if(cores[i].manufacturer == NULL) return NULL;
+ memcpy(cores[i].manufacturer, model, strlen(model));
+ cores[i].manufacturer[strlen(model)] = '\0';
+ cores[i].clockSpeed = freq;
+
+ cores[i].userTime = info[i].cpu_ticks[0] * multiplier;
+ cores[i].niceTime = info[i].cpu_ticks[3] * multiplier;
+ cores[i].systemTime = info[i].cpu_ticks[1] * multiplier;
+ cores[i].idleTime = info[i].cpu_ticks[2] * multiplier;
+ cores[i].iowaitTime = 0;
+ cores[i].irqTime = 0;
+ }
+ cores[numcpus] = (CpuInfo) { NULL, 0, 0, 0, 0, 0, 0, 0 };
+ sysctlbyname("machdep.cpu.brand_string", model, &size, NULL, 0);
+ memcpy(cores[0].manufacturer, model, strlen(model)); // Manufacturer of the first core dissapears for some reason
+ return cores;
+}
+#endif
+
+extern "C" CpuInfo *getCpuTime()
+{
+ CpuInfo *cores = (CpuInfo*) malloc(sizeof(CpuInfo));
+ FILE *file = fopen("/proc/stat", "r");
+ if (file == NULL) return NULL;
+
+ char buff[2048];
+ int coresIndex = -1;
+ int j = 0;
+
+ while (fgets(buff, 2048, file)) {
+ char *name = strndup(buff, 3);
+ if (name == NULL) return NULL;
+ if (!strncmp("cpu", name, 3) && isdigit(buff[3])) {
+ coresIndex++;
+ if (coresIndex > 0) {
+ cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
+ if (cores == NULL) return NULL;
+ }
+ int space;
+ for (int i = 0; i < (int) strlen(buff); i++) {
+ if (buff[i] == ' ') {
+ space = i;
+ break;
+ }
+ }
+ char *cpuDataStart = strndup((buff+space+1), strlen(buff));
+ if (cpuDataStart == NULL) return NULL;
+ char *cpuData = cpuDataStart;
+ // Time to be smart, What I am about to do is dangerous.
+ char *temp = (char*) &cores[coresIndex];
+ size_t start = offsetof(CpuInfo, userTime); // getting offset from `userTime` member.
+ temp = temp + start;
+ j = 0;
+ for (int i = 0; i < 6; i++, j++) {
+ cpuData = (cpuData+j); // offseting string.
+ for (j = 0; cpuData[j] != ' '; j++);
+ char *parseStr = strndup(cpuData, j);
+ if (parseStr == NULL) return NULL;
+ *(int*) temp = atoi(parseStr);
+ free(parseStr);
+ temp = temp + sizeof(int); // switching to next int member.
+ }
+ free(cpuDataStart);
+ }
+ free(name);
+ }
+ coresIndex++;
+ cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
+ if (cores == NULL) return NULL;
+ cores[coresIndex] = (CpuInfo) {NULL, 0, 0, 0, 0, 0, 0, 0};
+ fclose(file);
+ return cores;
+}
+
+extern "C" CpuInfo *getCpuInfoAndTime()
+{
+#ifdef __APPLE__
+ CpuInfo* arr = getCpuInfo();
+ if (arr == NULL) return (CpuInfo*) malloc(sizeof(CpuInfo));
+ return arr;
+#elif __linux__
+ CpuInfo* arr = getCpuInfo();
+ if (arr == NULL) return NULL;
+
+ CpuInfo* arr2 = getCpuTime();
+ if (arr2 == NULL) return NULL;
+
+ for (int i = 0; arr[i].manufacturer; i++) {
+ arr2[i].manufacturer = arr[i].manufacturer;
+ arr2[i].clockSpeed = arr[i].clockSpeed;
+ }
+ free(arr);
+
+ return arr2;
+#endif
+}
+
+extern "C" int getCpuArrayLen(CpuInfo *arr)
+{
+ int i = 0;
+ for (; arr[i].manufacturer; i++);
+ return i-1;
+}
+
+extern "C" void freeCpuInfoArray(CpuInfo *arr, int len)
+{
+ for (int i = 0; i < len; i++) {
+ free(arr[i].manufacturer);
+ }
+
+ free(arr);
+} \ No newline at end of file
diff --git a/src/bun.js/bindings/node_os/cpuinfo.h b/src/bun.js/bindings/node_os/cpuinfo.h
new file mode 100644
index 000000000..e604f0c30
--- /dev/null
+++ b/src/bun.js/bindings/node_os/cpuinfo.h
@@ -0,0 +1,23 @@
+#ifndef CPU_INFO_LIB
+#define CPU_INFO_LIB
+
+extern "C" {
+ typedef struct {
+ char *manufacturer;
+ float clockSpeed;
+ int userTime;
+ int niceTime;
+ int systemTime;
+ int idleTime;
+ int iowaitTime;
+ int irqTime;
+ } CpuInfo;
+
+ CpuInfo *getCpuInfo();
+ CpuInfo *getCpuTime();
+ CpuInfo *getCpuInfoAndTime();
+ int getCpuArrayLen(CpuInfo *arr);
+ void freeCpuInfoArray(CpuInfo *arr, int len);
+}
+
+#endif \ No newline at end of file
diff --git a/src/bun.js/bindings/node_os/interface_addresses.cpp b/src/bun.js/bindings/node_os/interface_addresses.cpp
new file mode 100644
index 000000000..706c43340
--- /dev/null
+++ b/src/bun.js/bindings/node_os/interface_addresses.cpp
@@ -0,0 +1,171 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <ifaddrs.h>
+#include <arpa/inet.h>
+
+#ifdef __linux__
+ #include <netpacket/packet.h>
+ #include <net/ethernet.h>
+#else
+ #include <net/if_dl.h>
+#endif
+
+#include "interface_addresses.h"
+
+extern "C" uint32_t getBitCountFromIPv4Mask(uint32_t mask) {
+ return __builtin_popcount(mask);
+}
+
+extern "C" uint32_t getBitCountFromIPv6Mask(const in6_addr &mask) {
+ uint32_t bitCount = 0;
+
+ for (uint32_t ii = 0; ii < 4; ii++) {
+ bitCount += __builtin_popcount(mask.s6_addr32[ii]);
+ }
+
+ return bitCount;
+}
+
+extern "C" NetworkInterface *getNetworkInterfaces() {
+ NetworkInterface *interfaces = (NetworkInterface*) malloc(sizeof(NetworkInterface));
+ if (interfaces == NULL) return NULL;
+
+ short interfacesIndex = -1;
+ struct ifaddrs *ifap, *ifa;
+ unsigned char *ptr;
+
+ getifaddrs (&ifap);
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET) {
+ struct sockaddr_in *sa = (struct sockaddr_in *) ifa->ifa_addr;
+ char addr[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &(sa->sin_addr), addr, sizeof(addr));
+ char netmask[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &(((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr), netmask, sizeof(netmask));
+ char *interface_name = ifa->ifa_name;
+
+ interfacesIndex++;
+ if (interfacesIndex > 0) {
+ interfaces = (NetworkInterface*) realloc(interfaces, (interfacesIndex+1) * sizeof(NetworkInterface));
+ if (interfaces == NULL) return NULL;
+ }
+
+ interfaces[interfacesIndex].address = strdup(addr);
+ if (interfaces[interfacesIndex].address == NULL) return NULL;
+
+ interfaces[interfacesIndex].netmask = strdup(netmask);
+ if (interfaces[interfacesIndex].netmask == NULL) return NULL;
+
+ interfaces[interfacesIndex].interface = strdup(interface_name);
+ if (interfaces[interfacesIndex].interface == NULL) return NULL;
+
+ interfaces[interfacesIndex].family = (char*) malloc(strlen("IPv4")+1);
+ memcpy(interfaces[interfacesIndex].family, "IPv4", strlen("IPv4"));
+ interfaces[interfacesIndex].family[strlen("IPv4")] = '\0';
+
+ interfaces[interfacesIndex].cidr = getBitCountFromIPv4Mask(((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr.s_addr);
+ interfaces[interfacesIndex].internal = !!(ifa->ifa_flags & 0x8);
+ } else if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET6) {
+ struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ifa->ifa_addr;
+ char addr[INET6_ADDRSTRLEN];
+ inet_ntop(AF_INET6, &(sa->sin6_addr), addr, sizeof(addr));
+ char netmask[INET6_ADDRSTRLEN];
+ inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) ifa->ifa_netmask)->sin6_addr), netmask, sizeof(netmask));
+ char *interface_name = ifa->ifa_name;
+
+ interfacesIndex++;
+ if (interfacesIndex > 0) {
+ interfaces = (NetworkInterface*) realloc(interfaces, (interfacesIndex+1) * sizeof(NetworkInterface));
+ if (interfaces == NULL) return NULL;
+ }
+
+ interfaces[interfacesIndex].address = strdup(addr);
+ if (interfaces[interfacesIndex].address == NULL) return NULL;
+
+ interfaces[interfacesIndex].netmask = strdup(netmask);
+ if (interfaces[interfacesIndex].netmask == NULL) return NULL;
+
+ interfaces[interfacesIndex].interface = interface_name;
+ if (interfaces[interfacesIndex].interface == NULL) return NULL;
+
+ interfaces[interfacesIndex].family = (char*) malloc(strlen("IPv6")+1);
+ memcpy(interfaces[interfacesIndex].family, "IPv6", strlen("IPv6"));
+ interfaces[interfacesIndex].family[strlen("IPv6")] = '\0';
+
+ interfaces[interfacesIndex].cidr = getBitCountFromIPv6Mask(sa->sin6_addr);
+ interfaces[interfacesIndex].scopeid = sa->sin6_scope_id;
+ interfaces[interfacesIndex].internal = !!(ifa->ifa_flags & 0x8);
+ }
+ }
+
+ interfacesIndex++;
+ interfaces = (NetworkInterface*) realloc(interfaces, (interfacesIndex+1) * sizeof(NetworkInterface));
+ if (interfaces == NULL) return NULL;
+ interfaces[interfacesIndex] = (NetworkInterface) {NULL, NULL, NULL, NULL, NULL, 0};
+
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ #ifdef __linux__
+ if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_PACKET) {
+ char macp[INET6_ADDRSTRLEN];
+ struct sockaddr_ll *s = (struct sockaddr_ll *) ifa->ifa_addr;
+ int i;
+ int len = 0;
+
+ for (i = 0; i < 6; i++) {
+ len += sprintf(macp+len, "%02X%s", s->sll_addr[i], i < 5 ? ":":"");
+ }
+
+ i = 0;
+
+ int arrLength = getNetworkInterfaceArrayLen(interfaces);
+
+ for (; i < arrLength; i++) {
+ if (strcmp(interfaces[i].interface, (ifa)->ifa_name) == 0) {
+ interfaces[i].mac = strdup(macp);
+ if (interfaces[i].mac == NULL) return NULL;
+ }
+ }
+ }
+ #else
+ if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_LINK) {
+ char macp[18];
+ ptr = (unsigned char *)LLADDR((struct sockaddr_dl *)(ifa)->ifa_addr);
+ sprintf(macp, "%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));
+
+ int arrLength = getNetworkInterfaceArrayLen(interfaces);
+ for (int i = 0; i < arrLength; i++) {
+ if (strcmp(interfaces[i].interface, (ifa)->ifa_name) == 0) {
+ interfaces[i].mac = strdup(macp);
+ if (interfaces[i].mac == NULL) return NULL;
+ }
+ }
+ }
+ #endif
+ }
+
+ freeifaddrs(ifap);
+
+ return interfaces;
+}
+
+extern "C" int getNetworkInterfaceArrayLen(NetworkInterface *arr) {
+ int i = 0;
+ for (; arr[i].address != NULL; i++);
+ return i;
+}
+
+extern "C" void freeNetworkInterfaceArray(NetworkInterface *arr, int len)
+{
+ for (int i = 0; i < len; i++) {
+ free(arr[i].address);
+ free(arr[i].family);
+ free(arr[i].interface);
+ free(arr[i].mac);
+ free(arr[i].netmask);
+ }
+
+ free(arr);
+} \ No newline at end of file
diff --git a/src/bun.js/bindings/node_os/interface_addresses.h b/src/bun.js/bindings/node_os/interface_addresses.h
new file mode 100644
index 000000000..72b1b31c3
--- /dev/null
+++ b/src/bun.js/bindings/node_os/interface_addresses.h
@@ -0,0 +1,24 @@
+#ifndef INTERFACE_ADDRESSES_LIB
+#define INTERFACE_ADDRESSES_LIB
+
+#include <stdint.h>
+
+extern "C" {
+
+ typedef struct {
+ char *interface;
+ char *address;
+ char *netmask;
+ char *family;
+ char *mac;
+ int cidr;
+ uint32_t scopeid;
+ int internal;
+ } NetworkInterface;
+
+ NetworkInterface *getNetworkInterfaces();
+ int getNetworkInterfaceArrayLen(NetworkInterface *arr);
+ void freeNetworkInterfaceArray(NetworkInterface *arr, int len);
+}
+
+#endif \ No newline at end of file
diff --git a/src/bun.js/bindings/node_os/system_memory.cpp b/src/bun.js/bindings/node_os/system_memory.cpp
new file mode 100644
index 000000000..5bc9c2f12
--- /dev/null
+++ b/src/bun.js/bindings/node_os/system_memory.cpp
@@ -0,0 +1,27 @@
+#include "system_memory.h"
+
+#include <stdint.h>
+
+#if __APPLE__
+#include <mach/mach.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
+
+extern "C" uint64_t getFreeMemoryDarwin_B() {
+ vm_statistics_data_t info;
+ mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
+
+ if (host_statistics(mach_host_self(), HOST_VM_INFO,
+ (host_info_t)&info, &count) != KERN_SUCCESS) {
+ return 0;
+ }
+
+ return (uint64_t) info.free_count * sysconf(_SC_PAGESIZE);
+}
+#else
+// Implemented in zig
+extern "C" uint64_t getFreeMemoryDarwin_B() {
+ return (uint64_t) 0;
+}
+#endif \ No newline at end of file
diff --git a/src/bun.js/bindings/node_os/system_memory.h b/src/bun.js/bindings/node_os/system_memory.h
new file mode 100644
index 000000000..2d794dd0e
--- /dev/null
+++ b/src/bun.js/bindings/node_os/system_memory.h
@@ -0,0 +1,10 @@
+#ifndef SYSTEM_MEMORY_LIB
+#define SYSTEM_MEMORY_LIB
+
+#include <stdint.h>
+
+extern "C" {
+ uint64_t getFreeMemoryDarwin_B();
+}
+
+#endif \ No newline at end of file
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index fc1817b9b..9b7446d9c 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -859,6 +859,15 @@ pub const VirtualMachine = struct {
.tag = ResolvedSource.Tag.@"node:process",
};
},
+ .@"node:os" => {
+ return ResolvedSource{
+ .allocator = null,
+ .source_code = ZigString.init(Node.Os.code),
+ .specifier = ZigString.init("node:os"),
+ .source_url = ZigString.init("node:os"),
+ .hash = 0,
+ };
+ },
.@"bun:ffi" => {
return ResolvedSource{
.allocator = null,
@@ -2791,6 +2800,7 @@ pub const HardcodedModule = enum {
@"node:fs/promises",
@"node:http",
@"node:module",
+ @"node:os",
@"node:stream",
@"node:path",
@"node:perf_hooks",
@@ -2824,6 +2834,7 @@ pub const HardcodedModule = enum {
.{ "node:fs/promises", HardcodedModule.@"node:fs/promises" },
.{ "node:http", HardcodedModule.@"node:http" },
.{ "node:module", HardcodedModule.@"node:module" },
+ .{ "node:os", HardcodedModule.@"node:os" },
.{ "node:path", HardcodedModule.@"node:path" },
.{ "node:path/posix", HardcodedModule.@"node:path" },
.{ "node:path/win32", HardcodedModule.@"node:path" },
@@ -2835,6 +2846,7 @@ pub const HardcodedModule = enum {
.{ "node:timers", HardcodedModule.@"node:timers" },
.{ "node:timers/promises", HardcodedModule.@"node:timers/promises" },
.{ "node:url", HardcodedModule.@"node:url" },
+ .{ "os", HardcodedModule.@"node:os" },
.{ "path", HardcodedModule.@"node:path" },
.{ "process", HardcodedModule.@"node:process" },
.{ "streams", HardcodedModule.@"node:stream" },
@@ -2866,6 +2878,7 @@ pub const HardcodedModule = enum {
.{ "node:fs/promises", "node:fs/promises" },
.{ "node:http", "node:http" },
.{ "node:module", "node:module" },
+ .{ "node:os", "node:os" },
.{ "node:path", "node:path" },
.{ "node:path/posix", "node:path" },
.{ "node:path/win32", "node:path" },
@@ -2877,6 +2890,7 @@ pub const HardcodedModule = enum {
.{ "node:timers", "node:timers" },
.{ "node:timers/promises", "node:timers/promises" },
.{ "node:url", "node:url" },
+ .{ "os", "node:os" },
.{ "path", "node:path" },
.{ "path/posix", "node:path" },
.{ "path/win32", "node:path" },
diff --git a/src/bun.js/node/node_os.zig b/src/bun.js/node/node_os.zig
new file mode 100644
index 000000000..edfd97e77
--- /dev/null
+++ b/src/bun.js/node/node_os.zig
@@ -0,0 +1,419 @@
+const std = @import("std");
+const builtin = @import("builtin");
+const bun = @import("../../global.zig");
+const C = bun.C;
+const string = bun.string;
+const strings = bun.strings;
+const JSC = @import("../../jsc.zig");
+const Environment = bun.Environment;
+const Global = bun.Global;
+const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false;
+const heap_allocator = bun.default_allocator;
+const constants = @import("./os/constants.zig");
+
+// From C ; bindings/node_os/
+pub const struct_InterfaceAddresses = extern struct {
+ interface: [*c]u8,
+ address: [*c]u8,
+ netmask: [*c]u8,
+ family: [*c]u8,
+ mac: [*c]u8,
+ cidr: c_int,
+ scopeid: u32,
+ internal: c_int,
+};
+pub extern fn getNetworkInterfaces() [*c]struct_InterfaceAddresses;
+pub extern fn getNetworkInterfaceArrayLen(arr: [*c]struct_InterfaceAddresses) usize;
+extern fn freeNetworkInterfaceArray(arr: [*c]struct_InterfaceAddresses, len: c_int) void;
+
+pub const struct_CpuInfo = extern struct {
+ manufacturer: [*c]u8,
+ clockSpeed: f32,
+ userTime: c_int,
+ niceTime: c_int,
+ systemTime: c_int,
+ idleTime: c_int,
+ iowaitTime: c_int,
+ irqTime: c_int,
+};
+extern fn getCpuInfo() [*c]struct_CpuInfo;
+extern fn getCpuTime() [*c]struct_CpuInfo;
+extern fn getCpuInfoAndTime() [*c]struct_CpuInfo;
+extern fn getCpuArrayLen(arr: [*c]struct_CpuInfo) usize;
+extern fn freeCpuInfoArray(arr: [*c]struct_CpuInfo, len: c_int) void;
+
+pub const Os = struct {
+ pub const name = "Bun__Os";
+ pub const code = @embedFile("../os.exports.js");
+
+ pub fn create(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue {
+ const module = JSC.JSValue.createEmptyObject(globalObject, 20);
+
+ module.put(globalObject, &JSC.ZigString.init("arch"), JSC.NewFunction(globalObject, &JSC.ZigString.init("arch"), 0, arch));
+ module.put(globalObject, &JSC.ZigString.init("cpus"), JSC.NewFunction(globalObject, &JSC.ZigString.init("cpus"), 0, cpus));
+ module.put(globalObject, &JSC.ZigString.init("endianness"), JSC.NewFunction(globalObject, &JSC.ZigString.init("endianness"), 0, endianness));
+ module.put(globalObject, &JSC.ZigString.init("freemem"), JSC.NewFunction(globalObject, &JSC.ZigString.init("freemem"), 0, freemem));
+ module.put(globalObject, &JSC.ZigString.init("getPriority"), JSC.NewFunction(globalObject, &JSC.ZigString.init("getPriority"), 1, getPriority));
+ module.put(globalObject, &JSC.ZigString.init("homedir"), JSC.NewFunction(globalObject, &JSC.ZigString.init("homedir"), 0, homedir));
+ module.put(globalObject, &JSC.ZigString.init("hostname"), JSC.NewFunction(globalObject, &JSC.ZigString.init("hostname"), 0, hostname));
+ module.put(globalObject, &JSC.ZigString.init("loadavg"), JSC.NewFunction(globalObject, &JSC.ZigString.init("loadavg"), 0, loadavg));
+ module.put(globalObject, &JSC.ZigString.init("networkInterfaces"), JSC.NewFunction(globalObject, &JSC.ZigString.init("networkInterfaces"), 0, networkInterfaces));
+ module.put(globalObject, &JSC.ZigString.init("platform"), JSC.NewFunction(globalObject, &JSC.ZigString.init("platform"), 0, platform));
+ module.put(globalObject, &JSC.ZigString.init("release"), JSC.NewFunction(globalObject, &JSC.ZigString.init("release"), 0, release));
+ module.put(globalObject, &JSC.ZigString.init("setPriority"), JSC.NewFunction(globalObject, &JSC.ZigString.init("setPriority"), 2, setPriority));
+ module.put(globalObject, &JSC.ZigString.init("tmpdir"), JSC.NewFunction(globalObject, &JSC.ZigString.init("tmpdir"), 0, tmpdir));
+ module.put(globalObject, &JSC.ZigString.init("totalmem"), JSC.NewFunction(globalObject, &JSC.ZigString.init("totalmem"), 0, @"totalmem"));
+ module.put(globalObject, &JSC.ZigString.init("type"), JSC.NewFunction(globalObject, &JSC.ZigString.init("type"), 0, @"type"));
+ module.put(globalObject, &JSC.ZigString.init("uptime"), JSC.NewFunction(globalObject, &JSC.ZigString.init("uptime"), 0, uptime));
+ module.put(globalObject, &JSC.ZigString.init("userInfo"), JSC.NewFunction(globalObject, &JSC.ZigString.init("userInfo"), 0, userInfo));
+ module.put(globalObject, &JSC.ZigString.init("version"), JSC.NewFunction(globalObject, &JSC.ZigString.init("version"), 0, version));
+
+ module.put(globalObject, &JSC.ZigString.init("devNull"), JSC.ZigString.init(devNull).withEncoding().toValue(globalObject));
+ module.put(globalObject, &JSC.ZigString.init("EOL"), JSC.ZigString.init(EOL).withEncoding().toValue(globalObject));
+
+ module.put(globalObject, &JSC.ZigString.init("constants"), constants.create(globalObject));
+
+ return module;
+ }
+
+ pub const EOL = if (Environment.isWindows) "\\r\\n" else "\\n";
+ pub const devNull = if (Environment.isWindows) "\\\\.\nul" else "/dev/null";
+
+ pub fn arch(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.ZigString.init(Global.arch_name).withEncoding().toValue(globalThis);
+ }
+
+ pub fn cpus(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ const cpus_ = getCpuInfoAndTime();
+ if (cpus_ == null) return JSC.JSArray.from(globalThis, &.{});
+
+ const len = getCpuArrayLen(cpus_);
+ const arr = cpus_[0..len];
+
+ var buf: [256]JSC.JSValue = undefined;
+ var result = std.ArrayListUnmanaged(JSC.JSValue){ .capacity = buf.len, .items = buf[0..1] };
+ result.items.len = 0;
+
+ for (arr) |_, index| {
+ var object = JSC.JSValue.createEmptyObject(globalThis, 3);
+ var timesObject = JSC.JSValue.createEmptyObject(globalThis, 5);
+
+ timesObject.put(globalThis, &JSC.ZigString.init("user"), JSC.JSValue.jsNumber(arr[index].userTime));
+ timesObject.put(globalThis, &JSC.ZigString.init("nice"), JSC.JSValue.jsNumber(arr[index].niceTime));
+ timesObject.put(globalThis, &JSC.ZigString.init("sys"), JSC.JSValue.jsNumber(arr[index].systemTime));
+ timesObject.put(globalThis, &JSC.ZigString.init("idle"), JSC.JSValue.jsNumber(arr[index].idleTime));
+ timesObject.put(globalThis, &JSC.ZigString.init("irq"), JSC.JSValue.jsNumber(arr[index].irqTime));
+
+ object.put(globalThis, &JSC.ZigString.init("model"), JSC.ZigString.init(std.mem.span(arr[index].manufacturer)).withEncoding().toValueGC(globalThis));
+ object.put(globalThis, &JSC.ZigString.init("speed"), JSC.JSValue.jsNumber(@floatToInt(i32, arr[index].clockSpeed)));
+ object.put(globalThis, &JSC.ZigString.init("times"), timesObject);
+
+ _ = result.appendAssumeCapacity(object);
+ }
+
+ freeCpuInfoArray(cpus_, @intCast(c_int, len));
+ heap_allocator.free(arr);
+ return JSC.JSArray.from(globalThis, result.toOwnedSlice(heap_allocator));
+ }
+
+ pub fn endianness(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ switch (comptime builtin.target.cpu.arch.endian()) {
+ .Big => {
+ return JSC.ZigString.init("BE").withEncoding().toValue(globalThis);
+ },
+ .Little => {
+ return JSC.ZigString.init("LE").withEncoding().toValue(globalThis);
+ },
+ }
+ }
+
+ pub fn freemem(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.JSValue.jsNumberFromUint64(C.getFreeMemory());
+ }
+
+ pub fn getPriority(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ var args_ = callframe.arguments(1);
+ var arguments: []const JSC.JSValue = args_.ptr[0..args_.len];
+
+ if (arguments.len > 0 and !arguments[0].isNumber()) {
+ const err = JSC.toTypeError(
+ JSC.Node.ErrorCode.ERR_INVALID_ARG_TYPE,
+ "getPriority() expects a number",
+ .{},
+ globalThis,
+ );
+ globalThis.vm().throwError(globalThis, err);
+ return JSC.JSValue.jsUndefined();
+ }
+
+ var pid = if (arguments.len > 0) arguments[0].asInt32() else 0;
+
+ const priority = C.getProcessPriority(pid);
+ if (priority == -1) {
+ //const info = JSC.JSValue.createEmptyObject(globalThis, 4);
+ //info.put(globalThis, &JSC.ZigString.init("errno"), JSC.JSValue.jsNumberFromInt32(-3));
+ //info.put(globalThis, &JSC.ZigString.init("code"), JSC.ZigString.init("ESRCH").withEncoding().toValueGC(globalThis));
+ //info.put(globalThis, &JSC.ZigString.init("message"), JSC.ZigString.init("no such process").withEncoding().toValueGC(globalThis));
+ //info.put(globalThis, &JSC.ZigString.init("syscall"), JSC.ZigString.init("uv_os_getpriority").withEncoding().toValueGC(globalThis));
+
+ const err = JSC.SystemError{
+ .message = JSC.ZigString.init("A system error occurred: uv_os_getpriority returned ESRCH (no such process)"),
+ .code = JSC.ZigString.init(@as(string, @tagName(JSC.Node.ErrorCode.ERR_SYSTEM_ERROR))),
+ //.info = info,
+ .errno = -3,
+ .syscall = JSC.ZigString.init("uv_os_getpriority"),
+ };
+
+ globalThis.vm().throwError(globalThis, err.toErrorInstance(globalThis));
+ return JSC.JSValue.jsUndefined();
+ }
+
+ return JSC.JSValue.jsNumberFromInt32(priority);
+ }
+
+ pub fn homedir(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ var dir: string = "unknown";
+ if (comptime Environment.isWindows)
+ dir = std.os.getenv("USERPROFILE") orelse "unknown"
+ else
+ dir = std.os.getenv("HOME") orelse "unknown";
+
+ return JSC.ZigString.init(dir).withEncoding().toValueGC(globalThis);
+ }
+
+ pub fn hostname(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ var name_buffer: [std.os.HOST_NAME_MAX]u8 = undefined;
+
+ return JSC.ZigString.init(std.os.gethostname(&name_buffer) catch "unknown").withEncoding().toValueGC(globalThis);
+ }
+
+ pub fn loadavg(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ const result = C.getSystemLoadavg();
+ return JSC.JSArray.from(globalThis, &.{
+ JSC.JSValue.jsDoubleNumber(result[0]),
+ JSC.JSValue.jsDoubleNumber(result[1]),
+ JSC.JSValue.jsDoubleNumber(result[2]),
+ });
+ }
+
+ pub fn networkInterfaces(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ const networkInterfaces_ = getNetworkInterfaces();
+ if (networkInterfaces_ == null) return JSC.JSValue.createEmptyObject(globalThis, 0);
+
+ const len = getNetworkInterfaceArrayLen(networkInterfaces_);
+ const arr = networkInterfaces_[0..len];
+
+ const object = JSC.JSValue.createEmptyObject(globalThis, 0);
+ var map = std.StringArrayHashMap(std.ArrayList(JSC.JSValue)).init(heap_allocator);
+ _ = map.ensureUnusedCapacity(len) catch unreachable;
+
+ defer map.deinit();
+
+ for (arr) |part| {
+ const interface = std.mem.span(part.interface);
+ const family = std.mem.span(part.family);
+ const netmask = std.mem.span(part.netmask);
+ const cidr = std.fmt.allocPrint(heap_allocator, "{s}/{}", .{ netmask, part.cidr }) catch unreachable;
+
+ var list = map.get(interface) orelse std.ArrayList(JSC.JSValue).init(heap_allocator);
+ var obj = JSC.JSValue.createEmptyObject(globalThis, if (strings.eqlComptime(family, "IPv6")) 7 else 6);
+ obj.put(globalThis, &JSC.ZigString.init("address"), JSC.ZigString.init(std.mem.span(part.address)).withEncoding().toValueGC(globalThis));
+ obj.put(globalThis, &JSC.ZigString.init("netmask"), JSC.ZigString.init(netmask).withEncoding().toValueGC(globalThis));
+ obj.put(globalThis, &JSC.ZigString.init("family"), JSC.ZigString.init(family).withEncoding().toValueGC(globalThis));
+ obj.put(globalThis, &JSC.ZigString.init("mac"), JSC.ZigString.init(std.mem.span(part.mac)).withEncoding().toValueGC(globalThis));
+ obj.put(globalThis, &JSC.ZigString.init("cidr"), JSC.ZigString.init(cidr).withEncoding().toValueGC(globalThis));
+ if (strings.eqlComptime(family, "IPv6")) obj.put(globalThis, &JSC.ZigString.init("scopeid"), JSC.JSValue.jsNumber(part.scopeid));
+ obj.put(globalThis, &JSC.ZigString.init("internal"), JSC.JSValue.jsBoolean(if (part.internal == 0) true else false));
+
+ _ = list.append(obj) catch unreachable;
+ _ = map.put(interface, list) catch unreachable;
+ }
+
+ for (map.keys()) |key| {
+ var value = map.get(key);
+
+ object.put(globalThis, &JSC.ZigString.init(key), JSC.JSArray.from(globalThis, value.?.toOwnedSlice()));
+ }
+
+ freeNetworkInterfaceArray(networkInterfaces_, @intCast(c_int, len));
+ heap_allocator.free(arr);
+ return object;
+ }
+
+ pub fn platform(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.ZigString.init(Global.os_name).withEncoding().toValueGC(globalThis);
+ }
+
+ pub fn release(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.ZigString.init(C.getRelease()).withEncoding().toValueGC(globalThis);
+ }
+
+ pub fn setPriority(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ var args_ = callframe.arguments(2);
+ var arguments: []const JSC.JSValue = args_.ptr[0..args_.len];
+
+ if (arguments.len == 0) {
+ const err = JSC.toTypeError(
+ JSC.Node.ErrorCode.ERR_INVALID_ARG_TYPE,
+ "The \"priority\" argument must be of type number. Received undefined",
+ .{},
+ globalThis,
+ );
+ globalThis.vm().throwError(globalThis, err);
+ return JSC.JSValue.jsUndefined();
+ }
+
+ const pid = if (arguments.len == 2) arguments[0].toInt32() else 0;
+ const priority = if (arguments.len == 2) arguments[1].toInt32() else arguments[0].toInt32();
+
+ if (priority < -20 or priority > 19) {
+ const err = JSC.toTypeError(
+ JSC.Node.ErrorCode.ERR_OUT_OF_RANGE,
+ "The value of \"priority\" is out of range. It must be >= -20 && <= 19",
+ .{},
+ globalThis,
+ );
+ globalThis.vm().throwError(globalThis, err);
+ return JSC.JSValue.jsUndefined();
+ }
+
+ const errcode = C.setProcessPriority(pid, priority);
+ switch (errcode) {
+ .SRCH => {
+ const err = JSC.SystemError{
+ .message = JSC.ZigString.init("A system error occurred: uv_os_setpriority returned ESRCH (no such process)"),
+ .code = JSC.ZigString.init(@as(string, @tagName(JSC.Node.ErrorCode.ERR_SYSTEM_ERROR))),
+ //.info = info,
+ .errno = -3,
+ .syscall = JSC.ZigString.init("uv_os_setpriority"),
+ };
+
+ globalThis.vm().throwError(globalThis, err.toErrorInstance(globalThis));
+ return JSC.JSValue.jsUndefined();
+ },
+ .ACCES => {
+ const err = JSC.SystemError{
+ .message = JSC.ZigString.init("A system error occurred: uv_os_setpriority returned EACCESS (permission denied)"),
+ .code = JSC.ZigString.init(@as(string, @tagName(JSC.Node.ErrorCode.ERR_SYSTEM_ERROR))),
+ //.info = info,
+ .errno = -13,
+ .syscall = JSC.ZigString.init("uv_os_setpriority"),
+ };
+
+ globalThis.vm().throwError(globalThis, err.toErrorInstance(globalThis));
+ return JSC.JSValue.jsUndefined();
+ },
+ else => {},
+ }
+
+ return JSC.JSValue.jsUndefined();
+ }
+
+ pub fn tmpdir(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ var dir: string = "unknown";
+ if (comptime Environment.isWindows) {
+ if (std.os.getenv("TEMP") orelse std.os.getenv("TMP")) |tmpdir_| {
+ dir = tmpdir_;
+ }
+
+ if (std.os.getenv("SYSTEMROOT") orelse std.os.getenv("WINDIR")) |systemdir_| {
+ dir = systemdir_ + "\\temp";
+ }
+
+ dir = "unknown";
+ } else {
+ dir = std.os.getenv("TMPDIR") orelse std.os.getenv("TMP") orelse std.os.getenv("TEMP") orelse "/tmp";
+ }
+
+ return JSC.ZigString.init(dir).withEncoding().toValueGC(globalThis);
+ }
+
+ pub fn totalmem(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.JSValue.jsNumberFromUint64(C.getTotalMemory());
+ }
+
+ pub fn @"type"(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ if (comptime Environment.isWindows)
+ return JSC.ZigString.init("Windows_NT").withEncoding().toValueGC(globalThis)
+ else if (comptime Environment.isMac)
+ return JSC.ZigString.init("Darwin").withEncoding().toValueGC(globalThis)
+ else if (comptime Environment.isLinux)
+ return JSC.ZigString.init("Linux").withEncoding().toValueGC(globalThis);
+
+ return JSC.ZigString.init(Global.os_name).withEncoding().toValueGC(globalThis);
+ }
+
+ pub fn uptime(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.JSValue.jsNumberFromUint64(C.getSystemUptime());
+ }
+
+ pub fn userInfo(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue {
+ const result = JSC.JSValue.createEmptyObject(globalThis, 5);
+
+ result.put(globalThis, &JSC.ZigString.init("homedir"), homedir(globalThis, callframe));
+
+ if (comptime Environment.isWindows) {
+ result.put(globalThis, &JSC.ZigString.init("username"), JSC.ZigString.init(std.os.getenv("USERNAME") orelse "unknown").withEncoding().toValueGC(globalThis));
+ result.put(globalThis, &JSC.ZigString.init("uid"), JSC.JSValue.jsNumber(-1));
+ result.put(globalThis, &JSC.ZigString.init("gid"), JSC.JSValue.jsNumber(-1));
+ result.put(globalThis, &JSC.ZigString.init("shell"), JSC.JSValue.jsNull());
+ } else {
+ const username = std.os.getenv("USER") orelse "unknown";
+
+ result.put(globalThis, &JSC.ZigString.init("username"), JSC.ZigString.init(username).withEncoding().toValueGC(globalThis));
+ result.put(globalThis, &JSC.ZigString.init("shell"), JSC.ZigString.init(std.os.getenv("SHELL") orelse "unknown").withEncoding().toValueGC(globalThis));
+
+ if (comptime Environment.isLinux) {
+ result.put(globalThis, &JSC.ZigString.init("uid"), JSC.JSValue.jsNumber(std.os.linux.getuid()));
+ result.put(globalThis, &JSC.ZigString.init("gid"), JSC.JSValue.jsNumber(std.os.linux.getgid()));
+ } else {
+ result.put(globalThis, &JSC.ZigString.init("uid"), JSC.JSValue.jsNumber(C.darwin.getuid()));
+ result.put(globalThis, &JSC.ZigString.init("gid"), JSC.JSValue.jsNumber(C.darwin.getgid()));
+ }
+ }
+
+ return result;
+ }
+
+ pub fn version(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
+ if (comptime is_bindgen) return JSC.JSValue.jsUndefined();
+
+ return JSC.ZigString.init(C.getVersion()).withEncoding().toValueGC(globalThis);
+ }
+};
+
+comptime {
+ std.testing.refAllDecls(Os);
+}
diff --git a/src/bun.js/node/os/constants.zig b/src/bun.js/node/os/constants.zig
new file mode 100644
index 000000000..809750dea
--- /dev/null
+++ b/src/bun.js/node/os/constants.zig
@@ -0,0 +1,295 @@
+const std = @import("std");
+const bun = @import("../../../global.zig");
+const string = bun.string;
+const Environment = bun.Environment;
+const JSC = @import("../../../jsc.zig");
+
+const ConstantType = enum { ERRNO, ERRNO_WIN, SIG, DLOPEN, OTHER };
+
+fn getErrnoConstant(comptime name: []const u8) comptime_int {
+ return if (@hasField(std.os.E, name))
+ return @enumToInt(@field(std.os.E, name))
+ else
+ return -1;
+}
+
+fn getWindowsErrnoConstant(comptime name: []const u8) comptime_int {
+ return if (@hasField(std.os.E, name))
+ return @enumToInt(@field(std.os.windows.ws2_32.WinsockError, name))
+ else
+ return -1;
+}
+
+fn getSignalsConstant(comptime name: []const u8) comptime_int {
+ return if (@hasDecl(std.os.SIG, name))
+ return @field(std.os.SIG, name)
+ else
+ return -1;
+}
+
+fn getDlopenConstant(comptime name: []const u8) comptime_int {
+ return if (@hasDecl(std.os.system.RTLD, name))
+ return @field(std.os.system.RTLD, name)
+ else
+ return -1;
+}
+
+fn defineConstant(globalObject: *JSC.JSGlobalObject, object: JSC.JSValue, comptime ctype: ConstantType, comptime name: string) void {
+ return __defineConstant(globalObject, object, ctype, name, null);
+}
+
+fn __defineConstant(globalObject: *JSC.JSGlobalObject, object: JSC.JSValue, comptime ctype: ConstantType, comptime name: string, comptime value: ?i32) void {
+ switch (ctype) {
+ .ERRNO => {
+ const constant = getErrnoConstant(name);
+ if (comptime constant != -1)
+ object.put(globalObject, &JSC.ZigString.init("E" ++ name), JSC.JSValue.jsNumber(constant));
+ },
+ .ERRNO_WIN => {
+ const constant = getWindowsErrnoConstant(name);
+ if (comptime constant != -1)
+ object.put(globalObject, &JSC.ZigString.init(name), JSC.JSValue.jsNumber(constant));
+ },
+ .SIG => {
+ const constant = getSignalsConstant(name);
+ if (comptime constant != -1)
+ object.put(globalObject, &JSC.ZigString.init("SIG" ++ name), JSC.JSValue.jsNumber(constant));
+ },
+ .DLOPEN => {
+ const constant = getDlopenConstant(name);
+ if (comptime constant != -1)
+ object.put(globalObject, &JSC.ZigString.init("RTLD_" ++ name), JSC.JSValue.jsNumber(constant));
+ },
+ .OTHER => {
+ object.put(globalObject, &JSC.ZigString.init(name), JSC.JSValue.jsNumberFromInt32(value.?));
+ },
+ }
+}
+
+pub fn create(globalObject: *JSC.JSGlobalObject) JSC.JSValue {
+ const object = JSC.JSValue.createEmptyObject(globalObject, 4);
+
+ object.put(globalObject, &JSC.ZigString.init("errno"), createErrno(globalObject));
+ object.put(globalObject, &JSC.ZigString.init("signals"), createSignals(globalObject));
+ object.put(globalObject, &JSC.ZigString.init("priority"), createPriority(globalObject));
+ object.put(globalObject, &JSC.ZigString.init("dlopen"), createDlopen(globalObject));
+ __defineConstant(globalObject, object, .OTHER, "UV_UDP_REUSEADDR", 4);
+
+ return object;
+}
+
+fn createErrno(globalObject: *JSC.JSGlobalObject) JSC.JSValue {
+ const object = JSC.JSValue.createEmptyObject(globalObject, 0);
+
+ defineConstant(globalObject, object, .ERRNO, "2BIG");
+ defineConstant(globalObject, object, .ERRNO, "ACCES");
+ defineConstant(globalObject, object, .ERRNO, "ADDRINUSE");
+ defineConstant(globalObject, object, .ERRNO, "ADDRNOTAVAIL");
+ defineConstant(globalObject, object, .ERRNO, "AFNOSUPPORT");
+ defineConstant(globalObject, object, .ERRNO, "AGAIN");
+ defineConstant(globalObject, object, .ERRNO, "ALREADY");
+ defineConstant(globalObject, object, .ERRNO, "BADF");
+ defineConstant(globalObject, object, .ERRNO, "BADMSG");
+ defineConstant(globalObject, object, .ERRNO, "BUSY");
+ defineConstant(globalObject, object, .ERRNO, "CANCELED");
+ defineConstant(globalObject, object, .ERRNO, "CHILD");
+ defineConstant(globalObject, object, .ERRNO, "CONNABORTED");
+ defineConstant(globalObject, object, .ERRNO, "CONNREFUSED");
+ defineConstant(globalObject, object, .ERRNO, "CONNRESET");
+ defineConstant(globalObject, object, .ERRNO, "DEADLK");
+ defineConstant(globalObject, object, .ERRNO, "DESTADDRREQ");
+ defineConstant(globalObject, object, .ERRNO, "DOM");
+ defineConstant(globalObject, object, .ERRNO, "DQUOT");
+ defineConstant(globalObject, object, .ERRNO, "EXIST");
+ defineConstant(globalObject, object, .ERRNO, "FAULT");
+ defineConstant(globalObject, object, .ERRNO, "FBIG");
+ defineConstant(globalObject, object, .ERRNO, "HOSTUNREACH");
+ defineConstant(globalObject, object, .ERRNO, "IDRM");
+ defineConstant(globalObject, object, .ERRNO, "ILSEQ");
+ defineConstant(globalObject, object, .ERRNO, "INPROGRESS");
+ defineConstant(globalObject, object, .ERRNO, "INTR");
+ defineConstant(globalObject, object, .ERRNO, "INVAL");
+ defineConstant(globalObject, object, .ERRNO, "IO");
+ defineConstant(globalObject, object, .ERRNO, "ISCONN");
+ defineConstant(globalObject, object, .ERRNO, "ISDIR");
+ defineConstant(globalObject, object, .ERRNO, "LOOP");
+ defineConstant(globalObject, object, .ERRNO, "MFILE");
+ defineConstant(globalObject, object, .ERRNO, "MLINK");
+ defineConstant(globalObject, object, .ERRNO, "MSGSIZE");
+ defineConstant(globalObject, object, .ERRNO, "MULTIHOP");
+ defineConstant(globalObject, object, .ERRNO, "NAMETOOLONG");
+ defineConstant(globalObject, object, .ERRNO, "NETDOWN");
+ defineConstant(globalObject, object, .ERRNO, "NETRESET");
+ defineConstant(globalObject, object, .ERRNO, "NETUNREACH");
+ defineConstant(globalObject, object, .ERRNO, "NFILE");
+ defineConstant(globalObject, object, .ERRNO, "NOBUFS");
+ defineConstant(globalObject, object, .ERRNO, "NODATA");
+ defineConstant(globalObject, object, .ERRNO, "NODEV");
+ defineConstant(globalObject, object, .ERRNO, "NOENT");
+ defineConstant(globalObject, object, .ERRNO, "NOEXEC");
+ defineConstant(globalObject, object, .ERRNO, "NOLCK");
+ defineConstant(globalObject, object, .ERRNO, "NOLINK");
+ defineConstant(globalObject, object, .ERRNO, "NOMEM");
+ defineConstant(globalObject, object, .ERRNO, "NOMSG");
+ defineConstant(globalObject, object, .ERRNO, "NOPROTOOPT");
+ defineConstant(globalObject, object, .ERRNO, "NOSPC");
+ defineConstant(globalObject, object, .ERRNO, "NOSR");
+ defineConstant(globalObject, object, .ERRNO, "NOSTR");
+ defineConstant(globalObject, object, .ERRNO, "NOSYS");
+ defineConstant(globalObject, object, .ERRNO, "NOTCONN");
+ defineConstant(globalObject, object, .ERRNO, "NOTDIR");
+ defineConstant(globalObject, object, .ERRNO, "NOTEMPTY");
+ defineConstant(globalObject, object, .ERRNO, "NOTSOCK");
+ defineConstant(globalObject, object, .ERRNO, "NOTSUP");
+ defineConstant(globalObject, object, .ERRNO, "NOTTY");
+ defineConstant(globalObject, object, .ERRNO, "NXIO");
+ defineConstant(globalObject, object, .ERRNO, "OPNOTSUPP");
+ defineConstant(globalObject, object, .ERRNO, "OVERFLOW");
+ defineConstant(globalObject, object, .ERRNO, "PERM");
+ defineConstant(globalObject, object, .ERRNO, "PIPE");
+ defineConstant(globalObject, object, .ERRNO, "PROTO");
+ defineConstant(globalObject, object, .ERRNO, "PROTONOSUPPORT");
+ defineConstant(globalObject, object, .ERRNO, "PROTOTYPE");
+ defineConstant(globalObject, object, .ERRNO, "RANGE");
+ defineConstant(globalObject, object, .ERRNO, "ROFS");
+ defineConstant(globalObject, object, .ERRNO, "SPIPE");
+ defineConstant(globalObject, object, .ERRNO, "SRCH");
+ defineConstant(globalObject, object, .ERRNO, "STALE");
+ defineConstant(globalObject, object, .ERRNO, "TIME");
+ defineConstant(globalObject, object, .ERRNO, "TIMEDOUT");
+ defineConstant(globalObject, object, .ERRNO, "TXTBSY");
+ defineConstant(globalObject, object, .ERRNO, "WOULDBLOCK");
+ defineConstant(globalObject, object, .ERRNO, "XDEV");
+
+ if (comptime Environment.isWindows) {
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEINTR");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEBADF");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEACCES");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEFAULT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEINVAL");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEMFILE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEWOULDBLOCK");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEINPROGRESS");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEALREADY");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENOTSOCK");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEDESTADDRREQ");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEMSGSIZE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEPROTOTYPE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENOPROTOOPT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEPROTONOSUPPORT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAESOCKTNOSUPPORT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEOPNOTSUPP");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEPFNOSUPPORT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEAFNOSUPPORT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEADDRINUSE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEADDRNOTAVAIL");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENETDOWN");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENETUNREACH");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENETRESET");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAECONNABORTED");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAECONNRESET");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENOBUFS");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEISCONN");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENOTCONN");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAESHUTDOWN");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAETOOMANYREFS");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAETIMEDOUT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAECONNREFUSED");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAELOOP");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENAMETOOLONG");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEHOSTDOWN");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEHOSTUNREACH");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENOTEMPTY");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEPROCLIM");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEUSERS");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEDQUOT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAESTALE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEREMOTE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSASYSNOTREADY");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAVERNOTSUPPORTED");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSANOTINITIALISED");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEDISCON");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAENOMORE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAECANCELLED");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEINVALIDPROCTABLE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEINVALIDPROVIDER");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEPROVIDERFAILEDINIT");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSASYSCALLFAILURE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSASERVICE_NOT_FOUND");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSATYPE_NOT_FOUND");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSA_E_NO_MORE");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSA_E_CANCELLED");
+ defineConstant(globalObject, object, .ERRNO_WIN, "WSAEREFUSED");
+ }
+
+ return object;
+}
+
+fn createSignals(globalObject: *JSC.JSGlobalObject) JSC.JSValue {
+ const object = JSC.JSValue.createEmptyObject(globalObject, 0);
+
+ defineConstant(globalObject, object, .SIG, "HUP");
+ defineConstant(globalObject, object, .SIG, "INT");
+ defineConstant(globalObject, object, .SIG, "QUIT");
+ defineConstant(globalObject, object, .SIG, "ILL");
+ defineConstant(globalObject, object, .SIG, "TRAP");
+ defineConstant(globalObject, object, .SIG, "ABRT");
+ defineConstant(globalObject, object, .SIG, "IOT");
+ defineConstant(globalObject, object, .SIG, "BUS");
+ defineConstant(globalObject, object, .SIG, "FPE");
+ defineConstant(globalObject, object, .SIG, "KILL");
+ defineConstant(globalObject, object, .SIG, "USR1");
+ defineConstant(globalObject, object, .SIG, "SEGV");
+ defineConstant(globalObject, object, .SIG, "USR2");
+ defineConstant(globalObject, object, .SIG, "PIPE");
+ defineConstant(globalObject, object, .SIG, "ALRM");
+ defineConstant(globalObject, object, .SIG, "TERM");
+ defineConstant(globalObject, object, .SIG, "CHLD");
+ defineConstant(globalObject, object, .SIG, "STKFLT");
+ defineConstant(globalObject, object, .SIG, "CONT");
+ defineConstant(globalObject, object, .SIG, "STOP");
+ defineConstant(globalObject, object, .SIG, "TSTP");
+ defineConstant(globalObject, object, .SIG, "BREAK");
+ defineConstant(globalObject, object, .SIG, "TTIN");
+ defineConstant(globalObject, object, .SIG, "TTOU");
+ defineConstant(globalObject, object, .SIG, "URG");
+ defineConstant(globalObject, object, .SIG, "XCPU");
+ defineConstant(globalObject, object, .SIG, "XFSZ");
+ defineConstant(globalObject, object, .SIG, "VTALRM");
+ defineConstant(globalObject, object, .SIG, "PROF");
+ defineConstant(globalObject, object, .SIG, "WINCH");
+ defineConstant(globalObject, object, .SIG, "IO");
+ defineConstant(globalObject, object, .SIG, "POLL");
+ defineConstant(globalObject, object, .SIG, "LOST");
+ defineConstant(globalObject, object, .SIG, "PWR");
+ defineConstant(globalObject, object, .SIG, "INFO");
+ defineConstant(globalObject, object, .SIG, "SYS");
+ defineConstant(globalObject, object, .SIG, "UNUSED");
+
+ return object;
+}
+
+fn createPriority(globalObject: *JSC.JSGlobalObject) JSC.JSValue {
+ const object = JSC.JSValue.createEmptyObject(globalObject, 6);
+
+ __defineConstant(globalObject, object, .OTHER, "PRIORITY_LOW", 19);
+ __defineConstant(globalObject, object, .OTHER, "PRIORITY_BELOW_NORMAL", 10);
+ __defineConstant(globalObject, object, .OTHER, "PRIORITY_NORMAL", 0);
+ __defineConstant(globalObject, object, .OTHER, "PRIORITY_ABOVE_NORMAL", -7);
+ __defineConstant(globalObject, object, .OTHER, "PRIORITY_HIGH", -14);
+ __defineConstant(globalObject, object, .OTHER, "PRIORITY_HIGHEST", -20);
+
+ return object;
+}
+
+fn createDlopen(globalObject: *JSC.JSGlobalObject) JSC.JSValue {
+ const object = JSC.JSValue.createEmptyObject(globalObject, 6);
+
+ defineConstant(globalObject, object, .DLOPEN, "LAZY");
+ defineConstant(globalObject, object, .DLOPEN, "NOW");
+ defineConstant(globalObject, object, .DLOPEN, "GLOBAL");
+ defineConstant(globalObject, object, .DLOPEN, "LOCAL");
+ defineConstant(globalObject, object, .DLOPEN, "DEEPBIND");
+
+ return object;
+}
diff --git a/src/bun.js/os.exports.js b/src/bun.js/os.exports.js
new file mode 100644
index 000000000..60eacab55
--- /dev/null
+++ b/src/bun.js/os.exports.js
@@ -0,0 +1,53 @@
+function bound(obj) {
+ return {
+ arch: obj.arch.bind(obj),
+ cpus: obj.cpus.bind(obj),
+ endianness: obj.endianness.bind(obj),
+ freemem: obj.freemem.bind(obj),
+ getPriority: obj.getPriority.bind(obj),
+ homedir: obj.homedir.bind(obj),
+ hostname: obj.hostname.bind(obj),
+ loadavg: obj.loadavg.bind(obj),
+ networkInterfaces: obj.networkInterfaces.bind(obj),
+ platform: obj.platform.bind(obj),
+ release: obj.release.bind(obj),
+ setPriority: obj.setPriority.bind(obj),
+ tmpdir: obj.tmpdir.bind(obj),
+ totalmem: obj.totalmem.bind(obj),
+ type: obj.type.bind(obj),
+ uptime: obj.uptime.bind(obj),
+ userInfo: obj.userInfo.bind(obj),
+ version: obj.version.bind(obj),
+ devNull: obj.devNull,
+ EOL: obj.EOL,
+ constants: obj.constants,
+ };
+}
+
+var os = bound(Bun._Os());
+
+export var {
+ arch,
+ cpus,
+ endianness,
+ freemem,
+ getPriority,
+ homedir,
+ hostname,
+ loadavg,
+ networkInterfaces,
+ platform,
+ release,
+ setPriority,
+ tmpdir,
+ totalmem,
+ type,
+ uptime,
+ userInfo,
+ version,
+ devNull,
+ EOL,
+ constants,
+} = os;
+
+export default os;