@@ -111,6 +111,13 @@ impl ConstantString {
111
111
}
112
112
}
113
113
114
+ pub fn as_atom ( & self ) -> Cow < Atom > {
115
+ match self {
116
+ Self :: Atom ( s) => Cow :: Borrowed ( s) ,
117
+ Self :: RcStr ( s) => Cow :: Owned ( s. as_str ( ) . into ( ) ) ,
118
+ }
119
+ }
120
+
114
121
pub fn is_empty ( & self ) -> bool {
115
122
self . as_str ( ) . is_empty ( )
116
123
}
@@ -482,6 +489,12 @@ pub enum JsValue {
482
489
/// A tenary operator `test ? cons : alt`
483
490
/// `(total_node_count, test, cons, alt)`
484
491
Tenary ( u32 , Box < JsValue > , Box < JsValue > , Box < JsValue > ) ,
492
+ /// A promise resolving to some value
493
+ /// `(total_node_count, value)`
494
+ Promise ( u32 , Box < JsValue > ) ,
495
+ /// An await call (potentially) unwrapping a promise.
496
+ /// `(total_node_count, value)`
497
+ Awaited ( u32 , Box < JsValue > ) ,
485
498
486
499
/// A for-of loop
487
500
///
@@ -734,6 +747,8 @@ impl Display for JsValue {
734
747
}
735
748
JsValue :: Iterated ( _, iterable) => write ! ( f, "Iterated({})" , iterable) ,
736
749
JsValue :: TypeOf ( _, operand) => write ! ( f, "typeof({})" , operand) ,
750
+ JsValue :: Promise ( _, operand) => write ! ( f, "Promise<{}>" , operand) ,
751
+ JsValue :: Awaited ( _, operand) => write ! ( f, "await({})" , operand) ,
737
752
}
738
753
}
739
754
}
@@ -795,6 +810,7 @@ impl JsValue {
795
810
| JsValue :: Object { .. }
796
811
| JsValue :: Alternatives { .. }
797
812
| JsValue :: Function ( ..)
813
+ | JsValue :: Promise ( ..)
798
814
| JsValue :: Member ( ..) => JsValueMetaKind :: Nested ,
799
815
JsValue :: Concat ( ..)
800
816
| JsValue :: Add ( ..)
@@ -807,6 +823,7 @@ impl JsValue {
807
823
| JsValue :: Tenary ( ..)
808
824
| JsValue :: MemberCall ( ..)
809
825
| JsValue :: Iterated ( ..)
826
+ | JsValue :: Awaited ( ..)
810
827
| JsValue :: TypeOf ( ..) => JsValueMetaKind :: Operation ,
811
828
JsValue :: Variable ( ..)
812
829
| JsValue :: Argument ( ..)
@@ -991,6 +1008,14 @@ impl JsValue {
991
1008
Self :: Member ( 1 + o. total_nodes ( ) + p. total_nodes ( ) , o, p)
992
1009
}
993
1010
1011
+ pub fn promise ( operand : Box < JsValue > ) -> Self {
1012
+ Self :: Promise ( 1 + operand. total_nodes ( ) , operand)
1013
+ }
1014
+
1015
+ pub fn awaited ( operand : Box < JsValue > ) -> Self {
1016
+ Self :: Awaited ( 1 + operand. total_nodes ( ) , operand)
1017
+ }
1018
+
994
1019
pub fn unknown (
995
1020
value : impl Into < Arc < JsValue > > ,
996
1021
side_effects : bool ,
@@ -1063,6 +1088,8 @@ impl JsValue {
1063
1088
| JsValue :: Member ( c, _, _)
1064
1089
| JsValue :: Function ( c, _, _)
1065
1090
| JsValue :: Iterated ( c, ..)
1091
+ | JsValue :: Promise ( c, ..)
1092
+ | JsValue :: Awaited ( c, ..)
1066
1093
| JsValue :: TypeOf ( c, ..) => * c,
1067
1094
}
1068
1095
}
@@ -1104,6 +1131,12 @@ impl JsValue {
1104
1131
JsValue :: Not ( c, r) => {
1105
1132
* c = 1 + r. total_nodes ( ) ;
1106
1133
}
1134
+ JsValue :: Promise ( c, r) => {
1135
+ * c = 1 + r. total_nodes ( ) ;
1136
+ }
1137
+ JsValue :: Awaited ( c, r) => {
1138
+ * c = 1 + r. total_nodes ( ) ;
1139
+ }
1107
1140
1108
1141
JsValue :: Object {
1109
1142
total_nodes : c,
@@ -1252,6 +1285,12 @@ impl JsValue {
1252
1285
JsValue :: TypeOf ( _, operand) => {
1253
1286
operand. make_unknown_without_content ( false , "node limit reached" ) ;
1254
1287
}
1288
+ JsValue :: Awaited ( _, operand) => {
1289
+ operand. make_unknown_without_content ( false , "node limit reached" ) ;
1290
+ }
1291
+ JsValue :: Promise ( _, operand) => {
1292
+ operand. make_unknown_without_content ( false , "node limit reached" ) ;
1293
+ }
1255
1294
JsValue :: Member ( _, o, p) => {
1256
1295
make_max_unknown ( [ & mut * * o, & mut * * p] . into_iter ( ) ) ;
1257
1296
self . update_total_nodes ( ) ;
@@ -1494,6 +1533,18 @@ impl JsValue {
1494
1533
operand. explain_internal_inner( hints, indent_depth, depth, unknown_depth)
1495
1534
)
1496
1535
}
1536
+ JsValue :: Promise ( _, operand) => {
1537
+ format ! (
1538
+ "Promise<{}>" ,
1539
+ operand. explain_internal_inner( hints, indent_depth, depth, unknown_depth)
1540
+ )
1541
+ }
1542
+ JsValue :: Awaited ( _, operand) => {
1543
+ format ! (
1544
+ "await({})" ,
1545
+ operand. explain_internal_inner( hints, indent_depth, depth, unknown_depth)
1546
+ )
1547
+ }
1497
1548
JsValue :: New ( _, callee, list) => {
1498
1549
format ! (
1499
1550
"new {}({})" ,
@@ -2111,6 +2162,8 @@ impl JsValue {
2111
2162
JsValue :: Argument ( _, _) => false ,
2112
2163
JsValue :: Iterated ( _, iterable) => iterable. has_side_effects ( ) ,
2113
2164
JsValue :: TypeOf ( _, operand) => operand. has_side_effects ( ) ,
2165
+ JsValue :: Promise ( _, operand) => operand. has_side_effects ( ) ,
2166
+ JsValue :: Awaited ( _, operand) => operand. has_side_effects ( ) ,
2114
2167
}
2115
2168
}
2116
2169
@@ -2307,7 +2360,8 @@ impl JsValue {
2307
2360
| JsValue :: Module ( ..)
2308
2361
| JsValue :: Function ( ..)
2309
2362
| JsValue :: WellKnownObject ( _)
2310
- | JsValue :: WellKnownFunction ( _) => Some ( false ) ,
2363
+ | JsValue :: WellKnownFunction ( _)
2364
+ | JsValue :: Promise ( _, _) => Some ( false ) ,
2311
2365
2312
2366
// Booleans are not strings
2313
2367
JsValue :: Not ( ..) | JsValue :: Binary ( ..) => Some ( false ) ,
@@ -2346,6 +2400,11 @@ impl JsValue {
2346
2400
_,
2347
2401
) => Some ( true ) ,
2348
2402
2403
+ JsValue :: Awaited ( _, operand) => match & * * operand {
2404
+ JsValue :: Promise ( _, v) => v. is_string ( ) ,
2405
+ v => v. is_string ( ) ,
2406
+ } ,
2407
+
2349
2408
JsValue :: FreeVar ( ..)
2350
2409
| JsValue :: Variable ( _)
2351
2410
| JsValue :: Unknown { .. }
@@ -2650,14 +2709,10 @@ macro_rules! for_each_children_async {
2650
2709
$value. update_total_nodes( ) ;
2651
2710
( $value, m1 || m2)
2652
2711
}
2653
- JsValue :: Iterated ( _, box iterable) => {
2654
- let ( new_iterable, modified) = $visit_fn( take( iterable) , $( $args) ,+) . await ?;
2655
- * iterable = new_iterable;
2656
-
2657
- $value. update_total_nodes( ) ;
2658
- ( $value, modified)
2659
- }
2660
- JsValue :: TypeOf ( _, box operand) => {
2712
+ JsValue :: Iterated ( _, box operand)
2713
+ | JsValue :: TypeOf ( _, box operand)
2714
+ | JsValue :: Promise ( _, box operand)
2715
+ | JsValue :: Awaited ( _, box operand) => {
2661
2716
let ( new_operand, modified) = $visit_fn( take( operand) , $( $args) ,+) . await ?;
2662
2717
* operand = new_operand;
2663
2718
@@ -2962,15 +3017,10 @@ impl JsValue {
2962
3017
modified
2963
3018
}
2964
3019
2965
- JsValue :: Iterated ( _, iterable) => {
2966
- let modified = visitor ( iterable) ;
2967
- if modified {
2968
- self . update_total_nodes ( ) ;
2969
- }
2970
- modified
2971
- }
2972
-
2973
- JsValue :: TypeOf ( _, operand) => {
3020
+ JsValue :: Iterated ( _, operand)
3021
+ | JsValue :: TypeOf ( _, operand)
3022
+ | JsValue :: Promise ( _, operand)
3023
+ | JsValue :: Awaited ( _, operand) => {
2974
3024
let modified = visitor ( operand) ;
2975
3025
if modified {
2976
3026
self . update_total_nodes ( ) ;
@@ -3164,11 +3214,10 @@ impl JsValue {
3164
3214
visitor ( alt) ;
3165
3215
}
3166
3216
3167
- JsValue :: Iterated ( _, iterable) => {
3168
- visitor ( iterable) ;
3169
- }
3170
-
3171
- JsValue :: TypeOf ( _, operand) => {
3217
+ JsValue :: Iterated ( _, operand)
3218
+ | JsValue :: TypeOf ( _, operand)
3219
+ | JsValue :: Promise ( _, operand)
3220
+ | JsValue :: Awaited ( _, operand) => {
3172
3221
visitor ( operand) ;
3173
3222
}
3174
3223
@@ -3566,10 +3615,10 @@ impl JsValue {
3566
3615
cons. similar_hash ( state, depth - 1 ) ;
3567
3616
alt. similar_hash ( state, depth - 1 ) ;
3568
3617
}
3569
- JsValue :: Iterated ( _, iterable ) => {
3570
- iterable . similar_hash ( state , depth - 1 ) ;
3571
- }
3572
- JsValue :: TypeOf ( _, operand) => {
3618
+ JsValue :: Iterated ( _, operand )
3619
+ | JsValue :: TypeOf ( _ , operand )
3620
+ | JsValue :: Promise ( _ , operand )
3621
+ | JsValue :: Awaited ( _, operand) => {
3573
3622
operand. similar_hash ( state, depth - 1 ) ;
3574
3623
}
3575
3624
JsValue :: Module ( ModuleValue {
@@ -3879,10 +3928,12 @@ pub mod test_utils {
3879
3928
box JsValue :: WellKnownFunction ( WellKnownFunctionKind :: Import ) ,
3880
3929
ref args,
3881
3930
) => match & args[ 0 ] {
3882
- JsValue :: Constant ( v) => JsValue :: Module ( ModuleValue {
3883
- module : v. to_string ( ) . into ( ) ,
3884
- annotations : ImportAnnotations :: default ( ) ,
3885
- } ) ,
3931
+ JsValue :: Constant ( ConstantValue :: Str ( v) ) => {
3932
+ JsValue :: promise ( Box :: new ( JsValue :: Module ( ModuleValue {
3933
+ module : v. as_atom ( ) . into_owned ( ) ,
3934
+ annotations : ImportAnnotations :: default ( ) ,
3935
+ } ) ) )
3936
+ }
3886
3937
_ => v. into_unknown ( true , "import() non constant" ) ,
3887
3938
} ,
3888
3939
JsValue :: Call (
0 commit comments