From 5ef36f1b6fa64d838f988d4b84110fae4c7015ac Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sun, 25 Sep 2022 14:56:22 -0700 Subject: Implement `isatty` in `node:tty` --- src/bun.js/modules/TTYModule.h | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/bun.js/modules/TTYModule.h (limited to 'src/bun.js/modules/TTYModule.h') diff --git a/src/bun.js/modules/TTYModule.h b/src/bun.js/modules/TTYModule.h new file mode 100644 index 000000000..fcc65b235 --- /dev/null +++ b/src/bun.js/modules/TTYModule.h @@ -0,0 +1,69 @@ +#include "../bindings/JSBuffer.h" +#include "../bindings/ZigGlobalObject.h" +#include "JavaScriptCore/JSGlobalObject.h" + +#include "JavaScriptCore/ObjectConstructor.h" + +namespace Zig { +using namespace WebCore; + +JSC_DEFINE_HOST_FUNCTION(jsFunctionTty_isatty, (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + if (callFrame->argumentCount() < 1) { + return JSValue::encode(jsBoolean(false)); + } + + auto scope = DECLARE_CATCH_SCOPE(vm); + int fd = callFrame->argument(0).toInt32(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + return JSValue::encode(jsBoolean(isatty(fd))); +} + +JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplementedYet, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + throwException(globalObject, throwScope, + createError(globalObject, "Not implemented yet"_s)); + return JSValue::encode(jsUndefined()); +} + +inline void generateTTYSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, + JSC::Identifier moduleKey, + Vector &exportNames, + JSC::MarkedArgumentBuffer &exportValues) { + JSC::VM &vm = lexicalGlobalObject->vm(); + GlobalObject *globalObject = + reinterpret_cast(lexicalGlobalObject); + + auto *tty = JSC::constructEmptyObject(globalObject, + globalObject->objectPrototype(), 3); + + auto *isattyFunction = + JSFunction::create(vm, globalObject, 1, "isatty"_s, jsFunctionTty_isatty, + ImplementationVisibility::Public); + + auto *notimpl = JSFunction::create(vm, globalObject, 0, "notimpl"_s, + jsFunctionNotImplementedYet, + ImplementationVisibility::Public, + NoIntrinsic, jsFunctionNotImplementedYet); + + exportNames.append(JSC::Identifier::fromString(vm, "isatty"_s)); + exportValues.append(isattyFunction); + + exportNames.append(JSC::Identifier::fromString(vm, "ReadStream"_s)); + tty->putDirect(vm, JSC::Identifier::fromString(vm, "ReadStream"_s), notimpl); + exportValues.append(notimpl); + + exportNames.append(JSC::Identifier::fromString(vm, "WriteStream"_s)); + tty->putDirect(vm, JSC::Identifier::fromString(vm, "WriteStream"_s), notimpl); + exportValues.append(notimpl); + + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(tty); +} + +} // namespace Zig -- cgit v1.2.3