@@ -185,6 +185,7 @@ pub enum ConstantValue {
185185 Null ,
186186 BigInt ( Box < BigInt > ) ,
187187 Regex ( Box < ( Atom , Atom ) > ) ,
188+ Evaluate ( RcStr ) ,
188189}
189190
190191impl ConstantValue {
@@ -203,25 +204,27 @@ impl ConstantValue {
203204 }
204205 }
205206
206- pub fn is_truthy ( & self ) -> bool {
207+ pub fn is_truthy ( & self ) -> Option < bool > {
207208 match self {
208- Self :: Undefined | Self :: False | Self :: Null => false ,
209- Self :: True | Self :: Regex ( ..) => true ,
210- Self :: Str ( s) => !s. is_empty ( ) ,
211- Self :: Num ( ConstantNumber ( n) ) => * n != 0.0 ,
212- Self :: BigInt ( n) => !n. is_zero ( ) ,
209+ Self :: Undefined | Self :: False | Self :: Null => Some ( false ) ,
210+ Self :: True | Self :: Regex ( ..) => Some ( true ) ,
211+ Self :: Str ( s) => Some ( !s. is_empty ( ) ) ,
212+ Self :: Num ( ConstantNumber ( n) ) => Some ( * n != 0.0 ) ,
213+ Self :: BigInt ( n) => Some ( !n. is_zero ( ) ) ,
214+ Self :: Evaluate ( _) => None ,
213215 }
214216 }
215217
216- pub fn is_nullish ( & self ) -> bool {
218+ pub fn is_nullish ( & self ) -> Option < bool > {
217219 match self {
218- Self :: Undefined | Self :: Null => true ,
220+ Self :: Undefined | Self :: Null => Some ( true ) ,
219221 Self :: Str ( ..)
220222 | Self :: Num ( ..)
221223 | Self :: True
222224 | Self :: False
223225 | Self :: BigInt ( ..)
224- | Self :: Regex ( ..) => false ,
226+ | Self :: Regex ( ..) => Some ( false ) ,
227+ Self :: Evaluate ( _) => None ,
225228 }
226229 }
227230
@@ -233,7 +236,16 @@ impl ConstantValue {
233236 }
234237
235238 pub fn is_value_type ( & self ) -> bool {
236- !matches ! ( self , Self :: Regex ( ..) )
239+ match self {
240+ ConstantValue :: Undefined
241+ | ConstantValue :: Null
242+ | ConstantValue :: Str ( _)
243+ | ConstantValue :: Num ( _)
244+ | ConstantValue :: True
245+ | ConstantValue :: False
246+ | ConstantValue :: BigInt ( _) => true ,
247+ ConstantValue :: Regex ( _) | ConstantValue :: Evaluate ( _) => false ,
248+ }
237249 }
238250}
239251
@@ -283,6 +295,7 @@ impl Display for ConstantValue {
283295 ConstantValue :: Num ( ConstantNumber ( n) ) => write ! ( f, "{n}" ) ,
284296 ConstantValue :: BigInt ( n) => write ! ( f, "{n}" ) ,
285297 ConstantValue :: Regex ( regex) => write ! ( f, "/{}/{}" , regex. 0 , regex. 1 ) ,
298+ ConstantValue :: Evaluate ( eval) => write ! ( f, "{eval}" ) ,
286299 }
287300 }
288301}
@@ -584,12 +597,37 @@ impl From<ConstantValue> for JsValue {
584597impl From < & CompileTimeDefineValue > for JsValue {
585598 fn from ( v : & CompileTimeDefineValue ) -> Self {
586599 match v {
587- CompileTimeDefineValue :: String ( s ) => JsValue :: Constant ( s . as_str ( ) . into ( ) ) ,
600+ CompileTimeDefineValue :: Null => JsValue :: Constant ( ConstantValue :: Null ) ,
588601 CompileTimeDefineValue :: Bool ( b) => JsValue :: Constant ( ( * b) . into ( ) ) ,
589- CompileTimeDefineValue :: JSON ( _) => {
590- JsValue :: unknown_empty ( false , "compile time injected JSON" )
602+ CompileTimeDefineValue :: Number ( n) => JsValue :: Constant ( ConstantValue :: Num (
603+ ConstantNumber ( n. as_str ( ) . parse :: < f64 > ( ) . unwrap ( ) ) ,
604+ ) ) ,
605+ CompileTimeDefineValue :: String ( s) => JsValue :: Constant ( s. as_str ( ) . into ( ) ) ,
606+ CompileTimeDefineValue :: Array ( a) => {
607+ let mut js_value = JsValue :: Array {
608+ total_nodes : a. len ( ) as u32 ,
609+ items : a. iter ( ) . map ( |i| i. into ( ) ) . collect ( ) ,
610+ mutable : false ,
611+ } ;
612+ js_value. update_total_nodes ( ) ;
613+ js_value
614+ }
615+ CompileTimeDefineValue :: Object ( m) => {
616+ let mut js_value = JsValue :: Object {
617+ total_nodes : m. len ( ) as u32 ,
618+ parts : m
619+ . iter ( )
620+ . map ( |( k, v) | ObjectPart :: KeyValue ( k. clone ( ) . into ( ) , v. into ( ) ) )
621+ . collect ( ) ,
622+ mutable : false ,
623+ } ;
624+ js_value. update_total_nodes ( ) ;
625+ js_value
591626 }
592627 CompileTimeDefineValue :: Undefined => JsValue :: Constant ( ConstantValue :: Undefined ) ,
628+ CompileTimeDefineValue :: Evaluate ( s) => {
629+ JsValue :: Constant ( ConstantValue :: Evaluate ( s. clone ( ) ) )
630+ }
593631 }
594632 }
595633}
@@ -2192,7 +2230,7 @@ impl JsValue {
21922230 /// Some if we know if or if not the value is truthy.
21932231 pub fn is_truthy ( & self ) -> Option < bool > {
21942232 match self {
2195- JsValue :: Constant ( c) => Some ( c. is_truthy ( ) ) ,
2233+ JsValue :: Constant ( c) => c. is_truthy ( ) ,
21962234 JsValue :: Concat ( ..) => self . is_empty_string ( ) . map ( |x| !x) ,
21972235 JsValue :: Url ( ..)
21982236 | JsValue :: Array { .. }
@@ -2273,7 +2311,7 @@ impl JsValue {
22732311 /// don't know. Returns Some if we know if or if not the value is nullish.
22742312 pub fn is_nullish ( & self ) -> Option < bool > {
22752313 match self {
2276- JsValue :: Constant ( c) => Some ( c. is_nullish ( ) ) ,
2314+ JsValue :: Constant ( c) => c. is_nullish ( ) ,
22772315 JsValue :: Concat ( ..)
22782316 | JsValue :: Url ( ..)
22792317 | JsValue :: Array { .. }
0 commit comments