diff options
| author | 2023-01-16 12:49:34 -0800 | |
|---|---|---|
| committer | 2023-01-16 12:49:57 -0800 | |
| commit | 4648131c416ad708c339789dd1979d4d8e738dd8 (patch) | |
| tree | 3eacabac543e8c0e7eb744798c798155c6046812 | |
| parent | b0702ce7b156f9dbeb3f4f65fe94b12044e28334 (diff) | |
| download | bun-4648131c416ad708c339789dd1979d4d8e738dd8.tar.gz bun-4648131c416ad708c339789dd1979d4d8e738dd8.tar.zst bun-4648131c416ad708c339789dd1979d4d8e738dd8.zip | |
Add missing `buffer` module exports
| -rw-r--r-- | src/bun.js/modules/BufferModule.h | 92 | ||||
| -rw-r--r-- | test/bun.js/buffer.test.js | 24 |
2 files changed, 101 insertions, 15 deletions
diff --git a/src/bun.js/modules/BufferModule.h b/src/bun.js/modules/BufferModule.h index afc00ffd1..42eab5173 100644 --- a/src/bun.js/modules/BufferModule.h +++ b/src/bun.js/modules/BufferModule.h @@ -1,9 +1,22 @@ #include "../bindings/JSBuffer.h" #include "../bindings/ZigGlobalObject.h" #include "JavaScriptCore/JSGlobalObject.h" +#include "JavaScriptCore/ObjectConstructor.h" namespace Zig { using namespace WebCore; +using namespace JSC; + +JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplemented, + (JSGlobalObject * globalObject, + CallFrame *callFrame)) { + VM &vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + throwException(globalObject, scope, + createError(globalObject, "Not implemented"_s)); + return JSValue::encode(jsUndefined()); +} inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, JSC::Identifier moduleKey, @@ -13,8 +26,22 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, GlobalObject *globalObject = reinterpret_cast<GlobalObject *>(lexicalGlobalObject); - exportNames.append(JSC::Identifier::fromString(vm, "Buffer"_s)); - exportValues.append(globalObject->JSBufferConstructor()); + JSC::JSObject *defaultObject = JSC::constructEmptyObject( + globalObject, globalObject->objectPrototype(), 12); + + defaultObject->putDirect(vm, + PropertyName(Identifier::fromUid( + vm.symbolRegistry().symbolForKey("CommonJS"_s))), + jsNumber(0), 0); + + auto exportProperty = [&](JSC::Identifier name, JSC::JSValue value) { + exportNames.append(name); + exportValues.append(value); + defaultObject->putDirect(vm, name, value, 0); + }; + + exportProperty(JSC::Identifier::fromString(vm, "Buffer"_s), + globalObject->JSBufferConstructor()); auto *slowBuffer = JSC::JSFunction::create( vm, globalObject, 0, "SlowBuffer"_s, WebCore::constructSlowBuffer, @@ -24,21 +51,60 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, vm, vm.propertyNames->prototype, globalObject->JSBufferPrototype(), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete); - exportNames.append(JSC::Identifier::fromString(vm, "SlowBuffer"_s)); - exportValues.append(slowBuffer); + exportProperty(JSC::Identifier::fromString(vm, "SlowBuffer"_s), slowBuffer); + auto blobIdent = JSC::Identifier::fromString(vm, "Blob"_s); + + JSValue blobValue = + lexicalGlobalObject->get(globalObject, PropertyName(blobIdent)); + exportProperty(blobIdent, blobValue); + + // TODO: implement File + exportProperty(JSC::Identifier::fromString(vm, "File"_s), blobValue); + + exportProperty(JSC::Identifier::fromString(vm, "INSPECT_MAX_BYTES"_s), + JSC::jsNumber(50)); + + exportProperty(JSC::Identifier::fromString(vm, "kMaxLength"_s), + JSC::jsNumber(4294967296LL)); + + exportProperty(JSC::Identifier::fromString(vm, "kStringMaxLength"_s), + JSC::jsNumber(536870888)); + + JSC::JSObject *constants = JSC::constructEmptyObject( + lexicalGlobalObject, globalObject->objectPrototype(), 2); + constants->putDirect(vm, JSC::Identifier::fromString(vm, "MAX_LENGTH"_s), + JSC::jsNumber(4294967296LL)); + constants->putDirect(vm, + JSC::Identifier::fromString(vm, "MAX_STRING_LENGTH"_s), + JSC::jsNumber(536870888)); + + exportProperty(JSC::Identifier::fromString(vm, "constants"_s), constants); + + JSC::Identifier atobI = JSC::Identifier::fromString(vm, "atob"_s); + JSC::JSValue atobV = + lexicalGlobalObject->get(globalObject, PropertyName(atobI)); + + JSC::Identifier btoaI = JSC::Identifier::fromString(vm, "btoa"_s); + JSC::JSValue btoaV = + lexicalGlobalObject->get(globalObject, PropertyName(btoaI)); + + exportProperty(atobI, atobV); + exportProperty(btoaI, btoaV); + + auto *transcode = InternalFunction::createFunctionThatMasqueradesAsUndefined( + vm, globalObject, 1, "transcode"_s, jsFunctionNotImplemented); - exportNames.append(JSC::Identifier::fromString(vm, "Blob"_s)); - exportValues.append(lexicalGlobalObject->get( - globalObject, PropertyName(Identifier::fromString(vm, "Blob"_s)))); + exportProperty(JSC::Identifier::fromString(vm, "transcode"_s), transcode); - exportNames.append(JSC::Identifier::fromString(vm, "INSPECT_MAX_BYTES"_s)); - exportValues.append(JSC::jsNumber(50)); + auto *resolveObjectURL = + InternalFunction::createFunctionThatMasqueradesAsUndefined( + vm, globalObject, 1, "resolveObjectURL"_s, jsFunctionNotImplemented); - exportNames.append(JSC::Identifier::fromString(vm, "kMaxLength"_s)); - exportValues.append(JSC::jsNumber(4294967296LL)); + exportProperty(JSC::Identifier::fromString(vm, "resolveObjectURL"_s), + resolveObjectURL); - exportNames.append(JSC::Identifier::fromString(vm, "kMaxLength"_s)); - exportValues.append(JSC::jsNumber(536870888)); + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(defaultObject); } } // namespace Zig diff --git a/test/bun.js/buffer.test.js b/test/bun.js/buffer.test.js index d263a80eb..64c6cc41f 100644 --- a/test/bun.js/buffer.test.js +++ b/test/bun.js/buffer.test.js @@ -1,6 +1,8 @@ import { describe, it, expect, beforeEach, afterEach } from "bun:test"; import { gc } from "./gc"; +const BufferModule = await import("buffer"); + beforeEach(() => gc()); afterEach(() => gc()); @@ -560,7 +562,7 @@ it("Buffer.swap16", () => { const examples = [ ["", ""], ["a1", "1a"], - ["a1b2", "1a2b"] + ["a1b2", "1a2b"], ]; for (let i = 0; i < examples.length; i++) { @@ -586,7 +588,7 @@ it("Buffer.swap32", () => { const examples = [ ["", ""], ["a1b2", "2b1a"], - ["a1b2c3d4", "2b1a4d3c"] + ["a1b2c3d4", "2b1a4d3c"], ]; for (let i = 0; i < examples.length; i++) { @@ -698,3 +700,21 @@ it("Buffer can be mocked", () => { buf.writeBigUInt64LE(0); }).not.toThrow(); }); + +it("constants", () => { + expect(BufferModule.constants.MAX_LENGTH).toBe(4294967296); + expect(BufferModule.constants.MAX_STRING_LENGTH).toBe(536870888); + expect(BufferModule.default.constants.MAX_LENGTH).toBe(4294967296); + expect(BufferModule.default.constants.MAX_STRING_LENGTH).toBe(536870888); +}); + +it("File", () => { + expect(BufferModule.File).toBe(Blob); +}); + +it("transcode", () => { + expect(typeof BufferModule.transcode).toBe("undefined"); + + // This is a masqueradesAsUndefined function + expect(() => BufferModule.transcode()).toThrow("Not implemented"); +}); |
