@@ -264,26 +264,15 @@ The `Resource` class represents a runtime instance of a resource type:
264
264
class Resource :
265
265
t: ResourceType
266
266
rep: int
267
- borrowers: MutableMapping[ComponentInstance, int ]
268
267
269
268
def __init__ (self , t , rep ):
270
269
self .t = t
271
270
self .rep = rep
272
- self .borrowers = {}
273
271
```
274
272
The ` t ` field points to the [ ` resourcetype ` ] ( Explainer.md#type-definitions )
275
273
used to create this ` Resource ` and is used to perform dynamic type checking
276
- below. Next, ` rep ` is the core representation of this ` Resource ` which is
277
- currently fixed to ` i32 ` as described in the Explainer. Lastly, ` borrowers ` is
278
- a list of component instances that currently hold a ` borrow ` handle along with
279
- the index of this ` borrow ` handle in the ` HandleTable ` . This map is used to
280
- deduplicate ` borrow ` handles, preserving the invariant that a component
281
- instance contains * at most one* handle referring to a particular ` Resource ` .
282
- While the map here is a Python dictionary, using static analysis of the flow of
283
- resource types throughout a component instance DAG, an AOT compiler can
284
- alternatively implement ` borrowers ` with a fixed-size array embedded directly
285
- in the resource, assigning a static index to each potential client instance in
286
- the DAG.
274
+ below. The ` rep ` field stores the representation value of this ` Resource ` whose
275
+ type is currently fixed to ` i32 ` as described in the Explainer.
287
276
288
277
The ` OwnHandle ` and ` BorrowHandle ` classes represent runtime handle values of
289
278
` own ` and ` borrow ` type, resp:
@@ -353,35 +342,25 @@ class HandleTable:
353
342
self .free = []
354
343
355
344
def insert (self , cx , h ):
356
- match h:
357
- case OwnHandle():
358
- assert (len (h.resource.borrowers) == 0 )
359
- case BorrowHandle():
360
- if cx.inst in h.resource.borrowers:
361
- return h.resource.borrowers[cx.inst]
362
-
363
345
if self .free:
364
346
i = self .free.pop()
365
347
assert (self .array[i] is None )
366
348
self .array[i] = h
367
349
else :
368
350
i = len (self .array)
369
351
self .array.append(h)
370
-
371
352
if isinstance (h, BorrowHandle):
372
- h.resource.borrowers[cx.inst] = i
373
353
cx.call.borrow_count += 1
374
354
return i
375
355
```
376
356
The ` HandleTable ` class maintains a dense array of handles that can contain
377
- holes created by the ` remove ` method (defined below). These holes are kept
378
- in a separate Python list here, but an optimizing implementation could instead
379
- store the free list in the free elements of ` array ` . When inserting a new
380
- handle, ` HandleTable ` first consults the ` free ` list, which is popped LIFO to
381
- better detect use-after-free bugs in the guest code. The ` insert ` method
382
- uses the ` Handle.borrowers ` map to deduplicate borrowed handles. For
383
- non-deduplicated handles, ` insert ` increments ` Call.borrow_count ` to guard that
384
- this handle has been dropped by the end of the call.
357
+ holes created by the ` remove ` method (defined below). These holes are kept in a
358
+ separate Python list here, but an optimizing implementation could instead store
359
+ the free list in the free elements of ` array ` . When inserting a new handle,
360
+ ` HandleTable ` first consults the ` free ` list, which is popped LIFO to better
361
+ detect use-after-free bugs in the guest code. The ` insert ` method increments
362
+ ` Call.borrow_count ` to guard that this handle has been dropped by the end of
363
+ the call.
385
364
386
365
The ` get ` method is used by other ` HandleTable ` methods and canonical
387
366
definitions below and uses dynamic guards to catch out-of-bounds and
@@ -418,11 +397,9 @@ for later recycling by `insert` (above).
418
397
match t:
419
398
case Own(_):
420
399
trap_if(not isinstance (h, OwnHandle))
421
- assert (len (h.resource.borrowers) == 0 )
422
400
case Borrow(_):
423
401
trap_if(not isinstance (h, BorrowHandle))
424
402
cx.call.borrow_count -= 1
425
- del h.resource.borrowers[cx.inst]
426
403
self .array[i] = None
427
404
self .free.append(i)
428
405
return h
0 commit comments