Skip to content

Commit

Permalink
#1386 add StubInterfaces config
Browse files Browse the repository at this point in the history
  • Loading branch information
Jand42 committed Feb 2, 2024
1 parent b343530 commit 6e957e6
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/compiler/WebSharper.Compiler.CSharp/ProjectReader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ let private getConstraints (genParams: seq<ITypeParameterSymbol>) (sr: CodeReade
}
) |> List.ofSeq

let private transformInterface (sr: R.SymbolReader) (annot: A.TypeAnnotation) (intf: INamedTypeSymbol) =
let private transformInterface (sr: R.SymbolReader) stubInterfaces (annot: A.TypeAnnotation) (intf: INamedTypeSymbol) =
if intf.TypeKind <> TypeKind.Interface || annot.IsForcedNotJavaScript then None else
let methods = ResizeArray()
let def =
Expand Down Expand Up @@ -170,7 +170,7 @@ let private transformInterface (sr: R.SymbolReader) (annot: A.TypeAnnotation) (i
NotResolvedMethods = List.ofSeq methods
Generics = getConstraints intf.TypeParameters sr
Type = annot.Type
IsStub = annot.IsStub
IsStub = stubInterfaces || annot.IsStub
}
)

Expand Down Expand Up @@ -1521,7 +1521,7 @@ let transformAssembly (comp : Compilation) (config: WsConfig) (rcomp: CSharpComp
for TypeWithAnnotation(t, d, a) in allTypes do
match t.TypeKind with
| TypeKind.Interface ->
transformInterface sr a t |> Option.iter comp.AddInterface
transformInterface sr config.StubInterfaces a t |> Option.iter comp.AddInterface
transformClass rcomp sr comp d a t |> Option.iter comp.AddClass
| TypeKind.Struct | TypeKind.Class ->
transformClass rcomp sr comp d a t |> Option.iter comp.AddClass
Expand Down
18 changes: 9 additions & 9 deletions src/compiler/WebSharper.Compiler.FSharp/ProjectReader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ let private getConstraints (genParams: seq<FSharpGenericParameter>) (sr: CodeRea
}
) |> List.ofSeq

let private transformInterface (sr: CodeReader.SymbolReader) parentAnnot (intf: FSharpEntity) =
let private transformInterface (sr: CodeReader.SymbolReader) stubInterfaces parentAnnot (intf: FSharpEntity) =
let annot =
sr.AttributeReader.GetTypeAnnot(parentAnnot, intf.Attributes)
if annot.IsForcedNotJavaScript then None else
Expand Down Expand Up @@ -155,7 +155,7 @@ let private transformInterface (sr: CodeReader.SymbolReader) parentAnnot (intf:
NotResolvedMethods = List.ofSeq methods
Generics = getConstraints intf.GenericParameters sr tparams
Type = annot.Type
IsStub = annot.IsStub
IsStub = stubInterfaces || annot.IsStub
}
)

Expand Down Expand Up @@ -224,7 +224,7 @@ let private transformInitAction (sc: Lazy<_ * StartupCode>) (comp: Compilation)

let private nrInline = N.Inline false

let rec private transformClass (sc: Lazy<_ * StartupCode>) (comp: Compilation) (ac: ArgCurrying.ResolveFuncArgs) (sr: CodeReader.SymbolReader) (classAnnots: Dictionary<FSharpEntity, TypeDefinition * A.TypeAnnotation>) (isInterface: TypeDefinition -> bool) (recMembers: Dictionary<FSMFV, Id * FSharpExpr>) (cls: FSharpEntity) (members: IList<SourceMemberOrEntity>) =
let rec private transformClass (sc: Lazy<_ * StartupCode>) (comp: Compilation) (ac: ArgCurrying.ResolveFuncArgs) (sr: CodeReader.SymbolReader) (classAnnots: Dictionary<FSharpEntity, TypeDefinition * A.TypeAnnotation>) (isInterface: TypeDefinition -> bool) stubInterfaces (recMembers: Dictionary<FSMFV, Id * FSharpExpr>) (cls: FSharpEntity) (members: IList<SourceMemberOrEntity>) =
let thisDef, annot = classAnnots.[cls]

