aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-11 00:56:35 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-11 00:56:35 -0700
commit3c87fbfd37f36a409c704ea3f5e2701eb0126c44 (patch)
tree0f89aa932a7a15b12cf239895194990c6d898cff /src/javascript/jsc/bindings
parentfd00950852c3f3bf49d9ce8f6a175753ad0167fd (diff)
downloadbun-3c87fbfd37f36a409c704ea3f5e2701eb0126c44.tar.gz
bun-3c87fbfd37f36a409c704ea3f5e2701eb0126c44.tar.zst
bun-3c87fbfd37f36a409c704ea3f5e2701eb0126c44.zip
[bun.js] Implement `import.meta.require`
This allows synchronous dynamic loading of `.node`, `.json`, and `.toml` files. It is not a CommonJS require, but it can be used that way so long as the content is not JavaScript.
Diffstat (limited to 'src/javascript/jsc/bindings')
-rw-r--r--src/javascript/jsc/bindings/BunBuiltinNames.h2
-rw-r--r--src/javascript/jsc/bindings/GlobalBuiltins.js0
-rw-r--r--src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.cpp100
-rw-r--r--src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.h122
-rw-r--r--src/javascript/jsc/bindings/WebCoreJSBuiltins.h4
-rw-r--r--src/javascript/jsc/bindings/ZigGlobalObject.cpp5
-rw-r--r--src/javascript/jsc/bindings/builtins/js/JSZigGlobalObject.js62
7 files changed, 294 insertions, 1 deletions
diff --git a/src/javascript/jsc/bindings/BunBuiltinNames.h b/src/javascript/jsc/bindings/BunBuiltinNames.h
index f02a92e17..28ab0235d 100644
--- a/src/javascript/jsc/bindings/BunBuiltinNames.h
+++ b/src/javascript/jsc/bindings/BunBuiltinNames.h
@@ -77,6 +77,8 @@ using namespace JSC;
macro(put) \
macro(read) \
macro(relative) \
+ macro(require) \
+ macro(resolveSync) \
macro(removeEventListener) \
macro(resolve) \
macro(resume) \
diff --git a/src/javascript/jsc/bindings/GlobalBuiltins.js b/src/javascript/jsc/bindings/GlobalBuiltins.js
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/javascript/jsc/bindings/GlobalBuiltins.js
diff --git a/src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.cpp b/src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.cpp
new file mode 100644
index 000000000..0bb8fa4aa
--- /dev/null
+++ b/src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2016 Apple Inc. All rights reserved.
+ * Copyright (c) 2022 Codeblog Corp. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// DO NOT EDIT THIS FILE. It is automatically generated from JavaScript files for
+// builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py
+
+#include "config.h"
+#include "JSZigGlobalObjectBuiltins.h"
+
+#include "WebCoreJSClientData.h"
+#include <JavaScriptCore/HeapInlines.h>
+#include <JavaScriptCore/IdentifierInlines.h>
+#include <JavaScriptCore/Intrinsic.h>
+#include <JavaScriptCore/JSCJSValueInlines.h>
+#include <JavaScriptCore/JSCellInlines.h>
+#include <JavaScriptCore/StructureInlines.h>
+#include <JavaScriptCore/VM.h>
+
+namespace WebCore {
+
+const JSC::ConstructAbility s_jsZigGlobalObjectRequireCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
+const JSC::ConstructorKind s_jsZigGlobalObjectRequireCodeConstructorKind = JSC::ConstructorKind::None;
+const int s_jsZigGlobalObjectRequireCodeLength = 1224;
+static const JSC::Intrinsic s_jsZigGlobalObjectRequireCodeIntrinsic = JSC::NoIntrinsic;
+const char* const s_jsZigGlobalObjectRequireCode =
+ "(function (name) {\n" \
+ " \"use strict\";\n" \
+ " if (typeof name !== \"string\") {\n" \
+ " @throwTypeError(\"require() expects a string as its argument\");\n" \
+ " }\n" \
+ "\n" \
+ " const resolved = this.resolveSync(name, this.path);\n" \
+ " var requireCache = (globalThis[Symbol.for(\"_requireCache\")] ||= new @Map);\n" \
+ " var cached = requireCache.@get(resolved);\n" \
+ " if (cached) {\n" \
+ " if (resolved.endsWith(\".node\")) {\n" \
+ " return cached.exports;\n" \
+ " }\n" \
+ "\n" \
+ " return cached;\n" \
+ " }\n" \
+ "\n" \
+ " //\n" \
+ " if (resolved.endsWith(\".json\")) {\n" \
+ " var fs = (globalThis[Symbol.for(\"_fs\")] ||= Bun.fs());\n" \
+ " var exports = JSON.parse(fs.readFileSync(resolved, \"utf8\"));\n" \
+ " requireCache.@set(resolved, exports);\n" \
+ " return cached;\n" \
+ " } else if (resolved.endsWith(\".node\")) {\n" \
+ " var module = { exports: {} };\n" \
+ " globalThis.process.dlopen(module, resolved);\n" \
+ " requireCache.@set(resolved, module);\n" \
+ " return module.exports;\n" \
+ " } else if (resolved.endsWith(\".toml\")) {\n" \
+ " var fs = (globalThis[Symbol.for(\"_fs\")] ||= Bun.fs());\n" \
+ " var exports = Bun.TOML.parse(fs.readFileSync(resolved, \"utf8\"));\n" \
+ " requireCache.@set(resolved, exports);\n" \
+ " return exports;\n" \
+ " }\n" \
+ "\n" \
+ " @throwTypeError(`Dynamic require isn't supported for file type: ${resolved.subsring(resolved.lastIndexOf(\".\") + 1) || resolved}`);\n" \
+ "})\n" \
+;
+
+
+#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
+JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \
+{\
+ JSVMClientData* clientData = static_cast<JSVMClientData*>(vm.clientData); \
+ return clientData->builtinFunctions().jsZigGlobalObjectBuiltins().codeName##Executable()->link(vm, nullptr, clientData->builtinFunctions().jsZigGlobalObjectBuiltins().codeName##Source(), std::nullopt, s_##codeName##Intrinsic); \
+}
+WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
+#undef DEFINE_BUILTIN_GENERATOR
+
+
+} // namespace WebCore
diff --git a/src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.h b/src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.h
new file mode 100644
index 000000000..251e599d2
--- /dev/null
+++ b/src/javascript/jsc/bindings/JSZigGlobalObjectBuiltins.h
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2016 Apple Inc. All rights reserved.
+ * Copyright (c) 2022 Codeblog Corp. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// DO NOT EDIT THIS FILE. It is automatically generated from JavaScript files for
+// builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py
+
+#pragma once
+
+#include <JavaScriptCore/BuiltinUtils.h>
+#include <JavaScriptCore/Identifier.h>
+#include <JavaScriptCore/JSFunction.h>
+#include <JavaScriptCore/UnlinkedFunctionExecutable.h>
+
+namespace JSC {
+class FunctionExecutable;
+}
+
+namespace WebCore {
+
+/* JSZigGlobalObject */
+extern const char* const s_jsZigGlobalObjectRequireCode;
+extern const int s_jsZigGlobalObjectRequireCodeLength;
+extern const JSC::ConstructAbility s_jsZigGlobalObjectRequireCodeConstructAbility;
+extern const JSC::ConstructorKind s_jsZigGlobalObjectRequireCodeConstructorKind;
+
+#define WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_DATA(macro) \
+ macro(require, jsZigGlobalObjectRequire, 1) \
+
+#define WEBCORE_BUILTIN_JSZIGGLOBALOBJECT_REQUIRE 1
+
+#define WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(macro) \
+ macro(jsZigGlobalObjectRequireCode, require, ASCIILiteral(), s_jsZigGlobalObjectRequireCodeLength) \
+
+#define WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_FUNCTION_NAME(macro) \
+ macro(require) \
+
+#define DECLARE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
+ JSC::FunctionExecutable* codeName##Generator(JSC::VM&);
+
+WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(DECLARE_BUILTIN_GENERATOR)
+#undef DECLARE_BUILTIN_GENERATOR
+
+class JSZigGlobalObjectBuiltinsWrapper : private JSC::WeakHandleOwner {
+public:
+ explicit JSZigGlobalObjectBuiltinsWrapper(JSC::VM& vm)
+ : m_vm(vm)
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_FUNCTION_NAME(INITIALIZE_BUILTIN_NAMES)
+#define INITIALIZE_BUILTIN_SOURCE_MEMBERS(name, functionName, overriddenName, length) , m_##name##Source(JSC::makeSource(StringImpl::createWithoutCopying(s_##name, length), { }))
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(INITIALIZE_BUILTIN_SOURCE_MEMBERS)
+#undef INITIALIZE_BUILTIN_SOURCE_MEMBERS
+ {
+ }
+
+#define EXPOSE_BUILTIN_EXECUTABLES(name, functionName, overriddenName, length) \
+ JSC::UnlinkedFunctionExecutable* name##Executable(); \
+ const JSC::SourceCode& name##Source() const { return m_##name##Source; }
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(EXPOSE_BUILTIN_EXECUTABLES)
+#undef EXPOSE_BUILTIN_EXECUTABLES
+
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
+
+ void exportNames();
+
+private:
+ JSC::VM& m_vm;
+
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_NAMES)
+
+#define DECLARE_BUILTIN_SOURCE_MEMBERS(name, functionName, overriddenName, length) \
+ JSC::SourceCode m_##name##Source;\
+ JSC::Weak<JSC::UnlinkedFunctionExecutable> m_##name##Executable;
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(DECLARE_BUILTIN_SOURCE_MEMBERS)
+#undef DECLARE_BUILTIN_SOURCE_MEMBERS
+
+};
+
+#define DEFINE_BUILTIN_EXECUTABLES(name, functionName, overriddenName, length) \
+inline JSC::UnlinkedFunctionExecutable* JSZigGlobalObjectBuiltinsWrapper::name##Executable() \
+{\
+ if (!m_##name##Executable) {\
+ JSC::Identifier executableName = functionName##PublicName();\
+ if (overriddenName)\
+ executableName = JSC::Identifier::fromString(m_vm, overriddenName);\
+ m_##name##Executable = JSC::Weak<JSC::UnlinkedFunctionExecutable>(JSC::createBuiltinExecutable(m_vm, m_##name##Source, executableName, s_##name##ConstructorKind, s_##name##ConstructAbility), this, &m_##name##Executable);\
+ }\
+ return m_##name##Executable.get();\
+}
+WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_CODE(DEFINE_BUILTIN_EXECUTABLES)
+#undef DEFINE_BUILTIN_EXECUTABLES
+
+inline void JSZigGlobalObjectBuiltinsWrapper::exportNames()
+{
+#define EXPORT_FUNCTION_NAME(name) m_vm.propertyNames->appendExternalName(name##PublicName(), name##PrivateName());
+ WEBCORE_FOREACH_JSZIGGLOBALOBJECT_BUILTIN_FUNCTION_NAME(EXPORT_FUNCTION_NAME)
+#undef EXPORT_FUNCTION_NAME
+}
+
+} // namespace WebCore
diff --git a/src/javascript/jsc/bindings/WebCoreJSBuiltins.h b/src/javascript/jsc/bindings/WebCoreJSBuiltins.h
index 515775d32..2c136b3ac 100644
--- a/src/javascript/jsc/bindings/WebCoreJSBuiltins.h
+++ b/src/javascript/jsc/bindings/WebCoreJSBuiltins.h
@@ -32,6 +32,7 @@
#include "JSBufferConstructorBuiltins.h"
#include "JSBufferPrototypeBuiltins.h"
+#include "JSZigGlobalObjectBuiltins.h"
#include <JavaScriptCore/VM.h>
namespace WebCore {
@@ -42,16 +43,19 @@ public:
: m_vm(vm)
, m_jsBufferConstructorBuiltins(m_vm)
, m_jsBufferPrototypeBuiltins(m_vm)
+ , m_jsZigGlobalObjectBuiltins(m_vm)
{
}
JSBufferConstructorBuiltinsWrapper& jsBufferConstructorBuiltins() { return m_jsBufferConstructorBuiltins; }
JSBufferPrototypeBuiltinsWrapper& jsBufferPrototypeBuiltins() { return m_jsBufferPrototypeBuiltins; }
+ JSZigGlobalObjectBuiltinsWrapper& jsZigGlobalObjectBuiltins() { return m_jsZigGlobalObjectBuiltins; }
private:
JSC::VM& m_vm;
JSBufferConstructorBuiltinsWrapper m_jsBufferConstructorBuiltins;
JSBufferPrototypeBuiltinsWrapper m_jsBufferPrototypeBuiltins;
+ JSZigGlobalObjectBuiltinsWrapper m_jsZigGlobalObjectBuiltins;
};
} // namespace WebCore
diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp
index f42b2a5a2..a3c84bcfc 100644
--- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp
+++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp
@@ -1173,7 +1173,7 @@ JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje
auto index = view.reverseFind('/', view.length());
if (index != WTF::notFound) {
metaProperties->putDirect(vm, clientData->builtinNames().dirPublicName(),
- JSC::jsSubstring(globalObject, keyString, 0, index));
+ JSC::jsSubstring(globalObject, keyString, 0, index + 1));
metaProperties->putDirect(
vm, clientData->builtinNames().filePublicName(),
JSC::jsSubstring(globalObject, keyString, index + 1, keyString->length() - index - 1));
@@ -1191,6 +1191,9 @@ JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje
WTF::String("resolveSync"_s), functionImportMeta__resolveSync),
JSC::PropertyAttribute::Function | 0);
+ metaProperties->putDirectBuiltinFunction(vm, globalObject, clientData->builtinNames().requirePublicName(),
+ jsZigGlobalObjectRequireCodeGenerator(vm),
+ JSC::PropertyAttribute::Builtin | 0);
}
metaProperties->putDirect(vm, clientData->builtinNames().pathPublicName(), key);
diff --git a/src/javascript/jsc/bindings/builtins/js/JSZigGlobalObject.js b/src/javascript/jsc/bindings/builtins/js/JSZigGlobalObject.js
new file mode 100644
index 000000000..ee6126035
--- /dev/null
+++ b/src/javascript/jsc/bindings/builtins/js/JSZigGlobalObject.js
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2022 Codeblog Corp. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+function require(name) {
+ "use strict";
+ if (typeof name !== "string") {
+ @throwTypeError("require() expects a string as its argument");
+ }
+
+ const resolved = this.resolveSync(name, this.path);
+ var requireCache = (globalThis[Symbol.for("_requireCache")] ||= new @Map);
+ var cached = requireCache.@get(resolved);
+ if (cached) {
+ if (resolved.endsWith(".node")) {
+ return cached.exports;
+ }
+
+ return cached;
+ }
+
+ // TODO: remove this hardcoding
+ if (resolved.endsWith(".json")) {
+ var fs = (globalThis[Symbol.for("_fs")] ||= Bun.fs());
+ var exports = JSON.parse(fs.readFileSync(resolved, "utf8"));
+ requireCache.@set(resolved, exports);
+ return cached;
+ } else if (resolved.endsWith(".node")) {
+ var module = { exports: {} };
+ globalThis.process.dlopen(module, resolved);
+ requireCache.@set(resolved, module);
+ return module.exports;
+ } else if (resolved.endsWith(".toml")) {
+ var fs = (globalThis[Symbol.for("_fs")] ||= Bun.fs());
+ var exports = Bun.TOML.parse(fs.readFileSync(resolved, "utf8"));
+ requireCache.@set(resolved, exports);
+ return exports;
+ }
+
+ @throwTypeError(`Dynamic require isn't supported for file type: ${resolved.subsring(resolved.lastIndexOf(".") + 1) || resolved}`);
+}