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>
This commit is contained in:
Jarred Sumner
2023-09-10 22:15:35 -08:00
committed by GitHub
parent edea4f095a
commit 51d3d43822
19 changed files with 482 additions and 74 deletions

View File

@@ -5,7 +5,7 @@ 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,
@@ -19,11 +19,86 @@ generateObjectModuleSourceCode(JSC::JSGlobalObject *globalObject,
PrivateSymbolMode::Exclude);
object->getPropertyNames(globalObject, properties,
DontEnumPropertiesMode::Exclude);
gcUnprotectNullTolerant(object);
for (auto &entry : properties) {
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);
}
};
}
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,
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) {
if (entry == vm.propertyNames->defaultKeyword) {
continue;
}
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);
}
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