aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/BunString.cpp23
-rw-r--r--src/bun.js/bindings/CommonJSModuleRecord.cpp6
-rw-r--r--src/bun.js/bindings/Process.cpp1324
-rw-r--r--src/bun.js/bindings/Process.h20
-rw-r--r--src/bun.js/bindings/Process.lut.h211
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp6
-rw-r--r--src/bun.js/bindings/webcore/DOMClientIsoSubspaces.h1
-rw-r--r--src/bun.js/bindings/webcore/DOMIsoSubspaces.h1
-rw-r--r--src/bun.js/modules/ProcessModule.h8
-rw-r--r--src/bun.js/node/types.zig30
-rw-r--r--src/bun.js/rare_data.zig8
-rw-r--r--src/bun.js/uuid.zig10
-rw-r--r--src/bun.js/webcore.zig11
-rw-r--r--src/bun.js/webcore/blob.zig2
-rw-r--r--src/bun.js/webcore/encoding.zig36
15 files changed, 1228 insertions, 469 deletions
diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp
index 4c8ff384e..21541d711 100644
--- a/src/bun.js/bindings/BunString.cpp
+++ b/src/bun.js/bindings/BunString.cpp
@@ -169,6 +169,29 @@ extern "C" JSC::EncodedJSValue BunString__toJS(JSC::JSGlobalObject* globalObject
return JSValue::encode(Bun::toJS(globalObject, *bunString));
}
+extern "C" BunString BunString__fromUTF16Unitialized(size_t length)
+{
+ unsigned utf16Length = length;
+ UChar* ptr;
+ auto impl = WTF::StringImpl::createUninitialized(utf16Length, ptr);
+ if (UNLIKELY(!ptr))
+ return { BunStringTag::Dead };
+
+ impl->ref();
+ return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } };
+}
+
+extern "C" BunString BunString__fromLatin1Unitialized(size_t length)
+{
+ unsigned latin1Length = length;
+ LChar* ptr;
+ auto impl = WTF::StringImpl::createUninitialized(latin1Length, ptr);
+ if (UNLIKELY(!ptr))
+ return { BunStringTag::Dead };
+ impl->ref();
+ return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } };
+}
+
extern "C" BunString BunString__fromUTF8(const char* bytes, size_t length)
{
if (simdutf::validate_utf8(bytes, length)) {
diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp
index c7dac89c2..8adba197c 100644
--- a/src/bun.js/bindings/CommonJSModuleRecord.cpp
+++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp
@@ -817,6 +817,12 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireCommonJS, (JSGlobalObject * lexicalGlo
WTF::String specifier = specifierValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});
+ // Special-case for "process" to just return the process object directly.
+ if (UNLIKELY(specifier == "process"_s || specifier == "node:process"_s)) {
+ jsDynamicCast<JSCommonJSModule*>(callframe->argument(1))->putDirect(vm, builtinNames(vm).exportsPublicName(), globalObject->processObject(), 0);
+ return JSValue::encode(globalObject->processObject());
+ }
+
WTF::String referrer = thisObject->id().toWTFString(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});
diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp
index 1d6b5d33a..7d7bdd982 100644
--- a/src/bun.js/bindings/Process.cpp
+++ b/src/bun.js/bindings/Process.cpp
@@ -11,13 +11,40 @@
#include <sys/stat.h>
#include "ZigConsoleClient.h"
#include <JavaScriptCore/GetterSetter.h>
+#include <JavaScriptCore/JSSet.h>
+#include <JavaScriptCore/LazyProperty.h>
+#include <JavaScriptCore/LazyPropertyInlines.h>
+#include <JavaScriptCore/VMTrapsInlines.h>
+
#pragma mark - Node.js Process
+#if defined(__APPLE__)
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#endif
+
+#if defined(__linux__)
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#endif
+
+#if !defined(_MSC_VER)
+#include <unistd.h> // setuid, getuid
+#endif
+
namespace Zig {
using namespace JSC;
#define REPORTED_NODE_VERSION "18.15.0"
+#define processObjectBindingCodeGenerator processObjectInternalsBindingCodeGenerator
+#define processObjectMainModuleCodeGenerator moduleMainCodeGenerator
+
+#if !defined(BUN_WEBKIT_VERSION)
+#define BUN_WEBKIT_VERSION "unknown"
+#endif
using JSGlobalObject = JSC::JSGlobalObject;
using Exception = JSC::Exception;
@@ -31,23 +58,20 @@ using JSObject = JSC::JSObject;
using JSNonFinalObject = JSC::JSNonFinalObject;
namespace JSCastingHelpers = JSC::JSCastingHelpers;
-static JSC_DECLARE_CUSTOM_SETTER(Process_setTitle);
-static JSC_DECLARE_CUSTOM_GETTER(Process_getArgv);
-static JSC_DECLARE_CUSTOM_SETTER(Process_setArgv);
-static JSC_DECLARE_CUSTOM_GETTER(Process_getTitle);
-static JSC_DECLARE_CUSTOM_GETTER(Process_getVersionsLazy);
-static JSC_DECLARE_CUSTOM_SETTER(Process_setVersionsLazy);
-
-static JSC_DECLARE_CUSTOM_GETTER(Process_getPID);
-static JSC_DECLARE_CUSTOM_GETTER(Process_getPPID);
-
-static JSC_DECLARE_HOST_FUNCTION(Process_functionCwd);
+JSC_DECLARE_CUSTOM_SETTER(Process_setTitle);
+JSC_DECLARE_CUSTOM_GETTER(Process_getArgv);
+JSC_DECLARE_CUSTOM_SETTER(Process_setArgv);
+JSC_DECLARE_CUSTOM_GETTER(Process_getTitle);
+JSC_DECLARE_CUSTOM_GETTER(Process_getPID);
+JSC_DECLARE_CUSTOM_GETTER(Process_getPPID);
+JSC_DECLARE_HOST_FUNCTION(Process_functionCwd);
static bool processIsExiting = false;
extern "C" uint8_t Bun__getExitCode(void*);
extern "C" uint8_t Bun__setExitCode(void*, uint8_t);
extern "C" void* Bun__getVM();
extern "C" Zig::GlobalObject* Bun__getDefaultGlobal();
+extern "C" const char* Bun__githubURL;
static void dispatchExitInternal(JSC::JSGlobalObject* globalObject, Process* process, int exitCode)
{
@@ -72,107 +96,6 @@ static void dispatchExitInternal(JSC::JSGlobalObject* globalObject, Process* pro
emitter.emit(event, arguments);
}
-static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, int fd)
-{
- auto& vm = globalObject->vm();
- auto scope = DECLARE_THROW_SCOPE(vm);
- auto* thisObject = reinterpret_cast<Zig::GlobalObject*>(globalObject);
- JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdioWriteStreamCodeGenerator(vm), globalObject);
- JSC::MarkedArgumentBuffer args;
- WTF::String process = WTF::String("node:process"_s);
- JSC::JSValue requireFunction = Zig::ImportMetaObject::createRequireFunction(
- vm,
- globalObject,
- process);
-
- args.append(JSC::jsNumber(fd));
- args.append(requireFunction);
-
- auto clientData = WebCore::clientData(vm);
- JSC::CallData callData = JSC::getCallData(getStdioWriteStream);
-
- NakedPtr<JSC::Exception> returnedException = nullptr;
- auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject->globalThis(), args, returnedException);
- RETURN_IF_EXCEPTION(scope, {});
-
- if (returnedException) {
- throwException(globalObject, scope, returnedException.get());
- return {};
- }
-
- return result;
-}
-
-JSC_DEFINE_CUSTOM_GETTER(
- Process_lazyStdinGetter,
- (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
-{
- auto& vm = globalObject->vm();
- auto scope = DECLARE_THROW_SCOPE(vm);
- JSC::JSValue value = JSC::JSValue::decode(thisValue);
- if (!value || value.isUndefinedOrNull() || !value.isObject())
- return JSValue::encode(jsUndefined());
-
- auto* thisObject = reinterpret_cast<Zig::GlobalObject*>(globalObject);
- JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdinStreamCodeGenerator(vm), globalObject);
- JSC::MarkedArgumentBuffer args;
- WTF::String process = WTF::String("node:process"_s);
- JSC::JSValue requireFunction = Zig::ImportMetaObject::createRequireFunction(
- vm,
- globalObject,
- process);
-
- args.append(JSC::jsNumber(STDIN_FILENO));
- args.append(requireFunction);
- args.append(thisObject->get(globalObject, PropertyName(JSC::Identifier::fromString(vm, "Bun"_s))));
-
- auto clientData = WebCore::clientData(vm);
- JSC::CallData callData = JSC::getCallData(getStdioWriteStream);
-
- NakedPtr<JSC::Exception> returnedException = nullptr;
- auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject->globalThis(), args, returnedException);
- RETURN_IF_EXCEPTION(scope, {});
-
- if (UNLIKELY(returnedException)) {
- throwException(globalObject, scope, returnedException.get());
- return {};
- }
-
- if (LIKELY(result))
- value.getObject()->putDirect(vm, property, result, 0);
-
- return JSValue::encode(result);
-}
-
-JSC_DEFINE_CUSTOM_GETTER(
- Process_lazyStdoutGetter,
- (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
-{
- JSValue value = JSValue::decode(thisValue);
- auto& vm = globalObject->vm();
- JSC::JSObject* thisObject = value.toObject(globalObject);
- JSC::JSValue stream = constructStdioWriteStream(globalObject, 1);
-
- if (stream)
- thisObject->putDirect(vm, property, stream, 0);
-
- return JSValue::encode(stream);
-}
-
-JSC_DEFINE_CUSTOM_GETTER(
- Process_lazyStderrGetter, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
-{
- JSValue value = JSValue::decode(thisValue);
- auto& vm = globalObject->vm();
- JSC::JSObject* thisObject = value.toObject(globalObject);
- JSC::JSValue stream = constructStdioWriteStream(globalObject, 2);
-
- if (stream)
- thisObject->putDirect(vm, property, stream, 0);
-
- return JSValue::encode(stream);
-}
-
JSC_DEFINE_CUSTOM_SETTER(Process_defaultSetter,
(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
JSC::EncodedJSValue value, JSC::PropertyName propertyName))
@@ -420,6 +343,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionExit,
Process__dispatchOnExit(zigGlobal, exitCode);
Bun__Process__exit(zigGlobal, exitCode);
+ __builtin_unreachable();
}
extern "C" uint64_t Bun__readOriginTimer(void*);
@@ -427,9 +351,12 @@ extern "C" uint64_t Bun__readOriginTimer(void*);
JSC_DEFINE_HOST_FUNCTION(Process_functionHRTime,
(JSC::JSGlobalObject * globalObject_, JSC::CallFrame* callFrame))
{
+
Zig::GlobalObject* globalObject
= reinterpret_cast<Zig::GlobalObject*>(globalObject_);
auto& vm = globalObject->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+
uint64_t time = Bun__readOriginTimer(globalObject->bunVM());
int64_t seconds = static_cast<int64_t>(time / 1000000000);
int64_t nanoseconds = time % 1000000000;
@@ -438,7 +365,6 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionHRTime,
JSC::JSValue arg0 = callFrame->uncheckedArgument(0);
if (!arg0.isUndefinedOrNull()) {
JSArray* relativeArray = JSC::jsDynamicCast<JSC::JSArray*>(arg0);
- auto throwScope = DECLARE_THROW_SCOPE(vm);
if ((!relativeArray && !arg0.isUndefinedOrNull()) || relativeArray->length() < 2) {
JSC::throwTypeError(globalObject, throwScope, "hrtime() argument must be an array or undefined"_s);
return JSC::JSValue::encode(JSC::JSValue {});
@@ -458,14 +384,28 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionHRTime,
seconds--;
nanoseconds += 1000000000;
}
- throwScope.release();
}
}
- auto* array = JSArray::create(vm, globalObject->originalArrayStructureForIndexingType(ArrayWithContiguous), 2);
- array->setIndexQuickly(vm, 0, JSC::jsNumber(seconds));
- array->setIndexQuickly(vm, 1, JSC::jsNumber(nanoseconds));
- return JSC::JSValue::encode(JSC::JSValue(array));
+ JSC::JSArray* array = nullptr;
+ {
+ JSC::ObjectInitializationScope initializationScope(vm);
+ if ((array = JSC::JSArray::tryCreateUninitializedRestricted(
+ initializationScope, nullptr,
+ globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous),
+ 2))) {
+
+ array->initializeIndex(initializationScope, 0, JSC::jsNumber(seconds));
+ array->initializeIndex(initializationScope, 1, JSC::jsNumber(nanoseconds));
+ }
+ }
+
+ if (UNLIKELY(!array)) {
+ JSC::throwOutOfMemoryError(globalObject, throwScope);
+ return JSC::JSValue::encode(JSC::JSValue {});
+ }
+
+ RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(array));
}
JSC_DEFINE_HOST_FUNCTION(Process_functionHRTimeBigInt,
@@ -497,34 +437,6 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionChdir,
return JSC::JSValue::encode(result);
}
-extern "C" const char* Bun__githubURL;
-
-JSC_DEFINE_CUSTOM_GETTER(Process_getterRelease, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName))
-{
- auto& vm = globalObject->vm();
-
- auto* release = JSC::constructEmptyObject(globalObject);
- release->putDirect(vm, Identifier::fromString(vm, "name"_s), jsString(vm, WTF::String("bun"_s)), 0);
- release->putDirect(vm, Identifier::fromString(vm, "lts"_s), jsBoolean(false), 0);
- release->putDirect(vm, Identifier::fromString(vm, "sourceUrl"_s), jsString(vm, WTF::String(Bun__githubURL, strlen(Bun__githubURL))), 0);
- release->putDirect(vm, Identifier::fromString(vm, "headersUrl"_s), jsEmptyString(vm), 0);
- release->putDirect(vm, Identifier::fromString(vm, "libUrl"_s), jsEmptyString(vm), 0);
-
- return JSValue::encode(release);
-}
-
-JSC_DEFINE_CUSTOM_SETTER(Process_setterRelease,
- (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
- JSC::EncodedJSValue value, JSC::PropertyName))
-{
- JSC::VM& vm = globalObject->vm();
-
- JSC::JSObject* thisObject = JSC::jsDynamicCast<JSC::JSObject*>(JSValue::decode(thisValue));
- thisObject->putDirect(vm, JSC::Identifier::fromString(vm, "release"_s), JSValue::decode(value), 0);
-
- return true;
-}
-
// static const NeverDestroyed<String> signalNames[] = {
// MAKE_STATIC_STRING_IMPL("SIGHUP"),
// MAKE_STATIC_STRING_IMPL("SIGINT"),
@@ -660,31 +572,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_emitWarning, (JSGlobalObject * lexicalGlobalObj
return JSValue::encode(jsUndefined());
}
-JSC_DEFINE_CUSTOM_GETTER(Process_lazyArgv0Getter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name))
-{
- JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject();
- EncodedJSValue ret = Bun__Process__getArgv0(globalObject);
-
- if (LIKELY(thisObject)) {
- thisObject->putDirect(globalObject->vm(), name, JSValue::decode(ret), 0);
- }
-
- return ret;
-}
-
-JSC_DEFINE_CUSTOM_GETTER(Process_lazyExecArgvGetter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name))
-{
- JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject();
- EncodedJSValue ret = Bun__Process__getExecArgv(globalObject);
-
- if (LIKELY(thisObject)) {
- thisObject->putDirect(globalObject->vm(), name, JSValue::decode(ret), 0);
- }
-
- return ret;
-}
-
-JSC_DEFINE_CUSTOM_GETTER(Process__getExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name))
+JSC_DEFINE_CUSTOM_GETTER(processExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name))
{
Process* process = jsDynamicCast<Process*>(JSValue::decode(thisValue));
if (!process) {
@@ -693,7 +581,7 @@ JSC_DEFINE_CUSTOM_GETTER(Process__getExitCode, (JSC::JSGlobalObject * lexicalGlo
return JSValue::encode(jsNumber(Bun__getExitCode(jsCast<Zig::GlobalObject*>(process->globalObject())->bunVM())));
}
-JSC_DEFINE_CUSTOM_SETTER(Process__setExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName))
+JSC_DEFINE_CUSTOM_SETTER(setProcessExitCode, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName))
{
Process* process = jsDynamicCast<Process*>(JSValue::decode(thisValue));
if (!process) {
@@ -724,158 +612,56 @@ JSC_DEFINE_CUSTOM_SETTER(Process__setExitCode, (JSC::JSGlobalObject * lexicalGlo
return true;
}
-JSC_DEFINE_CUSTOM_GETTER(Process_lazyExecPathGetter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name))
+static JSValue constructVersions(VM& vm, JSObject* processObject)
{
- JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject();
- EncodedJSValue ret = Bun__Process__getExecPath(globalObject);
-
- if (LIKELY(thisObject)) {
- thisObject->putDirect(globalObject->vm(), name, JSValue::decode(ret), 0);
- }
-
- return ret;
-}
-
-void Process::finishCreation(JSC::VM& vm)
-{
- Base::finishCreation(vm);
- auto clientData = WebCore::clientData(vm);
- auto* globalObject = reinterpret_cast<Zig::GlobalObject*>(this->globalObject());
-
- putDirectCustomAccessor(vm, clientData->builtinNames().pidPublicName(),
- JSC::CustomGetterSetter::create(vm, Process_getPID, nullptr),
- static_cast<unsigned>(JSC::PropertyAttribute::CustomValue));
-
- putDirectCustomAccessor(vm, clientData->builtinNames().ppidPublicName(),
- JSC::CustomGetterSetter::create(vm, Process_getPPID, nullptr),
- static_cast<unsigned>(JSC::PropertyAttribute::CustomValue));
-
- putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "title"_s),
- JSC::CustomGetterSetter::create(vm, Process_getTitle, Process_setTitle),
- static_cast<unsigned>(JSC::PropertyAttribute::CustomValue));
-
- putDirectCustomAccessor(vm, clientData->builtinNames().argvPublicName(),
- JSC::CustomGetterSetter::create(vm, Process_getArgv, Process_setArgv),
- static_cast<unsigned>(JSC::PropertyAttribute::CustomValue));
-
- putDirect(vm, JSC::Identifier::fromString(vm, "revision"_s),
- JSC::jsString(vm, makeAtomString(Bun__version_sha)), 0);
-
- this->putDirect(vm, clientData->builtinNames().nextTickPublicName(),
- JSC::JSFunction::create(vm, globalObject, 1,
- MAKE_STATIC_STRING_IMPL("nextTick"), Process_functionNextTick, ImplementationVisibility::Public),
- PropertyAttribute::Function | 0);
-
- this->putDirect(vm, JSC::Identifier::fromString(vm, "dlopen"_s),
- JSC::JSFunction::create(vm, globalObject, 1,
- MAKE_STATIC_STRING_IMPL("dlopen"), Process_functionDlopen, ImplementationVisibility::Public),
- PropertyAttribute::Function | 0);
-
- this->putDirect(vm, clientData->builtinNames().cwdPublicName(),
- JSC::JSFunction::create(vm, globalObject, 0,
- MAKE_STATIC_STRING_IMPL("cwd"), Process_functionCwd, ImplementationVisibility::Public),
- PropertyAttribute::Function | 0);
-
- this->putDirect(vm, clientData->builtinNames().chdirPublicName(),
- JSC::JSFunction::create(vm, globalObject, 0,
- MAKE_STATIC_STRING_IMPL("chdir"), Process_functionChdir, ImplementationVisibility::Public),
- PropertyAttribute::Function | 0);
-
- this->putDirect(vm, JSC::Identifier::fromString(vm, "exit"_s),
- JSC::JSFunction::create(vm, globalObject, 0,
- MAKE_STATIC_STRING_IMPL("exit"), Process_functionExit, ImplementationVisibility::Public),
- PropertyAttribute::Function | 0);
-
- putDirectCustomAccessor(
- vm, clientData->builtinNames().versionsPublicName(),
- JSC::CustomGetterSetter::create(vm, Process_getVersionsLazy, Process_setVersionsLazy), 0);
- // this should be transpiled out, but just incase
- this->putDirect(vm, JSC::Identifier::fromString(vm, "browser"_s),
- JSC::JSValue(false), PropertyAttribute::DontEnum | 0);
-
- this->putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "exitCode"_s),
- JSC::CustomGetterSetter::create(vm,
- Process__getExitCode,
- Process__setExitCode),
- 0);
-
- this->putDirect(vm, clientData->builtinNames().versionPublicName(),
- JSC::jsString(vm, makeString("v", REPORTED_NODE_VERSION)));
-
- // this gives some way of identifying at runtime whether the SSR is happening in node or not.
- // this should probably be renamed to what the name of the bundler is, instead of "notNodeJS"
- // but it must be something that won't evaluate to truthy in Node.js
- this->putDirect(vm, JSC::Identifier::fromString(vm, "isBun"_s), JSC::JSValue(true));
-#if defined(__APPLE__)
- this->putDirect(vm, JSC::Identifier::fromString(vm, "platform"_s),
- JSC::jsString(vm, makeAtomString("darwin")));
-#else
- this->putDirect(vm, JSC::Identifier::fromString(vm, "platform"_s),
- JSC::jsString(vm, makeAtomString("linux")));
-#endif
-
-#if defined(__x86_64__)
- this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s),
- JSC::jsString(vm, makeAtomString("x64")));
-#elif defined(__i386__)
- this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s),
- JSC::jsString(vm, makeAtomString("x86")));
-#elif defined(__arm__)
- this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s),
- JSC::jsString(vm, makeAtomString("arm")));
-#elif defined(__aarch64__)
- this->putDirect(vm, JSC::Identifier::fromString(vm, "arch"_s),
- JSC::jsString(vm, makeAtomString("arm64")));
-#endif
-
- JSC::JSFunction* hrtime = JSC::JSFunction::create(vm, globalObject, 0,
- MAKE_STATIC_STRING_IMPL("hrtime"), Process_functionHRTime, ImplementationVisibility::Public);
-
- JSC::JSFunction* hrtimeBigInt = JSC::JSFunction::create(vm, globalObject, 0,
- MAKE_STATIC_STRING_IMPL("bigint"), Process_functionHRTimeBigInt, ImplementationVisibility::Public);
-
- hrtime->putDirect(vm, JSC::Identifier::fromString(vm, "bigint"_s), hrtimeBigInt);
- this->putDirect(vm, JSC::Identifier::fromString(vm, "hrtime"_s), hrtime);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "release"_s)),
- JSC::CustomGetterSetter::create(vm, Process_getterRelease, Process_setterRelease), 0);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "stdout"_s)),
- JSC::CustomGetterSetter::create(vm, Process_lazyStdoutGetter, Process_defaultSetter), 0);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "stderr"_s)),
- JSC::CustomGetterSetter::create(vm, Process_lazyStderrGetter, Process_defaultSetter), 0);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "stdin"_s)),
- JSC::CustomGetterSetter::create(vm, Process_lazyStdinGetter, Process_defaultSetter), 0);
-
- this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "abort"_s),
- 0, Process_functionAbort, ImplementationVisibility::Public, NoIntrinsic, 0);
-
- this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "abort"_s),
- 0, Process_functionAbort, ImplementationVisibility::Public, NoIntrinsic, 0);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "argv0"_s)),
- JSC::CustomGetterSetter::create(vm, Process_lazyArgv0Getter, Process_defaultSetter), 0);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "execPath"_s)),
- JSC::CustomGetterSetter::create(vm, Process_lazyExecPathGetter, Process_defaultSetter), 0);
-
- this->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "execArgv"_s)),
- JSC::CustomGetterSetter::create(vm, Process_lazyExecArgvGetter, Process_defaultSetter), 0);
+ auto* globalObject = processObject->globalObject();
+ JSC::JSObject* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 19);
- this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "uptime"_s),
- 0, Process_functionUptime, ImplementationVisibility::Public, NoIntrinsic, 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "node"_s),
+ JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(REPORTED_NODE_VERSION))));
+ object->putDirect(
+ vm, JSC::Identifier::fromString(vm, "bun"_s),
+ JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(Bun__version + 1 /* prefix with v */))));
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s),
+ JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(BUN_WEBKIT_VERSION))));
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "boringssl"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_boringssl))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "libarchive"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_libarchive))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "mimalloc"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_mimalloc))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "picohttpparser"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_picohttpparser))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "uwebsockets"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_uws))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_webkit))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "zig"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zig))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "zlib"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zlib))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "tinycc"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_tinycc))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "lolhtml"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_lolhtml))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "ares"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_c_ares))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "usockets"_s),
+ JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_usockets))), 0);
- this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "umask"_s),
- 1, Process_functionUmask, ImplementationVisibility::Public, NoIntrinsic, 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "v8"_s), JSValue(JSC::jsString(vm, makeString("10.8.168.20-node.8"_s))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "uv"_s), JSValue(JSC::jsString(vm, makeString("1.44.2"_s))), 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "napi"_s), JSValue(JSC::jsString(vm, makeString("8"_s))), 0);
- this->putDirectBuiltinFunction(vm, globalObject, JSC::Identifier::fromString(vm, "binding"_s),
- processObjectInternalsBindingCodeGenerator(vm),
- 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "modules"_s),
+ JSC::JSValue(JSC::jsString(vm, makeAtomString("108"))));
- this->putDirect(vm, vm.propertyNames->toStringTagSymbol, jsString(vm, String("process"_s)), 0);
+ return object;
+}
+static JSValue constructProcessConfigObject(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
// target_defaults:
// { cflags: [],
// default_configuration: 'Release',
@@ -905,179 +691,779 @@ void Process::finishCreation(JSC::VM& vm)
JSC::jsNumber(1), 0);
config->putDirect(vm, JSC::Identifier::fromString(vm, "target_defaults"_s), JSC::constructEmptyObject(globalObject), 0);
config->putDirect(vm, JSC::Identifier::fromString(vm, "variables"_s), variables, 0);
- this->putDirect(vm, JSC::Identifier::fromString(vm, "config"_s), config, 0);
- this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "emitWarning"_s),
- 1, Process_emitWarning, ImplementationVisibility::Public, NoIntrinsic, 0);
+ return config;
+}
+
+static JSValue constructProcessReleaseObject(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ auto* release = JSC::constructEmptyObject(globalObject);
+ release->putDirect(vm, Identifier::fromString(vm, "name"_s), jsString(vm, WTF::String("bun"_s)), 0);
+ release->putDirect(vm, Identifier::fromString(vm, "lts"_s), jsBoolean(false), 0);
+ release->putDirect(vm, Identifier::fromString(vm, "sourceUrl"_s), jsString(vm, WTF::String(Bun__githubURL, strlen(Bun__githubURL))), 0);
+ release->putDirect(vm, Identifier::fromString(vm, "headersUrl"_s), jsEmptyString(vm), 0);
+ release->putDirect(vm, Identifier::fromString(vm, "libUrl"_s), jsEmptyString(vm), 0);
+
+ return release;
+}
+
+static JSValue constructProcessHrtimeObject(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ JSC::JSFunction* hrtime = JSC::JSFunction::create(vm, globalObject, 0,
+ String("hrtime"_s), Process_functionHRTime, ImplementationVisibility::Public);
+
+ JSC::JSFunction* hrtimeBigInt = JSC::JSFunction::create(vm, globalObject, 0,
+ String("bigint"_s), Process_functionHRTimeBigInt, ImplementationVisibility::Public);
+
+ hrtime->putDirect(vm, JSC::Identifier::fromString(vm, "bigint"_s), hrtimeBigInt);
+
+ return hrtime;
+}
+
+static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, int fd)
+{
+ auto& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdioWriteStreamCodeGenerator(vm), globalObject);
+ JSC::MarkedArgumentBuffer args;
+ args.append(JSC::jsNumber(fd));
+
+ auto clientData = WebCore::clientData(vm);
+ JSC::CallData callData = JSC::getCallData(getStdioWriteStream);
+
+ NakedPtr<JSC::Exception> returnedException = nullptr;
+ auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject->globalThis(), args, returnedException);
+ RETURN_IF_EXCEPTION(scope, {});
+
+ if (returnedException) {
+ throwException(globalObject, scope, returnedException.get());
+ return {};
+ }
+
+ return result;
+}
+
+static JSValue constructStdout(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = Bun__getDefaultGlobal();
+ return constructStdioWriteStream(globalObject, 1);
+}
+
+static JSValue constructStderr(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = Bun__getDefaultGlobal();
+ return constructStdioWriteStream(globalObject, 2);
+}
+
+static JSValue constructStdin(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = Bun__getDefaultGlobal();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ auto* thisObject = reinterpret_cast<Zig::GlobalObject*>(globalObject);
+ JSC::JSFunction* getStdioWriteStream = JSC::JSFunction::create(vm, processObjectInternalsGetStdinStreamCodeGenerator(vm), globalObject);
+ JSC::MarkedArgumentBuffer args;
+ args.append(JSC::jsNumber(STDIN_FILENO));
+
+ auto clientData = WebCore::clientData(vm);
+ JSC::CallData callData = JSC::getCallData(getStdioWriteStream);
+
+ NakedPtr<JSC::Exception> returnedException = nullptr;
+ auto result = JSC::call(globalObject, getStdioWriteStream, callData, globalObject, args, returnedException);
+ RETURN_IF_EXCEPTION(scope, {});
+
+ if (UNLIKELY(returnedException)) {
+ throwException(globalObject, scope, returnedException.get());
+ return {};
+ }
+
+ RELEASE_AND_RETURN(scope, result);
+}
+
+static JSValue constructPid(VM& vm, JSObject* processObject)
+{
+ return jsNumber(getpid());
+}
+
+static JSValue constructPpid(VM& vm, JSObject* processObject)
+{
+ return jsNumber(getppid());
+}
+
+static JSValue constructArgv0(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ return JSValue::decode(Bun__Process__getArgv0(globalObject));
+}
+
+static JSValue constructExecArgv(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ return JSValue::decode(Bun__Process__getExecArgv(globalObject));
+}
+
+static JSValue constructExecPath(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ return JSValue::decode(Bun__Process__getExecPath(globalObject));
+}
+
+static JSValue constructArgv(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ return JSValue::decode(Bun__Process__getArgv(globalObject));
+}
+
+static JSValue constructArch(VM& vm, JSObject* processObject)
+{
+#if defined(__x86_64__)
+ return JSC::jsString(vm, makeAtomString("x64"));
+#elif defined(__i386__)
+ return JSC::jsString(vm, makeAtomString("x86"));
+#elif defined(__arm__)
+ return JSC::jsString(vm, makeAtomString("arm"));
+#elif defined(__aarch64__)
+ return JSC::jsString(vm, makeAtomString("arm64"));
+#else
+#error "Unknown architecture"
+#endif
+}
- JSC::JSFunction* requireDotMainFunction = JSFunction::create(
+static JSValue constructPlatform(VM& vm, JSObject* processObject)
+{
+#if defined(__APPLE__)
+ return JSC::jsString(vm, makeAtomString("darwin"));
+#elif defined(__linux__)
+ return JSC::jsString(vm, makeAtomString("linux"));
+#else
+#error "Unknown platform"
+#endif
+}
+
+static JSValue constructBrowser(VM& vm, JSObject* processObject)
+{
+ return jsBoolean(false);
+}
+
+static JSValue constructVersion(VM& vm, JSObject* processObject)
+{
+ return JSC::jsString(vm, makeString("v", REPORTED_NODE_VERSION));
+}
+
+static JSValue constructIsBun(VM& vm, JSObject* processObject)
+{
+ return jsBoolean(true);
+}
+
+static JSValue constructRevision(VM& vm, JSObject* processObject)
+{
+ return JSC::jsString(vm, makeAtomString(Bun__version_sha));
+}
+
+static JSValue constructEnv(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = jsCast<Zig::GlobalObject*>(processObject->globalObject());
+ return globalObject->processEnvObject();
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functiongetuid, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ return JSValue::encode(jsNumber(getuid()));
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functiongeteuid, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ return JSValue::encode(jsNumber(geteuid()));
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functiongetegid, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ return JSValue::encode(jsNumber(getegid()));
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functiongetgid, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ return JSValue::encode(jsNumber(getgid()));
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functiongetgroups, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ auto& vm = globalObject->vm();
+ int ngroups = getgroups(0, nullptr);
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ if (ngroups == -1) {
+ SystemError error;
+ error.errno_ = errno;
+ error.syscall = Bun::toString("getgroups"_s);
+ throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject)));
+ return JSValue::encode(jsUndefined());
+ }
+
+ gid_t egid = getegid();
+ JSArray* groups = constructEmptyArray(globalObject, nullptr, static_cast<unsigned int>(ngroups));
+ Vector<gid_t> groupVector(ngroups);
+ getgroups(1, &egid);
+ bool needsEgid = true;
+ for (unsigned i = 0; i < ngroups; i++) {
+ auto current = groupVector[i];
+ if (current == needsEgid) {
+ needsEgid = false;
+ }
+
+ groups->putDirectIndex(globalObject, i, jsNumber(current));
+ }
+
+ if (needsEgid)
+ groups->push(globalObject, jsNumber(egid));
+
+ return JSValue::encode(groups);
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functionAssert, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ auto& vm = globalObject->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+
+ JSValue arg0 = callFrame->argument(0);
+ bool condition = arg0.toBoolean(globalObject);
+ RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
+ if (condition) {
+ return JSValue::encode(jsUndefined());
+ }
+
+ JSValue arg1 = callFrame->argument(1);
+ String message = arg1.isUndefined() ? String() : arg1.toWTFString(globalObject);
+ RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
+ auto error = createError(globalObject, makeString("Assertion failed: "_s, message));
+ error->putDirect(vm, Identifier::fromString(vm, "code"_s), jsString(vm, makeString("ERR_ASSERTION"_s)));
+ throwException(globalObject, throwScope, error);
+ return JSValue::encode(jsUndefined());
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functionReallyExit, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ auto& vm = globalObject->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ uint8_t exitCode = 0;
+ JSValue arg0 = callFrame->argument(0);
+ if (arg0.isNumber()) {
+ if (!arg0.isInt32()) {
+ throwRangeError(globalObject, throwScope, "The \"code\" argument must be an integer"_s);
+ return JSC::JSValue::encode(JSC::JSValue {});
+ }
+
+ int extiCode32 = arg0.toInt32(globalObject);
+ RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::JSValue {}));
+
+ if (extiCode32 < 0 || extiCode32 > 127) {
+ throwRangeError(globalObject, throwScope, "The \"code\" argument must be an integer between 0 and 127"_s);
+ return JSC::JSValue::encode(JSC::JSValue {});
+ }
+
+ exitCode = static_cast<uint8_t>(extiCode32);
+ } else if (!arg0.isUndefinedOrNull()) {
+ throwTypeError(globalObject, throwScope, "The \"code\" argument must be an integer"_s);
+ return JSC::JSValue::encode(JSC::JSValue {});
+ } else {
+ exitCode = Bun__getExitCode(Bun__getVM());
+ }
+
+ auto* zigGlobal = jsDynamicCast<Zig::GlobalObject*>(globalObject);
+ if (UNLIKELY(!zigGlobal)) {
+ zigGlobal = Bun__getDefaultGlobal();
+ }
+ Bun__Process__exit(zigGlobal, exitCode);
+ __builtin_unreachable();
+}
+
+template<typename Visitor>
+void Process::visitChildrenImpl(JSCell* cell, Visitor& visitor)
+{
+ Process* thisObject = jsCast<Process*>(cell);
+ ASSERT_GC_OBJECT_INHERITS(thisObject, info());
+ Base::visitChildren(thisObject, visitor);
+ thisObject->cpuUsageStructure.visit(visitor);
+ thisObject->memoryUsageStructure.visit(visitor);
+}
+
+DEFINE_VISIT_CHILDREN(Process);
+
+static Structure* constructCPUUsageStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject)
+{
+ JSC::Structure* structure = globalObject->structureCache().emptyObjectStructureForPrototype(globalObject, globalObject->objectPrototype(), 2);
+ PropertyOffset offset;
+ structure = structure->addPropertyTransition(
vm,
- moduleMainCodeGenerator(vm),
- globalObject->globalScope());
- // https://nodejs.org/api/process.html#processmainmodule
- this->putDirect(
+ structure,
+ JSC::Identifier::fromString(vm, "user"_s),
+ 0,
+ offset);
+ structure = structure->addPropertyTransition(
vm,
- JSC::Identifier::fromString(vm, "mainModule"_s),
- JSC::GetterSetter::create(vm, globalObject, requireDotMainFunction, JSValue()),
- PropertyAttribute::Builtin | PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | 0);
+ structure,
+ JSC::Identifier::fromString(vm, "system"_s),
+ 0,
+ offset);
+ return structure;
}
+static Structure* constructMemoryUsageStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject)
+{
+ JSC::Structure* structure = globalObject->structureCache().emptyObjectStructureForPrototype(globalObject, globalObject->objectPrototype(), 5);
+ PropertyOffset offset;
+ structure = structure->addPropertyTransition(
+ vm,
+ structure,
+ JSC::Identifier::fromString(vm, "rss"_s),
+ 0,
+ offset);
+ structure = structure->addPropertyTransition(
+ vm,
+ structure,
+ JSC::Identifier::fromString(vm, "heapTotal"_s),
+ 0,
+ offset);
+ structure = structure->addPropertyTransition(
+ vm,
+ structure,
+ JSC::Identifier::fromString(vm, "heapUsed"_s),
+ 0,
+ offset);
+ structure = structure->addPropertyTransition(
+ vm,
+ structure,
+ JSC::Identifier::fromString(vm, "external"_s),
+ 0,
+ offset);
+ structure = structure->addPropertyTransition(
+ vm,
+ structure,
+ JSC::Identifier::fromString(vm, "arrayBuffers"_s),
+ 0,
+ offset);
-const JSC::ClassInfo Process::s_info = { "Process"_s, &Base::s_info, nullptr, nullptr,
- CREATE_METHOD_TABLE(Process) };
+ return structure;
+}
-JSC_DEFINE_CUSTOM_GETTER(Process_getTitle, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+static Process* getProcessObject(JSC::JSGlobalObject* lexicalGlobalObject, JSValue thisValue)
{
- ZigString str;
- Bun__Process__getTitle(globalObject, &str);
- return JSValue::encode(Zig::toJSStringValue(str, globalObject));
+ Process* process = jsDynamicCast<Process*>(thisValue);
+
+ // Handle "var memoryUsage = process.memoryUsage; memoryUsage()"
+ if (UNLIKELY(!process)) {
+ // Handle calling this function from inside a node:vm
+ Zig::GlobalObject* zigGlobalObject = jsDynamicCast<Zig::GlobalObject*>(lexicalGlobalObject);
+
+ if (UNLIKELY(!zigGlobalObject)) {
+ zigGlobalObject = Bun__getDefaultGlobal();
+ }
+
+ return jsCast<Process*>(zigGlobalObject->processObject());
+ }
+
+ return process;
}
-JSC_DEFINE_CUSTOM_SETTER(Process_setTitle,
- (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
- JSC::EncodedJSValue value, JSC::PropertyName))
+JSC_DEFINE_HOST_FUNCTION(Process_functionCpuUsage,
+ (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = globalObject->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ struct rusage rusage;
+ if (getrusage(RUSAGE_SELF, &rusage) != 0) {
+ SystemError error;
+ error.errno_ = errno;
+ error.syscall = Bun::toString("getrusage"_s);
+ error.message = Bun::toString("Failed to get CPU usage"_s);
+ throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject)));
+ return JSValue::encode(jsUndefined());
+ }
- JSC::JSObject* thisObject = JSC::jsDynamicCast<JSC::JSObject*>(JSValue::decode(thisValue));
- JSC::JSString* jsString = JSC::jsDynamicCast<JSC::JSString*>(JSValue::decode(value));
- if (!thisObject || !jsString) {
- return false;
+ auto* process = getProcessObject(globalObject, callFrame->thisValue());
+
+ Structure* cpuUsageStructure = process->cpuUsageStructure.getInitializedOnMainThread(process);
+
+ constexpr double MICROS_PER_SEC = 1000000.0;
+
+ double user = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;
+ double system = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec;
+
+ if (callFrame->argumentCount() > 0) {
+ JSValue comparatorValue = callFrame->argument(0);
+ if (!comparatorValue.isUndefined()) {
+ if (UNLIKELY(!comparatorValue.isObject())) {
+ throwTypeError(globalObject, throwScope, "Expected an object as the first argument"_s);
+ return JSC::JSValue::encode(JSC::jsUndefined());
+ }
+
+ JSC::JSObject* comparator = comparatorValue.getObject();
+ JSValue userValue;
+ JSValue systemValue;
+
+ if (LIKELY(comparator->structureID() == cpuUsageStructure->id())) {
+ userValue = comparator->getDirect(0);
+ systemValue = comparator->getDirect(1);
+ } else {
+ userValue = comparator->getIfPropertyExists(globalObject, JSC::Identifier::fromString(vm, "user"_s));
+ RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::jsUndefined()));
+
+ systemValue = comparator->getIfPropertyExists(globalObject, JSC::Identifier::fromString(vm, "system"_s));
+ RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::jsUndefined()));
+ }
+
+ if (UNLIKELY(!userValue || !userValue.isNumber())) {
+ throwTypeError(globalObject, throwScope, "Expected a number for the user property"_s);
+ return JSC::JSValue::encode(JSC::jsUndefined());
+ }
+
+ if (UNLIKELY(!systemValue || !systemValue.isNumber())) {
+ throwTypeError(globalObject, throwScope, "Expected a number for the system property"_s);
+ return JSC::JSValue::encode(JSC::jsUndefined());
+ }
+
+ double userComparator = userValue.asNumber();
+ double systemComparator = systemValue.asNumber();
+
+ user -= userComparator;
+ system -= systemComparator;
+ }
}
- ZigString str = Zig::toZigString(jsString, globalObject);
- Bun__Process__setTitle(globalObject, &str);
+ JSC::JSObject* result = JSC::constructEmptyObject(vm, cpuUsageStructure);
+ RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::jsUndefined()));
- return true;
+ result->putDirectOffset(vm, 0, JSC::jsNumber(user));
+ result->putDirectOffset(vm, 1, JSC::jsNumber(system));
+
+ RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(result));
}
-JSC_DEFINE_CUSTOM_GETTER(Process_getArgv, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+static int getRSS(size_t* rss)
+{
+#if defined(__APPLE__)
+ mach_msg_type_number_t count;
+ task_basic_info_data_t info;
+ kern_return_t err;
+
+ count = TASK_BASIC_INFO_COUNT;
+ err = task_info(mach_task_self(),
+ TASK_BASIC_INFO,
+ reinterpret_cast<task_info_t>(&info),
+ &count);
+
+ if (err == KERN_SUCCESS) {
+ *rss = (size_t)info.resident_size;
+ return 0;
+ }
+
+ return -1;
+#elif defined(__linux__)
+ // Taken from libuv.
+ char buf[1024];
+ const char* s;
+ ssize_t n;
+ long val;
+ int fd;
+ int i;
+
+ do
+ fd = open("/proc/self/stat", O_RDONLY);
+ while (fd == -1 && errno == EINTR);
+
+ if (fd == -1)
+ return errno;
+
+ do
+ n = read(fd, buf, sizeof(buf) - 1);
+ while (n == -1 && errno == EINTR);
+
+ int closeErrno = 0;
+ do {
+ closeErrno = close(fd);
+ } while (closeErrno == -1 && errno == EINTR);
+
+ if (n == -1)
+ return errno;
+ buf[n] = '\0';
+
+ s = strchr(buf, ' ');
+ if (s == NULL)
+ goto err;
+
+ s += 1;
+ if (*s != '(')
+ goto err;
+
+ s = strchr(s, ')');
+ if (s == NULL)
+ goto err;
+
+ for (i = 1; i <= 22; i++) {
+ s = strchr(s + 1, ' ');
+ if (s == NULL)
+ goto err;
+ }
+
+ errno = 0;
+ val = strtol(s, NULL, 10);
+ if (errno != 0)
+ goto err;
+ if (val < 0)
+ goto err;
+
+ *rss = val * getpagesize();
+ return 0;
+
+err:
+ return EINVAL;
+#else
+#error "Unsupported platform"
+#endif
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functionMemoryUsage,
+ (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = globalObject->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ auto* process = getProcessObject(globalObject, callFrame->thisValue());
+
+ size_t current_rss = 0;
+ if (getRSS(&current_rss) != 0) {
+ SystemError error;
+ error.errno_ = errno;
+ error.syscall = Bun::toString("memoryUsage"_s);
+ error.message = Bun::toString("Failed to get memory usage"_s);
+ throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject)));
+ return JSC::JSValue::encode(JSC::JSValue {});
+ }
- Zig::Process* thisObject = JSC::jsDynamicCast<Zig::Process*>(JSValue::decode(thisValue));
- if (!thisObject) {
- return JSValue::encode(JSC::jsUndefined());
+ JSC::JSObject* result = JSC::constructEmptyObject(vm, process->memoryUsageStructure.getInitializedOnMainThread(process));
+ if (UNLIKELY(throwScope.exception())) {
+ return JSC::JSValue::encode(JSC::JSValue {});
}
- JSC::EncodedJSValue argv_ = Bun__Process__getArgv(globalObject);
- auto clientData = WebCore::clientData(vm);
+ // Node.js:
+ // {
+ // rss: 4935680,
+ // heapTotal: 1826816,
+ // heapUsed: 650472,
+ // external: 49879,
+ // arrayBuffers: 9386
+ // }
+
+ result->putDirectOffset(vm, 0, JSC::jsNumber(current_rss));
+ result->putDirectOffset(vm, 1, JSC::jsNumber(vm.heap.blockBytesAllocated()));
+
+ // heap.size() loops through every cell...
+ // TODO: add a binding for heap.sizeAfterLastCollection()
+ result->putDirectOffset(vm, 2, JSC::jsNumber(vm.heap.sizeAfterLastEdenCollection()));
- thisObject->putDirect(vm, clientData->builtinNames().argvPublicName(),
- JSC::JSValue::decode(argv_), 0);
+ result->putDirectOffset(vm, 3, JSC::jsNumber(vm.heap.externalMemorySize()));
- return argv_;
+ // We report 0 for this because m_arrayBuffers in JSC::Heap is private and we need to add a binding
+ // If we use objectTypeCounts(), it's hideously slow because it loops through every single object in the heap
+ // TODO: add a binding for m_arrayBuffers, registerWrapper() in TypedArrayController doesn't work
+ result->putDirectOffset(vm, 4, JSC::jsNumber(0));
+
+ RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(result));
}
-JSC_DEFINE_CUSTOM_SETTER(Process_setArgv,
- (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
- JSC::EncodedJSValue value, JSC::PropertyName))
+JSC_DEFINE_HOST_FUNCTION(Process_functionMemoryUsageRSS,
+ (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = globalObject->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
- JSC::JSObject* thisObject = JSC::jsDynamicCast<JSC::JSObject*>(JSValue::decode(thisValue));
- if (!thisObject) {
- return false;
+ size_t current_rss = 0;
+ if (getRSS(&current_rss) != 0) {
+ SystemError error;
+ error.errno_ = errno;
+ error.syscall = Bun::toString("memoryUsage"_s);
+ error.message = Bun::toString("Failed to get memory usage"_s);
+ throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject)));
+ return JSC::JSValue::encode(JSC::JSValue {});
}
- auto clientData = WebCore::clientData(vm);
+ RELEASE_AND_RETURN(throwScope, JSValue::encode(jsNumber(current_rss)));
+}
+
+JSC_DEFINE_HOST_FUNCTION(Process_functionOpenStdin, (JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ auto& vm = globalObject->vm();
+ Zig::GlobalObject* global = jsDynamicCast<Zig::GlobalObject*>(globalObject);
+ if (UNLIKELY(!global)) {
+ global = Bun__getDefaultGlobal();
+ }
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+
+ if (JSValue stdin = global->processObject()->getIfPropertyExists(globalObject, Identifier::fromString(vm, "stdin"_s))) {
+ RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
+
+ if (!stdin.isObject()) {
+ throwTypeError(globalObject, throwScope, "stdin is not an object"_s);
+ return JSValue::encode(jsUndefined());
+ }
- return thisObject->putDirect(vm, clientData->builtinNames().argvPublicName(),
- JSC::JSValue::decode(value));
+ JSValue resumeValue = stdin.getObject()->getIfPropertyExists(globalObject, Identifier::fromString(vm, "resume"_s));
+ RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
+ if (!resumeValue.isUndefinedOrNull()) {
+ auto resumeFunction = jsDynamicCast<JSFunction*>(resumeValue);
+ if (UNLIKELY(!resumeFunction)) {
+ throwTypeError(globalObject, throwScope, "stdin.resume is not a function"_s);
+ return JSValue::encode(jsUndefined());
+ }
+
+ auto callData = getCallData(resumeFunction);
+
+ MarkedArgumentBuffer args;
+ JSC::call(globalObject, resumeFunction, callData, stdin, args);
+ RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
+ }
+
+ RELEASE_AND_RETURN(throwScope, JSValue::encode(stdin));
+ }
+
+ RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}
-JSC_DEFINE_CUSTOM_GETTER(Process_getPID, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+JSC_DEFINE_HOST_FUNCTION(Process_stubEmptyFunction, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
- return JSC::JSValue::encode(JSC::JSValue(getpid()));
+ return JSValue::encode(jsUndefined());
}
-JSC_DEFINE_CUSTOM_GETTER(Process_getPPID, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+JSC_DEFINE_HOST_FUNCTION(Process_stubFunctionReturningArray, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
- return JSC::JSValue::encode(JSC::JSValue(getppid()));
+ return JSValue::encode(JSC::constructEmptyArray(globalObject, nullptr));
}
-#if !defined(BUN_WEBKIT_VERSION)
-#define BUN_WEBKIT_VERSION "unknown"
-#endif
+static JSValue Process_stubEmptyObject(VM& vm, JSObject* processObject)
+{
+ return JSC::constructEmptyObject(processObject->globalObject());
+}
-JSC_DEFINE_CUSTOM_GETTER(Process_getVersionsLazy,
- (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
- JSC::PropertyName))
+static JSValue Process_stubEmptyArray(VM& vm, JSObject* processObject)
{
- JSC::VM& vm = globalObject->vm();
- auto clientData = WebCore::clientData(vm);
+ return JSC::constructEmptyArray(processObject->globalObject(), nullptr);
+}
- Zig::Process* thisObject = JSC::jsDynamicCast<Zig::Process*>(JSValue::decode(thisValue));
- if (!thisObject) {
- return JSValue::encode(JSC::jsUndefined());
+static JSValue Process_stubEmptySet(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ return JSSet::create(vm, globalObject->setStructure());
+}
+
+static JSValue constructMemoryUsage(VM& vm, JSObject* processObject)
+{
+ auto* globalObject = processObject->globalObject();
+ JSC::JSFunction* memoryUsage = JSC::JSFunction::create(vm, globalObject, 0,
+ String("memoryUsage"_s), Process_functionMemoryUsage, ImplementationVisibility::Public);
+
+ JSC::JSFunction* rss = JSC::JSFunction::create(vm, globalObject, 0,
+ String("rss"_s), Process_functionMemoryUsageRSS, ImplementationVisibility::Public);
+
+ memoryUsage->putDirect(vm, JSC::Identifier::fromString(vm, "rss"_s), rss, JSC::PropertyAttribute::Function | 0);
+ return memoryUsage;
+}
+
+static JSValue constructFeatures(VM& vm, JSObject* processObject)
+{
+ // {
+ // inspector: true,
+ // debug: false,
+ // uv: true,
+ // ipv6: true,
+ // tls_alpn: true,
+ // tls_sni: true,
+ // tls_ocsp: true,
+ // tls: true,
+ // cached_builtins: [Getter]
+ // }
+ auto* globalObject = processObject->globalObject();
+ auto* object = constructEmptyObject(globalObject);
+
+ object->putDirect(vm, Identifier::fromString(vm, "inspector"_s), jsBoolean(true));
+#ifdef BUN_DEBUG
+ object->putDirect(vm, Identifier::fromString(vm, "debug"_s), jsBoolean(true));
+#else
+ object->putDirect(vm, Identifier::fromString(vm, "debug"_s), jsBoolean(false));
+#endif
+ // lying
+ object->putDirect(vm, Identifier::fromString(vm, "uv"_s), jsBoolean(true));
+
+ object->putDirect(vm, Identifier::fromString(vm, "ipv6"_s), jsBoolean(true));
+ object->putDirect(vm, Identifier::fromString(vm, "tls_alpn"_s), jsBoolean(true));
+ object->putDirect(vm, Identifier::fromString(vm, "tls_sni"_s), jsBoolean(true));
+ object->putDirect(vm, Identifier::fromString(vm, "tls_ocsp"_s), jsBoolean(true));
+ object->putDirect(vm, Identifier::fromString(vm, "tls"_s), jsBoolean(true));
+ object->putDirect(vm, Identifier::fromString(vm, "cached_builtins"_s), jsBoolean(true));
+
+ return object;
+}
+
+static int _debugPort;
+
+JSC_DEFINE_CUSTOM_GETTER(processDebugPort, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+{
+ if (_debugPort == 0) {
+ _debugPort = 9229;
}
- auto scope = DECLARE_THROW_SCOPE(vm);
- JSC::JSObject* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 19);
+ return JSC::JSValue::encode(jsNumber(_debugPort));
+}
- object->putDirect(vm, JSC::Identifier::fromString(vm, "node"_s),
- JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(REPORTED_NODE_VERSION))));
- object->putDirect(
- vm, JSC::Identifier::fromString(vm, "bun"_s),
- JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(Bun__version + 1 /* prefix with v */))));
- object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s),
- JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString(BUN_WEBKIT_VERSION))));
- object->putDirect(vm, JSC::Identifier::fromString(vm, "boringssl"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_boringssl))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "libarchive"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_libarchive))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "mimalloc"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_mimalloc))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "picohttpparser"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_picohttpparser))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "uwebsockets"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_uws))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "webkit"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_webkit))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "zig"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zig))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "zlib"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_zlib))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "tinycc"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_tinycc))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "lolhtml"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_lolhtml))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "ares"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_c_ares))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "usockets"_s),
- JSC::JSValue(JSC::jsString(vm, makeString(Bun__versions_usockets))), 0);
+JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort,
+ (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
+ JSC::EncodedJSValue encodedValue, JSC::PropertyName))
+{
+ auto& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ JSValue value = JSValue::decode(encodedValue);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "v8"_s), JSValue(JSC::jsString(vm, makeString("10.8.168.20-node.8"_s))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "uv"_s), JSValue(JSC::jsString(vm, makeString("1.44.2"_s))), 0);
- object->putDirect(vm, JSC::Identifier::fromString(vm, "napi"_s), JSValue(JSC::jsString(vm, makeString("8"_s))), 0);
+ if (!value.isInt32()) {
+ throwRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s);
+ return false;
+ }
- object->putDirect(vm, JSC::Identifier::fromString(vm, "modules"_s),
- JSC::JSValue(JSC::jsOwnedString(vm, makeAtomString("108"))));
+ int port = value.asInt32();
- thisObject->putDirect(vm, clientData->builtinNames().versionsPublicName(), object, 0);
+ if (port != 0) {
+ if (port < 1024 || port > 65535) {
+ throwRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s);
+ return false;
+ }
+ }
- RETURN_IF_EXCEPTION(scope, {});
+ _debugPort = port;
+ return true;
+}
- return JSValue::encode(object);
+JSC_DEFINE_CUSTOM_GETTER(processTitle, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+{
+ ZigString str;
+ Bun__Process__getTitle(globalObject, &str);
+ return JSValue::encode(Zig::toJSStringValue(str, globalObject));
}
-JSC_DEFINE_CUSTOM_SETTER(Process_setVersionsLazy,
+
+JSC_DEFINE_CUSTOM_SETTER(setProcessTitle,
(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
JSC::EncodedJSValue value, JSC::PropertyName))
{
JSC::VM& vm = globalObject->vm();
- auto clientData = WebCore::clientData(vm);
- Zig::Process* thisObject = JSC::jsDynamicCast<Zig::Process*>(JSValue::decode(thisValue));
- if (!thisObject) {
- return JSValue::encode(JSC::jsUndefined());
+ JSC::JSObject* thisObject = JSC::jsDynamicCast<JSC::JSObject*>(JSValue::decode(thisValue));
+ JSC::JSString* jsString = JSC::jsDynamicCast<JSC::JSString*>(JSValue::decode(value));
+ if (!thisObject || !jsString) {
+ return false;
}
- thisObject->putDirect(vm, clientData->builtinNames().versionsPublicName(),
- JSC::JSValue::decode(value), 0);
+ ZigString str = Zig::toZigString(jsString, globalObject);
+ Bun__Process__setTitle(globalObject, &str);
return true;
}
-static JSC_DEFINE_HOST_FUNCTION(Process_functionCwd,
+JSC_DEFINE_HOST_FUNCTION(Process_functionCwd,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
@@ -1091,4 +1477,88 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionCwd,
return JSC::JSValue::encode(result);
}
+/* Source for Process.lut.h
+@begin processObjectTable
+ abort Process_functionAbort Function 1
+ allowedNodeEnvironmentFlags Process_stubEmptySet PropertyCallback
+ arch constructArch PropertyCallback
+ argv constructArgv PropertyCallback
+ argv0 constructArgv0 PropertyCallback
+ assert Process_functionAssert Function 1
+ binding JSBuiltin Function 1
+ browser constructBrowser PropertyCallback
+ chdir Process_functionChdir Function 1
+ config constructProcessConfigObject PropertyCallback
+ cpuUsage Process_functionCpuUsage Function 1
+ cwd Process_functionCwd Function 1
+ debugPort processDebugPort CustomAccessor
+ dlopen Process_functionDlopen Function 1
+ emitWarning Process_emitWarning Function 1
+ env constructEnv PropertyCallback
+ execArgv constructExecArgv PropertyCallback
+ execPath constructExecPath PropertyCallback
+ exit Process_functionExit Function 1
+ exitCode processExitCode CustomAccessor
+ features constructFeatures PropertyCallback
+ getActiveResourcesInfo Process_stubFunctionReturningArray Function 0
+ getegid Process_functiongetegid Function 0
+ geteuid Process_functiongeteuid Function 0
+ getgid Process_functiongetgid Function 0
+ getgroups Process_functiongetgroups Function 0
+ getuid Process_functiongetuid Function 0
+ hrtime constructProcessHrtimeObject PropertyCallback
+ isBun constructIsBun PropertyCallback
+ mainModule JSBuiltin ReadOnly|Builtin|Accessor|Function 0
+ memoryUsage constructMemoryUsage PropertyCallback
+ moduleLoadList Process_stubEmptyArray PropertyCallback
+ nextTick Process_functionNextTick Function 1
+ openStdin Process_functionOpenStdin Function 0
+ pid constructPid PropertyCallback
+ platform constructPlatform PropertyCallback
+ ppid constructPpid PropertyCallback
+ reallyExit Process_functionReallyExit Function 1
+ release constructProcessReleaseObject PropertyCallback
+ revision constructRevision PropertyCallback
+ setSourceMapsEnabled Process_stubEmptyFunction Function 1
+ stderr constructStderr PropertyCallback
+ stdin constructStdin PropertyCallback
+ stdout constructStdout PropertyCallback
+ title processTitle CustomAccessor
+ umask Process_functionUmask Function 1
+ uptime Process_functionUptime Function 1
+ version constructVersion PropertyCallback
+ versions constructVersions PropertyCallback
+ _debugEnd Process_stubEmptyFunction Function 0
+ _debugProcess Process_stubEmptyFunction Function 0
+ _fatalException Process_stubEmptyFunction Function 1
+ _getActiveRequests Process_stubFunctionReturningArray Function 0
+ _getActiveHandles Process_stubFunctionReturningArray Function 0
+ _linkedBinding Process_stubEmptyFunction Function 0
+ _preload_modules Process_stubEmptyObject PropertyCallback
+ _rawDebug Process_stubEmptyFunction Function 0
+ _startProfilerIdleNotifier Process_stubEmptyFunction Function 0
+ _stopProfilerIdleNotifier Process_stubEmptyFunction Function 0
+ _tickCallback Process_stubEmptyFunction Function 0
+@end
+*/
+
+#include "Process.lut.h"
+const JSC::ClassInfo Process::s_info = { "Process"_s, &Base::s_info, &processObjectTable, nullptr,
+ CREATE_METHOD_TABLE(Process) };
+
+void Process::finishCreation(JSC::VM& vm)
+{
+ Base::finishCreation(vm);
+
+ this->cpuUsageStructure.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::Structure>::Initializer& init) {
+ init.set(constructCPUUsageStructure(init.vm, init.owner->globalObject()));
+ });
+
+ this->memoryUsageStructure.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::Structure>::Initializer& init) {
+ init.set(constructMemoryUsageStructure(init.vm, init.owner->globalObject()));
+ });
+
+ this->putDirect(vm, vm.propertyNames->toStringTagSymbol, jsString(vm, String("process"_s)), 0);
+}
+
} // namespace Zig
diff --git a/src/bun.js/bindings/Process.h b/src/bun.js/bindings/Process.h
index 322b39078..fbad9b1ff 100644
--- a/src/bun.js/bindings/Process.h
+++ b/src/bun.js/bindings/Process.h
@@ -28,7 +28,7 @@ public:
~Process();
- static constexpr unsigned StructureFlags = Base::StructureFlags;
+ static constexpr unsigned StructureFlags = Base::StructureFlags | HasStaticPropertyTable;
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject,
JSC::JSValue prototype)
@@ -45,6 +45,24 @@ public:
return accessor;
}
+ LazyProperty<JSObject, Structure> cpuUsageStructure;
+ LazyProperty<JSObject, Structure> memoryUsageStructure;
+
+ DECLARE_VISIT_CHILDREN;
+
+ template<typename, SubspaceAccess mode>
+ static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
+ {
+ if constexpr (mode == JSC::SubspaceAccess::Concurrently)
+ return nullptr;
+ return WebCore::subspaceForImpl<Process, WebCore::UseCustomHeapCellType::No>(
+ vm,
+ [](auto& spaces) { return spaces.m_clientSubspaceForProcessObject.get(); },
+ [](auto& spaces, auto&& space) { spaces.m_clientSubspaceForProcessObject = std::forward<decltype(space)>(space); },
+ [](auto& spaces) { return spaces.m_subspaceForProcessObject.get(); },
+ [](auto& spaces, auto&& space) { spaces.m_subspaceForProcessObject = std::forward<decltype(space)>(space); });
+ }
+
void finishCreation(JSC::VM& vm);
};
diff --git a/src/bun.js/bindings/Process.lut.h b/src/bun.js/bindings/Process.lut.h
new file mode 100644
index 000000000..81eb1b7b9
--- /dev/null
+++ b/src/bun.js/bindings/Process.lut.h
@@ -0,0 +1,211 @@
+// File generated via `make generate-builtins`
+static const struct CompactHashIndex processObjectTableIndex[142] = {
+ { 43, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 15, 129 },
+ { -1, -1 },
+ { -1, -1 },
+ { 18, 138 },
+ { -1, -1 },
+ { 45, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 3, 141 },
+ { 1, 128 },
+ { -1, -1 },
+ { 59, -1 },
+ { -1, -1 },
+ { 10, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 31, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 52, -1 },
+ { 27, -1 },
+ { 12, -1 },
+ { -1, -1 },
+ { 19, -1 },
+ { -1, -1 },
+ { 14, 137 },
+ { -1, -1 },
+ { 36, -1 },
+ { -1, -1 },
+ { 38, -1 },
+ { 55, -1 },
+ { 35, -1 },
+ { 6, 139 },
+ { -1, -1 },
+ { 51, -1 },
+ { 4, -1 },
+ { 47, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 2, -1 },
+ { 7, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 40, -1 },
+ { -1, -1 },
+ { 37, -1 },
+ { -1, -1 },
+ { 0, -1 },
+ { 26, 135 },
+ { 16, 130 },
+ { 39, -1 },
+ { -1, -1 },
+ { 23, -1 },
+ { 11, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 58, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 30, 136 },
+ { -1, -1 },
+ { 29, -1 },
+ { 22, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 24, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 20, -1 },
+ { -1, -1 },
+ { 5, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 48, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 13, 131 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 9, -1 },
+ { 25, 133 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 21, 134 },
+ { -1, -1 },
+ { -1, -1 },
+ { -1, -1 },
+ { 46, 140 },
+ { -1, -1 },
+ { 17, -1 },
+ { 8, -1 },
+ { 28, -1 },
+ { 32, 132 },
+ { 33, -1 },
+ { 34, -1 },
+ { 41, -1 },
+ { 42, -1 },
+ { 44, -1 },
+ { 49, -1 },
+ { 50, -1 },
+ { 53, -1 },
+ { 54, -1 },
+ { 56, -1 },
+ { 57, -1 },
+};
+
+static const struct HashTableValue processObjectTableValues[60] = {
+ { "abort"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionAbort, 1 } },
+ { "allowedNodeEnvironmentFlags"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptySet } },
+ { "arch"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArch } },
+ { "argv"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArgv } },
+ { "argv0"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArgv0 } },
+ { "assert"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionAssert, 1 } },
+ { "binding"_s, ((static_cast<unsigned>(PropertyAttribute::Function)) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, NoIntrinsic, { HashTableValue::BuiltinGeneratorType, processObjectBindingCodeGenerator, 1 } },
+ { "browser"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructBrowser } },
+ { "chdir"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionChdir, 1 } },
+ { "config"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessConfigObject } },
+ { "cpuUsage"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionCpuUsage, 1 } },
+ { "cwd"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionCwd, 1 } },
+ { "debugPort"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processDebugPort, setProcessDebugPort } },
+ { "dlopen"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionDlopen, 1 } },
+ { "emitWarning"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_emitWarning, 1 } },
+ { "env"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructEnv } },
+ { "execArgv"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructExecArgv } },
+ { "execPath"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructExecPath } },
+ { "exit"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionExit, 1 } },
+ { "exitCode"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processExitCode, setProcessExitCode } },
+ { "features"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructFeatures } },
+ { "getActiveResourcesInfo"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubFunctionReturningArray, 0 } },
+ { "getegid"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetegid, 0 } },
+ { "geteuid"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongeteuid, 0 } },
+ { "getgid"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetgid, 0 } },
+ { "getgroups"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetgroups, 0 } },
+ { "getuid"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functiongetuid, 0 } },
+ { "hrtime"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessHrtimeObject } },
+ { "isBun"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructIsBun } },
+ { "mainModule"_s, ((static_cast<unsigned>(PropertyAttribute::ReadOnly|PropertyAttribute::Builtin|PropertyAttribute::Accessor|PropertyAttribute::Function)) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, NoIntrinsic, { HashTableValue::BuiltinGeneratorType, processObjectMainModuleCodeGenerator, 0 } },
+ { "memoryUsage"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructMemoryUsage } },
+ { "moduleLoadList"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptyArray } },
+ { "nextTick"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionNextTick, 1 } },
+ { "openStdin"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionOpenStdin, 0 } },
+ { "pid"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructPid } },
+ { "platform"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructPlatform } },
+ { "ppid"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructPpid } },
+ { "reallyExit"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionReallyExit, 1 } },
+ { "release"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessReleaseObject } },
+ { "revision"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructRevision } },
+ { "setSourceMapsEnabled"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 1 } },
+ { "stderr"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructStderr } },
+ { "stdin"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructStdin } },
+ { "stdout"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructStdout } },
+ { "title"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processTitle, setProcessTitle } },
+ { "umask"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionUmask, 1 } },
+ { "uptime"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionUptime, 1 } },
+ { "version"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructVersion } },
+ { "versions"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructVersions } },
+ { "_debugEnd"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+ { "_debugProcess"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+ { "_fatalException"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 1 } },
+ { "_getActiveRequests"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubFunctionReturningArray, 0 } },
+ { "_getActiveHandles"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubFunctionReturningArray, 0 } },
+ { "_linkedBinding"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+ { "_preload_modules"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptyObject } },
+ { "_rawDebug"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+ { "_startProfilerIdleNotifier"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+ { "_stopProfilerIdleNotifier"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+ { "_tickCallback"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_stubEmptyFunction, 0 } },
+};
+
+static const struct HashTable processObjectTable =
+ { 60, 127, true, nullptr, processObjectTableValues, processObjectTableIndex };
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 4bb5445e1..3291b204e 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -3160,11 +3160,7 @@ void GlobalObject::finishCreation(VM& vm)
Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(init.owner);
auto* process = Zig::Process::create(
*globalObject, Zig::Process::createStructure(init.vm, init.owner, WebCore::JSEventEmitter::prototype(init.vm, *globalObject)));
- process->putDirectCustomAccessor(init.vm, JSC::Identifier::fromString(init.vm, "env"_s),
- JSC::CustomGetterSetter::create(init.vm, lazyProcessEnvGetter, lazyProcessEnvSetter),
- JSC::PropertyAttribute::DontDelete
- | JSC::PropertyAttribute::CustomValue
- | 0);
+
init.set(process);
});
diff --git a/src/bun.js/bindings/webcore/DOMClientIsoSubspaces.h b/src/bun.js/bindings/webcore/DOMClientIsoSubspaces.h
index 65875d091..82a2c6a24 100644
--- a/src/bun.js/bindings/webcore/DOMClientIsoSubspaces.h
+++ b/src/bun.js/bindings/webcore/DOMClientIsoSubspaces.h
@@ -38,6 +38,7 @@ public:
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForJSMockImplementation;
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForJSMockFunction;
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForMockWithImplementationCleanupData;
+ std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForProcessObject;
#include "ZigGeneratedClasses+DOMClientIsoSubspaces.h"
/* --- bun --- */
diff --git a/src/bun.js/bindings/webcore/DOMIsoSubspaces.h b/src/bun.js/bindings/webcore/DOMIsoSubspaces.h
index 433832450..f1b290d25 100644
--- a/src/bun.js/bindings/webcore/DOMIsoSubspaces.h
+++ b/src/bun.js/bindings/webcore/DOMIsoSubspaces.h
@@ -38,6 +38,7 @@ public:
std::unique_ptr<IsoSubspace> m_subspaceForJSMockImplementation;
std::unique_ptr<IsoSubspace> m_subspaceForJSMockFunction;
std::unique_ptr<IsoSubspace> m_subspaceForMockWithImplementationCleanupData;
+ std::unique_ptr<IsoSubspace> m_subspaceForProcessObject;
#include "ZigGeneratedClasses+DOMIsoSubspaces.h"
/*-- BUN --*/
diff --git a/src/bun.js/modules/ProcessModule.h b/src/bun.js/modules/ProcessModule.h
index 2df74598a..fab0298ae 100644
--- a/src/bun.js/modules/ProcessModule.h
+++ b/src/bun.js/modules/ProcessModule.h
@@ -44,11 +44,19 @@ inline void generateProcessSourceCode(JSC::JSGlobalObject *lexicalGlobalObject,
reinterpret_cast<GlobalObject *>(lexicalGlobalObject);
JSC::JSObject *process = globalObject->processObject();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ if (!process->staticPropertiesReified()) {
+ process->reifyAllStaticProperties(globalObject);
+ if (scope.exception())
+ return;
+ }
PropertyNameArray properties(vm, PropertyNameMode::Strings,
PrivateSymbolMode::Exclude);
process->getPropertyNames(globalObject, properties,
DontEnumPropertiesMode::Exclude);
+ if (scope.exception())
+ return;
exportNames.append(vm.propertyNames->defaultKeyword);
exportValues.append(process);
diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig
index 553b292d6..642039ba5 100644
--- a/src/bun.js/node/types.zig
+++ b/src/bun.js/node/types.zig
@@ -541,9 +541,18 @@ pub const Encoding = enum(u8) {
const result = JSC.ZigString.init(out).toValueGC(globalThis);
return result;
},
- else => {
- globalThis.throwInvalidArguments("Unexpected encoding", .{});
- return JSC.JSValue.zero;
+ .buffer => {
+ return JSC.ArrayBuffer.createBuffer(globalThis, input);
+ },
+
+ inline else => |enc| {
+ const res = JSC.WebCore.Encoder.toString(input.ptr, size, globalThis, enc);
+ if (res.isError()) {
+ globalThis.throwValue(res);
+ return .zero;
+ }
+
+ return res;
},
}
}
@@ -571,9 +580,18 @@ pub const Encoding = enum(u8) {
const result = JSC.ZigString.init(out).toValueGC(globalThis);
return result;
},
- else => {
- globalThis.throwInvalidArguments("Unexpected encoding", .{});
- return JSC.JSValue.zero;
+ .buffer => {
+ return JSC.ArrayBuffer.createBuffer(globalThis, input);
+ },
+ inline else => |enc| {
+ const res = JSC.WebCore.Encoder.toString(input.ptr, input.len, globalThis, enc);
+
+ if (res.isError()) {
+ globalThis.throwValue(res);
+ return .zero;
+ }
+
+ return res;
},
}
}
diff --git a/src/bun.js/rare_data.zig b/src/bun.js/rare_data.zig
index ddda96bf4..3b29896a4 100644
--- a/src/bun.js/rare_data.zig
+++ b/src/bun.js/rare_data.zig
@@ -9,6 +9,7 @@ const std = @import("std");
const BoringSSL = @import("root").bun.BoringSSL;
const bun = @import("root").bun;
const WebSocketClientMask = @import("../http/websocket_http_client.zig").Mask;
+const UUID = @import("./uuid.zig");
boring_ssl_engine: ?*BoringSSL.ENGINE = null,
editor_context: EditorContext = EditorContext{},
@@ -47,13 +48,16 @@ pub fn filePolls(this: *RareData, vm: *JSC.VirtualMachine) *JSC.FilePoll.HiveArr
};
}
-pub fn nextUUID(this: *RareData) [16]u8 {
+pub fn nextUUID(this: *RareData) UUID {
if (this.entropy_cache == null) {
this.entropy_cache = default_allocator.create(EntropyCache) catch unreachable;
this.entropy_cache.?.init();
}
- return this.entropy_cache.?.get();
+ this.entropy_cache.?.fill();
+
+ const bytes = this.entropy_cache.?.get();
+ return UUID.initWith(&bytes);
}
pub fn entropySlice(this: *RareData, len: usize) []u8 {
diff --git a/src/bun.js/uuid.zig b/src/bun.js/uuid.zig
index e8bdff661..e38ed567f 100644
--- a/src/bun.js/uuid.zig
+++ b/src/bun.js/uuid.zig
@@ -18,6 +18,16 @@ pub fn init() UUID {
uuid.bytes[6] = (uuid.bytes[6] & 0x0f) | 0x40;
// Variant 1
uuid.bytes[8] = (uuid.bytes[8] & 0x3f) | 0x80;
+
+ return uuid;
+}
+
+pub fn initWith(bytes: *const [16]u8) UUID {
+ var uuid = UUID{ .bytes = bytes.* };
+
+ uuid.bytes[6] = (uuid.bytes[6] & 0x0f) | 0x40;
+ uuid.bytes[8] = (uuid.bytes[8] & 0x3f) | 0x80;
+
return uuid;
}
diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig
index fd69b0262..8cb9ec80a 100644
--- a/src/bun.js/webcore.zig
+++ b/src/bun.js/webcore.zig
@@ -366,7 +366,6 @@ pub const Prompt = struct {
};
pub const Crypto = struct {
- const UUID = @import("./uuid.zig");
const BoringSSL = @import("root").bun.BoringSSL;
pub const Class = JSC.NewClass(
void,
@@ -693,9 +692,8 @@ pub const Crypto = struct {
_: []const JSC.JSValue,
) JSC.JSValue {
var out: [36]u8 = undefined;
- const uuid: UUID = .{
- .bytes = globalThis.bunVM().rareData().nextUUID(),
- };
+ const uuid = globalThis.bunVM().rareData().nextUUID();
+
uuid.print(&out);
return JSC.ZigString.init(&out).toValueGC(globalThis);
}
@@ -723,9 +721,8 @@ pub const Crypto = struct {
_: *anyopaque,
) callconv(.C) JSC.JSValue {
var out: [36]u8 = undefined;
- const uuid: UUID = .{
- .bytes = globalThis.bunVM().rareData().nextUUID(),
- };
+ const uuid = globalThis.bunVM().rareData().nextUUID();
+
uuid.print(&out);
return JSC.ZigString.init(&out).toValueGC(globalThis);
}
diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig
index 86b5414e3..ef2520049 100644
--- a/src/bun.js/webcore/blob.zig
+++ b/src/bun.js/webcore/blob.zig
@@ -249,7 +249,7 @@ pub const Blob = struct {
var hex_buf: [70]u8 = undefined;
const boundary = brk: {
- var random = globalThis.bunVM().rareData().nextUUID();
+ var random = globalThis.bunVM().rareData().nextUUID().bytes;
var formatter = std.fmt.fmtSliceHexLower(&random);
break :brk std.fmt.bufPrint(&hex_buf, "-WebkitFormBoundary{any}", .{formatter}) catch unreachable;
};
diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig
index bb1180acb..dd47ccc29 100644
--- a/src/bun.js/webcore/encoding.zig
+++ b/src/bun.js/webcore/encoding.zig
@@ -829,23 +829,18 @@ pub const Encoder = struct {
return ZigString.init(input).toValueGC(global);
}
- if (input.len < 512) {
- var buf: [512]u8 = undefined;
- var to = buf[0..input.len];
- strings.copyLatin1IntoASCII(to, input);
- return ZigString.init(to).toValueGC(global);
- }
-
- var to = allocator.alloc(u8, len) catch return ZigString.init("Out of memory").toErrorInstance(global);
- strings.copyLatin1IntoASCII(to, input);
- return ZigString.init(to).toExternalValue(global);
+ var str = bun.String.createUninitialized(.latin1, len) orelse return ZigString.init("Out of memory").toErrorInstance(global);
+ defer str.deref();
+ strings.copyLatin1IntoASCII(@constCast(str.latin1()), input);
+ return str.toJS(global);
},
.latin1 => {
- var to = allocator.alloc(u8, len) catch return ZigString.init("Out of memory").toErrorInstance(global);
+ var str = bun.String.createUninitialized(.latin1, len) orelse return ZigString.init("Out of memory").toErrorInstance(global);
+ defer str.deref();
- @memcpy(to, input_ptr[0..to.len]);
+ @memcpy(@constCast(str.latin1()), input_ptr[0..len]);
- return ZigString.init(to).toExternalValue(global);
+ return str.toJS(global);
},
.buffer, .utf8 => {
const converted = strings.toUTF16Alloc(allocator, input, false) catch return ZigString.init("Out of memory").toErrorInstance(global);
@@ -861,21 +856,22 @@ pub const Encoder = struct {
// Avoid incomplete characters
if (len / 2 == 0) return ZigString.Empty.toValue(global);
- var output = allocator.alloc(u16, len / 2) catch return ZigString.init("Out of memory").toErrorInstance(global);
- var output_bytes = std.mem.sliceAsBytes(output);
+ var output = bun.String.createUninitialized(.utf16, len / 2) orelse return ZigString.init("Out of memory").toErrorInstance(global);
+ defer output.deref();
+ var output_bytes = std.mem.sliceAsBytes(@constCast(output.utf16()));
output_bytes[output_bytes.len - 1] = 0;
@memcpy(output_bytes, input_ptr[0..output_bytes.len]);
- return ZigString.toExternalU16(output.ptr, output.len, global);
+ return output.toJS(global);
},
.hex => {
- var output = allocator.alloc(u8, input.len * 2) catch return ZigString.init("Out of memory").toErrorInstance(global);
+ var str = bun.String.createUninitialized(.latin1, len * 2) orelse return ZigString.init("Out of memory").toErrorInstance(global);
+ defer str.deref();
+ var output = @constCast(str.latin1());
const wrote = strings.encodeBytesToHex(output, input);
std.debug.assert(wrote == output.len);
- var val = ZigString.init(output);
- val.mark();
- return val.toExternalValue(global);
+ return str.toJS(global);
},
.base64url => {