aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-02-14 21:56:49 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-02-14 21:56:49 -0800
commit4dc6bf1b099a2a7425a5ff313cff9a9c828f6cd3 (patch)
treeca08eb558eaf421d70ea8cb9968e50d780b7cef0
parent7597e4ad2a813f53a8924d3820b339d487469bdd (diff)
downloadbun-4dc6bf1b099a2a7425a5ff313cff9a9c828f6cd3.tar.gz
bun-4dc6bf1b099a2a7425a5ff313cff9a9c828f6cd3.tar.zst
bun-4dc6bf1b099a2a7425a5ff313cff9a9c828f6cd3.zip
Add workaround for `tls` and `worker_threads`
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp14
-rw-r--r--src/bun.js/module_loader.zig42
-rw-r--r--test/bun.js/disabled-module.test.js9
3 files changed, 64 insertions, 1 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index bcb462d42..0f4fe92e2 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -1137,6 +1137,15 @@ enum ReadableStreamTag : int32_t {
Bytes = 4,
};
+JSC_DEFINE_HOST_FUNCTION(functionCallNotImplemented,
+ (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
+{
+ auto& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ throwTypeError(globalObject, scope, "Not implemented yet in Bun :("_s);
+ return JSC::JSValue::encode(JSC::JSValue {});
+}
+
// we're trying out a new way to do this lazy loading
static JSC_DEFINE_HOST_FUNCTION(functionLazyLoad,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
@@ -1156,6 +1165,7 @@ JSC:
static NeverDestroyed<const String> bunStreamString(MAKE_STATIC_STRING_IMPL("bun:stream"));
static NeverDestroyed<const String> noopString(MAKE_STATIC_STRING_IMPL("noop"));
static NeverDestroyed<const String> createImportMeta(MAKE_STATIC_STRING_IMPL("createImportMeta"));
+ static NeverDestroyed<const String> masqueradesAsUndefined(MAKE_STATIC_STRING_IMPL("masqueradesAsUndefined"));
JSC::JSValue moduleName = callFrame->argument(0);
if (moduleName.isNumber()) {
@@ -1235,6 +1245,10 @@ JSC:
return JSValue::encode(obj);
}
+ if (string == masqueradesAsUndefined) {
+ return JSValue::encode(InternalFunction::createFunctionThatMasqueradesAsUndefined(vm, globalObject, 0, String(), functionCallNotImplemented));
+ }
+
if (UNLIKELY(string == noopString)) {
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 0bd379e63..8b557377b 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -2021,6 +2021,44 @@ pub const ModuleLoader = struct {
.hash = 0,
};
}
+ } else if (DisabledModule.has(specifier)) {
+ return ResolvedSource{
+ .allocator = null,
+ .source_code = ZigString.init(
+ \\const symbol = Symbol.for("CommonJS");
+ \\const lazy = globalThis[Symbol.for("Bun.lazy")];
+ \\// creating this triggers a watchpoint in JSC, so lets delay doing that.
+ \\var masqueradesAsUndefined, hasMasqueradedAsUndefined;
+ \\const target = {[symbol]: 0};
+ \\const proxy = new Proxy(target, {
+ \\ get: function (target, prop) {
+ \\ if (prop in target) {
+ \\ return target[prop];
+ \\ }
+ \\
+ \\ if (!hasMasqueradedAsUndefined) {
+ \\ masqueradesAsUndefined = lazy("masqueradesAsUndefined");
+ \\ hasMasqueradedAsUndefined = true;
+ \\ }
+ \\
+ \\ return masqueradesAsUndefined;
+ \\ },
+ \\ set: function (target, prop, value) {
+ \\ target[prop] = value;
+ \\ return true;
+ \\ },
+ \\ has: function (target, prop) {
+ \\ return prop in target;
+ \\ },
+ \\});
+ \\
+ \\export default proxy;
+ \\
+ ),
+ .specifier = ZigString.init(specifier),
+ .source_url = ZigString.init(specifier),
+ .hash = 0,
+ };
}
return null;
@@ -2209,11 +2247,11 @@ pub const HardcodedModule = enum {
.{
.{ "assert", "node:assert" },
.{ "buffer", "node:buffer" },
- .{ "bun", "bun" },
.{ "bun:ffi", "bun:ffi" },
.{ "bun:jsc", "bun:jsc" },
.{ "bun:sqlite", "bun:sqlite" },
.{ "bun:wrap", "bun:wrap" },
+ .{ "bun", "bun" },
.{ "child_process", "node:child_process" },
.{ "crypto", "node:crypto" },
.{ "depd", "depd" },
@@ -2262,6 +2300,7 @@ pub const HardcodedModule = enum {
.{ "node:util", "node:util" },
.{ "node:util/types", "node:util/types" },
.{ "node:wasi", "node:wasi" },
+ .{ "node:worker_threads", "node:worker_threads" },
.{ "node:zlib", "node:zlib" },
.{ "os", "node:os" },
.{ "path", "node:path" },
@@ -2287,6 +2326,7 @@ pub const HardcodedModule = enum {
.{ "util", "node:util" },
.{ "util/types", "node:util/types" },
.{ "wasi", "node:wasi" },
+ .{ "worker_threads", "node:worker_threads" },
.{ "ws", "ws" },
.{ "ws/lib/websocket", "ws" },
.{ "zlib", "node:zlib" },
diff --git a/test/bun.js/disabled-module.test.js b/test/bun.js/disabled-module.test.js
new file mode 100644
index 000000000..5eb0db898
--- /dev/null
+++ b/test/bun.js/disabled-module.test.js
@@ -0,0 +1,9 @@
+import { describe, it, expect, beforeEach, afterEach, test } from "bun:test";
+
+test("not implemented yet module masquerades as undefined and throws an error", () => {
+ const worker_threads = import.meta.require("worker_threads");
+
+ expect(typeof worker_threads).toBe("object");
+ expect(typeof worker_threads.foo).toBe("undefined");
+ expect(() => worker_threads.foo()).toThrow("Not implemented yet in Bun :(");
+});