@@ -16,7 +16,9 @@ limitations under the License.
1616
1717local System = System
1818local throw = System .throw
19+ local try = System .try
1920local trunc = System .trunc
21+ local post = System .post
2022local Exception = System .Exception
2123local NotImplementedException = System .NotImplementedException
2224local ArgumentOutOfRangeException = System .ArgumentOutOfRangeException
@@ -134,13 +136,13 @@ function TaskExceptionHolder.__gc(this)
134136end
135137
136138local Task = {}
137- local taskIdCounter = 1
139+ local nextTaskId = 1
138140local currentTask
139141local completedTask
140142
141143local function getNewId ()
142- local id = taskIdCounter
143- taskIdCounter = taskIdCounter + 1
144+ local id = nextTaskId
145+ nextTaskId = nextTaskId + 1
144146 return id
145147end
146148
@@ -167,12 +169,10 @@ function Task.getStatus(this)
167169end
168170
169171local function getException (this )
170- local exceptionsHolder = this .exceptionsHolder
171- if exceptionsHolder then
172- return createExceptionObject (exceptionsHolder )
173- else
174- return nil
172+ if this .status == TaskStatusFaulted then
173+ return createExceptionObject (this .data )
175174 end
175+ return nil
176176end
177177
178178Task .getException = getException
@@ -193,7 +193,7 @@ function Task.getIsFaulted(this)
193193end
194194
195195local function fromResult (result )
196- return setmetatable ({ status = TaskStatusRanToCompletion , result = result }, Task )
196+ return setmetatable ({ status = TaskStatusRanToCompletion , data = result }, Task )
197197end
198198
199199Task .FromResult = fromResult
@@ -202,7 +202,7 @@ local function fromCanceled(cancellationToken)
202202 if cancellationToken and cancellationToken :getIsCancellationRequested () then
203203 throw (ArgumentOutOfRangeException (" cancellationToken" ))
204204 end
205- return setmetatable ({ status = TaskStatusCanceled , cancellationToken = cancellationToken }, Task )
205+ return setmetatable ({ status = TaskStatusCanceled , data = cancellationToken }, Task )
206206end
207207
208208Task .FromCanceled = fromCanceled
@@ -224,13 +224,7 @@ local function trySetComplete(this, status, data)
224224 end
225225
226226 this .status = status
227- if status == TaskStatusRanToCompletion then
228- this .result = data
229- elseif status == TaskStatusFaulted then
230- this .exceptionsHolder = data
231- elseif status == TaskStatusCanceled then
232- this .cancellationToken = data
233- end
227+ this .data = data
234228
235229 local continueActions = this .continueActions
236230 if continueActions then
@@ -261,8 +255,9 @@ local function newWaitingTask(isVoid)
261255 return setmetatable ({ status = TaskStatusWaitingForActivation , isVoid = isVoid }, Task )
262256end
263257
264- local setTimeout = System .config .setTimeout
265- local clearTimeout = System .config .clearTimeout
258+ local config = System .config
259+ local setTimeout = config .setTimeout
260+ local clearTimeout = config .clearTimeout
266261
267262function Task .Delay (delay , cancellationToken )
268263 if not setTimeout or not clearTimeout then
@@ -317,7 +312,22 @@ local function getContinueActions(task)
317312 return continueActions
318313end
319314
320- function Task .ContinueWith (this , ...)
315+ function Task .ContinueWith (this , continuationAction )
316+ local t = newWaitingTask ()
317+ local function f ()
318+ try (function ()
319+ assert (trySetResult (t , continuationAction ()))
320+ end , function (e )
321+ assert (trySetException (t , e ))
322+ end )
323+ end
324+ if isCompleted (this ) then
325+ post (f )
326+ else
327+ local continueActions = getContinueActions (task )
328+ tinsert (continueActions , f )
329+ end
330+ return t
321331end
322332
323333local function await (t , task )
@@ -327,9 +337,9 @@ local function await(t, task)
327337 local status = task .status
328338 local ok , v
329339 if status == TaskStatusRanToCompletion then
330- ok , v = true , task .result
340+ ok , v = true , task .data
331341 elseif status == TaskStatusFaulted then
332- ok , v = false , getException (task )
342+ ok , v = false , createExceptionObject (task . data )
333343 elseif status == TaskStatusCanceled then
334344 ok , v = false , TaskCanceledException (task )
335345 else
@@ -353,9 +363,9 @@ function Task.await(this, task)
353363 if status == TaskStatusWaitingForActivation then
354364 return await (this , task )
355365 elseif status == TaskStatusRanToCompletion then
356- return task .result
366+ return task .data
357367 elseif status == TaskStatusFaulted then
358- throw (getException (task ))
368+ throw (createExceptionObject (task . data ))
359369 elseif status == TaskStatusCanceled then
360370 throw (TaskCanceledException (task ))
361371 else
0 commit comments