Skip to content

Commit

Permalink
revert 84c91bf (#13214)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored Aug 10, 2024
1 parent b9ead44 commit 9302b42
Show file tree
Hide file tree
Showing 31 changed files with 2,024 additions and 260 deletions.
4 changes: 2 additions & 2 deletions docs/runtime/nodejs-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ The table below lists all globals implemented by Node.js and Bun's current compa

### [`TextDecoderStream`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream)

🔴 Not implemented.
🟢 Fully implemented.

### [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder)

🟢 Fully implemented.

### [`TextEncoderStream`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoderStream)

🔴 Not implemented.
🟢 Fully implemented.

### [`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream)

Expand Down
11 changes: 11 additions & 0 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ pub const ArrayBuffer = extern struct {
return Bun__createUint8ArrayForCopy(globalThis, bytes.ptr, bytes.len, true);
}

extern "C" fn Bun__allocUint8ArrayForCopy(*JSC.JSGlobalObject, usize, **anyopaque) JSValue;
pub fn allocBuffer(globalThis: *JSC.JSGlobalObject, len: usize) struct { JSValue, []u8 } {
var ptr: [*]u8 = undefined;
const buffer = Bun__allocUint8ArrayForCopy(globalThis, len, @ptrCast(&ptr));
if (buffer.isEmpty()) {
return .{ buffer, &.{} };
}
return .{ buffer, ptr[0..len] };
}

extern "C" fn Bun__createUint8ArrayForCopy(*JSC.JSGlobalObject, ptr: ?*const anyopaque, len: usize, buffer: bool) JSValue;
extern "C" fn Bun__createArrayBufferForCopy(*JSC.JSGlobalObject, ptr: ?*const anyopaque, len: usize) JSValue;

Expand Down Expand Up @@ -761,6 +771,7 @@ const TestScope = Test.TestScope;
const NodeFS = JSC.Node.NodeFS;
const TextEncoder = WebCore.TextEncoder;
const TextDecoder = WebCore.TextDecoder;
const TextEncoderStreamEncoder = WebCore.TextEncoderStreamEncoder;
const HTMLRewriter = JSC.Cloudflare.HTMLRewriter;
const Element = JSC.Cloudflare.Element;
const Comment = JSC.Cloudflare.Comment;
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/JSDOMWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class JSDOMWrapper : public JSDOMObject {
using DOMWrapped = ImplementationClass;

ImplementationClass& wrapped() const { return m_wrapped; }
Ref<ImplementationClass> protectedWrapped() const { return m_wrapped; }
static ptrdiff_t offsetOfWrapped() { return OBJECT_OFFSETOF(JSDOMWrapper, m_wrapped); }
constexpr static bool hasCustomPtrTraits() { return !std::is_same_v<PtrTraits, RawPtrTraits<ImplementationClass>>; };

Expand Down
21 changes: 21 additions & 0 deletions src/bun.js/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
#include "JSSQLStatement.h"
#include "JSStringDecoder.h"
#include "JSTextEncoder.h"
#include "JSTextEncoderStream.h"
#include "JSTextDecoderStream.h"
#include "JSTransformStream.h"
#include "JSTransformStreamDefaultController.h"
#include "JSURLSearchParams.h"
Expand Down Expand Up @@ -1268,6 +1270,8 @@ WEBCORE_GENERATED_CONSTRUCTOR_GETTER(ReadableStreamDefaultController)
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(ReadableStreamDefaultReader)
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(SubtleCrypto);
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(TextEncoder);
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(TextEncoderStream);
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(TextDecoderStream);
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(TransformStream)
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(TransformStreamDefaultController)
WEBCORE_GENERATED_CONSTRUCTOR_GETTER(URLSearchParams);
Expand Down Expand Up @@ -1696,6 +1700,22 @@ extern "C" JSC__JSValue Bun__createArrayBufferForCopy(JSC::JSGlobalObject* globa
RELEASE_AND_RETURN(scope, JSValue::encode(JSC::JSArrayBuffer::create(globalObject->vm(), globalObject->arrayBufferStructure(JSC::ArrayBufferSharingMode::Default), WTFMove(arrayBuffer))));
}

extern "C" JSC__JSValue Bun__allocUint8ArrayForCopy(JSC::JSGlobalObject* globalObject, size_t len, void** ptr)
{
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());

JSC::JSUint8Array* array = JSC::JSUint8Array::createUninitialized(globalObject, globalObject->m_typedArrayUint8.get(globalObject), len);

if (UNLIKELY(!array)) {
JSC::throwOutOfMemoryError(globalObject, scope);
return encodedJSValue();
}

*ptr = array->vector();

return JSValue::encode(array);
}

extern "C" JSC__JSValue Bun__createUint8ArrayForCopy(JSC::JSGlobalObject* globalObject, const void* ptr, size_t len, bool isBuffer)
{
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
Expand Down Expand Up @@ -3362,6 +3382,7 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm)
GlobalPropertyInfo(builtinNames.internalModuleRegistryPrivateName(), this->internalModuleRegistry(), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly),
GlobalPropertyInfo(builtinNames.processBindingConstantsPrivateName(), this->processBindingConstants(), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly),
GlobalPropertyInfo(builtinNames.requireMapPrivateName(), this->requireMap(), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | 0),
GlobalPropertyInfo(builtinNames.TextEncoderStreamEncoderPrivateName(), JSTextEncoderStreamEncoderConstructor(), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | 0),
};
addStaticGlobals(staticGlobals, std::size(staticGlobals));

Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/bindings/ZigGlobalObject.lut.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
ReadableStreamDefaultReader ReadableStreamDefaultReaderConstructorCallback PropertyCallback
SubtleCrypto SubtleCryptoConstructorCallback PropertyCallback
TextEncoder TextEncoderConstructorCallback PropertyCallback
TextEncoderStream TextEncoderStreamConstructorCallback PropertyCallback
TextDecoderStream TextDecoderStreamConstructorCallback PropertyCallback
TransformStream TransformStreamConstructorCallback PropertyCallback
TransformStreamDefaultController TransformStreamDefaultControllerConstructorCallback PropertyCallback
URL DOMURLConstructorCallback PropertyCallback
Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub const ZigString = extern struct {

pub fn dupeForJS(utf8: []const u8, allocator: std.mem.Allocator) !ZigString {
if (try strings.toUTF16Alloc(allocator, utf8, false, false)) |utf16| {
var out = ZigString.init16(utf16);
var out = ZigString.initUTF16(utf16);
out.mark();
out.markUTF16();
return out;
Expand Down Expand Up @@ -629,8 +629,8 @@ pub const ZigString = extern struct {
return shim.cppFn("toAtomicValue", .{ this, globalThis });
}

pub fn init16(slice_: []const u16) ZigString {
var out = ZigString{ ._unsafe_ptr_do_not_use = std.mem.sliceAsBytes(slice_).ptr, .len = slice_.len };
pub fn initUTF16(items: []const u16) ZigString {
var out = ZigString{ ._unsafe_ptr_do_not_use = @ptrCast(items), .len = items.len };
out.markUTF16();
return out;
}
Expand Down
9 changes: 9 additions & 0 deletions src/bun.js/bindings/bun-simdutf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ size_t simdutf__convert_valid_utf16be_to_utf32(const char16_t* buf, size_t len,
{
return simdutf::convert_valid_utf16be_to_utf32(buf, len, utf32_buffer);
}
size_t simdutf__convert_latin1_to_utf8(const char* input, size_t length, char* utf8_buffer)
{
return simdutf::convert_latin1_to_utf8(input, length, utf8_buffer);
}
void simdutf__change_endianness_utf16(const char16_t* buf, size_t length,
char16_t* output)
{
Expand Down Expand Up @@ -325,6 +329,11 @@ size_t simdutf__utf32_length_from_utf8(const char* input, size_t length)
return simdutf::utf32_length_from_utf8(input, length);
}

size_t simdutf__utf8_length_from_latin1(const char* input, size_t length)
{
return simdutf::utf8_length_from_latin1(input, length);
}

size_t simdutf__base64_encode(const char* input, size_t length, char* output, int is_urlsafe)
{
return simdutf::binary_to_base64(input, length, output, is_urlsafe ? simdutf::base64_url : simdutf::base64_default);
Expand Down
14 changes: 14 additions & 0 deletions src/bun.js/bindings/bun-simdutf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub extern fn simdutf__convert_utf16le_to_utf32_with_errors(buf: [*]const u16, l
pub extern fn simdutf__convert_utf16be_to_utf32_with_errors(buf: [*]const u16, len: usize, utf32_buffer: [*]u32) SIMDUTFResult;
pub extern fn simdutf__convert_valid_utf16le_to_utf32(buf: [*]const u16, len: usize, utf32_buffer: [*]u32) usize;
pub extern fn simdutf__convert_valid_utf16be_to_utf32(buf: [*]const u16, len: usize, utf32_buffer: [*]u32) usize;
pub extern fn simdutf__convert_latin1_to_utf8(buf: [*]const u8, len: usize, utf8_buffer: [*]u8) usize;
pub extern fn simdutf__change_endianness_utf16(buf: [*]const u16, length: usize, output: [*]u16) void;
pub extern fn simdutf__count_utf16le(buf: [*]const u16, length: usize) usize;
pub extern fn simdutf__count_utf16be(buf: [*]const u16, length: usize) usize;
Expand All @@ -93,6 +94,7 @@ pub extern fn simdutf__utf16_length_from_utf8(input: [*]const u8, length: usize)
pub extern fn simdutf__utf8_length_from_utf32(input: [*c]const c_uint, length: usize) usize;
pub extern fn simdutf__utf16_length_from_utf32(input: [*c]const c_uint, length: usize) usize;
pub extern fn simdutf__utf32_length_from_utf8(input: [*]const u8, length: usize) usize;
pub extern fn simdutf__utf8_length_from_latin1(input: [*]const u8, length: usize) usize;

pub const validate = struct {
pub const with_errors = struct {
Expand Down Expand Up @@ -126,6 +128,14 @@ pub const validate = struct {
};

pub const convert = struct {
pub const latin1 = struct {
pub const to = struct {
pub fn utf8(input: []const u8, output: []u8) usize {
return simdutf__convert_latin1_to_utf8(input.ptr, input.len, output.ptr);
}
};
};

pub const utf8 = struct {
pub const to = struct {
pub const utf16 = struct {
Expand Down Expand Up @@ -261,6 +271,10 @@ pub const length = struct {
}
};

pub fn latin1(input: []const u8) usize {
return simdutf__utf8_length_from_latin1(input.ptr, input.len);
}

pub fn utf32(input: []const u32) usize {
JSC.markBinding(@src());
return simdutf__utf8_length_from_utf32(input.ptr, input.len);
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/generated_classes_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ pub const Classes = struct {
pub const PostgresSQLQuery = JSC.Postgres.PostgresSQLQuery;
pub const BrotliEncoder = JSC.API.BrotliEncoder;
pub const BrotliDecoder = JSC.API.BrotliDecoder;
pub const TextEncoderStreamEncoder = JSC.WebCore.TextEncoderStreamEncoder;
};
21 changes: 13 additions & 8 deletions src/bun.js/bindings/napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <JavaScriptCore/JSArrayBuffer.h>
#include <JavaScriptCore/FunctionPrototype.h>
#include "CommonJSModuleRecord.h"
#include "wtf/text/ASCIIFastPath.h"

// #include <iostream>
using namespace JSC;
Expand Down Expand Up @@ -723,12 +724,15 @@ extern "C" napi_status napi_create_arraybuffer(napi_env env,
// it doesn't copy the string
// but it's only safe to use if we are not setting a property
// because we can't guarantee the lifetime of it
#define PROPERTY_NAME_FROM_UTF8(identifierName) \
size_t utf8Len = strlen(utf8name); \
JSC::PropertyName identifierName = LIKELY(charactersAreAllASCII(std::span { reinterpret_cast<const LChar*>(utf8name), utf8Len })) ? JSC::PropertyName(JSC::Identifier::fromString(vm, WTF::String(WTF::StringImpl::createWithoutCopying({ utf8name, utf8Len })))) : JSC::PropertyName(JSC::Identifier::fromString(vm, WTF::String::fromUTF8(utf8name)));
#define PROPERTY_NAME_FROM_UTF8(identifierName) \
size_t utf8Len = strlen(utf8Name); \
WTF::String nameString = LIKELY(WTF::charactersAreAllASCII(std::span { reinterpret_cast<const LChar*>(utf8Name), utf8Len })) \
? WTF::String(WTF::StringImpl::createWithoutCopying({ utf8Name, utf8Len })) \
: WTF::String::fromUTF8(utf8Name); \
JSC::PropertyName identifierName = JSC::Identifier::fromString(vm, nameString);

extern "C" napi_status napi_has_named_property(napi_env env, napi_value object,
const char* utf8name,
const char* utf8Name,
bool* result)
{
NAPI_PREMABLE
Expand All @@ -740,22 +744,23 @@ extern "C" napi_status napi_has_named_property(napi_env env, napi_value object,
auto globalObject = toJS(env);
auto& vm = globalObject->vm();

auto* target = toJS(object).getObject();
JSObject* target = toJS(object).getObject();
if (UNLIKELY(!target)) {
return napi_object_expected;
}

PROPERTY_NAME_FROM_UTF8(name);

auto scope = DECLARE_CATCH_SCOPE(vm);
*result = !!target->getIfPropertyExists(globalObject, name);
PropertySlot slot(target, PropertySlot::InternalMethodType::HasProperty);
*result = target->getPropertySlot(globalObject, name, slot);
RETURN_IF_EXCEPTION(scope, napi_generic_failure);

scope.clearException();
return napi_ok;
}
extern "C" napi_status napi_get_named_property(napi_env env, napi_value object,
const char* utf8name,
const char* utf8Name,
napi_value* result)
{
NAPI_PREMABLE
Expand All @@ -767,7 +772,7 @@ extern "C" napi_status napi_get_named_property(napi_env env, napi_value object,
auto globalObject = toJS(env);
auto& vm = globalObject->vm();

auto* target = toJS(object).getObject();
JSObject* target = toJS(object).getObject();
if (UNLIKELY(!target)) {
return napi_object_expected;
}
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/webcore/DOMClientIsoSubspaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ class DOMClientIsoSubspaces {
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForStaticRange;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForText;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextDecoder;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextDecoderStream;
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextDecoderStream;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextDecoderStreamDecoder;
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextEncoder;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextEncoderStream;
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextEncoderStream;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextEncoderStreamEncoder;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTextEvent;
// std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForTransitionEvent;
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/webcore/DOMIsoSubspaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,10 @@ class DOMIsoSubspaces {
// std::unique_ptr<IsoSubspace> m_subspaceForStaticRange;
// std::unique_ptr<IsoSubspace> m_subspaceForText;
// std::unique_ptr<IsoSubspace> m_subspaceForTextDecoder;
// std::unique_ptr<IsoSubspace> m_subspaceForTextDecoderStream;
std::unique_ptr<IsoSubspace> m_subspaceForTextDecoderStream;
// std::unique_ptr<IsoSubspace> m_subspaceForTextDecoderStreamDecoder;
std::unique_ptr<IsoSubspace> m_subspaceForTextEncoder;
// std::unique_ptr<IsoSubspace> m_subspaceForTextEncoderStream;
std::unique_ptr<IsoSubspace> m_subspaceForTextEncoderStream;
// std::unique_ptr<IsoSubspace> m_subspaceForTextEncoderStreamEncoder;
// std::unique_ptr<IsoSubspace> m_subspaceForTextEvent;
// std::unique_ptr<IsoSubspace> m_subspaceForTransitionEvent;
Expand Down
Loading

0 comments on commit 9302b42

Please sign in to comment.