aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ashcon Partovi <ashcon@partovi.net> 2023-07-05 13:16:10 -0700
committerGravatar Ashcon Partovi <ashcon@partovi.net> 2023-07-05 13:21:04 -0700
commit17bacd85f981180b1bf82515dfafafcada661fd7 (patch)
tree9b2ac8b6b4342c5becc541d4af8074af0afbd8c9
parentf61d9ef4761444d0550aea90b2656727d41ce1fa (diff)
downloadbun-17bacd85f981180b1bf82515dfafafcada661fd7.tar.gz
bun-17bacd85f981180b1bf82515dfafafcada661fd7.tar.zst
bun-17bacd85f981180b1bf82515dfafafcada661fd7.zip
Fix detect-libc
-rw-r--r--src/bun.js/bindings/BunDetectLibcModule.cpp34
-rw-r--r--src/bun.js/bindings/BunDetectLibcModule.h4
-rw-r--r--src/bun.js/bindings/JSSink.cpp2
-rw-r--r--src/bun.js/bindings/JSSink.h2
-rw-r--r--src/bun.js/bindings/JSSinkLookupTable.h2
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp6
-rw-r--r--src/bun.js/module_loader.zig2
-rw-r--r--src/js/out/modules/thirdparty/detect-libc.js29
-rw-r--r--src/js/out/modules/thirdparty/detect-libc.linux.js29
-rw-r--r--src/js/thirdparty/detect-libc.js32
-rw-r--r--src/js/thirdparty/detect-libc.linux.js35
-rwxr-xr-xtest/bun.lockbbin141113 -> 146552 bytes
-rw-r--r--test/js/first_party/detect-libc.test.ts20
-rw-r--r--test/js/first_party/undici.test.ts (renamed from test/js/first_party/undici/undici.test.ts)2
-rw-r--r--test/js/first_party/utf-8-validate.test.ts9
-rw-r--r--test/package.json6
16 files changed, 116 insertions, 98 deletions
diff --git a/src/bun.js/bindings/BunDetectLibcModule.cpp b/src/bun.js/bindings/BunDetectLibcModule.cpp
new file mode 100644
index 000000000..f7e2c1945
--- /dev/null
+++ b/src/bun.js/bindings/BunDetectLibcModule.cpp
@@ -0,0 +1,34 @@
+#include "root.h"
+
+#include "JavaScriptCore/JavaScript.h"
+#include "wtf/text/WTFString.h"
+#include "JavaScriptCore/ObjectConstructor.h"
+
+#if defined(__LINUX__)
+#include <gnu/libc-version.h>
+#endif
+
+using namespace JSC;
+using namespace WTF;
+
+JSC::JSObject* createDetectLibcModule(JSC::JSGlobalObject* globalObject)
+{
+ VM& vm = globalObject->vm();
+ JSC::JSObject* object = nullptr;
+
+ {
+ JSC::ObjectInitializationScope initializationScope(vm);
+ object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 2);
+ #if defined(__LINUX__)
+ auto version = JSC::jsString(vm, makeAtomString(gnu_get_libc_version()));
+ auto family = JSC::jsString(vm, makeAtomString("glibc"));
+ #else
+ auto version = JSC::jsNull();
+ auto family = JSC::jsNull();
+ #endif
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "version"_s), version, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
+ object->putDirect(vm, JSC::Identifier::fromString(vm, "family"_s), family, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
+ }
+
+ return object;
+}
diff --git a/src/bun.js/bindings/BunDetectLibcModule.h b/src/bun.js/bindings/BunDetectLibcModule.h
new file mode 100644
index 000000000..4051e6750
--- /dev/null
+++ b/src/bun.js/bindings/BunDetectLibcModule.h
@@ -0,0 +1,4 @@
+#include "root.h"
+#include "JavaScriptCore/JSObject.h"
+
+JSC::JSObject* createDetectLibcModule(JSC::JSGlobalObject* globalObject); \ No newline at end of file
diff --git a/src/bun.js/bindings/JSSink.cpp b/src/bun.js/bindings/JSSink.cpp
index 5f99d3792..3a69cc79a 100644
--- a/src/bun.js/bindings/JSSink.cpp
+++ b/src/bun.js/bindings/JSSink.cpp
@@ -1,6 +1,6 @@
// AUTO-GENERATED FILE. DO NOT EDIT.
-// Generated by 'make generate-sink' at 2023-07-02T16:19:51.440Z
+// Generated by 'make generate-sink' at 2023-07-05T19:37:39.554Z
// To regenerate this file, run:
//
// make generate-sink
diff --git a/src/bun.js/bindings/JSSink.h b/src/bun.js/bindings/JSSink.h
index 41d7065dc..dc68935d9 100644
--- a/src/bun.js/bindings/JSSink.h
+++ b/src/bun.js/bindings/JSSink.h
@@ -1,6 +1,6 @@
// AUTO-GENERATED FILE. DO NOT EDIT.
-// Generated by 'make generate-sink' at 2023-07-02T16:19:51.438Z
+// Generated by 'make generate-sink' at 2023-07-05T19:37:39.553Z
//
#pragma once
diff --git a/src/bun.js/bindings/JSSinkLookupTable.h b/src/bun.js/bindings/JSSinkLookupTable.h
index e4ed81629..7ff4c3f9c 100644
--- a/src/bun.js/bindings/JSSinkLookupTable.h
+++ b/src/bun.js/bindings/JSSinkLookupTable.h
@@ -1,4 +1,4 @@
-// Automatically generated from src/bun.js/bindings/JSSink.cpp using /home/cirospaciari/Repos/bun/src/bun.js/WebKit/Source/JavaScriptCore/create_hash_table. DO NOT EDIT!
+// Automatically generated from src/bun.js/bindings/JSSink.cpp using /Users/ashcon/Desktop/code/bun/src/bun.js/WebKit/Source/JavaScriptCore/create_hash_table. DO NOT EDIT!
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 4bb5445e1..09b82daf7 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -114,6 +114,8 @@
#include "JSEnvironmentVariableMap.h"
#include "DOMIsoSubspaces.h"
+#include "BunDetectLibcModule.h"
+
#if ENABLE(REMOTE_INSPECTOR)
#include "JavaScriptCore/RemoteInspectorServer.h"
#endif
@@ -1587,6 +1589,10 @@ JSC:
return JSValue::encode(obj);
}
+ if (string == "detect-libc"_s) {
+ return JSC::JSValue::encode(createDetectLibcModule(globalObject));
+ }
+
if (UNLIKELY(string == "noop"_s)) {
auto* obj = constructEmptyObject(globalObject);
obj->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "getterSetter"_s)), JSC::CustomGetterSetter::create(vm, noop_getter, noop_setter), 0);
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index e7e4d700e..2f3f7492b 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -1868,7 +1868,7 @@ pub const ModuleLoader = struct {
.@"node:wasi" => return jsResolvedSource(jsc_vm, jsc_vm.load_builtins_from_path, .@"node:wasi", "node/wasi.js", specifier),
.@"node:zlib" => return jsResolvedSource(jsc_vm, jsc_vm.load_builtins_from_path, .@"node:zlib", "node/zlib.js", specifier),
- .@"detect-libc" => return jsResolvedSource(jsc_vm, jsc_vm.load_builtins_from_path, .@"detect-libc", if (Environment.isLinux) "thirdparty/detect-libc.linux.js" else "thirdparty/detect-libc.js", specifier),
+ .@"detect-libc" => return jsResolvedSource(jsc_vm, jsc_vm.load_builtins_from_path, .@"detect-libc", "thirdparty/detect-libc.js", specifier),
.undici => return jsResolvedSource(jsc_vm, jsc_vm.load_builtins_from_path, .undici, "thirdparty/undici.js", specifier),
.ws => return jsResolvedSource(jsc_vm, jsc_vm.load_builtins_from_path, .ws, "thirdparty/ws.js", specifier),
diff --git a/src/js/out/modules/thirdparty/detect-libc.js b/src/js/out/modules/thirdparty/detect-libc.js
index 1c4e18a7a..47aaefa5e 100644
--- a/src/js/out/modules/thirdparty/detect-libc.js
+++ b/src/js/out/modules/thirdparty/detect-libc.js
@@ -1,28 +1,31 @@
-function family() {
- return Promise.resolve(familySync());
-}
-function familySync() {
- return null;
+function version() {
+ return libcVersion;
}
function versionAsync() {
- return Promise.resolve(version());
+ return Promise.resolve(libcVersion);
}
-function version() {
- return null;
+function family() {
+ return libcFamily;
}
-function isNonGlibcLinuxSync() {
- return !1;
+function familyAsync() {
+ return Promise.resolve(libcFamily);
}
function isNonGlibcLinux() {
- return Promise.resolve(isNonGlibcLinuxSync());
+ return Promise.resolve(libcFamily === "musl");
+}
+function isNonGlibcLinuxSync() {
+ return libcFamily === "musl";
}
-var GLIBC = "glibc", MUSL = "musl";
+var {
+ version: libcVersion,
+ family: libcFamily
+} = globalThis[Symbol.for("Bun.lazy")]("detect-libc"), GLIBC = "glibc", MUSL = "musl";
export {
versionAsync,
version,
isNonGlibcLinuxSync,
isNonGlibcLinux,
- familySync,
+ familyAsync,
family,
MUSL,
GLIBC
diff --git a/src/js/out/modules/thirdparty/detect-libc.linux.js b/src/js/out/modules/thirdparty/detect-libc.linux.js
deleted file mode 100644
index 1c4e18a7a..000000000
--- a/src/js/out/modules/thirdparty/detect-libc.linux.js
+++ /dev/null
@@ -1,29 +0,0 @@
-function family() {
- return Promise.resolve(familySync());
-}
-function familySync() {
- return null;
-}
-function versionAsync() {
- return Promise.resolve(version());
-}
-function version() {
- return null;
-}
-function isNonGlibcLinuxSync() {
- return !1;
-}
-function isNonGlibcLinux() {
- return Promise.resolve(isNonGlibcLinuxSync());
-}
-var GLIBC = "glibc", MUSL = "musl";
-export {
- versionAsync,
- version,
- isNonGlibcLinuxSync,
- isNonGlibcLinux,
- familySync,
- family,
- MUSL,
- GLIBC
-};
diff --git a/src/js/thirdparty/detect-libc.js b/src/js/thirdparty/detect-libc.js
index 514a76536..21110cc66 100644
--- a/src/js/thirdparty/detect-libc.js
+++ b/src/js/thirdparty/detect-libc.js
@@ -1,27 +1,33 @@
-// Hardcoded module "detect-libc" for darwin
-export function family() {
- return Promise.resolve(familySync());
-}
+// https://www.npmjs.com/package/detect-libc
-export function familySync() {
- return null;
-}
+const {
+ version: libcVersion,
+ family: libcFamily,
+} = globalThis[Symbol.for("Bun.lazy")]("detect-libc");
export const GLIBC = "glibc";
export const MUSL = "musl";
+export function version() {
+ return libcVersion;
+}
+
export function versionAsync() {
- return Promise.resolve(version());
+ return Promise.resolve(libcVersion);
}
-export function version() {
- return null;
+export function family() {
+ return libcFamily;
}
-export function isNonGlibcLinuxSync() {
- return false;
+export function familyAsync() {
+ return Promise.resolve(libcFamily);
}
export function isNonGlibcLinux() {
- return Promise.resolve(isNonGlibcLinuxSync());
+ return Promise.resolve(libcFamily === MUSL);
+}
+
+export function isNonGlibcLinuxSync() {
+ return libcFamily === MUSL;
}
diff --git a/src/js/thirdparty/detect-libc.linux.js b/src/js/thirdparty/detect-libc.linux.js
deleted file mode 100644
index 4c6557247..000000000
--- a/src/js/thirdparty/detect-libc.linux.js
+++ /dev/null
@@ -1,35 +0,0 @@
-// Hardcoded module "detect-libc" for linux
-export function family() {
- return Promise.resolve(familySync());
-}
-
-export function familySync() {
- if (process.platform === "linux") {
- return GLIBC;
- } else {
- return null;
- }
-}
-
-export const GLIBC = "glibc";
-export const MUSL = "musl";
-
-export function versionAsync() {
- return Promise.resolve(version());
-}
-
-export function version() {
- if (process.platform === "linux") {
- return "2.29";
- } else {
- return null;
- }
-}
-
-export function isNonGlibcLinuxSync() {
- return false;
-}
-
-export function isNonGlibcLinux() {
- return Promise.resolve(isNonGlibcLinuxSync());
-}
diff --git a/test/bun.lockb b/test/bun.lockb
index c35182d46..e8566947c 100755
--- a/test/bun.lockb
+++ b/test/bun.lockb
Binary files differ
diff --git a/test/js/first_party/detect-libc.test.ts b/test/js/first_party/detect-libc.test.ts
new file mode 100644
index 000000000..a5b77994f
--- /dev/null
+++ b/test/js/first_party/detect-libc.test.ts
@@ -0,0 +1,20 @@
+import { test, expect } from "bun:test";
+// @ts-ignore: @types/detect-libc is not accurate
+import { GLIBC, MUSL, version, versionAsync, family, familyAsync, isNonGlibcLinux, isNonGlibcLinuxSync } from "detect-libc";
+
+test("detect-libc", () => {
+ expect(GLIBC).toBe("glibc");
+ expect(MUSL).toBe("musl");
+ if (process.platform === "linux") {
+ expect(version()).toMatch(/^\d+\.\d+/);
+ expect(family()).toBe(GLIBC);
+ expect(isNonGlibcLinuxSync()).toBeFalse();
+ } else {
+ expect(version()).toBeNull();
+ expect(family()).toBeNull();
+ expect(isNonGlibcLinuxSync()).toBeFalse();
+ }
+ expect(versionAsync()).resolves.toBe(version());
+ expect(familyAsync()).resolves.toBe(family());
+ expect(isNonGlibcLinux()).resolves.toBe(isNonGlibcLinuxSync());
+});
diff --git a/test/js/first_party/undici/undici.test.ts b/test/js/first_party/undici.test.ts
index ab8430ee9..e57ef63ba 100644
--- a/test/js/first_party/undici/undici.test.ts
+++ b/test/js/first_party/undici.test.ts
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { request } from "undici";
-import { createServer } from "../../../http-test-server";
+import { createServer } from "../../http-test-server";
describe("undici", () => {
let serverCtl: ReturnType<typeof createServer>;
diff --git a/test/js/first_party/utf-8-validate.test.ts b/test/js/first_party/utf-8-validate.test.ts
new file mode 100644
index 000000000..33469c970
--- /dev/null
+++ b/test/js/first_party/utf-8-validate.test.ts
@@ -0,0 +1,9 @@
+import { test, expect } from "bun:test";
+import { isUtf8 } from "node:buffer";
+import isValidUTF8 from "utf-8-validate";
+
+test("utf-8-validate", () => {
+ expect(isValidUTF8).toBe(isUtf8);
+ expect(isValidUTF8(Buffer.from("😀"))).toBeTrue();
+ expect(isValidUTF8(Buffer.from([0xff]))).toBeFalse();
+});
diff --git a/test/package.json b/test/package.json
index 5529d3f20..da3974994 100644
--- a/test/package.json
+++ b/test/package.json
@@ -1,13 +1,13 @@
{
"name": "test",
"devDependencies": {
- "@types/dedent": "0.7.0"
+ "@types/dedent": "0.7.0",
+ "@types/react": "18.0.28",
+ "@types/react-dom": "18.0.11"
},
"dependencies": {
"@prisma/client": "4.15.0",
"@swc/core": "1.3.38",
- "@types/react": "18.0.28",
- "@types/react-dom": "18.0.11",
"bktree-fast": "0.0.7",
"body-parser": "1.20.2",
"dedent": "0.7.0",