aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/builtins/cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-21 03:12:59 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-21 03:12:59 -0800
commitd955bfe50f7aa15aca9df3bf0a40fea26a132381 (patch)
tree2433e2b9dfc4bf712903417008f9b0c5b3900d69 /src/bun.js/builtins/cpp
parentb8648adf873758a83911d3d28c94255d8c3fdd3c (diff)
downloadbun-d955bfe50f7aa15aca9df3bf0a40fea26a132381.tar.gz
bun-d955bfe50f7aa15aca9df3bf0a40fea26a132381.tar.zst
bun-d955bfe50f7aa15aca9df3bf0a40fea26a132381.zip
[buffer] Make Buffer.from pass more tests
Diffstat (limited to 'src/bun.js/builtins/cpp')
-rw-r--r--src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.cpp105
-rw-r--r--src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.h9
2 files changed, 60 insertions, 54 deletions
diff --git a/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.cpp b/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.cpp
index dd03c2a77..213060f58 100644
--- a/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.cpp
+++ b/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.cpp
@@ -48,67 +48,82 @@
namespace WebCore {
-const JSC::ConstructAbility s_jsBufferConstructorAllocCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
-const JSC::ConstructorKind s_jsBufferConstructorAllocCodeConstructorKind = JSC::ConstructorKind::None;
-const JSC::ImplementationVisibility s_jsBufferConstructorAllocCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
-const int s_jsBufferConstructorAllocCodeLength = 185;
-static const JSC::Intrinsic s_jsBufferConstructorAllocCodeIntrinsic = JSC::NoIntrinsic;
-const char* const s_jsBufferConstructorAllocCode =
- "(function (n) {\n" \
- " \"use strict\";\n" \
- " if (typeof n !== \"number\" || n < 0) {\n" \
- " @throwRangeError(\"n must be a positive integer less than 2^32\");\n" \
- " }\n" \
- " \n" \
- " return new this(n);\n" \
- "})\n" \
-;
-
const JSC::ConstructAbility s_jsBufferConstructorFromCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_jsBufferConstructorFromCodeConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_jsBufferConstructorFromCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
-const int s_jsBufferConstructorFromCodeLength = 1035;
+const int s_jsBufferConstructorFromCodeLength = 1832;
static const JSC::Intrinsic s_jsBufferConstructorFromCodeIntrinsic = JSC::NoIntrinsic;
const char* const s_jsBufferConstructorFromCode =
"(function (items) {\n" \
" \"use strict\";\n" \
"\n" \
" if (!@isConstructor(this))\n" \
- " @throwTypeError(\"Buffer.from requires |this| to be a constructor\");\n" \
+ " @throwTypeError(\"Buffer.from requires |this| to be a constructor\");\n" \
"\n" \
+ " if (@isUndefinedOrNull(items))\n" \
+ " @throwTypeError(\n" \
+ " \"The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object.\",\n" \
+ " );\n" \
"\n" \
- " //\n" \
- " if (typeof items === 'string' || (typeof items === 'object' && items && (items instanceof ArrayBuffer || items instanceof SharedArrayBuffer))) {\n" \
- " switch (@argumentCount()) {\n" \
- " case 1: {\n" \
- " return new this(items);\n" \
- " }\n" \
- " case 2: {\n" \
- " return new this(items, @argument(1));\n" \
- " }\n" \
- " default: {\n" \
- " return new this(items, @argument(1), @argument(2));\n" \
- " }\n" \
- " }\n" \
+ " //\n" \
+ " if (\n" \
+ " typeof items === \"string\" ||\n" \
+ " (typeof items === \"object\" &&\n" \
+ " (@isTypedArrayView(items) ||\n" \
+ " items instanceof ArrayBuffer ||\n" \
+ " items instanceof SharedArrayBuffer ||\n" \
+ " items instanceof @String))\n" \
+ " ) {\n" \
+ " switch (@argumentCount()) {\n" \
+ " case 1: {\n" \
+ " return new this(items);\n" \
+ " }\n" \
+ " case 2: {\n" \
+ " return new this(items, @argument(1));\n" \
+ " }\n" \
+ " default: {\n" \
+ " return new this(items, @argument(1), @argument(2));\n" \
+ " }\n" \
" }\n" \
+ " }\n" \
"\n" \
+ " var arrayLike = @toObject(\n" \
+ " items,\n" \
+ " \"The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object.\",\n" \
+ " );\n" \
"\n" \
- " var arrayLike = @toObject(items, \"Buffer.from requires an array-like object - not null or undefined\");\n" \
+ " if (!@isJSArray(arrayLike)) {\n" \
+ " const toPrimitive = @tryGetByIdWithWellKnownSymbol(items, \"toPrimitive\");\n" \
"\n" \
- " //\n" \
- " //\n" \
- " //\n" \
- " if (@isTypedArrayView(arrayLike)) {\n" \
- " var length = @typedArrayLength(arrayLike);\n" \
- " var result = this.allocUnsafe(length);\n" \
- " result.set(arrayLike);\n" \
- " return result;\n" \
- " } \n" \
+ " if (toPrimitive) {\n" \
+ " const primitive = toPrimitive.@call(items, \"string\");\n" \
+ "\n" \
+ " if (typeof primitive === \"string\") {\n" \
+ " switch (@argumentCount()) {\n" \
+ " case 1: {\n" \
+ " return new this(primitive);\n" \
+ " }\n" \
+ " case 2: {\n" \
+ " return new this(primitive, @argument(1));\n" \
+ " }\n" \
+ " default: {\n" \
+ " return new this(primitive, @argument(1), @argument(2));\n" \
+ " }\n" \
+ " }\n" \
+ " }\n" \
+ " }\n" \
+ "\n" \
+ " if (!(\"length\" in arrayLike) || @isCallable(arrayLike)) {\n" \
+ " @throwTypeError(\n" \
+ " \"The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object.\",\n" \
+ " );\n" \
+ " }\n" \
+ " }\n" \
"\n" \
- " //\n" \
- " //\n" \
- " //\n" \
- " return new this(@Uint8Array.from(arrayLike).buffer);\n" \
+ " //\n" \
+ " //\n" \
+ " //\n" \
+ " return new this(@Uint8Array.from(arrayLike).buffer);\n" \
"})\n" \
;
diff --git a/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.h b/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.h
index 05058bba3..f9181b402 100644
--- a/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.h
+++ b/src/bun.js/builtins/cpp/JSBufferConstructorBuiltins.h
@@ -47,11 +47,6 @@ class FunctionExecutable;
namespace WebCore {
/* JSBufferConstructor */
-extern const char* const s_jsBufferConstructorAllocCode;
-extern const int s_jsBufferConstructorAllocCodeLength;
-extern const JSC::ConstructAbility s_jsBufferConstructorAllocCodeConstructAbility;
-extern const JSC::ConstructorKind s_jsBufferConstructorAllocCodeConstructorKind;
-extern const JSC::ImplementationVisibility s_jsBufferConstructorAllocCodeImplementationVisibility;
extern const char* const s_jsBufferConstructorFromCode;
extern const int s_jsBufferConstructorFromCodeLength;
extern const JSC::ConstructAbility s_jsBufferConstructorFromCodeConstructAbility;
@@ -59,18 +54,14 @@ extern const JSC::ConstructorKind s_jsBufferConstructorFromCodeConstructorKind;
extern const JSC::ImplementationVisibility s_jsBufferConstructorFromCodeImplementationVisibility;
#define WEBCORE_FOREACH_JSBUFFERCONSTRUCTOR_BUILTIN_DATA(macro) \
- macro(alloc, jsBufferConstructorAlloc, 1) \
macro(from, jsBufferConstructorFrom, 1) \
-#define WEBCORE_BUILTIN_JSBUFFERCONSTRUCTOR_ALLOC 1
#define WEBCORE_BUILTIN_JSBUFFERCONSTRUCTOR_FROM 1
#define WEBCORE_FOREACH_JSBUFFERCONSTRUCTOR_BUILTIN_CODE(macro) \
- macro(jsBufferConstructorAllocCode, alloc, ASCIILiteral(), s_jsBufferConstructorAllocCodeLength) \
macro(jsBufferConstructorFromCode, from, ASCIILiteral(), s_jsBufferConstructorFromCodeLength) \
#define WEBCORE_FOREACH_JSBUFFERCONSTRUCTOR_BUILTIN_FUNCTION_NAME(macro) \
- macro(alloc) \
macro(from) \
#define DECLARE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \