aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/ZigGlobalObject.cpp
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-08-19 00:11:24 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-19 00:11:24 -0700
commitdb09ed15fd561b89b24b979b986e21a04576f7cc (patch)
treeafe32e4eccaf1fe3f2f4cd536c1f2a61efad28c1 /src/bun.js/bindings/ZigGlobalObject.cpp
parentbf517d9f8e993a9ed3a02d21094c3ce76d7953a1 (diff)
downloadbun-db09ed15fd561b89b24b979b986e21a04576f7cc.tar.gz
bun-db09ed15fd561b89b24b979b986e21a04576f7cc.tar.zst
bun-db09ed15fd561b89b24b979b986e21a04576f7cc.zip
tty `ReadStream`, `WriteStream`, and readline rawmode (#4179)
* tty `WriteStream`, `ReadStream`, and rawmode * tests * refactor prototypes * fix failing test * fix test and library usage * more merge * fix child_process test * create pseudo terminal for tty tests * match node logic * handle invalid tty * close descriptors * move tests to another process * fix test again * fix test on linux
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 016da0fd6..f8fc2ea2b 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -109,6 +109,7 @@
#include "NodeVMScript.h"
#include "ProcessIdentifier.h"
#include "SerializedScriptValue.h"
+#include "NodeTTYModule.h"
#include "ZigGeneratedClasses.h"
#include "JavaScriptCore/DateInstance.h"
@@ -1408,6 +1409,35 @@ JSC_DEFINE_HOST_FUNCTION(asyncHooksCleanupLater, (JSC::JSGlobalObject * globalOb
return JSC::JSValue::encode(JSC::jsUndefined());
}
+extern "C" int Bun__ttySetMode(int fd, int mode);
+
+JSC_DEFINE_HOST_FUNCTION(jsTTYSetMode, (JSC::JSGlobalObject * globalObject, CallFrame* callFrame))
+{
+ auto& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+
+ if (callFrame->argumentCount() != 2) {
+ throwTypeError(globalObject, scope, "Expected 2 arguments"_s);
+ return JSValue::encode(jsUndefined());
+ }
+
+ JSValue fd = callFrame->argument(0);
+ if (!fd.isNumber()) {
+ throwTypeError(globalObject, scope, "fd must be a number"_s);
+ return JSValue::encode(jsUndefined());
+ }
+
+ JSValue mode = callFrame->argument(1);
+ if (!mode.isNumber()) {
+ throwTypeError(globalObject, scope, "mode must be a number"_s);
+ return JSValue::encode(jsUndefined());
+ }
+
+ // Nodejs does not throw when ttySetMode fails. An Error event is emitted instead.
+ int err = Bun__ttySetMode(fd.asNumber(), mode.asNumber());
+ return JSValue::encode(jsNumber(err));
+}
+
JSC_DEFINE_CUSTOM_GETTER(noop_getter, (JSGlobalObject*, EncodedJSValue, PropertyName))
{
return JSC::JSValue::encode(JSC::jsUndefined());
@@ -1484,6 +1514,8 @@ JSC_DEFINE_HOST_FUNCTION(jsReceiveMessageOnPort, (JSGlobalObject * lexicalGlobal
return JSC::JSValue::encode(jsUndefined());
}
+extern JSC::EncodedJSValue Process_functionInternalGetWindowSize(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callFrame);
+
// we're trying out a new way to do this lazy loading
// this is $lazy() in js code
static JSC_DEFINE_HOST_FUNCTION(functionLazyLoad,
@@ -1660,6 +1692,18 @@ static JSC_DEFINE_HOST_FUNCTION(functionLazyLoad,
return JSValue::encode(obj);
}
+ if (string == "tty"_s) {
+ auto* obj = constructEmptyObject(globalObject);
+
+ obj->putDirect(vm, PropertyName(Identifier::fromString(vm, "ttySetMode"_s)), JSFunction::create(vm, globalObject, 0, "ttySetMode"_s, jsTTYSetMode, ImplementationVisibility::Public), 1);
+
+ obj->putDirect(vm, PropertyName(Identifier::fromString(vm, "isatty"_s)), JSFunction::create(vm, globalObject, 0, "isatty"_s, jsFunctionTty_isatty, ImplementationVisibility::Public), 1);
+
+ obj->putDirect(vm, PropertyName(Identifier::fromString(vm, "getWindowSize"_s)), JSFunction::create(vm, globalObject, 0, "getWindowSize"_s, Process_functionInternalGetWindowSize, ImplementationVisibility::Public), 2);
+
+ return JSValue::encode(obj);
+ }
+
if (UNLIKELY(string == "noop"_s)) {
auto* obj = constructEmptyObject(globalObject);
obj->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "getterSetter"_s)), JSC::CustomGetterSetter::create(vm, noop_getter, noop_setter), 0);