These are chat archives for opal/opal

11th
Apr 2017
Mitch VanDuyn
@catmando
Apr 11 2017 20:37
I am trying to build ObjectSpace.each_object. I am successfully enumerating all js objects, and sorting them out in opal objects
the only problem is for every class created I get what appears to be a dummy object
it has an $$id value
and a $$class value (key)
but no instance variables or anything else
Elia Schito
@elia
Apr 11 2017 20:38
@catmando are you using some JS api? (this is the curious question :smile: )
Mitch VanDuyn
@catmando
Apr 11 2017 20:38
is there some general way to filter out these "dummy guys"
Elia Schito
@elia
Apr 11 2017 20:39
wrt dummy obj those two are the only properties you've found on them?
Mitch VanDuyn
@catmando
Apr 11 2017 20:39
no I am following this SO
here is code so far:
module ObjectSpace
  def self.each_object(klass)
    objs = []
    matching_objs = []
    %x{
      var walk_the_object = function(js_obj) {
        var keys = Object.keys(js_obj) //get all own property names of the object

        keys.forEach( function ( key ) {
          var value = js_obj[ key ]; // get property value

          // if the property value is an object...
          if ( value && typeof value === 'object'  ) {

              // if we don't have this reference, and its an object of the class we want
              if ( #{objs}.indexOf( value ) < 0 ) {
                  #{objs}.push( value ); // store the reference
                  if ( Object.keys(value).indexOf('$$class') >= 0 && value.$$class == #{klass} ) {
                    #{matching_objs}.push ( value )
                  }
                  walk_the_object(value) // traverse all its own properties
              }
          }
        })
      }
      walk_the_object(window)
    }
    matching_objs
  end
end
Elia Schito
@elia
Apr 11 2017 20:41
interesting, the only thing left out would be objects trapped in closure vars right?
Mitch VanDuyn
@catmando
Apr 11 2017 20:41
I guess so... and I guess that is a bit of problem :-(
Elia Schito
@elia
Apr 11 2017 20:42
don't tell me, yesterday I was struggling to find a way to have weak refs in javascript (and it's impossible)
Mitch VanDuyn
@catmando
Apr 11 2017 20:48
anyway @elia do you understand this odd dummy instances?
they seem to be created when you create the class
but without invoking the initializer
Elia Schito
@elia
Apr 11 2017 20:49
@catmando those two are the only properties you've found on them?
Also I have a dejavu… like they're related to scope and get those props by error and are not real objects
Mitch VanDuyn
@catmando
Apr 11 2017 20:50
n
class:aclass : a id : 193364
constructor : $Foo()
proto: t
n
$$class : a
$$id : 193364
constructor : $Foo()
__proto__: t
Elia Schito
@elia
Apr 11 2017 20:52
obj.$$class.$$name ?
another way would be to walk up the __proto__ to see if you find anything familiar
Mitch VanDuyn
@catmando
Apr 11 2017 20:54
"Foo"
its basically an unitialized copy of Foo
Elia Schito
@elia
Apr 11 2017 20:55
looks more like an instance of Foo
or more likely its $$alloc property prototype
i.e. the JS constructor that's stored inside the $$alloc property of any class
klass.$new is basically obj = new klass.$$alloc; obj.$initialize(); return obj;
Mitch VanDuyn
@catmando
Apr 11 2017 21:01
right sorry, I meant instance
so its like an klass.$$alloc was called, without calling initialize.
its also displayed as "n" in the JS console
Elia Schito
@elia
Apr 11 2017 21:02
that could be due to minification
Mitch VanDuyn
@catmando
Apr 11 2017 21:02
where as a real Foo instance is displayed as "Foo"
nope no minification... I am compiling these on the fly with a new "playground" type console I am building.
Elia Schito
@elia
Apr 11 2017 21:03
not even of the corelib?
Mitch VanDuyn
@catmando
Apr 11 2017 21:03
sure
okay understand
Mitch VanDuyn
@catmando
Apr 11 2017 21:09
okay its the value of $$proto of the class....
in otherwords each class has a $$proto key which points to this "dummy" instance.
I can deal with that
Elia Schito
@elia
Apr 11 2017 21:10
👍🏼 that is the same of $$alloc.prototype
it's the JS prototype of its instances