@@ -8,6 +8,11 @@ open System
88open System.Collections
99open System.Collections .Generic
1010open Internal.Utilities
11+ #if FABLE_ COMPILER
12+ open Microsoft.FSharp .Collections
13+ open Microsoft.FSharp .Core
14+ open Microsoft.FSharp .Core .Operators
15+ #endif
1116
1217#if FX_ RESHAPED_ REFLECTION
1318open Microsoft.FSharp .Core .ReflectionAdapters
@@ -20,9 +25,10 @@ let (>>>&) (x:int32) (n:int32) = int32 (uint32 x >>> n)
2025let notlazy v = Lazy<_>. CreateFromValue v
2126
2227let inline isNonNull x = not ( isNull x)
23- let inline nonNull msg x = if isNull x then failwith ( " null: " ^ msg) else x
28+ let inline nonNull msg x = if isNull x then failwith ( " null: " + msg) else x
2429let (= ==) x y = LanguagePrimitives.PhysicalEquality x y
2530
31+ #if ! FABLE_ COMPILER
2632//---------------------------------------------------------------------
2733// Library: ReportTime
2834//---------------------------------------------------------------------
@@ -36,13 +42,19 @@ let reportTime =
3642 let first = match ! tFirst with None -> ( tFirst := Some t; t) | Some t -> t
3743 printf " ilwrite: TIME %10.3f (total) %10.3f (delta) - %s \n " ( t - first) ( t - prev) descr
3844 tPrev := Some t
45+ #endif
3946
4047//-------------------------------------------------------------------------
4148// Library: projections
4249//------------------------------------------------------------------------
4350
44- [<Struct>]
4551/// An efficient lazy for inline storage in a class type. Results in fewer thunks.
52+ #if FABLE_ COMPILER
53+ type InlineDelayInit < 'T when 'T : not struct >( f : unit -> 'T ) =
54+ let store = lazy ( f())
55+ member x.Value = store.Force()
56+ #else
57+ [<Struct>]
4658type InlineDelayInit < 'T when 'T : not struct > =
4759 new ( f : unit -> 'T ) = { store = Unchecked.defaultof< 'T>; func = System.Func<_>( f) }
4860 val mutable store : 'T
@@ -54,6 +66,7 @@ type InlineDelayInit<'T when 'T : not struct> =
5466 let res = System.Threading.LazyInitializer.EnsureInitialized(& x.store, x.func)
5567 x.func <- Unchecked.defaultof<_>
5668 res
69+ #endif
5770
5871//-------------------------------------------------------------------------
5972// Library: projections
@@ -172,7 +185,7 @@ module Option =
172185
173186module List =
174187
175- let item n xs = List.nth xs n
188+ let item n xs = List.item n xs
176189#if FX_ RESHAPED_ REFLECTION
177190 open PrimReflectionAdapters
178191 open Microsoft.FSharp .Core .ReflectionAdapters
@@ -233,7 +246,9 @@ module List =
233246 ch [] [] l
234247
235248 let mapq ( f : 'T -> 'T ) inp =
249+ #if ! FABLE_ COMPILER
236250 assert not ( typeof< 'T>. IsValueType)
251+ #endif
237252 match inp with
238253 | [] -> inp
239254 | _ ->
@@ -398,10 +413,10 @@ module String =
398413 if r = - 1 then indexNotFound() else r
399414
400415 let contains ( s : string ) ( c : char ) =
401- s.IndexOf( c, 0 , String.length s ) <> - 1
416+ s.IndexOf( c) <> - 1
402417
403418 let order = LanguagePrimitives.FastGenericComparer< string>
404-
419+
405420 let lowercase ( s : string ) =
406421 s.ToLowerInvariant()
407422
@@ -512,7 +527,8 @@ module Eventually =
512527 else forceWhile check ( work())
513528
514529 let force e = Option.get ( forceWhile ( fun () -> true ) e)
515-
530+
531+ #if ! FABLE_ COMPILER
516532 /// Keep running the computation bit by bit until a time limit is reached.
517533 /// The runner gets called each time the computation is restarted
518534 let repeatedlyProgressUntilDoneOrTimeShareOver timeShareInMilliseconds runner e =
@@ -532,6 +548,7 @@ module Eventually =
532548 loop( work())
533549 loop( e))
534550 runTimeShare e
551+ #endif
535552
536553 let rec bind k e =
537554 match e with
@@ -613,13 +630,15 @@ type MemoizationTable<'T,'U>(compute: 'T -> 'U, keyComparer: IEqualityComparer<'
613630 let table = new System.Collections.Generic.Dictionary< 'T, 'U>( keyComparer)
614631 member t.Apply ( x ) =
615632 if ( match canMemoize with None -> true | Some f -> f x) then
616- let mutable res = Unchecked.defaultof< 'U>
617- let ok = table.TryGetValue( x,& res)
633+ #if FABLE_ COMPILER
634+ (
635+ #else
636+ let ok , res = table.TryGetValue( x)
618637 if ok then res
619638 else
620639 lock table ( fun () ->
621- let mutable res = Unchecked.defaultof < 'U >
622- let ok = table.TryGetValue( x,& res )
640+ #endif
641+ let ok , res = table.TryGetValue( x)
623642 if ok then res
624643 else
625644 let res = compute x
@@ -661,12 +680,16 @@ type LazyWithContext<'T,'ctxt> =
661680 match x.funcOrException with
662681 | null -> x.value
663682 | _ ->
683+ #if FABLE_ COMPILER
684+ x.UnsynchronizedForce( ctxt)
685+ #else
664686 // Enter the lock in case another thread is in the process of evaluating the result
665687 System.Threading.Monitor.Enter( x);
666688 try
667689 x.UnsynchronizedForce( ctxt)
668690 finally
669691 System.Threading.Monitor.Exit( x)
692+ #endif
670693
671694 member x.UnsynchronizedForce( ctxt) =
672695 match x.funcOrException with
@@ -697,11 +720,11 @@ module Tables =
697720 let memoize f =
698721 let t = new Dictionary<_,_>( 1000 , HashIdentity.Structural)
699722 fun x ->
700- let mutable res = Unchecked.defaultof <_>
701- if t.TryGetValue ( x , & res ) then
723+ let ok , res = t.TryGetValue ( x )
724+ if ok then
702725 res
703726 else
704- res <- f x; t.[ x] <- res; res
727+ let res = f x in t.[ x] <- res; res
705728
706729//-------------------------------------------------------------------------
707730// Library: Name maps
@@ -808,10 +831,17 @@ type LayeredMap<'Key,'Value when 'Key : comparison> = Map<'Key,'Value>
808831type Map< 'Key, 'Value when 'Key : comparison> with
809832 static member Empty : Map< 'Key, 'Value> = Map.empty
810833
834+ #if FABLE_ COMPILER
835+ member m.TryGetValue ( key) =
836+ match m.TryFind key with
837+ | None -> false , Unchecked.defaultof<_>
838+ | Some r -> true , r
839+ #else
811840 member m.TryGetValue ( key, res: byref< 'Value>) =
812841 match m.TryFind key with
813842 | None -> false
814843 | Some r -> res <- r; true
844+ #endif
815845
816846 member x.Values = [ for ( KeyValue(_, v)) in x -> v ]
817847 member x.AddAndMarkAsCollapsible ( kvs: _[]) = ( x, kvs) ||> Array.fold ( fun x ( KeyValue ( k , v )) -> x.Add( k, v))
@@ -831,6 +861,7 @@ type LayeredMultiMap<'Key,'Value when 'Key : equality and 'Key : comparison>(con
831861 member x.Values = contents.Values |> List.concat
832862 static member Empty : LayeredMultiMap< 'Key, 'Value> = LayeredMultiMap LayeredMap.Empty
833863
864+ #if ! FABLE_ COMPILER
834865[< AutoOpen>]
835866module Shim =
836867
@@ -904,3 +935,4 @@ module Shim =
904935 System.Text.Encoding.GetEncoding( n)
905936
906937 let mutable FileSystem = DefaultFileSystem() :> IFileSystem
938+ #endif //!FABLE_COMPILER
0 commit comments