Skip to content

Commit f333d0d

Browse files
committed
implement Lazy<T> & Stopwatch
1 parent 5cda949 commit f333d0d

5 files changed

Lines changed: 223 additions & 34 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/All.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ return function(dir, conf)
5151
load("Threads.Timer")
5252
load("Threads.Thread")
5353
load("Threads.Task")
54+
load("Utilities")
5455
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Exception.lua‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ local function toString(this)
4040
return tconcat(t)
4141
end
4242

43+
local function ctorOfException(this, message, innerException)
44+
this.message = message
45+
this.innerException = innerException
46+
end
47+
4348
local Exception = define("System.Exception", {
4449
__tostring = toString,
50+
__ctor__ = ctorOfException,
4551
ToString = toString,
4652
getMessage = getMessage,
47-
48-
__ctor__ = function(this, message, innerException)
49-
this.message = message
50-
this.innerException = innerException
51-
end,
52-
53+
5354
getInnerException = function(this)
5455
return this.innerException
5556
end,
@@ -77,7 +78,7 @@ local ArgumentException = define("System.ArgumentException", {
7778
__inherits__ = { Exception },
7879

7980
__ctor__ = function(this, message, paramName, innerException)
80-
System.Exception.__ctor__(this, message or "Value does not fall within the expected range.", innerException)
81+
ctorOfException(this, message or "Value does not fall within the expected range.", innerException)
8182
this.paramName = paramName
8283
end,
8384

@@ -126,7 +127,7 @@ define("System.IndexOutOfRangeException", {
126127
__inherits__ = { Exception },
127128

128129
__ctor__ = function (this, message, innerException)
129-
Exception.__ctor__(this, message or "Index was outside the bounds of the array.", innerException)
130+
ctorOfException(this, message or "Index was outside the bounds of the array.", innerException)
130131
end
131132
})
132133

@@ -163,7 +164,7 @@ define("System.KeyNotFoundException", {
163164
__inherits__ = { Exception },
164165

165166
__ctor__ = function(this, message, innerException)
166-
Exception.__ctor__(this, message or "Key not found.", innerException)
167+
ctorOfException(this, message or "Key not found.", innerException)
167168
end
168169
})
169170

@@ -172,7 +173,7 @@ local ArithmeticException = define("System.ArithmeticException", {
172173
__inherits__ = { Exception },
173174

174175
__ctor__ = function(this, message, innerException)
175-
Exception.__ctor__(this, message or "Overflow or underflow in the arithmetic operation.", innerException)
176+
ctorOfException(this, message or "Overflow or underflow in the arithmetic operation.", innerException)
176177
end
177178
})
178179

@@ -199,7 +200,7 @@ define("System.FormatException", {
199200
__inherits__ = { Exception },
200201

201202
__ctor__ = function(this, message, innerException)
202-
Exception.__ctor__(this, message or "Invalid format.", innerException)
203+
ctorOfException(this, message or "Invalid format.", innerException)
203204
end
204205
})
205206

@@ -208,7 +209,7 @@ define("System.InvalidCastException", {
208209
__inherits__ = { Exception },
209210

210211
__ctor__ = function(this, message, innerException)
211-
Exception.__ctor__(this, message or "The cast is not valid.", innerException)
212+
ctorOfException(this, message or "The cast is not valid.", innerException)
212213
end
213214
})
214215

@@ -217,7 +218,7 @@ define("System.InvalidOperationException", {
217218
__inherits__ = { Exception },
218219

219220
__ctor__ = function(this, message, innerException)
220-
Exception.__ctor__(this, message or "Operation is not valid due to the current state of the object.", innerException)
221+
ctorOfException(this, message or "Operation is not valid due to the current state of the object.", innerException)
221222
end
222223
})
223224

@@ -226,7 +227,7 @@ define("System.NotImplementedException", {
226227
__inherits__ = { Exception },
227228

228229
__ctor__ = function(this, message, innerException)
229-
Exception.__ctor__(this, message or "The method or operation is not implemented.", innerException)
230+
ctorOfException(this, message or "The method or operation is not implemented.", innerException)
230231
end
231232
})
232233

@@ -235,7 +236,7 @@ define("System.NotSupportedException", {
235236
__inherits__ = { Exception },
236237

237238
__ctor__ = function(this, message, innerException)
238-
Exception.__ctor__(this, message or "Specified method is not supported.", innerException)
239+
ctorOfException(this, message or "Specified method is not supported.", innerException)
239240
end
240241
})
241242

@@ -244,7 +245,7 @@ define("System.NullReferenceException", {
244245
__inherits__ = { Exception },
245246

246247
__ctor__ = function(this, message, innerException)
247-
Exception.__ctor__(this, message or "Object reference not set to an instance of an object.", innerException)
248+
ctorOfException(this, message or "Object reference not set to an instance of an object.", innerException)
248249
end
249250
})
250251

@@ -253,7 +254,7 @@ define("System.RankException", {
253254
__inherits__ = { Exception },
254255

255256
__ctor__ = function(this, message, innerException)
256-
Exception.__ctor__(this, message or "Attempted to operate on an array with the incorrect number of dimensions.", innerException)
257+
ctorOfException(this, message or "Attempted to operate on an array with the incorrect number of dimensions.", innerException)
257258
end
258259
})
259260

@@ -262,6 +263,6 @@ define("System.TypeLoadException", {
262263
__inherits__ = { Exception },
263264

264265
__ctor__ = function(this, message, innerException)
265-
Exception.__ctor__(this, message or "Failed when load type.", innerException)
266+
ctorOfException(this, message or "Failed when load type.", innerException)
266267
end
267268
})
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
--[[
2+
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
--]]
16+
17+
local System = System
18+
local throw = System.throw
19+
local define = System.define
20+
local trunc = System.trunc
21+
local ArgumentNullException = System.ArgumentNullException
22+
local fromMilliseconds = System.TimeSpan.FromMilliseconds
23+
24+
local select = select
25+
local type = type
26+
local os = os
27+
local clock = os.clock
28+
local tostring = tostring
29+
30+
define("System.Environment", {
31+
Exit = os.exit,
32+
getStackTrace = debug.traceback,
33+
34+
getTickCount = function ()
35+
return trunc(clock() * 1000)
36+
end
37+
})
38+
39+
local Lazy = {
40+
created = false,
41+
42+
__ctor__ = function (this, ...)
43+
local n = select("#", ...)
44+
if n == 0 then
45+
elseif n == 1 then
46+
local valueFactory = ...
47+
if valueFactory == nil then
48+
throw(ArgumentNullException("valueFactory"))
49+
elseif type(valueFactory) ~= "boolean" then
50+
this.valueFactory = valueFactory
51+
end
52+
elseif n == 2 then
53+
local valueFactory = ...
54+
if valueFactory == nil then
55+
throw(ArgumentNullException("valueFactory"))
56+
end
57+
this.valueFactory = valueFactory
58+
end
59+
end,
60+
61+
getIsValueCreated = function (this)
62+
return this.created
63+
end,
64+
65+
getValue = function (this)
66+
if not this.created then
67+
local valueFactory = this.valueFactory
68+
if valueFactory then
69+
this.value = valueFactory()
70+
this.valueFactory = nil
71+
else
72+
this.value = this.__genericT__()
73+
end
74+
this.created = true
75+
end
76+
return this.value
77+
end,
78+
79+
ToString = function (this)
80+
if this.created then
81+
return this.value:ToString()
82+
end
83+
return "Value is not created."
84+
end
85+
}
86+
87+
define("System.Lazy", function (T)
88+
return {
89+
__genericT__ = T
90+
}
91+
end, Lazy)
92+
93+
local function getPrecision(seconds)
94+
local s = tostring(seconds)
95+
local i = s:find("%.")
96+
if i then
97+
return #s - i
98+
end
99+
return 0
100+
end
101+
102+
local ticker, frequency
103+
local time = System.config.time
104+
if time then
105+
local p1, p2 = getPrecision(time()), getPrecision(clock())
106+
if p1 > p2 then
107+
ticker = time
108+
frequency = 10 ^ p1
109+
else
110+
ticker = clock
111+
frequency = 10 ^ p2
112+
end
113+
else
114+
local p = getPrecision(clock())
115+
ticker = clock
116+
frequency = 10 ^ p
117+
end
118+
119+
local function getRawElapsedTicks(this)
120+
local timeElapsed = this.elapsed
121+
if this.running then
122+
local currentTimeStamp = ticker()
123+
local elapsedUntilNow = currentTimeStamp - this.startTimeStamp
124+
timeElapsed = timeElapsed + elapsedUntilNow
125+
end
126+
return timeElapsed
127+
end
128+
129+
local Stopwatch
130+
Stopwatch = define("System.Stopwatch", {
131+
elapsed = 0,
132+
running = false,
133+
IsHighResolution = false,
134+
Frequency = frequency,
135+
136+
StartNew = function ()
137+
local t = Stopwatch()
138+
t:Start()
139+
return t
140+
end,
141+
142+
GetTimestamp = function ()
143+
return trunc(ticker() * frequency)
144+
end,
145+
146+
Start = function (this)
147+
if not this.running then
148+
this.startTimeStamp = ticker()
149+
this.running = true
150+
end
151+
end,
152+
153+
Stop = function (this)
154+
if this.running then
155+
local endTimeStamp = ticker()
156+
local elapsedThisPeriod = endTimeStamp - this.startTimeStamp
157+
local elapsed = this.elapsed + elapsedThisPeriod
158+
this.running = false
159+
if elapsed < 0 then
160+
-- os.clock may be return negative value
161+
elapsed = 0
162+
end
163+
this.elapsed = elapsed
164+
end
165+
end,
166+
167+
Reset = function (this)
168+
this.elapsed = 0
169+
this.running = false
170+
this.startTimeStamp = 0
171+
end,
172+
173+
Restart = function (this)
174+
this.elapsed = 0
175+
this.startTimeStamp = ticker()
176+
this.running = true
177+
end,
178+
179+
getIsRunning = function (this)
180+
return this.running
181+
end,
182+
183+
getElapsed = function (this)
184+
return fromMilliseconds(getRawElapsedTicks(this))
185+
end,
186+
187+
getElapsedMilliseconds = function (this)
188+
return trunc(getRawElapsedTicks(this) * 1000)
189+
end,
190+
191+
getElapsedTicks = function (this)
192+
return trunc(getRawElapsedTicks(this) * frequency)
193+
end
194+
})

‎CSharp.lua/CoreSystem.Lua/Sample/test.lua‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ local function testAsync()
310310
-- Generated by CSharp.lua Compiler
311311
local System = System
312312
local Test
313-
System.usingDeclare(function (global)
313+
System.import(function (global)
314314
Test = global.Test
315315
end)
316316
System.namespace("Test", function (namespace)
@@ -371,3 +371,4 @@ test(testStringBuilder, "StringBuilder")
371371
test(testIO, "IO")
372372
--test(testConsole, "Console")
373373
--test(testAsync, "Async")
374+

‎CSharp.lua/System.xml‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ limitations under the License.
6969
<arg type="System.Int32" />
7070
<arg type="System.Int32" />
7171
<arg type="System.DateTimeKind" />
72-
</method>
72+
</method>
73+
</class>
74+
<class name="TimeZoneInfo">
75+
<property name="BaseUtcOffset" >
76+
<get Template="System.DateTime.BaseUtcOffset" />
77+
</property>
7378
</class>
7479
<class name="Object">
7580
<method name="Equals" Name="EqualsObj" >
@@ -127,20 +132,6 @@ limitations under the License.
127132
<method name="CreateInstance" ArgCount="1" Template="{0}.c()" />
128133
<method name="CreateInstance" ArgCount="2" Template="System.CreateInstance({0}, {*1})" />
129134
</class>
130-
<class name="Environment">
131-
<method name="Exit" Template="os.exit({0})" />
132-
<property name="TickCount">
133-
<get Template="System.currentTimeMillis()" />
134-
</property>
135-
<property name="StackTrace">
136-
<get Template="debug.traceback()" />
137-
</property>
138-
</class>
139-
<class name="TimeZoneInfo">
140-
<property name="BaseUtcOffset" >
141-
<get Template="System.DateTime.BaseUtcOffset" />
142-
</property>
143-
</class>
144135
<class name="Tuple">
145136
<method name="Create" ArgCount="1" Template="System.tuple({0})" />
146137
<method name="Create" ArgCount="2" Template="System.tuple({0}, {1})" />
@@ -490,6 +481,7 @@ limitations under the License.
490481
<class name="Debug">
491482
<method name="Assert" Template="assert({0}, {1})" />
492483
</class>
484+
<class name="Stopwatch" Name="System.Stopwatch" />
493485
</namespace>
494486
<namespace name="System.Diagnostics.Contracts">
495487
<class name="Contract">

0 commit comments

Comments
 (0)