diff options
author | 2022-01-22 19:39:23 -0800 | |
---|---|---|
committer | 2022-01-22 19:39:23 -0800 | |
commit | dc5745080d057a73ac8052a40c8950e8c7a8b51c (patch) | |
tree | eadf1f2c9516f1b920a112fcd0f9ba2c284cf422 | |
parent | 3dfac788fa5bed6fd8438515084d883c876e6a85 (diff) | |
download | bun-dc5745080d057a73ac8052a40c8950e8c7a8b51c.tar.gz bun-dc5745080d057a73ac8052a40c8950e8c7a8b51c.tar.zst bun-dc5745080d057a73ac8052a40c8950e8c7a8b51c.zip |
Ensure we fully copy strings
-rw-r--r-- | src/javascript/jsc/api/transpiler.zig | 6 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/helpers.h | 24 | ||||
-rw-r--r-- | src/linker.zig | 2 |
3 files changed, 23 insertions, 9 deletions
diff --git a/src/javascript/jsc/api/transpiler.zig b/src/javascript/jsc/api/transpiler.zig index a391dae26..c301311b3 100644 --- a/src/javascript/jsc/api/transpiler.zig +++ b/src/javascript/jsc/api/transpiler.zig @@ -207,9 +207,9 @@ pub const TransformTask = struct { buffer_writer = printer.ctx; buffer_writer.buffer.list.items = buffer_writer.written; - // This works around a mimalloc and/or Zig allocator bug - this.output_code = JSC.ZigString.init(buffer_writer.written); - this.output_code.mark(); + var output = JSC.ZigString.init(buffer_writer.written); + output.mark(); + this.output_code = output; } else { this.output_code = ZigString.init(""); } diff --git a/src/javascript/jsc/bindings/helpers.h b/src/javascript/jsc/bindings/helpers.h index 83ec088c5..d88d0cfde 100644 --- a/src/javascript/jsc/bindings/helpers.h +++ b/src/javascript/jsc/bindings/helpers.h @@ -66,10 +66,16 @@ static const JSC::ArgList makeArgs(JSC__JSValue *v, size_t count) { namespace Zig { +// 8 bit byte +// we tag the final two bits +// so 56 bits are copied over +// rest we zero out for consistentcy +static const unsigned char *untag(const unsigned char *ptr) { return ptr; } + static const JSC::Identifier toIdentifier(ZigString str, JSC::JSGlobalObject *global) { if (str.len == 0 || str.ptr == nullptr) { return JSC::Identifier::EmptyIdentifier; } - return JSC::Identifier::fromString(global->vm(), str.ptr, str.len); + return JSC::Identifier::fromString(global->vm(), untag(str.ptr), str.len); } static bool isTaggedUTF16Ptr(const unsigned char *ptr) { @@ -84,12 +90,18 @@ static const WTF::String toString(ZigString str) { if (str.len == 0 || str.ptr == nullptr) { return WTF::String(); } return !isTaggedUTF16Ptr(str.ptr) - ? WTF::String(WTF::StringImpl::createWithoutCopying(str.ptr, str.len)) + ? WTF::String(WTF::StringImpl::createWithoutCopying(untag(str.ptr), str.len)) : WTF::String(WTF::StringImpl::createWithoutCopying( - reinterpret_cast<const UChar *>(str.ptr), str.len)); + reinterpret_cast<const UChar *>(untag(str.ptr)), str.len)); } -static const WTF::String toStringCopy(ZigString str) { return toString(str).isolatedCopy(); } +static const WTF::String toStringCopy(ZigString str) { + if (str.len == 0 || str.ptr == nullptr) { return WTF::String(); } + + return !isTaggedUTF16Ptr(str.ptr) ? WTF::String(WTF::StringImpl::create(untag(str.ptr), str.len)) + : WTF::String(WTF::StringImpl::create( + reinterpret_cast<const UChar *>(untag(str.ptr)), str.len)); +} static WTF::String toStringNotConst(ZigString str) { return toString(str); } @@ -162,7 +174,9 @@ static ZigString toZigString(JSC::Identifier *str, JSC::JSGlobalObject *global) return toZigString(str->string()); } -static WTF::StringView toStringView(ZigString str) { return WTF::StringView(str.ptr, str.len); } +static WTF::StringView toStringView(ZigString str) { + return WTF::StringView(untag(str.ptr), str.len); +} static void throwException(JSC::ThrowScope &scope, ZigErrorType err, JSC::JSGlobalObject *global) { scope.throwException(global, diff --git a/src/linker.zig b/src/linker.zig index cc4080f79..b9bbe62e9 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -454,7 +454,7 @@ pub const Linker = struct { // We _assume_ you're importing ESM. // But, that assumption can be wrong without parsing code of the imports. // That's where in here, we inject - // > import {require} from 'bun:runtime'; + // > import {require} from 'bun:wrap'; // Since they definitely aren't using require, we don't have to worry about the symbol being renamed. if (needs_require and !result.ast.uses_require_ref) { result.ast.uses_require_ref = true; |