Skip to content

Commit cdb42a3

Browse files
committed
Major cleanup of the codebase
1 parent 727dd17 commit cdb42a3

25 files changed

+417
-435
lines changed

Files/Changelogs/0.2.1.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,17 @@ John Gietzen
77
## Changelog
88

99
* Exceptions of type System.Reflection.TargetInvocationException caused by a JavaScript-catchable
10-
error will no longer bubble up to the hosting code.
10+
error will no longer bubble up to the hosting code.
11+
* Renamed the Ast.NewVars module to Ast.Utils
12+
* Renamed Ast.Scope to Ast.FunctionScope
13+
* Renamed Ast.Tree.FunctionFast to Ast.Tree.Function
14+
* Renamed CLR2.fs to Legacy.fs
15+
* Removed the ExtensionMethods module
16+
* Removed GlobalScopeHelper.GetGlobalNice and Compiler.Identifier.getValueNice
17+
* Removed the FSharpOperators module
18+
* Changed several types, modules, constructors, methods and functions from public to private/internal
19+
* Added new constructor to CommonObject that only takes two parameters (Environment and CommonObject)
20+
and defaults to using the Environment.Maps.Base property schema
21+
* Converted Ast.GlobalScope, Ast.EvalMode and Ast.LookupMode from enums to unions
22+
* Added the RequireQualifiedAccess attribute to several modules and removed alot of cryptic aliases
23+
to keep the code clean and easy to read

Src/CLR4.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Changelogs", "Changelogs",
2626
..\Files\Changelogs\0.1.93.txt = ..\Files\Changelogs\0.1.93.txt
2727
..\Files\Changelogs\0.1.94.txt = ..\Files\Changelogs\0.1.94.txt
2828
..\Files\Changelogs\0.2.0.txt = ..\Files\Changelogs\0.2.0.txt
29+
..\Files\Changelogs\0.2.1.txt = ..\Files\Changelogs\0.2.1.txt
2930
EndProjectSection
3031
EndProject
3132
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{39A51F96-9421-4146-B875-D704349A51F4}"

Src/IronJS/Compiler.Analyzer.fs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
open IronJS
44
open IronJS.Compiler
5-
open IronJS.Compiler.Ast
65
open IronJS.Compiler.Parser
76

8-
module Analyzer =
7+
module internal Analyzer =
98

109
/// Tries to find a variable in a ScopeData chain
1110
let rec findVariable name (d:ScopeData option) =
1211
match d with
1312
| None -> None // We're past the top scope, means we didn't find anything
1413
| Some d ->
1514
match d.Scope with
16-
| Catch s ->
15+
| Ast.ScopeOption.Catch s ->
1716
// Catch scopes are simple, they only
1817
// contain one variable so check the name
1918
// and if it matches return 1 (which is the storage
@@ -24,7 +23,7 @@ module Analyzer =
2423
then (1, (!s).GlobalLevel) |> Some
2524
else d.Parent |> findVariable name
2625

27-
| Function s ->
26+
| Ast.ScopeOption.Function s ->
2827
// Try to locate the variable in
2928
// the current function scope
3029
match (!s).Variables |> Map.tryFind name with
@@ -34,36 +33,36 @@ module Analyzer =
3433
// original owning scope, we got everything we need
3534
// so just return the same values
3635
match var with
37-
| Shared(storageIndex, globalLevel, _) ->
36+
| Ast.Variable.Shared(storageIndex, globalLevel, _) ->
3837
(storageIndex, globalLevel) |> Some
3938

4039
// But it can also be a private variable in the current scope
4140
// which means we have to turn it into a shared variable instead
42-
| Private _ ->
43-
s |> Ast.NewVars.promotePrivateToShared name |> Some
41+
| Ast.Variable.Private _ ->
42+
s |> Ast.Utils.promotePrivateToShared name |> Some
4443

4544
| _ ->
4645
d.Parent |> findVariable name
4746

