8
8
9
9
#include " js/CharacterEncoding.h"
10
10
#include " js/Object.h" // JS::GetClass
11
- #include " js/PropertyAndElement.h" // JS_DefineProperty, JS_DefinePropertyById, JS_Enumerate, JS_GetProperty, JS_GetPropertyById, JS_SetProperty, JS_SetPropertyById, JS::IdVector
11
+ #include " js/PropertyAndElement.h" // JS_DefineProperty, JS_DefinePropertyById, JS_Enumerate, JS_GetProperty, JS_GetPropertyById, JS_SetProperty, JS_SetPropertyById
12
12
#include " js/PropertyDescriptor.h" // JS::PropertyDescriptor, JS_GetOwnPropertyDescriptorById
13
13
#include " js/SavedFrameAPI.h"
14
- #include " js/Value.h" // JS::Value, JS::StringValue
15
14
#include " jsfriendapi.h"
16
15
#include " WrapperFactory.h"
17
16
@@ -627,10 +626,7 @@ static bool ExtractArgs(JSContext* aCx, JS::CallArgs& aArgs,
627
626
return true ;
628
627
}
629
628
630
- enum class ModuleType { JSM, ESM };
631
-
632
- static bool ModuleGetterImpl (JSContext* aCx, unsigned aArgc, JS::Value* aVp,
633
- ModuleType aType) {
629
+ static bool ModuleGetter (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
634
630
JS::CallArgs args = JS::CallArgsFromVp (aArgc, aVp);
635
631
636
632
JS::Rooted<JSObject*> callee (aCx);
@@ -651,38 +647,17 @@ static bool ModuleGetterImpl(JSContext* aCx, unsigned aArgc, JS::Value* aVp,
651
647
RefPtr<mozJSComponentLoader> moduleloader = mozJSComponentLoader::Get ();
652
648
MOZ_ASSERT (moduleloader);
653
649
654
- JS::Rooted<JS::Value> value (aCx);
655
- if (aType == ModuleType::JSM) {
656
- JS::Rooted<JSObject*> moduleGlobal (aCx);
657
- JS::Rooted<JSObject*> moduleExports (aCx);
658
- nsresult rv = moduleloader->Import (aCx, uri, &moduleGlobal, &moduleExports);
659
- if (NS_FAILED(rv)) {
660
- Throw (aCx, rv);
661
- return false ;
662
- }
663
-
664
- // JSM's exports is from the same realm.
665
- if (!JS_GetPropertyById (aCx, moduleExports, id, &value)) {
666
- return false ;
667
- }
668
- } else {
669
- JS::Rooted<JSObject*> moduleNamespace (aCx);
670
- nsresult rv = moduleloader->ImportESModule (aCx, uri, &moduleNamespace);
671
- if (NS_FAILED(rv)) {
672
- Throw (aCx, rv);
673
- return false ;
674
- }
650
+ JS::Rooted<JSObject*> moduleGlobal (aCx);
651
+ JS::Rooted<JSObject*> moduleExports (aCx);
652
+ nsresult rv = moduleloader->Import (aCx, uri, &moduleGlobal, &moduleExports);
653
+ if (NS_FAILED(rv)) {
654
+ Throw (aCx, rv);
655
+ return false ;
656
+ }
675
657
676
- // ESM's namespace is from the module's realm.
677
- {
678
- JSAutoRealm ar (aCx, moduleNamespace);
679
- if (!JS_GetPropertyById (aCx, moduleNamespace, id, &value)) {
680
- return false ;
681
- }
682
- }
683
- if (!JS_WrapValue (aCx, &value)) {
684
- return false ;
685
- }
658
+ JS::Rooted<JS::Value> value (aCx);
659
+ if (!JS_GetPropertyById (aCx, moduleExports, id, &value)) {
660
+ return false ;
686
661
}
687
662
688
663
if (!JS_DefinePropertyById (aCx, thisObj, id, value, JSPROP_ENUMERATE)) {
@@ -693,15 +668,7 @@ static bool ModuleGetterImpl(JSContext* aCx, unsigned aArgc, JS::Value* aVp,
693
668
return true ;
694
669
}
695
670
696
- static bool JSModuleGetter (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
697
- return ModuleGetterImpl (aCx, aArgc, aVp, ModuleType::JSM);
698
- }
699
-
700
- static bool ESModuleGetter (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
701
- return ModuleGetterImpl (aCx, aArgc, aVp, ModuleType::ESM);
702
- }
703
-
704
- static bool ModuleSetterImpl (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
671
+ static bool ModuleSetter (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
705
672
JS::CallArgs args = JS::CallArgsFromVp (aArgc, aVp);
706
673
707
674
JS::Rooted<JSObject*> callee (aCx);
@@ -714,17 +681,8 @@ static bool ModuleSetterImpl(JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
714
681
return JS_DefinePropertyById (aCx, thisObj, id, args.get (0 ), JSPROP_ENUMERATE);
715
682
}
716
683
717
- static bool JSModuleSetter (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
718
- return ModuleSetterImpl (aCx, aArgc, aVp);
719
- }
720
-
721
- static bool ESModuleSetter (JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
722
- return ModuleSetterImpl (aCx, aArgc, aVp);
723
- }
724
-
725
- static bool DefineJSModuleGetter (JSContext* aCx, JS::Handle<JSObject*> aTarget,
726
- const nsAString& aId,
727
- const nsAString& aResourceURI) {
684
+ static bool DefineGetter (JSContext* aCx, JS::Handle<JSObject*> aTarget,
685
+ const nsAString& aId, const nsAString& aResourceURI) {
728
686
JS::Rooted<JS::Value> uri (aCx);
729
687
JS::Rooted<JS::Value> idValue (aCx);
730
688
JS::Rooted<jsid> id (aCx);
@@ -737,11 +695,11 @@ static bool DefineJSModuleGetter(JSContext* aCx, JS::Handle<JSObject*> aTarget,
737
695
738
696
JS::Rooted<JSObject*> getter (
739
697
aCx, JS_GetFunctionObject (
740
- js::NewFunctionByIdWithReserved (aCx, JSModuleGetter , 0 , 0 , id)));
698
+ js::NewFunctionByIdWithReserved (aCx, ModuleGetter , 0 , 0 , id)));
741
699
742
700
JS::Rooted<JSObject*> setter (
743
701
aCx, JS_GetFunctionObject (
744
- js::NewFunctionByIdWithReserved (aCx, JSModuleSetter , 0 , 0 , id)));
702
+ js::NewFunctionByIdWithReserved (aCx, ModuleSetter , 0 , 0 , id)));
745
703
746
704
if (!getter || !setter) {
747
705
JS_ReportOutOfMemory (aCx);
@@ -756,33 +714,6 @@ static bool DefineJSModuleGetter(JSContext* aCx, JS::Handle<JSObject*> aTarget,
756
714
return JS_DefinePropertyById (aCx, aTarget, id, getter, setter,
757
715
JSPROP_ENUMERATE);
758
716
}
759
-
760
- static bool DefineESModuleGetter (JSContext* aCx, JS::Handle<JSObject*> aTarget,
761
- JS::Handle<JS::PropertyKey> aId,
762
- JS::Handle<JS::Value> aResourceURI) {
763
- JS::Rooted<JS::Value> idVal (aCx, JS::StringValue (aId.toString ()));
764
-
765
- JS::Rooted<JSObject*> getter (
766
- aCx, JS_GetFunctionObject (js::NewFunctionByIdWithReserved (
767
- aCx, ESModuleGetter, 0 , 0 , aId)));
768
-
769
- JS::Rooted<JSObject*> setter (
770
- aCx, JS_GetFunctionObject (js::NewFunctionByIdWithReserved (
771
- aCx, ESModuleSetter, 0 , 0 , aId)));
772
-
773
- if (!getter || !setter) {
774
- JS_ReportOutOfMemory (aCx);
775
- return false ;
776
- }
777
-
778
- js::SetFunctionNativeReserved (getter, SLOT_ID, idVal);
779
- js::SetFunctionNativeReserved (setter, SLOT_ID, idVal);
780
-
781
- js::SetFunctionNativeReserved (getter, SLOT_URI, aResourceURI);
782
-
783
- return JS_DefinePropertyById (aCx, aTarget, aId, getter, setter,
784
- JSPROP_ENUMERATE);
785
- }
786
717
} // namespace module_getter
787
718
788
719
/* static */
@@ -791,48 +722,11 @@ void ChromeUtils::DefineModuleGetter(const GlobalObject& global,
791
722
const nsAString& id,
792
723
const nsAString& resourceURI,
793
724
ErrorResult& aRv) {
794
- if (!module_getter::DefineJSModuleGetter (global.Context (), target, id,
795
- resourceURI)) {
725
+ if (!module_getter::DefineGetter (global.Context (), target, id, resourceURI)) {
796
726
aRv.NoteJSContextException (global.Context ());
797
727
}
798
728
}
799
729
800
- /* static */
801
- void ChromeUtils::DefineESModuleGetters (const GlobalObject& global,
802
- JS::Handle<JSObject*> target,
803
- JS::Handle<JSObject*> modules,
804
- ErrorResult& aRv) {
805
- auto cx = global.Context ();
806
-
807
- JS::Rooted<JS::IdVector> props (cx, JS::IdVector (cx));
808
- if (!JS_Enumerate (cx, modules, &props)) {
809
- aRv.NoteJSContextException (cx);
810
- return ;
811
- }
812
-
813
- JS::Rooted<JS::PropertyKey> prop (cx);
814
- JS::Rooted<JS::Value> resourceURIVal (cx);
815
- for (JS::PropertyKey tmp : props) {
816
- prop = tmp;
817
-
818
- if (!prop.isString ()) {
819
- aRv.Throw (NS_ERROR_FAILURE);
820
- return ;
821
- }
822
-
823
- if (!JS_GetPropertyById (cx, modules, prop, &resourceURIVal)) {
824
- aRv.NoteJSContextException (cx);
825
- return ;
826
- }
827
-
828
- if (!module_getter::DefineESModuleGetter (cx, target, prop,
829
- resourceURIVal)) {
830
- aRv.NoteJSContextException (cx);
831
- return ;
832
- }
833
- }
834
- }
835
-
836
730
/* static */
837
731
void ChromeUtils::OriginAttributesToSuffix (
838
732
dom::GlobalObject& aGlobal, const dom::OriginAttributesDictionary& aAttrs,
0 commit comments