if isResourceType sr cls then
Expand Down Expand Up @@ -953,10 +953,10 @@ let rec private transformClass (sc: Lazy<_ * StartupCode>) (comp: Compilation) (
comp.AddQuotedMethod(td.Entity, m.Entity)
)
| SourceEntity (ent, nmembers) ->
transformClass sc comp ac sr classAnnots isInterface recMembers ent nmembers |> Option.iter comp.AddClass
transformClass sc comp ac sr classAnnots isInterface stubInterfaces recMembers ent nmembers |> Option.iter comp.AddClass
| SourceInterface i ->
transformInterface sr annot i |> Option.iter comp.AddInterface
transformClass sc comp ac sr classAnnots isInterface recMembers i (ResizeArray()) |> Option.iter comp.AddClass
transformInterface sr stubInterfaces annot i |> Option.iter comp.AddInterface
transformClass sc comp ac sr classAnnots isInterface stubInterfaces recMembers i (ResizeArray()) |> Option.iter comp.AddClass
| InitAction expr ->
transformInitAction sc comp sr annot recMembers expr

Expand Down Expand Up @@ -1709,10 +1709,10 @@ let transformAssembly (logger: LoggerBase) (comp : Compilation) assemblyName (co
| SourceMember _ -> failwith "impossible: top level member"
| InitAction _ -> failwith "impossible: top level init action"
| SourceEntity (c, m) ->
transformClass sc comp argCurrying sr classAnnotations isInterface recMembers c m |> Option.iter comp.AddClass
transformClass sc comp argCurrying sr classAnnotations isInterface config.StubInterfaces recMembers c m |> Option.iter comp.AddClass
| SourceInterface i ->
transformInterface sr rootTypeAnnot i |> Option.iter comp.AddInterface
transformClass sc comp argCurrying sr classAnnotations isInterface recMembers i [||] |> Option.iter comp.AddClass
transformInterface sr config.StubInterfaces rootTypeAnnot i |> Option.iter comp.AddInterface
transformClass sc comp argCurrying sr classAnnotations isInterface config.StubInterfaces recMembers i [||] |> Option.iter comp.AddClass

let getStartupCodeClass (def: TypeDefinition, sc: StartupCode) =

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/WebSharper.Compiler/CommandTools.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type WsConfig =
AnalyzeClosures : bool option
JavaScriptScope : JavaScriptScope
JavaScriptExport : JsExport[]
StubInterfaces : bool
JSOutputPath : string option
MinJSOutputPath : string option
SingleNoJSErrors : bool
Expand Down Expand Up @@ -135,6 +136,7 @@ type WsConfig =
AnalyzeClosures = None
JavaScriptScope = JSDefault
JavaScriptExport = [||]
StubInterfaces = false
JSOutputPath = None
MinJSOutputPath = None
SingleNoJSErrors = false
Expand Down Expand Up @@ -260,6 +262,8 @@ type WsConfig =
) |> Array.ofSeq
| _ -> argError (sprintf "Invalid value in %s for JavaScriptExport, expecting true or false or an array of strings." fileName)
res <- { res with JavaScriptExport = Array.append this.JavaScriptExport j }
| "stubinterfaces" ->
res <- { res with StubInterfaces = getBool k v }
| "jsoutput" ->
let path = getPath k v
//if path.EndsWith(".js") then
Expand Down
1 change: 1 addition & 0 deletions tests/WebSharper.Module.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ do()
[<JavaScript>]
let RunTests () =
Runner.RunTests [|
StubInterfaces.Tests
Import.Tests
|]
43 changes: 43 additions & 0 deletions tests/WebSharper.Module.Tests/StubInterfaces.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// $begin{copyright}
//
// This file is part of WebSharper
//
// Copyright (c) 2008-2016 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// $end{copyright}

module WebSharper.Module.Tests.StubInterfaces

open WebSharper
open WebSharper.JavaScript
open WebSharper.Testing

type ITest =
abstract member Something: int -> int

[<JavaScript>]
type TestImpl() =
interface ITest with
member this.Something x = x + 1

[<JavaScript>]
let Tests =
TestCategory "Stub Interfaces" {
Test "Config setting" {
let o = TestImpl()
equal ((o :> ITest).Something(3)) 4
equal (o?Something(3)) 4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Tests>True</Tests>
</PropertyGroup>
<ItemGroup>
<Compile Include="StubInterfaces.fs" />
<Compile Include="Import.fs" />
<Compile Include="Main.fs" />
<EmbeddedResource Include="sayHi.js" />
Expand Down
4 changes: 4 additions & 0 deletions tests/WebSharper.Module.Tests/wsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Project": "Library",
"StubInterfaces": true
}

0 comments on commit 6e957e6

Please sign in to comment.