4847
let rec buildVariables (d:ScopeData) =
4948
// First create variables in function scopes
5049
match d.Scope with
51-
| Catch _ -> () // Don't need to do anything for catch scopes
52-
| Function s ->
50+
| Ast.ScopeOption.Catch _ -> () // Don't need to do anything for catch scopes
51+
| Ast.ScopeOption.Function s ->
5352
match (!s).ScopeType with
54-
| ScopeType.GlobalScope ->
53+
| Ast.ScopeType.GlobalScope ->
5554
// Global scopes are easy, just copy
5655
// the variable set to the Globals property
5756
s := {!s with Globals = !d.Variables}
5857

59-
| ScopeType.FunctionScope ->
58+
| Ast.ScopeType.FunctionScope ->
6059
// First, copy all parameter names to the correct property
6160
s := {!s with ParameterNames = !d.Parameters}
6261

6362
// Create private variables for all
6463
// all variables in this scope
6564
for name in !d.Variables do
66-
s |> NewVars.createPrivateVariable name
65+
s |> Ast.Utils.createPrivateVariable name
6766

6867
// Then step through all missing
6968
// variables for this scope
@@ -74,7 +73,7 @@ module Analyzer =
7473
// We found a variable in the scope chain - either a previously shared
7574
// or one that used to be private and that was turned into a shared.
7675
// Create a new shared variable in this scope for the variable
77-
s |> NewVars.createSharedVariable name storageIndex globalLevel |> ignore
76+
s |> Ast.Utils.createSharedVariable name storageIndex globalLevel |> ignore
7877

7978
// Then build all child scopes variables
8079
for child in !d.Children do
@@ -84,15 +83,15 @@ module Analyzer =
8483
let globalLevel, closureLevel =
8584

8685
match d.Scope with
87-
| Catch s ->
86+
| Ast.ScopeOption.Catch s ->
8887
// Catch scope is simple, always increase closure level
8988
s := {!s with ClosureLevel = closureLevel + 1}
9089
(!s).GlobalLevel, (!s).ClosureLevel
9190

92-
| Function s ->
91+
| Ast.ScopeOption.Function s ->
9392

9493
let globalLevel =
95-
s |> NewVars.globalLevel
94+
s |> Ast.Utils.globalLevel
9695

9796
// Calculate the new closure level
9897
// which is either the same as previous
@@ -108,10 +107,10 @@ module Analyzer =
108107
// shared variables in the current scope
109108
let updateClosureLevels _ var =
110109
match var with
111-
| Shared(s, g, _) ->
110+
| Ast.Variable.Shared(s, g, _) ->
112111
if g = globalLevel
113-
then Shared(s, g, closureLevel)
114-
else Shared(s, g, levels.[g])
112+
then Ast.Variable.Shared(s, g, closureLevel)
113+
else Ast.Variable.Shared(s, g, levels.[g])
115114

116115
| _ -> var
117116

Src/IronJS/Compiler.Ast.fs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open IronJS.Support.Aliases
55
open System.Globalization
66

77
///
8+
[<RequireQualifiedAccess>]
89
module Ast =
910

1011
///
@@ -82,7 +83,7 @@ module Ast =
8283
| Eval of Tree
8384
| New of Tree * Tree list
8485
| Return of Tree
85-
| FunctionFast of string option * FunctionScope ref * Tree
86+
| Function of string option * FunctionScope ref * Tree
8687
| Invoke of Tree * Tree list
8788
| Label of string * Tree
8889
| For of string option * Tree * Tree * Tree * Tree
@@ -111,29 +112,29 @@ module Ast =
111112

112113
/// The two different types of scopes possible
113114
and ScopeType
114-
= GlobalScope = 0
115-
| FunctionScope = 1
115+
= GlobalScope
116+
| FunctionScope
116117

