4 people like it.

WebSharper: compile a quotation to JavaScript

This function compiles an F# quotation to a JavaScript string, or prints errors to stderr and returns None on failure.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
open Microsoft.FSharp.Quotations
open WebSharper
type AR = IntelliFactory.Core.AssemblyResolution.AssemblyResolver
module FE = WebSharper.Compiler.FrontEnd
 
let compile (expr: Expr) : string option =
    let loader = FE.Loader.Create (AR.Create()) (eprintfn "%O")
    let options =
        { FE.Options.Default with
            References =
                List.map loader.LoadFile [
                    // These contain the JavaScript implementation for most of the standard library
                    "WebSharper.Main.dll"
                    "WebSharper.Collections.dll"
                    "WebSharper.Control.dll"
                    // Add any other assemblies used in the quotation...
                ] }
    let compiler = FE.Prepare options (eprintfn "%O")
    compiler.Compile expr
    |> Option.map (fun e -> e.ReadableJavaScript)
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
namespace WebSharper
type AR = IntelliFactory.Core.AssemblyResolution.AssemblyResolver

Full name: Script.AR
namespace IntelliFactory
namespace IntelliFactory.Core
module AssemblyResolution

from IntelliFactory.Core
type AssemblyResolver
member Install : unit -> unit
member Remove : unit -> unit
member Resolve : AssemblyName -> Assembly option
member ResolvePath : AssemblyName -> string option
member SearchDirectories : searchPaths:seq<string> -> AssemblyResolver
member SearchPaths : searchPaths:seq<string> -> AssemblyResolver
member WithBaseDirectory : string -> AssemblyResolver
member Wrap : (unit -> 'T) -> 'T
static member Create : ?domain:AppDomain -> AssemblyResolver

Full name: IntelliFactory.Core.AssemblyResolution.AssemblyResolver
namespace WebSharper.Compiler
module FrontEnd

from WebSharper.Compiler
val compile : expr:Expr -> string option

Full name: Script.compile
val expr : Expr
Multiple items
type Expr =
  override Equals : obj:obj -> bool
  member GetFreeVars : unit -> seq<Var>
  member Substitute : substitution:(Var -> Expr option) -> Expr
  member ToString : full:bool -> string
  member CustomAttributes : Expr list
  member Type : Type
  static member AddressOf : target:Expr -> Expr
  static member AddressSet : target:Expr * value:Expr -> Expr
  static member Application : functionExpr:Expr * argument:Expr -> Expr
  static member Applications : functionExpr:Expr * arguments:Expr list list -> Expr
  ...

Full name: Microsoft.FSharp.Quotations.Expr

--------------------
type Expr<'T> =
  inherit Expr
  member Raw : Expr

Full name: Microsoft.FSharp.Quotations.Expr<_>
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
val loader : Compiler.Loader
type Loader = Compiler.Loader

Full name: WebSharper.Compiler.FrontEnd.Loader
static member Compiler.Loader.Create : resolver:Compiler.Abbreviations.AssemblyResolver -> log:(string -> unit) -> Compiler.Loader
static member IntelliFactory.Core.AssemblyResolution.AssemblyResolver.Create : ?domain:System.AppDomain -> IntelliFactory.Core.AssemblyResolution.AssemblyResolver
val eprintfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.eprintfn
val options : Compiler.FrontEnd.Options
type Options =
  {ErrorLimit: int;
   KeyPair: StrongNameKeyPair option;
   References: Assembly list;
   IncludeSourceMap: bool;}
  static member Default : Options

Full name: WebSharper.Compiler.FrontEnd.Options
property Compiler.FrontEnd.Options.Default: Compiler.FrontEnd.Options
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
member Compiler.Loader.LoadFile : path:string -> Compiler.Assembly
val compiler : Compiler.FrontEnd.Compiler
val Prepare : Compiler.FrontEnd.Options -> log:(Compiler.Message -> unit) -> Compiler.FrontEnd.Compiler

Full name: WebSharper.Compiler.FrontEnd.Prepare
member Compiler.FrontEnd.Compiler.Compile : quotation:Expr * ?name:string -> Compiler.FrontEnd.CompiledAssembly option
member Compiler.FrontEnd.Compiler.Compile : assembly:System.Reflection.Assembly * ?sourceMap:bool * ?typeScript:bool -> Compiler.FrontEnd.CompiledAssembly option
member Compiler.FrontEnd.Compiler.Compile : quotation:Expr * context:System.Reflection.Assembly * ?name:string -> Compiler.FrontEnd.CompiledAssembly option
member Compiler.FrontEnd.Compiler.Compile : assembly:Compiler.FrontEnd.Assembly * ?modifyAssembly:bool * ?sourceMap:bool * ?typeScript:bool -> Compiler.FrontEnd.CompiledAssembly option
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.map
val e : Compiler.FrontEnd.CompiledAssembly
property Compiler.CompiledAssembly.ReadableJavaScript: string

More information

Link:http://fssnip.net/rP
Posted:8 years ago
Author:Loïc `Tarmil` Denuzière
Tags: websharper , javascript , quotations , web