Advertisement
Guest User

Untitled

a guest
May 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <QuasAtWork> my polyfill for EMCAScript 6 Symbol() seems to be working :V
  2. <QuasAtWork> http://pastie.org/10210276
  3. <QuasAtWork> I mean other than the unavoidable fact that you can cast them to string
  4. <Altazimuth> How would that stop your polyfill from working?
  5. <QuasAtWork> which I need to abuse in this example to make sure the garbage collection is working properly on the dummy properties added to Object.prototype
  6. <QuasAtWork> see normally that would cause a memory leak
  7. <QuasAtWork> cuz every Symbol created would add a property to Object.prototype which wouldn't go away even if all Symbol instances with that unique ID were deleted
  8. <jmickle> i dont know whats going on here
  9. <QuasAtWork> but I am tricksy
  10. <Altazimuth> Ooooh memory leaks fun.
  11. <QuasAtWork> I have a native extension object which does ref counting
  12. <QuasAtWork> every time a Symbol() is GC'd it decs its ref to an object stored in a C++ map
  13. <QuasAtWork> if that ref count hits 0, it deletes the corresponding dummy property off of Object.prototype
  14. <QuasAtWork> and you can see that happen
  15. <QuasAtWork> I delete the symbol reference itself (b)
  16. <QuasAtWork> check the prop still exists, it does
  17. <QuasAtWork> force GC, it still exists, because there's a reference in the array returned by Object.getOwnPropertySymbols
  18. <QuasAtWork> so I delete the array too
  19. <QuasAtWork> check if it still exists; it does, because GC hasn't run
  20. <QuasAtWork> so I force GC again
  21. <jmickle> whats the use case for this, though?
  22. <QuasAtWork> and then you see the property no longer exists, and it throws
  23. <QuasAtWork> cuz it can't to toSource() on undefined :P
  24. <QuasAtWork> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
  25. <QuasAtWork> Symbols create private data in JavaScript more easily.
  26. <QuasAtWork> they are unique names and are automatically non-enumerable
  27. <QuasAtWork> they also, vis-a-vis "well known" symbols, allow certain new global APIs or "protocols" as JS likes to call them to exist
  28. <QuasAtWork> if your object has a Symbol.iterator property, then it automatically works with things that use the iterator protocol.
  29. <QuasAtWork> just as the chief example
  30. <QuasAtWork> see if they defined that just as "put it at 'Symbol.iterator'" with that being a normal string property, that would inevitably conflict with somebody somewhere's already existing JavaScript library and the world would blow up
  31. <QuasAtWork> but the actual object Symbol.iterator is something that *cannot* conflict with anybody's existing library bullshit.
  32. <QuasAtWork> I have to simulate it with UUIDs and use actual string props, because my interpreter doesn't support this natively
  33. <QuasAtWork> in a fully ECMA 6 compliant interpreter, a Symbol is a new 3rd kind of property and doesn't decay to a string at all
  34. <QuasAtWork> so that's what I meant about the polyfill not being FULLY compliant, because it simply can't be from a technical POV
  35. <QuasAtWork> but I have all the important stuff working :P
  36. <QuasAtWork> as long as one did not abuse Symbol.prototype.toString(), which any compliant code wouldn't, you'd see the same behavior
  37. <QuasAtWork> (again I abuse it in the example just to test that the GC of Object.prototype dummy props is working)
  38. <QuasAtWork> the dummy props have to exist so that if you use a Symbol to assign a property to an object, it becomes non-enumerable :V
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement