aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/modules/ObjectModule.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-09-10 22:15:35 -0800
committerGravatar GitHub <noreply@github.com> 2023-09-10 23:15:35 -0700
commit51d3d4382281f789f8175079ed426a63529eb3e7 (patch)
tree14f6fe77a1e3b300488e9343d8e9d54f64bde376 /src/bun.js/modules/ObjectModule.cpp
parentedea4f095a3bebf54f986c0fa038482316f4cde8 (diff)
downloadbun-51d3d4382281f789f8175079ed426a63529eb3e7.tar.gz
bun-51d3d4382281f789f8175079ed426a63529eb3e7.tar.zst
bun-51d3d4382281f789f8175079ed426a63529eb3e7.zip
Support named imports for json & toml files at runtime (#4783)
* Support named exports in json imports * Support named imports for `*.json` files * Remove stale comments * Don't export arrays as non-default * Add test for default exports * Don't break webpack --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/modules/ObjectModule.cpp')
-rw-r--r--src/bun.js/modules/ObjectModule.cpp77
1 files changed, 76 insertions, 1 deletions
diff --git a/src/bun.js/modules/ObjectModule.cpp b/src/bun.js/modules/ObjectModule.cpp
index 4272bec4e..ebb4af32e 100644
--- a/src/bun.js/modules/ObjectModule.cpp
+++ b/src/bun.js/modules/ObjectModule.cpp
@@ -5,7 +5,41 @@ JSC::SyntheticSourceProvider::SyntheticSourceGenerator
generateObjectModuleSourceCode(JSC::JSGlobalObject *globalObject,
JSC::JSObject *object) {
JSC::VM &vm = globalObject->vm();
+ gcProtectNullTolerant(object);
+ return [object](JSC::JSGlobalObject *lexicalGlobalObject,
+ JSC::Identifier moduleKey,
+ Vector<JSC::Identifier, 4> &exportNames,
+ JSC::MarkedArgumentBuffer &exportValues) -> void {
+ JSC::VM &vm = lexicalGlobalObject->vm();
+ GlobalObject *globalObject =
+ reinterpret_cast<GlobalObject *>(lexicalGlobalObject);
+ JSC::EnsureStillAliveScope stillAlive(object);
+
+ PropertyNameArray properties(vm, PropertyNameMode::Strings,
+ PrivateSymbolMode::Exclude);
+ object->getPropertyNames(globalObject, properties,
+ DontEnumPropertiesMode::Exclude);
+ gcUnprotectNullTolerant(object);
+ for (auto &entry : properties) {
+ exportNames.append(entry);
+
+ auto scope = DECLARE_CATCH_SCOPE(vm);
+ JSValue value = object->get(globalObject, entry);
+ if (scope.exception()) {
+ scope.clearException();
+ value = jsUndefined();
+ }
+ exportValues.append(value);
+ }
+ };
+}
+
+JSC::SyntheticSourceProvider::SyntheticSourceGenerator
+generateObjectModuleSourceCodeForJSON(JSC::JSGlobalObject *globalObject,
+ JSC::JSObject *object) {
+ JSC::VM &vm = globalObject->vm();
+ gcProtectNullTolerant(object);
return [object](JSC::JSGlobalObject *lexicalGlobalObject,
JSC::Identifier moduleKey,
Vector<JSC::Identifier, 4> &exportNames,
@@ -19,11 +53,52 @@ generateObjectModuleSourceCode(JSC::JSGlobalObject *globalObject,
PrivateSymbolMode::Exclude);
object->getPropertyNames(globalObject, properties,
DontEnumPropertiesMode::Exclude);
+ gcUnprotectNullTolerant(object);
for (auto &entry : properties) {
+ if (entry == vm.propertyNames->defaultKeyword) {
+ continue;
+ }
+
exportNames.append(entry);
- exportValues.append(object->get(globalObject, entry));
+
+ auto scope = DECLARE_CATCH_SCOPE(vm);
+ JSValue value = object->get(globalObject, entry);
+ if (scope.exception()) {
+ scope.clearException();
+ value = jsUndefined();
+ }
+ exportValues.append(value);
}
+
+ exportNames.append(vm.propertyNames->defaultKeyword);
+ exportValues.append(object);
+ };
+}
+
+JSC::SyntheticSourceProvider::SyntheticSourceGenerator
+generateJSValueModuleSourceCode(JSC::JSGlobalObject *globalObject,
+ JSC::JSValue value) {
+
+ if (value.isObject() && !JSC::isJSArray(value)) {
+ return generateObjectModuleSourceCodeForJSON(globalObject,
+ value.getObject());
+ }
+
+ if (value.isCell())
+ gcProtectNullTolerant(value.asCell());
+ return [value](JSC::JSGlobalObject *lexicalGlobalObject,
+ JSC::Identifier moduleKey,
+ Vector<JSC::Identifier, 4> &exportNames,
+ JSC::MarkedArgumentBuffer &exportValues) -> void {
+ JSC::VM &vm = lexicalGlobalObject->vm();
+ GlobalObject *globalObject =
+ reinterpret_cast<GlobalObject *>(lexicalGlobalObject);
+ exportNames.append(vm.propertyNames->defaultKeyword);
+ exportValues.append(value);
+
+ if (value.isCell())
+ gcUnprotectNullTolerant(value.asCell());
};
}
} // namespace Zig \ No newline at end of file