More evidence that memory safety is the future for programming languages

Sebastiaan Koppe mail at skoppe.eu
Tue Mar 31 14:48:42 UTC 2020


On Tuesday, 31 March 2020 at 11:38:40 UTC, Ola Fosheim Grøstad 
wrote:
> On Tuesday, 31 March 2020 at 07:26:38 UTC, Sebastiaan Koppe 
> wrote:
>> At first I tried reference counting, but found out there was 
>> significant bloat (I like to keep my web binaries small), 
>> eventually I settled for non-copyable objects so I get unique 
>> references, and release them on the JS side when the last and 
>> only reference goes out of scope.
>
> How do you do this? Do you do ref counting on the JS side?

No, I keep all the JS objects in a JS array so the GC won't free 
them. When the time comes I call a release function from D, which 
removes the object from the array.

In the off case the D code takes the hold of the same JS object 
twice (e.g. twice the same querySelector or similar), there would 
be 2 entries in the JS array for the same object, each having 
their own unique reference.

JS engines do objects in arrays pretty well. It works nicely 
combined with the unique reference semantics I have on the D 
side. And if you really need to you can wrap it in a refcount and 
get the best of both worlds.

> I see that there is a proposal for weak references for 
> javascript:
> https://v8.dev/features/weak-references
>
> I guess that could be useful.

That is pure JS though. There is a webassembly proposal to 
introduce anyref, whereby you can move js objects into wasm, and 
the js engine will track them. It might take a while before that 
is available.


More information about the Digitalmars-d mailing list