diff options
author | 2023-09-18 20:33:58 -0400 | |
---|---|---|
committer | 2023-09-18 17:33:58 -0700 | |
commit | eb1dc7eede31b12726ae320263850fefaaf490f3 (patch) | |
tree | 68a1a29b2505dc01fc65d94b0eecd40a06186778 | |
parent | 79dd196edd820db5212da1a524fd5f888b38a3aa (diff) | |
download | bun-eb1dc7eede31b12726ae320263850fefaaf490f3.tar.gz bun-eb1dc7eede31b12726ae320263850fefaaf490f3.tar.zst bun-eb1dc7eede31b12726ae320263850fefaaf490f3.zip |
fix(runtime/node): Allow `new Buffer.alloc()` + Upgrade WebKit (#5699)
* make bufferconstructor a static hash table
* chore: Upgrade WebKit to 4d995edbc44062b251be638818edcd88d7d14dd7
* make it constructable now
* fix comment
* yippee
* update CI workflows
-rw-r--r-- | .github/workflows/bun-linux-aarch64.yml | 2 | ||||
-rw-r--r-- | .github/workflows/bun-linux-build.yml | 4 | ||||
-rw-r--r-- | .github/workflows/bun-mac-aarch64.yml | 16 | ||||
-rw-r--r-- | .github/workflows/bun-mac-x64-baseline.yml | 16 | ||||
-rw-r--r-- | .github/workflows/bun-mac-x64.yml | 16 | ||||
-rwxr-xr-x | bun.lockb | bin | 73707 -> 73707 bytes | |||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 38 | ||||
-rw-r--r-- | src/bun.js/bindings/JSBuffer.lut.h | 52 | ||||
-rwxr-xr-x | src/bun.js/scripts/create_hash_table | 461 | ||||
-rw-r--r-- | src/js/_codegen/build-functions.ts | 23 | ||||
-rw-r--r-- | src/js/_codegen/static-hash-tables.ts | 1 | ||||
-rw-r--r-- | src/js/builtins.d.ts | 1 | ||||
-rw-r--r-- | src/js/builtins/JSBufferConstructor.ts | 3 | ||||
-rw-r--r-- | src/js/out/WebCoreJSBuiltins.cpp | 2 | ||||
-rw-r--r-- | test/js/node/buffer.test.js | 11 |
16 files changed, 452 insertions, 196 deletions
diff --git a/.github/workflows/bun-linux-aarch64.yml b/.github/workflows/bun-linux-aarch64.yml index e1b5ac6c0..49296bd2d 100644 --- a/.github/workflows/bun-linux-aarch64.yml +++ b/.github/workflows/bun-linux-aarch64.yml @@ -37,7 +37,7 @@ jobs: arch: aarch64 build_arch: arm64 runner: linux-arm64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-linux-arm64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-linux-arm64-lto.tar.gz" webkit_basename: "bun-webkit-linux-arm64-lto" build_machine_arch: aarch64 diff --git a/.github/workflows/bun-linux-build.yml b/.github/workflows/bun-linux-build.yml index 120391430..2bc131baf 100644 --- a/.github/workflows/bun-linux-build.yml +++ b/.github/workflows/bun-linux-build.yml @@ -47,7 +47,7 @@ jobs: arch: x86_64 build_arch: amd64 runner: big-ubuntu - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-linux-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-linux-amd64-lto.tar.gz" webkit_basename: "bun-webkit-linux-amd64-lto" build_machine_arch: x86_64 - cpu: nehalem @@ -55,7 +55,7 @@ jobs: arch: x86_64 build_arch: amd64 runner: big-ubuntu - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-linux-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-linux-amd64-lto.tar.gz" webkit_basename: "bun-webkit-linux-amd64-lto" build_machine_arch: x86_64 diff --git a/.github/workflows/bun-mac-aarch64.yml b/.github/workflows/bun-mac-aarch64.yml index 03c977c07..4bc4d1494 100644 --- a/.github/workflows/bun-mac-aarch64.yml +++ b/.github/workflows/bun-mac-aarch64.yml @@ -117,7 +117,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false # - cpu: haswell @@ -126,7 +126,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false # - cpu: nehalem @@ -135,7 +135,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true # - cpu: haswell @@ -144,7 +144,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true - cpu: native @@ -152,7 +152,7 @@ jobs: tag: bun-darwin-aarch64 obj: bun-obj-darwin-aarch64 artifact: bun-obj-darwin-aarch64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-arm64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-arm64-lto.tar.gz" runner: macos-arm64 dependencies: true compile_obj: true @@ -258,7 +258,7 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: haswell # arch: x86_64 # tag: bun-darwin-x64 @@ -266,14 +266,14 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" - cpu: native arch: aarch64 tag: bun-darwin-aarch64 obj: bun-obj-darwin-aarch64 package: bun-darwin-aarch64 artifact: bun-obj-darwin-aarch64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-arm64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-arm64-lto.tar.gz" runner: macos-arm64 steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/bun-mac-x64-baseline.yml b/.github/workflows/bun-mac-x64-baseline.yml index 7adbf03b4..cf3281bd8 100644 --- a/.github/workflows/bun-mac-x64-baseline.yml +++ b/.github/workflows/bun-mac-x64-baseline.yml @@ -117,7 +117,7 @@ jobs: obj: bun-obj-darwin-x64-baseline runner: macos-12 artifact: bun-obj-darwin-x64-baseline - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" dependencies: true compile_obj: false # - cpu: haswell @@ -126,7 +126,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false - cpu: nehalem @@ -135,7 +135,7 @@ jobs: obj: bun-obj-darwin-x64-baseline runner: macos-12 artifact: bun-obj-darwin-x64-baseline - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" dependencies: false compile_obj: true # - cpu: haswell @@ -144,7 +144,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true # - cpu: native @@ -152,7 +152,7 @@ jobs: # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # runner: macos-arm64 # dependencies: true # compile_obj: true @@ -259,7 +259,7 @@ jobs: package: bun-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64-baseline - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: haswell # arch: x86_64 # tag: bun-darwin-x64 @@ -267,14 +267,14 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: native # arch: aarch64 # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # package: bun-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # runner: macos-arm64 steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/bun-mac-x64.yml b/.github/workflows/bun-mac-x64.yml index d0e11a0be..a1d8b057a 100644 --- a/.github/workflows/bun-mac-x64.yml +++ b/.github/workflows/bun-mac-x64.yml @@ -117,7 +117,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false - cpu: haswell @@ -126,7 +126,7 @@ jobs: obj: bun-obj-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" dependencies: true compile_obj: false # - cpu: nehalem @@ -135,7 +135,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true - cpu: haswell @@ -144,7 +144,7 @@ jobs: obj: bun-obj-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" dependencies: false compile_obj: true # - cpu: native @@ -152,7 +152,7 @@ jobs: # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-arm64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-arm64-lto.tar.gz" # runner: macos-arm64 # dependencies: true # compile_obj: true @@ -261,7 +261,7 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" - cpu: haswell arch: x86_64 tag: bun-darwin-x64 @@ -269,14 +269,14 @@ jobs: package: bun-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: native # arch: aarch64 # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # package: bun-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15/bun-webkit-macos-arm64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-sept15-1/bun-webkit-macos-arm64-lto.tar.gz" # runner: macos-arm64 steps: - uses: actions/checkout@v3 Binary files differdiff --git a/package.json b/package.json index cacd1d5ff..7cd3ad94e 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "@types/react": "^18.0.25", "@typescript-eslint/eslint-plugin": "^5.31.0", "@typescript-eslint/parser": "^5.31.0", - "bun-webkit": "0.0.1-b4cf172ad2b038b8c4aa672c32d970d52e939c11" + "bun-webkit": "0.0.1-4d995edbc44062b251be638818edcd88d7d14dd7" }, "version": "0.0.0", "prettier": "./.prettierrc.cjs" diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index 9227e47b3..0ff8b76a5 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -1614,7 +1614,7 @@ JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_toBuffer, (JSGlobalObject * class JSBufferConstructor final : public JSC::InternalFunction { public: using Base = JSC::InternalFunction; - static constexpr unsigned StructureFlags = Base::StructureFlags; + static constexpr unsigned StructureFlags = Base::StructureFlags | HasStaticPropertyTable; ~JSBufferConstructor() = default; @@ -1700,8 +1700,6 @@ JSC_DEFINE_JIT_OPERATION(jsBufferConstructorAllocUnsafeSlowWithoutTypeChecks, JS JSC_ANNOTATE_HOST_FUNCTION(JSBufferConstructorConstruct, JSBufferConstructor::construct); -const ClassInfo JSBufferConstructor::s_info = { "Buffer"_s, nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSBufferConstructor) }; - class JSBuffer : public JSC::JSNonFinalObject { DECLARE_INFO; @@ -1917,27 +1915,27 @@ static const JSC::DOMJIT::Signature DOMJITSignaturejsBufferConstructorAllocUnsaf JSC::DOMJIT::Effect::forWriteKinds(JSC::DFG::AbstractHeapKind::Heap), JSC::SpecUint8Array, JSC::SpecInt32Only); -/* Hash table for constructor */ -static const HashTableValue JSBufferConstructorTableValues[] = { - // { "alloc"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DOMJITFunction), NoIntrinsic, { HashTableValue::DOMJITFunctionType, jsBufferConstructorFunction_alloc, &DOMJITSignaturejsBufferConstructorAlloc } }, - { "alloc"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_alloc, 1 } }, - // { "allocUnsafe"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DOMJITFunction), NoIntrinsic, { HashTableValue::DOMJITFunctionType, jsBufferConstructorFunction_allocUnsafe, &DOMJITSignaturejsBufferConstructorAllocUnsafe } }, - { "allocUnsafe"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_allocUnsafe, 1 } }, - // { "allocUnsafeSlow"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DOMJITFunction), NoIntrinsic, { HashTableValue::DOMJITFunctionType, jsBufferConstructorFunction_allocUnsafeSlow, &DOMJITSignaturejsBufferConstructorAllocUnsafeSlow } }, - { "allocUnsafeSlow"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_allocUnsafeSlow, 1 } }, - { "byteLength"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_byteLength, 2 } }, - { "compare"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_compare, 2 } }, - { "concat"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_concat, 2 } }, - { "from"_s, static_cast<unsigned>(JSC::PropertyAttribute::Builtin), NoIntrinsic, { HashTableValue::BuiltinGeneratorType, jsBufferConstructorFromCodeGenerator, 1 } }, - { "isBuffer"_s, static_cast<unsigned>(JSC::PropertyAttribute::Builtin), NoIntrinsic, { HashTableValue::BuiltinGeneratorType, jsBufferConstructorIsBufferCodeGenerator, 1 } }, - { "toBuffer"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_toBuffer, 1 } }, - { "isEncoding"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_isEncoding, 1 } }, -}; +/* Source for JSBuffer.lut.h +@begin jsBufferConstructorTable + alloc jsBufferConstructorFunction_alloc Constructable|Function 1 + allocUnsafe jsBufferConstructorFunction_allocUnsafe Constructable|Function 1 + allocUnsafeSlow jsBufferConstructorFunction_allocUnsafeSlow Constructable|Function 1 + byteLength jsBufferConstructorFunction_byteLength Function 2 + compare jsBufferConstructorFunction_compare Function 2 + concat jsBufferConstructorFunction_concat Function 2 + from JSBuiltin Builtin|Function 1 + isBuffer JSBuiltin Builtin|Function 1 + toBuffer jsBufferConstructorFunction_toBuffer Function 1 + isEncoding jsBufferConstructorFunction_isEncoding Function 1 +@end +*/ +#include "JSBuffer.lut.h" + +const ClassInfo JSBufferConstructor::s_info = { "Buffer"_s, nullptr, &jsBufferConstructorTable, nullptr, CREATE_METHOD_TABLE(JSBufferConstructor) }; void JSBufferConstructor::finishCreation(VM& vm, JSGlobalObject* globalObject, JSC::JSObject* prototype) { Base::finishCreation(vm, 3, "Buffer"_s, PropertyAdditionMode::WithoutStructureTransition); - reifyStaticProperties(vm, JSBufferConstructor::info(), JSBufferConstructorTableValues, *this); putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); prototype->putDirect(vm, vm.propertyNames->speciesSymbol, this, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); } diff --git a/src/bun.js/bindings/JSBuffer.lut.h b/src/bun.js/bindings/JSBuffer.lut.h new file mode 100644 index 000000000..47e8cb984 --- /dev/null +++ b/src/bun.js/bindings/JSBuffer.lut.h @@ -0,0 +1,52 @@ +// File generated via `make static-hash-table` / `make cpp` +static const struct CompactHashIndex jsBufferConstructorTableIndex[33] = { + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 1, 32 }, + { 0, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 8, -1 }, + { 3, -1 }, + { -1, -1 }, + { 5, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 9, -1 }, + { -1, -1 }, + { 4, -1 }, + { -1, -1 }, + { 6, -1 }, + { -1, -1 }, + { -1, -1 }, + { -1, -1 }, + { 2, -1 }, + { -1, -1 }, + { 7, -1 }, +}; + +static const struct HashTableValue jsBufferConstructorTableValues[10] = { + { "alloc"_s, static_cast<unsigned>(PropertyAttribute::Constructable|PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_alloc, 1 } }, + { "allocUnsafe"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_allocUnsafe, 1 } }, + { "allocUnsafeSlow"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_allocUnsafeSlow, 1 } }, + { "byteLength"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_byteLength, 2 } }, + { "compare"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_compare, 2 } }, + { "concat"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_concat, 2 } }, + { "from"_s, ((static_cast<unsigned>(PropertyAttribute::Builtin|PropertyAttribute::Function)) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, NoIntrinsic, { HashTableValue::BuiltinGeneratorType, jsBufferConstructorFromCodeGenerator, 1 } }, + { "isBuffer"_s, ((static_cast<unsigned>(PropertyAttribute::Builtin|PropertyAttribute::Function)) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, NoIntrinsic, { HashTableValue::BuiltinGeneratorType, jsBufferConstructorIsBufferCodeGenerator, 1 } }, + { "toBuffer"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_toBuffer, 1 } }, + { "isEncoding"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsBufferConstructorFunction_isEncoding, 1 } }, +}; + +static const struct HashTable jsBufferConstructorTable = + { 10, 31, false, nullptr, jsBufferConstructorTableValues, jsBufferConstructorTableIndex }; diff --git a/src/bun.js/scripts/create_hash_table b/src/bun.js/scripts/create_hash_table index e2645b429..bd604ceaa 100755 --- a/src/bun.js/scripts/create_hash_table +++ b/src/bun.js/scripts/create_hash_table @@ -5,7 +5,7 @@ # (c) 2000-2002 by Harri Porten <porten@kde.org> and # David Faure <faure@kde.org> # Modified (c) 2004 by Nikolas Zimmermann <wildfox@kde.org> -# Copyright (C) 2007-2022 Apple Inc. All rights reserved. +# Copyright (C) 2007-2023 Apple Inc. All rights reserved. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -24,6 +24,7 @@ use strict; use warnings; +use Math::BigInt; use Getopt::Long qw(:config pass_through); my $file = shift @ARGV or die("Must provide source file as final argument."); @@ -33,7 +34,6 @@ open(IN, $file) or die "No such file $file"; my @keys = (); my @attrs = (); my @values = (); -my @hashes = (); my @table = (); my @links = (); @@ -46,11 +46,13 @@ my $pefectHashSize; my $compactSize; my $compactHashSizeMask; my $banner = 0; -sub calcPerfectHashSize(); -sub calcCompactHashSize(); +my $mask64 = 2**64 - 1; +my $mask32 = 2**32 - 1; +sub calcPerfectHashSize($); +sub calcCompactHashSize($); sub output(); sub jsc_ucfirst($); -sub hashValue($); +sub hashValue($$); while (<IN>) { chomp; @@ -64,16 +66,11 @@ while (<IN>) { print STDERR "WARNING: \@begin without table name, skipping $_\n"; } } elsif (/^\@end\s*$/ && $inside) { - calcPerfectHashSize(); - calcCompactHashSize(); output(); @keys = (); @attrs = (); @values = (); - @hashes = (); - @table = (); - @links = (); $includeBuiltin = 0; $inside = 0; @@ -114,7 +111,6 @@ while (<IN>) { } else { push(@values, { "type" => "Lexer", "value" => $val }); } - push(@hashes, hashValue($key)); } elsif ($inside) { die "invalid data {" . $_ . "}"; } @@ -147,13 +143,14 @@ sub ceilingToPowerOf2 return $powerOf2; } -sub calcPerfectHashSize() +sub calcPerfectHashSize($) { + my ($isMac) = @_; tableSizeLoop: for ($pefectHashSize = ceilingToPowerOf2(scalar @keys); ; $pefectHashSize += $pefectHashSize) { my @table = (); foreach my $key (@keys) { - my $h = hashValue($key) % $pefectHashSize; + my $h = hashValue($key, $isMac) % $pefectHashSize; next tableSizeLoop if $table[$h]; $table[$h] = 1; } @@ -166,8 +163,9 @@ sub leftShift($$) { return (($value << $distance) & 0xFFFFFFFF); } -sub calcCompactHashSize() +sub calcCompactHashSize($) { + my ($isMac) = @_; my $compactHashSize = ceilingToPowerOf2(2 * @keys); $compactHashSizeMask = $compactHashSize - 1; $compactSize = $compactHashSize; @@ -176,7 +174,7 @@ sub calcCompactHashSize() my $i = 0; foreach my $key (@keys) { my $depth = 0; - my $h = hashValue($key) % $compactHashSize; + my $h = hashValue($key, $isMac) % $compactHashSize; while (defined($table[$h])) { if (defined($links[$h])) { $h = $links[$h]; @@ -194,60 +192,222 @@ sub calcCompactHashSize() } } +sub avalancheBits($) { + my ($value) = @_; + + $value &= $mask32; + + # Force "avalanching" of lower 32 bits + $value ^= leftShift($value, 3); + $value += ($value >> 5); + $value = ($value & $mask32); + $value ^= (leftShift($value, 2) & $mask32); + $value += ($value >> 15); + $value = $value & $mask32; + $value ^= (leftShift($value, 10) & $mask32); + + return $value; +} + +sub maskTop8BitsAndAvoidZero($) { + my ($value) = @_; + + $value &= $mask32; + + # Save 8 bits for StringImpl to use as flags. + $value &= 0xffffff; + + # This avoids ever returning a hash code of 0, since that is used to + # signal "hash not computed yet". Setting the high bit maintains + # reasonable fidelity to a hash code of 0 because it is likely to yield + # exactly 0 when hash lookup masks out the high bits. + $value = (0x80000000 >> 8) if ($value == 0); + + return $value; +} + # Paul Hsieh's SuperFastHash # http://www.azillionmonkeys.com/qed/hash.html -sub hashValue($) { - my @chars = split(/ */, $_[0]); - - # This hash is designed to work on 16-bit chunks at a time. But since the normal case - # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they - # were 16-bit chunks, which should give matching results - - my $EXP2_32 = 4294967296; - - my $hash = 0x9e3779b9; - my $l = scalar @chars; #I wish this was in Ruby --- Maks - my $rem = $l & 1; - $l = $l >> 1; - - my $s = 0; - - # Main loop - for (; $l > 0; $l--) { - $hash += ord($chars[$s]); - my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash; - $hash = (leftShift($hash, 16)% $EXP2_32) ^ $tmp; - $s += 2; - $hash += $hash >> 11; - $hash %= $EXP2_32; - } - - # Handle end case - if ($rem != 0) { - $hash += ord($chars[$s]); - $hash ^= (leftShift($hash, 11)% $EXP2_32); - $hash += $hash >> 17; - } - - # Force "avalanching" of final 127 bits - $hash ^= leftShift($hash, 3); - $hash += ($hash >> 5); - $hash = ($hash% $EXP2_32); - $hash ^= (leftShift($hash, 2)% $EXP2_32); - $hash += ($hash >> 15); - $hash = $hash% $EXP2_32; - $hash ^= (leftShift($hash, 10)% $EXP2_32); - - # Save 8 bits for StringImpl to use as flags. - $hash &= 0xffffff; - - # This avoids ever returning a hash code of 0, since that is used to - # signal "hash not computed yet". Setting the high bit maintains - # reasonable fidelity to a hash code of 0 because it is likely to yield - # exactly 0 when hash lookup masks out the high bits. - $hash = (0x80000000 >> 8) if ($hash == 0); - - return $hash; +sub superFastHash { + my @chars = @_; + + # This hash is designed to work on 16-bit chunks at a time. But since the normal case + # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they + # were 16-bit chunks, which should give matching results + + my $hash = 0x9e3779b9; + my $l = scalar @chars; #I wish this was in Ruby --- Maks + my $rem = $l & 1; + $l = $l >> 1; + + my $s = 0; + + # Main loop + for (; $l > 0; $l--) { + $hash += ord($chars[$s]); + my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash; + $hash = (leftShift($hash, 16) & $mask32) ^ $tmp; + $s += 2; + $hash += $hash >> 11; + $hash &= $mask32; + } + + # Handle end case + if ($rem != 0) { + $hash += ord($chars[$s]); + $hash ^= (leftShift($hash, 11) & $mask32); + $hash += $hash >> 17; + } + + $hash = avalancheBits($hash); + return maskTop8BitsAndAvoidZero($hash); +} + +sub uint64_add($$) { + my ($a, $b) = @_; + my $sum = $a + $b; + return $sum & $mask64; +} + +sub uint64_multi($$) { + my ($a, $b) = @_; + my $product = $a * $b; + return $product & $mask64; +} + +sub wymum($$) { + my ($A, $B) = @_; + + my $ha = $A >> 32; + my $hb = $B >> 32; + my $la = $A & $mask32; + my $lb = $B & $mask32; + my $hi; + my $lo; + my $rh = uint64_multi($ha, $hb); + my $rm0 = uint64_multi($ha, $lb); + my $rm1 = uint64_multi($hb, $la); + my $rl = uint64_multi($la, $lb); + my $t = uint64_add($rl, ($rm0 << 32)); + my $c = int($t < $rl); + + $lo = uint64_add($t, ($rm1 << 32)); + $c += int($lo < $t); + $hi = uint64_add($rh, uint64_add(($rm0 >> 32), uint64_add(($rm1 >> 32), $c))); + + return ($lo, $hi); +}; + +sub wymix($$) { + my ($A, $B) = @_; + ($A, $B) = wymum($A, $B); + return $A ^ $B; +} + +sub convert32BitTo64Bit($) { + my ($v) = @_; + my ($mask1) = 281470681808895; # 0x0000_ffff_0000_ffff + $v = ($v | ($v << 16)) & $mask1; + my ($mask2) = 71777214294589695; # 0x00ff_00ff_00ff_00ff + return ($v | ($v << 8)) & $mask2; +} + +sub convert16BitTo32Bit($) { + my ($v) = @_; + return ($v | ($v << 8)) & 0x00ff_00ff; +} + +sub wyhash { + # https://github.com/wangyi-fudan/wyhash + my @chars = @_; + my $charCount = scalar @chars; + my $byteCount = $charCount << 1; + my $charIndex = 0; + my $seed = 0; + my @secret = ( 11562461410679940143, 16646288086500911323, 10285213230658275043, 6384245875588680899 ); + my $move1 = (($byteCount >> 3) << 2) >> 1; + + $seed ^= wymix($seed ^ $secret[0], $secret[1]); + my $a = 0; + my $b = 0; + + local *c2i = sub { + my ($i) = @_; + return ord($chars[$i]); + }; + + local *wyr8 = sub { + my ($i) = @_; + my $v = c2i($i) | (c2i($i + 1) << 8) | (c2i($i + 2) << 16) | (c2i($i + 3) << 24); + return convert32BitTo64Bit($v); + }; + + local *wyr4 = sub { + my ($i) = @_; + my $v = c2i($i) | (c2i($i + 1) << 8); + return convert16BitTo32Bit($v); + }; + + local *wyr2 = sub { + my ($i) = @_; + return c2i($i) << 16; + }; + + if ($byteCount <= 16) { + if ($byteCount >= 4) { + $a = (wyr4($charIndex) << 32) | wyr4($charIndex + $move1); + $charIndex = $charIndex + $charCount - 2; + $b = (wyr4($charIndex) << 32) | wyr4($charIndex - $move1); + } elsif ($byteCount > 0) { + $a = wyr2($charIndex); + $b = 0; + } else { + $a = $b = 0; + } + } else { + my $i = $byteCount; + if ($i > 48) { + my $see1 = $seed; + my $see2 = $seed; + do { + $seed = wymix(wyr8($charIndex) ^ $secret[1], wyr8($charIndex + 4) ^ $seed); + $see1 = wymix(wyr8($charIndex + 8) ^ $secret[2], wyr8($charIndex + 12) ^ $see1); + $see2 = wymix(wyr8($charIndex + 16) ^ $secret[3], wyr8($charIndex + 20) ^ $see2); + $charIndex += 24; + $i -= 48; + } while ($i > 48); + $seed ^= $see1 ^ $see2; + } + while ($i > 16) { + $seed = wymix(wyr8($charIndex) ^ $secret[1], wyr8($charIndex + 4) ^ $seed); + $i -= 16; + $charIndex += 8; + } + my $move2 = $i >> 1; + $a = wyr8($charIndex + $move2 - 8); + $b = wyr8($charIndex + $move2 - 4); + } + $a ^= $secret[1]; + $b ^= $seed; + + ($a, $b) = wymum($a, $b); + my $hash = wymix($a ^ $secret[0] ^ $byteCount, $b ^ $secret[1]) & $mask32; + + return maskTop8BitsAndAvoidZero($hash); +} + +sub hashValue($$) { + my ($string, $isMac) = @_; + my @chars = split(/ */, $string); + my $charCount = scalar @chars; + if ($isMac) { + if ($charCount <= 48) { + return superFastHash(@chars); + } + return wyhash(@chars); + } else { + return superFastHash(@chars); + } } sub output() { @@ -267,81 +427,110 @@ sub output() { print "\n"; print "namespace JSC {\n"; print "\n"; - if ($compactSize != 0) { - print "static const struct CompactHashIndex ${nameIndex}\[$compactSize\] = {\n"; - for (my $i = 0; $i < $compactSize; $i++) { - my $T = -1; - if (defined($table[$i])) { $T = $table[$i]; } - my $L = -1; - if (defined($links[$i])) { $L = $links[$i]; } - print " { $T, $L },\n"; + + local *generateHashTableHelper = sub { + my ($isMac, $setToOldValues) = @_; + my $oldCompactSize = $compactSize; + my $oldCompactHashSizeMask = $compactHashSizeMask; + calcPerfectHashSize($isMac); + calcCompactHashSize($isMac); + + my $hashTableString = ""; + + if ($compactSize != 0) { + $hashTableString .= "static const struct CompactHashIndex ${nameIndex}\[$compactSize\] = {\n"; + for (my $i = 0; $i < $compactSize; $i++) { + my $T = -1; + if (defined($table[$i])) { $T = $table[$i]; } + my $L = -1; + if (defined($links[$i])) { $L = $links[$i]; } + $hashTableString .= " { $T, $L },\n"; + } + } else { + # MSVC dislikes empty arrays. + $hashTableString .= "static const struct CompactHashIndex ${nameIndex}\[1\] = {\n"; + $hashTableString .= " { 0, 0 }\n"; } - } else { - # MSVC dislikes empty arrays. - print "static const struct CompactHashIndex ${nameIndex}\[1\] = {\n"; - print " { 0, 0 }\n"; - } - print "};\n"; - print "\n"; + $hashTableString .= "};\n"; + $hashTableString .= "\n"; - my $packedSize = scalar @keys; - if ($packedSize != 0) { - print "static const struct HashTableValue ${nameEntries}\[$packedSize\] = {\n"; - } else { - # MSVC dislikes empty arrays. - print "static const struct HashTableValue ${nameEntries}\[1\] = {\n"; - print " { { }, 0, NoIntrinsic, { HashTableValue::End } }\n"; - } - my $i = 0; - foreach my $key (@keys) { - my $typeTag = ""; - my $firstValue = ""; - my $secondValue = ""; - my $hasSecondValue = 1; - my $intrinsic = "NoIntrinsic"; - - if ($values[$i]{"type"} eq "PropertyAttribute::Function") { - $typeTag = "NativeFunction"; - $firstValue = $values[$i]{"function"}; - $secondValue = $values[$i]{"params"}; - $intrinsic = $values[$i]{"intrinsic"}; - } elsif ($values[$i]{"type"} eq "PropertyAttribute::Property") { - $typeTag = "GetterSetter"; - $firstValue = $values[$i]{"get"}; - $secondValue = $values[$i]{"put"}; - } elsif ($values[$i]{"type"} eq "Lexer") { - $typeTag = "Lexer"; - $firstValue = $values[$i]{"value"}; - $hasSecondValue = 0; - } elsif ($values[$i]{"type"} eq "PropertyAttribute::CellProperty" || $values[$i]{"type"} eq "PropertyAttribute::ClassStructure") { - $typeTag = ($values[$i]{"type"} eq "PropertyAttribute::CellProperty") ? "LazyCellProperty" : "LazyClassStructure"; - $values[$i]{"property"} =~ /\A([a-zA-Z0-9_]+)::(.*)\Z/ or die; - $firstValue = "OBJECT_OFFSETOF($1, $2)"; - $hasSecondValue = 0; - } elsif ($values[$i]{"type"} eq "PropertyAttribute::PropertyCallback") { - $typeTag = "LazyProperty"; - $firstValue = $values[$i]{"cback"}; - $hasSecondValue = 0; + my $packedSize = scalar @keys; + if ($packedSize != 0) { + $hashTableString .= "static const struct HashTableValue ${nameEntries}\[$packedSize\] = {\n"; + } else { + # MSVC dislikes empty arrays. + $hashTableString .= "static const struct HashTableValue ${nameEntries}\[1\] = {\n"; + $hashTableString .= " { { }, 0, NoIntrinsic, { HashTableValue::End } }\n"; } - my $attributes = "PropertyAttribute::" . $attrs[$i]; - $attributes =~ s/\|/\|PropertyAttribute::/g; - $attributes = "static_cast<unsigned>(" . $attributes . ")"; - if ($values[$i]{"type"} eq "PropertyAttribute::Function" && $firstValue eq "JSBuiltin") { - $typeTag = "BuiltinGenerator"; - my $tableHead = $name; - $tableHead =~ s/Table$//; - print " { \"$key\"_s, (($attributes) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, $intrinsic, { HashTableValue::" . $typeTag . "Type, " . $tableHead . ucfirst($key) . "CodeGenerator, $secondValue } },\n"; + my $i = 0; + foreach my $key (@keys) { + my $typeTag = ""; + my $firstValue = ""; + my $secondValue = ""; + my $hasSecondValue = 1; + my $intrinsic = "NoIntrinsic"; + + if ($values[$i]{"type"} eq "PropertyAttribute::Function") { + $typeTag = "NativeFunction"; + $firstValue = $values[$i]{"function"}; + $secondValue = $values[$i]{"params"}; + $intrinsic = $values[$i]{"intrinsic"}; + } elsif ($values[$i]{"type"} eq "PropertyAttribute::Property") { + $typeTag = "GetterSetter"; + $firstValue = $values[$i]{"get"}; + $secondValue = $values[$i]{"put"}; + } elsif ($values[$i]{"type"} eq "Lexer") { + $typeTag = "Lexer"; + $firstValue = $values[$i]{"value"}; + $hasSecondValue = 0; + } elsif ($values[$i]{"type"} eq "PropertyAttribute::CellProperty" || $values[$i]{"type"} eq "PropertyAttribute::ClassStructure") { + $typeTag = ($values[$i]{"type"} eq "PropertyAttribute::CellProperty") ? "LazyCellProperty" : "LazyClassStructure"; + $values[$i]{"property"} =~ /\A([a-zA-Z0-9_]+)::(.*)\Z/ or die; + $firstValue = "OBJECT_OFFSETOF($1, $2)"; + $hasSecondValue = 0; + } elsif ($values[$i]{"type"} eq "PropertyAttribute::PropertyCallback") { + $typeTag = "LazyProperty"; + $firstValue = $values[$i]{"cback"}; + $hasSecondValue = 0; + } + + my $attributes = "PropertyAttribute::" . $attrs[$i]; + $attributes =~ s/\|/\|PropertyAttribute::/g; + $attributes = "static_cast<unsigned>(" . $attributes . ")"; + if ($values[$i]{"type"} eq "PropertyAttribute::Function" && $firstValue eq "JSBuiltin") { + $typeTag = "BuiltinGenerator"; + my $tableHead = $name; + $tableHead =~ s/Table$//; + $hashTableString .= " { \"$key\"_s, (($attributes) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin, $intrinsic, { HashTableValue::" . $typeTag . "Type, " . $tableHead . ucfirst($key) . "CodeGenerator, $secondValue } },\n"; + } + else { + $hashTableString .= " { \"$key\"_s, $attributes, $intrinsic, { HashTableValue::" . $typeTag . "Type, $firstValue" . ($hasSecondValue ? ", " . $secondValue : "") . " } },\n"; + } + $i++; } - else { - print " { \"$key\"_s, $attributes, $intrinsic, { HashTableValue::" . $typeTag . "Type, $firstValue" . ($hasSecondValue ? ", " . $secondValue : "") . " } },\n"; + $hashTableString .= "};\n"; + $hashTableString .= "\n"; + $hashTableString .= "static const struct HashTable $name =\n"; + $hashTableString .= " \{ $packedSize, $compactHashSizeMask, $hasSetter, nullptr, $nameEntries, $nameIndex \};\n"; + $hashTableString .= "\n"; + + @table = (); + @links = (); + if ($setToOldValues) { + $compactSize = $oldCompactSize; + $compactHashSizeMask = $oldCompactHashSizeMask; } - $i++; + return $hashTableString; + }; + + my $hashTableForMacOS = generateHashTableHelper(1, 1); + my $hashTableForIOS = generateHashTableHelper(0, 0); + my $hashTableToWrite = $hashTableForMacOS; + if ($hashTableForMacOS ne $hashTableForIOS) { + $hashTableToWrite = "#if PLATFORM(MAC)\n" . $hashTableForMacOS . "#else\n" . $hashTableForIOS . "#endif\n"; } - print "};\n"; - print "\n"; - print "static const struct HashTable $name =\n"; - print " \{ $packedSize, $compactHashSizeMask, $hasSetter, nullptr, $nameEntries, $nameIndex \};\n"; - print "\n"; + print $hashTableToWrite; + print "} // namespace JSC\n"; } diff --git a/src/js/_codegen/build-functions.ts b/src/js/_codegen/build-functions.ts index d4d6f9235..6e4114624 100644 --- a/src/js/_codegen/build-functions.ts +++ b/src/js/_codegen/build-functions.ts @@ -28,9 +28,9 @@ interface BundledBuiltin { name: string; directives: Record<string, any>; isGetter: boolean; - isConstructor: boolean; + constructAbility: string; + constructKind: string; isLinkTimeConstant: boolean; - isNakedConstructor: boolean; intrinsic: string; overriddenName: string; source: string; @@ -97,12 +97,13 @@ async function processFileSplit(filename: string): Promise<{ functions: BundledB throw new SyntaxError("Could not parse directive value " + directive[2] + " (must be JSON parsable)"); } if (name === "constructor") { - throw new SyntaxError("$constructor not implemented"); + directives.ConstructAbility = "CanConstruct"; + } else if (name === "nakedConstructor") { + directives.ConstructAbility = "CanConstruct"; + directives.ConstructKind = "Naked"; + } else { + directives[name] = value; } - if (name === "nakedConstructor") { - throw new SyntaxError("$nakedConstructor not implemented"); - } - directives[name] = value; contents = contents.slice(directive[0].length); } else if (match[1] === "export function" || match[1] === "export async function") { const declaration = contents.match( @@ -192,9 +193,9 @@ $$capture_start$$(${fn.async ? "async " : ""}${ params: fn.params, visibility: fn.directives.visibility ?? (fn.directives.linkTimeConstant ? "Private" : "Public"), isGetter: !!fn.directives.getter, - isConstructor: !!fn.directives.constructor, + constructAbility: fn.directives.ConstructAbility ?? "CannotConstruct", + constructKind: fn.directives.ConstructKind ?? "None", isLinkTimeConstant: !!fn.directives.linkTimeConstant, - isNakedConstructor: !!fn.directives.nakedConstructor, intrinsic: fn.directives.intrinsic ?? "NoIntrinsic", overriddenName: fn.directives.getter ? `"get ${fn.name}"_s` @@ -256,8 +257,8 @@ for (const { basename, functions } of files) { for (const fn of functions) { const name = `${lowerBasename}${cap(fn.name)}Code`; bundledCPP += `// ${fn.name} -const JSC::ConstructAbility s_${name}ConstructAbility = JSC::ConstructAbility::CannotConstruct; -const JSC::ConstructorKind s_${name}ConstructorKind = JSC::ConstructorKind::None; +const JSC::ConstructAbility s_${name}ConstructAbility = JSC::ConstructAbility::${fn.constructAbility}; +const JSC::ConstructorKind s_${name}ConstructorKind = JSC::ConstructorKind::${fn.constructKind}; const JSC::ImplementationVisibility s_${name}ImplementationVisibility = JSC::ImplementationVisibility::${fn.visibility}; const int s_${name}Length = ${fn.source.length}; static const JSC::Intrinsic s_${name}Intrinsic = JSC::NoIntrinsic; diff --git a/src/js/_codegen/static-hash-tables.ts b/src/js/_codegen/static-hash-tables.ts index be2aa908d..858974aa1 100644 --- a/src/js/_codegen/static-hash-tables.ts +++ b/src/js/_codegen/static-hash-tables.ts @@ -7,6 +7,7 @@ const STATIC_HASH_TABLES = [ // "src/bun.js/bindings/BunObject.cpp", "src/bun.js/bindings/ZigGlobalObject.lut.txt", + "src/bun.js/bindings/JSBuffer.cpp", "src/bun.js/bindings/Process.cpp", "src/bun.js/bindings/ProcessBindingConstants.cpp", "src/bun.js/bindings/ProcessBindingNatives.cpp", diff --git a/src/js/builtins.d.ts b/src/js/builtins.d.ts index 8d3e7df11..1b1a2328e 100644 --- a/src/js/builtins.d.ts +++ b/src/js/builtins.d.ts @@ -26,6 +26,7 @@ declare var $visibility: "Public" | "Private"; declare var $nakedConstructor: never; /** Assign to this directly above a function declaration (like a decorator) to set intrinsic */ declare var $intrinsic: string; +/** Assign to this directly above a function declaration (like a decorator) to make it a constructor. */ declare var $constructor; /** Place this directly above a function declaration (like a decorator) to NOT include "use strict" */ declare var $sloppy; diff --git a/src/js/builtins/JSBufferConstructor.ts b/src/js/builtins/JSBufferConstructor.ts index a1072ea10..69615d8dc 100644 --- a/src/js/builtins/JSBufferConstructor.ts +++ b/src/js/builtins/JSBufferConstructor.ts @@ -1,3 +1,6 @@ +// This is marked as a constructor because Node.js allows `new Buffer.from`, +// Some legacy dependencies depend on this, see #3638 +$constructor; export function from(items) { if ($isUndefinedOrNull(items)) { throw new TypeError( diff --git a/src/js/out/WebCoreJSBuiltins.cpp b/src/js/out/WebCoreJSBuiltins.cpp index d161cd315..4e927c00d 100644 --- a/src/js/out/WebCoreJSBuiltins.cpp +++ b/src/js/out/WebCoreJSBuiltins.cpp @@ -2023,7 +2023,7 @@ WEBCORE_FOREACH_READABLESTREAMBYOBREADER_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR) /* JSBufferConstructor.ts */ // from -const JSC::ConstructAbility s_jsBufferConstructorFromCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; +const JSC::ConstructAbility s_jsBufferConstructorFromCodeConstructAbility = JSC::ConstructAbility::CanConstruct; const JSC::ConstructorKind s_jsBufferConstructorFromCodeConstructorKind = JSC::ConstructorKind::None; const JSC::ImplementationVisibility s_jsBufferConstructorFromCodeImplementationVisibility = JSC::ImplementationVisibility::Public; const int s_jsBufferConstructorFromCodeLength = 1538; diff --git a/test/js/node/buffer.test.js b/test/js/node/buffer.test.js index afc9cdee8..0256934ce 100644 --- a/test/js/node/buffer.test.js +++ b/test/js/node/buffer.test.js @@ -2585,3 +2585,14 @@ it("construct buffer from hex, issue #4919", () => { expect(buf1).toStrictEqual(Buffer.from([])); expect(buf2).toStrictEqual(Buffer.from([0x63, 0xe9, 0xf6, 0xc4, 0xb0, 0x4f, 0xa8, 0xc8, 0x0f, 0x3f, 0xb0, 0xee])); }); + +it("new Buffer.alloc()", () => { + const buf = new Buffer.alloc(10); + expect(buf.length).toBe(10); + expect(buf[0]).toBe(0); +}); + +it("new Buffer.from()", () => { + const buf = new Buffer.from("🥶"); + expect(buf.length).toBe(4); +}); |