diff options
author | 2022-09-25 14:56:22 -0700 | |
---|---|---|
committer | 2022-09-25 14:56:22 -0700 | |
commit | 5ef36f1b6fa64d838f988d4b84110fae4c7015ac (patch) | |
tree | dc52d18ede3c567d97acf7798a8fba6104ba359b /src | |
parent | 7cc772cd391de428a9f9010e40abf0f8d3ba625a (diff) | |
download | bun-5ef36f1b6fa64d838f988d4b84110fae4c7015ac.tar.gz bun-5ef36f1b6fa64d838f988d4b84110fae4c7015ac.tar.zst bun-5ef36f1b6fa64d838f988d4b84110fae4c7015ac.zip |
Implement `isatty` in `node:tty`
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/bindings/ModuleLoader.cpp | 8 | ||||
-rw-r--r-- | src/bun.js/bindings/exports.zig | 1 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-handwritten.h | 1 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 5 | ||||
-rw-r--r-- | src/bun.js/modules/TTYModule.h | 69 |
5 files changed, 84 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ModuleLoader.cpp b/src/bun.js/bindings/ModuleLoader.cpp index 6f9a767e4..cb5ff0864 100644 --- a/src/bun.js/bindings/ModuleLoader.cpp +++ b/src/bun.js/bindings/ModuleLoader.cpp @@ -33,6 +33,7 @@ #include "../modules/StringDecoderModule.h" #include "../modules/ObjectModule.h" #include "../modules/NodeModuleModule.h" +#include "../modules/TTYModule.h" namespace Bun { using namespace Zig; @@ -395,6 +396,13 @@ static JSValue fetchSourceCode( return rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))); } + case SyntheticModuleType::TTY: { + auto source = JSC::SourceCode( + JSC::SyntheticSourceProvider::create(generateTTYSourceCode, + JSC::SourceOrigin(), WTFMove(moduleKey))); + + return rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))); + } case SyntheticModuleType::Process: { auto source = JSC::SourceCode( JSC::SyntheticSourceProvider::create(generateProcessSourceCode, diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 113160a7e..b2d2acccb 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -252,6 +252,7 @@ pub const ResolvedSource = extern struct { @"node:events" = 1026, @"node:string_decoder" = 1027, @"node:module" = 1028, + @"node:tty" = 1029, }; }; diff --git a/src/bun.js/bindings/headers-handwritten.h b/src/bun.js/bindings/headers-handwritten.h index f1716c7fd..9d97ba7f6 100644 --- a/src/bun.js/bindings/headers-handwritten.h +++ b/src/bun.js/bindings/headers-handwritten.h @@ -204,6 +204,7 @@ enum SyntheticModuleType : uint64_t { Events = 1026, StringDecoder = 1027, Module = 1028, + TTY = 1029, }; extern "C" const char* Bun__userAgent; diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index b9c381a14..ae1832b21 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -823,6 +823,7 @@ pub const VirtualMachine = struct { .@"node:module" => return jsSyntheticModule(.@"node:module"), .@"node:events" => return jsSyntheticModule(.@"node:events"), .@"node:process" => return jsSyntheticModule(.@"node:process"), + .@"node:tty" => return jsSyntheticModule(.@"node:tty"), .@"node:stream" => { return ResolvedSource{ .allocator = null, @@ -2625,6 +2626,7 @@ pub const HardcodedModule = enum { @"node:string_decoder", @"node:timers", @"node:timers/promises", + @"node:tty", @"node:url", @"undici", @"ws", @@ -2662,6 +2664,7 @@ pub const HardcodedModule = enum { .{ "node:string_decoder", HardcodedModule.@"node:string_decoder" }, .{ "node:timers", HardcodedModule.@"node:timers" }, .{ "node:timers/promises", HardcodedModule.@"node:timers/promises" }, + .{ "node:tty", HardcodedModule.@"node:tty" }, .{ "node:url", HardcodedModule.@"node:url" }, .{ "undici", HardcodedModule.@"undici" }, .{ "ws", HardcodedModule.@"ws" }, @@ -2705,6 +2708,7 @@ pub const HardcodedModule = enum { .{ "node:string_decoder", "node:string_decoder" }, .{ "node:timers", "node:timers" }, .{ "node:timers/promises", "node:timers/promises" }, + .{ "node:tty", "node:tty" }, .{ "node:url", "node:url" }, .{ "os", "node:os" }, .{ "path", "node:path" }, @@ -2718,6 +2722,7 @@ pub const HardcodedModule = enum { .{ "string_decoder", "node:string_decoder" }, .{ "timers", "node:timers" }, .{ "timers/promises", "node:timers/promises" }, + .{ "tty", "node:tty" }, .{ "undici", "undici" }, .{ "url", "node:url" }, .{ "ws", "ws" }, 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<JSC::Identifier, 4> &exportNames, + JSC::MarkedArgumentBuffer &exportValues) { + JSC::VM &vm = lexicalGlobalObject->vm(); + GlobalObject *globalObject = + reinterpret_cast<GlobalObject *>(lexicalGlobalObject); + + auto *tty = JSC::constructEmptyObject(globalObject, + globalObject->objectPrototype(), 3); + + auto *isattyFunction = + JSFunction::create(vm, globalObject, 1, "isatty"_s, jsFunctionTty_isatty, + ImplementationVisibility::Public); + + auto *notimpl = JSFunction::create(vm, globalObject, 0, "notimpl"_s, + jsFunctionNotImplementedYet, + ImplementationVisibility::Public, + NoIntrinsic, jsFunctionNotImplementedYet); + + exportNames.append(JSC::Identifier::fromString(vm, "isatty"_s)); + exportValues.append(isattyFunction); + + exportNames.append(JSC::Identifier::fromString(vm, "ReadStream"_s)); + tty->putDirect(vm, JSC::Identifier::fromString(vm, "ReadStream"_s), notimpl); + exportValues.append(notimpl); + + exportNames.append(JSC::Identifier::fromString(vm, "WriteStream"_s)); + tty->putDirect(vm, JSC::Identifier::fromString(vm, "WriteStream"_s), notimpl); + exportValues.append(notimpl); + + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(tty); +} + +} // namespace Zig |