aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-05 11:35:34 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-05 11:35:34 -0800
commit0ea8de40871052842f2e342b4c452f6746691487 (patch)
treea4e1028f90a801324966172e47b9b20533e4ad7a /src/bun.js
parent71293033474eea83c47b6c6948f991b1dc37ad22 (diff)
downloadbun-0ea8de40871052842f2e342b4c452f6746691487.tar.gz
bun-0ea8de40871052842f2e342b4c452f6746691487.tar.zst
bun-0ea8de40871052842f2e342b4c452f6746691487.zip
Make `console` an `AsyncIterable`
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp99
-rw-r--r--src/bun.js/bindings/bindings.cpp5
-rw-r--r--src/bun.js/bindings/bindings.zig6
-rw-r--r--src/bun.js/bindings/header-gen.zig2
-rw-r--r--src/bun.js/bindings/headers-cpp.h2
-rw-r--r--src/bun.js/bindings/headers.h3
-rw-r--r--src/bun.js/bindings/headers.zig1
-rw-r--r--src/bun.js/builtins/WebCoreJSBuiltins.cpp1
-rw-r--r--src/bun.js/builtins/cpp/ConsoleObjectBuiltins.cpp167
-rw-r--r--src/bun.js/builtins/cpp/ConsoleObjectBuiltins.h138
-rw-r--r--src/bun.js/builtins/cpp/ReadableStreamInternalsBuiltins.cpp53
-rw-r--r--src/bun.js/builtins/cpp/WebCoreJSBuiltins.h4
-rw-r--r--src/bun.js/builtins/js/ConsoleObject.js115
-rw-r--r--src/bun.js/builtins/js/ReadableStreamInternals.js51
14 files changed, 531 insertions, 116 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 403bb249c..a1a11a1a0 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -239,6 +239,42 @@ extern "C" JSC__JSGlobalObject* Zig__GlobalObject__create(JSClassRef* globalObje
return globalObject;
}
+JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync,
+ (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
+{
+
+ auto& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ JSC::JSValue key = callFrame->argument(0);
+
+ auto moduleKey = key.toWTFString(globalObject);
+ RETURN_IF_EXCEPTION(scope, JSValue::encode(JSC::jsUndefined()));
+
+ if (moduleKey.endsWith(".node"_s)) {
+ throwException(globalObject, scope, createTypeError(globalObject, "To load Node-API modules, use require() or process.dlopen instead of importSync."_s));
+ return JSValue::encode(JSC::jsUndefined());
+ }
+
+ auto specifier = Zig::toZigString(moduleKey);
+ ErrorableResolvedSource res;
+ res.success = false;
+ res.result.err.code = 0;
+ res.result.err.ptr = nullptr;
+
+ JSValue result = Bun::fetchSourceCodeSync(
+ reinterpret_cast<Zig::GlobalObject*>(globalObject),
+ &res,
+ &specifier,
+ &specifier);
+
+ if (result.isUndefined() || !result) {
+ return JSValue::encode(result);
+ }
+
+ globalObject->moduleLoader()->provideFetch(globalObject, key, jsCast<JSC::JSSourceCode*>(result)->sourceCode());
+ RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsUndefined()));
+}
+
extern "C" void* Zig__GlobalObject__getModuleRegistryMap(JSC__JSGlobalObject* arg0)
{
if (JSC::JSObject* loader = JSC::jsDynamicCast<JSC::JSObject*>(arg0->moduleLoader())) {
@@ -496,8 +532,6 @@ void GlobalObject::setConsole(void* console)
#pragma mark - Globals
-static JSC_DECLARE_HOST_FUNCTION(functionFulfillModuleSync);
-
JSC_DECLARE_CUSTOM_GETTER(functionLazyLoadStreamPrototypeMap_getter);
JSC_DEFINE_CUSTOM_GETTER(functionLazyLoadStreamPrototypeMap_getter,
@@ -676,8 +710,6 @@ JSC_DEFINE_CUSTOM_GETTER(lazyProcessEnvGetter,
globalObject->processEnvObject());
}
-static JSC_DECLARE_HOST_FUNCTION(functionQueueMicrotask);
-
static JSC_DEFINE_HOST_FUNCTION(functionQueueMicrotask,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -706,8 +738,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionQueueMicrotask,
return JSC::JSValue::encode(JSC::jsUndefined());
}
-static JSC_DECLARE_HOST_FUNCTION(functionSetTimeout);
-
static JSC_DEFINE_HOST_FUNCTION(functionSetTimeout,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -737,8 +767,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionSetTimeout,
return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(job), JSC::JSValue::encode(num));
}
-static JSC_DECLARE_HOST_FUNCTION(functionSetInterval);
-
static JSC_DEFINE_HOST_FUNCTION(functionSetInterval,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -763,8 +791,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionSetInterval,
JSC::JSValue::encode(num));
}
-static JSC_DECLARE_HOST_FUNCTION(functionClearInterval);
-
static JSC_DEFINE_HOST_FUNCTION(functionClearInterval,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -781,8 +807,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionClearInterval,
return Bun__Timer__clearInterval(globalObject, JSC::JSValue::encode(num));
}
-static JSC_DECLARE_HOST_FUNCTION(functionClearTimeout);
-
static JSC_DEFINE_HOST_FUNCTION(functionClearTimeout,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -799,8 +823,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionClearTimeout,
return Bun__Timer__clearTimeout(globalObject, JSC::JSValue::encode(num));
}
-static JSC_DECLARE_HOST_FUNCTION(functionBTOA);
-
static JSC_DEFINE_HOST_FUNCTION(functionBTOA,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -828,8 +850,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionBTOA,
return JSC::JSValue::encode(JSC::jsString(vm, WTF::base64EncodeToString(stringToEncode.latin1())));
}
-static JSC_DECLARE_HOST_FUNCTION(functionATOB);
-
static JSC_DEFINE_HOST_FUNCTION(functionATOB,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -862,8 +882,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionATOB,
return JSC::JSValue::encode(JSC::jsString(vm, WTF::String(decodedData->data(), decodedData->size())));
}
-static JSC_DECLARE_HOST_FUNCTION(functionHashCode);
-
static JSC_DEFINE_HOST_FUNCTION(functionHashCode,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -877,7 +895,6 @@ static JSC_DEFINE_HOST_FUNCTION(functionHashCode,
return JSC::JSValue::encode(jsNumber(view.hash()));
}
-static JSC_DECLARE_HOST_FUNCTION(functionReportError);
static JSC_DEFINE_HOST_FUNCTION(functionReportError,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -1025,7 +1042,6 @@ enum ReadableStreamTag : int32_t {
};
// we're trying out a new way to do this lazy loading
-static JSC_DECLARE_HOST_FUNCTION(functionLazyLoad);
static JSC_DEFINE_HOST_FUNCTION(functionLazyLoad,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -2617,6 +2633,12 @@ void GlobalObject::finishCreation(VM& vm)
// JSC default is 100
errorConstructor->putDirect(vm, vm.propertyNames->stackTraceLimit, jsNumber(DEFAULT_ERROR_STACK_TRACE_LIMIT), JSC::PropertyAttribute::DontEnum | 0);
+
+ JSC::JSValue console = this->get(this, JSC::Identifier::fromString(vm, "console"_s));
+ JSC::JSObject* consoleObject = console.getObject();
+ consoleObject->putDirectBuiltinFunction(vm, this, vm.propertyNames->asyncIteratorSymbol, consoleObjectAsyncIteratorCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete);
+ auto clientData = WebCore::clientData(vm);
+ consoleObject->putDirectBuiltinFunction(vm, this, clientData->builtinNames().writePublicName(), consoleObjectWriteCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::ReadOnly | PropertyAttribute::DontDelete);
}
JSC_DEFINE_HOST_FUNCTION(functionBunPeek,
@@ -2718,7 +2740,6 @@ extern "C" void Bun__setOnEachMicrotaskTick(JSC::VM* vm, void* ptr, void (*callb
// This implementation works the same as setTimeout(myFunction, 0)
// TODO: make it more efficient
// https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
-static JSC_DECLARE_HOST_FUNCTION(functionSetImmediate);
static JSC_DEFINE_HOST_FUNCTION(functionSetImmediate,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
@@ -3503,42 +3524,6 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderImportModule(JSGlobalObject* g
return result;
}
-static JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync,
- (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
-{
-
- auto& vm = globalObject->vm();
- auto scope = DECLARE_THROW_SCOPE(vm);
- JSC::JSValue key = callFrame->argument(0);
-
- auto moduleKey = key.toWTFString(globalObject);
- RETURN_IF_EXCEPTION(scope, JSValue::encode(JSC::jsUndefined()));
-
- if (moduleKey.endsWith(".node"_s)) {
- throwException(globalObject, scope, createTypeError(globalObject, "To load Node-API modules, use require() or process.dlopen instead of importSync."_s));
- return JSValue::encode(JSC::jsUndefined());
- }
-
- auto specifier = Zig::toZigString(moduleKey);
- ErrorableResolvedSource res;
- res.success = false;
- res.result.err.code = 0;
- res.result.err.ptr = nullptr;
-
- JSValue result = Bun::fetchSourceCodeSync(
- reinterpret_cast<Zig::GlobalObject*>(globalObject),
- &res,
- &specifier,
- &specifier);
-
- if (result.isUndefined() || !result) {
- return JSValue::encode(result);
- }
-
- globalObject->moduleLoader()->provideFetch(globalObject, key, jsCast<JSC::JSSourceCode*>(result)->sourceCode());
- RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsUndefined()));
-}
-
static JSC::JSInternalPromise* rejectedInternalPromise(JSC::JSGlobalObject* globalObject, JSC::JSValue value)
{
JSC::VM& vm = globalObject->vm();
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp
index 2da1ebbbb..9ac577473 100644
--- a/src/bun.js/bindings/bindings.cpp
+++ b/src/bun.js/bindings/bindings.cpp
@@ -3555,6 +3555,11 @@ restart:
}
}
+extern "C" JSC__JSValue JSC__JSValue__createRopeString(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* globalObject)
+{
+ return JSValue::encode(JSC::jsString(globalObject, JSC::JSValue::decode(JSValue0).toString(globalObject), JSC::JSValue::decode(JSValue1).toString(globalObject)));
+}
+
extern "C" size_t JSC__VM__blockBytesAllocated(JSC__VM* vm)
{
#if ENABLE(RESOURCE_USAGE)
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig
index ccf446d2d..0bd21ebac 100644
--- a/src/bun.js/bindings/bindings.zig
+++ b/src/bun.js/bindings/bindings.zig
@@ -2507,6 +2507,10 @@ pub const JSValue = enum(JSValueReprInt) {
return asPtr(this, T);
}
+ pub fn createRopeString(this: JSValue, rhs: JSValue, globalThis: *JSC.JSGlobalObject) JSValue {
+ return cppFn("createRopeString", .{ this, rhs, globalThis });
+ }
+
pub fn getErrorsProperty(this: JSValue, globalObject: *JSGlobalObject) JSValue {
return cppFn("getErrorsProperty", .{ this, globalObject });
}
@@ -3210,7 +3214,7 @@ pub const JSValue = enum(JSValueReprInt) {
return this.asNullableVoid().?;
}
- pub const Extern = [_][]const u8{ "forEachProperty", "coerceToInt32", "fastGet_", "getStaticProperty", "createUninitializedUint8Array", "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt64", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable", "toBooleanSlow", "deepEquals", "strictDeepEquals", "getIfPropertyExistsFromPath", "asBigIntCompare" };
+ pub const Extern = [_][]const u8{ "createRopeString", "forEachProperty", "coerceToInt32", "fastGet_", "getStaticProperty", "createUninitializedUint8Array", "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt64", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable", "toBooleanSlow", "deepEquals", "strictDeepEquals", "getIfPropertyExistsFromPath", "asBigIntCompare" };
};
extern "c" fn Microtask__run(*Microtask, *JSGlobalObject) void;
diff --git a/src/bun.js/bindings/header-gen.zig b/src/bun.js/bindings/header-gen.zig
index 5281aa0f0..57f0189d9 100644
--- a/src/bun.js/bindings/header-gen.zig
+++ b/src/bun.js/bindings/header-gen.zig
@@ -8,7 +8,7 @@ const UnionMeta = std.builtin.TypeInfo.Union;
const warn = std.debug.warn;
const StaticExport = @import("./static_export.zig");
const typeBaseName = @import("../../meta.zig").typeBaseName;
-
+const bun = @import("bun");
const TypeNameMap = bun.StringHashMap([]const u8);
fn isCppObject(comptime Type: type) bool {
diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h
index 34fdaba99..d5923902b 100644
--- a/src/bun.js/bindings/headers-cpp.h
+++ b/src/bun.js/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1669880224
+//-- AUTOGENERATED FILE -- 1670255504
// clang-format off
#pragma once
diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h
index a5aa965ef..ec58a0a37 100644
--- a/src/bun.js/bindings/headers.h
+++ b/src/bun.js/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format off
-//-- AUTOGENERATED FILE -- 1669974046
+//-- AUTOGENERATED FILE -- 1670255504
#pragma once
#include <stddef.h>
@@ -256,6 +256,7 @@ CPP_DECL JSC__JSValue JSC__JSValue__createEmptyObject(JSC__JSGlobalObject* arg0,
CPP_DECL JSC__JSValue JSC__JSValue__createInternalPromise(JSC__JSGlobalObject* arg0);
CPP_DECL JSC__JSValue JSC__JSValue__createObject2(JSC__JSGlobalObject* arg0, const ZigString* arg1, const ZigString* arg2, JSC__JSValue JSValue3, JSC__JSValue JSValue4);
CPP_DECL JSC__JSValue JSC__JSValue__createRangeError(const ZigString* arg0, const ZigString* arg1, JSC__JSGlobalObject* arg2);
+CPP_DECL JSC__JSValue JSC__JSValue__createRopeString(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2);
CPP_DECL JSC__JSValue JSC__JSValue__createStringArray(JSC__JSGlobalObject* arg0, ZigString* arg1, size_t arg2, bool arg3);
CPP_DECL JSC__JSValue JSC__JSValue__createTypeError(const ZigString* arg0, const ZigString* arg1, JSC__JSGlobalObject* arg2);
CPP_DECL JSC__JSValue JSC__JSValue__createUninitializedUint8Array(JSC__JSGlobalObject* arg0, size_t arg1);
diff --git a/src/bun.js/bindings/headers.zig b/src/bun.js/bindings/headers.zig
index 3446a7f55..6bb107ba0 100644
--- a/src/bun.js/bindings/headers.zig
+++ b/src/bun.js/bindings/headers.zig
@@ -180,6 +180,7 @@ pub extern fn JSC__JSValue__createEmptyObject(arg0: ?*JSC__JSGlobalObject, arg1:
pub extern fn JSC__JSValue__createInternalPromise(arg0: ?*JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__createObject2(arg0: ?*JSC__JSGlobalObject, arg1: [*c]const ZigString, arg2: [*c]const ZigString, JSValue3: JSC__JSValue, JSValue4: JSC__JSValue) JSC__JSValue;
pub extern fn JSC__JSValue__createRangeError(arg0: [*c]const ZigString, arg1: [*c]const ZigString, arg2: ?*JSC__JSGlobalObject) JSC__JSValue;
+pub extern fn JSC__JSValue__createRopeString(JSValue0: JSC__JSValue, JSValue1: JSC__JSValue, arg2: ?*JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__createStringArray(arg0: ?*JSC__JSGlobalObject, arg1: [*c]ZigString, arg2: usize, arg3: bool) JSC__JSValue;
pub extern fn JSC__JSValue__createTypeError(arg0: [*c]const ZigString, arg1: [*c]const ZigString, arg2: ?*JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__createUninitializedUint8Array(arg0: ?*JSC__JSGlobalObject, arg1: usize) JSC__JSValue;
diff --git a/src/bun.js/builtins/WebCoreJSBuiltins.cpp b/src/bun.js/builtins/WebCoreJSBuiltins.cpp
index f98915cb0..1e8fa06d6 100644
--- a/src/bun.js/builtins/WebCoreJSBuiltins.cpp
+++ b/src/bun.js/builtins/WebCoreJSBuiltins.cpp
@@ -35,6 +35,7 @@
// builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py
#include "ByteLengthQueuingStrategyBuiltins.cpp"
+#include "ConsoleObjectBuiltins.cpp"
#include "CountQueuingStrategyBuiltins.cpp"
#include "ImportMetaObjectBuiltins.cpp"
#include "JSBufferConstructorBuiltins.cpp"
diff --git a/src/bun.js/builtins/cpp/ConsoleObjectBuiltins.cpp b/src/bun.js/builtins/cpp/ConsoleObjectBuiltins.cpp
new file mode 100644
index 000000000..39080d690
--- /dev/null
+++ b/src/bun.js/builtins/cpp/ConsoleObjectBuiltins.cpp
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2015 Igalia
+ * Copyright (c) 2015 Igalia S.L.
+ * Copyright (c) 2015 Igalia.
+ * Copyright (c) 2015, 2016 Canon Inc. All rights reserved.
+ * Copyright (c) 2015, 2016, 2017 Canon Inc.
+ * Copyright (c) 2016, 2018 -2018 Apple Inc. All rights reserved.
+ * Copyright (c) 2016, 2020 Apple Inc. All rights reserved.
+ * Copyright (c) 2022 Codeblog Corp. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// DO NOT EDIT THIS FILE. It is automatically generated from JavaScript files for
+// builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py
+
+#include "config.h"
+#include "ConsoleObjectBuiltins.h"
+
+#include "WebCoreJSClientData.h"
+#include <JavaScriptCore/HeapInlines.h>
+#include <JavaScriptCore/IdentifierInlines.h>
+#include <JavaScriptCore/ImplementationVisibility.h>
+#include <JavaScriptCore/Intrinsic.h>
+#include <JavaScriptCore/JSCJSValueInlines.h>
+#include <JavaScriptCore/JSCellInlines.h>
+#include <JavaScriptCore/StructureInlines.h>
+#include <JavaScriptCore/VM.h>
+
+namespace WebCore {
+
+const JSC::ConstructAbility s_consoleObjectAsyncIteratorCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
+const JSC::ConstructorKind s_consoleObjectAsyncIteratorCodeConstructorKind = JSC::ConstructorKind::None;
+const JSC::ImplementationVisibility s_consoleObjectAsyncIteratorCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
+const int s_consoleObjectAsyncIteratorCodeLength = 2004;
+static const JSC::Intrinsic s_consoleObjectAsyncIteratorCodeIntrinsic = JSC::NoIntrinsic;
+const char* const s_consoleObjectAsyncIteratorCode =
+ "(function () {\n" \
+ " \"use strict\";\n" \
+ "\n" \
+ " const Iterator = async function* ConsoleAsyncIterator() {\n" \
+ " \"use strict\";\n" \
+ "\n" \
+ " const stream = @Bun.stdin.stream();\n" \
+ " var reader = stream.getReader();\n" \
+ "\n" \
+ " //\n" \
+ " var decoder = new globalThis.TextDecoder(\"utf-8\", { fatal: false });\n" \
+ " var deferredError;\n" \
+ " var indexOf = @Bun.indexOfLine;\n" \
+ "\n" \
+ " try {\n" \
+ " while (true) {\n" \
+ " var done, value;\n" \
+ " var pendingChunk;\n" \
+ " const firstResult = reader.readMany();\n" \
+ " if (@isPromise(firstResult)) {\n" \
+ " ({done, value} = await firstResult);\n" \
+ " } else {\n" \
+ " ({done, value} = firstResult);\n" \
+ " }\n" \
+ "\n" \
+ " if (done) {\n" \
+ " if (pendingChunk) {\n" \
+ " yield decoder.decode(pendingChunk);\n" \
+ " }\n" \
+ " return;\n" \
+ " }\n" \
+ " \n" \
+ " var actualChunk;\n" \
+ " //\n" \
+ " for (const chunk of value) {\n" \
+ " actualChunk = chunk;\n" \
+ " if (pendingChunk) {\n" \
+ " actualChunk = Buffer.concat([pendingChunk, chunk]);\n" \
+ " pendingChunk = null;\n" \
+ " }\n" \
+ "\n" \
+ " var last = 0;\n" \
+ " //\n" \
+ " var i = indexOf(actualChunk, last);\n" \
+ " while (i !== -1) {\n" \
+ " yield decoder.decode(actualChunk.subarray(last, i));\n" \
+ " last = i + 1;\n" \
+ " i = indexOf(actualChunk, last);\n" \
+ " }\n" \
+ "\n" \
+ " pendingChunk = actualChunk.subarray(last);\n" \
+ " }\n" \
+ " }\n" \
+ " } catch(e) {\n" \
+ " deferredError = e;\n" \
+ " } finally {\n" \
+ " reader.releaseLock();\n" \
+ "\n" \
+ " if (deferredError) {\n" \
+ " throw deferredError;\n" \
+ " }\n" \
+ " }\n" \
+ " };\n" \
+ "\n" \
+ " const symbol = globalThis.Symbol.asyncIterator;\n" \
+ " this[symbol] = Iterator;\n" \
+ " return Iterator();\n" \
+ "})\n" \
+;
+
+const JSC::ConstructAbility s_consoleObjectWriteCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
+const JSC::ConstructorKind s_consoleObjectWriteCodeConstructorKind = JSC::ConstructorKind::None;
+const JSC::ImplementationVisibility s_consoleObjectWriteCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
+const int s_consoleObjectWriteCodeLength = 524;
+static const JSC::Intrinsic s_consoleObjectWriteCodeIntrinsic = JSC::NoIntrinsic;
+const char* const s_consoleObjectWriteCode =
+ "(function (input) {\n" \
+ " \"use strict\";\n" \
+ "\n" \
+ " var writer = @getByIdDirectPrivate(this, \"writer\");\n" \
+ " if (!writer) {\n" \
+ " var length = @toLength(input?.length ?? 0);\n" \
+ " writer = @Bun.stdout.writer({highWaterMark: length > 65536 ? length : 65536});\n" \
+ " @putByIdDirectPrivate(this, \"writer\", writer);\n" \
+ " }\n" \
+ "\n" \
+ " var wrote = writer.write(input);\n" \
+ "\n" \
+ " const count = @argumentCount();\n" \
+ " for (var i = 1; i < count; i++) {\n" \
+ " wrote += writer.write(@argument(i));\n" \
+ " }\n" \
+ "\n" \
+ " writer.flush(true);\n" \
+ " return wrote;\n" \
+ "})\n" \
+;
+
+
+#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
+JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \
+{\
+ JSVMClientData* clientData = static_cast<JSVMClientData*>(vm.clientData); \
+ return clientData->builtinFunctions().consoleObjectBuiltins().codeName##Executable()->link(vm, nullptr, clientData->builtinFunctions().consoleObjectBuiltins().codeName##Source(), std::nullopt, s_##codeName##Intrinsic); \
+}
+WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
+#undef DEFINE_BUILTIN_GENERATOR
+
+
+} // namespace WebCore
diff --git a/src/bun.js/builtins/cpp/ConsoleObjectBuiltins.h b/src/bun.js/builtins/cpp/ConsoleObjectBuiltins.h
new file mode 100644
index 000000000..9e2a43707
--- /dev/null
+++ b/src/bun.js/builtins/cpp/ConsoleObjectBuiltins.h
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 Igalia
+ * Copyright (c) 2015 Igalia S.L.
+ * Copyright (c) 2015 Igalia.
+ * Copyright (c) 2015, 2016 Canon Inc. All rights reserved.
+ * Copyright (c) 2015, 2016, 2017 Canon Inc.
+ * Copyright (c) 2016, 2018 -2018 Apple Inc. All rights reserved.
+ * Copyright (c) 2016, 2020 Apple Inc. All rights reserved.
+ * Copyright (c) 2022 Codeblog Corp. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// DO NOT EDIT THIS FILE. It is automatically generated from JavaScript files for
+// builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py
+
+#pragma once
+
+#include <JavaScriptCore/BuiltinUtils.h>
+#include <JavaScriptCore/Identifier.h>
+#include <JavaScriptCore/JSFunction.h>
+#include <JavaScriptCore/UnlinkedFunctionExecutable.h>
+
+namespace JSC {
+class FunctionExecutable;
+}
+
+namespace WebCore {
+
+/* ConsoleObject */
+extern const char* const s_consoleObjectAsyncIteratorCode;
+extern const int s_consoleObjectAsyncIteratorCodeLength;
+extern const JSC::ConstructAbility s_consoleObjectAsyncIteratorCodeConstructAbility;
+extern const JSC::ConstructorKind s_consoleObjectAsyncIteratorCodeConstructorKind;
+extern const JSC::ImplementationVisibility s_consoleObjectAsyncIteratorCodeImplementationVisibility;
+extern const char* const s_consoleObjectWriteCode;
+extern const int s_consoleObjectWriteCodeLength;
+extern const JSC::ConstructAbility s_consoleObjectWriteCodeConstructAbility;
+extern const JSC::ConstructorKind s_consoleObjectWriteCodeConstructorKind;
+extern const JSC::ImplementationVisibility s_consoleObjectWriteCodeImplementationVisibility;
+
+#define WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_DATA(macro) \
+ macro(asyncIterator, consoleObjectAsyncIterator, 0) \
+ macro(write, consoleObjectWrite, 1) \
+
+#define WEBCORE_BUILTIN_CONSOLEOBJECT_ASYNCITERATOR 1
+#define WEBCORE_BUILTIN_CONSOLEOBJECT_WRITE 1
+
+#define WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(macro) \
+ macro(consoleObjectAsyncIteratorCode, asyncIterator, "[Symbol.asyncIterator]"_s, s_consoleObjectAsyncIteratorCodeLength) \
+ macro(consoleObjectWriteCode, write, ASCIILiteral(), s_consoleObjectWriteCodeLength) \
+
+#define WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_FUNCTION_NAME(macro) \
+ macro(asyncIterator) \
+ macro(write) \
+
+#define DECLARE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
+ JSC::FunctionExecutable* codeName##Generator(JSC::VM&);
+
+WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(DECLARE_BUILTIN_GENERATOR)
+#undef DECLARE_BUILTIN_GENERATOR
+
+class ConsoleObjectBuiltinsWrapper : private JSC::WeakHandleOwner {
+public:
+ explicit ConsoleObjectBuiltinsWrapper(JSC::VM& vm)
+ : m_vm(vm)
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_FUNCTION_NAME(INITIALIZE_BUILTIN_NAMES)
+#define INITIALIZE_BUILTIN_SOURCE_MEMBERS(name, functionName, overriddenName, length) , m_##name##Source(JSC::makeSource(StringImpl::createWithoutCopying(s_##name, length), { }))
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(INITIALIZE_BUILTIN_SOURCE_MEMBERS)
+#undef INITIALIZE_BUILTIN_SOURCE_MEMBERS
+ {
+ }
+
+#define EXPOSE_BUILTIN_EXECUTABLES(name, functionName, overriddenName, length) \
+ JSC::UnlinkedFunctionExecutable* name##Executable(); \
+ const JSC::SourceCode& name##Source() const { return m_##name##Source; }
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(EXPOSE_BUILTIN_EXECUTABLES)
+#undef EXPOSE_BUILTIN_EXECUTABLES
+
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
+
+ void exportNames();
+
+private:
+ JSC::VM& m_vm;
+
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_NAMES)
+
+#define DECLARE_BUILTIN_SOURCE_MEMBERS(name, functionName, overriddenName, length) \
+ JSC::SourceCode m_##name##Source;\
+ JSC::Weak<JSC::UnlinkedFunctionExecutable> m_##name##Executable;
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(DECLARE_BUILTIN_SOURCE_MEMBERS)
+#undef DECLARE_BUILTIN_SOURCE_MEMBERS
+
+};
+
+#define DEFINE_BUILTIN_EXECUTABLES(name, functionName, overriddenName, length) \
+inline JSC::UnlinkedFunctionExecutable* ConsoleObjectBuiltinsWrapper::name##Executable() \
+{\
+ if (!m_##name##Executable) {\
+ JSC::Identifier executableName = functionName##PublicName();\
+ if (overriddenName)\
+ executableName = JSC::Identifier::fromString(m_vm, overriddenName);\
+ m_##name##Executable = JSC::Weak<JSC::UnlinkedFunctionExecutable>(JSC::createBuiltinExecutable(m_vm, m_##name##Source, executableName, s_##name##ImplementationVisibility, s_##name##ConstructorKind, s_##name##ConstructAbility), this, &m_##name##Executable);\
+ }\
+ return m_##name##Executable.get();\
+}
+WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_CODE(DEFINE_BUILTIN_EXECUTABLES)
+#undef DEFINE_BUILTIN_EXECUTABLES
+
+inline void ConsoleObjectBuiltinsWrapper::exportNames()
+{
+#define EXPORT_FUNCTION_NAME(name) m_vm.propertyNames->appendExternalName(name##PublicName(), name##PrivateName());
+ WEBCORE_FOREACH_CONSOLEOBJECT_BUILTIN_FUNCTION_NAME(EXPORT_FUNCTION_NAME)
+#undef EXPORT_FUNCTION_NAME
+}
+
+} // namespace WebCore
diff --git a/src/bun.js/builtins/cpp/ReadableStreamInternalsBuiltins.cpp b/src/bun.js/builtins/cpp/ReadableStreamInternalsBuiltins.cpp
index 8431b9fb7..854d46faa 100644
--- a/src/bun.js/builtins/cpp/ReadableStreamInternalsBuiltins.cpp
+++ b/src/bun.js/builtins/cpp/ReadableStreamInternalsBuiltins.cpp
@@ -2621,7 +2621,7 @@ const char* const s_readableStreamInternalsReadableStreamToArrayDirectCode =
const JSC::ConstructAbility s_readableStreamInternalsReadableStreamDefineLazyIteratorsCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_readableStreamInternalsReadableStreamDefineLazyIteratorsCodeConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_readableStreamInternalsReadableStreamDefineLazyIteratorsCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
-const int s_readableStreamInternalsReadableStreamDefineLazyIteratorsCodeLength = 1650;
+const int s_readableStreamInternalsReadableStreamDefineLazyIteratorsCodeLength = 1478;
static const JSC::Intrinsic s_readableStreamInternalsReadableStreamDefineLazyIteratorsCodeIntrinsic = JSC::NoIntrinsic;
const char* const s_readableStreamInternalsReadableStreamDefineLazyIteratorsCode =
"(function (prototype) {\n" \
@@ -2632,37 +2632,34 @@ const char* const s_readableStreamInternalsReadableStreamDefineLazyIteratorsCode
" var ReadableStreamAsyncIterator = async function* ReadableStreamAsyncIterator(stream, preventCancel) {\n" \
" var reader = stream.getReader();\n" \
" var deferredError;\n" \
- " try {\n" \
- " while (true) {\n" \
- " var done, value;\n" \
- " const firstResult = reader.readMany();\n" \
- " if (@isPromise(firstResult)) {\n" \
- " const result = await firstResult;\n" \
- " done = result.done;\n" \
- " value = result.value;\n" \
- " } else {\n" \
- " done = firstResult.done;\n" \
- " value = firstResult.value;\n" \
- " }\n" \
- "\n" \
- " if (done) {\n" \
- " return;\n" \
- " }\n" \
- " yield* value;\n" \
- " }\n" \
- " } catch(e) {\n" \
- " deferredError = e;\n" \
- " } finally {\n" \
- " reader.releaseLock();\n" \
- "\n" \
- " if (!preventCancel) {\n" \
- " stream.cancel(deferredError);\n" \
+ " try {\n" \
+ " while (true) {\n" \
+ " var done, value;\n" \
+ " const firstResult = reader.readMany();\n" \
+ " if (@isPromise(firstResult)) {\n" \
+ " ({done, value} = await firstResult);\n" \
+ " } else {\n" \
+ " ({done, value} = firstResult);\n" \
+ " }\n" \
+ "\n" \
+ " if (done) {\n" \
+ " return;\n" \
+ " }\n" \
+ " yield* value;\n" \
" }\n" \
+ " } catch(e) {\n" \
+ " deferredError = e;\n" \
+ " } finally {\n" \
+ " reader.releaseLock();\n" \
"\n" \
- " if (deferredError) {\n" \
- " throw deferredError;\n" \
+ " if (!preventCancel) {\n" \
+ " stream.cancel(deferredError);\n" \
" }\n" \
+ "\n" \
+ " if (deferredError) {\n" \
+ " throw deferredError;\n" \
" }\n" \
+ " }\n" \
" };\n" \
" var createAsyncIterator = function asyncIterator() {\n" \
" return ReadableStreamAsyncIterator(this, false);\n" \
diff --git a/src/bun.js/builtins/cpp/WebCoreJSBuiltins.h b/src/bun.js/builtins/cpp/WebCoreJSBuiltins.h
index 85da33a83..0d6d0372c 100644
--- a/src/bun.js/builtins/cpp/WebCoreJSBuiltins.h
+++ b/src/bun.js/builtins/cpp/WebCoreJSBuiltins.h
@@ -37,6 +37,7 @@
#pragma once
#include "ByteLengthQueuingStrategyBuiltins.h"
+#include "ConsoleObjectBuiltins.h"
#include "CountQueuingStrategyBuiltins.h"
#include "ImportMetaObjectBuiltins.h"
#include "JSBufferConstructorBuiltins.h"
@@ -67,6 +68,7 @@ public:
explicit JSBuiltinFunctions(JSC::VM& vm)
: m_vm(vm)
, m_byteLengthQueuingStrategyBuiltins(m_vm)
+ , m_consoleObjectBuiltins(m_vm)
, m_countQueuingStrategyBuiltins(m_vm)
, m_importMetaObjectBuiltins(m_vm)
, m_jsBufferConstructorBuiltins(m_vm)
@@ -97,6 +99,7 @@ public:
}
ByteLengthQueuingStrategyBuiltinsWrapper& byteLengthQueuingStrategyBuiltins() { return m_byteLengthQueuingStrategyBuiltins; }
+ ConsoleObjectBuiltinsWrapper& consoleObjectBuiltins() { return m_consoleObjectBuiltins; }
CountQueuingStrategyBuiltinsWrapper& countQueuingStrategyBuiltins() { return m_countQueuingStrategyBuiltins; }
ImportMetaObjectBuiltinsWrapper& importMetaObjectBuiltins() { return m_importMetaObjectBuiltins; }
JSBufferConstructorBuiltinsWrapper& jsBufferConstructorBuiltins() { return m_jsBufferConstructorBuiltins; }
@@ -122,6 +125,7 @@ public:
private:
JSC::VM& m_vm;
ByteLengthQueuingStrategyBuiltinsWrapper m_byteLengthQueuingStrategyBuiltins;
+ ConsoleObjectBuiltinsWrapper m_consoleObjectBuiltins;
CountQueuingStrategyBuiltinsWrapper m_countQueuingStrategyBuiltins;
ImportMetaObjectBuiltinsWrapper m_importMetaObjectBuiltins;
JSBufferConstructorBuiltinsWrapper m_jsBufferConstructorBuiltins;
diff --git a/src/bun.js/builtins/js/ConsoleObject.js b/src/bun.js/builtins/js/ConsoleObject.js
new file mode 100644
index 000000000..8601748c6
--- /dev/null
+++ b/src/bun.js/builtins/js/ConsoleObject.js
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2022 Codeblog Corp. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+@overriddenName="[Symbol.asyncIterator]"
+function asyncIterator() {
+ "use strict";
+
+ const Iterator = async function* ConsoleAsyncIterator() {
+ "use strict";
+
+ const stream = @Bun.stdin.stream();
+ var reader = stream.getReader();
+
+ // TODO: use builtin
+ var decoder = new globalThis.TextDecoder("utf-8", { fatal: false });
+ var deferredError;
+ var indexOf = @Bun.indexOfLine;
+
+ try {
+ while (true) {
+ var done, value;
+ var pendingChunk;
+ const firstResult = reader.readMany();
+ if (@isPromise(firstResult)) {
+ ({done, value} = await firstResult);
+ } else {
+ ({done, value} = firstResult);
+ }
+
+ if (done) {
+ if (pendingChunk) {
+ yield decoder.decode(pendingChunk);
+ }
+ return;
+ }
+
+ var actualChunk;
+ // we assume it was given line-by-line
+ for (const chunk of value) {
+ actualChunk = chunk;
+ if (pendingChunk) {
+ actualChunk = Buffer.concat([pendingChunk, chunk]);
+ pendingChunk = null;
+ }
+
+ var last = 0;
+ // TODO: "\r", 0x4048, 0x4049, 0x404A, 0x404B, 0x404C, 0x404D, 0x404E, 0x404F
+ var i = indexOf(actualChunk, last);
+ while (i !== -1) {
+ yield decoder.decode(actualChunk.subarray(last, i));
+ last = i + 1;
+ i = indexOf(actualChunk, last);
+ }
+
+ pendingChunk = actualChunk.subarray(last);
+ }
+ }
+ } catch(e) {
+ deferredError = e;
+ } finally {
+ reader.releaseLock();
+
+ if (deferredError) {
+ throw deferredError;
+ }
+ }
+ };
+
+ const symbol = globalThis.Symbol.asyncIterator;
+ this[symbol] = Iterator;
+ return Iterator();
+}
+
+function write(input) {
+ "use strict";
+
+ var writer = @getByIdDirectPrivate(this, "writer");
+ if (!writer) {
+ var length = @toLength(input?.length ?? 0);
+ writer = @Bun.stdout.writer({highWaterMark: length > 65536 ? length : 65536});
+ @putByIdDirectPrivate(this, "writer", writer);
+ }
+
+ var wrote = writer.write(input);
+
+ const count = @argumentCount();
+ for (var i = 1; i < count; i++) {
+ wrote += writer.write(@argument(i));
+ }
+
+ writer.flush(true);
+ return wrote;
+} \ No newline at end of file
diff --git a/src/bun.js/builtins/js/ReadableStreamInternals.js b/src/bun.js/builtins/js/ReadableStreamInternals.js
index f317eeaec..ca7a1c355 100644
--- a/src/bun.js/builtins/js/ReadableStreamInternals.js
+++ b/src/bun.js/builtins/js/ReadableStreamInternals.js
@@ -2168,37 +2168,34 @@ function readableStreamDefineLazyIterators(prototype) {
var ReadableStreamAsyncIterator = async function* ReadableStreamAsyncIterator(stream, preventCancel) {
var reader = stream.getReader();
var deferredError;
- try {
- while (true) {
- var done, value;
- const firstResult = reader.readMany();
- if (@isPromise(firstResult)) {
- const result = await firstResult;
- done = result.done;
- value = result.value;
- } else {
- done = firstResult.done;
- value = firstResult.value;
- }
-
- if (done) {
- return;
- }
- yield* value;
- }
- } catch(e) {
- deferredError = e;
- } finally {
- reader.releaseLock();
-
- if (!preventCancel) {
- stream.cancel(deferredError);
+ try {
+ while (true) {
+ var done, value;
+ const firstResult = reader.readMany();
+ if (@isPromise(firstResult)) {
+ ({done, value} = await firstResult);
+ } else {
+ ({done, value} = firstResult);
+ }
+
+ if (done) {
+ return;
+ }
+ yield* value;
}
+ } catch(e) {
+ deferredError = e;
+ } finally {
+ reader.releaseLock();
- if (deferredError) {
- throw deferredError;
+ if (!preventCancel) {
+ stream.cancel(deferredError);
}
+
+ if (deferredError) {
+ throw deferredError;
}
+ }
};
var createAsyncIterator = function asyncIterator() {
return ReadableStreamAsyncIterator(this, false);