117118
/// The ways in a function can be effected by eval
118119
///
119120
/// Clean = No eval call that can effect this function detected
120121
/// Contains = An eval call exists inside this function
121122
/// Effected = An eval call exists in one of the scopes containing this function
122123
and EvalMode
123-
= Clean = 0
124-
| Contains = 1
125-
| Effected = 2
124+
= Clean
125+
| Contains
126+
| Effected
126127

127128
/// The two different types of lookup modes that a
128129
/// function can use, dynamic is used if a function
129130
/// contains either an eval call or a with statement
130131
/// otherwise static is used (which is a lot faster)
131132
and LookupMode
132-
= Static = 0
133-
| Dynamic = 1
133+
= Static
134+
| Dynamic
134135

135136
/// Represents a function scope
136-
and Scope = {
137+
and FunctionScope = {
137138
Id : uint64
138139

139140
GlobalLevel: int
@@ -154,10 +155,37 @@ module Ast =
154155
CatchScopes : CatchScope ref list
155156
ParameterNames : string list
156157
Globals : string Set
158+
}
157159

158-
} with
159-
static member NewGlobal = {Scope.New with ScopeType = ScopeType.GlobalScope}
160-
static member New = {
160+
/// Represents a catch scope
161+
and CatchScope = {
162+
Name : string
163+
GlobalLevel : int
164+
ClosureLevel : int
165+
CatchScopes : CatchScope ref list
166+
}
167+
168+
///
169+
and ScopeOption
170+
= Catch of CatchScope ref
171+
| Function of FunctionScope ref
172+
173+
///
174+
and [<NoComparison; NoEquality>] Variable
175+
= Shared of int * int * int
176+
| Private of int
177+
178+
and VariableMap = Map<string, Variable>
179+
180+
///
181+
[<RequireQualifiedAccess>]
182+
module Utils =
183+
184+
// Type short hand for scopes
185+
type private S = FunctionScope ref
186+
187+
///
188+
let createFunctionScope() = {
161189
Id = 0UL
162190

163191
GlobalLevel = 0
@@ -180,37 +208,18 @@ module Ast =
180208
Globals = Set.empty
181209
}
182210

183-
/// Represents a catch scope
184-
and CatchScope = {
185-
Name : string
186-
GlobalLevel : int
187-
ClosureLevel : int
188-
CatchScopes : CatchScope ref list
189-
} with
190-
static member New name globalLevel closureLevel = ref {
211+
///
212+
let createGlobalScope() =
213+
{createFunctionScope()with ScopeType = ScopeType.GlobalScope}
214+
215+
///
216+
let createCatchScope name globalLevel closureLevel = ref {
191217
Name = name
192218
GlobalLevel = globalLevel
193219
ClosureLevel = closureLevel
194220
CatchScopes = List.empty
195221
}
196222

197-
and FunctionScope = Scope
198-
199-
and ScopeOption
200-
= Catch of CatchScope ref
201-
| Function of FunctionScope ref
202-
203-
and [<NoComparison; NoEquality>] Variable
204-
= Shared of int * int * int
205-
| Private of int
206-
207-
and VariableMap = Map<string, Variable>
208-
209-
module NewVars =
210-
211-
// Type short hand for scopes
212-
type private S = FunctionScope ref
213-
214223
///
215224
let clone s = ref !s
216225

@@ -310,7 +319,7 @@ module Ast =
310319
///
311320
let addFunction (ast:Tree) (s:S) =
312321
match ast with
313-
| FunctionFast(Some name, _, _) ->
322+
| Tree.Function(Some name, _, _) ->
314323
s := {!s with Functions = (!s).Functions |> Map.add name ast}
315324

316325
| _ ->

Src/IronJS/Compiler.Context.fs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module Target =
6666
env |> create ast Mode.Global None
6767

6868
///
69-
module Labels =
69+
module internal Labels =
7070

7171
type LabelGroup =
7272
Map<string, int * Dlr.Label> * Map<int, Dlr.Label> ref
@@ -113,7 +113,7 @@ module Labels =
113113
}
114114

115115
///
116-
module Parameters =
116+
module internal Parameters =
117117

118118
///
119119
type T = {
@@ -147,7 +147,7 @@ module Parameters =
147147
(t |> environment) .-> "Return"
148148

149149
///
150-
module Context =
150+
module internal Context =
151151

152152
type T = {
153153
CompileFunction : Target.T -> Delegate
@@ -167,7 +167,7 @@ module Context =
167167
member x.Env = x.Parameters |> Parameters.environment
168168
member x.Globals = x.Parameters |> Parameters.globals
169169
member x.ReturnBox = x.Parameters |> Parameters.returnBox
170-
member x.DynamicLookup = x.Scope |> Ast.NewVars.hasDynamicLookup || x.InsideWith
170+
member x.DynamicLookup = x.Scope |> Ast.Utils.hasDynamicLookup || x.InsideWith
171171
member x.Compile ast = x.Compiler x ast
172172

173173
///
@@ -192,7 +192,8 @@ module Context =
192192
type Ctx = Context.T
193193

194194
///
195-
type [<AllowNullLiteral>] EvalTarget() =
195+
[<AllowNullLiteral>]
196+
type EvalTarget() =
196197
[<DefaultValue>] val mutable Target : BV
197198
[<DefaultValue>] val mutable GlobalLevel : int
198199
[<DefaultValue>] val mutable ClosureLevel : int

Src/IronJS/Compiler.ControlFlow.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open IronJS.Dlr.Operators
88
open IronJS.Compiler
99
open IronJS.Compiler.Context
1010

11-
module ControlFlow =
11+
module internal ControlFlow =
1212

1313
//----------------------------------------------------------------------------
1414
// 11.12 conditional

Src/IronJS/Compiler.Core.fs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ module Core =
5050
| Ast.BreakPoint(line, column) ->
5151

5252
#if ENABLE_BREAKPOINTS
53-
5453
let locals =
5554
Dlr.blockTmpT<MutableDict<string, obj>> (fun locals ->
5655
let addLocal (name, _) =
@@ -66,7 +65,6 @@ module Core =
6665

6766
let args = [!!!line; !!!column; locals]
6867
Dlr.invoke (ctx.Env .-> "BreakPoint") args
69-
7068
#else
7169
Dlr.void'
7270
#endif
@@ -110,7 +108,7 @@ module Core =
110108
| Ast.Invoke(func, args) -> Function.invoke ctx func args
111109
| Ast.New(func, args) -> Function.new' ctx func args
112110
| Ast.Return tree -> Function.return' ctx tree
113-
| Ast.FunctionFast(_, scope, _) -> Function.create ctx scope ast
111+
| Ast.Function(_, scope, _) -> Function.create ctx scope ast
114112

115113
//Control Flow
116114
| Ast.Switch(value, cases) -> ControlFlow.switch ctx value cases
@@ -142,7 +140,7 @@ module Core =
142140
///
143141
let private cloneScope (target:Target.T) =
144142
match target.Ast with
145-
| Ast.FunctionFast(_, s, ast) -> ref !s
143+
| Ast.Function(_, s, ast) -> ref !s
146144

147145
///
148146
let rec private compile (target:Target.T) =
@@ -157,9 +155,9 @@ module Core =
157155
Context.T.CompileFunction = compile
158156
Context.T.InsideWith = false
159157
Context.T.Scope = scope
160-
Context.T.ClosureLevel = scope $ Ast.NewVars.closureLevel
161-
Context.T.Variables = scope $ Ast.NewVars.variables
162-
Context.T.CatchScopes = scope $ Ast.NewVars.catchScopes $ ref
158+
Context.T.ClosureLevel = scope $ Ast.Utils.closureLevel
159+
Context.T.Variables = scope $ Ast.Utils.variables
160+
Context.T.CatchScopes = scope $ Ast.Utils.catchScopes $ ref
163161

164162
Context.T.Labels =
165163
{

0 commit comments

Comments
 (0)