aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/BunString.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-06-13 09:15:05 -0700
committerGravatar GitHub <noreply@github.com> 2023-06-13 09:15:05 -0700
commitbdb1b7124aec3ca42a13dd13309df4c8e4e3cc64 (patch)
tree57a7a278699999521f561959204a533ea9906f8e /src/bun.js/bindings/BunString.cpp
parentb93bdbb124fc7b1b4a09d414158e0107e8d66b92 (diff)
downloadbun-bdb1b7124aec3ca42a13dd13309df4c8e4e3cc64.tar.gz
bun-bdb1b7124aec3ca42a13dd13309df4c8e4e3cc64.tar.zst
bun-bdb1b7124aec3ca42a13dd13309df4c8e4e3cc64.zip
Fix crash in CJS (#3294)bun-v0.6.9
* 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>
Diffstat (limited to 'src/bun.js/bindings/BunString.cpp')
-rw-r--r--src/bun.js/bindings/BunString.cpp27
1 files changed, 26 insertions, 1 deletions
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<const LChar*>(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<WTF::ExternalStringImpl> impl = isLatin1 ? WTF::ExternalStringImpl::create(reinterpret_cast<const LChar*>(bytes), length, ctx, callback) :
+
+ WTF::ExternalStringImpl::create(reinterpret_cast<const UChar*>(bytes), length, ctx, callback);
+
+ return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } };
+}
+
extern "C" void BunString__toWTFString(BunString* bunString)
{
if (bunString->tag == BunStringTag::ZigString) {