aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-02 18:06:21 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-02 18:06:21 -0700
commitc3f8593f8cb1571b41b8233e5ded98e3d3f99fb0 (patch)
treec6d45c32b1d63b8bf6f347146b67255b0bccdb6b /src
parent4cbda049e97b2e1049cd2c4c93a617a54931b220 (diff)
downloadbun-c3f8593f8cb1571b41b8233e5ded98e3d3f99fb0.tar.gz
bun-c3f8593f8cb1571b41b8233e5ded98e3d3f99fb0.tar.zst
bun-c3f8593f8cb1571b41b8233e5ded98e3d3f99fb0.zip
[node:buffer] Implement `isUtf8` and `isAscii` (#3498)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/modules/BufferModule.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/bun.js/modules/BufferModule.h b/src/bun.js/modules/BufferModule.h
index a96fb18c8..6e6e39e9c 100644
--- a/src/bun.js/modules/BufferModule.h
+++ b/src/bun.js/modules/BufferModule.h
@@ -2,11 +2,127 @@
#include "../bindings/ZigGlobalObject.h"
#include "JavaScriptCore/JSGlobalObject.h"
#include "JavaScriptCore/ObjectConstructor.h"
+#include "simdutf.h"
namespace Zig {
using namespace WebCore;
using namespace JSC;
+// TODO: Add DOMJIT fast path
+JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_isUtf8,
+ (JSC::JSGlobalObject * lexicalGlobalObject,
+ JSC::CallFrame *callframe)) {
+ auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm());
+
+ auto buffer = callframe->argument(0);
+ auto *bufferView = JSC::jsDynamicCast<JSC::JSArrayBufferView *>(buffer);
+ const char *ptr = nullptr;
+ size_t byteLength = 0;
+ if (bufferView) {
+ if (UNLIKELY(bufferView->isDetached())) {
+ throwTypeError(lexicalGlobalObject, throwScope,
+ "ArrayBufferView is detached"_s);
+ return JSValue::encode({});
+ }
+
+ byteLength = bufferView->byteLength();
+
+ if (byteLength == 0) {
+ return JSValue::encode(jsBoolean(true));
+ }
+
+ ptr = reinterpret_cast<const char *>(bufferView->vector());
+ } else if (auto *arrayBuffer =
+ JSC::jsDynamicCast<JSC::JSArrayBuffer *>(buffer)) {
+ auto *impl = arrayBuffer->impl();
+
+ if (!impl) {
+ return JSValue::encode(jsBoolean(true));
+ }
+
+ if (UNLIKELY(impl->isDetached())) {
+ throwTypeError(lexicalGlobalObject, throwScope,
+ "ArrayBuffer is detached"_s);
+ return JSValue::encode({});
+ }
+
+ byteLength = impl->byteLength();
+
+ if (byteLength == 0) {
+ return JSValue::encode(jsBoolean(true));
+ }
+
+ ptr = reinterpret_cast<const char *>(impl->data());
+ } else {
+ throwVMError(
+ lexicalGlobalObject, throwScope,
+ createTypeError(lexicalGlobalObject,
+ "First argument must be an ArrayBufferView"_s));
+ return JSValue::encode({});
+ }
+
+ RELEASE_AND_RETURN(throwScope, JSValue::encode(jsBoolean(
+ simdutf::validate_utf8(ptr, byteLength))));
+}
+
+// TODO: Add DOMJIT fast path
+JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_isAscii,
+ (JSC::JSGlobalObject * lexicalGlobalObject,
+ JSC::CallFrame *callframe)) {
+ auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm());
+
+ auto buffer = callframe->argument(0);
+ auto *bufferView = JSC::jsDynamicCast<JSC::JSArrayBufferView *>(buffer);
+ const char *ptr = nullptr;
+ size_t byteLength = 0;
+ if (bufferView) {
+
+ if (UNLIKELY(bufferView->isDetached())) {
+ throwTypeError(lexicalGlobalObject, throwScope,
+ "ArrayBufferView is detached"_s);
+ return JSValue::encode({});
+ }
+
+ byteLength = bufferView->byteLength();
+
+ if (byteLength == 0) {
+ return JSValue::encode(jsBoolean(true));
+ }
+
+ ptr = reinterpret_cast<const char *>(bufferView->vector());
+ } else if (auto *arrayBuffer =
+ JSC::jsDynamicCast<JSC::JSArrayBuffer *>(buffer)) {
+ auto *impl = arrayBuffer->impl();
+ if (UNLIKELY(impl->isDetached())) {
+ throwTypeError(lexicalGlobalObject, throwScope,
+ "ArrayBuffer is detached"_s);
+ return JSValue::encode({});
+ }
+
+ if (!impl) {
+ return JSValue::encode(jsBoolean(true));
+ }
+
+ byteLength = impl->byteLength();
+
+ if (byteLength == 0) {
+ return JSValue::encode(jsBoolean(true));
+ }
+
+ ptr = reinterpret_cast<const char *>(impl->data());
+ } else {
+ throwVMError(
+ lexicalGlobalObject, throwScope,
+ createTypeError(lexicalGlobalObject,
+ "First argument must be an ArrayBufferView"_s));
+ return JSValue::encode({});
+ }
+
+ RELEASE_AND_RETURN(
+ throwScope,
+ JSValue::encode(jsBoolean(simdutf::validate_ascii(ptr, byteLength))));
+}
+
JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplemented,
(JSGlobalObject * globalObject,
CallFrame *callFrame)) {
@@ -106,6 +222,20 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject,
exportProperty(JSC::Identifier::fromString(vm, "resolveObjectURL"_s),
resolveObjectURL);
+ exportProperty(JSC::Identifier::fromString(vm, "isAscii"_s),
+ JSC::JSFunction::create(vm, globalObject, 1, "isAscii"_s,
+ jsBufferConstructorFunction_isAscii,
+ ImplementationVisibility::Public,
+ NoIntrinsic,
+ jsBufferConstructorFunction_isUtf8));
+
+ exportProperty(JSC::Identifier::fromString(vm, "isUtf8"_s),
+ JSC::JSFunction::create(vm, globalObject, 1, "isUtf8"_s,
+ jsBufferConstructorFunction_isUtf8,
+ ImplementationVisibility::Public,
+ NoIntrinsic,
+ jsBufferConstructorFunction_isUtf8));
+
exportNames.append(vm.propertyNames->defaultKeyword);
exportValues.append(defaultObject);
}
date lockfile * test(e2e): fix preact mdx e2e test * fix(mdx): disable .md support * test(e2e): fix vue-component test missing mdx * test(e2e): fix solid component needing import * fix: allow `client:only="solid"` as an alias to `solid-js` * chore: move to with-mdx example * chore: update MDX readme * chore: update example readme * chore: bump astro version * chore: update lockfile * Update mod.d.ts * feat: support `export const components` in MDX pages * chore: update mdx example * fix: update jsx-runtime with better slot support * refactor: remove object style support * chore: cleanup package exports * chore: add todo comment * refactor: improve isPage function, move to utils * refactor: dry up manual HMR updates * chore: add dev tests for MDX * chore: prefer set to array * chore: add changesets * fix(hmr): flip public/private route Co-authored-by: Nate Moore <nate@astro.build> 2022-06-30Fix integration name (`prefetch` instead of `lit`) (#3778)Gravatar hippotastic 2-1/+6 2022-06-30[ci] update lockfile (#3771)Gravatar Fred K. Bot 1-114/+112 Co-authored-by: FredKSchott <FredKSchott@users.noreply.github.com> 2022-06-30Integration Docs Next Steps (#3677)Gravatar Dan Jutan 11-314/+666 * sitemap readme skeleton + first sections * Revert "sitemap readme skeleton + first sections" This reverts commit cc55b312b6dc95522645002806d63f32c33d1956. * sitemap readme skeleton + first sections * remove canonicalURL option from sitemap * add customPages option to readme * sitemap examples * partytown * deno run command * reference deno example * node readme * netlify & vercel readmes * note that telemetry is installed * telemetry is *enabled*, not installed * Update packages/integrations/vercel/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/integrations/vercel/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * readme -> README * Update packages/integrations/deno/readme.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/integrations/deno/readme.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * qualify they * Update packages/integrations/sitemap/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Uppercase README names * Update packages/integrations/partytown/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * imports -> import typo * update changeset Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> 2022-06-30[ci] formatGravatar tony-sull 1-2/+2 2022-06-30refactor to provide better cli error handling (#3768)Gravatar Fred K. Schott 2-43/+37 2022-06-30[ci] release (#3772)@astrojs/preact@0.3.1Gravatar Fred K. Bot 12-22/+23 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-06-30Added Cloudflare adapter to README.md (#3773)Gravatar Isaac McFadyen 1-0/+1 2022-06-30[ci] formatGravatar hippotastic 1-5/+4 2022-06-30Fix "Invalid hook call" warning (#3769)Gravatar hippotastic 2-9/+79 * Fix "Invalid hook call" warning * Fix eslint warnings * Apply code review suggestions 2022-06-29[ci] release (#3759)astro@1.0.0-beta.59@astrojs/telemetry@0.2.2@astrojs/preact@0.3.0Gravatar Fred K. Bot 42-121/+117 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-06-29[ci] formatGravatar FredKSchott 8-35/+36 2022-06-29manual lockfile update (#3751)Gravatar Fred K. Schott 3-2659/+2871 * lockfile update * update lockfile gen script * Update index.ts 2022-06-29add error event to telemetry (#3750)Gravatar Fred K. Schott 16-85/+270