diff options
author | 2023-01-16 12:49:34 -0800 | |
---|---|---|
committer | 2023-01-16 12:49:57 -0800 | |
commit | 4648131c416ad708c339789dd1979d4d8e738dd8 (patch) | |
tree | 3eacabac543e8c0e7eb744798c798155c6046812 /src/bun.js/modules/BufferModule.h | |
parent | b0702ce7b156f9dbeb3f4f65fe94b12044e28334 (diff) | |
download | bun-4648131c416ad708c339789dd1979d4d8e738dd8.tar.gz bun-4648131c416ad708c339789dd1979d4d8e738dd8.tar.zst bun-4648131c416ad708c339789dd1979d4d8e738dd8.zip |
Add missing `buffer` module exports
Diffstat (limited to 'src/bun.js/modules/BufferModule.h')
-rw-r--r-- | src/bun.js/modules/BufferModule.h | 92 |
1 files changed, 79 insertions, 13 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 |