From bdb1b7124aec3ca42a13dd13309df4c8e4e3cc64 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 13 Jun 2023 09:15:05 -0700 Subject: Fix crash in CJS (#3294) * Fix crash in CJS * Add std.heap.ArenaAllocator * Use our arena allocator * Reduce JS parser memory usage and make HMR faster * Write some comments * fix test failure & clean up this code * Update javascript.zig * make arena usage safer --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/bindings/BunString.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/bun.js/bindings/BunString.cpp') diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp index 797f66545..249f68435 100644 --- a/src/bun.js/bindings/BunString.cpp +++ b/src/bun.js/bindings/BunString.cpp @@ -2,7 +2,8 @@ #include "headers-handwritten.h" #include "JavaScriptCore/JSCJSValueInlines.h" #include "helpers.h" - +#include "simdutf.h" +#include "wtf/text/ExternalStringImpl.h" using namespace JSC; extern "C" void Bun__WTFStringImpl__deref(WTF::StringImpl* impl) @@ -124,6 +125,30 @@ extern "C" JSC::EncodedJSValue BunString__toJS(JSC::JSGlobalObject* globalObject return JSValue::encode(Bun::toJS(globalObject, *bunString)); } +extern "C" BunString BunString__fromLatin1(const char* bytes, size_t length) +{ + return { BunStringTag::WTFStringImpl, { .wtf = &WTF::StringImpl::create(bytes, length).leakRef() } }; +} + +extern "C" BunString BunString__fromBytes(const char* bytes, size_t length) +{ + if (simdutf::validate_ascii(bytes, length)) { + return BunString__fromLatin1(bytes, length); + } + + auto str = WTF::String::fromUTF8ReplacingInvalidSequences(reinterpret_cast(bytes), length); + return Bun::fromString(str); +} + +extern "C" BunString BunString__createExternal(const char* bytes, size_t length, bool isLatin1, void* ctx, void (*callback)(void* arg0, void* arg1, size_t arg2)) +{ + Ref impl = isLatin1 ? WTF::ExternalStringImpl::create(reinterpret_cast(bytes), length, ctx, callback) : + + WTF::ExternalStringImpl::create(reinterpret_cast(bytes), length, ctx, callback); + + return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } }; +} + extern "C" void BunString__toWTFString(BunString* bunString) { if (bunString->tag == BunStringTag::ZigString) { -- cgit v1.2.3